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