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