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