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