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