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