xref: /vim-8.2.3635/src/normal.c (revision 899dddf8)
1 /* vi:set ts=8 sts=4 sw=4:
2  *
3  * VIM - Vi IMproved	by Bram Moolenaar
4  *
5  * Do ":help uganda"  in Vim to read copying and usage conditions.
6  * Do ":help credits" in Vim to see a list of people who contributed.
7  * See README.txt for an overview of the Vim source code.
8  */
9 /*
10  * normal.c:	Contains the main routine for processing characters in command
11  *		mode.  Communicates closely with the code in ops.c to handle
12  *		the operators.
13  */
14 
15 #include "vim.h"
16 
17 #ifdef FEAT_VISUAL
18 /*
19  * The Visual area is remembered for reselection.
20  */
21 static int	resel_VIsual_mode = NUL;	/* 'v', 'V', or Ctrl-V */
22 static linenr_T	resel_VIsual_line_count;	/* number of lines */
23 static colnr_T	resel_VIsual_col;		/* nr of cols or end col */
24 
25 static int	restart_VIsual_select = 0;
26 #endif
27 
28 static int
29 # ifdef __BORLANDC__
30 _RTLENTRYF
31 # endif
32 		nv_compare __ARGS((const void *s1, const void *s2));
33 static int	find_command __ARGS((int cmdchar));
34 static void	op_colon __ARGS((oparg_T *oap));
35 static void	op_function __ARGS((oparg_T *oap));
36 #if defined(FEAT_MOUSE) && defined(FEAT_VISUAL)
37 static void	find_start_of_word __ARGS((pos_T *));
38 static void	find_end_of_word __ARGS((pos_T *));
39 static int	get_mouse_class __ARGS((char_u *p));
40 #endif
41 static void	prep_redo_cmd __ARGS((cmdarg_T *cap));
42 static void	prep_redo __ARGS((int regname, long, int, int, int, int, int));
43 static int	checkclearop __ARGS((oparg_T *oap));
44 static int	checkclearopq __ARGS((oparg_T *oap));
45 static void	clearop __ARGS((oparg_T *oap));
46 static void	clearopbeep __ARGS((oparg_T *oap));
47 #ifdef FEAT_VISUAL
48 static void	unshift_special __ARGS((cmdarg_T *cap));
49 #endif
50 #ifdef FEAT_CMDL_INFO
51 static void	del_from_showcmd __ARGS((int));
52 #endif
53 
54 /*
55  * nv_*(): functions called to handle Normal and Visual mode commands.
56  * n_*(): functions called to handle Normal mode commands.
57  * v_*(): functions called to handle Visual mode commands.
58  */
59 static void	nv_ignore __ARGS((cmdarg_T *cap));
60 static void	nv_nop __ARGS((cmdarg_T *cap));
61 static void	nv_error __ARGS((cmdarg_T *cap));
62 static void	nv_help __ARGS((cmdarg_T *cap));
63 static void	nv_addsub __ARGS((cmdarg_T *cap));
64 static void	nv_page __ARGS((cmdarg_T *cap));
65 static void	nv_gd __ARGS((oparg_T *oap, int nchar, int thisblock));
66 static int	nv_screengo __ARGS((oparg_T *oap, int dir, long dist));
67 #ifdef FEAT_MOUSE
68 static void	nv_mousescroll __ARGS((cmdarg_T *cap));
69 static void	nv_mouse __ARGS((cmdarg_T *cap));
70 #endif
71 static void	nv_scroll_line __ARGS((cmdarg_T *cap));
72 static void	nv_zet __ARGS((cmdarg_T *cap));
73 #ifdef FEAT_GUI
74 static void	nv_ver_scrollbar __ARGS((cmdarg_T *cap));
75 static void	nv_hor_scrollbar __ARGS((cmdarg_T *cap));
76 #endif
77 #ifdef FEAT_GUI_TABLINE
78 static void	nv_tabline __ARGS((cmdarg_T *cap));
79 static void	nv_tabmenu __ARGS((cmdarg_T *cap));
80 #endif
81 static void	nv_exmode __ARGS((cmdarg_T *cap));
82 static void	nv_colon __ARGS((cmdarg_T *cap));
83 static void	nv_ctrlg __ARGS((cmdarg_T *cap));
84 static void	nv_ctrlh __ARGS((cmdarg_T *cap));
85 static void	nv_clear __ARGS((cmdarg_T *cap));
86 static void	nv_ctrlo __ARGS((cmdarg_T *cap));
87 static void	nv_hat __ARGS((cmdarg_T *cap));
88 static void	nv_Zet __ARGS((cmdarg_T *cap));
89 static void	nv_ident __ARGS((cmdarg_T *cap));
90 static void	nv_tagpop __ARGS((cmdarg_T *cap));
91 static void	nv_scroll __ARGS((cmdarg_T *cap));
92 static void	nv_right __ARGS((cmdarg_T *cap));
93 static void	nv_left __ARGS((cmdarg_T *cap));
94 static void	nv_up __ARGS((cmdarg_T *cap));
95 static void	nv_down __ARGS((cmdarg_T *cap));
96 #ifdef FEAT_SEARCHPATH
97 static void	nv_gotofile __ARGS((cmdarg_T *cap));
98 #endif
99 static void	nv_end __ARGS((cmdarg_T *cap));
100 static void	nv_dollar __ARGS((cmdarg_T *cap));
101 static void	nv_search __ARGS((cmdarg_T *cap));
102 static void	nv_next __ARGS((cmdarg_T *cap));
103 static void	normal_search __ARGS((cmdarg_T *cap, int dir, char_u *pat, int opt));
104 static void	nv_csearch __ARGS((cmdarg_T *cap));
105 static void	nv_brackets __ARGS((cmdarg_T *cap));
106 static void	nv_percent __ARGS((cmdarg_T *cap));
107 static void	nv_brace __ARGS((cmdarg_T *cap));
108 static void	nv_mark __ARGS((cmdarg_T *cap));
109 static void	nv_findpar __ARGS((cmdarg_T *cap));
110 static void	nv_undo __ARGS((cmdarg_T *cap));
111 static void	nv_kundo __ARGS((cmdarg_T *cap));
112 static void	nv_Replace __ARGS((cmdarg_T *cap));
113 #ifdef FEAT_VREPLACE
114 static void	nv_vreplace __ARGS((cmdarg_T *cap));
115 #endif
116 #ifdef FEAT_VISUAL
117 static void	v_swap_corners __ARGS((int cmdchar));
118 #endif
119 static void	nv_replace __ARGS((cmdarg_T *cap));
120 static void	n_swapchar __ARGS((cmdarg_T *cap));
121 static void	nv_cursormark __ARGS((cmdarg_T *cap, int flag, pos_T *pos));
122 #ifdef FEAT_VISUAL
123 static void	v_visop __ARGS((cmdarg_T *cap));
124 #endif
125 static void	nv_subst __ARGS((cmdarg_T *cap));
126 static void	nv_abbrev __ARGS((cmdarg_T *cap));
127 static void	nv_optrans __ARGS((cmdarg_T *cap));
128 static void	nv_gomark __ARGS((cmdarg_T *cap));
129 static void	nv_pcmark __ARGS((cmdarg_T *cap));
130 static void	nv_regname __ARGS((cmdarg_T *cap));
131 #ifdef FEAT_VISUAL
132 static void	nv_visual __ARGS((cmdarg_T *cap));
133 static void	n_start_visual_mode __ARGS((int c));
134 #endif
135 static void	nv_window __ARGS((cmdarg_T *cap));
136 static void	nv_suspend __ARGS((cmdarg_T *cap));
137 static void	nv_g_cmd __ARGS((cmdarg_T *cap));
138 static void	n_opencmd __ARGS((cmdarg_T *cap));
139 static void	nv_dot __ARGS((cmdarg_T *cap));
140 static void	nv_redo __ARGS((cmdarg_T *cap));
141 static void	nv_Undo __ARGS((cmdarg_T *cap));
142 static void	nv_tilde __ARGS((cmdarg_T *cap));
143 static void	nv_operator __ARGS((cmdarg_T *cap));
144 static void	nv_lineop __ARGS((cmdarg_T *cap));
145 static void	nv_home __ARGS((cmdarg_T *cap));
146 static void	nv_pipe __ARGS((cmdarg_T *cap));
147 static void	nv_bck_word __ARGS((cmdarg_T *cap));
148 static void	nv_wordcmd __ARGS((cmdarg_T *cap));
149 static void	nv_beginline __ARGS((cmdarg_T *cap));
150 #ifdef FEAT_VISUAL
151 static void	adjust_for_sel __ARGS((cmdarg_T *cap));
152 static int	unadjust_for_sel __ARGS((void));
153 static void	nv_select __ARGS((cmdarg_T *cap));
154 #endif
155 static void	nv_goto __ARGS((cmdarg_T *cap));
156 static void	nv_normal __ARGS((cmdarg_T *cap));
157 static void	nv_esc __ARGS((cmdarg_T *oap));
158 static void	nv_edit __ARGS((cmdarg_T *cap));
159 static void	invoke_edit __ARGS((cmdarg_T *cap, int repl, int cmd, int startln));
160 #ifdef FEAT_TEXTOBJ
161 static void	nv_object __ARGS((cmdarg_T *cap));
162 #endif
163 static void	nv_record __ARGS((cmdarg_T *cap));
164 static void	nv_at __ARGS((cmdarg_T *cap));
165 static void	nv_halfpage __ARGS((cmdarg_T *cap));
166 static void	nv_join __ARGS((cmdarg_T *cap));
167 static void	nv_put __ARGS((cmdarg_T *cap));
168 static void	nv_open __ARGS((cmdarg_T *cap));
169 #ifdef FEAT_SNIFF
170 static void	nv_sniff __ARGS((cmdarg_T *cap));
171 #endif
172 #ifdef FEAT_NETBEANS_INTG
173 static void	nv_nbcmd __ARGS((cmdarg_T *cap));
174 #endif
175 #ifdef FEAT_DND
176 static void	nv_drop __ARGS((cmdarg_T *cap));
177 #endif
178 #ifdef FEAT_AUTOCMD
179 static void	nv_cursorhold __ARGS((cmdarg_T *cap));
180 #endif
181 
182 /*
183  * Function to be called for a Normal or Visual mode command.
184  * The argument is a cmdarg_T.
185  */
186 typedef void (*nv_func_T) __ARGS((cmdarg_T *cap));
187 
188 /* Values for cmd_flags. */
189 #define NV_NCH	    0x01	  /* may need to get a second char */
190 #define NV_NCH_NOP  (0x02|NV_NCH) /* get second char when no operator pending */
191 #define NV_NCH_ALW  (0x04|NV_NCH) /* always get a second char */
192 #define NV_LANG	    0x08	/* second char needs language adjustment */
193 
194 #define NV_SS	    0x10	/* may start selection */
195 #define NV_SSS	    0x20	/* may start selection with shift modifier */
196 #define NV_STS	    0x40	/* may stop selection without shift modif. */
197 #define NV_RL	    0x80	/* 'rightleft' modifies command */
198 #define NV_KEEPREG  0x100	/* don't clear regname */
199 #define NV_NCW	    0x200	/* not allowed in command-line window */
200 
201 /*
202  * Generally speaking, every Normal mode command should either clear any
203  * pending operator (with *clearop*()), or set the motion type variable
204  * oap->motion_type.
205  *
206  * When a cursor motion command is made, it is marked as being a character or
207  * line oriented motion.  Then, if an operator is in effect, the operation
208  * becomes character or line oriented accordingly.
209  */
210 
211 /*
212  * This table contains one entry for every Normal or Visual mode command.
213  * The order doesn't matter, init_normal_cmds() will create a sorted index.
214  * It is faster when all keys from zero to '~' are present.
215  */
216 static const struct nv_cmd
217 {
218     int		cmd_char;	/* (first) command character */
219     nv_func_T   cmd_func;	/* function for this command */
220     short_u	cmd_flags;	/* NV_ flags */
221     short	cmd_arg;	/* value for ca.arg */
222 } nv_cmds[] =
223 {
224     {NUL,	nv_error,	0,			0},
225     {Ctrl_A,	nv_addsub,	0,			0},
226     {Ctrl_B,	nv_page,	NV_STS,			BACKWARD},
227     {Ctrl_C,	nv_esc,		0,			TRUE},
228     {Ctrl_D,	nv_halfpage,	0,			0},
229     {Ctrl_E,	nv_scroll_line,	0,			TRUE},
230     {Ctrl_F,	nv_page,	NV_STS,			FORWARD},
231     {Ctrl_G,	nv_ctrlg,	0,			0},
232     {Ctrl_H,	nv_ctrlh,	0,			0},
233     {Ctrl_I,	nv_pcmark,	0,			0},
234     {NL,	nv_down,	0,			FALSE},
235     {Ctrl_K,	nv_error,	0,			0},
236     {Ctrl_L,	nv_clear,	0,			0},
237     {Ctrl_M,	nv_down,	0,			TRUE},
238     {Ctrl_N,	nv_down,	NV_STS,			FALSE},
239     {Ctrl_O,	nv_ctrlo,	0,			0},
240     {Ctrl_P,	nv_up,		NV_STS,			FALSE},
241 #ifdef FEAT_VISUAL
242     {Ctrl_Q,	nv_visual,	0,			FALSE},
243 #else
244     {Ctrl_Q,	nv_ignore,	0,			0},
245 #endif
246     {Ctrl_R,	nv_redo,	0,			0},
247     {Ctrl_S,	nv_ignore,	0,			0},
248     {Ctrl_T,	nv_tagpop,	NV_NCW,			0},
249     {Ctrl_U,	nv_halfpage,	0,			0},
250 #ifdef FEAT_VISUAL
251     {Ctrl_V,	nv_visual,	0,			FALSE},
252     {'V',	nv_visual,	0,			FALSE},
253     {'v',	nv_visual,	0,			FALSE},
254 #else
255     {Ctrl_V,	nv_error,	0,			0},
256     {'V',	nv_error,	0,			0},
257     {'v',	nv_error,	0,			0},
258 #endif
259     {Ctrl_W,	nv_window,	0,			0},
260     {Ctrl_X,	nv_addsub,	0,			0},
261     {Ctrl_Y,	nv_scroll_line,	0,			FALSE},
262     {Ctrl_Z,	nv_suspend,	0,			0},
263     {ESC,	nv_esc,		0,			FALSE},
264     {Ctrl_BSL,	nv_normal,	NV_NCH_ALW,		0},
265     {Ctrl_RSB,	nv_ident,	NV_NCW,			0},
266     {Ctrl_HAT,	nv_hat,		NV_NCW,			0},
267     {Ctrl__,	nv_error,	0,			0},
268     {' ',	nv_right,	0,			0},
269     {'!',	nv_operator,	0,			0},
270     {'"',	nv_regname,	NV_NCH_NOP|NV_KEEPREG,	0},
271     {'#',	nv_ident,	0,			0},
272     {'$',	nv_dollar,	0,			0},
273     {'%',	nv_percent,	0,			0},
274     {'&',	nv_optrans,	0,			0},
275     {'\'',	nv_gomark,	NV_NCH_ALW,		TRUE},
276     {'(',	nv_brace,	0,			BACKWARD},
277     {')',	nv_brace,	0,			FORWARD},
278     {'*',	nv_ident,	0,			0},
279     {'+',	nv_down,	0,			TRUE},
280     {',',	nv_csearch,	0,			TRUE},
281     {'-',	nv_up,		0,			TRUE},
282     {'.',	nv_dot,		NV_KEEPREG,		0},
283     {'/',	nv_search,	0,			FALSE},
284     {'0',	nv_beginline,	0,			0},
285     {'1',	nv_ignore,	0,			0},
286     {'2',	nv_ignore,	0,			0},
287     {'3',	nv_ignore,	0,			0},
288     {'4',	nv_ignore,	0,			0},
289     {'5',	nv_ignore,	0,			0},
290     {'6',	nv_ignore,	0,			0},
291     {'7',	nv_ignore,	0,			0},
292     {'8',	nv_ignore,	0,			0},
293     {'9',	nv_ignore,	0,			0},
294     {':',	nv_colon,	0,			0},
295     {';',	nv_csearch,	0,			FALSE},
296     {'<',	nv_operator,	NV_RL,			0},
297     {'=',	nv_operator,	0,			0},
298     {'>',	nv_operator,	NV_RL,			0},
299     {'?',	nv_search,	0,			FALSE},
300     {'@',	nv_at,		NV_NCH_NOP,		FALSE},
301     {'A',	nv_edit,	0,			0},
302     {'B',	nv_bck_word,	0,			1},
303     {'C',	nv_abbrev,	NV_KEEPREG,		0},
304     {'D',	nv_abbrev,	NV_KEEPREG,		0},
305     {'E',	nv_wordcmd,	0,			TRUE},
306     {'F',	nv_csearch,	NV_NCH_ALW|NV_LANG,	BACKWARD},
307     {'G',	nv_goto,	0,			TRUE},
308     {'H',	nv_scroll,	0,			0},
309     {'I',	nv_edit,	0,			0},
310     {'J',	nv_join,	0,			0},
311     {'K',	nv_ident,	0,			0},
312     {'L',	nv_scroll,	0,			0},
313     {'M',	nv_scroll,	0,			0},
314     {'N',	nv_next,	0,			SEARCH_REV},
315     {'O',	nv_open,	0,			0},
316     {'P',	nv_put,		0,			0},
317     {'Q',	nv_exmode,	NV_NCW,			0},
318     {'R',	nv_Replace,	0,			FALSE},
319     {'S',	nv_subst,	NV_KEEPREG,		0},
320     {'T',	nv_csearch,	NV_NCH_ALW|NV_LANG,	BACKWARD},
321     {'U',	nv_Undo,	0,			0},
322     {'W',	nv_wordcmd,	0,			TRUE},
323     {'X',	nv_abbrev,	NV_KEEPREG,		0},
324     {'Y',	nv_abbrev,	NV_KEEPREG,		0},
325     {'Z',	nv_Zet,		NV_NCH_NOP|NV_NCW,	0},
326     {'[',	nv_brackets,	NV_NCH_ALW,		BACKWARD},
327     {'\\',	nv_error,	0,			0},
328     {']',	nv_brackets,	NV_NCH_ALW,		FORWARD},
329     {'^',	nv_beginline,	0,			BL_WHITE | BL_FIX},
330     {'_',	nv_lineop,	0,			0},
331     {'`',	nv_gomark,	NV_NCH_ALW,		FALSE},
332     {'a',	nv_edit,	NV_NCH,			0},
333     {'b',	nv_bck_word,	0,			0},
334     {'c',	nv_operator,	0,			0},
335     {'d',	nv_operator,	0,			0},
336     {'e',	nv_wordcmd,	0,			FALSE},
337     {'f',	nv_csearch,	NV_NCH_ALW|NV_LANG,	FORWARD},
338     {'g',	nv_g_cmd,	NV_NCH_ALW,		FALSE},
339     {'h',	nv_left,	NV_RL,			0},
340     {'i',	nv_edit,	NV_NCH,			0},
341     {'j',	nv_down,	0,			FALSE},
342     {'k',	nv_up,		0,			FALSE},
343     {'l',	nv_right,	NV_RL,			0},
344     {'m',	nv_mark,	NV_NCH_NOP,		0},
345     {'n',	nv_next,	0,			0},
346     {'o',	nv_open,	0,			0},
347     {'p',	nv_put,		0,			0},
348     {'q',	nv_record,	NV_NCH,			0},
349     {'r',	nv_replace,	NV_NCH_NOP|NV_LANG,	0},
350     {'s',	nv_subst,	NV_KEEPREG,		0},
351     {'t',	nv_csearch,	NV_NCH_ALW|NV_LANG,	FORWARD},
352     {'u',	nv_undo,	0,			0},
353     {'w',	nv_wordcmd,	0,			FALSE},
354     {'x',	nv_abbrev,	NV_KEEPREG,		0},
355     {'y',	nv_operator,	0,			0},
356     {'z',	nv_zet,		NV_NCH_ALW,		0},
357     {'{',	nv_findpar,	0,			BACKWARD},
358     {'|',	nv_pipe,	0,			0},
359     {'}',	nv_findpar,	0,			FORWARD},
360     {'~',	nv_tilde,	0,			0},
361 
362     /* pound sign */
363     {POUND,	nv_ident,	0,			0},
364 #ifdef FEAT_MOUSE
365     {K_MOUSEUP, nv_mousescroll,	0,			TRUE},
366     {K_MOUSEDOWN, nv_mousescroll, 0,			FALSE},
367     {K_LEFTMOUSE, nv_mouse,	0,			0},
368     {K_LEFTMOUSE_NM, nv_mouse,	0,			0},
369     {K_LEFTDRAG, nv_mouse,	0,			0},
370     {K_LEFTRELEASE, nv_mouse,	0,			0},
371     {K_LEFTRELEASE_NM, nv_mouse, 0,			0},
372     {K_MIDDLEMOUSE, nv_mouse,	0,			0},
373     {K_MIDDLEDRAG, nv_mouse,	0,			0},
374     {K_MIDDLERELEASE, nv_mouse,	0,			0},
375     {K_RIGHTMOUSE, nv_mouse,	0,			0},
376     {K_RIGHTDRAG, nv_mouse,	0,			0},
377     {K_RIGHTRELEASE, nv_mouse,	0,			0},
378     {K_X1MOUSE, nv_mouse,	0,			0},
379     {K_X1DRAG, nv_mouse,	0,			0},
380     {K_X1RELEASE, nv_mouse,	0,			0},
381     {K_X2MOUSE, nv_mouse,	0,			0},
382     {K_X2DRAG, nv_mouse,	0,			0},
383     {K_X2RELEASE, nv_mouse,	0,			0},
384 #endif
385     {K_IGNORE,	nv_ignore,	0,			0},
386     {K_NOP,	nv_nop,		0,			0},
387     {K_INS,	nv_edit,	0,			0},
388     {K_KINS,	nv_edit,	0,			0},
389     {K_BS,	nv_ctrlh,	0,			0},
390     {K_UP,	nv_up,		NV_SSS|NV_STS,		FALSE},
391     {K_S_UP,	nv_page,	NV_SS,			BACKWARD},
392     {K_DOWN,	nv_down,	NV_SSS|NV_STS,		FALSE},
393     {K_S_DOWN,	nv_page,	NV_SS,			FORWARD},
394     {K_LEFT,	nv_left,	NV_SSS|NV_STS|NV_RL,	0},
395     {K_S_LEFT,	nv_bck_word,	NV_SS|NV_RL,		0},
396     {K_C_LEFT,	nv_bck_word,	NV_SSS|NV_RL|NV_STS,	1},
397     {K_RIGHT,	nv_right,	NV_SSS|NV_STS|NV_RL,	0},
398     {K_S_RIGHT,	nv_wordcmd,	NV_SS|NV_RL,		FALSE},
399     {K_C_RIGHT,	nv_wordcmd,	NV_SSS|NV_RL|NV_STS,	TRUE},
400     {K_PAGEUP,	nv_page,	NV_SSS|NV_STS,		BACKWARD},
401     {K_KPAGEUP,	nv_page,	NV_SSS|NV_STS,		BACKWARD},
402     {K_PAGEDOWN, nv_page,	NV_SSS|NV_STS,		FORWARD},
403     {K_KPAGEDOWN, nv_page,	NV_SSS|NV_STS,		FORWARD},
404     {K_END,	nv_end,		NV_SSS|NV_STS,		FALSE},
405     {K_KEND,	nv_end,		NV_SSS|NV_STS,		FALSE},
406     {K_S_END,	nv_end,		NV_SS,			FALSE},
407     {K_C_END,	nv_end,		NV_SSS|NV_STS,		TRUE},
408     {K_HOME,	nv_home,	NV_SSS|NV_STS,		0},
409     {K_KHOME,	nv_home,	NV_SSS|NV_STS,		0},
410     {K_S_HOME,	nv_home,	NV_SS,			0},
411     {K_C_HOME,	nv_goto,	NV_SSS|NV_STS,		FALSE},
412     {K_DEL,	nv_abbrev,	0,			0},
413     {K_KDEL,	nv_abbrev,	0,			0},
414     {K_UNDO,	nv_kundo,	0,			0},
415     {K_HELP,	nv_help,	NV_NCW,			0},
416     {K_F1,	nv_help,	NV_NCW,			0},
417     {K_XF1,	nv_help,	NV_NCW,			0},
418 #ifdef FEAT_VISUAL
419     {K_SELECT,	nv_select,	0,			0},
420 #endif
421 #ifdef FEAT_GUI
422     {K_VER_SCROLLBAR, nv_ver_scrollbar, 0,		0},
423     {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0,		0},
424 #endif
425 #ifdef FEAT_GUI_TABLINE
426     {K_TABLINE, nv_tabline,	0,			0},
427     {K_TABMENU, nv_tabmenu,	0,			0},
428 #endif
429 #ifdef FEAT_FKMAP
430     {K_F8,	farsi_fkey,	0,			0},
431     {K_F9,	farsi_fkey,	0,			0},
432 #endif
433 #ifdef FEAT_SNIFF
434     {K_SNIFF,	nv_sniff,	0,			0},
435 #endif
436 #ifdef FEAT_NETBEANS_INTG
437     {K_F21,	nv_nbcmd,	NV_NCH_ALW,		0},
438 #endif
439 #ifdef FEAT_DND
440     {K_DROP,	nv_drop,	NV_STS,			0},
441 #endif
442 #ifdef FEAT_AUTOCMD
443     {K_CURSORHOLD, nv_cursorhold, 0,			0},
444 #endif
445 };
446 
447 /* Number of commands in nv_cmds[]. */
448 #define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
449 
450 /* Sorted index of commands in nv_cmds[]. */
451 static short nv_cmd_idx[NV_CMDS_SIZE];
452 
453 /* The highest index for which
454  * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
455 static int nv_max_linear;
456 
457 /*
458  * Compare functions for qsort() below, that checks the command character
459  * through the index in nv_cmd_idx[].
460  */
461     static int
462 #ifdef __BORLANDC__
463 _RTLENTRYF
464 #endif
465 nv_compare(s1, s2)
466     const void	*s1;
467     const void	*s2;
468 {
469     int		c1, c2;
470 
471     /* The commands are sorted on absolute value. */
472     c1 = nv_cmds[*(const short *)s1].cmd_char;
473     c2 = nv_cmds[*(const short *)s2].cmd_char;
474     if (c1 < 0)
475 	c1 = -c1;
476     if (c2 < 0)
477 	c2 = -c2;
478     return c1 - c2;
479 }
480 
481 /*
482  * Initialize the nv_cmd_idx[] table.
483  */
484     void
485 init_normal_cmds()
486 {
487     int		i;
488 
489     /* Fill the index table with a one to one relation. */
490     for (i = 0; i < NV_CMDS_SIZE; ++i)
491 	nv_cmd_idx[i] = i;
492 
493     /* Sort the commands by the command character.  */
494     qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
495 
496     /* Find the first entry that can't be indexed by the command character. */
497     for (i = 0; i < NV_CMDS_SIZE; ++i)
498 	if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
499 	    break;
500     nv_max_linear = i - 1;
501 }
502 
503 /*
504  * Search for a command in the commands table.
505  * Returns -1 for invalid command.
506  */
507     static int
508 find_command(cmdchar)
509     int		cmdchar;
510 {
511     int		i;
512     int		idx;
513     int		top, bot;
514     int		c;
515 
516 #ifdef FEAT_MBYTE
517     /* A multi-byte character is never a command. */
518     if (cmdchar >= 0x100)
519 	return -1;
520 #endif
521 
522     /* We use the absolute value of the character.  Special keys have a
523      * negative value, but are sorted on their absolute value. */
524     if (cmdchar < 0)
525 	cmdchar = -cmdchar;
526 
527     /* If the character is in the first part: The character is the index into
528      * nv_cmd_idx[]. */
529     if (cmdchar <= nv_max_linear)
530 	return nv_cmd_idx[cmdchar];
531 
532     /* Perform a binary search. */
533     bot = nv_max_linear + 1;
534     top = NV_CMDS_SIZE - 1;
535     idx = -1;
536     while (bot <= top)
537     {
538 	i = (top + bot) / 2;
539 	c = nv_cmds[nv_cmd_idx[i]].cmd_char;
540 	if (c < 0)
541 	    c = -c;
542 	if (cmdchar == c)
543 	{
544 	    idx = nv_cmd_idx[i];
545 	    break;
546 	}
547 	if (cmdchar > c)
548 	    bot = i + 1;
549 	else
550 	    top = i - 1;
551     }
552     return idx;
553 }
554 
555 /*
556  * Execute a command in Normal mode.
557  */
558 /*ARGSUSED*/
559     void
560 normal_cmd(oap, toplevel)
561     oparg_T	*oap;
562     int		toplevel;		/* TRUE when called from main() */
563 {
564     static long	opcount = 0;		/* ca.opcount saved here */
565     cmdarg_T	ca;			/* command arguments */
566     int		c;
567     int		ctrl_w = FALSE;		/* got CTRL-W command */
568     int		old_col = curwin->w_curswant;
569 #ifdef FEAT_CMDL_INFO
570     int		need_flushbuf;		/* need to call out_flush() */
571 #endif
572 #ifdef FEAT_VISUAL
573     pos_T	old_pos;		/* cursor position before command */
574     int		mapped_len;
575 #endif
576     static int	old_mapped_len = 0;
577     int		idx;
578 
579     vim_memset(&ca, 0, sizeof(ca));	/* also resets ca.retval */
580     ca.oap = oap;
581     ca.opcount = opcount;
582 
583 #ifdef FEAT_SNIFF
584     want_sniff_request = sniff_connected;
585 #endif
586 
587     /*
588      * If there is an operator pending, then the command we take this time
589      * will terminate it. Finish_op tells us to finish the operation before
590      * returning this time (unless the operation was cancelled).
591      */
592 #ifdef CURSOR_SHAPE
593     c = finish_op;
594 #endif
595     finish_op = (oap->op_type != OP_NOP);
596 #ifdef CURSOR_SHAPE
597     if (finish_op != c)
598     {
599 	ui_cursor_shape();		/* may show different cursor shape */
600 # ifdef FEAT_MOUSESHAPE
601 	update_mouseshape(-1);
602 # endif
603     }
604 #endif
605 
606     if (!finish_op && !oap->regname)
607 	ca.opcount = 0;
608 
609 #ifdef FEAT_VISUAL
610     mapped_len = typebuf_maplen();
611 #endif
612 
613     State = NORMAL_BUSY;
614 #ifdef USE_ON_FLY_SCROLL
615     dont_scroll = FALSE;	/* allow scrolling here */
616 #endif
617 
618     /*
619      * Get the command character from the user.
620      */
621     c = safe_vgetc();
622 
623 #ifdef FEAT_LANGMAP
624     LANGMAP_ADJUST(c, TRUE);
625 #endif
626 
627     /*
628      * If a mapping was started in Visual or Select mode, remember the length
629      * of the mapping.  This is used below to not return to Insert mode for as
630      * long as the mapping is being executed.
631      */
632     if (restart_edit == 0)
633 	old_mapped_len = 0;
634     else if (old_mapped_len
635 #ifdef FEAT_VISUAL
636 	    || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0)
637 #endif
638 	    )
639 	old_mapped_len = typebuf_maplen();
640 
641     if (c == NUL)
642 	c = K_ZERO;
643 
644 #ifdef FEAT_VISUAL
645     /*
646      * In Select mode, typed text replaces the selection.
647      */
648     if (VIsual_active
649 	    && VIsual_select
650 	    && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
651     {
652 # ifdef FEAT_MBYTE
653 	char_u	    buf[MB_MAXBYTES + 1];
654 
655 	buf[(*mb_char2bytes)(c, buf)] = NUL;
656 # else
657 	char_u	    buf[2];
658 
659 	buf[0] = c;
660 	buf[1] = NUL;
661 # endif
662 	/* Fake a "c"hange command.  When "restart_edit" is set (e.g., because
663 	 * 'insertmode' is set) fake a "d"elete command, Insert mode will
664 	 * restart automatically.
665 	 * Insert the typed character in the typeahead buffer, so that it will
666 	 * be mapped in Insert mode.  Required for ":lmap" to work.  May cause
667 	 * mapping a character from ":vnoremap"... */
668 	(void)ins_typebuf(buf, REMAP_YES, 0, !KeyTyped, FALSE);
669 	if (restart_edit != 0)
670 	    c = 'd';
671 	else
672 	    c = 'c';
673 	msg_nowait = TRUE;	/* don't delay going to insert mode */
674     }
675 #endif
676 
677 #ifdef FEAT_CMDL_INFO
678     need_flushbuf = add_to_showcmd(c);
679 #endif
680 
681 getcount:
682 #ifdef FEAT_VISUAL
683     if (!(VIsual_active && VIsual_select))
684 #endif
685     {
686 	/*
687 	 * Handle a count before a command and compute ca.count0.
688 	 * Note that '0' is a command and not the start of a count, but it's
689 	 * part of a count after other digits.
690 	 */
691 	while (    (c >= '1' && c <= '9')
692 		|| (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
693 	{
694 	    if (c == K_DEL || c == K_KDEL)
695 	    {
696 		ca.count0 /= 10;
697 #ifdef FEAT_CMDL_INFO
698 		del_from_showcmd(4);	/* delete the digit and ~@% */
699 #endif
700 	    }
701 	    else
702 		ca.count0 = ca.count0 * 10 + (c - '0');
703 	    if (ca.count0 < 0)	    /* got too large! */
704 		ca.count0 = 999999999L;
705 	    if (ctrl_w)
706 	    {
707 		++no_mapping;
708 		++allow_keys;		/* no mapping for nchar, but keys */
709 	    }
710 	    ++no_zero_mapping;		/* don't map zero here */
711 	    c = safe_vgetc();
712 #ifdef FEAT_LANGMAP
713 	    LANGMAP_ADJUST(c, TRUE);
714 #endif
715 	    --no_zero_mapping;
716 	    if (ctrl_w)
717 	    {
718 		--no_mapping;
719 		--allow_keys;
720 	    }
721 #ifdef FEAT_CMDL_INFO
722 	    need_flushbuf |= add_to_showcmd(c);
723 #endif
724 	}
725 
726 	/*
727 	 * If we got CTRL-W there may be a/another count
728 	 */
729 	if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
730 	{
731 	    ctrl_w = TRUE;
732 	    ca.opcount = ca.count0;	/* remember first count */
733 	    ca.count0 = 0;
734 	    ++no_mapping;
735 	    ++allow_keys;		/* no mapping for nchar, but keys */
736 	    c = safe_vgetc();		/* get next character */
737 #ifdef FEAT_LANGMAP
738 	    LANGMAP_ADJUST(c, TRUE);
739 #endif
740 	    --no_mapping;
741 	    --allow_keys;
742 #ifdef FEAT_CMDL_INFO
743 	    need_flushbuf |= add_to_showcmd(c);
744 #endif
745 	    goto getcount;		/* jump back */
746 	}
747     }
748 
749     /*
750      * If we're in the middle of an operator (including after entering a yank
751      * buffer with '"') AND we had a count before the operator, then that
752      * count overrides the current value of ca.count0.
753      * What this means effectively, is that commands like "3dw" get turned
754      * into "d3w" which makes things fall into place pretty neatly.
755      * If you give a count before AND after the operator, they are multiplied.
756      */
757     if (ca.opcount != 0)
758     {
759 	if (ca.count0)
760 	    ca.count0 *= ca.opcount;
761 	else
762 	    ca.count0 = ca.opcount;
763     }
764 
765     /*
766      * Always remember the count.  It will be set to zero (on the next call,
767      * above) when there is no pending operator.
768      * When called from main(), save the count for use by the "count" built-in
769      * variable.
770      */
771     ca.opcount = ca.count0;
772     ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
773 
774 #ifdef FEAT_EVAL
775     /*
776      * Only set v:count when called from main() and not a stuffed command.
777      */
778     if (toplevel && stuff_empty())
779 	set_vcount(ca.count0, ca.count1);
780 #endif
781 
782     /*
783      * Find the command character in the table of commands.
784      * For CTRL-W we already got nchar when looking for a count.
785      */
786     if (ctrl_w)
787     {
788 	ca.nchar = c;
789 	ca.cmdchar = Ctrl_W;
790     }
791     else
792 	ca.cmdchar = c;
793     idx = find_command(ca.cmdchar);
794     if (idx < 0)
795     {
796 	/* Not a known command: beep. */
797 	clearopbeep(oap);
798 	goto normal_end;
799     }
800 
801     if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
802     {
803 	/* This command is not allowed wile editing a ccmdline: beep. */
804 	clearopbeep(oap);
805 	text_locked_msg();
806 	goto normal_end;
807     }
808 
809 #ifdef FEAT_VISUAL
810     /*
811      * In Visual/Select mode, a few keys are handled in a special way.
812      */
813     if (VIsual_active)
814     {
815 	/* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
816 	if (km_stopsel
817 		&& (nv_cmds[idx].cmd_flags & NV_STS)
818 		&& !(mod_mask & MOD_MASK_SHIFT))
819 	{
820 	    end_visual_mode();
821 	    redraw_curbuf_later(INVERTED);
822 	}
823 
824 	/* Keys that work different when 'keymodel' contains "startsel" */
825 	if (km_startsel)
826 	{
827 	    if (nv_cmds[idx].cmd_flags & NV_SS)
828 	    {
829 		unshift_special(&ca);
830 		idx = find_command(ca.cmdchar);
831 	    }
832 	    else if ((nv_cmds[idx].cmd_flags & NV_SSS)
833 					       && (mod_mask & MOD_MASK_SHIFT))
834 	    {
835 		mod_mask &= ~MOD_MASK_SHIFT;
836 	    }
837 	}
838     }
839 #endif
840 
841 #ifdef FEAT_RIGHTLEFT
842     if (curwin->w_p_rl && KeyTyped && !KeyStuffed
843 					  && (nv_cmds[idx].cmd_flags & NV_RL))
844     {
845 	/* Invert horizontal movements and operations.  Only when typed by the
846 	 * user directly, not when the result of a mapping or "x" translated
847 	 * to "dl". */
848 	switch (ca.cmdchar)
849 	{
850 	    case 'l':	    ca.cmdchar = 'h'; break;
851 	    case K_RIGHT:   ca.cmdchar = K_LEFT; break;
852 	    case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
853 	    case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
854 	    case 'h':	    ca.cmdchar = 'l'; break;
855 	    case K_LEFT:    ca.cmdchar = K_RIGHT; break;
856 	    case K_S_LEFT:  ca.cmdchar = K_S_RIGHT; break;
857 	    case K_C_LEFT:  ca.cmdchar = K_C_RIGHT; break;
858 	    case '>':	    ca.cmdchar = '<'; break;
859 	    case '<':	    ca.cmdchar = '>'; break;
860 	}
861 	idx = find_command(ca.cmdchar);
862     }
863 #endif
864 
865     /*
866      * Get an additional character if we need one.
867      */
868     if ((nv_cmds[idx].cmd_flags & NV_NCH)
869 	    && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
870 		    && oap->op_type == OP_NOP)
871 		|| (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
872 		|| (ca.cmdchar == 'q'
873 		    && oap->op_type == OP_NOP
874 		    && !Recording
875 		    && !Exec_reg)
876 		|| ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
877 		    && (oap->op_type != OP_NOP
878 #ifdef FEAT_VISUAL
879 			|| VIsual_active
880 #endif
881 		       ))))
882     {
883 	int	*cp;
884 	int	repl = FALSE;	/* get character for replace mode */
885 	int	lit = FALSE;	/* get extra character literally */
886 	int	langmap_active = FALSE;    /* using :lmap mappings */
887 	int	lang;		/* getting a text character */
888 #ifdef USE_IM_CONTROL
889 	int	save_smd;	/* saved value of p_smd */
890 #endif
891 
892 	++no_mapping;
893 	++allow_keys;		/* no mapping for nchar, but allow key codes */
894 	if (ca.cmdchar == 'g')
895 	{
896 	    /*
897 	     * For 'g' get the next character now, so that we can check for
898 	     * "gr", "g'" and "g`".
899 	     */
900 	    ca.nchar = safe_vgetc();
901 #ifdef FEAT_LANGMAP
902 	    LANGMAP_ADJUST(ca.nchar, TRUE);
903 #endif
904 #ifdef FEAT_CMDL_INFO
905 	    need_flushbuf |= add_to_showcmd(ca.nchar);
906 #endif
907 	    if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
908 						      || ca.nchar == Ctrl_BSL)
909 	    {
910 		cp = &ca.extra_char;	/* need to get a third character */
911 		if (ca.nchar != 'r')
912 		    lit = TRUE;			/* get it literally */
913 		else
914 		    repl = TRUE;		/* get it in replace mode */
915 	    }
916 	    else
917 		cp = NULL;		/* no third character needed */
918 	}
919 	else
920 	{
921 	    if (ca.cmdchar == 'r')		/* get it in replace mode */
922 		repl = TRUE;
923 	    cp = &ca.nchar;
924 	}
925 	lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
926 
927 	/*
928 	 * Get a second or third character.
929 	 */
930 	if (cp != NULL)
931 	{
932 #ifdef CURSOR_SHAPE
933 	    if (repl)
934 	    {
935 		State = REPLACE;	/* pretend Replace mode */
936 		ui_cursor_shape();	/* show different cursor shape */
937 	    }
938 #endif
939 	    if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
940 	    {
941 		/* Allow mappings defined with ":lmap". */
942 		--no_mapping;
943 		--allow_keys;
944 		if (repl)
945 		    State = LREPLACE;
946 		else
947 		    State = LANGMAP;
948 		langmap_active = TRUE;
949 	    }
950 #ifdef USE_IM_CONTROL
951 	    save_smd = p_smd;
952 	    p_smd = FALSE;	/* Don't let the IM code show the mode here */
953 	    if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
954 		im_set_active(TRUE);
955 #endif
956 
957 	    *cp = safe_vgetc();
958 
959 	    if (langmap_active)
960 	    {
961 		/* Undo the decrement done above */
962 		++no_mapping;
963 		++allow_keys;
964 		State = NORMAL_BUSY;
965 	    }
966 #ifdef USE_IM_CONTROL
967 	    if (lang)
968 	    {
969 		if (curbuf->b_p_iminsert != B_IMODE_LMAP)
970 		    im_save_status(&curbuf->b_p_iminsert);
971 		im_set_active(FALSE);
972 	    }
973 	    p_smd = save_smd;
974 #endif
975 #ifdef CURSOR_SHAPE
976 	    State = NORMAL_BUSY;
977 #endif
978 #ifdef FEAT_CMDL_INFO
979 	    need_flushbuf |= add_to_showcmd(*cp);
980 #endif
981 
982 	    if (!lit)
983 	    {
984 #ifdef FEAT_DIGRAPHS
985 		/* Typing CTRL-K gets a digraph. */
986 		if (*cp == Ctrl_K
987 			&& ((nv_cmds[idx].cmd_flags & NV_LANG)
988 			    || cp == &ca.extra_char)
989 			&& vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
990 		{
991 		    c = get_digraph(FALSE);
992 		    if (c > 0)
993 		    {
994 			*cp = c;
995 # ifdef FEAT_CMDL_INFO
996 			/* Guessing how to update showcmd here... */
997 			del_from_showcmd(3);
998 			need_flushbuf |= add_to_showcmd(*cp);
999 # endif
1000 		    }
1001 		}
1002 #endif
1003 
1004 #ifdef FEAT_LANGMAP
1005 		/* adjust chars > 127, except after "tTfFr" commands */
1006 		LANGMAP_ADJUST(*cp, !lang);
1007 #endif
1008 #ifdef FEAT_RIGHTLEFT
1009 		/* adjust Hebrew mapped char */
1010 		if (p_hkmap && lang && KeyTyped)
1011 		    *cp = hkmap(*cp);
1012 # ifdef FEAT_FKMAP
1013 		/* adjust Farsi mapped char */
1014 		if (p_fkmap && lang && KeyTyped)
1015 		    *cp = fkmap(*cp);
1016 # endif
1017 #endif
1018 	    }
1019 
1020 	    /*
1021 	     * When the next character is CTRL-\ a following CTRL-N means the
1022 	     * command is aborted and we go to Normal mode.
1023 	     */
1024 	    if (cp == &ca.extra_char
1025 		    && ca.nchar == Ctrl_BSL
1026 		    && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1027 	    {
1028 		ca.cmdchar = Ctrl_BSL;
1029 		ca.nchar = ca.extra_char;
1030 		idx = find_command(ca.cmdchar);
1031 	    }
1032 	    else if (*cp == Ctrl_BSL)
1033 	    {
1034 		long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1035 
1036 		/* There is a busy wait here when typing "f<C-\>" and then
1037 		 * something different from CTRL-N.  Can't be avoided. */
1038 		while ((c = vpeekc()) <= 0 && towait > 0L)
1039 		{
1040 		    do_sleep(towait > 50L ? 50L : towait);
1041 		    towait -= 50L;
1042 		}
1043 		if (c > 0)
1044 		{
1045 		    c = safe_vgetc();
1046 		    if (c != Ctrl_N && c != Ctrl_G)
1047 			vungetc(c);
1048 		    else
1049 		    {
1050 			ca.cmdchar = Ctrl_BSL;
1051 			ca.nchar = c;
1052 			idx = find_command(ca.cmdchar);
1053 		    }
1054 		}
1055 	    }
1056 
1057 #ifdef FEAT_MBYTE
1058 	    /* When getting a text character and the next character is a
1059 	     * multi-byte character, it could be a composing character.
1060 	     * However, don't wait for it to arrive. */
1061 	    while (enc_utf8 && lang && (c = vpeekc()) > 0
1062 				 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1063 	    {
1064 		c = safe_vgetc();
1065 		if (!utf_iscomposing(c))
1066 		{
1067 		    vungetc(c);		/* it wasn't, put it back */
1068 		    break;
1069 		}
1070 		else if (ca.ncharC1 == 0)
1071 		    ca.ncharC1 = c;
1072 		else
1073 		    ca.ncharC2 = c;
1074 	    }
1075 #endif
1076 	}
1077 	--no_mapping;
1078 	--allow_keys;
1079     }
1080 
1081 #ifdef FEAT_CMDL_INFO
1082     /*
1083      * Flush the showcmd characters onto the screen so we can see them while
1084      * the command is being executed.  Only do this when the shown command was
1085      * actually displayed, otherwise this will slow down a lot when executing
1086      * mappings.
1087      */
1088     if (need_flushbuf)
1089 	out_flush();
1090 #endif
1091 #ifdef FEAT_AUTOCMD
1092     did_cursorhold = FALSE;
1093 #endif
1094 
1095     State = NORMAL;
1096 
1097     if (ca.nchar == ESC)
1098     {
1099 	clearop(oap);
1100 	if (restart_edit == 0 && goto_im())
1101 	    restart_edit = 'a';
1102 	goto normal_end;
1103     }
1104 
1105     if (ca.cmdchar != K_IGNORE)
1106     {
1107 	msg_didout = FALSE;    /* don't scroll screen up for normal command */
1108 	msg_col = 0;
1109     }
1110 
1111 #ifdef FEAT_VISUAL
1112     old_pos = curwin->w_cursor;		/* remember where cursor was */
1113 
1114     /* When 'keymodel' contains "startsel" some keys start Select/Visual
1115      * mode. */
1116     if (!VIsual_active && km_startsel)
1117     {
1118 	if (nv_cmds[idx].cmd_flags & NV_SS)
1119 	{
1120 	    start_selection();
1121 	    unshift_special(&ca);
1122 	    idx = find_command(ca.cmdchar);
1123 	}
1124 	else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1125 					   && (mod_mask & MOD_MASK_SHIFT))
1126 	{
1127 	    start_selection();
1128 	    mod_mask &= ~MOD_MASK_SHIFT;
1129 	}
1130     }
1131 #endif
1132 
1133     /*
1134      * Execute the command!
1135      * Call the command function found in the commands table.
1136      */
1137     ca.arg = nv_cmds[idx].cmd_arg;
1138     (nv_cmds[idx].cmd_func)(&ca);
1139 
1140     /*
1141      * If we didn't start or finish an operator, reset oap->regname, unless we
1142      * need it later.
1143      */
1144     if (!finish_op
1145 	    && !oap->op_type
1146 	    && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1147     {
1148 	clearop(oap);
1149 #ifdef FEAT_EVAL
1150 	set_reg_var('"');
1151 #endif
1152     }
1153 
1154     /* Get the length of mapped chars again after typing a count, second
1155      * character or "z333<cr>". */
1156     if (old_mapped_len > 0)
1157 	old_mapped_len = typebuf_maplen();
1158 
1159     /*
1160      * If an operation is pending, handle it...
1161      */
1162     do_pending_operator(&ca, old_col, FALSE);
1163 
1164     /*
1165      * Wait for a moment when a message is displayed that will be overwritten
1166      * by the mode message.
1167      * In Visual mode and with "^O" in Insert mode, a short message will be
1168      * overwritten by the mode message.  Wait a bit, until a key is hit.
1169      * In Visual mode, it's more important to keep the Visual area updated
1170      * than keeping a message (e.g. from a /pat search).
1171      * Only do this if the command was typed, not from a mapping.
1172      * Don't wait when emsg_silent is non-zero.
1173      * Also wait a bit after an error message, e.g. for "^O:".
1174      * Don't redraw the screen, it would remove the message.
1175      */
1176     if (       ((p_smd
1177 		    && msg_silent == 0
1178 		    && (restart_edit != 0
1179 #ifdef FEAT_VISUAL
1180 			|| (VIsual_active
1181 			    && old_pos.lnum == curwin->w_cursor.lnum
1182 			    && old_pos.col == curwin->w_cursor.col)
1183 #endif
1184 		       )
1185 		    && (clear_cmdline
1186 			|| redraw_cmdline)
1187 		    && (msg_didout || (msg_didany && msg_scroll))
1188 		    && !msg_nowait
1189 		    && KeyTyped)
1190 		|| (restart_edit != 0
1191 #ifdef FEAT_VISUAL
1192 		    && !VIsual_active
1193 #endif
1194 		    && (msg_scroll
1195 			|| emsg_on_display)))
1196 	    && oap->regname == 0
1197 	    && !(ca.retval & CA_COMMAND_BUSY)
1198 	    && stuff_empty()
1199 	    && typebuf_typed()
1200 	    && emsg_silent == 0
1201 	    && !did_wait_return
1202 	    && oap->op_type == OP_NOP)
1203     {
1204 	int	save_State = State;
1205 
1206 	/* Draw the cursor with the right shape here */
1207 	if (restart_edit != 0)
1208 	    State = INSERT;
1209 
1210 	/* If need to redraw, and there is a "keep_msg", redraw before the
1211 	 * delay */
1212 	if (must_redraw && keep_msg != NULL && !emsg_on_display)
1213 	{
1214 	    char_u	*kmsg;
1215 
1216 	    kmsg = keep_msg;
1217 	    keep_msg = NULL;
1218 	    /* showmode() will clear keep_msg, but we want to use it anyway */
1219 	    update_screen(0);
1220 	    /* now reset it, otherwise it's put in the history again */
1221 	    keep_msg = kmsg;
1222 	    msg_attr(kmsg, keep_msg_attr);
1223 	    vim_free(kmsg);
1224 	}
1225 	setcursor();
1226 	cursor_on();
1227 	out_flush();
1228 	if (msg_scroll || emsg_on_display)
1229 	    ui_delay(1000L, TRUE);	/* wait at least one second */
1230 	ui_delay(3000L, FALSE);		/* wait up to three seconds */
1231 	State = save_State;
1232 
1233 	msg_scroll = FALSE;
1234 	emsg_on_display = FALSE;
1235     }
1236 
1237     /*
1238      * Finish up after executing a Normal mode command.
1239      */
1240 normal_end:
1241 
1242     msg_nowait = FALSE;
1243 
1244     /* Reset finish_op, in case it was set */
1245 #ifdef CURSOR_SHAPE
1246     c = finish_op;
1247 #endif
1248     finish_op = FALSE;
1249 #ifdef CURSOR_SHAPE
1250     /* Redraw the cursor with another shape, if we were in Operator-pending
1251      * mode or did a replace command. */
1252     if (c || ca.cmdchar == 'r')
1253     {
1254 	ui_cursor_shape();		/* may show different cursor shape */
1255 # ifdef FEAT_MOUSESHAPE
1256 	update_mouseshape(-1);
1257 # endif
1258     }
1259 #endif
1260 
1261 #ifdef FEAT_CMDL_INFO
1262     if (oap->op_type == OP_NOP && oap->regname == 0)
1263 	clear_showcmd();
1264 #endif
1265 
1266     checkpcmark();		/* check if we moved since setting pcmark */
1267     vim_free(ca.searchbuf);
1268 
1269 #ifdef FEAT_MBYTE
1270     if (has_mbyte)
1271 	mb_adjust_cursor();
1272 #endif
1273 
1274 #ifdef FEAT_SCROLLBIND
1275     if (curwin->w_p_scb && toplevel)
1276     {
1277 	validate_cursor();	/* may need to update w_leftcol */
1278 	do_check_scrollbind(TRUE);
1279     }
1280 #endif
1281 
1282     /*
1283      * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1284      * if still inside a mapping that started in Visual mode).
1285      * May switch from Visual to Select mode after CTRL-O command.
1286      */
1287     if (       oap->op_type == OP_NOP
1288 #ifdef FEAT_VISUAL
1289 	    && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1290 		|| restart_VIsual_select == 1)
1291 #endif
1292 	    && !(ca.retval & CA_COMMAND_BUSY)
1293 	    && stuff_empty()
1294 	    && oap->regname == 0)
1295     {
1296 #ifdef FEAT_VISUAL
1297 	if (restart_VIsual_select == 1)
1298 	{
1299 	    VIsual_select = TRUE;
1300 	    showmode();
1301 	    restart_VIsual_select = 0;
1302 	}
1303 #endif
1304 	if (restart_edit != 0
1305 #ifdef FEAT_VISUAL
1306 		&& !VIsual_active
1307 #endif
1308 		&& old_mapped_len == 0)
1309 	    (void)edit(restart_edit, FALSE, 1L);
1310     }
1311 
1312 #ifdef FEAT_VISUAL
1313     if (restart_VIsual_select == 2)
1314 	restart_VIsual_select = 1;
1315 #endif
1316 
1317     /* Save count before an operator for next time. */
1318     opcount = ca.opcount;
1319 }
1320 
1321 /*
1322  * Handle an operator after visual mode or when the movement is finished
1323  */
1324     void
1325 do_pending_operator(cap, old_col, gui_yank)
1326     cmdarg_T	*cap;
1327     int		old_col;
1328     int		gui_yank;
1329 {
1330     oparg_T	*oap = cap->oap;
1331     pos_T	old_cursor;
1332     int		empty_region_error;
1333     int		restart_edit_save;
1334 
1335 #ifdef FEAT_VISUAL
1336     /* The visual area is remembered for redo */
1337     static int	    redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1338     static linenr_T redo_VIsual_line_count; /* number of lines */
1339     static colnr_T  redo_VIsual_col;	    /* number of cols or end column */
1340     static long	    redo_VIsual_count;	    /* count for Visual operator */
1341 # ifdef FEAT_VIRTUALEDIT
1342     int		    include_line_break = FALSE;
1343 # endif
1344 #endif
1345 
1346 #if defined(FEAT_CLIPBOARD)
1347     /*
1348      * Yank the visual area into the GUI selection register before we operate
1349      * on it and lose it forever.
1350      * Don't do it if a specific register was specified, so that ""x"*P works.
1351      * This could call do_pending_operator() recursively, but that's OK
1352      * because gui_yank will be TRUE for the nested call.
1353      */
1354     if (clip_star.available
1355 	    && oap->op_type != OP_NOP
1356 	    && !gui_yank
1357 # ifdef FEAT_VISUAL
1358 	    && VIsual_active
1359 	    && !redo_VIsual_busy
1360 # endif
1361 	    && oap->regname == 0)
1362 	clip_auto_select();
1363 #endif
1364     old_cursor = curwin->w_cursor;
1365 
1366     /*
1367      * If an operation is pending, handle it...
1368      */
1369     if ((finish_op
1370 #ifdef FEAT_VISUAL
1371 		|| VIsual_active
1372 #endif
1373 		) && oap->op_type != OP_NOP)
1374     {
1375 #ifdef FEAT_VISUAL
1376 	oap->is_VIsual = VIsual_active;
1377 	if (oap->motion_force == 'V')
1378 	    oap->motion_type = MLINE;
1379 	else if (oap->motion_force == 'v')
1380 	{
1381 	    /* If the motion was linewise, "inclusive" will not have been set.
1382 	     * Use "exclusive" to be consistent.  Makes "dvj" work nice. */
1383 	    if (oap->motion_type == MLINE)
1384 		oap->inclusive = FALSE;
1385 	    /* If the motion already was characterwise, toggle "inclusive" */
1386 	    else if (oap->motion_type == MCHAR)
1387 		oap->inclusive = !oap->inclusive;
1388 	    oap->motion_type = MCHAR;
1389 	}
1390 	else if (oap->motion_force == Ctrl_V)
1391 	{
1392 	    /* Change line- or characterwise motion into Visual block mode. */
1393 	    VIsual_active = TRUE;
1394 	    VIsual = oap->start;
1395 	    VIsual_mode = Ctrl_V;
1396 	    VIsual_select = FALSE;
1397 	    VIsual_reselect = FALSE;
1398 	}
1399 #endif
1400 
1401 	/* only redo yank when 'y' flag is in 'cpoptions' */
1402 	/* never redo "zf" (define fold) */
1403 	if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1404 #ifdef FEAT_VISUAL
1405 		&& (!VIsual_active || oap->motion_force)
1406 #endif
1407 		&& cap->cmdchar != 'D'
1408 #ifdef FEAT_FOLDING
1409 		&& oap->op_type != OP_FOLD
1410 		&& oap->op_type != OP_FOLDOPEN
1411 		&& oap->op_type != OP_FOLDOPENREC
1412 		&& oap->op_type != OP_FOLDCLOSE
1413 		&& oap->op_type != OP_FOLDCLOSEREC
1414 		&& oap->op_type != OP_FOLDDEL
1415 		&& oap->op_type != OP_FOLDDELREC
1416 #endif
1417 		)
1418 	{
1419 	    prep_redo(oap->regname, cap->count0,
1420 		    get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1421 		    oap->motion_force, cap->cmdchar, cap->nchar);
1422 	    if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1423 	    {
1424 		/*
1425 		 * If 'cpoptions' does not contain 'r', insert the search
1426 		 * pattern to really repeat the same command.
1427 		 */
1428 		if (vim_strchr(p_cpo, CPO_REDO) == NULL)
1429 		    AppendToRedobuffLit(cap->searchbuf, -1);
1430 		AppendToRedobuff(NL_STR);
1431 	    }
1432 	    else if (cap->cmdchar == ':')
1433 	    {
1434 		/* do_cmdline() has stored the first typed line in
1435 		 * "repeat_cmdline".  When several lines are typed repeating
1436 		 * won't be possible. */
1437 		if (repeat_cmdline == NULL)
1438 		    ResetRedobuff();
1439 		else
1440 		{
1441 		    AppendToRedobuffLit(repeat_cmdline, -1);
1442 		    AppendToRedobuff(NL_STR);
1443 		    vim_free(repeat_cmdline);
1444 		    repeat_cmdline = NULL;
1445 		}
1446 	    }
1447 	}
1448 
1449 #ifdef FEAT_VISUAL
1450 	if (redo_VIsual_busy)
1451 	{
1452 	    oap->start = curwin->w_cursor;
1453 	    curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1454 	    if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1455 		curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1456 	    VIsual_mode = redo_VIsual_mode;
1457 	    if (VIsual_mode == 'v')
1458 	    {
1459 		if (redo_VIsual_line_count <= 1)
1460 		    curwin->w_cursor.col += redo_VIsual_col - 1;
1461 		else
1462 		    curwin->w_cursor.col = redo_VIsual_col;
1463 	    }
1464 	    if (redo_VIsual_col == MAXCOL)
1465 	    {
1466 		curwin->w_curswant = MAXCOL;
1467 		coladvance((colnr_T)MAXCOL);
1468 	    }
1469 	    cap->count0 = redo_VIsual_count;
1470 	    if (redo_VIsual_count != 0)
1471 		cap->count1 = redo_VIsual_count;
1472 	    else
1473 		cap->count1 = 1;
1474 	}
1475 	else if (VIsual_active)
1476 	{
1477 	    /* Save the current VIsual area for '< and '> marks, and "gv" */
1478 	    curbuf->b_visual.vi_start = VIsual;
1479 	    curbuf->b_visual.vi_end = curwin->w_cursor;
1480 	    curbuf->b_visual.vi_mode = VIsual_mode;
1481 	    curbuf->b_visual.vi_curswant = curwin->w_curswant;
1482 # ifdef FEAT_EVAL
1483 	    curbuf->b_visual_mode_eval = VIsual_mode;
1484 # endif
1485 
1486 	    /* In Select mode, a linewise selection is operated upon like a
1487 	     * characterwise selection. */
1488 	    if (VIsual_select && VIsual_mode == 'V')
1489 	    {
1490 		if (lt(VIsual, curwin->w_cursor))
1491 		{
1492 		    VIsual.col = 0;
1493 		    curwin->w_cursor.col =
1494 			       (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1495 		}
1496 		else
1497 		{
1498 		    curwin->w_cursor.col = 0;
1499 		    VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1500 		}
1501 		VIsual_mode = 'v';
1502 	    }
1503 	    /* If 'selection' is "exclusive", backup one character for
1504 	     * charwise selections. */
1505 	    else if (VIsual_mode == 'v')
1506 	    {
1507 # ifdef FEAT_VIRTUALEDIT
1508 		include_line_break =
1509 # endif
1510 		    unadjust_for_sel();
1511 	    }
1512 
1513 	    oap->start = VIsual;
1514 	    if (VIsual_mode == 'V')
1515 		oap->start.col = 0;
1516 	}
1517 #endif /* FEAT_VISUAL */
1518 
1519 	/*
1520 	 * Set oap->start to the first position of the operated text, oap->end
1521 	 * to the end of the operated text.  w_cursor is equal to oap->start.
1522 	 */
1523 	if (lt(oap->start, curwin->w_cursor))
1524 	{
1525 #ifdef FEAT_FOLDING
1526 	    /* Include folded lines completely. */
1527 	    if (!VIsual_active)
1528 	    {
1529 		if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1530 		    oap->start.col = 0;
1531 		if (hasFolding(curwin->w_cursor.lnum, NULL,
1532 						      &curwin->w_cursor.lnum))
1533 		    curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1534 	    }
1535 #endif
1536 	    oap->end = curwin->w_cursor;
1537 	    curwin->w_cursor = oap->start;
1538 
1539 	    /* w_virtcol may have been updated; if the cursor goes back to its
1540 	     * previous position w_virtcol becomes invalid and isn't updated
1541 	     * automatically. */
1542 	    curwin->w_valid &= ~VALID_VIRTCOL;
1543 	}
1544 	else
1545 	{
1546 #ifdef FEAT_FOLDING
1547 	    /* Include folded lines completely. */
1548 	    if (!VIsual_active && oap->motion_type == MLINE)
1549 	    {
1550 		if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1551 									NULL))
1552 		    curwin->w_cursor.col = 0;
1553 		if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1554 		    oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1555 	    }
1556 #endif
1557 	    oap->end = oap->start;
1558 	    oap->start = curwin->w_cursor;
1559 	}
1560 
1561 	oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1562 
1563 #ifdef FEAT_VIRTUALEDIT
1564 	/* Set "virtual_op" before resetting VIsual_active. */
1565 	virtual_op = virtual_active();
1566 #endif
1567 
1568 #ifdef FEAT_VISUAL
1569 	if (VIsual_active || redo_VIsual_busy)
1570 	{
1571 	    if (VIsual_mode == Ctrl_V)	/* block mode */
1572 	    {
1573 		colnr_T	    start, end;
1574 
1575 		oap->block_mode = TRUE;
1576 
1577 		getvvcol(curwin, &(oap->start),
1578 				      &oap->start_vcol, NULL, &oap->end_vcol);
1579 		if (!redo_VIsual_busy)
1580 		{
1581 		    getvvcol(curwin, &(oap->end), &start, NULL, &end);
1582 
1583 		    if (start < oap->start_vcol)
1584 			oap->start_vcol = start;
1585 		    if (end > oap->end_vcol)
1586 		    {
1587 			if (*p_sel == 'e' && start >= 1
1588 						&& start - 1 >= oap->end_vcol)
1589 			    oap->end_vcol = start - 1;
1590 			else
1591 			    oap->end_vcol = end;
1592 		    }
1593 		}
1594 
1595 		/* if '$' was used, get oap->end_vcol from longest line */
1596 		if (curwin->w_curswant == MAXCOL)
1597 		{
1598 		    curwin->w_cursor.col = MAXCOL;
1599 		    oap->end_vcol = 0;
1600 		    for (curwin->w_cursor.lnum = oap->start.lnum;
1601 			    curwin->w_cursor.lnum <= oap->end.lnum;
1602 						      ++curwin->w_cursor.lnum)
1603 		    {
1604 			getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1605 			if (end > oap->end_vcol)
1606 			    oap->end_vcol = end;
1607 		    }
1608 		}
1609 		else if (redo_VIsual_busy)
1610 		    oap->end_vcol = oap->start_vcol + redo_VIsual_col - 1;
1611 		/*
1612 		 * Correct oap->end.col and oap->start.col to be the
1613 		 * upper-left and lower-right corner of the block area.
1614 		 *
1615 		 * (Actually, this does convert column positions into character
1616 		 * positions)
1617 		 */
1618 		curwin->w_cursor.lnum = oap->end.lnum;
1619 		coladvance(oap->end_vcol);
1620 		oap->end = curwin->w_cursor;
1621 
1622 		curwin->w_cursor = oap->start;
1623 		coladvance(oap->start_vcol);
1624 		oap->start = curwin->w_cursor;
1625 	    }
1626 
1627 	    if (!redo_VIsual_busy && !gui_yank)
1628 	    {
1629 		/*
1630 		 * Prepare to reselect and redo Visual: this is based on the
1631 		 * size of the Visual text
1632 		 */
1633 		resel_VIsual_mode = VIsual_mode;
1634 		if (curwin->w_curswant == MAXCOL)
1635 		    resel_VIsual_col = MAXCOL;
1636 		else if (VIsual_mode == Ctrl_V)
1637 		    resel_VIsual_col = oap->end_vcol - oap->start_vcol + 1;
1638 		else if (oap->line_count > 1)
1639 		    resel_VIsual_col = oap->end.col;
1640 		else
1641 		    resel_VIsual_col = oap->end.col - oap->start.col + 1;
1642 		resel_VIsual_line_count = oap->line_count;
1643 	    }
1644 
1645 	    /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1646 	    if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1647 		    && oap->op_type != OP_COLON
1648 #ifdef FEAT_FOLDING
1649 		    && oap->op_type != OP_FOLD
1650 		    && oap->op_type != OP_FOLDOPEN
1651 		    && oap->op_type != OP_FOLDOPENREC
1652 		    && oap->op_type != OP_FOLDCLOSE
1653 		    && oap->op_type != OP_FOLDCLOSEREC
1654 		    && oap->op_type != OP_FOLDDEL
1655 		    && oap->op_type != OP_FOLDDELREC
1656 #endif
1657 		    && oap->motion_force == NUL
1658 		    )
1659 	    {
1660 		/* Prepare for redoing.  Only use the nchar field for "r",
1661 		 * otherwise it might be the second char of the operator. */
1662 		prep_redo(oap->regname, 0L, NUL, 'v',
1663 				get_op_char(oap->op_type),
1664 				get_extra_op_char(oap->op_type),
1665 				oap->op_type == OP_REPLACE ? cap->nchar : NUL);
1666 		if (!redo_VIsual_busy)
1667 		{
1668 		    redo_VIsual_mode = resel_VIsual_mode;
1669 		    redo_VIsual_col = resel_VIsual_col;
1670 		    redo_VIsual_line_count = resel_VIsual_line_count;
1671 		    redo_VIsual_count = cap->count0;
1672 		}
1673 	    }
1674 
1675 	    /*
1676 	     * oap->inclusive defaults to TRUE.
1677 	     * If oap->end is on a NUL (empty line) oap->inclusive becomes
1678 	     * FALSE.  This makes "d}P" and "v}dP" work the same.
1679 	     */
1680 	    if (oap->motion_force == NUL || oap->motion_type == MLINE)
1681 		oap->inclusive = TRUE;
1682 	    if (VIsual_mode == 'V')
1683 		oap->motion_type = MLINE;
1684 	    else
1685 	    {
1686 		oap->motion_type = MCHAR;
1687 		if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1688 # ifdef FEAT_VIRTUALEDIT
1689 			&& (include_line_break || !virtual_op)
1690 # endif
1691 			)
1692 		{
1693 		    oap->inclusive = FALSE;
1694 		    /* Try to include the newline, unless it's an operator
1695 		     * that works on lines only */
1696 		    if (*p_sel != 'o'
1697 			    && !op_on_lines(oap->op_type)
1698 			    && oap->end.lnum < curbuf->b_ml.ml_line_count)
1699 		    {
1700 			++oap->end.lnum;
1701 			oap->end.col = 0;
1702 # ifdef FEAT_VIRTUALEDIT
1703 			oap->end.coladd = 0;
1704 # endif
1705 			++oap->line_count;
1706 		    }
1707 		}
1708 	    }
1709 
1710 	    redo_VIsual_busy = FALSE;
1711 
1712 	    /*
1713 	     * Switch Visual off now, so screen updating does
1714 	     * not show inverted text when the screen is redrawn.
1715 	     * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1716 	     * no screen redraw, so it is done here to remove the inverted
1717 	     * part.
1718 	     */
1719 	    if (!gui_yank)
1720 	    {
1721 		VIsual_active = FALSE;
1722 # ifdef FEAT_MOUSE
1723 		setmouse();
1724 		mouse_dragging = 0;
1725 # endif
1726 		if (mode_displayed)
1727 		    clear_cmdline = TRUE;   /* unshow visual mode later */
1728 #ifdef FEAT_CMDL_INFO
1729 		else
1730 		    clear_showcmd();
1731 #endif
1732 		if ((oap->op_type == OP_YANK
1733 			    || oap->op_type == OP_COLON
1734 			    || oap->op_type == OP_FUNCTION
1735 			    || oap->op_type == OP_FILTER)
1736 			&& oap->motion_force == NUL)
1737 		    redraw_curbuf_later(INVERTED);
1738 	    }
1739 	}
1740 #endif
1741 
1742 #ifdef FEAT_MBYTE
1743 	/* Include the trailing byte of a multi-byte char. */
1744 	if (has_mbyte && oap->inclusive)
1745 	{
1746 	    int		l;
1747 
1748 	    l = (*mb_ptr2len)(ml_get_pos(&oap->end));
1749 	    if (l > 1)
1750 		oap->end.col += l - 1;
1751 	}
1752 #endif
1753 	curwin->w_set_curswant = TRUE;
1754 
1755 	/*
1756 	 * oap->empty is set when start and end are the same.  The inclusive
1757 	 * flag affects this too, unless yanking and the end is on a NUL.
1758 	 */
1759 	oap->empty = (oap->motion_type == MCHAR
1760 		    && (!oap->inclusive
1761 			|| (oap->op_type == OP_YANK
1762 			    && gchar_pos(&oap->end) == NUL))
1763 		    && equalpos(oap->start, oap->end)
1764 #ifdef FEAT_VIRTUALEDIT
1765 		    && !(virtual_op && oap->start.coladd != oap->end.coladd)
1766 #endif
1767 		    );
1768 	/*
1769 	 * For delete, change and yank, it's an error to operate on an
1770 	 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1771 	 */
1772 	empty_region_error = (oap->empty
1773 				&& vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1774 
1775 #ifdef FEAT_VISUAL
1776 	/* Force a redraw when operating on an empty Visual region, when
1777 	 * 'modifiable is off or creating a fold. */
1778 	if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1779 # ifdef FEAT_FOLDING
1780 		    || oap->op_type == OP_FOLD
1781 # endif
1782 		    ))
1783 	    redraw_curbuf_later(INVERTED);
1784 #endif
1785 
1786 	/*
1787 	 * If the end of an operator is in column one while oap->motion_type
1788 	 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1789 	 * character in the previous line. If op_start is on or before the
1790 	 * first non-blank in the line, the operator becomes linewise
1791 	 * (strange, but that's the way vi does it).
1792 	 */
1793 	if (	   oap->motion_type == MCHAR
1794 		&& oap->inclusive == FALSE
1795 		&& !(cap->retval & CA_NO_ADJ_OP_END)
1796 		&& oap->end.col == 0
1797 #ifdef FEAT_VISUAL
1798 		&& (!oap->is_VIsual || *p_sel == 'o')
1799 		&& !oap->block_mode
1800 #endif
1801 		&& oap->line_count > 1)
1802 	{
1803 	    oap->end_adjusted = TRUE;	    /* remember that we did this */
1804 	    --oap->line_count;
1805 	    --oap->end.lnum;
1806 	    if (inindent(0))
1807 		oap->motion_type = MLINE;
1808 	    else
1809 	    {
1810 		oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1811 		if (oap->end.col)
1812 		{
1813 		    --oap->end.col;
1814 		    oap->inclusive = TRUE;
1815 		}
1816 	    }
1817 	}
1818 	else
1819 	    oap->end_adjusted = FALSE;
1820 
1821 	switch (oap->op_type)
1822 	{
1823 	case OP_LSHIFT:
1824 	case OP_RSHIFT:
1825 	    op_shift(oap, TRUE,
1826 #ifdef FEAT_VISUAL
1827 		    oap->is_VIsual ? (int)cap->count1 :
1828 #endif
1829 		    1);
1830 	    auto_format(FALSE, TRUE);
1831 	    break;
1832 
1833 	case OP_JOIN_NS:
1834 	case OP_JOIN:
1835 	    if (oap->line_count < 2)
1836 		oap->line_count = 2;
1837 	    if (curwin->w_cursor.lnum + oap->line_count - 1 >
1838 						   curbuf->b_ml.ml_line_count)
1839 		beep_flush();
1840 	    else
1841 	    {
1842 		do_do_join(oap->line_count, oap->op_type == OP_JOIN);
1843 		auto_format(FALSE, TRUE);
1844 	    }
1845 	    break;
1846 
1847 	case OP_DELETE:
1848 #ifdef FEAT_VISUAL
1849 	    VIsual_reselect = FALSE;	    /* don't reselect now */
1850 #endif
1851 	    if (empty_region_error)
1852 		vim_beep();
1853 	    else
1854 	    {
1855 		(void)op_delete(oap);
1856 		if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1857 		    u_save_cursor();	    /* cursor line wasn't saved yet */
1858 		auto_format(FALSE, TRUE);
1859 	    }
1860 	    break;
1861 
1862 	case OP_YANK:
1863 	    if (empty_region_error)
1864 	    {
1865 		if (!gui_yank)
1866 		    vim_beep();
1867 	    }
1868 	    else
1869 		(void)op_yank(oap, FALSE, !gui_yank);
1870 	    check_cursor_col();
1871 	    break;
1872 
1873 	case OP_CHANGE:
1874 #ifdef FEAT_VISUAL
1875 	    VIsual_reselect = FALSE;	    /* don't reselect now */
1876 #endif
1877 	    if (empty_region_error)
1878 		vim_beep();
1879 	    else
1880 	    {
1881 		/* This is a new edit command, not a restart.  Need to
1882 		 * remember it to make 'insertmode' work with mappings for
1883 		 * Visual mode.  But do this only once and not when typed and
1884 		 * 'insertmode' isn't set. */
1885 		if (p_im || !KeyTyped)
1886 		    restart_edit_save = restart_edit;
1887 		else
1888 		    restart_edit_save = 0;
1889 		restart_edit = 0;
1890 		/* Reset finish_op now, don't want it set inside edit(). */
1891 		finish_op = FALSE;
1892 		if (op_change(oap))	/* will call edit() */
1893 		    cap->retval |= CA_COMMAND_BUSY;
1894 		if (restart_edit == 0)
1895 		    restart_edit = restart_edit_save;
1896 	    }
1897 	    break;
1898 
1899 	case OP_FILTER:
1900 	    if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1901 		AppendToRedobuff((char_u *)"!\r");  /* use any last used !cmd */
1902 	    else
1903 		bangredo = TRUE;    /* do_bang() will put cmd in redo buffer */
1904 
1905 	case OP_INDENT:
1906 	case OP_COLON:
1907 
1908 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1909 	    /*
1910 	     * If 'equalprg' is empty, do the indenting internally.
1911 	     */
1912 	    if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1913 	    {
1914 # ifdef FEAT_LISP
1915 		if (curbuf->b_p_lisp)
1916 		{
1917 		    op_reindent(oap, get_lisp_indent);
1918 		    break;
1919 		}
1920 # endif
1921 # ifdef FEAT_CINDENT
1922 		op_reindent(oap,
1923 #  ifdef FEAT_EVAL
1924 			*curbuf->b_p_inde != NUL ? get_expr_indent :
1925 #  endif
1926 			    get_c_indent);
1927 		break;
1928 # endif
1929 	    }
1930 #endif
1931 
1932 	    op_colon(oap);
1933 	    break;
1934 
1935 	case OP_TILDE:
1936 	case OP_UPPER:
1937 	case OP_LOWER:
1938 	case OP_ROT13:
1939 	    if (empty_region_error)
1940 		vim_beep();
1941 	    else
1942 		op_tilde(oap);
1943 	    check_cursor_col();
1944 	    break;
1945 
1946 	case OP_FORMAT:
1947 #if defined(FEAT_EVAL)
1948 	    if (*curbuf->b_p_fex != NUL)
1949 		op_formatexpr(oap);	/* use expression */
1950 	    else
1951 #endif
1952 		if (*p_fp != NUL)
1953 		op_colon(oap);		/* use external command */
1954 	    else
1955 		op_format(oap, FALSE);	/* use internal function */
1956 	    break;
1957 
1958 	case OP_FORMAT2:
1959 	    op_format(oap, TRUE);	/* use internal function */
1960 	    break;
1961 
1962 	case OP_FUNCTION:
1963 	    op_function(oap);		/* call 'operatorfunc' */
1964 	    break;
1965 
1966 	case OP_INSERT:
1967 	case OP_APPEND:
1968 #ifdef FEAT_VISUAL
1969 	    VIsual_reselect = FALSE;	/* don't reselect now */
1970 #endif
1971 #ifdef FEAT_VISUALEXTRA
1972 	    if (empty_region_error)
1973 		vim_beep();
1974 	    else
1975 	    {
1976 		/* This is a new edit command, not a restart.  Need to
1977 		 * remember it to make 'insertmode' work with mappings for
1978 		 * Visual mode.  But do this only once. */
1979 		restart_edit_save = restart_edit;
1980 		restart_edit = 0;
1981 
1982 		op_insert(oap, cap->count1);
1983 
1984 		/* TODO: when inserting in several lines, should format all
1985 		 * the lines. */
1986 		auto_format(FALSE, TRUE);
1987 
1988 		if (restart_edit == 0)
1989 		    restart_edit = restart_edit_save;
1990 	    }
1991 #else
1992 	    vim_beep();
1993 #endif
1994 	    break;
1995 
1996 	case OP_REPLACE:
1997 #ifdef FEAT_VISUAL
1998 	    VIsual_reselect = FALSE;	/* don't reselect now */
1999 #endif
2000 #ifdef FEAT_VISUALEXTRA
2001 	    if (empty_region_error)
2002 #endif
2003 		vim_beep();
2004 #ifdef FEAT_VISUALEXTRA
2005 	    else
2006 		op_replace(oap, cap->nchar);
2007 #endif
2008 	    break;
2009 
2010 #ifdef FEAT_FOLDING
2011 	case OP_FOLD:
2012 	    VIsual_reselect = FALSE;	/* don't reselect now */
2013 	    foldCreate(oap->start.lnum, oap->end.lnum);
2014 	    break;
2015 
2016 	case OP_FOLDOPEN:
2017 	case OP_FOLDOPENREC:
2018 	case OP_FOLDCLOSE:
2019 	case OP_FOLDCLOSEREC:
2020 	    VIsual_reselect = FALSE;	/* don't reselect now */
2021 	    opFoldRange(oap->start.lnum, oap->end.lnum,
2022 		    oap->op_type == OP_FOLDOPEN
2023 					    || oap->op_type == OP_FOLDOPENREC,
2024 		    oap->op_type == OP_FOLDOPENREC
2025 					  || oap->op_type == OP_FOLDCLOSEREC,
2026 					  oap->is_VIsual);
2027 	    break;
2028 
2029 	case OP_FOLDDEL:
2030 	case OP_FOLDDELREC:
2031 	    VIsual_reselect = FALSE;	/* don't reselect now */
2032 	    deleteFold(oap->start.lnum, oap->end.lnum,
2033 			       oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2034 	    break;
2035 #endif
2036 	default:
2037 	    clearopbeep(oap);
2038 	}
2039 #ifdef FEAT_VIRTUALEDIT
2040 	virtual_op = MAYBE;
2041 #endif
2042 	if (!gui_yank)
2043 	{
2044 	    /*
2045 	     * if 'sol' not set, go back to old column for some commands
2046 	     */
2047 	    if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2048 		    && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2049 						|| oap->op_type == OP_DELETE))
2050 		coladvance(curwin->w_curswant = old_col);
2051 	}
2052 	else
2053 	{
2054 	    curwin->w_cursor = old_cursor;
2055 	}
2056 #ifdef FEAT_VISUAL
2057 	oap->block_mode = FALSE;
2058 #endif
2059 	clearop(oap);
2060     }
2061 }
2062 
2063 /*
2064  * Handle indent and format operators and visual mode ":".
2065  */
2066     static void
2067 op_colon(oap)
2068     oparg_T	*oap;
2069 {
2070     stuffcharReadbuff(':');
2071 #ifdef FEAT_VISUAL
2072     if (oap->is_VIsual)
2073 	stuffReadbuff((char_u *)"'<,'>");
2074     else
2075 #endif
2076     {
2077 	/*
2078 	 * Make the range look nice, so it can be repeated.
2079 	 */
2080 	if (oap->start.lnum == curwin->w_cursor.lnum)
2081 	    stuffcharReadbuff('.');
2082 	else
2083 	    stuffnumReadbuff((long)oap->start.lnum);
2084 	if (oap->end.lnum != oap->start.lnum)
2085 	{
2086 	    stuffcharReadbuff(',');
2087 	    if (oap->end.lnum == curwin->w_cursor.lnum)
2088 		stuffcharReadbuff('.');
2089 	    else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2090 		stuffcharReadbuff('$');
2091 	    else if (oap->start.lnum == curwin->w_cursor.lnum)
2092 	    {
2093 		stuffReadbuff((char_u *)".+");
2094 		stuffnumReadbuff((long)oap->line_count - 1);
2095 	    }
2096 	    else
2097 		stuffnumReadbuff((long)oap->end.lnum);
2098 	}
2099     }
2100     if (oap->op_type != OP_COLON)
2101 	stuffReadbuff((char_u *)"!");
2102     if (oap->op_type == OP_INDENT)
2103     {
2104 #ifndef FEAT_CINDENT
2105 	if (*get_equalprg() == NUL)
2106 	    stuffReadbuff((char_u *)"indent");
2107 	else
2108 #endif
2109 	    stuffReadbuff(get_equalprg());
2110 	stuffReadbuff((char_u *)"\n");
2111     }
2112     else if (oap->op_type == OP_FORMAT)
2113     {
2114 	if (*p_fp == NUL)
2115 	    stuffReadbuff((char_u *)"fmt");
2116 	else
2117 	    stuffReadbuff(p_fp);
2118 	stuffReadbuff((char_u *)"\n']");
2119     }
2120 
2121     /*
2122      * do_cmdline() does the rest
2123      */
2124 }
2125 
2126 /*
2127  * Handle the "g@" operator: call 'operatorfunc'.
2128  */
2129 /*ARGSUSED*/
2130     static void
2131 op_function(oap)
2132     oparg_T	*oap;
2133 {
2134 #ifdef FEAT_EVAL
2135     char_u	*(argv[1]);
2136 
2137     if (*p_opfunc == NUL)
2138 	EMSG(_("E774: 'operatorfunc' is empty"));
2139     else
2140     {
2141 	/* Set '[ and '] marks to text to be operated on. */
2142 	curbuf->b_op_start = oap->start;
2143 	curbuf->b_op_end = oap->end;
2144 	if (oap->motion_type != MLINE && !oap->inclusive)
2145 	    /* Exclude the end position. */
2146 	    decl(&curbuf->b_op_end);
2147 
2148 	if (oap->block_mode)
2149 	    argv[0] = (char_u *)"block";
2150 	else if (oap->motion_type == MLINE)
2151 	    argv[0] = (char_u *)"line";
2152 	else
2153 	    argv[0] = (char_u *)"char";
2154 	(void)call_func_retnr(p_opfunc, 1, argv, FALSE);
2155     }
2156 #else
2157     EMSG(_("E775: Eval feature not available"));
2158 #endif
2159 }
2160 
2161 #if defined(FEAT_MOUSE) || defined(PROTO)
2162 /*
2163  * Do the appropriate action for the current mouse click in the current mode.
2164  * Not used for Command-line mode.
2165  *
2166  * Normal Mode:
2167  * event	 modi-	position      visual	   change   action
2168  *		 fier	cursor			   window
2169  * left press	  -	yes	    end		    yes
2170  * left press	  C	yes	    end		    yes	    "^]" (2)
2171  * left press	  S	yes	    end		    yes	    "*" (2)
2172  * left drag	  -	yes	start if moved	    no
2173  * left relse	  -	yes	start if moved	    no
2174  * middle press	  -	yes	 if not active	    no	    put register
2175  * middle press	  -	yes	 if active	    no	    yank and put
2176  * right press	  -	yes	start or extend	    yes
2177  * right press	  S	yes	no change	    yes	    "#" (2)
2178  * right drag	  -	yes	extend		    no
2179  * right relse	  -	yes	extend		    no
2180  *
2181  * Insert or Replace Mode:
2182  * event	 modi-	position      visual	   change   action
2183  *		 fier	cursor			   window
2184  * left press	  -	yes	(cannot be active)  yes
2185  * left press	  C	yes	(cannot be active)  yes	    "CTRL-O^]" (2)
2186  * left press	  S	yes	(cannot be active)  yes	    "CTRL-O*" (2)
2187  * left drag	  -	yes	start or extend (1) no	    CTRL-O (1)
2188  * left relse	  -	yes	start or extend (1) no	    CTRL-O (1)
2189  * middle press	  -	no	(cannot be active)  no	    put register
2190  * right press	  -	yes	start or extend	    yes	    CTRL-O
2191  * right press	  S	yes	(cannot be active)  yes	    "CTRL-O#" (2)
2192  *
2193  * (1) only if mouse pointer moved since press
2194  * (2) only if click is in same buffer
2195  *
2196  * Return TRUE if start_arrow() should be called for edit mode.
2197  */
2198     int
2199 do_mouse(oap, c, dir, count, fixindent)
2200     oparg_T	*oap;		/* operator argument, can be NULL */
2201     int		c;		/* K_LEFTMOUSE, etc */
2202     int		dir;		/* Direction to 'put' if necessary */
2203     long	count;
2204     int		fixindent;	/* PUT_FIXINDENT if fixing indent necessary */
2205 {
2206     static int	do_always = FALSE;	/* ignore 'mouse' setting next time */
2207     static int	got_click = FALSE;	/* got a click some time back */
2208 
2209     int		which_button;	/* MOUSE_LEFT, _MIDDLE or _RIGHT */
2210     int		is_click;	/* If FALSE it's a drag or release event */
2211     int		is_drag;	/* If TRUE it's a drag event */
2212     int		jump_flags = 0;	/* flags for jump_to_mouse() */
2213     pos_T	start_visual;
2214     int		moved;		/* Has cursor moved? */
2215     int		in_status_line;	/* mouse in status line */
2216 #ifdef FEAT_VERTSPLIT
2217     int		in_sep_line;	/* mouse in vertical separator line */
2218 #endif
2219     int		c1, c2;
2220 #if defined(FEAT_FOLDING)
2221     pos_T	save_cursor;
2222 #endif
2223     win_T	*old_curwin = curwin;
2224 #ifdef FEAT_VISUAL
2225     static pos_T orig_cursor;
2226     colnr_T	leftcol, rightcol;
2227     pos_T	end_visual;
2228     int		diff;
2229     int		old_active = VIsual_active;
2230     int		old_mode = VIsual_mode;
2231 #endif
2232     int		regname;
2233 
2234 #if defined(FEAT_FOLDING)
2235     save_cursor = curwin->w_cursor;
2236 #endif
2237 
2238     /*
2239      * When GUI is active, always recognize mouse events, otherwise:
2240      * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2241      * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2242      * - For command line and insert mode 'mouse' is checked before calling
2243      *	 do_mouse().
2244      */
2245     if (do_always)
2246 	do_always = FALSE;
2247     else
2248 #ifdef FEAT_GUI
2249 	if (!gui.in_use)
2250 #endif
2251 	{
2252 #ifdef FEAT_VISUAL
2253 	    if (VIsual_active)
2254 	    {
2255 		if (!mouse_has(MOUSE_VISUAL))
2256 		    return FALSE;
2257 	    }
2258 	    else
2259 #endif
2260 		if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2261 		return FALSE;
2262 	}
2263 
2264     which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2265 
2266 #ifdef FEAT_MOUSESHAPE
2267     /* May have stopped dragging the status or separator line.  The pointer is
2268      * most likely still on the status or separator line. */
2269     if (!is_drag && drag_status_line)
2270     {
2271 	drag_status_line = FALSE;
2272 	update_mouseshape(SHAPE_IDX_STATUS);
2273     }
2274 # ifdef FEAT_VERTSPLIT
2275     if (!is_drag && drag_sep_line)
2276     {
2277 	drag_sep_line = FALSE;
2278 	update_mouseshape(SHAPE_IDX_VSEP);
2279     }
2280 # endif
2281 #endif
2282 
2283     /*
2284      * Ignore drag and release events if we didn't get a click.
2285      */
2286     if (is_click)
2287 	got_click = TRUE;
2288     else
2289     {
2290 	if (!got_click)			/* didn't get click, ignore */
2291 	    return FALSE;
2292 	if (!is_drag)			/* release, reset got_click */
2293 	    got_click = FALSE;
2294     }
2295 
2296 #ifndef FEAT_VISUAL
2297     /*
2298      * ALT is only used for starging/extending Visual mode.
2299      */
2300     if ((mod_mask & MOD_MASK_ALT))
2301 	return FALSE;
2302 #endif
2303 
2304     /*
2305      * CTRL right mouse button does CTRL-T
2306      */
2307     if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2308     {
2309 	if (State & INSERT)
2310 	    stuffcharReadbuff(Ctrl_O);
2311 	if (count > 1)
2312 	    stuffnumReadbuff(count);
2313 	stuffcharReadbuff(Ctrl_T);
2314 	got_click = FALSE;		/* ignore drag&release now */
2315 	return FALSE;
2316     }
2317 
2318     /*
2319      * CTRL only works with left mouse button
2320      */
2321     if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2322 	return FALSE;
2323 
2324     /*
2325      * When a modifier is down, ignore drag and release events, as well as
2326      * multiple clicks and the middle mouse button.
2327      * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2328      */
2329     if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2330 							     | MOD_MASK_META))
2331 	    && (!is_click
2332 		|| (mod_mask & MOD_MASK_MULTI_CLICK)
2333 		|| which_button == MOUSE_MIDDLE)
2334 	    && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
2335 		&& mouse_model_popup()
2336 		&& which_button == MOUSE_LEFT)
2337 	    && !((mod_mask & MOD_MASK_ALT)
2338 		&& !mouse_model_popup()
2339 		&& which_button == MOUSE_RIGHT)
2340 	    )
2341 	return FALSE;
2342 
2343     /*
2344      * If the button press was used as the movement command for an operator
2345      * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2346      * drag/release events.
2347      */
2348     if (!is_click && which_button == MOUSE_MIDDLE)
2349 	return FALSE;
2350 
2351     if (oap != NULL)
2352 	regname = oap->regname;
2353     else
2354 	regname = 0;
2355 
2356     /*
2357      * Middle mouse button does a 'put' of the selected text
2358      */
2359     if (which_button == MOUSE_MIDDLE)
2360     {
2361 	if (State == NORMAL)
2362 	{
2363 	    /*
2364 	     * If an operator was pending, we don't know what the user wanted
2365 	     * to do. Go back to normal mode: Clear the operator and beep().
2366 	     */
2367 	    if (oap != NULL && oap->op_type != OP_NOP)
2368 	    {
2369 		clearopbeep(oap);
2370 		return FALSE;
2371 	    }
2372 
2373 #ifdef FEAT_VISUAL
2374 	    /*
2375 	     * If visual was active, yank the highlighted text and put it
2376 	     * before the mouse pointer position.
2377 	     */
2378 	    if (VIsual_active)
2379 	    {
2380 		stuffcharReadbuff('y');
2381 		stuffcharReadbuff(K_MIDDLEMOUSE);
2382 		do_always = TRUE;	/* ignore 'mouse' setting next time */
2383 		return FALSE;
2384 	    }
2385 #endif
2386 	    /*
2387 	     * The rest is below jump_to_mouse()
2388 	     */
2389 	}
2390 
2391 	else if ((State & INSERT) == 0)
2392 	    return FALSE;
2393 
2394 	/*
2395 	 * Middle click in insert mode doesn't move the mouse, just insert the
2396 	 * contents of a register.  '.' register is special, can't insert that
2397 	 * with do_put().
2398 	 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2399 	 * happens for the GUI).
2400 	 */
2401 	if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2402 	{
2403 	    if (regname == '.')
2404 		insert_reg(regname, TRUE);
2405 	    else
2406 	    {
2407 #ifdef FEAT_CLIPBOARD
2408 		if (clip_star.available && regname == 0)
2409 		    regname = '*';
2410 #endif
2411 		if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2412 		    insert_reg(regname, TRUE);
2413 		else
2414 		{
2415 		    do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2416 
2417 		    /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2418 		    AppendCharToRedobuff(Ctrl_R);
2419 		    AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2420 		    AppendCharToRedobuff(regname == 0 ? '"' : regname);
2421 		}
2422 	    }
2423 	    return FALSE;
2424 	}
2425     }
2426 
2427     /* When dragging or button-up stay in the same window. */
2428     if (!is_click)
2429 	jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2430 
2431     start_visual.lnum = 0;
2432 
2433 #ifdef FEAT_WINDOWS
2434     /* Check for clicking in the tab page line. */
2435     if (mouse_row == 0 && firstwin->w_winrow > 0)
2436     {
2437 	got_click = FALSE;	/* ignore mouse-up and drag events */
2438 
2439 	/* click in a tab selects that tab page */
2440 	if (is_click
2441 # ifdef FEAT_CMDWIN
2442 		&& cmdwin_type == 0
2443 # endif
2444 		&& mouse_col < Columns)
2445 	{
2446 	    c1 = TabPageIdxs[mouse_col];
2447 	    if (c1 >= 0)
2448 	    {
2449 		if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2450 		{
2451 		    /* double click opens new page */
2452 		    end_visual_mode();
2453 		    tabpage_new();
2454 		    tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2455 		}
2456 		else
2457 		{
2458 		    /* Go to specified tab page, or next one if not clicking
2459 		     * on a label. */
2460 		    goto_tabpage(c1);
2461 
2462 		    /* It's like clicking on the status line of a window. */
2463 		    if (curwin != old_curwin)
2464 			end_visual_mode();
2465 		}
2466 	    }
2467 	    else if (c1 < 0)
2468 	    {
2469 		tabpage_T	*tp;
2470 
2471 		/* Close the current or specified tab page. */
2472 		if (c1 == -999)
2473 		    tp = curtab;
2474 		else
2475 		    tp = find_tabpage(-c1);
2476 		if (tp == curtab)
2477 		{
2478 		    if (first_tabpage->tp_next != NULL)
2479 			tabpage_close(FALSE);
2480 		}
2481 		else if (tp != NULL)
2482 		    tabpage_close_other(tp, FALSE);
2483 	    }
2484 	}
2485 	return TRUE;
2486     }
2487 #endif
2488 
2489     /*
2490      * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2491      * right button up   -> pop-up menu
2492      * shift-left button -> right button
2493      * alt-left button   -> alt-right button
2494      */
2495     if (mouse_model_popup())
2496     {
2497 	if (which_button == MOUSE_RIGHT
2498 			    && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2499 	{
2500 	    /*
2501 	     * NOTE: Ignore right button down and drag mouse events.
2502 	     * Windows only shows the popup menu on the button up event.
2503 	     */
2504 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON)
2505 	    if (!is_click)
2506 		return FALSE;
2507 #endif
2508 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2509 	    if (is_click || is_drag)
2510 		return FALSE;
2511 #endif
2512 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2513 	    || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2514 	    || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2515 	    if (gui.in_use)
2516 	    {
2517 		jump_flags = 0;
2518 		if (STRCMP(p_mousem, "popup_setpos") == 0)
2519 		{
2520 		    /* First set the cursor position before showing the popup
2521 		     * menu. */
2522 #ifdef FEAT_VISUAL
2523 		    if (VIsual_active)
2524 		    {
2525 			pos_T    m_pos;
2526 
2527 			/*
2528 			 * set MOUSE_MAY_STOP_VIS if we are outside the
2529 			 * selection or the current window (might have false
2530 			 * negative here)
2531 			 */
2532 			if (mouse_row < W_WINROW(curwin)
2533 			     || mouse_row
2534 				      > (W_WINROW(curwin) + curwin->w_height))
2535 			    jump_flags = MOUSE_MAY_STOP_VIS;
2536 			else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2537 			    jump_flags = MOUSE_MAY_STOP_VIS;
2538 			else
2539 			{
2540 			    if ((lt(curwin->w_cursor, VIsual)
2541 					&& (lt(m_pos, curwin->w_cursor)
2542 					    || lt(VIsual, m_pos)))
2543 				    || (lt(VIsual, curwin->w_cursor)
2544 					&& (lt(m_pos, VIsual)
2545 					    || lt(curwin->w_cursor, m_pos))))
2546 			    {
2547 				jump_flags = MOUSE_MAY_STOP_VIS;
2548 			    }
2549 			    else if (VIsual_mode == Ctrl_V)
2550 			    {
2551 				getvcols(curwin, &curwin->w_cursor, &VIsual,
2552 							 &leftcol, &rightcol);
2553 				getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2554 				if (m_pos.col < leftcol || m_pos.col > rightcol)
2555 				    jump_flags = MOUSE_MAY_STOP_VIS;
2556 			    }
2557 			}
2558 		    }
2559 		    else
2560 			jump_flags = MOUSE_MAY_STOP_VIS;
2561 #endif
2562 		}
2563 		if (jump_flags)
2564 		{
2565 		    jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2566 		    update_curbuf(
2567 #ifdef FEAT_VISUAL
2568 			    VIsual_active ? INVERTED :
2569 #endif
2570 			    VALID);
2571 		    setcursor();
2572 		    out_flush();    /* Update before showing popup menu */
2573 		}
2574 # ifdef FEAT_MENU
2575 		gui_show_popupmenu();
2576 # endif
2577 		return (jump_flags & CURSOR_MOVED) != 0;
2578 	    }
2579 	    else
2580 		return FALSE;
2581 #else
2582 	    return FALSE;
2583 #endif
2584 	}
2585 	if (which_button == MOUSE_LEFT
2586 				&& (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
2587 	{
2588 	    which_button = MOUSE_RIGHT;
2589 	    mod_mask &= ~MOD_MASK_SHIFT;
2590 	}
2591     }
2592 
2593 #ifdef FEAT_VISUAL
2594     if ((State & (NORMAL | INSERT))
2595 			    && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2596     {
2597 	if (which_button == MOUSE_LEFT)
2598 	{
2599 	    if (is_click)
2600 	    {
2601 		/* stop Visual mode for a left click in a window, but not when
2602 		 * on a status line */
2603 		if (VIsual_active)
2604 		    jump_flags |= MOUSE_MAY_STOP_VIS;
2605 	    }
2606 	    else if (mouse_has(MOUSE_VISUAL))
2607 		jump_flags |= MOUSE_MAY_VIS;
2608 	}
2609 	else if (which_button == MOUSE_RIGHT)
2610 	{
2611 	    if (is_click && VIsual_active)
2612 	    {
2613 		/*
2614 		 * Remember the start and end of visual before moving the
2615 		 * cursor.
2616 		 */
2617 		if (lt(curwin->w_cursor, VIsual))
2618 		{
2619 		    start_visual = curwin->w_cursor;
2620 		    end_visual = VIsual;
2621 		}
2622 		else
2623 		{
2624 		    start_visual = VIsual;
2625 		    end_visual = curwin->w_cursor;
2626 		}
2627 	    }
2628 	    jump_flags |= MOUSE_FOCUS;
2629 	    if (mouse_has(MOUSE_VISUAL))
2630 		jump_flags |= MOUSE_MAY_VIS;
2631 	}
2632     }
2633 #endif
2634 
2635     /*
2636      * If an operator is pending, ignore all drags and releases until the
2637      * next mouse click.
2638      */
2639     if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2640     {
2641 	got_click = FALSE;
2642 	oap->motion_type = MCHAR;
2643     }
2644 
2645     /* When releasing the button let jump_to_mouse() know. */
2646     if (!is_click && !is_drag)
2647 	jump_flags |= MOUSE_RELEASED;
2648 
2649     /*
2650      * JUMP!
2651      */
2652     jump_flags = jump_to_mouse(jump_flags,
2653 			oap == NULL ? NULL : &(oap->inclusive), which_button);
2654     moved = (jump_flags & CURSOR_MOVED);
2655     in_status_line = (jump_flags & IN_STATUS_LINE);
2656 #ifdef FEAT_VERTSPLIT
2657     in_sep_line = (jump_flags & IN_SEP_LINE);
2658 #endif
2659 
2660 #ifdef FEAT_NETBEANS_INTG
2661     if (usingNetbeans && isNetbeansBuffer(curbuf)
2662 			    && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2663     {
2664 	int key = KEY2TERMCAP1(c);
2665 
2666 	if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2667 					       || key == (int)KE_RIGHTRELEASE)
2668 	    netbeans_button_release(which_button);
2669     }
2670 #endif
2671 
2672     /* When jumping to another window, clear a pending operator.  That's a bit
2673      * friendlier than beeping and not jumping to that window. */
2674     if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2675 	clearop(oap);
2676 
2677 #ifdef FEAT_FOLDING
2678     if (mod_mask == 0
2679 	    && !is_drag
2680 	    && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2681 	    && which_button == MOUSE_LEFT)
2682     {
2683 	/* open or close a fold at this line */
2684 	if (jump_flags & MOUSE_FOLD_OPEN)
2685 	    openFold(curwin->w_cursor.lnum, 1L);
2686 	else
2687 	    closeFold(curwin->w_cursor.lnum, 1L);
2688 	/* don't move the cursor if still in the same window */
2689 	if (curwin == old_curwin)
2690 	    curwin->w_cursor = save_cursor;
2691     }
2692 #endif
2693 
2694 #if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2695     if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2696     {
2697 	clip_modeless(which_button, is_click, is_drag);
2698 	return FALSE;
2699     }
2700 #endif
2701 
2702 #ifdef FEAT_VISUAL
2703     /* Set global flag that we are extending the Visual area with mouse
2704      * dragging; temporarily mimimize 'scrolloff'. */
2705     if (VIsual_active && is_drag && p_so)
2706     {
2707 	/* In the very first line, allow scrolling one line */
2708 	if (mouse_row == 0)
2709 	    mouse_dragging = 2;
2710 	else
2711 	    mouse_dragging = 1;
2712     }
2713 
2714     /* When dragging the mouse above the window, scroll down. */
2715     if (is_drag && mouse_row < 0 && !in_status_line)
2716     {
2717 	scroll_redraw(FALSE, 1L);
2718 	mouse_row = 0;
2719     }
2720 
2721     if (start_visual.lnum)		/* right click in visual mode */
2722     {
2723        /* When ALT is pressed make Visual mode blockwise. */
2724        if (mod_mask & MOD_MASK_ALT)
2725 	   VIsual_mode = Ctrl_V;
2726 
2727 	/*
2728 	 * In Visual-block mode, divide the area in four, pick up the corner
2729 	 * that is in the quarter that the cursor is in.
2730 	 */
2731 	if (VIsual_mode == Ctrl_V)
2732 	{
2733 	    getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2734 	    if (curwin->w_curswant > (leftcol + rightcol) / 2)
2735 		end_visual.col = leftcol;
2736 	    else
2737 		end_visual.col = rightcol;
2738 	    if (curwin->w_cursor.lnum <
2739 				    (start_visual.lnum + end_visual.lnum) / 2)
2740 		end_visual.lnum = end_visual.lnum;
2741 	    else
2742 		end_visual.lnum = start_visual.lnum;
2743 
2744 	    /* move VIsual to the right column */
2745 	    start_visual = curwin->w_cursor;	    /* save the cursor pos */
2746 	    curwin->w_cursor = end_visual;
2747 	    coladvance(end_visual.col);
2748 	    VIsual = curwin->w_cursor;
2749 	    curwin->w_cursor = start_visual;	    /* restore the cursor */
2750 	}
2751 	else
2752 	{
2753 	    /*
2754 	     * If the click is before the start of visual, change the start.
2755 	     * If the click is after the end of visual, change the end.  If
2756 	     * the click is inside the visual, change the closest side.
2757 	     */
2758 	    if (lt(curwin->w_cursor, start_visual))
2759 		VIsual = end_visual;
2760 	    else if (lt(end_visual, curwin->w_cursor))
2761 		VIsual = start_visual;
2762 	    else
2763 	    {
2764 		/* In the same line, compare column number */
2765 		if (end_visual.lnum == start_visual.lnum)
2766 		{
2767 		    if (curwin->w_cursor.col - start_visual.col >
2768 				    end_visual.col - curwin->w_cursor.col)
2769 			VIsual = start_visual;
2770 		    else
2771 			VIsual = end_visual;
2772 		}
2773 
2774 		/* In different lines, compare line number */
2775 		else
2776 		{
2777 		    diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2778 				(end_visual.lnum - curwin->w_cursor.lnum);
2779 
2780 		    if (diff > 0)		/* closest to end */
2781 			VIsual = start_visual;
2782 		    else if (diff < 0)	/* closest to start */
2783 			VIsual = end_visual;
2784 		    else			/* in the middle line */
2785 		    {
2786 			if (curwin->w_cursor.col <
2787 					(start_visual.col + end_visual.col) / 2)
2788 			    VIsual = end_visual;
2789 			else
2790 			    VIsual = start_visual;
2791 		    }
2792 		}
2793 	    }
2794 	}
2795     }
2796     /*
2797      * If Visual mode started in insert mode, execute "CTRL-O"
2798      */
2799     else if ((State & INSERT) && VIsual_active)
2800 	stuffcharReadbuff(Ctrl_O);
2801 #endif
2802 
2803     /*
2804      * Middle mouse click: Put text before cursor.
2805      */
2806     if (which_button == MOUSE_MIDDLE)
2807     {
2808 #ifdef FEAT_CLIPBOARD
2809 	if (clip_star.available && regname == 0)
2810 	    regname = '*';
2811 #endif
2812 	if (yank_register_mline(regname))
2813 	{
2814 	    if (mouse_past_bottom)
2815 		dir = FORWARD;
2816 	}
2817 	else if (mouse_past_eol)
2818 	    dir = FORWARD;
2819 
2820 	if (fixindent)
2821 	{
2822 	    c1 = (dir == BACKWARD) ? '[' : ']';
2823 	    c2 = 'p';
2824 	}
2825 	else
2826 	{
2827 	    c1 = (dir == FORWARD) ? 'p' : 'P';
2828 	    c2 = NUL;
2829 	}
2830 	prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2831 
2832 	/*
2833 	 * Remember where the paste started, so in edit() Insstart can be set
2834 	 * to this position
2835 	 */
2836 	if (restart_edit != 0)
2837 	    where_paste_started = curwin->w_cursor;
2838 	do_put(regname, dir, count, fixindent | PUT_CURSEND);
2839     }
2840 
2841 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2842     /*
2843      * Ctrl-Mouse click or double click in a quickfix window jumps to the
2844      * error under the mouse pointer.
2845      */
2846     else if (((mod_mask & MOD_MASK_CTRL)
2847 		|| (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2848 	    && bt_quickfix(curbuf))
2849     {
2850 	if (State & INSERT)
2851 	    stuffcharReadbuff(Ctrl_O);
2852 	if (curwin->w_llist_ref == NULL)	/* quickfix window */
2853 	    stuffReadbuff((char_u *)":.cc\n");
2854 	else					/* location list window */
2855 	    stuffReadbuff((char_u *)":.ll\n");
2856 	got_click = FALSE;		/* ignore drag&release now */
2857     }
2858 #endif
2859 
2860     /*
2861      * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2862      * under the mouse pointer.
2863      */
2864     else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2865 		     && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2866     {
2867 	if (State & INSERT)
2868 	    stuffcharReadbuff(Ctrl_O);
2869 	stuffcharReadbuff(Ctrl_RSB);
2870 	got_click = FALSE;		/* ignore drag&release now */
2871     }
2872 
2873     /*
2874      * Shift-Mouse click searches for the next occurrence of the word under
2875      * the mouse pointer
2876      */
2877     else if ((mod_mask & MOD_MASK_SHIFT))
2878     {
2879 	if (State & INSERT
2880 #ifdef FEAT_VISUAL
2881 		|| (VIsual_active && VIsual_select)
2882 #endif
2883 		)
2884 	    stuffcharReadbuff(Ctrl_O);
2885 	if (which_button == MOUSE_LEFT)
2886 	    stuffcharReadbuff('*');
2887 	else	/* MOUSE_RIGHT */
2888 	    stuffcharReadbuff('#');
2889     }
2890 
2891     /* Handle double clicks, unless on status line */
2892     else if (in_status_line)
2893     {
2894 #ifdef FEAT_MOUSESHAPE
2895 	if ((is_drag || is_click) && !drag_status_line)
2896 	{
2897 	    drag_status_line = TRUE;
2898 	    update_mouseshape(-1);
2899 	}
2900 #endif
2901     }
2902 #ifdef FEAT_VERTSPLIT
2903     else if (in_sep_line)
2904     {
2905 # ifdef FEAT_MOUSESHAPE
2906 	if ((is_drag || is_click) && !drag_sep_line)
2907 	{
2908 	    drag_sep_line = TRUE;
2909 	    update_mouseshape(-1);
2910 	}
2911 # endif
2912     }
2913 #endif
2914 #ifdef FEAT_VISUAL
2915     else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
2916 	     && mouse_has(MOUSE_VISUAL))
2917     {
2918 	if (is_click || !VIsual_active)
2919 	{
2920 	    if (VIsual_active)
2921 		orig_cursor = VIsual;
2922 	    else
2923 	    {
2924 		check_visual_highlight();
2925 		VIsual = curwin->w_cursor;
2926 		orig_cursor = VIsual;
2927 		VIsual_active = TRUE;
2928 		VIsual_reselect = TRUE;
2929 		/* start Select mode if 'selectmode' contains "mouse" */
2930 		may_start_select('o');
2931 		setmouse();
2932 	    }
2933 	    if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2934 	    {
2935 		/* Double click with ALT pressed makes it blockwise. */
2936 		if (mod_mask & MOD_MASK_ALT)
2937 		    VIsual_mode = Ctrl_V;
2938 		else
2939 		    VIsual_mode = 'v';
2940 	    }
2941 	    else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
2942 		VIsual_mode = 'V';
2943 	    else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
2944 		VIsual_mode = Ctrl_V;
2945 #ifdef FEAT_CLIPBOARD
2946 	    /* Make sure the clipboard gets updated.  Needed because start and
2947 	     * end may still be the same, and the selection needs to be owned */
2948 	    clip_star.vmode = NUL;
2949 #endif
2950 	}
2951 	/*
2952 	 * A double click selects a word or a block.
2953 	 */
2954 	if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2955 	{
2956 	    pos_T	*pos = NULL;
2957 	    int		gc;
2958 
2959 	    if (is_click)
2960 	    {
2961 		/* If the character under the cursor (skipping white space) is
2962 		 * not a word character, try finding a match and select a (),
2963 		 * {}, [], #if/#endif, etc. block. */
2964 		end_visual = curwin->w_cursor;
2965 		while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
2966 		    inc(&end_visual);
2967 		if (oap != NULL)
2968 		    oap->motion_type = MCHAR;
2969 		if (oap != NULL
2970 			&& VIsual_mode == 'v'
2971 			&& !vim_iswordc(gchar_pos(&end_visual))
2972 			&& equalpos(curwin->w_cursor, VIsual)
2973 			&& (pos = findmatch(oap, NUL)) != NULL)
2974 		{
2975 		    curwin->w_cursor = *pos;
2976 		    if (oap->motion_type == MLINE)
2977 			VIsual_mode = 'V';
2978 		    else if (*p_sel == 'e')
2979 		    {
2980 			if (lt(curwin->w_cursor, VIsual))
2981 			    ++VIsual.col;
2982 			else
2983 			    ++curwin->w_cursor.col;
2984 		    }
2985 		}
2986 	    }
2987 
2988 	    if (pos == NULL && (is_click || is_drag))
2989 	    {
2990 		/* When not found a match or when dragging: extend to include
2991 		 * a word. */
2992 		if (lt(curwin->w_cursor, orig_cursor))
2993 		{
2994 		    find_start_of_word(&curwin->w_cursor);
2995 		    find_end_of_word(&VIsual);
2996 		}
2997 		else
2998 		{
2999 		    find_start_of_word(&VIsual);
3000 		    if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3001 #ifdef FEAT_MBYTE
3002 			curwin->w_cursor.col +=
3003 					 (*mb_ptr2len)(ml_get_cursor());
3004 #else
3005 			++curwin->w_cursor.col;
3006 #endif
3007 		    find_end_of_word(&curwin->w_cursor);
3008 		}
3009 	    }
3010 	    curwin->w_set_curswant = TRUE;
3011 	}
3012 	if (is_click)
3013 	    redraw_curbuf_later(INVERTED);	/* update the inversion */
3014     }
3015     else if (VIsual_active && !old_active)
3016     {
3017 	if (mod_mask & MOD_MASK_ALT)
3018 	    VIsual_mode = Ctrl_V;
3019 	else
3020 	    VIsual_mode = 'v';
3021     }
3022 
3023     /* If Visual mode changed show it later. */
3024     if ((!VIsual_active && old_active && mode_displayed)
3025 	    || (VIsual_active && p_smd && msg_silent == 0
3026 				 && (!old_active || VIsual_mode != old_mode)))
3027 	redraw_cmdline = TRUE;
3028 #endif
3029 
3030     return moved;
3031 }
3032 
3033 #ifdef FEAT_VISUAL
3034 /*
3035  * Move "pos" back to the start of the word it's in.
3036  */
3037     static void
3038 find_start_of_word(pos)
3039     pos_T	*pos;
3040 {
3041     char_u	*line;
3042     int		cclass;
3043     int		col;
3044 
3045     line = ml_get(pos->lnum);
3046     cclass = get_mouse_class(line + pos->col);
3047 
3048     while (pos->col > 0)
3049     {
3050 	col = pos->col - 1;
3051 #ifdef FEAT_MBYTE
3052 	col -= (*mb_head_off)(line, line + col);
3053 #endif
3054 	if (get_mouse_class(line + col) != cclass)
3055 	    break;
3056 	pos->col = col;
3057     }
3058 }
3059 
3060 /*
3061  * Move "pos" forward to the end of the word it's in.
3062  * When 'selection' is "exclusive", the position is just after the word.
3063  */
3064     static void
3065 find_end_of_word(pos)
3066     pos_T	*pos;
3067 {
3068     char_u	*line;
3069     int		cclass;
3070     int		col;
3071 
3072     line = ml_get(pos->lnum);
3073     if (*p_sel == 'e' && pos->col > 0)
3074     {
3075 	--pos->col;
3076 #ifdef FEAT_MBYTE
3077 	pos->col -= (*mb_head_off)(line, line + pos->col);
3078 #endif
3079     }
3080     cclass = get_mouse_class(line + pos->col);
3081     while (line[pos->col] != NUL)
3082     {
3083 #ifdef FEAT_MBYTE
3084 	col = pos->col + (*mb_ptr2len)(line + pos->col);
3085 #else
3086 	col = pos->col + 1;
3087 #endif
3088 	if (get_mouse_class(line + col) != cclass)
3089 	{
3090 	    if (*p_sel == 'e')
3091 		pos->col = col;
3092 	    break;
3093 	}
3094 	pos->col = col;
3095     }
3096 }
3097 
3098 /*
3099  * Get class of a character for selection: same class means same word.
3100  * 0: blank
3101  * 1: punctuation groups
3102  * 2: normal word character
3103  * >2: multi-byte word character.
3104  */
3105     static int
3106 get_mouse_class(p)
3107     char_u	*p;
3108 {
3109     int		c;
3110 
3111 #ifdef FEAT_MBYTE
3112     if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3113 	return mb_get_class(p);
3114 #endif
3115 
3116     c = *p;
3117     if (c == ' ' || c == '\t')
3118 	return 0;
3119 
3120     if (vim_iswordc(c))
3121 	return 2;
3122 
3123     /*
3124      * There are a few special cases where we want certain combinations of
3125      * characters to be considered as a single word.  These are things like
3126      * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc.  Otherwise, each
3127      * character is in it's own class.
3128      */
3129     if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3130 	return 1;
3131     return c;
3132 }
3133 #endif /* FEAT_VISUAL */
3134 #endif /* FEAT_MOUSE */
3135 
3136 #if defined(FEAT_VISUAL) || defined(PROTO)
3137 /*
3138  * Check if  highlighting for visual mode is possible, give a warning message
3139  * if not.
3140  */
3141     void
3142 check_visual_highlight()
3143 {
3144     static int	    did_check = FALSE;
3145 
3146     if (full_screen)
3147     {
3148 	if (!did_check && hl_attr(HLF_V) == 0)
3149 	    MSG(_("Warning: terminal cannot highlight"));
3150 	did_check = TRUE;
3151     }
3152 }
3153 
3154 /*
3155  * End Visual mode.
3156  * This function should ALWAYS be called to end Visual mode, except from
3157  * do_pending_operator().
3158  */
3159     void
3160 end_visual_mode()
3161 {
3162 #ifdef FEAT_CLIPBOARD
3163     /*
3164      * If we are using the clipboard, then remember what was selected in case
3165      * we need to paste it somewhere while we still own the selection.
3166      * Only do this when the clipboard is already owned.  Don't want to grab
3167      * the selection when hitting ESC.
3168      */
3169     if (clip_star.available && clip_star.owned)
3170 	clip_auto_select();
3171 #endif
3172 
3173     VIsual_active = FALSE;
3174 #ifdef FEAT_MOUSE
3175     setmouse();
3176     mouse_dragging = 0;
3177 #endif
3178 
3179     /* Save the current VIsual area for '< and '> marks, and "gv" */
3180     curbuf->b_visual.vi_mode = VIsual_mode;
3181     curbuf->b_visual.vi_start = VIsual;
3182     curbuf->b_visual.vi_end = curwin->w_cursor;
3183     curbuf->b_visual.vi_curswant = curwin->w_curswant;
3184 #ifdef FEAT_EVAL
3185     curbuf->b_visual_mode_eval = VIsual_mode;
3186 #endif
3187 #ifdef FEAT_VIRTUALEDIT
3188     if (!virtual_active())
3189 	curwin->w_cursor.coladd = 0;
3190 #endif
3191 
3192     if (mode_displayed)
3193 	clear_cmdline = TRUE;		/* unshow visual mode later */
3194 #ifdef FEAT_CMDL_INFO
3195     else
3196 	clear_showcmd();
3197 #endif
3198 
3199     /* Don't leave the cursor past the end of the line */
3200     if (curwin->w_cursor.col > 0 && *ml_get_cursor() == NUL)
3201 	--curwin->w_cursor.col;
3202 }
3203 
3204 /*
3205  * Reset VIsual_active and VIsual_reselect.
3206  */
3207     void
3208 reset_VIsual_and_resel()
3209 {
3210     if (VIsual_active)
3211     {
3212 	end_visual_mode();
3213 	redraw_curbuf_later(INVERTED);	/* delete the inversion later */
3214     }
3215     VIsual_reselect = FALSE;
3216 }
3217 
3218 /*
3219  * Reset VIsual_active and VIsual_reselect if it's set.
3220  */
3221     void
3222 reset_VIsual()
3223 {
3224     if (VIsual_active)
3225     {
3226 	end_visual_mode();
3227 	redraw_curbuf_later(INVERTED);	/* delete the inversion later */
3228 	VIsual_reselect = FALSE;
3229     }
3230 }
3231 #endif /* FEAT_VISUAL */
3232 
3233 #if defined(FEAT_BEVAL)
3234 static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3235 
3236 /*
3237  * Check for a balloon-eval special item to include when searching for an
3238  * identifier.  When "dir" is BACKWARD "ptr[-1]" must be valid!
3239  * Returns TRUE if the character at "*ptr" should be included.
3240  * "dir" is FORWARD or BACKWARD, the direction of searching.
3241  * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3242  * "bnp" points to a counter for square brackets.
3243  */
3244     static int
3245 find_is_eval_item(ptr, colp, bnp, dir)
3246     char_u	*ptr;
3247     int		*colp;
3248     int		*bnp;
3249     int		dir;
3250 {
3251     /* Accept everything inside []. */
3252     if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3253 	++*bnp;
3254     if (*bnp > 0)
3255     {
3256 	if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3257 	    --*bnp;
3258 	return TRUE;
3259     }
3260 
3261     /* skip over "s.var" */
3262     if (*ptr == '.')
3263 	return TRUE;
3264 
3265     /* two-character item: s->var */
3266     if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3267 	    && ptr[dir == BACKWARD ? -1 : 0] == '-')
3268     {
3269 	*colp += dir;
3270 	return TRUE;
3271     }
3272     return FALSE;
3273 }
3274 #endif
3275 
3276 /*
3277  * Find the identifier under or to the right of the cursor.
3278  * "find_type" can have one of three values:
3279  * FIND_IDENT:   find an identifier (keyword)
3280  * FIND_STRING:  find any non-white string
3281  * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
3282  * FIND_EVAL:	 find text useful for C program debugging
3283  *
3284  * There are three steps:
3285  * 1. Search forward for the start of an identifier/string.  Doesn't move if
3286  *    already on one.
3287  * 2. Search backward for the start of this identifier/string.
3288  *    This doesn't match the real Vi but I like it a little better and it
3289  *    shouldn't bother anyone.
3290  * 3. Search forward to the end of this identifier/string.
3291  *    When FIND_IDENT isn't defined, we backup until a blank.
3292  *
3293  * Returns the length of the string, or zero if no string is found.
3294  * If a string is found, a pointer to the string is put in "*string".  This
3295  * string is not always NUL terminated.
3296  */
3297     int
3298 find_ident_under_cursor(string, find_type)
3299     char_u	**string;
3300     int		find_type;
3301 {
3302     return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3303 				     curwin->w_cursor.col, string, find_type);
3304 }
3305 
3306 /*
3307  * Like find_ident_under_cursor(), but for any window and any position.
3308  * However: Uses 'iskeyword' from the current window!.
3309  */
3310     int
3311 find_ident_at_pos(wp, lnum, startcol, string, find_type)
3312     win_T	*wp;
3313     linenr_T	lnum;
3314     colnr_T	startcol;
3315     char_u	**string;
3316     int		find_type;
3317 {
3318     char_u	*ptr;
3319     int		col = 0;	    /* init to shut up GCC */
3320     int		i;
3321 #ifdef FEAT_MBYTE
3322     int		this_class = 0;
3323     int		prev_class;
3324     int		prevcol;
3325 #endif
3326 #if defined(FEAT_BEVAL)
3327     int		bn = 0;	    /* bracket nesting */
3328 #endif
3329 
3330     /*
3331      * if i == 0: try to find an identifier
3332      * if i == 1: try to find any non-white string
3333      */
3334     ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3335     for (i = (find_type & FIND_IDENT) ? 0 : 1;	i < 2; ++i)
3336     {
3337 	/*
3338 	 * 1. skip to start of identifier/string
3339 	 */
3340 	col = startcol;
3341 #ifdef FEAT_MBYTE
3342 	if (has_mbyte)
3343 	{
3344 	    while (ptr[col] != NUL)
3345 	    {
3346 # if defined(FEAT_BEVAL)
3347 		/* Stop at a ']' to evaluate "a[x]". */
3348 		if ((find_type & FIND_EVAL) && ptr[col] == ']')
3349 		    break;
3350 # endif
3351 		this_class = mb_get_class(ptr + col);
3352 		if (this_class != 0 && (i == 1 || this_class != 1))
3353 		    break;
3354 		col += (*mb_ptr2len)(ptr + col);
3355 	    }
3356 	}
3357 	else
3358 #endif
3359 	    while (ptr[col] != NUL
3360 		    && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
3361 # if defined(FEAT_BEVAL)
3362 		    && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3363 # endif
3364 		    )
3365 		++col;
3366 
3367 #if defined(FEAT_BEVAL)
3368 	/* When starting on a ']' count it, so that we include the '['. */
3369 	bn = ptr[col] == ']';
3370 #endif
3371 
3372 	/*
3373 	 * 2. Back up to start of identifier/string.
3374 	 */
3375 #ifdef FEAT_MBYTE
3376 	if (has_mbyte)
3377 	{
3378 	    /* Remember class of character under cursor. */
3379 # if defined(FEAT_BEVAL)
3380 	    if ((find_type & FIND_EVAL) && ptr[col] == ']')
3381 		this_class = mb_get_class((char_u *)"a");
3382 	    else
3383 # endif
3384 		this_class = mb_get_class(ptr + col);
3385 	    while (col > 0)
3386 	    {
3387 		prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3388 		prev_class = mb_get_class(ptr + prevcol);
3389 		if (this_class != prev_class
3390 			&& (i == 0
3391 			    || prev_class == 0
3392 			    || (find_type & FIND_IDENT))
3393 # if defined(FEAT_BEVAL)
3394 			&& (!(find_type & FIND_EVAL)
3395 			    || prevcol == 0
3396 			    || !find_is_eval_item(ptr + prevcol, &prevcol,
3397 							       &bn, BACKWARD))
3398 # endif
3399 			)
3400 		    break;
3401 		col = prevcol;
3402 	    }
3403 
3404 	    /* If we don't want just any old string, or we've found an
3405 	     * identifier, stop searching. */
3406 	    if (this_class > 2)
3407 		this_class = 2;
3408 	    if (!(find_type & FIND_STRING) || this_class == 2)
3409 		break;
3410 	}
3411 	else
3412 #endif
3413 	{
3414 	    while (col > 0
3415 		    && ((i == 0
3416 			    ? vim_iswordc(ptr[col - 1])
3417 			    : (!vim_iswhite(ptr[col - 1])
3418 				&& (!(find_type & FIND_IDENT)
3419 				    || !vim_iswordc(ptr[col - 1]))))
3420 #if defined(FEAT_BEVAL)
3421 			|| ((find_type & FIND_EVAL)
3422 			    && col > 1
3423 			    && find_is_eval_item(ptr + col - 1, &col,
3424 							       &bn, BACKWARD))
3425 #endif
3426 			))
3427 		--col;
3428 
3429 	    /* If we don't want just any old string, or we've found an
3430 	     * identifier, stop searching. */
3431 	    if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3432 		break;
3433 	}
3434     }
3435 
3436     if (ptr[col] == NUL || (i == 0 && (
3437 #ifdef FEAT_MBYTE
3438 		has_mbyte ? this_class != 2 :
3439 #endif
3440 		!vim_iswordc(ptr[col]))))
3441     {
3442 	/*
3443 	 * didn't find an identifier or string
3444 	 */
3445 	if (find_type & FIND_STRING)
3446 	    EMSG(_("E348: No string under cursor"));
3447 	else
3448 	    EMSG(_("E349: No identifier under cursor"));
3449 	return 0;
3450     }
3451     ptr += col;
3452     *string = ptr;
3453 
3454     /*
3455      * 3. Find the end if the identifier/string.
3456      */
3457 #if defined(FEAT_BEVAL)
3458     bn = 0;
3459     startcol -= col;
3460 #endif
3461     col = 0;
3462 #ifdef FEAT_MBYTE
3463     if (has_mbyte)
3464     {
3465 	/* Search for point of changing multibyte character class. */
3466 	this_class = mb_get_class(ptr);
3467 	while (ptr[col] != NUL
3468 		&& ((i == 0 ? mb_get_class(ptr + col) == this_class
3469 			    : mb_get_class(ptr + col) != 0)
3470 # if defined(FEAT_BEVAL)
3471 		    || ((find_type & FIND_EVAL)
3472 			&& col <= (int)startcol
3473 			&& find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3474 # endif
3475 		))
3476 	    col += (*mb_ptr2len)(ptr + col);
3477     }
3478     else
3479 #endif
3480 	while ((i == 0 ? vim_iswordc(ptr[col])
3481 		       : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
3482 # if defined(FEAT_BEVAL)
3483 		    || ((find_type & FIND_EVAL)
3484 			&& col <= (int)startcol
3485 			&& find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3486 # endif
3487 		)
3488 	{
3489 	    ++col;
3490 	}
3491 
3492     return col;
3493 }
3494 
3495 /*
3496  * Prepare for redo of a normal command.
3497  */
3498     static void
3499 prep_redo_cmd(cap)
3500     cmdarg_T  *cap;
3501 {
3502     prep_redo(cap->oap->regname, cap->count0,
3503 				     NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3504 }
3505 
3506 /*
3507  * Prepare for redo of any command.
3508  * Note that only the last argument can be a multi-byte char.
3509  */
3510     static void
3511 prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3512     int	    regname;
3513     long    num;
3514     int	    cmd1;
3515     int	    cmd2;
3516     int	    cmd3;
3517     int	    cmd4;
3518     int	    cmd5;
3519 {
3520     ResetRedobuff();
3521     if (regname != 0)	/* yank from specified buffer */
3522     {
3523 	AppendCharToRedobuff('"');
3524 	AppendCharToRedobuff(regname);
3525     }
3526     if (num)
3527 	AppendNumberToRedobuff(num);
3528 
3529     if (cmd1 != NUL)
3530 	AppendCharToRedobuff(cmd1);
3531     if (cmd2 != NUL)
3532 	AppendCharToRedobuff(cmd2);
3533     if (cmd3 != NUL)
3534 	AppendCharToRedobuff(cmd3);
3535     if (cmd4 != NUL)
3536 	AppendCharToRedobuff(cmd4);
3537     if (cmd5 != NUL)
3538 	AppendCharToRedobuff(cmd5);
3539 }
3540 
3541 /*
3542  * check for operator active and clear it
3543  *
3544  * return TRUE if operator was active
3545  */
3546     static int
3547 checkclearop(oap)
3548     oparg_T	*oap;
3549 {
3550     if (oap->op_type == OP_NOP)
3551 	return FALSE;
3552     clearopbeep(oap);
3553     return TRUE;
3554 }
3555 
3556 /*
3557  * check for operator or Visual active and clear it
3558  *
3559  * return TRUE if operator was active
3560  */
3561     static int
3562 checkclearopq(oap)
3563     oparg_T	*oap;
3564 {
3565     if (oap->op_type == OP_NOP
3566 #ifdef FEAT_VISUAL
3567 	    && !VIsual_active
3568 #endif
3569 	    )
3570 	return FALSE;
3571     clearopbeep(oap);
3572     return TRUE;
3573 }
3574 
3575     static void
3576 clearop(oap)
3577     oparg_T	*oap;
3578 {
3579     oap->op_type = OP_NOP;
3580     oap->regname = 0;
3581     oap->motion_force = NUL;
3582     oap->use_reg_one = FALSE;
3583 }
3584 
3585     static void
3586 clearopbeep(oap)
3587     oparg_T	*oap;
3588 {
3589     clearop(oap);
3590     beep_flush();
3591 }
3592 
3593 #ifdef FEAT_VISUAL
3594 /*
3595  * Remove the shift modifier from a special key.
3596  */
3597     static void
3598 unshift_special(cap)
3599     cmdarg_T	*cap;
3600 {
3601     switch (cap->cmdchar)
3602     {
3603 	case K_S_RIGHT:	cap->cmdchar = K_RIGHT; break;
3604 	case K_S_LEFT:	cap->cmdchar = K_LEFT; break;
3605 	case K_S_UP:	cap->cmdchar = K_UP; break;
3606 	case K_S_DOWN:	cap->cmdchar = K_DOWN; break;
3607 	case K_S_HOME:	cap->cmdchar = K_HOME; break;
3608 	case K_S_END:	cap->cmdchar = K_END; break;
3609     }
3610     cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3611 }
3612 #endif
3613 
3614 #if defined(FEAT_CMDL_INFO) || defined(PROTO)
3615 /*
3616  * Routines for displaying a partly typed command
3617  */
3618 
3619 #ifdef FEAT_VISUAL	/* need room for size of Visual area */
3620 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3621 #else
3622 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3623 #endif
3624 static char_u	showcmd_buf[SHOWCMD_BUFLEN];
3625 static char_u	old_showcmd_buf[SHOWCMD_BUFLEN];  /* For push_showcmd() */
3626 static int	showcmd_is_clear = TRUE;
3627 static int	showcmd_visual = FALSE;
3628 
3629 static void display_showcmd __ARGS((void));
3630 
3631     void
3632 clear_showcmd()
3633 {
3634     if (!p_sc)
3635 	return;
3636 
3637 #ifdef FEAT_VISUAL
3638     if (VIsual_active && !char_avail())
3639     {
3640 	int		i = lt(VIsual, curwin->w_cursor);
3641 	long		lines;
3642 	colnr_T		leftcol, rightcol;
3643 	linenr_T	top, bot;
3644 
3645 	/* Show the size of the Visual area. */
3646 	if (i)
3647 	{
3648 	    top = VIsual.lnum;
3649 	    bot = curwin->w_cursor.lnum;
3650 	}
3651 	else
3652 	{
3653 	    top = curwin->w_cursor.lnum;
3654 	    bot = VIsual.lnum;
3655 	}
3656 # ifdef FEAT_FOLDING
3657 	/* Include closed folds as a whole. */
3658 	hasFolding(top, &top, NULL);
3659 	hasFolding(bot, NULL, &bot);
3660 # endif
3661 	lines = bot - top + 1;
3662 
3663 	if (VIsual_mode == Ctrl_V)
3664 	{
3665 	    getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
3666 	    sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3667 					      (long)(rightcol - leftcol + 1));
3668 	}
3669 	else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3670 	    sprintf((char *)showcmd_buf, "%ld", lines);
3671 	else
3672 	    sprintf((char *)showcmd_buf, "%ld", (long)(i
3673 		    ? curwin->w_cursor.col - VIsual.col
3674 		    : VIsual.col - curwin->w_cursor.col) + (*p_sel != 'e'));
3675 	showcmd_buf[SHOWCMD_COLS] = NUL;	/* truncate */
3676 	showcmd_visual = TRUE;
3677     }
3678     else
3679 #endif
3680     {
3681 	showcmd_buf[0] = NUL;
3682 	showcmd_visual = FALSE;
3683 
3684 	/* Don't actually display something if there is nothing to clear. */
3685 	if (showcmd_is_clear)
3686 	    return;
3687     }
3688 
3689     display_showcmd();
3690 }
3691 
3692 /*
3693  * Add 'c' to string of shown command chars.
3694  * Return TRUE if output has been written (and setcursor() has been called).
3695  */
3696     int
3697 add_to_showcmd(c)
3698     int		c;
3699 {
3700     char_u	*p;
3701     int		old_len;
3702     int		extra_len;
3703     int		overflow;
3704 #if defined(FEAT_MOUSE)
3705     int		i;
3706     static int	ignore[] =
3707     {
3708 # ifdef FEAT_GUI
3709 	K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3710 	K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
3711 # endif
3712 	K_IGNORE,
3713 	K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3714 	K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3715 	K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
3716 	K_MOUSEDOWN, K_MOUSEUP,
3717 	K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
3718 	K_CURSORHOLD,
3719 	0
3720     };
3721 #endif
3722 
3723     if (!p_sc || msg_silent != 0)
3724 	return FALSE;
3725 
3726     if (showcmd_visual)
3727     {
3728 	showcmd_buf[0] = NUL;
3729 	showcmd_visual = FALSE;
3730     }
3731 
3732 #if defined(FEAT_MOUSE)
3733     /* Ignore keys that are scrollbar updates and mouse clicks */
3734     if (IS_SPECIAL(c))
3735 	for (i = 0; ignore[i] != 0; ++i)
3736 	    if (ignore[i] == c)
3737 		return FALSE;
3738 #endif
3739 
3740     p = transchar(c);
3741     old_len = (int)STRLEN(showcmd_buf);
3742     extra_len = (int)STRLEN(p);
3743     overflow = old_len + extra_len - SHOWCMD_COLS;
3744     if (overflow > 0)
3745 	STRCPY(showcmd_buf, showcmd_buf + overflow);
3746     STRCAT(showcmd_buf, p);
3747 
3748     if (char_avail())
3749 	return FALSE;
3750 
3751     display_showcmd();
3752 
3753     return TRUE;
3754 }
3755 
3756     void
3757 add_to_showcmd_c(c)
3758     int		c;
3759 {
3760     if (!add_to_showcmd(c))
3761 	setcursor();
3762 }
3763 
3764 /*
3765  * Delete 'len' characters from the end of the shown command.
3766  */
3767     static void
3768 del_from_showcmd(len)
3769     int	    len;
3770 {
3771     int	    old_len;
3772 
3773     if (!p_sc)
3774 	return;
3775 
3776     old_len = (int)STRLEN(showcmd_buf);
3777     if (len > old_len)
3778 	len = old_len;
3779     showcmd_buf[old_len - len] = NUL;
3780 
3781     if (!char_avail())
3782 	display_showcmd();
3783 }
3784 
3785 /*
3786  * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3787  * something and there is a partial mapping.
3788  */
3789     void
3790 push_showcmd()
3791 {
3792     if (p_sc)
3793 	STRCPY(old_showcmd_buf, showcmd_buf);
3794 }
3795 
3796     void
3797 pop_showcmd()
3798 {
3799     if (!p_sc)
3800 	return;
3801 
3802     STRCPY(showcmd_buf, old_showcmd_buf);
3803 
3804     display_showcmd();
3805 }
3806 
3807     static void
3808 display_showcmd()
3809 {
3810     int	    len;
3811 
3812     cursor_off();
3813 
3814     len = (int)STRLEN(showcmd_buf);
3815     if (len == 0)
3816 	showcmd_is_clear = TRUE;
3817     else
3818     {
3819 	screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3820 	showcmd_is_clear = FALSE;
3821     }
3822 
3823     /*
3824      * clear the rest of an old message by outputing up to SHOWCMD_COLS spaces
3825      */
3826     screen_puts((char_u *)"          " + len, (int)Rows - 1, sc_col + len, 0);
3827 
3828     setcursor();	    /* put cursor back where it belongs */
3829 }
3830 #endif
3831 
3832 #ifdef FEAT_SCROLLBIND
3833 /*
3834  * When "check" is FALSE, prepare for commands that scroll the window.
3835  * When "check" is TRUE, take care of scroll-binding after the window has
3836  * scrolled.  Called from normal_cmd() and edit().
3837  */
3838     void
3839 do_check_scrollbind(check)
3840     int		check;
3841 {
3842     static win_T	*old_curwin = NULL;
3843     static linenr_T	old_topline = 0;
3844 #ifdef FEAT_DIFF
3845     static int		old_topfill = 0;
3846 #endif
3847     static buf_T	*old_buf = NULL;
3848     static colnr_T	old_leftcol = 0;
3849 
3850     if (check && curwin->w_p_scb)
3851     {
3852 	/* If a ":syncbind" command was just used, don't scroll, only reset
3853 	 * the values. */
3854 	if (did_syncbind)
3855 	    did_syncbind = FALSE;
3856 	else if (curwin == old_curwin)
3857 	{
3858 	    /*
3859 	     * Synchronize other windows, as necessary according to
3860 	     * 'scrollbind'.  Don't do this after an ":edit" command, except
3861 	     * when 'diff' is set.
3862 	     */
3863 	    if ((curwin->w_buffer == old_buf
3864 #ifdef FEAT_DIFF
3865 			|| curwin->w_p_diff
3866 #endif
3867 		)
3868 		&& (curwin->w_topline != old_topline
3869 #ifdef FEAT_DIFF
3870 			|| curwin->w_topfill != old_topfill
3871 #endif
3872 			|| curwin->w_leftcol != old_leftcol))
3873 	    {
3874 		check_scrollbind(curwin->w_topline - old_topline,
3875 			(long)(curwin->w_leftcol - old_leftcol));
3876 	    }
3877 	}
3878 	else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
3879 	{
3880 	    /*
3881 	     * When switching between windows, make sure that the relative
3882 	     * vertical offset is valid for the new window.  The relative
3883 	     * offset is invalid whenever another 'scrollbind' window has
3884 	     * scrolled to a point that would force the current window to
3885 	     * scroll past the beginning or end of its buffer.  When the
3886 	     * resync is performed, some of the other 'scrollbind' windows may
3887 	     * need to jump so that the current window's relative position is
3888 	     * visible on-screen.
3889 	     */
3890 	    check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
3891 	}
3892 	curwin->w_scbind_pos = curwin->w_topline;
3893     }
3894 
3895     old_curwin = curwin;
3896     old_topline = curwin->w_topline;
3897 #ifdef FEAT_DIFF
3898     old_topfill = curwin->w_topfill;
3899 #endif
3900     old_buf = curwin->w_buffer;
3901     old_leftcol = curwin->w_leftcol;
3902 }
3903 
3904 /*
3905  * Synchronize any windows that have "scrollbind" set, based on the
3906  * number of rows by which the current window has changed
3907  * (1998-11-02 16:21:01  R. Edward Ralston <[email protected]>)
3908  */
3909     void
3910 check_scrollbind(topline_diff, leftcol_diff)
3911     linenr_T	topline_diff;
3912     long	leftcol_diff;
3913 {
3914     int		want_ver;
3915     int		want_hor;
3916     win_T	*old_curwin = curwin;
3917     buf_T	*old_curbuf = curbuf;
3918 #ifdef FEAT_VISUAL
3919     int		old_VIsual_select = VIsual_select;
3920     int		old_VIsual_active = VIsual_active;
3921 #endif
3922     colnr_T	tgt_leftcol = curwin->w_leftcol;
3923     long	topline;
3924     long	y;
3925 
3926     /*
3927      * check 'scrollopt' string for vertical and horizontal scroll options
3928      */
3929     want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
3930 #ifdef FEAT_DIFF
3931     want_ver |= old_curwin->w_p_diff;
3932 #endif
3933     want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
3934 
3935     /*
3936      * loop through the scrollbound windows and scroll accordingly
3937      */
3938 #ifdef FEAT_VISUAL
3939     VIsual_select = VIsual_active = 0;
3940 #endif
3941     for (curwin = firstwin; curwin; curwin = curwin->w_next)
3942     {
3943 	curbuf = curwin->w_buffer;
3944 	/* skip original window  and windows with 'noscrollbind' */
3945 	if (curwin != old_curwin && curwin->w_p_scb)
3946 	{
3947 	    /*
3948 	     * do the vertical scroll
3949 	     */
3950 	    if (want_ver)
3951 	    {
3952 #ifdef FEAT_DIFF
3953 		if (old_curwin->w_p_diff && curwin->w_p_diff)
3954 		{
3955 		    diff_set_topline(old_curwin, curwin);
3956 		}
3957 		else
3958 #endif
3959 		{
3960 		    curwin->w_scbind_pos += topline_diff;
3961 		    topline = curwin->w_scbind_pos;
3962 		    if (topline > curbuf->b_ml.ml_line_count)
3963 			topline = curbuf->b_ml.ml_line_count;
3964 		    if (topline < 1)
3965 			topline = 1;
3966 
3967 		    y = topline - curwin->w_topline;
3968 		    if (y > 0)
3969 			scrollup(y, FALSE);
3970 		    else
3971 			scrolldown(-y, FALSE);
3972 		}
3973 
3974 		redraw_later(VALID);
3975 		cursor_correct();
3976 #ifdef FEAT_WINDOWS
3977 		curwin->w_redr_status = TRUE;
3978 #endif
3979 	    }
3980 
3981 	    /*
3982 	     * do the horizontal scroll
3983 	     */
3984 	    if (want_hor && curwin->w_leftcol != tgt_leftcol)
3985 	    {
3986 		curwin->w_leftcol = tgt_leftcol;
3987 		leftcol_changed();
3988 	    }
3989 	}
3990     }
3991 
3992     /*
3993      * reset current-window
3994      */
3995 #ifdef FEAT_VISUAL
3996     VIsual_select = old_VIsual_select;
3997     VIsual_active = old_VIsual_active;
3998 #endif
3999     curwin = old_curwin;
4000     curbuf = old_curbuf;
4001 }
4002 #endif /* #ifdef FEAT_SCROLLBIND */
4003 
4004 /*
4005  * Command character that's ignored.
4006  * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
4007  * xon/xoff
4008  */
4009     static void
4010 nv_ignore(cap)
4011     cmdarg_T	*cap;
4012 {
4013     cap->retval |= CA_COMMAND_BUSY;	/* don't call edit() now */
4014 }
4015 
4016 /*
4017  * Command character that doesn't do anything, but unlike nv_ignore() does
4018  * start edit().  Used for "startinsert" executed while starting up.
4019  */
4020 /*ARGSUSED */
4021     static void
4022 nv_nop(cap)
4023     cmdarg_T	*cap;
4024 {
4025 }
4026 
4027 /*
4028  * Command character doesn't exist.
4029  */
4030     static void
4031 nv_error(cap)
4032     cmdarg_T	*cap;
4033 {
4034     clearopbeep(cap->oap);
4035 }
4036 
4037 /*
4038  * <Help> and <F1> commands.
4039  */
4040     static void
4041 nv_help(cap)
4042     cmdarg_T	*cap;
4043 {
4044     if (!checkclearopq(cap->oap))
4045 	ex_help(NULL);
4046 }
4047 
4048 /*
4049  * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4050  */
4051     static void
4052 nv_addsub(cap)
4053     cmdarg_T	*cap;
4054 {
4055     if (!checkclearopq(cap->oap)
4056 	    && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4057 	prep_redo_cmd(cap);
4058 }
4059 
4060 /*
4061  * CTRL-F, CTRL-B, etc: Scroll page up or down.
4062  */
4063     static void
4064 nv_page(cap)
4065     cmdarg_T	*cap;
4066 {
4067     if (!checkclearop(cap->oap))
4068 	(void)onepage(cap->arg, cap->count1);
4069 }
4070 
4071 /*
4072  * Implementation of "gd" and "gD" command.
4073  */
4074     static void
4075 nv_gd(oap, nchar, thisblock)
4076     oparg_T	*oap;
4077     int		nchar;
4078     int		thisblock;	/* 1 for "1gd" and "1gD" */
4079 {
4080     int		len;
4081     char_u	*ptr;
4082 
4083     if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
4084 	    || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
4085 	clearopbeep(oap);
4086 #ifdef FEAT_FOLDING
4087     else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4088 	foldOpenCursor();
4089 #endif
4090 }
4091 
4092 /*
4093  * Search for variable declaration of "ptr[len]".
4094  * When "locally" is TRUE in the current function ("gd"), otherwise in the
4095  * current file ("gD").
4096  * When "thisblock" is TRUE check the {} block scope.
4097  * Return FAIL when not found.
4098  */
4099     int
4100 find_decl(ptr, len, locally, thisblock, searchflags)
4101     char_u	*ptr;
4102     int		len;
4103     int		locally;
4104     int		thisblock;
4105     int		searchflags;	/* flags passed to searchit() */
4106 {
4107     char_u	*pat;
4108     pos_T	old_pos;
4109     pos_T	par_pos;
4110     pos_T	found_pos;
4111     int		t;
4112     int		save_p_ws;
4113     int		save_p_scs;
4114     int		retval = OK;
4115     int		incl;
4116 
4117     if ((pat = alloc(len + 7)) == NULL)
4118 	return FAIL;
4119 
4120     /* Put "\V" before the pattern to avoid that the special meaning of "."
4121      * and "~" causes trouble. */
4122     sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4123 								    len, ptr);
4124     old_pos = curwin->w_cursor;
4125     save_p_ws = p_ws;
4126     save_p_scs = p_scs;
4127     p_ws = FALSE;	/* don't wrap around end of file now */
4128     p_scs = FALSE;	/* don't switch ignorecase off now */
4129 
4130     /*
4131      * With "gD" go to line 1.
4132      * With "gd" Search back for the start of the current function, then go
4133      * back until a blank line.  If this fails go to line 1.
4134      */
4135     if (!locally || !findpar(&incl, BACKWARD, 1L, '{', FALSE))
4136     {
4137 	setpcmark();			/* Set in findpar() otherwise */
4138 	curwin->w_cursor.lnum = 1;
4139 	par_pos = curwin->w_cursor;
4140     }
4141     else
4142     {
4143 	par_pos = curwin->w_cursor;
4144 	while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4145 	    --curwin->w_cursor.lnum;
4146     }
4147     curwin->w_cursor.col = 0;
4148 
4149     /* Search forward for the identifier, ignore comment lines. */
4150     clearpos(&found_pos);
4151     for (;;)
4152     {
4153 	t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
4154 				  pat, 1L, searchflags, RE_LAST, (linenr_T)0);
4155 	if (curwin->w_cursor.lnum >= old_pos.lnum)
4156 	    t = FAIL;	/* match after start is failure too */
4157 
4158 	if (thisblock && t != FAIL)
4159 	{
4160 	    pos_T	*pos;
4161 
4162 	    /* Check that the block the match is in doesn't end before the
4163 	     * position where we started the search from. */
4164 	    if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4165 		     (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4166 		    && pos->lnum < old_pos.lnum)
4167 		continue;
4168 	}
4169 
4170 	if (t == FAIL)
4171 	{
4172 	    /* If we previously found a valid position, use it. */
4173 	    if (found_pos.lnum != 0)
4174 	    {
4175 		curwin->w_cursor = found_pos;
4176 		t = OK;
4177 	    }
4178 	    break;
4179 	}
4180 #ifdef FEAT_COMMENTS
4181 	if (get_leader_len(ml_get_curline(), NULL, FALSE) > 0)
4182 	{
4183 	    /* Ignore this line, continue at start of next line. */
4184 	    ++curwin->w_cursor.lnum;
4185 	    curwin->w_cursor.col = 0;
4186 	    continue;
4187 	}
4188 #endif
4189 	if (!locally)	/* global search: use first match found */
4190 	    break;
4191 	if (curwin->w_cursor.lnum >= par_pos.lnum)
4192 	{
4193 	    /* If we previously found a valid position, use it. */
4194 	    if (found_pos.lnum != 0)
4195 		curwin->w_cursor = found_pos;
4196 	    break;
4197 	}
4198 
4199 	/* For finding a local variable and the match is before the "{" search
4200 	 * to find a later match.  For K&R style function declarations this
4201 	 * skips the function header without types. */
4202 	found_pos = curwin->w_cursor;
4203     }
4204 
4205     if (t == FAIL)
4206     {
4207 	retval = FAIL;
4208 	curwin->w_cursor = old_pos;
4209     }
4210     else
4211     {
4212 	curwin->w_set_curswant = TRUE;
4213 	/* "n" searches forward now */
4214 	reset_search_dir();
4215     }
4216 
4217     vim_free(pat);
4218     p_ws = save_p_ws;
4219     p_scs = save_p_scs;
4220 
4221     return retval;
4222 }
4223 
4224 /*
4225  * Move 'dist' lines in direction 'dir', counting lines by *screen*
4226  * lines rather than lines in the file.
4227  * 'dist' must be positive.
4228  *
4229  * Return OK if able to move cursor, FAIL otherwise.
4230  */
4231     static int
4232 nv_screengo(oap, dir, dist)
4233     oparg_T	*oap;
4234     int		dir;
4235     long	dist;
4236 {
4237     int		linelen = linetabsize(ml_get_curline());
4238     int		retval = OK;
4239     int		atend = FALSE;
4240     int		n;
4241     int		col_off1;	/* margin offset for first screen line */
4242     int		col_off2;	/* margin offset for wrapped screen line */
4243     int		width1;		/* text width for first screen line */
4244     int		width2;		/* test width for wrapped screen line */
4245 
4246     oap->motion_type = MCHAR;
4247     oap->inclusive = FALSE;
4248 
4249     col_off1 = curwin_col_off();
4250     col_off2 = col_off1 - curwin_col_off2();
4251     width1 = W_WIDTH(curwin) - col_off1;
4252     width2 = W_WIDTH(curwin) - col_off2;
4253 
4254 #ifdef FEAT_VERTSPLIT
4255     if (curwin->w_width != 0)
4256     {
4257 #endif
4258       /*
4259        * Instead of sticking at the last character of the buffer line we
4260        * try to stick in the last column of the screen.
4261        */
4262       if (curwin->w_curswant == MAXCOL)
4263       {
4264 	atend = TRUE;
4265 	validate_virtcol();
4266 	if (width1 <= 0)
4267 	    curwin->w_curswant = 0;
4268 	else
4269 	{
4270 	    curwin->w_curswant = width1 - 1;
4271 	    if (curwin->w_virtcol > curwin->w_curswant)
4272 		curwin->w_curswant += ((curwin->w_virtcol
4273 			     - curwin->w_curswant - 1) / width2 + 1) * width2;
4274 	}
4275       }
4276       else
4277       {
4278 	if (linelen > width1)
4279 	    n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4280 	else
4281 	    n = width1;
4282 	if (curwin->w_curswant > (colnr_T)n + 1)
4283 	    curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4284 								     * width2;
4285       }
4286 
4287       while (dist--)
4288       {
4289 	if (dir == BACKWARD)
4290 	{
4291 	    if ((long)curwin->w_curswant >= width2)
4292 		/* move back within line */
4293 		curwin->w_curswant -= width2;
4294 	    else
4295 	    {
4296 		/* to previous line */
4297 		if (curwin->w_cursor.lnum == 1)
4298 		{
4299 		    retval = FAIL;
4300 		    break;
4301 		}
4302 		--curwin->w_cursor.lnum;
4303 #ifdef FEAT_FOLDING
4304 		/* Move to the start of a closed fold.  Don't do that when
4305 		 * 'foldopen' contains "all": it will open in a moment. */
4306 		if (!(fdo_flags & FDO_ALL))
4307 		    (void)hasFolding(curwin->w_cursor.lnum,
4308 						&curwin->w_cursor.lnum, NULL);
4309 #endif
4310 		linelen = linetabsize(ml_get_curline());
4311 		if (linelen > width1)
4312 		    curwin->w_curswant += (((linelen - width1 - 1) / width2)
4313 								+ 1) * width2;
4314 	    }
4315 	}
4316 	else /* dir == FORWARD */
4317 	{
4318 	    if (linelen > width1)
4319 		n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4320 	    else
4321 		n = width1;
4322 	    if (curwin->w_curswant + width2 < (colnr_T)n)
4323 		/* move forward within line */
4324 		curwin->w_curswant += width2;
4325 	    else
4326 	    {
4327 		/* to next line */
4328 #ifdef FEAT_FOLDING
4329 		/* Move to the end of a closed fold. */
4330 		(void)hasFolding(curwin->w_cursor.lnum, NULL,
4331 						      &curwin->w_cursor.lnum);
4332 #endif
4333 		if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4334 		{
4335 		    retval = FAIL;
4336 		    break;
4337 		}
4338 		curwin->w_cursor.lnum++;
4339 		curwin->w_curswant %= width2;
4340 	    }
4341 	}
4342       }
4343 #ifdef FEAT_VERTSPLIT
4344     }
4345 #endif
4346 
4347     coladvance(curwin->w_curswant);
4348 
4349 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4350     if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4351     {
4352 	/*
4353 	 * Check for landing on a character that got split at the end of the
4354 	 * last line.  We want to advance a screenline, not end up in the same
4355 	 * screenline or move two screenlines.
4356 	 */
4357 	validate_virtcol();
4358 	if (curwin->w_virtcol > curwin->w_curswant
4359 		&& (curwin->w_curswant < (colnr_T)width1
4360 		    ? (curwin->w_curswant > (colnr_T)width1 / 2)
4361 		    : ((curwin->w_curswant - width1) % width2
4362 						      > (colnr_T)width2 / 2)))
4363 	    --curwin->w_cursor.col;
4364     }
4365 #endif
4366 
4367     if (atend)
4368 	curwin->w_curswant = MAXCOL;	    /* stick in the last column */
4369 
4370     return retval;
4371 }
4372 
4373 #ifdef FEAT_MOUSE
4374 /*
4375  * Mouse scroll wheel: Default action is to scroll three lines, or one page
4376  * when Shift or Ctrl is used.
4377  * K_MOUSEUP (cap->arg == TRUE) or K_MOUSEDOWN (cap->arg == FALSE)
4378  */
4379     static void
4380 nv_mousescroll(cap)
4381     cmdarg_T	*cap;
4382 {
4383 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4384     win_T *old_curwin = curwin;
4385 
4386     /* Currently we only get the mouse coordinates in the GUI. */
4387     if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4388     {
4389 	int row, col;
4390 
4391 	row = mouse_row;
4392 	col = mouse_col;
4393 
4394 	/* find the window at the pointer coordinates */
4395 	curwin = mouse_find_win(&row, &col);
4396 	curbuf = curwin->w_buffer;
4397     }
4398 # endif
4399 
4400     if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4401     {
4402 	(void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4403     }
4404     else
4405     {
4406 	cap->count1 = 3;
4407 	cap->count0 = 3;
4408 	nv_scroll_line(cap);
4409     }
4410 
4411 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4412     curwin->w_redr_status = TRUE;
4413 
4414     curwin = old_curwin;
4415     curbuf = curwin->w_buffer;
4416 # endif
4417 }
4418 
4419 /*
4420  * Mouse clicks and drags.
4421  */
4422     static void
4423 nv_mouse(cap)
4424     cmdarg_T	*cap;
4425 {
4426     (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4427 }
4428 #endif
4429 
4430 /*
4431  * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4432  * cap->arg must be TRUE for CTRL-E.
4433  */
4434     static void
4435 nv_scroll_line(cap)
4436     cmdarg_T	*cap;
4437 {
4438     if (!checkclearop(cap->oap))
4439 	scroll_redraw(cap->arg, cap->count1);
4440 }
4441 
4442 /*
4443  * Scroll "count" lines up or down, and redraw.
4444  */
4445     void
4446 scroll_redraw(up, count)
4447     int		up;
4448     long	count;
4449 {
4450     linenr_T	prev_topline = curwin->w_topline;
4451 #ifdef FEAT_DIFF
4452     int		prev_topfill = curwin->w_topfill;
4453 #endif
4454     linenr_T	prev_lnum = curwin->w_cursor.lnum;
4455 
4456     if (up)
4457 	scrollup(count, TRUE);
4458     else
4459 	scrolldown(count, TRUE);
4460     if (p_so)
4461     {
4462 	/* Adjust the cursor position for 'scrolloff'.  Mark w_topline as
4463 	 * valid, otherwise the screen jumps back at the end of the file. */
4464 	cursor_correct();
4465 	check_cursor_moved(curwin);
4466 	curwin->w_valid |= VALID_TOPLINE;
4467 
4468 	/* If moved back to where we were, at least move the cursor, otherwise
4469 	 * we get stuck at one position.  Don't move the cursor up if the
4470 	 * first line of the buffer is already on the screen */
4471 	while (curwin->w_topline == prev_topline
4472 #ifdef FEAT_DIFF
4473 		&& curwin->w_topfill == prev_topfill
4474 #endif
4475 		)
4476 	{
4477 	    if (up)
4478 	    {
4479 		if (curwin->w_cursor.lnum > prev_lnum
4480 			|| cursor_down(1L, FALSE) == FAIL)
4481 		    break;
4482 	    }
4483 	    else
4484 	    {
4485 		if (curwin->w_cursor.lnum < prev_lnum
4486 			|| prev_topline == 1L
4487 			|| cursor_up(1L, FALSE) == FAIL)
4488 		    break;
4489 	    }
4490 	    /* Mark w_topline as valid, otherwise the screen jumps back at the
4491 	     * end of the file. */
4492 	    check_cursor_moved(curwin);
4493 	    curwin->w_valid |= VALID_TOPLINE;
4494 	}
4495     }
4496     if (curwin->w_cursor.lnum != prev_lnum)
4497 	coladvance(curwin->w_curswant);
4498     redraw_later(VALID);
4499 }
4500 
4501 /*
4502  * Commands that start with "z".
4503  */
4504     static void
4505 nv_zet(cap)
4506     cmdarg_T  *cap;
4507 {
4508     long	n;
4509     colnr_T	col;
4510     int		nchar = cap->nchar;
4511 #ifdef FEAT_FOLDING
4512     long	old_fdl = curwin->w_p_fdl;
4513     int		old_fen = curwin->w_p_fen;
4514 #endif
4515 #ifdef FEAT_SPELL
4516     int		undo = FALSE;
4517 #endif
4518 
4519     if (VIM_ISDIGIT(nchar))
4520     {
4521 	/*
4522 	 * "z123{nchar}": edit the count before obtaining {nchar}
4523 	 */
4524 	if (checkclearop(cap->oap))
4525 	    return;
4526 	n = nchar - '0';
4527 	for (;;)
4528 	{
4529 #ifdef USE_ON_FLY_SCROLL
4530 	    dont_scroll = TRUE;		/* disallow scrolling here */
4531 #endif
4532 	    ++no_mapping;
4533 	    ++allow_keys;   /* no mapping for nchar, but allow key codes */
4534 	    nchar = safe_vgetc();
4535 #ifdef FEAT_LANGMAP
4536 	    LANGMAP_ADJUST(nchar, TRUE);
4537 #endif
4538 	    --no_mapping;
4539 	    --allow_keys;
4540 #ifdef FEAT_CMDL_INFO
4541 	    (void)add_to_showcmd(nchar);
4542 #endif
4543 	    if (nchar == K_DEL || nchar == K_KDEL)
4544 		n /= 10;
4545 	    else if (VIM_ISDIGIT(nchar))
4546 		n = n * 10 + (nchar - '0');
4547 	    else if (nchar == CAR)
4548 	    {
4549 #ifdef FEAT_GUI
4550 		need_mouse_correct = TRUE;
4551 #endif
4552 		win_setheight((int)n);
4553 		break;
4554 	    }
4555 	    else if (nchar == 'l'
4556 		    || nchar == 'h'
4557 		    || nchar == K_LEFT
4558 		    || nchar == K_RIGHT)
4559 	    {
4560 		cap->count1 = n ? n * cap->count1 : cap->count1;
4561 		goto dozet;
4562 	    }
4563 	    else
4564 	    {
4565 		clearopbeep(cap->oap);
4566 		break;
4567 	    }
4568 	}
4569 	cap->oap->op_type = OP_NOP;
4570 	return;
4571     }
4572 
4573 dozet:
4574     if (
4575 #ifdef FEAT_FOLDING
4576 	    /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4577 	     * and "zC" only in Visual mode.  "zj" and "zk" are motion
4578 	     * commands. */
4579 	    cap->nchar != 'f' && cap->nchar != 'F'
4580 	    && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4581 	    && cap->nchar != 'j' && cap->nchar != 'k'
4582 	    &&
4583 #endif
4584 	    checkclearop(cap->oap))
4585 	return;
4586 
4587     /*
4588      * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4589      * If line number given, set cursor.
4590      */
4591     if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4592 	    && cap->count0
4593 	    && cap->count0 != curwin->w_cursor.lnum)
4594     {
4595 	setpcmark();
4596 	if (cap->count0 > curbuf->b_ml.ml_line_count)
4597 	    curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4598 	else
4599 	    curwin->w_cursor.lnum = cap->count0;
4600 	check_cursor_col();
4601     }
4602 
4603     switch (nchar)
4604     {
4605 		/* "z+", "z<CR>" and "zt": put cursor at top of screen */
4606     case '+':
4607 		if (cap->count0 == 0)
4608 		{
4609 		    /* No count given: put cursor at the line below screen */
4610 		    validate_botline();	/* make sure w_botline is valid */
4611 		    if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4612 			curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4613 		    else
4614 			curwin->w_cursor.lnum = curwin->w_botline;
4615 		}
4616 		/* FALLTHROUGH */
4617     case NL:
4618     case CAR:
4619     case K_KENTER:
4620 		beginline(BL_WHITE | BL_FIX);
4621 		/* FALLTHROUGH */
4622 
4623     case 't':	scroll_cursor_top(0, TRUE);
4624 		redraw_later(VALID);
4625 		break;
4626 
4627 		/* "z." and "zz": put cursor in middle of screen */
4628     case '.':	beginline(BL_WHITE | BL_FIX);
4629 		/* FALLTHROUGH */
4630 
4631     case 'z':	scroll_cursor_halfway(TRUE);
4632 		redraw_later(VALID);
4633 		break;
4634 
4635 		/* "z^", "z-" and "zb": put cursor at bottom of screen */
4636     case '^':	/* Strange Vi behavior: <count>z^ finds line at top of window
4637 		 * when <count> is at bottom of window, and puts that one at
4638 		 * bottom of window. */
4639 		if (cap->count0 != 0)
4640 		{
4641 		    scroll_cursor_bot(0, TRUE);
4642 		    curwin->w_cursor.lnum = curwin->w_topline;
4643 		}
4644 		else if (curwin->w_topline == 1)
4645 		    curwin->w_cursor.lnum = 1;
4646 		else
4647 		    curwin->w_cursor.lnum = curwin->w_topline - 1;
4648 		/* FALLTHROUGH */
4649     case '-':
4650 		beginline(BL_WHITE | BL_FIX);
4651 		/* FALLTHROUGH */
4652 
4653     case 'b':	scroll_cursor_bot(0, TRUE);
4654 		redraw_later(VALID);
4655 		break;
4656 
4657 		/* "zH" - scroll screen right half-page */
4658     case 'H':
4659 		cap->count1 *= W_WIDTH(curwin) / 2;
4660 		/* FALLTHROUGH */
4661 
4662 		/* "zh" - scroll screen to the right */
4663     case 'h':
4664     case K_LEFT:
4665 		if (!curwin->w_p_wrap)
4666 		{
4667 		    if ((colnr_T)cap->count1 > curwin->w_leftcol)
4668 			curwin->w_leftcol = 0;
4669 		    else
4670 			curwin->w_leftcol -= (colnr_T)cap->count1;
4671 		    leftcol_changed();
4672 		}
4673 		break;
4674 
4675 		/* "zL" - scroll screen left half-page */
4676     case 'L':	cap->count1 *= W_WIDTH(curwin) / 2;
4677 		/* FALLTHROUGH */
4678 
4679 		/* "zl" - scroll screen to the left */
4680     case 'l':
4681     case K_RIGHT:
4682 		if (!curwin->w_p_wrap)
4683 		{
4684 		    /* scroll the window left */
4685 		    curwin->w_leftcol += (colnr_T)cap->count1;
4686 		    leftcol_changed();
4687 		}
4688 		break;
4689 
4690 		/* "zs" - scroll screen, cursor at the start */
4691     case 's':	if (!curwin->w_p_wrap)
4692 		{
4693 #ifdef FEAT_FOLDING
4694 		    if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4695 			col = 0;	/* like the cursor is in col 0 */
4696 		    else
4697 #endif
4698 		    getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4699 		    if ((long)col > p_siso)
4700 			col -= p_siso;
4701 		    else
4702 			col = 0;
4703 		    if (curwin->w_leftcol != col)
4704 		    {
4705 			curwin->w_leftcol = col;
4706 			redraw_later(NOT_VALID);
4707 		    }
4708 		}
4709 		break;
4710 
4711 		/* "ze" - scroll screen, cursor at the end */
4712     case 'e':	if (!curwin->w_p_wrap)
4713 		{
4714 #ifdef FEAT_FOLDING
4715 		    if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4716 			col = 0;	/* like the cursor is in col 0 */
4717 		    else
4718 #endif
4719 		    getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4720 		    n = W_WIDTH(curwin) - curwin_col_off();
4721 		    if ((long)col + p_siso < n)
4722 			col = 0;
4723 		    else
4724 			col = col + p_siso - n + 1;
4725 		    if (curwin->w_leftcol != col)
4726 		    {
4727 			curwin->w_leftcol = col;
4728 			redraw_later(NOT_VALID);
4729 		    }
4730 		}
4731 		break;
4732 
4733 #ifdef FEAT_FOLDING
4734 		/* "zF": create fold command */
4735 		/* "zf": create fold operator */
4736     case 'F':
4737     case 'f':   if (foldManualAllowed(TRUE))
4738 		{
4739 		    cap->nchar = 'f';
4740 		    nv_operator(cap);
4741 		    curwin->w_p_fen = TRUE;
4742 
4743 		    /* "zF" is like "zfzf" */
4744 		    if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4745 		    {
4746 			nv_operator(cap);
4747 			finish_op = TRUE;
4748 		    }
4749 		}
4750 		else
4751 		    clearopbeep(cap->oap);
4752 		break;
4753 
4754 		/* "zd": delete fold at cursor */
4755 		/* "zD": delete fold at cursor recursively */
4756     case 'd':
4757     case 'D':	if (foldManualAllowed(FALSE))
4758 		{
4759 		    if (VIsual_active)
4760 			nv_operator(cap);
4761 		    else
4762 			deleteFold(curwin->w_cursor.lnum,
4763 				  curwin->w_cursor.lnum, nchar == 'D', FALSE);
4764 		}
4765 		break;
4766 
4767 		/* "zE": erease all folds */
4768     case 'E':	if (foldmethodIsManual(curwin))
4769 		{
4770 		    clearFolding(curwin);
4771 		    changed_window_setting();
4772 		}
4773 		else if (foldmethodIsMarker(curwin))
4774 		    deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4775 								 TRUE, FALSE);
4776 		else
4777 		    EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4778 		break;
4779 
4780 		/* "zn": fold none: reset 'foldenable' */
4781     case 'n':	curwin->w_p_fen = FALSE;
4782 		break;
4783 
4784 		/* "zN": fold Normal: set 'foldenable' */
4785     case 'N':	curwin->w_p_fen = TRUE;
4786 		break;
4787 
4788 		/* "zi": invert folding: toggle 'foldenable' */
4789     case 'i':	curwin->w_p_fen = !curwin->w_p_fen;
4790 		break;
4791 
4792 		/* "za": open closed fold or close open fold at cursor */
4793     case 'a':	if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4794 		    openFold(curwin->w_cursor.lnum, cap->count1);
4795 		else
4796 		{
4797 		    closeFold(curwin->w_cursor.lnum, cap->count1);
4798 		    curwin->w_p_fen = TRUE;
4799 		}
4800 		break;
4801 
4802 		/* "zA": open fold at cursor recursively */
4803     case 'A':	if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4804 		    openFoldRecurse(curwin->w_cursor.lnum);
4805 		else
4806 		{
4807 		    closeFoldRecurse(curwin->w_cursor.lnum);
4808 		    curwin->w_p_fen = TRUE;
4809 		}
4810 		break;
4811 
4812 		/* "zo": open fold at cursor or Visual area */
4813     case 'o':	if (VIsual_active)
4814 		    nv_operator(cap);
4815 		else
4816 		    openFold(curwin->w_cursor.lnum, cap->count1);
4817 		break;
4818 
4819 		/* "zO": open fold recursively */
4820     case 'O':	if (VIsual_active)
4821 		    nv_operator(cap);
4822 		else
4823 		    openFoldRecurse(curwin->w_cursor.lnum);
4824 		break;
4825 
4826 		/* "zc": close fold at cursor or Visual area */
4827     case 'c':	if (VIsual_active)
4828 		    nv_operator(cap);
4829 		else
4830 		    closeFold(curwin->w_cursor.lnum, cap->count1);
4831 		curwin->w_p_fen = TRUE;
4832 		break;
4833 
4834 		/* "zC": close fold recursively */
4835     case 'C':	if (VIsual_active)
4836 		    nv_operator(cap);
4837 		else
4838 		    closeFoldRecurse(curwin->w_cursor.lnum);
4839 		curwin->w_p_fen = TRUE;
4840 		break;
4841 
4842 		/* "zv": open folds at the cursor */
4843     case 'v':	foldOpenCursor();
4844 		break;
4845 
4846 		/* "zx": re-apply 'foldlevel' and open folds at the cursor */
4847     case 'x':	curwin->w_p_fen = TRUE;
4848 		newFoldLevel();		/* update right now */
4849 		foldOpenCursor();
4850 		break;
4851 
4852 		/* "zX": undo manual opens/closes, re-apply 'foldlevel' */
4853     case 'X':	curwin->w_p_fen = TRUE;
4854 		old_fdl = -1;		/* force an update */
4855 		break;
4856 
4857 		/* "zm": fold more */
4858     case 'm':	if (curwin->w_p_fdl > 0)
4859 		    --curwin->w_p_fdl;
4860 		old_fdl = -1;		/* force an update */
4861 		curwin->w_p_fen = TRUE;
4862 		break;
4863 
4864 		/* "zM": close all folds */
4865     case 'M':	curwin->w_p_fdl = 0;
4866 		old_fdl = -1;		/* force an update */
4867 		curwin->w_p_fen = TRUE;
4868 		break;
4869 
4870 		/* "zr": reduce folding */
4871     case 'r':	++curwin->w_p_fdl;
4872 		break;
4873 
4874 		/* "zR": open all folds */
4875     case 'R':	curwin->w_p_fdl = getDeepestNesting();
4876 		old_fdl = -1;		/* force an update */
4877 		break;
4878 
4879     case 'j':	/* "zj" move to next fold downwards */
4880     case 'k':	/* "zk" move to next fold upwards */
4881 		if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
4882 							  cap->count1) == FAIL)
4883 		    clearopbeep(cap->oap);
4884 		break;
4885 
4886 #endif /* FEAT_FOLDING */
4887 
4888 #ifdef FEAT_SPELL
4889     case 'u':	/* "zug" and "zuw": undo "zg" and "zw" */
4890 		++no_mapping;
4891 		++allow_keys;   /* no mapping for nchar, but allow key codes */
4892 		nchar = safe_vgetc();
4893 #ifdef FEAT_LANGMAP
4894 		LANGMAP_ADJUST(nchar, TRUE);
4895 #endif
4896 		--no_mapping;
4897 		--allow_keys;
4898 #ifdef FEAT_CMDL_INFO
4899 		(void)add_to_showcmd(nchar);
4900 #endif
4901 		if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
4902 		{
4903 		    clearopbeep(cap->oap);
4904 		    break;
4905 		}
4906 		undo = TRUE;
4907 		/*FALLTHROUGH*/
4908 
4909     case 'g':	/* "zg": add good word to word list */
4910     case 'w':	/* "zw": add wrong word to word list */
4911     case 'G':	/* "zG": add good word to temp word list */
4912     case 'W':	/* "zW": add wrong word to temp word list */
4913 		{
4914 		    char_u  *ptr = NULL;
4915 		    int	    len;
4916 
4917 		    if (checkclearop(cap->oap))
4918 			break;
4919 # ifdef FEAT_VISUAL
4920 		    if (VIsual_active && get_visual_text(cap, &ptr, &len)
4921 								      == FAIL)
4922 			return;
4923 # endif
4924 		    if (ptr == NULL)
4925 		    {
4926 			pos_T	pos = curwin->w_cursor;
4927 
4928 			/* Find bad word under the cursor. */
4929 			len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
4930 			if (len != 0 && curwin->w_cursor.col <= pos.col)
4931 			    ptr = ml_get_pos(&curwin->w_cursor);
4932 			curwin->w_cursor = pos;
4933 		    }
4934 
4935 		    if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
4936 							    FIND_IDENT)) == 0)
4937 			return;
4938 		    spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
4939 					    (nchar == 'G' || nchar == 'W')
4940 						       ? 0 : (int)cap->count1,
4941 					    undo);
4942 		}
4943 		break;
4944 
4945     case '=':	/* "z=": suggestions for a badly spelled word  */
4946 		if (!checkclearop(cap->oap))
4947 		    spell_suggest((int)cap->count0);
4948 		break;
4949 #endif
4950 
4951     default:	clearopbeep(cap->oap);
4952     }
4953 
4954 #ifdef FEAT_FOLDING
4955     /* Redraw when 'foldenable' changed */
4956     if (old_fen != curwin->w_p_fen)
4957     {
4958 # ifdef FEAT_DIFF
4959 	win_T	    *wp;
4960 
4961 	if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
4962 	{
4963 	    /* Adjust 'foldenable' in diff-synced windows. */
4964 	    FOR_ALL_WINDOWS(wp)
4965 	    {
4966 		if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
4967 		{
4968 		    wp->w_p_fen = curwin->w_p_fen;
4969 		    changed_window_setting_win(wp);
4970 		}
4971 	    }
4972 	}
4973 # endif
4974 	changed_window_setting();
4975     }
4976 
4977     /* Redraw when 'foldlevel' changed. */
4978     if (old_fdl != curwin->w_p_fdl)
4979 	newFoldLevel();
4980 #endif
4981 }
4982 
4983 #ifdef FEAT_GUI
4984 /*
4985  * Vertical scrollbar movement.
4986  */
4987     static void
4988 nv_ver_scrollbar(cap)
4989     cmdarg_T	*cap;
4990 {
4991     if (cap->oap->op_type != OP_NOP)
4992 	clearopbeep(cap->oap);
4993 
4994     /* Even if an operator was pending, we still want to scroll */
4995     gui_do_scroll();
4996 }
4997 
4998 /*
4999  * Horizontal scrollbar movement.
5000  */
5001     static void
5002 nv_hor_scrollbar(cap)
5003     cmdarg_T	*cap;
5004 {
5005     if (cap->oap->op_type != OP_NOP)
5006 	clearopbeep(cap->oap);
5007 
5008     /* Even if an operator was pending, we still want to scroll */
5009     gui_do_horiz_scroll();
5010 }
5011 #endif
5012 
5013 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
5014 /*
5015  * Click in GUI tab.
5016  */
5017     static void
5018 nv_tabline(cap)
5019     cmdarg_T	*cap;
5020 {
5021     if (cap->oap->op_type != OP_NOP)
5022 	clearopbeep(cap->oap);
5023 
5024     /* Even if an operator was pending, we still want to jump tabs. */
5025     goto_tabpage(current_tab);
5026 }
5027 
5028 /*
5029  * Selected item in tab line menu.
5030  */
5031     static void
5032 nv_tabmenu(cap)
5033     cmdarg_T	*cap;
5034 {
5035     if (cap->oap->op_type != OP_NOP)
5036 	clearopbeep(cap->oap);
5037 
5038     /* Even if an operator was pending, we still want to jump tabs. */
5039     handle_tabmenu();
5040 }
5041 
5042 /*
5043  * Handle selecting an item of the GUI tab line menu.
5044  * Used in Normal and Insert mode.
5045  */
5046     void
5047 handle_tabmenu()
5048 {
5049     switch (current_tabmenu)
5050     {
5051 	case TABLINE_MENU_CLOSE:
5052 	    if (current_tab == 0)
5053 		do_cmdline_cmd((char_u *)"tabclose");
5054 	    else
5055 	    {
5056 		vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5057 								 current_tab);
5058 		do_cmdline_cmd(IObuff);
5059 	    }
5060 	    break;
5061 
5062 	case TABLINE_MENU_NEW:
5063 	    if (current_tab > 0)
5064 		goto_tabpage(current_tab);
5065 	    do_cmdline_cmd((char_u *)"tabnew");
5066 	    break;
5067 
5068 	case TABLINE_MENU_OPEN:
5069 	    if (current_tab > 0)
5070 		goto_tabpage(current_tab);
5071 	    do_cmdline_cmd((char_u *)"browse tabnew");
5072 	    break;
5073     }
5074 }
5075 #endif
5076 
5077 /*
5078  * "Q" command.
5079  */
5080     static void
5081 nv_exmode(cap)
5082     cmdarg_T	*cap;
5083 {
5084     /*
5085      * Ignore 'Q' in Visual mode, just give a beep.
5086      */
5087 #ifdef FEAT_VISUAL
5088     if (VIsual_active)
5089 	vim_beep();
5090     else
5091 #endif
5092 	if (!checkclearop(cap->oap))
5093 	do_exmode(FALSE);
5094 }
5095 
5096 /*
5097  * Handle a ":" command.
5098  */
5099     static void
5100 nv_colon(cap)
5101     cmdarg_T  *cap;
5102 {
5103     int	    old_p_im;
5104 
5105 #ifdef FEAT_VISUAL
5106     if (VIsual_active)
5107 	nv_operator(cap);
5108     else
5109 #endif
5110     {
5111 	if (cap->oap->op_type != OP_NOP)
5112 	{
5113 	    /* Using ":" as a movement is characterwise exclusive. */
5114 	    cap->oap->motion_type = MCHAR;
5115 	    cap->oap->inclusive = FALSE;
5116 	}
5117 	else if (cap->count0)
5118 	{
5119 	    /* translate "count:" into ":.,.+(count - 1)" */
5120 	    stuffcharReadbuff('.');
5121 	    if (cap->count0 > 1)
5122 	    {
5123 		stuffReadbuff((char_u *)",.+");
5124 		stuffnumReadbuff((long)cap->count0 - 1L);
5125 	    }
5126 	}
5127 
5128 	/* When typing, don't type below an old message */
5129 	if (KeyTyped)
5130 	    compute_cmdrow();
5131 
5132 	old_p_im = p_im;
5133 
5134 	/* get a command line and execute it */
5135 	do_cmdline(NULL, getexline, NULL,
5136 			    cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5137 
5138 	/* If 'insertmode' changed, enter or exit Insert mode */
5139 	if (p_im != old_p_im)
5140 	{
5141 	    if (p_im)
5142 		restart_edit = 'i';
5143 	    else
5144 		restart_edit = 0;
5145 	}
5146 
5147 	/* The start of the operator may have become invalid by the Ex
5148 	 * command. */
5149 	if (cap->oap->op_type != OP_NOP
5150 		&& (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5151 		    || cap->oap->start.col >
5152 					 STRLEN(ml_get(cap->oap->start.lnum))))
5153 	    clearopbeep(cap->oap);
5154     }
5155 }
5156 
5157 /*
5158  * Handle CTRL-G command.
5159  */
5160     static void
5161 nv_ctrlg(cap)
5162     cmdarg_T *cap;
5163 {
5164 #ifdef FEAT_VISUAL
5165     if (VIsual_active)	/* toggle Selection/Visual mode */
5166     {
5167 	VIsual_select = !VIsual_select;
5168 	showmode();
5169     }
5170     else
5171 #endif
5172 	if (!checkclearop(cap->oap))
5173 	/* print full name if count given or :cd used */
5174 	fileinfo((int)cap->count0, FALSE, TRUE);
5175 }
5176 
5177 /*
5178  * Handle CTRL-H <Backspace> command.
5179  */
5180     static void
5181 nv_ctrlh(cap)
5182     cmdarg_T *cap;
5183 {
5184 #ifdef FEAT_VISUAL
5185     if (VIsual_active && VIsual_select)
5186     {
5187 	cap->cmdchar = 'x';	/* BS key behaves like 'x' in Select mode */
5188 	v_visop(cap);
5189     }
5190     else
5191 #endif
5192 	nv_left(cap);
5193 }
5194 
5195 /*
5196  * CTRL-L: clear screen and redraw.
5197  */
5198     static void
5199 nv_clear(cap)
5200     cmdarg_T	*cap;
5201 {
5202     if (!checkclearop(cap->oap))
5203     {
5204 #if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5205 	/*
5206 	 * Right now, the BeBox doesn't seem to have an easy way to detect
5207 	 * window resizing, so we cheat and make the user detect it
5208 	 * manually with CTRL-L instead
5209 	 */
5210 	ui_get_shellsize();
5211 #endif
5212 #ifdef FEAT_SYN_HL
5213 	/* Clear all syntax states to force resyncing. */
5214 	syn_stack_free_all(curbuf);
5215 #endif
5216 	redraw_later(CLEAR);
5217     }
5218 }
5219 
5220 /*
5221  * CTRL-O: In Select mode: switch to Visual mode for one command.
5222  * Otherwise: Go to older pcmark.
5223  */
5224     static void
5225 nv_ctrlo(cap)
5226     cmdarg_T	*cap;
5227 {
5228 #ifdef FEAT_VISUAL
5229     if (VIsual_active && VIsual_select)
5230     {
5231 	VIsual_select = FALSE;
5232 	showmode();
5233 	restart_VIsual_select = 2;	/* restart Select mode later */
5234     }
5235     else
5236 #endif
5237     {
5238 	cap->count1 = -cap->count1;
5239 	nv_pcmark(cap);
5240     }
5241 }
5242 
5243 /*
5244  * CTRL-^ command, short for ":e #"
5245  */
5246     static void
5247 nv_hat(cap)
5248     cmdarg_T	*cap;
5249 {
5250     if (!checkclearopq(cap->oap))
5251 	(void)buflist_getfile((int)cap->count0, (linenr_T)0,
5252 						GETF_SETMARK|GETF_ALT, FALSE);
5253 }
5254 
5255 /*
5256  * "Z" commands.
5257  */
5258     static void
5259 nv_Zet(cap)
5260     cmdarg_T *cap;
5261 {
5262     if (!checkclearopq(cap->oap))
5263     {
5264 	switch (cap->nchar)
5265 	{
5266 			/* "ZZ": equivalent to ":x". */
5267 	    case 'Z':	do_cmdline_cmd((char_u *)"x");
5268 			break;
5269 
5270 			/* "ZQ": equivalent to ":q!" (Elvis compatible). */
5271 	    case 'Q':	do_cmdline_cmd((char_u *)"q!");
5272 			break;
5273 
5274 	    default:	clearopbeep(cap->oap);
5275 	}
5276     }
5277 }
5278 
5279 #if defined(FEAT_WINDOWS) || defined(PROTO)
5280 /*
5281  * Call nv_ident() as if "c1" was used, with "c2" as next character.
5282  */
5283     void
5284 do_nv_ident(c1, c2)
5285     int		c1;
5286     int		c2;
5287 {
5288     oparg_T	oa;
5289     cmdarg_T	ca;
5290 
5291     clear_oparg(&oa);
5292     vim_memset(&ca, 0, sizeof(ca));
5293     ca.oap = &oa;
5294     ca.cmdchar = c1;
5295     ca.nchar = c2;
5296     nv_ident(&ca);
5297 }
5298 #endif
5299 
5300 /*
5301  * Handle the commands that use the word under the cursor.
5302  * [g] CTRL-]	:ta to current identifier
5303  * [g] 'K'	run program for current identifier
5304  * [g] '*'	/ to current identifier or string
5305  * [g] '#'	? to current identifier or string
5306  *  g  ']'	:tselect for current identifier
5307  */
5308     static void
5309 nv_ident(cap)
5310     cmdarg_T	*cap;
5311 {
5312     char_u	*ptr = NULL;
5313     char_u	*buf;
5314     char_u	*p;
5315     char_u	*kp;		/* value of 'keywordprg' */
5316     int		kp_help;	/* 'keywordprg' is ":help" */
5317     int		n = 0;		/* init for GCC */
5318     int		cmdchar;
5319     int		g_cmd;		/* "g" command */
5320     char_u	*aux_ptr;
5321     int		isman;
5322     int		isman_s;
5323 
5324     if (cap->cmdchar == 'g')	/* "g*", "g#", "g]" and "gCTRL-]" */
5325     {
5326 	cmdchar = cap->nchar;
5327 	g_cmd = TRUE;
5328     }
5329     else
5330     {
5331 	cmdchar = cap->cmdchar;
5332 	g_cmd = FALSE;
5333     }
5334 
5335     if (cmdchar == POUND)	/* the pound sign, '#' for English keyboards */
5336 	cmdchar = '#';
5337 
5338     /*
5339      * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5340      */
5341     if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5342     {
5343 #ifdef FEAT_VISUAL
5344 	if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5345 	    return;
5346 #endif
5347 	if (checkclearopq(cap->oap))
5348 	    return;
5349     }
5350 
5351     if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5352 		    (cmdchar == '*' || cmdchar == '#')
5353 				 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5354     {
5355 	clearop(cap->oap);
5356 	return;
5357     }
5358 
5359     /* Allocate buffer to put the command in.  Inserting backslashes can
5360      * double the length of the word.  p_kp / curbuf->b_p_kp could be added
5361      * and some numbers. */
5362     kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5363     kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5364 						 || STRCMP(kp, ":help") == 0);
5365     buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5366     if (buf == NULL)
5367 	return;
5368     buf[0] = NUL;
5369 
5370     switch (cmdchar)
5371     {
5372 	case '*':
5373 	case '#':
5374 	    /*
5375 	     * Put cursor at start of word, makes search skip the word
5376 	     * under the cursor.
5377 	     * Call setpcmark() first, so "*``" puts the cursor back where
5378 	     * it was.
5379 	     */
5380 	    setpcmark();
5381 	    curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5382 
5383 	    if (!g_cmd && vim_iswordp(ptr))
5384 		STRCPY(buf, "\\<");
5385 	    no_smartcase = TRUE;	/* don't use 'smartcase' now */
5386 	    break;
5387 
5388 	case 'K':
5389 	    if (kp_help)
5390 		STRCPY(buf, "he! ");
5391 	    else
5392 	    {
5393 		/* When a count is given, turn it into a range.  Is this
5394 		 * really what we want? */
5395 		isman = (STRCMP(kp, "man") == 0);
5396 		isman_s = (STRCMP(kp, "man -s") == 0);
5397 		if (cap->count0 != 0 && !(isman || isman_s))
5398 		    sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5399 
5400 		STRCAT(buf, "! ");
5401 		if (cap->count0 == 0 && isman_s)
5402 		    STRCAT(buf, "man");
5403 		else
5404 		    STRCAT(buf, kp);
5405 		STRCAT(buf, " ");
5406 		if (cap->count0 != 0 && (isman || isman_s))
5407 		{
5408 		    sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5409 		    STRCAT(buf, " ");
5410 		}
5411 	    }
5412 	    break;
5413 
5414 	case ']':
5415 #ifdef FEAT_CSCOPE
5416 	    if (p_cst)
5417 		STRCPY(buf, "cstag ");
5418 	    else
5419 #endif
5420 		STRCPY(buf, "ts ");
5421 	    break;
5422 
5423 	default:
5424 	    if (curbuf->b_help)
5425 		STRCPY(buf, "he! ");
5426 	    else if (g_cmd)
5427 		STRCPY(buf, "tj ");
5428 	    else
5429 		sprintf((char *)buf, "%ldta ", cap->count0);
5430     }
5431 
5432     /*
5433      * Now grab the chars in the identifier
5434      */
5435     if (cmdchar == '*')
5436 	aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5437     else if (cmdchar == '#')
5438 	aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
5439     else if (cmdchar == 'K' && !kp_help)
5440 	aux_ptr = (char_u *)" \t\\\"|!";
5441     else
5442 	/* Don't escape spaces and Tabs in a tag with a backslash */
5443 	aux_ptr = (char_u *)"\\|\"";
5444 
5445     p = buf + STRLEN(buf);
5446     while (n-- > 0)
5447     {
5448 	/* put a backslash before \ and some others */
5449 	if (vim_strchr(aux_ptr, *ptr) != NULL)
5450 	    *p++ = '\\';
5451 #ifdef FEAT_MBYTE
5452 	/* When current byte is a part of multibyte character, copy all bytes
5453 	 * of that character. */
5454 	if (has_mbyte)
5455 	{
5456 	    int i;
5457 	    int len = (*mb_ptr2len)(ptr) - 1;
5458 
5459 	    for (i = 0; i < len && n >= 1; ++i, --n)
5460 		*p++ = *ptr++;
5461 	}
5462 #endif
5463 	*p++ = *ptr++;
5464     }
5465     *p = NUL;
5466 
5467     /*
5468      * Execute the command.
5469      */
5470     if (cmdchar == '*' || cmdchar == '#')
5471     {
5472 	if (!g_cmd && (
5473 #ifdef FEAT_MBYTE
5474 		has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5475 #endif
5476 		vim_iswordc(ptr[-1])))
5477 	    STRCAT(buf, "\\>");
5478 #ifdef FEAT_CMDHIST
5479 	/* put pattern in search history */
5480 	add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5481 #endif
5482 	normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5483     }
5484     else
5485 	do_cmdline_cmd(buf);
5486 
5487     vim_free(buf);
5488 }
5489 
5490 #if defined(FEAT_VISUAL) || defined(PROTO)
5491 /*
5492  * Get visually selected text, within one line only.
5493  * Returns FAIL if more than one line selected.
5494  */
5495     int
5496 get_visual_text(cap, pp, lenp)
5497     cmdarg_T	*cap;
5498     char_u	**pp;	    /* return: start of selected text */
5499     int		*lenp;	    /* return: length of selected text */
5500 {
5501     if (VIsual_mode != 'V')
5502 	unadjust_for_sel();
5503     if (VIsual.lnum != curwin->w_cursor.lnum)
5504     {
5505 	if (cap != NULL)
5506 	    clearopbeep(cap->oap);
5507 	return FAIL;
5508     }
5509     if (VIsual_mode == 'V')
5510     {
5511 	*pp = ml_get_curline();
5512 	*lenp = (int)STRLEN(*pp);
5513     }
5514     else
5515     {
5516 	if (lt(curwin->w_cursor, VIsual))
5517 	{
5518 	    *pp = ml_get_pos(&curwin->w_cursor);
5519 	    *lenp = VIsual.col - curwin->w_cursor.col + 1;
5520 	}
5521 	else
5522 	{
5523 	    *pp = ml_get_pos(&VIsual);
5524 	    *lenp = curwin->w_cursor.col - VIsual.col + 1;
5525 	}
5526 #ifdef FEAT_MBYTE
5527 	if (has_mbyte)
5528 	    /* Correct the length to include the whole last character. */
5529 	    *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
5530 #endif
5531     }
5532     reset_VIsual_and_resel();
5533     return OK;
5534 }
5535 #endif
5536 
5537 /*
5538  * CTRL-T: backwards in tag stack
5539  */
5540     static void
5541 nv_tagpop(cap)
5542     cmdarg_T	*cap;
5543 {
5544     if (!checkclearopq(cap->oap))
5545 	do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5546 }
5547 
5548 /*
5549  * Handle scrolling command 'H', 'L' and 'M'.
5550  */
5551     static void
5552 nv_scroll(cap)
5553     cmdarg_T  *cap;
5554 {
5555     int		used = 0;
5556     long	n;
5557 #ifdef FEAT_FOLDING
5558     linenr_T	lnum;
5559 #endif
5560     int		half;
5561 
5562     cap->oap->motion_type = MLINE;
5563     setpcmark();
5564 
5565     if (cap->cmdchar == 'L')
5566     {
5567 	validate_botline();	    /* make sure curwin->w_botline is valid */
5568 	curwin->w_cursor.lnum = curwin->w_botline - 1;
5569 	if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5570 	    curwin->w_cursor.lnum = 1;
5571 	else
5572 	{
5573 #ifdef FEAT_FOLDING
5574 	    if (hasAnyFolding(curwin))
5575 	    {
5576 		/* Count a fold for one screen line. */
5577 		for (n = cap->count1 - 1; n > 0
5578 			    && curwin->w_cursor.lnum > curwin->w_topline; --n)
5579 		{
5580 		    (void)hasFolding(curwin->w_cursor.lnum,
5581 						&curwin->w_cursor.lnum, NULL);
5582 		    --curwin->w_cursor.lnum;
5583 		}
5584 	    }
5585 	    else
5586 #endif
5587 		curwin->w_cursor.lnum -= cap->count1 - 1;
5588 	}
5589     }
5590     else
5591     {
5592 	if (cap->cmdchar == 'M')
5593 	{
5594 #ifdef FEAT_DIFF
5595 	    /* Don't count filler lines above the window. */
5596 	    used -= diff_check_fill(curwin, curwin->w_topline)
5597 							  - curwin->w_topfill;
5598 #endif
5599 	    validate_botline();	    /* make sure w_empty_rows is valid */
5600 	    half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5601 	    for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5602 	    {
5603 #ifdef FEAT_DIFF
5604 		/* Count half he number of filler lines to be "below this
5605 		 * line" and half to be "above the next line". */
5606 		if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5607 							     + n) / 2 >= half)
5608 		{
5609 		    --n;
5610 		    break;
5611 		}
5612 #endif
5613 		used += plines(curwin->w_topline + n);
5614 		if (used >= half)
5615 		    break;
5616 #ifdef FEAT_FOLDING
5617 		if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5618 		    n = lnum - curwin->w_topline;
5619 #endif
5620 	    }
5621 	    if (n > 0 && used > curwin->w_height)
5622 		--n;
5623 	}
5624 	else /* (cap->cmdchar == 'H') */
5625 	{
5626 	    n = cap->count1 - 1;
5627 #ifdef FEAT_FOLDING
5628 	    if (hasAnyFolding(curwin))
5629 	    {
5630 		/* Count a fold for one screen line. */
5631 		lnum = curwin->w_topline;
5632 		while (n-- > 0 && lnum < curwin->w_botline - 1)
5633 		{
5634 		    hasFolding(lnum, NULL, &lnum);
5635 		    ++lnum;
5636 		}
5637 		n = lnum - curwin->w_topline;
5638 	    }
5639 #endif
5640 	}
5641 	curwin->w_cursor.lnum = curwin->w_topline + n;
5642 	if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5643 	    curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5644     }
5645 
5646     cursor_correct();	/* correct for 'so' */
5647     beginline(BL_SOL | BL_FIX);
5648 }
5649 
5650 /*
5651  * Cursor right commands.
5652  */
5653     static void
5654 nv_right(cap)
5655     cmdarg_T	*cap;
5656 {
5657     long	n;
5658 #ifdef FEAT_VISUAL
5659     int		PAST_LINE;
5660 #else
5661 # define PAST_LINE 0
5662 #endif
5663 
5664     if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5665     {
5666 	/* <C-Right> and <S-Right> move a word or WORD right */
5667 	if (mod_mask & MOD_MASK_CTRL)
5668 	    cap->arg = TRUE;
5669 	nv_wordcmd(cap);
5670 	return;
5671     }
5672 
5673     cap->oap->motion_type = MCHAR;
5674     cap->oap->inclusive = FALSE;
5675 #ifdef FEAT_VISUAL
5676     PAST_LINE = (VIsual_active && *p_sel != 'o');
5677 
5678 # ifdef FEAT_VIRTUALEDIT
5679     /*
5680      * In virtual mode, there's no such thing as "PAST_LINE", as lines are
5681      * (theoretically) infinitly long.
5682      */
5683     if (virtual_active())
5684 	PAST_LINE = 0;
5685 # endif
5686 #endif
5687 
5688     for (n = cap->count1; n > 0; --n)
5689     {
5690 	if ((!PAST_LINE && oneright() == FAIL)
5691 		|| (PAST_LINE && *ml_get_cursor() == NUL))
5692 	{
5693 	    /*
5694 	     *	  <Space> wraps to next line if 'whichwrap' has 's'.
5695 	     *	      'l' wraps to next line if 'whichwrap' has 'l'.
5696 	     * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
5697 	     */
5698 	    if (       ((cap->cmdchar == ' '
5699 			    && vim_strchr(p_ww, 's') != NULL)
5700 			|| (cap->cmdchar == 'l'
5701 			    && vim_strchr(p_ww, 'l') != NULL)
5702 			|| (cap->cmdchar == K_RIGHT
5703 			    && vim_strchr(p_ww, '>') != NULL))
5704 		    && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5705 	    {
5706 		/* When deleting we also count the NL as a character.
5707 		 * Set cap->oap->inclusive when last char in the line is
5708 		 * included, move to next line after that */
5709 		if (	   cap->oap->op_type != OP_NOP
5710 			&& !cap->oap->inclusive
5711 			&& !lineempty(curwin->w_cursor.lnum))
5712 		    cap->oap->inclusive = TRUE;
5713 		else
5714 		{
5715 		    ++curwin->w_cursor.lnum;
5716 		    curwin->w_cursor.col = 0;
5717 #ifdef FEAT_VIRTUALEDIT
5718 		    curwin->w_cursor.coladd = 0;
5719 #endif
5720 		    curwin->w_set_curswant = TRUE;
5721 		    cap->oap->inclusive = FALSE;
5722 		}
5723 		continue;
5724 	    }
5725 	    if (cap->oap->op_type == OP_NOP)
5726 	    {
5727 		/* Only beep and flush if not moved at all */
5728 		if (n == cap->count1)
5729 		    beep_flush();
5730 	    }
5731 	    else
5732 	    {
5733 		if (!lineempty(curwin->w_cursor.lnum))
5734 		    cap->oap->inclusive = TRUE;
5735 	    }
5736 	    break;
5737 	}
5738 #ifdef FEAT_VISUAL
5739 	else if (PAST_LINE)
5740 	{
5741 	    curwin->w_set_curswant = TRUE;
5742 # ifdef FEAT_VIRTUALEDIT
5743 	    if (virtual_active())
5744 		oneright();
5745 	    else
5746 # endif
5747 	    {
5748 # ifdef FEAT_MBYTE
5749 		if (has_mbyte)
5750 		    curwin->w_cursor.col +=
5751 					 (*mb_ptr2len)(ml_get_cursor());
5752 		else
5753 # endif
5754 		    ++curwin->w_cursor.col;
5755 	    }
5756 	}
5757 #endif
5758     }
5759 #ifdef FEAT_FOLDING
5760     if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5761 					       && cap->oap->op_type == OP_NOP)
5762 	foldOpenCursor();
5763 #endif
5764 }
5765 
5766 /*
5767  * Cursor left commands.
5768  *
5769  * Returns TRUE when operator end should not be adjusted.
5770  */
5771     static void
5772 nv_left(cap)
5773     cmdarg_T	*cap;
5774 {
5775     long	n;
5776 
5777     if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5778     {
5779 	/* <C-Left> and <S-Left> move a word or WORD left */
5780 	if (mod_mask & MOD_MASK_CTRL)
5781 	    cap->arg = 1;
5782 	nv_bck_word(cap);
5783 	return;
5784     }
5785 
5786     cap->oap->motion_type = MCHAR;
5787     cap->oap->inclusive = FALSE;
5788     for (n = cap->count1; n > 0; --n)
5789     {
5790 	if (oneleft() == FAIL)
5791 	{
5792 	    /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
5793 	     *		 'h' wraps to previous line if 'whichwrap' has 'h'.
5794 	     *	   CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
5795 	     */
5796 	    if (       (((cap->cmdchar == K_BS
5797 				|| cap->cmdchar == Ctrl_H)
5798 			    && vim_strchr(p_ww, 'b') != NULL)
5799 			|| (cap->cmdchar == 'h'
5800 			    && vim_strchr(p_ww, 'h') != NULL)
5801 			|| (cap->cmdchar == K_LEFT
5802 			    && vim_strchr(p_ww, '<') != NULL))
5803 		    && curwin->w_cursor.lnum > 1)
5804 	    {
5805 		--(curwin->w_cursor.lnum);
5806 		coladvance((colnr_T)MAXCOL);
5807 		curwin->w_set_curswant = TRUE;
5808 
5809 		/* When the NL before the first char has to be deleted we
5810 		 * put the cursor on the NUL after the previous line.
5811 		 * This is a very special case, be careful!
5812 		 * don't adjust op_end now, otherwise it won't work */
5813 		if (	   (cap->oap->op_type == OP_DELETE
5814 			    || cap->oap->op_type == OP_CHANGE)
5815 			&& !lineempty(curwin->w_cursor.lnum))
5816 		{
5817 		    ++curwin->w_cursor.col;
5818 		    cap->retval |= CA_NO_ADJ_OP_END;
5819 		}
5820 		continue;
5821 	    }
5822 	    /* Only beep and flush if not moved at all */
5823 	    else if (cap->oap->op_type == OP_NOP && n == cap->count1)
5824 		beep_flush();
5825 	    break;
5826 	}
5827     }
5828 #ifdef FEAT_FOLDING
5829     if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5830 					       && cap->oap->op_type == OP_NOP)
5831 	foldOpenCursor();
5832 #endif
5833 }
5834 
5835 /*
5836  * Cursor up commands.
5837  * cap->arg is TRUE for "-": Move cursor to first non-blank.
5838  */
5839     static void
5840 nv_up(cap)
5841     cmdarg_T	*cap;
5842 {
5843     if (mod_mask & MOD_MASK_SHIFT)
5844     {
5845 	/* <S-Up> is page up */
5846 	cap->arg = BACKWARD;
5847 	nv_page(cap);
5848     }
5849     else
5850     {
5851 	cap->oap->motion_type = MLINE;
5852 	if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5853 	    clearopbeep(cap->oap);
5854 	else if (cap->arg)
5855 	    beginline(BL_WHITE | BL_FIX);
5856     }
5857 }
5858 
5859 /*
5860  * Cursor down commands.
5861  * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
5862  */
5863     static void
5864 nv_down(cap)
5865     cmdarg_T	*cap;
5866 {
5867     if (mod_mask & MOD_MASK_SHIFT)
5868     {
5869 	/* <S-Down> is page down */
5870 	cap->arg = FORWARD;
5871 	nv_page(cap);
5872     }
5873     else
5874 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5875     /* In a quickfix window a <CR> jumps to the error under the cursor. */
5876     if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
5877 	if (curwin->w_llist_ref == NULL)
5878 	    do_cmdline_cmd((char_u *)".cc");	/* quickfix window */
5879 	else
5880 	    do_cmdline_cmd((char_u *)".ll");	/* location list window */
5881     else
5882 #endif
5883     {
5884 #ifdef FEAT_CMDWIN
5885 	/* In the cmdline window a <CR> executes the command. */
5886 	if (cmdwin_type != 0 && cap->cmdchar == CAR)
5887 	    cmdwin_result = CAR;
5888 	else
5889 #endif
5890 	{
5891 	    cap->oap->motion_type = MLINE;
5892 	    if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5893 		clearopbeep(cap->oap);
5894 	    else if (cap->arg)
5895 		beginline(BL_WHITE | BL_FIX);
5896 	}
5897     }
5898 }
5899 
5900 #ifdef FEAT_SEARCHPATH
5901 /*
5902  * Grab the file name under the cursor and edit it.
5903  */
5904     static void
5905 nv_gotofile(cap)
5906     cmdarg_T	*cap;
5907 {
5908     char_u	*ptr;
5909     linenr_T	lnum = -1;
5910 
5911     if (text_locked())
5912     {
5913 	clearopbeep(cap->oap);
5914 	text_locked_msg();
5915 	return;
5916     }
5917 
5918     ptr = grab_file_name(cap->count1, &lnum);
5919 
5920     if (ptr != NULL)
5921     {
5922 	/* do autowrite if necessary */
5923 	if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
5924 	    autowrite(curbuf, FALSE);
5925 	setpcmark();
5926 	(void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
5927 					       P_HID(curbuf) ? ECMD_HIDE : 0);
5928 	if (cap->nchar == 'F' && lnum >= 0)
5929 	{
5930 	    curwin->w_cursor.lnum = lnum;
5931 	    check_cursor_lnum();
5932 	    beginline(BL_SOL | BL_FIX);
5933 	}
5934 	vim_free(ptr);
5935     }
5936     else
5937 	clearop(cap->oap);
5938 }
5939 #endif
5940 
5941 /*
5942  * <End> command: to end of current line or last line.
5943  */
5944     static void
5945 nv_end(cap)
5946     cmdarg_T	*cap;
5947 {
5948     if (cap->arg || (mod_mask & MOD_MASK_CTRL))	/* CTRL-END = goto last line */
5949     {
5950 	cap->arg = TRUE;
5951 	nv_goto(cap);
5952 	cap->count1 = 1;		/* to end of current line */
5953     }
5954     nv_dollar(cap);
5955 }
5956 
5957 /*
5958  * Handle the "$" command.
5959  */
5960     static void
5961 nv_dollar(cap)
5962     cmdarg_T	*cap;
5963 {
5964     cap->oap->motion_type = MCHAR;
5965     cap->oap->inclusive = TRUE;
5966 #ifdef FEAT_VIRTUALEDIT
5967     /* In virtual mode when off the edge of a line and an operator
5968      * is pending (whew!) keep the cursor where it is.
5969      * Otherwise, send it to the end of the line. */
5970     if (!virtual_active() || gchar_cursor() != NUL
5971 					       || cap->oap->op_type == OP_NOP)
5972 #endif
5973 	curwin->w_curswant = MAXCOL;	/* so we stay at the end */
5974     if (cursor_down((long)(cap->count1 - 1),
5975 					 cap->oap->op_type == OP_NOP) == FAIL)
5976 	clearopbeep(cap->oap);
5977 #ifdef FEAT_FOLDING
5978     else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
5979 	foldOpenCursor();
5980 #endif
5981 }
5982 
5983 /*
5984  * Implementation of '?' and '/' commands.
5985  * If cap->arg is TRUE don't set PC mark.
5986  */
5987     static void
5988 nv_search(cap)
5989     cmdarg_T	    *cap;
5990 {
5991     oparg_T	*oap = cap->oap;
5992 
5993     if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
5994     {
5995 	/* Translate "g??" to "g?g?" */
5996 	cap->cmdchar = 'g';
5997 	cap->nchar = '?';
5998 	nv_operator(cap);
5999 	return;
6000     }
6001 
6002     cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6003 
6004     if (cap->searchbuf == NULL)
6005     {
6006 	clearop(oap);
6007 	return;
6008     }
6009 
6010     normal_search(cap, cap->cmdchar, cap->searchbuf,
6011 						(cap->arg ? 0 : SEARCH_MARK));
6012 }
6013 
6014 /*
6015  * Handle "N" and "n" commands.
6016  * cap->arg is SEARCH_REV for "N", 0 for "n".
6017  */
6018     static void
6019 nv_next(cap)
6020     cmdarg_T	*cap;
6021 {
6022     normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6023 }
6024 
6025 /*
6026  * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6027  * Uses only cap->count1 and cap->oap from "cap".
6028  */
6029     static void
6030 normal_search(cap, dir, pat, opt)
6031     cmdarg_T	*cap;
6032     int		dir;
6033     char_u	*pat;
6034     int		opt;		/* extra flags for do_search() */
6035 {
6036     int		i;
6037 
6038     cap->oap->motion_type = MCHAR;
6039     cap->oap->inclusive = FALSE;
6040     cap->oap->use_reg_one = TRUE;
6041     curwin->w_set_curswant = TRUE;
6042 
6043     i = do_search(cap->oap, dir, pat, cap->count1,
6044 				 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG);
6045     if (i == 0)
6046 	clearop(cap->oap);
6047     else
6048     {
6049 	if (i == 2)
6050 	    cap->oap->motion_type = MLINE;
6051 #ifdef FEAT_VIRTUALEDIT
6052 	curwin->w_cursor.coladd = 0;
6053 #endif
6054 #ifdef FEAT_FOLDING
6055 	if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6056 	    foldOpenCursor();
6057 #endif
6058     }
6059 
6060     /* "/$" will put the cursor after the end of the line, may need to
6061      * correct that here */
6062     check_cursor();
6063 }
6064 
6065 /*
6066  * Character search commands.
6067  * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6068  * ',' and FALSE for ';'.
6069  * cap->nchar is NUL for ',' and ';' (repeat the search)
6070  */
6071     static void
6072 nv_csearch(cap)
6073     cmdarg_T	*cap;
6074 {
6075     int		t_cmd;
6076 
6077     if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6078 	t_cmd = TRUE;
6079     else
6080 	t_cmd = FALSE;
6081 
6082     cap->oap->motion_type = MCHAR;
6083     if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6084 	clearopbeep(cap->oap);
6085     else
6086     {
6087 	curwin->w_set_curswant = TRUE;
6088 #ifdef FEAT_VIRTUALEDIT
6089 	/* Include a Tab for "tx" and for "dfx". */
6090 	if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6091 		&& (t_cmd || cap->oap->op_type != OP_NOP))
6092 	{
6093 	    colnr_T	scol, ecol;
6094 
6095 	    getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6096 	    curwin->w_cursor.coladd = ecol - scol;
6097 	}
6098 	else
6099 	    curwin->w_cursor.coladd = 0;
6100 #endif
6101 #ifdef FEAT_VISUAL
6102 	adjust_for_sel(cap);
6103 #endif
6104 #ifdef FEAT_FOLDING
6105 	if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6106 	    foldOpenCursor();
6107 #endif
6108     }
6109 }
6110 
6111 /*
6112  * "[" and "]" commands.
6113  * cap->arg is BACKWARD for "[" and FORWARD for "]".
6114  */
6115     static void
6116 nv_brackets(cap)
6117     cmdarg_T	*cap;
6118 {
6119     pos_T	new_pos;
6120     pos_T	prev_pos;
6121     pos_T	*pos = NULL;	    /* init for GCC */
6122     pos_T	old_pos;	    /* cursor position before command */
6123     int		flag;
6124     long	n;
6125     int		findc;
6126     int		c;
6127 
6128     cap->oap->motion_type = MCHAR;
6129     cap->oap->inclusive = FALSE;
6130     old_pos = curwin->w_cursor;
6131 #ifdef FEAT_VIRTUALEDIT
6132     curwin->w_cursor.coladd = 0;	    /* TODO: don't do this for an error. */
6133 #endif
6134 
6135 #ifdef FEAT_SEARCHPATH
6136     /*
6137      * "[f" or "]f" : Edit file under the cursor (same as "gf")
6138      */
6139     if (cap->nchar == 'f')
6140 	nv_gotofile(cap);
6141     else
6142 #endif
6143 
6144 #ifdef FEAT_FIND_ID
6145     /*
6146      * Find the occurence(s) of the identifier or define under cursor
6147      * in current and included files or jump to the first occurence.
6148      *
6149      *			search	     list	    jump
6150      *		      fwd   bwd    fwd	 bwd	 fwd	bwd
6151      * identifier     "]i"  "[i"   "]I"  "[I"	"]^I"  "[^I"
6152      * define	      "]d"  "[d"   "]D"  "[D"	"]^D"  "[^D"
6153      */
6154     if (vim_strchr((char_u *)
6155 #ifdef EBCDIC
6156 		"iI\005dD\067",
6157 #else
6158 		"iI\011dD\004",
6159 #endif
6160 		cap->nchar) != NULL)
6161     {
6162 	char_u	*ptr;
6163 	int	len;
6164 
6165 	if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6166 	    clearop(cap->oap);
6167 	else
6168 	{
6169 	    find_pattern_in_path(ptr, 0, len, TRUE,
6170 		cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6171 		((cap->nchar & 0xf) == ('d' & 0xf)) ?  FIND_DEFINE : FIND_ANY,
6172 		cap->count1,
6173 		isupper(cap->nchar) ? ACTION_SHOW_ALL :
6174 			    islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6175 		cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6176 		(linenr_T)MAXLNUM);
6177 	    curwin->w_set_curswant = TRUE;
6178 	}
6179     }
6180     else
6181 #endif
6182 
6183     /*
6184      * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6185      * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6186      * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6187      * "[m" or "]m" search for prev/next start of (Java) method.
6188      * "[M" or "]M" search for prev/next end of (Java) method.
6189      */
6190     if (  (cap->cmdchar == '['
6191 		&& vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6192 	    || (cap->cmdchar == ']'
6193 		&& vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6194     {
6195 	if (cap->nchar == '*')
6196 	    cap->nchar = '/';
6197 	new_pos.lnum = 0;
6198 	prev_pos.lnum = 0;
6199 	if (cap->nchar == 'm' || cap->nchar == 'M')
6200 	{
6201 	    if (cap->cmdchar == '[')
6202 		findc = '{';
6203 	    else
6204 		findc = '}';
6205 	    n = 9999;
6206 	}
6207 	else
6208 	{
6209 	    findc = cap->nchar;
6210 	    n = cap->count1;
6211 	}
6212 	for ( ; n > 0; --n)
6213 	{
6214 	    if ((pos = findmatchlimit(cap->oap, findc,
6215 		(cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6216 	    {
6217 		if (new_pos.lnum == 0)	/* nothing found */
6218 		{
6219 		    if (cap->nchar != 'm' && cap->nchar != 'M')
6220 			clearopbeep(cap->oap);
6221 		}
6222 		else
6223 		    pos = &new_pos;	/* use last one found */
6224 		break;
6225 	    }
6226 	    prev_pos = new_pos;
6227 	    curwin->w_cursor = *pos;
6228 	    new_pos = *pos;
6229 	}
6230 	curwin->w_cursor = old_pos;
6231 
6232 	/*
6233 	 * Handle "[m", "]m", "[M" and "[M".  The findmatchlimit() only
6234 	 * brought us to the match for "[m" and "]M" when inside a method.
6235 	 * Try finding the '{' or '}' we want to be at.
6236 	 * Also repeat for the given count.
6237 	 */
6238 	if (cap->nchar == 'm' || cap->nchar == 'M')
6239 	{
6240 	    /* norm is TRUE for "]M" and "[m" */
6241 	    int	    norm = ((findc == '{') == (cap->nchar == 'm'));
6242 
6243 	    n = cap->count1;
6244 	    /* found a match: we were inside a method */
6245 	    if (prev_pos.lnum != 0)
6246 	    {
6247 		pos = &prev_pos;
6248 		curwin->w_cursor = prev_pos;
6249 		if (norm)
6250 		    --n;
6251 	    }
6252 	    else
6253 		pos = NULL;
6254 	    while (n > 0)
6255 	    {
6256 		for (;;)
6257 		{
6258 		    if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6259 		    {
6260 			/* if not found anything, that's an error */
6261 			if (pos == NULL)
6262 			    clearopbeep(cap->oap);
6263 			n = 0;
6264 			break;
6265 		    }
6266 		    c = gchar_cursor();
6267 		    if (c == '{' || c == '}')
6268 		    {
6269 			/* Must have found end/start of class: use it.
6270 			 * Or found the place to be at. */
6271 			if ((c == findc && norm) || (n == 1 && !norm))
6272 			{
6273 			    new_pos = curwin->w_cursor;
6274 			    pos = &new_pos;
6275 			    n = 0;
6276 			}
6277 			/* if no match found at all, we started outside of the
6278 			 * class and we're inside now.  Just go on. */
6279 			else if (new_pos.lnum == 0)
6280 			{
6281 			    new_pos = curwin->w_cursor;
6282 			    pos = &new_pos;
6283 			}
6284 			/* found start/end of other method: go to match */
6285 			else if ((pos = findmatchlimit(cap->oap, findc,
6286 			    (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6287 								  0)) == NULL)
6288 			    n = 0;
6289 			else
6290 			    curwin->w_cursor = *pos;
6291 			break;
6292 		    }
6293 		}
6294 		--n;
6295 	    }
6296 	    curwin->w_cursor = old_pos;
6297 	    if (pos == NULL && new_pos.lnum != 0)
6298 		clearopbeep(cap->oap);
6299 	}
6300 	if (pos != NULL)
6301 	{
6302 	    setpcmark();
6303 	    curwin->w_cursor = *pos;
6304 	    curwin->w_set_curswant = TRUE;
6305 #ifdef FEAT_FOLDING
6306 	    if ((fdo_flags & FDO_BLOCK) && KeyTyped
6307 					       && cap->oap->op_type == OP_NOP)
6308 		foldOpenCursor();
6309 #endif
6310 	}
6311     }
6312 
6313     /*
6314      * "[[", "[]", "]]" and "][": move to start or end of function
6315      */
6316     else if (cap->nchar == '[' || cap->nchar == ']')
6317     {
6318 	if (cap->nchar == cap->cmdchar)		    /* "]]" or "[[" */
6319 	    flag = '{';
6320 	else
6321 	    flag = '}';		    /* "][" or "[]" */
6322 
6323 	curwin->w_set_curswant = TRUE;
6324 	/*
6325 	 * Imitate strange Vi behaviour: When using "]]" with an operator
6326 	 * we also stop at '}'.
6327 	 */
6328 	if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
6329 	      (cap->oap->op_type != OP_NOP
6330 				      && cap->arg == FORWARD && flag == '{')))
6331 	    clearopbeep(cap->oap);
6332 	else
6333 	{
6334 	    if (cap->oap->op_type == OP_NOP)
6335 		beginline(BL_WHITE | BL_FIX);
6336 #ifdef FEAT_FOLDING
6337 	    if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6338 		foldOpenCursor();
6339 #endif
6340 	}
6341     }
6342 
6343     /*
6344      * "[p", "[P", "]P" and "]p": put with indent adjustment
6345      */
6346     else if (cap->nchar == 'p' || cap->nchar == 'P')
6347     {
6348 	if (!checkclearopq(cap->oap))
6349 	{
6350 	    prep_redo_cmd(cap);
6351 	    do_put(cap->oap->regname,
6352 	      (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6353 						  cap->count1, PUT_FIXINDENT);
6354 	}
6355     }
6356 
6357     /*
6358      * "['", "[`", "]'" and "]`": jump to next mark
6359      */
6360     else if (cap->nchar == '\'' || cap->nchar == '`')
6361     {
6362 	pos = &curwin->w_cursor;
6363 	for (n = cap->count1; n > 0; --n)
6364 	{
6365 	    prev_pos = *pos;
6366 	    pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6367 							  cap->nchar == '\'');
6368 	    if (pos == NULL)
6369 		break;
6370 	}
6371 	if (pos == NULL)
6372 	    pos = &prev_pos;
6373 	nv_cursormark(cap, cap->nchar == '\'', pos);
6374     }
6375 
6376 #ifdef FEAT_MOUSE
6377     /*
6378      * [ or ] followed by a middle mouse click: put selected text with
6379      * indent adjustment.  Any other button just does as usual.
6380      */
6381     else if (cap->nchar >= K_LEFTMOUSE && cap->nchar <= K_RIGHTRELEASE)
6382     {
6383 	(void)do_mouse(cap->oap, cap->nchar,
6384 		       (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6385 		       cap->count1, PUT_FIXINDENT);
6386     }
6387 #endif /* FEAT_MOUSE */
6388 
6389 #ifdef FEAT_FOLDING
6390     /*
6391      * "[z" and "]z": move to start or end of open fold.
6392      */
6393     else if (cap->nchar == 'z')
6394     {
6395 	if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6396 							 cap->count1) == FAIL)
6397 	    clearopbeep(cap->oap);
6398     }
6399 #endif
6400 
6401 #ifdef FEAT_DIFF
6402     /*
6403      * "[c" and "]c": move to next or previous diff-change.
6404      */
6405     else if (cap->nchar == 'c')
6406     {
6407 	if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6408 							 cap->count1) == FAIL)
6409 	    clearopbeep(cap->oap);
6410     }
6411 #endif
6412 
6413 #ifdef FEAT_SPELL
6414     /*
6415      * "[s", "[S", "]s" and "]S": move to next spell error.
6416      */
6417     else if (cap->nchar == 's' || cap->nchar == 'S')
6418     {
6419 	setpcmark();
6420 	for (n = 0; n < cap->count1; ++n)
6421 	    if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6422 			  cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
6423 	    {
6424 		clearopbeep(cap->oap);
6425 		break;
6426 	    }
6427     }
6428 #endif
6429 
6430     /* Not a valid cap->nchar. */
6431     else
6432 	clearopbeep(cap->oap);
6433 }
6434 
6435 /*
6436  * Handle Normal mode "%" command.
6437  */
6438     static void
6439 nv_percent(cap)
6440     cmdarg_T	*cap;
6441 {
6442     pos_T	*pos;
6443 #ifdef FEAT_FOLDING
6444     linenr_T	lnum = curwin->w_cursor.lnum;
6445 #endif
6446 
6447     cap->oap->inclusive = TRUE;
6448     if (cap->count0)	    /* {cnt}% : goto {cnt} percentage in file */
6449     {
6450 	if (cap->count0 > 100)
6451 	    clearopbeep(cap->oap);
6452 	else
6453 	{
6454 	    cap->oap->motion_type = MLINE;
6455 	    setpcmark();
6456 	    /* Round up, so CTRL-G will give same value.  Watch out for a
6457 	     * large line count, the line number must not go negative! */
6458 	    if (curbuf->b_ml.ml_line_count > 1000000)
6459 		curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6460 							 / 100L * cap->count0;
6461 	    else
6462 		curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6463 						    cap->count0 + 99L) / 100L;
6464 	    if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6465 		curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6466 	    beginline(BL_SOL | BL_FIX);
6467 	}
6468     }
6469     else		    /* "%" : go to matching paren */
6470     {
6471 	cap->oap->motion_type = MCHAR;
6472 	cap->oap->use_reg_one = TRUE;
6473 	if ((pos = findmatch(cap->oap, NUL)) == NULL)
6474 	    clearopbeep(cap->oap);
6475 	else
6476 	{
6477 	    setpcmark();
6478 	    curwin->w_cursor = *pos;
6479 	    curwin->w_set_curswant = TRUE;
6480 #ifdef FEAT_VIRTUALEDIT
6481 	    curwin->w_cursor.coladd = 0;
6482 #endif
6483 #ifdef FEAT_VISUAL
6484 	    adjust_for_sel(cap);
6485 #endif
6486 	}
6487     }
6488 #ifdef FEAT_FOLDING
6489     if (cap->oap->op_type == OP_NOP
6490 	    && lnum != curwin->w_cursor.lnum
6491 	    && (fdo_flags & FDO_PERCENT)
6492 	    && KeyTyped)
6493 	foldOpenCursor();
6494 #endif
6495 }
6496 
6497 /*
6498  * Handle "(" and ")" commands.
6499  * cap->arg is BACKWARD for "(" and FORWARD for ")".
6500  */
6501     static void
6502 nv_brace(cap)
6503     cmdarg_T	*cap;
6504 {
6505     cap->oap->motion_type = MCHAR;
6506     cap->oap->use_reg_one = TRUE;
6507     /* The motion used to be inclusive for "(", but that is not what Vi does. */
6508     cap->oap->inclusive = FALSE;
6509     curwin->w_set_curswant = TRUE;
6510 
6511     if (findsent(cap->arg, cap->count1) == FAIL)
6512 	clearopbeep(cap->oap);
6513     else
6514     {
6515 #ifdef FEAT_VIRTUALEDIT
6516 	curwin->w_cursor.coladd = 0;
6517 #endif
6518 #ifdef FEAT_FOLDING
6519 	if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6520 	    foldOpenCursor();
6521 #endif
6522     }
6523 }
6524 
6525 /*
6526  * "m" command: Mark a position.
6527  */
6528     static void
6529 nv_mark(cap)
6530     cmdarg_T	*cap;
6531 {
6532     if (!checkclearop(cap->oap))
6533     {
6534 	if (setmark(cap->nchar) == FAIL)
6535 	    clearopbeep(cap->oap);
6536     }
6537 }
6538 
6539 /*
6540  * "{" and "}" commands.
6541  * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6542  */
6543     static void
6544 nv_findpar(cap)
6545     cmdarg_T	*cap;
6546 {
6547     cap->oap->motion_type = MCHAR;
6548     cap->oap->inclusive = FALSE;
6549     cap->oap->use_reg_one = TRUE;
6550     curwin->w_set_curswant = TRUE;
6551     if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
6552 	clearopbeep(cap->oap);
6553     else
6554     {
6555 #ifdef FEAT_VIRTUALEDIT
6556 	curwin->w_cursor.coladd = 0;
6557 #endif
6558 #ifdef FEAT_FOLDING
6559 	if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6560 	    foldOpenCursor();
6561 #endif
6562     }
6563 }
6564 
6565 /*
6566  * "u" command: Undo or make lower case.
6567  */
6568     static void
6569 nv_undo(cap)
6570     cmdarg_T	*cap;
6571 {
6572     if (cap->oap->op_type == OP_LOWER
6573 #ifdef FEAT_VISUAL
6574 	    || VIsual_active
6575 #endif
6576 	    )
6577     {
6578 	/* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6579 	cap->cmdchar = 'g';
6580 	cap->nchar = 'u';
6581 	nv_operator(cap);
6582     }
6583     else
6584 	nv_kundo(cap);
6585 }
6586 
6587 /*
6588  * <Undo> command.
6589  */
6590     static void
6591 nv_kundo(cap)
6592     cmdarg_T	*cap;
6593 {
6594     if (!checkclearopq(cap->oap))
6595     {
6596 	u_undo((int)cap->count1);
6597 	curwin->w_set_curswant = TRUE;
6598     }
6599 }
6600 
6601 /*
6602  * Handle the "r" command.
6603  */
6604     static void
6605 nv_replace(cap)
6606     cmdarg_T	*cap;
6607 {
6608     char_u	*ptr;
6609     int		had_ctrl_v;
6610     long	n;
6611 
6612     if (checkclearop(cap->oap))
6613 	return;
6614 
6615     /* get another character */
6616     if (cap->nchar == Ctrl_V)
6617     {
6618 	had_ctrl_v = Ctrl_V;
6619 	cap->nchar = get_literal();
6620 	/* Don't redo a multibyte character with CTRL-V. */
6621 	if (cap->nchar > DEL)
6622 	    had_ctrl_v = NUL;
6623     }
6624     else
6625 	had_ctrl_v = NUL;
6626 
6627 #ifdef FEAT_VISUAL
6628     /* Visual mode "r" */
6629     if (VIsual_active)
6630     {
6631 	nv_operator(cap);
6632 	return;
6633     }
6634 #endif
6635 
6636 #ifdef FEAT_VIRTUALEDIT
6637     /* Break tabs, etc. */
6638     if (virtual_active())
6639     {
6640 	if (u_save_cursor() == FAIL)
6641 	    return;
6642 	if (gchar_cursor() == NUL)
6643 	{
6644 	    /* Add extra space and put the cursor on the first one. */
6645 	    coladvance_force((colnr_T)(getviscol() + cap->count1));
6646 	    curwin->w_cursor.col -= cap->count1;
6647 	}
6648 	else if (gchar_cursor() == TAB)
6649 	    coladvance_force(getviscol());
6650     }
6651 #endif
6652 
6653     /*
6654      * Check for a special key or not enough characters to replace.
6655      */
6656     ptr = ml_get_cursor();
6657     if (IS_SPECIAL(cap->nchar) || STRLEN(ptr) < (unsigned)cap->count1
6658 #ifdef FEAT_MBYTE
6659 	    || (has_mbyte && mb_charlen(ptr) < cap->count1)
6660 #endif
6661 	    )
6662     {
6663 	clearopbeep(cap->oap);
6664 	return;
6665     }
6666 
6667     /*
6668      * Replacing with a TAB is done by edit() when it is complicated because
6669      * 'expandtab' or 'smarttab' is set.  CTRL-V TAB inserts a literal TAB.
6670      * Other characters are done below to avoid problems with things like
6671      * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6672      */
6673     if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6674     {
6675 	stuffnumReadbuff(cap->count1);
6676 	stuffcharReadbuff('R');
6677 	stuffcharReadbuff('\t');
6678 	stuffcharReadbuff(ESC);
6679 	return;
6680     }
6681 
6682     /* save line for undo */
6683     if (u_save_cursor() == FAIL)
6684 	return;
6685 
6686     if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6687     {
6688 	/*
6689 	 * Replace character(s) by a single newline.
6690 	 * Strange vi behaviour: Only one newline is inserted.
6691 	 * Delete the characters here.
6692 	 * Insert the newline with an insert command, takes care of
6693 	 * autoindent.	The insert command depends on being on the last
6694 	 * character of a line or not.
6695 	 */
6696 #ifdef FEAT_MBYTE
6697 	(void)del_chars(cap->count1, FALSE);	/* delete the characters */
6698 #else
6699 	(void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
6700 #endif
6701 	stuffcharReadbuff('\r');
6702 	stuffcharReadbuff(ESC);
6703 
6704 	/* Give 'r' to edit(), to get the redo command right. */
6705 	invoke_edit(cap, TRUE, 'r', FALSE);
6706     }
6707     else
6708     {
6709 	prep_redo(cap->oap->regname, cap->count1,
6710 				       NUL, 'r', NUL, had_ctrl_v, cap->nchar);
6711 
6712 	curbuf->b_op_start = curwin->w_cursor;
6713 #ifdef FEAT_MBYTE
6714 	if (has_mbyte)
6715 	{
6716 	    int		old_State = State;
6717 
6718 	    if (cap->ncharC1 != 0)
6719 		AppendCharToRedobuff(cap->ncharC1);
6720 	    if (cap->ncharC2 != 0)
6721 		AppendCharToRedobuff(cap->ncharC2);
6722 
6723 	    /* This is slow, but it handles replacing a single-byte with a
6724 	     * multi-byte and the other way around.  Also handles adding
6725 	     * composing characters for utf-8. */
6726 	    for (n = cap->count1; n > 0; --n)
6727 	    {
6728 		State = REPLACE;
6729 		ins_char(cap->nchar);
6730 		State = old_State;
6731 		if (cap->ncharC1 != 0)
6732 		    ins_char(cap->ncharC1);
6733 		if (cap->ncharC2 != 0)
6734 		    ins_char(cap->ncharC2);
6735 	    }
6736 	}
6737 	else
6738 #endif
6739 	{
6740 	    /*
6741 	     * Replace the characters within one line.
6742 	     */
6743 	    for (n = cap->count1; n > 0; --n)
6744 	    {
6745 		/*
6746 		 * Get ptr again, because u_save and/or showmatch() will have
6747 		 * released the line.  At the same time we let know that the
6748 		 * line will be changed.
6749 		 */
6750 		ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
6751 		ptr[curwin->w_cursor.col] = cap->nchar;
6752 		if (p_sm && msg_silent == 0)
6753 		    showmatch(cap->nchar);
6754 		++curwin->w_cursor.col;
6755 	    }
6756 #ifdef FEAT_NETBEANS_INTG
6757 	    if (usingNetbeans)
6758 	    {
6759 		colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
6760 
6761 		netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
6762 							    (long)cap->count1);
6763 		netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
6764 					       &ptr[start], (int)cap->count1);
6765 	    }
6766 #endif
6767 
6768 	    /* mark the buffer as changed and prepare for displaying */
6769 	    changed_bytes(curwin->w_cursor.lnum,
6770 			       (colnr_T)(curwin->w_cursor.col - cap->count1));
6771 	}
6772 	--curwin->w_cursor.col;	    /* cursor on the last replaced char */
6773 #ifdef FEAT_MBYTE
6774 	/* if the character on the left of the current cursor is a multi-byte
6775 	 * character, move two characters left */
6776 	if (has_mbyte)
6777 	    mb_adjust_cursor();
6778 #endif
6779 	curbuf->b_op_end = curwin->w_cursor;
6780 	curwin->w_set_curswant = TRUE;
6781 	set_last_insert(cap->nchar);
6782     }
6783 }
6784 
6785 #ifdef FEAT_VISUAL
6786 /*
6787  * 'o': Exchange start and end of Visual area.
6788  * 'O': same, but in block mode exchange left and right corners.
6789  */
6790     static void
6791 v_swap_corners(cmdchar)
6792     int		cmdchar;
6793 {
6794     pos_T	old_cursor;
6795     colnr_T	left, right;
6796 
6797     if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
6798     {
6799 	old_cursor = curwin->w_cursor;
6800 	getvcols(curwin, &old_cursor, &VIsual, &left, &right);
6801 	curwin->w_cursor.lnum = VIsual.lnum;
6802 	coladvance(left);
6803 	VIsual = curwin->w_cursor;
6804 
6805 	curwin->w_cursor.lnum = old_cursor.lnum;
6806 	curwin->w_curswant = right;
6807 	/* 'selection "exclusive" and cursor at right-bottom corner: move it
6808 	 * right one column */
6809 	if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
6810 	    ++curwin->w_curswant;
6811 	coladvance(curwin->w_curswant);
6812 	if (curwin->w_cursor.col == old_cursor.col
6813 #ifdef FEAT_VIRTUALEDIT
6814 		&& (!virtual_active()
6815 		    || curwin->w_cursor.coladd == old_cursor.coladd)
6816 #endif
6817 		)
6818 	{
6819 	    curwin->w_cursor.lnum = VIsual.lnum;
6820 	    if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
6821 		++right;
6822 	    coladvance(right);
6823 	    VIsual = curwin->w_cursor;
6824 
6825 	    curwin->w_cursor.lnum = old_cursor.lnum;
6826 	    coladvance(left);
6827 	    curwin->w_curswant = left;
6828 	}
6829     }
6830     else
6831     {
6832 	old_cursor = curwin->w_cursor;
6833 	curwin->w_cursor = VIsual;
6834 	VIsual = old_cursor;
6835 	curwin->w_set_curswant = TRUE;
6836     }
6837 }
6838 #endif /* FEAT_VISUAL */
6839 
6840 /*
6841  * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
6842  */
6843     static void
6844 nv_Replace(cap)
6845     cmdarg_T	    *cap;
6846 {
6847 #ifdef FEAT_VISUAL
6848     if (VIsual_active)		/* "R" is replace lines */
6849     {
6850 	cap->cmdchar = 'c';
6851 	cap->nchar = NUL;
6852 	VIsual_mode = 'V';
6853 	nv_operator(cap);
6854     }
6855     else
6856 #endif
6857 	if (!checkclearopq(cap->oap))
6858     {
6859 	if (!curbuf->b_p_ma)
6860 	    EMSG(_(e_modifiable));
6861 	else
6862 	{
6863 #ifdef FEAT_VIRTUALEDIT
6864 	    if (virtual_active())
6865 		coladvance(getviscol());
6866 #endif
6867 	    invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
6868 	}
6869     }
6870 }
6871 
6872 #ifdef FEAT_VREPLACE
6873 /*
6874  * "gr".
6875  */
6876     static void
6877 nv_vreplace(cap)
6878     cmdarg_T	*cap;
6879 {
6880 # ifdef FEAT_VISUAL
6881     if (VIsual_active)
6882     {
6883 	cap->cmdchar = 'r';
6884 	cap->nchar = cap->extra_char;
6885 	nv_replace(cap);	/* Do same as "r" in Visual mode for now */
6886     }
6887     else
6888 # endif
6889 	if (!checkclearopq(cap->oap))
6890     {
6891 	if (!curbuf->b_p_ma)
6892 	    EMSG(_(e_modifiable));
6893 	else
6894 	{
6895 	    if (cap->extra_char == Ctrl_V)	/* get another character */
6896 		cap->extra_char = get_literal();
6897 	    stuffcharReadbuff(cap->extra_char);
6898 	    stuffcharReadbuff(ESC);
6899 # ifdef FEAT_VIRTUALEDIT
6900 	    if (virtual_active())
6901 		coladvance(getviscol());
6902 # endif
6903 	    invoke_edit(cap, TRUE, 'v', FALSE);
6904 	}
6905     }
6906 }
6907 #endif
6908 
6909 /*
6910  * Swap case for "~" command, when it does not work like an operator.
6911  */
6912     static void
6913 n_swapchar(cap)
6914     cmdarg_T	*cap;
6915 {
6916     long	n;
6917     pos_T	startpos;
6918     int		did_change = 0;
6919 #ifdef FEAT_NETBEANS_INTG
6920     pos_T	pos;
6921     char_u	*ptr;
6922     int		count;
6923 #endif
6924 
6925     if (checkclearopq(cap->oap))
6926 	return;
6927 
6928     if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
6929     {
6930 	clearopbeep(cap->oap);
6931 	return;
6932     }
6933 
6934     prep_redo_cmd(cap);
6935 
6936     if (u_save_cursor() == FAIL)
6937 	return;
6938 
6939     startpos = curwin->w_cursor;
6940 #ifdef FEAT_NETBEANS_INTG
6941     pos = startpos;
6942 #endif
6943     for (n = cap->count1; n > 0; --n)
6944     {
6945 	did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
6946 	inc_cursor();
6947 	if (gchar_cursor() == NUL)
6948 	{
6949 	    if (vim_strchr(p_ww, '~') != NULL
6950 		    && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6951 	    {
6952 #ifdef FEAT_NETBEANS_INTG
6953 		if (usingNetbeans)
6954 		{
6955 		    if (did_change)
6956 		    {
6957 			ptr = ml_get(pos.lnum);
6958 			count = STRLEN(ptr) - pos.col;
6959 			netbeans_removed(curbuf, pos.lnum, pos.col,
6960 								 (long)count);
6961 			netbeans_inserted(curbuf, pos.lnum, pos.col,
6962 							&ptr[pos.col], count);
6963 		    }
6964 		    pos.col = 0;
6965 		    pos.lnum++;
6966 		}
6967 #endif
6968 		++curwin->w_cursor.lnum;
6969 		curwin->w_cursor.col = 0;
6970 		if (n > 1)
6971 		{
6972 		    if (u_savesub(curwin->w_cursor.lnum) == FAIL)
6973 			break;
6974 		    u_clearline();
6975 		}
6976 	    }
6977 	    else
6978 		break;
6979 	}
6980     }
6981 #ifdef FEAT_NETBEANS_INTG
6982     if (did_change && usingNetbeans)
6983     {
6984 	ptr = ml_get(pos.lnum);
6985 	count = curwin->w_cursor.col - pos.col;
6986 	netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
6987 	netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
6988     }
6989 #endif
6990 
6991 
6992     check_cursor();
6993     curwin->w_set_curswant = TRUE;
6994     if (did_change)
6995     {
6996 	changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
6997 									  0L);
6998 	curbuf->b_op_start = startpos;
6999 	curbuf->b_op_end = curwin->w_cursor;
7000 	if (curbuf->b_op_end.col > 0)
7001 	    --curbuf->b_op_end.col;
7002     }
7003 }
7004 
7005 /*
7006  * Move cursor to mark.
7007  */
7008     static void
7009 nv_cursormark(cap, flag, pos)
7010     cmdarg_T	*cap;
7011     int		flag;
7012     pos_T	*pos;
7013 {
7014     if (check_mark(pos) == FAIL)
7015 	clearop(cap->oap);
7016     else
7017     {
7018 	if (cap->cmdchar == '\''
7019 		|| cap->cmdchar == '`'
7020 		|| cap->cmdchar == '['
7021 		|| cap->cmdchar == ']')
7022 	    setpcmark();
7023 	curwin->w_cursor = *pos;
7024 	if (flag)
7025 	    beginline(BL_WHITE | BL_FIX);
7026 	else
7027 	    check_cursor();
7028     }
7029     cap->oap->motion_type = flag ? MLINE : MCHAR;
7030     if (cap->cmdchar == '`')
7031 	cap->oap->use_reg_one = TRUE;
7032     cap->oap->inclusive = FALSE;		/* ignored if not MCHAR */
7033     curwin->w_set_curswant = TRUE;
7034 }
7035 
7036 #ifdef FEAT_VISUAL
7037 /*
7038  * Handle commands that are operators in Visual mode.
7039  */
7040     static void
7041 v_visop(cap)
7042     cmdarg_T	*cap;
7043 {
7044     static char_u trans[] = "YyDdCcxdXdAAIIrr";
7045 
7046     /* Uppercase means linewise, except in block mode, then "D" deletes till
7047      * the end of the line, and "C" replaces til EOL */
7048     if (isupper(cap->cmdchar))
7049     {
7050 	if (VIsual_mode != Ctrl_V)
7051 	    VIsual_mode = 'V';
7052 	else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7053 	    curwin->w_curswant = MAXCOL;
7054     }
7055     cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7056     nv_operator(cap);
7057 }
7058 #endif
7059 
7060 /*
7061  * "s" and "S" commands.
7062  */
7063     static void
7064 nv_subst(cap)
7065     cmdarg_T	*cap;
7066 {
7067 #ifdef FEAT_VISUAL
7068     if (VIsual_active)	/* "vs" and "vS" are the same as "vc" */
7069     {
7070 	if (cap->cmdchar == 'S')
7071 	    VIsual_mode = 'V';
7072 	cap->cmdchar = 'c';
7073 	nv_operator(cap);
7074     }
7075     else
7076 #endif
7077 	nv_optrans(cap);
7078 }
7079 
7080 /*
7081  * Abbreviated commands.
7082  */
7083     static void
7084 nv_abbrev(cap)
7085     cmdarg_T	*cap;
7086 {
7087     if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7088 	cap->cmdchar = 'x';		/* DEL key behaves like 'x' */
7089 
7090 #ifdef FEAT_VISUAL
7091     /* in Visual mode these commands are operators */
7092     if (VIsual_active)
7093 	v_visop(cap);
7094     else
7095 #endif
7096 	nv_optrans(cap);
7097 }
7098 
7099 /*
7100  * Translate a command into another command.
7101  */
7102     static void
7103 nv_optrans(cap)
7104     cmdarg_T	*cap;
7105 {
7106     static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7107 			      (char_u *)"d$", (char_u *)"c$",
7108 			      (char_u *)"cl", (char_u *)"cc",
7109 			      (char_u *)"yy", (char_u *)":s\r"};
7110     static char_u *str = (char_u *)"xXDCsSY&";
7111 
7112     if (!checkclearopq(cap->oap))
7113     {
7114 	/* In Vi "2D" doesn't delete the next line.  Can't translate it
7115 	 * either, because "2." should also not use the count. */
7116 	if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7117 	{
7118 	    cap->oap->start = curwin->w_cursor;
7119 	    cap->oap->op_type = OP_DELETE;
7120 	    cap->count1 = 1;
7121 	    nv_dollar(cap);
7122 	    finish_op = TRUE;
7123 	    ResetRedobuff();
7124 	    AppendCharToRedobuff('D');
7125 	}
7126 	else
7127 	{
7128 	    if (cap->count0)
7129 		stuffnumReadbuff(cap->count0);
7130 	    stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7131 	}
7132     }
7133     cap->opcount = 0;
7134 }
7135 
7136 /*
7137  * "'" and "`" commands.  Also for "g'" and "g`".
7138  * cap->arg is TRUE for "'" and "g'".
7139  */
7140     static void
7141 nv_gomark(cap)
7142     cmdarg_T	*cap;
7143 {
7144     pos_T	*pos;
7145     int		c;
7146 #ifdef FEAT_FOLDING
7147     linenr_T	lnum = curwin->w_cursor.lnum;
7148     int		old_KeyTyped = KeyTyped;    /* getting file may reset it */
7149 #endif
7150 
7151     if (cap->cmdchar == 'g')
7152 	c = cap->extra_char;
7153     else
7154 	c = cap->nchar;
7155     pos = getmark(c, (cap->oap->op_type == OP_NOP));
7156     if (pos == (pos_T *)-1)	    /* jumped to other file */
7157     {
7158 	if (cap->arg)
7159 	{
7160 	    check_cursor_lnum();
7161 	    beginline(BL_WHITE | BL_FIX);
7162 	}
7163 	else
7164 	    check_cursor();
7165     }
7166     else
7167 	nv_cursormark(cap, cap->arg, pos);
7168 
7169 #ifdef FEAT_VIRTUALEDIT
7170     /* May need to clear the coladd that a mark includes. */
7171     if (!virtual_active())
7172 	curwin->w_cursor.coladd = 0;
7173 #endif
7174 #ifdef FEAT_FOLDING
7175     if (cap->oap->op_type == OP_NOP
7176 	    && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7177 	    && (fdo_flags & FDO_MARK)
7178 	    && old_KeyTyped)
7179 	foldOpenCursor();
7180 #endif
7181 }
7182 
7183 /*
7184  * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7185  */
7186     static void
7187 nv_pcmark(cap)
7188     cmdarg_T	*cap;
7189 {
7190 #ifdef FEAT_JUMPLIST
7191     pos_T	*pos;
7192 # ifdef FEAT_FOLDING
7193     linenr_T	lnum = curwin->w_cursor.lnum;
7194     int		old_KeyTyped = KeyTyped;    /* getting file may reset it */
7195 # endif
7196 
7197     if (!checkclearopq(cap->oap))
7198     {
7199 	if (cap->cmdchar == 'g')
7200 	    pos = movechangelist((int)cap->count1);
7201 	else
7202 	    pos = movemark((int)cap->count1);
7203 	if (pos == (pos_T *)-1)		/* jump to other file */
7204 	{
7205 	    curwin->w_set_curswant = TRUE;
7206 	    check_cursor();
7207 	}
7208 	else if (pos != NULL)		    /* can jump */
7209 	    nv_cursormark(cap, FALSE, pos);
7210 	else if (cap->cmdchar == 'g')
7211 	{
7212 	    if (curbuf->b_changelistlen == 0)
7213 		EMSG(_("E664: changelist is empty"));
7214 	    else if (cap->count1 < 0)
7215 		EMSG(_("E662: At start of changelist"));
7216 	    else
7217 		EMSG(_("E663: At end of changelist"));
7218 	}
7219 	else
7220 	    clearopbeep(cap->oap);
7221 # ifdef FEAT_FOLDING
7222 	if (cap->oap->op_type == OP_NOP
7223 		&& (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7224 		&& (fdo_flags & FDO_MARK)
7225 		&& old_KeyTyped)
7226 	    foldOpenCursor();
7227 # endif
7228     }
7229 #else
7230     clearopbeep(cap->oap);
7231 #endif
7232 }
7233 
7234 /*
7235  * Handle '"' command.
7236  */
7237     static void
7238 nv_regname(cap)
7239     cmdarg_T	*cap;
7240 {
7241     if (checkclearop(cap->oap))
7242 	return;
7243 #ifdef FEAT_EVAL
7244     if (cap->nchar == '=')
7245 	cap->nchar = get_expr_register();
7246 #endif
7247     if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7248     {
7249 	cap->oap->regname = cap->nchar;
7250 	cap->opcount = cap->count0;	/* remember count before '"' */
7251 #ifdef FEAT_EVAL
7252 	set_reg_var(cap->oap->regname);
7253 #endif
7254     }
7255     else
7256 	clearopbeep(cap->oap);
7257 }
7258 
7259 #ifdef FEAT_VISUAL
7260 /*
7261  * Handle "v", "V" and "CTRL-V" commands.
7262  * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7263  * is TRUE.
7264  * Handle CTRL-Q just like CTRL-V.
7265  */
7266     static void
7267 nv_visual(cap)
7268     cmdarg_T	*cap;
7269 {
7270     if (cap->cmdchar == Ctrl_Q)
7271 	cap->cmdchar = Ctrl_V;
7272 
7273     /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7274      * characterwise, linewise, or blockwise. */
7275     if (cap->oap->op_type != OP_NOP)
7276     {
7277 	cap->oap->motion_force = cap->cmdchar;
7278 	finish_op = FALSE;	/* operator doesn't finish now but later */
7279 	return;
7280     }
7281 
7282     VIsual_select = cap->arg;
7283     if (VIsual_active)	    /* change Visual mode */
7284     {
7285 	if (VIsual_mode == cap->cmdchar)    /* stop visual mode */
7286 	    end_visual_mode();
7287 	else				    /* toggle char/block mode */
7288 	{				    /*	   or char/line mode */
7289 	    VIsual_mode = cap->cmdchar;
7290 	    showmode();
7291 	}
7292 	redraw_curbuf_later(INVERTED);	    /* update the inversion */
7293     }
7294     else		    /* start Visual mode */
7295     {
7296 	check_visual_highlight();
7297 	if (cap->count0)		    /* use previously selected part */
7298 	{
7299 	    if (resel_VIsual_mode == NUL)   /* there is none */
7300 	    {
7301 		beep_flush();
7302 		return;
7303 	    }
7304 	    VIsual = curwin->w_cursor;
7305 
7306 	    VIsual_active = TRUE;
7307 	    VIsual_reselect = TRUE;
7308 	    if (!cap->arg)
7309 		/* start Select mode when 'selectmode' contains "cmd" */
7310 		may_start_select('c');
7311 #ifdef FEAT_MOUSE
7312 	    setmouse();
7313 #endif
7314 	    if (p_smd && msg_silent == 0)
7315 		redraw_cmdline = TRUE;	    /* show visual mode later */
7316 	    /*
7317 	     * For V and ^V, we multiply the number of lines even if there
7318 	     * was only one -- webb
7319 	     */
7320 	    if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7321 	    {
7322 		curwin->w_cursor.lnum +=
7323 				    resel_VIsual_line_count * cap->count0 - 1;
7324 		if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7325 		    curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7326 	    }
7327 	    VIsual_mode = resel_VIsual_mode;
7328 	    if (VIsual_mode == 'v')
7329 	    {
7330 		if (resel_VIsual_line_count <= 1)
7331 		    curwin->w_cursor.col += resel_VIsual_col * cap->count0 - 1;
7332 		else
7333 		    curwin->w_cursor.col = resel_VIsual_col;
7334 		check_cursor_col();
7335 	    }
7336 	    if (resel_VIsual_col == MAXCOL)
7337 	    {
7338 		curwin->w_curswant = MAXCOL;
7339 		coladvance((colnr_T)MAXCOL);
7340 	    }
7341 	    else if (VIsual_mode == Ctrl_V)
7342 	    {
7343 		validate_virtcol();
7344 		curwin->w_curswant = curwin->w_virtcol
7345 					 + resel_VIsual_col * cap->count0 - 1;
7346 		coladvance(curwin->w_curswant);
7347 	    }
7348 	    else
7349 		curwin->w_set_curswant = TRUE;
7350 	    redraw_curbuf_later(INVERTED);	/* show the inversion */
7351 	}
7352 	else
7353 	{
7354 	    if (!cap->arg)
7355 		/* start Select mode when 'selectmode' contains "cmd" */
7356 		may_start_select('c');
7357 	    n_start_visual_mode(cap->cmdchar);
7358 	}
7359     }
7360 }
7361 
7362 /*
7363  * Start selection for Shift-movement keys.
7364  */
7365     void
7366 start_selection()
7367 {
7368     /* if 'selectmode' contains "key", start Select mode */
7369     may_start_select('k');
7370     n_start_visual_mode('v');
7371 }
7372 
7373 /*
7374  * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7375  */
7376     void
7377 may_start_select(c)
7378     int		c;
7379 {
7380     VIsual_select = (stuff_empty() && typebuf_typed()
7381 		    && (vim_strchr(p_slm, c) != NULL));
7382 }
7383 
7384 /*
7385  * Start Visual mode "c".
7386  * Should set VIsual_select before calling this.
7387  */
7388     static void
7389 n_start_visual_mode(c)
7390     int		c;
7391 {
7392     VIsual_mode = c;
7393     VIsual_active = TRUE;
7394     VIsual_reselect = TRUE;
7395 #ifdef FEAT_VIRTUALEDIT
7396     /* Corner case: the 0 position in a tab may change when going into
7397      * virtualedit.  Recalculate curwin->w_cursor to avoid bad hilighting.
7398      */
7399     if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7400 	coladvance(curwin->w_virtcol);
7401 #endif
7402     VIsual = curwin->w_cursor;
7403 
7404 #ifdef FEAT_FOLDING
7405     foldAdjustVisual();
7406 #endif
7407 
7408 #ifdef FEAT_MOUSE
7409     setmouse();
7410 #endif
7411     if (p_smd && msg_silent == 0)
7412 	redraw_cmdline = TRUE;	/* show visual mode later */
7413 #ifdef FEAT_CLIPBOARD
7414     /* Make sure the clipboard gets updated.  Needed because start and
7415      * end may still be the same, and the selection needs to be owned */
7416     clip_star.vmode = NUL;
7417 #endif
7418 
7419     /* Only need to redraw this line, unless still need to redraw an old
7420      * Visual area (when 'lazyredraw' is set). */
7421     if (curwin->w_redr_type < INVERTED)
7422     {
7423 	curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7424 	curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7425     }
7426 }
7427 
7428 #endif /* FEAT_VISUAL */
7429 
7430 /*
7431  * CTRL-W: Window commands
7432  */
7433     static void
7434 nv_window(cap)
7435     cmdarg_T	*cap;
7436 {
7437 #ifdef FEAT_WINDOWS
7438     if (!checkclearop(cap->oap))
7439 	do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7440 #else
7441     (void)checkclearop(cap->oap);
7442 #endif
7443 }
7444 
7445 /*
7446  * CTRL-Z: Suspend
7447  */
7448     static void
7449 nv_suspend(cap)
7450     cmdarg_T	*cap;
7451 {
7452     clearop(cap->oap);
7453 #ifdef FEAT_VISUAL
7454     if (VIsual_active)
7455 	end_visual_mode();		/* stop Visual mode */
7456 #endif
7457     do_cmdline_cmd((char_u *)"st");
7458 }
7459 
7460 /*
7461  * Commands starting with "g".
7462  */
7463     static void
7464 nv_g_cmd(cap)
7465     cmdarg_T	*cap;
7466 {
7467     oparg_T	*oap = cap->oap;
7468 #ifdef FEAT_VISUAL
7469     pos_T	tpos;
7470 #endif
7471     int		i;
7472     int		flag = FALSE;
7473 
7474     switch (cap->nchar)
7475     {
7476 #ifdef MEM_PROFILE
7477     /*
7478      * "g^A": dump log of used memory.
7479      */
7480     case Ctrl_A:
7481 	vim_mem_profile_dump();
7482 	break;
7483 #endif
7484 
7485 #ifdef FEAT_VREPLACE
7486     /*
7487      * "gR": Enter virtual replace mode.
7488      */
7489     case 'R':
7490 	cap->arg = TRUE;
7491 	nv_Replace(cap);
7492 	break;
7493 
7494     case 'r':
7495 	nv_vreplace(cap);
7496 	break;
7497 #endif
7498 
7499     case '&':
7500 	do_cmdline_cmd((char_u *)"%s//~/&");
7501 	break;
7502 
7503 #ifdef FEAT_VISUAL
7504     /*
7505      * "gv": Reselect the previous Visual area.  If Visual already active,
7506      *	     exchange previous and current Visual area.
7507      */
7508     case 'v':
7509 	if (checkclearop(oap))
7510 	    break;
7511 
7512 	if (	   curbuf->b_visual.vi_start.lnum == 0
7513 		|| curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7514 		|| curbuf->b_visual.vi_end.lnum == 0)
7515 	    beep_flush();
7516 	else
7517 	{
7518 	    /* set w_cursor to the start of the Visual area, tpos to the end */
7519 	    if (VIsual_active)
7520 	    {
7521 		i = VIsual_mode;
7522 		VIsual_mode = curbuf->b_visual.vi_mode;
7523 		curbuf->b_visual.vi_mode = i;
7524 # ifdef FEAT_EVAL
7525 		curbuf->b_visual_mode_eval = i;
7526 # endif
7527 		i = curwin->w_curswant;
7528 		curwin->w_curswant = curbuf->b_visual.vi_curswant;
7529 		curbuf->b_visual.vi_curswant = i;
7530 
7531 		tpos = curbuf->b_visual.vi_end;
7532 		curbuf->b_visual.vi_end = curwin->w_cursor;
7533 		curwin->w_cursor = curbuf->b_visual.vi_start;
7534 		curbuf->b_visual.vi_start = VIsual;
7535 	    }
7536 	    else
7537 	    {
7538 		VIsual_mode = curbuf->b_visual.vi_mode;
7539 		curwin->w_curswant = curbuf->b_visual.vi_curswant;
7540 		tpos = curbuf->b_visual.vi_end;
7541 		curwin->w_cursor = curbuf->b_visual.vi_start;
7542 	    }
7543 
7544 	    VIsual_active = TRUE;
7545 	    VIsual_reselect = TRUE;
7546 
7547 	    /* Set Visual to the start and w_cursor to the end of the Visual
7548 	     * area.  Make sure they are on an existing character. */
7549 	    check_cursor();
7550 	    VIsual = curwin->w_cursor;
7551 	    curwin->w_cursor = tpos;
7552 	    check_cursor();
7553 	    update_topline();
7554 	    /*
7555 	     * When called from normal "g" command: start Select mode when
7556 	     * 'selectmode' contains "cmd".  When called for K_SELECT, always
7557 	     * start Select mode.
7558 	     */
7559 	    if (cap->arg)
7560 		VIsual_select = TRUE;
7561 	    else
7562 		may_start_select('c');
7563 #ifdef FEAT_MOUSE
7564 	    setmouse();
7565 #endif
7566 #ifdef FEAT_CLIPBOARD
7567 	    /* Make sure the clipboard gets updated.  Needed because start and
7568 	     * end are still the same, and the selection needs to be owned */
7569 	    clip_star.vmode = NUL;
7570 #endif
7571 	    redraw_curbuf_later(INVERTED);
7572 	    showmode();
7573 	}
7574 	break;
7575     /*
7576      * "gV": Don't reselect the previous Visual area after a Select mode
7577      *	     mapping of menu.
7578      */
7579     case 'V':
7580 	VIsual_reselect = FALSE;
7581 	break;
7582 
7583     /*
7584      * "gh":  start Select mode.
7585      * "gH":  start Select line mode.
7586      * "g^H": start Select block mode.
7587      */
7588     case K_BS:
7589 	cap->nchar = Ctrl_H;
7590 	/* FALLTHROUGH */
7591     case 'h':
7592     case 'H':
7593     case Ctrl_H:
7594 # ifdef EBCDIC
7595 	/* EBCDIC: 'v'-'h' != '^v'-'^h' */
7596 	if (cap->nchar == Ctrl_H)
7597 	    cap->cmdchar = Ctrl_V;
7598 	else
7599 # endif
7600 	cap->cmdchar = cap->nchar + ('v' - 'h');
7601 	cap->arg = TRUE;
7602 	nv_visual(cap);
7603 	break;
7604 #endif /* FEAT_VISUAL */
7605 
7606     /*
7607      * "gj" and "gk" two new funny movement keys -- up and down
7608      * movement based on *screen* line rather than *file* line.
7609      */
7610     case 'j':
7611     case K_DOWN:
7612 	/* with 'nowrap' it works just like the normal "j" command; also when
7613 	 * in a closed fold */
7614 	if (!curwin->w_p_wrap
7615 #ifdef FEAT_FOLDING
7616 		|| hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7617 #endif
7618 		)
7619 	{
7620 	    oap->motion_type = MLINE;
7621 	    i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7622 	}
7623 	else
7624 	    i = nv_screengo(oap, FORWARD, cap->count1);
7625 	if (i == FAIL)
7626 	    clearopbeep(oap);
7627 	break;
7628 
7629     case 'k':
7630     case K_UP:
7631 	/* with 'nowrap' it works just like the normal "k" command; also when
7632 	 * in a closed fold */
7633 	if (!curwin->w_p_wrap
7634 #ifdef FEAT_FOLDING
7635 		|| hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7636 #endif
7637 	   )
7638 	{
7639 	    oap->motion_type = MLINE;
7640 	    i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7641 	}
7642 	else
7643 	    i = nv_screengo(oap, BACKWARD, cap->count1);
7644 	if (i == FAIL)
7645 	    clearopbeep(oap);
7646 	break;
7647 
7648     /*
7649      * "gJ": join two lines without inserting a space.
7650      */
7651     case 'J':
7652 	nv_join(cap);
7653 	break;
7654 
7655     /*
7656      * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7657      * "gm": middle of "g0" and "g$".
7658      */
7659     case '^':
7660 	flag = TRUE;
7661 	/* FALLTHROUGH */
7662 
7663     case '0':
7664     case 'm':
7665     case K_HOME:
7666     case K_KHOME:
7667 	oap->motion_type = MCHAR;
7668 	oap->inclusive = FALSE;
7669 	if (curwin->w_p_wrap
7670 #ifdef FEAT_VERTSPLIT
7671 		&& curwin->w_width != 0
7672 #endif
7673 		)
7674 	{
7675 	    int		width1 = W_WIDTH(curwin) - curwin_col_off();
7676 	    int		width2 = width1 + curwin_col_off2();
7677 
7678 	    validate_virtcol();
7679 	    i = 0;
7680 	    if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
7681 		i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
7682 	}
7683 	else
7684 	    i = curwin->w_leftcol;
7685 	/* Go to the middle of the screen line.  When 'number' is on and lines
7686 	 * are wrapping the middle can be more to the left.*/
7687 	if (cap->nchar == 'm')
7688 	    i += (W_WIDTH(curwin) - curwin_col_off()
7689 		    + ((curwin->w_p_wrap && i > 0)
7690 			? curwin_col_off2() : 0)) / 2;
7691 	coladvance((colnr_T)i);
7692 	if (flag)
7693 	{
7694 	    do
7695 		i = gchar_cursor();
7696 	    while (vim_iswhite(i) && oneright() == OK);
7697 	}
7698 	curwin->w_set_curswant = TRUE;
7699 	break;
7700 
7701     case '_':
7702 	/* "g_": to the last non-blank character in the line or <count> lines
7703 	 * downward. */
7704 	cap->oap->motion_type = MCHAR;
7705 	cap->oap->inclusive = TRUE;
7706 	curwin->w_curswant = MAXCOL;
7707 	if (cursor_down((long)(cap->count1 - 1),
7708 					 cap->oap->op_type == OP_NOP) == FAIL)
7709 	    clearopbeep(cap->oap);
7710 	else
7711 	{
7712 	    char_u  *ptr = ml_get_curline();
7713 
7714 	    /* In Visual mode we may end up after the line. */
7715 	    if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
7716 		--curwin->w_cursor.col;
7717 
7718 	    /* Decrease the cursor column until it's on a non-blank. */
7719 	    while (curwin->w_cursor.col > 0
7720 				    && vim_iswhite(ptr[curwin->w_cursor.col]))
7721 		--curwin->w_cursor.col;
7722 	    curwin->w_set_curswant = TRUE;
7723 	}
7724 	break;
7725 
7726     case '$':
7727     case K_END:
7728     case K_KEND:
7729 	{
7730 	    int col_off = curwin_col_off();
7731 
7732 	    oap->motion_type = MCHAR;
7733 	    oap->inclusive = TRUE;
7734 	    if (curwin->w_p_wrap
7735 #ifdef FEAT_VERTSPLIT
7736 		    && curwin->w_width != 0
7737 #endif
7738 	       )
7739 	    {
7740 		curwin->w_curswant = MAXCOL;    /* so we stay at the end */
7741 		if (cap->count1 == 1)
7742 		{
7743 		    int		width1 = W_WIDTH(curwin) - col_off;
7744 		    int		width2 = width1 + curwin_col_off2();
7745 
7746 		    validate_virtcol();
7747 		    i = width1 - 1;
7748 		    if (curwin->w_virtcol >= (colnr_T)width1)
7749 			i += ((curwin->w_virtcol - width1) / width2 + 1)
7750 								     * width2;
7751 		    coladvance((colnr_T)i);
7752 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
7753 		    if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
7754 		    {
7755 			/*
7756 			 * Check for landing on a character that got split at
7757 			 * the end of the line.  We do not want to advance to
7758 			 * the next screen line.
7759 			 */
7760 			validate_virtcol();
7761 			if (curwin->w_virtcol > (colnr_T)i)
7762 			    --curwin->w_cursor.col;
7763 		    }
7764 #endif
7765 		}
7766 		else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
7767 		    clearopbeep(oap);
7768 	    }
7769 	    else
7770 	    {
7771 		i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
7772 		coladvance((colnr_T)i);
7773 
7774 		/* Make sure we stick in this column. */
7775 		validate_virtcol();
7776 		curwin->w_curswant = curwin->w_virtcol;
7777 		curwin->w_set_curswant = FALSE;
7778 	    }
7779 	}
7780 	break;
7781 
7782     /*
7783      * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
7784      */
7785     case '*':
7786     case '#':
7787 #if POUND != '#'
7788     case POUND:		/* pound sign (sometimes equal to '#') */
7789 #endif
7790     case Ctrl_RSB:		/* :tag or :tselect for current identifier */
7791     case ']':			/* :tselect for current identifier */
7792 	nv_ident(cap);
7793 	break;
7794 
7795     /*
7796      * ge and gE: go back to end of word
7797      */
7798     case 'e':
7799     case 'E':
7800 	oap->motion_type = MCHAR;
7801 	curwin->w_set_curswant = TRUE;
7802 	oap->inclusive = TRUE;
7803 	if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
7804 	    clearopbeep(oap);
7805 	break;
7806 
7807     /*
7808      * "g CTRL-G": display info about cursor position
7809      */
7810     case Ctrl_G:
7811 	cursor_pos_info();
7812 	break;
7813 
7814     /*
7815      * "gi": start Insert at the last position.
7816      */
7817     case 'i':
7818 	if (curbuf->b_last_insert.lnum != 0)
7819 	{
7820 	    curwin->w_cursor = curbuf->b_last_insert;
7821 	    check_cursor_lnum();
7822 	    i = (int)STRLEN(ml_get_curline());
7823 	    if (curwin->w_cursor.col > (colnr_T)i)
7824 	    {
7825 #ifdef FEAT_VIRTUALEDIT
7826 		if (virtual_active())
7827 		    curwin->w_cursor.coladd += curwin->w_cursor.col - i;
7828 #endif
7829 		curwin->w_cursor.col = i;
7830 	    }
7831 	}
7832 	cap->cmdchar = 'i';
7833 	nv_edit(cap);
7834 	break;
7835 
7836     /*
7837      * "gI": Start insert in column 1.
7838      */
7839     case 'I':
7840 	beginline(0);
7841 	if (!checkclearopq(oap))
7842 	    invoke_edit(cap, FALSE, 'g', FALSE);
7843 	break;
7844 
7845 #ifdef FEAT_SEARCHPATH
7846     /*
7847      * "gf": goto file, edit file under cursor
7848      * "]f" and "[f": can also be used.
7849      */
7850     case 'f':
7851     case 'F':
7852 	nv_gotofile(cap);
7853 	break;
7854 #endif
7855 
7856 	/* "g'm" and "g`m": jump to mark without setting pcmark */
7857     case '\'':
7858 	cap->arg = TRUE;
7859 	/*FALLTHROUGH*/
7860     case '`':
7861 	nv_gomark(cap);
7862 	break;
7863 
7864     /*
7865      * "gs": Goto sleep.
7866      */
7867     case 's':
7868 	do_sleep(cap->count1 * 1000L);
7869 	break;
7870 
7871     /*
7872      * "ga": Display the ascii value of the character under the
7873      * cursor.	It is displayed in decimal, hex, and octal. -- webb
7874      */
7875     case 'a':
7876 	do_ascii(NULL);
7877 	break;
7878 
7879 #ifdef FEAT_MBYTE
7880     /*
7881      * "g8": Display the bytes used for the UTF-8 character under the
7882      * cursor.	It is displayed in hex.
7883      * "8g8" finds illegal byte sequence.
7884      */
7885     case '8':
7886 	if (cap->count0 == 8)
7887 	    utf_find_illegal();
7888 	else
7889 	    show_utf8();
7890 	break;
7891 #endif
7892 
7893     case '<':
7894 	show_sb_text();
7895 	break;
7896 
7897     /*
7898      * "gg": Goto the first line in file.  With a count it goes to
7899      * that line number like for "G". -- webb
7900      */
7901     case 'g':
7902 	cap->arg = FALSE;
7903 	nv_goto(cap);
7904 	break;
7905 
7906     /*
7907      *	 Two-character operators:
7908      *	 "gq"	    Format text
7909      *	 "gw"	    Format text and keep cursor position
7910      *	 "g~"	    Toggle the case of the text.
7911      *	 "gu"	    Change text to lower case.
7912      *	 "gU"	    Change text to upper case.
7913      *   "g?"	    rot13 encoding
7914      *   "g@"	    call 'operatorfunc'
7915      */
7916     case 'q':
7917     case 'w':
7918 	oap->cursor_start = curwin->w_cursor;
7919 	/*FALLTHROUGH*/
7920     case '~':
7921     case 'u':
7922     case 'U':
7923     case '?':
7924     case '@':
7925 	nv_operator(cap);
7926 	break;
7927 
7928     /*
7929      * "gd": Find first occurence of pattern under the cursor in the
7930      *	 current function
7931      * "gD": idem, but in the current file.
7932      */
7933     case 'd':
7934     case 'D':
7935 	nv_gd(oap, cap->nchar, (int)cap->count0);
7936 	break;
7937 
7938 #ifdef FEAT_MOUSE
7939     /*
7940      * g<*Mouse> : <C-*mouse>
7941      */
7942     case K_MIDDLEMOUSE:
7943     case K_MIDDLEDRAG:
7944     case K_MIDDLERELEASE:
7945     case K_LEFTMOUSE:
7946     case K_LEFTDRAG:
7947     case K_LEFTRELEASE:
7948     case K_RIGHTMOUSE:
7949     case K_RIGHTDRAG:
7950     case K_RIGHTRELEASE:
7951     case K_X1MOUSE:
7952     case K_X1DRAG:
7953     case K_X1RELEASE:
7954     case K_X2MOUSE:
7955     case K_X2DRAG:
7956     case K_X2RELEASE:
7957 	mod_mask = MOD_MASK_CTRL;
7958 	(void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
7959 	break;
7960 #endif
7961 
7962     case K_IGNORE:
7963 	break;
7964 
7965     /*
7966      * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
7967      */
7968     case 'p':
7969     case 'P':
7970 	nv_put(cap);
7971 	break;
7972 
7973 #ifdef FEAT_BYTEOFF
7974     /* "go": goto byte count from start of buffer */
7975     case 'o':
7976 	goto_byte(cap->count0);
7977 	break;
7978 #endif
7979 
7980     /* "gQ": improved Ex mode */
7981     case 'Q':
7982 	if (text_locked())
7983 	{
7984 	    clearopbeep(cap->oap);
7985 	    text_locked_msg();
7986 	    break;
7987 	}
7988 
7989 	if (!checkclearopq(oap))
7990 	    do_exmode(TRUE);
7991 	break;
7992 
7993 #ifdef FEAT_JUMPLIST
7994     case ',':
7995 	nv_pcmark(cap);
7996 	break;
7997 
7998     case ';':
7999 	cap->count1 = -cap->count1;
8000 	nv_pcmark(cap);
8001 	break;
8002 #endif
8003 
8004 #ifdef FEAT_WINDOWS
8005     case 't':
8006 	goto_tabpage((int)cap->count0);
8007 	break;
8008     case 'T':
8009 	goto_tabpage(-(int)cap->count1);
8010 	break;
8011 #endif
8012 
8013     case '+':
8014     case '-': /* "g+" and "g-": undo or redo along the timeline */
8015 	if (!checkclearopq(oap))
8016 	    undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
8017 								FALSE, FALSE);
8018 	break;
8019 
8020     default:
8021 	clearopbeep(oap);
8022 	break;
8023     }
8024 }
8025 
8026 /*
8027  * Handle "o" and "O" commands.
8028  */
8029     static void
8030 n_opencmd(cap)
8031     cmdarg_T	*cap;
8032 {
8033     if (!checkclearopq(cap->oap))
8034     {
8035 #ifdef FEAT_FOLDING
8036 	if (cap->cmdchar == 'O')
8037 	    /* Open above the first line of a folded sequence of lines */
8038 	    (void)hasFolding(curwin->w_cursor.lnum,
8039 						&curwin->w_cursor.lnum, NULL);
8040 	else
8041 	    /* Open below the last line of a folded sequence of lines */
8042 	    (void)hasFolding(curwin->w_cursor.lnum,
8043 						NULL, &curwin->w_cursor.lnum);
8044 #endif
8045 	if (u_save((linenr_T)(curwin->w_cursor.lnum -
8046 					       (cap->cmdchar == 'O' ? 1 : 0)),
8047 		   (linenr_T)(curwin->w_cursor.lnum +
8048 					       (cap->cmdchar == 'o' ? 1 : 0))
8049 		       ) == OK
8050 		&& open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8051 #ifdef FEAT_COMMENTS
8052 		    has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8053 #endif
8054 		    0, 0))
8055 	{
8056 	    /* When '#' is in 'cpoptions' ignore the count. */
8057 	    if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8058 		cap->count1 = 1;
8059 	    invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8060 	}
8061     }
8062 }
8063 
8064 /*
8065  * "." command: redo last change.
8066  */
8067     static void
8068 nv_dot(cap)
8069     cmdarg_T	*cap;
8070 {
8071     if (!checkclearopq(cap->oap))
8072     {
8073 	/*
8074 	 * If "restart_edit" is TRUE, the last but one command is repeated
8075 	 * instead of the last command (inserting text). This is used for
8076 	 * CTRL-O <.> in insert mode.
8077 	 */
8078 	if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8079 	    clearopbeep(cap->oap);
8080     }
8081 }
8082 
8083 /*
8084  * CTRL-R: undo undo
8085  */
8086     static void
8087 nv_redo(cap)
8088     cmdarg_T	*cap;
8089 {
8090     if (!checkclearopq(cap->oap))
8091     {
8092 	u_redo((int)cap->count1);
8093 	curwin->w_set_curswant = TRUE;
8094     }
8095 }
8096 
8097 /*
8098  * Handle "U" command.
8099  */
8100     static void
8101 nv_Undo(cap)
8102     cmdarg_T	*cap;
8103 {
8104     /* In Visual mode and typing "gUU" triggers an operator */
8105     if (cap->oap->op_type == OP_UPPER
8106 #ifdef FEAT_VISUAL
8107 	    || VIsual_active
8108 #endif
8109 	    )
8110     {
8111 	/* translate "gUU" to "gUgU" */
8112 	cap->cmdchar = 'g';
8113 	cap->nchar = 'U';
8114 	nv_operator(cap);
8115     }
8116     else if (!checkclearopq(cap->oap))
8117     {
8118 	u_undoline();
8119 	curwin->w_set_curswant = TRUE;
8120     }
8121 }
8122 
8123 /*
8124  * '~' command: If tilde is not an operator and Visual is off: swap case of a
8125  * single character.
8126  */
8127     static void
8128 nv_tilde(cap)
8129     cmdarg_T	*cap;
8130 {
8131     if (!p_to
8132 #ifdef FEAT_VISUAL
8133 	    && !VIsual_active
8134 #endif
8135 	    && cap->oap->op_type != OP_TILDE)
8136 	n_swapchar(cap);
8137     else
8138 	nv_operator(cap);
8139 }
8140 
8141 /*
8142  * Handle an operator command.
8143  * The actual work is done by do_pending_operator().
8144  */
8145     static void
8146 nv_operator(cap)
8147     cmdarg_T	*cap;
8148 {
8149     int	    op_type;
8150 
8151     op_type = get_op_type(cap->cmdchar, cap->nchar);
8152 
8153     if (op_type == cap->oap->op_type)	    /* double operator works on lines */
8154 	nv_lineop(cap);
8155     else if (!checkclearop(cap->oap))
8156     {
8157 	cap->oap->start = curwin->w_cursor;
8158 	cap->oap->op_type = op_type;
8159     }
8160 }
8161 
8162 /*
8163  * Handle linewise operator "dd", "yy", etc.
8164  *
8165  * "_" is is a strange motion command that helps make operators more logical.
8166  * It is actually implemented, but not documented in the real Vi.  This motion
8167  * command actually refers to "the current line".  Commands like "dd" and "yy"
8168  * are really an alternate form of "d_" and "y_".  It does accept a count, so
8169  * "d3_" works to delete 3 lines.
8170  */
8171     static void
8172 nv_lineop(cap)
8173     cmdarg_T	*cap;
8174 {
8175     cap->oap->motion_type = MLINE;
8176     if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8177 	clearopbeep(cap->oap);
8178     else if (  cap->oap->op_type == OP_DELETE
8179 	    || cap->oap->op_type == OP_LSHIFT
8180 	    || cap->oap->op_type == OP_RSHIFT)
8181 	beginline(BL_SOL | BL_FIX);
8182     else if (cap->oap->op_type != OP_YANK)	/* 'Y' does not move cursor */
8183 	beginline(BL_WHITE | BL_FIX);
8184 }
8185 
8186 /*
8187  * <Home> command.
8188  */
8189     static void
8190 nv_home(cap)
8191     cmdarg_T	*cap;
8192 {
8193     /* CTRL-HOME is like "gg" */
8194     if (mod_mask & MOD_MASK_CTRL)
8195 	nv_goto(cap);
8196     else
8197     {
8198 	cap->count0 = 1;
8199 	nv_pipe(cap);
8200     }
8201     ins_at_eol = FALSE;	    /* Don't move cursor past eol (only necessary in a
8202 			       one-character line). */
8203 }
8204 
8205 /*
8206  * "|" command.
8207  */
8208     static void
8209 nv_pipe(cap)
8210     cmdarg_T *cap;
8211 {
8212     cap->oap->motion_type = MCHAR;
8213     cap->oap->inclusive = FALSE;
8214     beginline(0);
8215     if (cap->count0 > 0)
8216     {
8217 	coladvance((colnr_T)(cap->count0 - 1));
8218 	curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8219     }
8220     else
8221 	curwin->w_curswant = 0;
8222     /* keep curswant at the column where we wanted to go, not where
8223        we ended; differs if line is too short */
8224     curwin->w_set_curswant = FALSE;
8225 }
8226 
8227 /*
8228  * Handle back-word command "b" and "B".
8229  * cap->arg is 1 for "B"
8230  */
8231     static void
8232 nv_bck_word(cap)
8233     cmdarg_T	*cap;
8234 {
8235     cap->oap->motion_type = MCHAR;
8236     cap->oap->inclusive = FALSE;
8237     curwin->w_set_curswant = TRUE;
8238     if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8239 	clearopbeep(cap->oap);
8240 #ifdef FEAT_FOLDING
8241     else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8242 	foldOpenCursor();
8243 #endif
8244 }
8245 
8246 /*
8247  * Handle word motion commands "e", "E", "w" and "W".
8248  * cap->arg is TRUE for "E" and "W".
8249  */
8250     static void
8251 nv_wordcmd(cap)
8252     cmdarg_T	*cap;
8253 {
8254     int		n;
8255     int		word_end;
8256     int		flag = FALSE;
8257 
8258     /*
8259      * Set inclusive for the "E" and "e" command.
8260      */
8261     if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8262 	word_end = TRUE;
8263     else
8264 	word_end = FALSE;
8265     cap->oap->inclusive = word_end;
8266 
8267     /*
8268      * "cw" and "cW" are a special case.
8269      */
8270     if (!word_end && cap->oap->op_type == OP_CHANGE)
8271     {
8272 	n = gchar_cursor();
8273 	if (n != NUL)			/* not an empty line */
8274 	{
8275 	    if (vim_iswhite(n))
8276 	    {
8277 		/*
8278 		 * Reproduce a funny Vi behaviour: "cw" on a blank only
8279 		 * changes one character, not all blanks until the start of
8280 		 * the next word.  Only do this when the 'w' flag is included
8281 		 * in 'cpoptions'.
8282 		 */
8283 		if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8284 		{
8285 		    cap->oap->inclusive = TRUE;
8286 		    cap->oap->motion_type = MCHAR;
8287 		    return;
8288 		}
8289 	    }
8290 	    else
8291 	    {
8292 		/*
8293 		 * This is a little strange. To match what the real Vi does,
8294 		 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8295 		 * that we are not on a space or a TAB.  This seems impolite
8296 		 * at first, but it's really more what we mean when we say
8297 		 * 'cw'.
8298 		 * Another strangeness: When standing on the end of a word
8299 		 * "ce" will change until the end of the next wordt, but "cw"
8300 		 * will change only one character! This is done by setting
8301 		 * flag.
8302 		 */
8303 		cap->oap->inclusive = TRUE;
8304 		word_end = TRUE;
8305 		flag = TRUE;
8306 	    }
8307 	}
8308     }
8309 
8310     cap->oap->motion_type = MCHAR;
8311     curwin->w_set_curswant = TRUE;
8312     if (word_end)
8313 	n = end_word(cap->count1, cap->arg, flag, FALSE);
8314     else
8315 	n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8316 
8317     /* Don't leave the cursor on the NUL past a line */
8318     if (curwin->w_cursor.col && gchar_cursor() == NUL)
8319     {
8320 	--curwin->w_cursor.col;
8321 	cap->oap->inclusive = TRUE;
8322     }
8323 
8324     if (n == FAIL && cap->oap->op_type == OP_NOP)
8325 	clearopbeep(cap->oap);
8326     else
8327     {
8328 #ifdef FEAT_VISUAL
8329 	adjust_for_sel(cap);
8330 #endif
8331 #ifdef FEAT_FOLDING
8332 	if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8333 	    foldOpenCursor();
8334 #endif
8335     }
8336 }
8337 
8338 /*
8339  * "0" and "^" commands.
8340  * cap->arg is the argument for beginline().
8341  */
8342     static void
8343 nv_beginline(cap)
8344     cmdarg_T	*cap;
8345 {
8346     cap->oap->motion_type = MCHAR;
8347     cap->oap->inclusive = FALSE;
8348     beginline(cap->arg);
8349 #ifdef FEAT_FOLDING
8350     if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8351 	foldOpenCursor();
8352 #endif
8353     ins_at_eol = FALSE;	    /* Don't move cursor past eol (only necessary in a
8354 			       one-character line). */
8355 }
8356 
8357 #ifdef FEAT_VISUAL
8358 /*
8359  * In exclusive Visual mode, may include the last character.
8360  */
8361     static void
8362 adjust_for_sel(cap)
8363     cmdarg_T	*cap;
8364 {
8365     if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8366 	    && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8367     {
8368 # ifdef FEAT_MBYTE
8369 	if (has_mbyte)
8370 	    inc_cursor();
8371 	else
8372 # endif
8373 	    ++curwin->w_cursor.col;
8374 	cap->oap->inclusive = FALSE;
8375     }
8376 }
8377 
8378 /*
8379  * Exclude last character at end of Visual area for 'selection' == "exclusive".
8380  * Should check VIsual_mode before calling this.
8381  * Returns TRUE when backed up to the previous line.
8382  */
8383     static int
8384 unadjust_for_sel()
8385 {
8386     pos_T	*pp;
8387 
8388     if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8389     {
8390 	if (lt(VIsual, curwin->w_cursor))
8391 	    pp = &curwin->w_cursor;
8392 	else
8393 	    pp = &VIsual;
8394 #ifdef FEAT_VIRTUALEDIT
8395 	if (pp->coladd > 0)
8396 	    --pp->coladd;
8397 	else
8398 #endif
8399 	if (pp->col > 0)
8400 	{
8401 	    --pp->col;
8402 #ifdef FEAT_MBYTE
8403 	    mb_adjustpos(pp);
8404 #endif
8405 	}
8406 	else if (pp->lnum > 1)
8407 	{
8408 	    --pp->lnum;
8409 	    pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8410 	    return TRUE;
8411 	}
8412     }
8413     return FALSE;
8414 }
8415 
8416 /*
8417  * SELECT key in Normal or Visual mode: end of Select mode mapping.
8418  */
8419     static void
8420 nv_select(cap)
8421     cmdarg_T	*cap;
8422 {
8423     if (VIsual_active)
8424 	VIsual_select = TRUE;
8425     else if (VIsual_reselect)
8426     {
8427 	cap->nchar = 'v';	    /* fake "gv" command */
8428 	cap->arg = TRUE;
8429 	nv_g_cmd(cap);
8430     }
8431 }
8432 
8433 #endif
8434 
8435 /*
8436  * "G", "gg", CTRL-END, CTRL-HOME.
8437  * cap->arg is TRUE for "G".
8438  */
8439     static void
8440 nv_goto(cap)
8441     cmdarg_T	*cap;
8442 {
8443     linenr_T	lnum;
8444 
8445     if (cap->arg)
8446 	lnum = curbuf->b_ml.ml_line_count;
8447     else
8448 	lnum = 1L;
8449     cap->oap->motion_type = MLINE;
8450     setpcmark();
8451 
8452     /* When a count is given, use it instead of the default lnum */
8453     if (cap->count0 != 0)
8454 	lnum = cap->count0;
8455     if (lnum < 1L)
8456 	lnum = 1L;
8457     else if (lnum > curbuf->b_ml.ml_line_count)
8458 	lnum = curbuf->b_ml.ml_line_count;
8459     curwin->w_cursor.lnum = lnum;
8460     beginline(BL_SOL | BL_FIX);
8461 #ifdef FEAT_FOLDING
8462     if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8463 	foldOpenCursor();
8464 #endif
8465 }
8466 
8467 /*
8468  * CTRL-\ in Normal mode.
8469  */
8470     static void
8471 nv_normal(cap)
8472     cmdarg_T	*cap;
8473 {
8474     if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8475     {
8476 	clearop(cap->oap);
8477 	if (restart_edit != 0 && mode_displayed)
8478 	    clear_cmdline = TRUE;		/* unshow mode later */
8479 	restart_edit = 0;
8480 #ifdef FEAT_CMDWIN
8481 	if (cmdwin_type != 0)
8482 	    cmdwin_result = Ctrl_C;
8483 #endif
8484 #ifdef FEAT_VISUAL
8485 	if (VIsual_active)
8486 	{
8487 	    end_visual_mode();		/* stop Visual */
8488 	    redraw_curbuf_later(INVERTED);
8489 	}
8490 #endif
8491 	/* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8492 	if (cap->nchar == Ctrl_G && p_im)
8493 	    restart_edit = 'a';
8494     }
8495     else
8496 	clearopbeep(cap->oap);
8497 }
8498 
8499 /*
8500  * ESC in Normal mode: beep, but don't flush buffers.
8501  * Don't even beep if we are canceling a command.
8502  */
8503     static void
8504 nv_esc(cap)
8505     cmdarg_T	*cap;
8506 {
8507     int		no_reason;
8508 
8509     no_reason = (cap->oap->op_type == OP_NOP
8510 		&& cap->opcount == 0
8511 		&& cap->count0 == 0
8512 		&& cap->oap->regname == 0
8513 		&& !p_im);
8514 
8515     if (cap->arg)		/* TRUE for CTRL-C */
8516     {
8517 	if (restart_edit == 0
8518 #ifdef FEAT_CMDWIN
8519 		&& cmdwin_type == 0
8520 #endif
8521 #ifdef FEAT_VISUAL
8522 		&& !VIsual_active
8523 #endif
8524 		&& no_reason)
8525 	    MSG(_("Type  :quit<Enter>  to exit Vim"));
8526 
8527 	/* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8528 	 * set again below when halfway a mapping. */
8529 	if (!p_im)
8530 	    restart_edit = 0;
8531 #ifdef FEAT_CMDWIN
8532 	if (cmdwin_type != 0)
8533 	{
8534 	    cmdwin_result = K_IGNORE;
8535 	    got_int = FALSE;	/* don't stop executing autocommands et al. */
8536 	    return;
8537 	}
8538 #endif
8539     }
8540 
8541 #ifdef FEAT_VISUAL
8542     if (VIsual_active)
8543     {
8544 	end_visual_mode();	/* stop Visual */
8545 	check_cursor_col();	/* make sure cursor is not beyond EOL */
8546 	curwin->w_set_curswant = TRUE;
8547 	redraw_curbuf_later(INVERTED);
8548     }
8549     else
8550 #endif
8551 	if (no_reason)
8552 	    vim_beep();
8553     clearop(cap->oap);
8554 
8555     /* A CTRL-C is often used at the start of a menu.  When 'insertmode' is
8556      * set return to Insert mode afterwards. */
8557     if (restart_edit == 0 && goto_im()
8558 #ifdef FEAT_EX_EXTRA
8559 	    && ex_normal_busy == 0
8560 #endif
8561 	    )
8562 	restart_edit = 'a';
8563 }
8564 
8565 /*
8566  * Handle "A", "a", "I", "i" and <Insert> commands.
8567  */
8568     static void
8569 nv_edit(cap)
8570     cmdarg_T *cap;
8571 {
8572     /* <Insert> is equal to "i" */
8573     if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8574 	cap->cmdchar = 'i';
8575 
8576 #ifdef FEAT_VISUAL
8577     /* in Visual mode "A" and "I" are an operator */
8578     if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8579 	v_visop(cap);
8580 
8581     /* in Visual mode and after an operator "a" and "i" are for text objects */
8582     else
8583 #endif
8584 	if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8585 	    && (cap->oap->op_type != OP_NOP
8586 #ifdef FEAT_VISUAL
8587 		|| VIsual_active
8588 #endif
8589 		))
8590     {
8591 #ifdef FEAT_TEXTOBJ
8592 	nv_object(cap);
8593 #else
8594 	clearopbeep(cap->oap);
8595 #endif
8596     }
8597     else if (!curbuf->b_p_ma && !p_im)
8598     {
8599 	/* Only give this error when 'insertmode' is off. */
8600 	EMSG(_(e_modifiable));
8601 	clearop(cap->oap);
8602     }
8603     else if (!checkclearopq(cap->oap))
8604     {
8605 	switch (cap->cmdchar)
8606 	{
8607 	    case 'A':	/* "A"ppend after the line */
8608 		curwin->w_set_curswant = TRUE;
8609 #ifdef FEAT_VIRTUALEDIT
8610 		if (ve_flags == VE_ALL)
8611 		{
8612 		    int save_State = State;
8613 
8614 		    /* Pretent Insert mode here to allow the cursor on the
8615 		     * character past the end of the line */
8616 		    State = INSERT;
8617 		    coladvance((colnr_T)MAXCOL);
8618 		    State = save_State;
8619 		}
8620 		else
8621 #endif
8622 		    curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8623 		break;
8624 
8625 	    case 'I':	/* "I"nsert before the first non-blank */
8626 		if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
8627 		    beginline(BL_WHITE);
8628 		else
8629 		    beginline(BL_WHITE|BL_FIX);
8630 		break;
8631 
8632 	    case 'a':	/* "a"ppend is like "i"nsert on the next character. */
8633 #ifdef FEAT_VIRTUALEDIT
8634 		/* increment coladd when in virtual space, increment the
8635 		 * column otherwise, also to append after an unprintable char */
8636 		if (virtual_active()
8637 			&& (curwin->w_cursor.coladd > 0
8638 			    || *ml_get_cursor() == NUL
8639 			    || *ml_get_cursor() == TAB))
8640 		    curwin->w_cursor.coladd++;
8641 		else
8642 #endif
8643 		if (*ml_get_cursor() != NUL)
8644 		    inc_cursor();
8645 		break;
8646 	}
8647 
8648 #ifdef FEAT_VIRTUALEDIT
8649 	if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
8650 	{
8651 	    int save_State = State;
8652 
8653 	    /* Pretent Insert mode here to allow the cursor on the
8654 	     * character past the end of the line */
8655 	    State = INSERT;
8656 	    coladvance(getviscol());
8657 	    State = save_State;
8658 	}
8659 #endif
8660 
8661 	invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
8662     }
8663 }
8664 
8665 /*
8666  * Invoke edit() and take care of "restart_edit" and the return value.
8667  */
8668     static void
8669 invoke_edit(cap, repl, cmd, startln)
8670     cmdarg_T	*cap;
8671     int		repl;		/* "r" or "gr" command */
8672     int		cmd;
8673     int		startln;
8674 {
8675     int		restart_edit_save = 0;
8676 
8677     /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
8678      * mode recursively.  But when doing "a<C-O>." or "a<C-O>rx" we do allow
8679      * it. */
8680     if (repl || !stuff_empty())
8681 	restart_edit_save = restart_edit;
8682     else
8683 	restart_edit_save = 0;
8684 
8685     /* Always reset "restart_edit", this is not a restarted edit. */
8686     restart_edit = 0;
8687 
8688     if (edit(cmd, startln, cap->count1))
8689 	cap->retval |= CA_COMMAND_BUSY;
8690 
8691     if (restart_edit == 0)
8692 	restart_edit = restart_edit_save;
8693 }
8694 
8695 #ifdef FEAT_TEXTOBJ
8696 /*
8697  * "a" or "i" while an operator is pending or in Visual mode: object motion.
8698  */
8699     static void
8700 nv_object(cap)
8701     cmdarg_T	*cap;
8702 {
8703     int		flag;
8704     int		include;
8705     char_u	*mps_save;
8706 
8707     if (cap->cmdchar == 'i')
8708 	include = FALSE;    /* "ix" = inner object: exclude white space */
8709     else
8710 	include = TRUE;	    /* "ax" = an object: include white space */
8711 
8712     /* Make sure (), [], {} and <> are in 'matchpairs' */
8713     mps_save = curbuf->b_p_mps;
8714     curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
8715 
8716     switch (cap->nchar)
8717     {
8718 	case 'w': /* "aw" = a word */
8719 		flag = current_word(cap->oap, cap->count1, include, FALSE);
8720 		break;
8721 	case 'W': /* "aW" = a WORD */
8722 		flag = current_word(cap->oap, cap->count1, include, TRUE);
8723 		break;
8724 	case 'b': /* "ab" = a braces block */
8725 	case '(':
8726 	case ')':
8727 		flag = current_block(cap->oap, cap->count1, include, '(', ')');
8728 		break;
8729 	case 'B': /* "aB" = a Brackets block */
8730 	case '{':
8731 	case '}':
8732 		flag = current_block(cap->oap, cap->count1, include, '{', '}');
8733 		break;
8734 	case '[': /* "a[" = a [] block */
8735 	case ']':
8736 		flag = current_block(cap->oap, cap->count1, include, '[', ']');
8737 		break;
8738 	case '<': /* "a<" = a <> block */
8739 	case '>':
8740 		flag = current_block(cap->oap, cap->count1, include, '<', '>');
8741 		break;
8742 	case 't': /* "at" = a tag block (xml and html) */
8743 		flag = current_tagblock(cap->oap, cap->count1, include);
8744 		break;
8745 	case 'p': /* "ap" = a paragraph */
8746 		flag = current_par(cap->oap, cap->count1, include, 'p');
8747 		break;
8748 	case 's': /* "as" = a sentence */
8749 		flag = current_sent(cap->oap, cap->count1, include);
8750 		break;
8751 	case '"': /* "a"" = a double quoted string */
8752 	case '\'': /* "a'" = a single quoted string */
8753 	case '`': /* "a`" = a backtick quoted string */
8754 		flag = current_quote(cap->oap, cap->count1, include,
8755 								  cap->nchar);
8756 		break;
8757 #if 0	/* TODO */
8758 	case 'S': /* "aS" = a section */
8759 	case 'f': /* "af" = a filename */
8760 	case 'u': /* "au" = a URL */
8761 #endif
8762 	default:
8763 		flag = FAIL;
8764 		break;
8765     }
8766 
8767     curbuf->b_p_mps = mps_save;
8768     if (flag == FAIL)
8769 	clearopbeep(cap->oap);
8770     adjust_cursor_col();
8771     curwin->w_set_curswant = TRUE;
8772 }
8773 #endif
8774 
8775 /*
8776  * "q" command: Start/stop recording.
8777  * "q:", "q/", "q?": edit command-line in command-line window.
8778  */
8779     static void
8780 nv_record(cap)
8781     cmdarg_T	*cap;
8782 {
8783     if (cap->oap->op_type == OP_FORMAT)
8784     {
8785 	/* "gqq" is the same as "gqgq": format line */
8786 	cap->cmdchar = 'g';
8787 	cap->nchar = 'q';
8788 	nv_operator(cap);
8789     }
8790     else if (!checkclearop(cap->oap))
8791     {
8792 #ifdef FEAT_CMDWIN
8793 	if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
8794 	{
8795 	    stuffcharReadbuff(cap->nchar);
8796 	    stuffcharReadbuff(K_CMDWIN);
8797 	}
8798 	else
8799 #endif
8800 	    /* (stop) recording into a named register, unless executing a
8801 	     * register */
8802 	    if (!Exec_reg && do_record(cap->nchar) == FAIL)
8803 		clearopbeep(cap->oap);
8804     }
8805 }
8806 
8807 /*
8808  * Handle the "@r" command.
8809  */
8810     static void
8811 nv_at(cap)
8812     cmdarg_T	*cap;
8813 {
8814     if (checkclearop(cap->oap))
8815 	return;
8816 #ifdef FEAT_EVAL
8817     if (cap->nchar == '=')
8818     {
8819 	if (get_expr_register() == NUL)
8820 	    return;
8821     }
8822 #endif
8823     while (cap->count1-- && !got_int)
8824     {
8825 	if (do_execreg(cap->nchar, FALSE, FALSE) == FAIL)
8826 	{
8827 	    clearopbeep(cap->oap);
8828 	    break;
8829 	}
8830 	line_breakcheck();
8831     }
8832 }
8833 
8834 /*
8835  * Handle the CTRL-U and CTRL-D commands.
8836  */
8837     static void
8838 nv_halfpage(cap)
8839     cmdarg_T	*cap;
8840 {
8841     if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
8842 	    || (cap->cmdchar == Ctrl_D
8843 		&& curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
8844 	clearopbeep(cap->oap);
8845     else if (!checkclearop(cap->oap))
8846 	halfpage(cap->cmdchar == Ctrl_D, cap->count0);
8847 }
8848 
8849 /*
8850  * Handle "J" or "gJ" command.
8851  */
8852     static void
8853 nv_join(cap)
8854     cmdarg_T *cap;
8855 {
8856 #ifdef FEAT_VISUAL
8857     if (VIsual_active)	/* join the visual lines */
8858 	nv_operator(cap);
8859     else
8860 #endif
8861 	if (!checkclearop(cap->oap))
8862     {
8863 	if (cap->count0 <= 1)
8864 	    cap->count0 = 2;	    /* default for join is two lines! */
8865 	if (curwin->w_cursor.lnum + cap->count0 - 1 >
8866 						   curbuf->b_ml.ml_line_count)
8867 	    clearopbeep(cap->oap);  /* beyond last line */
8868 	else
8869 	{
8870 	    prep_redo(cap->oap->regname, cap->count0,
8871 			 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
8872 	    do_do_join(cap->count0, cap->nchar == NUL);
8873 	}
8874     }
8875 }
8876 
8877 /*
8878  * "P", "gP", "p" and "gp" commands.
8879  */
8880     static void
8881 nv_put(cap)
8882     cmdarg_T  *cap;
8883 {
8884 #ifdef FEAT_VISUAL
8885     int		regname = 0;
8886     void	*reg1 = NULL, *reg2 = NULL;
8887     int		empty = FALSE;
8888     int		was_visual = FALSE;
8889 #endif
8890     int		dir;
8891     int		flags = 0;
8892 
8893     if (cap->oap->op_type != OP_NOP)
8894     {
8895 #ifdef FEAT_DIFF
8896 	/* "dp" is ":diffput" */
8897 	if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
8898 	{
8899 	    clearop(cap->oap);
8900 	    nv_diffgetput(TRUE);
8901 	}
8902 	else
8903 #endif
8904 	clearopbeep(cap->oap);
8905     }
8906     else
8907     {
8908 	dir = (cap->cmdchar == 'P'
8909 		|| (cap->cmdchar == 'g' && cap->nchar == 'P'))
8910 							 ? BACKWARD : FORWARD;
8911 	prep_redo_cmd(cap);
8912 	if (cap->cmdchar == 'g')
8913 	    flags |= PUT_CURSEND;
8914 
8915 #ifdef FEAT_VISUAL
8916 	if (VIsual_active)
8917 	{
8918 	    /* Putting in Visual mode: The put text replaces the selected
8919 	     * text.  First delete the selected text, then put the new text.
8920 	     * Need to save and restore the registers that the delete
8921 	     * overwrites if the old contents is being put.
8922 	     */
8923 	    was_visual = TRUE;
8924 	    regname = cap->oap->regname;
8925 # ifdef FEAT_CLIPBOARD
8926 	    adjust_clip_reg(&regname);
8927 # endif
8928 	    if (regname == 0 || VIM_ISDIGIT(regname)
8929 # ifdef FEAT_CLIPBOARD
8930 		    || (clip_unnamed && (regname == '*' || regname == '+'))
8931 # endif
8932 
8933 		    )
8934 	    {
8935 		/* the delete is going to overwrite the register we want to
8936 		 * put, save it first. */
8937 		reg1 = get_register(regname, TRUE);
8938 	    }
8939 
8940 	    /* Now delete the selected text. */
8941 	    cap->cmdchar = 'd';
8942 	    cap->nchar = NUL;
8943 	    cap->oap->regname = NUL;
8944 	    nv_operator(cap);
8945 	    do_pending_operator(cap, 0, FALSE);
8946 	    empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8947 
8948 	    /* delete PUT_LINE_BACKWARD; */
8949 	    cap->oap->regname = regname;
8950 
8951 	    if (reg1 != NULL)
8952 	    {
8953 		/* Delete probably changed the register we want to put, save
8954 		 * it first. Then put back what was there before the delete. */
8955 		reg2 = get_register(regname, FALSE);
8956 		put_register(regname, reg1);
8957 	    }
8958 
8959 	    /* When deleted a linewise Visual area, put the register as
8960 	     * lines to avoid it joined with the next line.  When deletion was
8961 	     * characterwise, split a line when putting lines. */
8962 	    if (VIsual_mode == 'V')
8963 		flags |= PUT_LINE;
8964 	    else if (VIsual_mode == 'v')
8965 		flags |= PUT_LINE_SPLIT;
8966 	    if (VIsual_mode == Ctrl_V && dir == FORWARD)
8967 		flags |= PUT_LINE_FORWARD;
8968 	    dir = BACKWARD;
8969 	    if ((VIsual_mode != 'V'
8970 			&& curwin->w_cursor.col < curbuf->b_op_start.col)
8971 		    || (VIsual_mode == 'V'
8972 			&& curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
8973 		/* cursor is at the end of the line or end of file, put
8974 		 * forward. */
8975 		dir = FORWARD;
8976 	}
8977 #endif
8978 	do_put(cap->oap->regname, dir, cap->count1, flags);
8979 
8980 #ifdef FEAT_VISUAL
8981 	/* If a register was saved, put it back now. */
8982 	if (reg2 != NULL)
8983 	    put_register(regname, reg2);
8984 
8985 	/* What to reselect with "gv"?  Selecting the just put text seems to
8986 	 * be the most useful, since the original text was removed. */
8987 	if (was_visual)
8988 	{
8989 	    curbuf->b_visual.vi_start = curbuf->b_op_start;
8990 	    curbuf->b_visual.vi_end = curbuf->b_op_end;
8991 	}
8992 
8993 	/* When all lines were selected and deleted do_put() leaves an empty
8994 	 * line that needs to be deleted now. */
8995 	if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
8996 	    ml_delete(curbuf->b_ml.ml_line_count, TRUE);
8997 #endif
8998 	auto_format(FALSE, TRUE);
8999     }
9000 }
9001 
9002 /*
9003  * "o" and "O" commands.
9004  */
9005     static void
9006 nv_open(cap)
9007     cmdarg_T	*cap;
9008 {
9009 #ifdef FEAT_DIFF
9010     /* "do" is ":diffget" */
9011     if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9012     {
9013 	clearop(cap->oap);
9014 	nv_diffgetput(FALSE);
9015     }
9016     else
9017 #endif
9018 #ifdef FEAT_VISUAL
9019     if (VIsual_active)  /* switch start and end of visual */
9020 	v_swap_corners(cap->cmdchar);
9021     else
9022 #endif
9023 	n_opencmd(cap);
9024 }
9025 
9026 #ifdef FEAT_SNIFF
9027 /*ARGSUSED*/
9028     static void
9029 nv_sniff(cap)
9030     cmdarg_T	*cap;
9031 {
9032     ProcessSniffRequests();
9033 }
9034 #endif
9035 
9036 #ifdef FEAT_NETBEANS_INTG
9037     static void
9038 nv_nbcmd(cap)
9039     cmdarg_T	*cap;
9040 {
9041     netbeans_keycommand(cap->nchar);
9042 }
9043 #endif
9044 
9045 #ifdef FEAT_DND
9046 /*ARGSUSED*/
9047     static void
9048 nv_drop(cap)
9049     cmdarg_T	*cap;
9050 {
9051     do_put('~', BACKWARD, 1L, PUT_CURSEND);
9052 }
9053 #endif
9054 
9055 #ifdef FEAT_AUTOCMD
9056 /*
9057  * Trigger CursorHold event.
9058  * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9059  * input buffer.  "did_cursorhold" is set to avoid retriggering.
9060  */
9061 /*ARGSUSED*/
9062     static void
9063 nv_cursorhold(cap)
9064     cmdarg_T	*cap;
9065 {
9066     apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9067     did_cursorhold = TRUE;
9068     cap->retval |= CA_COMMAND_BUSY;	/* don't call edit() now */
9069 }
9070 #endif
9071