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