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