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