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