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