Enable basic AppleScript support
[MacVim.git] / src / normal.c
blob15dfe24cf8285dbd1bde846883ab0ff3668a28bd
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 did_cursorhold = FALSE;
1136 #endif
1138 State = NORMAL;
1140 if (ca.nchar == ESC)
1142 clearop(oap);
1143 if (restart_edit == 0 && goto_im())
1144 restart_edit = 'a';
1145 goto normal_end;
1148 if (ca.cmdchar != K_IGNORE)
1150 msg_didout = FALSE; /* don't scroll screen up for normal command */
1151 msg_col = 0;
1154 #ifdef FEAT_VISUAL
1155 old_pos = curwin->w_cursor; /* remember where cursor was */
1157 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1158 * mode. */
1159 if (!VIsual_active && km_startsel)
1161 if (nv_cmds[idx].cmd_flags & NV_SS)
1163 start_selection();
1164 unshift_special(&ca);
1165 idx = find_command(ca.cmdchar);
1167 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1168 && (mod_mask & MOD_MASK_SHIFT))
1170 start_selection();
1171 mod_mask &= ~MOD_MASK_SHIFT;
1174 #endif
1177 * Execute the command!
1178 * Call the command function found in the commands table.
1180 ca.arg = nv_cmds[idx].cmd_arg;
1181 (nv_cmds[idx].cmd_func)(&ca);
1184 * If we didn't start or finish an operator, reset oap->regname, unless we
1185 * need it later.
1187 if (!finish_op
1188 && !oap->op_type
1189 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1191 clearop(oap);
1192 #ifdef FEAT_EVAL
1193 set_reg_var('"');
1194 #endif
1197 #ifdef FEAT_VISUAL
1198 /* Get the length of mapped chars again after typing a count, second
1199 * character or "z333<cr>". */
1200 if (old_mapped_len > 0)
1201 old_mapped_len = typebuf_maplen();
1202 #endif
1205 * If an operation is pending, handle it...
1207 do_pending_operator(&ca, old_col, FALSE);
1210 * Wait for a moment when a message is displayed that will be overwritten
1211 * by the mode message.
1212 * In Visual mode and with "^O" in Insert mode, a short message will be
1213 * overwritten by the mode message. Wait a bit, until a key is hit.
1214 * In Visual mode, it's more important to keep the Visual area updated
1215 * than keeping a message (e.g. from a /pat search).
1216 * Only do this if the command was typed, not from a mapping.
1217 * Don't wait when emsg_silent is non-zero.
1218 * Also wait a bit after an error message, e.g. for "^O:".
1219 * Don't redraw the screen, it would remove the message.
1221 if ( ((p_smd
1222 && msg_silent == 0
1223 && (restart_edit != 0
1224 #ifdef FEAT_VISUAL
1225 || (VIsual_active
1226 && old_pos.lnum == curwin->w_cursor.lnum
1227 && old_pos.col == curwin->w_cursor.col)
1228 #endif
1230 && (clear_cmdline
1231 || redraw_cmdline)
1232 && (msg_didout || (msg_didany && msg_scroll))
1233 && !msg_nowait
1234 && KeyTyped)
1235 || (restart_edit != 0
1236 #ifdef FEAT_VISUAL
1237 && !VIsual_active
1238 #endif
1239 && (msg_scroll
1240 || emsg_on_display)))
1241 && oap->regname == 0
1242 && !(ca.retval & CA_COMMAND_BUSY)
1243 && stuff_empty()
1244 && typebuf_typed()
1245 && emsg_silent == 0
1246 && !did_wait_return
1247 && oap->op_type == OP_NOP)
1249 int save_State = State;
1251 /* Draw the cursor with the right shape here */
1252 if (restart_edit != 0)
1253 State = INSERT;
1255 /* If need to redraw, and there is a "keep_msg", redraw before the
1256 * delay */
1257 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1259 char_u *kmsg;
1261 kmsg = keep_msg;
1262 keep_msg = NULL;
1263 /* showmode() will clear keep_msg, but we want to use it anyway */
1264 update_screen(0);
1265 /* now reset it, otherwise it's put in the history again */
1266 keep_msg = kmsg;
1267 msg_attr(kmsg, keep_msg_attr);
1268 vim_free(kmsg);
1270 setcursor();
1271 cursor_on();
1272 out_flush();
1273 if (msg_scroll || emsg_on_display)
1274 ui_delay(1000L, TRUE); /* wait at least one second */
1275 ui_delay(3000L, FALSE); /* wait up to three seconds */
1276 State = save_State;
1278 msg_scroll = FALSE;
1279 emsg_on_display = FALSE;
1283 * Finish up after executing a Normal mode command.
1285 normal_end:
1287 msg_nowait = FALSE;
1289 /* Reset finish_op, in case it was set */
1290 #ifdef CURSOR_SHAPE
1291 c = finish_op;
1292 #endif
1293 finish_op = FALSE;
1294 #ifdef CURSOR_SHAPE
1295 /* Redraw the cursor with another shape, if we were in Operator-pending
1296 * mode or did a replace command. */
1297 if (c || ca.cmdchar == 'r')
1299 ui_cursor_shape(); /* may show different cursor shape */
1300 # ifdef FEAT_MOUSESHAPE
1301 update_mouseshape(-1);
1302 # endif
1304 #endif
1306 #ifdef FEAT_CMDL_INFO
1307 if (oap->op_type == OP_NOP && oap->regname == 0
1308 # ifdef FEAT_AUTOCMD
1309 && ca.cmdchar != K_CURSORHOLD
1310 # endif
1312 clear_showcmd();
1313 #endif
1315 checkpcmark(); /* check if we moved since setting pcmark */
1316 vim_free(ca.searchbuf);
1318 #ifdef FEAT_MBYTE
1319 if (has_mbyte)
1320 mb_adjust_cursor();
1321 #endif
1323 #ifdef FEAT_SCROLLBIND
1324 if (curwin->w_p_scb && toplevel)
1326 validate_cursor(); /* may need to update w_leftcol */
1327 do_check_scrollbind(TRUE);
1329 #endif
1332 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1333 * if still inside a mapping that started in Visual mode).
1334 * May switch from Visual to Select mode after CTRL-O command.
1336 if ( oap->op_type == OP_NOP
1337 #ifdef FEAT_VISUAL
1338 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1339 || restart_VIsual_select == 1)
1340 #else
1341 && restart_edit != 0
1342 #endif
1343 && !(ca.retval & CA_COMMAND_BUSY)
1344 && stuff_empty()
1345 && oap->regname == 0)
1347 #ifdef FEAT_VISUAL
1348 if (restart_VIsual_select == 1)
1350 VIsual_select = TRUE;
1351 showmode();
1352 restart_VIsual_select = 0;
1354 #endif
1355 if (restart_edit != 0
1356 #ifdef FEAT_VISUAL
1357 && !VIsual_active && old_mapped_len == 0
1358 #endif
1360 (void)edit(restart_edit, FALSE, 1L);
1363 #ifdef FEAT_VISUAL
1364 if (restart_VIsual_select == 2)
1365 restart_VIsual_select = 1;
1366 #endif
1368 /* Save count before an operator for next time. */
1369 opcount = ca.opcount;
1373 * Handle an operator after visual mode or when the movement is finished
1375 void
1376 do_pending_operator(cap, old_col, gui_yank)
1377 cmdarg_T *cap;
1378 int old_col;
1379 int gui_yank;
1381 oparg_T *oap = cap->oap;
1382 pos_T old_cursor;
1383 int empty_region_error;
1384 int restart_edit_save;
1386 #ifdef FEAT_VISUAL
1387 /* The visual area is remembered for redo */
1388 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1389 static linenr_T redo_VIsual_line_count; /* number of lines */
1390 static colnr_T redo_VIsual_col; /* number of cols or end column */
1391 static long redo_VIsual_count; /* count for Visual operator */
1392 # ifdef FEAT_VIRTUALEDIT
1393 int include_line_break = FALSE;
1394 # endif
1395 #endif
1397 #if defined(FEAT_CLIPBOARD)
1399 * Yank the visual area into the GUI selection register before we operate
1400 * on it and lose it forever.
1401 * Don't do it if a specific register was specified, so that ""x"*P works.
1402 * This could call do_pending_operator() recursively, but that's OK
1403 * because gui_yank will be TRUE for the nested call.
1405 if (clip_star.available
1406 && oap->op_type != OP_NOP
1407 && !gui_yank
1408 # ifdef FEAT_VISUAL
1409 && VIsual_active
1410 && !redo_VIsual_busy
1411 # endif
1412 && oap->regname == 0)
1413 clip_auto_select();
1414 #endif
1415 old_cursor = curwin->w_cursor;
1418 * If an operation is pending, handle it...
1420 if ((finish_op
1421 #ifdef FEAT_VISUAL
1422 || VIsual_active
1423 #endif
1424 ) && oap->op_type != OP_NOP)
1426 #ifdef FEAT_VISUAL
1427 oap->is_VIsual = VIsual_active;
1428 if (oap->motion_force == 'V')
1429 oap->motion_type = MLINE;
1430 else if (oap->motion_force == 'v')
1432 /* If the motion was linewise, "inclusive" will not have been set.
1433 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1434 if (oap->motion_type == MLINE)
1435 oap->inclusive = FALSE;
1436 /* If the motion already was characterwise, toggle "inclusive" */
1437 else if (oap->motion_type == MCHAR)
1438 oap->inclusive = !oap->inclusive;
1439 oap->motion_type = MCHAR;
1441 else if (oap->motion_force == Ctrl_V)
1443 /* Change line- or characterwise motion into Visual block mode. */
1444 VIsual_active = TRUE;
1445 VIsual = oap->start;
1446 VIsual_mode = Ctrl_V;
1447 VIsual_select = FALSE;
1448 VIsual_reselect = FALSE;
1450 #endif
1452 /* only redo yank when 'y' flag is in 'cpoptions' */
1453 /* never redo "zf" (define fold) */
1454 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1455 #ifdef FEAT_VISUAL
1456 && (!VIsual_active || oap->motion_force)
1457 #endif
1458 && cap->cmdchar != 'D'
1459 #ifdef FEAT_FOLDING
1460 && oap->op_type != OP_FOLD
1461 && oap->op_type != OP_FOLDOPEN
1462 && oap->op_type != OP_FOLDOPENREC
1463 && oap->op_type != OP_FOLDCLOSE
1464 && oap->op_type != OP_FOLDCLOSEREC
1465 && oap->op_type != OP_FOLDDEL
1466 && oap->op_type != OP_FOLDDELREC
1467 #endif
1470 prep_redo(oap->regname, cap->count0,
1471 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1472 oap->motion_force, cap->cmdchar, cap->nchar);
1473 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1476 * If 'cpoptions' does not contain 'r', insert the search
1477 * pattern to really repeat the same command.
1479 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
1480 AppendToRedobuffLit(cap->searchbuf, -1);
1481 AppendToRedobuff(NL_STR);
1483 else if (cap->cmdchar == ':')
1485 /* do_cmdline() has stored the first typed line in
1486 * "repeat_cmdline". When several lines are typed repeating
1487 * won't be possible. */
1488 if (repeat_cmdline == NULL)
1489 ResetRedobuff();
1490 else
1492 AppendToRedobuffLit(repeat_cmdline, -1);
1493 AppendToRedobuff(NL_STR);
1494 vim_free(repeat_cmdline);
1495 repeat_cmdline = NULL;
1500 #ifdef FEAT_VISUAL
1501 if (redo_VIsual_busy)
1503 oap->start = curwin->w_cursor;
1504 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1505 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1506 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1507 VIsual_mode = redo_VIsual_mode;
1508 if (VIsual_mode == 'v')
1510 if (redo_VIsual_line_count <= 1)
1511 curwin->w_cursor.col += redo_VIsual_col - 1;
1512 else
1513 curwin->w_cursor.col = redo_VIsual_col;
1515 if (redo_VIsual_col == MAXCOL)
1517 curwin->w_curswant = MAXCOL;
1518 coladvance((colnr_T)MAXCOL);
1520 cap->count0 = redo_VIsual_count;
1521 if (redo_VIsual_count != 0)
1522 cap->count1 = redo_VIsual_count;
1523 else
1524 cap->count1 = 1;
1526 else if (VIsual_active)
1528 if (!gui_yank)
1530 /* Save the current VIsual area for '< and '> marks, and "gv" */
1531 curbuf->b_visual.vi_start = VIsual;
1532 curbuf->b_visual.vi_end = curwin->w_cursor;
1533 curbuf->b_visual.vi_mode = VIsual_mode;
1534 curbuf->b_visual.vi_curswant = curwin->w_curswant;
1535 # ifdef FEAT_EVAL
1536 curbuf->b_visual_mode_eval = VIsual_mode;
1537 # endif
1540 /* In Select mode, a linewise selection is operated upon like a
1541 * characterwise selection. */
1542 if (VIsual_select && VIsual_mode == 'V')
1544 if (lt(VIsual, curwin->w_cursor))
1546 VIsual.col = 0;
1547 curwin->w_cursor.col =
1548 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1550 else
1552 curwin->w_cursor.col = 0;
1553 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1555 VIsual_mode = 'v';
1557 /* If 'selection' is "exclusive", backup one character for
1558 * charwise selections. */
1559 else if (VIsual_mode == 'v')
1561 # ifdef FEAT_VIRTUALEDIT
1562 include_line_break =
1563 # endif
1564 unadjust_for_sel();
1567 oap->start = VIsual;
1568 if (VIsual_mode == 'V')
1569 oap->start.col = 0;
1571 #endif /* FEAT_VISUAL */
1574 * Set oap->start to the first position of the operated text, oap->end
1575 * to the end of the operated text. w_cursor is equal to oap->start.
1577 if (lt(oap->start, curwin->w_cursor))
1579 #ifdef FEAT_FOLDING
1580 /* Include folded lines completely. */
1581 if (!VIsual_active)
1583 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1584 oap->start.col = 0;
1585 if (hasFolding(curwin->w_cursor.lnum, NULL,
1586 &curwin->w_cursor.lnum))
1587 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1589 #endif
1590 oap->end = curwin->w_cursor;
1591 curwin->w_cursor = oap->start;
1593 /* w_virtcol may have been updated; if the cursor goes back to its
1594 * previous position w_virtcol becomes invalid and isn't updated
1595 * automatically. */
1596 curwin->w_valid &= ~VALID_VIRTCOL;
1598 else
1600 #ifdef FEAT_FOLDING
1601 /* Include folded lines completely. */
1602 if (!VIsual_active && oap->motion_type == MLINE)
1604 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1605 NULL))
1606 curwin->w_cursor.col = 0;
1607 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1608 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1610 #endif
1611 oap->end = oap->start;
1612 oap->start = curwin->w_cursor;
1615 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1617 #ifdef FEAT_VIRTUALEDIT
1618 /* Set "virtual_op" before resetting VIsual_active. */
1619 virtual_op = virtual_active();
1620 #endif
1622 #ifdef FEAT_VISUAL
1623 if (VIsual_active || redo_VIsual_busy)
1625 if (VIsual_mode == Ctrl_V) /* block mode */
1627 colnr_T start, end;
1629 oap->block_mode = TRUE;
1631 getvvcol(curwin, &(oap->start),
1632 &oap->start_vcol, NULL, &oap->end_vcol);
1633 if (!redo_VIsual_busy)
1635 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1637 if (start < oap->start_vcol)
1638 oap->start_vcol = start;
1639 if (end > oap->end_vcol)
1641 if (*p_sel == 'e' && start >= 1
1642 && start - 1 >= oap->end_vcol)
1643 oap->end_vcol = start - 1;
1644 else
1645 oap->end_vcol = end;
1649 /* if '$' was used, get oap->end_vcol from longest line */
1650 if (curwin->w_curswant == MAXCOL)
1652 curwin->w_cursor.col = MAXCOL;
1653 oap->end_vcol = 0;
1654 for (curwin->w_cursor.lnum = oap->start.lnum;
1655 curwin->w_cursor.lnum <= oap->end.lnum;
1656 ++curwin->w_cursor.lnum)
1658 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1659 if (end > oap->end_vcol)
1660 oap->end_vcol = end;
1663 else if (redo_VIsual_busy)
1664 oap->end_vcol = oap->start_vcol + redo_VIsual_col - 1;
1666 * Correct oap->end.col and oap->start.col to be the
1667 * upper-left and lower-right corner of the block area.
1669 * (Actually, this does convert column positions into character
1670 * positions)
1672 curwin->w_cursor.lnum = oap->end.lnum;
1673 coladvance(oap->end_vcol);
1674 oap->end = curwin->w_cursor;
1676 curwin->w_cursor = oap->start;
1677 coladvance(oap->start_vcol);
1678 oap->start = curwin->w_cursor;
1681 if (!redo_VIsual_busy && !gui_yank)
1684 * Prepare to reselect and redo Visual: this is based on the
1685 * size of the Visual text
1687 resel_VIsual_mode = VIsual_mode;
1688 if (curwin->w_curswant == MAXCOL)
1689 resel_VIsual_col = MAXCOL;
1690 else if (VIsual_mode == Ctrl_V)
1691 resel_VIsual_col = oap->end_vcol - oap->start_vcol + 1;
1692 else if (oap->line_count > 1)
1693 resel_VIsual_col = oap->end.col;
1694 else
1695 resel_VIsual_col = oap->end.col - oap->start.col + 1;
1696 resel_VIsual_line_count = oap->line_count;
1699 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1700 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1701 && oap->op_type != OP_COLON
1702 #ifdef FEAT_FOLDING
1703 && oap->op_type != OP_FOLD
1704 && oap->op_type != OP_FOLDOPEN
1705 && oap->op_type != OP_FOLDOPENREC
1706 && oap->op_type != OP_FOLDCLOSE
1707 && oap->op_type != OP_FOLDCLOSEREC
1708 && oap->op_type != OP_FOLDDEL
1709 && oap->op_type != OP_FOLDDELREC
1710 #endif
1711 && oap->motion_force == NUL
1714 /* Prepare for redoing. Only use the nchar field for "r",
1715 * otherwise it might be the second char of the operator. */
1716 prep_redo(oap->regname, 0L, NUL, 'v',
1717 get_op_char(oap->op_type),
1718 get_extra_op_char(oap->op_type),
1719 oap->op_type == OP_REPLACE ? cap->nchar : NUL);
1720 if (!redo_VIsual_busy)
1722 redo_VIsual_mode = resel_VIsual_mode;
1723 redo_VIsual_col = resel_VIsual_col;
1724 redo_VIsual_line_count = resel_VIsual_line_count;
1725 redo_VIsual_count = cap->count0;
1730 * oap->inclusive defaults to TRUE.
1731 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1732 * FALSE. This makes "d}P" and "v}dP" work the same.
1734 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1735 oap->inclusive = TRUE;
1736 if (VIsual_mode == 'V')
1737 oap->motion_type = MLINE;
1738 else
1740 oap->motion_type = MCHAR;
1741 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1742 # ifdef FEAT_VIRTUALEDIT
1743 && (include_line_break || !virtual_op)
1744 # endif
1747 oap->inclusive = FALSE;
1748 /* Try to include the newline, unless it's an operator
1749 * that works on lines only */
1750 if (*p_sel != 'o'
1751 && !op_on_lines(oap->op_type)
1752 && oap->end.lnum < curbuf->b_ml.ml_line_count)
1754 ++oap->end.lnum;
1755 oap->end.col = 0;
1756 # ifdef FEAT_VIRTUALEDIT
1757 oap->end.coladd = 0;
1758 # endif
1759 ++oap->line_count;
1764 redo_VIsual_busy = FALSE;
1767 * Switch Visual off now, so screen updating does
1768 * not show inverted text when the screen is redrawn.
1769 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1770 * no screen redraw, so it is done here to remove the inverted
1771 * part.
1773 if (!gui_yank)
1775 VIsual_active = FALSE;
1776 # ifdef FEAT_MOUSE
1777 setmouse();
1778 mouse_dragging = 0;
1779 # endif
1780 if (mode_displayed)
1781 clear_cmdline = TRUE; /* unshow visual mode later */
1782 #ifdef FEAT_CMDL_INFO
1783 else
1784 clear_showcmd();
1785 #endif
1786 if ((oap->op_type == OP_YANK
1787 || oap->op_type == OP_COLON
1788 || oap->op_type == OP_FUNCTION
1789 || oap->op_type == OP_FILTER)
1790 && oap->motion_force == NUL)
1791 redraw_curbuf_later(INVERTED);
1794 #endif
1796 #ifdef FEAT_MBYTE
1797 /* Include the trailing byte of a multi-byte char. */
1798 if (has_mbyte && oap->inclusive)
1800 int l;
1802 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
1803 if (l > 1)
1804 oap->end.col += l - 1;
1806 #endif
1807 curwin->w_set_curswant = TRUE;
1810 * oap->empty is set when start and end are the same. The inclusive
1811 * flag affects this too, unless yanking and the end is on a NUL.
1813 oap->empty = (oap->motion_type == MCHAR
1814 && (!oap->inclusive
1815 || (oap->op_type == OP_YANK
1816 && gchar_pos(&oap->end) == NUL))
1817 && equalpos(oap->start, oap->end)
1818 #ifdef FEAT_VIRTUALEDIT
1819 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1820 #endif
1823 * For delete, change and yank, it's an error to operate on an
1824 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1826 empty_region_error = (oap->empty
1827 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1829 #ifdef FEAT_VISUAL
1830 /* Force a redraw when operating on an empty Visual region, when
1831 * 'modifiable is off or creating a fold. */
1832 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1833 # ifdef FEAT_FOLDING
1834 || oap->op_type == OP_FOLD
1835 # endif
1837 redraw_curbuf_later(INVERTED);
1838 #endif
1841 * If the end of an operator is in column one while oap->motion_type
1842 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1843 * character in the previous line. If op_start is on or before the
1844 * first non-blank in the line, the operator becomes linewise
1845 * (strange, but that's the way vi does it).
1847 if ( oap->motion_type == MCHAR
1848 && oap->inclusive == FALSE
1849 && !(cap->retval & CA_NO_ADJ_OP_END)
1850 && oap->end.col == 0
1851 #ifdef FEAT_VISUAL
1852 && (!oap->is_VIsual || *p_sel == 'o')
1853 && !oap->block_mode
1854 #endif
1855 && oap->line_count > 1)
1857 oap->end_adjusted = TRUE; /* remember that we did this */
1858 --oap->line_count;
1859 --oap->end.lnum;
1860 if (inindent(0))
1861 oap->motion_type = MLINE;
1862 else
1864 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1865 if (oap->end.col)
1867 --oap->end.col;
1868 oap->inclusive = TRUE;
1872 else
1873 oap->end_adjusted = FALSE;
1875 switch (oap->op_type)
1877 case OP_LSHIFT:
1878 case OP_RSHIFT:
1879 op_shift(oap, TRUE,
1880 #ifdef FEAT_VISUAL
1881 oap->is_VIsual ? (int)cap->count1 :
1882 #endif
1884 auto_format(FALSE, TRUE);
1885 break;
1887 case OP_JOIN_NS:
1888 case OP_JOIN:
1889 if (oap->line_count < 2)
1890 oap->line_count = 2;
1891 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1892 curbuf->b_ml.ml_line_count)
1893 beep_flush();
1894 else
1896 do_do_join(oap->line_count, oap->op_type == OP_JOIN);
1897 auto_format(FALSE, TRUE);
1899 break;
1901 case OP_DELETE:
1902 #ifdef FEAT_VISUAL
1903 VIsual_reselect = FALSE; /* don't reselect now */
1904 #endif
1905 if (empty_region_error)
1906 vim_beep();
1907 else
1909 (void)op_delete(oap);
1910 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1911 u_save_cursor(); /* cursor line wasn't saved yet */
1912 auto_format(FALSE, TRUE);
1914 break;
1916 case OP_YANK:
1917 if (empty_region_error)
1919 if (!gui_yank)
1920 vim_beep();
1922 else
1923 (void)op_yank(oap, FALSE, !gui_yank);
1924 check_cursor_col();
1925 break;
1927 case OP_CHANGE:
1928 #ifdef FEAT_VISUAL
1929 VIsual_reselect = FALSE; /* don't reselect now */
1930 #endif
1931 if (empty_region_error)
1932 vim_beep();
1933 else
1935 /* This is a new edit command, not a restart. Need to
1936 * remember it to make 'insertmode' work with mappings for
1937 * Visual mode. But do this only once and not when typed and
1938 * 'insertmode' isn't set. */
1939 if (p_im || !KeyTyped)
1940 restart_edit_save = restart_edit;
1941 else
1942 restart_edit_save = 0;
1943 restart_edit = 0;
1944 /* Reset finish_op now, don't want it set inside edit(). */
1945 finish_op = FALSE;
1946 if (op_change(oap)) /* will call edit() */
1947 cap->retval |= CA_COMMAND_BUSY;
1948 if (restart_edit == 0)
1949 restart_edit = restart_edit_save;
1951 break;
1953 case OP_FILTER:
1954 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1955 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1956 else
1957 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
1959 case OP_INDENT:
1960 case OP_COLON:
1962 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1964 * If 'equalprg' is empty, do the indenting internally.
1966 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1968 # ifdef FEAT_LISP
1969 if (curbuf->b_p_lisp)
1971 op_reindent(oap, get_lisp_indent);
1972 break;
1974 # endif
1975 # ifdef FEAT_CINDENT
1976 op_reindent(oap,
1977 # ifdef FEAT_EVAL
1978 *curbuf->b_p_inde != NUL ? get_expr_indent :
1979 # endif
1980 get_c_indent);
1981 break;
1982 # endif
1984 #endif
1986 op_colon(oap);
1987 break;
1989 case OP_TILDE:
1990 case OP_UPPER:
1991 case OP_LOWER:
1992 case OP_ROT13:
1993 if (empty_region_error)
1994 vim_beep();
1995 else
1996 op_tilde(oap);
1997 check_cursor_col();
1998 break;
2000 case OP_FORMAT:
2001 #if defined(FEAT_EVAL)
2002 if (*curbuf->b_p_fex != NUL)
2003 op_formatexpr(oap); /* use expression */
2004 else
2005 #endif
2006 if (*p_fp != NUL)
2007 op_colon(oap); /* use external command */
2008 else
2009 op_format(oap, FALSE); /* use internal function */
2010 break;
2012 case OP_FORMAT2:
2013 op_format(oap, TRUE); /* use internal function */
2014 break;
2016 case OP_FUNCTION:
2017 op_function(oap); /* call 'operatorfunc' */
2018 break;
2020 case OP_INSERT:
2021 case OP_APPEND:
2022 #ifdef FEAT_VISUAL
2023 VIsual_reselect = FALSE; /* don't reselect now */
2024 #endif
2025 #ifdef FEAT_VISUALEXTRA
2026 if (empty_region_error)
2027 vim_beep();
2028 else
2030 /* This is a new edit command, not a restart. Need to
2031 * remember it to make 'insertmode' work with mappings for
2032 * Visual mode. But do this only once. */
2033 restart_edit_save = restart_edit;
2034 restart_edit = 0;
2036 op_insert(oap, cap->count1);
2038 /* TODO: when inserting in several lines, should format all
2039 * the lines. */
2040 auto_format(FALSE, TRUE);
2042 if (restart_edit == 0)
2043 restart_edit = restart_edit_save;
2045 #else
2046 vim_beep();
2047 #endif
2048 break;
2050 case OP_REPLACE:
2051 #ifdef FEAT_VISUAL
2052 VIsual_reselect = FALSE; /* don't reselect now */
2053 #endif
2054 #ifdef FEAT_VISUALEXTRA
2055 if (empty_region_error)
2056 #endif
2057 vim_beep();
2058 #ifdef FEAT_VISUALEXTRA
2059 else
2060 op_replace(oap, cap->nchar);
2061 #endif
2062 break;
2064 #ifdef FEAT_FOLDING
2065 case OP_FOLD:
2066 VIsual_reselect = FALSE; /* don't reselect now */
2067 foldCreate(oap->start.lnum, oap->end.lnum);
2068 break;
2070 case OP_FOLDOPEN:
2071 case OP_FOLDOPENREC:
2072 case OP_FOLDCLOSE:
2073 case OP_FOLDCLOSEREC:
2074 VIsual_reselect = FALSE; /* don't reselect now */
2075 opFoldRange(oap->start.lnum, oap->end.lnum,
2076 oap->op_type == OP_FOLDOPEN
2077 || oap->op_type == OP_FOLDOPENREC,
2078 oap->op_type == OP_FOLDOPENREC
2079 || oap->op_type == OP_FOLDCLOSEREC,
2080 oap->is_VIsual);
2081 break;
2083 case OP_FOLDDEL:
2084 case OP_FOLDDELREC:
2085 VIsual_reselect = FALSE; /* don't reselect now */
2086 deleteFold(oap->start.lnum, oap->end.lnum,
2087 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2088 break;
2089 #endif
2090 default:
2091 clearopbeep(oap);
2093 #ifdef FEAT_VIRTUALEDIT
2094 virtual_op = MAYBE;
2095 #endif
2096 if (!gui_yank)
2099 * if 'sol' not set, go back to old column for some commands
2101 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2102 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2103 || oap->op_type == OP_DELETE))
2104 coladvance(curwin->w_curswant = old_col);
2106 else
2108 curwin->w_cursor = old_cursor;
2110 #ifdef FEAT_VISUAL
2111 oap->block_mode = FALSE;
2112 #endif
2113 clearop(oap);
2118 * Handle indent and format operators and visual mode ":".
2120 static void
2121 op_colon(oap)
2122 oparg_T *oap;
2124 stuffcharReadbuff(':');
2125 #ifdef FEAT_VISUAL
2126 if (oap->is_VIsual)
2127 stuffReadbuff((char_u *)"'<,'>");
2128 else
2129 #endif
2132 * Make the range look nice, so it can be repeated.
2134 if (oap->start.lnum == curwin->w_cursor.lnum)
2135 stuffcharReadbuff('.');
2136 else
2137 stuffnumReadbuff((long)oap->start.lnum);
2138 if (oap->end.lnum != oap->start.lnum)
2140 stuffcharReadbuff(',');
2141 if (oap->end.lnum == curwin->w_cursor.lnum)
2142 stuffcharReadbuff('.');
2143 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2144 stuffcharReadbuff('$');
2145 else if (oap->start.lnum == curwin->w_cursor.lnum)
2147 stuffReadbuff((char_u *)".+");
2148 stuffnumReadbuff((long)oap->line_count - 1);
2150 else
2151 stuffnumReadbuff((long)oap->end.lnum);
2154 if (oap->op_type != OP_COLON)
2155 stuffReadbuff((char_u *)"!");
2156 if (oap->op_type == OP_INDENT)
2158 #ifndef FEAT_CINDENT
2159 if (*get_equalprg() == NUL)
2160 stuffReadbuff((char_u *)"indent");
2161 else
2162 #endif
2163 stuffReadbuff(get_equalprg());
2164 stuffReadbuff((char_u *)"\n");
2166 else if (oap->op_type == OP_FORMAT)
2168 if (*p_fp == NUL)
2169 stuffReadbuff((char_u *)"fmt");
2170 else
2171 stuffReadbuff(p_fp);
2172 stuffReadbuff((char_u *)"\n']");
2176 * do_cmdline() does the rest
2181 * Handle the "g@" operator: call 'operatorfunc'.
2183 /*ARGSUSED*/
2184 static void
2185 op_function(oap)
2186 oparg_T *oap;
2188 #ifdef FEAT_EVAL
2189 char_u *(argv[1]);
2191 if (*p_opfunc == NUL)
2192 EMSG(_("E774: 'operatorfunc' is empty"));
2193 else
2195 /* Set '[ and '] marks to text to be operated on. */
2196 curbuf->b_op_start = oap->start;
2197 curbuf->b_op_end = oap->end;
2198 if (oap->motion_type != MLINE && !oap->inclusive)
2199 /* Exclude the end position. */
2200 decl(&curbuf->b_op_end);
2202 if (oap->block_mode)
2203 argv[0] = (char_u *)"block";
2204 else if (oap->motion_type == MLINE)
2205 argv[0] = (char_u *)"line";
2206 else
2207 argv[0] = (char_u *)"char";
2208 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
2210 #else
2211 EMSG(_("E775: Eval feature not available"));
2212 #endif
2215 #if defined(FEAT_MOUSE) || defined(PROTO)
2217 * Do the appropriate action for the current mouse click in the current mode.
2218 * Not used for Command-line mode.
2220 * Normal Mode:
2221 * event modi- position visual change action
2222 * fier cursor window
2223 * left press - yes end yes
2224 * left press C yes end yes "^]" (2)
2225 * left press S yes end yes "*" (2)
2226 * left drag - yes start if moved no
2227 * left relse - yes start if moved no
2228 * middle press - yes if not active no put register
2229 * middle press - yes if active no yank and put
2230 * right press - yes start or extend yes
2231 * right press S yes no change yes "#" (2)
2232 * right drag - yes extend no
2233 * right relse - yes extend no
2235 * Insert or Replace Mode:
2236 * event modi- position visual change action
2237 * fier cursor window
2238 * left press - yes (cannot be active) yes
2239 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2240 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2241 * left drag - yes start or extend (1) no CTRL-O (1)
2242 * left relse - yes start or extend (1) no CTRL-O (1)
2243 * middle press - no (cannot be active) no put register
2244 * right press - yes start or extend yes CTRL-O
2245 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2247 * (1) only if mouse pointer moved since press
2248 * (2) only if click is in same buffer
2250 * Return TRUE if start_arrow() should be called for edit mode.
2253 do_mouse(oap, c, dir, count, fixindent)
2254 oparg_T *oap; /* operator argument, can be NULL */
2255 int c; /* K_LEFTMOUSE, etc */
2256 int dir; /* Direction to 'put' if necessary */
2257 long count;
2258 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2260 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2261 static int got_click = FALSE; /* got a click some time back */
2263 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2264 int is_click; /* If FALSE it's a drag or release event */
2265 int is_drag; /* If TRUE it's a drag event */
2266 int jump_flags = 0; /* flags for jump_to_mouse() */
2267 pos_T start_visual;
2268 int moved; /* Has cursor moved? */
2269 int in_status_line; /* mouse in status line */
2270 #ifdef FEAT_VERTSPLIT
2271 int in_sep_line; /* mouse in vertical separator line */
2272 #endif
2273 int c1, c2;
2274 #if defined(FEAT_FOLDING)
2275 pos_T save_cursor;
2276 #endif
2277 win_T *old_curwin = curwin;
2278 #ifdef FEAT_VISUAL
2279 static pos_T orig_cursor;
2280 colnr_T leftcol, rightcol;
2281 pos_T end_visual;
2282 int diff;
2283 int old_active = VIsual_active;
2284 int old_mode = VIsual_mode;
2285 #endif
2286 int regname;
2288 #if defined(FEAT_FOLDING)
2289 save_cursor = curwin->w_cursor;
2290 #endif
2293 * When GUI is active, always recognize mouse events, otherwise:
2294 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2295 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2296 * - For command line and insert mode 'mouse' is checked before calling
2297 * do_mouse().
2299 if (do_always)
2300 do_always = FALSE;
2301 else
2302 #ifdef FEAT_GUI
2303 if (!gui.in_use)
2304 #endif
2306 #ifdef FEAT_VISUAL
2307 if (VIsual_active)
2309 if (!mouse_has(MOUSE_VISUAL))
2310 return FALSE;
2312 else
2313 #endif
2314 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2315 return FALSE;
2318 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2320 #ifdef FEAT_MOUSESHAPE
2321 /* May have stopped dragging the status or separator line. The pointer is
2322 * most likely still on the status or separator line. */
2323 if (!is_drag && drag_status_line)
2325 drag_status_line = FALSE;
2326 update_mouseshape(SHAPE_IDX_STATUS);
2328 # ifdef FEAT_VERTSPLIT
2329 if (!is_drag && drag_sep_line)
2331 drag_sep_line = FALSE;
2332 update_mouseshape(SHAPE_IDX_VSEP);
2334 # endif
2335 #endif
2338 * Ignore drag and release events if we didn't get a click.
2340 if (is_click)
2341 got_click = TRUE;
2342 else
2344 if (!got_click) /* didn't get click, ignore */
2345 return FALSE;
2346 if (!is_drag) /* release, reset got_click */
2347 got_click = FALSE;
2350 #ifndef FEAT_VISUAL
2352 * ALT is only used for starging/extending Visual mode.
2354 if ((mod_mask & MOD_MASK_ALT))
2355 return FALSE;
2356 #endif
2359 * CTRL right mouse button does CTRL-T
2361 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2363 if (State & INSERT)
2364 stuffcharReadbuff(Ctrl_O);
2365 if (count > 1)
2366 stuffnumReadbuff(count);
2367 stuffcharReadbuff(Ctrl_T);
2368 got_click = FALSE; /* ignore drag&release now */
2369 return FALSE;
2373 * CTRL only works with left mouse button
2375 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2376 return FALSE;
2379 * When a modifier is down, ignore drag and release events, as well as
2380 * multiple clicks and the middle mouse button.
2381 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2383 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2384 | MOD_MASK_META))
2385 && (!is_click
2386 || (mod_mask & MOD_MASK_MULTI_CLICK)
2387 || which_button == MOUSE_MIDDLE)
2388 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
2389 && mouse_model_popup()
2390 && which_button == MOUSE_LEFT)
2391 && !((mod_mask & MOD_MASK_ALT)
2392 && !mouse_model_popup()
2393 && which_button == MOUSE_RIGHT)
2395 return FALSE;
2398 * If the button press was used as the movement command for an operator
2399 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2400 * drag/release events.
2402 if (!is_click && which_button == MOUSE_MIDDLE)
2403 return FALSE;
2405 if (oap != NULL)
2406 regname = oap->regname;
2407 else
2408 regname = 0;
2411 * Middle mouse button does a 'put' of the selected text
2413 if (which_button == MOUSE_MIDDLE)
2415 if (State == NORMAL)
2418 * If an operator was pending, we don't know what the user wanted
2419 * to do. Go back to normal mode: Clear the operator and beep().
2421 if (oap != NULL && oap->op_type != OP_NOP)
2423 clearopbeep(oap);
2424 return FALSE;
2427 #ifdef FEAT_VISUAL
2429 * If visual was active, yank the highlighted text and put it
2430 * before the mouse pointer position.
2431 * In Select mode replace the highlighted text with the clipboard.
2433 if (VIsual_active)
2435 if (VIsual_select)
2437 stuffcharReadbuff(Ctrl_G);
2438 stuffReadbuff((char_u *)"\"+p");
2440 else
2442 stuffcharReadbuff('y');
2443 stuffcharReadbuff(K_MIDDLEMOUSE);
2445 do_always = TRUE; /* ignore 'mouse' setting next time */
2446 return FALSE;
2448 #endif
2450 * The rest is below jump_to_mouse()
2454 else if ((State & INSERT) == 0)
2455 return FALSE;
2458 * Middle click in insert mode doesn't move the mouse, just insert the
2459 * contents of a register. '.' register is special, can't insert that
2460 * with do_put().
2461 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2462 * happens for the GUI).
2464 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2466 if (regname == '.')
2467 insert_reg(regname, TRUE);
2468 else
2470 #ifdef FEAT_CLIPBOARD
2471 if (clip_star.available && regname == 0)
2472 regname = '*';
2473 #endif
2474 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2475 insert_reg(regname, TRUE);
2476 else
2478 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2480 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2481 AppendCharToRedobuff(Ctrl_R);
2482 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2483 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2486 return FALSE;
2490 /* When dragging or button-up stay in the same window. */
2491 if (!is_click)
2492 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2494 start_visual.lnum = 0;
2496 #ifdef FEAT_WINDOWS
2497 /* Check for clicking in the tab page line. */
2498 if (mouse_row == 0 && firstwin->w_winrow > 0)
2500 if (is_drag)
2501 return FALSE;
2502 got_click = FALSE; /* ignore mouse-up and drag events */
2504 /* click in a tab selects that tab page */
2505 if (is_click
2506 # ifdef FEAT_CMDWIN
2507 && cmdwin_type == 0
2508 # endif
2509 && mouse_col < Columns)
2511 c1 = TabPageIdxs[mouse_col];
2512 if (c1 >= 0)
2514 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2516 /* double click opens new page */
2517 end_visual_mode();
2518 tabpage_new();
2519 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2521 else
2523 /* Go to specified tab page, or next one if not clicking
2524 * on a label. */
2525 goto_tabpage(c1);
2527 /* It's like clicking on the status line of a window. */
2528 if (curwin != old_curwin)
2529 end_visual_mode();
2532 else if (c1 < 0)
2534 tabpage_T *tp;
2536 /* Close the current or specified tab page. */
2537 if (c1 == -999)
2538 tp = curtab;
2539 else
2540 tp = find_tabpage(-c1);
2541 if (tp == curtab)
2543 if (first_tabpage->tp_next != NULL)
2544 tabpage_close(FALSE);
2546 else if (tp != NULL)
2547 tabpage_close_other(tp, FALSE);
2550 return TRUE;
2552 #endif
2555 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2556 * right button up -> pop-up menu
2557 * shift-left button -> right button
2558 * alt-left button -> alt-right button
2560 if (mouse_model_popup())
2562 if (which_button == MOUSE_RIGHT
2563 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2566 * NOTE: Ignore right button down and drag mouse events.
2567 * Windows only shows the popup menu on the button up event.
2569 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2570 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) \
2571 || defined(FEAT_GUI_MACVIM)
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 || defined(FEAT_GUI_MACVIM)
2583 if (gui.in_use)
2585 jump_flags = 0;
2586 if (STRCMP(p_mousem, "popup_setpos") == 0)
2588 /* First set the cursor position before showing the popup
2589 * menu. */
2590 #ifdef FEAT_VISUAL
2591 if (VIsual_active)
2593 pos_T m_pos;
2596 * set MOUSE_MAY_STOP_VIS if we are outside the
2597 * selection or the current window (might have false
2598 * negative here)
2600 if (mouse_row < W_WINROW(curwin)
2601 || mouse_row
2602 > (W_WINROW(curwin) + curwin->w_height))
2603 jump_flags = MOUSE_MAY_STOP_VIS;
2604 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2605 jump_flags = MOUSE_MAY_STOP_VIS;
2606 else
2608 if ((lt(curwin->w_cursor, VIsual)
2609 && (lt(m_pos, curwin->w_cursor)
2610 || lt(VIsual, m_pos)))
2611 || (lt(VIsual, curwin->w_cursor)
2612 && (lt(m_pos, VIsual)
2613 || lt(curwin->w_cursor, m_pos))))
2615 jump_flags = MOUSE_MAY_STOP_VIS;
2617 else if (VIsual_mode == Ctrl_V)
2619 getvcols(curwin, &curwin->w_cursor, &VIsual,
2620 &leftcol, &rightcol);
2621 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2622 if (m_pos.col < leftcol || m_pos.col > rightcol)
2623 jump_flags = MOUSE_MAY_STOP_VIS;
2627 else
2628 jump_flags = MOUSE_MAY_STOP_VIS;
2629 #endif
2631 if (jump_flags)
2633 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2634 update_curbuf(
2635 #ifdef FEAT_VISUAL
2636 VIsual_active ? INVERTED :
2637 #endif
2638 VALID);
2639 setcursor();
2640 out_flush(); /* Update before showing popup menu */
2642 # ifdef FEAT_MENU
2643 gui_show_popupmenu();
2644 # endif
2645 return (jump_flags & CURSOR_MOVED) != 0;
2647 else
2648 return FALSE;
2649 #else
2650 return FALSE;
2651 #endif
2653 if (which_button == MOUSE_LEFT
2654 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
2656 which_button = MOUSE_RIGHT;
2657 mod_mask &= ~MOD_MASK_SHIFT;
2661 #ifdef FEAT_VISUAL
2662 if ((State & (NORMAL | INSERT))
2663 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2665 if (which_button == MOUSE_LEFT)
2667 if (is_click)
2669 /* stop Visual mode for a left click in a window, but not when
2670 * on a status line */
2671 if (VIsual_active)
2672 jump_flags |= MOUSE_MAY_STOP_VIS;
2674 else if (mouse_has(MOUSE_VISUAL))
2675 jump_flags |= MOUSE_MAY_VIS;
2677 else if (which_button == MOUSE_RIGHT)
2679 if (is_click && VIsual_active)
2682 * Remember the start and end of visual before moving the
2683 * cursor.
2685 if (lt(curwin->w_cursor, VIsual))
2687 start_visual = curwin->w_cursor;
2688 end_visual = VIsual;
2690 else
2692 start_visual = VIsual;
2693 end_visual = curwin->w_cursor;
2696 jump_flags |= MOUSE_FOCUS;
2697 if (mouse_has(MOUSE_VISUAL))
2698 jump_flags |= MOUSE_MAY_VIS;
2701 #endif
2704 * If an operator is pending, ignore all drags and releases until the
2705 * next mouse click.
2707 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2709 got_click = FALSE;
2710 oap->motion_type = MCHAR;
2713 /* When releasing the button let jump_to_mouse() know. */
2714 if (!is_click && !is_drag)
2715 jump_flags |= MOUSE_RELEASED;
2718 * JUMP!
2720 jump_flags = jump_to_mouse(jump_flags,
2721 oap == NULL ? NULL : &(oap->inclusive), which_button);
2722 moved = (jump_flags & CURSOR_MOVED);
2723 in_status_line = (jump_flags & IN_STATUS_LINE);
2724 #ifdef FEAT_VERTSPLIT
2725 in_sep_line = (jump_flags & IN_SEP_LINE);
2726 #endif
2728 #ifdef FEAT_NETBEANS_INTG
2729 if (usingNetbeans && isNetbeansBuffer(curbuf)
2730 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2732 int key = KEY2TERMCAP1(c);
2734 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2735 || key == (int)KE_RIGHTRELEASE)
2736 netbeans_button_release(which_button);
2738 #endif
2740 /* When jumping to another window, clear a pending operator. That's a bit
2741 * friendlier than beeping and not jumping to that window. */
2742 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2743 clearop(oap);
2745 #ifdef FEAT_FOLDING
2746 if (mod_mask == 0
2747 && !is_drag
2748 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2749 && which_button == MOUSE_LEFT)
2751 /* open or close a fold at this line */
2752 if (jump_flags & MOUSE_FOLD_OPEN)
2753 openFold(curwin->w_cursor.lnum, 1L);
2754 else
2755 closeFold(curwin->w_cursor.lnum, 1L);
2756 /* don't move the cursor if still in the same window */
2757 if (curwin == old_curwin)
2758 curwin->w_cursor = save_cursor;
2760 #endif
2762 #if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2763 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2765 clip_modeless(which_button, is_click, is_drag);
2766 return FALSE;
2768 #endif
2770 #ifdef FEAT_VISUAL
2771 /* Set global flag that we are extending the Visual area with mouse
2772 * dragging; temporarily minimize 'scrolloff'. */
2773 if (VIsual_active && is_drag && p_so)
2775 /* In the very first line, allow scrolling one line */
2776 if (mouse_row == 0)
2777 mouse_dragging = 2;
2778 else
2779 mouse_dragging = 1;
2782 /* When dragging the mouse above the window, scroll down. */
2783 if (is_drag && mouse_row < 0 && !in_status_line)
2785 scroll_redraw(FALSE, 1L);
2786 mouse_row = 0;
2789 if (start_visual.lnum) /* right click in visual mode */
2791 /* When ALT is pressed make Visual mode blockwise. */
2792 if (mod_mask & MOD_MASK_ALT)
2793 VIsual_mode = Ctrl_V;
2796 * In Visual-block mode, divide the area in four, pick up the corner
2797 * that is in the quarter that the cursor is in.
2799 if (VIsual_mode == Ctrl_V)
2801 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2802 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2803 end_visual.col = leftcol;
2804 else
2805 end_visual.col = rightcol;
2806 if (curwin->w_cursor.lnum <
2807 (start_visual.lnum + end_visual.lnum) / 2)
2808 end_visual.lnum = end_visual.lnum;
2809 else
2810 end_visual.lnum = start_visual.lnum;
2812 /* move VIsual to the right column */
2813 start_visual = curwin->w_cursor; /* save the cursor pos */
2814 curwin->w_cursor = end_visual;
2815 coladvance(end_visual.col);
2816 VIsual = curwin->w_cursor;
2817 curwin->w_cursor = start_visual; /* restore the cursor */
2819 else
2822 * If the click is before the start of visual, change the start.
2823 * If the click is after the end of visual, change the end. If
2824 * the click is inside the visual, change the closest side.
2826 if (lt(curwin->w_cursor, start_visual))
2827 VIsual = end_visual;
2828 else if (lt(end_visual, curwin->w_cursor))
2829 VIsual = start_visual;
2830 else
2832 /* In the same line, compare column number */
2833 if (end_visual.lnum == start_visual.lnum)
2835 if (curwin->w_cursor.col - start_visual.col >
2836 end_visual.col - curwin->w_cursor.col)
2837 VIsual = start_visual;
2838 else
2839 VIsual = end_visual;
2842 /* In different lines, compare line number */
2843 else
2845 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2846 (end_visual.lnum - curwin->w_cursor.lnum);
2848 if (diff > 0) /* closest to end */
2849 VIsual = start_visual;
2850 else if (diff < 0) /* closest to start */
2851 VIsual = end_visual;
2852 else /* in the middle line */
2854 if (curwin->w_cursor.col <
2855 (start_visual.col + end_visual.col) / 2)
2856 VIsual = end_visual;
2857 else
2858 VIsual = start_visual;
2865 * If Visual mode started in insert mode, execute "CTRL-O"
2867 else if ((State & INSERT) && VIsual_active)
2868 stuffcharReadbuff(Ctrl_O);
2869 #endif
2872 * Middle mouse click: Put text before cursor.
2874 if (which_button == MOUSE_MIDDLE)
2876 #ifdef FEAT_CLIPBOARD
2877 if (clip_star.available && regname == 0)
2878 regname = '*';
2879 #endif
2880 if (yank_register_mline(regname))
2882 if (mouse_past_bottom)
2883 dir = FORWARD;
2885 else if (mouse_past_eol)
2886 dir = FORWARD;
2888 if (fixindent)
2890 c1 = (dir == BACKWARD) ? '[' : ']';
2891 c2 = 'p';
2893 else
2895 c1 = (dir == FORWARD) ? 'p' : 'P';
2896 c2 = NUL;
2898 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2901 * Remember where the paste started, so in edit() Insstart can be set
2902 * to this position
2904 if (restart_edit != 0)
2905 where_paste_started = curwin->w_cursor;
2906 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2909 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2911 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2912 * error under the mouse pointer.
2914 else if (((mod_mask & MOD_MASK_CTRL)
2915 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2916 && bt_quickfix(curbuf))
2918 if (State & INSERT)
2919 stuffcharReadbuff(Ctrl_O);
2920 if (curwin->w_llist_ref == NULL) /* quickfix window */
2921 stuffReadbuff((char_u *)":.cc\n");
2922 else /* location list window */
2923 stuffReadbuff((char_u *)":.ll\n");
2924 got_click = FALSE; /* ignore drag&release now */
2926 #endif
2929 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2930 * under the mouse pointer.
2932 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2933 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2935 if (State & INSERT)
2936 stuffcharReadbuff(Ctrl_O);
2937 stuffcharReadbuff(Ctrl_RSB);
2938 got_click = FALSE; /* ignore drag&release now */
2942 * Shift-Mouse click searches for the next occurrence of the word under
2943 * the mouse pointer
2945 else if ((mod_mask & MOD_MASK_SHIFT))
2947 if (State & INSERT
2948 #ifdef FEAT_VISUAL
2949 || (VIsual_active && VIsual_select)
2950 #endif
2952 stuffcharReadbuff(Ctrl_O);
2953 if (which_button == MOUSE_LEFT)
2954 stuffcharReadbuff('*');
2955 else /* MOUSE_RIGHT */
2956 stuffcharReadbuff('#');
2959 /* Handle double clicks, unless on status line */
2960 else if (in_status_line)
2962 #ifdef FEAT_MOUSESHAPE
2963 if ((is_drag || is_click) && !drag_status_line)
2965 drag_status_line = TRUE;
2966 update_mouseshape(-1);
2968 #endif
2970 #ifdef FEAT_VERTSPLIT
2971 else if (in_sep_line)
2973 # ifdef FEAT_MOUSESHAPE
2974 if ((is_drag || is_click) && !drag_sep_line)
2976 drag_sep_line = TRUE;
2977 update_mouseshape(-1);
2979 # endif
2981 #endif
2982 #ifdef FEAT_VISUAL
2983 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
2984 && mouse_has(MOUSE_VISUAL))
2986 if (is_click || !VIsual_active)
2988 if (VIsual_active)
2989 orig_cursor = VIsual;
2990 else
2992 check_visual_highlight();
2993 VIsual = curwin->w_cursor;
2994 orig_cursor = VIsual;
2995 VIsual_active = TRUE;
2996 VIsual_reselect = TRUE;
2997 /* start Select mode if 'selectmode' contains "mouse" */
2998 may_start_select('o');
2999 setmouse();
3001 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3003 /* Double click with ALT pressed makes it blockwise. */
3004 if (mod_mask & MOD_MASK_ALT)
3005 VIsual_mode = Ctrl_V;
3006 else
3007 VIsual_mode = 'v';
3009 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3010 VIsual_mode = 'V';
3011 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3012 VIsual_mode = Ctrl_V;
3013 #ifdef FEAT_CLIPBOARD
3014 /* Make sure the clipboard gets updated. Needed because start and
3015 * end may still be the same, and the selection needs to be owned */
3016 clip_star.vmode = NUL;
3017 #endif
3020 * A double click selects a word or a block.
3022 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3024 pos_T *pos = NULL;
3025 int gc;
3027 if (is_click)
3029 /* If the character under the cursor (skipping white space) is
3030 * not a word character, try finding a match and select a (),
3031 * {}, [], #if/#endif, etc. block. */
3032 end_visual = curwin->w_cursor;
3033 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
3034 inc(&end_visual);
3035 if (oap != NULL)
3036 oap->motion_type = MCHAR;
3037 if (oap != NULL
3038 && VIsual_mode == 'v'
3039 && !vim_iswordc(gchar_pos(&end_visual))
3040 && equalpos(curwin->w_cursor, VIsual)
3041 && (pos = findmatch(oap, NUL)) != NULL)
3043 curwin->w_cursor = *pos;
3044 if (oap->motion_type == MLINE)
3045 VIsual_mode = 'V';
3046 else if (*p_sel == 'e')
3048 if (lt(curwin->w_cursor, VIsual))
3049 ++VIsual.col;
3050 else
3051 ++curwin->w_cursor.col;
3056 if (pos == NULL && (is_click || is_drag))
3058 /* When not found a match or when dragging: extend to include
3059 * a word. */
3060 if (lt(curwin->w_cursor, orig_cursor))
3062 find_start_of_word(&curwin->w_cursor);
3063 find_end_of_word(&VIsual);
3065 else
3067 find_start_of_word(&VIsual);
3068 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3069 #ifdef FEAT_MBYTE
3070 curwin->w_cursor.col +=
3071 (*mb_ptr2len)(ml_get_cursor());
3072 #else
3073 ++curwin->w_cursor.col;
3074 #endif
3075 find_end_of_word(&curwin->w_cursor);
3078 curwin->w_set_curswant = TRUE;
3080 if (is_click)
3081 redraw_curbuf_later(INVERTED); /* update the inversion */
3083 else if (VIsual_active && !old_active)
3085 if (mod_mask & MOD_MASK_ALT)
3086 VIsual_mode = Ctrl_V;
3087 else
3088 VIsual_mode = 'v';
3091 /* If Visual mode changed show it later. */
3092 if ((!VIsual_active && old_active && mode_displayed)
3093 || (VIsual_active && p_smd && msg_silent == 0
3094 && (!old_active || VIsual_mode != old_mode)))
3095 redraw_cmdline = TRUE;
3096 #endif
3098 return moved;
3101 #ifdef FEAT_VISUAL
3103 * Move "pos" back to the start of the word it's in.
3105 static void
3106 find_start_of_word(pos)
3107 pos_T *pos;
3109 char_u *line;
3110 int cclass;
3111 int col;
3113 line = ml_get(pos->lnum);
3114 cclass = get_mouse_class(line + pos->col);
3116 while (pos->col > 0)
3118 col = pos->col - 1;
3119 #ifdef FEAT_MBYTE
3120 col -= (*mb_head_off)(line, line + col);
3121 #endif
3122 if (get_mouse_class(line + col) != cclass)
3123 break;
3124 pos->col = col;
3129 * Move "pos" forward to the end of the word it's in.
3130 * When 'selection' is "exclusive", the position is just after the word.
3132 static void
3133 find_end_of_word(pos)
3134 pos_T *pos;
3136 char_u *line;
3137 int cclass;
3138 int col;
3140 line = ml_get(pos->lnum);
3141 if (*p_sel == 'e' && pos->col > 0)
3143 --pos->col;
3144 #ifdef FEAT_MBYTE
3145 pos->col -= (*mb_head_off)(line, line + pos->col);
3146 #endif
3148 cclass = get_mouse_class(line + pos->col);
3149 while (line[pos->col] != NUL)
3151 #ifdef FEAT_MBYTE
3152 col = pos->col + (*mb_ptr2len)(line + pos->col);
3153 #else
3154 col = pos->col + 1;
3155 #endif
3156 if (get_mouse_class(line + col) != cclass)
3158 if (*p_sel == 'e')
3159 pos->col = col;
3160 break;
3162 pos->col = col;
3167 * Get class of a character for selection: same class means same word.
3168 * 0: blank
3169 * 1: punctuation groups
3170 * 2: normal word character
3171 * >2: multi-byte word character.
3173 static int
3174 get_mouse_class(p)
3175 char_u *p;
3177 int c;
3179 #ifdef FEAT_MBYTE
3180 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3181 return mb_get_class(p);
3182 #endif
3184 c = *p;
3185 if (c == ' ' || c == '\t')
3186 return 0;
3188 if (vim_iswordc(c))
3189 return 2;
3192 * There are a few special cases where we want certain combinations of
3193 * characters to be considered as a single word. These are things like
3194 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
3195 * character is in it's own class.
3197 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3198 return 1;
3199 return c;
3201 #endif /* FEAT_VISUAL */
3202 #endif /* FEAT_MOUSE */
3204 #if defined(FEAT_VISUAL) || defined(PROTO)
3206 * Check if highlighting for visual mode is possible, give a warning message
3207 * if not.
3209 void
3210 check_visual_highlight()
3212 static int did_check = FALSE;
3214 if (full_screen)
3216 if (!did_check && hl_attr(HLF_V) == 0)
3217 MSG(_("Warning: terminal cannot highlight"));
3218 did_check = TRUE;
3223 * End Visual mode.
3224 * This function should ALWAYS be called to end Visual mode, except from
3225 * do_pending_operator().
3227 void
3228 end_visual_mode()
3230 #ifdef FEAT_CLIPBOARD
3232 * If we are using the clipboard, then remember what was selected in case
3233 * we need to paste it somewhere while we still own the selection.
3234 * Only do this when the clipboard is already owned. Don't want to grab
3235 * the selection when hitting ESC.
3237 if (clip_star.available && clip_star.owned)
3238 clip_auto_select();
3239 #endif
3241 VIsual_active = FALSE;
3242 #ifdef FEAT_MOUSE
3243 setmouse();
3244 mouse_dragging = 0;
3245 #endif
3247 /* Save the current VIsual area for '< and '> marks, and "gv" */
3248 curbuf->b_visual.vi_mode = VIsual_mode;
3249 curbuf->b_visual.vi_start = VIsual;
3250 curbuf->b_visual.vi_end = curwin->w_cursor;
3251 curbuf->b_visual.vi_curswant = curwin->w_curswant;
3252 #ifdef FEAT_EVAL
3253 curbuf->b_visual_mode_eval = VIsual_mode;
3254 #endif
3255 #ifdef FEAT_VIRTUALEDIT
3256 if (!virtual_active())
3257 curwin->w_cursor.coladd = 0;
3258 #endif
3260 if (mode_displayed)
3261 clear_cmdline = TRUE; /* unshow visual mode later */
3262 #ifdef FEAT_CMDL_INFO
3263 else
3264 clear_showcmd();
3265 #endif
3267 adjust_cursor_eol();
3271 * Reset VIsual_active and VIsual_reselect.
3273 void
3274 reset_VIsual_and_resel()
3276 if (VIsual_active)
3278 end_visual_mode();
3279 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3281 VIsual_reselect = FALSE;
3285 * Reset VIsual_active and VIsual_reselect if it's set.
3287 void
3288 reset_VIsual()
3290 if (VIsual_active)
3292 end_visual_mode();
3293 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3294 VIsual_reselect = FALSE;
3297 #endif /* FEAT_VISUAL */
3299 #if defined(FEAT_BEVAL)
3300 static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3303 * Check for a balloon-eval special item to include when searching for an
3304 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3305 * Returns TRUE if the character at "*ptr" should be included.
3306 * "dir" is FORWARD or BACKWARD, the direction of searching.
3307 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3308 * "bnp" points to a counter for square brackets.
3310 static int
3311 find_is_eval_item(ptr, colp, bnp, dir)
3312 char_u *ptr;
3313 int *colp;
3314 int *bnp;
3315 int dir;
3317 /* Accept everything inside []. */
3318 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3319 ++*bnp;
3320 if (*bnp > 0)
3322 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3323 --*bnp;
3324 return TRUE;
3327 /* skip over "s.var" */
3328 if (*ptr == '.')
3329 return TRUE;
3331 /* two-character item: s->var */
3332 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3333 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3335 *colp += dir;
3336 return TRUE;
3338 return FALSE;
3340 #endif
3343 * Find the identifier under or to the right of the cursor.
3344 * "find_type" can have one of three values:
3345 * FIND_IDENT: find an identifier (keyword)
3346 * FIND_STRING: find any non-white string
3347 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
3348 * FIND_EVAL: find text useful for C program debugging
3350 * There are three steps:
3351 * 1. Search forward for the start of an identifier/string. Doesn't move if
3352 * already on one.
3353 * 2. Search backward for the start of this identifier/string.
3354 * This doesn't match the real Vi but I like it a little better and it
3355 * shouldn't bother anyone.
3356 * 3. Search forward to the end of this identifier/string.
3357 * When FIND_IDENT isn't defined, we backup until a blank.
3359 * Returns the length of the string, or zero if no string is found.
3360 * If a string is found, a pointer to the string is put in "*string". This
3361 * string is not always NUL terminated.
3364 find_ident_under_cursor(string, find_type)
3365 char_u **string;
3366 int find_type;
3368 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3369 curwin->w_cursor.col, string, find_type);
3373 * Like find_ident_under_cursor(), but for any window and any position.
3374 * However: Uses 'iskeyword' from the current window!.
3377 find_ident_at_pos(wp, lnum, startcol, string, find_type)
3378 win_T *wp;
3379 linenr_T lnum;
3380 colnr_T startcol;
3381 char_u **string;
3382 int find_type;
3384 char_u *ptr;
3385 int col = 0; /* init to shut up GCC */
3386 int i;
3387 #ifdef FEAT_MBYTE
3388 int this_class = 0;
3389 int prev_class;
3390 int prevcol;
3391 #endif
3392 #if defined(FEAT_BEVAL)
3393 int bn = 0; /* bracket nesting */
3394 #endif
3397 * if i == 0: try to find an identifier
3398 * if i == 1: try to find any non-white string
3400 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3401 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3404 * 1. skip to start of identifier/string
3406 col = startcol;
3407 #ifdef FEAT_MBYTE
3408 if (has_mbyte)
3410 while (ptr[col] != NUL)
3412 # if defined(FEAT_BEVAL)
3413 /* Stop at a ']' to evaluate "a[x]". */
3414 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3415 break;
3416 # endif
3417 this_class = mb_get_class(ptr + col);
3418 if (this_class != 0 && (i == 1 || this_class != 1))
3419 break;
3420 col += (*mb_ptr2len)(ptr + col);
3423 else
3424 #endif
3425 while (ptr[col] != NUL
3426 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
3427 # if defined(FEAT_BEVAL)
3428 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3429 # endif
3431 ++col;
3433 #if defined(FEAT_BEVAL)
3434 /* When starting on a ']' count it, so that we include the '['. */
3435 bn = ptr[col] == ']';
3436 #endif
3439 * 2. Back up to start of identifier/string.
3441 #ifdef FEAT_MBYTE
3442 if (has_mbyte)
3444 /* Remember class of character under cursor. */
3445 # if defined(FEAT_BEVAL)
3446 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3447 this_class = mb_get_class((char_u *)"a");
3448 else
3449 # endif
3450 this_class = mb_get_class(ptr + col);
3451 while (col > 0 && this_class != 0)
3453 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3454 prev_class = mb_get_class(ptr + prevcol);
3455 if (this_class != prev_class
3456 && (i == 0
3457 || prev_class == 0
3458 || (find_type & FIND_IDENT))
3459 # if defined(FEAT_BEVAL)
3460 && (!(find_type & FIND_EVAL)
3461 || prevcol == 0
3462 || !find_is_eval_item(ptr + prevcol, &prevcol,
3463 &bn, BACKWARD))
3464 # endif
3466 break;
3467 col = prevcol;
3470 /* If we don't want just any old string, or we've found an
3471 * identifier, stop searching. */
3472 if (this_class > 2)
3473 this_class = 2;
3474 if (!(find_type & FIND_STRING) || this_class == 2)
3475 break;
3477 else
3478 #endif
3480 while (col > 0
3481 && ((i == 0
3482 ? vim_iswordc(ptr[col - 1])
3483 : (!vim_iswhite(ptr[col - 1])
3484 && (!(find_type & FIND_IDENT)
3485 || !vim_iswordc(ptr[col - 1]))))
3486 #if defined(FEAT_BEVAL)
3487 || ((find_type & FIND_EVAL)
3488 && col > 1
3489 && find_is_eval_item(ptr + col - 1, &col,
3490 &bn, BACKWARD))
3491 #endif
3493 --col;
3495 /* If we don't want just any old string, or we've found an
3496 * identifier, stop searching. */
3497 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3498 break;
3502 if (ptr[col] == NUL || (i == 0 && (
3503 #ifdef FEAT_MBYTE
3504 has_mbyte ? this_class != 2 :
3505 #endif
3506 !vim_iswordc(ptr[col]))))
3509 * didn't find an identifier or string
3511 if (find_type & FIND_STRING)
3512 EMSG(_("E348: No string under cursor"));
3513 else
3514 EMSG(_("E349: No identifier under cursor"));
3515 return 0;
3517 ptr += col;
3518 *string = ptr;
3521 * 3. Find the end if the identifier/string.
3523 #if defined(FEAT_BEVAL)
3524 bn = 0;
3525 startcol -= col;
3526 #endif
3527 col = 0;
3528 #ifdef FEAT_MBYTE
3529 if (has_mbyte)
3531 /* Search for point of changing multibyte character class. */
3532 this_class = mb_get_class(ptr);
3533 while (ptr[col] != NUL
3534 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3535 : mb_get_class(ptr + col) != 0)
3536 # if defined(FEAT_BEVAL)
3537 || ((find_type & FIND_EVAL)
3538 && col <= (int)startcol
3539 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3540 # endif
3542 col += (*mb_ptr2len)(ptr + col);
3544 else
3545 #endif
3546 while ((i == 0 ? vim_iswordc(ptr[col])
3547 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
3548 # if defined(FEAT_BEVAL)
3549 || ((find_type & FIND_EVAL)
3550 && col <= (int)startcol
3551 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3552 # endif
3555 ++col;
3558 return col;
3562 * Prepare for redo of a normal command.
3564 static void
3565 prep_redo_cmd(cap)
3566 cmdarg_T *cap;
3568 prep_redo(cap->oap->regname, cap->count0,
3569 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3573 * Prepare for redo of any command.
3574 * Note that only the last argument can be a multi-byte char.
3576 static void
3577 prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3578 int regname;
3579 long num;
3580 int cmd1;
3581 int cmd2;
3582 int cmd3;
3583 int cmd4;
3584 int cmd5;
3586 ResetRedobuff();
3587 if (regname != 0) /* yank from specified buffer */
3589 AppendCharToRedobuff('"');
3590 AppendCharToRedobuff(regname);
3592 if (num)
3593 AppendNumberToRedobuff(num);
3595 if (cmd1 != NUL)
3596 AppendCharToRedobuff(cmd1);
3597 if (cmd2 != NUL)
3598 AppendCharToRedobuff(cmd2);
3599 if (cmd3 != NUL)
3600 AppendCharToRedobuff(cmd3);
3601 if (cmd4 != NUL)
3602 AppendCharToRedobuff(cmd4);
3603 if (cmd5 != NUL)
3604 AppendCharToRedobuff(cmd5);
3608 * check for operator active and clear it
3610 * return TRUE if operator was active
3612 static int
3613 checkclearop(oap)
3614 oparg_T *oap;
3616 if (oap->op_type == OP_NOP)
3617 return FALSE;
3618 clearopbeep(oap);
3619 return TRUE;
3623 * Check for operator or Visual active. Clear active operator.
3625 * Return TRUE if operator or Visual was active.
3627 static int
3628 checkclearopq(oap)
3629 oparg_T *oap;
3631 if (oap->op_type == OP_NOP
3632 #ifdef FEAT_VISUAL
3633 && !VIsual_active
3634 #endif
3636 return FALSE;
3637 clearopbeep(oap);
3638 return TRUE;
3641 static void
3642 clearop(oap)
3643 oparg_T *oap;
3645 oap->op_type = OP_NOP;
3646 oap->regname = 0;
3647 oap->motion_force = NUL;
3648 oap->use_reg_one = FALSE;
3651 static void
3652 clearopbeep(oap)
3653 oparg_T *oap;
3655 clearop(oap);
3656 beep_flush();
3659 #ifdef FEAT_VISUAL
3661 * Remove the shift modifier from a special key.
3663 static void
3664 unshift_special(cap)
3665 cmdarg_T *cap;
3667 switch (cap->cmdchar)
3669 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3670 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3671 case K_S_UP: cap->cmdchar = K_UP; break;
3672 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3673 case K_S_HOME: cap->cmdchar = K_HOME; break;
3674 case K_S_END: cap->cmdchar = K_END; break;
3676 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3678 #endif
3680 #if defined(FEAT_CMDL_INFO) || defined(PROTO)
3682 * Routines for displaying a partly typed command
3685 #ifdef FEAT_VISUAL /* need room for size of Visual area */
3686 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3687 #else
3688 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3689 #endif
3690 static char_u showcmd_buf[SHOWCMD_BUFLEN];
3691 static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3692 static int showcmd_is_clear = TRUE;
3693 static int showcmd_visual = FALSE;
3695 static void display_showcmd __ARGS((void));
3697 void
3698 clear_showcmd()
3700 if (!p_sc)
3701 return;
3703 #ifdef FEAT_VISUAL
3704 if (VIsual_active && !char_avail())
3706 int i = lt(VIsual, curwin->w_cursor);
3707 long lines;
3708 colnr_T leftcol, rightcol;
3709 linenr_T top, bot;
3711 /* Show the size of the Visual area. */
3712 if (i)
3714 top = VIsual.lnum;
3715 bot = curwin->w_cursor.lnum;
3717 else
3719 top = curwin->w_cursor.lnum;
3720 bot = VIsual.lnum;
3722 # ifdef FEAT_FOLDING
3723 /* Include closed folds as a whole. */
3724 hasFolding(top, &top, NULL);
3725 hasFolding(bot, NULL, &bot);
3726 # endif
3727 lines = bot - top + 1;
3729 if (VIsual_mode == Ctrl_V)
3731 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
3732 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3733 (long)(rightcol - leftcol + 1));
3735 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3736 sprintf((char *)showcmd_buf, "%ld", lines);
3737 else
3738 sprintf((char *)showcmd_buf, "%ld", (long)(i
3739 ? curwin->w_cursor.col - VIsual.col
3740 : VIsual.col - curwin->w_cursor.col) + (*p_sel != 'e'));
3741 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3742 showcmd_visual = TRUE;
3744 else
3745 #endif
3747 showcmd_buf[0] = NUL;
3748 showcmd_visual = FALSE;
3750 /* Don't actually display something if there is nothing to clear. */
3751 if (showcmd_is_clear)
3752 return;
3755 display_showcmd();
3759 * Add 'c' to string of shown command chars.
3760 * Return TRUE if output has been written (and setcursor() has been called).
3763 add_to_showcmd(c)
3764 int c;
3766 char_u *p;
3767 int old_len;
3768 int extra_len;
3769 int overflow;
3770 #if defined(FEAT_MOUSE)
3771 int i;
3772 static int ignore[] =
3774 # ifdef FEAT_GUI
3775 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3776 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
3777 # endif
3778 K_IGNORE,
3779 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3780 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3781 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
3782 K_MOUSEDOWN, K_MOUSEUP,
3783 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
3784 K_CURSORHOLD,
3787 #endif
3789 if (!p_sc || msg_silent != 0)
3790 return FALSE;
3792 if (showcmd_visual)
3794 showcmd_buf[0] = NUL;
3795 showcmd_visual = FALSE;
3798 #if defined(FEAT_MOUSE)
3799 /* Ignore keys that are scrollbar updates and mouse clicks */
3800 if (IS_SPECIAL(c))
3801 for (i = 0; ignore[i] != 0; ++i)
3802 if (ignore[i] == c)
3803 return FALSE;
3804 #endif
3806 p = transchar(c);
3807 old_len = (int)STRLEN(showcmd_buf);
3808 extra_len = (int)STRLEN(p);
3809 overflow = old_len + extra_len - SHOWCMD_COLS;
3810 if (overflow > 0)
3811 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3812 old_len - overflow + 1);
3813 STRCAT(showcmd_buf, p);
3815 if (char_avail())
3816 return FALSE;
3818 display_showcmd();
3820 return TRUE;
3823 void
3824 add_to_showcmd_c(c)
3825 int c;
3827 if (!add_to_showcmd(c))
3828 setcursor();
3832 * Delete 'len' characters from the end of the shown command.
3834 static void
3835 del_from_showcmd(len)
3836 int len;
3838 int old_len;
3840 if (!p_sc)
3841 return;
3843 old_len = (int)STRLEN(showcmd_buf);
3844 if (len > old_len)
3845 len = old_len;
3846 showcmd_buf[old_len - len] = NUL;
3848 if (!char_avail())
3849 display_showcmd();
3853 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3854 * something and there is a partial mapping.
3856 void
3857 push_showcmd()
3859 if (p_sc)
3860 STRCPY(old_showcmd_buf, showcmd_buf);
3863 void
3864 pop_showcmd()
3866 if (!p_sc)
3867 return;
3869 STRCPY(showcmd_buf, old_showcmd_buf);
3871 display_showcmd();
3874 static void
3875 display_showcmd()
3877 int len;
3879 cursor_off();
3881 len = (int)STRLEN(showcmd_buf);
3882 if (len == 0)
3883 showcmd_is_clear = TRUE;
3884 else
3886 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3887 showcmd_is_clear = FALSE;
3891 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3892 * spaces
3894 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3896 setcursor(); /* put cursor back where it belongs */
3898 #endif
3900 #ifdef FEAT_SCROLLBIND
3902 * When "check" is FALSE, prepare for commands that scroll the window.
3903 * When "check" is TRUE, take care of scroll-binding after the window has
3904 * scrolled. Called from normal_cmd() and edit().
3906 void
3907 do_check_scrollbind(check)
3908 int check;
3910 static win_T *old_curwin = NULL;
3911 static linenr_T old_topline = 0;
3912 #ifdef FEAT_DIFF
3913 static int old_topfill = 0;
3914 #endif
3915 static buf_T *old_buf = NULL;
3916 static colnr_T old_leftcol = 0;
3918 if (check && curwin->w_p_scb)
3920 /* If a ":syncbind" command was just used, don't scroll, only reset
3921 * the values. */
3922 if (did_syncbind)
3923 did_syncbind = FALSE;
3924 else if (curwin == old_curwin)
3927 * Synchronize other windows, as necessary according to
3928 * 'scrollbind'. Don't do this after an ":edit" command, except
3929 * when 'diff' is set.
3931 if ((curwin->w_buffer == old_buf
3932 #ifdef FEAT_DIFF
3933 || curwin->w_p_diff
3934 #endif
3936 && (curwin->w_topline != old_topline
3937 #ifdef FEAT_DIFF
3938 || curwin->w_topfill != old_topfill
3939 #endif
3940 || curwin->w_leftcol != old_leftcol))
3942 check_scrollbind(curwin->w_topline - old_topline,
3943 (long)(curwin->w_leftcol - old_leftcol));
3946 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
3949 * When switching between windows, make sure that the relative
3950 * vertical offset is valid for the new window. The relative
3951 * offset is invalid whenever another 'scrollbind' window has
3952 * scrolled to a point that would force the current window to
3953 * scroll past the beginning or end of its buffer. When the
3954 * resync is performed, some of the other 'scrollbind' windows may
3955 * need to jump so that the current window's relative position is
3956 * visible on-screen.
3958 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
3960 curwin->w_scbind_pos = curwin->w_topline;
3963 old_curwin = curwin;
3964 old_topline = curwin->w_topline;
3965 #ifdef FEAT_DIFF
3966 old_topfill = curwin->w_topfill;
3967 #endif
3968 old_buf = curwin->w_buffer;
3969 old_leftcol = curwin->w_leftcol;
3973 * Synchronize any windows that have "scrollbind" set, based on the
3974 * number of rows by which the current window has changed
3975 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3977 void
3978 check_scrollbind(topline_diff, leftcol_diff)
3979 linenr_T topline_diff;
3980 long leftcol_diff;
3982 int want_ver;
3983 int want_hor;
3984 win_T *old_curwin = curwin;
3985 buf_T *old_curbuf = curbuf;
3986 #ifdef FEAT_VISUAL
3987 int old_VIsual_select = VIsual_select;
3988 int old_VIsual_active = VIsual_active;
3989 #endif
3990 colnr_T tgt_leftcol = curwin->w_leftcol;
3991 long topline;
3992 long y;
3995 * check 'scrollopt' string for vertical and horizontal scroll options
3997 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
3998 #ifdef FEAT_DIFF
3999 want_ver |= old_curwin->w_p_diff;
4000 #endif
4001 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4004 * loop through the scrollbound windows and scroll accordingly
4006 #ifdef FEAT_VISUAL
4007 VIsual_select = VIsual_active = 0;
4008 #endif
4009 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4011 curbuf = curwin->w_buffer;
4012 /* skip original window and windows with 'noscrollbind' */
4013 if (curwin != old_curwin && curwin->w_p_scb)
4016 * do the vertical scroll
4018 if (want_ver)
4020 #ifdef FEAT_DIFF
4021 if (old_curwin->w_p_diff && curwin->w_p_diff)
4023 diff_set_topline(old_curwin, curwin);
4025 else
4026 #endif
4028 curwin->w_scbind_pos += topline_diff;
4029 topline = curwin->w_scbind_pos;
4030 if (topline > curbuf->b_ml.ml_line_count)
4031 topline = curbuf->b_ml.ml_line_count;
4032 if (topline < 1)
4033 topline = 1;
4035 y = topline - curwin->w_topline;
4036 if (y > 0)
4037 scrollup(y, FALSE);
4038 else
4039 scrolldown(-y, FALSE);
4042 redraw_later(VALID);
4043 cursor_correct();
4044 #ifdef FEAT_WINDOWS
4045 curwin->w_redr_status = TRUE;
4046 #endif
4050 * do the horizontal scroll
4052 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4054 curwin->w_leftcol = tgt_leftcol;
4055 leftcol_changed();
4061 * reset current-window
4063 #ifdef FEAT_VISUAL
4064 VIsual_select = old_VIsual_select;
4065 VIsual_active = old_VIsual_active;
4066 #endif
4067 curwin = old_curwin;
4068 curbuf = old_curbuf;
4070 #endif /* #ifdef FEAT_SCROLLBIND */
4073 * Command character that's ignored.
4074 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
4075 * xon/xoff
4077 static void
4078 nv_ignore(cap)
4079 cmdarg_T *cap;
4081 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
4085 * Command character that doesn't do anything, but unlike nv_ignore() does
4086 * start edit(). Used for "startinsert" executed while starting up.
4088 /*ARGSUSED */
4089 static void
4090 nv_nop(cap)
4091 cmdarg_T *cap;
4096 * Command character doesn't exist.
4098 static void
4099 nv_error(cap)
4100 cmdarg_T *cap;
4102 clearopbeep(cap->oap);
4106 * <Help> and <F1> commands.
4108 static void
4109 nv_help(cap)
4110 cmdarg_T *cap;
4112 if (!checkclearopq(cap->oap))
4113 ex_help(NULL);
4117 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4119 static void
4120 nv_addsub(cap)
4121 cmdarg_T *cap;
4123 if (!checkclearopq(cap->oap)
4124 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4125 prep_redo_cmd(cap);
4129 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4131 static void
4132 nv_page(cap)
4133 cmdarg_T *cap;
4135 if (!checkclearop(cap->oap))
4137 #ifdef FEAT_WINDOWS
4138 if (mod_mask & MOD_MASK_CTRL)
4140 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4141 if (cap->arg == BACKWARD)
4142 goto_tabpage(-(int)cap->count1);
4143 else
4144 goto_tabpage((int)cap->count0);
4146 else
4147 #endif
4148 (void)onepage(cap->arg, cap->count1);
4153 * Implementation of "gd" and "gD" command.
4155 static void
4156 nv_gd(oap, nchar, thisblock)
4157 oparg_T *oap;
4158 int nchar;
4159 int thisblock; /* 1 for "1gd" and "1gD" */
4161 int len;
4162 char_u *ptr;
4164 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
4165 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
4166 clearopbeep(oap);
4167 #ifdef FEAT_FOLDING
4168 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4169 foldOpenCursor();
4170 #endif
4174 * Search for variable declaration of "ptr[len]".
4175 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4176 * current file ("gD").
4177 * When "thisblock" is TRUE check the {} block scope.
4178 * Return FAIL when not found.
4181 find_decl(ptr, len, locally, thisblock, searchflags)
4182 char_u *ptr;
4183 int len;
4184 int locally;
4185 int thisblock;
4186 int searchflags; /* flags passed to searchit() */
4188 char_u *pat;
4189 pos_T old_pos;
4190 pos_T par_pos;
4191 pos_T found_pos;
4192 int t;
4193 int save_p_ws;
4194 int save_p_scs;
4195 int retval = OK;
4196 int incll;
4198 if ((pat = alloc(len + 7)) == NULL)
4199 return FAIL;
4201 /* Put "\V" before the pattern to avoid that the special meaning of "."
4202 * and "~" causes trouble. */
4203 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4204 len, ptr);
4205 old_pos = curwin->w_cursor;
4206 save_p_ws = p_ws;
4207 save_p_scs = p_scs;
4208 p_ws = FALSE; /* don't wrap around end of file now */
4209 p_scs = FALSE; /* don't switch ignorecase off now */
4212 * With "gD" go to line 1.
4213 * With "gd" Search back for the start of the current function, then go
4214 * back until a blank line. If this fails go to line 1.
4216 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
4218 setpcmark(); /* Set in findpar() otherwise */
4219 curwin->w_cursor.lnum = 1;
4220 par_pos = curwin->w_cursor;
4222 else
4224 par_pos = curwin->w_cursor;
4225 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4226 --curwin->w_cursor.lnum;
4228 curwin->w_cursor.col = 0;
4230 /* Search forward for the identifier, ignore comment lines. */
4231 clearpos(&found_pos);
4232 for (;;)
4234 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
4235 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
4236 if (curwin->w_cursor.lnum >= old_pos.lnum)
4237 t = FAIL; /* match after start is failure too */
4239 if (thisblock && t != FAIL)
4241 pos_T *pos;
4243 /* Check that the block the match is in doesn't end before the
4244 * position where we started the search from. */
4245 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4246 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4247 && pos->lnum < old_pos.lnum)
4248 continue;
4251 if (t == FAIL)
4253 /* If we previously found a valid position, use it. */
4254 if (found_pos.lnum != 0)
4256 curwin->w_cursor = found_pos;
4257 t = OK;
4259 break;
4261 #ifdef FEAT_COMMENTS
4262 if (get_leader_len(ml_get_curline(), NULL, FALSE) > 0)
4264 /* Ignore this line, continue at start of next line. */
4265 ++curwin->w_cursor.lnum;
4266 curwin->w_cursor.col = 0;
4267 continue;
4269 #endif
4270 if (!locally) /* global search: use first match found */
4271 break;
4272 if (curwin->w_cursor.lnum >= par_pos.lnum)
4274 /* If we previously found a valid position, use it. */
4275 if (found_pos.lnum != 0)
4276 curwin->w_cursor = found_pos;
4277 break;
4280 /* For finding a local variable and the match is before the "{" search
4281 * to find a later match. For K&R style function declarations this
4282 * skips the function header without types. */
4283 found_pos = curwin->w_cursor;
4286 if (t == FAIL)
4288 retval = FAIL;
4289 curwin->w_cursor = old_pos;
4291 else
4293 curwin->w_set_curswant = TRUE;
4294 /* "n" searches forward now */
4295 reset_search_dir();
4298 vim_free(pat);
4299 p_ws = save_p_ws;
4300 p_scs = save_p_scs;
4302 return retval;
4306 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4307 * lines rather than lines in the file.
4308 * 'dist' must be positive.
4310 * Return OK if able to move cursor, FAIL otherwise.
4312 static int
4313 nv_screengo(oap, dir, dist)
4314 oparg_T *oap;
4315 int dir;
4316 long dist;
4318 int linelen = linetabsize(ml_get_curline());
4319 int retval = OK;
4320 int atend = FALSE;
4321 int n;
4322 int col_off1; /* margin offset for first screen line */
4323 int col_off2; /* margin offset for wrapped screen line */
4324 int width1; /* text width for first screen line */
4325 int width2; /* test width for wrapped screen line */
4327 oap->motion_type = MCHAR;
4328 oap->inclusive = FALSE;
4330 col_off1 = curwin_col_off();
4331 col_off2 = col_off1 - curwin_col_off2();
4332 width1 = W_WIDTH(curwin) - col_off1;
4333 width2 = W_WIDTH(curwin) - col_off2;
4335 #ifdef FEAT_VERTSPLIT
4336 if (curwin->w_width != 0)
4338 #endif
4340 * Instead of sticking at the last character of the buffer line we
4341 * try to stick in the last column of the screen.
4343 if (curwin->w_curswant == MAXCOL)
4345 atend = TRUE;
4346 validate_virtcol();
4347 if (width1 <= 0)
4348 curwin->w_curswant = 0;
4349 else
4351 curwin->w_curswant = width1 - 1;
4352 if (curwin->w_virtcol > curwin->w_curswant)
4353 curwin->w_curswant += ((curwin->w_virtcol
4354 - curwin->w_curswant - 1) / width2 + 1) * width2;
4357 else
4359 if (linelen > width1)
4360 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4361 else
4362 n = width1;
4363 if (curwin->w_curswant > (colnr_T)n + 1)
4364 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4365 * width2;
4368 while (dist--)
4370 if (dir == BACKWARD)
4372 if ((long)curwin->w_curswant >= width2)
4373 /* move back within line */
4374 curwin->w_curswant -= width2;
4375 else
4377 /* to previous line */
4378 if (curwin->w_cursor.lnum == 1)
4380 retval = FAIL;
4381 break;
4383 --curwin->w_cursor.lnum;
4384 #ifdef FEAT_FOLDING
4385 /* Move to the start of a closed fold. Don't do that when
4386 * 'foldopen' contains "all": it will open in a moment. */
4387 if (!(fdo_flags & FDO_ALL))
4388 (void)hasFolding(curwin->w_cursor.lnum,
4389 &curwin->w_cursor.lnum, NULL);
4390 #endif
4391 linelen = linetabsize(ml_get_curline());
4392 if (linelen > width1)
4393 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4394 + 1) * width2;
4397 else /* dir == FORWARD */
4399 if (linelen > width1)
4400 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4401 else
4402 n = width1;
4403 if (curwin->w_curswant + width2 < (colnr_T)n)
4404 /* move forward within line */
4405 curwin->w_curswant += width2;
4406 else
4408 /* to next line */
4409 #ifdef FEAT_FOLDING
4410 /* Move to the end of a closed fold. */
4411 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4412 &curwin->w_cursor.lnum);
4413 #endif
4414 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4416 retval = FAIL;
4417 break;
4419 curwin->w_cursor.lnum++;
4420 curwin->w_curswant %= width2;
4424 #ifdef FEAT_VERTSPLIT
4426 #endif
4428 coladvance(curwin->w_curswant);
4430 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4431 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4434 * Check for landing on a character that got split at the end of the
4435 * last line. We want to advance a screenline, not end up in the same
4436 * screenline or move two screenlines.
4438 validate_virtcol();
4439 if (curwin->w_virtcol > curwin->w_curswant
4440 && (curwin->w_curswant < (colnr_T)width1
4441 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4442 : ((curwin->w_curswant - width1) % width2
4443 > (colnr_T)width2 / 2)))
4444 --curwin->w_cursor.col;
4446 #endif
4448 if (atend)
4449 curwin->w_curswant = MAXCOL; /* stick in the last column */
4451 return retval;
4454 #ifdef FEAT_MOUSE
4456 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4457 * when Shift or Ctrl is used.
4458 * K_MOUSEUP (cap->arg == TRUE) or K_MOUSEDOWN (cap->arg == FALSE)
4460 static void
4461 nv_mousescroll(cap)
4462 cmdarg_T *cap;
4464 # ifdef FEAT_GUI_SCROLL_WHEEL_FORCE
4465 int scroll_wheel_force = 0;
4466 # endif
4467 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4468 win_T *old_curwin = curwin;
4470 /* Currently we only get the mouse coordinates in the GUI. */
4471 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4473 int row, col;
4475 row = mouse_row;
4476 col = mouse_col;
4478 /* find the window at the pointer coordinates */
4479 curwin = mouse_find_win(&row, &col);
4480 curbuf = curwin->w_buffer;
4482 # endif
4484 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4486 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4488 else
4490 # ifdef FEAT_GUI_SCROLL_WHEEL_FORCE
4491 if (gui.in_use && gui.scroll_wheel_force >= 1) {
4492 scroll_wheel_force = gui.scroll_wheel_force;
4493 if (scroll_wheel_force > 1000)
4494 scroll_wheel_force = 1000;
4496 cap->count1 = scroll_wheel_force;
4497 cap->count0 = scroll_wheel_force;
4498 } else {
4499 cap->count1 = 3;
4500 cap->count0 = 3;
4502 # else
4503 cap->count1 = 3;
4504 cap->count0 = 3;
4505 # endif
4506 nv_scroll_line(cap);
4509 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4510 curwin->w_redr_status = TRUE;
4512 curwin = old_curwin;
4513 curbuf = curwin->w_buffer;
4514 # endif
4518 * Mouse clicks and drags.
4520 static void
4521 nv_mouse(cap)
4522 cmdarg_T *cap;
4524 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4526 #endif
4529 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4530 * cap->arg must be TRUE for CTRL-E.
4532 static void
4533 nv_scroll_line(cap)
4534 cmdarg_T *cap;
4536 if (!checkclearop(cap->oap))
4537 scroll_redraw(cap->arg, cap->count1);
4541 * Scroll "count" lines up or down, and redraw.
4543 void
4544 scroll_redraw(up, count)
4545 int up;
4546 long count;
4548 linenr_T prev_topline = curwin->w_topline;
4549 #ifdef FEAT_DIFF
4550 int prev_topfill = curwin->w_topfill;
4551 #endif
4552 linenr_T prev_lnum = curwin->w_cursor.lnum;
4554 if (up)
4555 scrollup(count, TRUE);
4556 else
4557 scrolldown(count, TRUE);
4558 if (p_so)
4560 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4561 * valid, otherwise the screen jumps back at the end of the file. */
4562 cursor_correct();
4563 check_cursor_moved(curwin);
4564 curwin->w_valid |= VALID_TOPLINE;
4566 /* If moved back to where we were, at least move the cursor, otherwise
4567 * we get stuck at one position. Don't move the cursor up if the
4568 * first line of the buffer is already on the screen */
4569 while (curwin->w_topline == prev_topline
4570 #ifdef FEAT_DIFF
4571 && curwin->w_topfill == prev_topfill
4572 #endif
4575 if (up)
4577 if (curwin->w_cursor.lnum > prev_lnum
4578 || cursor_down(1L, FALSE) == FAIL)
4579 break;
4581 else
4583 if (curwin->w_cursor.lnum < prev_lnum
4584 || prev_topline == 1L
4585 || cursor_up(1L, FALSE) == FAIL)
4586 break;
4588 /* Mark w_topline as valid, otherwise the screen jumps back at the
4589 * end of the file. */
4590 check_cursor_moved(curwin);
4591 curwin->w_valid |= VALID_TOPLINE;
4594 if (curwin->w_cursor.lnum != prev_lnum)
4595 coladvance(curwin->w_curswant);
4596 redraw_later(VALID);
4600 * Commands that start with "z".
4602 static void
4603 nv_zet(cap)
4604 cmdarg_T *cap;
4606 long n;
4607 colnr_T col;
4608 int nchar = cap->nchar;
4609 #ifdef FEAT_FOLDING
4610 long old_fdl = curwin->w_p_fdl;
4611 int old_fen = curwin->w_p_fen;
4612 #endif
4613 #ifdef FEAT_SPELL
4614 int undo = FALSE;
4615 #endif
4617 if (VIM_ISDIGIT(nchar))
4620 * "z123{nchar}": edit the count before obtaining {nchar}
4622 if (checkclearop(cap->oap))
4623 return;
4624 n = nchar - '0';
4625 for (;;)
4627 #ifdef USE_ON_FLY_SCROLL
4628 dont_scroll = TRUE; /* disallow scrolling here */
4629 #endif
4630 ++no_mapping;
4631 ++allow_keys; /* no mapping for nchar, but allow key codes */
4632 nchar = plain_vgetc();
4633 #ifdef FEAT_LANGMAP
4634 LANGMAP_ADJUST(nchar, TRUE);
4635 #endif
4636 --no_mapping;
4637 --allow_keys;
4638 #ifdef FEAT_CMDL_INFO
4639 (void)add_to_showcmd(nchar);
4640 #endif
4641 if (nchar == K_DEL || nchar == K_KDEL)
4642 n /= 10;
4643 else if (VIM_ISDIGIT(nchar))
4644 n = n * 10 + (nchar - '0');
4645 else if (nchar == CAR)
4647 #ifdef FEAT_GUI
4648 need_mouse_correct = TRUE;
4649 #endif
4650 win_setheight((int)n);
4651 break;
4653 else if (nchar == 'l'
4654 || nchar == 'h'
4655 || nchar == K_LEFT
4656 || nchar == K_RIGHT)
4658 cap->count1 = n ? n * cap->count1 : cap->count1;
4659 goto dozet;
4661 else
4663 clearopbeep(cap->oap);
4664 break;
4667 cap->oap->op_type = OP_NOP;
4668 return;
4671 dozet:
4672 if (
4673 #ifdef FEAT_FOLDING
4674 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4675 * and "zC" only in Visual mode. "zj" and "zk" are motion
4676 * commands. */
4677 cap->nchar != 'f' && cap->nchar != 'F'
4678 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4679 && cap->nchar != 'j' && cap->nchar != 'k'
4681 #endif
4682 checkclearop(cap->oap))
4683 return;
4686 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4687 * If line number given, set cursor.
4689 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4690 && cap->count0
4691 && cap->count0 != curwin->w_cursor.lnum)
4693 setpcmark();
4694 if (cap->count0 > curbuf->b_ml.ml_line_count)
4695 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4696 else
4697 curwin->w_cursor.lnum = cap->count0;
4698 check_cursor_col();
4701 switch (nchar)
4703 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4704 case '+':
4705 if (cap->count0 == 0)
4707 /* No count given: put cursor at the line below screen */
4708 validate_botline(); /* make sure w_botline is valid */
4709 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4710 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4711 else
4712 curwin->w_cursor.lnum = curwin->w_botline;
4714 /* FALLTHROUGH */
4715 case NL:
4716 case CAR:
4717 case K_KENTER:
4718 beginline(BL_WHITE | BL_FIX);
4719 /* FALLTHROUGH */
4721 case 't': scroll_cursor_top(0, TRUE);
4722 redraw_later(VALID);
4723 break;
4725 /* "z." and "zz": put cursor in middle of screen */
4726 case '.': beginline(BL_WHITE | BL_FIX);
4727 /* FALLTHROUGH */
4729 case 'z': scroll_cursor_halfway(TRUE);
4730 redraw_later(VALID);
4731 break;
4733 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4734 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4735 * when <count> is at bottom of window, and puts that one at
4736 * bottom of window. */
4737 if (cap->count0 != 0)
4739 scroll_cursor_bot(0, TRUE);
4740 curwin->w_cursor.lnum = curwin->w_topline;
4742 else if (curwin->w_topline == 1)
4743 curwin->w_cursor.lnum = 1;
4744 else
4745 curwin->w_cursor.lnum = curwin->w_topline - 1;
4746 /* FALLTHROUGH */
4747 case '-':
4748 beginline(BL_WHITE | BL_FIX);
4749 /* FALLTHROUGH */
4751 case 'b': scroll_cursor_bot(0, TRUE);
4752 redraw_later(VALID);
4753 break;
4755 /* "zH" - scroll screen right half-page */
4756 case 'H':
4757 cap->count1 *= W_WIDTH(curwin) / 2;
4758 /* FALLTHROUGH */
4760 /* "zh" - scroll screen to the right */
4761 case 'h':
4762 case K_LEFT:
4763 if (!curwin->w_p_wrap)
4765 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4766 curwin->w_leftcol = 0;
4767 else
4768 curwin->w_leftcol -= (colnr_T)cap->count1;
4769 leftcol_changed();
4771 break;
4773 /* "zL" - scroll screen left half-page */
4774 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4775 /* FALLTHROUGH */
4777 /* "zl" - scroll screen to the left */
4778 case 'l':
4779 case K_RIGHT:
4780 if (!curwin->w_p_wrap)
4782 /* scroll the window left */
4783 curwin->w_leftcol += (colnr_T)cap->count1;
4784 leftcol_changed();
4786 break;
4788 /* "zs" - scroll screen, cursor at the start */
4789 case 's': if (!curwin->w_p_wrap)
4791 #ifdef FEAT_FOLDING
4792 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4793 col = 0; /* like the cursor is in col 0 */
4794 else
4795 #endif
4796 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4797 if ((long)col > p_siso)
4798 col -= p_siso;
4799 else
4800 col = 0;
4801 if (curwin->w_leftcol != col)
4803 curwin->w_leftcol = col;
4804 redraw_later(NOT_VALID);
4807 break;
4809 /* "ze" - scroll screen, cursor at the end */
4810 case 'e': if (!curwin->w_p_wrap)
4812 #ifdef FEAT_FOLDING
4813 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4814 col = 0; /* like the cursor is in col 0 */
4815 else
4816 #endif
4817 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4818 n = W_WIDTH(curwin) - curwin_col_off();
4819 if ((long)col + p_siso < n)
4820 col = 0;
4821 else
4822 col = col + p_siso - n + 1;
4823 if (curwin->w_leftcol != col)
4825 curwin->w_leftcol = col;
4826 redraw_later(NOT_VALID);
4829 break;
4831 #ifdef FEAT_FOLDING
4832 /* "zF": create fold command */
4833 /* "zf": create fold operator */
4834 case 'F':
4835 case 'f': if (foldManualAllowed(TRUE))
4837 cap->nchar = 'f';
4838 nv_operator(cap);
4839 curwin->w_p_fen = TRUE;
4841 /* "zF" is like "zfzf" */
4842 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4844 nv_operator(cap);
4845 finish_op = TRUE;
4848 else
4849 clearopbeep(cap->oap);
4850 break;
4852 /* "zd": delete fold at cursor */
4853 /* "zD": delete fold at cursor recursively */
4854 case 'd':
4855 case 'D': if (foldManualAllowed(FALSE))
4857 if (VIsual_active)
4858 nv_operator(cap);
4859 else
4860 deleteFold(curwin->w_cursor.lnum,
4861 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4863 break;
4865 /* "zE": erease all folds */
4866 case 'E': if (foldmethodIsManual(curwin))
4868 clearFolding(curwin);
4869 changed_window_setting();
4871 else if (foldmethodIsMarker(curwin))
4872 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4873 TRUE, FALSE);
4874 else
4875 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4876 break;
4878 /* "zn": fold none: reset 'foldenable' */
4879 case 'n': curwin->w_p_fen = FALSE;
4880 break;
4882 /* "zN": fold Normal: set 'foldenable' */
4883 case 'N': curwin->w_p_fen = TRUE;
4884 break;
4886 /* "zi": invert folding: toggle 'foldenable' */
4887 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4888 break;
4890 /* "za": open closed fold or close open fold at cursor */
4891 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4892 openFold(curwin->w_cursor.lnum, cap->count1);
4893 else
4895 closeFold(curwin->w_cursor.lnum, cap->count1);
4896 curwin->w_p_fen = TRUE;
4898 break;
4900 /* "zA": open fold at cursor recursively */
4901 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4902 openFoldRecurse(curwin->w_cursor.lnum);
4903 else
4905 closeFoldRecurse(curwin->w_cursor.lnum);
4906 curwin->w_p_fen = TRUE;
4908 break;
4910 /* "zo": open fold at cursor or Visual area */
4911 case 'o': if (VIsual_active)
4912 nv_operator(cap);
4913 else
4914 openFold(curwin->w_cursor.lnum, cap->count1);
4915 break;
4917 /* "zO": open fold recursively */
4918 case 'O': if (VIsual_active)
4919 nv_operator(cap);
4920 else
4921 openFoldRecurse(curwin->w_cursor.lnum);
4922 break;
4924 /* "zc": close fold at cursor or Visual area */
4925 case 'c': if (VIsual_active)
4926 nv_operator(cap);
4927 else
4928 closeFold(curwin->w_cursor.lnum, cap->count1);
4929 curwin->w_p_fen = TRUE;
4930 break;
4932 /* "zC": close fold recursively */
4933 case 'C': if (VIsual_active)
4934 nv_operator(cap);
4935 else
4936 closeFoldRecurse(curwin->w_cursor.lnum);
4937 curwin->w_p_fen = TRUE;
4938 break;
4940 /* "zv": open folds at the cursor */
4941 case 'v': foldOpenCursor();
4942 break;
4944 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
4945 case 'x': curwin->w_p_fen = TRUE;
4946 newFoldLevel(); /* update right now */
4947 foldOpenCursor();
4948 break;
4950 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
4951 case 'X': curwin->w_p_fen = TRUE;
4952 old_fdl = -1; /* force an update */
4953 break;
4955 /* "zm": fold more */
4956 case 'm': if (curwin->w_p_fdl > 0)
4957 --curwin->w_p_fdl;
4958 old_fdl = -1; /* force an update */
4959 curwin->w_p_fen = TRUE;
4960 break;
4962 /* "zM": close all folds */
4963 case 'M': curwin->w_p_fdl = 0;
4964 old_fdl = -1; /* force an update */
4965 curwin->w_p_fen = TRUE;
4966 break;
4968 /* "zr": reduce folding */
4969 case 'r': ++curwin->w_p_fdl;
4970 break;
4972 /* "zR": open all folds */
4973 case 'R': curwin->w_p_fdl = getDeepestNesting();
4974 old_fdl = -1; /* force an update */
4975 break;
4977 case 'j': /* "zj" move to next fold downwards */
4978 case 'k': /* "zk" move to next fold upwards */
4979 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
4980 cap->count1) == FAIL)
4981 clearopbeep(cap->oap);
4982 break;
4984 #endif /* FEAT_FOLDING */
4986 #ifdef FEAT_SPELL
4987 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
4988 ++no_mapping;
4989 ++allow_keys; /* no mapping for nchar, but allow key codes */
4990 nchar = plain_vgetc();
4991 #ifdef FEAT_LANGMAP
4992 LANGMAP_ADJUST(nchar, TRUE);
4993 #endif
4994 --no_mapping;
4995 --allow_keys;
4996 #ifdef FEAT_CMDL_INFO
4997 (void)add_to_showcmd(nchar);
4998 #endif
4999 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5001 clearopbeep(cap->oap);
5002 break;
5004 undo = TRUE;
5005 /*FALLTHROUGH*/
5007 case 'g': /* "zg": add good word to word list */
5008 case 'w': /* "zw": add wrong word to word list */
5009 case 'G': /* "zG": add good word to temp word list */
5010 case 'W': /* "zW": add wrong word to temp word list */
5012 char_u *ptr = NULL;
5013 int len;
5015 if (checkclearop(cap->oap))
5016 break;
5017 # ifdef FEAT_VISUAL
5018 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5019 == FAIL)
5020 return;
5021 # endif
5022 if (ptr == NULL)
5024 pos_T pos = curwin->w_cursor;
5026 /* Find bad word under the cursor. */
5027 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
5028 if (len != 0 && curwin->w_cursor.col <= pos.col)
5029 ptr = ml_get_pos(&curwin->w_cursor);
5030 curwin->w_cursor = pos;
5033 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5034 FIND_IDENT)) == 0)
5035 return;
5036 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
5037 (nchar == 'G' || nchar == 'W')
5038 ? 0 : (int)cap->count1,
5039 undo);
5041 break;
5043 case '=': /* "z=": suggestions for a badly spelled word */
5044 if (!checkclearop(cap->oap))
5045 spell_suggest((int)cap->count0);
5046 break;
5047 #endif
5049 default: clearopbeep(cap->oap);
5052 #ifdef FEAT_FOLDING
5053 /* Redraw when 'foldenable' changed */
5054 if (old_fen != curwin->w_p_fen)
5056 # ifdef FEAT_DIFF
5057 win_T *wp;
5059 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5061 /* Adjust 'foldenable' in diff-synced windows. */
5062 FOR_ALL_WINDOWS(wp)
5064 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5066 wp->w_p_fen = curwin->w_p_fen;
5067 changed_window_setting_win(wp);
5071 # endif
5072 changed_window_setting();
5075 /* Redraw when 'foldlevel' changed. */
5076 if (old_fdl != curwin->w_p_fdl)
5077 newFoldLevel();
5078 #endif
5081 #ifdef FEAT_GUI
5083 * Vertical scrollbar movement.
5085 static void
5086 nv_ver_scrollbar(cap)
5087 cmdarg_T *cap;
5089 if (cap->oap->op_type != OP_NOP)
5090 clearopbeep(cap->oap);
5092 /* Even if an operator was pending, we still want to scroll */
5093 gui_do_scroll();
5097 * Horizontal scrollbar movement.
5099 static void
5100 nv_hor_scrollbar(cap)
5101 cmdarg_T *cap;
5103 if (cap->oap->op_type != OP_NOP)
5104 clearopbeep(cap->oap);
5106 /* Even if an operator was pending, we still want to scroll */
5107 gui_do_horiz_scroll();
5109 #endif
5111 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
5113 * Click in GUI tab.
5115 static void
5116 nv_tabline(cap)
5117 cmdarg_T *cap;
5119 if (cap->oap->op_type != OP_NOP)
5120 clearopbeep(cap->oap);
5122 /* Even if an operator was pending, we still want to jump tabs. */
5123 goto_tabpage(current_tab);
5127 * Selected item in tab line menu.
5129 static void
5130 nv_tabmenu(cap)
5131 cmdarg_T *cap;
5133 if (cap->oap->op_type != OP_NOP)
5134 clearopbeep(cap->oap);
5136 /* Even if an operator was pending, we still want to jump tabs. */
5137 handle_tabmenu();
5141 * Handle selecting an item of the GUI tab line menu.
5142 * Used in Normal and Insert mode.
5144 void
5145 handle_tabmenu()
5147 switch (current_tabmenu)
5149 case TABLINE_MENU_CLOSE:
5150 if (current_tab == 0)
5151 do_cmdline_cmd((char_u *)"tabclose");
5152 else
5154 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5155 current_tab);
5156 do_cmdline_cmd(IObuff);
5158 break;
5160 case TABLINE_MENU_NEW:
5161 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5162 current_tab > 0 ? current_tab - 1 : 999);
5163 do_cmdline_cmd(IObuff);
5164 break;
5166 case TABLINE_MENU_OPEN:
5167 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5168 current_tab > 0 ? current_tab - 1 : 999);
5169 do_cmdline_cmd(IObuff);
5170 break;
5173 #endif
5176 * "Q" command.
5178 static void
5179 nv_exmode(cap)
5180 cmdarg_T *cap;
5183 * Ignore 'Q' in Visual mode, just give a beep.
5185 #ifdef FEAT_VISUAL
5186 if (VIsual_active)
5187 vim_beep();
5188 else
5189 #endif
5190 if (!checkclearop(cap->oap))
5191 do_exmode(FALSE);
5195 * Handle a ":" command.
5197 static void
5198 nv_colon(cap)
5199 cmdarg_T *cap;
5201 int old_p_im;
5203 #ifdef FEAT_VISUAL
5204 if (VIsual_active)
5205 nv_operator(cap);
5206 else
5207 #endif
5209 if (cap->oap->op_type != OP_NOP)
5211 /* Using ":" as a movement is characterwise exclusive. */
5212 cap->oap->motion_type = MCHAR;
5213 cap->oap->inclusive = FALSE;
5215 else if (cap->count0)
5217 /* translate "count:" into ":.,.+(count - 1)" */
5218 stuffcharReadbuff('.');
5219 if (cap->count0 > 1)
5221 stuffReadbuff((char_u *)",.+");
5222 stuffnumReadbuff((long)cap->count0 - 1L);
5226 /* When typing, don't type below an old message */
5227 if (KeyTyped)
5228 compute_cmdrow();
5230 old_p_im = p_im;
5232 /* get a command line and execute it */
5233 do_cmdline(NULL, getexline, NULL,
5234 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5236 /* If 'insertmode' changed, enter or exit Insert mode */
5237 if (p_im != old_p_im)
5239 if (p_im)
5240 restart_edit = 'i';
5241 else
5242 restart_edit = 0;
5245 /* The start of the operator may have become invalid by the Ex
5246 * command. */
5247 if (cap->oap->op_type != OP_NOP
5248 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5249 || cap->oap->start.col >
5250 STRLEN(ml_get(cap->oap->start.lnum))))
5251 clearopbeep(cap->oap);
5256 * Handle CTRL-G command.
5258 static void
5259 nv_ctrlg(cap)
5260 cmdarg_T *cap;
5262 #ifdef FEAT_VISUAL
5263 if (VIsual_active) /* toggle Selection/Visual mode */
5265 VIsual_select = !VIsual_select;
5266 showmode();
5268 else
5269 #endif
5270 if (!checkclearop(cap->oap))
5271 /* print full name if count given or :cd used */
5272 fileinfo((int)cap->count0, FALSE, TRUE);
5276 * Handle CTRL-H <Backspace> command.
5278 static void
5279 nv_ctrlh(cap)
5280 cmdarg_T *cap;
5282 #ifdef FEAT_VISUAL
5283 if (VIsual_active && VIsual_select)
5285 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5286 v_visop(cap);
5288 else
5289 #endif
5290 nv_left(cap);
5294 * CTRL-L: clear screen and redraw.
5296 static void
5297 nv_clear(cap)
5298 cmdarg_T *cap;
5300 if (!checkclearop(cap->oap))
5302 #if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5304 * Right now, the BeBox doesn't seem to have an easy way to detect
5305 * window resizing, so we cheat and make the user detect it
5306 * manually with CTRL-L instead
5308 ui_get_shellsize();
5309 #endif
5310 #ifdef FEAT_SYN_HL
5311 /* Clear all syntax states to force resyncing. */
5312 syn_stack_free_all(curbuf);
5313 #endif
5314 redraw_later(CLEAR);
5319 * CTRL-O: In Select mode: switch to Visual mode for one command.
5320 * Otherwise: Go to older pcmark.
5322 static void
5323 nv_ctrlo(cap)
5324 cmdarg_T *cap;
5326 #ifdef FEAT_VISUAL
5327 if (VIsual_active && VIsual_select)
5329 VIsual_select = FALSE;
5330 showmode();
5331 restart_VIsual_select = 2; /* restart Select mode later */
5333 else
5334 #endif
5336 cap->count1 = -cap->count1;
5337 nv_pcmark(cap);
5342 * CTRL-^ command, short for ":e #"
5344 static void
5345 nv_hat(cap)
5346 cmdarg_T *cap;
5348 if (!checkclearopq(cap->oap))
5349 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5350 GETF_SETMARK|GETF_ALT, FALSE);
5354 * "Z" commands.
5356 static void
5357 nv_Zet(cap)
5358 cmdarg_T *cap;
5360 if (!checkclearopq(cap->oap))
5362 switch (cap->nchar)
5364 /* "ZZ": equivalent to ":x". */
5365 case 'Z': do_cmdline_cmd((char_u *)"x");
5366 break;
5368 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5369 case 'Q': do_cmdline_cmd((char_u *)"q!");
5370 break;
5372 default: clearopbeep(cap->oap);
5377 #if defined(FEAT_WINDOWS) || defined(PROTO)
5379 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5381 void
5382 do_nv_ident(c1, c2)
5383 int c1;
5384 int c2;
5386 oparg_T oa;
5387 cmdarg_T ca;
5389 clear_oparg(&oa);
5390 vim_memset(&ca, 0, sizeof(ca));
5391 ca.oap = &oa;
5392 ca.cmdchar = c1;
5393 ca.nchar = c2;
5394 nv_ident(&ca);
5396 #endif
5399 * Handle the commands that use the word under the cursor.
5400 * [g] CTRL-] :ta to current identifier
5401 * [g] 'K' run program for current identifier
5402 * [g] '*' / to current identifier or string
5403 * [g] '#' ? to current identifier or string
5404 * g ']' :tselect for current identifier
5406 static void
5407 nv_ident(cap)
5408 cmdarg_T *cap;
5410 char_u *ptr = NULL;
5411 char_u *buf;
5412 char_u *p;
5413 char_u *kp; /* value of 'keywordprg' */
5414 int kp_help; /* 'keywordprg' is ":help" */
5415 int n = 0; /* init for GCC */
5416 int cmdchar;
5417 int g_cmd; /* "g" command */
5418 char_u *aux_ptr;
5419 int isman;
5420 int isman_s;
5422 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5424 cmdchar = cap->nchar;
5425 g_cmd = TRUE;
5427 else
5429 cmdchar = cap->cmdchar;
5430 g_cmd = FALSE;
5433 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5434 cmdchar = '#';
5437 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5439 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5441 #ifdef FEAT_VISUAL
5442 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5443 return;
5444 #endif
5445 if (checkclearopq(cap->oap))
5446 return;
5449 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5450 (cmdchar == '*' || cmdchar == '#')
5451 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5453 clearop(cap->oap);
5454 return;
5457 /* Allocate buffer to put the command in. Inserting backslashes can
5458 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5459 * and some numbers. */
5460 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5461 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5462 || STRCMP(kp, ":help") == 0);
5463 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5464 if (buf == NULL)
5465 return;
5466 buf[0] = NUL;
5468 switch (cmdchar)
5470 case '*':
5471 case '#':
5473 * Put cursor at start of word, makes search skip the word
5474 * under the cursor.
5475 * Call setpcmark() first, so "*``" puts the cursor back where
5476 * it was.
5478 setpcmark();
5479 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5481 if (!g_cmd && vim_iswordp(ptr))
5482 STRCPY(buf, "\\<");
5483 no_smartcase = TRUE; /* don't use 'smartcase' now */
5484 break;
5486 case 'K':
5487 if (kp_help)
5488 STRCPY(buf, "he! ");
5489 else
5491 /* An external command will probably use an argument starting
5492 * with "-" as an option. To avoid trouble we skip the "-". */
5493 while (*ptr == '-')
5494 ++ptr;
5496 /* When a count is given, turn it into a range. Is this
5497 * really what we want? */
5498 isman = (STRCMP(kp, "man") == 0);
5499 isman_s = (STRCMP(kp, "man -s") == 0);
5500 if (cap->count0 != 0 && !(isman || isman_s))
5501 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5503 STRCAT(buf, "! ");
5504 if (cap->count0 == 0 && isman_s)
5505 STRCAT(buf, "man");
5506 else
5507 STRCAT(buf, kp);
5508 STRCAT(buf, " ");
5509 if (cap->count0 != 0 && (isman || isman_s))
5511 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5512 STRCAT(buf, " ");
5515 break;
5517 case ']':
5518 #ifdef FEAT_CSCOPE
5519 if (p_cst)
5520 STRCPY(buf, "cstag ");
5521 else
5522 #endif
5523 STRCPY(buf, "ts ");
5524 break;
5526 default:
5527 if (curbuf->b_help)
5528 STRCPY(buf, "he! ");
5529 else if (g_cmd)
5530 STRCPY(buf, "tj ");
5531 else
5532 sprintf((char *)buf, "%ldta ", cap->count0);
5536 * Now grab the chars in the identifier
5538 if (cmdchar == 'K' && !kp_help)
5540 /* Escape the argument properly for a shell command */
5541 p = vim_strsave_shellescape(ptr, TRUE);
5542 if (p == NULL)
5544 vim_free(buf);
5545 return;
5547 buf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5548 if (buf == NULL)
5550 vim_free(buf);
5551 vim_free(p);
5552 return;
5554 STRCAT(buf, p);
5555 vim_free(p);
5557 else
5559 if (cmdchar == '*')
5560 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5561 else if (cmdchar == '#')
5562 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
5563 else
5564 /* Don't escape spaces and Tabs in a tag with a backslash */
5565 aux_ptr = (char_u *)"\\|\"\n*?[";
5567 p = buf + STRLEN(buf);
5568 while (n-- > 0)
5570 /* put a backslash before \ and some others */
5571 if (vim_strchr(aux_ptr, *ptr) != NULL)
5572 *p++ = '\\';
5573 #ifdef FEAT_MBYTE
5574 /* When current byte is a part of multibyte character, copy all
5575 * bytes of that character. */
5576 if (has_mbyte)
5578 int i;
5579 int len = (*mb_ptr2len)(ptr) - 1;
5581 for (i = 0; i < len && n >= 1; ++i, --n)
5582 *p++ = *ptr++;
5584 #endif
5585 *p++ = *ptr++;
5587 *p = NUL;
5591 * Execute the command.
5593 if (cmdchar == '*' || cmdchar == '#')
5595 if (!g_cmd && (
5596 #ifdef FEAT_MBYTE
5597 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5598 #endif
5599 vim_iswordc(ptr[-1])))
5600 STRCAT(buf, "\\>");
5601 #ifdef FEAT_CMDHIST
5602 /* put pattern in search history */
5603 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5604 #endif
5605 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5607 else
5608 do_cmdline_cmd(buf);
5610 vim_free(buf);
5613 #if defined(FEAT_VISUAL) || defined(PROTO)
5615 * Get visually selected text, within one line only.
5616 * Returns FAIL if more than one line selected.
5619 get_visual_text(cap, pp, lenp)
5620 cmdarg_T *cap;
5621 char_u **pp; /* return: start of selected text */
5622 int *lenp; /* return: length of selected text */
5624 if (VIsual_mode != 'V')
5625 unadjust_for_sel();
5626 if (VIsual.lnum != curwin->w_cursor.lnum)
5628 if (cap != NULL)
5629 clearopbeep(cap->oap);
5630 return FAIL;
5632 if (VIsual_mode == 'V')
5634 *pp = ml_get_curline();
5635 *lenp = (int)STRLEN(*pp);
5637 else
5639 if (lt(curwin->w_cursor, VIsual))
5641 *pp = ml_get_pos(&curwin->w_cursor);
5642 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5644 else
5646 *pp = ml_get_pos(&VIsual);
5647 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5649 #ifdef FEAT_MBYTE
5650 if (has_mbyte)
5651 /* Correct the length to include the whole last character. */
5652 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
5653 #endif
5655 reset_VIsual_and_resel();
5656 return OK;
5658 #endif
5661 * CTRL-T: backwards in tag stack
5663 static void
5664 nv_tagpop(cap)
5665 cmdarg_T *cap;
5667 if (!checkclearopq(cap->oap))
5668 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5672 * Handle scrolling command 'H', 'L' and 'M'.
5674 static void
5675 nv_scroll(cap)
5676 cmdarg_T *cap;
5678 int used = 0;
5679 long n;
5680 #ifdef FEAT_FOLDING
5681 linenr_T lnum;
5682 #endif
5683 int half;
5685 cap->oap->motion_type = MLINE;
5686 setpcmark();
5688 if (cap->cmdchar == 'L')
5690 validate_botline(); /* make sure curwin->w_botline is valid */
5691 curwin->w_cursor.lnum = curwin->w_botline - 1;
5692 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5693 curwin->w_cursor.lnum = 1;
5694 else
5696 #ifdef FEAT_FOLDING
5697 if (hasAnyFolding(curwin))
5699 /* Count a fold for one screen line. */
5700 for (n = cap->count1 - 1; n > 0
5701 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5703 (void)hasFolding(curwin->w_cursor.lnum,
5704 &curwin->w_cursor.lnum, NULL);
5705 --curwin->w_cursor.lnum;
5708 else
5709 #endif
5710 curwin->w_cursor.lnum -= cap->count1 - 1;
5713 else
5715 if (cap->cmdchar == 'M')
5717 #ifdef FEAT_DIFF
5718 /* Don't count filler lines above the window. */
5719 used -= diff_check_fill(curwin, curwin->w_topline)
5720 - curwin->w_topfill;
5721 #endif
5722 validate_botline(); /* make sure w_empty_rows is valid */
5723 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5724 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5726 #ifdef FEAT_DIFF
5727 /* Count half he number of filler lines to be "below this
5728 * line" and half to be "above the next line". */
5729 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5730 + n) / 2 >= half)
5732 --n;
5733 break;
5735 #endif
5736 used += plines(curwin->w_topline + n);
5737 if (used >= half)
5738 break;
5739 #ifdef FEAT_FOLDING
5740 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5741 n = lnum - curwin->w_topline;
5742 #endif
5744 if (n > 0 && used > curwin->w_height)
5745 --n;
5747 else /* (cap->cmdchar == 'H') */
5749 n = cap->count1 - 1;
5750 #ifdef FEAT_FOLDING
5751 if (hasAnyFolding(curwin))
5753 /* Count a fold for one screen line. */
5754 lnum = curwin->w_topline;
5755 while (n-- > 0 && lnum < curwin->w_botline - 1)
5757 hasFolding(lnum, NULL, &lnum);
5758 ++lnum;
5760 n = lnum - curwin->w_topline;
5762 #endif
5764 curwin->w_cursor.lnum = curwin->w_topline + n;
5765 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5766 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5769 cursor_correct(); /* correct for 'so' */
5770 beginline(BL_SOL | BL_FIX);
5774 * Cursor right commands.
5776 static void
5777 nv_right(cap)
5778 cmdarg_T *cap;
5780 long n;
5781 #ifdef FEAT_VISUAL
5782 int PAST_LINE;
5783 #else
5784 # define PAST_LINE 0
5785 #endif
5787 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5789 /* <C-Right> and <S-Right> move a word or WORD right */
5790 if (mod_mask & MOD_MASK_CTRL)
5791 cap->arg = TRUE;
5792 nv_wordcmd(cap);
5793 return;
5796 cap->oap->motion_type = MCHAR;
5797 cap->oap->inclusive = FALSE;
5798 #ifdef FEAT_VISUAL
5799 PAST_LINE = (VIsual_active && *p_sel != 'o');
5801 # ifdef FEAT_VIRTUALEDIT
5803 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
5804 * (theoretically) infinitely long.
5806 if (virtual_active())
5807 PAST_LINE = 0;
5808 # endif
5809 #endif
5811 for (n = cap->count1; n > 0; --n)
5813 if ((!PAST_LINE && oneright() == FAIL)
5814 || (PAST_LINE && *ml_get_cursor() == NUL))
5817 * <Space> wraps to next line if 'whichwrap' has 's'.
5818 * 'l' wraps to next line if 'whichwrap' has 'l'.
5819 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
5821 if ( ((cap->cmdchar == ' '
5822 && vim_strchr(p_ww, 's') != NULL)
5823 || (cap->cmdchar == 'l'
5824 && vim_strchr(p_ww, 'l') != NULL)
5825 || (cap->cmdchar == K_RIGHT
5826 && vim_strchr(p_ww, '>') != NULL))
5827 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5829 /* When deleting we also count the NL as a character.
5830 * Set cap->oap->inclusive when last char in the line is
5831 * included, move to next line after that */
5832 if ( cap->oap->op_type != OP_NOP
5833 && !cap->oap->inclusive
5834 && !lineempty(curwin->w_cursor.lnum))
5835 cap->oap->inclusive = TRUE;
5836 else
5838 ++curwin->w_cursor.lnum;
5839 curwin->w_cursor.col = 0;
5840 #ifdef FEAT_VIRTUALEDIT
5841 curwin->w_cursor.coladd = 0;
5842 #endif
5843 curwin->w_set_curswant = TRUE;
5844 cap->oap->inclusive = FALSE;
5846 continue;
5848 if (cap->oap->op_type == OP_NOP)
5850 /* Only beep and flush if not moved at all */
5851 if (n == cap->count1)
5852 beep_flush();
5854 else
5856 if (!lineempty(curwin->w_cursor.lnum))
5857 cap->oap->inclusive = TRUE;
5859 break;
5861 #ifdef FEAT_VISUAL
5862 else if (PAST_LINE)
5864 curwin->w_set_curswant = TRUE;
5865 # ifdef FEAT_VIRTUALEDIT
5866 if (virtual_active())
5867 oneright();
5868 else
5869 # endif
5871 # ifdef FEAT_MBYTE
5872 if (has_mbyte)
5873 curwin->w_cursor.col +=
5874 (*mb_ptr2len)(ml_get_cursor());
5875 else
5876 # endif
5877 ++curwin->w_cursor.col;
5880 #endif
5882 #ifdef FEAT_FOLDING
5883 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5884 && cap->oap->op_type == OP_NOP)
5885 foldOpenCursor();
5886 #endif
5890 * Cursor left commands.
5892 * Returns TRUE when operator end should not be adjusted.
5894 static void
5895 nv_left(cap)
5896 cmdarg_T *cap;
5898 long n;
5900 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5902 /* <C-Left> and <S-Left> move a word or WORD left */
5903 if (mod_mask & MOD_MASK_CTRL)
5904 cap->arg = 1;
5905 nv_bck_word(cap);
5906 return;
5909 cap->oap->motion_type = MCHAR;
5910 cap->oap->inclusive = FALSE;
5911 for (n = cap->count1; n > 0; --n)
5913 if (oneleft() == FAIL)
5915 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
5916 * 'h' wraps to previous line if 'whichwrap' has 'h'.
5917 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
5919 if ( (((cap->cmdchar == K_BS
5920 || cap->cmdchar == Ctrl_H)
5921 && vim_strchr(p_ww, 'b') != NULL)
5922 || (cap->cmdchar == 'h'
5923 && vim_strchr(p_ww, 'h') != NULL)
5924 || (cap->cmdchar == K_LEFT
5925 && vim_strchr(p_ww, '<') != NULL))
5926 && curwin->w_cursor.lnum > 1)
5928 --(curwin->w_cursor.lnum);
5929 coladvance((colnr_T)MAXCOL);
5930 curwin->w_set_curswant = TRUE;
5932 /* When the NL before the first char has to be deleted we
5933 * put the cursor on the NUL after the previous line.
5934 * This is a very special case, be careful!
5935 * Don't adjust op_end now, otherwise it won't work. */
5936 if ( (cap->oap->op_type == OP_DELETE
5937 || cap->oap->op_type == OP_CHANGE)
5938 && !lineempty(curwin->w_cursor.lnum))
5940 if (*ml_get_cursor() != NUL)
5941 ++curwin->w_cursor.col;
5942 cap->retval |= CA_NO_ADJ_OP_END;
5944 continue;
5946 /* Only beep and flush if not moved at all */
5947 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
5948 beep_flush();
5949 break;
5952 #ifdef FEAT_FOLDING
5953 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5954 && cap->oap->op_type == OP_NOP)
5955 foldOpenCursor();
5956 #endif
5960 * Cursor up commands.
5961 * cap->arg is TRUE for "-": Move cursor to first non-blank.
5963 static void
5964 nv_up(cap)
5965 cmdarg_T *cap;
5967 if (mod_mask & MOD_MASK_SHIFT)
5969 /* <S-Up> is page up */
5970 cap->arg = BACKWARD;
5971 nv_page(cap);
5973 else
5975 cap->oap->motion_type = MLINE;
5976 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5977 clearopbeep(cap->oap);
5978 else if (cap->arg)
5979 beginline(BL_WHITE | BL_FIX);
5984 * Cursor down commands.
5985 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
5987 static void
5988 nv_down(cap)
5989 cmdarg_T *cap;
5991 if (mod_mask & MOD_MASK_SHIFT)
5993 /* <S-Down> is page down */
5994 cap->arg = FORWARD;
5995 nv_page(cap);
5997 else
5998 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5999 /* In a quickfix window a <CR> jumps to the error under the cursor. */
6000 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
6001 if (curwin->w_llist_ref == NULL)
6002 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6003 else
6004 do_cmdline_cmd((char_u *)".ll"); /* location list window */
6005 else
6006 #endif
6008 #ifdef FEAT_CMDWIN
6009 /* In the cmdline window a <CR> executes the command. */
6010 if (cmdwin_type != 0 && cap->cmdchar == CAR)
6011 cmdwin_result = CAR;
6012 else
6013 #endif
6015 cap->oap->motion_type = MLINE;
6016 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6017 clearopbeep(cap->oap);
6018 else if (cap->arg)
6019 beginline(BL_WHITE | BL_FIX);
6024 #ifdef FEAT_SEARCHPATH
6026 * Grab the file name under the cursor and edit it.
6028 static void
6029 nv_gotofile(cap)
6030 cmdarg_T *cap;
6032 char_u *ptr;
6033 linenr_T lnum = -1;
6035 if (text_locked())
6037 clearopbeep(cap->oap);
6038 text_locked_msg();
6039 return;
6041 #ifdef FEAT_AUTOCMD
6042 if (curbuf_locked())
6044 clearop(cap->oap);
6045 return;
6047 #endif
6049 ptr = grab_file_name(cap->count1, &lnum);
6051 if (ptr != NULL)
6053 /* do autowrite if necessary */
6054 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6055 autowrite(curbuf, FALSE);
6056 setpcmark();
6057 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
6058 P_HID(curbuf) ? ECMD_HIDE : 0);
6059 if (cap->nchar == 'F' && lnum >= 0)
6061 curwin->w_cursor.lnum = lnum;
6062 check_cursor_lnum();
6063 beginline(BL_SOL | BL_FIX);
6065 vim_free(ptr);
6067 else
6068 clearop(cap->oap);
6070 #endif
6073 * <End> command: to end of current line or last line.
6075 static void
6076 nv_end(cap)
6077 cmdarg_T *cap;
6079 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
6081 cap->arg = TRUE;
6082 nv_goto(cap);
6083 cap->count1 = 1; /* to end of current line */
6085 nv_dollar(cap);
6089 * Handle the "$" command.
6091 static void
6092 nv_dollar(cap)
6093 cmdarg_T *cap;
6095 cap->oap->motion_type = MCHAR;
6096 cap->oap->inclusive = TRUE;
6097 #ifdef FEAT_VIRTUALEDIT
6098 /* In virtual mode when off the edge of a line and an operator
6099 * is pending (whew!) keep the cursor where it is.
6100 * Otherwise, send it to the end of the line. */
6101 if (!virtual_active() || gchar_cursor() != NUL
6102 || cap->oap->op_type == OP_NOP)
6103 #endif
6104 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6105 if (cursor_down((long)(cap->count1 - 1),
6106 cap->oap->op_type == OP_NOP) == FAIL)
6107 clearopbeep(cap->oap);
6108 #ifdef FEAT_FOLDING
6109 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6110 foldOpenCursor();
6111 #endif
6115 * Implementation of '?' and '/' commands.
6116 * If cap->arg is TRUE don't set PC mark.
6118 static void
6119 nv_search(cap)
6120 cmdarg_T *cap;
6122 oparg_T *oap = cap->oap;
6124 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6126 /* Translate "g??" to "g?g?" */
6127 cap->cmdchar = 'g';
6128 cap->nchar = '?';
6129 nv_operator(cap);
6130 return;
6133 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6135 if (cap->searchbuf == NULL)
6137 clearop(oap);
6138 return;
6141 normal_search(cap, cap->cmdchar, cap->searchbuf,
6142 (cap->arg ? 0 : SEARCH_MARK));
6146 * Handle "N" and "n" commands.
6147 * cap->arg is SEARCH_REV for "N", 0 for "n".
6149 static void
6150 nv_next(cap)
6151 cmdarg_T *cap;
6153 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6157 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6158 * Uses only cap->count1 and cap->oap from "cap".
6160 static void
6161 normal_search(cap, dir, pat, opt)
6162 cmdarg_T *cap;
6163 int dir;
6164 char_u *pat;
6165 int opt; /* extra flags for do_search() */
6167 int i;
6169 cap->oap->motion_type = MCHAR;
6170 cap->oap->inclusive = FALSE;
6171 cap->oap->use_reg_one = TRUE;
6172 curwin->w_set_curswant = TRUE;
6174 i = do_search(cap->oap, dir, pat, cap->count1,
6175 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
6176 if (i == 0)
6177 clearop(cap->oap);
6178 else
6180 if (i == 2)
6181 cap->oap->motion_type = MLINE;
6182 #ifdef FEAT_VIRTUALEDIT
6183 curwin->w_cursor.coladd = 0;
6184 #endif
6185 #ifdef FEAT_FOLDING
6186 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6187 foldOpenCursor();
6188 #endif
6191 /* "/$" will put the cursor after the end of the line, may need to
6192 * correct that here */
6193 check_cursor();
6197 * Character search commands.
6198 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6199 * ',' and FALSE for ';'.
6200 * cap->nchar is NUL for ',' and ';' (repeat the search)
6202 static void
6203 nv_csearch(cap)
6204 cmdarg_T *cap;
6206 int t_cmd;
6208 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6209 t_cmd = TRUE;
6210 else
6211 t_cmd = FALSE;
6213 cap->oap->motion_type = MCHAR;
6214 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6215 clearopbeep(cap->oap);
6216 else
6218 curwin->w_set_curswant = TRUE;
6219 #ifdef FEAT_VIRTUALEDIT
6220 /* Include a Tab for "tx" and for "dfx". */
6221 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6222 && (t_cmd || cap->oap->op_type != OP_NOP))
6224 colnr_T scol, ecol;
6226 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6227 curwin->w_cursor.coladd = ecol - scol;
6229 else
6230 curwin->w_cursor.coladd = 0;
6231 #endif
6232 #ifdef FEAT_VISUAL
6233 adjust_for_sel(cap);
6234 #endif
6235 #ifdef FEAT_FOLDING
6236 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6237 foldOpenCursor();
6238 #endif
6243 * "[" and "]" commands.
6244 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6246 static void
6247 nv_brackets(cap)
6248 cmdarg_T *cap;
6250 pos_T new_pos;
6251 pos_T prev_pos;
6252 pos_T *pos = NULL; /* init for GCC */
6253 pos_T old_pos; /* cursor position before command */
6254 int flag;
6255 long n;
6256 int findc;
6257 int c;
6259 cap->oap->motion_type = MCHAR;
6260 cap->oap->inclusive = FALSE;
6261 old_pos = curwin->w_cursor;
6262 #ifdef FEAT_VIRTUALEDIT
6263 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6264 #endif
6266 #ifdef FEAT_SEARCHPATH
6268 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6270 if (cap->nchar == 'f')
6271 nv_gotofile(cap);
6272 else
6273 #endif
6275 #ifdef FEAT_FIND_ID
6277 * Find the occurrence(s) of the identifier or define under cursor
6278 * in current and included files or jump to the first occurrence.
6280 * search list jump
6281 * fwd bwd fwd bwd fwd bwd
6282 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6283 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6285 if (vim_strchr((char_u *)
6286 #ifdef EBCDIC
6287 "iI\005dD\067",
6288 #else
6289 "iI\011dD\004",
6290 #endif
6291 cap->nchar) != NULL)
6293 char_u *ptr;
6294 int len;
6296 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6297 clearop(cap->oap);
6298 else
6300 find_pattern_in_path(ptr, 0, len, TRUE,
6301 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6302 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6303 cap->count1,
6304 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6305 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6306 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6307 (linenr_T)MAXLNUM);
6308 curwin->w_set_curswant = TRUE;
6311 else
6312 #endif
6315 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6316 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6317 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6318 * "[m" or "]m" search for prev/next start of (Java) method.
6319 * "[M" or "]M" search for prev/next end of (Java) method.
6321 if ( (cap->cmdchar == '['
6322 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6323 || (cap->cmdchar == ']'
6324 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6326 if (cap->nchar == '*')
6327 cap->nchar = '/';
6328 new_pos.lnum = 0;
6329 prev_pos.lnum = 0;
6330 if (cap->nchar == 'm' || cap->nchar == 'M')
6332 if (cap->cmdchar == '[')
6333 findc = '{';
6334 else
6335 findc = '}';
6336 n = 9999;
6338 else
6340 findc = cap->nchar;
6341 n = cap->count1;
6343 for ( ; n > 0; --n)
6345 if ((pos = findmatchlimit(cap->oap, findc,
6346 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6348 if (new_pos.lnum == 0) /* nothing found */
6350 if (cap->nchar != 'm' && cap->nchar != 'M')
6351 clearopbeep(cap->oap);
6353 else
6354 pos = &new_pos; /* use last one found */
6355 break;
6357 prev_pos = new_pos;
6358 curwin->w_cursor = *pos;
6359 new_pos = *pos;
6361 curwin->w_cursor = old_pos;
6364 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6365 * brought us to the match for "[m" and "]M" when inside a method.
6366 * Try finding the '{' or '}' we want to be at.
6367 * Also repeat for the given count.
6369 if (cap->nchar == 'm' || cap->nchar == 'M')
6371 /* norm is TRUE for "]M" and "[m" */
6372 int norm = ((findc == '{') == (cap->nchar == 'm'));
6374 n = cap->count1;
6375 /* found a match: we were inside a method */
6376 if (prev_pos.lnum != 0)
6378 pos = &prev_pos;
6379 curwin->w_cursor = prev_pos;
6380 if (norm)
6381 --n;
6383 else
6384 pos = NULL;
6385 while (n > 0)
6387 for (;;)
6389 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6391 /* if not found anything, that's an error */
6392 if (pos == NULL)
6393 clearopbeep(cap->oap);
6394 n = 0;
6395 break;
6397 c = gchar_cursor();
6398 if (c == '{' || c == '}')
6400 /* Must have found end/start of class: use it.
6401 * Or found the place to be at. */
6402 if ((c == findc && norm) || (n == 1 && !norm))
6404 new_pos = curwin->w_cursor;
6405 pos = &new_pos;
6406 n = 0;
6408 /* if no match found at all, we started outside of the
6409 * class and we're inside now. Just go on. */
6410 else if (new_pos.lnum == 0)
6412 new_pos = curwin->w_cursor;
6413 pos = &new_pos;
6415 /* found start/end of other method: go to match */
6416 else if ((pos = findmatchlimit(cap->oap, findc,
6417 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6418 0)) == NULL)
6419 n = 0;
6420 else
6421 curwin->w_cursor = *pos;
6422 break;
6425 --n;
6427 curwin->w_cursor = old_pos;
6428 if (pos == NULL && new_pos.lnum != 0)
6429 clearopbeep(cap->oap);
6431 if (pos != NULL)
6433 setpcmark();
6434 curwin->w_cursor = *pos;
6435 curwin->w_set_curswant = TRUE;
6436 #ifdef FEAT_FOLDING
6437 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6438 && cap->oap->op_type == OP_NOP)
6439 foldOpenCursor();
6440 #endif
6445 * "[[", "[]", "]]" and "][": move to start or end of function
6447 else if (cap->nchar == '[' || cap->nchar == ']')
6449 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6450 flag = '{';
6451 else
6452 flag = '}'; /* "][" or "[]" */
6454 curwin->w_set_curswant = TRUE;
6456 * Imitate strange Vi behaviour: When using "]]" with an operator
6457 * we also stop at '}'.
6459 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
6460 (cap->oap->op_type != OP_NOP
6461 && cap->arg == FORWARD && flag == '{')))
6462 clearopbeep(cap->oap);
6463 else
6465 if (cap->oap->op_type == OP_NOP)
6466 beginline(BL_WHITE | BL_FIX);
6467 #ifdef FEAT_FOLDING
6468 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6469 foldOpenCursor();
6470 #endif
6475 * "[p", "[P", "]P" and "]p": put with indent adjustment
6477 else if (cap->nchar == 'p' || cap->nchar == 'P')
6479 if (!checkclearop(cap->oap))
6481 prep_redo_cmd(cap);
6482 do_put(cap->oap->regname,
6483 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6484 cap->count1, PUT_FIXINDENT);
6489 * "['", "[`", "]'" and "]`": jump to next mark
6491 else if (cap->nchar == '\'' || cap->nchar == '`')
6493 pos = &curwin->w_cursor;
6494 for (n = cap->count1; n > 0; --n)
6496 prev_pos = *pos;
6497 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6498 cap->nchar == '\'');
6499 if (pos == NULL)
6500 break;
6502 if (pos == NULL)
6503 pos = &prev_pos;
6504 nv_cursormark(cap, cap->nchar == '\'', pos);
6507 #ifdef FEAT_MOUSE
6509 * [ or ] followed by a middle mouse click: put selected text with
6510 * indent adjustment. Any other button just does as usual.
6512 else if (cap->nchar >= K_LEFTMOUSE && cap->nchar <= K_RIGHTRELEASE)
6514 (void)do_mouse(cap->oap, cap->nchar,
6515 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6516 cap->count1, PUT_FIXINDENT);
6518 #endif /* FEAT_MOUSE */
6520 #ifdef FEAT_FOLDING
6522 * "[z" and "]z": move to start or end of open fold.
6524 else if (cap->nchar == 'z')
6526 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6527 cap->count1) == FAIL)
6528 clearopbeep(cap->oap);
6530 #endif
6532 #ifdef FEAT_DIFF
6534 * "[c" and "]c": move to next or previous diff-change.
6536 else if (cap->nchar == 'c')
6538 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6539 cap->count1) == FAIL)
6540 clearopbeep(cap->oap);
6542 #endif
6544 #ifdef FEAT_SPELL
6546 * "[s", "[S", "]s" and "]S": move to next spell error.
6548 else if (cap->nchar == 's' || cap->nchar == 'S')
6550 setpcmark();
6551 for (n = 0; n < cap->count1; ++n)
6552 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6553 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
6555 clearopbeep(cap->oap);
6556 break;
6558 # ifdef FEAT_FOLDING
6559 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6560 foldOpenCursor();
6561 # endif
6563 #endif
6565 /* Not a valid cap->nchar. */
6566 else
6567 clearopbeep(cap->oap);
6571 * Handle Normal mode "%" command.
6573 static void
6574 nv_percent(cap)
6575 cmdarg_T *cap;
6577 pos_T *pos;
6578 #ifdef FEAT_FOLDING
6579 linenr_T lnum = curwin->w_cursor.lnum;
6580 #endif
6582 cap->oap->inclusive = TRUE;
6583 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6585 if (cap->count0 > 100)
6586 clearopbeep(cap->oap);
6587 else
6589 cap->oap->motion_type = MLINE;
6590 setpcmark();
6591 /* Round up, so CTRL-G will give same value. Watch out for a
6592 * large line count, the line number must not go negative! */
6593 if (curbuf->b_ml.ml_line_count > 1000000)
6594 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6595 / 100L * cap->count0;
6596 else
6597 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6598 cap->count0 + 99L) / 100L;
6599 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6600 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6601 beginline(BL_SOL | BL_FIX);
6604 else /* "%" : go to matching paren */
6606 cap->oap->motion_type = MCHAR;
6607 cap->oap->use_reg_one = TRUE;
6608 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6609 clearopbeep(cap->oap);
6610 else
6612 setpcmark();
6613 curwin->w_cursor = *pos;
6614 curwin->w_set_curswant = TRUE;
6615 #ifdef FEAT_VIRTUALEDIT
6616 curwin->w_cursor.coladd = 0;
6617 #endif
6618 #ifdef FEAT_VISUAL
6619 adjust_for_sel(cap);
6620 #endif
6623 #ifdef FEAT_FOLDING
6624 if (cap->oap->op_type == OP_NOP
6625 && lnum != curwin->w_cursor.lnum
6626 && (fdo_flags & FDO_PERCENT)
6627 && KeyTyped)
6628 foldOpenCursor();
6629 #endif
6633 * Handle "(" and ")" commands.
6634 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6636 static void
6637 nv_brace(cap)
6638 cmdarg_T *cap;
6640 cap->oap->motion_type = MCHAR;
6641 cap->oap->use_reg_one = TRUE;
6642 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6643 cap->oap->inclusive = FALSE;
6644 curwin->w_set_curswant = TRUE;
6646 if (findsent(cap->arg, cap->count1) == FAIL)
6647 clearopbeep(cap->oap);
6648 else
6650 /* Don't leave the cursor on the NUL past end of line. */
6651 adjust_cursor(cap->oap);
6652 #ifdef FEAT_VIRTUALEDIT
6653 curwin->w_cursor.coladd = 0;
6654 #endif
6655 #ifdef FEAT_FOLDING
6656 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6657 foldOpenCursor();
6658 #endif
6663 * "m" command: Mark a position.
6665 static void
6666 nv_mark(cap)
6667 cmdarg_T *cap;
6669 if (!checkclearop(cap->oap))
6671 if (setmark(cap->nchar) == FAIL)
6672 clearopbeep(cap->oap);
6677 * "{" and "}" commands.
6678 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6680 static void
6681 nv_findpar(cap)
6682 cmdarg_T *cap;
6684 cap->oap->motion_type = MCHAR;
6685 cap->oap->inclusive = FALSE;
6686 cap->oap->use_reg_one = TRUE;
6687 curwin->w_set_curswant = TRUE;
6688 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
6689 clearopbeep(cap->oap);
6690 else
6692 #ifdef FEAT_VIRTUALEDIT
6693 curwin->w_cursor.coladd = 0;
6694 #endif
6695 #ifdef FEAT_FOLDING
6696 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6697 foldOpenCursor();
6698 #endif
6703 * "u" command: Undo or make lower case.
6705 static void
6706 nv_undo(cap)
6707 cmdarg_T *cap;
6709 if (cap->oap->op_type == OP_LOWER
6710 #ifdef FEAT_VISUAL
6711 || VIsual_active
6712 #endif
6715 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6716 cap->cmdchar = 'g';
6717 cap->nchar = 'u';
6718 nv_operator(cap);
6720 else
6721 nv_kundo(cap);
6725 * <Undo> command.
6727 static void
6728 nv_kundo(cap)
6729 cmdarg_T *cap;
6731 if (!checkclearopq(cap->oap))
6733 u_undo((int)cap->count1);
6734 curwin->w_set_curswant = TRUE;
6739 * Handle the "r" command.
6741 static void
6742 nv_replace(cap)
6743 cmdarg_T *cap;
6745 char_u *ptr;
6746 int had_ctrl_v;
6747 long n;
6749 if (checkclearop(cap->oap))
6750 return;
6752 /* get another character */
6753 if (cap->nchar == Ctrl_V)
6755 had_ctrl_v = Ctrl_V;
6756 cap->nchar = get_literal();
6757 /* Don't redo a multibyte character with CTRL-V. */
6758 if (cap->nchar > DEL)
6759 had_ctrl_v = NUL;
6761 else
6762 had_ctrl_v = NUL;
6764 /* Abort if the character is a special key. */
6765 if (IS_SPECIAL(cap->nchar))
6767 clearopbeep(cap->oap);
6768 return;
6771 #ifdef FEAT_VISUAL
6772 /* Visual mode "r" */
6773 if (VIsual_active)
6775 nv_operator(cap);
6776 return;
6778 #endif
6780 #ifdef FEAT_VIRTUALEDIT
6781 /* Break tabs, etc. */
6782 if (virtual_active())
6784 if (u_save_cursor() == FAIL)
6785 return;
6786 if (gchar_cursor() == NUL)
6788 /* Add extra space and put the cursor on the first one. */
6789 coladvance_force((colnr_T)(getviscol() + cap->count1));
6790 curwin->w_cursor.col -= cap->count1;
6792 else if (gchar_cursor() == TAB)
6793 coladvance_force(getviscol());
6795 #endif
6797 /* Abort if not enough characters to replace. */
6798 ptr = ml_get_cursor();
6799 if (STRLEN(ptr) < (unsigned)cap->count1
6800 #ifdef FEAT_MBYTE
6801 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6802 #endif
6805 clearopbeep(cap->oap);
6806 return;
6810 * Replacing with a TAB is done by edit() when it is complicated because
6811 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6812 * Other characters are done below to avoid problems with things like
6813 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6815 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6817 stuffnumReadbuff(cap->count1);
6818 stuffcharReadbuff('R');
6819 stuffcharReadbuff('\t');
6820 stuffcharReadbuff(ESC);
6821 return;
6824 /* save line for undo */
6825 if (u_save_cursor() == FAIL)
6826 return;
6828 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6831 * Replace character(s) by a single newline.
6832 * Strange vi behaviour: Only one newline is inserted.
6833 * Delete the characters here.
6834 * Insert the newline with an insert command, takes care of
6835 * autoindent. The insert command depends on being on the last
6836 * character of a line or not.
6838 #ifdef FEAT_MBYTE
6839 (void)del_chars(cap->count1, FALSE); /* delete the characters */
6840 #else
6841 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
6842 #endif
6843 stuffcharReadbuff('\r');
6844 stuffcharReadbuff(ESC);
6846 /* Give 'r' to edit(), to get the redo command right. */
6847 invoke_edit(cap, TRUE, 'r', FALSE);
6849 else
6851 prep_redo(cap->oap->regname, cap->count1,
6852 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
6854 curbuf->b_op_start = curwin->w_cursor;
6855 #ifdef FEAT_MBYTE
6856 if (has_mbyte)
6858 int old_State = State;
6860 if (cap->ncharC1 != 0)
6861 AppendCharToRedobuff(cap->ncharC1);
6862 if (cap->ncharC2 != 0)
6863 AppendCharToRedobuff(cap->ncharC2);
6865 /* This is slow, but it handles replacing a single-byte with a
6866 * multi-byte and the other way around. Also handles adding
6867 * composing characters for utf-8. */
6868 for (n = cap->count1; n > 0; --n)
6870 State = REPLACE;
6871 ins_char(cap->nchar);
6872 State = old_State;
6873 if (cap->ncharC1 != 0)
6874 ins_char(cap->ncharC1);
6875 if (cap->ncharC2 != 0)
6876 ins_char(cap->ncharC2);
6879 else
6880 #endif
6883 * Replace the characters within one line.
6885 for (n = cap->count1; n > 0; --n)
6888 * Get ptr again, because u_save and/or showmatch() will have
6889 * released the line. At the same time we let know that the
6890 * line will be changed.
6892 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
6893 ptr[curwin->w_cursor.col] = cap->nchar;
6894 if (p_sm && msg_silent == 0)
6895 showmatch(cap->nchar);
6896 ++curwin->w_cursor.col;
6898 #ifdef FEAT_NETBEANS_INTG
6899 if (usingNetbeans)
6901 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
6903 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
6904 (long)cap->count1);
6905 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
6906 &ptr[start], (int)cap->count1);
6908 #endif
6910 /* mark the buffer as changed and prepare for displaying */
6911 changed_bytes(curwin->w_cursor.lnum,
6912 (colnr_T)(curwin->w_cursor.col - cap->count1));
6914 --curwin->w_cursor.col; /* cursor on the last replaced char */
6915 #ifdef FEAT_MBYTE
6916 /* if the character on the left of the current cursor is a multi-byte
6917 * character, move two characters left */
6918 if (has_mbyte)
6919 mb_adjust_cursor();
6920 #endif
6921 curbuf->b_op_end = curwin->w_cursor;
6922 curwin->w_set_curswant = TRUE;
6923 set_last_insert(cap->nchar);
6927 #ifdef FEAT_VISUAL
6929 * 'o': Exchange start and end of Visual area.
6930 * 'O': same, but in block mode exchange left and right corners.
6932 static void
6933 v_swap_corners(cmdchar)
6934 int cmdchar;
6936 pos_T old_cursor;
6937 colnr_T left, right;
6939 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
6941 old_cursor = curwin->w_cursor;
6942 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
6943 curwin->w_cursor.lnum = VIsual.lnum;
6944 coladvance(left);
6945 VIsual = curwin->w_cursor;
6947 curwin->w_cursor.lnum = old_cursor.lnum;
6948 curwin->w_curswant = right;
6949 /* 'selection "exclusive" and cursor at right-bottom corner: move it
6950 * right one column */
6951 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
6952 ++curwin->w_curswant;
6953 coladvance(curwin->w_curswant);
6954 if (curwin->w_cursor.col == old_cursor.col
6955 #ifdef FEAT_VIRTUALEDIT
6956 && (!virtual_active()
6957 || curwin->w_cursor.coladd == old_cursor.coladd)
6958 #endif
6961 curwin->w_cursor.lnum = VIsual.lnum;
6962 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
6963 ++right;
6964 coladvance(right);
6965 VIsual = curwin->w_cursor;
6967 curwin->w_cursor.lnum = old_cursor.lnum;
6968 coladvance(left);
6969 curwin->w_curswant = left;
6972 else
6974 old_cursor = curwin->w_cursor;
6975 curwin->w_cursor = VIsual;
6976 VIsual = old_cursor;
6977 curwin->w_set_curswant = TRUE;
6980 #endif /* FEAT_VISUAL */
6983 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
6985 static void
6986 nv_Replace(cap)
6987 cmdarg_T *cap;
6989 #ifdef FEAT_VISUAL
6990 if (VIsual_active) /* "R" is replace lines */
6992 cap->cmdchar = 'c';
6993 cap->nchar = NUL;
6994 VIsual_mode = 'V';
6995 nv_operator(cap);
6997 else
6998 #endif
6999 if (!checkclearopq(cap->oap))
7001 if (!curbuf->b_p_ma)
7002 EMSG(_(e_modifiable));
7003 else
7005 #ifdef FEAT_VIRTUALEDIT
7006 if (virtual_active())
7007 coladvance(getviscol());
7008 #endif
7009 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7014 #ifdef FEAT_VREPLACE
7016 * "gr".
7018 static void
7019 nv_vreplace(cap)
7020 cmdarg_T *cap;
7022 # ifdef FEAT_VISUAL
7023 if (VIsual_active)
7025 cap->cmdchar = 'r';
7026 cap->nchar = cap->extra_char;
7027 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7029 else
7030 # endif
7031 if (!checkclearopq(cap->oap))
7033 if (!curbuf->b_p_ma)
7034 EMSG(_(e_modifiable));
7035 else
7037 if (cap->extra_char == Ctrl_V) /* get another character */
7038 cap->extra_char = get_literal();
7039 stuffcharReadbuff(cap->extra_char);
7040 stuffcharReadbuff(ESC);
7041 # ifdef FEAT_VIRTUALEDIT
7042 if (virtual_active())
7043 coladvance(getviscol());
7044 # endif
7045 invoke_edit(cap, TRUE, 'v', FALSE);
7049 #endif
7052 * Swap case for "~" command, when it does not work like an operator.
7054 static void
7055 n_swapchar(cap)
7056 cmdarg_T *cap;
7058 long n;
7059 pos_T startpos;
7060 int did_change = 0;
7061 #ifdef FEAT_NETBEANS_INTG
7062 pos_T pos;
7063 char_u *ptr;
7064 int count;
7065 #endif
7067 if (checkclearopq(cap->oap))
7068 return;
7070 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7072 clearopbeep(cap->oap);
7073 return;
7076 prep_redo_cmd(cap);
7078 if (u_save_cursor() == FAIL)
7079 return;
7081 startpos = curwin->w_cursor;
7082 #ifdef FEAT_NETBEANS_INTG
7083 pos = startpos;
7084 #endif
7085 for (n = cap->count1; n > 0; --n)
7087 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7088 inc_cursor();
7089 if (gchar_cursor() == NUL)
7091 if (vim_strchr(p_ww, '~') != NULL
7092 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7094 #ifdef FEAT_NETBEANS_INTG
7095 if (usingNetbeans)
7097 if (did_change)
7099 ptr = ml_get(pos.lnum);
7100 count = (int)STRLEN(ptr) - pos.col;
7101 netbeans_removed(curbuf, pos.lnum, pos.col,
7102 (long)count);
7103 netbeans_inserted(curbuf, pos.lnum, pos.col,
7104 &ptr[pos.col], count);
7106 pos.col = 0;
7107 pos.lnum++;
7109 #endif
7110 ++curwin->w_cursor.lnum;
7111 curwin->w_cursor.col = 0;
7112 if (n > 1)
7114 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7115 break;
7116 u_clearline();
7119 else
7120 break;
7123 #ifdef FEAT_NETBEANS_INTG
7124 if (did_change && usingNetbeans)
7126 ptr = ml_get(pos.lnum);
7127 count = curwin->w_cursor.col - pos.col;
7128 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7129 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
7131 #endif
7134 check_cursor();
7135 curwin->w_set_curswant = TRUE;
7136 if (did_change)
7138 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7139 0L);
7140 curbuf->b_op_start = startpos;
7141 curbuf->b_op_end = curwin->w_cursor;
7142 if (curbuf->b_op_end.col > 0)
7143 --curbuf->b_op_end.col;
7148 * Move cursor to mark.
7150 static void
7151 nv_cursormark(cap, flag, pos)
7152 cmdarg_T *cap;
7153 int flag;
7154 pos_T *pos;
7156 if (check_mark(pos) == FAIL)
7157 clearop(cap->oap);
7158 else
7160 if (cap->cmdchar == '\''
7161 || cap->cmdchar == '`'
7162 || cap->cmdchar == '['
7163 || cap->cmdchar == ']')
7164 setpcmark();
7165 curwin->w_cursor = *pos;
7166 if (flag)
7167 beginline(BL_WHITE | BL_FIX);
7168 else
7169 check_cursor();
7171 cap->oap->motion_type = flag ? MLINE : MCHAR;
7172 if (cap->cmdchar == '`')
7173 cap->oap->use_reg_one = TRUE;
7174 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7175 curwin->w_set_curswant = TRUE;
7178 #ifdef FEAT_VISUAL
7180 * Handle commands that are operators in Visual mode.
7182 static void
7183 v_visop(cap)
7184 cmdarg_T *cap;
7186 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7188 /* Uppercase means linewise, except in block mode, then "D" deletes till
7189 * the end of the line, and "C" replaces til EOL */
7190 if (isupper(cap->cmdchar))
7192 if (VIsual_mode != Ctrl_V)
7193 VIsual_mode = 'V';
7194 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7195 curwin->w_curswant = MAXCOL;
7197 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7198 nv_operator(cap);
7200 #endif
7203 * "s" and "S" commands.
7205 static void
7206 nv_subst(cap)
7207 cmdarg_T *cap;
7209 #ifdef FEAT_VISUAL
7210 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7212 if (cap->cmdchar == 'S')
7213 VIsual_mode = 'V';
7214 cap->cmdchar = 'c';
7215 nv_operator(cap);
7217 else
7218 #endif
7219 nv_optrans(cap);
7223 * Abbreviated commands.
7225 static void
7226 nv_abbrev(cap)
7227 cmdarg_T *cap;
7229 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7230 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7232 #ifdef FEAT_VISUAL
7233 /* in Visual mode these commands are operators */
7234 if (VIsual_active)
7235 v_visop(cap);
7236 else
7237 #endif
7238 nv_optrans(cap);
7242 * Translate a command into another command.
7244 static void
7245 nv_optrans(cap)
7246 cmdarg_T *cap;
7248 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7249 (char_u *)"d$", (char_u *)"c$",
7250 (char_u *)"cl", (char_u *)"cc",
7251 (char_u *)"yy", (char_u *)":s\r"};
7252 static char_u *str = (char_u *)"xXDCsSY&";
7254 if (!checkclearopq(cap->oap))
7256 /* In Vi "2D" doesn't delete the next line. Can't translate it
7257 * either, because "2." should also not use the count. */
7258 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7260 cap->oap->start = curwin->w_cursor;
7261 cap->oap->op_type = OP_DELETE;
7262 #ifdef FEAT_EVAL
7263 set_op_var(OP_DELETE);
7264 #endif
7265 cap->count1 = 1;
7266 nv_dollar(cap);
7267 finish_op = TRUE;
7268 ResetRedobuff();
7269 AppendCharToRedobuff('D');
7271 else
7273 if (cap->count0)
7274 stuffnumReadbuff(cap->count0);
7275 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7278 cap->opcount = 0;
7282 * "'" and "`" commands. Also for "g'" and "g`".
7283 * cap->arg is TRUE for "'" and "g'".
7285 static void
7286 nv_gomark(cap)
7287 cmdarg_T *cap;
7289 pos_T *pos;
7290 int c;
7291 #ifdef FEAT_FOLDING
7292 linenr_T lnum = curwin->w_cursor.lnum;
7293 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7294 #endif
7296 if (cap->cmdchar == 'g')
7297 c = cap->extra_char;
7298 else
7299 c = cap->nchar;
7300 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7301 if (pos == (pos_T *)-1) /* jumped to other file */
7303 if (cap->arg)
7305 check_cursor_lnum();
7306 beginline(BL_WHITE | BL_FIX);
7308 else
7309 check_cursor();
7311 else
7312 nv_cursormark(cap, cap->arg, pos);
7314 #ifdef FEAT_VIRTUALEDIT
7315 /* May need to clear the coladd that a mark includes. */
7316 if (!virtual_active())
7317 curwin->w_cursor.coladd = 0;
7318 #endif
7319 #ifdef FEAT_FOLDING
7320 if (cap->oap->op_type == OP_NOP
7321 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7322 && (fdo_flags & FDO_MARK)
7323 && old_KeyTyped)
7324 foldOpenCursor();
7325 #endif
7329 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7331 static void
7332 nv_pcmark(cap)
7333 cmdarg_T *cap;
7335 #ifdef FEAT_JUMPLIST
7336 pos_T *pos;
7337 # ifdef FEAT_FOLDING
7338 linenr_T lnum = curwin->w_cursor.lnum;
7339 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7340 # endif
7342 if (!checkclearopq(cap->oap))
7344 if (cap->cmdchar == 'g')
7345 pos = movechangelist((int)cap->count1);
7346 else
7347 pos = movemark((int)cap->count1);
7348 if (pos == (pos_T *)-1) /* jump to other file */
7350 curwin->w_set_curswant = TRUE;
7351 check_cursor();
7353 else if (pos != NULL) /* can jump */
7354 nv_cursormark(cap, FALSE, pos);
7355 else if (cap->cmdchar == 'g')
7357 if (curbuf->b_changelistlen == 0)
7358 EMSG(_("E664: changelist is empty"));
7359 else if (cap->count1 < 0)
7360 EMSG(_("E662: At start of changelist"));
7361 else
7362 EMSG(_("E663: At end of changelist"));
7364 else
7365 clearopbeep(cap->oap);
7366 # ifdef FEAT_FOLDING
7367 if (cap->oap->op_type == OP_NOP
7368 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7369 && (fdo_flags & FDO_MARK)
7370 && old_KeyTyped)
7371 foldOpenCursor();
7372 # endif
7374 #else
7375 clearopbeep(cap->oap);
7376 #endif
7380 * Handle '"' command.
7382 static void
7383 nv_regname(cap)
7384 cmdarg_T *cap;
7386 if (checkclearop(cap->oap))
7387 return;
7388 #ifdef FEAT_EVAL
7389 if (cap->nchar == '=')
7390 cap->nchar = get_expr_register();
7391 #endif
7392 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7394 cap->oap->regname = cap->nchar;
7395 cap->opcount = cap->count0; /* remember count before '"' */
7396 #ifdef FEAT_EVAL
7397 set_reg_var(cap->oap->regname);
7398 #endif
7400 else
7401 clearopbeep(cap->oap);
7404 #ifdef FEAT_VISUAL
7406 * Handle "v", "V" and "CTRL-V" commands.
7407 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7408 * is TRUE.
7409 * Handle CTRL-Q just like CTRL-V.
7411 static void
7412 nv_visual(cap)
7413 cmdarg_T *cap;
7415 if (cap->cmdchar == Ctrl_Q)
7416 cap->cmdchar = Ctrl_V;
7418 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7419 * characterwise, linewise, or blockwise. */
7420 if (cap->oap->op_type != OP_NOP)
7422 cap->oap->motion_force = cap->cmdchar;
7423 finish_op = FALSE; /* operator doesn't finish now but later */
7424 return;
7427 VIsual_select = cap->arg;
7428 if (VIsual_active) /* change Visual mode */
7430 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7431 end_visual_mode();
7432 else /* toggle char/block mode */
7433 { /* or char/line mode */
7434 VIsual_mode = cap->cmdchar;
7435 showmode();
7437 redraw_curbuf_later(INVERTED); /* update the inversion */
7439 else /* start Visual mode */
7441 check_visual_highlight();
7442 if (cap->count0) /* use previously selected part */
7444 if (resel_VIsual_mode == NUL) /* there is none */
7446 beep_flush();
7447 return;
7449 VIsual = curwin->w_cursor;
7451 VIsual_active = TRUE;
7452 VIsual_reselect = TRUE;
7453 if (!cap->arg)
7454 /* start Select mode when 'selectmode' contains "cmd" */
7455 may_start_select('c');
7456 #ifdef FEAT_MOUSE
7457 setmouse();
7458 #endif
7459 if (p_smd && msg_silent == 0)
7460 redraw_cmdline = TRUE; /* show visual mode later */
7462 * For V and ^V, we multiply the number of lines even if there
7463 * was only one -- webb
7465 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7467 curwin->w_cursor.lnum +=
7468 resel_VIsual_line_count * cap->count0 - 1;
7469 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7470 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7472 VIsual_mode = resel_VIsual_mode;
7473 if (VIsual_mode == 'v')
7475 if (resel_VIsual_line_count <= 1)
7476 curwin->w_cursor.col += resel_VIsual_col * cap->count0 - 1;
7477 else
7478 curwin->w_cursor.col = resel_VIsual_col;
7479 check_cursor_col();
7481 if (resel_VIsual_col == MAXCOL)
7483 curwin->w_curswant = MAXCOL;
7484 coladvance((colnr_T)MAXCOL);
7486 else if (VIsual_mode == Ctrl_V)
7488 validate_virtcol();
7489 curwin->w_curswant = curwin->w_virtcol
7490 + resel_VIsual_col * cap->count0 - 1;
7491 coladvance(curwin->w_curswant);
7493 else
7494 curwin->w_set_curswant = TRUE;
7495 redraw_curbuf_later(INVERTED); /* show the inversion */
7497 else
7499 if (!cap->arg)
7500 /* start Select mode when 'selectmode' contains "cmd" */
7501 may_start_select('c');
7502 n_start_visual_mode(cap->cmdchar);
7508 * Start selection for Shift-movement keys.
7510 void
7511 start_selection()
7513 /* if 'selectmode' contains "key", start Select mode */
7514 may_start_select('k');
7515 n_start_visual_mode('v');
7519 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7521 void
7522 may_start_select(c)
7523 int c;
7525 VIsual_select = (stuff_empty() && typebuf_typed()
7526 && (vim_strchr(p_slm, c) != NULL));
7530 * Start Visual mode "c".
7531 * Should set VIsual_select before calling this.
7533 static void
7534 n_start_visual_mode(c)
7535 int c;
7537 VIsual_mode = c;
7538 VIsual_active = TRUE;
7539 VIsual_reselect = TRUE;
7540 #ifdef FEAT_VIRTUALEDIT
7541 /* Corner case: the 0 position in a tab may change when going into
7542 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7544 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7545 coladvance(curwin->w_virtcol);
7546 #endif
7547 VIsual = curwin->w_cursor;
7549 #ifdef FEAT_FOLDING
7550 foldAdjustVisual();
7551 #endif
7553 #ifdef FEAT_MOUSE
7554 setmouse();
7555 #endif
7556 if (p_smd && msg_silent == 0)
7557 redraw_cmdline = TRUE; /* show visual mode later */
7558 #ifdef FEAT_CLIPBOARD
7559 /* Make sure the clipboard gets updated. Needed because start and
7560 * end may still be the same, and the selection needs to be owned */
7561 clip_star.vmode = NUL;
7562 #endif
7564 /* Only need to redraw this line, unless still need to redraw an old
7565 * Visual area (when 'lazyredraw' is set). */
7566 if (curwin->w_redr_type < INVERTED)
7568 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7569 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7573 #endif /* FEAT_VISUAL */
7576 * CTRL-W: Window commands
7578 static void
7579 nv_window(cap)
7580 cmdarg_T *cap;
7582 #ifdef FEAT_WINDOWS
7583 if (!checkclearop(cap->oap))
7584 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7585 #else
7586 (void)checkclearop(cap->oap);
7587 #endif
7591 * CTRL-Z: Suspend
7593 static void
7594 nv_suspend(cap)
7595 cmdarg_T *cap;
7597 clearop(cap->oap);
7598 #ifdef FEAT_VISUAL
7599 if (VIsual_active)
7600 end_visual_mode(); /* stop Visual mode */
7601 #endif
7602 do_cmdline_cmd((char_u *)"st");
7606 * Commands starting with "g".
7608 static void
7609 nv_g_cmd(cap)
7610 cmdarg_T *cap;
7612 oparg_T *oap = cap->oap;
7613 #ifdef FEAT_VISUAL
7614 pos_T tpos;
7615 #endif
7616 int i;
7617 int flag = FALSE;
7619 switch (cap->nchar)
7621 #ifdef MEM_PROFILE
7623 * "g^A": dump log of used memory.
7625 case Ctrl_A:
7626 vim_mem_profile_dump();
7627 break;
7628 #endif
7630 #ifdef FEAT_VREPLACE
7632 * "gR": Enter virtual replace mode.
7634 case 'R':
7635 cap->arg = TRUE;
7636 nv_Replace(cap);
7637 break;
7639 case 'r':
7640 nv_vreplace(cap);
7641 break;
7642 #endif
7644 case '&':
7645 do_cmdline_cmd((char_u *)"%s//~/&");
7646 break;
7648 #ifdef FEAT_VISUAL
7650 * "gv": Reselect the previous Visual area. If Visual already active,
7651 * exchange previous and current Visual area.
7653 case 'v':
7654 if (checkclearop(oap))
7655 break;
7657 if ( curbuf->b_visual.vi_start.lnum == 0
7658 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7659 || curbuf->b_visual.vi_end.lnum == 0)
7660 beep_flush();
7661 else
7663 /* set w_cursor to the start of the Visual area, tpos to the end */
7664 if (VIsual_active)
7666 i = VIsual_mode;
7667 VIsual_mode = curbuf->b_visual.vi_mode;
7668 curbuf->b_visual.vi_mode = i;
7669 # ifdef FEAT_EVAL
7670 curbuf->b_visual_mode_eval = i;
7671 # endif
7672 i = curwin->w_curswant;
7673 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7674 curbuf->b_visual.vi_curswant = i;
7676 tpos = curbuf->b_visual.vi_end;
7677 curbuf->b_visual.vi_end = curwin->w_cursor;
7678 curwin->w_cursor = curbuf->b_visual.vi_start;
7679 curbuf->b_visual.vi_start = VIsual;
7681 else
7683 VIsual_mode = curbuf->b_visual.vi_mode;
7684 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7685 tpos = curbuf->b_visual.vi_end;
7686 curwin->w_cursor = curbuf->b_visual.vi_start;
7689 VIsual_active = TRUE;
7690 VIsual_reselect = TRUE;
7692 /* Set Visual to the start and w_cursor to the end of the Visual
7693 * area. Make sure they are on an existing character. */
7694 check_cursor();
7695 VIsual = curwin->w_cursor;
7696 curwin->w_cursor = tpos;
7697 check_cursor();
7698 update_topline();
7700 * When called from normal "g" command: start Select mode when
7701 * 'selectmode' contains "cmd". When called for K_SELECT, always
7702 * start Select mode.
7704 if (cap->arg)
7705 VIsual_select = TRUE;
7706 else
7707 may_start_select('c');
7708 #ifdef FEAT_MOUSE
7709 setmouse();
7710 #endif
7711 #ifdef FEAT_CLIPBOARD
7712 /* Make sure the clipboard gets updated. Needed because start and
7713 * end are still the same, and the selection needs to be owned */
7714 clip_star.vmode = NUL;
7715 #endif
7716 redraw_curbuf_later(INVERTED);
7717 showmode();
7719 break;
7721 * "gV": Don't reselect the previous Visual area after a Select mode
7722 * mapping of menu.
7724 case 'V':
7725 VIsual_reselect = FALSE;
7726 break;
7729 * "gh": start Select mode.
7730 * "gH": start Select line mode.
7731 * "g^H": start Select block mode.
7733 case K_BS:
7734 cap->nchar = Ctrl_H;
7735 /* FALLTHROUGH */
7736 case 'h':
7737 case 'H':
7738 case Ctrl_H:
7739 # ifdef EBCDIC
7740 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7741 if (cap->nchar == Ctrl_H)
7742 cap->cmdchar = Ctrl_V;
7743 else
7744 # endif
7745 cap->cmdchar = cap->nchar + ('v' - 'h');
7746 cap->arg = TRUE;
7747 nv_visual(cap);
7748 break;
7749 #endif /* FEAT_VISUAL */
7752 * "gj" and "gk" two new funny movement keys -- up and down
7753 * movement based on *screen* line rather than *file* line.
7755 case 'j':
7756 case K_DOWN:
7757 /* with 'nowrap' it works just like the normal "j" command; also when
7758 * in a closed fold */
7759 if (!curwin->w_p_wrap
7760 #ifdef FEAT_FOLDING
7761 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7762 #endif
7765 oap->motion_type = MLINE;
7766 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7768 else
7769 i = nv_screengo(oap, FORWARD, cap->count1);
7770 if (i == FAIL)
7771 clearopbeep(oap);
7772 break;
7774 case 'k':
7775 case K_UP:
7776 /* with 'nowrap' it works just like the normal "k" command; also when
7777 * in a closed fold */
7778 if (!curwin->w_p_wrap
7779 #ifdef FEAT_FOLDING
7780 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7781 #endif
7784 oap->motion_type = MLINE;
7785 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7787 else
7788 i = nv_screengo(oap, BACKWARD, cap->count1);
7789 if (i == FAIL)
7790 clearopbeep(oap);
7791 break;
7794 * "gJ": join two lines without inserting a space.
7796 case 'J':
7797 nv_join(cap);
7798 break;
7801 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7802 * "gm": middle of "g0" and "g$".
7804 case '^':
7805 flag = TRUE;
7806 /* FALLTHROUGH */
7808 case '0':
7809 case 'm':
7810 case K_HOME:
7811 case K_KHOME:
7812 oap->motion_type = MCHAR;
7813 oap->inclusive = FALSE;
7814 if (curwin->w_p_wrap
7815 #ifdef FEAT_VERTSPLIT
7816 && curwin->w_width != 0
7817 #endif
7820 int width1 = W_WIDTH(curwin) - curwin_col_off();
7821 int width2 = width1 + curwin_col_off2();
7823 validate_virtcol();
7824 i = 0;
7825 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
7826 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
7828 else
7829 i = curwin->w_leftcol;
7830 /* Go to the middle of the screen line. When 'number' is on and lines
7831 * are wrapping the middle can be more to the left.*/
7832 if (cap->nchar == 'm')
7833 i += (W_WIDTH(curwin) - curwin_col_off()
7834 + ((curwin->w_p_wrap && i > 0)
7835 ? curwin_col_off2() : 0)) / 2;
7836 coladvance((colnr_T)i);
7837 if (flag)
7840 i = gchar_cursor();
7841 while (vim_iswhite(i) && oneright() == OK);
7843 curwin->w_set_curswant = TRUE;
7844 break;
7846 case '_':
7847 /* "g_": to the last non-blank character in the line or <count> lines
7848 * downward. */
7849 cap->oap->motion_type = MCHAR;
7850 cap->oap->inclusive = TRUE;
7851 curwin->w_curswant = MAXCOL;
7852 if (cursor_down((long)(cap->count1 - 1),
7853 cap->oap->op_type == OP_NOP) == FAIL)
7854 clearopbeep(cap->oap);
7855 else
7857 char_u *ptr = ml_get_curline();
7859 /* In Visual mode we may end up after the line. */
7860 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
7861 --curwin->w_cursor.col;
7863 /* Decrease the cursor column until it's on a non-blank. */
7864 while (curwin->w_cursor.col > 0
7865 && vim_iswhite(ptr[curwin->w_cursor.col]))
7866 --curwin->w_cursor.col;
7867 curwin->w_set_curswant = TRUE;
7869 break;
7871 case '$':
7872 case K_END:
7873 case K_KEND:
7875 int col_off = curwin_col_off();
7877 oap->motion_type = MCHAR;
7878 oap->inclusive = TRUE;
7879 if (curwin->w_p_wrap
7880 #ifdef FEAT_VERTSPLIT
7881 && curwin->w_width != 0
7882 #endif
7885 curwin->w_curswant = MAXCOL; /* so we stay at the end */
7886 if (cap->count1 == 1)
7888 int width1 = W_WIDTH(curwin) - col_off;
7889 int width2 = width1 + curwin_col_off2();
7891 validate_virtcol();
7892 i = width1 - 1;
7893 if (curwin->w_virtcol >= (colnr_T)width1)
7894 i += ((curwin->w_virtcol - width1) / width2 + 1)
7895 * width2;
7896 coladvance((colnr_T)i);
7897 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
7898 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
7901 * Check for landing on a character that got split at
7902 * the end of the line. We do not want to advance to
7903 * the next screen line.
7905 validate_virtcol();
7906 if (curwin->w_virtcol > (colnr_T)i)
7907 --curwin->w_cursor.col;
7909 #endif
7911 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
7912 clearopbeep(oap);
7914 else
7916 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
7917 coladvance((colnr_T)i);
7919 /* Make sure we stick in this column. */
7920 validate_virtcol();
7921 curwin->w_curswant = curwin->w_virtcol;
7922 curwin->w_set_curswant = FALSE;
7925 break;
7928 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
7930 case '*':
7931 case '#':
7932 #if POUND != '#'
7933 case POUND: /* pound sign (sometimes equal to '#') */
7934 #endif
7935 case Ctrl_RSB: /* :tag or :tselect for current identifier */
7936 case ']': /* :tselect for current identifier */
7937 nv_ident(cap);
7938 break;
7941 * ge and gE: go back to end of word
7943 case 'e':
7944 case 'E':
7945 oap->motion_type = MCHAR;
7946 curwin->w_set_curswant = TRUE;
7947 oap->inclusive = TRUE;
7948 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
7949 clearopbeep(oap);
7950 break;
7953 * "g CTRL-G": display info about cursor position
7955 case Ctrl_G:
7956 cursor_pos_info();
7957 break;
7960 * "gi": start Insert at the last position.
7962 case 'i':
7963 if (curbuf->b_last_insert.lnum != 0)
7965 curwin->w_cursor = curbuf->b_last_insert;
7966 check_cursor_lnum();
7967 i = (int)STRLEN(ml_get_curline());
7968 if (curwin->w_cursor.col > (colnr_T)i)
7970 #ifdef FEAT_VIRTUALEDIT
7971 if (virtual_active())
7972 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
7973 #endif
7974 curwin->w_cursor.col = i;
7977 cap->cmdchar = 'i';
7978 nv_edit(cap);
7979 break;
7982 * "gI": Start insert in column 1.
7984 case 'I':
7985 beginline(0);
7986 if (!checkclearopq(oap))
7987 invoke_edit(cap, FALSE, 'g', FALSE);
7988 break;
7990 #ifdef FEAT_SEARCHPATH
7992 * "gf": goto file, edit file under cursor
7993 * "]f" and "[f": can also be used.
7995 case 'f':
7996 case 'F':
7997 nv_gotofile(cap);
7998 break;
7999 #endif
8001 /* "g'm" and "g`m": jump to mark without setting pcmark */
8002 case '\'':
8003 cap->arg = TRUE;
8004 /*FALLTHROUGH*/
8005 case '`':
8006 nv_gomark(cap);
8007 break;
8010 * "gs": Goto sleep.
8012 case 's':
8013 do_sleep(cap->count1 * 1000L);
8014 break;
8017 * "ga": Display the ascii value of the character under the
8018 * cursor. It is displayed in decimal, hex, and octal. -- webb
8020 case 'a':
8021 do_ascii(NULL);
8022 break;
8024 #ifdef FEAT_MBYTE
8026 * "g8": Display the bytes used for the UTF-8 character under the
8027 * cursor. It is displayed in hex.
8028 * "8g8" finds illegal byte sequence.
8030 case '8':
8031 if (cap->count0 == 8)
8032 utf_find_illegal();
8033 else
8034 show_utf8();
8035 break;
8036 #endif
8038 case '<':
8039 show_sb_text();
8040 break;
8043 * "gg": Goto the first line in file. With a count it goes to
8044 * that line number like for "G". -- webb
8046 case 'g':
8047 cap->arg = FALSE;
8048 nv_goto(cap);
8049 break;
8052 * Two-character operators:
8053 * "gq" Format text
8054 * "gw" Format text and keep cursor position
8055 * "g~" Toggle the case of the text.
8056 * "gu" Change text to lower case.
8057 * "gU" Change text to upper case.
8058 * "g?" rot13 encoding
8059 * "g@" call 'operatorfunc'
8061 case 'q':
8062 case 'w':
8063 oap->cursor_start = curwin->w_cursor;
8064 /*FALLTHROUGH*/
8065 case '~':
8066 case 'u':
8067 case 'U':
8068 case '?':
8069 case '@':
8070 nv_operator(cap);
8071 break;
8074 * "gd": Find first occurrence of pattern under the cursor in the
8075 * current function
8076 * "gD": idem, but in the current file.
8078 case 'd':
8079 case 'D':
8080 nv_gd(oap, cap->nchar, (int)cap->count0);
8081 break;
8083 #ifdef FEAT_MOUSE
8085 * g<*Mouse> : <C-*mouse>
8087 case K_MIDDLEMOUSE:
8088 case K_MIDDLEDRAG:
8089 case K_MIDDLERELEASE:
8090 case K_LEFTMOUSE:
8091 case K_LEFTDRAG:
8092 case K_LEFTRELEASE:
8093 case K_RIGHTMOUSE:
8094 case K_RIGHTDRAG:
8095 case K_RIGHTRELEASE:
8096 case K_X1MOUSE:
8097 case K_X1DRAG:
8098 case K_X1RELEASE:
8099 case K_X2MOUSE:
8100 case K_X2DRAG:
8101 case K_X2RELEASE:
8102 mod_mask = MOD_MASK_CTRL;
8103 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8104 break;
8105 #endif
8107 case K_IGNORE:
8108 break;
8111 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8113 case 'p':
8114 case 'P':
8115 nv_put(cap);
8116 break;
8118 #ifdef FEAT_BYTEOFF
8119 /* "go": goto byte count from start of buffer */
8120 case 'o':
8121 goto_byte(cap->count0);
8122 break;
8123 #endif
8125 /* "gQ": improved Ex mode */
8126 case 'Q':
8127 if (text_locked())
8129 clearopbeep(cap->oap);
8130 text_locked_msg();
8131 break;
8134 if (!checkclearopq(oap))
8135 do_exmode(TRUE);
8136 break;
8138 #ifdef FEAT_JUMPLIST
8139 case ',':
8140 nv_pcmark(cap);
8141 break;
8143 case ';':
8144 cap->count1 = -cap->count1;
8145 nv_pcmark(cap);
8146 break;
8147 #endif
8149 #ifdef FEAT_WINDOWS
8150 case 't':
8151 goto_tabpage((int)cap->count0);
8152 break;
8153 case 'T':
8154 goto_tabpage(-(int)cap->count1);
8155 break;
8156 #endif
8158 case '+':
8159 case '-': /* "g+" and "g-": undo or redo along the timeline */
8160 if (!checkclearopq(oap))
8161 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
8162 FALSE, FALSE);
8163 break;
8165 default:
8166 clearopbeep(oap);
8167 break;
8172 * Handle "o" and "O" commands.
8174 static void
8175 n_opencmd(cap)
8176 cmdarg_T *cap;
8178 if (!checkclearopq(cap->oap))
8180 #ifdef FEAT_FOLDING
8181 if (cap->cmdchar == 'O')
8182 /* Open above the first line of a folded sequence of lines */
8183 (void)hasFolding(curwin->w_cursor.lnum,
8184 &curwin->w_cursor.lnum, NULL);
8185 else
8186 /* Open below the last line of a folded sequence of lines */
8187 (void)hasFolding(curwin->w_cursor.lnum,
8188 NULL, &curwin->w_cursor.lnum);
8189 #endif
8190 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8191 (cap->cmdchar == 'O' ? 1 : 0)),
8192 (linenr_T)(curwin->w_cursor.lnum +
8193 (cap->cmdchar == 'o' ? 1 : 0))
8194 ) == OK
8195 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8196 #ifdef FEAT_COMMENTS
8197 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8198 #endif
8199 0, 0))
8201 /* When '#' is in 'cpoptions' ignore the count. */
8202 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8203 cap->count1 = 1;
8204 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8210 * "." command: redo last change.
8212 static void
8213 nv_dot(cap)
8214 cmdarg_T *cap;
8216 if (!checkclearopq(cap->oap))
8219 * If "restart_edit" is TRUE, the last but one command is repeated
8220 * instead of the last command (inserting text). This is used for
8221 * CTRL-O <.> in insert mode.
8223 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8224 clearopbeep(cap->oap);
8229 * CTRL-R: undo undo
8231 static void
8232 nv_redo(cap)
8233 cmdarg_T *cap;
8235 if (!checkclearopq(cap->oap))
8237 u_redo((int)cap->count1);
8238 curwin->w_set_curswant = TRUE;
8243 * Handle "U" command.
8245 static void
8246 nv_Undo(cap)
8247 cmdarg_T *cap;
8249 /* In Visual mode and typing "gUU" triggers an operator */
8250 if (cap->oap->op_type == OP_UPPER
8251 #ifdef FEAT_VISUAL
8252 || VIsual_active
8253 #endif
8256 /* translate "gUU" to "gUgU" */
8257 cap->cmdchar = 'g';
8258 cap->nchar = 'U';
8259 nv_operator(cap);
8261 else if (!checkclearopq(cap->oap))
8263 u_undoline();
8264 curwin->w_set_curswant = TRUE;
8269 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8270 * single character.
8272 static void
8273 nv_tilde(cap)
8274 cmdarg_T *cap;
8276 if (!p_to
8277 #ifdef FEAT_VISUAL
8278 && !VIsual_active
8279 #endif
8280 && cap->oap->op_type != OP_TILDE)
8281 n_swapchar(cap);
8282 else
8283 nv_operator(cap);
8287 * Handle an operator command.
8288 * The actual work is done by do_pending_operator().
8290 static void
8291 nv_operator(cap)
8292 cmdarg_T *cap;
8294 int op_type;
8296 op_type = get_op_type(cap->cmdchar, cap->nchar);
8298 if (op_type == cap->oap->op_type) /* double operator works on lines */
8299 nv_lineop(cap);
8300 else if (!checkclearop(cap->oap))
8302 cap->oap->start = curwin->w_cursor;
8303 cap->oap->op_type = op_type;
8304 #ifdef FEAT_EVAL
8305 set_op_var(op_type);
8306 #endif
8310 #ifdef FEAT_EVAL
8312 * Set v:operator to the characters for "optype".
8314 static void
8315 set_op_var(optype)
8316 int optype;
8318 char_u opchars[3];
8320 if (optype == OP_NOP)
8321 set_vim_var_string(VV_OP, NULL, 0);
8322 else
8324 opchars[0] = get_op_char(optype);
8325 opchars[1] = get_extra_op_char(optype);
8326 opchars[2] = NUL;
8327 set_vim_var_string(VV_OP, opchars, -1);
8330 #endif
8333 * Handle linewise operator "dd", "yy", etc.
8335 * "_" is is a strange motion command that helps make operators more logical.
8336 * It is actually implemented, but not documented in the real Vi. This motion
8337 * command actually refers to "the current line". Commands like "dd" and "yy"
8338 * are really an alternate form of "d_" and "y_". It does accept a count, so
8339 * "d3_" works to delete 3 lines.
8341 static void
8342 nv_lineop(cap)
8343 cmdarg_T *cap;
8345 cap->oap->motion_type = MLINE;
8346 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8347 clearopbeep(cap->oap);
8348 else if ( cap->oap->op_type == OP_DELETE
8349 || cap->oap->op_type == OP_LSHIFT
8350 || cap->oap->op_type == OP_RSHIFT)
8351 beginline(BL_SOL | BL_FIX);
8352 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8353 beginline(BL_WHITE | BL_FIX);
8357 * <Home> command.
8359 static void
8360 nv_home(cap)
8361 cmdarg_T *cap;
8363 /* CTRL-HOME is like "gg" */
8364 if (mod_mask & MOD_MASK_CTRL)
8365 nv_goto(cap);
8366 else
8368 cap->count0 = 1;
8369 nv_pipe(cap);
8371 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8372 one-character line). */
8376 * "|" command.
8378 static void
8379 nv_pipe(cap)
8380 cmdarg_T *cap;
8382 cap->oap->motion_type = MCHAR;
8383 cap->oap->inclusive = FALSE;
8384 beginline(0);
8385 if (cap->count0 > 0)
8387 coladvance((colnr_T)(cap->count0 - 1));
8388 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8390 else
8391 curwin->w_curswant = 0;
8392 /* keep curswant at the column where we wanted to go, not where
8393 we ended; differs if line is too short */
8394 curwin->w_set_curswant = FALSE;
8398 * Handle back-word command "b" and "B".
8399 * cap->arg is 1 for "B"
8401 static void
8402 nv_bck_word(cap)
8403 cmdarg_T *cap;
8405 cap->oap->motion_type = MCHAR;
8406 cap->oap->inclusive = FALSE;
8407 curwin->w_set_curswant = TRUE;
8408 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8409 clearopbeep(cap->oap);
8410 #ifdef FEAT_FOLDING
8411 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8412 foldOpenCursor();
8413 #endif
8417 * Handle word motion commands "e", "E", "w" and "W".
8418 * cap->arg is TRUE for "E" and "W".
8420 static void
8421 nv_wordcmd(cap)
8422 cmdarg_T *cap;
8424 int n;
8425 int word_end;
8426 int flag = FALSE;
8427 pos_T startpos = curwin->w_cursor;
8430 * Set inclusive for the "E" and "e" command.
8432 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8433 word_end = TRUE;
8434 else
8435 word_end = FALSE;
8436 cap->oap->inclusive = word_end;
8439 * "cw" and "cW" are a special case.
8441 if (!word_end && cap->oap->op_type == OP_CHANGE)
8443 n = gchar_cursor();
8444 if (n != NUL) /* not an empty line */
8446 if (vim_iswhite(n))
8449 * Reproduce a funny Vi behaviour: "cw" on a blank only
8450 * changes one character, not all blanks until the start of
8451 * the next word. Only do this when the 'w' flag is included
8452 * in 'cpoptions'.
8454 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8456 cap->oap->inclusive = TRUE;
8457 cap->oap->motion_type = MCHAR;
8458 return;
8461 else
8464 * This is a little strange. To match what the real Vi does,
8465 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8466 * that we are not on a space or a TAB. This seems impolite
8467 * at first, but it's really more what we mean when we say
8468 * 'cw'.
8469 * Another strangeness: When standing on the end of a word
8470 * "ce" will change until the end of the next wordt, but "cw"
8471 * will change only one character! This is done by setting
8472 * flag.
8474 cap->oap->inclusive = TRUE;
8475 word_end = TRUE;
8476 flag = TRUE;
8481 cap->oap->motion_type = MCHAR;
8482 curwin->w_set_curswant = TRUE;
8483 if (word_end)
8484 n = end_word(cap->count1, cap->arg, flag, FALSE);
8485 else
8486 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8488 /* Don't leave the cursor on the NUL past the end of line. Unless we
8489 * didn't move it forward. */
8490 if (lt(startpos, curwin->w_cursor))
8491 adjust_cursor(cap->oap);
8493 if (n == FAIL && cap->oap->op_type == OP_NOP)
8494 clearopbeep(cap->oap);
8495 else
8497 #ifdef FEAT_VISUAL
8498 adjust_for_sel(cap);
8499 #endif
8500 #ifdef FEAT_FOLDING
8501 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8502 foldOpenCursor();
8503 #endif
8508 * Used after a movement command: If the cursor ends up on the NUL after the
8509 * end of the line, may move it back to the last character and make the motion
8510 * inclusive.
8512 static void
8513 adjust_cursor(oap)
8514 oparg_T *oap;
8516 /* The cursor cannot remain on the NUL when:
8517 * - the column is > 0
8518 * - not in Visual mode or 'selection' is "o"
8519 * - 'virtualedit' is not "all" and not "onemore".
8521 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
8522 #ifdef FEAT_VISUAL
8523 && (!VIsual_active || *p_sel == 'o')
8524 #endif
8525 #ifdef FEAT_VIRTUALEDIT
8526 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8527 #endif
8530 --curwin->w_cursor.col;
8531 #ifdef FEAT_MBYTE
8532 /* prevent cursor from moving on the trail byte */
8533 if (has_mbyte)
8534 mb_adjust_cursor();
8535 #endif
8536 oap->inclusive = TRUE;
8541 * "0" and "^" commands.
8542 * cap->arg is the argument for beginline().
8544 static void
8545 nv_beginline(cap)
8546 cmdarg_T *cap;
8548 cap->oap->motion_type = MCHAR;
8549 cap->oap->inclusive = FALSE;
8550 beginline(cap->arg);
8551 #ifdef FEAT_FOLDING
8552 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8553 foldOpenCursor();
8554 #endif
8555 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8556 one-character line). */
8559 #ifdef FEAT_VISUAL
8561 * In exclusive Visual mode, may include the last character.
8563 static void
8564 adjust_for_sel(cap)
8565 cmdarg_T *cap;
8567 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8568 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8570 # ifdef FEAT_MBYTE
8571 if (has_mbyte)
8572 inc_cursor();
8573 else
8574 # endif
8575 ++curwin->w_cursor.col;
8576 cap->oap->inclusive = FALSE;
8581 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8582 * Should check VIsual_mode before calling this.
8583 * Returns TRUE when backed up to the previous line.
8585 static int
8586 unadjust_for_sel()
8588 pos_T *pp;
8590 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8592 if (lt(VIsual, curwin->w_cursor))
8593 pp = &curwin->w_cursor;
8594 else
8595 pp = &VIsual;
8596 #ifdef FEAT_VIRTUALEDIT
8597 if (pp->coladd > 0)
8598 --pp->coladd;
8599 else
8600 #endif
8601 if (pp->col > 0)
8603 --pp->col;
8604 #ifdef FEAT_MBYTE
8605 mb_adjustpos(pp);
8606 #endif
8608 else if (pp->lnum > 1)
8610 --pp->lnum;
8611 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8612 return TRUE;
8615 return FALSE;
8619 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8621 static void
8622 nv_select(cap)
8623 cmdarg_T *cap;
8625 if (VIsual_active)
8626 VIsual_select = TRUE;
8627 else if (VIsual_reselect)
8629 cap->nchar = 'v'; /* fake "gv" command */
8630 cap->arg = TRUE;
8631 nv_g_cmd(cap);
8635 #endif
8638 * "G", "gg", CTRL-END, CTRL-HOME.
8639 * cap->arg is TRUE for "G".
8641 static void
8642 nv_goto(cap)
8643 cmdarg_T *cap;
8645 linenr_T lnum;
8647 if (cap->arg)
8648 lnum = curbuf->b_ml.ml_line_count;
8649 else
8650 lnum = 1L;
8651 cap->oap->motion_type = MLINE;
8652 setpcmark();
8654 /* When a count is given, use it instead of the default lnum */
8655 if (cap->count0 != 0)
8656 lnum = cap->count0;
8657 if (lnum < 1L)
8658 lnum = 1L;
8659 else if (lnum > curbuf->b_ml.ml_line_count)
8660 lnum = curbuf->b_ml.ml_line_count;
8661 curwin->w_cursor.lnum = lnum;
8662 beginline(BL_SOL | BL_FIX);
8663 #ifdef FEAT_FOLDING
8664 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8665 foldOpenCursor();
8666 #endif
8670 * CTRL-\ in Normal mode.
8672 static void
8673 nv_normal(cap)
8674 cmdarg_T *cap;
8676 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8678 clearop(cap->oap);
8679 if (restart_edit != 0 && mode_displayed)
8680 clear_cmdline = TRUE; /* unshow mode later */
8681 restart_edit = 0;
8682 #ifdef FEAT_CMDWIN
8683 if (cmdwin_type != 0)
8684 cmdwin_result = Ctrl_C;
8685 #endif
8686 #ifdef FEAT_VISUAL
8687 if (VIsual_active)
8689 end_visual_mode(); /* stop Visual */
8690 redraw_curbuf_later(INVERTED);
8692 #endif
8693 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8694 if (cap->nchar == Ctrl_G && p_im)
8695 restart_edit = 'a';
8697 else
8698 clearopbeep(cap->oap);
8702 * ESC in Normal mode: beep, but don't flush buffers.
8703 * Don't even beep if we are canceling a command.
8705 static void
8706 nv_esc(cap)
8707 cmdarg_T *cap;
8709 int no_reason;
8711 no_reason = (cap->oap->op_type == OP_NOP
8712 && cap->opcount == 0
8713 && cap->count0 == 0
8714 && cap->oap->regname == 0
8715 && !p_im);
8717 if (cap->arg) /* TRUE for CTRL-C */
8719 if (restart_edit == 0
8720 #ifdef FEAT_CMDWIN
8721 && cmdwin_type == 0
8722 #endif
8723 #ifdef FEAT_VISUAL
8724 && !VIsual_active
8725 #endif
8726 && no_reason)
8727 MSG(_("Type :quit<Enter> to exit Vim"));
8729 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8730 * set again below when halfway a mapping. */
8731 if (!p_im)
8732 restart_edit = 0;
8733 #ifdef FEAT_CMDWIN
8734 if (cmdwin_type != 0)
8736 cmdwin_result = K_IGNORE;
8737 got_int = FALSE; /* don't stop executing autocommands et al. */
8738 return;
8740 #endif
8743 #ifdef FEAT_VISUAL
8744 if (VIsual_active)
8746 end_visual_mode(); /* stop Visual */
8747 check_cursor_col(); /* make sure cursor is not beyond EOL */
8748 curwin->w_set_curswant = TRUE;
8749 redraw_curbuf_later(INVERTED);
8751 else
8752 #endif
8753 if (no_reason)
8754 vim_beep();
8755 clearop(cap->oap);
8757 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8758 * set return to Insert mode afterwards. */
8759 if (restart_edit == 0 && goto_im()
8760 #ifdef FEAT_EX_EXTRA
8761 && ex_normal_busy == 0
8762 #endif
8764 restart_edit = 'a';
8768 * Handle "A", "a", "I", "i" and <Insert> commands.
8770 static void
8771 nv_edit(cap)
8772 cmdarg_T *cap;
8774 /* <Insert> is equal to "i" */
8775 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8776 cap->cmdchar = 'i';
8778 #ifdef FEAT_VISUAL
8779 /* in Visual mode "A" and "I" are an operator */
8780 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8781 v_visop(cap);
8783 /* in Visual mode and after an operator "a" and "i" are for text objects */
8784 else
8785 #endif
8786 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8787 && (cap->oap->op_type != OP_NOP
8788 #ifdef FEAT_VISUAL
8789 || VIsual_active
8790 #endif
8793 #ifdef FEAT_TEXTOBJ
8794 nv_object(cap);
8795 #else
8796 clearopbeep(cap->oap);
8797 #endif
8799 else if (!curbuf->b_p_ma && !p_im)
8801 /* Only give this error when 'insertmode' is off. */
8802 EMSG(_(e_modifiable));
8803 clearop(cap->oap);
8805 else if (!checkclearopq(cap->oap))
8807 switch (cap->cmdchar)
8809 case 'A': /* "A"ppend after the line */
8810 curwin->w_set_curswant = TRUE;
8811 #ifdef FEAT_VIRTUALEDIT
8812 if (ve_flags == VE_ALL)
8814 int save_State = State;
8816 /* Pretent Insert mode here to allow the cursor on the
8817 * character past the end of the line */
8818 State = INSERT;
8819 coladvance((colnr_T)MAXCOL);
8820 State = save_State;
8822 else
8823 #endif
8824 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8825 break;
8827 case 'I': /* "I"nsert before the first non-blank */
8828 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
8829 beginline(BL_WHITE);
8830 else
8831 beginline(BL_WHITE|BL_FIX);
8832 break;
8834 case 'a': /* "a"ppend is like "i"nsert on the next character. */
8835 #ifdef FEAT_VIRTUALEDIT
8836 /* increment coladd when in virtual space, increment the
8837 * column otherwise, also to append after an unprintable char */
8838 if (virtual_active()
8839 && (curwin->w_cursor.coladd > 0
8840 || *ml_get_cursor() == NUL
8841 || *ml_get_cursor() == TAB))
8842 curwin->w_cursor.coladd++;
8843 else
8844 #endif
8845 if (*ml_get_cursor() != NUL)
8846 inc_cursor();
8847 break;
8850 #ifdef FEAT_VIRTUALEDIT
8851 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
8853 int save_State = State;
8855 /* Pretent Insert mode here to allow the cursor on the
8856 * character past the end of the line */
8857 State = INSERT;
8858 coladvance(getviscol());
8859 State = save_State;
8861 #endif
8863 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
8868 * Invoke edit() and take care of "restart_edit" and the return value.
8870 static void
8871 invoke_edit(cap, repl, cmd, startln)
8872 cmdarg_T *cap;
8873 int repl; /* "r" or "gr" command */
8874 int cmd;
8875 int startln;
8877 int restart_edit_save = 0;
8879 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
8880 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
8881 * it. */
8882 if (repl || !stuff_empty())
8883 restart_edit_save = restart_edit;
8884 else
8885 restart_edit_save = 0;
8887 /* Always reset "restart_edit", this is not a restarted edit. */
8888 restart_edit = 0;
8890 if (edit(cmd, startln, cap->count1))
8891 cap->retval |= CA_COMMAND_BUSY;
8893 if (restart_edit == 0)
8894 restart_edit = restart_edit_save;
8897 #ifdef FEAT_TEXTOBJ
8899 * "a" or "i" while an operator is pending or in Visual mode: object motion.
8901 static void
8902 nv_object(cap)
8903 cmdarg_T *cap;
8905 int flag;
8906 int include;
8907 char_u *mps_save;
8909 if (cap->cmdchar == 'i')
8910 include = FALSE; /* "ix" = inner object: exclude white space */
8911 else
8912 include = TRUE; /* "ax" = an object: include white space */
8914 /* Make sure (), [], {} and <> are in 'matchpairs' */
8915 mps_save = curbuf->b_p_mps;
8916 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
8918 switch (cap->nchar)
8920 case 'w': /* "aw" = a word */
8921 flag = current_word(cap->oap, cap->count1, include, FALSE);
8922 break;
8923 case 'W': /* "aW" = a WORD */
8924 flag = current_word(cap->oap, cap->count1, include, TRUE);
8925 break;
8926 case 'b': /* "ab" = a braces block */
8927 case '(':
8928 case ')':
8929 flag = current_block(cap->oap, cap->count1, include, '(', ')');
8930 break;
8931 case 'B': /* "aB" = a Brackets block */
8932 case '{':
8933 case '}':
8934 flag = current_block(cap->oap, cap->count1, include, '{', '}');
8935 break;
8936 case '[': /* "a[" = a [] block */
8937 case ']':
8938 flag = current_block(cap->oap, cap->count1, include, '[', ']');
8939 break;
8940 case '<': /* "a<" = a <> block */
8941 case '>':
8942 flag = current_block(cap->oap, cap->count1, include, '<', '>');
8943 break;
8944 case 't': /* "at" = a tag block (xml and html) */
8945 flag = current_tagblock(cap->oap, cap->count1, include);
8946 break;
8947 case 'p': /* "ap" = a paragraph */
8948 flag = current_par(cap->oap, cap->count1, include, 'p');
8949 break;
8950 case 's': /* "as" = a sentence */
8951 flag = current_sent(cap->oap, cap->count1, include);
8952 break;
8953 case '"': /* "a"" = a double quoted string */
8954 case '\'': /* "a'" = a single quoted string */
8955 case '`': /* "a`" = a backtick quoted string */
8956 flag = current_quote(cap->oap, cap->count1, include,
8957 cap->nchar);
8958 break;
8959 #if 0 /* TODO */
8960 case 'S': /* "aS" = a section */
8961 case 'f': /* "af" = a filename */
8962 case 'u': /* "au" = a URL */
8963 #endif
8964 default:
8965 flag = FAIL;
8966 break;
8969 curbuf->b_p_mps = mps_save;
8970 if (flag == FAIL)
8971 clearopbeep(cap->oap);
8972 adjust_cursor_col();
8973 curwin->w_set_curswant = TRUE;
8975 #endif
8978 * "q" command: Start/stop recording.
8979 * "q:", "q/", "q?": edit command-line in command-line window.
8981 static void
8982 nv_record(cap)
8983 cmdarg_T *cap;
8985 if (cap->oap->op_type == OP_FORMAT)
8987 /* "gqq" is the same as "gqgq": format line */
8988 cap->cmdchar = 'g';
8989 cap->nchar = 'q';
8990 nv_operator(cap);
8992 else if (!checkclearop(cap->oap))
8994 #ifdef FEAT_CMDWIN
8995 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
8997 stuffcharReadbuff(cap->nchar);
8998 stuffcharReadbuff(K_CMDWIN);
9000 else
9001 #endif
9002 /* (stop) recording into a named register, unless executing a
9003 * register */
9004 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9005 clearopbeep(cap->oap);
9010 * Handle the "@r" command.
9012 static void
9013 nv_at(cap)
9014 cmdarg_T *cap;
9016 if (checkclearop(cap->oap))
9017 return;
9018 #ifdef FEAT_EVAL
9019 if (cap->nchar == '=')
9021 if (get_expr_register() == NUL)
9022 return;
9024 #endif
9025 while (cap->count1-- && !got_int)
9027 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
9029 clearopbeep(cap->oap);
9030 break;
9032 line_breakcheck();
9037 * Handle the CTRL-U and CTRL-D commands.
9039 static void
9040 nv_halfpage(cap)
9041 cmdarg_T *cap;
9043 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9044 || (cap->cmdchar == Ctrl_D
9045 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9046 clearopbeep(cap->oap);
9047 else if (!checkclearop(cap->oap))
9048 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9052 * Handle "J" or "gJ" command.
9054 static void
9055 nv_join(cap)
9056 cmdarg_T *cap;
9058 #ifdef FEAT_VISUAL
9059 if (VIsual_active) /* join the visual lines */
9060 nv_operator(cap);
9061 else
9062 #endif
9063 if (!checkclearop(cap->oap))
9065 if (cap->count0 <= 1)
9066 cap->count0 = 2; /* default for join is two lines! */
9067 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9068 curbuf->b_ml.ml_line_count)
9069 clearopbeep(cap->oap); /* beyond last line */
9070 else
9072 prep_redo(cap->oap->regname, cap->count0,
9073 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9074 do_do_join(cap->count0, cap->nchar == NUL);
9080 * "P", "gP", "p" and "gp" commands.
9082 static void
9083 nv_put(cap)
9084 cmdarg_T *cap;
9086 #ifdef FEAT_VISUAL
9087 int regname = 0;
9088 void *reg1 = NULL, *reg2 = NULL;
9089 int empty = FALSE;
9090 int was_visual = FALSE;
9091 #endif
9092 int dir;
9093 int flags = 0;
9095 if (cap->oap->op_type != OP_NOP)
9097 #ifdef FEAT_DIFF
9098 /* "dp" is ":diffput" */
9099 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9101 clearop(cap->oap);
9102 nv_diffgetput(TRUE);
9104 else
9105 #endif
9106 clearopbeep(cap->oap);
9108 else
9110 dir = (cap->cmdchar == 'P'
9111 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9112 ? BACKWARD : FORWARD;
9113 prep_redo_cmd(cap);
9114 if (cap->cmdchar == 'g')
9115 flags |= PUT_CURSEND;
9117 #ifdef FEAT_VISUAL
9118 if (VIsual_active)
9120 /* Putting in Visual mode: The put text replaces the selected
9121 * text. First delete the selected text, then put the new text.
9122 * Need to save and restore the registers that the delete
9123 * overwrites if the old contents is being put.
9125 was_visual = TRUE;
9126 regname = cap->oap->regname;
9127 # ifdef FEAT_CLIPBOARD
9128 adjust_clip_reg(&regname);
9129 # endif
9130 if (regname == 0 || VIM_ISDIGIT(regname)
9131 # ifdef FEAT_CLIPBOARD
9132 || (clip_unnamed && (regname == '*' || regname == '+'))
9133 # endif
9137 /* the delete is going to overwrite the register we want to
9138 * put, save it first. */
9139 reg1 = get_register(regname, TRUE);
9142 /* Now delete the selected text. */
9143 cap->cmdchar = 'd';
9144 cap->nchar = NUL;
9145 cap->oap->regname = NUL;
9146 nv_operator(cap);
9147 do_pending_operator(cap, 0, FALSE);
9148 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
9150 /* delete PUT_LINE_BACKWARD; */
9151 cap->oap->regname = regname;
9153 if (reg1 != NULL)
9155 /* Delete probably changed the register we want to put, save
9156 * it first. Then put back what was there before the delete. */
9157 reg2 = get_register(regname, FALSE);
9158 put_register(regname, reg1);
9161 /* When deleted a linewise Visual area, put the register as
9162 * lines to avoid it joined with the next line. When deletion was
9163 * characterwise, split a line when putting lines. */
9164 if (VIsual_mode == 'V')
9165 flags |= PUT_LINE;
9166 else if (VIsual_mode == 'v')
9167 flags |= PUT_LINE_SPLIT;
9168 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9169 flags |= PUT_LINE_FORWARD;
9170 dir = BACKWARD;
9171 if ((VIsual_mode != 'V'
9172 && curwin->w_cursor.col < curbuf->b_op_start.col)
9173 || (VIsual_mode == 'V'
9174 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9175 /* cursor is at the end of the line or end of file, put
9176 * forward. */
9177 dir = FORWARD;
9179 #endif
9180 do_put(cap->oap->regname, dir, cap->count1, flags);
9182 #ifdef FEAT_VISUAL
9183 /* If a register was saved, put it back now. */
9184 if (reg2 != NULL)
9185 put_register(regname, reg2);
9187 /* What to reselect with "gv"? Selecting the just put text seems to
9188 * be the most useful, since the original text was removed. */
9189 if (was_visual)
9191 curbuf->b_visual.vi_start = curbuf->b_op_start;
9192 curbuf->b_visual.vi_end = curbuf->b_op_end;
9195 /* When all lines were selected and deleted do_put() leaves an empty
9196 * line that needs to be deleted now. */
9197 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
9199 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
9201 /* If the cursor was in that line, move it to the end of the last
9202 * line. */
9203 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9205 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9206 coladvance((colnr_T)MAXCOL);
9209 #endif
9210 auto_format(FALSE, TRUE);
9215 * "o" and "O" commands.
9217 static void
9218 nv_open(cap)
9219 cmdarg_T *cap;
9221 #ifdef FEAT_DIFF
9222 /* "do" is ":diffget" */
9223 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9225 clearop(cap->oap);
9226 nv_diffgetput(FALSE);
9228 else
9229 #endif
9230 #ifdef FEAT_VISUAL
9231 if (VIsual_active) /* switch start and end of visual */
9232 v_swap_corners(cap->cmdchar);
9233 else
9234 #endif
9235 n_opencmd(cap);
9238 #ifdef FEAT_SNIFF
9239 /*ARGSUSED*/
9240 static void
9241 nv_sniff(cap)
9242 cmdarg_T *cap;
9244 ProcessSniffRequests();
9246 #endif
9248 #ifdef FEAT_NETBEANS_INTG
9249 static void
9250 nv_nbcmd(cap)
9251 cmdarg_T *cap;
9253 netbeans_keycommand(cap->nchar);
9255 #endif
9257 #ifdef FEAT_DND
9258 /*ARGSUSED*/
9259 static void
9260 nv_drop(cap)
9261 cmdarg_T *cap;
9263 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9265 #endif
9267 #ifdef FEAT_AUTOCMD
9269 * Trigger CursorHold event.
9270 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9271 * input buffer. "did_cursorhold" is set to avoid retriggering.
9273 /*ARGSUSED*/
9274 static void
9275 nv_cursorhold(cap)
9276 cmdarg_T *cap;
9278 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9279 did_cursorhold = TRUE;
9280 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
9282 #endif