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