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