floating point: fix whitespace and spelling issues
[vim_extended.git] / src / normal.c
blobc1e630a3317dd4f692e4a8d78e31a47236f2353c
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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.
15 #include "vim.h"
17 #ifdef FEAT_VISUAL
19 * The Visual area is remembered for reselection.
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 */
25 static int restart_VIsual_select = 0;
26 #endif
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 static void op_function __ARGS((oparg_T *oap));
36 #if defined(FEAT_MOUSE) && defined(FEAT_VISUAL)
37 static void find_start_of_word __ARGS((pos_T *));
38 static void find_end_of_word __ARGS((pos_T *));
39 static int get_mouse_class __ARGS((char_u *p));
40 #endif
41 static void prep_redo_cmd __ARGS((cmdarg_T *cap));
42 static void prep_redo __ARGS((int regname, long, int, int, int, int, int));
43 static int checkclearop __ARGS((oparg_T *oap));
44 static int checkclearopq __ARGS((oparg_T *oap));
45 static void clearop __ARGS((oparg_T *oap));
46 static void clearopbeep __ARGS((oparg_T *oap));
47 #ifdef FEAT_VISUAL
48 static void unshift_special __ARGS((cmdarg_T *cap));
49 #endif
50 #ifdef FEAT_CMDL_INFO
51 static void del_from_showcmd __ARGS((int));
52 #endif
55 * nv_*(): functions called to handle Normal and Visual mode commands.
56 * n_*(): functions called to handle Normal mode commands.
57 * v_*(): functions called to handle Visual mode commands.
59 static void nv_ignore __ARGS((cmdarg_T *cap));
60 static void nv_nop __ARGS((cmdarg_T *cap));
61 static void nv_error __ARGS((cmdarg_T *cap));
62 static void nv_help __ARGS((cmdarg_T *cap));
63 static void nv_addsub __ARGS((cmdarg_T *cap));
64 static void nv_page __ARGS((cmdarg_T *cap));
65 static void nv_gd __ARGS((oparg_T *oap, int nchar, int thisblock));
66 static int nv_screengo __ARGS((oparg_T *oap, int dir, long dist));
67 #ifdef FEAT_MOUSE
68 static void nv_mousescroll __ARGS((cmdarg_T *cap));
69 static void nv_mouse __ARGS((cmdarg_T *cap));
70 #endif
71 static void nv_scroll_line __ARGS((cmdarg_T *cap));
72 static void nv_zet __ARGS((cmdarg_T *cap));
73 #ifdef FEAT_GUI
74 static void nv_ver_scrollbar __ARGS((cmdarg_T *cap));
75 static void nv_hor_scrollbar __ARGS((cmdarg_T *cap));
76 #endif
77 #ifdef FEAT_GUI_TABLINE
78 static void nv_tabline __ARGS((cmdarg_T *cap));
79 static void nv_tabmenu __ARGS((cmdarg_T *cap));
80 #endif
81 static void nv_exmode __ARGS((cmdarg_T *cap));
82 static void nv_colon __ARGS((cmdarg_T *cap));
83 static void nv_ctrlg __ARGS((cmdarg_T *cap));
84 static void nv_ctrlh __ARGS((cmdarg_T *cap));
85 static void nv_clear __ARGS((cmdarg_T *cap));
86 static void nv_ctrlo __ARGS((cmdarg_T *cap));
87 static void nv_hat __ARGS((cmdarg_T *cap));
88 static void nv_Zet __ARGS((cmdarg_T *cap));
89 static void nv_ident __ARGS((cmdarg_T *cap));
90 static void nv_tagpop __ARGS((cmdarg_T *cap));
91 static void nv_scroll __ARGS((cmdarg_T *cap));
92 static void nv_right __ARGS((cmdarg_T *cap));
93 static void nv_left __ARGS((cmdarg_T *cap));
94 static void nv_up __ARGS((cmdarg_T *cap));
95 static void nv_down __ARGS((cmdarg_T *cap));
96 #ifdef FEAT_SEARCHPATH
97 static void nv_gotofile __ARGS((cmdarg_T *cap));
98 #endif
99 static void nv_end __ARGS((cmdarg_T *cap));
100 static void nv_dollar __ARGS((cmdarg_T *cap));
101 static void nv_search __ARGS((cmdarg_T *cap));
102 static void nv_next __ARGS((cmdarg_T *cap));
103 static void normal_search __ARGS((cmdarg_T *cap, int dir, char_u *pat, int opt));
104 static void nv_csearch __ARGS((cmdarg_T *cap));
105 static void nv_brackets __ARGS((cmdarg_T *cap));
106 static void nv_percent __ARGS((cmdarg_T *cap));
107 static void nv_brace __ARGS((cmdarg_T *cap));
108 static void nv_mark __ARGS((cmdarg_T *cap));
109 static void nv_findpar __ARGS((cmdarg_T *cap));
110 static void nv_undo __ARGS((cmdarg_T *cap));
111 static void nv_kundo __ARGS((cmdarg_T *cap));
112 static void nv_Replace __ARGS((cmdarg_T *cap));
113 #ifdef FEAT_VREPLACE
114 static void nv_vreplace __ARGS((cmdarg_T *cap));
115 #endif
116 #ifdef FEAT_VISUAL
117 static void v_swap_corners __ARGS((int cmdchar));
118 #endif
119 static void nv_replace __ARGS((cmdarg_T *cap));
120 static void n_swapchar __ARGS((cmdarg_T *cap));
121 static void nv_cursormark __ARGS((cmdarg_T *cap, int flag, pos_T *pos));
122 #ifdef FEAT_VISUAL
123 static void v_visop __ARGS((cmdarg_T *cap));
124 #endif
125 static void nv_subst __ARGS((cmdarg_T *cap));
126 static void nv_abbrev __ARGS((cmdarg_T *cap));
127 static void nv_optrans __ARGS((cmdarg_T *cap));
128 static void nv_gomark __ARGS((cmdarg_T *cap));
129 static void nv_pcmark __ARGS((cmdarg_T *cap));
130 static void nv_regname __ARGS((cmdarg_T *cap));
131 #ifdef FEAT_VISUAL
132 static void nv_visual __ARGS((cmdarg_T *cap));
133 static void n_start_visual_mode __ARGS((int c));
134 #endif
135 static void nv_window __ARGS((cmdarg_T *cap));
136 static void nv_suspend __ARGS((cmdarg_T *cap));
137 static void nv_g_cmd __ARGS((cmdarg_T *cap));
138 static void n_opencmd __ARGS((cmdarg_T *cap));
139 static void nv_dot __ARGS((cmdarg_T *cap));
140 static void nv_redo __ARGS((cmdarg_T *cap));
141 static void nv_Undo __ARGS((cmdarg_T *cap));
142 static void nv_tilde __ARGS((cmdarg_T *cap));
143 static void nv_operator __ARGS((cmdarg_T *cap));
144 #ifdef FEAT_EVAL
145 static void set_op_var __ARGS((int optype));
146 #endif
147 static void nv_lineop __ARGS((cmdarg_T *cap));
148 static void nv_home __ARGS((cmdarg_T *cap));
149 static void nv_pipe __ARGS((cmdarg_T *cap));
150 static void nv_bck_word __ARGS((cmdarg_T *cap));
151 static void nv_wordcmd __ARGS((cmdarg_T *cap));
152 static void nv_beginline __ARGS((cmdarg_T *cap));
153 static void adjust_cursor __ARGS((oparg_T *oap));
154 #ifdef FEAT_VISUAL
155 static void adjust_for_sel __ARGS((cmdarg_T *cap));
156 static int unadjust_for_sel __ARGS((void));
157 static void nv_select __ARGS((cmdarg_T *cap));
158 #endif
159 static void nv_goto __ARGS((cmdarg_T *cap));
160 static void nv_normal __ARGS((cmdarg_T *cap));
161 static void nv_esc __ARGS((cmdarg_T *oap));
162 static void nv_edit __ARGS((cmdarg_T *cap));
163 static void invoke_edit __ARGS((cmdarg_T *cap, int repl, int cmd, int startln));
164 #ifdef FEAT_TEXTOBJ
165 static void nv_object __ARGS((cmdarg_T *cap));
166 #endif
167 static void nv_record __ARGS((cmdarg_T *cap));
168 static void nv_at __ARGS((cmdarg_T *cap));
169 static void nv_halfpage __ARGS((cmdarg_T *cap));
170 static void nv_join __ARGS((cmdarg_T *cap));
171 static void nv_put __ARGS((cmdarg_T *cap));
172 static void nv_open __ARGS((cmdarg_T *cap));
173 #ifdef FEAT_SNIFF
174 static void nv_sniff __ARGS((cmdarg_T *cap));
175 #endif
176 #ifdef FEAT_NETBEANS_INTG
177 static void nv_nbcmd __ARGS((cmdarg_T *cap));
178 #endif
179 #ifdef FEAT_DND
180 static void nv_drop __ARGS((cmdarg_T *cap));
181 #endif
182 #ifdef FEAT_AUTOCMD
183 static void nv_cursorhold __ARGS((cmdarg_T *cap));
184 #endif
187 * Function to be called for a Normal or Visual mode command.
188 * The argument is a cmdarg_T.
190 typedef void (*nv_func_T) __ARGS((cmdarg_T *cap));
192 /* Values for cmd_flags. */
193 #define NV_NCH 0x01 /* may need to get a second char */
194 #define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
195 #define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
196 #define NV_LANG 0x08 /* second char needs language adjustment */
198 #define NV_SS 0x10 /* may start selection */
199 #define NV_SSS 0x20 /* may start selection with shift modifier */
200 #define NV_STS 0x40 /* may stop selection without shift modif. */
201 #define NV_RL 0x80 /* 'rightleft' modifies command */
202 #define NV_KEEPREG 0x100 /* don't clear regname */
203 #define NV_NCW 0x200 /* not allowed in command-line window */
206 * Generally speaking, every Normal mode command should either clear any
207 * pending operator (with *clearop*()), or set the motion type variable
208 * oap->motion_type.
210 * When a cursor motion command is made, it is marked as being a character or
211 * line oriented motion. Then, if an operator is in effect, the operation
212 * becomes character or line oriented accordingly.
216 * This table contains one entry for every Normal or Visual mode command.
217 * The order doesn't matter, init_normal_cmds() will create a sorted index.
218 * It is faster when all keys from zero to '~' are present.
220 static const struct nv_cmd
222 int cmd_char; /* (first) command character */
223 nv_func_T cmd_func; /* function for this command */
224 short_u cmd_flags; /* NV_ flags */
225 short cmd_arg; /* value for ca.arg */
226 } nv_cmds[] =
228 {NUL, nv_error, 0, 0},
229 {Ctrl_A, nv_addsub, 0, 0},
230 {Ctrl_B, nv_page, NV_STS, BACKWARD},
231 {Ctrl_C, nv_esc, 0, TRUE},
232 {Ctrl_D, nv_halfpage, 0, 0},
233 {Ctrl_E, nv_scroll_line, 0, TRUE},
234 {Ctrl_F, nv_page, NV_STS, FORWARD},
235 {Ctrl_G, nv_ctrlg, 0, 0},
236 {Ctrl_H, nv_ctrlh, 0, 0},
237 {Ctrl_I, nv_pcmark, 0, 0},
238 {NL, nv_down, 0, FALSE},
239 {Ctrl_K, nv_error, 0, 0},
240 {Ctrl_L, nv_clear, 0, 0},
241 {Ctrl_M, nv_down, 0, TRUE},
242 {Ctrl_N, nv_down, NV_STS, FALSE},
243 {Ctrl_O, nv_ctrlo, 0, 0},
244 {Ctrl_P, nv_up, NV_STS, FALSE},
245 #ifdef FEAT_VISUAL
246 {Ctrl_Q, nv_visual, 0, FALSE},
247 #else
248 {Ctrl_Q, nv_ignore, 0, 0},
249 #endif
250 {Ctrl_R, nv_redo, 0, 0},
251 {Ctrl_S, nv_ignore, 0, 0},
252 {Ctrl_T, nv_tagpop, NV_NCW, 0},
253 {Ctrl_U, nv_halfpage, 0, 0},
254 #ifdef FEAT_VISUAL
255 {Ctrl_V, nv_visual, 0, FALSE},
256 {'V', nv_visual, 0, FALSE},
257 {'v', nv_visual, 0, FALSE},
258 #else
259 {Ctrl_V, nv_error, 0, 0},
260 {'V', nv_error, 0, 0},
261 {'v', nv_error, 0, 0},
262 #endif
263 {Ctrl_W, nv_window, 0, 0},
264 {Ctrl_X, nv_addsub, 0, 0},
265 {Ctrl_Y, nv_scroll_line, 0, FALSE},
266 {Ctrl_Z, nv_suspend, 0, 0},
267 {ESC, nv_esc, 0, FALSE},
268 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
269 {Ctrl_RSB, nv_ident, NV_NCW, 0},
270 {Ctrl_HAT, nv_hat, NV_NCW, 0},
271 {Ctrl__, nv_error, 0, 0},
272 {' ', nv_right, 0, 0},
273 {'!', nv_operator, 0, 0},
274 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
275 {'#', nv_ident, 0, 0},
276 {'$', nv_dollar, 0, 0},
277 {'%', nv_percent, 0, 0},
278 {'&', nv_optrans, 0, 0},
279 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
280 {'(', nv_brace, 0, BACKWARD},
281 {')', nv_brace, 0, FORWARD},
282 {'*', nv_ident, 0, 0},
283 {'+', nv_down, 0, TRUE},
284 {',', nv_csearch, 0, TRUE},
285 {'-', nv_up, 0, TRUE},
286 {'.', nv_dot, NV_KEEPREG, 0},
287 {'/', nv_search, 0, FALSE},
288 {'0', nv_beginline, 0, 0},
289 {'1', nv_ignore, 0, 0},
290 {'2', nv_ignore, 0, 0},
291 {'3', nv_ignore, 0, 0},
292 {'4', nv_ignore, 0, 0},
293 {'5', nv_ignore, 0, 0},
294 {'6', nv_ignore, 0, 0},
295 {'7', nv_ignore, 0, 0},
296 {'8', nv_ignore, 0, 0},
297 {'9', nv_ignore, 0, 0},
298 {':', nv_colon, 0, 0},
299 {';', nv_csearch, 0, FALSE},
300 {'<', nv_operator, NV_RL, 0},
301 {'=', nv_operator, 0, 0},
302 {'>', nv_operator, NV_RL, 0},
303 {'?', nv_search, 0, FALSE},
304 {'@', nv_at, NV_NCH_NOP, FALSE},
305 {'A', nv_edit, 0, 0},
306 {'B', nv_bck_word, 0, 1},
307 {'C', nv_abbrev, NV_KEEPREG, 0},
308 {'D', nv_abbrev, NV_KEEPREG, 0},
309 {'E', nv_wordcmd, 0, TRUE},
310 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
311 {'G', nv_goto, 0, TRUE},
312 {'H', nv_scroll, 0, 0},
313 {'I', nv_edit, 0, 0},
314 {'J', nv_join, 0, 0},
315 {'K', nv_ident, 0, 0},
316 {'L', nv_scroll, 0, 0},
317 {'M', nv_scroll, 0, 0},
318 {'N', nv_next, 0, SEARCH_REV},
319 {'O', nv_open, 0, 0},
320 {'P', nv_put, 0, 0},
321 {'Q', nv_exmode, NV_NCW, 0},
322 {'R', nv_Replace, 0, FALSE},
323 {'S', nv_subst, NV_KEEPREG, 0},
324 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
325 {'U', nv_Undo, 0, 0},
326 {'W', nv_wordcmd, 0, TRUE},
327 {'X', nv_abbrev, NV_KEEPREG, 0},
328 {'Y', nv_abbrev, NV_KEEPREG, 0},
329 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
330 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
331 {'\\', nv_error, 0, 0},
332 {']', nv_brackets, NV_NCH_ALW, FORWARD},
333 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
334 {'_', nv_lineop, 0, 0},
335 {'`', nv_gomark, NV_NCH_ALW, FALSE},
336 {'a', nv_edit, NV_NCH, 0},
337 {'b', nv_bck_word, 0, 0},
338 {'c', nv_operator, 0, 0},
339 {'d', nv_operator, 0, 0},
340 {'e', nv_wordcmd, 0, FALSE},
341 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
342 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
343 {'h', nv_left, NV_RL, 0},
344 {'i', nv_edit, NV_NCH, 0},
345 {'j', nv_down, 0, FALSE},
346 {'k', nv_up, 0, FALSE},
347 {'l', nv_right, NV_RL, 0},
348 {'m', nv_mark, NV_NCH_NOP, 0},
349 {'n', nv_next, 0, 0},
350 {'o', nv_open, 0, 0},
351 {'p', nv_put, 0, 0},
352 {'q', nv_record, NV_NCH, 0},
353 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
354 {'s', nv_subst, NV_KEEPREG, 0},
355 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
356 {'u', nv_undo, 0, 0},
357 {'w', nv_wordcmd, 0, FALSE},
358 {'x', nv_abbrev, NV_KEEPREG, 0},
359 {'y', nv_operator, 0, 0},
360 {'z', nv_zet, NV_NCH_ALW, 0},
361 {'{', nv_findpar, 0, BACKWARD},
362 {'|', nv_pipe, 0, 0},
363 {'}', nv_findpar, 0, FORWARD},
364 {'~', nv_tilde, 0, 0},
366 /* pound sign */
367 {POUND, nv_ident, 0, 0},
368 #ifdef FEAT_MOUSE
369 {K_MOUSEUP, nv_mousescroll, 0, TRUE},
370 {K_MOUSEDOWN, nv_mousescroll, 0, FALSE},
371 {K_LEFTMOUSE, nv_mouse, 0, 0},
372 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
373 {K_LEFTDRAG, nv_mouse, 0, 0},
374 {K_LEFTRELEASE, nv_mouse, 0, 0},
375 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
376 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
377 {K_MIDDLEDRAG, nv_mouse, 0, 0},
378 {K_MIDDLERELEASE, nv_mouse, 0, 0},
379 {K_RIGHTMOUSE, nv_mouse, 0, 0},
380 {K_RIGHTDRAG, nv_mouse, 0, 0},
381 {K_RIGHTRELEASE, nv_mouse, 0, 0},
382 {K_X1MOUSE, nv_mouse, 0, 0},
383 {K_X1DRAG, nv_mouse, 0, 0},
384 {K_X1RELEASE, nv_mouse, 0, 0},
385 {K_X2MOUSE, nv_mouse, 0, 0},
386 {K_X2DRAG, nv_mouse, 0, 0},
387 {K_X2RELEASE, nv_mouse, 0, 0},
388 #endif
389 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
390 {K_NOP, nv_nop, 0, 0},
391 {K_INS, nv_edit, 0, 0},
392 {K_KINS, nv_edit, 0, 0},
393 {K_BS, nv_ctrlh, 0, 0},
394 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
395 {K_S_UP, nv_page, NV_SS, BACKWARD},
396 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
397 {K_S_DOWN, nv_page, NV_SS, FORWARD},
398 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
399 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
400 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
401 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
402 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
403 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
404 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
405 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
406 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
407 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
408 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
409 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
410 {K_S_END, nv_end, NV_SS, FALSE},
411 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
412 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
413 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
414 {K_S_HOME, nv_home, NV_SS, 0},
415 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
416 {K_DEL, nv_abbrev, 0, 0},
417 {K_KDEL, nv_abbrev, 0, 0},
418 {K_UNDO, nv_kundo, 0, 0},
419 {K_HELP, nv_help, NV_NCW, 0},
420 {K_F1, nv_help, NV_NCW, 0},
421 {K_XF1, nv_help, NV_NCW, 0},
422 #ifdef FEAT_VISUAL
423 {K_SELECT, nv_select, 0, 0},
424 #endif
425 #ifdef FEAT_GUI
426 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
427 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
428 #endif
429 #ifdef FEAT_GUI_TABLINE
430 {K_TABLINE, nv_tabline, 0, 0},
431 {K_TABMENU, nv_tabmenu, 0, 0},
432 #endif
433 #ifdef FEAT_FKMAP
434 {K_F8, farsi_fkey, 0, 0},
435 {K_F9, farsi_fkey, 0, 0},
436 #endif
437 #ifdef FEAT_SNIFF
438 {K_SNIFF, nv_sniff, 0, 0},
439 #endif
440 #ifdef FEAT_NETBEANS_INTG
441 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
442 #endif
443 #ifdef FEAT_DND
444 {K_DROP, nv_drop, NV_STS, 0},
445 #endif
446 #ifdef FEAT_AUTOCMD
447 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
448 #endif
451 /* Number of commands in nv_cmds[]. */
452 #define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
454 /* Sorted index of commands in nv_cmds[]. */
455 static short nv_cmd_idx[NV_CMDS_SIZE];
457 /* The highest index for which
458 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
459 static int nv_max_linear;
462 * Compare functions for qsort() below, that checks the command character
463 * through the index in nv_cmd_idx[].
465 static int
466 #ifdef __BORLANDC__
467 _RTLENTRYF
468 #endif
469 nv_compare(s1, s2)
470 const void *s1;
471 const void *s2;
473 int c1, c2;
475 /* The commands are sorted on absolute value. */
476 c1 = nv_cmds[*(const short *)s1].cmd_char;
477 c2 = nv_cmds[*(const short *)s2].cmd_char;
478 if (c1 < 0)
479 c1 = -c1;
480 if (c2 < 0)
481 c2 = -c2;
482 return c1 - c2;
486 * Initialize the nv_cmd_idx[] table.
488 void
489 init_normal_cmds()
491 int i;
493 /* Fill the index table with a one to one relation. */
494 for (i = 0; i < NV_CMDS_SIZE; ++i)
495 nv_cmd_idx[i] = i;
497 /* Sort the commands by the command character. */
498 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
500 /* Find the first entry that can't be indexed by the command character. */
501 for (i = 0; i < NV_CMDS_SIZE; ++i)
502 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
503 break;
504 nv_max_linear = i - 1;
508 * Search for a command in the commands table.
509 * Returns -1 for invalid command.
511 static int
512 find_command(cmdchar)
513 int cmdchar;
515 int i;
516 int idx;
517 int top, bot;
518 int c;
520 #ifdef FEAT_MBYTE
521 /* A multi-byte character is never a command. */
522 if (cmdchar >= 0x100)
523 return -1;
524 #endif
526 /* We use the absolute value of the character. Special keys have a
527 * negative value, but are sorted on their absolute value. */
528 if (cmdchar < 0)
529 cmdchar = -cmdchar;
531 /* If the character is in the first part: The character is the index into
532 * nv_cmd_idx[]. */
533 if (cmdchar <= nv_max_linear)
534 return nv_cmd_idx[cmdchar];
536 /* Perform a binary search. */
537 bot = nv_max_linear + 1;
538 top = NV_CMDS_SIZE - 1;
539 idx = -1;
540 while (bot <= top)
542 i = (top + bot) / 2;
543 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
544 if (c < 0)
545 c = -c;
546 if (cmdchar == c)
548 idx = nv_cmd_idx[i];
549 break;
551 if (cmdchar > c)
552 bot = i + 1;
553 else
554 top = i - 1;
556 return idx;
560 * Execute a command in Normal mode.
562 /*ARGSUSED*/
563 void
564 normal_cmd(oap, toplevel)
565 oparg_T *oap;
566 int toplevel; /* TRUE when called from main() */
568 cmdarg_T ca; /* command arguments */
569 int c;
570 int ctrl_w = FALSE; /* got CTRL-W command */
571 int old_col = curwin->w_curswant;
572 #ifdef FEAT_CMDL_INFO
573 int need_flushbuf; /* need to call out_flush() */
574 #endif
575 #ifdef FEAT_VISUAL
576 pos_T old_pos; /* cursor position before command */
577 int mapped_len;
578 static int old_mapped_len = 0;
579 #endif
580 int idx;
582 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
583 ca.oap = oap;
585 /* Use a count remembered from before entering an operator. After typing
586 * "3d" we return from normal_cmd() and come back here, the "3" is
587 * remembered in "opcount". */
588 ca.opcount = opcount;
590 #ifdef FEAT_SNIFF
591 want_sniff_request = sniff_connected;
592 #endif
595 * If there is an operator pending, then the command we take this time
596 * will terminate it. Finish_op tells us to finish the operation before
597 * returning this time (unless the operation was cancelled).
599 #ifdef CURSOR_SHAPE
600 c = finish_op;
601 #endif
602 finish_op = (oap->op_type != OP_NOP);
603 #ifdef CURSOR_SHAPE
604 if (finish_op != c)
606 ui_cursor_shape(); /* may show different cursor shape */
607 # ifdef FEAT_MOUSESHAPE
608 update_mouseshape(-1);
609 # endif
611 #endif
613 /* When not finishing an operator and no register name typed, reset the
614 * count. */
615 if (!finish_op && !oap->regname)
616 ca.opcount = 0;
618 #ifdef FEAT_AUTOCMD
619 /* Restore counts from before receiving K_CURSORHOLD. This means after
620 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
621 * "3 * 2". */
622 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
624 ca.opcount = oap->prev_opcount;
625 ca.count0 = oap->prev_count0;
626 oap->prev_opcount = 0;
627 oap->prev_count0 = 0;
629 #endif
631 #ifdef FEAT_VISUAL
632 mapped_len = typebuf_maplen();
633 #endif
635 State = NORMAL_BUSY;
636 #ifdef USE_ON_FLY_SCROLL
637 dont_scroll = FALSE; /* allow scrolling here */
638 #endif
641 * Get the command character from the user.
643 c = safe_vgetc();
645 #ifdef FEAT_LANGMAP
646 LANGMAP_ADJUST(c, TRUE);
647 #endif
649 #ifdef FEAT_VISUAL
651 * If a mapping was started in Visual or Select mode, remember the length
652 * of the mapping. This is used below to not return to Insert mode for as
653 * long as the mapping is being executed.
655 if (restart_edit == 0)
656 old_mapped_len = 0;
657 else if (old_mapped_len
658 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
659 old_mapped_len = typebuf_maplen();
660 #endif
662 if (c == NUL)
663 c = K_ZERO;
665 #ifdef FEAT_VISUAL
667 * In Select mode, typed text replaces the selection.
669 if (VIsual_active
670 && VIsual_select
671 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
673 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
674 * 'insertmode' is set) fake a "d"elete command, Insert mode will
675 * restart automatically.
676 * Insert the typed character in the typeahead buffer, so that it can
677 * be mapped in Insert mode. Required for ":lmap" to work. */
678 ins_char_typebuf(c);
679 if (restart_edit != 0)
680 c = 'd';
681 else
682 c = 'c';
683 msg_nowait = TRUE; /* don't delay going to insert mode */
685 #endif
687 #ifdef FEAT_CMDL_INFO
688 need_flushbuf = add_to_showcmd(c);
689 #endif
691 getcount:
692 #ifdef FEAT_VISUAL
693 if (!(VIsual_active && VIsual_select))
694 #endif
697 * Handle a count before a command and compute ca.count0.
698 * Note that '0' is a command and not the start of a count, but it's
699 * part of a count after other digits.
701 while ( (c >= '1' && c <= '9')
702 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
704 if (c == K_DEL || c == K_KDEL)
706 ca.count0 /= 10;
707 #ifdef FEAT_CMDL_INFO
708 del_from_showcmd(4); /* delete the digit and ~@% */
709 #endif
711 else
712 ca.count0 = ca.count0 * 10 + (c - '0');
713 if (ca.count0 < 0) /* got too large! */
714 ca.count0 = 999999999L;
715 #ifdef FEAT_EVAL
716 /* Set v:count here, when called from main() and not a stuffed
717 * command, so that v:count can be used in an expression mapping
718 * right after the count. */
719 if (toplevel && stuff_empty())
720 set_vcount(ca.count0, ca.count0 == 0 ? 1 : ca.count0);
721 #endif
722 if (ctrl_w)
724 ++no_mapping;
725 ++allow_keys; /* no mapping for nchar, but keys */
727 ++no_zero_mapping; /* don't map zero here */
728 c = plain_vgetc();
729 #ifdef FEAT_LANGMAP
730 LANGMAP_ADJUST(c, TRUE);
731 #endif
732 --no_zero_mapping;
733 if (ctrl_w)
735 --no_mapping;
736 --allow_keys;
738 #ifdef FEAT_CMDL_INFO
739 need_flushbuf |= add_to_showcmd(c);
740 #endif
744 * If we got CTRL-W there may be a/another count
746 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
748 ctrl_w = TRUE;
749 ca.opcount = ca.count0; /* remember first count */
750 ca.count0 = 0;
751 ++no_mapping;
752 ++allow_keys; /* no mapping for nchar, but keys */
753 c = plain_vgetc(); /* get next character */
754 #ifdef FEAT_LANGMAP
755 LANGMAP_ADJUST(c, TRUE);
756 #endif
757 --no_mapping;
758 --allow_keys;
759 #ifdef FEAT_CMDL_INFO
760 need_flushbuf |= add_to_showcmd(c);
761 #endif
762 goto getcount; /* jump back */
766 #ifdef FEAT_AUTOCMD
767 if (c == K_CURSORHOLD)
769 /* Save the count values so that ca.opcount and ca.count0 are exactly
770 * the same when coming back here after handling K_CURSORHOLD. */
771 oap->prev_opcount = ca.opcount;
772 oap->prev_count0 = ca.count0;
774 else
775 #endif
776 if (ca.opcount != 0)
779 * If we're in the middle of an operator (including after entering a
780 * yank buffer with '"') AND we had a count before the operator, then
781 * that count overrides the current value of ca.count0.
782 * What this means effectively, is that commands like "3dw" get turned
783 * into "d3w" which makes things fall into place pretty neatly.
784 * If you give a count before AND after the operator, they are
785 * multiplied.
787 if (ca.count0)
788 ca.count0 *= ca.opcount;
789 else
790 ca.count0 = ca.opcount;
794 * Always remember the count. It will be set to zero (on the next call,
795 * above) when there is no pending operator.
796 * When called from main(), save the count for use by the "count" built-in
797 * variable.
799 ca.opcount = ca.count0;
800 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
802 #ifdef FEAT_EVAL
804 * Only set v:count when called from main() and not a stuffed command.
806 if (toplevel && stuff_empty())
807 set_vcount(ca.count0, ca.count1);
808 #endif
811 * Find the command character in the table of commands.
812 * For CTRL-W we already got nchar when looking for a count.
814 if (ctrl_w)
816 ca.nchar = c;
817 ca.cmdchar = Ctrl_W;
819 else
820 ca.cmdchar = c;
821 idx = find_command(ca.cmdchar);
822 if (idx < 0)
824 /* Not a known command: beep. */
825 clearopbeep(oap);
826 goto normal_end;
829 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
831 /* This command is not allowed while editing a ccmdline: beep. */
832 clearopbeep(oap);
833 text_locked_msg();
834 goto normal_end;
836 #ifdef FEAT_AUTOCMD
837 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
838 goto normal_end;
839 #endif
841 #ifdef FEAT_VISUAL
843 * In Visual/Select mode, a few keys are handled in a special way.
845 if (VIsual_active)
847 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
848 if (km_stopsel
849 && (nv_cmds[idx].cmd_flags & NV_STS)
850 && !(mod_mask & MOD_MASK_SHIFT))
852 end_visual_mode();
853 redraw_curbuf_later(INVERTED);
856 /* Keys that work different when 'keymodel' contains "startsel" */
857 if (km_startsel)
859 if (nv_cmds[idx].cmd_flags & NV_SS)
861 unshift_special(&ca);
862 idx = find_command(ca.cmdchar);
863 if (idx < 0)
865 /* Just in case */
866 clearopbeep(oap);
867 goto normal_end;
870 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
871 && (mod_mask & MOD_MASK_SHIFT))
873 mod_mask &= ~MOD_MASK_SHIFT;
877 #endif
879 #ifdef FEAT_RIGHTLEFT
880 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
881 && (nv_cmds[idx].cmd_flags & NV_RL))
883 /* Invert horizontal movements and operations. Only when typed by the
884 * user directly, not when the result of a mapping or "x" translated
885 * to "dl". */
886 switch (ca.cmdchar)
888 case 'l': ca.cmdchar = 'h'; break;
889 case K_RIGHT: ca.cmdchar = K_LEFT; break;
890 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
891 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
892 case 'h': ca.cmdchar = 'l'; break;
893 case K_LEFT: ca.cmdchar = K_RIGHT; break;
894 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
895 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
896 case '>': ca.cmdchar = '<'; break;
897 case '<': ca.cmdchar = '>'; break;
899 idx = find_command(ca.cmdchar);
901 #endif
904 * Get an additional character if we need one.
906 if ((nv_cmds[idx].cmd_flags & NV_NCH)
907 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
908 && oap->op_type == OP_NOP)
909 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
910 || (ca.cmdchar == 'q'
911 && oap->op_type == OP_NOP
912 && !Recording
913 && !Exec_reg)
914 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
915 && (oap->op_type != OP_NOP
916 #ifdef FEAT_VISUAL
917 || VIsual_active
918 #endif
919 ))))
921 int *cp;
922 int repl = FALSE; /* get character for replace mode */
923 int lit = FALSE; /* get extra character literally */
924 int langmap_active = FALSE; /* using :lmap mappings */
925 int lang; /* getting a text character */
926 #ifdef USE_IM_CONTROL
927 int save_smd; /* saved value of p_smd */
928 #endif
930 ++no_mapping;
931 ++allow_keys; /* no mapping for nchar, but allow key codes */
932 #ifdef FEAT_AUTOCMD
933 /* Don't generate a CursorHold event here, most commands can't handle
934 * it, e.g., nv_replace(), nv_csearch(). */
935 did_cursorhold = TRUE;
936 #endif
937 if (ca.cmdchar == 'g')
940 * For 'g' get the next character now, so that we can check for
941 * "gr", "g'" and "g`".
943 ca.nchar = plain_vgetc();
944 #ifdef FEAT_LANGMAP
945 LANGMAP_ADJUST(ca.nchar, TRUE);
946 #endif
947 #ifdef FEAT_CMDL_INFO
948 need_flushbuf |= add_to_showcmd(ca.nchar);
949 #endif
950 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
951 || ca.nchar == Ctrl_BSL)
953 cp = &ca.extra_char; /* need to get a third character */
954 if (ca.nchar != 'r')
955 lit = TRUE; /* get it literally */
956 else
957 repl = TRUE; /* get it in replace mode */
959 else
960 cp = NULL; /* no third character needed */
962 else
964 if (ca.cmdchar == 'r') /* get it in replace mode */
965 repl = TRUE;
966 cp = &ca.nchar;
968 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
971 * Get a second or third character.
973 if (cp != NULL)
975 #ifdef CURSOR_SHAPE
976 if (repl)
978 State = REPLACE; /* pretend Replace mode */
979 ui_cursor_shape(); /* show different cursor shape */
981 #endif
982 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
984 /* Allow mappings defined with ":lmap". */
985 --no_mapping;
986 --allow_keys;
987 if (repl)
988 State = LREPLACE;
989 else
990 State = LANGMAP;
991 langmap_active = TRUE;
993 #ifdef USE_IM_CONTROL
994 save_smd = p_smd;
995 p_smd = FALSE; /* Don't let the IM code show the mode here */
996 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
997 im_set_active(TRUE);
998 #endif
1000 *cp = plain_vgetc();
1002 if (langmap_active)
1004 /* Undo the decrement done above */
1005 ++no_mapping;
1006 ++allow_keys;
1007 State = NORMAL_BUSY;
1009 #ifdef USE_IM_CONTROL
1010 if (lang)
1012 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
1013 im_save_status(&curbuf->b_p_iminsert);
1014 im_set_active(FALSE);
1016 p_smd = save_smd;
1017 #endif
1018 #ifdef CURSOR_SHAPE
1019 State = NORMAL_BUSY;
1020 #endif
1021 #ifdef FEAT_CMDL_INFO
1022 need_flushbuf |= add_to_showcmd(*cp);
1023 #endif
1025 if (!lit)
1027 #ifdef FEAT_DIGRAPHS
1028 /* Typing CTRL-K gets a digraph. */
1029 if (*cp == Ctrl_K
1030 && ((nv_cmds[idx].cmd_flags & NV_LANG)
1031 || cp == &ca.extra_char)
1032 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1034 c = get_digraph(FALSE);
1035 if (c > 0)
1037 *cp = c;
1038 # ifdef FEAT_CMDL_INFO
1039 /* Guessing how to update showcmd here... */
1040 del_from_showcmd(3);
1041 need_flushbuf |= add_to_showcmd(*cp);
1042 # endif
1045 #endif
1047 #ifdef FEAT_LANGMAP
1048 /* adjust chars > 127, except after "tTfFr" commands */
1049 LANGMAP_ADJUST(*cp, !lang);
1050 #endif
1051 #ifdef FEAT_RIGHTLEFT
1052 /* adjust Hebrew mapped char */
1053 if (p_hkmap && lang && KeyTyped)
1054 *cp = hkmap(*cp);
1055 # ifdef FEAT_FKMAP
1056 /* adjust Farsi mapped char */
1057 if (p_fkmap && lang && KeyTyped)
1058 *cp = fkmap(*cp);
1059 # endif
1060 #endif
1064 * When the next character is CTRL-\ a following CTRL-N means the
1065 * command is aborted and we go to Normal mode.
1067 if (cp == &ca.extra_char
1068 && ca.nchar == Ctrl_BSL
1069 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1071 ca.cmdchar = Ctrl_BSL;
1072 ca.nchar = ca.extra_char;
1073 idx = find_command(ca.cmdchar);
1075 else if (*cp == Ctrl_BSL)
1077 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1079 /* There is a busy wait here when typing "f<C-\>" and then
1080 * something different from CTRL-N. Can't be avoided. */
1081 while ((c = vpeekc()) <= 0 && towait > 0L)
1083 do_sleep(towait > 50L ? 50L : towait);
1084 towait -= 50L;
1086 if (c > 0)
1088 c = plain_vgetc();
1089 if (c != Ctrl_N && c != Ctrl_G)
1090 vungetc(c);
1091 else
1093 ca.cmdchar = Ctrl_BSL;
1094 ca.nchar = c;
1095 idx = find_command(ca.cmdchar);
1100 #ifdef FEAT_MBYTE
1101 /* When getting a text character and the next character is a
1102 * multi-byte character, it could be a composing character.
1103 * However, don't wait for it to arrive. */
1104 while (enc_utf8 && lang && (c = vpeekc()) > 0
1105 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1107 c = plain_vgetc();
1108 if (!utf_iscomposing(c))
1110 vungetc(c); /* it wasn't, put it back */
1111 break;
1113 else if (ca.ncharC1 == 0)
1114 ca.ncharC1 = c;
1115 else
1116 ca.ncharC2 = c;
1118 #endif
1120 --no_mapping;
1121 --allow_keys;
1124 #ifdef FEAT_CMDL_INFO
1126 * Flush the showcmd characters onto the screen so we can see them while
1127 * the command is being executed. Only do this when the shown command was
1128 * actually displayed, otherwise this will slow down a lot when executing
1129 * mappings.
1131 if (need_flushbuf)
1132 out_flush();
1133 #endif
1134 #ifdef FEAT_AUTOCMD
1135 if (ca.cmdchar != K_IGNORE)
1136 did_cursorhold = FALSE;
1137 #endif
1139 State = NORMAL;
1141 if (ca.nchar == ESC)
1143 clearop(oap);
1144 if (restart_edit == 0 && goto_im())
1145 restart_edit = 'a';
1146 goto normal_end;
1149 if (ca.cmdchar != K_IGNORE)
1151 msg_didout = FALSE; /* don't scroll screen up for normal command */
1152 msg_col = 0;
1155 #ifdef FEAT_VISUAL
1156 old_pos = curwin->w_cursor; /* remember where cursor was */
1158 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1159 * mode. */
1160 if (!VIsual_active && km_startsel)
1162 if (nv_cmds[idx].cmd_flags & NV_SS)
1164 start_selection();
1165 unshift_special(&ca);
1166 idx = find_command(ca.cmdchar);
1168 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1169 && (mod_mask & MOD_MASK_SHIFT))
1171 start_selection();
1172 mod_mask &= ~MOD_MASK_SHIFT;
1175 #endif
1178 * Execute the command!
1179 * Call the command function found in the commands table.
1181 ca.arg = nv_cmds[idx].cmd_arg;
1182 (nv_cmds[idx].cmd_func)(&ca);
1185 * If we didn't start or finish an operator, reset oap->regname, unless we
1186 * need it later.
1188 if (!finish_op
1189 && !oap->op_type
1190 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1192 clearop(oap);
1193 #ifdef FEAT_EVAL
1194 set_reg_var('"');
1195 #endif
1198 #ifdef FEAT_VISUAL
1199 /* Get the length of mapped chars again after typing a count, second
1200 * character or "z333<cr>". */
1201 if (old_mapped_len > 0)
1202 old_mapped_len = typebuf_maplen();
1203 #endif
1206 * If an operation is pending, handle it...
1208 do_pending_operator(&ca, old_col, FALSE);
1211 * Wait for a moment when a message is displayed that will be overwritten
1212 * by the mode message.
1213 * In Visual mode and with "^O" in Insert mode, a short message will be
1214 * overwritten by the mode message. Wait a bit, until a key is hit.
1215 * In Visual mode, it's more important to keep the Visual area updated
1216 * than keeping a message (e.g. from a /pat search).
1217 * Only do this if the command was typed, not from a mapping.
1218 * Don't wait when emsg_silent is non-zero.
1219 * Also wait a bit after an error message, e.g. for "^O:".
1220 * Don't redraw the screen, it would remove the message.
1222 if ( ((p_smd
1223 && msg_silent == 0
1224 && (restart_edit != 0
1225 #ifdef FEAT_VISUAL
1226 || (VIsual_active
1227 && old_pos.lnum == curwin->w_cursor.lnum
1228 && old_pos.col == curwin->w_cursor.col)
1229 #endif
1231 && (clear_cmdline
1232 || redraw_cmdline)
1233 && (msg_didout || (msg_didany && msg_scroll))
1234 && !msg_nowait
1235 && KeyTyped)
1236 || (restart_edit != 0
1237 #ifdef FEAT_VISUAL
1238 && !VIsual_active
1239 #endif
1240 && (msg_scroll
1241 || emsg_on_display)))
1242 && oap->regname == 0
1243 && !(ca.retval & CA_COMMAND_BUSY)
1244 && stuff_empty()
1245 && typebuf_typed()
1246 && emsg_silent == 0
1247 && !did_wait_return
1248 && oap->op_type == OP_NOP)
1250 int save_State = State;
1252 /* Draw the cursor with the right shape here */
1253 if (restart_edit != 0)
1254 State = INSERT;
1256 /* If need to redraw, and there is a "keep_msg", redraw before the
1257 * delay */
1258 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1260 char_u *kmsg;
1262 kmsg = keep_msg;
1263 keep_msg = NULL;
1264 /* showmode() will clear keep_msg, but we want to use it anyway */
1265 update_screen(0);
1266 /* now reset it, otherwise it's put in the history again */
1267 keep_msg = kmsg;
1268 msg_attr(kmsg, keep_msg_attr);
1269 vim_free(kmsg);
1271 setcursor();
1272 cursor_on();
1273 out_flush();
1274 if (msg_scroll || emsg_on_display)
1275 ui_delay(1000L, TRUE); /* wait at least one second */
1276 ui_delay(3000L, FALSE); /* wait up to three seconds */
1277 State = save_State;
1279 msg_scroll = FALSE;
1280 emsg_on_display = FALSE;
1284 * Finish up after executing a Normal mode command.
1286 normal_end:
1288 msg_nowait = FALSE;
1290 /* Reset finish_op, in case it was set */
1291 #ifdef CURSOR_SHAPE
1292 c = finish_op;
1293 #endif
1294 finish_op = FALSE;
1295 #ifdef CURSOR_SHAPE
1296 /* Redraw the cursor with another shape, if we were in Operator-pending
1297 * mode or did a replace command. */
1298 if (c || ca.cmdchar == 'r')
1300 ui_cursor_shape(); /* may show different cursor shape */
1301 # ifdef FEAT_MOUSESHAPE
1302 update_mouseshape(-1);
1303 # endif
1305 #endif
1307 #ifdef FEAT_CMDL_INFO
1308 if (oap->op_type == OP_NOP && oap->regname == 0
1309 # ifdef FEAT_AUTOCMD
1310 && ca.cmdchar != K_CURSORHOLD
1311 # endif
1313 clear_showcmd();
1314 #endif
1316 checkpcmark(); /* check if we moved since setting pcmark */
1317 vim_free(ca.searchbuf);
1319 #ifdef FEAT_MBYTE
1320 if (has_mbyte)
1321 mb_adjust_cursor();
1322 #endif
1324 #ifdef FEAT_SCROLLBIND
1325 if (curwin->w_p_scb && toplevel)
1327 validate_cursor(); /* may need to update w_leftcol */
1328 do_check_scrollbind(TRUE);
1330 #endif
1333 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1334 * if still inside a mapping that started in Visual mode).
1335 * May switch from Visual to Select mode after CTRL-O command.
1337 if ( oap->op_type == OP_NOP
1338 #ifdef FEAT_VISUAL
1339 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1340 || restart_VIsual_select == 1)
1341 #else
1342 && restart_edit != 0
1343 #endif
1344 && !(ca.retval & CA_COMMAND_BUSY)
1345 && stuff_empty()
1346 && oap->regname == 0)
1348 #ifdef FEAT_VISUAL
1349 if (restart_VIsual_select == 1)
1351 VIsual_select = TRUE;
1352 showmode();
1353 restart_VIsual_select = 0;
1355 #endif
1356 if (restart_edit != 0
1357 #ifdef FEAT_VISUAL
1358 && !VIsual_active && old_mapped_len == 0
1359 #endif
1361 (void)edit(restart_edit, FALSE, 1L);
1364 #ifdef FEAT_VISUAL
1365 if (restart_VIsual_select == 2)
1366 restart_VIsual_select = 1;
1367 #endif
1369 /* Save count before an operator for next time. */
1370 opcount = ca.opcount;
1374 * Handle an operator after visual mode or when the movement is finished
1376 void
1377 do_pending_operator(cap, old_col, gui_yank)
1378 cmdarg_T *cap;
1379 int old_col;
1380 int gui_yank;
1382 oparg_T *oap = cap->oap;
1383 pos_T old_cursor;
1384 int empty_region_error;
1385 int restart_edit_save;
1387 #ifdef FEAT_VISUAL
1388 /* The visual area is remembered for redo */
1389 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1390 static linenr_T redo_VIsual_line_count; /* number of lines */
1391 static colnr_T redo_VIsual_col; /* number of cols or end column */
1392 static long redo_VIsual_count; /* count for Visual operator */
1393 # ifdef FEAT_VIRTUALEDIT
1394 int include_line_break = FALSE;
1395 # endif
1396 #endif
1398 #if defined(FEAT_CLIPBOARD)
1400 * Yank the visual area into the GUI selection register before we operate
1401 * on it and lose it forever.
1402 * Don't do it if a specific register was specified, so that ""x"*P works.
1403 * This could call do_pending_operator() recursively, but that's OK
1404 * because gui_yank will be TRUE for the nested call.
1406 if (clip_star.available
1407 && oap->op_type != OP_NOP
1408 && !gui_yank
1409 # ifdef FEAT_VISUAL
1410 && VIsual_active
1411 && !redo_VIsual_busy
1412 # endif
1413 && oap->regname == 0)
1414 clip_auto_select();
1415 #endif
1416 old_cursor = curwin->w_cursor;
1419 * If an operation is pending, handle it...
1421 if ((finish_op
1422 #ifdef FEAT_VISUAL
1423 || VIsual_active
1424 #endif
1425 ) && oap->op_type != OP_NOP)
1427 #ifdef FEAT_VISUAL
1428 oap->is_VIsual = VIsual_active;
1429 if (oap->motion_force == 'V')
1430 oap->motion_type = MLINE;
1431 else if (oap->motion_force == 'v')
1433 /* If the motion was linewise, "inclusive" will not have been set.
1434 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1435 if (oap->motion_type == MLINE)
1436 oap->inclusive = FALSE;
1437 /* If the motion already was characterwise, toggle "inclusive" */
1438 else if (oap->motion_type == MCHAR)
1439 oap->inclusive = !oap->inclusive;
1440 oap->motion_type = MCHAR;
1442 else if (oap->motion_force == Ctrl_V)
1444 /* Change line- or characterwise motion into Visual block mode. */
1445 VIsual_active = TRUE;
1446 VIsual = oap->start;
1447 VIsual_mode = Ctrl_V;
1448 VIsual_select = FALSE;
1449 VIsual_reselect = FALSE;
1451 #endif
1453 /* only redo yank when 'y' flag is in 'cpoptions' */
1454 /* never redo "zf" (define fold) */
1455 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1456 #ifdef FEAT_VISUAL
1457 && (!VIsual_active || oap->motion_force)
1458 #endif
1459 && cap->cmdchar != 'D'
1460 #ifdef FEAT_FOLDING
1461 && oap->op_type != OP_FOLD
1462 && oap->op_type != OP_FOLDOPEN
1463 && oap->op_type != OP_FOLDOPENREC
1464 && oap->op_type != OP_FOLDCLOSE
1465 && oap->op_type != OP_FOLDCLOSEREC
1466 && oap->op_type != OP_FOLDDEL
1467 && oap->op_type != OP_FOLDDELREC
1468 #endif
1471 prep_redo(oap->regname, cap->count0,
1472 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1473 oap->motion_force, cap->cmdchar, cap->nchar);
1474 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1477 * If 'cpoptions' does not contain 'r', insert the search
1478 * pattern to really repeat the same command.
1480 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
1481 AppendToRedobuffLit(cap->searchbuf, -1);
1482 AppendToRedobuff(NL_STR);
1484 else if (cap->cmdchar == ':')
1486 /* do_cmdline() has stored the first typed line in
1487 * "repeat_cmdline". When several lines are typed repeating
1488 * won't be possible. */
1489 if (repeat_cmdline == NULL)
1490 ResetRedobuff();
1491 else
1493 AppendToRedobuffLit(repeat_cmdline, -1);
1494 AppendToRedobuff(NL_STR);
1495 vim_free(repeat_cmdline);
1496 repeat_cmdline = NULL;
1501 #ifdef FEAT_VISUAL
1502 if (redo_VIsual_busy)
1504 oap->start = curwin->w_cursor;
1505 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1506 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1507 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1508 VIsual_mode = redo_VIsual_mode;
1509 if (VIsual_mode == 'v')
1511 if (redo_VIsual_line_count <= 1)
1512 curwin->w_cursor.col += redo_VIsual_col - 1;
1513 else
1514 curwin->w_cursor.col = redo_VIsual_col;
1516 if (redo_VIsual_col == MAXCOL)
1518 curwin->w_curswant = MAXCOL;
1519 coladvance((colnr_T)MAXCOL);
1521 cap->count0 = redo_VIsual_count;
1522 if (redo_VIsual_count != 0)
1523 cap->count1 = redo_VIsual_count;
1524 else
1525 cap->count1 = 1;
1527 else if (VIsual_active)
1529 if (!gui_yank)
1531 /* Save the current VIsual area for '< and '> marks, and "gv" */
1532 curbuf->b_visual.vi_start = VIsual;
1533 curbuf->b_visual.vi_end = curwin->w_cursor;
1534 curbuf->b_visual.vi_mode = VIsual_mode;
1535 curbuf->b_visual.vi_curswant = curwin->w_curswant;
1536 # ifdef FEAT_EVAL
1537 curbuf->b_visual_mode_eval = VIsual_mode;
1538 # endif
1541 /* In Select mode, a linewise selection is operated upon like a
1542 * characterwise selection. */
1543 if (VIsual_select && VIsual_mode == 'V')
1545 if (lt(VIsual, curwin->w_cursor))
1547 VIsual.col = 0;
1548 curwin->w_cursor.col =
1549 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1551 else
1553 curwin->w_cursor.col = 0;
1554 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1556 VIsual_mode = 'v';
1558 /* If 'selection' is "exclusive", backup one character for
1559 * charwise selections. */
1560 else if (VIsual_mode == 'v')
1562 # ifdef FEAT_VIRTUALEDIT
1563 include_line_break =
1564 # endif
1565 unadjust_for_sel();
1568 oap->start = VIsual;
1569 if (VIsual_mode == 'V')
1570 oap->start.col = 0;
1572 #endif /* FEAT_VISUAL */
1575 * Set oap->start to the first position of the operated text, oap->end
1576 * to the end of the operated text. w_cursor is equal to oap->start.
1578 if (lt(oap->start, curwin->w_cursor))
1580 #ifdef FEAT_FOLDING
1581 /* Include folded lines completely. */
1582 if (!VIsual_active)
1584 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1585 oap->start.col = 0;
1586 if (hasFolding(curwin->w_cursor.lnum, NULL,
1587 &curwin->w_cursor.lnum))
1588 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1590 #endif
1591 oap->end = curwin->w_cursor;
1592 curwin->w_cursor = oap->start;
1594 /* w_virtcol may have been updated; if the cursor goes back to its
1595 * previous position w_virtcol becomes invalid and isn't updated
1596 * automatically. */
1597 curwin->w_valid &= ~VALID_VIRTCOL;
1599 else
1601 #ifdef FEAT_FOLDING
1602 /* Include folded lines completely. */
1603 if (!VIsual_active && oap->motion_type == MLINE)
1605 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1606 NULL))
1607 curwin->w_cursor.col = 0;
1608 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1609 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1611 #endif
1612 oap->end = oap->start;
1613 oap->start = curwin->w_cursor;
1616 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1618 #ifdef FEAT_VIRTUALEDIT
1619 /* Set "virtual_op" before resetting VIsual_active. */
1620 virtual_op = virtual_active();
1621 #endif
1623 #ifdef FEAT_VISUAL
1624 if (VIsual_active || redo_VIsual_busy)
1626 if (VIsual_mode == Ctrl_V) /* block mode */
1628 colnr_T start, end;
1630 oap->block_mode = TRUE;
1632 getvvcol(curwin, &(oap->start),
1633 &oap->start_vcol, NULL, &oap->end_vcol);
1634 if (!redo_VIsual_busy)
1636 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1638 if (start < oap->start_vcol)
1639 oap->start_vcol = start;
1640 if (end > oap->end_vcol)
1642 if (*p_sel == 'e' && start >= 1
1643 && start - 1 >= oap->end_vcol)
1644 oap->end_vcol = start - 1;
1645 else
1646 oap->end_vcol = end;
1650 /* if '$' was used, get oap->end_vcol from longest line */
1651 if (curwin->w_curswant == MAXCOL)
1653 curwin->w_cursor.col = MAXCOL;
1654 oap->end_vcol = 0;
1655 for (curwin->w_cursor.lnum = oap->start.lnum;
1656 curwin->w_cursor.lnum <= oap->end.lnum;
1657 ++curwin->w_cursor.lnum)
1659 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1660 if (end > oap->end_vcol)
1661 oap->end_vcol = end;
1664 else if (redo_VIsual_busy)
1665 oap->end_vcol = oap->start_vcol + redo_VIsual_col - 1;
1667 * Correct oap->end.col and oap->start.col to be the
1668 * upper-left and lower-right corner of the block area.
1670 * (Actually, this does convert column positions into character
1671 * positions)
1673 curwin->w_cursor.lnum = oap->end.lnum;
1674 coladvance(oap->end_vcol);
1675 oap->end = curwin->w_cursor;
1677 curwin->w_cursor = oap->start;
1678 coladvance(oap->start_vcol);
1679 oap->start = curwin->w_cursor;
1682 if (!redo_VIsual_busy && !gui_yank)
1685 * Prepare to reselect and redo Visual: this is based on the
1686 * size of the Visual text
1688 resel_VIsual_mode = VIsual_mode;
1689 if (curwin->w_curswant == MAXCOL)
1690 resel_VIsual_col = MAXCOL;
1691 else if (VIsual_mode == Ctrl_V)
1692 resel_VIsual_col = oap->end_vcol - oap->start_vcol + 1;
1693 else if (oap->line_count > 1)
1694 resel_VIsual_col = oap->end.col;
1695 else
1696 resel_VIsual_col = oap->end.col - oap->start.col + 1;
1697 resel_VIsual_line_count = oap->line_count;
1700 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1701 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1702 && oap->op_type != OP_COLON
1703 #ifdef FEAT_FOLDING
1704 && oap->op_type != OP_FOLD
1705 && oap->op_type != OP_FOLDOPEN
1706 && oap->op_type != OP_FOLDOPENREC
1707 && oap->op_type != OP_FOLDCLOSE
1708 && oap->op_type != OP_FOLDCLOSEREC
1709 && oap->op_type != OP_FOLDDEL
1710 && oap->op_type != OP_FOLDDELREC
1711 #endif
1712 && oap->motion_force == NUL
1715 /* Prepare for redoing. Only use the nchar field for "r",
1716 * otherwise it might be the second char of the operator. */
1717 prep_redo(oap->regname, 0L, NUL, 'v',
1718 get_op_char(oap->op_type),
1719 get_extra_op_char(oap->op_type),
1720 oap->op_type == OP_REPLACE ? cap->nchar : NUL);
1721 if (!redo_VIsual_busy)
1723 redo_VIsual_mode = resel_VIsual_mode;
1724 redo_VIsual_col = resel_VIsual_col;
1725 redo_VIsual_line_count = resel_VIsual_line_count;
1726 redo_VIsual_count = cap->count0;
1731 * oap->inclusive defaults to TRUE.
1732 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1733 * FALSE. This makes "d}P" and "v}dP" work the same.
1735 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1736 oap->inclusive = TRUE;
1737 if (VIsual_mode == 'V')
1738 oap->motion_type = MLINE;
1739 else
1741 oap->motion_type = MCHAR;
1742 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1743 # ifdef FEAT_VIRTUALEDIT
1744 && (include_line_break || !virtual_op)
1745 # endif
1748 oap->inclusive = FALSE;
1749 /* Try to include the newline, unless it's an operator
1750 * that works on lines only */
1751 if (*p_sel != 'o'
1752 && !op_on_lines(oap->op_type)
1753 && oap->end.lnum < curbuf->b_ml.ml_line_count)
1755 ++oap->end.lnum;
1756 oap->end.col = 0;
1757 # ifdef FEAT_VIRTUALEDIT
1758 oap->end.coladd = 0;
1759 # endif
1760 ++oap->line_count;
1765 redo_VIsual_busy = FALSE;
1768 * Switch Visual off now, so screen updating does
1769 * not show inverted text when the screen is redrawn.
1770 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1771 * no screen redraw, so it is done here to remove the inverted
1772 * part.
1774 if (!gui_yank)
1776 VIsual_active = FALSE;
1777 # ifdef FEAT_MOUSE
1778 setmouse();
1779 mouse_dragging = 0;
1780 # endif
1781 if (mode_displayed)
1782 clear_cmdline = TRUE; /* unshow visual mode later */
1783 #ifdef FEAT_CMDL_INFO
1784 else
1785 clear_showcmd();
1786 #endif
1787 if ((oap->op_type == OP_YANK
1788 || oap->op_type == OP_COLON
1789 || oap->op_type == OP_FUNCTION
1790 || oap->op_type == OP_FILTER)
1791 && oap->motion_force == NUL)
1792 redraw_curbuf_later(INVERTED);
1795 #endif
1797 #ifdef FEAT_MBYTE
1798 /* Include the trailing byte of a multi-byte char. */
1799 if (has_mbyte && oap->inclusive)
1801 int l;
1803 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
1804 if (l > 1)
1805 oap->end.col += l - 1;
1807 #endif
1808 curwin->w_set_curswant = TRUE;
1811 * oap->empty is set when start and end are the same. The inclusive
1812 * flag affects this too, unless yanking and the end is on a NUL.
1814 oap->empty = (oap->motion_type == MCHAR
1815 && (!oap->inclusive
1816 || (oap->op_type == OP_YANK
1817 && gchar_pos(&oap->end) == NUL))
1818 && equalpos(oap->start, oap->end)
1819 #ifdef FEAT_VIRTUALEDIT
1820 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1821 #endif
1824 * For delete, change and yank, it's an error to operate on an
1825 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1827 empty_region_error = (oap->empty
1828 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1830 #ifdef FEAT_VISUAL
1831 /* Force a redraw when operating on an empty Visual region, when
1832 * 'modifiable is off or creating a fold. */
1833 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1834 # ifdef FEAT_FOLDING
1835 || oap->op_type == OP_FOLD
1836 # endif
1838 redraw_curbuf_later(INVERTED);
1839 #endif
1842 * If the end of an operator is in column one while oap->motion_type
1843 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1844 * character in the previous line. If op_start is on or before the
1845 * first non-blank in the line, the operator becomes linewise
1846 * (strange, but that's the way vi does it).
1848 if ( oap->motion_type == MCHAR
1849 && oap->inclusive == FALSE
1850 && !(cap->retval & CA_NO_ADJ_OP_END)
1851 && oap->end.col == 0
1852 #ifdef FEAT_VISUAL
1853 && (!oap->is_VIsual || *p_sel == 'o')
1854 && !oap->block_mode
1855 #endif
1856 && oap->line_count > 1)
1858 oap->end_adjusted = TRUE; /* remember that we did this */
1859 --oap->line_count;
1860 --oap->end.lnum;
1861 if (inindent(0))
1862 oap->motion_type = MLINE;
1863 else
1865 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1866 if (oap->end.col)
1868 --oap->end.col;
1869 oap->inclusive = TRUE;
1873 else
1874 oap->end_adjusted = FALSE;
1876 switch (oap->op_type)
1878 case OP_LSHIFT:
1879 case OP_RSHIFT:
1880 op_shift(oap, TRUE,
1881 #ifdef FEAT_VISUAL
1882 oap->is_VIsual ? (int)cap->count1 :
1883 #endif
1885 auto_format(FALSE, TRUE);
1886 break;
1888 case OP_JOIN_NS:
1889 case OP_JOIN:
1890 if (oap->line_count < 2)
1891 oap->line_count = 2;
1892 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1893 curbuf->b_ml.ml_line_count)
1894 beep_flush();
1895 else
1897 do_do_join(oap->line_count, oap->op_type == OP_JOIN);
1898 auto_format(FALSE, TRUE);
1900 break;
1902 case OP_DELETE:
1903 #ifdef FEAT_VISUAL
1904 VIsual_reselect = FALSE; /* don't reselect now */
1905 #endif
1906 if (empty_region_error)
1907 vim_beep();
1908 else
1910 (void)op_delete(oap);
1911 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1912 u_save_cursor(); /* cursor line wasn't saved yet */
1913 auto_format(FALSE, TRUE);
1915 break;
1917 case OP_YANK:
1918 if (empty_region_error)
1920 if (!gui_yank)
1921 vim_beep();
1923 else
1924 (void)op_yank(oap, FALSE, !gui_yank);
1925 check_cursor_col();
1926 break;
1928 case OP_CHANGE:
1929 #ifdef FEAT_VISUAL
1930 VIsual_reselect = FALSE; /* don't reselect now */
1931 #endif
1932 if (empty_region_error)
1933 vim_beep();
1934 else
1936 /* This is a new edit command, not a restart. Need to
1937 * remember it to make 'insertmode' work with mappings for
1938 * Visual mode. But do this only once and not when typed and
1939 * 'insertmode' isn't set. */
1940 if (p_im || !KeyTyped)
1941 restart_edit_save = restart_edit;
1942 else
1943 restart_edit_save = 0;
1944 restart_edit = 0;
1945 /* Reset finish_op now, don't want it set inside edit(). */
1946 finish_op = FALSE;
1947 if (op_change(oap)) /* will call edit() */
1948 cap->retval |= CA_COMMAND_BUSY;
1949 if (restart_edit == 0)
1950 restart_edit = restart_edit_save;
1952 break;
1954 case OP_FILTER:
1955 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1956 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1957 else
1958 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
1960 case OP_INDENT:
1961 case OP_COLON:
1963 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1965 * If 'equalprg' is empty, do the indenting internally.
1967 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1969 # ifdef FEAT_LISP
1970 if (curbuf->b_p_lisp)
1972 op_reindent(oap, get_lisp_indent);
1973 break;
1975 # endif
1976 # ifdef FEAT_CINDENT
1977 op_reindent(oap,
1978 # ifdef FEAT_EVAL
1979 *curbuf->b_p_inde != NUL ? get_expr_indent :
1980 # endif
1981 get_c_indent);
1982 break;
1983 # endif
1985 #endif
1987 op_colon(oap);
1988 break;
1990 case OP_TILDE:
1991 case OP_UPPER:
1992 case OP_LOWER:
1993 case OP_ROT13:
1994 if (empty_region_error)
1995 vim_beep();
1996 else
1997 op_tilde(oap);
1998 check_cursor_col();
1999 break;
2001 case OP_FORMAT:
2002 #if defined(FEAT_EVAL)
2003 if (*curbuf->b_p_fex != NUL)
2004 op_formatexpr(oap); /* use expression */
2005 else
2006 #endif
2007 if (*p_fp != NUL)
2008 op_colon(oap); /* use external command */
2009 else
2010 op_format(oap, FALSE); /* use internal function */
2011 break;
2013 case OP_FORMAT2:
2014 op_format(oap, TRUE); /* use internal function */
2015 break;
2017 case OP_FUNCTION:
2018 op_function(oap); /* call 'operatorfunc' */
2019 break;
2021 case OP_INSERT:
2022 case OP_APPEND:
2023 #ifdef FEAT_VISUAL
2024 VIsual_reselect = FALSE; /* don't reselect now */
2025 #endif
2026 #ifdef FEAT_VISUALEXTRA
2027 if (empty_region_error)
2028 vim_beep();
2029 else
2031 /* This is a new edit command, not a restart. Need to
2032 * remember it to make 'insertmode' work with mappings for
2033 * Visual mode. But do this only once. */
2034 restart_edit_save = restart_edit;
2035 restart_edit = 0;
2037 op_insert(oap, cap->count1);
2039 /* TODO: when inserting in several lines, should format all
2040 * the lines. */
2041 auto_format(FALSE, TRUE);
2043 if (restart_edit == 0)
2044 restart_edit = restart_edit_save;
2046 #else
2047 vim_beep();
2048 #endif
2049 break;
2051 case OP_REPLACE:
2052 #ifdef FEAT_VISUAL
2053 VIsual_reselect = FALSE; /* don't reselect now */
2054 #endif
2055 #ifdef FEAT_VISUALEXTRA
2056 if (empty_region_error)
2057 #endif
2058 vim_beep();
2059 #ifdef FEAT_VISUALEXTRA
2060 else
2061 op_replace(oap, cap->nchar);
2062 #endif
2063 break;
2065 #ifdef FEAT_FOLDING
2066 case OP_FOLD:
2067 VIsual_reselect = FALSE; /* don't reselect now */
2068 foldCreate(oap->start.lnum, oap->end.lnum);
2069 break;
2071 case OP_FOLDOPEN:
2072 case OP_FOLDOPENREC:
2073 case OP_FOLDCLOSE:
2074 case OP_FOLDCLOSEREC:
2075 VIsual_reselect = FALSE; /* don't reselect now */
2076 opFoldRange(oap->start.lnum, oap->end.lnum,
2077 oap->op_type == OP_FOLDOPEN
2078 || oap->op_type == OP_FOLDOPENREC,
2079 oap->op_type == OP_FOLDOPENREC
2080 || oap->op_type == OP_FOLDCLOSEREC,
2081 oap->is_VIsual);
2082 break;
2084 case OP_FOLDDEL:
2085 case OP_FOLDDELREC:
2086 VIsual_reselect = FALSE; /* don't reselect now */
2087 deleteFold(oap->start.lnum, oap->end.lnum,
2088 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2089 break;
2090 #endif
2091 default:
2092 clearopbeep(oap);
2094 #ifdef FEAT_VIRTUALEDIT
2095 virtual_op = MAYBE;
2096 #endif
2097 if (!gui_yank)
2100 * if 'sol' not set, go back to old column for some commands
2102 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2103 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2104 || oap->op_type == OP_DELETE))
2105 coladvance(curwin->w_curswant = old_col);
2107 else
2109 curwin->w_cursor = old_cursor;
2111 #ifdef FEAT_VISUAL
2112 oap->block_mode = FALSE;
2113 #endif
2114 clearop(oap);
2119 * Handle indent and format operators and visual mode ":".
2121 static void
2122 op_colon(oap)
2123 oparg_T *oap;
2125 stuffcharReadbuff(':');
2126 #ifdef FEAT_VISUAL
2127 if (oap->is_VIsual)
2128 stuffReadbuff((char_u *)"'<,'>");
2129 else
2130 #endif
2133 * Make the range look nice, so it can be repeated.
2135 if (oap->start.lnum == curwin->w_cursor.lnum)
2136 stuffcharReadbuff('.');
2137 else
2138 stuffnumReadbuff((long)oap->start.lnum);
2139 if (oap->end.lnum != oap->start.lnum)
2141 stuffcharReadbuff(',');
2142 if (oap->end.lnum == curwin->w_cursor.lnum)
2143 stuffcharReadbuff('.');
2144 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2145 stuffcharReadbuff('$');
2146 else if (oap->start.lnum == curwin->w_cursor.lnum)
2148 stuffReadbuff((char_u *)".+");
2149 stuffnumReadbuff((long)oap->line_count - 1);
2151 else
2152 stuffnumReadbuff((long)oap->end.lnum);
2155 if (oap->op_type != OP_COLON)
2156 stuffReadbuff((char_u *)"!");
2157 if (oap->op_type == OP_INDENT)
2159 #ifndef FEAT_CINDENT
2160 if (*get_equalprg() == NUL)
2161 stuffReadbuff((char_u *)"indent");
2162 else
2163 #endif
2164 stuffReadbuff(get_equalprg());
2165 stuffReadbuff((char_u *)"\n");
2167 else if (oap->op_type == OP_FORMAT)
2169 if (*p_fp == NUL)
2170 stuffReadbuff((char_u *)"fmt");
2171 else
2172 stuffReadbuff(p_fp);
2173 stuffReadbuff((char_u *)"\n']");
2177 * do_cmdline() does the rest
2182 * Handle the "g@" operator: call 'operatorfunc'.
2184 /*ARGSUSED*/
2185 static void
2186 op_function(oap)
2187 oparg_T *oap;
2189 #ifdef FEAT_EVAL
2190 char_u *(argv[1]);
2192 if (*p_opfunc == NUL)
2193 EMSG(_("E774: 'operatorfunc' is empty"));
2194 else
2196 /* Set '[ and '] marks to text to be operated on. */
2197 curbuf->b_op_start = oap->start;
2198 curbuf->b_op_end = oap->end;
2199 if (oap->motion_type != MLINE && !oap->inclusive)
2200 /* Exclude the end position. */
2201 decl(&curbuf->b_op_end);
2203 if (oap->block_mode)
2204 argv[0] = (char_u *)"block";
2205 else if (oap->motion_type == MLINE)
2206 argv[0] = (char_u *)"line";
2207 else
2208 argv[0] = (char_u *)"char";
2209 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
2211 #else
2212 EMSG(_("E775: Eval feature not available"));
2213 #endif
2216 #if defined(FEAT_MOUSE) || defined(PROTO)
2218 * Do the appropriate action for the current mouse click in the current mode.
2219 * Not used for Command-line mode.
2221 * Normal Mode:
2222 * event modi- position visual change action
2223 * fier cursor window
2224 * left press - yes end yes
2225 * left press C yes end yes "^]" (2)
2226 * left press S yes end yes "*" (2)
2227 * left drag - yes start if moved no
2228 * left relse - yes start if moved no
2229 * middle press - yes if not active no put register
2230 * middle press - yes if active no yank and put
2231 * right press - yes start or extend yes
2232 * right press S yes no change yes "#" (2)
2233 * right drag - yes extend no
2234 * right relse - yes extend no
2236 * Insert or Replace Mode:
2237 * event modi- position visual change action
2238 * fier cursor window
2239 * left press - yes (cannot be active) yes
2240 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2241 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2242 * left drag - yes start or extend (1) no CTRL-O (1)
2243 * left relse - yes start or extend (1) no CTRL-O (1)
2244 * middle press - no (cannot be active) no put register
2245 * right press - yes start or extend yes CTRL-O
2246 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2248 * (1) only if mouse pointer moved since press
2249 * (2) only if click is in same buffer
2251 * Return TRUE if start_arrow() should be called for edit mode.
2254 do_mouse(oap, c, dir, count, fixindent)
2255 oparg_T *oap; /* operator argument, can be NULL */
2256 int c; /* K_LEFTMOUSE, etc */
2257 int dir; /* Direction to 'put' if necessary */
2258 long count;
2259 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2261 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2262 static int got_click = FALSE; /* got a click some time back */
2264 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2265 int is_click; /* If FALSE it's a drag or release event */
2266 int is_drag; /* If TRUE it's a drag event */
2267 int jump_flags = 0; /* flags for jump_to_mouse() */
2268 pos_T start_visual;
2269 int moved; /* Has cursor moved? */
2270 int in_status_line; /* mouse in status line */
2271 #ifdef FEAT_VERTSPLIT
2272 int in_sep_line; /* mouse in vertical separator line */
2273 #endif
2274 int c1, c2;
2275 #if defined(FEAT_FOLDING)
2276 pos_T save_cursor;
2277 #endif
2278 win_T *old_curwin = curwin;
2279 #ifdef FEAT_VISUAL
2280 static pos_T orig_cursor;
2281 colnr_T leftcol, rightcol;
2282 pos_T end_visual;
2283 int diff;
2284 int old_active = VIsual_active;
2285 int old_mode = VIsual_mode;
2286 #endif
2287 int regname;
2289 #if defined(FEAT_FOLDING)
2290 save_cursor = curwin->w_cursor;
2291 #endif
2294 * When GUI is active, always recognize mouse events, otherwise:
2295 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2296 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2297 * - For command line and insert mode 'mouse' is checked before calling
2298 * do_mouse().
2300 if (do_always)
2301 do_always = FALSE;
2302 else
2303 #ifdef FEAT_GUI
2304 if (!gui.in_use)
2305 #endif
2307 #ifdef FEAT_VISUAL
2308 if (VIsual_active)
2310 if (!mouse_has(MOUSE_VISUAL))
2311 return FALSE;
2313 else
2314 #endif
2315 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2316 return FALSE;
2319 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2321 #ifdef FEAT_MOUSESHAPE
2322 /* May have stopped dragging the status or separator line. The pointer is
2323 * most likely still on the status or separator line. */
2324 if (!is_drag && drag_status_line)
2326 drag_status_line = FALSE;
2327 update_mouseshape(SHAPE_IDX_STATUS);
2329 # ifdef FEAT_VERTSPLIT
2330 if (!is_drag && drag_sep_line)
2332 drag_sep_line = FALSE;
2333 update_mouseshape(SHAPE_IDX_VSEP);
2335 # endif
2336 #endif
2339 * Ignore drag and release events if we didn't get a click.
2341 if (is_click)
2342 got_click = TRUE;
2343 else
2345 if (!got_click) /* didn't get click, ignore */
2346 return FALSE;
2347 if (!is_drag) /* release, reset got_click */
2348 got_click = FALSE;
2351 #ifndef FEAT_VISUAL
2353 * ALT is only used for starging/extending Visual mode.
2355 if ((mod_mask & MOD_MASK_ALT))
2356 return FALSE;
2357 #endif
2360 * CTRL right mouse button does CTRL-T
2362 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2364 if (State & INSERT)
2365 stuffcharReadbuff(Ctrl_O);
2366 if (count > 1)
2367 stuffnumReadbuff(count);
2368 stuffcharReadbuff(Ctrl_T);
2369 got_click = FALSE; /* ignore drag&release now */
2370 return FALSE;
2374 * CTRL only works with left mouse button
2376 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2377 return FALSE;
2380 * When a modifier is down, ignore drag and release events, as well as
2381 * multiple clicks and the middle mouse button.
2382 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2384 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2385 | MOD_MASK_META))
2386 && (!is_click
2387 || (mod_mask & MOD_MASK_MULTI_CLICK)
2388 || which_button == MOUSE_MIDDLE)
2389 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
2390 && mouse_model_popup()
2391 && which_button == MOUSE_LEFT)
2392 && !((mod_mask & MOD_MASK_ALT)
2393 && !mouse_model_popup()
2394 && which_button == MOUSE_RIGHT)
2396 return FALSE;
2399 * If the button press was used as the movement command for an operator
2400 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2401 * drag/release events.
2403 if (!is_click && which_button == MOUSE_MIDDLE)
2404 return FALSE;
2406 if (oap != NULL)
2407 regname = oap->regname;
2408 else
2409 regname = 0;
2412 * Middle mouse button does a 'put' of the selected text
2414 if (which_button == MOUSE_MIDDLE)
2416 if (State == NORMAL)
2419 * If an operator was pending, we don't know what the user wanted
2420 * to do. Go back to normal mode: Clear the operator and beep().
2422 if (oap != NULL && oap->op_type != OP_NOP)
2424 clearopbeep(oap);
2425 return FALSE;
2428 #ifdef FEAT_VISUAL
2430 * If visual was active, yank the highlighted text and put it
2431 * before the mouse pointer position.
2432 * In Select mode replace the highlighted text with the clipboard.
2434 if (VIsual_active)
2436 if (VIsual_select)
2438 stuffcharReadbuff(Ctrl_G);
2439 stuffReadbuff((char_u *)"\"+p");
2441 else
2443 stuffcharReadbuff('y');
2444 stuffcharReadbuff(K_MIDDLEMOUSE);
2446 do_always = TRUE; /* ignore 'mouse' setting next time */
2447 return FALSE;
2449 #endif
2451 * The rest is below jump_to_mouse()
2455 else if ((State & INSERT) == 0)
2456 return FALSE;
2459 * Middle click in insert mode doesn't move the mouse, just insert the
2460 * contents of a register. '.' register is special, can't insert that
2461 * with do_put().
2462 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2463 * happens for the GUI).
2465 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2467 if (regname == '.')
2468 insert_reg(regname, TRUE);
2469 else
2471 #ifdef FEAT_CLIPBOARD
2472 if (clip_star.available && regname == 0)
2473 regname = '*';
2474 #endif
2475 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2476 insert_reg(regname, TRUE);
2477 else
2479 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2481 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2482 AppendCharToRedobuff(Ctrl_R);
2483 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2484 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2487 return FALSE;
2491 /* When dragging or button-up stay in the same window. */
2492 if (!is_click)
2493 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2495 start_visual.lnum = 0;
2497 #ifdef FEAT_WINDOWS
2498 /* Check for clicking in the tab page line. */
2499 if (mouse_row == 0 && firstwin->w_winrow > 0)
2501 if (is_drag)
2502 return FALSE;
2503 got_click = FALSE; /* ignore mouse-up and drag events */
2505 /* click in a tab selects that tab page */
2506 if (is_click
2507 # ifdef FEAT_CMDWIN
2508 && cmdwin_type == 0
2509 # endif
2510 && mouse_col < Columns)
2512 c1 = TabPageIdxs[mouse_col];
2513 if (c1 >= 0)
2515 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2517 /* double click opens new page */
2518 end_visual_mode();
2519 tabpage_new();
2520 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2522 else
2524 /* Go to specified tab page, or next one if not clicking
2525 * on a label. */
2526 goto_tabpage(c1);
2528 /* It's like clicking on the status line of a window. */
2529 if (curwin != old_curwin)
2530 end_visual_mode();
2533 else if (c1 < 0)
2535 tabpage_T *tp;
2537 /* Close the current or specified tab page. */
2538 if (c1 == -999)
2539 tp = curtab;
2540 else
2541 tp = find_tabpage(-c1);
2542 if (tp == curtab)
2544 if (first_tabpage->tp_next != NULL)
2545 tabpage_close(FALSE);
2547 else if (tp != NULL)
2548 tabpage_close_other(tp, FALSE);
2551 return TRUE;
2553 #endif
2556 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2557 * right button up -> pop-up menu
2558 * shift-left button -> right button
2559 * alt-left button -> alt-right button
2561 if (mouse_model_popup())
2563 if (which_button == MOUSE_RIGHT
2564 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2567 * NOTE: Ignore right button down and drag mouse events.
2568 * Windows only shows the popup menu on the button up event.
2570 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2571 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2572 if (!is_click)
2573 return FALSE;
2574 #endif
2575 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2576 if (is_click || is_drag)
2577 return FALSE;
2578 #endif
2579 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2580 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2581 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2582 if (gui.in_use)
2584 jump_flags = 0;
2585 if (STRCMP(p_mousem, "popup_setpos") == 0)
2587 /* First set the cursor position before showing the popup
2588 * menu. */
2589 #ifdef FEAT_VISUAL
2590 if (VIsual_active)
2592 pos_T m_pos;
2595 * set MOUSE_MAY_STOP_VIS if we are outside the
2596 * selection or the current window (might have false
2597 * negative here)
2599 if (mouse_row < W_WINROW(curwin)
2600 || mouse_row
2601 > (W_WINROW(curwin) + curwin->w_height))
2602 jump_flags = MOUSE_MAY_STOP_VIS;
2603 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2604 jump_flags = MOUSE_MAY_STOP_VIS;
2605 else
2607 if ((lt(curwin->w_cursor, VIsual)
2608 && (lt(m_pos, curwin->w_cursor)
2609 || lt(VIsual, m_pos)))
2610 || (lt(VIsual, curwin->w_cursor)
2611 && (lt(m_pos, VIsual)
2612 || lt(curwin->w_cursor, m_pos))))
2614 jump_flags = MOUSE_MAY_STOP_VIS;
2616 else if (VIsual_mode == Ctrl_V)
2618 getvcols(curwin, &curwin->w_cursor, &VIsual,
2619 &leftcol, &rightcol);
2620 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2621 if (m_pos.col < leftcol || m_pos.col > rightcol)
2622 jump_flags = MOUSE_MAY_STOP_VIS;
2626 else
2627 jump_flags = MOUSE_MAY_STOP_VIS;
2628 #endif
2630 if (jump_flags)
2632 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2633 update_curbuf(
2634 #ifdef FEAT_VISUAL
2635 VIsual_active ? INVERTED :
2636 #endif
2637 VALID);
2638 setcursor();
2639 out_flush(); /* Update before showing popup menu */
2641 # ifdef FEAT_MENU
2642 gui_show_popupmenu();
2643 # endif
2644 return (jump_flags & CURSOR_MOVED) != 0;
2646 else
2647 return FALSE;
2648 #else
2649 return FALSE;
2650 #endif
2652 if (which_button == MOUSE_LEFT
2653 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
2655 which_button = MOUSE_RIGHT;
2656 mod_mask &= ~MOD_MASK_SHIFT;
2660 #ifdef FEAT_VISUAL
2661 if ((State & (NORMAL | INSERT))
2662 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2664 if (which_button == MOUSE_LEFT)
2666 if (is_click)
2668 /* stop Visual mode for a left click in a window, but not when
2669 * on a status line */
2670 if (VIsual_active)
2671 jump_flags |= MOUSE_MAY_STOP_VIS;
2673 else if (mouse_has(MOUSE_VISUAL))
2674 jump_flags |= MOUSE_MAY_VIS;
2676 else if (which_button == MOUSE_RIGHT)
2678 if (is_click && VIsual_active)
2681 * Remember the start and end of visual before moving the
2682 * cursor.
2684 if (lt(curwin->w_cursor, VIsual))
2686 start_visual = curwin->w_cursor;
2687 end_visual = VIsual;
2689 else
2691 start_visual = VIsual;
2692 end_visual = curwin->w_cursor;
2695 jump_flags |= MOUSE_FOCUS;
2696 if (mouse_has(MOUSE_VISUAL))
2697 jump_flags |= MOUSE_MAY_VIS;
2700 #endif
2703 * If an operator is pending, ignore all drags and releases until the
2704 * next mouse click.
2706 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2708 got_click = FALSE;
2709 oap->motion_type = MCHAR;
2712 /* When releasing the button let jump_to_mouse() know. */
2713 if (!is_click && !is_drag)
2714 jump_flags |= MOUSE_RELEASED;
2717 * JUMP!
2719 jump_flags = jump_to_mouse(jump_flags,
2720 oap == NULL ? NULL : &(oap->inclusive), which_button);
2721 moved = (jump_flags & CURSOR_MOVED);
2722 in_status_line = (jump_flags & IN_STATUS_LINE);
2723 #ifdef FEAT_VERTSPLIT
2724 in_sep_line = (jump_flags & IN_SEP_LINE);
2725 #endif
2727 #ifdef FEAT_NETBEANS_INTG
2728 if (usingNetbeans && isNetbeansBuffer(curbuf)
2729 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2731 int key = KEY2TERMCAP1(c);
2733 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2734 || key == (int)KE_RIGHTRELEASE)
2735 netbeans_button_release(which_button);
2737 #endif
2739 /* When jumping to another window, clear a pending operator. That's a bit
2740 * friendlier than beeping and not jumping to that window. */
2741 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2742 clearop(oap);
2744 #ifdef FEAT_FOLDING
2745 if (mod_mask == 0
2746 && !is_drag
2747 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2748 && which_button == MOUSE_LEFT)
2750 /* open or close a fold at this line */
2751 if (jump_flags & MOUSE_FOLD_OPEN)
2752 openFold(curwin->w_cursor.lnum, 1L);
2753 else
2754 closeFold(curwin->w_cursor.lnum, 1L);
2755 /* don't move the cursor if still in the same window */
2756 if (curwin == old_curwin)
2757 curwin->w_cursor = save_cursor;
2759 #endif
2761 #if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2762 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2764 clip_modeless(which_button, is_click, is_drag);
2765 return FALSE;
2767 #endif
2769 #ifdef FEAT_VISUAL
2770 /* Set global flag that we are extending the Visual area with mouse
2771 * dragging; temporarily minimize 'scrolloff'. */
2772 if (VIsual_active && is_drag && p_so)
2774 /* In the very first line, allow scrolling one line */
2775 if (mouse_row == 0)
2776 mouse_dragging = 2;
2777 else
2778 mouse_dragging = 1;
2781 /* When dragging the mouse above the window, scroll down. */
2782 if (is_drag && mouse_row < 0 && !in_status_line)
2784 scroll_redraw(FALSE, 1L);
2785 mouse_row = 0;
2788 if (start_visual.lnum) /* right click in visual mode */
2790 /* When ALT is pressed make Visual mode blockwise. */
2791 if (mod_mask & MOD_MASK_ALT)
2792 VIsual_mode = Ctrl_V;
2795 * In Visual-block mode, divide the area in four, pick up the corner
2796 * that is in the quarter that the cursor is in.
2798 if (VIsual_mode == Ctrl_V)
2800 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2801 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2802 end_visual.col = leftcol;
2803 else
2804 end_visual.col = rightcol;
2805 if (curwin->w_cursor.lnum <
2806 (start_visual.lnum + end_visual.lnum) / 2)
2807 end_visual.lnum = end_visual.lnum;
2808 else
2809 end_visual.lnum = start_visual.lnum;
2811 /* move VIsual to the right column */
2812 start_visual = curwin->w_cursor; /* save the cursor pos */
2813 curwin->w_cursor = end_visual;
2814 coladvance(end_visual.col);
2815 VIsual = curwin->w_cursor;
2816 curwin->w_cursor = start_visual; /* restore the cursor */
2818 else
2821 * If the click is before the start of visual, change the start.
2822 * If the click is after the end of visual, change the end. If
2823 * the click is inside the visual, change the closest side.
2825 if (lt(curwin->w_cursor, start_visual))
2826 VIsual = end_visual;
2827 else if (lt(end_visual, curwin->w_cursor))
2828 VIsual = start_visual;
2829 else
2831 /* In the same line, compare column number */
2832 if (end_visual.lnum == start_visual.lnum)
2834 if (curwin->w_cursor.col - start_visual.col >
2835 end_visual.col - curwin->w_cursor.col)
2836 VIsual = start_visual;
2837 else
2838 VIsual = end_visual;
2841 /* In different lines, compare line number */
2842 else
2844 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2845 (end_visual.lnum - curwin->w_cursor.lnum);
2847 if (diff > 0) /* closest to end */
2848 VIsual = start_visual;
2849 else if (diff < 0) /* closest to start */
2850 VIsual = end_visual;
2851 else /* in the middle line */
2853 if (curwin->w_cursor.col <
2854 (start_visual.col + end_visual.col) / 2)
2855 VIsual = end_visual;
2856 else
2857 VIsual = start_visual;
2864 * If Visual mode started in insert mode, execute "CTRL-O"
2866 else if ((State & INSERT) && VIsual_active)
2867 stuffcharReadbuff(Ctrl_O);
2868 #endif
2871 * Middle mouse click: Put text before cursor.
2873 if (which_button == MOUSE_MIDDLE)
2875 #ifdef FEAT_CLIPBOARD
2876 if (clip_star.available && regname == 0)
2877 regname = '*';
2878 #endif
2879 if (yank_register_mline(regname))
2881 if (mouse_past_bottom)
2882 dir = FORWARD;
2884 else if (mouse_past_eol)
2885 dir = FORWARD;
2887 if (fixindent)
2889 c1 = (dir == BACKWARD) ? '[' : ']';
2890 c2 = 'p';
2892 else
2894 c1 = (dir == FORWARD) ? 'p' : 'P';
2895 c2 = NUL;
2897 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2900 * Remember where the paste started, so in edit() Insstart can be set
2901 * to this position
2903 if (restart_edit != 0)
2904 where_paste_started = curwin->w_cursor;
2905 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2908 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2910 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2911 * error under the mouse pointer.
2913 else if (((mod_mask & MOD_MASK_CTRL)
2914 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2915 && bt_quickfix(curbuf))
2917 if (State & INSERT)
2918 stuffcharReadbuff(Ctrl_O);
2919 if (curwin->w_llist_ref == NULL) /* quickfix window */
2920 stuffReadbuff((char_u *)":.cc\n");
2921 else /* location list window */
2922 stuffReadbuff((char_u *)":.ll\n");
2923 got_click = FALSE; /* ignore drag&release now */
2925 #endif
2928 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2929 * under the mouse pointer.
2931 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2932 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2934 if (State & INSERT)
2935 stuffcharReadbuff(Ctrl_O);
2936 stuffcharReadbuff(Ctrl_RSB);
2937 got_click = FALSE; /* ignore drag&release now */
2941 * Shift-Mouse click searches for the next occurrence of the word under
2942 * the mouse pointer
2944 else if ((mod_mask & MOD_MASK_SHIFT))
2946 if (State & INSERT
2947 #ifdef FEAT_VISUAL
2948 || (VIsual_active && VIsual_select)
2949 #endif
2951 stuffcharReadbuff(Ctrl_O);
2952 if (which_button == MOUSE_LEFT)
2953 stuffcharReadbuff('*');
2954 else /* MOUSE_RIGHT */
2955 stuffcharReadbuff('#');
2958 /* Handle double clicks, unless on status line */
2959 else if (in_status_line)
2961 #ifdef FEAT_MOUSESHAPE
2962 if ((is_drag || is_click) && !drag_status_line)
2964 drag_status_line = TRUE;
2965 update_mouseshape(-1);
2967 #endif
2969 #ifdef FEAT_VERTSPLIT
2970 else if (in_sep_line)
2972 # ifdef FEAT_MOUSESHAPE
2973 if ((is_drag || is_click) && !drag_sep_line)
2975 drag_sep_line = TRUE;
2976 update_mouseshape(-1);
2978 # endif
2980 #endif
2981 #ifdef FEAT_VISUAL
2982 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
2983 && mouse_has(MOUSE_VISUAL))
2985 if (is_click || !VIsual_active)
2987 if (VIsual_active)
2988 orig_cursor = VIsual;
2989 else
2991 check_visual_highlight();
2992 VIsual = curwin->w_cursor;
2993 orig_cursor = VIsual;
2994 VIsual_active = TRUE;
2995 VIsual_reselect = TRUE;
2996 /* start Select mode if 'selectmode' contains "mouse" */
2997 may_start_select('o');
2998 setmouse();
3000 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3002 /* Double click with ALT pressed makes it blockwise. */
3003 if (mod_mask & MOD_MASK_ALT)
3004 VIsual_mode = Ctrl_V;
3005 else
3006 VIsual_mode = 'v';
3008 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3009 VIsual_mode = 'V';
3010 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3011 VIsual_mode = Ctrl_V;
3012 #ifdef FEAT_CLIPBOARD
3013 /* Make sure the clipboard gets updated. Needed because start and
3014 * end may still be the same, and the selection needs to be owned */
3015 clip_star.vmode = NUL;
3016 #endif
3019 * A double click selects a word or a block.
3021 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3023 pos_T *pos = NULL;
3024 int gc;
3026 if (is_click)
3028 /* If the character under the cursor (skipping white space) is
3029 * not a word character, try finding a match and select a (),
3030 * {}, [], #if/#endif, etc. block. */
3031 end_visual = curwin->w_cursor;
3032 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
3033 inc(&end_visual);
3034 if (oap != NULL)
3035 oap->motion_type = MCHAR;
3036 if (oap != NULL
3037 && VIsual_mode == 'v'
3038 && !vim_iswordc(gchar_pos(&end_visual))
3039 && equalpos(curwin->w_cursor, VIsual)
3040 && (pos = findmatch(oap, NUL)) != NULL)
3042 curwin->w_cursor = *pos;
3043 if (oap->motion_type == MLINE)
3044 VIsual_mode = 'V';
3045 else if (*p_sel == 'e')
3047 if (lt(curwin->w_cursor, VIsual))
3048 ++VIsual.col;
3049 else
3050 ++curwin->w_cursor.col;
3055 if (pos == NULL && (is_click || is_drag))
3057 /* When not found a match or when dragging: extend to include
3058 * a word. */
3059 if (lt(curwin->w_cursor, orig_cursor))
3061 find_start_of_word(&curwin->w_cursor);
3062 find_end_of_word(&VIsual);
3064 else
3066 find_start_of_word(&VIsual);
3067 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3068 #ifdef FEAT_MBYTE
3069 curwin->w_cursor.col +=
3070 (*mb_ptr2len)(ml_get_cursor());
3071 #else
3072 ++curwin->w_cursor.col;
3073 #endif
3074 find_end_of_word(&curwin->w_cursor);
3077 curwin->w_set_curswant = TRUE;
3079 if (is_click)
3080 redraw_curbuf_later(INVERTED); /* update the inversion */
3082 else if (VIsual_active && !old_active)
3084 if (mod_mask & MOD_MASK_ALT)
3085 VIsual_mode = Ctrl_V;
3086 else
3087 VIsual_mode = 'v';
3090 /* If Visual mode changed show it later. */
3091 if ((!VIsual_active && old_active && mode_displayed)
3092 || (VIsual_active && p_smd && msg_silent == 0
3093 && (!old_active || VIsual_mode != old_mode)))
3094 redraw_cmdline = TRUE;
3095 #endif
3097 return moved;
3100 #ifdef FEAT_VISUAL
3102 * Move "pos" back to the start of the word it's in.
3104 static void
3105 find_start_of_word(pos)
3106 pos_T *pos;
3108 char_u *line;
3109 int cclass;
3110 int col;
3112 line = ml_get(pos->lnum);
3113 cclass = get_mouse_class(line + pos->col);
3115 while (pos->col > 0)
3117 col = pos->col - 1;
3118 #ifdef FEAT_MBYTE
3119 col -= (*mb_head_off)(line, line + col);
3120 #endif
3121 if (get_mouse_class(line + col) != cclass)
3122 break;
3123 pos->col = col;
3128 * Move "pos" forward to the end of the word it's in.
3129 * When 'selection' is "exclusive", the position is just after the word.
3131 static void
3132 find_end_of_word(pos)
3133 pos_T *pos;
3135 char_u *line;
3136 int cclass;
3137 int col;
3139 line = ml_get(pos->lnum);
3140 if (*p_sel == 'e' && pos->col > 0)
3142 --pos->col;
3143 #ifdef FEAT_MBYTE
3144 pos->col -= (*mb_head_off)(line, line + pos->col);
3145 #endif
3147 cclass = get_mouse_class(line + pos->col);
3148 while (line[pos->col] != NUL)
3150 #ifdef FEAT_MBYTE
3151 col = pos->col + (*mb_ptr2len)(line + pos->col);
3152 #else
3153 col = pos->col + 1;
3154 #endif
3155 if (get_mouse_class(line + col) != cclass)
3157 if (*p_sel == 'e')
3158 pos->col = col;
3159 break;
3161 pos->col = col;
3166 * Get class of a character for selection: same class means same word.
3167 * 0: blank
3168 * 1: punctuation groups
3169 * 2: normal word character
3170 * >2: multi-byte word character.
3172 static int
3173 get_mouse_class(p)
3174 char_u *p;
3176 int c;
3178 #ifdef FEAT_MBYTE
3179 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3180 return mb_get_class(p);
3181 #endif
3183 c = *p;
3184 if (c == ' ' || c == '\t')
3185 return 0;
3187 if (vim_iswordc(c))
3188 return 2;
3191 * There are a few special cases where we want certain combinations of
3192 * characters to be considered as a single word. These are things like
3193 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
3194 * character is in it's own class.
3196 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3197 return 1;
3198 return c;
3200 #endif /* FEAT_VISUAL */
3201 #endif /* FEAT_MOUSE */
3203 #if defined(FEAT_VISUAL) || defined(PROTO)
3205 * Check if highlighting for visual mode is possible, give a warning message
3206 * if not.
3208 void
3209 check_visual_highlight()
3211 static int did_check = FALSE;
3213 if (full_screen)
3215 if (!did_check && hl_attr(HLF_V) == 0)
3216 MSG(_("Warning: terminal cannot highlight"));
3217 did_check = TRUE;
3222 * End Visual mode.
3223 * This function should ALWAYS be called to end Visual mode, except from
3224 * do_pending_operator().
3226 void
3227 end_visual_mode()
3229 #ifdef FEAT_CLIPBOARD
3231 * If we are using the clipboard, then remember what was selected in case
3232 * we need to paste it somewhere while we still own the selection.
3233 * Only do this when the clipboard is already owned. Don't want to grab
3234 * the selection when hitting ESC.
3236 if (clip_star.available && clip_star.owned)
3237 clip_auto_select();
3238 #endif
3240 VIsual_active = FALSE;
3241 #ifdef FEAT_MOUSE
3242 setmouse();
3243 mouse_dragging = 0;
3244 #endif
3246 /* Save the current VIsual area for '< and '> marks, and "gv" */
3247 curbuf->b_visual.vi_mode = VIsual_mode;
3248 curbuf->b_visual.vi_start = VIsual;
3249 curbuf->b_visual.vi_end = curwin->w_cursor;
3250 curbuf->b_visual.vi_curswant = curwin->w_curswant;
3251 #ifdef FEAT_EVAL
3252 curbuf->b_visual_mode_eval = VIsual_mode;
3253 #endif
3254 #ifdef FEAT_VIRTUALEDIT
3255 if (!virtual_active())
3256 curwin->w_cursor.coladd = 0;
3257 #endif
3259 if (mode_displayed)
3260 clear_cmdline = TRUE; /* unshow visual mode later */
3261 #ifdef FEAT_CMDL_INFO
3262 else
3263 clear_showcmd();
3264 #endif
3266 adjust_cursor_eol();
3270 * Reset VIsual_active and VIsual_reselect.
3272 void
3273 reset_VIsual_and_resel()
3275 if (VIsual_active)
3277 end_visual_mode();
3278 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3280 VIsual_reselect = FALSE;
3284 * Reset VIsual_active and VIsual_reselect if it's set.
3286 void
3287 reset_VIsual()
3289 if (VIsual_active)
3291 end_visual_mode();
3292 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3293 VIsual_reselect = FALSE;
3296 #endif /* FEAT_VISUAL */
3298 #if defined(FEAT_BEVAL)
3299 static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3302 * Check for a balloon-eval special item to include when searching for an
3303 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3304 * Returns TRUE if the character at "*ptr" should be included.
3305 * "dir" is FORWARD or BACKWARD, the direction of searching.
3306 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3307 * "bnp" points to a counter for square brackets.
3309 static int
3310 find_is_eval_item(ptr, colp, bnp, dir)
3311 char_u *ptr;
3312 int *colp;
3313 int *bnp;
3314 int dir;
3316 /* Accept everything inside []. */
3317 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3318 ++*bnp;
3319 if (*bnp > 0)
3321 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3322 --*bnp;
3323 return TRUE;
3326 /* skip over "s.var" */
3327 if (*ptr == '.')
3328 return TRUE;
3330 /* two-character item: s->var */
3331 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3332 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3334 *colp += dir;
3335 return TRUE;
3337 return FALSE;
3339 #endif
3342 * Find the identifier under or to the right of the cursor.
3343 * "find_type" can have one of three values:
3344 * FIND_IDENT: find an identifier (keyword)
3345 * FIND_STRING: find any non-white string
3346 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
3347 * FIND_EVAL: find text useful for C program debugging
3349 * There are three steps:
3350 * 1. Search forward for the start of an identifier/string. Doesn't move if
3351 * already on one.
3352 * 2. Search backward for the start of this identifier/string.
3353 * This doesn't match the real Vi but I like it a little better and it
3354 * shouldn't bother anyone.
3355 * 3. Search forward to the end of this identifier/string.
3356 * When FIND_IDENT isn't defined, we backup until a blank.
3358 * Returns the length of the string, or zero if no string is found.
3359 * If a string is found, a pointer to the string is put in "*string". This
3360 * string is not always NUL terminated.
3363 find_ident_under_cursor(string, find_type)
3364 char_u **string;
3365 int find_type;
3367 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3368 curwin->w_cursor.col, string, find_type);
3372 * Like find_ident_under_cursor(), but for any window and any position.
3373 * However: Uses 'iskeyword' from the current window!.
3376 find_ident_at_pos(wp, lnum, startcol, string, find_type)
3377 win_T *wp;
3378 linenr_T lnum;
3379 colnr_T startcol;
3380 char_u **string;
3381 int find_type;
3383 char_u *ptr;
3384 int col = 0; /* init to shut up GCC */
3385 int i;
3386 #ifdef FEAT_MBYTE
3387 int this_class = 0;
3388 int prev_class;
3389 int prevcol;
3390 #endif
3391 #if defined(FEAT_BEVAL)
3392 int bn = 0; /* bracket nesting */
3393 #endif
3396 * if i == 0: try to find an identifier
3397 * if i == 1: try to find any non-white string
3399 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3400 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3403 * 1. skip to start of identifier/string
3405 col = startcol;
3406 #ifdef FEAT_MBYTE
3407 if (has_mbyte)
3409 while (ptr[col] != NUL)
3411 # if defined(FEAT_BEVAL)
3412 /* Stop at a ']' to evaluate "a[x]". */
3413 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3414 break;
3415 # endif
3416 this_class = mb_get_class(ptr + col);
3417 if (this_class != 0 && (i == 1 || this_class != 1))
3418 break;
3419 col += (*mb_ptr2len)(ptr + col);
3422 else
3423 #endif
3424 while (ptr[col] != NUL
3425 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
3426 # if defined(FEAT_BEVAL)
3427 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3428 # endif
3430 ++col;
3432 #if defined(FEAT_BEVAL)
3433 /* When starting on a ']' count it, so that we include the '['. */
3434 bn = ptr[col] == ']';
3435 #endif
3438 * 2. Back up to start of identifier/string.
3440 #ifdef FEAT_MBYTE
3441 if (has_mbyte)
3443 /* Remember class of character under cursor. */
3444 # if defined(FEAT_BEVAL)
3445 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3446 this_class = mb_get_class((char_u *)"a");
3447 else
3448 # endif
3449 this_class = mb_get_class(ptr + col);
3450 while (col > 0 && this_class != 0)
3452 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3453 prev_class = mb_get_class(ptr + prevcol);
3454 if (this_class != prev_class
3455 && (i == 0
3456 || prev_class == 0
3457 || (find_type & FIND_IDENT))
3458 # if defined(FEAT_BEVAL)
3459 && (!(find_type & FIND_EVAL)
3460 || prevcol == 0
3461 || !find_is_eval_item(ptr + prevcol, &prevcol,
3462 &bn, BACKWARD))
3463 # endif
3465 break;
3466 col = prevcol;
3469 /* If we don't want just any old string, or we've found an
3470 * identifier, stop searching. */
3471 if (this_class > 2)
3472 this_class = 2;
3473 if (!(find_type & FIND_STRING) || this_class == 2)
3474 break;
3476 else
3477 #endif
3479 while (col > 0
3480 && ((i == 0
3481 ? vim_iswordc(ptr[col - 1])
3482 : (!vim_iswhite(ptr[col - 1])
3483 && (!(find_type & FIND_IDENT)
3484 || !vim_iswordc(ptr[col - 1]))))
3485 #if defined(FEAT_BEVAL)
3486 || ((find_type & FIND_EVAL)
3487 && col > 1
3488 && find_is_eval_item(ptr + col - 1, &col,
3489 &bn, BACKWARD))
3490 #endif
3492 --col;
3494 /* If we don't want just any old string, or we've found an
3495 * identifier, stop searching. */
3496 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3497 break;
3501 if (ptr[col] == NUL || (i == 0 && (
3502 #ifdef FEAT_MBYTE
3503 has_mbyte ? this_class != 2 :
3504 #endif
3505 !vim_iswordc(ptr[col]))))
3508 * didn't find an identifier or string
3510 if (find_type & FIND_STRING)
3511 EMSG(_("E348: No string under cursor"));
3512 else
3513 EMSG(_("E349: No identifier under cursor"));
3514 return 0;
3516 ptr += col;
3517 *string = ptr;
3520 * 3. Find the end if the identifier/string.
3522 #if defined(FEAT_BEVAL)
3523 bn = 0;
3524 startcol -= col;
3525 #endif
3526 col = 0;
3527 #ifdef FEAT_MBYTE
3528 if (has_mbyte)
3530 /* Search for point of changing multibyte character class. */
3531 this_class = mb_get_class(ptr);
3532 while (ptr[col] != NUL
3533 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3534 : mb_get_class(ptr + col) != 0)
3535 # if defined(FEAT_BEVAL)
3536 || ((find_type & FIND_EVAL)
3537 && col <= (int)startcol
3538 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3539 # endif
3541 col += (*mb_ptr2len)(ptr + col);
3543 else
3544 #endif
3545 while ((i == 0 ? vim_iswordc(ptr[col])
3546 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
3547 # if defined(FEAT_BEVAL)
3548 || ((find_type & FIND_EVAL)
3549 && col <= (int)startcol
3550 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3551 # endif
3554 ++col;
3557 return col;
3561 * Prepare for redo of a normal command.
3563 static void
3564 prep_redo_cmd(cap)
3565 cmdarg_T *cap;
3567 prep_redo(cap->oap->regname, cap->count0,
3568 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3572 * Prepare for redo of any command.
3573 * Note that only the last argument can be a multi-byte char.
3575 static void
3576 prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3577 int regname;
3578 long num;
3579 int cmd1;
3580 int cmd2;
3581 int cmd3;
3582 int cmd4;
3583 int cmd5;
3585 ResetRedobuff();
3586 if (regname != 0) /* yank from specified buffer */
3588 AppendCharToRedobuff('"');
3589 AppendCharToRedobuff(regname);
3591 if (num)
3592 AppendNumberToRedobuff(num);
3594 if (cmd1 != NUL)
3595 AppendCharToRedobuff(cmd1);
3596 if (cmd2 != NUL)
3597 AppendCharToRedobuff(cmd2);
3598 if (cmd3 != NUL)
3599 AppendCharToRedobuff(cmd3);
3600 if (cmd4 != NUL)
3601 AppendCharToRedobuff(cmd4);
3602 if (cmd5 != NUL)
3603 AppendCharToRedobuff(cmd5);
3607 * check for operator active and clear it
3609 * return TRUE if operator was active
3611 static int
3612 checkclearop(oap)
3613 oparg_T *oap;
3615 if (oap->op_type == OP_NOP)
3616 return FALSE;
3617 clearopbeep(oap);
3618 return TRUE;
3622 * Check for operator or Visual active. Clear active operator.
3624 * Return TRUE if operator or Visual was active.
3626 static int
3627 checkclearopq(oap)
3628 oparg_T *oap;
3630 if (oap->op_type == OP_NOP
3631 #ifdef FEAT_VISUAL
3632 && !VIsual_active
3633 #endif
3635 return FALSE;
3636 clearopbeep(oap);
3637 return TRUE;
3640 static void
3641 clearop(oap)
3642 oparg_T *oap;
3644 oap->op_type = OP_NOP;
3645 oap->regname = 0;
3646 oap->motion_force = NUL;
3647 oap->use_reg_one = FALSE;
3650 static void
3651 clearopbeep(oap)
3652 oparg_T *oap;
3654 clearop(oap);
3655 beep_flush();
3658 #ifdef FEAT_VISUAL
3660 * Remove the shift modifier from a special key.
3662 static void
3663 unshift_special(cap)
3664 cmdarg_T *cap;
3666 switch (cap->cmdchar)
3668 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3669 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3670 case K_S_UP: cap->cmdchar = K_UP; break;
3671 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3672 case K_S_HOME: cap->cmdchar = K_HOME; break;
3673 case K_S_END: cap->cmdchar = K_END; break;
3675 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3677 #endif
3679 #if defined(FEAT_CMDL_INFO) || defined(PROTO)
3681 * Routines for displaying a partly typed command
3684 #ifdef FEAT_VISUAL /* need room for size of Visual area */
3685 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3686 #else
3687 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3688 #endif
3689 static char_u showcmd_buf[SHOWCMD_BUFLEN];
3690 static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3691 static int showcmd_is_clear = TRUE;
3692 static int showcmd_visual = FALSE;
3694 static void display_showcmd __ARGS((void));
3696 void
3697 clear_showcmd()
3699 if (!p_sc)
3700 return;
3702 #ifdef FEAT_VISUAL
3703 if (VIsual_active && !char_avail())
3705 int i = lt(VIsual, curwin->w_cursor);
3706 long lines;
3707 colnr_T leftcol, rightcol;
3708 linenr_T top, bot;
3710 /* Show the size of the Visual area. */
3711 if (i)
3713 top = VIsual.lnum;
3714 bot = curwin->w_cursor.lnum;
3716 else
3718 top = curwin->w_cursor.lnum;
3719 bot = VIsual.lnum;
3721 # ifdef FEAT_FOLDING
3722 /* Include closed folds as a whole. */
3723 hasFolding(top, &top, NULL);
3724 hasFolding(bot, NULL, &bot);
3725 # endif
3726 lines = bot - top + 1;
3728 if (VIsual_mode == Ctrl_V)
3730 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
3731 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3732 (long)(rightcol - leftcol + 1));
3734 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3735 sprintf((char *)showcmd_buf, "%ld", lines);
3736 else
3737 sprintf((char *)showcmd_buf, "%ld", (long)(i
3738 ? curwin->w_cursor.col - VIsual.col
3739 : VIsual.col - curwin->w_cursor.col) + (*p_sel != 'e'));
3740 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3741 showcmd_visual = TRUE;
3743 else
3744 #endif
3746 showcmd_buf[0] = NUL;
3747 showcmd_visual = FALSE;
3749 /* Don't actually display something if there is nothing to clear. */
3750 if (showcmd_is_clear)
3751 return;
3754 display_showcmd();
3758 * Add 'c' to string of shown command chars.
3759 * Return TRUE if output has been written (and setcursor() has been called).
3762 add_to_showcmd(c)
3763 int c;
3765 char_u *p;
3766 int old_len;
3767 int extra_len;
3768 int overflow;
3769 #if defined(FEAT_MOUSE)
3770 int i;
3771 static int ignore[] =
3773 # ifdef FEAT_GUI
3774 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3775 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
3776 # endif
3777 K_IGNORE,
3778 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3779 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3780 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
3781 K_MOUSEDOWN, K_MOUSEUP,
3782 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
3783 K_CURSORHOLD,
3786 #endif
3788 if (!p_sc || msg_silent != 0)
3789 return FALSE;
3791 if (showcmd_visual)
3793 showcmd_buf[0] = NUL;
3794 showcmd_visual = FALSE;
3797 #if defined(FEAT_MOUSE)
3798 /* Ignore keys that are scrollbar updates and mouse clicks */
3799 if (IS_SPECIAL(c))
3800 for (i = 0; ignore[i] != 0; ++i)
3801 if (ignore[i] == c)
3802 return FALSE;
3803 #endif
3805 p = transchar(c);
3806 old_len = (int)STRLEN(showcmd_buf);
3807 extra_len = (int)STRLEN(p);
3808 overflow = old_len + extra_len - SHOWCMD_COLS;
3809 if (overflow > 0)
3810 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3811 old_len - overflow + 1);
3812 STRCAT(showcmd_buf, p);
3814 if (char_avail())
3815 return FALSE;
3817 display_showcmd();
3819 return TRUE;
3822 void
3823 add_to_showcmd_c(c)
3824 int c;
3826 if (!add_to_showcmd(c))
3827 setcursor();
3831 * Delete 'len' characters from the end of the shown command.
3833 static void
3834 del_from_showcmd(len)
3835 int len;
3837 int old_len;
3839 if (!p_sc)
3840 return;
3842 old_len = (int)STRLEN(showcmd_buf);
3843 if (len > old_len)
3844 len = old_len;
3845 showcmd_buf[old_len - len] = NUL;
3847 if (!char_avail())
3848 display_showcmd();
3852 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3853 * something and there is a partial mapping.
3855 void
3856 push_showcmd()
3858 if (p_sc)
3859 STRCPY(old_showcmd_buf, showcmd_buf);
3862 void
3863 pop_showcmd()
3865 if (!p_sc)
3866 return;
3868 STRCPY(showcmd_buf, old_showcmd_buf);
3870 display_showcmd();
3873 static void
3874 display_showcmd()
3876 int len;
3878 cursor_off();
3880 len = (int)STRLEN(showcmd_buf);
3881 if (len == 0)
3882 showcmd_is_clear = TRUE;
3883 else
3885 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3886 showcmd_is_clear = FALSE;
3890 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3891 * spaces
3893 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3895 setcursor(); /* put cursor back where it belongs */
3897 #endif
3899 #ifdef FEAT_SCROLLBIND
3901 * When "check" is FALSE, prepare for commands that scroll the window.
3902 * When "check" is TRUE, take care of scroll-binding after the window has
3903 * scrolled. Called from normal_cmd() and edit().
3905 void
3906 do_check_scrollbind(check)
3907 int check;
3909 static win_T *old_curwin = NULL;
3910 static linenr_T old_topline = 0;
3911 #ifdef FEAT_DIFF
3912 static int old_topfill = 0;
3913 #endif
3914 static buf_T *old_buf = NULL;
3915 static colnr_T old_leftcol = 0;
3917 if (check && curwin->w_p_scb)
3919 /* If a ":syncbind" command was just used, don't scroll, only reset
3920 * the values. */
3921 if (did_syncbind)
3922 did_syncbind = FALSE;
3923 else if (curwin == old_curwin)
3926 * Synchronize other windows, as necessary according to
3927 * 'scrollbind'. Don't do this after an ":edit" command, except
3928 * when 'diff' is set.
3930 if ((curwin->w_buffer == old_buf
3931 #ifdef FEAT_DIFF
3932 || curwin->w_p_diff
3933 #endif
3935 && (curwin->w_topline != old_topline
3936 #ifdef FEAT_DIFF
3937 || curwin->w_topfill != old_topfill
3938 #endif
3939 || curwin->w_leftcol != old_leftcol))
3941 check_scrollbind(curwin->w_topline - old_topline,
3942 (long)(curwin->w_leftcol - old_leftcol));
3945 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
3948 * When switching between windows, make sure that the relative
3949 * vertical offset is valid for the new window. The relative
3950 * offset is invalid whenever another 'scrollbind' window has
3951 * scrolled to a point that would force the current window to
3952 * scroll past the beginning or end of its buffer. When the
3953 * resync is performed, some of the other 'scrollbind' windows may
3954 * need to jump so that the current window's relative position is
3955 * visible on-screen.
3957 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
3959 curwin->w_scbind_pos = curwin->w_topline;
3962 old_curwin = curwin;
3963 old_topline = curwin->w_topline;
3964 #ifdef FEAT_DIFF
3965 old_topfill = curwin->w_topfill;
3966 #endif
3967 old_buf = curwin->w_buffer;
3968 old_leftcol = curwin->w_leftcol;
3972 * Synchronize any windows that have "scrollbind" set, based on the
3973 * number of rows by which the current window has changed
3974 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3976 void
3977 check_scrollbind(topline_diff, leftcol_diff)
3978 linenr_T topline_diff;
3979 long leftcol_diff;
3981 int want_ver;
3982 int want_hor;
3983 win_T *old_curwin = curwin;
3984 buf_T *old_curbuf = curbuf;
3985 #ifdef FEAT_VISUAL
3986 int old_VIsual_select = VIsual_select;
3987 int old_VIsual_active = VIsual_active;
3988 #endif
3989 colnr_T tgt_leftcol = curwin->w_leftcol;
3990 long topline;
3991 long y;
3994 * check 'scrollopt' string for vertical and horizontal scroll options
3996 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
3997 #ifdef FEAT_DIFF
3998 want_ver |= old_curwin->w_p_diff;
3999 #endif
4000 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4003 * loop through the scrollbound windows and scroll accordingly
4005 #ifdef FEAT_VISUAL
4006 VIsual_select = VIsual_active = 0;
4007 #endif
4008 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4010 curbuf = curwin->w_buffer;
4011 /* skip original window and windows with 'noscrollbind' */
4012 if (curwin != old_curwin && curwin->w_p_scb)
4015 * do the vertical scroll
4017 if (want_ver)
4019 #ifdef FEAT_DIFF
4020 if (old_curwin->w_p_diff && curwin->w_p_diff)
4022 diff_set_topline(old_curwin, curwin);
4024 else
4025 #endif
4027 curwin->w_scbind_pos += topline_diff;
4028 topline = curwin->w_scbind_pos;
4029 if (topline > curbuf->b_ml.ml_line_count)
4030 topline = curbuf->b_ml.ml_line_count;
4031 if (topline < 1)
4032 topline = 1;
4034 y = topline - curwin->w_topline;
4035 if (y > 0)
4036 scrollup(y, FALSE);
4037 else
4038 scrolldown(-y, FALSE);
4041 redraw_later(VALID);
4042 cursor_correct();
4043 #ifdef FEAT_WINDOWS
4044 curwin->w_redr_status = TRUE;
4045 #endif
4049 * do the horizontal scroll
4051 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4053 curwin->w_leftcol = tgt_leftcol;
4054 leftcol_changed();
4060 * reset current-window
4062 #ifdef FEAT_VISUAL
4063 VIsual_select = old_VIsual_select;
4064 VIsual_active = old_VIsual_active;
4065 #endif
4066 curwin = old_curwin;
4067 curbuf = old_curbuf;
4069 #endif /* #ifdef FEAT_SCROLLBIND */
4072 * Command character that's ignored.
4073 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
4074 * xon/xoff
4076 static void
4077 nv_ignore(cap)
4078 cmdarg_T *cap;
4080 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
4084 * Command character that doesn't do anything, but unlike nv_ignore() does
4085 * start edit(). Used for "startinsert" executed while starting up.
4087 /*ARGSUSED */
4088 static void
4089 nv_nop(cap)
4090 cmdarg_T *cap;
4095 * Command character doesn't exist.
4097 static void
4098 nv_error(cap)
4099 cmdarg_T *cap;
4101 clearopbeep(cap->oap);
4105 * <Help> and <F1> commands.
4107 static void
4108 nv_help(cap)
4109 cmdarg_T *cap;
4111 if (!checkclearopq(cap->oap))
4112 ex_help(NULL);
4116 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4118 static void
4119 nv_addsub(cap)
4120 cmdarg_T *cap;
4122 if (!checkclearopq(cap->oap)
4123 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4124 prep_redo_cmd(cap);
4128 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4130 static void
4131 nv_page(cap)
4132 cmdarg_T *cap;
4134 if (!checkclearop(cap->oap))
4136 #ifdef FEAT_WINDOWS
4137 if (mod_mask & MOD_MASK_CTRL)
4139 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4140 if (cap->arg == BACKWARD)
4141 goto_tabpage(-(int)cap->count1);
4142 else
4143 goto_tabpage((int)cap->count0);
4145 else
4146 #endif
4147 (void)onepage(cap->arg, cap->count1);
4152 * Implementation of "gd" and "gD" command.
4154 static void
4155 nv_gd(oap, nchar, thisblock)
4156 oparg_T *oap;
4157 int nchar;
4158 int thisblock; /* 1 for "1gd" and "1gD" */
4160 int len;
4161 char_u *ptr;
4163 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
4164 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
4165 clearopbeep(oap);
4166 #ifdef FEAT_FOLDING
4167 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4168 foldOpenCursor();
4169 #endif
4173 * Search for variable declaration of "ptr[len]".
4174 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4175 * current file ("gD").
4176 * When "thisblock" is TRUE check the {} block scope.
4177 * Return FAIL when not found.
4180 find_decl(ptr, len, locally, thisblock, searchflags)
4181 char_u *ptr;
4182 int len;
4183 int locally;
4184 int thisblock;
4185 int searchflags; /* flags passed to searchit() */
4187 char_u *pat;
4188 pos_T old_pos;
4189 pos_T par_pos;
4190 pos_T found_pos;
4191 int t;
4192 int save_p_ws;
4193 int save_p_scs;
4194 int retval = OK;
4195 int incll;
4197 if ((pat = alloc(len + 7)) == NULL)
4198 return FAIL;
4200 /* Put "\V" before the pattern to avoid that the special meaning of "."
4201 * and "~" causes trouble. */
4202 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4203 len, ptr);
4204 old_pos = curwin->w_cursor;
4205 save_p_ws = p_ws;
4206 save_p_scs = p_scs;
4207 p_ws = FALSE; /* don't wrap around end of file now */
4208 p_scs = FALSE; /* don't switch ignorecase off now */
4211 * With "gD" go to line 1.
4212 * With "gd" Search back for the start of the current function, then go
4213 * back until a blank line. If this fails go to line 1.
4215 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
4217 setpcmark(); /* Set in findpar() otherwise */
4218 curwin->w_cursor.lnum = 1;
4219 par_pos = curwin->w_cursor;
4221 else
4223 par_pos = curwin->w_cursor;
4224 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4225 --curwin->w_cursor.lnum;
4227 curwin->w_cursor.col = 0;
4229 /* Search forward for the identifier, ignore comment lines. */
4230 clearpos(&found_pos);
4231 for (;;)
4233 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
4234 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
4235 if (curwin->w_cursor.lnum >= old_pos.lnum)
4236 t = FAIL; /* match after start is failure too */
4238 if (thisblock && t != FAIL)
4240 pos_T *pos;
4242 /* Check that the block the match is in doesn't end before the
4243 * position where we started the search from. */
4244 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4245 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4246 && pos->lnum < old_pos.lnum)
4247 continue;
4250 if (t == FAIL)
4252 /* If we previously found a valid position, use it. */
4253 if (found_pos.lnum != 0)
4255 curwin->w_cursor = found_pos;
4256 t = OK;
4258 break;
4260 #ifdef FEAT_COMMENTS
4261 if (get_leader_len(ml_get_curline(), NULL, FALSE) > 0)
4263 /* Ignore this line, continue at start of next line. */
4264 ++curwin->w_cursor.lnum;
4265 curwin->w_cursor.col = 0;
4266 continue;
4268 #endif
4269 if (!locally) /* global search: use first match found */
4270 break;
4271 if (curwin->w_cursor.lnum >= par_pos.lnum)
4273 /* If we previously found a valid position, use it. */
4274 if (found_pos.lnum != 0)
4275 curwin->w_cursor = found_pos;
4276 break;
4279 /* For finding a local variable and the match is before the "{" search
4280 * to find a later match. For K&R style function declarations this
4281 * skips the function header without types. */
4282 found_pos = curwin->w_cursor;
4285 if (t == FAIL)
4287 retval = FAIL;
4288 curwin->w_cursor = old_pos;
4290 else
4292 curwin->w_set_curswant = TRUE;
4293 /* "n" searches forward now */
4294 reset_search_dir();
4297 vim_free(pat);
4298 p_ws = save_p_ws;
4299 p_scs = save_p_scs;
4301 return retval;
4305 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4306 * lines rather than lines in the file.
4307 * 'dist' must be positive.
4309 * Return OK if able to move cursor, FAIL otherwise.
4311 static int
4312 nv_screengo(oap, dir, dist)
4313 oparg_T *oap;
4314 int dir;
4315 long dist;
4317 int linelen = linetabsize(ml_get_curline());
4318 int retval = OK;
4319 int atend = FALSE;
4320 int n;
4321 int col_off1; /* margin offset for first screen line */
4322 int col_off2; /* margin offset for wrapped screen line */
4323 int width1; /* text width for first screen line */
4324 int width2; /* test width for wrapped screen line */
4326 oap->motion_type = MCHAR;
4327 oap->inclusive = FALSE;
4329 col_off1 = curwin_col_off();
4330 col_off2 = col_off1 - curwin_col_off2();
4331 width1 = W_WIDTH(curwin) - col_off1;
4332 width2 = W_WIDTH(curwin) - col_off2;
4334 #ifdef FEAT_VERTSPLIT
4335 if (curwin->w_width != 0)
4337 #endif
4339 * Instead of sticking at the last character of the buffer line we
4340 * try to stick in the last column of the screen.
4342 if (curwin->w_curswant == MAXCOL)
4344 atend = TRUE;
4345 validate_virtcol();
4346 if (width1 <= 0)
4347 curwin->w_curswant = 0;
4348 else
4350 curwin->w_curswant = width1 - 1;
4351 if (curwin->w_virtcol > curwin->w_curswant)
4352 curwin->w_curswant += ((curwin->w_virtcol
4353 - curwin->w_curswant - 1) / width2 + 1) * width2;
4356 else
4358 if (linelen > width1)
4359 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4360 else
4361 n = width1;
4362 if (curwin->w_curswant > (colnr_T)n + 1)
4363 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4364 * width2;
4367 while (dist--)
4369 if (dir == BACKWARD)
4371 if ((long)curwin->w_curswant >= width2)
4372 /* move back within line */
4373 curwin->w_curswant -= width2;
4374 else
4376 /* to previous line */
4377 if (curwin->w_cursor.lnum == 1)
4379 retval = FAIL;
4380 break;
4382 --curwin->w_cursor.lnum;
4383 #ifdef FEAT_FOLDING
4384 /* Move to the start of a closed fold. Don't do that when
4385 * 'foldopen' contains "all": it will open in a moment. */
4386 if (!(fdo_flags & FDO_ALL))
4387 (void)hasFolding(curwin->w_cursor.lnum,
4388 &curwin->w_cursor.lnum, NULL);
4389 #endif
4390 linelen = linetabsize(ml_get_curline());
4391 if (linelen > width1)
4392 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4393 + 1) * width2;
4396 else /* dir == FORWARD */
4398 if (linelen > width1)
4399 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4400 else
4401 n = width1;
4402 if (curwin->w_curswant + width2 < (colnr_T)n)
4403 /* move forward within line */
4404 curwin->w_curswant += width2;
4405 else
4407 /* to next line */
4408 #ifdef FEAT_FOLDING
4409 /* Move to the end of a closed fold. */
4410 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4411 &curwin->w_cursor.lnum);
4412 #endif
4413 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4415 retval = FAIL;
4416 break;
4418 curwin->w_cursor.lnum++;
4419 curwin->w_curswant %= width2;
4423 #ifdef FEAT_VERTSPLIT
4425 #endif
4427 coladvance(curwin->w_curswant);
4429 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4430 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4433 * Check for landing on a character that got split at the end of the
4434 * last line. We want to advance a screenline, not end up in the same
4435 * screenline or move two screenlines.
4437 validate_virtcol();
4438 if (curwin->w_virtcol > curwin->w_curswant
4439 && (curwin->w_curswant < (colnr_T)width1
4440 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4441 : ((curwin->w_curswant - width1) % width2
4442 > (colnr_T)width2 / 2)))
4443 --curwin->w_cursor.col;
4445 #endif
4447 if (atend)
4448 curwin->w_curswant = MAXCOL; /* stick in the last column */
4450 return retval;
4453 #ifdef FEAT_MOUSE
4455 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4456 * when Shift or Ctrl is used.
4457 * K_MOUSEUP (cap->arg == TRUE) or K_MOUSEDOWN (cap->arg == FALSE)
4459 static void
4460 nv_mousescroll(cap)
4461 cmdarg_T *cap;
4463 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4464 win_T *old_curwin = curwin;
4466 /* Currently we only get the mouse coordinates in the GUI. */
4467 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4469 int row, col;
4471 row = mouse_row;
4472 col = mouse_col;
4474 /* find the window at the pointer coordinates */
4475 curwin = mouse_find_win(&row, &col);
4476 curbuf = curwin->w_buffer;
4478 # endif
4480 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4482 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4484 else
4486 cap->count1 = 3;
4487 cap->count0 = 3;
4488 nv_scroll_line(cap);
4491 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4492 curwin->w_redr_status = TRUE;
4494 curwin = old_curwin;
4495 curbuf = curwin->w_buffer;
4496 # endif
4500 * Mouse clicks and drags.
4502 static void
4503 nv_mouse(cap)
4504 cmdarg_T *cap;
4506 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4508 #endif
4511 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4512 * cap->arg must be TRUE for CTRL-E.
4514 static void
4515 nv_scroll_line(cap)
4516 cmdarg_T *cap;
4518 if (!checkclearop(cap->oap))
4519 scroll_redraw(cap->arg, cap->count1);
4523 * Scroll "count" lines up or down, and redraw.
4525 void
4526 scroll_redraw(up, count)
4527 int up;
4528 long count;
4530 linenr_T prev_topline = curwin->w_topline;
4531 #ifdef FEAT_DIFF
4532 int prev_topfill = curwin->w_topfill;
4533 #endif
4534 linenr_T prev_lnum = curwin->w_cursor.lnum;
4536 if (up)
4537 scrollup(count, TRUE);
4538 else
4539 scrolldown(count, TRUE);
4540 if (p_so)
4542 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4543 * valid, otherwise the screen jumps back at the end of the file. */
4544 cursor_correct();
4545 check_cursor_moved(curwin);
4546 curwin->w_valid |= VALID_TOPLINE;
4548 /* If moved back to where we were, at least move the cursor, otherwise
4549 * we get stuck at one position. Don't move the cursor up if the
4550 * first line of the buffer is already on the screen */
4551 while (curwin->w_topline == prev_topline
4552 #ifdef FEAT_DIFF
4553 && curwin->w_topfill == prev_topfill
4554 #endif
4557 if (up)
4559 if (curwin->w_cursor.lnum > prev_lnum
4560 || cursor_down(1L, FALSE) == FAIL)
4561 break;
4563 else
4565 if (curwin->w_cursor.lnum < prev_lnum
4566 || prev_topline == 1L
4567 || cursor_up(1L, FALSE) == FAIL)
4568 break;
4570 /* Mark w_topline as valid, otherwise the screen jumps back at the
4571 * end of the file. */
4572 check_cursor_moved(curwin);
4573 curwin->w_valid |= VALID_TOPLINE;
4576 if (curwin->w_cursor.lnum != prev_lnum)
4577 coladvance(curwin->w_curswant);
4578 redraw_later(VALID);
4582 * Commands that start with "z".
4584 static void
4585 nv_zet(cap)
4586 cmdarg_T *cap;
4588 long n;
4589 colnr_T col;
4590 int nchar = cap->nchar;
4591 #ifdef FEAT_FOLDING
4592 long old_fdl = curwin->w_p_fdl;
4593 int old_fen = curwin->w_p_fen;
4594 #endif
4595 #ifdef FEAT_SPELL
4596 int undo = FALSE;
4597 #endif
4599 if (VIM_ISDIGIT(nchar))
4602 * "z123{nchar}": edit the count before obtaining {nchar}
4604 if (checkclearop(cap->oap))
4605 return;
4606 n = nchar - '0';
4607 for (;;)
4609 #ifdef USE_ON_FLY_SCROLL
4610 dont_scroll = TRUE; /* disallow scrolling here */
4611 #endif
4612 ++no_mapping;
4613 ++allow_keys; /* no mapping for nchar, but allow key codes */
4614 nchar = plain_vgetc();
4615 #ifdef FEAT_LANGMAP
4616 LANGMAP_ADJUST(nchar, TRUE);
4617 #endif
4618 --no_mapping;
4619 --allow_keys;
4620 #ifdef FEAT_CMDL_INFO
4621 (void)add_to_showcmd(nchar);
4622 #endif
4623 if (nchar == K_DEL || nchar == K_KDEL)
4624 n /= 10;
4625 else if (VIM_ISDIGIT(nchar))
4626 n = n * 10 + (nchar - '0');
4627 else if (nchar == CAR)
4629 #ifdef FEAT_GUI
4630 need_mouse_correct = TRUE;
4631 #endif
4632 win_setheight((int)n);
4633 break;
4635 else if (nchar == 'l'
4636 || nchar == 'h'
4637 || nchar == K_LEFT
4638 || nchar == K_RIGHT)
4640 cap->count1 = n ? n * cap->count1 : cap->count1;
4641 goto dozet;
4643 else
4645 clearopbeep(cap->oap);
4646 break;
4649 cap->oap->op_type = OP_NOP;
4650 return;
4653 dozet:
4654 if (
4655 #ifdef FEAT_FOLDING
4656 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4657 * and "zC" only in Visual mode. "zj" and "zk" are motion
4658 * commands. */
4659 cap->nchar != 'f' && cap->nchar != 'F'
4660 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4661 && cap->nchar != 'j' && cap->nchar != 'k'
4663 #endif
4664 checkclearop(cap->oap))
4665 return;
4668 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4669 * If line number given, set cursor.
4671 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4672 && cap->count0
4673 && cap->count0 != curwin->w_cursor.lnum)
4675 setpcmark();
4676 if (cap->count0 > curbuf->b_ml.ml_line_count)
4677 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4678 else
4679 curwin->w_cursor.lnum = cap->count0;
4680 check_cursor_col();
4683 switch (nchar)
4685 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4686 case '+':
4687 if (cap->count0 == 0)
4689 /* No count given: put cursor at the line below screen */
4690 validate_botline(); /* make sure w_botline is valid */
4691 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4692 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4693 else
4694 curwin->w_cursor.lnum = curwin->w_botline;
4696 /* FALLTHROUGH */
4697 case NL:
4698 case CAR:
4699 case K_KENTER:
4700 beginline(BL_WHITE | BL_FIX);
4701 /* FALLTHROUGH */
4703 case 't': scroll_cursor_top(0, TRUE);
4704 redraw_later(VALID);
4705 break;
4707 /* "z." and "zz": put cursor in middle of screen */
4708 case '.': beginline(BL_WHITE | BL_FIX);
4709 /* FALLTHROUGH */
4711 case 'z': scroll_cursor_halfway(TRUE);
4712 redraw_later(VALID);
4713 break;
4715 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4716 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4717 * when <count> is at bottom of window, and puts that one at
4718 * bottom of window. */
4719 if (cap->count0 != 0)
4721 scroll_cursor_bot(0, TRUE);
4722 curwin->w_cursor.lnum = curwin->w_topline;
4724 else if (curwin->w_topline == 1)
4725 curwin->w_cursor.lnum = 1;
4726 else
4727 curwin->w_cursor.lnum = curwin->w_topline - 1;
4728 /* FALLTHROUGH */
4729 case '-':
4730 beginline(BL_WHITE | BL_FIX);
4731 /* FALLTHROUGH */
4733 case 'b': scroll_cursor_bot(0, TRUE);
4734 redraw_later(VALID);
4735 break;
4737 /* "zH" - scroll screen right half-page */
4738 case 'H':
4739 cap->count1 *= W_WIDTH(curwin) / 2;
4740 /* FALLTHROUGH */
4742 /* "zh" - scroll screen to the right */
4743 case 'h':
4744 case K_LEFT:
4745 if (!curwin->w_p_wrap)
4747 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4748 curwin->w_leftcol = 0;
4749 else
4750 curwin->w_leftcol -= (colnr_T)cap->count1;
4751 leftcol_changed();
4753 break;
4755 /* "zL" - scroll screen left half-page */
4756 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4757 /* FALLTHROUGH */
4759 /* "zl" - scroll screen to the left */
4760 case 'l':
4761 case K_RIGHT:
4762 if (!curwin->w_p_wrap)
4764 /* scroll the window left */
4765 curwin->w_leftcol += (colnr_T)cap->count1;
4766 leftcol_changed();
4768 break;
4770 /* "zs" - scroll screen, cursor at the start */
4771 case 's': if (!curwin->w_p_wrap)
4773 #ifdef FEAT_FOLDING
4774 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4775 col = 0; /* like the cursor is in col 0 */
4776 else
4777 #endif
4778 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4779 if ((long)col > p_siso)
4780 col -= p_siso;
4781 else
4782 col = 0;
4783 if (curwin->w_leftcol != col)
4785 curwin->w_leftcol = col;
4786 redraw_later(NOT_VALID);
4789 break;
4791 /* "ze" - scroll screen, cursor at the end */
4792 case 'e': if (!curwin->w_p_wrap)
4794 #ifdef FEAT_FOLDING
4795 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4796 col = 0; /* like the cursor is in col 0 */
4797 else
4798 #endif
4799 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4800 n = W_WIDTH(curwin) - curwin_col_off();
4801 if ((long)col + p_siso < n)
4802 col = 0;
4803 else
4804 col = col + p_siso - n + 1;
4805 if (curwin->w_leftcol != col)
4807 curwin->w_leftcol = col;
4808 redraw_later(NOT_VALID);
4811 break;
4813 #ifdef FEAT_FOLDING
4814 /* "zF": create fold command */
4815 /* "zf": create fold operator */
4816 case 'F':
4817 case 'f': if (foldManualAllowed(TRUE))
4819 cap->nchar = 'f';
4820 nv_operator(cap);
4821 curwin->w_p_fen = TRUE;
4823 /* "zF" is like "zfzf" */
4824 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4826 nv_operator(cap);
4827 finish_op = TRUE;
4830 else
4831 clearopbeep(cap->oap);
4832 break;
4834 /* "zd": delete fold at cursor */
4835 /* "zD": delete fold at cursor recursively */
4836 case 'd':
4837 case 'D': if (foldManualAllowed(FALSE))
4839 if (VIsual_active)
4840 nv_operator(cap);
4841 else
4842 deleteFold(curwin->w_cursor.lnum,
4843 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4845 break;
4847 /* "zE": erease all folds */
4848 case 'E': if (foldmethodIsManual(curwin))
4850 clearFolding(curwin);
4851 changed_window_setting();
4853 else if (foldmethodIsMarker(curwin))
4854 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4855 TRUE, FALSE);
4856 else
4857 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4858 break;
4860 /* "zn": fold none: reset 'foldenable' */
4861 case 'n': curwin->w_p_fen = FALSE;
4862 break;
4864 /* "zN": fold Normal: set 'foldenable' */
4865 case 'N': curwin->w_p_fen = TRUE;
4866 break;
4868 /* "zi": invert folding: toggle 'foldenable' */
4869 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4870 break;
4872 /* "za": open closed fold or close open fold at cursor */
4873 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4874 openFold(curwin->w_cursor.lnum, cap->count1);
4875 else
4877 closeFold(curwin->w_cursor.lnum, cap->count1);
4878 curwin->w_p_fen = TRUE;
4880 break;
4882 /* "zA": open fold at cursor recursively */
4883 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4884 openFoldRecurse(curwin->w_cursor.lnum);
4885 else
4887 closeFoldRecurse(curwin->w_cursor.lnum);
4888 curwin->w_p_fen = TRUE;
4890 break;
4892 /* "zo": open fold at cursor or Visual area */
4893 case 'o': if (VIsual_active)
4894 nv_operator(cap);
4895 else
4896 openFold(curwin->w_cursor.lnum, cap->count1);
4897 break;
4899 /* "zO": open fold recursively */
4900 case 'O': if (VIsual_active)
4901 nv_operator(cap);
4902 else
4903 openFoldRecurse(curwin->w_cursor.lnum);
4904 break;
4906 /* "zc": close fold at cursor or Visual area */
4907 case 'c': if (VIsual_active)
4908 nv_operator(cap);
4909 else
4910 closeFold(curwin->w_cursor.lnum, cap->count1);
4911 curwin->w_p_fen = TRUE;
4912 break;
4914 /* "zC": close fold recursively */
4915 case 'C': if (VIsual_active)
4916 nv_operator(cap);
4917 else
4918 closeFoldRecurse(curwin->w_cursor.lnum);
4919 curwin->w_p_fen = TRUE;
4920 break;
4922 /* "zv": open folds at the cursor */
4923 case 'v': foldOpenCursor();
4924 break;
4926 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
4927 case 'x': curwin->w_p_fen = TRUE;
4928 newFoldLevel(); /* update right now */
4929 foldOpenCursor();
4930 break;
4932 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
4933 case 'X': curwin->w_p_fen = TRUE;
4934 old_fdl = -1; /* force an update */
4935 break;
4937 /* "zm": fold more */
4938 case 'm': if (curwin->w_p_fdl > 0)
4939 --curwin->w_p_fdl;
4940 old_fdl = -1; /* force an update */
4941 curwin->w_p_fen = TRUE;
4942 break;
4944 /* "zM": close all folds */
4945 case 'M': curwin->w_p_fdl = 0;
4946 old_fdl = -1; /* force an update */
4947 curwin->w_p_fen = TRUE;
4948 break;
4950 /* "zr": reduce folding */
4951 case 'r': ++curwin->w_p_fdl;
4952 break;
4954 /* "zR": open all folds */
4955 case 'R': curwin->w_p_fdl = getDeepestNesting();
4956 old_fdl = -1; /* force an update */
4957 break;
4959 case 'j': /* "zj" move to next fold downwards */
4960 case 'k': /* "zk" move to next fold upwards */
4961 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
4962 cap->count1) == FAIL)
4963 clearopbeep(cap->oap);
4964 break;
4966 #endif /* FEAT_FOLDING */
4968 #ifdef FEAT_SPELL
4969 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
4970 ++no_mapping;
4971 ++allow_keys; /* no mapping for nchar, but allow key codes */
4972 nchar = plain_vgetc();
4973 #ifdef FEAT_LANGMAP
4974 LANGMAP_ADJUST(nchar, TRUE);
4975 #endif
4976 --no_mapping;
4977 --allow_keys;
4978 #ifdef FEAT_CMDL_INFO
4979 (void)add_to_showcmd(nchar);
4980 #endif
4981 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
4983 clearopbeep(cap->oap);
4984 break;
4986 undo = TRUE;
4987 /*FALLTHROUGH*/
4989 case 'g': /* "zg": add good word to word list */
4990 case 'w': /* "zw": add wrong word to word list */
4991 case 'G': /* "zG": add good word to temp word list */
4992 case 'W': /* "zW": add wrong word to temp word list */
4994 char_u *ptr = NULL;
4995 int len;
4997 if (checkclearop(cap->oap))
4998 break;
4999 # ifdef FEAT_VISUAL
5000 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5001 == FAIL)
5002 return;
5003 # endif
5004 if (ptr == NULL)
5006 pos_T pos = curwin->w_cursor;
5008 /* Find bad word under the cursor. */
5009 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
5010 if (len != 0 && curwin->w_cursor.col <= pos.col)
5011 ptr = ml_get_pos(&curwin->w_cursor);
5012 curwin->w_cursor = pos;
5015 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5016 FIND_IDENT)) == 0)
5017 return;
5018 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
5019 (nchar == 'G' || nchar == 'W')
5020 ? 0 : (int)cap->count1,
5021 undo);
5023 break;
5025 case '=': /* "z=": suggestions for a badly spelled word */
5026 if (!checkclearop(cap->oap))
5027 spell_suggest((int)cap->count0);
5028 break;
5029 #endif
5031 default: clearopbeep(cap->oap);
5034 #ifdef FEAT_FOLDING
5035 /* Redraw when 'foldenable' changed */
5036 if (old_fen != curwin->w_p_fen)
5038 # ifdef FEAT_DIFF
5039 win_T *wp;
5041 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5043 /* Adjust 'foldenable' in diff-synced windows. */
5044 FOR_ALL_WINDOWS(wp)
5046 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5048 wp->w_p_fen = curwin->w_p_fen;
5049 changed_window_setting_win(wp);
5053 # endif
5054 changed_window_setting();
5057 /* Redraw when 'foldlevel' changed. */
5058 if (old_fdl != curwin->w_p_fdl)
5059 newFoldLevel();
5060 #endif
5063 #ifdef FEAT_GUI
5065 * Vertical scrollbar movement.
5067 static void
5068 nv_ver_scrollbar(cap)
5069 cmdarg_T *cap;
5071 if (cap->oap->op_type != OP_NOP)
5072 clearopbeep(cap->oap);
5074 /* Even if an operator was pending, we still want to scroll */
5075 gui_do_scroll();
5079 * Horizontal scrollbar movement.
5081 static void
5082 nv_hor_scrollbar(cap)
5083 cmdarg_T *cap;
5085 if (cap->oap->op_type != OP_NOP)
5086 clearopbeep(cap->oap);
5088 /* Even if an operator was pending, we still want to scroll */
5089 gui_do_horiz_scroll();
5091 #endif
5093 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
5095 * Click in GUI tab.
5097 static void
5098 nv_tabline(cap)
5099 cmdarg_T *cap;
5101 if (cap->oap->op_type != OP_NOP)
5102 clearopbeep(cap->oap);
5104 /* Even if an operator was pending, we still want to jump tabs. */
5105 goto_tabpage(current_tab);
5109 * Selected item in tab line menu.
5111 static void
5112 nv_tabmenu(cap)
5113 cmdarg_T *cap;
5115 if (cap->oap->op_type != OP_NOP)
5116 clearopbeep(cap->oap);
5118 /* Even if an operator was pending, we still want to jump tabs. */
5119 handle_tabmenu();
5123 * Handle selecting an item of the GUI tab line menu.
5124 * Used in Normal and Insert mode.
5126 void
5127 handle_tabmenu()
5129 switch (current_tabmenu)
5131 case TABLINE_MENU_CLOSE:
5132 if (current_tab == 0)
5133 do_cmdline_cmd((char_u *)"tabclose");
5134 else
5136 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5137 current_tab);
5138 do_cmdline_cmd(IObuff);
5140 break;
5142 case TABLINE_MENU_NEW:
5143 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5144 current_tab > 0 ? current_tab - 1 : 999);
5145 do_cmdline_cmd(IObuff);
5146 break;
5148 case TABLINE_MENU_OPEN:
5149 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5150 current_tab > 0 ? current_tab - 1 : 999);
5151 do_cmdline_cmd(IObuff);
5152 break;
5155 #endif
5158 * "Q" command.
5160 static void
5161 nv_exmode(cap)
5162 cmdarg_T *cap;
5165 * Ignore 'Q' in Visual mode, just give a beep.
5167 #ifdef FEAT_VISUAL
5168 if (VIsual_active)
5169 vim_beep();
5170 else
5171 #endif
5172 if (!checkclearop(cap->oap))
5173 do_exmode(FALSE);
5177 * Handle a ":" command.
5179 static void
5180 nv_colon(cap)
5181 cmdarg_T *cap;
5183 int old_p_im;
5185 #ifdef FEAT_VISUAL
5186 if (VIsual_active)
5187 nv_operator(cap);
5188 else
5189 #endif
5191 if (cap->oap->op_type != OP_NOP)
5193 /* Using ":" as a movement is characterwise exclusive. */
5194 cap->oap->motion_type = MCHAR;
5195 cap->oap->inclusive = FALSE;
5197 else if (cap->count0)
5199 /* translate "count:" into ":.,.+(count - 1)" */
5200 stuffcharReadbuff('.');
5201 if (cap->count0 > 1)
5203 stuffReadbuff((char_u *)",.+");
5204 stuffnumReadbuff((long)cap->count0 - 1L);
5208 /* When typing, don't type below an old message */
5209 if (KeyTyped)
5210 compute_cmdrow();
5212 old_p_im = p_im;
5214 /* get a command line and execute it */
5215 do_cmdline(NULL, getexline, NULL,
5216 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5218 /* If 'insertmode' changed, enter or exit Insert mode */
5219 if (p_im != old_p_im)
5221 if (p_im)
5222 restart_edit = 'i';
5223 else
5224 restart_edit = 0;
5227 /* The start of the operator may have become invalid by the Ex
5228 * command. */
5229 if (cap->oap->op_type != OP_NOP
5230 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5231 || cap->oap->start.col >
5232 STRLEN(ml_get(cap->oap->start.lnum))))
5233 clearopbeep(cap->oap);
5238 * Handle CTRL-G command.
5240 static void
5241 nv_ctrlg(cap)
5242 cmdarg_T *cap;
5244 #ifdef FEAT_VISUAL
5245 if (VIsual_active) /* toggle Selection/Visual mode */
5247 VIsual_select = !VIsual_select;
5248 showmode();
5250 else
5251 #endif
5252 if (!checkclearop(cap->oap))
5253 /* print full name if count given or :cd used */
5254 fileinfo((int)cap->count0, FALSE, TRUE);
5258 * Handle CTRL-H <Backspace> command.
5260 static void
5261 nv_ctrlh(cap)
5262 cmdarg_T *cap;
5264 #ifdef FEAT_VISUAL
5265 if (VIsual_active && VIsual_select)
5267 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5268 v_visop(cap);
5270 else
5271 #endif
5272 nv_left(cap);
5276 * CTRL-L: clear screen and redraw.
5278 static void
5279 nv_clear(cap)
5280 cmdarg_T *cap;
5282 if (!checkclearop(cap->oap))
5284 #if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5286 * Right now, the BeBox doesn't seem to have an easy way to detect
5287 * window resizing, so we cheat and make the user detect it
5288 * manually with CTRL-L instead
5290 ui_get_shellsize();
5291 #endif
5292 #ifdef FEAT_SYN_HL
5293 /* Clear all syntax states to force resyncing. */
5294 syn_stack_free_all(curbuf);
5295 #endif
5296 redraw_later(CLEAR);
5301 * CTRL-O: In Select mode: switch to Visual mode for one command.
5302 * Otherwise: Go to older pcmark.
5304 static void
5305 nv_ctrlo(cap)
5306 cmdarg_T *cap;
5308 #ifdef FEAT_VISUAL
5309 if (VIsual_active && VIsual_select)
5311 VIsual_select = FALSE;
5312 showmode();
5313 restart_VIsual_select = 2; /* restart Select mode later */
5315 else
5316 #endif
5318 cap->count1 = -cap->count1;
5319 nv_pcmark(cap);
5324 * CTRL-^ command, short for ":e #"
5326 static void
5327 nv_hat(cap)
5328 cmdarg_T *cap;
5330 if (!checkclearopq(cap->oap))
5331 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5332 GETF_SETMARK|GETF_ALT, FALSE);
5336 * "Z" commands.
5338 static void
5339 nv_Zet(cap)
5340 cmdarg_T *cap;
5342 if (!checkclearopq(cap->oap))
5344 switch (cap->nchar)
5346 /* "ZZ": equivalent to ":x". */
5347 case 'Z': do_cmdline_cmd((char_u *)"x");
5348 break;
5350 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5351 case 'Q': do_cmdline_cmd((char_u *)"q!");
5352 break;
5354 default: clearopbeep(cap->oap);
5359 #if defined(FEAT_WINDOWS) || defined(PROTO)
5361 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5363 void
5364 do_nv_ident(c1, c2)
5365 int c1;
5366 int c2;
5368 oparg_T oa;
5369 cmdarg_T ca;
5371 clear_oparg(&oa);
5372 vim_memset(&ca, 0, sizeof(ca));
5373 ca.oap = &oa;
5374 ca.cmdchar = c1;
5375 ca.nchar = c2;
5376 nv_ident(&ca);
5378 #endif
5381 * Handle the commands that use the word under the cursor.
5382 * [g] CTRL-] :ta to current identifier
5383 * [g] 'K' run program for current identifier
5384 * [g] '*' / to current identifier or string
5385 * [g] '#' ? to current identifier or string
5386 * g ']' :tselect for current identifier
5388 static void
5389 nv_ident(cap)
5390 cmdarg_T *cap;
5392 char_u *ptr = NULL;
5393 char_u *buf;
5394 char_u *p;
5395 char_u *kp; /* value of 'keywordprg' */
5396 int kp_help; /* 'keywordprg' is ":help" */
5397 int n = 0; /* init for GCC */
5398 int cmdchar;
5399 int g_cmd; /* "g" command */
5400 char_u *aux_ptr;
5401 int isman;
5402 int isman_s;
5404 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5406 cmdchar = cap->nchar;
5407 g_cmd = TRUE;
5409 else
5411 cmdchar = cap->cmdchar;
5412 g_cmd = FALSE;
5415 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5416 cmdchar = '#';
5419 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5421 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5423 #ifdef FEAT_VISUAL
5424 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5425 return;
5426 #endif
5427 if (checkclearopq(cap->oap))
5428 return;
5431 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5432 (cmdchar == '*' || cmdchar == '#')
5433 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5435 clearop(cap->oap);
5436 return;
5439 /* Allocate buffer to put the command in. Inserting backslashes can
5440 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5441 * and some numbers. */
5442 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5443 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5444 || STRCMP(kp, ":help") == 0);
5445 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5446 if (buf == NULL)
5447 return;
5448 buf[0] = NUL;
5450 switch (cmdchar)
5452 case '*':
5453 case '#':
5455 * Put cursor at start of word, makes search skip the word
5456 * under the cursor.
5457 * Call setpcmark() first, so "*``" puts the cursor back where
5458 * it was.
5460 setpcmark();
5461 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5463 if (!g_cmd && vim_iswordp(ptr))
5464 STRCPY(buf, "\\<");
5465 no_smartcase = TRUE; /* don't use 'smartcase' now */
5466 break;
5468 case 'K':
5469 if (kp_help)
5470 STRCPY(buf, "he! ");
5471 else
5473 /* An external command will probably use an argument starting
5474 * with "-" as an option. To avoid trouble we skip the "-". */
5475 while (*ptr == '-')
5476 ++ptr;
5478 /* When a count is given, turn it into a range. Is this
5479 * really what we want? */
5480 isman = (STRCMP(kp, "man") == 0);
5481 isman_s = (STRCMP(kp, "man -s") == 0);
5482 if (cap->count0 != 0 && !(isman || isman_s))
5483 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5485 STRCAT(buf, "! ");
5486 if (cap->count0 == 0 && isman_s)
5487 STRCAT(buf, "man");
5488 else
5489 STRCAT(buf, kp);
5490 STRCAT(buf, " ");
5491 if (cap->count0 != 0 && (isman || isman_s))
5493 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5494 STRCAT(buf, " ");
5497 break;
5499 case ']':
5500 #ifdef FEAT_CSCOPE
5501 if (p_cst)
5502 STRCPY(buf, "cstag ");
5503 else
5504 #endif
5505 STRCPY(buf, "ts ");
5506 break;
5508 default:
5509 if (curbuf->b_help)
5510 STRCPY(buf, "he! ");
5511 else if (g_cmd)
5512 STRCPY(buf, "tj ");
5513 else
5514 sprintf((char *)buf, "%ldta ", cap->count0);
5518 * Now grab the chars in the identifier
5520 if (cmdchar == 'K' && !kp_help)
5522 /* Escape the argument properly for a shell command */
5523 p = vim_strsave_shellescape(ptr, TRUE);
5524 if (p == NULL)
5526 vim_free(buf);
5527 return;
5529 buf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5530 if (buf == NULL)
5532 vim_free(buf);
5533 vim_free(p);
5534 return;
5536 STRCAT(buf, p);
5537 vim_free(p);
5539 else
5541 if (cmdchar == '*')
5542 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5543 else if (cmdchar == '#')
5544 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
5545 else
5546 /* Don't escape spaces and Tabs in a tag with a backslash */
5547 aux_ptr = (char_u *)"\\|\"\n*?[";
5549 p = buf + STRLEN(buf);
5550 while (n-- > 0)
5552 /* put a backslash before \ and some others */
5553 if (vim_strchr(aux_ptr, *ptr) != NULL)
5554 *p++ = '\\';
5555 #ifdef FEAT_MBYTE
5556 /* When current byte is a part of multibyte character, copy all
5557 * bytes of that character. */
5558 if (has_mbyte)
5560 int i;
5561 int len = (*mb_ptr2len)(ptr) - 1;
5563 for (i = 0; i < len && n >= 1; ++i, --n)
5564 *p++ = *ptr++;
5566 #endif
5567 *p++ = *ptr++;
5569 *p = NUL;
5573 * Execute the command.
5575 if (cmdchar == '*' || cmdchar == '#')
5577 if (!g_cmd && (
5578 #ifdef FEAT_MBYTE
5579 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5580 #endif
5581 vim_iswordc(ptr[-1])))
5582 STRCAT(buf, "\\>");
5583 #ifdef FEAT_CMDHIST
5584 /* put pattern in search history */
5585 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5586 #endif
5587 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5589 else
5590 do_cmdline_cmd(buf);
5592 vim_free(buf);
5595 #if defined(FEAT_VISUAL) || defined(PROTO)
5597 * Get visually selected text, within one line only.
5598 * Returns FAIL if more than one line selected.
5601 get_visual_text(cap, pp, lenp)
5602 cmdarg_T *cap;
5603 char_u **pp; /* return: start of selected text */
5604 int *lenp; /* return: length of selected text */
5606 if (VIsual_mode != 'V')
5607 unadjust_for_sel();
5608 if (VIsual.lnum != curwin->w_cursor.lnum)
5610 if (cap != NULL)
5611 clearopbeep(cap->oap);
5612 return FAIL;
5614 if (VIsual_mode == 'V')
5616 *pp = ml_get_curline();
5617 *lenp = (int)STRLEN(*pp);
5619 else
5621 if (lt(curwin->w_cursor, VIsual))
5623 *pp = ml_get_pos(&curwin->w_cursor);
5624 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5626 else
5628 *pp = ml_get_pos(&VIsual);
5629 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5631 #ifdef FEAT_MBYTE
5632 if (has_mbyte)
5633 /* Correct the length to include the whole last character. */
5634 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
5635 #endif
5637 reset_VIsual_and_resel();
5638 return OK;
5640 #endif
5643 * CTRL-T: backwards in tag stack
5645 static void
5646 nv_tagpop(cap)
5647 cmdarg_T *cap;
5649 if (!checkclearopq(cap->oap))
5650 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5654 * Handle scrolling command 'H', 'L' and 'M'.
5656 static void
5657 nv_scroll(cap)
5658 cmdarg_T *cap;
5660 int used = 0;
5661 long n;
5662 #ifdef FEAT_FOLDING
5663 linenr_T lnum;
5664 #endif
5665 int half;
5667 cap->oap->motion_type = MLINE;
5668 setpcmark();
5670 if (cap->cmdchar == 'L')
5672 validate_botline(); /* make sure curwin->w_botline is valid */
5673 curwin->w_cursor.lnum = curwin->w_botline - 1;
5674 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5675 curwin->w_cursor.lnum = 1;
5676 else
5678 #ifdef FEAT_FOLDING
5679 if (hasAnyFolding(curwin))
5681 /* Count a fold for one screen line. */
5682 for (n = cap->count1 - 1; n > 0
5683 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5685 (void)hasFolding(curwin->w_cursor.lnum,
5686 &curwin->w_cursor.lnum, NULL);
5687 --curwin->w_cursor.lnum;
5690 else
5691 #endif
5692 curwin->w_cursor.lnum -= cap->count1 - 1;
5695 else
5697 if (cap->cmdchar == 'M')
5699 #ifdef FEAT_DIFF
5700 /* Don't count filler lines above the window. */
5701 used -= diff_check_fill(curwin, curwin->w_topline)
5702 - curwin->w_topfill;
5703 #endif
5704 validate_botline(); /* make sure w_empty_rows is valid */
5705 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5706 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5708 #ifdef FEAT_DIFF
5709 /* Count half he number of filler lines to be "below this
5710 * line" and half to be "above the next line". */
5711 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5712 + n) / 2 >= half)
5714 --n;
5715 break;
5717 #endif
5718 used += plines(curwin->w_topline + n);
5719 if (used >= half)
5720 break;
5721 #ifdef FEAT_FOLDING
5722 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5723 n = lnum - curwin->w_topline;
5724 #endif
5726 if (n > 0 && used > curwin->w_height)
5727 --n;
5729 else /* (cap->cmdchar == 'H') */
5731 n = cap->count1 - 1;
5732 #ifdef FEAT_FOLDING
5733 if (hasAnyFolding(curwin))
5735 /* Count a fold for one screen line. */
5736 lnum = curwin->w_topline;
5737 while (n-- > 0 && lnum < curwin->w_botline - 1)
5739 hasFolding(lnum, NULL, &lnum);
5740 ++lnum;
5742 n = lnum - curwin->w_topline;
5744 #endif
5746 curwin->w_cursor.lnum = curwin->w_topline + n;
5747 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5748 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5751 cursor_correct(); /* correct for 'so' */
5752 beginline(BL_SOL | BL_FIX);
5756 * Cursor right commands.
5758 static void
5759 nv_right(cap)
5760 cmdarg_T *cap;
5762 long n;
5763 #ifdef FEAT_VISUAL
5764 int PAST_LINE;
5765 #else
5766 # define PAST_LINE 0
5767 #endif
5769 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5771 /* <C-Right> and <S-Right> move a word or WORD right */
5772 if (mod_mask & MOD_MASK_CTRL)
5773 cap->arg = TRUE;
5774 nv_wordcmd(cap);
5775 return;
5778 cap->oap->motion_type = MCHAR;
5779 cap->oap->inclusive = FALSE;
5780 #ifdef FEAT_VISUAL
5781 PAST_LINE = (VIsual_active && *p_sel != 'o');
5783 # ifdef FEAT_VIRTUALEDIT
5785 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
5786 * (theoretically) infinitely long.
5788 if (virtual_active())
5789 PAST_LINE = 0;
5790 # endif
5791 #endif
5793 for (n = cap->count1; n > 0; --n)
5795 if ((!PAST_LINE && oneright() == FAIL)
5796 || (PAST_LINE && *ml_get_cursor() == NUL))
5799 * <Space> wraps to next line if 'whichwrap' has 's'.
5800 * 'l' wraps to next line if 'whichwrap' has 'l'.
5801 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
5803 if ( ((cap->cmdchar == ' '
5804 && vim_strchr(p_ww, 's') != NULL)
5805 || (cap->cmdchar == 'l'
5806 && vim_strchr(p_ww, 'l') != NULL)
5807 || (cap->cmdchar == K_RIGHT
5808 && vim_strchr(p_ww, '>') != NULL))
5809 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5811 /* When deleting we also count the NL as a character.
5812 * Set cap->oap->inclusive when last char in the line is
5813 * included, move to next line after that */
5814 if ( cap->oap->op_type != OP_NOP
5815 && !cap->oap->inclusive
5816 && !lineempty(curwin->w_cursor.lnum))
5817 cap->oap->inclusive = TRUE;
5818 else
5820 ++curwin->w_cursor.lnum;
5821 curwin->w_cursor.col = 0;
5822 #ifdef FEAT_VIRTUALEDIT
5823 curwin->w_cursor.coladd = 0;
5824 #endif
5825 curwin->w_set_curswant = TRUE;
5826 cap->oap->inclusive = FALSE;
5828 continue;
5830 if (cap->oap->op_type == OP_NOP)
5832 /* Only beep and flush if not moved at all */
5833 if (n == cap->count1)
5834 beep_flush();
5836 else
5838 if (!lineempty(curwin->w_cursor.lnum))
5839 cap->oap->inclusive = TRUE;
5841 break;
5843 #ifdef FEAT_VISUAL
5844 else if (PAST_LINE)
5846 curwin->w_set_curswant = TRUE;
5847 # ifdef FEAT_VIRTUALEDIT
5848 if (virtual_active())
5849 oneright();
5850 else
5851 # endif
5853 # ifdef FEAT_MBYTE
5854 if (has_mbyte)
5855 curwin->w_cursor.col +=
5856 (*mb_ptr2len)(ml_get_cursor());
5857 else
5858 # endif
5859 ++curwin->w_cursor.col;
5862 #endif
5864 #ifdef FEAT_FOLDING
5865 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5866 && cap->oap->op_type == OP_NOP)
5867 foldOpenCursor();
5868 #endif
5872 * Cursor left commands.
5874 * Returns TRUE when operator end should not be adjusted.
5876 static void
5877 nv_left(cap)
5878 cmdarg_T *cap;
5880 long n;
5882 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5884 /* <C-Left> and <S-Left> move a word or WORD left */
5885 if (mod_mask & MOD_MASK_CTRL)
5886 cap->arg = 1;
5887 nv_bck_word(cap);
5888 return;
5891 cap->oap->motion_type = MCHAR;
5892 cap->oap->inclusive = FALSE;
5893 for (n = cap->count1; n > 0; --n)
5895 if (oneleft() == FAIL)
5897 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
5898 * 'h' wraps to previous line if 'whichwrap' has 'h'.
5899 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
5901 if ( (((cap->cmdchar == K_BS
5902 || cap->cmdchar == Ctrl_H)
5903 && vim_strchr(p_ww, 'b') != NULL)
5904 || (cap->cmdchar == 'h'
5905 && vim_strchr(p_ww, 'h') != NULL)
5906 || (cap->cmdchar == K_LEFT
5907 && vim_strchr(p_ww, '<') != NULL))
5908 && curwin->w_cursor.lnum > 1)
5910 --(curwin->w_cursor.lnum);
5911 coladvance((colnr_T)MAXCOL);
5912 curwin->w_set_curswant = TRUE;
5914 /* When the NL before the first char has to be deleted we
5915 * put the cursor on the NUL after the previous line.
5916 * This is a very special case, be careful!
5917 * Don't adjust op_end now, otherwise it won't work. */
5918 if ( (cap->oap->op_type == OP_DELETE
5919 || cap->oap->op_type == OP_CHANGE)
5920 && !lineempty(curwin->w_cursor.lnum))
5922 if (*ml_get_cursor() != NUL)
5923 ++curwin->w_cursor.col;
5924 cap->retval |= CA_NO_ADJ_OP_END;
5926 continue;
5928 /* Only beep and flush if not moved at all */
5929 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
5930 beep_flush();
5931 break;
5934 #ifdef FEAT_FOLDING
5935 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5936 && cap->oap->op_type == OP_NOP)
5937 foldOpenCursor();
5938 #endif
5942 * Cursor up commands.
5943 * cap->arg is TRUE for "-": Move cursor to first non-blank.
5945 static void
5946 nv_up(cap)
5947 cmdarg_T *cap;
5949 if (mod_mask & MOD_MASK_SHIFT)
5951 /* <S-Up> is page up */
5952 cap->arg = BACKWARD;
5953 nv_page(cap);
5955 else
5957 cap->oap->motion_type = MLINE;
5958 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5959 clearopbeep(cap->oap);
5960 else if (cap->arg)
5961 beginline(BL_WHITE | BL_FIX);
5966 * Cursor down commands.
5967 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
5969 static void
5970 nv_down(cap)
5971 cmdarg_T *cap;
5973 if (mod_mask & MOD_MASK_SHIFT)
5975 /* <S-Down> is page down */
5976 cap->arg = FORWARD;
5977 nv_page(cap);
5979 else
5980 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5981 /* In a quickfix window a <CR> jumps to the error under the cursor. */
5982 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
5983 if (curwin->w_llist_ref == NULL)
5984 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
5985 else
5986 do_cmdline_cmd((char_u *)".ll"); /* location list window */
5987 else
5988 #endif
5990 #ifdef FEAT_CMDWIN
5991 /* In the cmdline window a <CR> executes the command. */
5992 if (cmdwin_type != 0 && cap->cmdchar == CAR)
5993 cmdwin_result = CAR;
5994 else
5995 #endif
5997 cap->oap->motion_type = MLINE;
5998 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5999 clearopbeep(cap->oap);
6000 else if (cap->arg)
6001 beginline(BL_WHITE | BL_FIX);
6006 #ifdef FEAT_SEARCHPATH
6008 * Grab the file name under the cursor and edit it.
6010 static void
6011 nv_gotofile(cap)
6012 cmdarg_T *cap;
6014 char_u *ptr;
6015 linenr_T lnum = -1;
6017 if (text_locked())
6019 clearopbeep(cap->oap);
6020 text_locked_msg();
6021 return;
6023 #ifdef FEAT_AUTOCMD
6024 if (curbuf_locked())
6026 clearop(cap->oap);
6027 return;
6029 #endif
6031 ptr = grab_file_name(cap->count1, &lnum);
6033 if (ptr != NULL)
6035 /* do autowrite if necessary */
6036 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6037 autowrite(curbuf, FALSE);
6038 setpcmark();
6039 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
6040 P_HID(curbuf) ? ECMD_HIDE : 0);
6041 if (cap->nchar == 'F' && lnum >= 0)
6043 curwin->w_cursor.lnum = lnum;
6044 check_cursor_lnum();
6045 beginline(BL_SOL | BL_FIX);
6047 vim_free(ptr);
6049 else
6050 clearop(cap->oap);
6052 #endif
6055 * <End> command: to end of current line or last line.
6057 static void
6058 nv_end(cap)
6059 cmdarg_T *cap;
6061 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
6063 cap->arg = TRUE;
6064 nv_goto(cap);
6065 cap->count1 = 1; /* to end of current line */
6067 nv_dollar(cap);
6071 * Handle the "$" command.
6073 static void
6074 nv_dollar(cap)
6075 cmdarg_T *cap;
6077 cap->oap->motion_type = MCHAR;
6078 cap->oap->inclusive = TRUE;
6079 #ifdef FEAT_VIRTUALEDIT
6080 /* In virtual mode when off the edge of a line and an operator
6081 * is pending (whew!) keep the cursor where it is.
6082 * Otherwise, send it to the end of the line. */
6083 if (!virtual_active() || gchar_cursor() != NUL
6084 || cap->oap->op_type == OP_NOP)
6085 #endif
6086 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6087 if (cursor_down((long)(cap->count1 - 1),
6088 cap->oap->op_type == OP_NOP) == FAIL)
6089 clearopbeep(cap->oap);
6090 #ifdef FEAT_FOLDING
6091 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6092 foldOpenCursor();
6093 #endif
6097 * Implementation of '?' and '/' commands.
6098 * If cap->arg is TRUE don't set PC mark.
6100 static void
6101 nv_search(cap)
6102 cmdarg_T *cap;
6104 oparg_T *oap = cap->oap;
6106 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6108 /* Translate "g??" to "g?g?" */
6109 cap->cmdchar = 'g';
6110 cap->nchar = '?';
6111 nv_operator(cap);
6112 return;
6115 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6117 if (cap->searchbuf == NULL)
6119 clearop(oap);
6120 return;
6123 normal_search(cap, cap->cmdchar, cap->searchbuf,
6124 (cap->arg ? 0 : SEARCH_MARK));
6128 * Handle "N" and "n" commands.
6129 * cap->arg is SEARCH_REV for "N", 0 for "n".
6131 static void
6132 nv_next(cap)
6133 cmdarg_T *cap;
6135 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6139 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6140 * Uses only cap->count1 and cap->oap from "cap".
6142 static void
6143 normal_search(cap, dir, pat, opt)
6144 cmdarg_T *cap;
6145 int dir;
6146 char_u *pat;
6147 int opt; /* extra flags for do_search() */
6149 int i;
6151 cap->oap->motion_type = MCHAR;
6152 cap->oap->inclusive = FALSE;
6153 cap->oap->use_reg_one = TRUE;
6154 curwin->w_set_curswant = TRUE;
6156 i = do_search(cap->oap, dir, pat, cap->count1,
6157 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
6158 if (i == 0)
6159 clearop(cap->oap);
6160 else
6162 if (i == 2)
6163 cap->oap->motion_type = MLINE;
6164 #ifdef FEAT_VIRTUALEDIT
6165 curwin->w_cursor.coladd = 0;
6166 #endif
6167 #ifdef FEAT_FOLDING
6168 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6169 foldOpenCursor();
6170 #endif
6173 /* "/$" will put the cursor after the end of the line, may need to
6174 * correct that here */
6175 check_cursor();
6179 * Character search commands.
6180 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6181 * ',' and FALSE for ';'.
6182 * cap->nchar is NUL for ',' and ';' (repeat the search)
6184 static void
6185 nv_csearch(cap)
6186 cmdarg_T *cap;
6188 int t_cmd;
6190 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6191 t_cmd = TRUE;
6192 else
6193 t_cmd = FALSE;
6195 cap->oap->motion_type = MCHAR;
6196 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6197 clearopbeep(cap->oap);
6198 else
6200 curwin->w_set_curswant = TRUE;
6201 #ifdef FEAT_VIRTUALEDIT
6202 /* Include a Tab for "tx" and for "dfx". */
6203 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6204 && (t_cmd || cap->oap->op_type != OP_NOP))
6206 colnr_T scol, ecol;
6208 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6209 curwin->w_cursor.coladd = ecol - scol;
6211 else
6212 curwin->w_cursor.coladd = 0;
6213 #endif
6214 #ifdef FEAT_VISUAL
6215 adjust_for_sel(cap);
6216 #endif
6217 #ifdef FEAT_FOLDING
6218 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6219 foldOpenCursor();
6220 #endif
6225 * "[" and "]" commands.
6226 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6228 static void
6229 nv_brackets(cap)
6230 cmdarg_T *cap;
6232 pos_T new_pos;
6233 pos_T prev_pos;
6234 pos_T *pos = NULL; /* init for GCC */
6235 pos_T old_pos; /* cursor position before command */
6236 int flag;
6237 long n;
6238 int findc;
6239 int c;
6241 cap->oap->motion_type = MCHAR;
6242 cap->oap->inclusive = FALSE;
6243 old_pos = curwin->w_cursor;
6244 #ifdef FEAT_VIRTUALEDIT
6245 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6246 #endif
6248 #ifdef FEAT_SEARCHPATH
6250 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6252 if (cap->nchar == 'f')
6253 nv_gotofile(cap);
6254 else
6255 #endif
6257 #ifdef FEAT_FIND_ID
6259 * Find the occurrence(s) of the identifier or define under cursor
6260 * in current and included files or jump to the first occurrence.
6262 * search list jump
6263 * fwd bwd fwd bwd fwd bwd
6264 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6265 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6267 if (vim_strchr((char_u *)
6268 #ifdef EBCDIC
6269 "iI\005dD\067",
6270 #else
6271 "iI\011dD\004",
6272 #endif
6273 cap->nchar) != NULL)
6275 char_u *ptr;
6276 int len;
6278 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6279 clearop(cap->oap);
6280 else
6282 find_pattern_in_path(ptr, 0, len, TRUE,
6283 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6284 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6285 cap->count1,
6286 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6287 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6288 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6289 (linenr_T)MAXLNUM);
6290 curwin->w_set_curswant = TRUE;
6293 else
6294 #endif
6297 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6298 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6299 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6300 * "[m" or "]m" search for prev/next start of (Java) method.
6301 * "[M" or "]M" search for prev/next end of (Java) method.
6303 if ( (cap->cmdchar == '['
6304 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6305 || (cap->cmdchar == ']'
6306 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6308 if (cap->nchar == '*')
6309 cap->nchar = '/';
6310 new_pos.lnum = 0;
6311 prev_pos.lnum = 0;
6312 if (cap->nchar == 'm' || cap->nchar == 'M')
6314 if (cap->cmdchar == '[')
6315 findc = '{';
6316 else
6317 findc = '}';
6318 n = 9999;
6320 else
6322 findc = cap->nchar;
6323 n = cap->count1;
6325 for ( ; n > 0; --n)
6327 if ((pos = findmatchlimit(cap->oap, findc,
6328 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6330 if (new_pos.lnum == 0) /* nothing found */
6332 if (cap->nchar != 'm' && cap->nchar != 'M')
6333 clearopbeep(cap->oap);
6335 else
6336 pos = &new_pos; /* use last one found */
6337 break;
6339 prev_pos = new_pos;
6340 curwin->w_cursor = *pos;
6341 new_pos = *pos;
6343 curwin->w_cursor = old_pos;
6346 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6347 * brought us to the match for "[m" and "]M" when inside a method.
6348 * Try finding the '{' or '}' we want to be at.
6349 * Also repeat for the given count.
6351 if (cap->nchar == 'm' || cap->nchar == 'M')
6353 /* norm is TRUE for "]M" and "[m" */
6354 int norm = ((findc == '{') == (cap->nchar == 'm'));
6356 n = cap->count1;
6357 /* found a match: we were inside a method */
6358 if (prev_pos.lnum != 0)
6360 pos = &prev_pos;
6361 curwin->w_cursor = prev_pos;
6362 if (norm)
6363 --n;
6365 else
6366 pos = NULL;
6367 while (n > 0)
6369 for (;;)
6371 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6373 /* if not found anything, that's an error */
6374 if (pos == NULL)
6375 clearopbeep(cap->oap);
6376 n = 0;
6377 break;
6379 c = gchar_cursor();
6380 if (c == '{' || c == '}')
6382 /* Must have found end/start of class: use it.
6383 * Or found the place to be at. */
6384 if ((c == findc && norm) || (n == 1 && !norm))
6386 new_pos = curwin->w_cursor;
6387 pos = &new_pos;
6388 n = 0;
6390 /* if no match found at all, we started outside of the
6391 * class and we're inside now. Just go on. */
6392 else if (new_pos.lnum == 0)
6394 new_pos = curwin->w_cursor;
6395 pos = &new_pos;
6397 /* found start/end of other method: go to match */
6398 else if ((pos = findmatchlimit(cap->oap, findc,
6399 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6400 0)) == NULL)
6401 n = 0;
6402 else
6403 curwin->w_cursor = *pos;
6404 break;
6407 --n;
6409 curwin->w_cursor = old_pos;
6410 if (pos == NULL && new_pos.lnum != 0)
6411 clearopbeep(cap->oap);
6413 if (pos != NULL)
6415 setpcmark();
6416 curwin->w_cursor = *pos;
6417 curwin->w_set_curswant = TRUE;
6418 #ifdef FEAT_FOLDING
6419 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6420 && cap->oap->op_type == OP_NOP)
6421 foldOpenCursor();
6422 #endif
6427 * "[[", "[]", "]]" and "][": move to start or end of function
6429 else if (cap->nchar == '[' || cap->nchar == ']')
6431 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6432 flag = '{';
6433 else
6434 flag = '}'; /* "][" or "[]" */
6436 curwin->w_set_curswant = TRUE;
6438 * Imitate strange Vi behaviour: When using "]]" with an operator
6439 * we also stop at '}'.
6441 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
6442 (cap->oap->op_type != OP_NOP
6443 && cap->arg == FORWARD && flag == '{')))
6444 clearopbeep(cap->oap);
6445 else
6447 if (cap->oap->op_type == OP_NOP)
6448 beginline(BL_WHITE | BL_FIX);
6449 #ifdef FEAT_FOLDING
6450 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6451 foldOpenCursor();
6452 #endif
6457 * "[p", "[P", "]P" and "]p": put with indent adjustment
6459 else if (cap->nchar == 'p' || cap->nchar == 'P')
6461 if (!checkclearop(cap->oap))
6463 prep_redo_cmd(cap);
6464 do_put(cap->oap->regname,
6465 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6466 cap->count1, PUT_FIXINDENT);
6471 * "['", "[`", "]'" and "]`": jump to next mark
6473 else if (cap->nchar == '\'' || cap->nchar == '`')
6475 pos = &curwin->w_cursor;
6476 for (n = cap->count1; n > 0; --n)
6478 prev_pos = *pos;
6479 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6480 cap->nchar == '\'');
6481 if (pos == NULL)
6482 break;
6484 if (pos == NULL)
6485 pos = &prev_pos;
6486 nv_cursormark(cap, cap->nchar == '\'', pos);
6489 #ifdef FEAT_MOUSE
6491 * [ or ] followed by a middle mouse click: put selected text with
6492 * indent adjustment. Any other button just does as usual.
6494 else if (cap->nchar >= K_LEFTMOUSE && cap->nchar <= K_RIGHTRELEASE)
6496 (void)do_mouse(cap->oap, cap->nchar,
6497 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6498 cap->count1, PUT_FIXINDENT);
6500 #endif /* FEAT_MOUSE */
6502 #ifdef FEAT_FOLDING
6504 * "[z" and "]z": move to start or end of open fold.
6506 else if (cap->nchar == 'z')
6508 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6509 cap->count1) == FAIL)
6510 clearopbeep(cap->oap);
6512 #endif
6514 #ifdef FEAT_DIFF
6516 * "[c" and "]c": move to next or previous diff-change.
6518 else if (cap->nchar == 'c')
6520 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6521 cap->count1) == FAIL)
6522 clearopbeep(cap->oap);
6524 #endif
6526 #ifdef FEAT_SPELL
6528 * "[s", "[S", "]s" and "]S": move to next spell error.
6530 else if (cap->nchar == 's' || cap->nchar == 'S')
6532 setpcmark();
6533 for (n = 0; n < cap->count1; ++n)
6534 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6535 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
6537 clearopbeep(cap->oap);
6538 break;
6540 # ifdef FEAT_FOLDING
6541 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6542 foldOpenCursor();
6543 # endif
6545 #endif
6547 /* Not a valid cap->nchar. */
6548 else
6549 clearopbeep(cap->oap);
6553 * Handle Normal mode "%" command.
6555 static void
6556 nv_percent(cap)
6557 cmdarg_T *cap;
6559 pos_T *pos;
6560 #ifdef FEAT_FOLDING
6561 linenr_T lnum = curwin->w_cursor.lnum;
6562 #endif
6564 cap->oap->inclusive = TRUE;
6565 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6567 if (cap->count0 > 100)
6568 clearopbeep(cap->oap);
6569 else
6571 cap->oap->motion_type = MLINE;
6572 setpcmark();
6573 /* Round up, so CTRL-G will give same value. Watch out for a
6574 * large line count, the line number must not go negative! */
6575 if (curbuf->b_ml.ml_line_count > 1000000)
6576 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6577 / 100L * cap->count0;
6578 else
6579 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6580 cap->count0 + 99L) / 100L;
6581 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6582 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6583 beginline(BL_SOL | BL_FIX);
6586 else /* "%" : go to matching paren */
6588 cap->oap->motion_type = MCHAR;
6589 cap->oap->use_reg_one = TRUE;
6590 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6591 clearopbeep(cap->oap);
6592 else
6594 setpcmark();
6595 curwin->w_cursor = *pos;
6596 curwin->w_set_curswant = TRUE;
6597 #ifdef FEAT_VIRTUALEDIT
6598 curwin->w_cursor.coladd = 0;
6599 #endif
6600 #ifdef FEAT_VISUAL
6601 adjust_for_sel(cap);
6602 #endif
6605 #ifdef FEAT_FOLDING
6606 if (cap->oap->op_type == OP_NOP
6607 && lnum != curwin->w_cursor.lnum
6608 && (fdo_flags & FDO_PERCENT)
6609 && KeyTyped)
6610 foldOpenCursor();
6611 #endif
6615 * Handle "(" and ")" commands.
6616 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6618 static void
6619 nv_brace(cap)
6620 cmdarg_T *cap;
6622 cap->oap->motion_type = MCHAR;
6623 cap->oap->use_reg_one = TRUE;
6624 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6625 cap->oap->inclusive = FALSE;
6626 curwin->w_set_curswant = TRUE;
6628 if (findsent(cap->arg, cap->count1) == FAIL)
6629 clearopbeep(cap->oap);
6630 else
6632 /* Don't leave the cursor on the NUL past end of line. */
6633 adjust_cursor(cap->oap);
6634 #ifdef FEAT_VIRTUALEDIT
6635 curwin->w_cursor.coladd = 0;
6636 #endif
6637 #ifdef FEAT_FOLDING
6638 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6639 foldOpenCursor();
6640 #endif
6645 * "m" command: Mark a position.
6647 static void
6648 nv_mark(cap)
6649 cmdarg_T *cap;
6651 if (!checkclearop(cap->oap))
6653 if (setmark(cap->nchar) == FAIL)
6654 clearopbeep(cap->oap);
6659 * "{" and "}" commands.
6660 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6662 static void
6663 nv_findpar(cap)
6664 cmdarg_T *cap;
6666 cap->oap->motion_type = MCHAR;
6667 cap->oap->inclusive = FALSE;
6668 cap->oap->use_reg_one = TRUE;
6669 curwin->w_set_curswant = TRUE;
6670 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
6671 clearopbeep(cap->oap);
6672 else
6674 #ifdef FEAT_VIRTUALEDIT
6675 curwin->w_cursor.coladd = 0;
6676 #endif
6677 #ifdef FEAT_FOLDING
6678 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6679 foldOpenCursor();
6680 #endif
6685 * "u" command: Undo or make lower case.
6687 static void
6688 nv_undo(cap)
6689 cmdarg_T *cap;
6691 if (cap->oap->op_type == OP_LOWER
6692 #ifdef FEAT_VISUAL
6693 || VIsual_active
6694 #endif
6697 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6698 cap->cmdchar = 'g';
6699 cap->nchar = 'u';
6700 nv_operator(cap);
6702 else
6703 nv_kundo(cap);
6707 * <Undo> command.
6709 static void
6710 nv_kundo(cap)
6711 cmdarg_T *cap;
6713 if (!checkclearopq(cap->oap))
6715 u_undo((int)cap->count1);
6716 curwin->w_set_curswant = TRUE;
6721 * Handle the "r" command.
6723 static void
6724 nv_replace(cap)
6725 cmdarg_T *cap;
6727 char_u *ptr;
6728 int had_ctrl_v;
6729 long n;
6731 if (checkclearop(cap->oap))
6732 return;
6734 /* get another character */
6735 if (cap->nchar == Ctrl_V)
6737 had_ctrl_v = Ctrl_V;
6738 cap->nchar = get_literal();
6739 /* Don't redo a multibyte character with CTRL-V. */
6740 if (cap->nchar > DEL)
6741 had_ctrl_v = NUL;
6743 else
6744 had_ctrl_v = NUL;
6746 /* Abort if the character is a special key. */
6747 if (IS_SPECIAL(cap->nchar))
6749 clearopbeep(cap->oap);
6750 return;
6753 #ifdef FEAT_VISUAL
6754 /* Visual mode "r" */
6755 if (VIsual_active)
6757 nv_operator(cap);
6758 return;
6760 #endif
6762 #ifdef FEAT_VIRTUALEDIT
6763 /* Break tabs, etc. */
6764 if (virtual_active())
6766 if (u_save_cursor() == FAIL)
6767 return;
6768 if (gchar_cursor() == NUL)
6770 /* Add extra space and put the cursor on the first one. */
6771 coladvance_force((colnr_T)(getviscol() + cap->count1));
6772 curwin->w_cursor.col -= cap->count1;
6774 else if (gchar_cursor() == TAB)
6775 coladvance_force(getviscol());
6777 #endif
6779 /* Abort if not enough characters to replace. */
6780 ptr = ml_get_cursor();
6781 if (STRLEN(ptr) < (unsigned)cap->count1
6782 #ifdef FEAT_MBYTE
6783 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6784 #endif
6787 clearopbeep(cap->oap);
6788 return;
6792 * Replacing with a TAB is done by edit() when it is complicated because
6793 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6794 * Other characters are done below to avoid problems with things like
6795 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6797 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6799 stuffnumReadbuff(cap->count1);
6800 stuffcharReadbuff('R');
6801 stuffcharReadbuff('\t');
6802 stuffcharReadbuff(ESC);
6803 return;
6806 /* save line for undo */
6807 if (u_save_cursor() == FAIL)
6808 return;
6810 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6813 * Replace character(s) by a single newline.
6814 * Strange vi behaviour: Only one newline is inserted.
6815 * Delete the characters here.
6816 * Insert the newline with an insert command, takes care of
6817 * autoindent. The insert command depends on being on the last
6818 * character of a line or not.
6820 #ifdef FEAT_MBYTE
6821 (void)del_chars(cap->count1, FALSE); /* delete the characters */
6822 #else
6823 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
6824 #endif
6825 stuffcharReadbuff('\r');
6826 stuffcharReadbuff(ESC);
6828 /* Give 'r' to edit(), to get the redo command right. */
6829 invoke_edit(cap, TRUE, 'r', FALSE);
6831 else
6833 prep_redo(cap->oap->regname, cap->count1,
6834 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
6836 curbuf->b_op_start = curwin->w_cursor;
6837 #ifdef FEAT_MBYTE
6838 if (has_mbyte)
6840 int old_State = State;
6842 if (cap->ncharC1 != 0)
6843 AppendCharToRedobuff(cap->ncharC1);
6844 if (cap->ncharC2 != 0)
6845 AppendCharToRedobuff(cap->ncharC2);
6847 /* This is slow, but it handles replacing a single-byte with a
6848 * multi-byte and the other way around. Also handles adding
6849 * composing characters for utf-8. */
6850 for (n = cap->count1; n > 0; --n)
6852 State = REPLACE;
6853 ins_char(cap->nchar);
6854 State = old_State;
6855 if (cap->ncharC1 != 0)
6856 ins_char(cap->ncharC1);
6857 if (cap->ncharC2 != 0)
6858 ins_char(cap->ncharC2);
6861 else
6862 #endif
6865 * Replace the characters within one line.
6867 for (n = cap->count1; n > 0; --n)
6870 * Get ptr again, because u_save and/or showmatch() will have
6871 * released the line. At the same time we let know that the
6872 * line will be changed.
6874 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
6875 ptr[curwin->w_cursor.col] = cap->nchar;
6876 if (p_sm && msg_silent == 0)
6877 showmatch(cap->nchar);
6878 ++curwin->w_cursor.col;
6880 #ifdef FEAT_NETBEANS_INTG
6881 if (usingNetbeans)
6883 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
6885 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
6886 (long)cap->count1);
6887 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
6888 &ptr[start], (int)cap->count1);
6890 #endif
6892 /* mark the buffer as changed and prepare for displaying */
6893 changed_bytes(curwin->w_cursor.lnum,
6894 (colnr_T)(curwin->w_cursor.col - cap->count1));
6896 --curwin->w_cursor.col; /* cursor on the last replaced char */
6897 #ifdef FEAT_MBYTE
6898 /* if the character on the left of the current cursor is a multi-byte
6899 * character, move two characters left */
6900 if (has_mbyte)
6901 mb_adjust_cursor();
6902 #endif
6903 curbuf->b_op_end = curwin->w_cursor;
6904 curwin->w_set_curswant = TRUE;
6905 set_last_insert(cap->nchar);
6909 #ifdef FEAT_VISUAL
6911 * 'o': Exchange start and end of Visual area.
6912 * 'O': same, but in block mode exchange left and right corners.
6914 static void
6915 v_swap_corners(cmdchar)
6916 int cmdchar;
6918 pos_T old_cursor;
6919 colnr_T left, right;
6921 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
6923 old_cursor = curwin->w_cursor;
6924 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
6925 curwin->w_cursor.lnum = VIsual.lnum;
6926 coladvance(left);
6927 VIsual = curwin->w_cursor;
6929 curwin->w_cursor.lnum = old_cursor.lnum;
6930 curwin->w_curswant = right;
6931 /* 'selection "exclusive" and cursor at right-bottom corner: move it
6932 * right one column */
6933 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
6934 ++curwin->w_curswant;
6935 coladvance(curwin->w_curswant);
6936 if (curwin->w_cursor.col == old_cursor.col
6937 #ifdef FEAT_VIRTUALEDIT
6938 && (!virtual_active()
6939 || curwin->w_cursor.coladd == old_cursor.coladd)
6940 #endif
6943 curwin->w_cursor.lnum = VIsual.lnum;
6944 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
6945 ++right;
6946 coladvance(right);
6947 VIsual = curwin->w_cursor;
6949 curwin->w_cursor.lnum = old_cursor.lnum;
6950 coladvance(left);
6951 curwin->w_curswant = left;
6954 else
6956 old_cursor = curwin->w_cursor;
6957 curwin->w_cursor = VIsual;
6958 VIsual = old_cursor;
6959 curwin->w_set_curswant = TRUE;
6962 #endif /* FEAT_VISUAL */
6965 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
6967 static void
6968 nv_Replace(cap)
6969 cmdarg_T *cap;
6971 #ifdef FEAT_VISUAL
6972 if (VIsual_active) /* "R" is replace lines */
6974 cap->cmdchar = 'c';
6975 cap->nchar = NUL;
6976 VIsual_mode = 'V';
6977 nv_operator(cap);
6979 else
6980 #endif
6981 if (!checkclearopq(cap->oap))
6983 if (!curbuf->b_p_ma)
6984 EMSG(_(e_modifiable));
6985 else
6987 #ifdef FEAT_VIRTUALEDIT
6988 if (virtual_active())
6989 coladvance(getviscol());
6990 #endif
6991 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
6996 #ifdef FEAT_VREPLACE
6998 * "gr".
7000 static void
7001 nv_vreplace(cap)
7002 cmdarg_T *cap;
7004 # ifdef FEAT_VISUAL
7005 if (VIsual_active)
7007 cap->cmdchar = 'r';
7008 cap->nchar = cap->extra_char;
7009 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7011 else
7012 # endif
7013 if (!checkclearopq(cap->oap))
7015 if (!curbuf->b_p_ma)
7016 EMSG(_(e_modifiable));
7017 else
7019 if (cap->extra_char == Ctrl_V) /* get another character */
7020 cap->extra_char = get_literal();
7021 stuffcharReadbuff(cap->extra_char);
7022 stuffcharReadbuff(ESC);
7023 # ifdef FEAT_VIRTUALEDIT
7024 if (virtual_active())
7025 coladvance(getviscol());
7026 # endif
7027 invoke_edit(cap, TRUE, 'v', FALSE);
7031 #endif
7034 * Swap case for "~" command, when it does not work like an operator.
7036 static void
7037 n_swapchar(cap)
7038 cmdarg_T *cap;
7040 long n;
7041 pos_T startpos;
7042 int did_change = 0;
7043 #ifdef FEAT_NETBEANS_INTG
7044 pos_T pos;
7045 char_u *ptr;
7046 int count;
7047 #endif
7049 if (checkclearopq(cap->oap))
7050 return;
7052 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7054 clearopbeep(cap->oap);
7055 return;
7058 prep_redo_cmd(cap);
7060 if (u_save_cursor() == FAIL)
7061 return;
7063 startpos = curwin->w_cursor;
7064 #ifdef FEAT_NETBEANS_INTG
7065 pos = startpos;
7066 #endif
7067 for (n = cap->count1; n > 0; --n)
7069 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7070 inc_cursor();
7071 if (gchar_cursor() == NUL)
7073 if (vim_strchr(p_ww, '~') != NULL
7074 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7076 #ifdef FEAT_NETBEANS_INTG
7077 if (usingNetbeans)
7079 if (did_change)
7081 ptr = ml_get(pos.lnum);
7082 count = (int)STRLEN(ptr) - pos.col;
7083 netbeans_removed(curbuf, pos.lnum, pos.col,
7084 (long)count);
7085 netbeans_inserted(curbuf, pos.lnum, pos.col,
7086 &ptr[pos.col], count);
7088 pos.col = 0;
7089 pos.lnum++;
7091 #endif
7092 ++curwin->w_cursor.lnum;
7093 curwin->w_cursor.col = 0;
7094 if (n > 1)
7096 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7097 break;
7098 u_clearline();
7101 else
7102 break;
7105 #ifdef FEAT_NETBEANS_INTG
7106 if (did_change && usingNetbeans)
7108 ptr = ml_get(pos.lnum);
7109 count = curwin->w_cursor.col - pos.col;
7110 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7111 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
7113 #endif
7116 check_cursor();
7117 curwin->w_set_curswant = TRUE;
7118 if (did_change)
7120 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7121 0L);
7122 curbuf->b_op_start = startpos;
7123 curbuf->b_op_end = curwin->w_cursor;
7124 if (curbuf->b_op_end.col > 0)
7125 --curbuf->b_op_end.col;
7130 * Move cursor to mark.
7132 static void
7133 nv_cursormark(cap, flag, pos)
7134 cmdarg_T *cap;
7135 int flag;
7136 pos_T *pos;
7138 if (check_mark(pos) == FAIL)
7139 clearop(cap->oap);
7140 else
7142 if (cap->cmdchar == '\''
7143 || cap->cmdchar == '`'
7144 || cap->cmdchar == '['
7145 || cap->cmdchar == ']')
7146 setpcmark();
7147 curwin->w_cursor = *pos;
7148 if (flag)
7149 beginline(BL_WHITE | BL_FIX);
7150 else
7151 check_cursor();
7153 cap->oap->motion_type = flag ? MLINE : MCHAR;
7154 if (cap->cmdchar == '`')
7155 cap->oap->use_reg_one = TRUE;
7156 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7157 curwin->w_set_curswant = TRUE;
7160 #ifdef FEAT_VISUAL
7162 * Handle commands that are operators in Visual mode.
7164 static void
7165 v_visop(cap)
7166 cmdarg_T *cap;
7168 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7170 /* Uppercase means linewise, except in block mode, then "D" deletes till
7171 * the end of the line, and "C" replaces til EOL */
7172 if (isupper(cap->cmdchar))
7174 if (VIsual_mode != Ctrl_V)
7175 VIsual_mode = 'V';
7176 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7177 curwin->w_curswant = MAXCOL;
7179 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7180 nv_operator(cap);
7182 #endif
7185 * "s" and "S" commands.
7187 static void
7188 nv_subst(cap)
7189 cmdarg_T *cap;
7191 #ifdef FEAT_VISUAL
7192 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7194 if (cap->cmdchar == 'S')
7195 VIsual_mode = 'V';
7196 cap->cmdchar = 'c';
7197 nv_operator(cap);
7199 else
7200 #endif
7201 nv_optrans(cap);
7205 * Abbreviated commands.
7207 static void
7208 nv_abbrev(cap)
7209 cmdarg_T *cap;
7211 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7212 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7214 #ifdef FEAT_VISUAL
7215 /* in Visual mode these commands are operators */
7216 if (VIsual_active)
7217 v_visop(cap);
7218 else
7219 #endif
7220 nv_optrans(cap);
7224 * Translate a command into another command.
7226 static void
7227 nv_optrans(cap)
7228 cmdarg_T *cap;
7230 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7231 (char_u *)"d$", (char_u *)"c$",
7232 (char_u *)"cl", (char_u *)"cc",
7233 (char_u *)"yy", (char_u *)":s\r"};
7234 static char_u *str = (char_u *)"xXDCsSY&";
7236 if (!checkclearopq(cap->oap))
7238 /* In Vi "2D" doesn't delete the next line. Can't translate it
7239 * either, because "2." should also not use the count. */
7240 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7242 cap->oap->start = curwin->w_cursor;
7243 cap->oap->op_type = OP_DELETE;
7244 #ifdef FEAT_EVAL
7245 set_op_var(OP_DELETE);
7246 #endif
7247 cap->count1 = 1;
7248 nv_dollar(cap);
7249 finish_op = TRUE;
7250 ResetRedobuff();
7251 AppendCharToRedobuff('D');
7253 else
7255 if (cap->count0)
7256 stuffnumReadbuff(cap->count0);
7257 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7260 cap->opcount = 0;
7264 * "'" and "`" commands. Also for "g'" and "g`".
7265 * cap->arg is TRUE for "'" and "g'".
7267 static void
7268 nv_gomark(cap)
7269 cmdarg_T *cap;
7271 pos_T *pos;
7272 int c;
7273 #ifdef FEAT_FOLDING
7274 linenr_T lnum = curwin->w_cursor.lnum;
7275 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7276 #endif
7278 if (cap->cmdchar == 'g')
7279 c = cap->extra_char;
7280 else
7281 c = cap->nchar;
7282 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7283 if (pos == (pos_T *)-1) /* jumped to other file */
7285 if (cap->arg)
7287 check_cursor_lnum();
7288 beginline(BL_WHITE | BL_FIX);
7290 else
7291 check_cursor();
7293 else
7294 nv_cursormark(cap, cap->arg, pos);
7296 #ifdef FEAT_VIRTUALEDIT
7297 /* May need to clear the coladd that a mark includes. */
7298 if (!virtual_active())
7299 curwin->w_cursor.coladd = 0;
7300 #endif
7301 #ifdef FEAT_FOLDING
7302 if (cap->oap->op_type == OP_NOP
7303 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7304 && (fdo_flags & FDO_MARK)
7305 && old_KeyTyped)
7306 foldOpenCursor();
7307 #endif
7311 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7313 static void
7314 nv_pcmark(cap)
7315 cmdarg_T *cap;
7317 #ifdef FEAT_JUMPLIST
7318 pos_T *pos;
7319 # ifdef FEAT_FOLDING
7320 linenr_T lnum = curwin->w_cursor.lnum;
7321 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7322 # endif
7324 if (!checkclearopq(cap->oap))
7326 if (cap->cmdchar == 'g')
7327 pos = movechangelist((int)cap->count1);
7328 else
7329 pos = movemark((int)cap->count1);
7330 if (pos == (pos_T *)-1) /* jump to other file */
7332 curwin->w_set_curswant = TRUE;
7333 check_cursor();
7335 else if (pos != NULL) /* can jump */
7336 nv_cursormark(cap, FALSE, pos);
7337 else if (cap->cmdchar == 'g')
7339 if (curbuf->b_changelistlen == 0)
7340 EMSG(_("E664: changelist is empty"));
7341 else if (cap->count1 < 0)
7342 EMSG(_("E662: At start of changelist"));
7343 else
7344 EMSG(_("E663: At end of changelist"));
7346 else
7347 clearopbeep(cap->oap);
7348 # ifdef FEAT_FOLDING
7349 if (cap->oap->op_type == OP_NOP
7350 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7351 && (fdo_flags & FDO_MARK)
7352 && old_KeyTyped)
7353 foldOpenCursor();
7354 # endif
7356 #else
7357 clearopbeep(cap->oap);
7358 #endif
7362 * Handle '"' command.
7364 static void
7365 nv_regname(cap)
7366 cmdarg_T *cap;
7368 if (checkclearop(cap->oap))
7369 return;
7370 #ifdef FEAT_EVAL
7371 if (cap->nchar == '=')
7372 cap->nchar = get_expr_register();
7373 #endif
7374 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7376 cap->oap->regname = cap->nchar;
7377 cap->opcount = cap->count0; /* remember count before '"' */
7378 #ifdef FEAT_EVAL
7379 set_reg_var(cap->oap->regname);
7380 #endif
7382 else
7383 clearopbeep(cap->oap);
7386 #ifdef FEAT_VISUAL
7388 * Handle "v", "V" and "CTRL-V" commands.
7389 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7390 * is TRUE.
7391 * Handle CTRL-Q just like CTRL-V.
7393 static void
7394 nv_visual(cap)
7395 cmdarg_T *cap;
7397 if (cap->cmdchar == Ctrl_Q)
7398 cap->cmdchar = Ctrl_V;
7400 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7401 * characterwise, linewise, or blockwise. */
7402 if (cap->oap->op_type != OP_NOP)
7404 cap->oap->motion_force = cap->cmdchar;
7405 finish_op = FALSE; /* operator doesn't finish now but later */
7406 return;
7409 VIsual_select = cap->arg;
7410 if (VIsual_active) /* change Visual mode */
7412 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7413 end_visual_mode();
7414 else /* toggle char/block mode */
7415 { /* or char/line mode */
7416 VIsual_mode = cap->cmdchar;
7417 showmode();
7419 redraw_curbuf_later(INVERTED); /* update the inversion */
7421 else /* start Visual mode */
7423 check_visual_highlight();
7424 if (cap->count0) /* use previously selected part */
7426 if (resel_VIsual_mode == NUL) /* there is none */
7428 beep_flush();
7429 return;
7431 VIsual = curwin->w_cursor;
7433 VIsual_active = TRUE;
7434 VIsual_reselect = TRUE;
7435 if (!cap->arg)
7436 /* start Select mode when 'selectmode' contains "cmd" */
7437 may_start_select('c');
7438 #ifdef FEAT_MOUSE
7439 setmouse();
7440 #endif
7441 if (p_smd && msg_silent == 0)
7442 redraw_cmdline = TRUE; /* show visual mode later */
7444 * For V and ^V, we multiply the number of lines even if there
7445 * was only one -- webb
7447 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7449 curwin->w_cursor.lnum +=
7450 resel_VIsual_line_count * cap->count0 - 1;
7451 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7452 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7454 VIsual_mode = resel_VIsual_mode;
7455 if (VIsual_mode == 'v')
7457 if (resel_VIsual_line_count <= 1)
7458 curwin->w_cursor.col += resel_VIsual_col * cap->count0 - 1;
7459 else
7460 curwin->w_cursor.col = resel_VIsual_col;
7461 check_cursor_col();
7463 if (resel_VIsual_col == MAXCOL)
7465 curwin->w_curswant = MAXCOL;
7466 coladvance((colnr_T)MAXCOL);
7468 else if (VIsual_mode == Ctrl_V)
7470 validate_virtcol();
7471 curwin->w_curswant = curwin->w_virtcol
7472 + resel_VIsual_col * cap->count0 - 1;
7473 coladvance(curwin->w_curswant);
7475 else
7476 curwin->w_set_curswant = TRUE;
7477 redraw_curbuf_later(INVERTED); /* show the inversion */
7479 else
7481 if (!cap->arg)
7482 /* start Select mode when 'selectmode' contains "cmd" */
7483 may_start_select('c');
7484 n_start_visual_mode(cap->cmdchar);
7490 * Start selection for Shift-movement keys.
7492 void
7493 start_selection()
7495 /* if 'selectmode' contains "key", start Select mode */
7496 may_start_select('k');
7497 n_start_visual_mode('v');
7501 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7503 void
7504 may_start_select(c)
7505 int c;
7507 VIsual_select = (stuff_empty() && typebuf_typed()
7508 && (vim_strchr(p_slm, c) != NULL));
7512 * Start Visual mode "c".
7513 * Should set VIsual_select before calling this.
7515 static void
7516 n_start_visual_mode(c)
7517 int c;
7519 VIsual_mode = c;
7520 VIsual_active = TRUE;
7521 VIsual_reselect = TRUE;
7522 #ifdef FEAT_VIRTUALEDIT
7523 /* Corner case: the 0 position in a tab may change when going into
7524 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7526 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7527 coladvance(curwin->w_virtcol);
7528 #endif
7529 VIsual = curwin->w_cursor;
7531 #ifdef FEAT_FOLDING
7532 foldAdjustVisual();
7533 #endif
7535 #ifdef FEAT_MOUSE
7536 setmouse();
7537 #endif
7538 if (p_smd && msg_silent == 0)
7539 redraw_cmdline = TRUE; /* show visual mode later */
7540 #ifdef FEAT_CLIPBOARD
7541 /* Make sure the clipboard gets updated. Needed because start and
7542 * end may still be the same, and the selection needs to be owned */
7543 clip_star.vmode = NUL;
7544 #endif
7546 /* Only need to redraw this line, unless still need to redraw an old
7547 * Visual area (when 'lazyredraw' is set). */
7548 if (curwin->w_redr_type < INVERTED)
7550 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7551 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7555 #endif /* FEAT_VISUAL */
7558 * CTRL-W: Window commands
7560 static void
7561 nv_window(cap)
7562 cmdarg_T *cap;
7564 #ifdef FEAT_WINDOWS
7565 if (!checkclearop(cap->oap))
7566 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7567 #else
7568 (void)checkclearop(cap->oap);
7569 #endif
7573 * CTRL-Z: Suspend
7575 static void
7576 nv_suspend(cap)
7577 cmdarg_T *cap;
7579 clearop(cap->oap);
7580 #ifdef FEAT_VISUAL
7581 if (VIsual_active)
7582 end_visual_mode(); /* stop Visual mode */
7583 #endif
7584 do_cmdline_cmd((char_u *)"st");
7588 * Commands starting with "g".
7590 static void
7591 nv_g_cmd(cap)
7592 cmdarg_T *cap;
7594 oparg_T *oap = cap->oap;
7595 #ifdef FEAT_VISUAL
7596 pos_T tpos;
7597 #endif
7598 int i;
7599 int flag = FALSE;
7601 switch (cap->nchar)
7603 #ifdef MEM_PROFILE
7605 * "g^A": dump log of used memory.
7607 case Ctrl_A:
7608 vim_mem_profile_dump();
7609 break;
7610 #endif
7612 #ifdef FEAT_VREPLACE
7614 * "gR": Enter virtual replace mode.
7616 case 'R':
7617 cap->arg = TRUE;
7618 nv_Replace(cap);
7619 break;
7621 case 'r':
7622 nv_vreplace(cap);
7623 break;
7624 #endif
7626 case '&':
7627 do_cmdline_cmd((char_u *)"%s//~/&");
7628 break;
7630 #ifdef FEAT_VISUAL
7632 * "gv": Reselect the previous Visual area. If Visual already active,
7633 * exchange previous and current Visual area.
7635 case 'v':
7636 if (checkclearop(oap))
7637 break;
7639 if ( curbuf->b_visual.vi_start.lnum == 0
7640 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7641 || curbuf->b_visual.vi_end.lnum == 0)
7642 beep_flush();
7643 else
7645 /* set w_cursor to the start of the Visual area, tpos to the end */
7646 if (VIsual_active)
7648 i = VIsual_mode;
7649 VIsual_mode = curbuf->b_visual.vi_mode;
7650 curbuf->b_visual.vi_mode = i;
7651 # ifdef FEAT_EVAL
7652 curbuf->b_visual_mode_eval = i;
7653 # endif
7654 i = curwin->w_curswant;
7655 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7656 curbuf->b_visual.vi_curswant = i;
7658 tpos = curbuf->b_visual.vi_end;
7659 curbuf->b_visual.vi_end = curwin->w_cursor;
7660 curwin->w_cursor = curbuf->b_visual.vi_start;
7661 curbuf->b_visual.vi_start = VIsual;
7663 else
7665 VIsual_mode = curbuf->b_visual.vi_mode;
7666 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7667 tpos = curbuf->b_visual.vi_end;
7668 curwin->w_cursor = curbuf->b_visual.vi_start;
7671 VIsual_active = TRUE;
7672 VIsual_reselect = TRUE;
7674 /* Set Visual to the start and w_cursor to the end of the Visual
7675 * area. Make sure they are on an existing character. */
7676 check_cursor();
7677 VIsual = curwin->w_cursor;
7678 curwin->w_cursor = tpos;
7679 check_cursor();
7680 update_topline();
7682 * When called from normal "g" command: start Select mode when
7683 * 'selectmode' contains "cmd". When called for K_SELECT, always
7684 * start Select mode.
7686 if (cap->arg)
7687 VIsual_select = TRUE;
7688 else
7689 may_start_select('c');
7690 #ifdef FEAT_MOUSE
7691 setmouse();
7692 #endif
7693 #ifdef FEAT_CLIPBOARD
7694 /* Make sure the clipboard gets updated. Needed because start and
7695 * end are still the same, and the selection needs to be owned */
7696 clip_star.vmode = NUL;
7697 #endif
7698 redraw_curbuf_later(INVERTED);
7699 showmode();
7701 break;
7703 * "gV": Don't reselect the previous Visual area after a Select mode
7704 * mapping of menu.
7706 case 'V':
7707 VIsual_reselect = FALSE;
7708 break;
7711 * "gh": start Select mode.
7712 * "gH": start Select line mode.
7713 * "g^H": start Select block mode.
7715 case K_BS:
7716 cap->nchar = Ctrl_H;
7717 /* FALLTHROUGH */
7718 case 'h':
7719 case 'H':
7720 case Ctrl_H:
7721 # ifdef EBCDIC
7722 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7723 if (cap->nchar == Ctrl_H)
7724 cap->cmdchar = Ctrl_V;
7725 else
7726 # endif
7727 cap->cmdchar = cap->nchar + ('v' - 'h');
7728 cap->arg = TRUE;
7729 nv_visual(cap);
7730 break;
7731 #endif /* FEAT_VISUAL */
7734 * "gj" and "gk" two new funny movement keys -- up and down
7735 * movement based on *screen* line rather than *file* line.
7737 case 'j':
7738 case K_DOWN:
7739 /* with 'nowrap' it works just like the normal "j" command; also when
7740 * in a closed fold */
7741 if (!curwin->w_p_wrap
7742 #ifdef FEAT_FOLDING
7743 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7744 #endif
7747 oap->motion_type = MLINE;
7748 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7750 else
7751 i = nv_screengo(oap, FORWARD, cap->count1);
7752 if (i == FAIL)
7753 clearopbeep(oap);
7754 break;
7756 case 'k':
7757 case K_UP:
7758 /* with 'nowrap' it works just like the normal "k" command; also when
7759 * in a closed fold */
7760 if (!curwin->w_p_wrap
7761 #ifdef FEAT_FOLDING
7762 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7763 #endif
7766 oap->motion_type = MLINE;
7767 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7769 else
7770 i = nv_screengo(oap, BACKWARD, cap->count1);
7771 if (i == FAIL)
7772 clearopbeep(oap);
7773 break;
7776 * "gJ": join two lines without inserting a space.
7778 case 'J':
7779 nv_join(cap);
7780 break;
7783 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7784 * "gm": middle of "g0" and "g$".
7786 case '^':
7787 flag = TRUE;
7788 /* FALLTHROUGH */
7790 case '0':
7791 case 'm':
7792 case K_HOME:
7793 case K_KHOME:
7794 oap->motion_type = MCHAR;
7795 oap->inclusive = FALSE;
7796 if (curwin->w_p_wrap
7797 #ifdef FEAT_VERTSPLIT
7798 && curwin->w_width != 0
7799 #endif
7802 int width1 = W_WIDTH(curwin) - curwin_col_off();
7803 int width2 = width1 + curwin_col_off2();
7805 validate_virtcol();
7806 i = 0;
7807 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
7808 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
7810 else
7811 i = curwin->w_leftcol;
7812 /* Go to the middle of the screen line. When 'number' is on and lines
7813 * are wrapping the middle can be more to the left.*/
7814 if (cap->nchar == 'm')
7815 i += (W_WIDTH(curwin) - curwin_col_off()
7816 + ((curwin->w_p_wrap && i > 0)
7817 ? curwin_col_off2() : 0)) / 2;
7818 coladvance((colnr_T)i);
7819 if (flag)
7822 i = gchar_cursor();
7823 while (vim_iswhite(i) && oneright() == OK);
7825 curwin->w_set_curswant = TRUE;
7826 break;
7828 case '_':
7829 /* "g_": to the last non-blank character in the line or <count> lines
7830 * downward. */
7831 cap->oap->motion_type = MCHAR;
7832 cap->oap->inclusive = TRUE;
7833 curwin->w_curswant = MAXCOL;
7834 if (cursor_down((long)(cap->count1 - 1),
7835 cap->oap->op_type == OP_NOP) == FAIL)
7836 clearopbeep(cap->oap);
7837 else
7839 char_u *ptr = ml_get_curline();
7841 /* In Visual mode we may end up after the line. */
7842 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
7843 --curwin->w_cursor.col;
7845 /* Decrease the cursor column until it's on a non-blank. */
7846 while (curwin->w_cursor.col > 0
7847 && vim_iswhite(ptr[curwin->w_cursor.col]))
7848 --curwin->w_cursor.col;
7849 curwin->w_set_curswant = TRUE;
7851 break;
7853 case '$':
7854 case K_END:
7855 case K_KEND:
7857 int col_off = curwin_col_off();
7859 oap->motion_type = MCHAR;
7860 oap->inclusive = TRUE;
7861 if (curwin->w_p_wrap
7862 #ifdef FEAT_VERTSPLIT
7863 && curwin->w_width != 0
7864 #endif
7867 curwin->w_curswant = MAXCOL; /* so we stay at the end */
7868 if (cap->count1 == 1)
7870 int width1 = W_WIDTH(curwin) - col_off;
7871 int width2 = width1 + curwin_col_off2();
7873 validate_virtcol();
7874 i = width1 - 1;
7875 if (curwin->w_virtcol >= (colnr_T)width1)
7876 i += ((curwin->w_virtcol - width1) / width2 + 1)
7877 * width2;
7878 coladvance((colnr_T)i);
7879 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
7880 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
7883 * Check for landing on a character that got split at
7884 * the end of the line. We do not want to advance to
7885 * the next screen line.
7887 validate_virtcol();
7888 if (curwin->w_virtcol > (colnr_T)i)
7889 --curwin->w_cursor.col;
7891 #endif
7893 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
7894 clearopbeep(oap);
7896 else
7898 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
7899 coladvance((colnr_T)i);
7901 /* Make sure we stick in this column. */
7902 validate_virtcol();
7903 curwin->w_curswant = curwin->w_virtcol;
7904 curwin->w_set_curswant = FALSE;
7907 break;
7910 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
7912 case '*':
7913 case '#':
7914 #if POUND != '#'
7915 case POUND: /* pound sign (sometimes equal to '#') */
7916 #endif
7917 case Ctrl_RSB: /* :tag or :tselect for current identifier */
7918 case ']': /* :tselect for current identifier */
7919 nv_ident(cap);
7920 break;
7923 * ge and gE: go back to end of word
7925 case 'e':
7926 case 'E':
7927 oap->motion_type = MCHAR;
7928 curwin->w_set_curswant = TRUE;
7929 oap->inclusive = TRUE;
7930 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
7931 clearopbeep(oap);
7932 break;
7935 * "g CTRL-G": display info about cursor position
7937 case Ctrl_G:
7938 cursor_pos_info();
7939 break;
7942 * "gi": start Insert at the last position.
7944 case 'i':
7945 if (curbuf->b_last_insert.lnum != 0)
7947 curwin->w_cursor = curbuf->b_last_insert;
7948 check_cursor_lnum();
7949 i = (int)STRLEN(ml_get_curline());
7950 if (curwin->w_cursor.col > (colnr_T)i)
7952 #ifdef FEAT_VIRTUALEDIT
7953 if (virtual_active())
7954 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
7955 #endif
7956 curwin->w_cursor.col = i;
7959 cap->cmdchar = 'i';
7960 nv_edit(cap);
7961 break;
7964 * "gI": Start insert in column 1.
7966 case 'I':
7967 beginline(0);
7968 if (!checkclearopq(oap))
7969 invoke_edit(cap, FALSE, 'g', FALSE);
7970 break;
7972 #ifdef FEAT_SEARCHPATH
7974 * "gf": goto file, edit file under cursor
7975 * "]f" and "[f": can also be used.
7977 case 'f':
7978 case 'F':
7979 nv_gotofile(cap);
7980 break;
7981 #endif
7983 /* "g'm" and "g`m": jump to mark without setting pcmark */
7984 case '\'':
7985 cap->arg = TRUE;
7986 /*FALLTHROUGH*/
7987 case '`':
7988 nv_gomark(cap);
7989 break;
7992 * "gs": Goto sleep.
7994 case 's':
7995 do_sleep(cap->count1 * 1000L);
7996 break;
7999 * "ga": Display the ascii value of the character under the
8000 * cursor. It is displayed in decimal, hex, and octal. -- webb
8002 case 'a':
8003 do_ascii(NULL);
8004 break;
8006 #ifdef FEAT_MBYTE
8008 * "g8": Display the bytes used for the UTF-8 character under the
8009 * cursor. It is displayed in hex.
8010 * "8g8" finds illegal byte sequence.
8012 case '8':
8013 if (cap->count0 == 8)
8014 utf_find_illegal();
8015 else
8016 show_utf8();
8017 break;
8018 #endif
8020 case '<':
8021 show_sb_text();
8022 break;
8025 * "gg": Goto the first line in file. With a count it goes to
8026 * that line number like for "G". -- webb
8028 case 'g':
8029 cap->arg = FALSE;
8030 nv_goto(cap);
8031 break;
8034 * Two-character operators:
8035 * "gq" Format text
8036 * "gw" Format text and keep cursor position
8037 * "g~" Toggle the case of the text.
8038 * "gu" Change text to lower case.
8039 * "gU" Change text to upper case.
8040 * "g?" rot13 encoding
8041 * "g@" call 'operatorfunc'
8043 case 'q':
8044 case 'w':
8045 oap->cursor_start = curwin->w_cursor;
8046 /*FALLTHROUGH*/
8047 case '~':
8048 case 'u':
8049 case 'U':
8050 case '?':
8051 case '@':
8052 nv_operator(cap);
8053 break;
8056 * "gd": Find first occurrence of pattern under the cursor in the
8057 * current function
8058 * "gD": idem, but in the current file.
8060 case 'd':
8061 case 'D':
8062 nv_gd(oap, cap->nchar, (int)cap->count0);
8063 break;
8065 #ifdef FEAT_MOUSE
8067 * g<*Mouse> : <C-*mouse>
8069 case K_MIDDLEMOUSE:
8070 case K_MIDDLEDRAG:
8071 case K_MIDDLERELEASE:
8072 case K_LEFTMOUSE:
8073 case K_LEFTDRAG:
8074 case K_LEFTRELEASE:
8075 case K_RIGHTMOUSE:
8076 case K_RIGHTDRAG:
8077 case K_RIGHTRELEASE:
8078 case K_X1MOUSE:
8079 case K_X1DRAG:
8080 case K_X1RELEASE:
8081 case K_X2MOUSE:
8082 case K_X2DRAG:
8083 case K_X2RELEASE:
8084 mod_mask = MOD_MASK_CTRL;
8085 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8086 break;
8087 #endif
8089 case K_IGNORE:
8090 break;
8093 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8095 case 'p':
8096 case 'P':
8097 nv_put(cap);
8098 break;
8100 #ifdef FEAT_BYTEOFF
8101 /* "go": goto byte count from start of buffer */
8102 case 'o':
8103 goto_byte(cap->count0);
8104 break;
8105 #endif
8107 /* "gQ": improved Ex mode */
8108 case 'Q':
8109 if (text_locked())
8111 clearopbeep(cap->oap);
8112 text_locked_msg();
8113 break;
8116 if (!checkclearopq(oap))
8117 do_exmode(TRUE);
8118 break;
8120 #ifdef FEAT_JUMPLIST
8121 case ',':
8122 nv_pcmark(cap);
8123 break;
8125 case ';':
8126 cap->count1 = -cap->count1;
8127 nv_pcmark(cap);
8128 break;
8129 #endif
8131 #ifdef FEAT_WINDOWS
8132 case 't':
8133 goto_tabpage((int)cap->count0);
8134 break;
8135 case 'T':
8136 goto_tabpage(-(int)cap->count1);
8137 break;
8138 #endif
8140 case '+':
8141 case '-': /* "g+" and "g-": undo or redo along the timeline */
8142 if (!checkclearopq(oap))
8143 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
8144 FALSE, FALSE);
8145 break;
8147 default:
8148 clearopbeep(oap);
8149 break;
8154 * Handle "o" and "O" commands.
8156 static void
8157 n_opencmd(cap)
8158 cmdarg_T *cap;
8160 if (!checkclearopq(cap->oap))
8162 #ifdef FEAT_FOLDING
8163 if (cap->cmdchar == 'O')
8164 /* Open above the first line of a folded sequence of lines */
8165 (void)hasFolding(curwin->w_cursor.lnum,
8166 &curwin->w_cursor.lnum, NULL);
8167 else
8168 /* Open below the last line of a folded sequence of lines */
8169 (void)hasFolding(curwin->w_cursor.lnum,
8170 NULL, &curwin->w_cursor.lnum);
8171 #endif
8172 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8173 (cap->cmdchar == 'O' ? 1 : 0)),
8174 (linenr_T)(curwin->w_cursor.lnum +
8175 (cap->cmdchar == 'o' ? 1 : 0))
8176 ) == OK
8177 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8178 #ifdef FEAT_COMMENTS
8179 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8180 #endif
8181 0, 0))
8183 /* When '#' is in 'cpoptions' ignore the count. */
8184 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8185 cap->count1 = 1;
8186 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8192 * "." command: redo last change.
8194 static void
8195 nv_dot(cap)
8196 cmdarg_T *cap;
8198 if (!checkclearopq(cap->oap))
8201 * If "restart_edit" is TRUE, the last but one command is repeated
8202 * instead of the last command (inserting text). This is used for
8203 * CTRL-O <.> in insert mode.
8205 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8206 clearopbeep(cap->oap);
8211 * CTRL-R: undo undo
8213 static void
8214 nv_redo(cap)
8215 cmdarg_T *cap;
8217 if (!checkclearopq(cap->oap))
8219 u_redo((int)cap->count1);
8220 curwin->w_set_curswant = TRUE;
8225 * Handle "U" command.
8227 static void
8228 nv_Undo(cap)
8229 cmdarg_T *cap;
8231 /* In Visual mode and typing "gUU" triggers an operator */
8232 if (cap->oap->op_type == OP_UPPER
8233 #ifdef FEAT_VISUAL
8234 || VIsual_active
8235 #endif
8238 /* translate "gUU" to "gUgU" */
8239 cap->cmdchar = 'g';
8240 cap->nchar = 'U';
8241 nv_operator(cap);
8243 else if (!checkclearopq(cap->oap))
8245 u_undoline();
8246 curwin->w_set_curswant = TRUE;
8251 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8252 * single character.
8254 static void
8255 nv_tilde(cap)
8256 cmdarg_T *cap;
8258 if (!p_to
8259 #ifdef FEAT_VISUAL
8260 && !VIsual_active
8261 #endif
8262 && cap->oap->op_type != OP_TILDE)
8263 n_swapchar(cap);
8264 else
8265 nv_operator(cap);
8269 * Handle an operator command.
8270 * The actual work is done by do_pending_operator().
8272 static void
8273 nv_operator(cap)
8274 cmdarg_T *cap;
8276 int op_type;
8278 op_type = get_op_type(cap->cmdchar, cap->nchar);
8280 if (op_type == cap->oap->op_type) /* double operator works on lines */
8281 nv_lineop(cap);
8282 else if (!checkclearop(cap->oap))
8284 cap->oap->start = curwin->w_cursor;
8285 cap->oap->op_type = op_type;
8286 #ifdef FEAT_EVAL
8287 set_op_var(op_type);
8288 #endif
8292 #ifdef FEAT_EVAL
8294 * Set v:operator to the characters for "optype".
8296 static void
8297 set_op_var(optype)
8298 int optype;
8300 char_u opchars[3];
8302 if (optype == OP_NOP)
8303 set_vim_var_string(VV_OP, NULL, 0);
8304 else
8306 opchars[0] = get_op_char(optype);
8307 opchars[1] = get_extra_op_char(optype);
8308 opchars[2] = NUL;
8309 set_vim_var_string(VV_OP, opchars, -1);
8312 #endif
8315 * Handle linewise operator "dd", "yy", etc.
8317 * "_" is is a strange motion command that helps make operators more logical.
8318 * It is actually implemented, but not documented in the real Vi. This motion
8319 * command actually refers to "the current line". Commands like "dd" and "yy"
8320 * are really an alternate form of "d_" and "y_". It does accept a count, so
8321 * "d3_" works to delete 3 lines.
8323 static void
8324 nv_lineop(cap)
8325 cmdarg_T *cap;
8327 cap->oap->motion_type = MLINE;
8328 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8329 clearopbeep(cap->oap);
8330 else if ( cap->oap->op_type == OP_DELETE
8331 || cap->oap->op_type == OP_LSHIFT
8332 || cap->oap->op_type == OP_RSHIFT)
8333 beginline(BL_SOL | BL_FIX);
8334 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8335 beginline(BL_WHITE | BL_FIX);
8339 * <Home> command.
8341 static void
8342 nv_home(cap)
8343 cmdarg_T *cap;
8345 /* CTRL-HOME is like "gg" */
8346 if (mod_mask & MOD_MASK_CTRL)
8347 nv_goto(cap);
8348 else
8350 cap->count0 = 1;
8351 nv_pipe(cap);
8353 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8354 one-character line). */
8358 * "|" command.
8360 static void
8361 nv_pipe(cap)
8362 cmdarg_T *cap;
8364 cap->oap->motion_type = MCHAR;
8365 cap->oap->inclusive = FALSE;
8366 beginline(0);
8367 if (cap->count0 > 0)
8369 coladvance((colnr_T)(cap->count0 - 1));
8370 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8372 else
8373 curwin->w_curswant = 0;
8374 /* keep curswant at the column where we wanted to go, not where
8375 we ended; differs if line is too short */
8376 curwin->w_set_curswant = FALSE;
8380 * Handle back-word command "b" and "B".
8381 * cap->arg is 1 for "B"
8383 static void
8384 nv_bck_word(cap)
8385 cmdarg_T *cap;
8387 cap->oap->motion_type = MCHAR;
8388 cap->oap->inclusive = FALSE;
8389 curwin->w_set_curswant = TRUE;
8390 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8391 clearopbeep(cap->oap);
8392 #ifdef FEAT_FOLDING
8393 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8394 foldOpenCursor();
8395 #endif
8399 * Handle word motion commands "e", "E", "w" and "W".
8400 * cap->arg is TRUE for "E" and "W".
8402 static void
8403 nv_wordcmd(cap)
8404 cmdarg_T *cap;
8406 int n;
8407 int word_end;
8408 int flag = FALSE;
8409 pos_T startpos = curwin->w_cursor;
8412 * Set inclusive for the "E" and "e" command.
8414 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8415 word_end = TRUE;
8416 else
8417 word_end = FALSE;
8418 cap->oap->inclusive = word_end;
8421 * "cw" and "cW" are a special case.
8423 if (!word_end && cap->oap->op_type == OP_CHANGE)
8425 n = gchar_cursor();
8426 if (n != NUL) /* not an empty line */
8428 if (vim_iswhite(n))
8431 * Reproduce a funny Vi behaviour: "cw" on a blank only
8432 * changes one character, not all blanks until the start of
8433 * the next word. Only do this when the 'w' flag is included
8434 * in 'cpoptions'.
8436 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8438 cap->oap->inclusive = TRUE;
8439 cap->oap->motion_type = MCHAR;
8440 return;
8443 else
8446 * This is a little strange. To match what the real Vi does,
8447 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8448 * that we are not on a space or a TAB. This seems impolite
8449 * at first, but it's really more what we mean when we say
8450 * 'cw'.
8451 * Another strangeness: When standing on the end of a word
8452 * "ce" will change until the end of the next wordt, but "cw"
8453 * will change only one character! This is done by setting
8454 * flag.
8456 cap->oap->inclusive = TRUE;
8457 word_end = TRUE;
8458 flag = TRUE;
8463 cap->oap->motion_type = MCHAR;
8464 curwin->w_set_curswant = TRUE;
8465 if (word_end)
8466 n = end_word(cap->count1, cap->arg, flag, FALSE);
8467 else
8468 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8470 /* Don't leave the cursor on the NUL past the end of line. Unless we
8471 * didn't move it forward. */
8472 if (lt(startpos, curwin->w_cursor))
8473 adjust_cursor(cap->oap);
8475 if (n == FAIL && cap->oap->op_type == OP_NOP)
8476 clearopbeep(cap->oap);
8477 else
8479 #ifdef FEAT_VISUAL
8480 adjust_for_sel(cap);
8481 #endif
8482 #ifdef FEAT_FOLDING
8483 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8484 foldOpenCursor();
8485 #endif
8490 * Used after a movement command: If the cursor ends up on the NUL after the
8491 * end of the line, may move it back to the last character and make the motion
8492 * inclusive.
8494 static void
8495 adjust_cursor(oap)
8496 oparg_T *oap;
8498 /* The cursor cannot remain on the NUL when:
8499 * - the column is > 0
8500 * - not in Visual mode or 'selection' is "o"
8501 * - 'virtualedit' is not "all" and not "onemore".
8503 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
8504 #ifdef FEAT_VISUAL
8505 && (!VIsual_active || *p_sel == 'o')
8506 #endif
8507 #ifdef FEAT_VIRTUALEDIT
8508 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8509 #endif
8512 --curwin->w_cursor.col;
8513 #ifdef FEAT_MBYTE
8514 /* prevent cursor from moving on the trail byte */
8515 if (has_mbyte)
8516 mb_adjust_cursor();
8517 #endif
8518 oap->inclusive = TRUE;
8523 * "0" and "^" commands.
8524 * cap->arg is the argument for beginline().
8526 static void
8527 nv_beginline(cap)
8528 cmdarg_T *cap;
8530 cap->oap->motion_type = MCHAR;
8531 cap->oap->inclusive = FALSE;
8532 beginline(cap->arg);
8533 #ifdef FEAT_FOLDING
8534 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8535 foldOpenCursor();
8536 #endif
8537 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8538 one-character line). */
8541 #ifdef FEAT_VISUAL
8543 * In exclusive Visual mode, may include the last character.
8545 static void
8546 adjust_for_sel(cap)
8547 cmdarg_T *cap;
8549 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8550 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8552 # ifdef FEAT_MBYTE
8553 if (has_mbyte)
8554 inc_cursor();
8555 else
8556 # endif
8557 ++curwin->w_cursor.col;
8558 cap->oap->inclusive = FALSE;
8563 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8564 * Should check VIsual_mode before calling this.
8565 * Returns TRUE when backed up to the previous line.
8567 static int
8568 unadjust_for_sel()
8570 pos_T *pp;
8572 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8574 if (lt(VIsual, curwin->w_cursor))
8575 pp = &curwin->w_cursor;
8576 else
8577 pp = &VIsual;
8578 #ifdef FEAT_VIRTUALEDIT
8579 if (pp->coladd > 0)
8580 --pp->coladd;
8581 else
8582 #endif
8583 if (pp->col > 0)
8585 --pp->col;
8586 #ifdef FEAT_MBYTE
8587 mb_adjustpos(pp);
8588 #endif
8590 else if (pp->lnum > 1)
8592 --pp->lnum;
8593 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8594 return TRUE;
8597 return FALSE;
8601 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8603 static void
8604 nv_select(cap)
8605 cmdarg_T *cap;
8607 if (VIsual_active)
8608 VIsual_select = TRUE;
8609 else if (VIsual_reselect)
8611 cap->nchar = 'v'; /* fake "gv" command */
8612 cap->arg = TRUE;
8613 nv_g_cmd(cap);
8617 #endif
8620 * "G", "gg", CTRL-END, CTRL-HOME.
8621 * cap->arg is TRUE for "G".
8623 static void
8624 nv_goto(cap)
8625 cmdarg_T *cap;
8627 linenr_T lnum;
8629 if (cap->arg)
8630 lnum = curbuf->b_ml.ml_line_count;
8631 else
8632 lnum = 1L;
8633 cap->oap->motion_type = MLINE;
8634 setpcmark();
8636 /* When a count is given, use it instead of the default lnum */
8637 if (cap->count0 != 0)
8638 lnum = cap->count0;
8639 if (lnum < 1L)
8640 lnum = 1L;
8641 else if (lnum > curbuf->b_ml.ml_line_count)
8642 lnum = curbuf->b_ml.ml_line_count;
8643 curwin->w_cursor.lnum = lnum;
8644 beginline(BL_SOL | BL_FIX);
8645 #ifdef FEAT_FOLDING
8646 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8647 foldOpenCursor();
8648 #endif
8652 * CTRL-\ in Normal mode.
8654 static void
8655 nv_normal(cap)
8656 cmdarg_T *cap;
8658 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8660 clearop(cap->oap);
8661 if (restart_edit != 0 && mode_displayed)
8662 clear_cmdline = TRUE; /* unshow mode later */
8663 restart_edit = 0;
8664 #ifdef FEAT_CMDWIN
8665 if (cmdwin_type != 0)
8666 cmdwin_result = Ctrl_C;
8667 #endif
8668 #ifdef FEAT_VISUAL
8669 if (VIsual_active)
8671 end_visual_mode(); /* stop Visual */
8672 redraw_curbuf_later(INVERTED);
8674 #endif
8675 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8676 if (cap->nchar == Ctrl_G && p_im)
8677 restart_edit = 'a';
8679 else
8680 clearopbeep(cap->oap);
8684 * ESC in Normal mode: beep, but don't flush buffers.
8685 * Don't even beep if we are canceling a command.
8687 static void
8688 nv_esc(cap)
8689 cmdarg_T *cap;
8691 int no_reason;
8693 no_reason = (cap->oap->op_type == OP_NOP
8694 && cap->opcount == 0
8695 && cap->count0 == 0
8696 && cap->oap->regname == 0
8697 && !p_im);
8699 if (cap->arg) /* TRUE for CTRL-C */
8701 if (restart_edit == 0
8702 #ifdef FEAT_CMDWIN
8703 && cmdwin_type == 0
8704 #endif
8705 #ifdef FEAT_VISUAL
8706 && !VIsual_active
8707 #endif
8708 && no_reason)
8709 MSG(_("Type :quit<Enter> to exit Vim"));
8711 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8712 * set again below when halfway a mapping. */
8713 if (!p_im)
8714 restart_edit = 0;
8715 #ifdef FEAT_CMDWIN
8716 if (cmdwin_type != 0)
8718 cmdwin_result = K_IGNORE;
8719 got_int = FALSE; /* don't stop executing autocommands et al. */
8720 return;
8722 #endif
8725 #ifdef FEAT_VISUAL
8726 if (VIsual_active)
8728 end_visual_mode(); /* stop Visual */
8729 check_cursor_col(); /* make sure cursor is not beyond EOL */
8730 curwin->w_set_curswant = TRUE;
8731 redraw_curbuf_later(INVERTED);
8733 else
8734 #endif
8735 if (no_reason)
8736 vim_beep();
8737 clearop(cap->oap);
8739 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8740 * set return to Insert mode afterwards. */
8741 if (restart_edit == 0 && goto_im()
8742 #ifdef FEAT_EX_EXTRA
8743 && ex_normal_busy == 0
8744 #endif
8746 restart_edit = 'a';
8750 * Handle "A", "a", "I", "i" and <Insert> commands.
8752 static void
8753 nv_edit(cap)
8754 cmdarg_T *cap;
8756 /* <Insert> is equal to "i" */
8757 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8758 cap->cmdchar = 'i';
8760 #ifdef FEAT_VISUAL
8761 /* in Visual mode "A" and "I" are an operator */
8762 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8763 v_visop(cap);
8765 /* in Visual mode and after an operator "a" and "i" are for text objects */
8766 else
8767 #endif
8768 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8769 && (cap->oap->op_type != OP_NOP
8770 #ifdef FEAT_VISUAL
8771 || VIsual_active
8772 #endif
8775 #ifdef FEAT_TEXTOBJ
8776 nv_object(cap);
8777 #else
8778 clearopbeep(cap->oap);
8779 #endif
8781 else if (!curbuf->b_p_ma && !p_im)
8783 /* Only give this error when 'insertmode' is off. */
8784 EMSG(_(e_modifiable));
8785 clearop(cap->oap);
8787 else if (!checkclearopq(cap->oap))
8789 switch (cap->cmdchar)
8791 case 'A': /* "A"ppend after the line */
8792 curwin->w_set_curswant = TRUE;
8793 #ifdef FEAT_VIRTUALEDIT
8794 if (ve_flags == VE_ALL)
8796 int save_State = State;
8798 /* Pretent Insert mode here to allow the cursor on the
8799 * character past the end of the line */
8800 State = INSERT;
8801 coladvance((colnr_T)MAXCOL);
8802 State = save_State;
8804 else
8805 #endif
8806 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8807 break;
8809 case 'I': /* "I"nsert before the first non-blank */
8810 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
8811 beginline(BL_WHITE);
8812 else
8813 beginline(BL_WHITE|BL_FIX);
8814 break;
8816 case 'a': /* "a"ppend is like "i"nsert on the next character. */
8817 #ifdef FEAT_VIRTUALEDIT
8818 /* increment coladd when in virtual space, increment the
8819 * column otherwise, also to append after an unprintable char */
8820 if (virtual_active()
8821 && (curwin->w_cursor.coladd > 0
8822 || *ml_get_cursor() == NUL
8823 || *ml_get_cursor() == TAB))
8824 curwin->w_cursor.coladd++;
8825 else
8826 #endif
8827 if (*ml_get_cursor() != NUL)
8828 inc_cursor();
8829 break;
8832 #ifdef FEAT_VIRTUALEDIT
8833 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
8835 int save_State = State;
8837 /* Pretent Insert mode here to allow the cursor on the
8838 * character past the end of the line */
8839 State = INSERT;
8840 coladvance(getviscol());
8841 State = save_State;
8843 #endif
8845 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
8850 * Invoke edit() and take care of "restart_edit" and the return value.
8852 static void
8853 invoke_edit(cap, repl, cmd, startln)
8854 cmdarg_T *cap;
8855 int repl; /* "r" or "gr" command */
8856 int cmd;
8857 int startln;
8859 int restart_edit_save = 0;
8861 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
8862 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
8863 * it. */
8864 if (repl || !stuff_empty())
8865 restart_edit_save = restart_edit;
8866 else
8867 restart_edit_save = 0;
8869 /* Always reset "restart_edit", this is not a restarted edit. */
8870 restart_edit = 0;
8872 if (edit(cmd, startln, cap->count1))
8873 cap->retval |= CA_COMMAND_BUSY;
8875 if (restart_edit == 0)
8876 restart_edit = restart_edit_save;
8879 #ifdef FEAT_TEXTOBJ
8881 * "a" or "i" while an operator is pending or in Visual mode: object motion.
8883 static void
8884 nv_object(cap)
8885 cmdarg_T *cap;
8887 int flag;
8888 int include;
8889 char_u *mps_save;
8891 if (cap->cmdchar == 'i')
8892 include = FALSE; /* "ix" = inner object: exclude white space */
8893 else
8894 include = TRUE; /* "ax" = an object: include white space */
8896 /* Make sure (), [], {} and <> are in 'matchpairs' */
8897 mps_save = curbuf->b_p_mps;
8898 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
8900 switch (cap->nchar)
8902 case 'w': /* "aw" = a word */
8903 flag = current_word(cap->oap, cap->count1, include, FALSE);
8904 break;
8905 case 'W': /* "aW" = a WORD */
8906 flag = current_word(cap->oap, cap->count1, include, TRUE);
8907 break;
8908 case 'b': /* "ab" = a braces block */
8909 case '(':
8910 case ')':
8911 flag = current_block(cap->oap, cap->count1, include, '(', ')');
8912 break;
8913 case 'B': /* "aB" = a Brackets block */
8914 case '{':
8915 case '}':
8916 flag = current_block(cap->oap, cap->count1, include, '{', '}');
8917 break;
8918 case '[': /* "a[" = a [] block */
8919 case ']':
8920 flag = current_block(cap->oap, cap->count1, include, '[', ']');
8921 break;
8922 case '<': /* "a<" = a <> block */
8923 case '>':
8924 flag = current_block(cap->oap, cap->count1, include, '<', '>');
8925 break;
8926 case 't': /* "at" = a tag block (xml and html) */
8927 flag = current_tagblock(cap->oap, cap->count1, include);
8928 break;
8929 case 'p': /* "ap" = a paragraph */
8930 flag = current_par(cap->oap, cap->count1, include, 'p');
8931 break;
8932 case 's': /* "as" = a sentence */
8933 flag = current_sent(cap->oap, cap->count1, include);
8934 break;
8935 case '"': /* "a"" = a double quoted string */
8936 case '\'': /* "a'" = a single quoted string */
8937 case '`': /* "a`" = a backtick quoted string */
8938 flag = current_quote(cap->oap, cap->count1, include,
8939 cap->nchar);
8940 break;
8941 #if 0 /* TODO */
8942 case 'S': /* "aS" = a section */
8943 case 'f': /* "af" = a filename */
8944 case 'u': /* "au" = a URL */
8945 #endif
8946 default:
8947 flag = FAIL;
8948 break;
8951 curbuf->b_p_mps = mps_save;
8952 if (flag == FAIL)
8953 clearopbeep(cap->oap);
8954 adjust_cursor_col();
8955 curwin->w_set_curswant = TRUE;
8957 #endif
8960 * "q" command: Start/stop recording.
8961 * "q:", "q/", "q?": edit command-line in command-line window.
8963 static void
8964 nv_record(cap)
8965 cmdarg_T *cap;
8967 if (cap->oap->op_type == OP_FORMAT)
8969 /* "gqq" is the same as "gqgq": format line */
8970 cap->cmdchar = 'g';
8971 cap->nchar = 'q';
8972 nv_operator(cap);
8974 else if (!checkclearop(cap->oap))
8976 #ifdef FEAT_CMDWIN
8977 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
8979 stuffcharReadbuff(cap->nchar);
8980 stuffcharReadbuff(K_CMDWIN);
8982 else
8983 #endif
8984 /* (stop) recording into a named register, unless executing a
8985 * register */
8986 if (!Exec_reg && do_record(cap->nchar) == FAIL)
8987 clearopbeep(cap->oap);
8992 * Handle the "@r" command.
8994 static void
8995 nv_at(cap)
8996 cmdarg_T *cap;
8998 if (checkclearop(cap->oap))
8999 return;
9000 #ifdef FEAT_EVAL
9001 if (cap->nchar == '=')
9003 if (get_expr_register() == NUL)
9004 return;
9006 #endif
9007 while (cap->count1-- && !got_int)
9009 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
9011 clearopbeep(cap->oap);
9012 break;
9014 line_breakcheck();
9019 * Handle the CTRL-U and CTRL-D commands.
9021 static void
9022 nv_halfpage(cap)
9023 cmdarg_T *cap;
9025 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9026 || (cap->cmdchar == Ctrl_D
9027 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9028 clearopbeep(cap->oap);
9029 else if (!checkclearop(cap->oap))
9030 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9034 * Handle "J" or "gJ" command.
9036 static void
9037 nv_join(cap)
9038 cmdarg_T *cap;
9040 #ifdef FEAT_VISUAL
9041 if (VIsual_active) /* join the visual lines */
9042 nv_operator(cap);
9043 else
9044 #endif
9045 if (!checkclearop(cap->oap))
9047 if (cap->count0 <= 1)
9048 cap->count0 = 2; /* default for join is two lines! */
9049 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9050 curbuf->b_ml.ml_line_count)
9051 clearopbeep(cap->oap); /* beyond last line */
9052 else
9054 prep_redo(cap->oap->regname, cap->count0,
9055 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9056 do_do_join(cap->count0, cap->nchar == NUL);
9062 * "P", "gP", "p" and "gp" commands.
9064 static void
9065 nv_put(cap)
9066 cmdarg_T *cap;
9068 #ifdef FEAT_VISUAL
9069 int regname = 0;
9070 void *reg1 = NULL, *reg2 = NULL;
9071 int empty = FALSE;
9072 int was_visual = FALSE;
9073 #endif
9074 int dir;
9075 int flags = 0;
9077 if (cap->oap->op_type != OP_NOP)
9079 #ifdef FEAT_DIFF
9080 /* "dp" is ":diffput" */
9081 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9083 clearop(cap->oap);
9084 nv_diffgetput(TRUE);
9086 else
9087 #endif
9088 clearopbeep(cap->oap);
9090 else
9092 dir = (cap->cmdchar == 'P'
9093 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9094 ? BACKWARD : FORWARD;
9095 prep_redo_cmd(cap);
9096 if (cap->cmdchar == 'g')
9097 flags |= PUT_CURSEND;
9099 #ifdef FEAT_VISUAL
9100 if (VIsual_active)
9102 /* Putting in Visual mode: The put text replaces the selected
9103 * text. First delete the selected text, then put the new text.
9104 * Need to save and restore the registers that the delete
9105 * overwrites if the old contents is being put.
9107 was_visual = TRUE;
9108 regname = cap->oap->regname;
9109 # ifdef FEAT_CLIPBOARD
9110 adjust_clip_reg(&regname);
9111 # endif
9112 if (regname == 0 || VIM_ISDIGIT(regname)
9113 # ifdef FEAT_CLIPBOARD
9114 || (clip_unnamed && (regname == '*' || regname == '+'))
9115 # endif
9119 /* the delete is going to overwrite the register we want to
9120 * put, save it first. */
9121 reg1 = get_register(regname, TRUE);
9124 /* Now delete the selected text. */
9125 cap->cmdchar = 'd';
9126 cap->nchar = NUL;
9127 cap->oap->regname = NUL;
9128 nv_operator(cap);
9129 do_pending_operator(cap, 0, FALSE);
9130 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
9132 /* delete PUT_LINE_BACKWARD; */
9133 cap->oap->regname = regname;
9135 if (reg1 != NULL)
9137 /* Delete probably changed the register we want to put, save
9138 * it first. Then put back what was there before the delete. */
9139 reg2 = get_register(regname, FALSE);
9140 put_register(regname, reg1);
9143 /* When deleted a linewise Visual area, put the register as
9144 * lines to avoid it joined with the next line. When deletion was
9145 * characterwise, split a line when putting lines. */
9146 if (VIsual_mode == 'V')
9147 flags |= PUT_LINE;
9148 else if (VIsual_mode == 'v')
9149 flags |= PUT_LINE_SPLIT;
9150 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9151 flags |= PUT_LINE_FORWARD;
9152 dir = BACKWARD;
9153 if ((VIsual_mode != 'V'
9154 && curwin->w_cursor.col < curbuf->b_op_start.col)
9155 || (VIsual_mode == 'V'
9156 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9157 /* cursor is at the end of the line or end of file, put
9158 * forward. */
9159 dir = FORWARD;
9161 #endif
9162 do_put(cap->oap->regname, dir, cap->count1, flags);
9164 #ifdef FEAT_VISUAL
9165 /* If a register was saved, put it back now. */
9166 if (reg2 != NULL)
9167 put_register(regname, reg2);
9169 /* What to reselect with "gv"? Selecting the just put text seems to
9170 * be the most useful, since the original text was removed. */
9171 if (was_visual)
9173 curbuf->b_visual.vi_start = curbuf->b_op_start;
9174 curbuf->b_visual.vi_end = curbuf->b_op_end;
9177 /* When all lines were selected and deleted do_put() leaves an empty
9178 * line that needs to be deleted now. */
9179 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
9181 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
9183 /* If the cursor was in that line, move it to the end of the last
9184 * line. */
9185 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9187 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9188 coladvance((colnr_T)MAXCOL);
9191 #endif
9192 auto_format(FALSE, TRUE);
9197 * "o" and "O" commands.
9199 static void
9200 nv_open(cap)
9201 cmdarg_T *cap;
9203 #ifdef FEAT_DIFF
9204 /* "do" is ":diffget" */
9205 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9207 clearop(cap->oap);
9208 nv_diffgetput(FALSE);
9210 else
9211 #endif
9212 #ifdef FEAT_VISUAL
9213 if (VIsual_active) /* switch start and end of visual */
9214 v_swap_corners(cap->cmdchar);
9215 else
9216 #endif
9217 n_opencmd(cap);
9220 #ifdef FEAT_SNIFF
9221 /*ARGSUSED*/
9222 static void
9223 nv_sniff(cap)
9224 cmdarg_T *cap;
9226 ProcessSniffRequests();
9228 #endif
9230 #ifdef FEAT_NETBEANS_INTG
9231 static void
9232 nv_nbcmd(cap)
9233 cmdarg_T *cap;
9235 netbeans_keycommand(cap->nchar);
9237 #endif
9239 #ifdef FEAT_DND
9240 /*ARGSUSED*/
9241 static void
9242 nv_drop(cap)
9243 cmdarg_T *cap;
9245 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9247 #endif
9249 #ifdef FEAT_AUTOCMD
9251 * Trigger CursorHold event.
9252 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9253 * input buffer. "did_cursorhold" is set to avoid retriggering.
9255 /*ARGSUSED*/
9256 static void
9257 nv_cursorhold(cap)
9258 cmdarg_T *cap;
9260 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9261 did_cursorhold = TRUE;
9262 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
9264 #endif