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