Merge branch 'vim'
[MacVim.git] / src / normal.c
blob869721a9390f6ba279c9ad2d5741034e182c9993
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
186 static char *e_noident = N_("E349: No identifier under cursor");
189 * Function to be called for a Normal or Visual mode command.
190 * The argument is a cmdarg_T.
192 typedef void (*nv_func_T) __ARGS((cmdarg_T *cap));
194 /* Values for cmd_flags. */
195 #define NV_NCH 0x01 /* may need to get a second char */
196 #define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
197 #define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
198 #define NV_LANG 0x08 /* second char needs language adjustment */
200 #define NV_SS 0x10 /* may start selection */
201 #define NV_SSS 0x20 /* may start selection with shift modifier */
202 #define NV_STS 0x40 /* may stop selection without shift modif. */
203 #define NV_RL 0x80 /* 'rightleft' modifies command */
204 #define NV_KEEPREG 0x100 /* don't clear regname */
205 #define NV_NCW 0x200 /* not allowed in command-line window */
208 * Generally speaking, every Normal mode command should either clear any
209 * pending operator (with *clearop*()), or set the motion type variable
210 * oap->motion_type.
212 * When a cursor motion command is made, it is marked as being a character or
213 * line oriented motion. Then, if an operator is in effect, the operation
214 * becomes character or line oriented accordingly.
218 * This table contains one entry for every Normal or Visual mode command.
219 * The order doesn't matter, init_normal_cmds() will create a sorted index.
220 * It is faster when all keys from zero to '~' are present.
222 static const struct nv_cmd
224 int cmd_char; /* (first) command character */
225 nv_func_T cmd_func; /* function for this command */
226 short_u cmd_flags; /* NV_ flags */
227 short cmd_arg; /* value for ca.arg */
228 } nv_cmds[] =
230 {NUL, nv_error, 0, 0},
231 {Ctrl_A, nv_addsub, 0, 0},
232 {Ctrl_B, nv_page, NV_STS, BACKWARD},
233 {Ctrl_C, nv_esc, 0, TRUE},
234 {Ctrl_D, nv_halfpage, 0, 0},
235 {Ctrl_E, nv_scroll_line, 0, TRUE},
236 {Ctrl_F, nv_page, NV_STS, FORWARD},
237 {Ctrl_G, nv_ctrlg, 0, 0},
238 {Ctrl_H, nv_ctrlh, 0, 0},
239 {Ctrl_I, nv_pcmark, 0, 0},
240 {NL, nv_down, 0, FALSE},
241 {Ctrl_K, nv_error, 0, 0},
242 {Ctrl_L, nv_clear, 0, 0},
243 {Ctrl_M, nv_down, 0, TRUE},
244 {Ctrl_N, nv_down, NV_STS, FALSE},
245 {Ctrl_O, nv_ctrlo, 0, 0},
246 {Ctrl_P, nv_up, NV_STS, FALSE},
247 #ifdef FEAT_VISUAL
248 {Ctrl_Q, nv_visual, 0, FALSE},
249 #else
250 {Ctrl_Q, nv_ignore, 0, 0},
251 #endif
252 {Ctrl_R, nv_redo, 0, 0},
253 {Ctrl_S, nv_ignore, 0, 0},
254 {Ctrl_T, nv_tagpop, NV_NCW, 0},
255 {Ctrl_U, nv_halfpage, 0, 0},
256 #ifdef FEAT_VISUAL
257 {Ctrl_V, nv_visual, 0, FALSE},
258 {'V', nv_visual, 0, FALSE},
259 {'v', nv_visual, 0, FALSE},
260 #else
261 {Ctrl_V, nv_error, 0, 0},
262 {'V', nv_error, 0, 0},
263 {'v', nv_error, 0, 0},
264 #endif
265 {Ctrl_W, nv_window, 0, 0},
266 {Ctrl_X, nv_addsub, 0, 0},
267 {Ctrl_Y, nv_scroll_line, 0, FALSE},
268 {Ctrl_Z, nv_suspend, 0, 0},
269 {ESC, nv_esc, 0, FALSE},
270 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
271 {Ctrl_RSB, nv_ident, NV_NCW, 0},
272 {Ctrl_HAT, nv_hat, NV_NCW, 0},
273 {Ctrl__, nv_error, 0, 0},
274 {' ', nv_right, 0, 0},
275 {'!', nv_operator, 0, 0},
276 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
277 {'#', nv_ident, 0, 0},
278 {'$', nv_dollar, 0, 0},
279 {'%', nv_percent, 0, 0},
280 {'&', nv_optrans, 0, 0},
281 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
282 {'(', nv_brace, 0, BACKWARD},
283 {')', nv_brace, 0, FORWARD},
284 {'*', nv_ident, 0, 0},
285 {'+', nv_down, 0, TRUE},
286 {',', nv_csearch, 0, TRUE},
287 {'-', nv_up, 0, TRUE},
288 {'.', nv_dot, NV_KEEPREG, 0},
289 {'/', nv_search, 0, FALSE},
290 {'0', nv_beginline, 0, 0},
291 {'1', nv_ignore, 0, 0},
292 {'2', nv_ignore, 0, 0},
293 {'3', nv_ignore, 0, 0},
294 {'4', nv_ignore, 0, 0},
295 {'5', nv_ignore, 0, 0},
296 {'6', nv_ignore, 0, 0},
297 {'7', nv_ignore, 0, 0},
298 {'8', nv_ignore, 0, 0},
299 {'9', nv_ignore, 0, 0},
300 {':', nv_colon, 0, 0},
301 {';', nv_csearch, 0, FALSE},
302 {'<', nv_operator, NV_RL, 0},
303 {'=', nv_operator, 0, 0},
304 {'>', nv_operator, NV_RL, 0},
305 {'?', nv_search, 0, FALSE},
306 {'@', nv_at, NV_NCH_NOP, FALSE},
307 {'A', nv_edit, 0, 0},
308 {'B', nv_bck_word, 0, 1},
309 {'C', nv_abbrev, NV_KEEPREG, 0},
310 {'D', nv_abbrev, NV_KEEPREG, 0},
311 {'E', nv_wordcmd, 0, TRUE},
312 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
313 {'G', nv_goto, 0, TRUE},
314 {'H', nv_scroll, 0, 0},
315 {'I', nv_edit, 0, 0},
316 {'J', nv_join, 0, 0},
317 {'K', nv_ident, 0, 0},
318 {'L', nv_scroll, 0, 0},
319 {'M', nv_scroll, 0, 0},
320 {'N', nv_next, 0, SEARCH_REV},
321 {'O', nv_open, 0, 0},
322 {'P', nv_put, 0, 0},
323 {'Q', nv_exmode, NV_NCW, 0},
324 {'R', nv_Replace, 0, FALSE},
325 {'S', nv_subst, NV_KEEPREG, 0},
326 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
327 {'U', nv_Undo, 0, 0},
328 {'W', nv_wordcmd, 0, TRUE},
329 {'X', nv_abbrev, NV_KEEPREG, 0},
330 {'Y', nv_abbrev, NV_KEEPREG, 0},
331 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
332 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
333 {'\\', nv_error, 0, 0},
334 {']', nv_brackets, NV_NCH_ALW, FORWARD},
335 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
336 {'_', nv_lineop, 0, 0},
337 {'`', nv_gomark, NV_NCH_ALW, FALSE},
338 {'a', nv_edit, NV_NCH, 0},
339 {'b', nv_bck_word, 0, 0},
340 {'c', nv_operator, 0, 0},
341 {'d', nv_operator, 0, 0},
342 {'e', nv_wordcmd, 0, FALSE},
343 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
344 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
345 {'h', nv_left, NV_RL, 0},
346 {'i', nv_edit, NV_NCH, 0},
347 {'j', nv_down, 0, FALSE},
348 {'k', nv_up, 0, FALSE},
349 {'l', nv_right, NV_RL, 0},
350 {'m', nv_mark, NV_NCH_NOP, 0},
351 {'n', nv_next, 0, 0},
352 {'o', nv_open, 0, 0},
353 {'p', nv_put, 0, 0},
354 {'q', nv_record, NV_NCH, 0},
355 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
356 {'s', nv_subst, NV_KEEPREG, 0},
357 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
358 {'u', nv_undo, 0, 0},
359 {'w', nv_wordcmd, 0, FALSE},
360 {'x', nv_abbrev, NV_KEEPREG, 0},
361 {'y', nv_operator, 0, 0},
362 {'z', nv_zet, NV_NCH_ALW, 0},
363 {'{', nv_findpar, 0, BACKWARD},
364 {'|', nv_pipe, 0, 0},
365 {'}', nv_findpar, 0, FORWARD},
366 {'~', nv_tilde, 0, 0},
368 /* pound sign */
369 {POUND, nv_ident, 0, 0},
370 #ifdef FEAT_MOUSE
371 {K_MOUSEUP, nv_mousescroll, 0, TRUE},
372 {K_MOUSEDOWN, nv_mousescroll, 0, FALSE},
373 {K_LEFTMOUSE, nv_mouse, 0, 0},
374 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
375 {K_LEFTDRAG, nv_mouse, 0, 0},
376 {K_LEFTRELEASE, nv_mouse, 0, 0},
377 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
378 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
379 {K_MIDDLEDRAG, nv_mouse, 0, 0},
380 {K_MIDDLERELEASE, nv_mouse, 0, 0},
381 {K_RIGHTMOUSE, nv_mouse, 0, 0},
382 {K_RIGHTDRAG, nv_mouse, 0, 0},
383 {K_RIGHTRELEASE, nv_mouse, 0, 0},
384 {K_X1MOUSE, nv_mouse, 0, 0},
385 {K_X1DRAG, nv_mouse, 0, 0},
386 {K_X1RELEASE, nv_mouse, 0, 0},
387 {K_X2MOUSE, nv_mouse, 0, 0},
388 {K_X2DRAG, nv_mouse, 0, 0},
389 {K_X2RELEASE, nv_mouse, 0, 0},
390 #endif
391 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
392 {K_NOP, nv_nop, 0, 0},
393 {K_INS, nv_edit, 0, 0},
394 {K_KINS, nv_edit, 0, 0},
395 {K_BS, nv_ctrlh, 0, 0},
396 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
397 {K_S_UP, nv_page, NV_SS, BACKWARD},
398 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
399 {K_S_DOWN, nv_page, NV_SS, FORWARD},
400 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
401 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
402 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
403 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
404 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
405 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
406 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
407 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
408 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
409 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
410 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
411 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
412 {K_S_END, nv_end, NV_SS, FALSE},
413 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
414 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
415 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
416 {K_S_HOME, nv_home, NV_SS, 0},
417 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
418 {K_DEL, nv_abbrev, 0, 0},
419 {K_KDEL, nv_abbrev, 0, 0},
420 {K_UNDO, nv_kundo, 0, 0},
421 {K_HELP, nv_help, NV_NCW, 0},
422 {K_F1, nv_help, NV_NCW, 0},
423 {K_XF1, nv_help, NV_NCW, 0},
424 #ifdef FEAT_VISUAL
425 {K_SELECT, nv_select, 0, 0},
426 #endif
427 #ifdef FEAT_GUI
428 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
429 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
430 #endif
431 #ifdef FEAT_GUI_TABLINE
432 {K_TABLINE, nv_tabline, 0, 0},
433 {K_TABMENU, nv_tabmenu, 0, 0},
434 #endif
435 #ifdef FEAT_FKMAP
436 {K_F8, farsi_fkey, 0, 0},
437 {K_F9, farsi_fkey, 0, 0},
438 #endif
439 #ifdef FEAT_SNIFF
440 {K_SNIFF, nv_sniff, 0, 0},
441 #endif
442 #ifdef FEAT_NETBEANS_INTG
443 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
444 #endif
445 #ifdef FEAT_DND
446 {K_DROP, nv_drop, NV_STS, 0},
447 #endif
448 #ifdef FEAT_AUTOCMD
449 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
450 #endif
453 /* Number of commands in nv_cmds[]. */
454 #define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
456 /* Sorted index of commands in nv_cmds[]. */
457 static short nv_cmd_idx[NV_CMDS_SIZE];
459 /* The highest index for which
460 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
461 static int nv_max_linear;
464 * Compare functions for qsort() below, that checks the command character
465 * through the index in nv_cmd_idx[].
467 static int
468 #ifdef __BORLANDC__
469 _RTLENTRYF
470 #endif
471 nv_compare(s1, s2)
472 const void *s1;
473 const void *s2;
475 int c1, c2;
477 /* The commands are sorted on absolute value. */
478 c1 = nv_cmds[*(const short *)s1].cmd_char;
479 c2 = nv_cmds[*(const short *)s2].cmd_char;
480 if (c1 < 0)
481 c1 = -c1;
482 if (c2 < 0)
483 c2 = -c2;
484 return c1 - c2;
488 * Initialize the nv_cmd_idx[] table.
490 void
491 init_normal_cmds()
493 int i;
495 /* Fill the index table with a one to one relation. */
496 for (i = 0; i < NV_CMDS_SIZE; ++i)
497 nv_cmd_idx[i] = i;
499 /* Sort the commands by the command character. */
500 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
502 /* Find the first entry that can't be indexed by the command character. */
503 for (i = 0; i < NV_CMDS_SIZE; ++i)
504 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
505 break;
506 nv_max_linear = i - 1;
510 * Search for a command in the commands table.
511 * Returns -1 for invalid command.
513 static int
514 find_command(cmdchar)
515 int cmdchar;
517 int i;
518 int idx;
519 int top, bot;
520 int c;
522 #ifdef FEAT_MBYTE
523 /* A multi-byte character is never a command. */
524 if (cmdchar >= 0x100)
525 return -1;
526 #endif
528 /* We use the absolute value of the character. Special keys have a
529 * negative value, but are sorted on their absolute value. */
530 if (cmdchar < 0)
531 cmdchar = -cmdchar;
533 /* If the character is in the first part: The character is the index into
534 * nv_cmd_idx[]. */
535 if (cmdchar <= nv_max_linear)
536 return nv_cmd_idx[cmdchar];
538 /* Perform a binary search. */
539 bot = nv_max_linear + 1;
540 top = NV_CMDS_SIZE - 1;
541 idx = -1;
542 while (bot <= top)
544 i = (top + bot) / 2;
545 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
546 if (c < 0)
547 c = -c;
548 if (cmdchar == c)
550 idx = nv_cmd_idx[i];
551 break;
553 if (cmdchar > c)
554 bot = i + 1;
555 else
556 top = i - 1;
558 return idx;
562 * Execute a command in Normal mode.
564 /*ARGSUSED*/
565 void
566 normal_cmd(oap, toplevel)
567 oparg_T *oap;
568 int toplevel; /* TRUE when called from main() */
570 cmdarg_T ca; /* command arguments */
571 int c;
572 int ctrl_w = FALSE; /* got CTRL-W command */
573 int old_col = curwin->w_curswant;
574 #ifdef FEAT_CMDL_INFO
575 int need_flushbuf; /* need to call out_flush() */
576 #endif
577 #ifdef FEAT_VISUAL
578 pos_T old_pos; /* cursor position before command */
579 int mapped_len;
580 static int old_mapped_len = 0;
581 #endif
582 int idx;
584 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
585 ca.oap = oap;
587 /* Use a count remembered from before entering an operator. After typing
588 * "3d" we return from normal_cmd() and come back here, the "3" is
589 * remembered in "opcount". */
590 ca.opcount = opcount;
592 #ifdef FEAT_SNIFF
593 want_sniff_request = sniff_connected;
594 #endif
597 * If there is an operator pending, then the command we take this time
598 * will terminate it. Finish_op tells us to finish the operation before
599 * returning this time (unless the operation was cancelled).
601 #ifdef CURSOR_SHAPE
602 c = finish_op;
603 #endif
604 finish_op = (oap->op_type != OP_NOP);
605 #ifdef CURSOR_SHAPE
606 if (finish_op != c)
608 ui_cursor_shape(); /* may show different cursor shape */
609 # ifdef FEAT_MOUSESHAPE
610 update_mouseshape(-1);
611 # endif
613 #endif
615 /* When not finishing an operator and no register name typed, reset the
616 * count. */
617 if (!finish_op && !oap->regname)
618 ca.opcount = 0;
620 #ifdef FEAT_AUTOCMD
621 /* Restore counts from before receiving K_CURSORHOLD. This means after
622 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
623 * "3 * 2". */
624 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
626 ca.opcount = oap->prev_opcount;
627 ca.count0 = oap->prev_count0;
628 oap->prev_opcount = 0;
629 oap->prev_count0 = 0;
631 #endif
633 #ifdef FEAT_VISUAL
634 mapped_len = typebuf_maplen();
635 #endif
637 State = NORMAL_BUSY;
638 #ifdef USE_ON_FLY_SCROLL
639 dont_scroll = FALSE; /* allow scrolling here */
640 #endif
643 * Get the command character from the user.
645 c = safe_vgetc();
647 #ifdef FEAT_LANGMAP
648 LANGMAP_ADJUST(c, TRUE);
649 #endif
651 #ifdef FEAT_VISUAL
653 * If a mapping was started in Visual or Select mode, remember the length
654 * of the mapping. This is used below to not return to Insert mode for as
655 * long as the mapping is being executed.
657 if (restart_edit == 0)
658 old_mapped_len = 0;
659 else if (old_mapped_len
660 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
661 old_mapped_len = typebuf_maplen();
662 #endif
664 if (c == NUL)
665 c = K_ZERO;
667 #ifdef FEAT_VISUAL
669 * In Select mode, typed text replaces the selection.
671 if (VIsual_active
672 && VIsual_select
673 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
675 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
676 * 'insertmode' is set) fake a "d"elete command, Insert mode will
677 * restart automatically.
678 * Insert the typed character in the typeahead buffer, so that it can
679 * be mapped in Insert mode. Required for ":lmap" to work. */
680 ins_char_typebuf(c);
681 if (restart_edit != 0)
682 c = 'd';
683 else
684 c = 'c';
685 msg_nowait = TRUE; /* don't delay going to insert mode */
687 #endif
689 #ifdef FEAT_CMDL_INFO
690 need_flushbuf = add_to_showcmd(c);
691 #endif
693 getcount:
694 #ifdef FEAT_VISUAL
695 if (!(VIsual_active && VIsual_select))
696 #endif
699 * Handle a count before a command and compute ca.count0.
700 * Note that '0' is a command and not the start of a count, but it's
701 * part of a count after other digits.
703 while ( (c >= '1' && c <= '9')
704 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
706 if (c == K_DEL || c == K_KDEL)
708 ca.count0 /= 10;
709 #ifdef FEAT_CMDL_INFO
710 del_from_showcmd(4); /* delete the digit and ~@% */
711 #endif
713 else
714 ca.count0 = ca.count0 * 10 + (c - '0');
715 if (ca.count0 < 0) /* got too large! */
716 ca.count0 = 999999999L;
717 #ifdef FEAT_EVAL
718 /* Set v:count here, when called from main() and not a stuffed
719 * command, so that v:count can be used in an expression mapping
720 * right after the count. */
721 if (toplevel && stuff_empty())
722 set_vcount(ca.count0, ca.count0 == 0 ? 1 : ca.count0);
723 #endif
724 if (ctrl_w)
726 ++no_mapping;
727 ++allow_keys; /* no mapping for nchar, but keys */
729 ++no_zero_mapping; /* don't map zero here */
730 c = plain_vgetc();
731 #ifdef FEAT_LANGMAP
732 LANGMAP_ADJUST(c, TRUE);
733 #endif
734 --no_zero_mapping;
735 if (ctrl_w)
737 --no_mapping;
738 --allow_keys;
740 #ifdef FEAT_CMDL_INFO
741 need_flushbuf |= add_to_showcmd(c);
742 #endif
746 * If we got CTRL-W there may be a/another count
748 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
750 ctrl_w = TRUE;
751 ca.opcount = ca.count0; /* remember first count */
752 ca.count0 = 0;
753 ++no_mapping;
754 ++allow_keys; /* no mapping for nchar, but keys */
755 c = plain_vgetc(); /* get next character */
756 #ifdef FEAT_LANGMAP
757 LANGMAP_ADJUST(c, TRUE);
758 #endif
759 --no_mapping;
760 --allow_keys;
761 #ifdef FEAT_CMDL_INFO
762 need_flushbuf |= add_to_showcmd(c);
763 #endif
764 goto getcount; /* jump back */
768 #ifdef FEAT_AUTOCMD
769 if (c == K_CURSORHOLD)
771 /* Save the count values so that ca.opcount and ca.count0 are exactly
772 * the same when coming back here after handling K_CURSORHOLD. */
773 oap->prev_opcount = ca.opcount;
774 oap->prev_count0 = ca.count0;
776 else
777 #endif
778 if (ca.opcount != 0)
781 * If we're in the middle of an operator (including after entering a
782 * yank buffer with '"') AND we had a count before the operator, then
783 * that count overrides the current value of ca.count0.
784 * What this means effectively, is that commands like "3dw" get turned
785 * into "d3w" which makes things fall into place pretty neatly.
786 * If you give a count before AND after the operator, they are
787 * multiplied.
789 if (ca.count0)
790 ca.count0 *= ca.opcount;
791 else
792 ca.count0 = ca.opcount;
796 * Always remember the count. It will be set to zero (on the next call,
797 * above) when there is no pending operator.
798 * When called from main(), save the count for use by the "count" built-in
799 * variable.
801 ca.opcount = ca.count0;
802 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
804 #ifdef FEAT_EVAL
806 * Only set v:count when called from main() and not a stuffed command.
808 if (toplevel && stuff_empty())
809 set_vcount(ca.count0, ca.count1);
810 #endif
813 * Find the command character in the table of commands.
814 * For CTRL-W we already got nchar when looking for a count.
816 if (ctrl_w)
818 ca.nchar = c;
819 ca.cmdchar = Ctrl_W;
821 else
822 ca.cmdchar = c;
823 idx = find_command(ca.cmdchar);
824 if (idx < 0)
826 /* Not a known command: beep. */
827 clearopbeep(oap);
828 goto normal_end;
831 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
833 /* This command is not allowed while editing a ccmdline: beep. */
834 clearopbeep(oap);
835 text_locked_msg();
836 goto normal_end;
838 #ifdef FEAT_AUTOCMD
839 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
840 goto normal_end;
841 #endif
843 #ifdef FEAT_VISUAL
845 * In Visual/Select mode, a few keys are handled in a special way.
847 if (VIsual_active)
849 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
850 if (km_stopsel
851 && (nv_cmds[idx].cmd_flags & NV_STS)
852 && !(mod_mask & MOD_MASK_SHIFT))
854 end_visual_mode();
855 redraw_curbuf_later(INVERTED);
858 /* Keys that work different when 'keymodel' contains "startsel" */
859 if (km_startsel)
861 if (nv_cmds[idx].cmd_flags & NV_SS)
863 unshift_special(&ca);
864 idx = find_command(ca.cmdchar);
865 if (idx < 0)
867 /* Just in case */
868 clearopbeep(oap);
869 goto normal_end;
872 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
873 && (mod_mask & MOD_MASK_SHIFT))
875 mod_mask &= ~MOD_MASK_SHIFT;
879 #endif
881 #ifdef FEAT_RIGHTLEFT
882 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
883 && (nv_cmds[idx].cmd_flags & NV_RL))
885 /* Invert horizontal movements and operations. Only when typed by the
886 * user directly, not when the result of a mapping or "x" translated
887 * to "dl". */
888 switch (ca.cmdchar)
890 case 'l': ca.cmdchar = 'h'; break;
891 case K_RIGHT: ca.cmdchar = K_LEFT; break;
892 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
893 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
894 case 'h': ca.cmdchar = 'l'; break;
895 case K_LEFT: ca.cmdchar = K_RIGHT; break;
896 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
897 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
898 case '>': ca.cmdchar = '<'; break;
899 case '<': ca.cmdchar = '>'; break;
901 idx = find_command(ca.cmdchar);
903 #endif
906 * Get an additional character if we need one.
908 if ((nv_cmds[idx].cmd_flags & NV_NCH)
909 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
910 && oap->op_type == OP_NOP)
911 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
912 || (ca.cmdchar == 'q'
913 && oap->op_type == OP_NOP
914 && !Recording
915 && !Exec_reg)
916 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
917 && (oap->op_type != OP_NOP
918 #ifdef FEAT_VISUAL
919 || VIsual_active
920 #endif
921 ))))
923 int *cp;
924 int repl = FALSE; /* get character for replace mode */
925 int lit = FALSE; /* get extra character literally */
926 int langmap_active = FALSE; /* using :lmap mappings */
927 int lang; /* getting a text character */
928 #ifdef USE_IM_CONTROL
929 int save_smd; /* saved value of p_smd */
930 #endif
932 ++no_mapping;
933 ++allow_keys; /* no mapping for nchar, but allow key codes */
934 #ifdef FEAT_AUTOCMD
935 /* Don't generate a CursorHold event here, most commands can't handle
936 * it, e.g., nv_replace(), nv_csearch(). */
937 did_cursorhold = TRUE;
938 #endif
939 if (ca.cmdchar == 'g')
942 * For 'g' get the next character now, so that we can check for
943 * "gr", "g'" and "g`".
945 ca.nchar = plain_vgetc();
946 #ifdef FEAT_LANGMAP
947 LANGMAP_ADJUST(ca.nchar, TRUE);
948 #endif
949 #ifdef FEAT_CMDL_INFO
950 need_flushbuf |= add_to_showcmd(ca.nchar);
951 #endif
952 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
953 || ca.nchar == Ctrl_BSL)
955 cp = &ca.extra_char; /* need to get a third character */
956 if (ca.nchar != 'r')
957 lit = TRUE; /* get it literally */
958 else
959 repl = TRUE; /* get it in replace mode */
961 else
962 cp = NULL; /* no third character needed */
964 else
966 if (ca.cmdchar == 'r') /* get it in replace mode */
967 repl = TRUE;
968 cp = &ca.nchar;
970 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
973 * Get a second or third character.
975 if (cp != NULL)
977 #ifdef CURSOR_SHAPE
978 if (repl)
980 State = REPLACE; /* pretend Replace mode */
981 ui_cursor_shape(); /* show different cursor shape */
983 #endif
984 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
986 /* Allow mappings defined with ":lmap". */
987 --no_mapping;
988 --allow_keys;
989 if (repl)
990 State = LREPLACE;
991 else
992 State = LANGMAP;
993 langmap_active = TRUE;
995 #ifdef USE_IM_CONTROL
996 save_smd = p_smd;
997 p_smd = FALSE; /* Don't let the IM code show the mode here */
998 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
999 im_set_active(TRUE);
1000 #endif
1002 *cp = plain_vgetc();
1004 if (langmap_active)
1006 /* Undo the decrement done above */
1007 ++no_mapping;
1008 ++allow_keys;
1009 State = NORMAL_BUSY;
1011 #ifdef USE_IM_CONTROL
1012 if (lang)
1014 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
1015 im_save_status(&curbuf->b_p_iminsert);
1016 im_set_active(FALSE);
1018 p_smd = save_smd;
1019 #endif
1020 #ifdef CURSOR_SHAPE
1021 State = NORMAL_BUSY;
1022 #endif
1023 #ifdef FEAT_CMDL_INFO
1024 need_flushbuf |= add_to_showcmd(*cp);
1025 #endif
1027 if (!lit)
1029 #ifdef FEAT_DIGRAPHS
1030 /* Typing CTRL-K gets a digraph. */
1031 if (*cp == Ctrl_K
1032 && ((nv_cmds[idx].cmd_flags & NV_LANG)
1033 || cp == &ca.extra_char)
1034 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1036 c = get_digraph(FALSE);
1037 if (c > 0)
1039 *cp = c;
1040 # ifdef FEAT_CMDL_INFO
1041 /* Guessing how to update showcmd here... */
1042 del_from_showcmd(3);
1043 need_flushbuf |= add_to_showcmd(*cp);
1044 # endif
1047 #endif
1049 #ifdef FEAT_LANGMAP
1050 /* adjust chars > 127, except after "tTfFr" commands */
1051 LANGMAP_ADJUST(*cp, !lang);
1052 #endif
1053 #ifdef FEAT_RIGHTLEFT
1054 /* adjust Hebrew mapped char */
1055 if (p_hkmap && lang && KeyTyped)
1056 *cp = hkmap(*cp);
1057 # ifdef FEAT_FKMAP
1058 /* adjust Farsi mapped char */
1059 if (p_fkmap && lang && KeyTyped)
1060 *cp = fkmap(*cp);
1061 # endif
1062 #endif
1066 * When the next character is CTRL-\ a following CTRL-N means the
1067 * command is aborted and we go to Normal mode.
1069 if (cp == &ca.extra_char
1070 && ca.nchar == Ctrl_BSL
1071 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1073 ca.cmdchar = Ctrl_BSL;
1074 ca.nchar = ca.extra_char;
1075 idx = find_command(ca.cmdchar);
1077 else if (*cp == Ctrl_BSL)
1079 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1081 /* There is a busy wait here when typing "f<C-\>" and then
1082 * something different from CTRL-N. Can't be avoided. */
1083 while ((c = vpeekc()) <= 0 && towait > 0L)
1085 do_sleep(towait > 50L ? 50L : towait);
1086 towait -= 50L;
1088 if (c > 0)
1090 c = plain_vgetc();
1091 if (c != Ctrl_N && c != Ctrl_G)
1092 vungetc(c);
1093 else
1095 ca.cmdchar = Ctrl_BSL;
1096 ca.nchar = c;
1097 idx = find_command(ca.cmdchar);
1102 #ifdef FEAT_MBYTE
1103 /* When getting a text character and the next character is a
1104 * multi-byte character, it could be a composing character.
1105 * However, don't wait for it to arrive. */
1106 while (enc_utf8 && lang && (c = vpeekc()) > 0
1107 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1109 c = plain_vgetc();
1110 if (!utf_iscomposing(c))
1112 vungetc(c); /* it wasn't, put it back */
1113 break;
1115 else if (ca.ncharC1 == 0)
1116 ca.ncharC1 = c;
1117 else
1118 ca.ncharC2 = c;
1120 #endif
1122 --no_mapping;
1123 --allow_keys;
1126 #ifdef FEAT_CMDL_INFO
1128 * Flush the showcmd characters onto the screen so we can see them while
1129 * the command is being executed. Only do this when the shown command was
1130 * actually displayed, otherwise this will slow down a lot when executing
1131 * mappings.
1133 if (need_flushbuf)
1134 out_flush();
1135 #endif
1136 #ifdef FEAT_AUTOCMD
1137 if (ca.cmdchar != K_IGNORE)
1138 did_cursorhold = FALSE;
1139 #endif
1141 State = NORMAL;
1143 if (ca.nchar == ESC)
1145 clearop(oap);
1146 if (restart_edit == 0 && goto_im())
1147 restart_edit = 'a';
1148 goto normal_end;
1151 if (ca.cmdchar != K_IGNORE)
1153 msg_didout = FALSE; /* don't scroll screen up for normal command */
1154 msg_col = 0;
1157 #ifdef FEAT_VISUAL
1158 old_pos = curwin->w_cursor; /* remember where cursor was */
1160 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1161 * mode. */
1162 if (!VIsual_active && km_startsel)
1164 if (nv_cmds[idx].cmd_flags & NV_SS)
1166 start_selection();
1167 unshift_special(&ca);
1168 idx = find_command(ca.cmdchar);
1170 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1171 && (mod_mask & MOD_MASK_SHIFT))
1173 start_selection();
1174 mod_mask &= ~MOD_MASK_SHIFT;
1177 #endif
1180 * Execute the command!
1181 * Call the command function found in the commands table.
1183 ca.arg = nv_cmds[idx].cmd_arg;
1184 (nv_cmds[idx].cmd_func)(&ca);
1187 * If we didn't start or finish an operator, reset oap->regname, unless we
1188 * need it later.
1190 if (!finish_op
1191 && !oap->op_type
1192 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1194 clearop(oap);
1195 #ifdef FEAT_EVAL
1196 set_reg_var('"');
1197 #endif
1200 #ifdef FEAT_VISUAL
1201 /* Get the length of mapped chars again after typing a count, second
1202 * character or "z333<cr>". */
1203 if (old_mapped_len > 0)
1204 old_mapped_len = typebuf_maplen();
1205 #endif
1208 * If an operation is pending, handle it...
1210 do_pending_operator(&ca, old_col, FALSE);
1213 * Wait for a moment when a message is displayed that will be overwritten
1214 * by the mode message.
1215 * In Visual mode and with "^O" in Insert mode, a short message will be
1216 * overwritten by the mode message. Wait a bit, until a key is hit.
1217 * In Visual mode, it's more important to keep the Visual area updated
1218 * than keeping a message (e.g. from a /pat search).
1219 * Only do this if the command was typed, not from a mapping.
1220 * Don't wait when emsg_silent is non-zero.
1221 * Also wait a bit after an error message, e.g. for "^O:".
1222 * Don't redraw the screen, it would remove the message.
1224 if ( ((p_smd
1225 && msg_silent == 0
1226 && (restart_edit != 0
1227 #ifdef FEAT_VISUAL
1228 || (VIsual_active
1229 && old_pos.lnum == curwin->w_cursor.lnum
1230 && old_pos.col == curwin->w_cursor.col)
1231 #endif
1233 && (clear_cmdline
1234 || redraw_cmdline)
1235 && (msg_didout || (msg_didany && msg_scroll))
1236 && !msg_nowait
1237 && KeyTyped)
1238 || (restart_edit != 0
1239 #ifdef FEAT_VISUAL
1240 && !VIsual_active
1241 #endif
1242 && (msg_scroll
1243 || emsg_on_display)))
1244 && oap->regname == 0
1245 && !(ca.retval & CA_COMMAND_BUSY)
1246 && stuff_empty()
1247 && typebuf_typed()
1248 && emsg_silent == 0
1249 && !did_wait_return
1250 && oap->op_type == OP_NOP)
1252 int save_State = State;
1254 /* Draw the cursor with the right shape here */
1255 if (restart_edit != 0)
1256 State = INSERT;
1258 /* If need to redraw, and there is a "keep_msg", redraw before the
1259 * delay */
1260 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1262 char_u *kmsg;
1264 kmsg = keep_msg;
1265 keep_msg = NULL;
1266 /* showmode() will clear keep_msg, but we want to use it anyway */
1267 update_screen(0);
1268 /* now reset it, otherwise it's put in the history again */
1269 keep_msg = kmsg;
1270 msg_attr(kmsg, keep_msg_attr);
1271 vim_free(kmsg);
1273 setcursor();
1274 cursor_on();
1275 out_flush();
1276 if (msg_scroll || emsg_on_display)
1277 ui_delay(1000L, TRUE); /* wait at least one second */
1278 ui_delay(3000L, FALSE); /* wait up to three seconds */
1279 State = save_State;
1281 msg_scroll = FALSE;
1282 emsg_on_display = FALSE;
1286 * Finish up after executing a Normal mode command.
1288 normal_end:
1290 msg_nowait = FALSE;
1292 /* Reset finish_op, in case it was set */
1293 #ifdef CURSOR_SHAPE
1294 c = finish_op;
1295 #endif
1296 finish_op = FALSE;
1297 #ifdef CURSOR_SHAPE
1298 /* Redraw the cursor with another shape, if we were in Operator-pending
1299 * mode or did a replace command. */
1300 if (c || ca.cmdchar == 'r')
1302 ui_cursor_shape(); /* may show different cursor shape */
1303 # ifdef FEAT_MOUSESHAPE
1304 update_mouseshape(-1);
1305 # endif
1307 #endif
1309 #ifdef FEAT_CMDL_INFO
1310 if (oap->op_type == OP_NOP && oap->regname == 0
1311 # ifdef FEAT_AUTOCMD
1312 && ca.cmdchar != K_CURSORHOLD
1313 # endif
1315 clear_showcmd();
1316 #endif
1318 checkpcmark(); /* check if we moved since setting pcmark */
1319 vim_free(ca.searchbuf);
1321 #ifdef FEAT_MBYTE
1322 if (has_mbyte)
1323 mb_adjust_cursor();
1324 #endif
1326 #ifdef FEAT_SCROLLBIND
1327 if (curwin->w_p_scb && toplevel)
1329 validate_cursor(); /* may need to update w_leftcol */
1330 do_check_scrollbind(TRUE);
1332 #endif
1335 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1336 * if still inside a mapping that started in Visual mode).
1337 * May switch from Visual to Select mode after CTRL-O command.
1339 if ( oap->op_type == OP_NOP
1340 #ifdef FEAT_VISUAL
1341 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1342 || restart_VIsual_select == 1)
1343 #else
1344 && restart_edit != 0
1345 #endif
1346 && !(ca.retval & CA_COMMAND_BUSY)
1347 && stuff_empty()
1348 && oap->regname == 0)
1350 #ifdef FEAT_VISUAL
1351 if (restart_VIsual_select == 1)
1353 VIsual_select = TRUE;
1354 showmode();
1355 restart_VIsual_select = 0;
1357 #endif
1358 if (restart_edit != 0
1359 #ifdef FEAT_VISUAL
1360 && !VIsual_active && old_mapped_len == 0
1361 #endif
1363 (void)edit(restart_edit, FALSE, 1L);
1366 #ifdef FEAT_VISUAL
1367 if (restart_VIsual_select == 2)
1368 restart_VIsual_select = 1;
1369 #endif
1371 /* Save count before an operator for next time. */
1372 opcount = ca.opcount;
1376 * Handle an operator after visual mode or when the movement is finished
1378 void
1379 do_pending_operator(cap, old_col, gui_yank)
1380 cmdarg_T *cap;
1381 int old_col;
1382 int gui_yank;
1384 oparg_T *oap = cap->oap;
1385 pos_T old_cursor;
1386 int empty_region_error;
1387 int restart_edit_save;
1389 #ifdef FEAT_VISUAL
1390 /* The visual area is remembered for redo */
1391 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1392 static linenr_T redo_VIsual_line_count; /* number of lines */
1393 static colnr_T redo_VIsual_col; /* number of cols or end column */
1394 static long redo_VIsual_count; /* count for Visual operator */
1395 # ifdef FEAT_VIRTUALEDIT
1396 int include_line_break = FALSE;
1397 # endif
1398 #endif
1400 #if defined(FEAT_CLIPBOARD)
1402 * Yank the visual area into the GUI selection register before we operate
1403 * on it and lose it forever.
1404 * Don't do it if a specific register was specified, so that ""x"*P works.
1405 * This could call do_pending_operator() recursively, but that's OK
1406 * because gui_yank will be TRUE for the nested call.
1408 if (clip_star.available
1409 && oap->op_type != OP_NOP
1410 && !gui_yank
1411 # ifdef FEAT_VISUAL
1412 && VIsual_active
1413 && !redo_VIsual_busy
1414 # endif
1415 && oap->regname == 0)
1416 clip_auto_select();
1417 #endif
1418 old_cursor = curwin->w_cursor;
1421 * If an operation is pending, handle it...
1423 if ((finish_op
1424 #ifdef FEAT_VISUAL
1425 || VIsual_active
1426 #endif
1427 ) && oap->op_type != OP_NOP)
1429 #ifdef FEAT_VISUAL
1430 oap->is_VIsual = VIsual_active;
1431 if (oap->motion_force == 'V')
1432 oap->motion_type = MLINE;
1433 else if (oap->motion_force == 'v')
1435 /* If the motion was linewise, "inclusive" will not have been set.
1436 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1437 if (oap->motion_type == MLINE)
1438 oap->inclusive = FALSE;
1439 /* If the motion already was characterwise, toggle "inclusive" */
1440 else if (oap->motion_type == MCHAR)
1441 oap->inclusive = !oap->inclusive;
1442 oap->motion_type = MCHAR;
1444 else if (oap->motion_force == Ctrl_V)
1446 /* Change line- or characterwise motion into Visual block mode. */
1447 VIsual_active = TRUE;
1448 VIsual = oap->start;
1449 VIsual_mode = Ctrl_V;
1450 VIsual_select = FALSE;
1451 VIsual_reselect = FALSE;
1453 #endif
1455 /* only redo yank when 'y' flag is in 'cpoptions' */
1456 /* never redo "zf" (define fold) */
1457 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1458 #ifdef FEAT_VISUAL
1459 && (!VIsual_active || oap->motion_force)
1460 #endif
1461 && cap->cmdchar != 'D'
1462 #ifdef FEAT_FOLDING
1463 && oap->op_type != OP_FOLD
1464 && oap->op_type != OP_FOLDOPEN
1465 && oap->op_type != OP_FOLDOPENREC
1466 && oap->op_type != OP_FOLDCLOSE
1467 && oap->op_type != OP_FOLDCLOSEREC
1468 && oap->op_type != OP_FOLDDEL
1469 && oap->op_type != OP_FOLDDELREC
1470 #endif
1473 prep_redo(oap->regname, cap->count0,
1474 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1475 oap->motion_force, cap->cmdchar, cap->nchar);
1476 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1479 * If 'cpoptions' does not contain 'r', insert the search
1480 * pattern to really repeat the same command.
1482 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
1483 AppendToRedobuffLit(cap->searchbuf, -1);
1484 AppendToRedobuff(NL_STR);
1486 else if (cap->cmdchar == ':')
1488 /* do_cmdline() has stored the first typed line in
1489 * "repeat_cmdline". When several lines are typed repeating
1490 * won't be possible. */
1491 if (repeat_cmdline == NULL)
1492 ResetRedobuff();
1493 else
1495 AppendToRedobuffLit(repeat_cmdline, -1);
1496 AppendToRedobuff(NL_STR);
1497 vim_free(repeat_cmdline);
1498 repeat_cmdline = NULL;
1503 #ifdef FEAT_VISUAL
1504 if (redo_VIsual_busy)
1506 oap->start = curwin->w_cursor;
1507 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1508 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1509 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1510 VIsual_mode = redo_VIsual_mode;
1511 if (VIsual_mode == 'v')
1513 if (redo_VIsual_line_count <= 1)
1514 curwin->w_cursor.col += redo_VIsual_col - 1;
1515 else
1516 curwin->w_cursor.col = redo_VIsual_col;
1518 if (redo_VIsual_col == MAXCOL)
1520 curwin->w_curswant = MAXCOL;
1521 coladvance((colnr_T)MAXCOL);
1523 cap->count0 = redo_VIsual_count;
1524 if (redo_VIsual_count != 0)
1525 cap->count1 = redo_VIsual_count;
1526 else
1527 cap->count1 = 1;
1529 else if (VIsual_active)
1531 if (!gui_yank)
1533 /* Save the current VIsual area for '< and '> marks, and "gv" */
1534 curbuf->b_visual.vi_start = VIsual;
1535 curbuf->b_visual.vi_end = curwin->w_cursor;
1536 curbuf->b_visual.vi_mode = VIsual_mode;
1537 curbuf->b_visual.vi_curswant = curwin->w_curswant;
1538 # ifdef FEAT_EVAL
1539 curbuf->b_visual_mode_eval = VIsual_mode;
1540 # endif
1543 /* In Select mode, a linewise selection is operated upon like a
1544 * characterwise selection. */
1545 if (VIsual_select && VIsual_mode == 'V')
1547 if (lt(VIsual, curwin->w_cursor))
1549 VIsual.col = 0;
1550 curwin->w_cursor.col =
1551 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1553 else
1555 curwin->w_cursor.col = 0;
1556 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1558 VIsual_mode = 'v';
1560 /* If 'selection' is "exclusive", backup one character for
1561 * charwise selections. */
1562 else if (VIsual_mode == 'v')
1564 # ifdef FEAT_VIRTUALEDIT
1565 include_line_break =
1566 # endif
1567 unadjust_for_sel();
1570 oap->start = VIsual;
1571 if (VIsual_mode == 'V')
1572 oap->start.col = 0;
1574 #endif /* FEAT_VISUAL */
1577 * Set oap->start to the first position of the operated text, oap->end
1578 * to the end of the operated text. w_cursor is equal to oap->start.
1580 if (lt(oap->start, curwin->w_cursor))
1582 #ifdef FEAT_FOLDING
1583 /* Include folded lines completely. */
1584 if (!VIsual_active)
1586 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1587 oap->start.col = 0;
1588 if (hasFolding(curwin->w_cursor.lnum, NULL,
1589 &curwin->w_cursor.lnum))
1590 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1592 #endif
1593 oap->end = curwin->w_cursor;
1594 curwin->w_cursor = oap->start;
1596 /* w_virtcol may have been updated; if the cursor goes back to its
1597 * previous position w_virtcol becomes invalid and isn't updated
1598 * automatically. */
1599 curwin->w_valid &= ~VALID_VIRTCOL;
1601 else
1603 #ifdef FEAT_FOLDING
1604 /* Include folded lines completely. */
1605 if (!VIsual_active && oap->motion_type == MLINE)
1607 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1608 NULL))
1609 curwin->w_cursor.col = 0;
1610 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1611 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1613 #endif
1614 oap->end = oap->start;
1615 oap->start = curwin->w_cursor;
1618 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1620 #ifdef FEAT_VIRTUALEDIT
1621 /* Set "virtual_op" before resetting VIsual_active. */
1622 virtual_op = virtual_active();
1623 #endif
1625 #ifdef FEAT_VISUAL
1626 if (VIsual_active || redo_VIsual_busy)
1628 if (VIsual_mode == Ctrl_V) /* block mode */
1630 colnr_T start, end;
1632 oap->block_mode = TRUE;
1634 getvvcol(curwin, &(oap->start),
1635 &oap->start_vcol, NULL, &oap->end_vcol);
1636 if (!redo_VIsual_busy)
1638 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1640 if (start < oap->start_vcol)
1641 oap->start_vcol = start;
1642 if (end > oap->end_vcol)
1644 if (*p_sel == 'e' && start >= 1
1645 && start - 1 >= oap->end_vcol)
1646 oap->end_vcol = start - 1;
1647 else
1648 oap->end_vcol = end;
1652 /* if '$' was used, get oap->end_vcol from longest line */
1653 if (curwin->w_curswant == MAXCOL)
1655 curwin->w_cursor.col = MAXCOL;
1656 oap->end_vcol = 0;
1657 for (curwin->w_cursor.lnum = oap->start.lnum;
1658 curwin->w_cursor.lnum <= oap->end.lnum;
1659 ++curwin->w_cursor.lnum)
1661 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1662 if (end > oap->end_vcol)
1663 oap->end_vcol = end;
1666 else if (redo_VIsual_busy)
1667 oap->end_vcol = oap->start_vcol + redo_VIsual_col - 1;
1669 * Correct oap->end.col and oap->start.col to be the
1670 * upper-left and lower-right corner of the block area.
1672 * (Actually, this does convert column positions into character
1673 * positions)
1675 curwin->w_cursor.lnum = oap->end.lnum;
1676 coladvance(oap->end_vcol);
1677 oap->end = curwin->w_cursor;
1679 curwin->w_cursor = oap->start;
1680 coladvance(oap->start_vcol);
1681 oap->start = curwin->w_cursor;
1684 if (!redo_VIsual_busy && !gui_yank)
1687 * Prepare to reselect and redo Visual: this is based on the
1688 * size of the Visual text
1690 resel_VIsual_mode = VIsual_mode;
1691 if (curwin->w_curswant == MAXCOL)
1692 resel_VIsual_col = MAXCOL;
1693 else if (VIsual_mode == Ctrl_V)
1694 resel_VIsual_col = oap->end_vcol - oap->start_vcol + 1;
1695 else if (oap->line_count > 1)
1696 resel_VIsual_col = oap->end.col;
1697 else
1698 resel_VIsual_col = oap->end.col - oap->start.col + 1;
1699 resel_VIsual_line_count = oap->line_count;
1702 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1703 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1704 && oap->op_type != OP_COLON
1705 #ifdef FEAT_FOLDING
1706 && oap->op_type != OP_FOLD
1707 && oap->op_type != OP_FOLDOPEN
1708 && oap->op_type != OP_FOLDOPENREC
1709 && oap->op_type != OP_FOLDCLOSE
1710 && oap->op_type != OP_FOLDCLOSEREC
1711 && oap->op_type != OP_FOLDDEL
1712 && oap->op_type != OP_FOLDDELREC
1713 #endif
1714 && oap->motion_force == NUL
1717 /* Prepare for redoing. Only use the nchar field for "r",
1718 * otherwise it might be the second char of the operator. */
1719 prep_redo(oap->regname, 0L, NUL, 'v',
1720 get_op_char(oap->op_type),
1721 get_extra_op_char(oap->op_type),
1722 oap->op_type == OP_REPLACE ? cap->nchar : NUL);
1723 if (!redo_VIsual_busy)
1725 redo_VIsual_mode = resel_VIsual_mode;
1726 redo_VIsual_col = resel_VIsual_col;
1727 redo_VIsual_line_count = resel_VIsual_line_count;
1728 redo_VIsual_count = cap->count0;
1733 * oap->inclusive defaults to TRUE.
1734 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1735 * FALSE. This makes "d}P" and "v}dP" work the same.
1737 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1738 oap->inclusive = TRUE;
1739 if (VIsual_mode == 'V')
1740 oap->motion_type = MLINE;
1741 else
1743 oap->motion_type = MCHAR;
1744 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1745 # ifdef FEAT_VIRTUALEDIT
1746 && (include_line_break || !virtual_op)
1747 # endif
1750 oap->inclusive = FALSE;
1751 /* Try to include the newline, unless it's an operator
1752 * that works on lines only */
1753 if (*p_sel != 'o'
1754 && !op_on_lines(oap->op_type)
1755 && oap->end.lnum < curbuf->b_ml.ml_line_count)
1757 ++oap->end.lnum;
1758 oap->end.col = 0;
1759 # ifdef FEAT_VIRTUALEDIT
1760 oap->end.coladd = 0;
1761 # endif
1762 ++oap->line_count;
1767 redo_VIsual_busy = FALSE;
1770 * Switch Visual off now, so screen updating does
1771 * not show inverted text when the screen is redrawn.
1772 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1773 * no screen redraw, so it is done here to remove the inverted
1774 * part.
1776 if (!gui_yank)
1778 VIsual_active = FALSE;
1779 # ifdef FEAT_MOUSE
1780 setmouse();
1781 mouse_dragging = 0;
1782 # endif
1783 if (mode_displayed)
1784 clear_cmdline = TRUE; /* unshow visual mode later */
1785 #ifdef FEAT_CMDL_INFO
1786 else
1787 clear_showcmd();
1788 #endif
1789 if ((oap->op_type == OP_YANK
1790 || oap->op_type == OP_COLON
1791 || oap->op_type == OP_FUNCTION
1792 || oap->op_type == OP_FILTER)
1793 && oap->motion_force == NUL)
1794 redraw_curbuf_later(INVERTED);
1797 #endif
1799 #ifdef FEAT_MBYTE
1800 /* Include the trailing byte of a multi-byte char. */
1801 if (has_mbyte && oap->inclusive)
1803 int l;
1805 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
1806 if (l > 1)
1807 oap->end.col += l - 1;
1809 #endif
1810 curwin->w_set_curswant = TRUE;
1813 * oap->empty is set when start and end are the same. The inclusive
1814 * flag affects this too, unless yanking and the end is on a NUL.
1816 oap->empty = (oap->motion_type == MCHAR
1817 && (!oap->inclusive
1818 || (oap->op_type == OP_YANK
1819 && gchar_pos(&oap->end) == NUL))
1820 && equalpos(oap->start, oap->end)
1821 #ifdef FEAT_VIRTUALEDIT
1822 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1823 #endif
1826 * For delete, change and yank, it's an error to operate on an
1827 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1829 empty_region_error = (oap->empty
1830 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1832 #ifdef FEAT_VISUAL
1833 /* Force a redraw when operating on an empty Visual region, when
1834 * 'modifiable is off or creating a fold. */
1835 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1836 # ifdef FEAT_FOLDING
1837 || oap->op_type == OP_FOLD
1838 # endif
1840 redraw_curbuf_later(INVERTED);
1841 #endif
1844 * If the end of an operator is in column one while oap->motion_type
1845 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1846 * character in the previous line. If op_start is on or before the
1847 * first non-blank in the line, the operator becomes linewise
1848 * (strange, but that's the way vi does it).
1850 if ( oap->motion_type == MCHAR
1851 && oap->inclusive == FALSE
1852 && !(cap->retval & CA_NO_ADJ_OP_END)
1853 && oap->end.col == 0
1854 #ifdef FEAT_VISUAL
1855 && (!oap->is_VIsual || *p_sel == 'o')
1856 && !oap->block_mode
1857 #endif
1858 && oap->line_count > 1)
1860 oap->end_adjusted = TRUE; /* remember that we did this */
1861 --oap->line_count;
1862 --oap->end.lnum;
1863 if (inindent(0))
1864 oap->motion_type = MLINE;
1865 else
1867 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1868 if (oap->end.col)
1870 --oap->end.col;
1871 oap->inclusive = TRUE;
1875 else
1876 oap->end_adjusted = FALSE;
1878 switch (oap->op_type)
1880 case OP_LSHIFT:
1881 case OP_RSHIFT:
1882 op_shift(oap, TRUE,
1883 #ifdef FEAT_VISUAL
1884 oap->is_VIsual ? (int)cap->count1 :
1885 #endif
1887 auto_format(FALSE, TRUE);
1888 break;
1890 case OP_JOIN_NS:
1891 case OP_JOIN:
1892 if (oap->line_count < 2)
1893 oap->line_count = 2;
1894 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1895 curbuf->b_ml.ml_line_count)
1896 beep_flush();
1897 else
1899 do_do_join(oap->line_count, oap->op_type == OP_JOIN);
1900 auto_format(FALSE, TRUE);
1902 break;
1904 case OP_DELETE:
1905 #ifdef FEAT_VISUAL
1906 VIsual_reselect = FALSE; /* don't reselect now */
1907 #endif
1908 if (empty_region_error)
1909 vim_beep();
1910 else
1912 (void)op_delete(oap);
1913 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1914 u_save_cursor(); /* cursor line wasn't saved yet */
1915 auto_format(FALSE, TRUE);
1917 break;
1919 case OP_YANK:
1920 if (empty_region_error)
1922 if (!gui_yank)
1923 vim_beep();
1925 else
1926 (void)op_yank(oap, FALSE, !gui_yank);
1927 check_cursor_col();
1928 break;
1930 case OP_CHANGE:
1931 #ifdef FEAT_VISUAL
1932 VIsual_reselect = FALSE; /* don't reselect now */
1933 #endif
1934 if (empty_region_error)
1935 vim_beep();
1936 else
1938 /* This is a new edit command, not a restart. Need to
1939 * remember it to make 'insertmode' work with mappings for
1940 * Visual mode. But do this only once and not when typed and
1941 * 'insertmode' isn't set. */
1942 if (p_im || !KeyTyped)
1943 restart_edit_save = restart_edit;
1944 else
1945 restart_edit_save = 0;
1946 restart_edit = 0;
1947 /* Reset finish_op now, don't want it set inside edit(). */
1948 finish_op = FALSE;
1949 if (op_change(oap)) /* will call edit() */
1950 cap->retval |= CA_COMMAND_BUSY;
1951 if (restart_edit == 0)
1952 restart_edit = restart_edit_save;
1954 break;
1956 case OP_FILTER:
1957 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1958 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1959 else
1960 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
1962 case OP_INDENT:
1963 case OP_COLON:
1965 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1967 * If 'equalprg' is empty, do the indenting internally.
1969 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1971 # ifdef FEAT_LISP
1972 if (curbuf->b_p_lisp)
1974 op_reindent(oap, get_lisp_indent);
1975 break;
1977 # endif
1978 # ifdef FEAT_CINDENT
1979 op_reindent(oap,
1980 # ifdef FEAT_EVAL
1981 *curbuf->b_p_inde != NUL ? get_expr_indent :
1982 # endif
1983 get_c_indent);
1984 break;
1985 # endif
1987 #endif
1989 op_colon(oap);
1990 break;
1992 case OP_TILDE:
1993 case OP_UPPER:
1994 case OP_LOWER:
1995 case OP_ROT13:
1996 if (empty_region_error)
1997 vim_beep();
1998 else
1999 op_tilde(oap);
2000 check_cursor_col();
2001 break;
2003 case OP_FORMAT:
2004 #if defined(FEAT_EVAL)
2005 if (*curbuf->b_p_fex != NUL)
2006 op_formatexpr(oap); /* use expression */
2007 else
2008 #endif
2009 if (*p_fp != NUL)
2010 op_colon(oap); /* use external command */
2011 else
2012 op_format(oap, FALSE); /* use internal function */
2013 break;
2015 case OP_FORMAT2:
2016 op_format(oap, TRUE); /* use internal function */
2017 break;
2019 case OP_FUNCTION:
2020 op_function(oap); /* call 'operatorfunc' */
2021 break;
2023 case OP_INSERT:
2024 case OP_APPEND:
2025 #ifdef FEAT_VISUAL
2026 VIsual_reselect = FALSE; /* don't reselect now */
2027 #endif
2028 #ifdef FEAT_VISUALEXTRA
2029 if (empty_region_error)
2030 vim_beep();
2031 else
2033 /* This is a new edit command, not a restart. Need to
2034 * remember it to make 'insertmode' work with mappings for
2035 * Visual mode. But do this only once. */
2036 restart_edit_save = restart_edit;
2037 restart_edit = 0;
2039 op_insert(oap, cap->count1);
2041 /* TODO: when inserting in several lines, should format all
2042 * the lines. */
2043 auto_format(FALSE, TRUE);
2045 if (restart_edit == 0)
2046 restart_edit = restart_edit_save;
2048 #else
2049 vim_beep();
2050 #endif
2051 break;
2053 case OP_REPLACE:
2054 #ifdef FEAT_VISUAL
2055 VIsual_reselect = FALSE; /* don't reselect now */
2056 #endif
2057 #ifdef FEAT_VISUALEXTRA
2058 if (empty_region_error)
2059 #endif
2060 vim_beep();
2061 #ifdef FEAT_VISUALEXTRA
2062 else
2063 op_replace(oap, cap->nchar);
2064 #endif
2065 break;
2067 #ifdef FEAT_FOLDING
2068 case OP_FOLD:
2069 VIsual_reselect = FALSE; /* don't reselect now */
2070 foldCreate(oap->start.lnum, oap->end.lnum);
2071 break;
2073 case OP_FOLDOPEN:
2074 case OP_FOLDOPENREC:
2075 case OP_FOLDCLOSE:
2076 case OP_FOLDCLOSEREC:
2077 VIsual_reselect = FALSE; /* don't reselect now */
2078 opFoldRange(oap->start.lnum, oap->end.lnum,
2079 oap->op_type == OP_FOLDOPEN
2080 || oap->op_type == OP_FOLDOPENREC,
2081 oap->op_type == OP_FOLDOPENREC
2082 || oap->op_type == OP_FOLDCLOSEREC,
2083 oap->is_VIsual);
2084 break;
2086 case OP_FOLDDEL:
2087 case OP_FOLDDELREC:
2088 VIsual_reselect = FALSE; /* don't reselect now */
2089 deleteFold(oap->start.lnum, oap->end.lnum,
2090 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2091 break;
2092 #endif
2093 default:
2094 clearopbeep(oap);
2096 #ifdef FEAT_VIRTUALEDIT
2097 virtual_op = MAYBE;
2098 #endif
2099 if (!gui_yank)
2102 * if 'sol' not set, go back to old column for some commands
2104 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2105 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2106 || oap->op_type == OP_DELETE))
2107 coladvance(curwin->w_curswant = old_col);
2109 else
2111 curwin->w_cursor = old_cursor;
2113 #ifdef FEAT_VISUAL
2114 oap->block_mode = FALSE;
2115 #endif
2116 clearop(oap);
2121 * Handle indent and format operators and visual mode ":".
2123 static void
2124 op_colon(oap)
2125 oparg_T *oap;
2127 stuffcharReadbuff(':');
2128 #ifdef FEAT_VISUAL
2129 if (oap->is_VIsual)
2130 stuffReadbuff((char_u *)"'<,'>");
2131 else
2132 #endif
2135 * Make the range look nice, so it can be repeated.
2137 if (oap->start.lnum == curwin->w_cursor.lnum)
2138 stuffcharReadbuff('.');
2139 else
2140 stuffnumReadbuff((long)oap->start.lnum);
2141 if (oap->end.lnum != oap->start.lnum)
2143 stuffcharReadbuff(',');
2144 if (oap->end.lnum == curwin->w_cursor.lnum)
2145 stuffcharReadbuff('.');
2146 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2147 stuffcharReadbuff('$');
2148 else if (oap->start.lnum == curwin->w_cursor.lnum)
2150 stuffReadbuff((char_u *)".+");
2151 stuffnumReadbuff((long)oap->line_count - 1);
2153 else
2154 stuffnumReadbuff((long)oap->end.lnum);
2157 if (oap->op_type != OP_COLON)
2158 stuffReadbuff((char_u *)"!");
2159 if (oap->op_type == OP_INDENT)
2161 #ifndef FEAT_CINDENT
2162 if (*get_equalprg() == NUL)
2163 stuffReadbuff((char_u *)"indent");
2164 else
2165 #endif
2166 stuffReadbuff(get_equalprg());
2167 stuffReadbuff((char_u *)"\n");
2169 else if (oap->op_type == OP_FORMAT)
2171 if (*p_fp == NUL)
2172 stuffReadbuff((char_u *)"fmt");
2173 else
2174 stuffReadbuff(p_fp);
2175 stuffReadbuff((char_u *)"\n']");
2179 * do_cmdline() does the rest
2184 * Handle the "g@" operator: call 'operatorfunc'.
2186 /*ARGSUSED*/
2187 static void
2188 op_function(oap)
2189 oparg_T *oap;
2191 #ifdef FEAT_EVAL
2192 char_u *(argv[1]);
2194 if (*p_opfunc == NUL)
2195 EMSG(_("E774: 'operatorfunc' is empty"));
2196 else
2198 /* Set '[ and '] marks to text to be operated on. */
2199 curbuf->b_op_start = oap->start;
2200 curbuf->b_op_end = oap->end;
2201 if (oap->motion_type != MLINE && !oap->inclusive)
2202 /* Exclude the end position. */
2203 decl(&curbuf->b_op_end);
2205 if (oap->block_mode)
2206 argv[0] = (char_u *)"block";
2207 else if (oap->motion_type == MLINE)
2208 argv[0] = (char_u *)"line";
2209 else
2210 argv[0] = (char_u *)"char";
2211 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
2213 #else
2214 EMSG(_("E775: Eval feature not available"));
2215 #endif
2218 #if defined(FEAT_MOUSE) || defined(PROTO)
2220 * Do the appropriate action for the current mouse click in the current mode.
2221 * Not used for Command-line mode.
2223 * Normal Mode:
2224 * event modi- position visual change action
2225 * fier cursor window
2226 * left press - yes end yes
2227 * left press C yes end yes "^]" (2)
2228 * left press S yes end yes "*" (2)
2229 * left drag - yes start if moved no
2230 * left relse - yes start if moved no
2231 * middle press - yes if not active no put register
2232 * middle press - yes if active no yank and put
2233 * right press - yes start or extend yes
2234 * right press S yes no change yes "#" (2)
2235 * right drag - yes extend no
2236 * right relse - yes extend no
2238 * Insert or Replace Mode:
2239 * event modi- position visual change action
2240 * fier cursor window
2241 * left press - yes (cannot be active) yes
2242 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2243 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2244 * left drag - yes start or extend (1) no CTRL-O (1)
2245 * left relse - yes start or extend (1) no CTRL-O (1)
2246 * middle press - no (cannot be active) no put register
2247 * right press - yes start or extend yes CTRL-O
2248 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2250 * (1) only if mouse pointer moved since press
2251 * (2) only if click is in same buffer
2253 * Return TRUE if start_arrow() should be called for edit mode.
2256 do_mouse(oap, c, dir, count, fixindent)
2257 oparg_T *oap; /* operator argument, can be NULL */
2258 int c; /* K_LEFTMOUSE, etc */
2259 int dir; /* Direction to 'put' if necessary */
2260 long count;
2261 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2263 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2264 static int got_click = FALSE; /* got a click some time back */
2266 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2267 int is_click; /* If FALSE it's a drag or release event */
2268 int is_drag; /* If TRUE it's a drag event */
2269 int jump_flags = 0; /* flags for jump_to_mouse() */
2270 pos_T start_visual;
2271 int moved; /* Has cursor moved? */
2272 int in_status_line; /* mouse in status line */
2273 #ifdef FEAT_VERTSPLIT
2274 int in_sep_line; /* mouse in vertical separator line */
2275 #endif
2276 int c1, c2;
2277 #if defined(FEAT_FOLDING)
2278 pos_T save_cursor;
2279 #endif
2280 win_T *old_curwin = curwin;
2281 #ifdef FEAT_VISUAL
2282 static pos_T orig_cursor;
2283 colnr_T leftcol, rightcol;
2284 pos_T end_visual;
2285 int diff;
2286 int old_active = VIsual_active;
2287 int old_mode = VIsual_mode;
2288 #endif
2289 int regname;
2291 #if defined(FEAT_FOLDING)
2292 save_cursor = curwin->w_cursor;
2293 #endif
2296 * When GUI is active, always recognize mouse events, otherwise:
2297 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2298 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2299 * - For command line and insert mode 'mouse' is checked before calling
2300 * do_mouse().
2302 if (do_always)
2303 do_always = FALSE;
2304 else
2305 #ifdef FEAT_GUI
2306 if (!gui.in_use)
2307 #endif
2309 #ifdef FEAT_VISUAL
2310 if (VIsual_active)
2312 if (!mouse_has(MOUSE_VISUAL))
2313 return FALSE;
2315 else
2316 #endif
2317 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2318 return FALSE;
2321 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2323 #ifdef FEAT_MOUSESHAPE
2324 /* May have stopped dragging the status or separator line. The pointer is
2325 * most likely still on the status or separator line. */
2326 if (!is_drag && drag_status_line)
2328 drag_status_line = FALSE;
2329 update_mouseshape(SHAPE_IDX_STATUS);
2331 # ifdef FEAT_VERTSPLIT
2332 if (!is_drag && drag_sep_line)
2334 drag_sep_line = FALSE;
2335 update_mouseshape(SHAPE_IDX_VSEP);
2337 # endif
2338 #endif
2341 * Ignore drag and release events if we didn't get a click.
2343 if (is_click)
2344 got_click = TRUE;
2345 else
2347 if (!got_click) /* didn't get click, ignore */
2348 return FALSE;
2349 if (!is_drag) /* release, reset got_click */
2350 got_click = FALSE;
2353 #ifndef FEAT_VISUAL
2355 * ALT is only used for starging/extending Visual mode.
2357 if ((mod_mask & MOD_MASK_ALT))
2358 return FALSE;
2359 #endif
2362 * CTRL right mouse button does CTRL-T
2364 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2366 if (State & INSERT)
2367 stuffcharReadbuff(Ctrl_O);
2368 if (count > 1)
2369 stuffnumReadbuff(count);
2370 stuffcharReadbuff(Ctrl_T);
2371 got_click = FALSE; /* ignore drag&release now */
2372 return FALSE;
2376 * CTRL only works with left mouse button
2378 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2379 return FALSE;
2382 * When a modifier is down, ignore drag and release events, as well as
2383 * multiple clicks and the middle mouse button.
2384 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2386 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2387 | MOD_MASK_META))
2388 && (!is_click
2389 || (mod_mask & MOD_MASK_MULTI_CLICK)
2390 || which_button == MOUSE_MIDDLE)
2391 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
2392 && mouse_model_popup()
2393 && which_button == MOUSE_LEFT)
2394 && !((mod_mask & MOD_MASK_ALT)
2395 && !mouse_model_popup()
2396 && which_button == MOUSE_RIGHT)
2398 return FALSE;
2401 * If the button press was used as the movement command for an operator
2402 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2403 * drag/release events.
2405 if (!is_click && which_button == MOUSE_MIDDLE)
2406 return FALSE;
2408 if (oap != NULL)
2409 regname = oap->regname;
2410 else
2411 regname = 0;
2414 * Middle mouse button does a 'put' of the selected text
2416 if (which_button == MOUSE_MIDDLE)
2418 if (State == NORMAL)
2421 * If an operator was pending, we don't know what the user wanted
2422 * to do. Go back to normal mode: Clear the operator and beep().
2424 if (oap != NULL && oap->op_type != OP_NOP)
2426 clearopbeep(oap);
2427 return FALSE;
2430 #ifdef FEAT_VISUAL
2432 * If visual was active, yank the highlighted text and put it
2433 * before the mouse pointer position.
2434 * In Select mode replace the highlighted text with the clipboard.
2436 if (VIsual_active)
2438 if (VIsual_select)
2440 stuffcharReadbuff(Ctrl_G);
2441 stuffReadbuff((char_u *)"\"+p");
2443 else
2445 stuffcharReadbuff('y');
2446 stuffcharReadbuff(K_MIDDLEMOUSE);
2448 do_always = TRUE; /* ignore 'mouse' setting next time */
2449 return FALSE;
2451 #endif
2453 * The rest is below jump_to_mouse()
2457 else if ((State & INSERT) == 0)
2458 return FALSE;
2461 * Middle click in insert mode doesn't move the mouse, just insert the
2462 * contents of a register. '.' register is special, can't insert that
2463 * with do_put().
2464 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2465 * happens for the GUI).
2467 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2469 if (regname == '.')
2470 insert_reg(regname, TRUE);
2471 else
2473 #ifdef FEAT_CLIPBOARD
2474 if (clip_star.available && regname == 0)
2475 regname = '*';
2476 #endif
2477 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2478 insert_reg(regname, TRUE);
2479 else
2481 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2483 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2484 AppendCharToRedobuff(Ctrl_R);
2485 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2486 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2489 return FALSE;
2493 /* When dragging or button-up stay in the same window. */
2494 if (!is_click)
2495 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2497 start_visual.lnum = 0;
2499 #ifdef FEAT_WINDOWS
2500 /* Check for clicking in the tab page line. */
2501 if (mouse_row == 0 && firstwin->w_winrow > 0)
2503 if (is_drag)
2504 return FALSE;
2505 got_click = FALSE; /* ignore mouse-up and drag events */
2507 /* click in a tab selects that tab page */
2508 if (is_click
2509 # ifdef FEAT_CMDWIN
2510 && cmdwin_type == 0
2511 # endif
2512 && mouse_col < Columns)
2514 c1 = TabPageIdxs[mouse_col];
2515 if (c1 >= 0)
2517 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2519 /* double click opens new page */
2520 end_visual_mode();
2521 tabpage_new();
2522 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2524 else
2526 /* Go to specified tab page, or next one if not clicking
2527 * on a label. */
2528 goto_tabpage(c1);
2530 /* It's like clicking on the status line of a window. */
2531 if (curwin != old_curwin)
2532 end_visual_mode();
2535 else if (c1 < 0)
2537 tabpage_T *tp;
2539 /* Close the current or specified tab page. */
2540 if (c1 == -999)
2541 tp = curtab;
2542 else
2543 tp = find_tabpage(-c1);
2544 if (tp == curtab)
2546 if (first_tabpage->tp_next != NULL)
2547 tabpage_close(FALSE);
2549 else if (tp != NULL)
2550 tabpage_close_other(tp, FALSE);
2553 return TRUE;
2555 #endif
2558 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2559 * right button up -> pop-up menu
2560 * shift-left button -> right button
2561 * alt-left button -> alt-right button
2563 if (mouse_model_popup())
2565 if (which_button == MOUSE_RIGHT
2566 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2569 * NOTE: Ignore right button down and drag mouse events.
2570 * Windows only shows the popup menu on the button up event.
2572 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2573 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) \
2574 || defined(FEAT_GUI_MACVIM)
2575 if (!is_click)
2576 return FALSE;
2577 #endif
2578 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2579 if (is_click || is_drag)
2580 return FALSE;
2581 #endif
2582 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2583 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2584 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON) \
2585 || defined(FEAT_GUI_MACVIM)
2586 if (gui.in_use)
2588 jump_flags = 0;
2589 if (STRCMP(p_mousem, "popup_setpos") == 0)
2591 /* First set the cursor position before showing the popup
2592 * menu. */
2593 #ifdef FEAT_VISUAL
2594 if (VIsual_active)
2596 pos_T m_pos;
2599 * set MOUSE_MAY_STOP_VIS if we are outside the
2600 * selection or the current window (might have false
2601 * negative here)
2603 if (mouse_row < W_WINROW(curwin)
2604 || mouse_row
2605 > (W_WINROW(curwin) + curwin->w_height))
2606 jump_flags = MOUSE_MAY_STOP_VIS;
2607 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2608 jump_flags = MOUSE_MAY_STOP_VIS;
2609 else
2611 if ((lt(curwin->w_cursor, VIsual)
2612 && (lt(m_pos, curwin->w_cursor)
2613 || lt(VIsual, m_pos)))
2614 || (lt(VIsual, curwin->w_cursor)
2615 && (lt(m_pos, VIsual)
2616 || lt(curwin->w_cursor, m_pos))))
2618 jump_flags = MOUSE_MAY_STOP_VIS;
2620 else if (VIsual_mode == Ctrl_V)
2622 getvcols(curwin, &curwin->w_cursor, &VIsual,
2623 &leftcol, &rightcol);
2624 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2625 if (m_pos.col < leftcol || m_pos.col > rightcol)
2626 jump_flags = MOUSE_MAY_STOP_VIS;
2630 else
2631 jump_flags = MOUSE_MAY_STOP_VIS;
2632 #endif
2634 if (jump_flags)
2636 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2637 update_curbuf(
2638 #ifdef FEAT_VISUAL
2639 VIsual_active ? INVERTED :
2640 #endif
2641 VALID);
2642 setcursor();
2643 out_flush(); /* Update before showing popup menu */
2645 # ifdef FEAT_MENU
2646 gui_show_popupmenu();
2647 # endif
2648 return (jump_flags & CURSOR_MOVED) != 0;
2650 else
2651 return FALSE;
2652 #else
2653 return FALSE;
2654 #endif
2656 if (which_button == MOUSE_LEFT
2657 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
2659 which_button = MOUSE_RIGHT;
2660 mod_mask &= ~MOD_MASK_SHIFT;
2664 #ifdef FEAT_VISUAL
2665 if ((State & (NORMAL | INSERT))
2666 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2668 if (which_button == MOUSE_LEFT)
2670 if (is_click)
2672 /* stop Visual mode for a left click in a window, but not when
2673 * on a status line */
2674 if (VIsual_active)
2675 jump_flags |= MOUSE_MAY_STOP_VIS;
2677 else if (mouse_has(MOUSE_VISUAL))
2678 jump_flags |= MOUSE_MAY_VIS;
2680 else if (which_button == MOUSE_RIGHT)
2682 if (is_click && VIsual_active)
2685 * Remember the start and end of visual before moving the
2686 * cursor.
2688 if (lt(curwin->w_cursor, VIsual))
2690 start_visual = curwin->w_cursor;
2691 end_visual = VIsual;
2693 else
2695 start_visual = VIsual;
2696 end_visual = curwin->w_cursor;
2699 jump_flags |= MOUSE_FOCUS;
2700 if (mouse_has(MOUSE_VISUAL))
2701 jump_flags |= MOUSE_MAY_VIS;
2704 #endif
2707 * If an operator is pending, ignore all drags and releases until the
2708 * next mouse click.
2710 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2712 got_click = FALSE;
2713 oap->motion_type = MCHAR;
2716 /* When releasing the button let jump_to_mouse() know. */
2717 if (!is_click && !is_drag)
2718 jump_flags |= MOUSE_RELEASED;
2721 * JUMP!
2723 jump_flags = jump_to_mouse(jump_flags,
2724 oap == NULL ? NULL : &(oap->inclusive), which_button);
2725 moved = (jump_flags & CURSOR_MOVED);
2726 in_status_line = (jump_flags & IN_STATUS_LINE);
2727 #ifdef FEAT_VERTSPLIT
2728 in_sep_line = (jump_flags & IN_SEP_LINE);
2729 #endif
2731 #ifdef FEAT_NETBEANS_INTG
2732 if (usingNetbeans && isNetbeansBuffer(curbuf)
2733 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2735 int key = KEY2TERMCAP1(c);
2737 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2738 || key == (int)KE_RIGHTRELEASE)
2739 netbeans_button_release(which_button);
2741 #endif
2743 /* When jumping to another window, clear a pending operator. That's a bit
2744 * friendlier than beeping and not jumping to that window. */
2745 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2746 clearop(oap);
2748 #ifdef FEAT_FOLDING
2749 if (mod_mask == 0
2750 && !is_drag
2751 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2752 && which_button == MOUSE_LEFT)
2754 /* open or close a fold at this line */
2755 if (jump_flags & MOUSE_FOLD_OPEN)
2756 openFold(curwin->w_cursor.lnum, 1L);
2757 else
2758 closeFold(curwin->w_cursor.lnum, 1L);
2759 /* don't move the cursor if still in the same window */
2760 if (curwin == old_curwin)
2761 curwin->w_cursor = save_cursor;
2763 #endif
2765 #if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2766 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2768 clip_modeless(which_button, is_click, is_drag);
2769 return FALSE;
2771 #endif
2773 #ifdef FEAT_VISUAL
2774 /* Set global flag that we are extending the Visual area with mouse
2775 * dragging; temporarily minimize 'scrolloff'. */
2776 if (VIsual_active && is_drag && p_so)
2778 /* In the very first line, allow scrolling one line */
2779 if (mouse_row == 0)
2780 mouse_dragging = 2;
2781 else
2782 mouse_dragging = 1;
2785 /* When dragging the mouse above the window, scroll down. */
2786 if (is_drag && mouse_row < 0 && !in_status_line)
2788 scroll_redraw(FALSE, 1L);
2789 mouse_row = 0;
2792 if (start_visual.lnum) /* right click in visual mode */
2794 /* When ALT is pressed make Visual mode blockwise. */
2795 if (mod_mask & MOD_MASK_ALT)
2796 VIsual_mode = Ctrl_V;
2799 * In Visual-block mode, divide the area in four, pick up the corner
2800 * that is in the quarter that the cursor is in.
2802 if (VIsual_mode == Ctrl_V)
2804 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2805 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2806 end_visual.col = leftcol;
2807 else
2808 end_visual.col = rightcol;
2809 if (curwin->w_cursor.lnum <
2810 (start_visual.lnum + end_visual.lnum) / 2)
2811 end_visual.lnum = end_visual.lnum;
2812 else
2813 end_visual.lnum = start_visual.lnum;
2815 /* move VIsual to the right column */
2816 start_visual = curwin->w_cursor; /* save the cursor pos */
2817 curwin->w_cursor = end_visual;
2818 coladvance(end_visual.col);
2819 VIsual = curwin->w_cursor;
2820 curwin->w_cursor = start_visual; /* restore the cursor */
2822 else
2825 * If the click is before the start of visual, change the start.
2826 * If the click is after the end of visual, change the end. If
2827 * the click is inside the visual, change the closest side.
2829 if (lt(curwin->w_cursor, start_visual))
2830 VIsual = end_visual;
2831 else if (lt(end_visual, curwin->w_cursor))
2832 VIsual = start_visual;
2833 else
2835 /* In the same line, compare column number */
2836 if (end_visual.lnum == start_visual.lnum)
2838 if (curwin->w_cursor.col - start_visual.col >
2839 end_visual.col - curwin->w_cursor.col)
2840 VIsual = start_visual;
2841 else
2842 VIsual = end_visual;
2845 /* In different lines, compare line number */
2846 else
2848 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2849 (end_visual.lnum - curwin->w_cursor.lnum);
2851 if (diff > 0) /* closest to end */
2852 VIsual = start_visual;
2853 else if (diff < 0) /* closest to start */
2854 VIsual = end_visual;
2855 else /* in the middle line */
2857 if (curwin->w_cursor.col <
2858 (start_visual.col + end_visual.col) / 2)
2859 VIsual = end_visual;
2860 else
2861 VIsual = start_visual;
2868 * If Visual mode started in insert mode, execute "CTRL-O"
2870 else if ((State & INSERT) && VIsual_active)
2871 stuffcharReadbuff(Ctrl_O);
2872 #endif
2875 * Middle mouse click: Put text before cursor.
2877 if (which_button == MOUSE_MIDDLE)
2879 #ifdef FEAT_CLIPBOARD
2880 if (clip_star.available && regname == 0)
2881 regname = '*';
2882 #endif
2883 if (yank_register_mline(regname))
2885 if (mouse_past_bottom)
2886 dir = FORWARD;
2888 else if (mouse_past_eol)
2889 dir = FORWARD;
2891 if (fixindent)
2893 c1 = (dir == BACKWARD) ? '[' : ']';
2894 c2 = 'p';
2896 else
2898 c1 = (dir == FORWARD) ? 'p' : 'P';
2899 c2 = NUL;
2901 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2904 * Remember where the paste started, so in edit() Insstart can be set
2905 * to this position
2907 if (restart_edit != 0)
2908 where_paste_started = curwin->w_cursor;
2909 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2912 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2914 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2915 * error under the mouse pointer.
2917 else if (((mod_mask & MOD_MASK_CTRL)
2918 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2919 && bt_quickfix(curbuf))
2921 if (State & INSERT)
2922 stuffcharReadbuff(Ctrl_O);
2923 if (curwin->w_llist_ref == NULL) /* quickfix window */
2924 stuffReadbuff((char_u *)":.cc\n");
2925 else /* location list window */
2926 stuffReadbuff((char_u *)":.ll\n");
2927 got_click = FALSE; /* ignore drag&release now */
2929 #endif
2932 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2933 * under the mouse pointer.
2935 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2936 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2938 if (State & INSERT)
2939 stuffcharReadbuff(Ctrl_O);
2940 stuffcharReadbuff(Ctrl_RSB);
2941 got_click = FALSE; /* ignore drag&release now */
2945 * Shift-Mouse click searches for the next occurrence of the word under
2946 * the mouse pointer
2948 else if ((mod_mask & MOD_MASK_SHIFT))
2950 if (State & INSERT
2951 #ifdef FEAT_VISUAL
2952 || (VIsual_active && VIsual_select)
2953 #endif
2955 stuffcharReadbuff(Ctrl_O);
2956 if (which_button == MOUSE_LEFT)
2957 stuffcharReadbuff('*');
2958 else /* MOUSE_RIGHT */
2959 stuffcharReadbuff('#');
2962 /* Handle double clicks, unless on status line */
2963 else if (in_status_line)
2965 #ifdef FEAT_MOUSESHAPE
2966 if ((is_drag || is_click) && !drag_status_line)
2968 drag_status_line = TRUE;
2969 update_mouseshape(-1);
2971 #endif
2973 #ifdef FEAT_VERTSPLIT
2974 else if (in_sep_line)
2976 # ifdef FEAT_MOUSESHAPE
2977 if ((is_drag || is_click) && !drag_sep_line)
2979 drag_sep_line = TRUE;
2980 update_mouseshape(-1);
2982 # endif
2984 #endif
2985 #ifdef FEAT_VISUAL
2986 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
2987 && mouse_has(MOUSE_VISUAL))
2989 if (is_click || !VIsual_active)
2991 if (VIsual_active)
2992 orig_cursor = VIsual;
2993 else
2995 check_visual_highlight();
2996 VIsual = curwin->w_cursor;
2997 orig_cursor = VIsual;
2998 VIsual_active = TRUE;
2999 VIsual_reselect = TRUE;
3000 /* start Select mode if 'selectmode' contains "mouse" */
3001 may_start_select('o');
3002 setmouse();
3004 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3006 /* Double click with ALT pressed makes it blockwise. */
3007 if (mod_mask & MOD_MASK_ALT)
3008 VIsual_mode = Ctrl_V;
3009 else
3010 VIsual_mode = 'v';
3012 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3013 VIsual_mode = 'V';
3014 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3015 VIsual_mode = Ctrl_V;
3016 #ifdef FEAT_CLIPBOARD
3017 /* Make sure the clipboard gets updated. Needed because start and
3018 * end may still be the same, and the selection needs to be owned */
3019 clip_star.vmode = NUL;
3020 #endif
3023 * A double click selects a word or a block.
3025 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3027 pos_T *pos = NULL;
3028 int gc;
3030 if (is_click)
3032 /* If the character under the cursor (skipping white space) is
3033 * not a word character, try finding a match and select a (),
3034 * {}, [], #if/#endif, etc. block. */
3035 end_visual = curwin->w_cursor;
3036 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
3037 inc(&end_visual);
3038 if (oap != NULL)
3039 oap->motion_type = MCHAR;
3040 if (oap != NULL
3041 && VIsual_mode == 'v'
3042 && !vim_iswordc(gchar_pos(&end_visual))
3043 && equalpos(curwin->w_cursor, VIsual)
3044 && (pos = findmatch(oap, NUL)) != NULL)
3046 curwin->w_cursor = *pos;
3047 if (oap->motion_type == MLINE)
3048 VIsual_mode = 'V';
3049 else if (*p_sel == 'e')
3051 if (lt(curwin->w_cursor, VIsual))
3052 ++VIsual.col;
3053 else
3054 ++curwin->w_cursor.col;
3059 if (pos == NULL && (is_click || is_drag))
3061 /* When not found a match or when dragging: extend to include
3062 * a word. */
3063 if (lt(curwin->w_cursor, orig_cursor))
3065 find_start_of_word(&curwin->w_cursor);
3066 find_end_of_word(&VIsual);
3068 else
3070 find_start_of_word(&VIsual);
3071 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3072 #ifdef FEAT_MBYTE
3073 curwin->w_cursor.col +=
3074 (*mb_ptr2len)(ml_get_cursor());
3075 #else
3076 ++curwin->w_cursor.col;
3077 #endif
3078 find_end_of_word(&curwin->w_cursor);
3081 curwin->w_set_curswant = TRUE;
3083 if (is_click)
3084 redraw_curbuf_later(INVERTED); /* update the inversion */
3086 else if (VIsual_active && !old_active)
3088 if (mod_mask & MOD_MASK_ALT)
3089 VIsual_mode = Ctrl_V;
3090 else
3091 VIsual_mode = 'v';
3094 /* If Visual mode changed show it later. */
3095 if ((!VIsual_active && old_active && mode_displayed)
3096 || (VIsual_active && p_smd && msg_silent == 0
3097 && (!old_active || VIsual_mode != old_mode)))
3098 redraw_cmdline = TRUE;
3099 #endif
3101 return moved;
3104 #ifdef FEAT_VISUAL
3106 * Move "pos" back to the start of the word it's in.
3108 static void
3109 find_start_of_word(pos)
3110 pos_T *pos;
3112 char_u *line;
3113 int cclass;
3114 int col;
3116 line = ml_get(pos->lnum);
3117 cclass = get_mouse_class(line + pos->col);
3119 while (pos->col > 0)
3121 col = pos->col - 1;
3122 #ifdef FEAT_MBYTE
3123 col -= (*mb_head_off)(line, line + col);
3124 #endif
3125 if (get_mouse_class(line + col) != cclass)
3126 break;
3127 pos->col = col;
3132 * Move "pos" forward to the end of the word it's in.
3133 * When 'selection' is "exclusive", the position is just after the word.
3135 static void
3136 find_end_of_word(pos)
3137 pos_T *pos;
3139 char_u *line;
3140 int cclass;
3141 int col;
3143 line = ml_get(pos->lnum);
3144 if (*p_sel == 'e' && pos->col > 0)
3146 --pos->col;
3147 #ifdef FEAT_MBYTE
3148 pos->col -= (*mb_head_off)(line, line + pos->col);
3149 #endif
3151 cclass = get_mouse_class(line + pos->col);
3152 while (line[pos->col] != NUL)
3154 #ifdef FEAT_MBYTE
3155 col = pos->col + (*mb_ptr2len)(line + pos->col);
3156 #else
3157 col = pos->col + 1;
3158 #endif
3159 if (get_mouse_class(line + col) != cclass)
3161 if (*p_sel == 'e')
3162 pos->col = col;
3163 break;
3165 pos->col = col;
3170 * Get class of a character for selection: same class means same word.
3171 * 0: blank
3172 * 1: punctuation groups
3173 * 2: normal word character
3174 * >2: multi-byte word character.
3176 static int
3177 get_mouse_class(p)
3178 char_u *p;
3180 int c;
3182 #ifdef FEAT_MBYTE
3183 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3184 return mb_get_class(p);
3185 #endif
3187 c = *p;
3188 if (c == ' ' || c == '\t')
3189 return 0;
3191 if (vim_iswordc(c))
3192 return 2;
3195 * There are a few special cases where we want certain combinations of
3196 * characters to be considered as a single word. These are things like
3197 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
3198 * character is in it's own class.
3200 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3201 return 1;
3202 return c;
3204 #endif /* FEAT_VISUAL */
3205 #endif /* FEAT_MOUSE */
3207 #if defined(FEAT_VISUAL) || defined(PROTO)
3209 * Check if highlighting for visual mode is possible, give a warning message
3210 * if not.
3212 void
3213 check_visual_highlight()
3215 static int did_check = FALSE;
3217 if (full_screen)
3219 if (!did_check && hl_attr(HLF_V) == 0)
3220 MSG(_("Warning: terminal cannot highlight"));
3221 did_check = TRUE;
3226 * End Visual mode.
3227 * This function should ALWAYS be called to end Visual mode, except from
3228 * do_pending_operator().
3230 void
3231 end_visual_mode()
3233 #ifdef FEAT_CLIPBOARD
3235 * If we are using the clipboard, then remember what was selected in case
3236 * we need to paste it somewhere while we still own the selection.
3237 * Only do this when the clipboard is already owned. Don't want to grab
3238 * the selection when hitting ESC.
3240 if (clip_star.available && clip_star.owned)
3241 clip_auto_select();
3242 #endif
3244 VIsual_active = FALSE;
3245 #ifdef FEAT_MOUSE
3246 setmouse();
3247 mouse_dragging = 0;
3248 #endif
3250 /* Save the current VIsual area for '< and '> marks, and "gv" */
3251 curbuf->b_visual.vi_mode = VIsual_mode;
3252 curbuf->b_visual.vi_start = VIsual;
3253 curbuf->b_visual.vi_end = curwin->w_cursor;
3254 curbuf->b_visual.vi_curswant = curwin->w_curswant;
3255 #ifdef FEAT_EVAL
3256 curbuf->b_visual_mode_eval = VIsual_mode;
3257 #endif
3258 #ifdef FEAT_VIRTUALEDIT
3259 if (!virtual_active())
3260 curwin->w_cursor.coladd = 0;
3261 #endif
3263 if (mode_displayed)
3264 clear_cmdline = TRUE; /* unshow visual mode later */
3265 #ifdef FEAT_CMDL_INFO
3266 else
3267 clear_showcmd();
3268 #endif
3270 adjust_cursor_eol();
3274 * Reset VIsual_active and VIsual_reselect.
3276 void
3277 reset_VIsual_and_resel()
3279 if (VIsual_active)
3281 end_visual_mode();
3282 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3284 VIsual_reselect = FALSE;
3288 * Reset VIsual_active and VIsual_reselect if it's set.
3290 void
3291 reset_VIsual()
3293 if (VIsual_active)
3295 end_visual_mode();
3296 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3297 VIsual_reselect = FALSE;
3300 #endif /* FEAT_VISUAL */
3302 #if defined(FEAT_BEVAL)
3303 static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3306 * Check for a balloon-eval special item to include when searching for an
3307 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3308 * Returns TRUE if the character at "*ptr" should be included.
3309 * "dir" is FORWARD or BACKWARD, the direction of searching.
3310 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3311 * "bnp" points to a counter for square brackets.
3313 static int
3314 find_is_eval_item(ptr, colp, bnp, dir)
3315 char_u *ptr;
3316 int *colp;
3317 int *bnp;
3318 int dir;
3320 /* Accept everything inside []. */
3321 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3322 ++*bnp;
3323 if (*bnp > 0)
3325 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3326 --*bnp;
3327 return TRUE;
3330 /* skip over "s.var" */
3331 if (*ptr == '.')
3332 return TRUE;
3334 /* two-character item: s->var */
3335 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3336 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3338 *colp += dir;
3339 return TRUE;
3341 return FALSE;
3343 #endif
3346 * Find the identifier under or to the right of the cursor.
3347 * "find_type" can have one of three values:
3348 * FIND_IDENT: find an identifier (keyword)
3349 * FIND_STRING: find any non-white string
3350 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
3351 * FIND_EVAL: find text useful for C program debugging
3353 * There are three steps:
3354 * 1. Search forward for the start of an identifier/string. Doesn't move if
3355 * already on one.
3356 * 2. Search backward for the start of this identifier/string.
3357 * This doesn't match the real Vi but I like it a little better and it
3358 * shouldn't bother anyone.
3359 * 3. Search forward to the end of this identifier/string.
3360 * When FIND_IDENT isn't defined, we backup until a blank.
3362 * Returns the length of the string, or zero if no string is found.
3363 * If a string is found, a pointer to the string is put in "*string". This
3364 * string is not always NUL terminated.
3367 find_ident_under_cursor(string, find_type)
3368 char_u **string;
3369 int find_type;
3371 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3372 curwin->w_cursor.col, string, find_type);
3376 * Like find_ident_under_cursor(), but for any window and any position.
3377 * However: Uses 'iskeyword' from the current window!.
3380 find_ident_at_pos(wp, lnum, startcol, string, find_type)
3381 win_T *wp;
3382 linenr_T lnum;
3383 colnr_T startcol;
3384 char_u **string;
3385 int find_type;
3387 char_u *ptr;
3388 int col = 0; /* init to shut up GCC */
3389 int i;
3390 #ifdef FEAT_MBYTE
3391 int this_class = 0;
3392 int prev_class;
3393 int prevcol;
3394 #endif
3395 #if defined(FEAT_BEVAL)
3396 int bn = 0; /* bracket nesting */
3397 #endif
3400 * if i == 0: try to find an identifier
3401 * if i == 1: try to find any non-white string
3403 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3404 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3407 * 1. skip to start of identifier/string
3409 col = startcol;
3410 #ifdef FEAT_MBYTE
3411 if (has_mbyte)
3413 while (ptr[col] != NUL)
3415 # if defined(FEAT_BEVAL)
3416 /* Stop at a ']' to evaluate "a[x]". */
3417 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3418 break;
3419 # endif
3420 this_class = mb_get_class(ptr + col);
3421 if (this_class != 0 && (i == 1 || this_class != 1))
3422 break;
3423 col += (*mb_ptr2len)(ptr + col);
3426 else
3427 #endif
3428 while (ptr[col] != NUL
3429 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
3430 # if defined(FEAT_BEVAL)
3431 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3432 # endif
3434 ++col;
3436 #if defined(FEAT_BEVAL)
3437 /* When starting on a ']' count it, so that we include the '['. */
3438 bn = ptr[col] == ']';
3439 #endif
3442 * 2. Back up to start of identifier/string.
3444 #ifdef FEAT_MBYTE
3445 if (has_mbyte)
3447 /* Remember class of character under cursor. */
3448 # if defined(FEAT_BEVAL)
3449 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3450 this_class = mb_get_class((char_u *)"a");
3451 else
3452 # endif
3453 this_class = mb_get_class(ptr + col);
3454 while (col > 0 && this_class != 0)
3456 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3457 prev_class = mb_get_class(ptr + prevcol);
3458 if (this_class != prev_class
3459 && (i == 0
3460 || prev_class == 0
3461 || (find_type & FIND_IDENT))
3462 # if defined(FEAT_BEVAL)
3463 && (!(find_type & FIND_EVAL)
3464 || prevcol == 0
3465 || !find_is_eval_item(ptr + prevcol, &prevcol,
3466 &bn, BACKWARD))
3467 # endif
3469 break;
3470 col = prevcol;
3473 /* If we don't want just any old string, or we've found an
3474 * identifier, stop searching. */
3475 if (this_class > 2)
3476 this_class = 2;
3477 if (!(find_type & FIND_STRING) || this_class == 2)
3478 break;
3480 else
3481 #endif
3483 while (col > 0
3484 && ((i == 0
3485 ? vim_iswordc(ptr[col - 1])
3486 : (!vim_iswhite(ptr[col - 1])
3487 && (!(find_type & FIND_IDENT)
3488 || !vim_iswordc(ptr[col - 1]))))
3489 #if defined(FEAT_BEVAL)
3490 || ((find_type & FIND_EVAL)
3491 && col > 1
3492 && find_is_eval_item(ptr + col - 1, &col,
3493 &bn, BACKWARD))
3494 #endif
3496 --col;
3498 /* If we don't want just any old string, or we've found an
3499 * identifier, stop searching. */
3500 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3501 break;
3505 if (ptr[col] == NUL || (i == 0 && (
3506 #ifdef FEAT_MBYTE
3507 has_mbyte ? this_class != 2 :
3508 #endif
3509 !vim_iswordc(ptr[col]))))
3512 * didn't find an identifier or string
3514 if (find_type & FIND_STRING)
3515 EMSG(_("E348: No string under cursor"));
3516 else
3517 EMSG(_(e_noident));
3518 return 0;
3520 ptr += col;
3521 *string = ptr;
3524 * 3. Find the end if the identifier/string.
3526 #if defined(FEAT_BEVAL)
3527 bn = 0;
3528 startcol -= col;
3529 #endif
3530 col = 0;
3531 #ifdef FEAT_MBYTE
3532 if (has_mbyte)
3534 /* Search for point of changing multibyte character class. */
3535 this_class = mb_get_class(ptr);
3536 while (ptr[col] != NUL
3537 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3538 : mb_get_class(ptr + col) != 0)
3539 # if defined(FEAT_BEVAL)
3540 || ((find_type & FIND_EVAL)
3541 && col <= (int)startcol
3542 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3543 # endif
3545 col += (*mb_ptr2len)(ptr + col);
3547 else
3548 #endif
3549 while ((i == 0 ? vim_iswordc(ptr[col])
3550 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
3551 # if defined(FEAT_BEVAL)
3552 || ((find_type & FIND_EVAL)
3553 && col <= (int)startcol
3554 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3555 # endif
3558 ++col;
3561 return col;
3565 * Prepare for redo of a normal command.
3567 static void
3568 prep_redo_cmd(cap)
3569 cmdarg_T *cap;
3571 prep_redo(cap->oap->regname, cap->count0,
3572 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3576 * Prepare for redo of any command.
3577 * Note that only the last argument can be a multi-byte char.
3579 static void
3580 prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3581 int regname;
3582 long num;
3583 int cmd1;
3584 int cmd2;
3585 int cmd3;
3586 int cmd4;
3587 int cmd5;
3589 ResetRedobuff();
3590 if (regname != 0) /* yank from specified buffer */
3592 AppendCharToRedobuff('"');
3593 AppendCharToRedobuff(regname);
3595 if (num)
3596 AppendNumberToRedobuff(num);
3598 if (cmd1 != NUL)
3599 AppendCharToRedobuff(cmd1);
3600 if (cmd2 != NUL)
3601 AppendCharToRedobuff(cmd2);
3602 if (cmd3 != NUL)
3603 AppendCharToRedobuff(cmd3);
3604 if (cmd4 != NUL)
3605 AppendCharToRedobuff(cmd4);
3606 if (cmd5 != NUL)
3607 AppendCharToRedobuff(cmd5);
3611 * check for operator active and clear it
3613 * return TRUE if operator was active
3615 static int
3616 checkclearop(oap)
3617 oparg_T *oap;
3619 if (oap->op_type == OP_NOP)
3620 return FALSE;
3621 clearopbeep(oap);
3622 return TRUE;
3626 * Check for operator or Visual active. Clear active operator.
3628 * Return TRUE if operator or Visual was active.
3630 static int
3631 checkclearopq(oap)
3632 oparg_T *oap;
3634 if (oap->op_type == OP_NOP
3635 #ifdef FEAT_VISUAL
3636 && !VIsual_active
3637 #endif
3639 return FALSE;
3640 clearopbeep(oap);
3641 return TRUE;
3644 static void
3645 clearop(oap)
3646 oparg_T *oap;
3648 oap->op_type = OP_NOP;
3649 oap->regname = 0;
3650 oap->motion_force = NUL;
3651 oap->use_reg_one = FALSE;
3654 static void
3655 clearopbeep(oap)
3656 oparg_T *oap;
3658 clearop(oap);
3659 beep_flush();
3662 #ifdef FEAT_VISUAL
3664 * Remove the shift modifier from a special key.
3666 static void
3667 unshift_special(cap)
3668 cmdarg_T *cap;
3670 switch (cap->cmdchar)
3672 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3673 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3674 case K_S_UP: cap->cmdchar = K_UP; break;
3675 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3676 case K_S_HOME: cap->cmdchar = K_HOME; break;
3677 case K_S_END: cap->cmdchar = K_END; break;
3679 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3681 #endif
3683 #if defined(FEAT_CMDL_INFO) || defined(PROTO)
3685 * Routines for displaying a partly typed command
3688 #ifdef FEAT_VISUAL /* need room for size of Visual area */
3689 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3690 #else
3691 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3692 #endif
3693 static char_u showcmd_buf[SHOWCMD_BUFLEN];
3694 static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3695 static int showcmd_is_clear = TRUE;
3696 static int showcmd_visual = FALSE;
3698 static void display_showcmd __ARGS((void));
3700 void
3701 clear_showcmd()
3703 if (!p_sc)
3704 return;
3706 #ifdef FEAT_VISUAL
3707 if (VIsual_active && !char_avail())
3709 int i = lt(VIsual, curwin->w_cursor);
3710 long lines;
3711 colnr_T leftcol, rightcol;
3712 linenr_T top, bot;
3714 /* Show the size of the Visual area. */
3715 if (i)
3717 top = VIsual.lnum;
3718 bot = curwin->w_cursor.lnum;
3720 else
3722 top = curwin->w_cursor.lnum;
3723 bot = VIsual.lnum;
3725 # ifdef FEAT_FOLDING
3726 /* Include closed folds as a whole. */
3727 hasFolding(top, &top, NULL);
3728 hasFolding(bot, NULL, &bot);
3729 # endif
3730 lines = bot - top + 1;
3732 if (VIsual_mode == Ctrl_V)
3734 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
3735 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3736 (long)(rightcol - leftcol + 1));
3738 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3739 sprintf((char *)showcmd_buf, "%ld", lines);
3740 else
3741 sprintf((char *)showcmd_buf, "%ld", (long)(i
3742 ? curwin->w_cursor.col - VIsual.col
3743 : VIsual.col - curwin->w_cursor.col) + (*p_sel != 'e'));
3744 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3745 showcmd_visual = TRUE;
3747 else
3748 #endif
3750 showcmd_buf[0] = NUL;
3751 showcmd_visual = FALSE;
3753 /* Don't actually display something if there is nothing to clear. */
3754 if (showcmd_is_clear)
3755 return;
3758 display_showcmd();
3762 * Add 'c' to string of shown command chars.
3763 * Return TRUE if output has been written (and setcursor() has been called).
3766 add_to_showcmd(c)
3767 int c;
3769 char_u *p;
3770 int old_len;
3771 int extra_len;
3772 int overflow;
3773 #if defined(FEAT_MOUSE)
3774 int i;
3775 static int ignore[] =
3777 # ifdef FEAT_GUI
3778 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3779 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
3780 # endif
3781 K_IGNORE,
3782 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3783 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3784 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
3785 K_MOUSEDOWN, K_MOUSEUP,
3786 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
3787 K_CURSORHOLD,
3790 #endif
3792 if (!p_sc || msg_silent != 0)
3793 return FALSE;
3795 if (showcmd_visual)
3797 showcmd_buf[0] = NUL;
3798 showcmd_visual = FALSE;
3801 #if defined(FEAT_MOUSE)
3802 /* Ignore keys that are scrollbar updates and mouse clicks */
3803 if (IS_SPECIAL(c))
3804 for (i = 0; ignore[i] != 0; ++i)
3805 if (ignore[i] == c)
3806 return FALSE;
3807 #endif
3809 p = transchar(c);
3810 old_len = (int)STRLEN(showcmd_buf);
3811 extra_len = (int)STRLEN(p);
3812 overflow = old_len + extra_len - SHOWCMD_COLS;
3813 if (overflow > 0)
3814 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3815 old_len - overflow + 1);
3816 STRCAT(showcmd_buf, p);
3818 if (char_avail())
3819 return FALSE;
3821 display_showcmd();
3823 return TRUE;
3826 void
3827 add_to_showcmd_c(c)
3828 int c;
3830 if (!add_to_showcmd(c))
3831 setcursor();
3835 * Delete 'len' characters from the end of the shown command.
3837 static void
3838 del_from_showcmd(len)
3839 int len;
3841 int old_len;
3843 if (!p_sc)
3844 return;
3846 old_len = (int)STRLEN(showcmd_buf);
3847 if (len > old_len)
3848 len = old_len;
3849 showcmd_buf[old_len - len] = NUL;
3851 if (!char_avail())
3852 display_showcmd();
3856 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3857 * something and there is a partial mapping.
3859 void
3860 push_showcmd()
3862 if (p_sc)
3863 STRCPY(old_showcmd_buf, showcmd_buf);
3866 void
3867 pop_showcmd()
3869 if (!p_sc)
3870 return;
3872 STRCPY(showcmd_buf, old_showcmd_buf);
3874 display_showcmd();
3877 static void
3878 display_showcmd()
3880 int len;
3882 cursor_off();
3884 len = (int)STRLEN(showcmd_buf);
3885 if (len == 0)
3886 showcmd_is_clear = TRUE;
3887 else
3889 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3890 showcmd_is_clear = FALSE;
3894 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3895 * spaces
3897 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3899 setcursor(); /* put cursor back where it belongs */
3901 #endif
3903 #ifdef FEAT_SCROLLBIND
3905 * When "check" is FALSE, prepare for commands that scroll the window.
3906 * When "check" is TRUE, take care of scroll-binding after the window has
3907 * scrolled. Called from normal_cmd() and edit().
3909 void
3910 do_check_scrollbind(check)
3911 int check;
3913 static win_T *old_curwin = NULL;
3914 static linenr_T old_topline = 0;
3915 #ifdef FEAT_DIFF
3916 static int old_topfill = 0;
3917 #endif
3918 static buf_T *old_buf = NULL;
3919 static colnr_T old_leftcol = 0;
3921 if (check && curwin->w_p_scb)
3923 /* If a ":syncbind" command was just used, don't scroll, only reset
3924 * the values. */
3925 if (did_syncbind)
3926 did_syncbind = FALSE;
3927 else if (curwin == old_curwin)
3930 * Synchronize other windows, as necessary according to
3931 * 'scrollbind'. Don't do this after an ":edit" command, except
3932 * when 'diff' is set.
3934 if ((curwin->w_buffer == old_buf
3935 #ifdef FEAT_DIFF
3936 || curwin->w_p_diff
3937 #endif
3939 && (curwin->w_topline != old_topline
3940 #ifdef FEAT_DIFF
3941 || curwin->w_topfill != old_topfill
3942 #endif
3943 || curwin->w_leftcol != old_leftcol))
3945 check_scrollbind(curwin->w_topline - old_topline,
3946 (long)(curwin->w_leftcol - old_leftcol));
3949 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
3952 * When switching between windows, make sure that the relative
3953 * vertical offset is valid for the new window. The relative
3954 * offset is invalid whenever another 'scrollbind' window has
3955 * scrolled to a point that would force the current window to
3956 * scroll past the beginning or end of its buffer. When the
3957 * resync is performed, some of the other 'scrollbind' windows may
3958 * need to jump so that the current window's relative position is
3959 * visible on-screen.
3961 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
3963 curwin->w_scbind_pos = curwin->w_topline;
3966 old_curwin = curwin;
3967 old_topline = curwin->w_topline;
3968 #ifdef FEAT_DIFF
3969 old_topfill = curwin->w_topfill;
3970 #endif
3971 old_buf = curwin->w_buffer;
3972 old_leftcol = curwin->w_leftcol;
3976 * Synchronize any windows that have "scrollbind" set, based on the
3977 * number of rows by which the current window has changed
3978 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3980 void
3981 check_scrollbind(topline_diff, leftcol_diff)
3982 linenr_T topline_diff;
3983 long leftcol_diff;
3985 int want_ver;
3986 int want_hor;
3987 win_T *old_curwin = curwin;
3988 buf_T *old_curbuf = curbuf;
3989 #ifdef FEAT_VISUAL
3990 int old_VIsual_select = VIsual_select;
3991 int old_VIsual_active = VIsual_active;
3992 #endif
3993 colnr_T tgt_leftcol = curwin->w_leftcol;
3994 long topline;
3995 long y;
3998 * check 'scrollopt' string for vertical and horizontal scroll options
4000 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4001 #ifdef FEAT_DIFF
4002 want_ver |= old_curwin->w_p_diff;
4003 #endif
4004 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4007 * loop through the scrollbound windows and scroll accordingly
4009 #ifdef FEAT_VISUAL
4010 VIsual_select = VIsual_active = 0;
4011 #endif
4012 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4014 curbuf = curwin->w_buffer;
4015 /* skip original window and windows with 'noscrollbind' */
4016 if (curwin != old_curwin && curwin->w_p_scb)
4019 * do the vertical scroll
4021 if (want_ver)
4023 #ifdef FEAT_DIFF
4024 if (old_curwin->w_p_diff && curwin->w_p_diff)
4026 diff_set_topline(old_curwin, curwin);
4028 else
4029 #endif
4031 curwin->w_scbind_pos += topline_diff;
4032 topline = curwin->w_scbind_pos;
4033 if (topline > curbuf->b_ml.ml_line_count)
4034 topline = curbuf->b_ml.ml_line_count;
4035 if (topline < 1)
4036 topline = 1;
4038 y = topline - curwin->w_topline;
4039 if (y > 0)
4040 scrollup(y, FALSE);
4041 else
4042 scrolldown(-y, FALSE);
4045 redraw_later(VALID);
4046 cursor_correct();
4047 #ifdef FEAT_WINDOWS
4048 curwin->w_redr_status = TRUE;
4049 #endif
4053 * do the horizontal scroll
4055 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4057 curwin->w_leftcol = tgt_leftcol;
4058 leftcol_changed();
4064 * reset current-window
4066 #ifdef FEAT_VISUAL
4067 VIsual_select = old_VIsual_select;
4068 VIsual_active = old_VIsual_active;
4069 #endif
4070 curwin = old_curwin;
4071 curbuf = old_curbuf;
4073 #endif /* #ifdef FEAT_SCROLLBIND */
4076 * Command character that's ignored.
4077 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
4078 * xon/xoff
4080 static void
4081 nv_ignore(cap)
4082 cmdarg_T *cap;
4084 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
4088 * Command character that doesn't do anything, but unlike nv_ignore() does
4089 * start edit(). Used for "startinsert" executed while starting up.
4091 /*ARGSUSED */
4092 static void
4093 nv_nop(cap)
4094 cmdarg_T *cap;
4099 * Command character doesn't exist.
4101 static void
4102 nv_error(cap)
4103 cmdarg_T *cap;
4105 clearopbeep(cap->oap);
4109 * <Help> and <F1> commands.
4111 static void
4112 nv_help(cap)
4113 cmdarg_T *cap;
4115 if (!checkclearopq(cap->oap))
4116 ex_help(NULL);
4120 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4122 static void
4123 nv_addsub(cap)
4124 cmdarg_T *cap;
4126 if (!checkclearopq(cap->oap)
4127 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4128 prep_redo_cmd(cap);
4132 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4134 static void
4135 nv_page(cap)
4136 cmdarg_T *cap;
4138 if (!checkclearop(cap->oap))
4140 #ifdef FEAT_WINDOWS
4141 if (mod_mask & MOD_MASK_CTRL)
4143 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4144 if (cap->arg == BACKWARD)
4145 goto_tabpage(-(int)cap->count1);
4146 else
4147 goto_tabpage((int)cap->count0);
4149 else
4150 #endif
4151 (void)onepage(cap->arg, cap->count1);
4156 * Implementation of "gd" and "gD" command.
4158 static void
4159 nv_gd(oap, nchar, thisblock)
4160 oparg_T *oap;
4161 int nchar;
4162 int thisblock; /* 1 for "1gd" and "1gD" */
4164 int len;
4165 char_u *ptr;
4167 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
4168 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
4169 clearopbeep(oap);
4170 #ifdef FEAT_FOLDING
4171 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4172 foldOpenCursor();
4173 #endif
4177 * Search for variable declaration of "ptr[len]".
4178 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4179 * current file ("gD").
4180 * When "thisblock" is TRUE check the {} block scope.
4181 * Return FAIL when not found.
4184 find_decl(ptr, len, locally, thisblock, searchflags)
4185 char_u *ptr;
4186 int len;
4187 int locally;
4188 int thisblock;
4189 int searchflags; /* flags passed to searchit() */
4191 char_u *pat;
4192 pos_T old_pos;
4193 pos_T par_pos;
4194 pos_T found_pos;
4195 int t;
4196 int save_p_ws;
4197 int save_p_scs;
4198 int retval = OK;
4199 int incll;
4201 if ((pat = alloc(len + 7)) == NULL)
4202 return FAIL;
4204 /* Put "\V" before the pattern to avoid that the special meaning of "."
4205 * and "~" causes trouble. */
4206 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4207 len, ptr);
4208 old_pos = curwin->w_cursor;
4209 save_p_ws = p_ws;
4210 save_p_scs = p_scs;
4211 p_ws = FALSE; /* don't wrap around end of file now */
4212 p_scs = FALSE; /* don't switch ignorecase off now */
4215 * With "gD" go to line 1.
4216 * With "gd" Search back for the start of the current function, then go
4217 * back until a blank line. If this fails go to line 1.
4219 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
4221 setpcmark(); /* Set in findpar() otherwise */
4222 curwin->w_cursor.lnum = 1;
4223 par_pos = curwin->w_cursor;
4225 else
4227 par_pos = curwin->w_cursor;
4228 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4229 --curwin->w_cursor.lnum;
4231 curwin->w_cursor.col = 0;
4233 /* Search forward for the identifier, ignore comment lines. */
4234 clearpos(&found_pos);
4235 for (;;)
4237 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
4238 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
4239 if (curwin->w_cursor.lnum >= old_pos.lnum)
4240 t = FAIL; /* match after start is failure too */
4242 if (thisblock && t != FAIL)
4244 pos_T *pos;
4246 /* Check that the block the match is in doesn't end before the
4247 * position where we started the search from. */
4248 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4249 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4250 && pos->lnum < old_pos.lnum)
4251 continue;
4254 if (t == FAIL)
4256 /* If we previously found a valid position, use it. */
4257 if (found_pos.lnum != 0)
4259 curwin->w_cursor = found_pos;
4260 t = OK;
4262 break;
4264 #ifdef FEAT_COMMENTS
4265 if (get_leader_len(ml_get_curline(), NULL, FALSE) > 0)
4267 /* Ignore this line, continue at start of next line. */
4268 ++curwin->w_cursor.lnum;
4269 curwin->w_cursor.col = 0;
4270 continue;
4272 #endif
4273 if (!locally) /* global search: use first match found */
4274 break;
4275 if (curwin->w_cursor.lnum >= par_pos.lnum)
4277 /* If we previously found a valid position, use it. */
4278 if (found_pos.lnum != 0)
4279 curwin->w_cursor = found_pos;
4280 break;
4283 /* For finding a local variable and the match is before the "{" search
4284 * to find a later match. For K&R style function declarations this
4285 * skips the function header without types. */
4286 found_pos = curwin->w_cursor;
4289 if (t == FAIL)
4291 retval = FAIL;
4292 curwin->w_cursor = old_pos;
4294 else
4296 curwin->w_set_curswant = TRUE;
4297 /* "n" searches forward now */
4298 reset_search_dir();
4301 vim_free(pat);
4302 p_ws = save_p_ws;
4303 p_scs = save_p_scs;
4305 return retval;
4309 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4310 * lines rather than lines in the file.
4311 * 'dist' must be positive.
4313 * Return OK if able to move cursor, FAIL otherwise.
4315 static int
4316 nv_screengo(oap, dir, dist)
4317 oparg_T *oap;
4318 int dir;
4319 long dist;
4321 int linelen = linetabsize(ml_get_curline());
4322 int retval = OK;
4323 int atend = FALSE;
4324 int n;
4325 int col_off1; /* margin offset for first screen line */
4326 int col_off2; /* margin offset for wrapped screen line */
4327 int width1; /* text width for first screen line */
4328 int width2; /* test width for wrapped screen line */
4330 oap->motion_type = MCHAR;
4331 oap->inclusive = FALSE;
4333 col_off1 = curwin_col_off();
4334 col_off2 = col_off1 - curwin_col_off2();
4335 width1 = W_WIDTH(curwin) - col_off1;
4336 width2 = W_WIDTH(curwin) - col_off2;
4338 #ifdef FEAT_VERTSPLIT
4339 if (curwin->w_width != 0)
4341 #endif
4343 * Instead of sticking at the last character of the buffer line we
4344 * try to stick in the last column of the screen.
4346 if (curwin->w_curswant == MAXCOL)
4348 atend = TRUE;
4349 validate_virtcol();
4350 if (width1 <= 0)
4351 curwin->w_curswant = 0;
4352 else
4354 curwin->w_curswant = width1 - 1;
4355 if (curwin->w_virtcol > curwin->w_curswant)
4356 curwin->w_curswant += ((curwin->w_virtcol
4357 - curwin->w_curswant - 1) / width2 + 1) * width2;
4360 else
4362 if (linelen > width1)
4363 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4364 else
4365 n = width1;
4366 if (curwin->w_curswant > (colnr_T)n + 1)
4367 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4368 * width2;
4371 while (dist--)
4373 if (dir == BACKWARD)
4375 if ((long)curwin->w_curswant >= width2)
4376 /* move back within line */
4377 curwin->w_curswant -= width2;
4378 else
4380 /* to previous line */
4381 if (curwin->w_cursor.lnum == 1)
4383 retval = FAIL;
4384 break;
4386 --curwin->w_cursor.lnum;
4387 #ifdef FEAT_FOLDING
4388 /* Move to the start of a closed fold. Don't do that when
4389 * 'foldopen' contains "all": it will open in a moment. */
4390 if (!(fdo_flags & FDO_ALL))
4391 (void)hasFolding(curwin->w_cursor.lnum,
4392 &curwin->w_cursor.lnum, NULL);
4393 #endif
4394 linelen = linetabsize(ml_get_curline());
4395 if (linelen > width1)
4396 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4397 + 1) * width2;
4400 else /* dir == FORWARD */
4402 if (linelen > width1)
4403 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4404 else
4405 n = width1;
4406 if (curwin->w_curswant + width2 < (colnr_T)n)
4407 /* move forward within line */
4408 curwin->w_curswant += width2;
4409 else
4411 /* to next line */
4412 #ifdef FEAT_FOLDING
4413 /* Move to the end of a closed fold. */
4414 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4415 &curwin->w_cursor.lnum);
4416 #endif
4417 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4419 retval = FAIL;
4420 break;
4422 curwin->w_cursor.lnum++;
4423 curwin->w_curswant %= width2;
4427 #ifdef FEAT_VERTSPLIT
4429 #endif
4431 coladvance(curwin->w_curswant);
4433 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4434 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4437 * Check for landing on a character that got split at the end of the
4438 * last line. We want to advance a screenline, not end up in the same
4439 * screenline or move two screenlines.
4441 validate_virtcol();
4442 if (curwin->w_virtcol > curwin->w_curswant
4443 && (curwin->w_curswant < (colnr_T)width1
4444 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4445 : ((curwin->w_curswant - width1) % width2
4446 > (colnr_T)width2 / 2)))
4447 --curwin->w_cursor.col;
4449 #endif
4451 if (atend)
4452 curwin->w_curswant = MAXCOL; /* stick in the last column */
4454 return retval;
4457 #ifdef FEAT_MOUSE
4459 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4460 * when Shift or Ctrl is used.
4461 * K_MOUSEUP (cap->arg == TRUE) or K_MOUSEDOWN (cap->arg == FALSE)
4463 static void
4464 nv_mousescroll(cap)
4465 cmdarg_T *cap;
4467 # ifdef FEAT_GUI_SCROLL_WHEEL_FORCE
4468 int scroll_wheel_force = 0;
4469 # endif
4470 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4471 win_T *old_curwin = curwin;
4473 /* Currently we only get the mouse coordinates in the GUI. */
4474 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4476 int row, col;
4478 row = mouse_row;
4479 col = mouse_col;
4481 /* find the window at the pointer coordinates */
4482 curwin = mouse_find_win(&row, &col);
4483 curbuf = curwin->w_buffer;
4485 # endif
4487 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4489 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4491 else
4493 # ifdef FEAT_GUI_SCROLL_WHEEL_FORCE
4494 if (gui.in_use && gui.scroll_wheel_force >= 1) {
4495 scroll_wheel_force = gui.scroll_wheel_force;
4496 if (scroll_wheel_force > 1000)
4497 scroll_wheel_force = 1000;
4499 cap->count1 = scroll_wheel_force;
4500 cap->count0 = scroll_wheel_force;
4501 } else {
4502 cap->count1 = 3;
4503 cap->count0 = 3;
4505 # else
4506 cap->count1 = 3;
4507 cap->count0 = 3;
4508 # endif
4509 nv_scroll_line(cap);
4512 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4513 curwin->w_redr_status = TRUE;
4515 curwin = old_curwin;
4516 curbuf = curwin->w_buffer;
4517 # endif
4521 * Mouse clicks and drags.
4523 static void
4524 nv_mouse(cap)
4525 cmdarg_T *cap;
4527 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4529 #endif
4532 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4533 * cap->arg must be TRUE for CTRL-E.
4535 static void
4536 nv_scroll_line(cap)
4537 cmdarg_T *cap;
4539 if (!checkclearop(cap->oap))
4540 scroll_redraw(cap->arg, cap->count1);
4544 * Scroll "count" lines up or down, and redraw.
4546 void
4547 scroll_redraw(up, count)
4548 int up;
4549 long count;
4551 linenr_T prev_topline = curwin->w_topline;
4552 #ifdef FEAT_DIFF
4553 int prev_topfill = curwin->w_topfill;
4554 #endif
4555 linenr_T prev_lnum = curwin->w_cursor.lnum;
4557 if (up)
4558 scrollup(count, TRUE);
4559 else
4560 scrolldown(count, TRUE);
4561 if (p_so)
4563 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4564 * valid, otherwise the screen jumps back at the end of the file. */
4565 cursor_correct();
4566 check_cursor_moved(curwin);
4567 curwin->w_valid |= VALID_TOPLINE;
4569 /* If moved back to where we were, at least move the cursor, otherwise
4570 * we get stuck at one position. Don't move the cursor up if the
4571 * first line of the buffer is already on the screen */
4572 while (curwin->w_topline == prev_topline
4573 #ifdef FEAT_DIFF
4574 && curwin->w_topfill == prev_topfill
4575 #endif
4578 if (up)
4580 if (curwin->w_cursor.lnum > prev_lnum
4581 || cursor_down(1L, FALSE) == FAIL)
4582 break;
4584 else
4586 if (curwin->w_cursor.lnum < prev_lnum
4587 || prev_topline == 1L
4588 || cursor_up(1L, FALSE) == FAIL)
4589 break;
4591 /* Mark w_topline as valid, otherwise the screen jumps back at the
4592 * end of the file. */
4593 check_cursor_moved(curwin);
4594 curwin->w_valid |= VALID_TOPLINE;
4597 if (curwin->w_cursor.lnum != prev_lnum)
4598 coladvance(curwin->w_curswant);
4599 redraw_later(VALID);
4603 * Commands that start with "z".
4605 static void
4606 nv_zet(cap)
4607 cmdarg_T *cap;
4609 long n;
4610 colnr_T col;
4611 int nchar = cap->nchar;
4612 #ifdef FEAT_FOLDING
4613 long old_fdl = curwin->w_p_fdl;
4614 int old_fen = curwin->w_p_fen;
4615 #endif
4616 #ifdef FEAT_SPELL
4617 int undo = FALSE;
4618 #endif
4620 if (VIM_ISDIGIT(nchar))
4623 * "z123{nchar}": edit the count before obtaining {nchar}
4625 if (checkclearop(cap->oap))
4626 return;
4627 n = nchar - '0';
4628 for (;;)
4630 #ifdef USE_ON_FLY_SCROLL
4631 dont_scroll = TRUE; /* disallow scrolling here */
4632 #endif
4633 ++no_mapping;
4634 ++allow_keys; /* no mapping for nchar, but allow key codes */
4635 nchar = plain_vgetc();
4636 #ifdef FEAT_LANGMAP
4637 LANGMAP_ADJUST(nchar, TRUE);
4638 #endif
4639 --no_mapping;
4640 --allow_keys;
4641 #ifdef FEAT_CMDL_INFO
4642 (void)add_to_showcmd(nchar);
4643 #endif
4644 if (nchar == K_DEL || nchar == K_KDEL)
4645 n /= 10;
4646 else if (VIM_ISDIGIT(nchar))
4647 n = n * 10 + (nchar - '0');
4648 else if (nchar == CAR)
4650 #ifdef FEAT_GUI
4651 need_mouse_correct = TRUE;
4652 #endif
4653 win_setheight((int)n);
4654 break;
4656 else if (nchar == 'l'
4657 || nchar == 'h'
4658 || nchar == K_LEFT
4659 || nchar == K_RIGHT)
4661 cap->count1 = n ? n * cap->count1 : cap->count1;
4662 goto dozet;
4664 else
4666 clearopbeep(cap->oap);
4667 break;
4670 cap->oap->op_type = OP_NOP;
4671 return;
4674 dozet:
4675 if (
4676 #ifdef FEAT_FOLDING
4677 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4678 * and "zC" only in Visual mode. "zj" and "zk" are motion
4679 * commands. */
4680 cap->nchar != 'f' && cap->nchar != 'F'
4681 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4682 && cap->nchar != 'j' && cap->nchar != 'k'
4684 #endif
4685 checkclearop(cap->oap))
4686 return;
4689 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4690 * If line number given, set cursor.
4692 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4693 && cap->count0
4694 && cap->count0 != curwin->w_cursor.lnum)
4696 setpcmark();
4697 if (cap->count0 > curbuf->b_ml.ml_line_count)
4698 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4699 else
4700 curwin->w_cursor.lnum = cap->count0;
4701 check_cursor_col();
4704 switch (nchar)
4706 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4707 case '+':
4708 if (cap->count0 == 0)
4710 /* No count given: put cursor at the line below screen */
4711 validate_botline(); /* make sure w_botline is valid */
4712 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4713 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4714 else
4715 curwin->w_cursor.lnum = curwin->w_botline;
4717 /* FALLTHROUGH */
4718 case NL:
4719 case CAR:
4720 case K_KENTER:
4721 beginline(BL_WHITE | BL_FIX);
4722 /* FALLTHROUGH */
4724 case 't': scroll_cursor_top(0, TRUE);
4725 redraw_later(VALID);
4726 break;
4728 /* "z." and "zz": put cursor in middle of screen */
4729 case '.': beginline(BL_WHITE | BL_FIX);
4730 /* FALLTHROUGH */
4732 case 'z': scroll_cursor_halfway(TRUE);
4733 redraw_later(VALID);
4734 break;
4736 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4737 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4738 * when <count> is at bottom of window, and puts that one at
4739 * bottom of window. */
4740 if (cap->count0 != 0)
4742 scroll_cursor_bot(0, TRUE);
4743 curwin->w_cursor.lnum = curwin->w_topline;
4745 else if (curwin->w_topline == 1)
4746 curwin->w_cursor.lnum = 1;
4747 else
4748 curwin->w_cursor.lnum = curwin->w_topline - 1;
4749 /* FALLTHROUGH */
4750 case '-':
4751 beginline(BL_WHITE | BL_FIX);
4752 /* FALLTHROUGH */
4754 case 'b': scroll_cursor_bot(0, TRUE);
4755 redraw_later(VALID);
4756 break;
4758 /* "zH" - scroll screen right half-page */
4759 case 'H':
4760 cap->count1 *= W_WIDTH(curwin) / 2;
4761 /* FALLTHROUGH */
4763 /* "zh" - scroll screen to the right */
4764 case 'h':
4765 case K_LEFT:
4766 if (!curwin->w_p_wrap)
4768 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4769 curwin->w_leftcol = 0;
4770 else
4771 curwin->w_leftcol -= (colnr_T)cap->count1;
4772 leftcol_changed();
4774 break;
4776 /* "zL" - scroll screen left half-page */
4777 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4778 /* FALLTHROUGH */
4780 /* "zl" - scroll screen to the left */
4781 case 'l':
4782 case K_RIGHT:
4783 if (!curwin->w_p_wrap)
4785 /* scroll the window left */
4786 curwin->w_leftcol += (colnr_T)cap->count1;
4787 leftcol_changed();
4789 break;
4791 /* "zs" - scroll screen, cursor at the start */
4792 case 's': if (!curwin->w_p_wrap)
4794 #ifdef FEAT_FOLDING
4795 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4796 col = 0; /* like the cursor is in col 0 */
4797 else
4798 #endif
4799 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4800 if ((long)col > p_siso)
4801 col -= p_siso;
4802 else
4803 col = 0;
4804 if (curwin->w_leftcol != col)
4806 curwin->w_leftcol = col;
4807 redraw_later(NOT_VALID);
4810 break;
4812 /* "ze" - scroll screen, cursor at the end */
4813 case 'e': if (!curwin->w_p_wrap)
4815 #ifdef FEAT_FOLDING
4816 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4817 col = 0; /* like the cursor is in col 0 */
4818 else
4819 #endif
4820 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4821 n = W_WIDTH(curwin) - curwin_col_off();
4822 if ((long)col + p_siso < n)
4823 col = 0;
4824 else
4825 col = col + p_siso - n + 1;
4826 if (curwin->w_leftcol != col)
4828 curwin->w_leftcol = col;
4829 redraw_later(NOT_VALID);
4832 break;
4834 #ifdef FEAT_FOLDING
4835 /* "zF": create fold command */
4836 /* "zf": create fold operator */
4837 case 'F':
4838 case 'f': if (foldManualAllowed(TRUE))
4840 cap->nchar = 'f';
4841 nv_operator(cap);
4842 curwin->w_p_fen = TRUE;
4844 /* "zF" is like "zfzf" */
4845 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4847 nv_operator(cap);
4848 finish_op = TRUE;
4851 else
4852 clearopbeep(cap->oap);
4853 break;
4855 /* "zd": delete fold at cursor */
4856 /* "zD": delete fold at cursor recursively */
4857 case 'd':
4858 case 'D': if (foldManualAllowed(FALSE))
4860 if (VIsual_active)
4861 nv_operator(cap);
4862 else
4863 deleteFold(curwin->w_cursor.lnum,
4864 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4866 break;
4868 /* "zE": erease all folds */
4869 case 'E': if (foldmethodIsManual(curwin))
4871 clearFolding(curwin);
4872 changed_window_setting();
4874 else if (foldmethodIsMarker(curwin))
4875 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4876 TRUE, FALSE);
4877 else
4878 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4879 break;
4881 /* "zn": fold none: reset 'foldenable' */
4882 case 'n': curwin->w_p_fen = FALSE;
4883 break;
4885 /* "zN": fold Normal: set 'foldenable' */
4886 case 'N': curwin->w_p_fen = TRUE;
4887 break;
4889 /* "zi": invert folding: toggle 'foldenable' */
4890 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4891 break;
4893 /* "za": open closed fold or close open fold at cursor */
4894 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4895 openFold(curwin->w_cursor.lnum, cap->count1);
4896 else
4898 closeFold(curwin->w_cursor.lnum, cap->count1);
4899 curwin->w_p_fen = TRUE;
4901 break;
4903 /* "zA": open fold at cursor recursively */
4904 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4905 openFoldRecurse(curwin->w_cursor.lnum);
4906 else
4908 closeFoldRecurse(curwin->w_cursor.lnum);
4909 curwin->w_p_fen = TRUE;
4911 break;
4913 /* "zo": open fold at cursor or Visual area */
4914 case 'o': if (VIsual_active)
4915 nv_operator(cap);
4916 else
4917 openFold(curwin->w_cursor.lnum, cap->count1);
4918 break;
4920 /* "zO": open fold recursively */
4921 case 'O': if (VIsual_active)
4922 nv_operator(cap);
4923 else
4924 openFoldRecurse(curwin->w_cursor.lnum);
4925 break;
4927 /* "zc": close fold at cursor or Visual area */
4928 case 'c': if (VIsual_active)
4929 nv_operator(cap);
4930 else
4931 closeFold(curwin->w_cursor.lnum, cap->count1);
4932 curwin->w_p_fen = TRUE;
4933 break;
4935 /* "zC": close fold recursively */
4936 case 'C': if (VIsual_active)
4937 nv_operator(cap);
4938 else
4939 closeFoldRecurse(curwin->w_cursor.lnum);
4940 curwin->w_p_fen = TRUE;
4941 break;
4943 /* "zv": open folds at the cursor */
4944 case 'v': foldOpenCursor();
4945 break;
4947 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
4948 case 'x': curwin->w_p_fen = TRUE;
4949 newFoldLevel(); /* update right now */
4950 foldOpenCursor();
4951 break;
4953 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
4954 case 'X': curwin->w_p_fen = TRUE;
4955 old_fdl = -1; /* force an update */
4956 break;
4958 /* "zm": fold more */
4959 case 'm': if (curwin->w_p_fdl > 0)
4960 --curwin->w_p_fdl;
4961 old_fdl = -1; /* force an update */
4962 curwin->w_p_fen = TRUE;
4963 break;
4965 /* "zM": close all folds */
4966 case 'M': curwin->w_p_fdl = 0;
4967 old_fdl = -1; /* force an update */
4968 curwin->w_p_fen = TRUE;
4969 break;
4971 /* "zr": reduce folding */
4972 case 'r': ++curwin->w_p_fdl;
4973 break;
4975 /* "zR": open all folds */
4976 case 'R': curwin->w_p_fdl = getDeepestNesting();
4977 old_fdl = -1; /* force an update */
4978 break;
4980 case 'j': /* "zj" move to next fold downwards */
4981 case 'k': /* "zk" move to next fold upwards */
4982 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
4983 cap->count1) == FAIL)
4984 clearopbeep(cap->oap);
4985 break;
4987 #endif /* FEAT_FOLDING */
4989 #ifdef FEAT_SPELL
4990 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
4991 ++no_mapping;
4992 ++allow_keys; /* no mapping for nchar, but allow key codes */
4993 nchar = plain_vgetc();
4994 #ifdef FEAT_LANGMAP
4995 LANGMAP_ADJUST(nchar, TRUE);
4996 #endif
4997 --no_mapping;
4998 --allow_keys;
4999 #ifdef FEAT_CMDL_INFO
5000 (void)add_to_showcmd(nchar);
5001 #endif
5002 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5004 clearopbeep(cap->oap);
5005 break;
5007 undo = TRUE;
5008 /*FALLTHROUGH*/
5010 case 'g': /* "zg": add good word to word list */
5011 case 'w': /* "zw": add wrong word to word list */
5012 case 'G': /* "zG": add good word to temp word list */
5013 case 'W': /* "zW": add wrong word to temp word list */
5015 char_u *ptr = NULL;
5016 int len;
5018 if (checkclearop(cap->oap))
5019 break;
5020 # ifdef FEAT_VISUAL
5021 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5022 == FAIL)
5023 return;
5024 # endif
5025 if (ptr == NULL)
5027 pos_T pos = curwin->w_cursor;
5029 /* Find bad word under the cursor. */
5030 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
5031 if (len != 0 && curwin->w_cursor.col <= pos.col)
5032 ptr = ml_get_pos(&curwin->w_cursor);
5033 curwin->w_cursor = pos;
5036 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5037 FIND_IDENT)) == 0)
5038 return;
5039 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
5040 (nchar == 'G' || nchar == 'W')
5041 ? 0 : (int)cap->count1,
5042 undo);
5044 break;
5046 case '=': /* "z=": suggestions for a badly spelled word */
5047 if (!checkclearop(cap->oap))
5048 spell_suggest((int)cap->count0);
5049 break;
5050 #endif
5052 default: clearopbeep(cap->oap);
5055 #ifdef FEAT_FOLDING
5056 /* Redraw when 'foldenable' changed */
5057 if (old_fen != curwin->w_p_fen)
5059 # ifdef FEAT_DIFF
5060 win_T *wp;
5062 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5064 /* Adjust 'foldenable' in diff-synced windows. */
5065 FOR_ALL_WINDOWS(wp)
5067 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5069 wp->w_p_fen = curwin->w_p_fen;
5070 changed_window_setting_win(wp);
5074 # endif
5075 changed_window_setting();
5078 /* Redraw when 'foldlevel' changed. */
5079 if (old_fdl != curwin->w_p_fdl)
5080 newFoldLevel();
5081 #endif
5084 #ifdef FEAT_GUI
5086 * Vertical scrollbar movement.
5088 static void
5089 nv_ver_scrollbar(cap)
5090 cmdarg_T *cap;
5092 if (cap->oap->op_type != OP_NOP)
5093 clearopbeep(cap->oap);
5095 /* Even if an operator was pending, we still want to scroll */
5096 gui_do_scroll();
5100 * Horizontal scrollbar movement.
5102 static void
5103 nv_hor_scrollbar(cap)
5104 cmdarg_T *cap;
5106 if (cap->oap->op_type != OP_NOP)
5107 clearopbeep(cap->oap);
5109 /* Even if an operator was pending, we still want to scroll */
5110 gui_do_horiz_scroll();
5112 #endif
5114 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
5116 * Click in GUI tab.
5118 static void
5119 nv_tabline(cap)
5120 cmdarg_T *cap;
5122 if (cap->oap->op_type != OP_NOP)
5123 clearopbeep(cap->oap);
5125 /* Even if an operator was pending, we still want to jump tabs. */
5126 goto_tabpage(current_tab);
5130 * Selected item in tab line menu.
5132 static void
5133 nv_tabmenu(cap)
5134 cmdarg_T *cap;
5136 if (cap->oap->op_type != OP_NOP)
5137 clearopbeep(cap->oap);
5139 /* Even if an operator was pending, we still want to jump tabs. */
5140 handle_tabmenu();
5144 * Handle selecting an item of the GUI tab line menu.
5145 * Used in Normal and Insert mode.
5147 void
5148 handle_tabmenu()
5150 switch (current_tabmenu)
5152 case TABLINE_MENU_CLOSE:
5153 if (current_tab == 0)
5154 do_cmdline_cmd((char_u *)"tabclose");
5155 else
5157 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5158 current_tab);
5159 do_cmdline_cmd(IObuff);
5161 break;
5163 case TABLINE_MENU_NEW:
5164 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5165 current_tab > 0 ? current_tab - 1 : 999);
5166 do_cmdline_cmd(IObuff);
5167 break;
5169 case TABLINE_MENU_OPEN:
5170 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5171 current_tab > 0 ? current_tab - 1 : 999);
5172 do_cmdline_cmd(IObuff);
5173 break;
5176 #endif
5179 * "Q" command.
5181 static void
5182 nv_exmode(cap)
5183 cmdarg_T *cap;
5186 * Ignore 'Q' in Visual mode, just give a beep.
5188 #ifdef FEAT_VISUAL
5189 if (VIsual_active)
5190 vim_beep();
5191 else
5192 #endif
5193 if (!checkclearop(cap->oap))
5194 do_exmode(FALSE);
5198 * Handle a ":" command.
5200 static void
5201 nv_colon(cap)
5202 cmdarg_T *cap;
5204 int old_p_im;
5206 #ifdef FEAT_VISUAL
5207 if (VIsual_active)
5208 nv_operator(cap);
5209 else
5210 #endif
5212 if (cap->oap->op_type != OP_NOP)
5214 /* Using ":" as a movement is characterwise exclusive. */
5215 cap->oap->motion_type = MCHAR;
5216 cap->oap->inclusive = FALSE;
5218 else if (cap->count0)
5220 /* translate "count:" into ":.,.+(count - 1)" */
5221 stuffcharReadbuff('.');
5222 if (cap->count0 > 1)
5224 stuffReadbuff((char_u *)",.+");
5225 stuffnumReadbuff((long)cap->count0 - 1L);
5229 /* When typing, don't type below an old message */
5230 if (KeyTyped)
5231 compute_cmdrow();
5233 old_p_im = p_im;
5235 /* get a command line and execute it */
5236 do_cmdline(NULL, getexline, NULL,
5237 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5239 /* If 'insertmode' changed, enter or exit Insert mode */
5240 if (p_im != old_p_im)
5242 if (p_im)
5243 restart_edit = 'i';
5244 else
5245 restart_edit = 0;
5248 /* The start of the operator may have become invalid by the Ex
5249 * command. */
5250 if (cap->oap->op_type != OP_NOP
5251 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5252 || cap->oap->start.col >
5253 STRLEN(ml_get(cap->oap->start.lnum))))
5254 clearopbeep(cap->oap);
5259 * Handle CTRL-G command.
5261 static void
5262 nv_ctrlg(cap)
5263 cmdarg_T *cap;
5265 #ifdef FEAT_VISUAL
5266 if (VIsual_active) /* toggle Selection/Visual mode */
5268 VIsual_select = !VIsual_select;
5269 showmode();
5271 else
5272 #endif
5273 if (!checkclearop(cap->oap))
5274 /* print full name if count given or :cd used */
5275 fileinfo((int)cap->count0, FALSE, TRUE);
5279 * Handle CTRL-H <Backspace> command.
5281 static void
5282 nv_ctrlh(cap)
5283 cmdarg_T *cap;
5285 #ifdef FEAT_VISUAL
5286 if (VIsual_active && VIsual_select)
5288 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5289 v_visop(cap);
5291 else
5292 #endif
5293 nv_left(cap);
5297 * CTRL-L: clear screen and redraw.
5299 static void
5300 nv_clear(cap)
5301 cmdarg_T *cap;
5303 if (!checkclearop(cap->oap))
5305 #if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5307 * Right now, the BeBox doesn't seem to have an easy way to detect
5308 * window resizing, so we cheat and make the user detect it
5309 * manually with CTRL-L instead
5311 ui_get_shellsize();
5312 #endif
5313 #ifdef FEAT_SYN_HL
5314 /* Clear all syntax states to force resyncing. */
5315 syn_stack_free_all(curbuf);
5316 #endif
5317 redraw_later(CLEAR);
5322 * CTRL-O: In Select mode: switch to Visual mode for one command.
5323 * Otherwise: Go to older pcmark.
5325 static void
5326 nv_ctrlo(cap)
5327 cmdarg_T *cap;
5329 #ifdef FEAT_VISUAL
5330 if (VIsual_active && VIsual_select)
5332 VIsual_select = FALSE;
5333 showmode();
5334 restart_VIsual_select = 2; /* restart Select mode later */
5336 else
5337 #endif
5339 cap->count1 = -cap->count1;
5340 nv_pcmark(cap);
5345 * CTRL-^ command, short for ":e #"
5347 static void
5348 nv_hat(cap)
5349 cmdarg_T *cap;
5351 if (!checkclearopq(cap->oap))
5352 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5353 GETF_SETMARK|GETF_ALT, FALSE);
5357 * "Z" commands.
5359 static void
5360 nv_Zet(cap)
5361 cmdarg_T *cap;
5363 if (!checkclearopq(cap->oap))
5365 switch (cap->nchar)
5367 /* "ZZ": equivalent to ":x". */
5368 case 'Z': do_cmdline_cmd((char_u *)"x");
5369 break;
5371 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5372 case 'Q': do_cmdline_cmd((char_u *)"q!");
5373 break;
5375 default: clearopbeep(cap->oap);
5380 #if defined(FEAT_WINDOWS) || defined(PROTO)
5382 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5384 void
5385 do_nv_ident(c1, c2)
5386 int c1;
5387 int c2;
5389 oparg_T oa;
5390 cmdarg_T ca;
5392 clear_oparg(&oa);
5393 vim_memset(&ca, 0, sizeof(ca));
5394 ca.oap = &oa;
5395 ca.cmdchar = c1;
5396 ca.nchar = c2;
5397 nv_ident(&ca);
5399 #endif
5402 * Handle the commands that use the word under the cursor.
5403 * [g] CTRL-] :ta to current identifier
5404 * [g] 'K' run program for current identifier
5405 * [g] '*' / to current identifier or string
5406 * [g] '#' ? to current identifier or string
5407 * g ']' :tselect for current identifier
5409 static void
5410 nv_ident(cap)
5411 cmdarg_T *cap;
5413 char_u *ptr = NULL;
5414 char_u *buf;
5415 char_u *p;
5416 char_u *kp; /* value of 'keywordprg' */
5417 int kp_help; /* 'keywordprg' is ":help" */
5418 int n = 0; /* init for GCC */
5419 int cmdchar;
5420 int g_cmd; /* "g" command */
5421 char_u *aux_ptr;
5422 int isman;
5423 int isman_s;
5425 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5427 cmdchar = cap->nchar;
5428 g_cmd = TRUE;
5430 else
5432 cmdchar = cap->cmdchar;
5433 g_cmd = FALSE;
5436 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5437 cmdchar = '#';
5440 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5442 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5444 #ifdef FEAT_VISUAL
5445 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5446 return;
5447 #endif
5448 if (checkclearopq(cap->oap))
5449 return;
5452 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5453 (cmdchar == '*' || cmdchar == '#')
5454 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5456 clearop(cap->oap);
5457 return;
5460 /* Allocate buffer to put the command in. Inserting backslashes can
5461 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5462 * and some numbers. */
5463 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5464 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5465 || STRCMP(kp, ":help") == 0);
5466 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5467 if (buf == NULL)
5468 return;
5469 buf[0] = NUL;
5471 switch (cmdchar)
5473 case '*':
5474 case '#':
5476 * Put cursor at start of word, makes search skip the word
5477 * under the cursor.
5478 * Call setpcmark() first, so "*``" puts the cursor back where
5479 * it was.
5481 setpcmark();
5482 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5484 if (!g_cmd && vim_iswordp(ptr))
5485 STRCPY(buf, "\\<");
5486 no_smartcase = TRUE; /* don't use 'smartcase' now */
5487 break;
5489 case 'K':
5490 if (kp_help)
5491 STRCPY(buf, "he! ");
5492 else
5494 /* An external command will probably use an argument starting
5495 * with "-" as an option. To avoid trouble we skip the "-". */
5496 while (*ptr == '-' && n > 0)
5498 ++ptr;
5499 --n;
5501 if (n == 0)
5503 EMSG(_(e_noident)); /* found dashes only */
5504 vim_free(buf);
5505 return;
5508 /* When a count is given, turn it into a range. Is this
5509 * really what we want? */
5510 isman = (STRCMP(kp, "man") == 0);
5511 isman_s = (STRCMP(kp, "man -s") == 0);
5512 if (cap->count0 != 0 && !(isman || isman_s))
5513 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5515 STRCAT(buf, "! ");
5516 if (cap->count0 == 0 && isman_s)
5517 STRCAT(buf, "man");
5518 else
5519 STRCAT(buf, kp);
5520 STRCAT(buf, " ");
5521 if (cap->count0 != 0 && (isman || isman_s))
5523 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5524 STRCAT(buf, " ");
5527 break;
5529 case ']':
5530 #ifdef FEAT_CSCOPE
5531 if (p_cst)
5532 STRCPY(buf, "cstag ");
5533 else
5534 #endif
5535 STRCPY(buf, "ts ");
5536 break;
5538 default:
5539 if (curbuf->b_help)
5540 STRCPY(buf, "he! ");
5541 else if (g_cmd)
5542 STRCPY(buf, "tj ");
5543 else
5544 sprintf((char *)buf, "%ldta ", cap->count0);
5548 * Now grab the chars in the identifier
5550 if (cmdchar == 'K' && !kp_help)
5552 /* Escape the argument properly for a shell command */
5553 ptr = vim_strnsave(ptr, n);
5554 p = vim_strsave_shellescape(ptr, TRUE);
5555 vim_free(ptr);
5556 if (p == NULL)
5558 vim_free(buf);
5559 return;
5561 buf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5562 if (buf == NULL)
5564 vim_free(buf);
5565 vim_free(p);
5566 return;
5568 STRCAT(buf, p);
5569 vim_free(p);
5571 else
5573 if (cmdchar == '*')
5574 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5575 else if (cmdchar == '#')
5576 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
5577 else
5578 /* Don't escape spaces and Tabs in a tag with a backslash */
5579 aux_ptr = (char_u *)"\\|\"\n*?[";
5581 p = buf + STRLEN(buf);
5582 while (n-- > 0)
5584 /* put a backslash before \ and some others */
5585 if (vim_strchr(aux_ptr, *ptr) != NULL)
5586 *p++ = '\\';
5587 #ifdef FEAT_MBYTE
5588 /* When current byte is a part of multibyte character, copy all
5589 * bytes of that character. */
5590 if (has_mbyte)
5592 int i;
5593 int len = (*mb_ptr2len)(ptr) - 1;
5595 for (i = 0; i < len && n >= 1; ++i, --n)
5596 *p++ = *ptr++;
5598 #endif
5599 *p++ = *ptr++;
5601 *p = NUL;
5605 * Execute the command.
5607 if (cmdchar == '*' || cmdchar == '#')
5609 if (!g_cmd && (
5610 #ifdef FEAT_MBYTE
5611 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5612 #endif
5613 vim_iswordc(ptr[-1])))
5614 STRCAT(buf, "\\>");
5615 #ifdef FEAT_CMDHIST
5616 /* put pattern in search history */
5617 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5618 #endif
5619 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5621 else
5622 do_cmdline_cmd(buf);
5624 vim_free(buf);
5627 #if defined(FEAT_VISUAL) || defined(PROTO)
5629 * Get visually selected text, within one line only.
5630 * Returns FAIL if more than one line selected.
5633 get_visual_text(cap, pp, lenp)
5634 cmdarg_T *cap;
5635 char_u **pp; /* return: start of selected text */
5636 int *lenp; /* return: length of selected text */
5638 if (VIsual_mode != 'V')
5639 unadjust_for_sel();
5640 if (VIsual.lnum != curwin->w_cursor.lnum)
5642 if (cap != NULL)
5643 clearopbeep(cap->oap);
5644 return FAIL;
5646 if (VIsual_mode == 'V')
5648 *pp = ml_get_curline();
5649 *lenp = (int)STRLEN(*pp);
5651 else
5653 if (lt(curwin->w_cursor, VIsual))
5655 *pp = ml_get_pos(&curwin->w_cursor);
5656 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5658 else
5660 *pp = ml_get_pos(&VIsual);
5661 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5663 #ifdef FEAT_MBYTE
5664 if (has_mbyte)
5665 /* Correct the length to include the whole last character. */
5666 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
5667 #endif
5669 reset_VIsual_and_resel();
5670 return OK;
5672 #endif
5675 * CTRL-T: backwards in tag stack
5677 static void
5678 nv_tagpop(cap)
5679 cmdarg_T *cap;
5681 if (!checkclearopq(cap->oap))
5682 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5686 * Handle scrolling command 'H', 'L' and 'M'.
5688 static void
5689 nv_scroll(cap)
5690 cmdarg_T *cap;
5692 int used = 0;
5693 long n;
5694 #ifdef FEAT_FOLDING
5695 linenr_T lnum;
5696 #endif
5697 int half;
5699 cap->oap->motion_type = MLINE;
5700 setpcmark();
5702 if (cap->cmdchar == 'L')
5704 validate_botline(); /* make sure curwin->w_botline is valid */
5705 curwin->w_cursor.lnum = curwin->w_botline - 1;
5706 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5707 curwin->w_cursor.lnum = 1;
5708 else
5710 #ifdef FEAT_FOLDING
5711 if (hasAnyFolding(curwin))
5713 /* Count a fold for one screen line. */
5714 for (n = cap->count1 - 1; n > 0
5715 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5717 (void)hasFolding(curwin->w_cursor.lnum,
5718 &curwin->w_cursor.lnum, NULL);
5719 --curwin->w_cursor.lnum;
5722 else
5723 #endif
5724 curwin->w_cursor.lnum -= cap->count1 - 1;
5727 else
5729 if (cap->cmdchar == 'M')
5731 #ifdef FEAT_DIFF
5732 /* Don't count filler lines above the window. */
5733 used -= diff_check_fill(curwin, curwin->w_topline)
5734 - curwin->w_topfill;
5735 #endif
5736 validate_botline(); /* make sure w_empty_rows is valid */
5737 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5738 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5740 #ifdef FEAT_DIFF
5741 /* Count half he number of filler lines to be "below this
5742 * line" and half to be "above the next line". */
5743 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5744 + n) / 2 >= half)
5746 --n;
5747 break;
5749 #endif
5750 used += plines(curwin->w_topline + n);
5751 if (used >= half)
5752 break;
5753 #ifdef FEAT_FOLDING
5754 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5755 n = lnum - curwin->w_topline;
5756 #endif
5758 if (n > 0 && used > curwin->w_height)
5759 --n;
5761 else /* (cap->cmdchar == 'H') */
5763 n = cap->count1 - 1;
5764 #ifdef FEAT_FOLDING
5765 if (hasAnyFolding(curwin))
5767 /* Count a fold for one screen line. */
5768 lnum = curwin->w_topline;
5769 while (n-- > 0 && lnum < curwin->w_botline - 1)
5771 hasFolding(lnum, NULL, &lnum);
5772 ++lnum;
5774 n = lnum - curwin->w_topline;
5776 #endif
5778 curwin->w_cursor.lnum = curwin->w_topline + n;
5779 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5780 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5783 cursor_correct(); /* correct for 'so' */
5784 beginline(BL_SOL | BL_FIX);
5788 * Cursor right commands.
5790 static void
5791 nv_right(cap)
5792 cmdarg_T *cap;
5794 long n;
5795 #ifdef FEAT_VISUAL
5796 int PAST_LINE;
5797 #else
5798 # define PAST_LINE 0
5799 #endif
5801 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5803 /* <C-Right> and <S-Right> move a word or WORD right */
5804 if (mod_mask & MOD_MASK_CTRL)
5805 cap->arg = TRUE;
5806 nv_wordcmd(cap);
5807 return;
5810 cap->oap->motion_type = MCHAR;
5811 cap->oap->inclusive = FALSE;
5812 #ifdef FEAT_VISUAL
5813 PAST_LINE = (VIsual_active && *p_sel != 'o');
5815 # ifdef FEAT_VIRTUALEDIT
5817 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
5818 * (theoretically) infinitely long.
5820 if (virtual_active())
5821 PAST_LINE = 0;
5822 # endif
5823 #endif
5825 for (n = cap->count1; n > 0; --n)
5827 if ((!PAST_LINE && oneright() == FAIL)
5828 || (PAST_LINE && *ml_get_cursor() == NUL))
5831 * <Space> wraps to next line if 'whichwrap' has 's'.
5832 * 'l' wraps to next line if 'whichwrap' has 'l'.
5833 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
5835 if ( ((cap->cmdchar == ' '
5836 && vim_strchr(p_ww, 's') != NULL)
5837 || (cap->cmdchar == 'l'
5838 && vim_strchr(p_ww, 'l') != NULL)
5839 || (cap->cmdchar == K_RIGHT
5840 && vim_strchr(p_ww, '>') != NULL))
5841 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5843 /* When deleting we also count the NL as a character.
5844 * Set cap->oap->inclusive when last char in the line is
5845 * included, move to next line after that */
5846 if ( cap->oap->op_type != OP_NOP
5847 && !cap->oap->inclusive
5848 && !lineempty(curwin->w_cursor.lnum))
5849 cap->oap->inclusive = TRUE;
5850 else
5852 ++curwin->w_cursor.lnum;
5853 curwin->w_cursor.col = 0;
5854 #ifdef FEAT_VIRTUALEDIT
5855 curwin->w_cursor.coladd = 0;
5856 #endif
5857 curwin->w_set_curswant = TRUE;
5858 cap->oap->inclusive = FALSE;
5860 continue;
5862 if (cap->oap->op_type == OP_NOP)
5864 /* Only beep and flush if not moved at all */
5865 if (n == cap->count1)
5866 beep_flush();
5868 else
5870 if (!lineempty(curwin->w_cursor.lnum))
5871 cap->oap->inclusive = TRUE;
5873 break;
5875 #ifdef FEAT_VISUAL
5876 else if (PAST_LINE)
5878 curwin->w_set_curswant = TRUE;
5879 # ifdef FEAT_VIRTUALEDIT
5880 if (virtual_active())
5881 oneright();
5882 else
5883 # endif
5885 # ifdef FEAT_MBYTE
5886 if (has_mbyte)
5887 curwin->w_cursor.col +=
5888 (*mb_ptr2len)(ml_get_cursor());
5889 else
5890 # endif
5891 ++curwin->w_cursor.col;
5894 #endif
5896 #ifdef FEAT_FOLDING
5897 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5898 && cap->oap->op_type == OP_NOP)
5899 foldOpenCursor();
5900 #endif
5904 * Cursor left commands.
5906 * Returns TRUE when operator end should not be adjusted.
5908 static void
5909 nv_left(cap)
5910 cmdarg_T *cap;
5912 long n;
5914 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5916 /* <C-Left> and <S-Left> move a word or WORD left */
5917 if (mod_mask & MOD_MASK_CTRL)
5918 cap->arg = 1;
5919 nv_bck_word(cap);
5920 return;
5923 cap->oap->motion_type = MCHAR;
5924 cap->oap->inclusive = FALSE;
5925 for (n = cap->count1; n > 0; --n)
5927 if (oneleft() == FAIL)
5929 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
5930 * 'h' wraps to previous line if 'whichwrap' has 'h'.
5931 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
5933 if ( (((cap->cmdchar == K_BS
5934 || cap->cmdchar == Ctrl_H)
5935 && vim_strchr(p_ww, 'b') != NULL)
5936 || (cap->cmdchar == 'h'
5937 && vim_strchr(p_ww, 'h') != NULL)
5938 || (cap->cmdchar == K_LEFT
5939 && vim_strchr(p_ww, '<') != NULL))
5940 && curwin->w_cursor.lnum > 1)
5942 --(curwin->w_cursor.lnum);
5943 coladvance((colnr_T)MAXCOL);
5944 curwin->w_set_curswant = TRUE;
5946 /* When the NL before the first char has to be deleted we
5947 * put the cursor on the NUL after the previous line.
5948 * This is a very special case, be careful!
5949 * Don't adjust op_end now, otherwise it won't work. */
5950 if ( (cap->oap->op_type == OP_DELETE
5951 || cap->oap->op_type == OP_CHANGE)
5952 && !lineempty(curwin->w_cursor.lnum))
5954 if (*ml_get_cursor() != NUL)
5955 ++curwin->w_cursor.col;
5956 cap->retval |= CA_NO_ADJ_OP_END;
5958 continue;
5960 /* Only beep and flush if not moved at all */
5961 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
5962 beep_flush();
5963 break;
5966 #ifdef FEAT_FOLDING
5967 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5968 && cap->oap->op_type == OP_NOP)
5969 foldOpenCursor();
5970 #endif
5974 * Cursor up commands.
5975 * cap->arg is TRUE for "-": Move cursor to first non-blank.
5977 static void
5978 nv_up(cap)
5979 cmdarg_T *cap;
5981 if (mod_mask & MOD_MASK_SHIFT)
5983 /* <S-Up> is page up */
5984 cap->arg = BACKWARD;
5985 nv_page(cap);
5987 else
5989 cap->oap->motion_type = MLINE;
5990 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5991 clearopbeep(cap->oap);
5992 else if (cap->arg)
5993 beginline(BL_WHITE | BL_FIX);
5998 * Cursor down commands.
5999 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6001 static void
6002 nv_down(cap)
6003 cmdarg_T *cap;
6005 if (mod_mask & MOD_MASK_SHIFT)
6007 /* <S-Down> is page down */
6008 cap->arg = FORWARD;
6009 nv_page(cap);
6011 else
6012 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6013 /* In a quickfix window a <CR> jumps to the error under the cursor. */
6014 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
6015 if (curwin->w_llist_ref == NULL)
6016 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6017 else
6018 do_cmdline_cmd((char_u *)".ll"); /* location list window */
6019 else
6020 #endif
6022 #ifdef FEAT_CMDWIN
6023 /* In the cmdline window a <CR> executes the command. */
6024 if (cmdwin_type != 0 && cap->cmdchar == CAR)
6025 cmdwin_result = CAR;
6026 else
6027 #endif
6029 cap->oap->motion_type = MLINE;
6030 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6031 clearopbeep(cap->oap);
6032 else if (cap->arg)
6033 beginline(BL_WHITE | BL_FIX);
6038 #ifdef FEAT_SEARCHPATH
6040 * Grab the file name under the cursor and edit it.
6042 static void
6043 nv_gotofile(cap)
6044 cmdarg_T *cap;
6046 char_u *ptr;
6047 linenr_T lnum = -1;
6049 if (text_locked())
6051 clearopbeep(cap->oap);
6052 text_locked_msg();
6053 return;
6055 #ifdef FEAT_AUTOCMD
6056 if (curbuf_locked())
6058 clearop(cap->oap);
6059 return;
6061 #endif
6063 ptr = grab_file_name(cap->count1, &lnum);
6065 if (ptr != NULL)
6067 /* do autowrite if necessary */
6068 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6069 autowrite(curbuf, FALSE);
6070 setpcmark();
6071 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
6072 P_HID(curbuf) ? ECMD_HIDE : 0);
6073 if (cap->nchar == 'F' && lnum >= 0)
6075 curwin->w_cursor.lnum = lnum;
6076 check_cursor_lnum();
6077 beginline(BL_SOL | BL_FIX);
6079 vim_free(ptr);
6081 else
6082 clearop(cap->oap);
6084 #endif
6087 * <End> command: to end of current line or last line.
6089 static void
6090 nv_end(cap)
6091 cmdarg_T *cap;
6093 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
6095 cap->arg = TRUE;
6096 nv_goto(cap);
6097 cap->count1 = 1; /* to end of current line */
6099 nv_dollar(cap);
6103 * Handle the "$" command.
6105 static void
6106 nv_dollar(cap)
6107 cmdarg_T *cap;
6109 cap->oap->motion_type = MCHAR;
6110 cap->oap->inclusive = TRUE;
6111 #ifdef FEAT_VIRTUALEDIT
6112 /* In virtual mode when off the edge of a line and an operator
6113 * is pending (whew!) keep the cursor where it is.
6114 * Otherwise, send it to the end of the line. */
6115 if (!virtual_active() || gchar_cursor() != NUL
6116 || cap->oap->op_type == OP_NOP)
6117 #endif
6118 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6119 if (cursor_down((long)(cap->count1 - 1),
6120 cap->oap->op_type == OP_NOP) == FAIL)
6121 clearopbeep(cap->oap);
6122 #ifdef FEAT_FOLDING
6123 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6124 foldOpenCursor();
6125 #endif
6129 * Implementation of '?' and '/' commands.
6130 * If cap->arg is TRUE don't set PC mark.
6132 static void
6133 nv_search(cap)
6134 cmdarg_T *cap;
6136 oparg_T *oap = cap->oap;
6138 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6140 /* Translate "g??" to "g?g?" */
6141 cap->cmdchar = 'g';
6142 cap->nchar = '?';
6143 nv_operator(cap);
6144 return;
6147 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6149 if (cap->searchbuf == NULL)
6151 clearop(oap);
6152 return;
6155 normal_search(cap, cap->cmdchar, cap->searchbuf,
6156 (cap->arg ? 0 : SEARCH_MARK));
6160 * Handle "N" and "n" commands.
6161 * cap->arg is SEARCH_REV for "N", 0 for "n".
6163 static void
6164 nv_next(cap)
6165 cmdarg_T *cap;
6167 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6171 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6172 * Uses only cap->count1 and cap->oap from "cap".
6174 static void
6175 normal_search(cap, dir, pat, opt)
6176 cmdarg_T *cap;
6177 int dir;
6178 char_u *pat;
6179 int opt; /* extra flags for do_search() */
6181 int i;
6183 cap->oap->motion_type = MCHAR;
6184 cap->oap->inclusive = FALSE;
6185 cap->oap->use_reg_one = TRUE;
6186 curwin->w_set_curswant = TRUE;
6188 i = do_search(cap->oap, dir, pat, cap->count1,
6189 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
6190 if (i == 0)
6191 clearop(cap->oap);
6192 else
6194 if (i == 2)
6195 cap->oap->motion_type = MLINE;
6196 #ifdef FEAT_VIRTUALEDIT
6197 curwin->w_cursor.coladd = 0;
6198 #endif
6199 #ifdef FEAT_FOLDING
6200 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6201 foldOpenCursor();
6202 #endif
6205 /* "/$" will put the cursor after the end of the line, may need to
6206 * correct that here */
6207 check_cursor();
6211 * Character search commands.
6212 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6213 * ',' and FALSE for ';'.
6214 * cap->nchar is NUL for ',' and ';' (repeat the search)
6216 static void
6217 nv_csearch(cap)
6218 cmdarg_T *cap;
6220 int t_cmd;
6222 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6223 t_cmd = TRUE;
6224 else
6225 t_cmd = FALSE;
6227 cap->oap->motion_type = MCHAR;
6228 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6229 clearopbeep(cap->oap);
6230 else
6232 curwin->w_set_curswant = TRUE;
6233 #ifdef FEAT_VIRTUALEDIT
6234 /* Include a Tab for "tx" and for "dfx". */
6235 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6236 && (t_cmd || cap->oap->op_type != OP_NOP))
6238 colnr_T scol, ecol;
6240 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6241 curwin->w_cursor.coladd = ecol - scol;
6243 else
6244 curwin->w_cursor.coladd = 0;
6245 #endif
6246 #ifdef FEAT_VISUAL
6247 adjust_for_sel(cap);
6248 #endif
6249 #ifdef FEAT_FOLDING
6250 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6251 foldOpenCursor();
6252 #endif
6257 * "[" and "]" commands.
6258 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6260 static void
6261 nv_brackets(cap)
6262 cmdarg_T *cap;
6264 pos_T new_pos;
6265 pos_T prev_pos;
6266 pos_T *pos = NULL; /* init for GCC */
6267 pos_T old_pos; /* cursor position before command */
6268 int flag;
6269 long n;
6270 int findc;
6271 int c;
6273 cap->oap->motion_type = MCHAR;
6274 cap->oap->inclusive = FALSE;
6275 old_pos = curwin->w_cursor;
6276 #ifdef FEAT_VIRTUALEDIT
6277 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6278 #endif
6280 #ifdef FEAT_SEARCHPATH
6282 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6284 if (cap->nchar == 'f')
6285 nv_gotofile(cap);
6286 else
6287 #endif
6289 #ifdef FEAT_FIND_ID
6291 * Find the occurrence(s) of the identifier or define under cursor
6292 * in current and included files or jump to the first occurrence.
6294 * search list jump
6295 * fwd bwd fwd bwd fwd bwd
6296 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6297 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6299 if (vim_strchr((char_u *)
6300 #ifdef EBCDIC
6301 "iI\005dD\067",
6302 #else
6303 "iI\011dD\004",
6304 #endif
6305 cap->nchar) != NULL)
6307 char_u *ptr;
6308 int len;
6310 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6311 clearop(cap->oap);
6312 else
6314 find_pattern_in_path(ptr, 0, len, TRUE,
6315 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6316 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6317 cap->count1,
6318 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6319 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6320 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6321 (linenr_T)MAXLNUM);
6322 curwin->w_set_curswant = TRUE;
6325 else
6326 #endif
6329 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6330 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6331 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6332 * "[m" or "]m" search for prev/next start of (Java) method.
6333 * "[M" or "]M" search for prev/next end of (Java) method.
6335 if ( (cap->cmdchar == '['
6336 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6337 || (cap->cmdchar == ']'
6338 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6340 if (cap->nchar == '*')
6341 cap->nchar = '/';
6342 new_pos.lnum = 0;
6343 prev_pos.lnum = 0;
6344 if (cap->nchar == 'm' || cap->nchar == 'M')
6346 if (cap->cmdchar == '[')
6347 findc = '{';
6348 else
6349 findc = '}';
6350 n = 9999;
6352 else
6354 findc = cap->nchar;
6355 n = cap->count1;
6357 for ( ; n > 0; --n)
6359 if ((pos = findmatchlimit(cap->oap, findc,
6360 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6362 if (new_pos.lnum == 0) /* nothing found */
6364 if (cap->nchar != 'm' && cap->nchar != 'M')
6365 clearopbeep(cap->oap);
6367 else
6368 pos = &new_pos; /* use last one found */
6369 break;
6371 prev_pos = new_pos;
6372 curwin->w_cursor = *pos;
6373 new_pos = *pos;
6375 curwin->w_cursor = old_pos;
6378 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6379 * brought us to the match for "[m" and "]M" when inside a method.
6380 * Try finding the '{' or '}' we want to be at.
6381 * Also repeat for the given count.
6383 if (cap->nchar == 'm' || cap->nchar == 'M')
6385 /* norm is TRUE for "]M" and "[m" */
6386 int norm = ((findc == '{') == (cap->nchar == 'm'));
6388 n = cap->count1;
6389 /* found a match: we were inside a method */
6390 if (prev_pos.lnum != 0)
6392 pos = &prev_pos;
6393 curwin->w_cursor = prev_pos;
6394 if (norm)
6395 --n;
6397 else
6398 pos = NULL;
6399 while (n > 0)
6401 for (;;)
6403 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6405 /* if not found anything, that's an error */
6406 if (pos == NULL)
6407 clearopbeep(cap->oap);
6408 n = 0;
6409 break;
6411 c = gchar_cursor();
6412 if (c == '{' || c == '}')
6414 /* Must have found end/start of class: use it.
6415 * Or found the place to be at. */
6416 if ((c == findc && norm) || (n == 1 && !norm))
6418 new_pos = curwin->w_cursor;
6419 pos = &new_pos;
6420 n = 0;
6422 /* if no match found at all, we started outside of the
6423 * class and we're inside now. Just go on. */
6424 else if (new_pos.lnum == 0)
6426 new_pos = curwin->w_cursor;
6427 pos = &new_pos;
6429 /* found start/end of other method: go to match */
6430 else if ((pos = findmatchlimit(cap->oap, findc,
6431 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6432 0)) == NULL)
6433 n = 0;
6434 else
6435 curwin->w_cursor = *pos;
6436 break;
6439 --n;
6441 curwin->w_cursor = old_pos;
6442 if (pos == NULL && new_pos.lnum != 0)
6443 clearopbeep(cap->oap);
6445 if (pos != NULL)
6447 setpcmark();
6448 curwin->w_cursor = *pos;
6449 curwin->w_set_curswant = TRUE;
6450 #ifdef FEAT_FOLDING
6451 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6452 && cap->oap->op_type == OP_NOP)
6453 foldOpenCursor();
6454 #endif
6459 * "[[", "[]", "]]" and "][": move to start or end of function
6461 else if (cap->nchar == '[' || cap->nchar == ']')
6463 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6464 flag = '{';
6465 else
6466 flag = '}'; /* "][" or "[]" */
6468 curwin->w_set_curswant = TRUE;
6470 * Imitate strange Vi behaviour: When using "]]" with an operator
6471 * we also stop at '}'.
6473 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
6474 (cap->oap->op_type != OP_NOP
6475 && cap->arg == FORWARD && flag == '{')))
6476 clearopbeep(cap->oap);
6477 else
6479 if (cap->oap->op_type == OP_NOP)
6480 beginline(BL_WHITE | BL_FIX);
6481 #ifdef FEAT_FOLDING
6482 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6483 foldOpenCursor();
6484 #endif
6489 * "[p", "[P", "]P" and "]p": put with indent adjustment
6491 else if (cap->nchar == 'p' || cap->nchar == 'P')
6493 if (!checkclearop(cap->oap))
6495 prep_redo_cmd(cap);
6496 do_put(cap->oap->regname,
6497 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6498 cap->count1, PUT_FIXINDENT);
6503 * "['", "[`", "]'" and "]`": jump to next mark
6505 else if (cap->nchar == '\'' || cap->nchar == '`')
6507 pos = &curwin->w_cursor;
6508 for (n = cap->count1; n > 0; --n)
6510 prev_pos = *pos;
6511 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6512 cap->nchar == '\'');
6513 if (pos == NULL)
6514 break;
6516 if (pos == NULL)
6517 pos = &prev_pos;
6518 nv_cursormark(cap, cap->nchar == '\'', pos);
6521 #ifdef FEAT_MOUSE
6523 * [ or ] followed by a middle mouse click: put selected text with
6524 * indent adjustment. Any other button just does as usual.
6526 else if (cap->nchar >= K_LEFTMOUSE && cap->nchar <= K_RIGHTRELEASE)
6528 (void)do_mouse(cap->oap, cap->nchar,
6529 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6530 cap->count1, PUT_FIXINDENT);
6532 #endif /* FEAT_MOUSE */
6534 #ifdef FEAT_FOLDING
6536 * "[z" and "]z": move to start or end of open fold.
6538 else if (cap->nchar == 'z')
6540 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6541 cap->count1) == FAIL)
6542 clearopbeep(cap->oap);
6544 #endif
6546 #ifdef FEAT_DIFF
6548 * "[c" and "]c": move to next or previous diff-change.
6550 else if (cap->nchar == 'c')
6552 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6553 cap->count1) == FAIL)
6554 clearopbeep(cap->oap);
6556 #endif
6558 #ifdef FEAT_SPELL
6560 * "[s", "[S", "]s" and "]S": move to next spell error.
6562 else if (cap->nchar == 's' || cap->nchar == 'S')
6564 setpcmark();
6565 for (n = 0; n < cap->count1; ++n)
6566 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6567 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
6569 clearopbeep(cap->oap);
6570 break;
6572 # ifdef FEAT_FOLDING
6573 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6574 foldOpenCursor();
6575 # endif
6577 #endif
6579 /* Not a valid cap->nchar. */
6580 else
6581 clearopbeep(cap->oap);
6585 * Handle Normal mode "%" command.
6587 static void
6588 nv_percent(cap)
6589 cmdarg_T *cap;
6591 pos_T *pos;
6592 #ifdef FEAT_FOLDING
6593 linenr_T lnum = curwin->w_cursor.lnum;
6594 #endif
6596 cap->oap->inclusive = TRUE;
6597 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6599 if (cap->count0 > 100)
6600 clearopbeep(cap->oap);
6601 else
6603 cap->oap->motion_type = MLINE;
6604 setpcmark();
6605 /* Round up, so CTRL-G will give same value. Watch out for a
6606 * large line count, the line number must not go negative! */
6607 if (curbuf->b_ml.ml_line_count > 1000000)
6608 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6609 / 100L * cap->count0;
6610 else
6611 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6612 cap->count0 + 99L) / 100L;
6613 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6614 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6615 beginline(BL_SOL | BL_FIX);
6618 else /* "%" : go to matching paren */
6620 cap->oap->motion_type = MCHAR;
6621 cap->oap->use_reg_one = TRUE;
6622 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6623 clearopbeep(cap->oap);
6624 else
6626 setpcmark();
6627 curwin->w_cursor = *pos;
6628 curwin->w_set_curswant = TRUE;
6629 #ifdef FEAT_VIRTUALEDIT
6630 curwin->w_cursor.coladd = 0;
6631 #endif
6632 #ifdef FEAT_VISUAL
6633 adjust_for_sel(cap);
6634 #endif
6637 #ifdef FEAT_FOLDING
6638 if (cap->oap->op_type == OP_NOP
6639 && lnum != curwin->w_cursor.lnum
6640 && (fdo_flags & FDO_PERCENT)
6641 && KeyTyped)
6642 foldOpenCursor();
6643 #endif
6647 * Handle "(" and ")" commands.
6648 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6650 static void
6651 nv_brace(cap)
6652 cmdarg_T *cap;
6654 cap->oap->motion_type = MCHAR;
6655 cap->oap->use_reg_one = TRUE;
6656 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6657 cap->oap->inclusive = FALSE;
6658 curwin->w_set_curswant = TRUE;
6660 if (findsent(cap->arg, cap->count1) == FAIL)
6661 clearopbeep(cap->oap);
6662 else
6664 /* Don't leave the cursor on the NUL past end of line. */
6665 adjust_cursor(cap->oap);
6666 #ifdef FEAT_VIRTUALEDIT
6667 curwin->w_cursor.coladd = 0;
6668 #endif
6669 #ifdef FEAT_FOLDING
6670 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6671 foldOpenCursor();
6672 #endif
6677 * "m" command: Mark a position.
6679 static void
6680 nv_mark(cap)
6681 cmdarg_T *cap;
6683 if (!checkclearop(cap->oap))
6685 if (setmark(cap->nchar) == FAIL)
6686 clearopbeep(cap->oap);
6691 * "{" and "}" commands.
6692 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6694 static void
6695 nv_findpar(cap)
6696 cmdarg_T *cap;
6698 cap->oap->motion_type = MCHAR;
6699 cap->oap->inclusive = FALSE;
6700 cap->oap->use_reg_one = TRUE;
6701 curwin->w_set_curswant = TRUE;
6702 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
6703 clearopbeep(cap->oap);
6704 else
6706 #ifdef FEAT_VIRTUALEDIT
6707 curwin->w_cursor.coladd = 0;
6708 #endif
6709 #ifdef FEAT_FOLDING
6710 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6711 foldOpenCursor();
6712 #endif
6717 * "u" command: Undo or make lower case.
6719 static void
6720 nv_undo(cap)
6721 cmdarg_T *cap;
6723 if (cap->oap->op_type == OP_LOWER
6724 #ifdef FEAT_VISUAL
6725 || VIsual_active
6726 #endif
6729 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6730 cap->cmdchar = 'g';
6731 cap->nchar = 'u';
6732 nv_operator(cap);
6734 else
6735 nv_kundo(cap);
6739 * <Undo> command.
6741 static void
6742 nv_kundo(cap)
6743 cmdarg_T *cap;
6745 if (!checkclearopq(cap->oap))
6747 u_undo((int)cap->count1);
6748 curwin->w_set_curswant = TRUE;
6753 * Handle the "r" command.
6755 static void
6756 nv_replace(cap)
6757 cmdarg_T *cap;
6759 char_u *ptr;
6760 int had_ctrl_v;
6761 long n;
6763 if (checkclearop(cap->oap))
6764 return;
6766 /* get another character */
6767 if (cap->nchar == Ctrl_V)
6769 had_ctrl_v = Ctrl_V;
6770 cap->nchar = get_literal();
6771 /* Don't redo a multibyte character with CTRL-V. */
6772 if (cap->nchar > DEL)
6773 had_ctrl_v = NUL;
6775 else
6776 had_ctrl_v = NUL;
6778 /* Abort if the character is a special key. */
6779 if (IS_SPECIAL(cap->nchar))
6781 clearopbeep(cap->oap);
6782 return;
6785 #ifdef FEAT_VISUAL
6786 /* Visual mode "r" */
6787 if (VIsual_active)
6789 nv_operator(cap);
6790 return;
6792 #endif
6794 #ifdef FEAT_VIRTUALEDIT
6795 /* Break tabs, etc. */
6796 if (virtual_active())
6798 if (u_save_cursor() == FAIL)
6799 return;
6800 if (gchar_cursor() == NUL)
6802 /* Add extra space and put the cursor on the first one. */
6803 coladvance_force((colnr_T)(getviscol() + cap->count1));
6804 curwin->w_cursor.col -= cap->count1;
6806 else if (gchar_cursor() == TAB)
6807 coladvance_force(getviscol());
6809 #endif
6811 /* Abort if not enough characters to replace. */
6812 ptr = ml_get_cursor();
6813 if (STRLEN(ptr) < (unsigned)cap->count1
6814 #ifdef FEAT_MBYTE
6815 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6816 #endif
6819 clearopbeep(cap->oap);
6820 return;
6824 * Replacing with a TAB is done by edit() when it is complicated because
6825 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6826 * Other characters are done below to avoid problems with things like
6827 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6829 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6831 stuffnumReadbuff(cap->count1);
6832 stuffcharReadbuff('R');
6833 stuffcharReadbuff('\t');
6834 stuffcharReadbuff(ESC);
6835 return;
6838 /* save line for undo */
6839 if (u_save_cursor() == FAIL)
6840 return;
6842 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6845 * Replace character(s) by a single newline.
6846 * Strange vi behaviour: Only one newline is inserted.
6847 * Delete the characters here.
6848 * Insert the newline with an insert command, takes care of
6849 * autoindent. The insert command depends on being on the last
6850 * character of a line or not.
6852 #ifdef FEAT_MBYTE
6853 (void)del_chars(cap->count1, FALSE); /* delete the characters */
6854 #else
6855 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
6856 #endif
6857 stuffcharReadbuff('\r');
6858 stuffcharReadbuff(ESC);
6860 /* Give 'r' to edit(), to get the redo command right. */
6861 invoke_edit(cap, TRUE, 'r', FALSE);
6863 else
6865 prep_redo(cap->oap->regname, cap->count1,
6866 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
6868 curbuf->b_op_start = curwin->w_cursor;
6869 #ifdef FEAT_MBYTE
6870 if (has_mbyte)
6872 int old_State = State;
6874 if (cap->ncharC1 != 0)
6875 AppendCharToRedobuff(cap->ncharC1);
6876 if (cap->ncharC2 != 0)
6877 AppendCharToRedobuff(cap->ncharC2);
6879 /* This is slow, but it handles replacing a single-byte with a
6880 * multi-byte and the other way around. Also handles adding
6881 * composing characters for utf-8. */
6882 for (n = cap->count1; n > 0; --n)
6884 State = REPLACE;
6885 ins_char(cap->nchar);
6886 State = old_State;
6887 if (cap->ncharC1 != 0)
6888 ins_char(cap->ncharC1);
6889 if (cap->ncharC2 != 0)
6890 ins_char(cap->ncharC2);
6893 else
6894 #endif
6897 * Replace the characters within one line.
6899 for (n = cap->count1; n > 0; --n)
6902 * Get ptr again, because u_save and/or showmatch() will have
6903 * released the line. At the same time we let know that the
6904 * line will be changed.
6906 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
6907 ptr[curwin->w_cursor.col] = cap->nchar;
6908 if (p_sm && msg_silent == 0)
6909 showmatch(cap->nchar);
6910 ++curwin->w_cursor.col;
6912 #ifdef FEAT_NETBEANS_INTG
6913 if (usingNetbeans)
6915 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
6917 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
6918 (long)cap->count1);
6919 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
6920 &ptr[start], (int)cap->count1);
6922 #endif
6924 /* mark the buffer as changed and prepare for displaying */
6925 changed_bytes(curwin->w_cursor.lnum,
6926 (colnr_T)(curwin->w_cursor.col - cap->count1));
6928 --curwin->w_cursor.col; /* cursor on the last replaced char */
6929 #ifdef FEAT_MBYTE
6930 /* if the character on the left of the current cursor is a multi-byte
6931 * character, move two characters left */
6932 if (has_mbyte)
6933 mb_adjust_cursor();
6934 #endif
6935 curbuf->b_op_end = curwin->w_cursor;
6936 curwin->w_set_curswant = TRUE;
6937 set_last_insert(cap->nchar);
6941 #ifdef FEAT_VISUAL
6943 * 'o': Exchange start and end of Visual area.
6944 * 'O': same, but in block mode exchange left and right corners.
6946 static void
6947 v_swap_corners(cmdchar)
6948 int cmdchar;
6950 pos_T old_cursor;
6951 colnr_T left, right;
6953 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
6955 old_cursor = curwin->w_cursor;
6956 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
6957 curwin->w_cursor.lnum = VIsual.lnum;
6958 coladvance(left);
6959 VIsual = curwin->w_cursor;
6961 curwin->w_cursor.lnum = old_cursor.lnum;
6962 curwin->w_curswant = right;
6963 /* 'selection "exclusive" and cursor at right-bottom corner: move it
6964 * right one column */
6965 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
6966 ++curwin->w_curswant;
6967 coladvance(curwin->w_curswant);
6968 if (curwin->w_cursor.col == old_cursor.col
6969 #ifdef FEAT_VIRTUALEDIT
6970 && (!virtual_active()
6971 || curwin->w_cursor.coladd == old_cursor.coladd)
6972 #endif
6975 curwin->w_cursor.lnum = VIsual.lnum;
6976 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
6977 ++right;
6978 coladvance(right);
6979 VIsual = curwin->w_cursor;
6981 curwin->w_cursor.lnum = old_cursor.lnum;
6982 coladvance(left);
6983 curwin->w_curswant = left;
6986 else
6988 old_cursor = curwin->w_cursor;
6989 curwin->w_cursor = VIsual;
6990 VIsual = old_cursor;
6991 curwin->w_set_curswant = TRUE;
6994 #endif /* FEAT_VISUAL */
6997 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
6999 static void
7000 nv_Replace(cap)
7001 cmdarg_T *cap;
7003 #ifdef FEAT_VISUAL
7004 if (VIsual_active) /* "R" is replace lines */
7006 cap->cmdchar = 'c';
7007 cap->nchar = NUL;
7008 VIsual_mode = 'V';
7009 nv_operator(cap);
7011 else
7012 #endif
7013 if (!checkclearopq(cap->oap))
7015 if (!curbuf->b_p_ma)
7016 EMSG(_(e_modifiable));
7017 else
7019 #ifdef FEAT_VIRTUALEDIT
7020 if (virtual_active())
7021 coladvance(getviscol());
7022 #endif
7023 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7028 #ifdef FEAT_VREPLACE
7030 * "gr".
7032 static void
7033 nv_vreplace(cap)
7034 cmdarg_T *cap;
7036 # ifdef FEAT_VISUAL
7037 if (VIsual_active)
7039 cap->cmdchar = 'r';
7040 cap->nchar = cap->extra_char;
7041 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7043 else
7044 # endif
7045 if (!checkclearopq(cap->oap))
7047 if (!curbuf->b_p_ma)
7048 EMSG(_(e_modifiable));
7049 else
7051 if (cap->extra_char == Ctrl_V) /* get another character */
7052 cap->extra_char = get_literal();
7053 stuffcharReadbuff(cap->extra_char);
7054 stuffcharReadbuff(ESC);
7055 # ifdef FEAT_VIRTUALEDIT
7056 if (virtual_active())
7057 coladvance(getviscol());
7058 # endif
7059 invoke_edit(cap, TRUE, 'v', FALSE);
7063 #endif
7066 * Swap case for "~" command, when it does not work like an operator.
7068 static void
7069 n_swapchar(cap)
7070 cmdarg_T *cap;
7072 long n;
7073 pos_T startpos;
7074 int did_change = 0;
7075 #ifdef FEAT_NETBEANS_INTG
7076 pos_T pos;
7077 char_u *ptr;
7078 int count;
7079 #endif
7081 if (checkclearopq(cap->oap))
7082 return;
7084 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7086 clearopbeep(cap->oap);
7087 return;
7090 prep_redo_cmd(cap);
7092 if (u_save_cursor() == FAIL)
7093 return;
7095 startpos = curwin->w_cursor;
7096 #ifdef FEAT_NETBEANS_INTG
7097 pos = startpos;
7098 #endif
7099 for (n = cap->count1; n > 0; --n)
7101 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7102 inc_cursor();
7103 if (gchar_cursor() == NUL)
7105 if (vim_strchr(p_ww, '~') != NULL
7106 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7108 #ifdef FEAT_NETBEANS_INTG
7109 if (usingNetbeans)
7111 if (did_change)
7113 ptr = ml_get(pos.lnum);
7114 count = (int)STRLEN(ptr) - pos.col;
7115 netbeans_removed(curbuf, pos.lnum, pos.col,
7116 (long)count);
7117 netbeans_inserted(curbuf, pos.lnum, pos.col,
7118 &ptr[pos.col], count);
7120 pos.col = 0;
7121 pos.lnum++;
7123 #endif
7124 ++curwin->w_cursor.lnum;
7125 curwin->w_cursor.col = 0;
7126 if (n > 1)
7128 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7129 break;
7130 u_clearline();
7133 else
7134 break;
7137 #ifdef FEAT_NETBEANS_INTG
7138 if (did_change && usingNetbeans)
7140 ptr = ml_get(pos.lnum);
7141 count = curwin->w_cursor.col - pos.col;
7142 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7143 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
7145 #endif
7148 check_cursor();
7149 curwin->w_set_curswant = TRUE;
7150 if (did_change)
7152 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7153 0L);
7154 curbuf->b_op_start = startpos;
7155 curbuf->b_op_end = curwin->w_cursor;
7156 if (curbuf->b_op_end.col > 0)
7157 --curbuf->b_op_end.col;
7162 * Move cursor to mark.
7164 static void
7165 nv_cursormark(cap, flag, pos)
7166 cmdarg_T *cap;
7167 int flag;
7168 pos_T *pos;
7170 if (check_mark(pos) == FAIL)
7171 clearop(cap->oap);
7172 else
7174 if (cap->cmdchar == '\''
7175 || cap->cmdchar == '`'
7176 || cap->cmdchar == '['
7177 || cap->cmdchar == ']')
7178 setpcmark();
7179 curwin->w_cursor = *pos;
7180 if (flag)
7181 beginline(BL_WHITE | BL_FIX);
7182 else
7183 check_cursor();
7185 cap->oap->motion_type = flag ? MLINE : MCHAR;
7186 if (cap->cmdchar == '`')
7187 cap->oap->use_reg_one = TRUE;
7188 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7189 curwin->w_set_curswant = TRUE;
7192 #ifdef FEAT_VISUAL
7194 * Handle commands that are operators in Visual mode.
7196 static void
7197 v_visop(cap)
7198 cmdarg_T *cap;
7200 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7202 /* Uppercase means linewise, except in block mode, then "D" deletes till
7203 * the end of the line, and "C" replaces til EOL */
7204 if (isupper(cap->cmdchar))
7206 if (VIsual_mode != Ctrl_V)
7207 VIsual_mode = 'V';
7208 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7209 curwin->w_curswant = MAXCOL;
7211 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7212 nv_operator(cap);
7214 #endif
7217 * "s" and "S" commands.
7219 static void
7220 nv_subst(cap)
7221 cmdarg_T *cap;
7223 #ifdef FEAT_VISUAL
7224 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7226 if (cap->cmdchar == 'S')
7227 VIsual_mode = 'V';
7228 cap->cmdchar = 'c';
7229 nv_operator(cap);
7231 else
7232 #endif
7233 nv_optrans(cap);
7237 * Abbreviated commands.
7239 static void
7240 nv_abbrev(cap)
7241 cmdarg_T *cap;
7243 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7244 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7246 #ifdef FEAT_VISUAL
7247 /* in Visual mode these commands are operators */
7248 if (VIsual_active)
7249 v_visop(cap);
7250 else
7251 #endif
7252 nv_optrans(cap);
7256 * Translate a command into another command.
7258 static void
7259 nv_optrans(cap)
7260 cmdarg_T *cap;
7262 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7263 (char_u *)"d$", (char_u *)"c$",
7264 (char_u *)"cl", (char_u *)"cc",
7265 (char_u *)"yy", (char_u *)":s\r"};
7266 static char_u *str = (char_u *)"xXDCsSY&";
7268 if (!checkclearopq(cap->oap))
7270 /* In Vi "2D" doesn't delete the next line. Can't translate it
7271 * either, because "2." should also not use the count. */
7272 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7274 cap->oap->start = curwin->w_cursor;
7275 cap->oap->op_type = OP_DELETE;
7276 #ifdef FEAT_EVAL
7277 set_op_var(OP_DELETE);
7278 #endif
7279 cap->count1 = 1;
7280 nv_dollar(cap);
7281 finish_op = TRUE;
7282 ResetRedobuff();
7283 AppendCharToRedobuff('D');
7285 else
7287 if (cap->count0)
7288 stuffnumReadbuff(cap->count0);
7289 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7292 cap->opcount = 0;
7296 * "'" and "`" commands. Also for "g'" and "g`".
7297 * cap->arg is TRUE for "'" and "g'".
7299 static void
7300 nv_gomark(cap)
7301 cmdarg_T *cap;
7303 pos_T *pos;
7304 int c;
7305 #ifdef FEAT_FOLDING
7306 linenr_T lnum = curwin->w_cursor.lnum;
7307 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7308 #endif
7310 if (cap->cmdchar == 'g')
7311 c = cap->extra_char;
7312 else
7313 c = cap->nchar;
7314 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7315 if (pos == (pos_T *)-1) /* jumped to other file */
7317 if (cap->arg)
7319 check_cursor_lnum();
7320 beginline(BL_WHITE | BL_FIX);
7322 else
7323 check_cursor();
7325 else
7326 nv_cursormark(cap, cap->arg, pos);
7328 #ifdef FEAT_VIRTUALEDIT
7329 /* May need to clear the coladd that a mark includes. */
7330 if (!virtual_active())
7331 curwin->w_cursor.coladd = 0;
7332 #endif
7333 #ifdef FEAT_FOLDING
7334 if (cap->oap->op_type == OP_NOP
7335 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7336 && (fdo_flags & FDO_MARK)
7337 && old_KeyTyped)
7338 foldOpenCursor();
7339 #endif
7343 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7345 static void
7346 nv_pcmark(cap)
7347 cmdarg_T *cap;
7349 #ifdef FEAT_JUMPLIST
7350 pos_T *pos;
7351 # ifdef FEAT_FOLDING
7352 linenr_T lnum = curwin->w_cursor.lnum;
7353 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7354 # endif
7356 if (!checkclearopq(cap->oap))
7358 if (cap->cmdchar == 'g')
7359 pos = movechangelist((int)cap->count1);
7360 else
7361 pos = movemark((int)cap->count1);
7362 if (pos == (pos_T *)-1) /* jump to other file */
7364 curwin->w_set_curswant = TRUE;
7365 check_cursor();
7367 else if (pos != NULL) /* can jump */
7368 nv_cursormark(cap, FALSE, pos);
7369 else if (cap->cmdchar == 'g')
7371 if (curbuf->b_changelistlen == 0)
7372 EMSG(_("E664: changelist is empty"));
7373 else if (cap->count1 < 0)
7374 EMSG(_("E662: At start of changelist"));
7375 else
7376 EMSG(_("E663: At end of changelist"));
7378 else
7379 clearopbeep(cap->oap);
7380 # ifdef FEAT_FOLDING
7381 if (cap->oap->op_type == OP_NOP
7382 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7383 && (fdo_flags & FDO_MARK)
7384 && old_KeyTyped)
7385 foldOpenCursor();
7386 # endif
7388 #else
7389 clearopbeep(cap->oap);
7390 #endif
7394 * Handle '"' command.
7396 static void
7397 nv_regname(cap)
7398 cmdarg_T *cap;
7400 if (checkclearop(cap->oap))
7401 return;
7402 #ifdef FEAT_EVAL
7403 if (cap->nchar == '=')
7404 cap->nchar = get_expr_register();
7405 #endif
7406 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7408 cap->oap->regname = cap->nchar;
7409 cap->opcount = cap->count0; /* remember count before '"' */
7410 #ifdef FEAT_EVAL
7411 set_reg_var(cap->oap->regname);
7412 #endif
7414 else
7415 clearopbeep(cap->oap);
7418 #ifdef FEAT_VISUAL
7420 * Handle "v", "V" and "CTRL-V" commands.
7421 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7422 * is TRUE.
7423 * Handle CTRL-Q just like CTRL-V.
7425 static void
7426 nv_visual(cap)
7427 cmdarg_T *cap;
7429 if (cap->cmdchar == Ctrl_Q)
7430 cap->cmdchar = Ctrl_V;
7432 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7433 * characterwise, linewise, or blockwise. */
7434 if (cap->oap->op_type != OP_NOP)
7436 cap->oap->motion_force = cap->cmdchar;
7437 finish_op = FALSE; /* operator doesn't finish now but later */
7438 return;
7441 VIsual_select = cap->arg;
7442 if (VIsual_active) /* change Visual mode */
7444 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7445 end_visual_mode();
7446 else /* toggle char/block mode */
7447 { /* or char/line mode */
7448 VIsual_mode = cap->cmdchar;
7449 showmode();
7451 redraw_curbuf_later(INVERTED); /* update the inversion */
7453 else /* start Visual mode */
7455 check_visual_highlight();
7456 if (cap->count0) /* use previously selected part */
7458 if (resel_VIsual_mode == NUL) /* there is none */
7460 beep_flush();
7461 return;
7463 VIsual = curwin->w_cursor;
7465 VIsual_active = TRUE;
7466 VIsual_reselect = TRUE;
7467 if (!cap->arg)
7468 /* start Select mode when 'selectmode' contains "cmd" */
7469 may_start_select('c');
7470 #ifdef FEAT_MOUSE
7471 setmouse();
7472 #endif
7473 if (p_smd && msg_silent == 0)
7474 redraw_cmdline = TRUE; /* show visual mode later */
7476 * For V and ^V, we multiply the number of lines even if there
7477 * was only one -- webb
7479 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7481 curwin->w_cursor.lnum +=
7482 resel_VIsual_line_count * cap->count0 - 1;
7483 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7484 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7486 VIsual_mode = resel_VIsual_mode;
7487 if (VIsual_mode == 'v')
7489 if (resel_VIsual_line_count <= 1)
7490 curwin->w_cursor.col += resel_VIsual_col * cap->count0 - 1;
7491 else
7492 curwin->w_cursor.col = resel_VIsual_col;
7493 check_cursor_col();
7495 if (resel_VIsual_col == MAXCOL)
7497 curwin->w_curswant = MAXCOL;
7498 coladvance((colnr_T)MAXCOL);
7500 else if (VIsual_mode == Ctrl_V)
7502 validate_virtcol();
7503 curwin->w_curswant = curwin->w_virtcol
7504 + resel_VIsual_col * cap->count0 - 1;
7505 coladvance(curwin->w_curswant);
7507 else
7508 curwin->w_set_curswant = TRUE;
7509 redraw_curbuf_later(INVERTED); /* show the inversion */
7511 else
7513 if (!cap->arg)
7514 /* start Select mode when 'selectmode' contains "cmd" */
7515 may_start_select('c');
7516 n_start_visual_mode(cap->cmdchar);
7522 * Start selection for Shift-movement keys.
7524 void
7525 start_selection()
7527 /* if 'selectmode' contains "key", start Select mode */
7528 may_start_select('k');
7529 n_start_visual_mode('v');
7533 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7535 void
7536 may_start_select(c)
7537 int c;
7539 VIsual_select = (stuff_empty() && typebuf_typed()
7540 && (vim_strchr(p_slm, c) != NULL));
7544 * Start Visual mode "c".
7545 * Should set VIsual_select before calling this.
7547 static void
7548 n_start_visual_mode(c)
7549 int c;
7551 VIsual_mode = c;
7552 VIsual_active = TRUE;
7553 VIsual_reselect = TRUE;
7554 #ifdef FEAT_VIRTUALEDIT
7555 /* Corner case: the 0 position in a tab may change when going into
7556 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7558 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7559 coladvance(curwin->w_virtcol);
7560 #endif
7561 VIsual = curwin->w_cursor;
7563 #ifdef FEAT_FOLDING
7564 foldAdjustVisual();
7565 #endif
7567 #ifdef FEAT_MOUSE
7568 setmouse();
7569 #endif
7570 if (p_smd && msg_silent == 0)
7571 redraw_cmdline = TRUE; /* show visual mode later */
7572 #ifdef FEAT_CLIPBOARD
7573 /* Make sure the clipboard gets updated. Needed because start and
7574 * end may still be the same, and the selection needs to be owned */
7575 clip_star.vmode = NUL;
7576 #endif
7578 /* Only need to redraw this line, unless still need to redraw an old
7579 * Visual area (when 'lazyredraw' is set). */
7580 if (curwin->w_redr_type < INVERTED)
7582 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7583 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7587 #endif /* FEAT_VISUAL */
7590 * CTRL-W: Window commands
7592 static void
7593 nv_window(cap)
7594 cmdarg_T *cap;
7596 #ifdef FEAT_WINDOWS
7597 if (!checkclearop(cap->oap))
7598 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7599 #else
7600 (void)checkclearop(cap->oap);
7601 #endif
7605 * CTRL-Z: Suspend
7607 static void
7608 nv_suspend(cap)
7609 cmdarg_T *cap;
7611 clearop(cap->oap);
7612 #ifdef FEAT_VISUAL
7613 if (VIsual_active)
7614 end_visual_mode(); /* stop Visual mode */
7615 #endif
7616 do_cmdline_cmd((char_u *)"st");
7620 * Commands starting with "g".
7622 static void
7623 nv_g_cmd(cap)
7624 cmdarg_T *cap;
7626 oparg_T *oap = cap->oap;
7627 #ifdef FEAT_VISUAL
7628 pos_T tpos;
7629 #endif
7630 int i;
7631 int flag = FALSE;
7633 switch (cap->nchar)
7635 #ifdef MEM_PROFILE
7637 * "g^A": dump log of used memory.
7639 case Ctrl_A:
7640 vim_mem_profile_dump();
7641 break;
7642 #endif
7644 #ifdef FEAT_VREPLACE
7646 * "gR": Enter virtual replace mode.
7648 case 'R':
7649 cap->arg = TRUE;
7650 nv_Replace(cap);
7651 break;
7653 case 'r':
7654 nv_vreplace(cap);
7655 break;
7656 #endif
7658 case '&':
7659 do_cmdline_cmd((char_u *)"%s//~/&");
7660 break;
7662 #ifdef FEAT_VISUAL
7664 * "gv": Reselect the previous Visual area. If Visual already active,
7665 * exchange previous and current Visual area.
7667 case 'v':
7668 if (checkclearop(oap))
7669 break;
7671 if ( curbuf->b_visual.vi_start.lnum == 0
7672 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7673 || curbuf->b_visual.vi_end.lnum == 0)
7674 beep_flush();
7675 else
7677 /* set w_cursor to the start of the Visual area, tpos to the end */
7678 if (VIsual_active)
7680 i = VIsual_mode;
7681 VIsual_mode = curbuf->b_visual.vi_mode;
7682 curbuf->b_visual.vi_mode = i;
7683 # ifdef FEAT_EVAL
7684 curbuf->b_visual_mode_eval = i;
7685 # endif
7686 i = curwin->w_curswant;
7687 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7688 curbuf->b_visual.vi_curswant = i;
7690 tpos = curbuf->b_visual.vi_end;
7691 curbuf->b_visual.vi_end = curwin->w_cursor;
7692 curwin->w_cursor = curbuf->b_visual.vi_start;
7693 curbuf->b_visual.vi_start = VIsual;
7695 else
7697 VIsual_mode = curbuf->b_visual.vi_mode;
7698 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7699 tpos = curbuf->b_visual.vi_end;
7700 curwin->w_cursor = curbuf->b_visual.vi_start;
7703 VIsual_active = TRUE;
7704 VIsual_reselect = TRUE;
7706 /* Set Visual to the start and w_cursor to the end of the Visual
7707 * area. Make sure they are on an existing character. */
7708 check_cursor();
7709 VIsual = curwin->w_cursor;
7710 curwin->w_cursor = tpos;
7711 check_cursor();
7712 update_topline();
7714 * When called from normal "g" command: start Select mode when
7715 * 'selectmode' contains "cmd". When called for K_SELECT, always
7716 * start Select mode.
7718 if (cap->arg)
7719 VIsual_select = TRUE;
7720 else
7721 may_start_select('c');
7722 #ifdef FEAT_MOUSE
7723 setmouse();
7724 #endif
7725 #ifdef FEAT_CLIPBOARD
7726 /* Make sure the clipboard gets updated. Needed because start and
7727 * end are still the same, and the selection needs to be owned */
7728 clip_star.vmode = NUL;
7729 #endif
7730 redraw_curbuf_later(INVERTED);
7731 showmode();
7733 break;
7735 * "gV": Don't reselect the previous Visual area after a Select mode
7736 * mapping of menu.
7738 case 'V':
7739 VIsual_reselect = FALSE;
7740 break;
7743 * "gh": start Select mode.
7744 * "gH": start Select line mode.
7745 * "g^H": start Select block mode.
7747 case K_BS:
7748 cap->nchar = Ctrl_H;
7749 /* FALLTHROUGH */
7750 case 'h':
7751 case 'H':
7752 case Ctrl_H:
7753 # ifdef EBCDIC
7754 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7755 if (cap->nchar == Ctrl_H)
7756 cap->cmdchar = Ctrl_V;
7757 else
7758 # endif
7759 cap->cmdchar = cap->nchar + ('v' - 'h');
7760 cap->arg = TRUE;
7761 nv_visual(cap);
7762 break;
7763 #endif /* FEAT_VISUAL */
7766 * "gj" and "gk" two new funny movement keys -- up and down
7767 * movement based on *screen* line rather than *file* line.
7769 case 'j':
7770 case K_DOWN:
7771 /* with 'nowrap' it works just like the normal "j" command; also when
7772 * in a closed fold */
7773 if (!curwin->w_p_wrap
7774 #ifdef FEAT_FOLDING
7775 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7776 #endif
7779 oap->motion_type = MLINE;
7780 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7782 else
7783 i = nv_screengo(oap, FORWARD, cap->count1);
7784 if (i == FAIL)
7785 clearopbeep(oap);
7786 break;
7788 case 'k':
7789 case K_UP:
7790 /* with 'nowrap' it works just like the normal "k" command; also when
7791 * in a closed fold */
7792 if (!curwin->w_p_wrap
7793 #ifdef FEAT_FOLDING
7794 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7795 #endif
7798 oap->motion_type = MLINE;
7799 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7801 else
7802 i = nv_screengo(oap, BACKWARD, cap->count1);
7803 if (i == FAIL)
7804 clearopbeep(oap);
7805 break;
7808 * "gJ": join two lines without inserting a space.
7810 case 'J':
7811 nv_join(cap);
7812 break;
7815 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7816 * "gm": middle of "g0" and "g$".
7818 case '^':
7819 flag = TRUE;
7820 /* FALLTHROUGH */
7822 case '0':
7823 case 'm':
7824 case K_HOME:
7825 case K_KHOME:
7826 oap->motion_type = MCHAR;
7827 oap->inclusive = FALSE;
7828 if (curwin->w_p_wrap
7829 #ifdef FEAT_VERTSPLIT
7830 && curwin->w_width != 0
7831 #endif
7834 int width1 = W_WIDTH(curwin) - curwin_col_off();
7835 int width2 = width1 + curwin_col_off2();
7837 validate_virtcol();
7838 i = 0;
7839 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
7840 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
7842 else
7843 i = curwin->w_leftcol;
7844 /* Go to the middle of the screen line. When 'number' is on and lines
7845 * are wrapping the middle can be more to the left.*/
7846 if (cap->nchar == 'm')
7847 i += (W_WIDTH(curwin) - curwin_col_off()
7848 + ((curwin->w_p_wrap && i > 0)
7849 ? curwin_col_off2() : 0)) / 2;
7850 coladvance((colnr_T)i);
7851 if (flag)
7854 i = gchar_cursor();
7855 while (vim_iswhite(i) && oneright() == OK);
7857 curwin->w_set_curswant = TRUE;
7858 break;
7860 case '_':
7861 /* "g_": to the last non-blank character in the line or <count> lines
7862 * downward. */
7863 cap->oap->motion_type = MCHAR;
7864 cap->oap->inclusive = TRUE;
7865 curwin->w_curswant = MAXCOL;
7866 if (cursor_down((long)(cap->count1 - 1),
7867 cap->oap->op_type == OP_NOP) == FAIL)
7868 clearopbeep(cap->oap);
7869 else
7871 char_u *ptr = ml_get_curline();
7873 /* In Visual mode we may end up after the line. */
7874 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
7875 --curwin->w_cursor.col;
7877 /* Decrease the cursor column until it's on a non-blank. */
7878 while (curwin->w_cursor.col > 0
7879 && vim_iswhite(ptr[curwin->w_cursor.col]))
7880 --curwin->w_cursor.col;
7881 curwin->w_set_curswant = TRUE;
7883 break;
7885 case '$':
7886 case K_END:
7887 case K_KEND:
7889 int col_off = curwin_col_off();
7891 oap->motion_type = MCHAR;
7892 oap->inclusive = TRUE;
7893 if (curwin->w_p_wrap
7894 #ifdef FEAT_VERTSPLIT
7895 && curwin->w_width != 0
7896 #endif
7899 curwin->w_curswant = MAXCOL; /* so we stay at the end */
7900 if (cap->count1 == 1)
7902 int width1 = W_WIDTH(curwin) - col_off;
7903 int width2 = width1 + curwin_col_off2();
7905 validate_virtcol();
7906 i = width1 - 1;
7907 if (curwin->w_virtcol >= (colnr_T)width1)
7908 i += ((curwin->w_virtcol - width1) / width2 + 1)
7909 * width2;
7910 coladvance((colnr_T)i);
7911 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
7912 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
7915 * Check for landing on a character that got split at
7916 * the end of the line. We do not want to advance to
7917 * the next screen line.
7919 validate_virtcol();
7920 if (curwin->w_virtcol > (colnr_T)i)
7921 --curwin->w_cursor.col;
7923 #endif
7925 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
7926 clearopbeep(oap);
7928 else
7930 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
7931 coladvance((colnr_T)i);
7933 /* Make sure we stick in this column. */
7934 validate_virtcol();
7935 curwin->w_curswant = curwin->w_virtcol;
7936 curwin->w_set_curswant = FALSE;
7939 break;
7942 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
7944 case '*':
7945 case '#':
7946 #if POUND != '#'
7947 case POUND: /* pound sign (sometimes equal to '#') */
7948 #endif
7949 case Ctrl_RSB: /* :tag or :tselect for current identifier */
7950 case ']': /* :tselect for current identifier */
7951 nv_ident(cap);
7952 break;
7955 * ge and gE: go back to end of word
7957 case 'e':
7958 case 'E':
7959 oap->motion_type = MCHAR;
7960 curwin->w_set_curswant = TRUE;
7961 oap->inclusive = TRUE;
7962 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
7963 clearopbeep(oap);
7964 break;
7967 * "g CTRL-G": display info about cursor position
7969 case Ctrl_G:
7970 cursor_pos_info();
7971 break;
7974 * "gi": start Insert at the last position.
7976 case 'i':
7977 if (curbuf->b_last_insert.lnum != 0)
7979 curwin->w_cursor = curbuf->b_last_insert;
7980 check_cursor_lnum();
7981 i = (int)STRLEN(ml_get_curline());
7982 if (curwin->w_cursor.col > (colnr_T)i)
7984 #ifdef FEAT_VIRTUALEDIT
7985 if (virtual_active())
7986 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
7987 #endif
7988 curwin->w_cursor.col = i;
7991 cap->cmdchar = 'i';
7992 nv_edit(cap);
7993 break;
7996 * "gI": Start insert in column 1.
7998 case 'I':
7999 beginline(0);
8000 if (!checkclearopq(oap))
8001 invoke_edit(cap, FALSE, 'g', FALSE);
8002 break;
8004 #ifdef FEAT_SEARCHPATH
8006 * "gf": goto file, edit file under cursor
8007 * "]f" and "[f": can also be used.
8009 case 'f':
8010 case 'F':
8011 nv_gotofile(cap);
8012 break;
8013 #endif
8015 /* "g'm" and "g`m": jump to mark without setting pcmark */
8016 case '\'':
8017 cap->arg = TRUE;
8018 /*FALLTHROUGH*/
8019 case '`':
8020 nv_gomark(cap);
8021 break;
8024 * "gs": Goto sleep.
8026 case 's':
8027 do_sleep(cap->count1 * 1000L);
8028 break;
8031 * "ga": Display the ascii value of the character under the
8032 * cursor. It is displayed in decimal, hex, and octal. -- webb
8034 case 'a':
8035 do_ascii(NULL);
8036 break;
8038 #ifdef FEAT_MBYTE
8040 * "g8": Display the bytes used for the UTF-8 character under the
8041 * cursor. It is displayed in hex.
8042 * "8g8" finds illegal byte sequence.
8044 case '8':
8045 if (cap->count0 == 8)
8046 utf_find_illegal();
8047 else
8048 show_utf8();
8049 break;
8050 #endif
8052 case '<':
8053 show_sb_text();
8054 break;
8057 * "gg": Goto the first line in file. With a count it goes to
8058 * that line number like for "G". -- webb
8060 case 'g':
8061 cap->arg = FALSE;
8062 nv_goto(cap);
8063 break;
8066 * Two-character operators:
8067 * "gq" Format text
8068 * "gw" Format text and keep cursor position
8069 * "g~" Toggle the case of the text.
8070 * "gu" Change text to lower case.
8071 * "gU" Change text to upper case.
8072 * "g?" rot13 encoding
8073 * "g@" call 'operatorfunc'
8075 case 'q':
8076 case 'w':
8077 oap->cursor_start = curwin->w_cursor;
8078 /*FALLTHROUGH*/
8079 case '~':
8080 case 'u':
8081 case 'U':
8082 case '?':
8083 case '@':
8084 nv_operator(cap);
8085 break;
8088 * "gd": Find first occurrence of pattern under the cursor in the
8089 * current function
8090 * "gD": idem, but in the current file.
8092 case 'd':
8093 case 'D':
8094 nv_gd(oap, cap->nchar, (int)cap->count0);
8095 break;
8097 #ifdef FEAT_MOUSE
8099 * g<*Mouse> : <C-*mouse>
8101 case K_MIDDLEMOUSE:
8102 case K_MIDDLEDRAG:
8103 case K_MIDDLERELEASE:
8104 case K_LEFTMOUSE:
8105 case K_LEFTDRAG:
8106 case K_LEFTRELEASE:
8107 case K_RIGHTMOUSE:
8108 case K_RIGHTDRAG:
8109 case K_RIGHTRELEASE:
8110 case K_X1MOUSE:
8111 case K_X1DRAG:
8112 case K_X1RELEASE:
8113 case K_X2MOUSE:
8114 case K_X2DRAG:
8115 case K_X2RELEASE:
8116 mod_mask = MOD_MASK_CTRL;
8117 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8118 break;
8119 #endif
8121 case K_IGNORE:
8122 break;
8125 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8127 case 'p':
8128 case 'P':
8129 nv_put(cap);
8130 break;
8132 #ifdef FEAT_BYTEOFF
8133 /* "go": goto byte count from start of buffer */
8134 case 'o':
8135 goto_byte(cap->count0);
8136 break;
8137 #endif
8139 /* "gQ": improved Ex mode */
8140 case 'Q':
8141 if (text_locked())
8143 clearopbeep(cap->oap);
8144 text_locked_msg();
8145 break;
8148 if (!checkclearopq(oap))
8149 do_exmode(TRUE);
8150 break;
8152 #ifdef FEAT_JUMPLIST
8153 case ',':
8154 nv_pcmark(cap);
8155 break;
8157 case ';':
8158 cap->count1 = -cap->count1;
8159 nv_pcmark(cap);
8160 break;
8161 #endif
8163 #ifdef FEAT_WINDOWS
8164 case 't':
8165 goto_tabpage((int)cap->count0);
8166 break;
8167 case 'T':
8168 goto_tabpage(-(int)cap->count1);
8169 break;
8170 #endif
8172 case '+':
8173 case '-': /* "g+" and "g-": undo or redo along the timeline */
8174 if (!checkclearopq(oap))
8175 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
8176 FALSE, FALSE);
8177 break;
8179 default:
8180 clearopbeep(oap);
8181 break;
8186 * Handle "o" and "O" commands.
8188 static void
8189 n_opencmd(cap)
8190 cmdarg_T *cap;
8192 if (!checkclearopq(cap->oap))
8194 #ifdef FEAT_FOLDING
8195 if (cap->cmdchar == 'O')
8196 /* Open above the first line of a folded sequence of lines */
8197 (void)hasFolding(curwin->w_cursor.lnum,
8198 &curwin->w_cursor.lnum, NULL);
8199 else
8200 /* Open below the last line of a folded sequence of lines */
8201 (void)hasFolding(curwin->w_cursor.lnum,
8202 NULL, &curwin->w_cursor.lnum);
8203 #endif
8204 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8205 (cap->cmdchar == 'O' ? 1 : 0)),
8206 (linenr_T)(curwin->w_cursor.lnum +
8207 (cap->cmdchar == 'o' ? 1 : 0))
8208 ) == OK
8209 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8210 #ifdef FEAT_COMMENTS
8211 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8212 #endif
8213 0, 0))
8215 /* When '#' is in 'cpoptions' ignore the count. */
8216 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8217 cap->count1 = 1;
8218 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8224 * "." command: redo last change.
8226 static void
8227 nv_dot(cap)
8228 cmdarg_T *cap;
8230 if (!checkclearopq(cap->oap))
8233 * If "restart_edit" is TRUE, the last but one command is repeated
8234 * instead of the last command (inserting text). This is used for
8235 * CTRL-O <.> in insert mode.
8237 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8238 clearopbeep(cap->oap);
8243 * CTRL-R: undo undo
8245 static void
8246 nv_redo(cap)
8247 cmdarg_T *cap;
8249 if (!checkclearopq(cap->oap))
8251 u_redo((int)cap->count1);
8252 curwin->w_set_curswant = TRUE;
8257 * Handle "U" command.
8259 static void
8260 nv_Undo(cap)
8261 cmdarg_T *cap;
8263 /* In Visual mode and typing "gUU" triggers an operator */
8264 if (cap->oap->op_type == OP_UPPER
8265 #ifdef FEAT_VISUAL
8266 || VIsual_active
8267 #endif
8270 /* translate "gUU" to "gUgU" */
8271 cap->cmdchar = 'g';
8272 cap->nchar = 'U';
8273 nv_operator(cap);
8275 else if (!checkclearopq(cap->oap))
8277 u_undoline();
8278 curwin->w_set_curswant = TRUE;
8283 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8284 * single character.
8286 static void
8287 nv_tilde(cap)
8288 cmdarg_T *cap;
8290 if (!p_to
8291 #ifdef FEAT_VISUAL
8292 && !VIsual_active
8293 #endif
8294 && cap->oap->op_type != OP_TILDE)
8295 n_swapchar(cap);
8296 else
8297 nv_operator(cap);
8301 * Handle an operator command.
8302 * The actual work is done by do_pending_operator().
8304 static void
8305 nv_operator(cap)
8306 cmdarg_T *cap;
8308 int op_type;
8310 op_type = get_op_type(cap->cmdchar, cap->nchar);
8312 if (op_type == cap->oap->op_type) /* double operator works on lines */
8313 nv_lineop(cap);
8314 else if (!checkclearop(cap->oap))
8316 cap->oap->start = curwin->w_cursor;
8317 cap->oap->op_type = op_type;
8318 #ifdef FEAT_EVAL
8319 set_op_var(op_type);
8320 #endif
8324 #ifdef FEAT_EVAL
8326 * Set v:operator to the characters for "optype".
8328 static void
8329 set_op_var(optype)
8330 int optype;
8332 char_u opchars[3];
8334 if (optype == OP_NOP)
8335 set_vim_var_string(VV_OP, NULL, 0);
8336 else
8338 opchars[0] = get_op_char(optype);
8339 opchars[1] = get_extra_op_char(optype);
8340 opchars[2] = NUL;
8341 set_vim_var_string(VV_OP, opchars, -1);
8344 #endif
8347 * Handle linewise operator "dd", "yy", etc.
8349 * "_" is is a strange motion command that helps make operators more logical.
8350 * It is actually implemented, but not documented in the real Vi. This motion
8351 * command actually refers to "the current line". Commands like "dd" and "yy"
8352 * are really an alternate form of "d_" and "y_". It does accept a count, so
8353 * "d3_" works to delete 3 lines.
8355 static void
8356 nv_lineop(cap)
8357 cmdarg_T *cap;
8359 cap->oap->motion_type = MLINE;
8360 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8361 clearopbeep(cap->oap);
8362 else if ( cap->oap->op_type == OP_DELETE
8363 || cap->oap->op_type == OP_LSHIFT
8364 || cap->oap->op_type == OP_RSHIFT)
8365 beginline(BL_SOL | BL_FIX);
8366 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8367 beginline(BL_WHITE | BL_FIX);
8371 * <Home> command.
8373 static void
8374 nv_home(cap)
8375 cmdarg_T *cap;
8377 /* CTRL-HOME is like "gg" */
8378 if (mod_mask & MOD_MASK_CTRL)
8379 nv_goto(cap);
8380 else
8382 cap->count0 = 1;
8383 nv_pipe(cap);
8385 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8386 one-character line). */
8390 * "|" command.
8392 static void
8393 nv_pipe(cap)
8394 cmdarg_T *cap;
8396 cap->oap->motion_type = MCHAR;
8397 cap->oap->inclusive = FALSE;
8398 beginline(0);
8399 if (cap->count0 > 0)
8401 coladvance((colnr_T)(cap->count0 - 1));
8402 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8404 else
8405 curwin->w_curswant = 0;
8406 /* keep curswant at the column where we wanted to go, not where
8407 we ended; differs if line is too short */
8408 curwin->w_set_curswant = FALSE;
8412 * Handle back-word command "b" and "B".
8413 * cap->arg is 1 for "B"
8415 static void
8416 nv_bck_word(cap)
8417 cmdarg_T *cap;
8419 cap->oap->motion_type = MCHAR;
8420 cap->oap->inclusive = FALSE;
8421 curwin->w_set_curswant = TRUE;
8422 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8423 clearopbeep(cap->oap);
8424 #ifdef FEAT_FOLDING
8425 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8426 foldOpenCursor();
8427 #endif
8431 * Handle word motion commands "e", "E", "w" and "W".
8432 * cap->arg is TRUE for "E" and "W".
8434 static void
8435 nv_wordcmd(cap)
8436 cmdarg_T *cap;
8438 int n;
8439 int word_end;
8440 int flag = FALSE;
8441 pos_T startpos = curwin->w_cursor;
8444 * Set inclusive for the "E" and "e" command.
8446 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8447 word_end = TRUE;
8448 else
8449 word_end = FALSE;
8450 cap->oap->inclusive = word_end;
8453 * "cw" and "cW" are a special case.
8455 if (!word_end && cap->oap->op_type == OP_CHANGE)
8457 n = gchar_cursor();
8458 if (n != NUL) /* not an empty line */
8460 if (vim_iswhite(n))
8463 * Reproduce a funny Vi behaviour: "cw" on a blank only
8464 * changes one character, not all blanks until the start of
8465 * the next word. Only do this when the 'w' flag is included
8466 * in 'cpoptions'.
8468 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8470 cap->oap->inclusive = TRUE;
8471 cap->oap->motion_type = MCHAR;
8472 return;
8475 else
8478 * This is a little strange. To match what the real Vi does,
8479 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8480 * that we are not on a space or a TAB. This seems impolite
8481 * at first, but it's really more what we mean when we say
8482 * 'cw'.
8483 * Another strangeness: When standing on the end of a word
8484 * "ce" will change until the end of the next wordt, but "cw"
8485 * will change only one character! This is done by setting
8486 * flag.
8488 cap->oap->inclusive = TRUE;
8489 word_end = TRUE;
8490 flag = TRUE;
8495 cap->oap->motion_type = MCHAR;
8496 curwin->w_set_curswant = TRUE;
8497 if (word_end)
8498 n = end_word(cap->count1, cap->arg, flag, FALSE);
8499 else
8500 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8502 /* Don't leave the cursor on the NUL past the end of line. Unless we
8503 * didn't move it forward. */
8504 if (lt(startpos, curwin->w_cursor))
8505 adjust_cursor(cap->oap);
8507 if (n == FAIL && cap->oap->op_type == OP_NOP)
8508 clearopbeep(cap->oap);
8509 else
8511 #ifdef FEAT_VISUAL
8512 adjust_for_sel(cap);
8513 #endif
8514 #ifdef FEAT_FOLDING
8515 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8516 foldOpenCursor();
8517 #endif
8522 * Used after a movement command: If the cursor ends up on the NUL after the
8523 * end of the line, may move it back to the last character and make the motion
8524 * inclusive.
8526 static void
8527 adjust_cursor(oap)
8528 oparg_T *oap;
8530 /* The cursor cannot remain on the NUL when:
8531 * - the column is > 0
8532 * - not in Visual mode or 'selection' is "o"
8533 * - 'virtualedit' is not "all" and not "onemore".
8535 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
8536 #ifdef FEAT_VISUAL
8537 && (!VIsual_active || *p_sel == 'o')
8538 #endif
8539 #ifdef FEAT_VIRTUALEDIT
8540 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8541 #endif
8544 --curwin->w_cursor.col;
8545 #ifdef FEAT_MBYTE
8546 /* prevent cursor from moving on the trail byte */
8547 if (has_mbyte)
8548 mb_adjust_cursor();
8549 #endif
8550 oap->inclusive = TRUE;
8555 * "0" and "^" commands.
8556 * cap->arg is the argument for beginline().
8558 static void
8559 nv_beginline(cap)
8560 cmdarg_T *cap;
8562 cap->oap->motion_type = MCHAR;
8563 cap->oap->inclusive = FALSE;
8564 beginline(cap->arg);
8565 #ifdef FEAT_FOLDING
8566 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8567 foldOpenCursor();
8568 #endif
8569 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8570 one-character line). */
8573 #ifdef FEAT_VISUAL
8575 * In exclusive Visual mode, may include the last character.
8577 static void
8578 adjust_for_sel(cap)
8579 cmdarg_T *cap;
8581 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8582 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8584 # ifdef FEAT_MBYTE
8585 if (has_mbyte)
8586 inc_cursor();
8587 else
8588 # endif
8589 ++curwin->w_cursor.col;
8590 cap->oap->inclusive = FALSE;
8595 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8596 * Should check VIsual_mode before calling this.
8597 * Returns TRUE when backed up to the previous line.
8599 static int
8600 unadjust_for_sel()
8602 pos_T *pp;
8604 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8606 if (lt(VIsual, curwin->w_cursor))
8607 pp = &curwin->w_cursor;
8608 else
8609 pp = &VIsual;
8610 #ifdef FEAT_VIRTUALEDIT
8611 if (pp->coladd > 0)
8612 --pp->coladd;
8613 else
8614 #endif
8615 if (pp->col > 0)
8617 --pp->col;
8618 #ifdef FEAT_MBYTE
8619 mb_adjustpos(pp);
8620 #endif
8622 else if (pp->lnum > 1)
8624 --pp->lnum;
8625 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8626 return TRUE;
8629 return FALSE;
8633 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8635 static void
8636 nv_select(cap)
8637 cmdarg_T *cap;
8639 if (VIsual_active)
8640 VIsual_select = TRUE;
8641 else if (VIsual_reselect)
8643 cap->nchar = 'v'; /* fake "gv" command */
8644 cap->arg = TRUE;
8645 nv_g_cmd(cap);
8649 #endif
8652 * "G", "gg", CTRL-END, CTRL-HOME.
8653 * cap->arg is TRUE for "G".
8655 static void
8656 nv_goto(cap)
8657 cmdarg_T *cap;
8659 linenr_T lnum;
8661 if (cap->arg)
8662 lnum = curbuf->b_ml.ml_line_count;
8663 else
8664 lnum = 1L;
8665 cap->oap->motion_type = MLINE;
8666 setpcmark();
8668 /* When a count is given, use it instead of the default lnum */
8669 if (cap->count0 != 0)
8670 lnum = cap->count0;
8671 if (lnum < 1L)
8672 lnum = 1L;
8673 else if (lnum > curbuf->b_ml.ml_line_count)
8674 lnum = curbuf->b_ml.ml_line_count;
8675 curwin->w_cursor.lnum = lnum;
8676 beginline(BL_SOL | BL_FIX);
8677 #ifdef FEAT_FOLDING
8678 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8679 foldOpenCursor();
8680 #endif
8684 * CTRL-\ in Normal mode.
8686 static void
8687 nv_normal(cap)
8688 cmdarg_T *cap;
8690 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8692 clearop(cap->oap);
8693 if (restart_edit != 0 && mode_displayed)
8694 clear_cmdline = TRUE; /* unshow mode later */
8695 restart_edit = 0;
8696 #ifdef FEAT_CMDWIN
8697 if (cmdwin_type != 0)
8698 cmdwin_result = Ctrl_C;
8699 #endif
8700 #ifdef FEAT_VISUAL
8701 if (VIsual_active)
8703 end_visual_mode(); /* stop Visual */
8704 redraw_curbuf_later(INVERTED);
8706 #endif
8707 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8708 if (cap->nchar == Ctrl_G && p_im)
8709 restart_edit = 'a';
8711 else
8712 clearopbeep(cap->oap);
8716 * ESC in Normal mode: beep, but don't flush buffers.
8717 * Don't even beep if we are canceling a command.
8719 static void
8720 nv_esc(cap)
8721 cmdarg_T *cap;
8723 int no_reason;
8725 no_reason = (cap->oap->op_type == OP_NOP
8726 && cap->opcount == 0
8727 && cap->count0 == 0
8728 && cap->oap->regname == 0
8729 && !p_im);
8731 if (cap->arg) /* TRUE for CTRL-C */
8733 if (restart_edit == 0
8734 #ifdef FEAT_CMDWIN
8735 && cmdwin_type == 0
8736 #endif
8737 #ifdef FEAT_VISUAL
8738 && !VIsual_active
8739 #endif
8740 && no_reason)
8741 MSG(_("Type :quit<Enter> to exit Vim"));
8743 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8744 * set again below when halfway a mapping. */
8745 if (!p_im)
8746 restart_edit = 0;
8747 #ifdef FEAT_CMDWIN
8748 if (cmdwin_type != 0)
8750 cmdwin_result = K_IGNORE;
8751 got_int = FALSE; /* don't stop executing autocommands et al. */
8752 return;
8754 #endif
8757 #ifdef FEAT_VISUAL
8758 if (VIsual_active)
8760 end_visual_mode(); /* stop Visual */
8761 check_cursor_col(); /* make sure cursor is not beyond EOL */
8762 curwin->w_set_curswant = TRUE;
8763 redraw_curbuf_later(INVERTED);
8765 else
8766 #endif
8767 if (no_reason)
8768 vim_beep();
8769 clearop(cap->oap);
8771 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8772 * set return to Insert mode afterwards. */
8773 if (restart_edit == 0 && goto_im()
8774 #ifdef FEAT_EX_EXTRA
8775 && ex_normal_busy == 0
8776 #endif
8778 restart_edit = 'a';
8782 * Handle "A", "a", "I", "i" and <Insert> commands.
8784 static void
8785 nv_edit(cap)
8786 cmdarg_T *cap;
8788 /* <Insert> is equal to "i" */
8789 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8790 cap->cmdchar = 'i';
8792 #ifdef FEAT_VISUAL
8793 /* in Visual mode "A" and "I" are an operator */
8794 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8795 v_visop(cap);
8797 /* in Visual mode and after an operator "a" and "i" are for text objects */
8798 else
8799 #endif
8800 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8801 && (cap->oap->op_type != OP_NOP
8802 #ifdef FEAT_VISUAL
8803 || VIsual_active
8804 #endif
8807 #ifdef FEAT_TEXTOBJ
8808 nv_object(cap);
8809 #else
8810 clearopbeep(cap->oap);
8811 #endif
8813 else if (!curbuf->b_p_ma && !p_im)
8815 /* Only give this error when 'insertmode' is off. */
8816 EMSG(_(e_modifiable));
8817 clearop(cap->oap);
8819 else if (!checkclearopq(cap->oap))
8821 switch (cap->cmdchar)
8823 case 'A': /* "A"ppend after the line */
8824 curwin->w_set_curswant = TRUE;
8825 #ifdef FEAT_VIRTUALEDIT
8826 if (ve_flags == VE_ALL)
8828 int save_State = State;
8830 /* Pretent Insert mode here to allow the cursor on the
8831 * character past the end of the line */
8832 State = INSERT;
8833 coladvance((colnr_T)MAXCOL);
8834 State = save_State;
8836 else
8837 #endif
8838 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8839 break;
8841 case 'I': /* "I"nsert before the first non-blank */
8842 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
8843 beginline(BL_WHITE);
8844 else
8845 beginline(BL_WHITE|BL_FIX);
8846 break;
8848 case 'a': /* "a"ppend is like "i"nsert on the next character. */
8849 #ifdef FEAT_VIRTUALEDIT
8850 /* increment coladd when in virtual space, increment the
8851 * column otherwise, also to append after an unprintable char */
8852 if (virtual_active()
8853 && (curwin->w_cursor.coladd > 0
8854 || *ml_get_cursor() == NUL
8855 || *ml_get_cursor() == TAB))
8856 curwin->w_cursor.coladd++;
8857 else
8858 #endif
8859 if (*ml_get_cursor() != NUL)
8860 inc_cursor();
8861 break;
8864 #ifdef FEAT_VIRTUALEDIT
8865 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
8867 int save_State = State;
8869 /* Pretent Insert mode here to allow the cursor on the
8870 * character past the end of the line */
8871 State = INSERT;
8872 coladvance(getviscol());
8873 State = save_State;
8875 #endif
8877 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
8882 * Invoke edit() and take care of "restart_edit" and the return value.
8884 static void
8885 invoke_edit(cap, repl, cmd, startln)
8886 cmdarg_T *cap;
8887 int repl; /* "r" or "gr" command */
8888 int cmd;
8889 int startln;
8891 int restart_edit_save = 0;
8893 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
8894 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
8895 * it. */
8896 if (repl || !stuff_empty())
8897 restart_edit_save = restart_edit;
8898 else
8899 restart_edit_save = 0;
8901 /* Always reset "restart_edit", this is not a restarted edit. */
8902 restart_edit = 0;
8904 if (edit(cmd, startln, cap->count1))
8905 cap->retval |= CA_COMMAND_BUSY;
8907 if (restart_edit == 0)
8908 restart_edit = restart_edit_save;
8911 #ifdef FEAT_TEXTOBJ
8913 * "a" or "i" while an operator is pending or in Visual mode: object motion.
8915 static void
8916 nv_object(cap)
8917 cmdarg_T *cap;
8919 int flag;
8920 int include;
8921 char_u *mps_save;
8923 if (cap->cmdchar == 'i')
8924 include = FALSE; /* "ix" = inner object: exclude white space */
8925 else
8926 include = TRUE; /* "ax" = an object: include white space */
8928 /* Make sure (), [], {} and <> are in 'matchpairs' */
8929 mps_save = curbuf->b_p_mps;
8930 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
8932 switch (cap->nchar)
8934 case 'w': /* "aw" = a word */
8935 flag = current_word(cap->oap, cap->count1, include, FALSE);
8936 break;
8937 case 'W': /* "aW" = a WORD */
8938 flag = current_word(cap->oap, cap->count1, include, TRUE);
8939 break;
8940 case 'b': /* "ab" = a braces block */
8941 case '(':
8942 case ')':
8943 flag = current_block(cap->oap, cap->count1, include, '(', ')');
8944 break;
8945 case 'B': /* "aB" = a Brackets block */
8946 case '{':
8947 case '}':
8948 flag = current_block(cap->oap, cap->count1, include, '{', '}');
8949 break;
8950 case '[': /* "a[" = a [] block */
8951 case ']':
8952 flag = current_block(cap->oap, cap->count1, include, '[', ']');
8953 break;
8954 case '<': /* "a<" = a <> block */
8955 case '>':
8956 flag = current_block(cap->oap, cap->count1, include, '<', '>');
8957 break;
8958 case 't': /* "at" = a tag block (xml and html) */
8959 flag = current_tagblock(cap->oap, cap->count1, include);
8960 break;
8961 case 'p': /* "ap" = a paragraph */
8962 flag = current_par(cap->oap, cap->count1, include, 'p');
8963 break;
8964 case 's': /* "as" = a sentence */
8965 flag = current_sent(cap->oap, cap->count1, include);
8966 break;
8967 case '"': /* "a"" = a double quoted string */
8968 case '\'': /* "a'" = a single quoted string */
8969 case '`': /* "a`" = a backtick quoted string */
8970 flag = current_quote(cap->oap, cap->count1, include,
8971 cap->nchar);
8972 break;
8973 #if 0 /* TODO */
8974 case 'S': /* "aS" = a section */
8975 case 'f': /* "af" = a filename */
8976 case 'u': /* "au" = a URL */
8977 #endif
8978 default:
8979 flag = FAIL;
8980 break;
8983 curbuf->b_p_mps = mps_save;
8984 if (flag == FAIL)
8985 clearopbeep(cap->oap);
8986 adjust_cursor_col();
8987 curwin->w_set_curswant = TRUE;
8989 #endif
8992 * "q" command: Start/stop recording.
8993 * "q:", "q/", "q?": edit command-line in command-line window.
8995 static void
8996 nv_record(cap)
8997 cmdarg_T *cap;
8999 if (cap->oap->op_type == OP_FORMAT)
9001 /* "gqq" is the same as "gqgq": format line */
9002 cap->cmdchar = 'g';
9003 cap->nchar = 'q';
9004 nv_operator(cap);
9006 else if (!checkclearop(cap->oap))
9008 #ifdef FEAT_CMDWIN
9009 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9011 stuffcharReadbuff(cap->nchar);
9012 stuffcharReadbuff(K_CMDWIN);
9014 else
9015 #endif
9016 /* (stop) recording into a named register, unless executing a
9017 * register */
9018 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9019 clearopbeep(cap->oap);
9024 * Handle the "@r" command.
9026 static void
9027 nv_at(cap)
9028 cmdarg_T *cap;
9030 if (checkclearop(cap->oap))
9031 return;
9032 #ifdef FEAT_EVAL
9033 if (cap->nchar == '=')
9035 if (get_expr_register() == NUL)
9036 return;
9038 #endif
9039 while (cap->count1-- && !got_int)
9041 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
9043 clearopbeep(cap->oap);
9044 break;
9046 line_breakcheck();
9051 * Handle the CTRL-U and CTRL-D commands.
9053 static void
9054 nv_halfpage(cap)
9055 cmdarg_T *cap;
9057 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9058 || (cap->cmdchar == Ctrl_D
9059 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9060 clearopbeep(cap->oap);
9061 else if (!checkclearop(cap->oap))
9062 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9066 * Handle "J" or "gJ" command.
9068 static void
9069 nv_join(cap)
9070 cmdarg_T *cap;
9072 #ifdef FEAT_VISUAL
9073 if (VIsual_active) /* join the visual lines */
9074 nv_operator(cap);
9075 else
9076 #endif
9077 if (!checkclearop(cap->oap))
9079 if (cap->count0 <= 1)
9080 cap->count0 = 2; /* default for join is two lines! */
9081 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9082 curbuf->b_ml.ml_line_count)
9083 clearopbeep(cap->oap); /* beyond last line */
9084 else
9086 prep_redo(cap->oap->regname, cap->count0,
9087 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9088 do_do_join(cap->count0, cap->nchar == NUL);
9094 * "P", "gP", "p" and "gp" commands.
9096 static void
9097 nv_put(cap)
9098 cmdarg_T *cap;
9100 #ifdef FEAT_VISUAL
9101 int regname = 0;
9102 void *reg1 = NULL, *reg2 = NULL;
9103 int empty = FALSE;
9104 int was_visual = FALSE;
9105 #endif
9106 int dir;
9107 int flags = 0;
9109 if (cap->oap->op_type != OP_NOP)
9111 #ifdef FEAT_DIFF
9112 /* "dp" is ":diffput" */
9113 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9115 clearop(cap->oap);
9116 nv_diffgetput(TRUE);
9118 else
9119 #endif
9120 clearopbeep(cap->oap);
9122 else
9124 dir = (cap->cmdchar == 'P'
9125 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9126 ? BACKWARD : FORWARD;
9127 prep_redo_cmd(cap);
9128 if (cap->cmdchar == 'g')
9129 flags |= PUT_CURSEND;
9131 #ifdef FEAT_VISUAL
9132 if (VIsual_active)
9134 /* Putting in Visual mode: The put text replaces the selected
9135 * text. First delete the selected text, then put the new text.
9136 * Need to save and restore the registers that the delete
9137 * overwrites if the old contents is being put.
9139 was_visual = TRUE;
9140 regname = cap->oap->regname;
9141 # ifdef FEAT_CLIPBOARD
9142 adjust_clip_reg(&regname);
9143 # endif
9144 if (regname == 0 || VIM_ISDIGIT(regname)
9145 # ifdef FEAT_CLIPBOARD
9146 || (clip_unnamed && (regname == '*' || regname == '+'))
9147 # endif
9151 /* the delete is going to overwrite the register we want to
9152 * put, save it first. */
9153 reg1 = get_register(regname, TRUE);
9156 /* Now delete the selected text. */
9157 cap->cmdchar = 'd';
9158 cap->nchar = NUL;
9159 cap->oap->regname = NUL;
9160 nv_operator(cap);
9161 do_pending_operator(cap, 0, FALSE);
9162 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
9164 /* delete PUT_LINE_BACKWARD; */
9165 cap->oap->regname = regname;
9167 if (reg1 != NULL)
9169 /* Delete probably changed the register we want to put, save
9170 * it first. Then put back what was there before the delete. */
9171 reg2 = get_register(regname, FALSE);
9172 put_register(regname, reg1);
9175 /* When deleted a linewise Visual area, put the register as
9176 * lines to avoid it joined with the next line. When deletion was
9177 * characterwise, split a line when putting lines. */
9178 if (VIsual_mode == 'V')
9179 flags |= PUT_LINE;
9180 else if (VIsual_mode == 'v')
9181 flags |= PUT_LINE_SPLIT;
9182 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9183 flags |= PUT_LINE_FORWARD;
9184 dir = BACKWARD;
9185 if ((VIsual_mode != 'V'
9186 && curwin->w_cursor.col < curbuf->b_op_start.col)
9187 || (VIsual_mode == 'V'
9188 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9189 /* cursor is at the end of the line or end of file, put
9190 * forward. */
9191 dir = FORWARD;
9193 #endif
9194 do_put(cap->oap->regname, dir, cap->count1, flags);
9196 #ifdef FEAT_VISUAL
9197 /* If a register was saved, put it back now. */
9198 if (reg2 != NULL)
9199 put_register(regname, reg2);
9201 /* What to reselect with "gv"? Selecting the just put text seems to
9202 * be the most useful, since the original text was removed. */
9203 if (was_visual)
9205 curbuf->b_visual.vi_start = curbuf->b_op_start;
9206 curbuf->b_visual.vi_end = curbuf->b_op_end;
9209 /* When all lines were selected and deleted do_put() leaves an empty
9210 * line that needs to be deleted now. */
9211 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
9213 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
9215 /* If the cursor was in that line, move it to the end of the last
9216 * line. */
9217 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9219 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9220 coladvance((colnr_T)MAXCOL);
9223 #endif
9224 auto_format(FALSE, TRUE);
9229 * "o" and "O" commands.
9231 static void
9232 nv_open(cap)
9233 cmdarg_T *cap;
9235 #ifdef FEAT_DIFF
9236 /* "do" is ":diffget" */
9237 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9239 clearop(cap->oap);
9240 nv_diffgetput(FALSE);
9242 else
9243 #endif
9244 #ifdef FEAT_VISUAL
9245 if (VIsual_active) /* switch start and end of visual */
9246 v_swap_corners(cap->cmdchar);
9247 else
9248 #endif
9249 n_opencmd(cap);
9252 #ifdef FEAT_SNIFF
9253 /*ARGSUSED*/
9254 static void
9255 nv_sniff(cap)
9256 cmdarg_T *cap;
9258 ProcessSniffRequests();
9260 #endif
9262 #ifdef FEAT_NETBEANS_INTG
9263 static void
9264 nv_nbcmd(cap)
9265 cmdarg_T *cap;
9267 netbeans_keycommand(cap->nchar);
9269 #endif
9271 #ifdef FEAT_DND
9272 /*ARGSUSED*/
9273 static void
9274 nv_drop(cap)
9275 cmdarg_T *cap;
9277 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9279 #endif
9281 #ifdef FEAT_AUTOCMD
9283 * Trigger CursorHold event.
9284 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9285 * input buffer. "did_cursorhold" is set to avoid retriggering.
9287 /*ARGSUSED*/
9288 static void
9289 nv_cursorhold(cap)
9290 cmdarg_T *cap;
9292 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9293 did_cursorhold = TRUE;
9294 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
9296 #endif