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