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