Update runtime files
[MacVim.git] / src / normal.c
blob324133047cee8cbb7b61280b7d3a6d0d8fe68893
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;
583 #ifdef FEAT_EVAL
584 int set_prevcount = FALSE;
585 #endif
587 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
588 ca.oap = oap;
590 /* Use a count remembered from before entering an operator. After typing
591 * "3d" we return from normal_cmd() and come back here, the "3" is
592 * remembered in "opcount". */
593 ca.opcount = opcount;
595 #ifdef FEAT_SNIFF
596 want_sniff_request = sniff_connected;
597 #endif
600 * If there is an operator pending, then the command we take this time
601 * will terminate it. Finish_op tells us to finish the operation before
602 * returning this time (unless the operation was cancelled).
604 #ifdef CURSOR_SHAPE
605 c = finish_op;
606 #endif
607 finish_op = (oap->op_type != OP_NOP);
608 #ifdef CURSOR_SHAPE
609 if (finish_op != c)
611 ui_cursor_shape(); /* may show different cursor shape */
612 # ifdef FEAT_MOUSESHAPE
613 update_mouseshape(-1);
614 # endif
616 #endif
618 /* When not finishing an operator and no register name typed, reset the
619 * count. */
620 if (!finish_op && !oap->regname)
622 ca.opcount = 0;
623 #ifdef FEAT_EVAL
624 set_prevcount = TRUE;
625 #endif
628 #ifdef FEAT_AUTOCMD
629 /* Restore counts from before receiving K_CURSORHOLD. This means after
630 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
631 * "3 * 2". */
632 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
634 ca.opcount = oap->prev_opcount;
635 ca.count0 = oap->prev_count0;
636 oap->prev_opcount = 0;
637 oap->prev_count0 = 0;
639 #endif
641 #ifdef FEAT_VISUAL
642 mapped_len = typebuf_maplen();
643 #endif
645 State = NORMAL_BUSY;
646 #ifdef USE_ON_FLY_SCROLL
647 dont_scroll = FALSE; /* allow scrolling here */
648 #endif
651 * Get the command character from the user.
653 c = safe_vgetc();
654 LANGMAP_ADJUST(c, TRUE);
656 #ifdef FEAT_VISUAL
658 * If a mapping was started in Visual or Select mode, remember the length
659 * of the mapping. This is used below to not return to Insert mode for as
660 * long as the mapping is being executed.
662 if (restart_edit == 0)
663 old_mapped_len = 0;
664 else if (old_mapped_len
665 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
666 old_mapped_len = typebuf_maplen();
667 #endif
669 if (c == NUL)
670 c = K_ZERO;
672 #ifdef FEAT_VISUAL
674 * In Select mode, typed text replaces the selection.
676 if (VIsual_active
677 && VIsual_select
678 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
680 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
681 * 'insertmode' is set) fake a "d"elete command, Insert mode will
682 * restart automatically.
683 * Insert the typed character in the typeahead buffer, so that it can
684 * be mapped in Insert mode. Required for ":lmap" to work. */
685 ins_char_typebuf(c);
686 if (restart_edit != 0)
687 c = 'd';
688 else
689 c = 'c';
690 msg_nowait = TRUE; /* don't delay going to insert mode */
692 #endif
694 #ifdef FEAT_CMDL_INFO
695 need_flushbuf = add_to_showcmd(c);
696 #endif
698 getcount:
699 #ifdef FEAT_VISUAL
700 if (!(VIsual_active && VIsual_select))
701 #endif
704 * Handle a count before a command and compute ca.count0.
705 * Note that '0' is a command and not the start of a count, but it's
706 * part of a count after other digits.
708 while ( (c >= '1' && c <= '9')
709 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
711 if (c == K_DEL || c == K_KDEL)
713 ca.count0 /= 10;
714 #ifdef FEAT_CMDL_INFO
715 del_from_showcmd(4); /* delete the digit and ~@% */
716 #endif
718 else
719 ca.count0 = ca.count0 * 10 + (c - '0');
720 if (ca.count0 < 0) /* got too large! */
721 ca.count0 = 999999999L;
722 #ifdef FEAT_EVAL
723 /* Set v:count here, when called from main() and not a stuffed
724 * command, so that v:count can be used in an expression mapping
725 * right after the count. */
726 if (toplevel && stuff_empty())
728 long count = ca.count0;
730 /* multiply with ca.opcount the same way as below */
731 if (ca.opcount != 0)
732 count = ca.opcount * (count == 0 ? 1 : count);
733 set_vcount(count, count == 0 ? 1 : count, set_prevcount);
734 set_prevcount = FALSE; /* only set v:prevcount once */
736 #endif
737 if (ctrl_w)
739 ++no_mapping;
740 ++allow_keys; /* no mapping for nchar, but keys */
742 ++no_zero_mapping; /* don't map zero here */
743 c = plain_vgetc();
744 LANGMAP_ADJUST(c, TRUE);
745 --no_zero_mapping;
746 if (ctrl_w)
748 --no_mapping;
749 --allow_keys;
751 #ifdef FEAT_CMDL_INFO
752 need_flushbuf |= add_to_showcmd(c);
753 #endif
757 * If we got CTRL-W there may be a/another count
759 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
761 ctrl_w = TRUE;
762 ca.opcount = ca.count0; /* remember first count */
763 ca.count0 = 0;
764 ++no_mapping;
765 ++allow_keys; /* no mapping for nchar, but keys */
766 c = plain_vgetc(); /* get next character */
767 LANGMAP_ADJUST(c, TRUE);
768 --no_mapping;
769 --allow_keys;
770 #ifdef FEAT_CMDL_INFO
771 need_flushbuf |= add_to_showcmd(c);
772 #endif
773 goto getcount; /* jump back */
777 #ifdef FEAT_AUTOCMD
778 if (c == K_CURSORHOLD)
780 /* Save the count values so that ca.opcount and ca.count0 are exactly
781 * the same when coming back here after handling K_CURSORHOLD. */
782 oap->prev_opcount = ca.opcount;
783 oap->prev_count0 = ca.count0;
785 else
786 #endif
787 if (ca.opcount != 0)
790 * If we're in the middle of an operator (including after entering a
791 * yank buffer with '"') AND we had a count before the operator, then
792 * that count overrides the current value of ca.count0.
793 * What this means effectively, is that commands like "3dw" get turned
794 * into "d3w" which makes things fall into place pretty neatly.
795 * If you give a count before AND after the operator, they are
796 * multiplied.
798 if (ca.count0)
799 ca.count0 *= ca.opcount;
800 else
801 ca.count0 = ca.opcount;
805 * Always remember the count. It will be set to zero (on the next call,
806 * above) when there is no pending operator.
807 * When called from main(), save the count for use by the "count" built-in
808 * variable.
810 ca.opcount = ca.count0;
811 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
813 #ifdef FEAT_EVAL
815 * Only set v:count when called from main() and not a stuffed command.
817 if (toplevel && stuff_empty())
818 set_vcount(ca.count0, ca.count1, set_prevcount);
819 #endif
822 * Find the command character in the table of commands.
823 * For CTRL-W we already got nchar when looking for a count.
825 if (ctrl_w)
827 ca.nchar = c;
828 ca.cmdchar = Ctrl_W;
830 else
831 ca.cmdchar = c;
832 idx = find_command(ca.cmdchar);
833 if (idx < 0)
835 /* Not a known command: beep. */
836 clearopbeep(oap);
837 goto normal_end;
840 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
842 /* This command is not allowed while editing a ccmdline: beep. */
843 clearopbeep(oap);
844 text_locked_msg();
845 goto normal_end;
847 #ifdef FEAT_AUTOCMD
848 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
849 goto normal_end;
850 #endif
852 #ifdef FEAT_VISUAL
854 * In Visual/Select mode, a few keys are handled in a special way.
856 if (VIsual_active)
858 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
859 if (km_stopsel
860 && (nv_cmds[idx].cmd_flags & NV_STS)
861 && !(mod_mask & MOD_MASK_SHIFT))
863 end_visual_mode();
864 redraw_curbuf_later(INVERTED);
867 /* Keys that work different when 'keymodel' contains "startsel" */
868 if (km_startsel)
870 if (nv_cmds[idx].cmd_flags & NV_SS)
872 unshift_special(&ca);
873 idx = find_command(ca.cmdchar);
874 if (idx < 0)
876 /* Just in case */
877 clearopbeep(oap);
878 goto normal_end;
881 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
882 && (mod_mask & MOD_MASK_SHIFT))
884 mod_mask &= ~MOD_MASK_SHIFT;
888 #endif
890 #ifdef FEAT_RIGHTLEFT
891 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
892 && (nv_cmds[idx].cmd_flags & NV_RL))
894 /* Invert horizontal movements and operations. Only when typed by the
895 * user directly, not when the result of a mapping or "x" translated
896 * to "dl". */
897 switch (ca.cmdchar)
899 case 'l': ca.cmdchar = 'h'; break;
900 case K_RIGHT: ca.cmdchar = K_LEFT; break;
901 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
902 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
903 case 'h': ca.cmdchar = 'l'; break;
904 case K_LEFT: ca.cmdchar = K_RIGHT; break;
905 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
906 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
907 case '>': ca.cmdchar = '<'; break;
908 case '<': ca.cmdchar = '>'; break;
910 idx = find_command(ca.cmdchar);
912 #endif
915 * Get an additional character if we need one.
917 if ((nv_cmds[idx].cmd_flags & NV_NCH)
918 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
919 && oap->op_type == OP_NOP)
920 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
921 || (ca.cmdchar == 'q'
922 && oap->op_type == OP_NOP
923 && !Recording
924 && !Exec_reg)
925 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
926 && (oap->op_type != OP_NOP
927 #ifdef FEAT_VISUAL
928 || VIsual_active
929 #endif
930 ))))
932 int *cp;
933 int repl = FALSE; /* get character for replace mode */
934 int lit = FALSE; /* get extra character literally */
935 int langmap_active = FALSE; /* using :lmap mappings */
936 int lang; /* getting a text character */
937 #ifdef USE_IM_CONTROL
938 int save_smd; /* saved value of p_smd */
939 #endif
941 ++no_mapping;
942 ++allow_keys; /* no mapping for nchar, but allow key codes */
943 #ifdef FEAT_AUTOCMD
944 /* Don't generate a CursorHold event here, most commands can't handle
945 * it, e.g., nv_replace(), nv_csearch(). */
946 did_cursorhold = TRUE;
947 #endif
948 if (ca.cmdchar == 'g')
951 * For 'g' get the next character now, so that we can check for
952 * "gr", "g'" and "g`".
954 ca.nchar = plain_vgetc();
955 LANGMAP_ADJUST(ca.nchar, TRUE);
956 #ifdef FEAT_CMDL_INFO
957 need_flushbuf |= add_to_showcmd(ca.nchar);
958 #endif
959 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
960 || ca.nchar == Ctrl_BSL)
962 cp = &ca.extra_char; /* need to get a third character */
963 if (ca.nchar != 'r')
964 lit = TRUE; /* get it literally */
965 else
966 repl = TRUE; /* get it in replace mode */
968 else
969 cp = NULL; /* no third character needed */
971 else
973 if (ca.cmdchar == 'r') /* get it in replace mode */
974 repl = TRUE;
975 cp = &ca.nchar;
977 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
980 * Get a second or third character.
982 if (cp != NULL)
984 #ifdef CURSOR_SHAPE
985 if (repl)
987 State = REPLACE; /* pretend Replace mode */
988 ui_cursor_shape(); /* show different cursor shape */
990 #endif
991 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
993 /* Allow mappings defined with ":lmap". */
994 --no_mapping;
995 --allow_keys;
996 if (repl)
997 State = LREPLACE;
998 else
999 State = LANGMAP;
1000 langmap_active = TRUE;
1002 #ifdef USE_IM_CONTROL
1003 save_smd = p_smd;
1004 p_smd = FALSE; /* Don't let the IM code show the mode here */
1005 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
1006 im_set_active(TRUE);
1007 #endif
1009 *cp = plain_vgetc();
1011 if (langmap_active)
1013 /* Undo the decrement done above */
1014 ++no_mapping;
1015 ++allow_keys;
1016 State = NORMAL_BUSY;
1018 #ifdef USE_IM_CONTROL
1019 if (lang)
1021 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
1022 im_save_status(&curbuf->b_p_iminsert);
1023 im_set_active(FALSE);
1025 p_smd = save_smd;
1026 #endif
1027 #ifdef CURSOR_SHAPE
1028 State = NORMAL_BUSY;
1029 #endif
1030 #ifdef FEAT_CMDL_INFO
1031 need_flushbuf |= add_to_showcmd(*cp);
1032 #endif
1034 if (!lit)
1036 #ifdef FEAT_DIGRAPHS
1037 /* Typing CTRL-K gets a digraph. */
1038 if (*cp == Ctrl_K
1039 && ((nv_cmds[idx].cmd_flags & NV_LANG)
1040 || cp == &ca.extra_char)
1041 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1043 c = get_digraph(FALSE);
1044 if (c > 0)
1046 *cp = c;
1047 # ifdef FEAT_CMDL_INFO
1048 /* Guessing how to update showcmd here... */
1049 del_from_showcmd(3);
1050 need_flushbuf |= add_to_showcmd(*cp);
1051 # endif
1054 #endif
1056 /* adjust chars > 127, except after "tTfFr" commands */
1057 LANGMAP_ADJUST(*cp, !lang);
1058 #ifdef FEAT_RIGHTLEFT
1059 /* adjust Hebrew mapped char */
1060 if (p_hkmap && lang && KeyTyped)
1061 *cp = hkmap(*cp);
1062 # ifdef FEAT_FKMAP
1063 /* adjust Farsi mapped char */
1064 if (p_fkmap && lang && KeyTyped)
1065 *cp = fkmap(*cp);
1066 # endif
1067 #endif
1071 * When the next character is CTRL-\ a following CTRL-N means the
1072 * command is aborted and we go to Normal mode.
1074 if (cp == &ca.extra_char
1075 && ca.nchar == Ctrl_BSL
1076 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1078 ca.cmdchar = Ctrl_BSL;
1079 ca.nchar = ca.extra_char;
1080 idx = find_command(ca.cmdchar);
1082 else if (*cp == Ctrl_BSL)
1084 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1086 /* There is a busy wait here when typing "f<C-\>" and then
1087 * something different from CTRL-N. Can't be avoided. */
1088 while ((c = vpeekc()) <= 0 && towait > 0L)
1090 do_sleep(towait > 50L ? 50L : towait);
1091 towait -= 50L;
1093 if (c > 0)
1095 c = plain_vgetc();
1096 if (c != Ctrl_N && c != Ctrl_G)
1097 vungetc(c);
1098 else
1100 ca.cmdchar = Ctrl_BSL;
1101 ca.nchar = c;
1102 idx = find_command(ca.cmdchar);
1107 #ifdef FEAT_MBYTE
1108 /* When getting a text character and the next character is a
1109 * multi-byte character, it could be a composing character.
1110 * However, don't wait for it to arrive. */
1111 while (enc_utf8 && lang && (c = vpeekc()) > 0
1112 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1114 c = plain_vgetc();
1115 if (!utf_iscomposing(c))
1117 vungetc(c); /* it wasn't, put it back */
1118 break;
1120 else if (ca.ncharC1 == 0)
1121 ca.ncharC1 = c;
1122 else
1123 ca.ncharC2 = c;
1125 #endif
1127 --no_mapping;
1128 --allow_keys;
1131 #ifdef FEAT_CMDL_INFO
1133 * Flush the showcmd characters onto the screen so we can see them while
1134 * the command is being executed. Only do this when the shown command was
1135 * actually displayed, otherwise this will slow down a lot when executing
1136 * mappings.
1138 if (need_flushbuf)
1139 out_flush();
1140 #endif
1141 #ifdef FEAT_AUTOCMD
1142 if (ca.cmdchar != K_IGNORE)
1143 did_cursorhold = FALSE;
1144 #endif
1146 State = NORMAL;
1148 if (ca.nchar == ESC)
1150 clearop(oap);
1151 if (restart_edit == 0 && goto_im())
1152 restart_edit = 'a';
1153 goto normal_end;
1156 if (ca.cmdchar != K_IGNORE)
1158 msg_didout = FALSE; /* don't scroll screen up for normal command */
1159 msg_col = 0;
1162 #ifdef FEAT_VISUAL
1163 old_pos = curwin->w_cursor; /* remember where cursor was */
1165 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1166 * mode. */
1167 if (!VIsual_active && km_startsel)
1169 if (nv_cmds[idx].cmd_flags & NV_SS)
1171 start_selection();
1172 unshift_special(&ca);
1173 idx = find_command(ca.cmdchar);
1175 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1176 && (mod_mask & MOD_MASK_SHIFT))
1178 start_selection();
1179 mod_mask &= ~MOD_MASK_SHIFT;
1182 #endif
1185 * Execute the command!
1186 * Call the command function found in the commands table.
1188 ca.arg = nv_cmds[idx].cmd_arg;
1189 (nv_cmds[idx].cmd_func)(&ca);
1192 * If we didn't start or finish an operator, reset oap->regname, unless we
1193 * need it later.
1195 if (!finish_op
1196 && !oap->op_type
1197 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1199 clearop(oap);
1200 #ifdef FEAT_EVAL
1201 set_reg_var('"');
1202 #endif
1205 #ifdef FEAT_VISUAL
1206 /* Get the length of mapped chars again after typing a count, second
1207 * character or "z333<cr>". */
1208 if (old_mapped_len > 0)
1209 old_mapped_len = typebuf_maplen();
1210 #endif
1213 * If an operation is pending, handle it...
1215 do_pending_operator(&ca, old_col, FALSE);
1218 * Wait for a moment when a message is displayed that will be overwritten
1219 * by the mode message.
1220 * In Visual mode and with "^O" in Insert mode, a short message will be
1221 * overwritten by the mode message. Wait a bit, until a key is hit.
1222 * In Visual mode, it's more important to keep the Visual area updated
1223 * than keeping a message (e.g. from a /pat search).
1224 * Only do this if the command was typed, not from a mapping.
1225 * Don't wait when emsg_silent is non-zero.
1226 * Also wait a bit after an error message, e.g. for "^O:".
1227 * Don't redraw the screen, it would remove the message.
1229 if ( ((p_smd
1230 && msg_silent == 0
1231 && (restart_edit != 0
1232 #ifdef FEAT_VISUAL
1233 || (VIsual_active
1234 && old_pos.lnum == curwin->w_cursor.lnum
1235 && old_pos.col == curwin->w_cursor.col)
1236 #endif
1238 && (clear_cmdline
1239 || redraw_cmdline)
1240 && (msg_didout || (msg_didany && msg_scroll))
1241 && !msg_nowait
1242 && KeyTyped)
1243 || (restart_edit != 0
1244 #ifdef FEAT_VISUAL
1245 && !VIsual_active
1246 #endif
1247 && (msg_scroll
1248 || emsg_on_display)))
1249 && oap->regname == 0
1250 && !(ca.retval & CA_COMMAND_BUSY)
1251 && stuff_empty()
1252 && typebuf_typed()
1253 && emsg_silent == 0
1254 && !did_wait_return
1255 && oap->op_type == OP_NOP)
1257 int save_State = State;
1259 /* Draw the cursor with the right shape here */
1260 if (restart_edit != 0)
1261 State = INSERT;
1263 /* If need to redraw, and there is a "keep_msg", redraw before the
1264 * delay */
1265 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1267 char_u *kmsg;
1269 kmsg = keep_msg;
1270 keep_msg = NULL;
1271 /* showmode() will clear keep_msg, but we want to use it anyway */
1272 update_screen(0);
1273 /* now reset it, otherwise it's put in the history again */
1274 keep_msg = kmsg;
1275 msg_attr(kmsg, keep_msg_attr);
1276 vim_free(kmsg);
1278 setcursor();
1279 cursor_on();
1280 out_flush();
1281 if (msg_scroll || emsg_on_display)
1282 ui_delay(1000L, TRUE); /* wait at least one second */
1283 ui_delay(3000L, FALSE); /* wait up to three seconds */
1284 State = save_State;
1286 msg_scroll = FALSE;
1287 emsg_on_display = FALSE;
1291 * Finish up after executing a Normal mode command.
1293 normal_end:
1295 msg_nowait = FALSE;
1297 /* Reset finish_op, in case it was set */
1298 #ifdef CURSOR_SHAPE
1299 c = finish_op;
1300 #endif
1301 finish_op = FALSE;
1302 #ifdef CURSOR_SHAPE
1303 /* Redraw the cursor with another shape, if we were in Operator-pending
1304 * mode or did a replace command. */
1305 if (c || ca.cmdchar == 'r')
1307 ui_cursor_shape(); /* may show different cursor shape */
1308 # ifdef FEAT_MOUSESHAPE
1309 update_mouseshape(-1);
1310 # endif
1312 #endif
1314 #ifdef FEAT_CMDL_INFO
1315 if (oap->op_type == OP_NOP && oap->regname == 0
1316 # ifdef FEAT_AUTOCMD
1317 && ca.cmdchar != K_CURSORHOLD
1318 # endif
1320 clear_showcmd();
1321 #endif
1323 checkpcmark(); /* check if we moved since setting pcmark */
1324 vim_free(ca.searchbuf);
1326 #ifdef FEAT_MBYTE
1327 if (has_mbyte)
1328 mb_adjust_cursor();
1329 #endif
1331 #ifdef FEAT_SCROLLBIND
1332 if (curwin->w_p_scb && toplevel)
1334 validate_cursor(); /* may need to update w_leftcol */
1335 do_check_scrollbind(TRUE);
1337 #endif
1340 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1341 * if still inside a mapping that started in Visual mode).
1342 * May switch from Visual to Select mode after CTRL-O command.
1344 if ( oap->op_type == OP_NOP
1345 #ifdef FEAT_VISUAL
1346 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1347 || restart_VIsual_select == 1)
1348 #else
1349 && restart_edit != 0
1350 #endif
1351 && !(ca.retval & CA_COMMAND_BUSY)
1352 && stuff_empty()
1353 && oap->regname == 0)
1355 #ifdef FEAT_VISUAL
1356 if (restart_VIsual_select == 1)
1358 VIsual_select = TRUE;
1359 showmode();
1360 restart_VIsual_select = 0;
1362 #endif
1363 if (restart_edit != 0
1364 #ifdef FEAT_VISUAL
1365 && !VIsual_active && old_mapped_len == 0
1366 #endif
1368 (void)edit(restart_edit, FALSE, 1L);
1371 #ifdef FEAT_VISUAL
1372 if (restart_VIsual_select == 2)
1373 restart_VIsual_select = 1;
1374 #endif
1376 /* Save count before an operator for next time. */
1377 opcount = ca.opcount;
1381 * Handle an operator after visual mode or when the movement is finished
1383 void
1384 do_pending_operator(cap, old_col, gui_yank)
1385 cmdarg_T *cap;
1386 int old_col;
1387 int gui_yank;
1389 oparg_T *oap = cap->oap;
1390 pos_T old_cursor;
1391 int empty_region_error;
1392 int restart_edit_save;
1394 #ifdef FEAT_VISUAL
1395 /* The visual area is remembered for redo */
1396 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1397 static linenr_T redo_VIsual_line_count; /* number of lines */
1398 static colnr_T redo_VIsual_col; /* number of cols or end column */
1399 static long redo_VIsual_count; /* count for Visual operator */
1400 # ifdef FEAT_VIRTUALEDIT
1401 int include_line_break = FALSE;
1402 # endif
1403 #endif
1405 #if defined(FEAT_CLIPBOARD)
1407 * Yank the visual area into the GUI selection register before we operate
1408 * on it and lose it forever.
1409 * Don't do it if a specific register was specified, so that ""x"*P works.
1410 * This could call do_pending_operator() recursively, but that's OK
1411 * because gui_yank will be TRUE for the nested call.
1413 if (clip_star.available
1414 && oap->op_type != OP_NOP
1415 && !gui_yank
1416 # ifdef FEAT_VISUAL
1417 && VIsual_active
1418 && !redo_VIsual_busy
1419 # endif
1420 && oap->regname == 0)
1421 clip_auto_select();
1422 #endif
1423 old_cursor = curwin->w_cursor;
1426 * If an operation is pending, handle it...
1428 if ((finish_op
1429 #ifdef FEAT_VISUAL
1430 || VIsual_active
1431 #endif
1432 ) && oap->op_type != OP_NOP)
1434 #ifdef FEAT_VISUAL
1435 oap->is_VIsual = VIsual_active;
1436 if (oap->motion_force == 'V')
1437 oap->motion_type = MLINE;
1438 else if (oap->motion_force == 'v')
1440 /* If the motion was linewise, "inclusive" will not have been set.
1441 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1442 if (oap->motion_type == MLINE)
1443 oap->inclusive = FALSE;
1444 /* If the motion already was characterwise, toggle "inclusive" */
1445 else if (oap->motion_type == MCHAR)
1446 oap->inclusive = !oap->inclusive;
1447 oap->motion_type = MCHAR;
1449 else if (oap->motion_force == Ctrl_V)
1451 /* Change line- or characterwise motion into Visual block mode. */
1452 VIsual_active = TRUE;
1453 VIsual = oap->start;
1454 VIsual_mode = Ctrl_V;
1455 VIsual_select = FALSE;
1456 VIsual_reselect = FALSE;
1458 #endif
1460 /* only redo yank when 'y' flag is in 'cpoptions' */
1461 /* never redo "zf" (define fold) */
1462 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1463 #ifdef FEAT_VISUAL
1464 && (!VIsual_active || oap->motion_force)
1465 #endif
1466 && cap->cmdchar != 'D'
1467 #ifdef FEAT_FOLDING
1468 && oap->op_type != OP_FOLD
1469 && oap->op_type != OP_FOLDOPEN
1470 && oap->op_type != OP_FOLDOPENREC
1471 && oap->op_type != OP_FOLDCLOSE
1472 && oap->op_type != OP_FOLDCLOSEREC
1473 && oap->op_type != OP_FOLDDEL
1474 && oap->op_type != OP_FOLDDELREC
1475 #endif
1478 prep_redo(oap->regname, cap->count0,
1479 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1480 oap->motion_force, cap->cmdchar, cap->nchar);
1481 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1484 * If 'cpoptions' does not contain 'r', insert the search
1485 * pattern to really repeat the same command.
1487 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
1488 AppendToRedobuffLit(cap->searchbuf, -1);
1489 AppendToRedobuff(NL_STR);
1491 else if (cap->cmdchar == ':')
1493 /* do_cmdline() has stored the first typed line in
1494 * "repeat_cmdline". When several lines are typed repeating
1495 * won't be possible. */
1496 if (repeat_cmdline == NULL)
1497 ResetRedobuff();
1498 else
1500 AppendToRedobuffLit(repeat_cmdline, -1);
1501 AppendToRedobuff(NL_STR);
1502 vim_free(repeat_cmdline);
1503 repeat_cmdline = NULL;
1508 #ifdef FEAT_VISUAL
1509 if (redo_VIsual_busy)
1511 oap->start = curwin->w_cursor;
1512 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1513 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1514 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1515 VIsual_mode = redo_VIsual_mode;
1516 if (VIsual_mode == 'v')
1518 if (redo_VIsual_line_count <= 1)
1519 curwin->w_cursor.col += redo_VIsual_col - 1;
1520 else
1521 curwin->w_cursor.col = redo_VIsual_col;
1523 if (redo_VIsual_col == MAXCOL)
1525 curwin->w_curswant = MAXCOL;
1526 coladvance((colnr_T)MAXCOL);
1528 cap->count0 = redo_VIsual_count;
1529 if (redo_VIsual_count != 0)
1530 cap->count1 = redo_VIsual_count;
1531 else
1532 cap->count1 = 1;
1534 else if (VIsual_active)
1536 if (!gui_yank)
1538 /* Save the current VIsual area for '< and '> marks, and "gv" */
1539 curbuf->b_visual.vi_start = VIsual;
1540 curbuf->b_visual.vi_end = curwin->w_cursor;
1541 curbuf->b_visual.vi_mode = VIsual_mode;
1542 curbuf->b_visual.vi_curswant = curwin->w_curswant;
1543 # ifdef FEAT_EVAL
1544 curbuf->b_visual_mode_eval = VIsual_mode;
1545 # endif
1548 /* In Select mode, a linewise selection is operated upon like a
1549 * characterwise selection. */
1550 if (VIsual_select && VIsual_mode == 'V')
1552 if (lt(VIsual, curwin->w_cursor))
1554 VIsual.col = 0;
1555 curwin->w_cursor.col =
1556 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1558 else
1560 curwin->w_cursor.col = 0;
1561 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1563 VIsual_mode = 'v';
1565 /* If 'selection' is "exclusive", backup one character for
1566 * charwise selections. */
1567 else if (VIsual_mode == 'v')
1569 # ifdef FEAT_VIRTUALEDIT
1570 include_line_break =
1571 # endif
1572 unadjust_for_sel();
1575 oap->start = VIsual;
1576 if (VIsual_mode == 'V')
1577 oap->start.col = 0;
1579 #endif /* FEAT_VISUAL */
1582 * Set oap->start to the first position of the operated text, oap->end
1583 * to the end of the operated text. w_cursor is equal to oap->start.
1585 if (lt(oap->start, curwin->w_cursor))
1587 #ifdef FEAT_FOLDING
1588 /* Include folded lines completely. */
1589 if (!VIsual_active)
1591 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1592 oap->start.col = 0;
1593 if (hasFolding(curwin->w_cursor.lnum, NULL,
1594 &curwin->w_cursor.lnum))
1595 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1597 #endif
1598 oap->end = curwin->w_cursor;
1599 curwin->w_cursor = oap->start;
1601 /* w_virtcol may have been updated; if the cursor goes back to its
1602 * previous position w_virtcol becomes invalid and isn't updated
1603 * automatically. */
1604 curwin->w_valid &= ~VALID_VIRTCOL;
1606 else
1608 #ifdef FEAT_FOLDING
1609 /* Include folded lines completely. */
1610 if (!VIsual_active && oap->motion_type == MLINE)
1612 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1613 NULL))
1614 curwin->w_cursor.col = 0;
1615 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1616 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1618 #endif
1619 oap->end = oap->start;
1620 oap->start = curwin->w_cursor;
1623 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1625 #ifdef FEAT_VIRTUALEDIT
1626 /* Set "virtual_op" before resetting VIsual_active. */
1627 virtual_op = virtual_active();
1628 #endif
1630 #ifdef FEAT_VISUAL
1631 if (VIsual_active || redo_VIsual_busy)
1633 if (VIsual_mode == Ctrl_V) /* block mode */
1635 colnr_T start, end;
1637 oap->block_mode = TRUE;
1639 getvvcol(curwin, &(oap->start),
1640 &oap->start_vcol, NULL, &oap->end_vcol);
1641 if (!redo_VIsual_busy)
1643 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1645 if (start < oap->start_vcol)
1646 oap->start_vcol = start;
1647 if (end > oap->end_vcol)
1649 if (*p_sel == 'e' && start >= 1
1650 && start - 1 >= oap->end_vcol)
1651 oap->end_vcol = start - 1;
1652 else
1653 oap->end_vcol = end;
1657 /* if '$' was used, get oap->end_vcol from longest line */
1658 if (curwin->w_curswant == MAXCOL)
1660 curwin->w_cursor.col = MAXCOL;
1661 oap->end_vcol = 0;
1662 for (curwin->w_cursor.lnum = oap->start.lnum;
1663 curwin->w_cursor.lnum <= oap->end.lnum;
1664 ++curwin->w_cursor.lnum)
1666 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1667 if (end > oap->end_vcol)
1668 oap->end_vcol = end;
1671 else if (redo_VIsual_busy)
1672 oap->end_vcol = oap->start_vcol + redo_VIsual_col - 1;
1674 * Correct oap->end.col and oap->start.col to be the
1675 * upper-left and lower-right corner of the block area.
1677 * (Actually, this does convert column positions into character
1678 * positions)
1680 curwin->w_cursor.lnum = oap->end.lnum;
1681 coladvance(oap->end_vcol);
1682 oap->end = curwin->w_cursor;
1684 curwin->w_cursor = oap->start;
1685 coladvance(oap->start_vcol);
1686 oap->start = curwin->w_cursor;
1689 if (!redo_VIsual_busy && !gui_yank)
1692 * Prepare to reselect and redo Visual: this is based on the
1693 * size of the Visual text
1695 resel_VIsual_mode = VIsual_mode;
1696 if (curwin->w_curswant == MAXCOL)
1697 resel_VIsual_col = MAXCOL;
1698 else if (VIsual_mode == Ctrl_V)
1699 resel_VIsual_col = oap->end_vcol - oap->start_vcol + 1;
1700 else if (oap->line_count > 1)
1701 resel_VIsual_col = oap->end.col;
1702 else
1703 resel_VIsual_col = oap->end.col - oap->start.col + 1;
1704 resel_VIsual_line_count = oap->line_count;
1707 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1708 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1709 && oap->op_type != OP_COLON
1710 #ifdef FEAT_FOLDING
1711 && oap->op_type != OP_FOLD
1712 && oap->op_type != OP_FOLDOPEN
1713 && oap->op_type != OP_FOLDOPENREC
1714 && oap->op_type != OP_FOLDCLOSE
1715 && oap->op_type != OP_FOLDCLOSEREC
1716 && oap->op_type != OP_FOLDDEL
1717 && oap->op_type != OP_FOLDDELREC
1718 #endif
1719 && oap->motion_force == NUL
1722 /* Prepare for redoing. Only use the nchar field for "r",
1723 * otherwise it might be the second char of the operator. */
1724 prep_redo(oap->regname, 0L, NUL, 'v',
1725 get_op_char(oap->op_type),
1726 get_extra_op_char(oap->op_type),
1727 oap->op_type == OP_REPLACE ? cap->nchar : NUL);
1728 if (!redo_VIsual_busy)
1730 redo_VIsual_mode = resel_VIsual_mode;
1731 redo_VIsual_col = resel_VIsual_col;
1732 redo_VIsual_line_count = resel_VIsual_line_count;
1733 redo_VIsual_count = cap->count0;
1738 * oap->inclusive defaults to TRUE.
1739 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1740 * FALSE. This makes "d}P" and "v}dP" work the same.
1742 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1743 oap->inclusive = TRUE;
1744 if (VIsual_mode == 'V')
1745 oap->motion_type = MLINE;
1746 else
1748 oap->motion_type = MCHAR;
1749 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1750 # ifdef FEAT_VIRTUALEDIT
1751 && (include_line_break || !virtual_op)
1752 # endif
1755 oap->inclusive = FALSE;
1756 /* Try to include the newline, unless it's an operator
1757 * that works on lines only */
1758 if (*p_sel != 'o'
1759 && !op_on_lines(oap->op_type)
1760 && oap->end.lnum < curbuf->b_ml.ml_line_count)
1762 ++oap->end.lnum;
1763 oap->end.col = 0;
1764 # ifdef FEAT_VIRTUALEDIT
1765 oap->end.coladd = 0;
1766 # endif
1767 ++oap->line_count;
1772 redo_VIsual_busy = FALSE;
1775 * Switch Visual off now, so screen updating does
1776 * not show inverted text when the screen is redrawn.
1777 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1778 * no screen redraw, so it is done here to remove the inverted
1779 * part.
1781 if (!gui_yank)
1783 VIsual_active = FALSE;
1784 # ifdef FEAT_MOUSE
1785 setmouse();
1786 mouse_dragging = 0;
1787 # endif
1788 if (mode_displayed)
1789 clear_cmdline = TRUE; /* unshow visual mode later */
1790 #ifdef FEAT_CMDL_INFO
1791 else
1792 clear_showcmd();
1793 #endif
1794 if ((oap->op_type == OP_YANK
1795 || oap->op_type == OP_COLON
1796 || oap->op_type == OP_FUNCTION
1797 || oap->op_type == OP_FILTER)
1798 && oap->motion_force == NUL)
1799 redraw_curbuf_later(INVERTED);
1802 #endif
1804 #ifdef FEAT_MBYTE
1805 /* Include the trailing byte of a multi-byte char. */
1806 if (has_mbyte && oap->inclusive)
1808 int l;
1810 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
1811 if (l > 1)
1812 oap->end.col += l - 1;
1814 #endif
1815 curwin->w_set_curswant = TRUE;
1818 * oap->empty is set when start and end are the same. The inclusive
1819 * flag affects this too, unless yanking and the end is on a NUL.
1821 oap->empty = (oap->motion_type == MCHAR
1822 && (!oap->inclusive
1823 || (oap->op_type == OP_YANK
1824 && gchar_pos(&oap->end) == NUL))
1825 && equalpos(oap->start, oap->end)
1826 #ifdef FEAT_VIRTUALEDIT
1827 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1828 #endif
1831 * For delete, change and yank, it's an error to operate on an
1832 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1834 empty_region_error = (oap->empty
1835 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1837 #ifdef FEAT_VISUAL
1838 /* Force a redraw when operating on an empty Visual region, when
1839 * 'modifiable is off or creating a fold. */
1840 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1841 # ifdef FEAT_FOLDING
1842 || oap->op_type == OP_FOLD
1843 # endif
1845 redraw_curbuf_later(INVERTED);
1846 #endif
1849 * If the end of an operator is in column one while oap->motion_type
1850 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1851 * character in the previous line. If op_start is on or before the
1852 * first non-blank in the line, the operator becomes linewise
1853 * (strange, but that's the way vi does it).
1855 if ( oap->motion_type == MCHAR
1856 && oap->inclusive == FALSE
1857 && !(cap->retval & CA_NO_ADJ_OP_END)
1858 && oap->end.col == 0
1859 #ifdef FEAT_VISUAL
1860 && (!oap->is_VIsual || *p_sel == 'o')
1861 && !oap->block_mode
1862 #endif
1863 && oap->line_count > 1)
1865 oap->end_adjusted = TRUE; /* remember that we did this */
1866 --oap->line_count;
1867 --oap->end.lnum;
1868 if (inindent(0))
1869 oap->motion_type = MLINE;
1870 else
1872 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1873 if (oap->end.col)
1875 --oap->end.col;
1876 oap->inclusive = TRUE;
1880 else
1881 oap->end_adjusted = FALSE;
1883 switch (oap->op_type)
1885 case OP_LSHIFT:
1886 case OP_RSHIFT:
1887 op_shift(oap, TRUE,
1888 #ifdef FEAT_VISUAL
1889 oap->is_VIsual ? (int)cap->count1 :
1890 #endif
1892 auto_format(FALSE, TRUE);
1893 break;
1895 case OP_JOIN_NS:
1896 case OP_JOIN:
1897 if (oap->line_count < 2)
1898 oap->line_count = 2;
1899 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1900 curbuf->b_ml.ml_line_count)
1901 beep_flush();
1902 else
1904 do_do_join(oap->line_count, oap->op_type == OP_JOIN);
1905 auto_format(FALSE, TRUE);
1907 break;
1909 case OP_DELETE:
1910 #ifdef FEAT_VISUAL
1911 VIsual_reselect = FALSE; /* don't reselect now */
1912 #endif
1913 if (empty_region_error)
1914 vim_beep();
1915 else
1917 (void)op_delete(oap);
1918 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1919 u_save_cursor(); /* cursor line wasn't saved yet */
1920 auto_format(FALSE, TRUE);
1922 break;
1924 case OP_YANK:
1925 if (empty_region_error)
1927 if (!gui_yank)
1928 vim_beep();
1930 else
1931 (void)op_yank(oap, FALSE, !gui_yank);
1932 check_cursor_col();
1933 break;
1935 case OP_CHANGE:
1936 #ifdef FEAT_VISUAL
1937 VIsual_reselect = FALSE; /* don't reselect now */
1938 #endif
1939 if (empty_region_error)
1940 vim_beep();
1941 else
1943 /* This is a new edit command, not a restart. Need to
1944 * remember it to make 'insertmode' work with mappings for
1945 * Visual mode. But do this only once and not when typed and
1946 * 'insertmode' isn't set. */
1947 if (p_im || !KeyTyped)
1948 restart_edit_save = restart_edit;
1949 else
1950 restart_edit_save = 0;
1951 restart_edit = 0;
1952 /* Reset finish_op now, don't want it set inside edit(). */
1953 finish_op = FALSE;
1954 if (op_change(oap)) /* will call edit() */
1955 cap->retval |= CA_COMMAND_BUSY;
1956 if (restart_edit == 0)
1957 restart_edit = restart_edit_save;
1959 break;
1961 case OP_FILTER:
1962 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1963 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1964 else
1965 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
1967 case OP_INDENT:
1968 case OP_COLON:
1970 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1972 * If 'equalprg' is empty, do the indenting internally.
1974 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1976 # ifdef FEAT_LISP
1977 if (curbuf->b_p_lisp)
1979 op_reindent(oap, get_lisp_indent);
1980 break;
1982 # endif
1983 # ifdef FEAT_CINDENT
1984 op_reindent(oap,
1985 # ifdef FEAT_EVAL
1986 *curbuf->b_p_inde != NUL ? get_expr_indent :
1987 # endif
1988 get_c_indent);
1989 break;
1990 # endif
1992 #endif
1994 op_colon(oap);
1995 break;
1997 case OP_TILDE:
1998 case OP_UPPER:
1999 case OP_LOWER:
2000 case OP_ROT13:
2001 if (empty_region_error)
2002 vim_beep();
2003 else
2004 op_tilde(oap);
2005 check_cursor_col();
2006 break;
2008 case OP_FORMAT:
2009 #if defined(FEAT_EVAL)
2010 if (*curbuf->b_p_fex != NUL)
2011 op_formatexpr(oap); /* use expression */
2012 else
2013 #endif
2014 if (*p_fp != NUL)
2015 op_colon(oap); /* use external command */
2016 else
2017 op_format(oap, FALSE); /* use internal function */
2018 break;
2020 case OP_FORMAT2:
2021 op_format(oap, TRUE); /* use internal function */
2022 break;
2024 case OP_FUNCTION:
2025 op_function(oap); /* call 'operatorfunc' */
2026 break;
2028 case OP_INSERT:
2029 case OP_APPEND:
2030 #ifdef FEAT_VISUAL
2031 VIsual_reselect = FALSE; /* don't reselect now */
2032 #endif
2033 #ifdef FEAT_VISUALEXTRA
2034 if (empty_region_error)
2035 vim_beep();
2036 else
2038 /* This is a new edit command, not a restart. Need to
2039 * remember it to make 'insertmode' work with mappings for
2040 * Visual mode. But do this only once. */
2041 restart_edit_save = restart_edit;
2042 restart_edit = 0;
2044 op_insert(oap, cap->count1);
2046 /* TODO: when inserting in several lines, should format all
2047 * the lines. */
2048 auto_format(FALSE, TRUE);
2050 if (restart_edit == 0)
2051 restart_edit = restart_edit_save;
2053 #else
2054 vim_beep();
2055 #endif
2056 break;
2058 case OP_REPLACE:
2059 #ifdef FEAT_VISUAL
2060 VIsual_reselect = FALSE; /* don't reselect now */
2061 #endif
2062 #ifdef FEAT_VISUALEXTRA
2063 if (empty_region_error)
2064 #endif
2065 vim_beep();
2066 #ifdef FEAT_VISUALEXTRA
2067 else
2068 op_replace(oap, cap->nchar);
2069 #endif
2070 break;
2072 #ifdef FEAT_FOLDING
2073 case OP_FOLD:
2074 VIsual_reselect = FALSE; /* don't reselect now */
2075 foldCreate(oap->start.lnum, oap->end.lnum);
2076 break;
2078 case OP_FOLDOPEN:
2079 case OP_FOLDOPENREC:
2080 case OP_FOLDCLOSE:
2081 case OP_FOLDCLOSEREC:
2082 VIsual_reselect = FALSE; /* don't reselect now */
2083 opFoldRange(oap->start.lnum, oap->end.lnum,
2084 oap->op_type == OP_FOLDOPEN
2085 || oap->op_type == OP_FOLDOPENREC,
2086 oap->op_type == OP_FOLDOPENREC
2087 || oap->op_type == OP_FOLDCLOSEREC,
2088 oap->is_VIsual);
2089 break;
2091 case OP_FOLDDEL:
2092 case OP_FOLDDELREC:
2093 VIsual_reselect = FALSE; /* don't reselect now */
2094 deleteFold(oap->start.lnum, oap->end.lnum,
2095 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2096 break;
2097 #endif
2098 default:
2099 clearopbeep(oap);
2101 #ifdef FEAT_VIRTUALEDIT
2102 virtual_op = MAYBE;
2103 #endif
2104 if (!gui_yank)
2107 * if 'sol' not set, go back to old column for some commands
2109 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2110 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2111 || oap->op_type == OP_DELETE))
2112 coladvance(curwin->w_curswant = old_col);
2114 else
2116 curwin->w_cursor = old_cursor;
2118 #ifdef FEAT_VISUAL
2119 oap->block_mode = FALSE;
2120 #endif
2121 clearop(oap);
2126 * Handle indent and format operators and visual mode ":".
2128 static void
2129 op_colon(oap)
2130 oparg_T *oap;
2132 stuffcharReadbuff(':');
2133 #ifdef FEAT_VISUAL
2134 if (oap->is_VIsual)
2135 stuffReadbuff((char_u *)"'<,'>");
2136 else
2137 #endif
2140 * Make the range look nice, so it can be repeated.
2142 if (oap->start.lnum == curwin->w_cursor.lnum)
2143 stuffcharReadbuff('.');
2144 else
2145 stuffnumReadbuff((long)oap->start.lnum);
2146 if (oap->end.lnum != oap->start.lnum)
2148 stuffcharReadbuff(',');
2149 if (oap->end.lnum == curwin->w_cursor.lnum)
2150 stuffcharReadbuff('.');
2151 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2152 stuffcharReadbuff('$');
2153 else if (oap->start.lnum == curwin->w_cursor.lnum)
2155 stuffReadbuff((char_u *)".+");
2156 stuffnumReadbuff((long)oap->line_count - 1);
2158 else
2159 stuffnumReadbuff((long)oap->end.lnum);
2162 if (oap->op_type != OP_COLON)
2163 stuffReadbuff((char_u *)"!");
2164 if (oap->op_type == OP_INDENT)
2166 #ifndef FEAT_CINDENT
2167 if (*get_equalprg() == NUL)
2168 stuffReadbuff((char_u *)"indent");
2169 else
2170 #endif
2171 stuffReadbuff(get_equalprg());
2172 stuffReadbuff((char_u *)"\n");
2174 else if (oap->op_type == OP_FORMAT)
2176 if (*p_fp == NUL)
2177 stuffReadbuff((char_u *)"fmt");
2178 else
2179 stuffReadbuff(p_fp);
2180 stuffReadbuff((char_u *)"\n']");
2184 * do_cmdline() does the rest
2189 * Handle the "g@" operator: call 'operatorfunc'.
2191 /*ARGSUSED*/
2192 static void
2193 op_function(oap)
2194 oparg_T *oap;
2196 #ifdef FEAT_EVAL
2197 char_u *(argv[1]);
2199 if (*p_opfunc == NUL)
2200 EMSG(_("E774: 'operatorfunc' is empty"));
2201 else
2203 /* Set '[ and '] marks to text to be operated on. */
2204 curbuf->b_op_start = oap->start;
2205 curbuf->b_op_end = oap->end;
2206 if (oap->motion_type != MLINE && !oap->inclusive)
2207 /* Exclude the end position. */
2208 decl(&curbuf->b_op_end);
2210 if (oap->block_mode)
2211 argv[0] = (char_u *)"block";
2212 else if (oap->motion_type == MLINE)
2213 argv[0] = (char_u *)"line";
2214 else
2215 argv[0] = (char_u *)"char";
2216 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
2218 #else
2219 EMSG(_("E775: Eval feature not available"));
2220 #endif
2223 #if defined(FEAT_MOUSE) || defined(PROTO)
2225 * Do the appropriate action for the current mouse click in the current mode.
2226 * Not used for Command-line mode.
2228 * Normal Mode:
2229 * event modi- position visual change action
2230 * fier cursor window
2231 * left press - yes end yes
2232 * left press C yes end yes "^]" (2)
2233 * left press S yes end yes "*" (2)
2234 * left drag - yes start if moved no
2235 * left relse - yes start if moved no
2236 * middle press - yes if not active no put register
2237 * middle press - yes if active no yank and put
2238 * right press - yes start or extend yes
2239 * right press S yes no change yes "#" (2)
2240 * right drag - yes extend no
2241 * right relse - yes extend no
2243 * Insert or Replace Mode:
2244 * event modi- position visual change action
2245 * fier cursor window
2246 * left press - yes (cannot be active) yes
2247 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2248 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2249 * left drag - yes start or extend (1) no CTRL-O (1)
2250 * left relse - yes start or extend (1) no CTRL-O (1)
2251 * middle press - no (cannot be active) no put register
2252 * right press - yes start or extend yes CTRL-O
2253 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2255 * (1) only if mouse pointer moved since press
2256 * (2) only if click is in same buffer
2258 * Return TRUE if start_arrow() should be called for edit mode.
2261 do_mouse(oap, c, dir, count, fixindent)
2262 oparg_T *oap; /* operator argument, can be NULL */
2263 int c; /* K_LEFTMOUSE, etc */
2264 int dir; /* Direction to 'put' if necessary */
2265 long count;
2266 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2268 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2269 static int got_click = FALSE; /* got a click some time back */
2271 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2272 int is_click; /* If FALSE it's a drag or release event */
2273 int is_drag; /* If TRUE it's a drag event */
2274 int jump_flags = 0; /* flags for jump_to_mouse() */
2275 pos_T start_visual;
2276 int moved; /* Has cursor moved? */
2277 int in_status_line; /* mouse in status line */
2278 #ifdef FEAT_VERTSPLIT
2279 int in_sep_line; /* mouse in vertical separator line */
2280 #endif
2281 int c1, c2;
2282 #if defined(FEAT_FOLDING)
2283 pos_T save_cursor;
2284 #endif
2285 win_T *old_curwin = curwin;
2286 #ifdef FEAT_VISUAL
2287 static pos_T orig_cursor;
2288 colnr_T leftcol, rightcol;
2289 pos_T end_visual;
2290 int diff;
2291 int old_active = VIsual_active;
2292 int old_mode = VIsual_mode;
2293 #endif
2294 int regname;
2296 #if defined(FEAT_FOLDING)
2297 save_cursor = curwin->w_cursor;
2298 #endif
2301 * When GUI is active, always recognize mouse events, otherwise:
2302 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2303 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2304 * - For command line and insert mode 'mouse' is checked before calling
2305 * do_mouse().
2307 if (do_always)
2308 do_always = FALSE;
2309 else
2310 #ifdef FEAT_GUI
2311 if (!gui.in_use)
2312 #endif
2314 #ifdef FEAT_VISUAL
2315 if (VIsual_active)
2317 if (!mouse_has(MOUSE_VISUAL))
2318 return FALSE;
2320 else
2321 #endif
2322 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2323 return FALSE;
2326 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2328 #ifdef FEAT_MOUSESHAPE
2329 /* May have stopped dragging the status or separator line. The pointer is
2330 * most likely still on the status or separator line. */
2331 if (!is_drag && drag_status_line)
2333 drag_status_line = FALSE;
2334 update_mouseshape(SHAPE_IDX_STATUS);
2336 # ifdef FEAT_VERTSPLIT
2337 if (!is_drag && drag_sep_line)
2339 drag_sep_line = FALSE;
2340 update_mouseshape(SHAPE_IDX_VSEP);
2342 # endif
2343 #endif
2346 * Ignore drag and release events if we didn't get a click.
2348 if (is_click)
2349 got_click = TRUE;
2350 else
2352 if (!got_click) /* didn't get click, ignore */
2353 return FALSE;
2354 if (!is_drag) /* release, reset got_click */
2355 got_click = FALSE;
2358 #ifndef FEAT_VISUAL
2360 * ALT is only used for starging/extending Visual mode.
2362 if ((mod_mask & MOD_MASK_ALT))
2363 return FALSE;
2364 #endif
2367 * CTRL right mouse button does CTRL-T
2369 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2371 if (State & INSERT)
2372 stuffcharReadbuff(Ctrl_O);
2373 if (count > 1)
2374 stuffnumReadbuff(count);
2375 stuffcharReadbuff(Ctrl_T);
2376 got_click = FALSE; /* ignore drag&release now */
2377 return FALSE;
2381 * CTRL only works with left mouse button
2383 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2384 return FALSE;
2387 * When a modifier is down, ignore drag and release events, as well as
2388 * multiple clicks and the middle mouse button.
2389 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2391 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2392 | MOD_MASK_META))
2393 && (!is_click
2394 || (mod_mask & MOD_MASK_MULTI_CLICK)
2395 || which_button == MOUSE_MIDDLE)
2396 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
2397 && mouse_model_popup()
2398 && which_button == MOUSE_LEFT)
2399 && !((mod_mask & MOD_MASK_ALT)
2400 && !mouse_model_popup()
2401 && which_button == MOUSE_RIGHT)
2403 return FALSE;
2406 * If the button press was used as the movement command for an operator
2407 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2408 * drag/release events.
2410 if (!is_click && which_button == MOUSE_MIDDLE)
2411 return FALSE;
2413 if (oap != NULL)
2414 regname = oap->regname;
2415 else
2416 regname = 0;
2419 * Middle mouse button does a 'put' of the selected text
2421 if (which_button == MOUSE_MIDDLE)
2423 if (State == NORMAL)
2426 * If an operator was pending, we don't know what the user wanted
2427 * to do. Go back to normal mode: Clear the operator and beep().
2429 if (oap != NULL && oap->op_type != OP_NOP)
2431 clearopbeep(oap);
2432 return FALSE;
2435 #ifdef FEAT_VISUAL
2437 * If visual was active, yank the highlighted text and put it
2438 * before the mouse pointer position.
2439 * In Select mode replace the highlighted text with the clipboard.
2441 if (VIsual_active)
2443 if (VIsual_select)
2445 stuffcharReadbuff(Ctrl_G);
2446 stuffReadbuff((char_u *)"\"+p");
2448 else
2450 stuffcharReadbuff('y');
2451 stuffcharReadbuff(K_MIDDLEMOUSE);
2453 do_always = TRUE; /* ignore 'mouse' setting next time */
2454 return FALSE;
2456 #endif
2458 * The rest is below jump_to_mouse()
2462 else if ((State & INSERT) == 0)
2463 return FALSE;
2466 * Middle click in insert mode doesn't move the mouse, just insert the
2467 * contents of a register. '.' register is special, can't insert that
2468 * with do_put().
2469 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2470 * happens for the GUI).
2472 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2474 if (regname == '.')
2475 insert_reg(regname, TRUE);
2476 else
2478 #ifdef FEAT_CLIPBOARD
2479 if (clip_star.available && regname == 0)
2480 regname = '*';
2481 #endif
2482 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2483 insert_reg(regname, TRUE);
2484 else
2486 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2488 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2489 AppendCharToRedobuff(Ctrl_R);
2490 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2491 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2494 return FALSE;
2498 /* When dragging or button-up stay in the same window. */
2499 if (!is_click)
2500 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2502 start_visual.lnum = 0;
2504 #ifdef FEAT_WINDOWS
2505 /* Check for clicking in the tab page line. */
2506 if (mouse_row == 0 && firstwin->w_winrow > 0)
2508 if (is_drag)
2509 return FALSE;
2510 got_click = FALSE; /* ignore mouse-up and drag events */
2512 /* click in a tab selects that tab page */
2513 if (is_click
2514 # ifdef FEAT_CMDWIN
2515 && cmdwin_type == 0
2516 # endif
2517 && mouse_col < Columns)
2519 c1 = TabPageIdxs[mouse_col];
2520 if (c1 >= 0)
2522 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2524 /* double click opens new page */
2525 end_visual_mode();
2526 tabpage_new();
2527 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2529 else
2531 /* Go to specified tab page, or next one if not clicking
2532 * on a label. */
2533 goto_tabpage(c1);
2535 /* It's like clicking on the status line of a window. */
2536 if (curwin != old_curwin)
2537 end_visual_mode();
2540 else if (c1 < 0)
2542 tabpage_T *tp;
2544 /* Close the current or specified tab page. */
2545 if (c1 == -999)
2546 tp = curtab;
2547 else
2548 tp = find_tabpage(-c1);
2549 if (tp == curtab)
2551 if (first_tabpage->tp_next != NULL)
2552 tabpage_close(FALSE);
2554 else if (tp != NULL)
2555 tabpage_close_other(tp, FALSE);
2558 return TRUE;
2560 #endif
2563 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2564 * right button up -> pop-up menu
2565 * shift-left button -> right button
2566 * alt-left button -> alt-right button
2568 if (mouse_model_popup())
2570 if (which_button == MOUSE_RIGHT
2571 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2574 * NOTE: Ignore right button down and drag mouse events.
2575 * Windows only shows the popup menu on the button up event.
2577 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2578 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2579 if (!is_click)
2580 return FALSE;
2581 #endif
2582 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2583 if (is_click || is_drag)
2584 return FALSE;
2585 #endif
2586 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2587 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2588 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2589 if (gui.in_use)
2591 jump_flags = 0;
2592 if (STRCMP(p_mousem, "popup_setpos") == 0)
2594 /* First set the cursor position before showing the popup
2595 * menu. */
2596 #ifdef FEAT_VISUAL
2597 if (VIsual_active)
2599 pos_T m_pos;
2602 * set MOUSE_MAY_STOP_VIS if we are outside the
2603 * selection or the current window (might have false
2604 * negative here)
2606 if (mouse_row < W_WINROW(curwin)
2607 || mouse_row
2608 > (W_WINROW(curwin) + curwin->w_height))
2609 jump_flags = MOUSE_MAY_STOP_VIS;
2610 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2611 jump_flags = MOUSE_MAY_STOP_VIS;
2612 else
2614 if ((lt(curwin->w_cursor, VIsual)
2615 && (lt(m_pos, curwin->w_cursor)
2616 || lt(VIsual, m_pos)))
2617 || (lt(VIsual, curwin->w_cursor)
2618 && (lt(m_pos, VIsual)
2619 || lt(curwin->w_cursor, m_pos))))
2621 jump_flags = MOUSE_MAY_STOP_VIS;
2623 else if (VIsual_mode == Ctrl_V)
2625 getvcols(curwin, &curwin->w_cursor, &VIsual,
2626 &leftcol, &rightcol);
2627 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2628 if (m_pos.col < leftcol || m_pos.col > rightcol)
2629 jump_flags = MOUSE_MAY_STOP_VIS;
2633 else
2634 jump_flags = MOUSE_MAY_STOP_VIS;
2635 #endif
2637 if (jump_flags)
2639 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2640 update_curbuf(
2641 #ifdef FEAT_VISUAL
2642 VIsual_active ? INVERTED :
2643 #endif
2644 VALID);
2645 setcursor();
2646 out_flush(); /* Update before showing popup menu */
2648 # ifdef FEAT_MENU
2649 gui_show_popupmenu();
2650 # endif
2651 return (jump_flags & CURSOR_MOVED) != 0;
2653 else
2654 return FALSE;
2655 #else
2656 return FALSE;
2657 #endif
2659 if (which_button == MOUSE_LEFT
2660 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
2662 which_button = MOUSE_RIGHT;
2663 mod_mask &= ~MOD_MASK_SHIFT;
2667 #ifdef FEAT_VISUAL
2668 if ((State & (NORMAL | INSERT))
2669 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2671 if (which_button == MOUSE_LEFT)
2673 if (is_click)
2675 /* stop Visual mode for a left click in a window, but not when
2676 * on a status line */
2677 if (VIsual_active)
2678 jump_flags |= MOUSE_MAY_STOP_VIS;
2680 else if (mouse_has(MOUSE_VISUAL))
2681 jump_flags |= MOUSE_MAY_VIS;
2683 else if (which_button == MOUSE_RIGHT)
2685 if (is_click && VIsual_active)
2688 * Remember the start and end of visual before moving the
2689 * cursor.
2691 if (lt(curwin->w_cursor, VIsual))
2693 start_visual = curwin->w_cursor;
2694 end_visual = VIsual;
2696 else
2698 start_visual = VIsual;
2699 end_visual = curwin->w_cursor;
2702 jump_flags |= MOUSE_FOCUS;
2703 if (mouse_has(MOUSE_VISUAL))
2704 jump_flags |= MOUSE_MAY_VIS;
2707 #endif
2710 * If an operator is pending, ignore all drags and releases until the
2711 * next mouse click.
2713 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2715 got_click = FALSE;
2716 oap->motion_type = MCHAR;
2719 /* When releasing the button let jump_to_mouse() know. */
2720 if (!is_click && !is_drag)
2721 jump_flags |= MOUSE_RELEASED;
2724 * JUMP!
2726 jump_flags = jump_to_mouse(jump_flags,
2727 oap == NULL ? NULL : &(oap->inclusive), which_button);
2728 moved = (jump_flags & CURSOR_MOVED);
2729 in_status_line = (jump_flags & IN_STATUS_LINE);
2730 #ifdef FEAT_VERTSPLIT
2731 in_sep_line = (jump_flags & IN_SEP_LINE);
2732 #endif
2734 #ifdef FEAT_NETBEANS_INTG
2735 if (usingNetbeans && isNetbeansBuffer(curbuf)
2736 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2738 int key = KEY2TERMCAP1(c);
2740 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2741 || key == (int)KE_RIGHTRELEASE)
2742 netbeans_button_release(which_button);
2744 #endif
2746 /* When jumping to another window, clear a pending operator. That's a bit
2747 * friendlier than beeping and not jumping to that window. */
2748 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2749 clearop(oap);
2751 #ifdef FEAT_FOLDING
2752 if (mod_mask == 0
2753 && !is_drag
2754 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2755 && which_button == MOUSE_LEFT)
2757 /* open or close a fold at this line */
2758 if (jump_flags & MOUSE_FOLD_OPEN)
2759 openFold(curwin->w_cursor.lnum, 1L);
2760 else
2761 closeFold(curwin->w_cursor.lnum, 1L);
2762 /* don't move the cursor if still in the same window */
2763 if (curwin == old_curwin)
2764 curwin->w_cursor = save_cursor;
2766 #endif
2768 #if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2769 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2771 clip_modeless(which_button, is_click, is_drag);
2772 return FALSE;
2774 #endif
2776 #ifdef FEAT_VISUAL
2777 /* Set global flag that we are extending the Visual area with mouse
2778 * dragging; temporarily minimize 'scrolloff'. */
2779 if (VIsual_active && is_drag && p_so)
2781 /* In the very first line, allow scrolling one line */
2782 if (mouse_row == 0)
2783 mouse_dragging = 2;
2784 else
2785 mouse_dragging = 1;
2788 /* When dragging the mouse above the window, scroll down. */
2789 if (is_drag && mouse_row < 0 && !in_status_line)
2791 scroll_redraw(FALSE, 1L);
2792 mouse_row = 0;
2795 if (start_visual.lnum) /* right click in visual mode */
2797 /* When ALT is pressed make Visual mode blockwise. */
2798 if (mod_mask & MOD_MASK_ALT)
2799 VIsual_mode = Ctrl_V;
2802 * In Visual-block mode, divide the area in four, pick up the corner
2803 * that is in the quarter that the cursor is in.
2805 if (VIsual_mode == Ctrl_V)
2807 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2808 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2809 end_visual.col = leftcol;
2810 else
2811 end_visual.col = rightcol;
2812 if (curwin->w_cursor.lnum <
2813 (start_visual.lnum + end_visual.lnum) / 2)
2814 end_visual.lnum = end_visual.lnum;
2815 else
2816 end_visual.lnum = start_visual.lnum;
2818 /* move VIsual to the right column */
2819 start_visual = curwin->w_cursor; /* save the cursor pos */
2820 curwin->w_cursor = end_visual;
2821 coladvance(end_visual.col);
2822 VIsual = curwin->w_cursor;
2823 curwin->w_cursor = start_visual; /* restore the cursor */
2825 else
2828 * If the click is before the start of visual, change the start.
2829 * If the click is after the end of visual, change the end. If
2830 * the click is inside the visual, change the closest side.
2832 if (lt(curwin->w_cursor, start_visual))
2833 VIsual = end_visual;
2834 else if (lt(end_visual, curwin->w_cursor))
2835 VIsual = start_visual;
2836 else
2838 /* In the same line, compare column number */
2839 if (end_visual.lnum == start_visual.lnum)
2841 if (curwin->w_cursor.col - start_visual.col >
2842 end_visual.col - curwin->w_cursor.col)
2843 VIsual = start_visual;
2844 else
2845 VIsual = end_visual;
2848 /* In different lines, compare line number */
2849 else
2851 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2852 (end_visual.lnum - curwin->w_cursor.lnum);
2854 if (diff > 0) /* closest to end */
2855 VIsual = start_visual;
2856 else if (diff < 0) /* closest to start */
2857 VIsual = end_visual;
2858 else /* in the middle line */
2860 if (curwin->w_cursor.col <
2861 (start_visual.col + end_visual.col) / 2)
2862 VIsual = end_visual;
2863 else
2864 VIsual = start_visual;
2871 * If Visual mode started in insert mode, execute "CTRL-O"
2873 else if ((State & INSERT) && VIsual_active)
2874 stuffcharReadbuff(Ctrl_O);
2875 #endif
2878 * Middle mouse click: Put text before cursor.
2880 if (which_button == MOUSE_MIDDLE)
2882 #ifdef FEAT_CLIPBOARD
2883 if (clip_star.available && regname == 0)
2884 regname = '*';
2885 #endif
2886 if (yank_register_mline(regname))
2888 if (mouse_past_bottom)
2889 dir = FORWARD;
2891 else if (mouse_past_eol)
2892 dir = FORWARD;
2894 if (fixindent)
2896 c1 = (dir == BACKWARD) ? '[' : ']';
2897 c2 = 'p';
2899 else
2901 c1 = (dir == FORWARD) ? 'p' : 'P';
2902 c2 = NUL;
2904 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2907 * Remember where the paste started, so in edit() Insstart can be set
2908 * to this position
2910 if (restart_edit != 0)
2911 where_paste_started = curwin->w_cursor;
2912 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2915 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2917 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2918 * error under the mouse pointer.
2920 else if (((mod_mask & MOD_MASK_CTRL)
2921 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2922 && bt_quickfix(curbuf))
2924 if (State & INSERT)
2925 stuffcharReadbuff(Ctrl_O);
2926 if (curwin->w_llist_ref == NULL) /* quickfix window */
2927 stuffReadbuff((char_u *)":.cc\n");
2928 else /* location list window */
2929 stuffReadbuff((char_u *)":.ll\n");
2930 got_click = FALSE; /* ignore drag&release now */
2932 #endif
2935 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2936 * under the mouse pointer.
2938 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2939 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2941 if (State & INSERT)
2942 stuffcharReadbuff(Ctrl_O);
2943 stuffcharReadbuff(Ctrl_RSB);
2944 got_click = FALSE; /* ignore drag&release now */
2948 * Shift-Mouse click searches for the next occurrence of the word under
2949 * the mouse pointer
2951 else if ((mod_mask & MOD_MASK_SHIFT))
2953 if (State & INSERT
2954 #ifdef FEAT_VISUAL
2955 || (VIsual_active && VIsual_select)
2956 #endif
2958 stuffcharReadbuff(Ctrl_O);
2959 if (which_button == MOUSE_LEFT)
2960 stuffcharReadbuff('*');
2961 else /* MOUSE_RIGHT */
2962 stuffcharReadbuff('#');
2965 /* Handle double clicks, unless on status line */
2966 else if (in_status_line)
2968 #ifdef FEAT_MOUSESHAPE
2969 if ((is_drag || is_click) && !drag_status_line)
2971 drag_status_line = TRUE;
2972 update_mouseshape(-1);
2974 #endif
2976 #ifdef FEAT_VERTSPLIT
2977 else if (in_sep_line)
2979 # ifdef FEAT_MOUSESHAPE
2980 if ((is_drag || is_click) && !drag_sep_line)
2982 drag_sep_line = TRUE;
2983 update_mouseshape(-1);
2985 # endif
2987 #endif
2988 #ifdef FEAT_VISUAL
2989 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
2990 && mouse_has(MOUSE_VISUAL))
2992 if (is_click || !VIsual_active)
2994 if (VIsual_active)
2995 orig_cursor = VIsual;
2996 else
2998 check_visual_highlight();
2999 VIsual = curwin->w_cursor;
3000 orig_cursor = VIsual;
3001 VIsual_active = TRUE;
3002 VIsual_reselect = TRUE;
3003 /* start Select mode if 'selectmode' contains "mouse" */
3004 may_start_select('o');
3005 setmouse();
3007 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3009 /* Double click with ALT pressed makes it blockwise. */
3010 if (mod_mask & MOD_MASK_ALT)
3011 VIsual_mode = Ctrl_V;
3012 else
3013 VIsual_mode = 'v';
3015 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3016 VIsual_mode = 'V';
3017 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3018 VIsual_mode = Ctrl_V;
3019 #ifdef FEAT_CLIPBOARD
3020 /* Make sure the clipboard gets updated. Needed because start and
3021 * end may still be the same, and the selection needs to be owned */
3022 clip_star.vmode = NUL;
3023 #endif
3026 * A double click selects a word or a block.
3028 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3030 pos_T *pos = NULL;
3031 int gc;
3033 if (is_click)
3035 /* If the character under the cursor (skipping white space) is
3036 * not a word character, try finding a match and select a (),
3037 * {}, [], #if/#endif, etc. block. */
3038 end_visual = curwin->w_cursor;
3039 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
3040 inc(&end_visual);
3041 if (oap != NULL)
3042 oap->motion_type = MCHAR;
3043 if (oap != NULL
3044 && VIsual_mode == 'v'
3045 && !vim_iswordc(gchar_pos(&end_visual))
3046 && equalpos(curwin->w_cursor, VIsual)
3047 && (pos = findmatch(oap, NUL)) != NULL)
3049 curwin->w_cursor = *pos;
3050 if (oap->motion_type == MLINE)
3051 VIsual_mode = 'V';
3052 else if (*p_sel == 'e')
3054 if (lt(curwin->w_cursor, VIsual))
3055 ++VIsual.col;
3056 else
3057 ++curwin->w_cursor.col;
3062 if (pos == NULL && (is_click || is_drag))
3064 /* When not found a match or when dragging: extend to include
3065 * a word. */
3066 if (lt(curwin->w_cursor, orig_cursor))
3068 find_start_of_word(&curwin->w_cursor);
3069 find_end_of_word(&VIsual);
3071 else
3073 find_start_of_word(&VIsual);
3074 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3075 #ifdef FEAT_MBYTE
3076 curwin->w_cursor.col +=
3077 (*mb_ptr2len)(ml_get_cursor());
3078 #else
3079 ++curwin->w_cursor.col;
3080 #endif
3081 find_end_of_word(&curwin->w_cursor);
3084 curwin->w_set_curswant = TRUE;
3086 if (is_click)
3087 redraw_curbuf_later(INVERTED); /* update the inversion */
3089 else if (VIsual_active && !old_active)
3091 if (mod_mask & MOD_MASK_ALT)
3092 VIsual_mode = Ctrl_V;
3093 else
3094 VIsual_mode = 'v';
3097 /* If Visual mode changed show it later. */
3098 if ((!VIsual_active && old_active && mode_displayed)
3099 || (VIsual_active && p_smd && msg_silent == 0
3100 && (!old_active || VIsual_mode != old_mode)))
3101 redraw_cmdline = TRUE;
3102 #endif
3104 return moved;
3107 #ifdef FEAT_VISUAL
3109 * Move "pos" back to the start of the word it's in.
3111 static void
3112 find_start_of_word(pos)
3113 pos_T *pos;
3115 char_u *line;
3116 int cclass;
3117 int col;
3119 line = ml_get(pos->lnum);
3120 cclass = get_mouse_class(line + pos->col);
3122 while (pos->col > 0)
3124 col = pos->col - 1;
3125 #ifdef FEAT_MBYTE
3126 col -= (*mb_head_off)(line, line + col);
3127 #endif
3128 if (get_mouse_class(line + col) != cclass)
3129 break;
3130 pos->col = col;
3135 * Move "pos" forward to the end of the word it's in.
3136 * When 'selection' is "exclusive", the position is just after the word.
3138 static void
3139 find_end_of_word(pos)
3140 pos_T *pos;
3142 char_u *line;
3143 int cclass;
3144 int col;
3146 line = ml_get(pos->lnum);
3147 if (*p_sel == 'e' && pos->col > 0)
3149 --pos->col;
3150 #ifdef FEAT_MBYTE
3151 pos->col -= (*mb_head_off)(line, line + pos->col);
3152 #endif
3154 cclass = get_mouse_class(line + pos->col);
3155 while (line[pos->col] != NUL)
3157 #ifdef FEAT_MBYTE
3158 col = pos->col + (*mb_ptr2len)(line + pos->col);
3159 #else
3160 col = pos->col + 1;
3161 #endif
3162 if (get_mouse_class(line + col) != cclass)
3164 if (*p_sel == 'e')
3165 pos->col = col;
3166 break;
3168 pos->col = col;
3173 * Get class of a character for selection: same class means same word.
3174 * 0: blank
3175 * 1: punctuation groups
3176 * 2: normal word character
3177 * >2: multi-byte word character.
3179 static int
3180 get_mouse_class(p)
3181 char_u *p;
3183 int c;
3185 #ifdef FEAT_MBYTE
3186 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3187 return mb_get_class(p);
3188 #endif
3190 c = *p;
3191 if (c == ' ' || c == '\t')
3192 return 0;
3194 if (vim_iswordc(c))
3195 return 2;
3198 * There are a few special cases where we want certain combinations of
3199 * characters to be considered as a single word. These are things like
3200 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
3201 * character is in it's own class.
3203 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3204 return 1;
3205 return c;
3207 #endif /* FEAT_VISUAL */
3208 #endif /* FEAT_MOUSE */
3210 #if defined(FEAT_VISUAL) || defined(PROTO)
3212 * Check if highlighting for visual mode is possible, give a warning message
3213 * if not.
3215 void
3216 check_visual_highlight()
3218 static int did_check = FALSE;
3220 if (full_screen)
3222 if (!did_check && hl_attr(HLF_V) == 0)
3223 MSG(_("Warning: terminal cannot highlight"));
3224 did_check = TRUE;
3229 * End Visual mode.
3230 * This function should ALWAYS be called to end Visual mode, except from
3231 * do_pending_operator().
3233 void
3234 end_visual_mode()
3236 #ifdef FEAT_CLIPBOARD
3238 * If we are using the clipboard, then remember what was selected in case
3239 * we need to paste it somewhere while we still own the selection.
3240 * Only do this when the clipboard is already owned. Don't want to grab
3241 * the selection when hitting ESC.
3243 if (clip_star.available && clip_star.owned)
3244 clip_auto_select();
3245 #endif
3247 VIsual_active = FALSE;
3248 #ifdef FEAT_MOUSE
3249 setmouse();
3250 mouse_dragging = 0;
3251 #endif
3253 /* Save the current VIsual area for '< and '> marks, and "gv" */
3254 curbuf->b_visual.vi_mode = VIsual_mode;
3255 curbuf->b_visual.vi_start = VIsual;
3256 curbuf->b_visual.vi_end = curwin->w_cursor;
3257 curbuf->b_visual.vi_curswant = curwin->w_curswant;
3258 #ifdef FEAT_EVAL
3259 curbuf->b_visual_mode_eval = VIsual_mode;
3260 #endif
3261 #ifdef FEAT_VIRTUALEDIT
3262 if (!virtual_active())
3263 curwin->w_cursor.coladd = 0;
3264 #endif
3266 if (mode_displayed)
3267 clear_cmdline = TRUE; /* unshow visual mode later */
3268 #ifdef FEAT_CMDL_INFO
3269 else
3270 clear_showcmd();
3271 #endif
3273 adjust_cursor_eol();
3277 * Reset VIsual_active and VIsual_reselect.
3279 void
3280 reset_VIsual_and_resel()
3282 if (VIsual_active)
3284 end_visual_mode();
3285 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3287 VIsual_reselect = FALSE;
3291 * Reset VIsual_active and VIsual_reselect if it's set.
3293 void
3294 reset_VIsual()
3296 if (VIsual_active)
3298 end_visual_mode();
3299 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3300 VIsual_reselect = FALSE;
3303 #endif /* FEAT_VISUAL */
3305 #if defined(FEAT_BEVAL)
3306 static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3309 * Check for a balloon-eval special item to include when searching for an
3310 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3311 * Returns TRUE if the character at "*ptr" should be included.
3312 * "dir" is FORWARD or BACKWARD, the direction of searching.
3313 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3314 * "bnp" points to a counter for square brackets.
3316 static int
3317 find_is_eval_item(ptr, colp, bnp, dir)
3318 char_u *ptr;
3319 int *colp;
3320 int *bnp;
3321 int dir;
3323 /* Accept everything inside []. */
3324 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3325 ++*bnp;
3326 if (*bnp > 0)
3328 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3329 --*bnp;
3330 return TRUE;
3333 /* skip over "s.var" */
3334 if (*ptr == '.')
3335 return TRUE;
3337 /* two-character item: s->var */
3338 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3339 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3341 *colp += dir;
3342 return TRUE;
3344 return FALSE;
3346 #endif
3349 * Find the identifier under or to the right of the cursor.
3350 * "find_type" can have one of three values:
3351 * FIND_IDENT: find an identifier (keyword)
3352 * FIND_STRING: find any non-white string
3353 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
3354 * FIND_EVAL: find text useful for C program debugging
3356 * There are three steps:
3357 * 1. Search forward for the start of an identifier/string. Doesn't move if
3358 * already on one.
3359 * 2. Search backward for the start of this identifier/string.
3360 * This doesn't match the real Vi but I like it a little better and it
3361 * shouldn't bother anyone.
3362 * 3. Search forward to the end of this identifier/string.
3363 * When FIND_IDENT isn't defined, we backup until a blank.
3365 * Returns the length of the string, or zero if no string is found.
3366 * If a string is found, a pointer to the string is put in "*string". This
3367 * string is not always NUL terminated.
3370 find_ident_under_cursor(string, find_type)
3371 char_u **string;
3372 int find_type;
3374 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3375 curwin->w_cursor.col, string, find_type);
3379 * Like find_ident_under_cursor(), but for any window and any position.
3380 * However: Uses 'iskeyword' from the current window!.
3383 find_ident_at_pos(wp, lnum, startcol, string, find_type)
3384 win_T *wp;
3385 linenr_T lnum;
3386 colnr_T startcol;
3387 char_u **string;
3388 int find_type;
3390 char_u *ptr;
3391 int col = 0; /* init to shut up GCC */
3392 int i;
3393 #ifdef FEAT_MBYTE
3394 int this_class = 0;
3395 int prev_class;
3396 int prevcol;
3397 #endif
3398 #if defined(FEAT_BEVAL)
3399 int bn = 0; /* bracket nesting */
3400 #endif
3403 * if i == 0: try to find an identifier
3404 * if i == 1: try to find any non-white string
3406 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3407 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3410 * 1. skip to start of identifier/string
3412 col = startcol;
3413 #ifdef FEAT_MBYTE
3414 if (has_mbyte)
3416 while (ptr[col] != NUL)
3418 # if defined(FEAT_BEVAL)
3419 /* Stop at a ']' to evaluate "a[x]". */
3420 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3421 break;
3422 # endif
3423 this_class = mb_get_class(ptr + col);
3424 if (this_class != 0 && (i == 1 || this_class != 1))
3425 break;
3426 col += (*mb_ptr2len)(ptr + col);
3429 else
3430 #endif
3431 while (ptr[col] != NUL
3432 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
3433 # if defined(FEAT_BEVAL)
3434 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3435 # endif
3437 ++col;
3439 #if defined(FEAT_BEVAL)
3440 /* When starting on a ']' count it, so that we include the '['. */
3441 bn = ptr[col] == ']';
3442 #endif
3445 * 2. Back up to start of identifier/string.
3447 #ifdef FEAT_MBYTE
3448 if (has_mbyte)
3450 /* Remember class of character under cursor. */
3451 # if defined(FEAT_BEVAL)
3452 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3453 this_class = mb_get_class((char_u *)"a");
3454 else
3455 # endif
3456 this_class = mb_get_class(ptr + col);
3457 while (col > 0 && this_class != 0)
3459 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3460 prev_class = mb_get_class(ptr + prevcol);
3461 if (this_class != prev_class
3462 && (i == 0
3463 || prev_class == 0
3464 || (find_type & FIND_IDENT))
3465 # if defined(FEAT_BEVAL)
3466 && (!(find_type & FIND_EVAL)
3467 || prevcol == 0
3468 || !find_is_eval_item(ptr + prevcol, &prevcol,
3469 &bn, BACKWARD))
3470 # endif
3472 break;
3473 col = prevcol;
3476 /* If we don't want just any old string, or we've found an
3477 * identifier, stop searching. */
3478 if (this_class > 2)
3479 this_class = 2;
3480 if (!(find_type & FIND_STRING) || this_class == 2)
3481 break;
3483 else
3484 #endif
3486 while (col > 0
3487 && ((i == 0
3488 ? vim_iswordc(ptr[col - 1])
3489 : (!vim_iswhite(ptr[col - 1])
3490 && (!(find_type & FIND_IDENT)
3491 || !vim_iswordc(ptr[col - 1]))))
3492 #if defined(FEAT_BEVAL)
3493 || ((find_type & FIND_EVAL)
3494 && col > 1
3495 && find_is_eval_item(ptr + col - 1, &col,
3496 &bn, BACKWARD))
3497 #endif
3499 --col;
3501 /* If we don't want just any old string, or we've found an
3502 * identifier, stop searching. */
3503 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3504 break;
3508 if (ptr[col] == NUL || (i == 0 && (
3509 #ifdef FEAT_MBYTE
3510 has_mbyte ? this_class != 2 :
3511 #endif
3512 !vim_iswordc(ptr[col]))))
3515 * didn't find an identifier or string
3517 if (find_type & FIND_STRING)
3518 EMSG(_("E348: No string under cursor"));
3519 else
3520 EMSG(_(e_noident));
3521 return 0;
3523 ptr += col;
3524 *string = ptr;
3527 * 3. Find the end if the identifier/string.
3529 #if defined(FEAT_BEVAL)
3530 bn = 0;
3531 startcol -= col;
3532 #endif
3533 col = 0;
3534 #ifdef FEAT_MBYTE
3535 if (has_mbyte)
3537 /* Search for point of changing multibyte character class. */
3538 this_class = mb_get_class(ptr);
3539 while (ptr[col] != NUL
3540 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3541 : mb_get_class(ptr + col) != 0)
3542 # if defined(FEAT_BEVAL)
3543 || ((find_type & FIND_EVAL)
3544 && col <= (int)startcol
3545 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3546 # endif
3548 col += (*mb_ptr2len)(ptr + col);
3550 else
3551 #endif
3552 while ((i == 0 ? vim_iswordc(ptr[col])
3553 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
3554 # if defined(FEAT_BEVAL)
3555 || ((find_type & FIND_EVAL)
3556 && col <= (int)startcol
3557 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3558 # endif
3561 ++col;
3564 return col;
3568 * Prepare for redo of a normal command.
3570 static void
3571 prep_redo_cmd(cap)
3572 cmdarg_T *cap;
3574 prep_redo(cap->oap->regname, cap->count0,
3575 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3579 * Prepare for redo of any command.
3580 * Note that only the last argument can be a multi-byte char.
3582 static void
3583 prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3584 int regname;
3585 long num;
3586 int cmd1;
3587 int cmd2;
3588 int cmd3;
3589 int cmd4;
3590 int cmd5;
3592 ResetRedobuff();
3593 if (regname != 0) /* yank from specified buffer */
3595 AppendCharToRedobuff('"');
3596 AppendCharToRedobuff(regname);
3598 if (num)
3599 AppendNumberToRedobuff(num);
3601 if (cmd1 != NUL)
3602 AppendCharToRedobuff(cmd1);
3603 if (cmd2 != NUL)
3604 AppendCharToRedobuff(cmd2);
3605 if (cmd3 != NUL)
3606 AppendCharToRedobuff(cmd3);
3607 if (cmd4 != NUL)
3608 AppendCharToRedobuff(cmd4);
3609 if (cmd5 != NUL)
3610 AppendCharToRedobuff(cmd5);
3614 * check for operator active and clear it
3616 * return TRUE if operator was active
3618 static int
3619 checkclearop(oap)
3620 oparg_T *oap;
3622 if (oap->op_type == OP_NOP)
3623 return FALSE;
3624 clearopbeep(oap);
3625 return TRUE;
3629 * Check for operator or Visual active. Clear active operator.
3631 * Return TRUE if operator or Visual was active.
3633 static int
3634 checkclearopq(oap)
3635 oparg_T *oap;
3637 if (oap->op_type == OP_NOP
3638 #ifdef FEAT_VISUAL
3639 && !VIsual_active
3640 #endif
3642 return FALSE;
3643 clearopbeep(oap);
3644 return TRUE;
3647 static void
3648 clearop(oap)
3649 oparg_T *oap;
3651 oap->op_type = OP_NOP;
3652 oap->regname = 0;
3653 oap->motion_force = NUL;
3654 oap->use_reg_one = FALSE;
3657 static void
3658 clearopbeep(oap)
3659 oparg_T *oap;
3661 clearop(oap);
3662 beep_flush();
3665 #ifdef FEAT_VISUAL
3667 * Remove the shift modifier from a special key.
3669 static void
3670 unshift_special(cap)
3671 cmdarg_T *cap;
3673 switch (cap->cmdchar)
3675 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3676 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3677 case K_S_UP: cap->cmdchar = K_UP; break;
3678 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3679 case K_S_HOME: cap->cmdchar = K_HOME; break;
3680 case K_S_END: cap->cmdchar = K_END; break;
3682 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3684 #endif
3686 #if defined(FEAT_CMDL_INFO) || defined(PROTO)
3688 * Routines for displaying a partly typed command
3691 #ifdef FEAT_VISUAL /* need room for size of Visual area */
3692 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3693 #else
3694 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3695 #endif
3696 static char_u showcmd_buf[SHOWCMD_BUFLEN];
3697 static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3698 static int showcmd_is_clear = TRUE;
3699 static int showcmd_visual = FALSE;
3701 static void display_showcmd __ARGS((void));
3703 void
3704 clear_showcmd()
3706 if (!p_sc)
3707 return;
3709 #ifdef FEAT_VISUAL
3710 if (VIsual_active && !char_avail())
3712 int i = lt(VIsual, curwin->w_cursor);
3713 long lines;
3714 colnr_T leftcol, rightcol;
3715 linenr_T top, bot;
3717 /* Show the size of the Visual area. */
3718 if (i)
3720 top = VIsual.lnum;
3721 bot = curwin->w_cursor.lnum;
3723 else
3725 top = curwin->w_cursor.lnum;
3726 bot = VIsual.lnum;
3728 # ifdef FEAT_FOLDING
3729 /* Include closed folds as a whole. */
3730 hasFolding(top, &top, NULL);
3731 hasFolding(bot, NULL, &bot);
3732 # endif
3733 lines = bot - top + 1;
3735 if (VIsual_mode == Ctrl_V)
3737 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
3738 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3739 (long)(rightcol - leftcol + 1));
3741 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3742 sprintf((char *)showcmd_buf, "%ld", lines);
3743 else
3744 sprintf((char *)showcmd_buf, "%ld", (long)(i
3745 ? curwin->w_cursor.col - VIsual.col
3746 : VIsual.col - curwin->w_cursor.col) + (*p_sel != 'e'));
3747 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3748 showcmd_visual = TRUE;
3750 else
3751 #endif
3753 showcmd_buf[0] = NUL;
3754 showcmd_visual = FALSE;
3756 /* Don't actually display something if there is nothing to clear. */
3757 if (showcmd_is_clear)
3758 return;
3761 display_showcmd();
3765 * Add 'c' to string of shown command chars.
3766 * Return TRUE if output has been written (and setcursor() has been called).
3769 add_to_showcmd(c)
3770 int c;
3772 char_u *p;
3773 int old_len;
3774 int extra_len;
3775 int overflow;
3776 #if defined(FEAT_MOUSE)
3777 int i;
3778 static int ignore[] =
3780 # ifdef FEAT_GUI
3781 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3782 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
3783 # endif
3784 K_IGNORE,
3785 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3786 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3787 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
3788 K_MOUSEDOWN, K_MOUSEUP,
3789 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
3790 K_CURSORHOLD,
3793 #endif
3795 if (!p_sc || msg_silent != 0)
3796 return FALSE;
3798 if (showcmd_visual)
3800 showcmd_buf[0] = NUL;
3801 showcmd_visual = FALSE;
3804 #if defined(FEAT_MOUSE)
3805 /* Ignore keys that are scrollbar updates and mouse clicks */
3806 if (IS_SPECIAL(c))
3807 for (i = 0; ignore[i] != 0; ++i)
3808 if (ignore[i] == c)
3809 return FALSE;
3810 #endif
3812 p = transchar(c);
3813 old_len = (int)STRLEN(showcmd_buf);
3814 extra_len = (int)STRLEN(p);
3815 overflow = old_len + extra_len - SHOWCMD_COLS;
3816 if (overflow > 0)
3817 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3818 old_len - overflow + 1);
3819 STRCAT(showcmd_buf, p);
3821 if (char_avail())
3822 return FALSE;
3824 display_showcmd();
3826 return TRUE;
3829 void
3830 add_to_showcmd_c(c)
3831 int c;
3833 if (!add_to_showcmd(c))
3834 setcursor();
3838 * Delete 'len' characters from the end of the shown command.
3840 static void
3841 del_from_showcmd(len)
3842 int len;
3844 int old_len;
3846 if (!p_sc)
3847 return;
3849 old_len = (int)STRLEN(showcmd_buf);
3850 if (len > old_len)
3851 len = old_len;
3852 showcmd_buf[old_len - len] = NUL;
3854 if (!char_avail())
3855 display_showcmd();
3859 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3860 * something and there is a partial mapping.
3862 void
3863 push_showcmd()
3865 if (p_sc)
3866 STRCPY(old_showcmd_buf, showcmd_buf);
3869 void
3870 pop_showcmd()
3872 if (!p_sc)
3873 return;
3875 STRCPY(showcmd_buf, old_showcmd_buf);
3877 display_showcmd();
3880 static void
3881 display_showcmd()
3883 int len;
3885 cursor_off();
3887 len = (int)STRLEN(showcmd_buf);
3888 if (len == 0)
3889 showcmd_is_clear = TRUE;
3890 else
3892 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3893 showcmd_is_clear = FALSE;
3897 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3898 * spaces
3900 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3902 setcursor(); /* put cursor back where it belongs */
3904 #endif
3906 #ifdef FEAT_SCROLLBIND
3908 * When "check" is FALSE, prepare for commands that scroll the window.
3909 * When "check" is TRUE, take care of scroll-binding after the window has
3910 * scrolled. Called from normal_cmd() and edit().
3912 void
3913 do_check_scrollbind(check)
3914 int check;
3916 static win_T *old_curwin = NULL;
3917 static linenr_T old_topline = 0;
3918 #ifdef FEAT_DIFF
3919 static int old_topfill = 0;
3920 #endif
3921 static buf_T *old_buf = NULL;
3922 static colnr_T old_leftcol = 0;
3924 if (check && curwin->w_p_scb)
3926 /* If a ":syncbind" command was just used, don't scroll, only reset
3927 * the values. */
3928 if (did_syncbind)
3929 did_syncbind = FALSE;
3930 else if (curwin == old_curwin)
3933 * Synchronize other windows, as necessary according to
3934 * 'scrollbind'. Don't do this after an ":edit" command, except
3935 * when 'diff' is set.
3937 if ((curwin->w_buffer == old_buf
3938 #ifdef FEAT_DIFF
3939 || curwin->w_p_diff
3940 #endif
3942 && (curwin->w_topline != old_topline
3943 #ifdef FEAT_DIFF
3944 || curwin->w_topfill != old_topfill
3945 #endif
3946 || curwin->w_leftcol != old_leftcol))
3948 check_scrollbind(curwin->w_topline - old_topline,
3949 (long)(curwin->w_leftcol - old_leftcol));
3952 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
3955 * When switching between windows, make sure that the relative
3956 * vertical offset is valid for the new window. The relative
3957 * offset is invalid whenever another 'scrollbind' window has
3958 * scrolled to a point that would force the current window to
3959 * scroll past the beginning or end of its buffer. When the
3960 * resync is performed, some of the other 'scrollbind' windows may
3961 * need to jump so that the current window's relative position is
3962 * visible on-screen.
3964 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
3966 curwin->w_scbind_pos = curwin->w_topline;
3969 old_curwin = curwin;
3970 old_topline = curwin->w_topline;
3971 #ifdef FEAT_DIFF
3972 old_topfill = curwin->w_topfill;
3973 #endif
3974 old_buf = curwin->w_buffer;
3975 old_leftcol = curwin->w_leftcol;
3979 * Synchronize any windows that have "scrollbind" set, based on the
3980 * number of rows by which the current window has changed
3981 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3983 void
3984 check_scrollbind(topline_diff, leftcol_diff)
3985 linenr_T topline_diff;
3986 long leftcol_diff;
3988 int want_ver;
3989 int want_hor;
3990 win_T *old_curwin = curwin;
3991 buf_T *old_curbuf = curbuf;
3992 #ifdef FEAT_VISUAL
3993 int old_VIsual_select = VIsual_select;
3994 int old_VIsual_active = VIsual_active;
3995 #endif
3996 colnr_T tgt_leftcol = curwin->w_leftcol;
3997 long topline;
3998 long y;
4001 * check 'scrollopt' string for vertical and horizontal scroll options
4003 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4004 #ifdef FEAT_DIFF
4005 want_ver |= old_curwin->w_p_diff;
4006 #endif
4007 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4010 * loop through the scrollbound windows and scroll accordingly
4012 #ifdef FEAT_VISUAL
4013 VIsual_select = VIsual_active = 0;
4014 #endif
4015 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4017 curbuf = curwin->w_buffer;
4018 /* skip original window and windows with 'noscrollbind' */
4019 if (curwin != old_curwin && curwin->w_p_scb)
4022 * do the vertical scroll
4024 if (want_ver)
4026 #ifdef FEAT_DIFF
4027 if (old_curwin->w_p_diff && curwin->w_p_diff)
4029 diff_set_topline(old_curwin, curwin);
4031 else
4032 #endif
4034 curwin->w_scbind_pos += topline_diff;
4035 topline = curwin->w_scbind_pos;
4036 if (topline > curbuf->b_ml.ml_line_count)
4037 topline = curbuf->b_ml.ml_line_count;
4038 if (topline < 1)
4039 topline = 1;
4041 y = topline - curwin->w_topline;
4042 if (y > 0)
4043 scrollup(y, FALSE);
4044 else
4045 scrolldown(-y, FALSE);
4048 redraw_later(VALID);
4049 cursor_correct();
4050 #ifdef FEAT_WINDOWS
4051 curwin->w_redr_status = TRUE;
4052 #endif
4056 * do the horizontal scroll
4058 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4060 curwin->w_leftcol = tgt_leftcol;
4061 leftcol_changed();
4067 * reset current-window
4069 #ifdef FEAT_VISUAL
4070 VIsual_select = old_VIsual_select;
4071 VIsual_active = old_VIsual_active;
4072 #endif
4073 curwin = old_curwin;
4074 curbuf = old_curbuf;
4076 #endif /* #ifdef FEAT_SCROLLBIND */
4079 * Command character that's ignored.
4080 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
4081 * xon/xoff
4083 static void
4084 nv_ignore(cap)
4085 cmdarg_T *cap;
4087 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
4091 * Command character that doesn't do anything, but unlike nv_ignore() does
4092 * start edit(). Used for "startinsert" executed while starting up.
4094 /*ARGSUSED */
4095 static void
4096 nv_nop(cap)
4097 cmdarg_T *cap;
4102 * Command character doesn't exist.
4104 static void
4105 nv_error(cap)
4106 cmdarg_T *cap;
4108 clearopbeep(cap->oap);
4112 * <Help> and <F1> commands.
4114 static void
4115 nv_help(cap)
4116 cmdarg_T *cap;
4118 if (!checkclearopq(cap->oap))
4119 ex_help(NULL);
4123 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4125 static void
4126 nv_addsub(cap)
4127 cmdarg_T *cap;
4129 if (!checkclearopq(cap->oap)
4130 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4131 prep_redo_cmd(cap);
4135 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4137 static void
4138 nv_page(cap)
4139 cmdarg_T *cap;
4141 if (!checkclearop(cap->oap))
4143 #ifdef FEAT_WINDOWS
4144 if (mod_mask & MOD_MASK_CTRL)
4146 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4147 if (cap->arg == BACKWARD)
4148 goto_tabpage(-(int)cap->count1);
4149 else
4150 goto_tabpage((int)cap->count0);
4152 else
4153 #endif
4154 (void)onepage(cap->arg, cap->count1);
4159 * Implementation of "gd" and "gD" command.
4161 static void
4162 nv_gd(oap, nchar, thisblock)
4163 oparg_T *oap;
4164 int nchar;
4165 int thisblock; /* 1 for "1gd" and "1gD" */
4167 int len;
4168 char_u *ptr;
4170 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
4171 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
4172 clearopbeep(oap);
4173 #ifdef FEAT_FOLDING
4174 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4175 foldOpenCursor();
4176 #endif
4180 * Search for variable declaration of "ptr[len]".
4181 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4182 * current file ("gD").
4183 * When "thisblock" is TRUE check the {} block scope.
4184 * Return FAIL when not found.
4187 find_decl(ptr, len, locally, thisblock, searchflags)
4188 char_u *ptr;
4189 int len;
4190 int locally;
4191 int thisblock;
4192 int searchflags; /* flags passed to searchit() */
4194 char_u *pat;
4195 pos_T old_pos;
4196 pos_T par_pos;
4197 pos_T found_pos;
4198 int t;
4199 int save_p_ws;
4200 int save_p_scs;
4201 int retval = OK;
4202 int incll;
4204 if ((pat = alloc(len + 7)) == NULL)
4205 return FAIL;
4207 /* Put "\V" before the pattern to avoid that the special meaning of "."
4208 * and "~" causes trouble. */
4209 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4210 len, ptr);
4211 old_pos = curwin->w_cursor;
4212 save_p_ws = p_ws;
4213 save_p_scs = p_scs;
4214 p_ws = FALSE; /* don't wrap around end of file now */
4215 p_scs = FALSE; /* don't switch ignorecase off now */
4218 * With "gD" go to line 1.
4219 * With "gd" Search back for the start of the current function, then go
4220 * back until a blank line. If this fails go to line 1.
4222 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
4224 setpcmark(); /* Set in findpar() otherwise */
4225 curwin->w_cursor.lnum = 1;
4226 par_pos = curwin->w_cursor;
4228 else
4230 par_pos = curwin->w_cursor;
4231 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4232 --curwin->w_cursor.lnum;
4234 curwin->w_cursor.col = 0;
4236 /* Search forward for the identifier, ignore comment lines. */
4237 clearpos(&found_pos);
4238 for (;;)
4240 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
4241 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
4242 if (curwin->w_cursor.lnum >= old_pos.lnum)
4243 t = FAIL; /* match after start is failure too */
4245 if (thisblock && t != FAIL)
4247 pos_T *pos;
4249 /* Check that the block the match is in doesn't end before the
4250 * position where we started the search from. */
4251 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4252 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4253 && pos->lnum < old_pos.lnum)
4254 continue;
4257 if (t == FAIL)
4259 /* If we previously found a valid position, use it. */
4260 if (found_pos.lnum != 0)
4262 curwin->w_cursor = found_pos;
4263 t = OK;
4265 break;
4267 #ifdef FEAT_COMMENTS
4268 if (get_leader_len(ml_get_curline(), NULL, FALSE) > 0)
4270 /* Ignore this line, continue at start of next line. */
4271 ++curwin->w_cursor.lnum;
4272 curwin->w_cursor.col = 0;
4273 continue;
4275 #endif
4276 if (!locally) /* global search: use first match found */
4277 break;
4278 if (curwin->w_cursor.lnum >= par_pos.lnum)
4280 /* If we previously found a valid position, use it. */
4281 if (found_pos.lnum != 0)
4282 curwin->w_cursor = found_pos;
4283 break;
4286 /* For finding a local variable and the match is before the "{" search
4287 * to find a later match. For K&R style function declarations this
4288 * skips the function header without types. */
4289 found_pos = curwin->w_cursor;
4292 if (t == FAIL)
4294 retval = FAIL;
4295 curwin->w_cursor = old_pos;
4297 else
4299 curwin->w_set_curswant = TRUE;
4300 /* "n" searches forward now */
4301 reset_search_dir();
4304 vim_free(pat);
4305 p_ws = save_p_ws;
4306 p_scs = save_p_scs;
4308 return retval;
4312 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4313 * lines rather than lines in the file.
4314 * 'dist' must be positive.
4316 * Return OK if able to move cursor, FAIL otherwise.
4318 static int
4319 nv_screengo(oap, dir, dist)
4320 oparg_T *oap;
4321 int dir;
4322 long dist;
4324 int linelen = linetabsize(ml_get_curline());
4325 int retval = OK;
4326 int atend = FALSE;
4327 int n;
4328 int col_off1; /* margin offset for first screen line */
4329 int col_off2; /* margin offset for wrapped screen line */
4330 int width1; /* text width for first screen line */
4331 int width2; /* test width for wrapped screen line */
4333 oap->motion_type = MCHAR;
4334 oap->inclusive = FALSE;
4336 col_off1 = curwin_col_off();
4337 col_off2 = col_off1 - curwin_col_off2();
4338 width1 = W_WIDTH(curwin) - col_off1;
4339 width2 = W_WIDTH(curwin) - col_off2;
4341 #ifdef FEAT_VERTSPLIT
4342 if (curwin->w_width != 0)
4344 #endif
4346 * Instead of sticking at the last character of the buffer line we
4347 * try to stick in the last column of the screen.
4349 if (curwin->w_curswant == MAXCOL)
4351 atend = TRUE;
4352 validate_virtcol();
4353 if (width1 <= 0)
4354 curwin->w_curswant = 0;
4355 else
4357 curwin->w_curswant = width1 - 1;
4358 if (curwin->w_virtcol > curwin->w_curswant)
4359 curwin->w_curswant += ((curwin->w_virtcol
4360 - curwin->w_curswant - 1) / width2 + 1) * width2;
4363 else
4365 if (linelen > width1)
4366 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4367 else
4368 n = width1;
4369 if (curwin->w_curswant > (colnr_T)n + 1)
4370 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4371 * width2;
4374 while (dist--)
4376 if (dir == BACKWARD)
4378 if ((long)curwin->w_curswant >= width2)
4379 /* move back within line */
4380 curwin->w_curswant -= width2;
4381 else
4383 /* to previous line */
4384 if (curwin->w_cursor.lnum == 1)
4386 retval = FAIL;
4387 break;
4389 --curwin->w_cursor.lnum;
4390 #ifdef FEAT_FOLDING
4391 /* Move to the start of a closed fold. Don't do that when
4392 * 'foldopen' contains "all": it will open in a moment. */
4393 if (!(fdo_flags & FDO_ALL))
4394 (void)hasFolding(curwin->w_cursor.lnum,
4395 &curwin->w_cursor.lnum, NULL);
4396 #endif
4397 linelen = linetabsize(ml_get_curline());
4398 if (linelen > width1)
4399 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4400 + 1) * width2;
4403 else /* dir == FORWARD */
4405 if (linelen > width1)
4406 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4407 else
4408 n = width1;
4409 if (curwin->w_curswant + width2 < (colnr_T)n)
4410 /* move forward within line */
4411 curwin->w_curswant += width2;
4412 else
4414 /* to next line */
4415 #ifdef FEAT_FOLDING
4416 /* Move to the end of a closed fold. */
4417 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4418 &curwin->w_cursor.lnum);
4419 #endif
4420 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4422 retval = FAIL;
4423 break;
4425 curwin->w_cursor.lnum++;
4426 curwin->w_curswant %= width2;
4430 #ifdef FEAT_VERTSPLIT
4432 #endif
4434 coladvance(curwin->w_curswant);
4436 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4437 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4440 * Check for landing on a character that got split at the end of the
4441 * last line. We want to advance a screenline, not end up in the same
4442 * screenline or move two screenlines.
4444 validate_virtcol();
4445 if (curwin->w_virtcol > curwin->w_curswant
4446 && (curwin->w_curswant < (colnr_T)width1
4447 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4448 : ((curwin->w_curswant - width1) % width2
4449 > (colnr_T)width2 / 2)))
4450 --curwin->w_cursor.col;
4452 #endif
4454 if (atend)
4455 curwin->w_curswant = MAXCOL; /* stick in the last column */
4457 return retval;
4460 #ifdef FEAT_MOUSE
4462 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4463 * when Shift or Ctrl is used.
4464 * K_MOUSEUP (cap->arg == TRUE) or K_MOUSEDOWN (cap->arg == FALSE)
4466 static void
4467 nv_mousescroll(cap)
4468 cmdarg_T *cap;
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 cap->count1 = 3;
4494 cap->count0 = 3;
4495 nv_scroll_line(cap);
4498 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4499 curwin->w_redr_status = TRUE;
4501 curwin = old_curwin;
4502 curbuf = curwin->w_buffer;
4503 # endif
4507 * Mouse clicks and drags.
4509 static void
4510 nv_mouse(cap)
4511 cmdarg_T *cap;
4513 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4515 #endif
4518 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4519 * cap->arg must be TRUE for CTRL-E.
4521 static void
4522 nv_scroll_line(cap)
4523 cmdarg_T *cap;
4525 if (!checkclearop(cap->oap))
4526 scroll_redraw(cap->arg, cap->count1);
4530 * Scroll "count" lines up or down, and redraw.
4532 void
4533 scroll_redraw(up, count)
4534 int up;
4535 long count;
4537 linenr_T prev_topline = curwin->w_topline;
4538 #ifdef FEAT_DIFF
4539 int prev_topfill = curwin->w_topfill;
4540 #endif
4541 linenr_T prev_lnum = curwin->w_cursor.lnum;
4543 if (up)
4544 scrollup(count, TRUE);
4545 else
4546 scrolldown(count, TRUE);
4547 if (p_so)
4549 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4550 * valid, otherwise the screen jumps back at the end of the file. */
4551 cursor_correct();
4552 check_cursor_moved(curwin);
4553 curwin->w_valid |= VALID_TOPLINE;
4555 /* If moved back to where we were, at least move the cursor, otherwise
4556 * we get stuck at one position. Don't move the cursor up if the
4557 * first line of the buffer is already on the screen */
4558 while (curwin->w_topline == prev_topline
4559 #ifdef FEAT_DIFF
4560 && curwin->w_topfill == prev_topfill
4561 #endif
4564 if (up)
4566 if (curwin->w_cursor.lnum > prev_lnum
4567 || cursor_down(1L, FALSE) == FAIL)
4568 break;
4570 else
4572 if (curwin->w_cursor.lnum < prev_lnum
4573 || prev_topline == 1L
4574 || cursor_up(1L, FALSE) == FAIL)
4575 break;
4577 /* Mark w_topline as valid, otherwise the screen jumps back at the
4578 * end of the file. */
4579 check_cursor_moved(curwin);
4580 curwin->w_valid |= VALID_TOPLINE;
4583 if (curwin->w_cursor.lnum != prev_lnum)
4584 coladvance(curwin->w_curswant);
4585 redraw_later(VALID);
4589 * Commands that start with "z".
4591 static void
4592 nv_zet(cap)
4593 cmdarg_T *cap;
4595 long n;
4596 colnr_T col;
4597 int nchar = cap->nchar;
4598 #ifdef FEAT_FOLDING
4599 long old_fdl = curwin->w_p_fdl;
4600 int old_fen = curwin->w_p_fen;
4601 #endif
4602 #ifdef FEAT_SPELL
4603 int undo = FALSE;
4604 #endif
4606 if (VIM_ISDIGIT(nchar))
4609 * "z123{nchar}": edit the count before obtaining {nchar}
4611 if (checkclearop(cap->oap))
4612 return;
4613 n = nchar - '0';
4614 for (;;)
4616 #ifdef USE_ON_FLY_SCROLL
4617 dont_scroll = TRUE; /* disallow scrolling here */
4618 #endif
4619 ++no_mapping;
4620 ++allow_keys; /* no mapping for nchar, but allow key codes */
4621 nchar = plain_vgetc();
4622 LANGMAP_ADJUST(nchar, TRUE);
4623 --no_mapping;
4624 --allow_keys;
4625 #ifdef FEAT_CMDL_INFO
4626 (void)add_to_showcmd(nchar);
4627 #endif
4628 if (nchar == K_DEL || nchar == K_KDEL)
4629 n /= 10;
4630 else if (VIM_ISDIGIT(nchar))
4631 n = n * 10 + (nchar - '0');
4632 else if (nchar == CAR)
4634 #ifdef FEAT_GUI
4635 need_mouse_correct = TRUE;
4636 #endif
4637 win_setheight((int)n);
4638 break;
4640 else if (nchar == 'l'
4641 || nchar == 'h'
4642 || nchar == K_LEFT
4643 || nchar == K_RIGHT)
4645 cap->count1 = n ? n * cap->count1 : cap->count1;
4646 goto dozet;
4648 else
4650 clearopbeep(cap->oap);
4651 break;
4654 cap->oap->op_type = OP_NOP;
4655 return;
4658 dozet:
4659 if (
4660 #ifdef FEAT_FOLDING
4661 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4662 * and "zC" only in Visual mode. "zj" and "zk" are motion
4663 * commands. */
4664 cap->nchar != 'f' && cap->nchar != 'F'
4665 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4666 && cap->nchar != 'j' && cap->nchar != 'k'
4668 #endif
4669 checkclearop(cap->oap))
4670 return;
4673 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4674 * If line number given, set cursor.
4676 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4677 && cap->count0
4678 && cap->count0 != curwin->w_cursor.lnum)
4680 setpcmark();
4681 if (cap->count0 > curbuf->b_ml.ml_line_count)
4682 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4683 else
4684 curwin->w_cursor.lnum = cap->count0;
4685 check_cursor_col();
4688 switch (nchar)
4690 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4691 case '+':
4692 if (cap->count0 == 0)
4694 /* No count given: put cursor at the line below screen */
4695 validate_botline(); /* make sure w_botline is valid */
4696 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4697 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4698 else
4699 curwin->w_cursor.lnum = curwin->w_botline;
4701 /* FALLTHROUGH */
4702 case NL:
4703 case CAR:
4704 case K_KENTER:
4705 beginline(BL_WHITE | BL_FIX);
4706 /* FALLTHROUGH */
4708 case 't': scroll_cursor_top(0, TRUE);
4709 redraw_later(VALID);
4710 break;
4712 /* "z." and "zz": put cursor in middle of screen */
4713 case '.': beginline(BL_WHITE | BL_FIX);
4714 /* FALLTHROUGH */
4716 case 'z': scroll_cursor_halfway(TRUE);
4717 redraw_later(VALID);
4718 break;
4720 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4721 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4722 * when <count> is at bottom of window, and puts that one at
4723 * bottom of window. */
4724 if (cap->count0 != 0)
4726 scroll_cursor_bot(0, TRUE);
4727 curwin->w_cursor.lnum = curwin->w_topline;
4729 else if (curwin->w_topline == 1)
4730 curwin->w_cursor.lnum = 1;
4731 else
4732 curwin->w_cursor.lnum = curwin->w_topline - 1;
4733 /* FALLTHROUGH */
4734 case '-':
4735 beginline(BL_WHITE | BL_FIX);
4736 /* FALLTHROUGH */
4738 case 'b': scroll_cursor_bot(0, TRUE);
4739 redraw_later(VALID);
4740 break;
4742 /* "zH" - scroll screen right half-page */
4743 case 'H':
4744 cap->count1 *= W_WIDTH(curwin) / 2;
4745 /* FALLTHROUGH */
4747 /* "zh" - scroll screen to the right */
4748 case 'h':
4749 case K_LEFT:
4750 if (!curwin->w_p_wrap)
4752 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4753 curwin->w_leftcol = 0;
4754 else
4755 curwin->w_leftcol -= (colnr_T)cap->count1;
4756 leftcol_changed();
4758 break;
4760 /* "zL" - scroll screen left half-page */
4761 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4762 /* FALLTHROUGH */
4764 /* "zl" - scroll screen to the left */
4765 case 'l':
4766 case K_RIGHT:
4767 if (!curwin->w_p_wrap)
4769 /* scroll the window left */
4770 curwin->w_leftcol += (colnr_T)cap->count1;
4771 leftcol_changed();
4773 break;
4775 /* "zs" - scroll screen, cursor at the start */
4776 case 's': if (!curwin->w_p_wrap)
4778 #ifdef FEAT_FOLDING
4779 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4780 col = 0; /* like the cursor is in col 0 */
4781 else
4782 #endif
4783 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4784 if ((long)col > p_siso)
4785 col -= p_siso;
4786 else
4787 col = 0;
4788 if (curwin->w_leftcol != col)
4790 curwin->w_leftcol = col;
4791 redraw_later(NOT_VALID);
4794 break;
4796 /* "ze" - scroll screen, cursor at the end */
4797 case 'e': if (!curwin->w_p_wrap)
4799 #ifdef FEAT_FOLDING
4800 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4801 col = 0; /* like the cursor is in col 0 */
4802 else
4803 #endif
4804 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4805 n = W_WIDTH(curwin) - curwin_col_off();
4806 if ((long)col + p_siso < n)
4807 col = 0;
4808 else
4809 col = col + p_siso - n + 1;
4810 if (curwin->w_leftcol != col)
4812 curwin->w_leftcol = col;
4813 redraw_later(NOT_VALID);
4816 break;
4818 #ifdef FEAT_FOLDING
4819 /* "zF": create fold command */
4820 /* "zf": create fold operator */
4821 case 'F':
4822 case 'f': if (foldManualAllowed(TRUE))
4824 cap->nchar = 'f';
4825 nv_operator(cap);
4826 curwin->w_p_fen = TRUE;
4828 /* "zF" is like "zfzf" */
4829 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4831 nv_operator(cap);
4832 finish_op = TRUE;
4835 else
4836 clearopbeep(cap->oap);
4837 break;
4839 /* "zd": delete fold at cursor */
4840 /* "zD": delete fold at cursor recursively */
4841 case 'd':
4842 case 'D': if (foldManualAllowed(FALSE))
4844 if (VIsual_active)
4845 nv_operator(cap);
4846 else
4847 deleteFold(curwin->w_cursor.lnum,
4848 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4850 break;
4852 /* "zE": erease all folds */
4853 case 'E': if (foldmethodIsManual(curwin))
4855 clearFolding(curwin);
4856 changed_window_setting();
4858 else if (foldmethodIsMarker(curwin))
4859 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4860 TRUE, FALSE);
4861 else
4862 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4863 break;
4865 /* "zn": fold none: reset 'foldenable' */
4866 case 'n': curwin->w_p_fen = FALSE;
4867 break;
4869 /* "zN": fold Normal: set 'foldenable' */
4870 case 'N': curwin->w_p_fen = TRUE;
4871 break;
4873 /* "zi": invert folding: toggle 'foldenable' */
4874 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4875 break;
4877 /* "za": open closed fold or close open fold at cursor */
4878 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4879 openFold(curwin->w_cursor.lnum, cap->count1);
4880 else
4882 closeFold(curwin->w_cursor.lnum, cap->count1);
4883 curwin->w_p_fen = TRUE;
4885 break;
4887 /* "zA": open fold at cursor recursively */
4888 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4889 openFoldRecurse(curwin->w_cursor.lnum);
4890 else
4892 closeFoldRecurse(curwin->w_cursor.lnum);
4893 curwin->w_p_fen = TRUE;
4895 break;
4897 /* "zo": open fold at cursor or Visual area */
4898 case 'o': if (VIsual_active)
4899 nv_operator(cap);
4900 else
4901 openFold(curwin->w_cursor.lnum, cap->count1);
4902 break;
4904 /* "zO": open fold recursively */
4905 case 'O': if (VIsual_active)
4906 nv_operator(cap);
4907 else
4908 openFoldRecurse(curwin->w_cursor.lnum);
4909 break;
4911 /* "zc": close fold at cursor or Visual area */
4912 case 'c': if (VIsual_active)
4913 nv_operator(cap);
4914 else
4915 closeFold(curwin->w_cursor.lnum, cap->count1);
4916 curwin->w_p_fen = TRUE;
4917 break;
4919 /* "zC": close fold recursively */
4920 case 'C': if (VIsual_active)
4921 nv_operator(cap);
4922 else
4923 closeFoldRecurse(curwin->w_cursor.lnum);
4924 curwin->w_p_fen = TRUE;
4925 break;
4927 /* "zv": open folds at the cursor */
4928 case 'v': foldOpenCursor();
4929 break;
4931 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
4932 case 'x': curwin->w_p_fen = TRUE;
4933 newFoldLevel(); /* update right now */
4934 foldOpenCursor();
4935 break;
4937 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
4938 case 'X': curwin->w_p_fen = TRUE;
4939 old_fdl = -1; /* force an update */
4940 break;
4942 /* "zm": fold more */
4943 case 'm': if (curwin->w_p_fdl > 0)
4944 --curwin->w_p_fdl;
4945 old_fdl = -1; /* force an update */
4946 curwin->w_p_fen = TRUE;
4947 break;
4949 /* "zM": close all folds */
4950 case 'M': curwin->w_p_fdl = 0;
4951 old_fdl = -1; /* force an update */
4952 curwin->w_p_fen = TRUE;
4953 break;
4955 /* "zr": reduce folding */
4956 case 'r': ++curwin->w_p_fdl;
4957 break;
4959 /* "zR": open all folds */
4960 case 'R': curwin->w_p_fdl = getDeepestNesting();
4961 old_fdl = -1; /* force an update */
4962 break;
4964 case 'j': /* "zj" move to next fold downwards */
4965 case 'k': /* "zk" move to next fold upwards */
4966 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
4967 cap->count1) == FAIL)
4968 clearopbeep(cap->oap);
4969 break;
4971 #endif /* FEAT_FOLDING */
4973 #ifdef FEAT_SPELL
4974 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
4975 ++no_mapping;
4976 ++allow_keys; /* no mapping for nchar, but allow key codes */
4977 nchar = plain_vgetc();
4978 LANGMAP_ADJUST(nchar, TRUE);
4979 --no_mapping;
4980 --allow_keys;
4981 #ifdef FEAT_CMDL_INFO
4982 (void)add_to_showcmd(nchar);
4983 #endif
4984 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
4986 clearopbeep(cap->oap);
4987 break;
4989 undo = TRUE;
4990 /*FALLTHROUGH*/
4992 case 'g': /* "zg": add good word to word list */
4993 case 'w': /* "zw": add wrong word to word list */
4994 case 'G': /* "zG": add good word to temp word list */
4995 case 'W': /* "zW": add wrong word to temp word list */
4997 char_u *ptr = NULL;
4998 int len;
5000 if (checkclearop(cap->oap))
5001 break;
5002 # ifdef FEAT_VISUAL
5003 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5004 == FAIL)
5005 return;
5006 # endif
5007 if (ptr == NULL)
5009 pos_T pos = curwin->w_cursor;
5011 /* Find bad word under the cursor. */
5012 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
5013 if (len != 0 && curwin->w_cursor.col <= pos.col)
5014 ptr = ml_get_pos(&curwin->w_cursor);
5015 curwin->w_cursor = pos;
5018 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5019 FIND_IDENT)) == 0)
5020 return;
5021 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
5022 (nchar == 'G' || nchar == 'W')
5023 ? 0 : (int)cap->count1,
5024 undo);
5026 break;
5028 case '=': /* "z=": suggestions for a badly spelled word */
5029 if (!checkclearop(cap->oap))
5030 spell_suggest((int)cap->count0);
5031 break;
5032 #endif
5034 default: clearopbeep(cap->oap);
5037 #ifdef FEAT_FOLDING
5038 /* Redraw when 'foldenable' changed */
5039 if (old_fen != curwin->w_p_fen)
5041 # ifdef FEAT_DIFF
5042 win_T *wp;
5044 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5046 /* Adjust 'foldenable' in diff-synced windows. */
5047 FOR_ALL_WINDOWS(wp)
5049 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5051 wp->w_p_fen = curwin->w_p_fen;
5052 changed_window_setting_win(wp);
5056 # endif
5057 changed_window_setting();
5060 /* Redraw when 'foldlevel' changed. */
5061 if (old_fdl != curwin->w_p_fdl)
5062 newFoldLevel();
5063 #endif
5066 #ifdef FEAT_GUI
5068 * Vertical scrollbar movement.
5070 static void
5071 nv_ver_scrollbar(cap)
5072 cmdarg_T *cap;
5074 if (cap->oap->op_type != OP_NOP)
5075 clearopbeep(cap->oap);
5077 /* Even if an operator was pending, we still want to scroll */
5078 gui_do_scroll();
5082 * Horizontal scrollbar movement.
5084 static void
5085 nv_hor_scrollbar(cap)
5086 cmdarg_T *cap;
5088 if (cap->oap->op_type != OP_NOP)
5089 clearopbeep(cap->oap);
5091 /* Even if an operator was pending, we still want to scroll */
5092 gui_do_horiz_scroll();
5094 #endif
5096 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
5098 * Click in GUI tab.
5100 static void
5101 nv_tabline(cap)
5102 cmdarg_T *cap;
5104 if (cap->oap->op_type != OP_NOP)
5105 clearopbeep(cap->oap);
5107 /* Even if an operator was pending, we still want to jump tabs. */
5108 goto_tabpage(current_tab);
5112 * Selected item in tab line menu.
5114 static void
5115 nv_tabmenu(cap)
5116 cmdarg_T *cap;
5118 if (cap->oap->op_type != OP_NOP)
5119 clearopbeep(cap->oap);
5121 /* Even if an operator was pending, we still want to jump tabs. */
5122 handle_tabmenu();
5126 * Handle selecting an item of the GUI tab line menu.
5127 * Used in Normal and Insert mode.
5129 void
5130 handle_tabmenu()
5132 switch (current_tabmenu)
5134 case TABLINE_MENU_CLOSE:
5135 if (current_tab == 0)
5136 do_cmdline_cmd((char_u *)"tabclose");
5137 else
5139 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5140 current_tab);
5141 do_cmdline_cmd(IObuff);
5143 break;
5145 case TABLINE_MENU_NEW:
5146 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5147 current_tab > 0 ? current_tab - 1 : 999);
5148 do_cmdline_cmd(IObuff);
5149 break;
5151 case TABLINE_MENU_OPEN:
5152 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5153 current_tab > 0 ? current_tab - 1 : 999);
5154 do_cmdline_cmd(IObuff);
5155 break;
5158 #endif
5161 * "Q" command.
5163 static void
5164 nv_exmode(cap)
5165 cmdarg_T *cap;
5168 * Ignore 'Q' in Visual mode, just give a beep.
5170 #ifdef FEAT_VISUAL
5171 if (VIsual_active)
5172 vim_beep();
5173 else
5174 #endif
5175 if (!checkclearop(cap->oap))
5176 do_exmode(FALSE);
5180 * Handle a ":" command.
5182 static void
5183 nv_colon(cap)
5184 cmdarg_T *cap;
5186 int old_p_im;
5188 #ifdef FEAT_VISUAL
5189 if (VIsual_active)
5190 nv_operator(cap);
5191 else
5192 #endif
5194 if (cap->oap->op_type != OP_NOP)
5196 /* Using ":" as a movement is characterwise exclusive. */
5197 cap->oap->motion_type = MCHAR;
5198 cap->oap->inclusive = FALSE;
5200 else if (cap->count0)
5202 /* translate "count:" into ":.,.+(count - 1)" */
5203 stuffcharReadbuff('.');
5204 if (cap->count0 > 1)
5206 stuffReadbuff((char_u *)",.+");
5207 stuffnumReadbuff((long)cap->count0 - 1L);
5211 /* When typing, don't type below an old message */
5212 if (KeyTyped)
5213 compute_cmdrow();
5215 old_p_im = p_im;
5217 /* get a command line and execute it */
5218 do_cmdline(NULL, getexline, NULL,
5219 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5221 /* If 'insertmode' changed, enter or exit Insert mode */
5222 if (p_im != old_p_im)
5224 if (p_im)
5225 restart_edit = 'i';
5226 else
5227 restart_edit = 0;
5230 /* The start of the operator may have become invalid by the Ex
5231 * command. */
5232 if (cap->oap->op_type != OP_NOP
5233 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5234 || cap->oap->start.col >
5235 STRLEN(ml_get(cap->oap->start.lnum))))
5236 clearopbeep(cap->oap);
5241 * Handle CTRL-G command.
5243 static void
5244 nv_ctrlg(cap)
5245 cmdarg_T *cap;
5247 #ifdef FEAT_VISUAL
5248 if (VIsual_active) /* toggle Selection/Visual mode */
5250 VIsual_select = !VIsual_select;
5251 showmode();
5253 else
5254 #endif
5255 if (!checkclearop(cap->oap))
5256 /* print full name if count given or :cd used */
5257 fileinfo((int)cap->count0, FALSE, TRUE);
5261 * Handle CTRL-H <Backspace> command.
5263 static void
5264 nv_ctrlh(cap)
5265 cmdarg_T *cap;
5267 #ifdef FEAT_VISUAL
5268 if (VIsual_active && VIsual_select)
5270 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5271 v_visop(cap);
5273 else
5274 #endif
5275 nv_left(cap);
5279 * CTRL-L: clear screen and redraw.
5281 static void
5282 nv_clear(cap)
5283 cmdarg_T *cap;
5285 if (!checkclearop(cap->oap))
5287 #if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5289 * Right now, the BeBox doesn't seem to have an easy way to detect
5290 * window resizing, so we cheat and make the user detect it
5291 * manually with CTRL-L instead
5293 ui_get_shellsize();
5294 #endif
5295 #ifdef FEAT_SYN_HL
5296 /* Clear all syntax states to force resyncing. */
5297 syn_stack_free_all(curbuf);
5298 #endif
5299 redraw_later(CLEAR);
5304 * CTRL-O: In Select mode: switch to Visual mode for one command.
5305 * Otherwise: Go to older pcmark.
5307 static void
5308 nv_ctrlo(cap)
5309 cmdarg_T *cap;
5311 #ifdef FEAT_VISUAL
5312 if (VIsual_active && VIsual_select)
5314 VIsual_select = FALSE;
5315 showmode();
5316 restart_VIsual_select = 2; /* restart Select mode later */
5318 else
5319 #endif
5321 cap->count1 = -cap->count1;
5322 nv_pcmark(cap);
5327 * CTRL-^ command, short for ":e #"
5329 static void
5330 nv_hat(cap)
5331 cmdarg_T *cap;
5333 if (!checkclearopq(cap->oap))
5334 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5335 GETF_SETMARK|GETF_ALT, FALSE);
5339 * "Z" commands.
5341 static void
5342 nv_Zet(cap)
5343 cmdarg_T *cap;
5345 if (!checkclearopq(cap->oap))
5347 switch (cap->nchar)
5349 /* "ZZ": equivalent to ":x". */
5350 case 'Z': do_cmdline_cmd((char_u *)"x");
5351 break;
5353 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5354 case 'Q': do_cmdline_cmd((char_u *)"q!");
5355 break;
5357 default: clearopbeep(cap->oap);
5362 #if defined(FEAT_WINDOWS) || defined(PROTO)
5364 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5366 void
5367 do_nv_ident(c1, c2)
5368 int c1;
5369 int c2;
5371 oparg_T oa;
5372 cmdarg_T ca;
5374 clear_oparg(&oa);
5375 vim_memset(&ca, 0, sizeof(ca));
5376 ca.oap = &oa;
5377 ca.cmdchar = c1;
5378 ca.nchar = c2;
5379 nv_ident(&ca);
5381 #endif
5384 * Handle the commands that use the word under the cursor.
5385 * [g] CTRL-] :ta to current identifier
5386 * [g] 'K' run program for current identifier
5387 * [g] '*' / to current identifier or string
5388 * [g] '#' ? to current identifier or string
5389 * g ']' :tselect for current identifier
5391 static void
5392 nv_ident(cap)
5393 cmdarg_T *cap;
5395 char_u *ptr = NULL;
5396 char_u *buf;
5397 char_u *p;
5398 char_u *kp; /* value of 'keywordprg' */
5399 int kp_help; /* 'keywordprg' is ":help" */
5400 int n = 0; /* init for GCC */
5401 int cmdchar;
5402 int g_cmd; /* "g" command */
5403 char_u *aux_ptr;
5404 int isman;
5405 int isman_s;
5407 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5409 cmdchar = cap->nchar;
5410 g_cmd = TRUE;
5412 else
5414 cmdchar = cap->cmdchar;
5415 g_cmd = FALSE;
5418 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5419 cmdchar = '#';
5422 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5424 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5426 #ifdef FEAT_VISUAL
5427 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5428 return;
5429 #endif
5430 if (checkclearopq(cap->oap))
5431 return;
5434 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5435 (cmdchar == '*' || cmdchar == '#')
5436 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5438 clearop(cap->oap);
5439 return;
5442 /* Allocate buffer to put the command in. Inserting backslashes can
5443 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5444 * and some numbers. */
5445 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5446 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5447 || STRCMP(kp, ":help") == 0);
5448 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5449 if (buf == NULL)
5450 return;
5451 buf[0] = NUL;
5453 switch (cmdchar)
5455 case '*':
5456 case '#':
5458 * Put cursor at start of word, makes search skip the word
5459 * under the cursor.
5460 * Call setpcmark() first, so "*``" puts the cursor back where
5461 * it was.
5463 setpcmark();
5464 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5466 if (!g_cmd && vim_iswordp(ptr))
5467 STRCPY(buf, "\\<");
5468 no_smartcase = TRUE; /* don't use 'smartcase' now */
5469 break;
5471 case 'K':
5472 if (kp_help)
5473 STRCPY(buf, "he! ");
5474 else
5476 /* An external command will probably use an argument starting
5477 * with "-" as an option. To avoid trouble we skip the "-". */
5478 while (*ptr == '-' && n > 0)
5480 ++ptr;
5481 --n;
5483 if (n == 0)
5485 EMSG(_(e_noident)); /* found dashes only */
5486 vim_free(buf);
5487 return;
5490 /* When a count is given, turn it into a range. Is this
5491 * really what we want? */
5492 isman = (STRCMP(kp, "man") == 0);
5493 isman_s = (STRCMP(kp, "man -s") == 0);
5494 if (cap->count0 != 0 && !(isman || isman_s))
5495 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5497 STRCAT(buf, "! ");
5498 if (cap->count0 == 0 && isman_s)
5499 STRCAT(buf, "man");
5500 else
5501 STRCAT(buf, kp);
5502 STRCAT(buf, " ");
5503 if (cap->count0 != 0 && (isman || isman_s))
5505 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5506 STRCAT(buf, " ");
5509 break;
5511 case ']':
5512 #ifdef FEAT_CSCOPE
5513 if (p_cst)
5514 STRCPY(buf, "cstag ");
5515 else
5516 #endif
5517 STRCPY(buf, "ts ");
5518 break;
5520 default:
5521 if (curbuf->b_help)
5522 STRCPY(buf, "he! ");
5523 else if (g_cmd)
5524 STRCPY(buf, "tj ");
5525 else
5526 sprintf((char *)buf, "%ldta ", cap->count0);
5530 * Now grab the chars in the identifier
5532 if (cmdchar == 'K' && !kp_help)
5534 /* Escape the argument properly for a shell command */
5535 ptr = vim_strnsave(ptr, n);
5536 p = vim_strsave_shellescape(ptr, TRUE);
5537 vim_free(ptr);
5538 if (p == NULL)
5540 vim_free(buf);
5541 return;
5543 buf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5544 if (buf == NULL)
5546 vim_free(buf);
5547 vim_free(p);
5548 return;
5550 STRCAT(buf, p);
5551 vim_free(p);
5553 else
5555 if (cmdchar == '*')
5556 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5557 else if (cmdchar == '#')
5558 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
5559 else
5560 /* Don't escape spaces and Tabs in a tag with a backslash */
5561 aux_ptr = (char_u *)"\\|\"\n*?[";
5563 p = buf + STRLEN(buf);
5564 while (n-- > 0)
5566 /* put a backslash before \ and some others */
5567 if (vim_strchr(aux_ptr, *ptr) != NULL)
5568 *p++ = '\\';
5569 #ifdef FEAT_MBYTE
5570 /* When current byte is a part of multibyte character, copy all
5571 * bytes of that character. */
5572 if (has_mbyte)
5574 int i;
5575 int len = (*mb_ptr2len)(ptr) - 1;
5577 for (i = 0; i < len && n >= 1; ++i, --n)
5578 *p++ = *ptr++;
5580 #endif
5581 *p++ = *ptr++;
5583 *p = NUL;
5587 * Execute the command.
5589 if (cmdchar == '*' || cmdchar == '#')
5591 if (!g_cmd && (
5592 #ifdef FEAT_MBYTE
5593 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5594 #endif
5595 vim_iswordc(ptr[-1])))
5596 STRCAT(buf, "\\>");
5597 #ifdef FEAT_CMDHIST
5598 /* put pattern in search history */
5599 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5600 #endif
5601 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5603 else
5604 do_cmdline_cmd(buf);
5606 vim_free(buf);
5609 #if defined(FEAT_VISUAL) || defined(PROTO)
5611 * Get visually selected text, within one line only.
5612 * Returns FAIL if more than one line selected.
5615 get_visual_text(cap, pp, lenp)
5616 cmdarg_T *cap;
5617 char_u **pp; /* return: start of selected text */
5618 int *lenp; /* return: length of selected text */
5620 if (VIsual_mode != 'V')
5621 unadjust_for_sel();
5622 if (VIsual.lnum != curwin->w_cursor.lnum)
5624 if (cap != NULL)
5625 clearopbeep(cap->oap);
5626 return FAIL;
5628 if (VIsual_mode == 'V')
5630 *pp = ml_get_curline();
5631 *lenp = (int)STRLEN(*pp);
5633 else
5635 if (lt(curwin->w_cursor, VIsual))
5637 *pp = ml_get_pos(&curwin->w_cursor);
5638 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5640 else
5642 *pp = ml_get_pos(&VIsual);
5643 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5645 #ifdef FEAT_MBYTE
5646 if (has_mbyte)
5647 /* Correct the length to include the whole last character. */
5648 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
5649 #endif
5651 reset_VIsual_and_resel();
5652 return OK;
5654 #endif
5657 * CTRL-T: backwards in tag stack
5659 static void
5660 nv_tagpop(cap)
5661 cmdarg_T *cap;
5663 if (!checkclearopq(cap->oap))
5664 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5668 * Handle scrolling command 'H', 'L' and 'M'.
5670 static void
5671 nv_scroll(cap)
5672 cmdarg_T *cap;
5674 int used = 0;
5675 long n;
5676 #ifdef FEAT_FOLDING
5677 linenr_T lnum;
5678 #endif
5679 int half;
5681 cap->oap->motion_type = MLINE;
5682 setpcmark();
5684 if (cap->cmdchar == 'L')
5686 validate_botline(); /* make sure curwin->w_botline is valid */
5687 curwin->w_cursor.lnum = curwin->w_botline - 1;
5688 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5689 curwin->w_cursor.lnum = 1;
5690 else
5692 #ifdef FEAT_FOLDING
5693 if (hasAnyFolding(curwin))
5695 /* Count a fold for one screen line. */
5696 for (n = cap->count1 - 1; n > 0
5697 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5699 (void)hasFolding(curwin->w_cursor.lnum,
5700 &curwin->w_cursor.lnum, NULL);
5701 --curwin->w_cursor.lnum;
5704 else
5705 #endif
5706 curwin->w_cursor.lnum -= cap->count1 - 1;
5709 else
5711 if (cap->cmdchar == 'M')
5713 #ifdef FEAT_DIFF
5714 /* Don't count filler lines above the window. */
5715 used -= diff_check_fill(curwin, curwin->w_topline)
5716 - curwin->w_topfill;
5717 #endif
5718 validate_botline(); /* make sure w_empty_rows is valid */
5719 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5720 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5722 #ifdef FEAT_DIFF
5723 /* Count half he number of filler lines to be "below this
5724 * line" and half to be "above the next line". */
5725 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5726 + n) / 2 >= half)
5728 --n;
5729 break;
5731 #endif
5732 used += plines(curwin->w_topline + n);
5733 if (used >= half)
5734 break;
5735 #ifdef FEAT_FOLDING
5736 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5737 n = lnum - curwin->w_topline;
5738 #endif
5740 if (n > 0 && used > curwin->w_height)
5741 --n;
5743 else /* (cap->cmdchar == 'H') */
5745 n = cap->count1 - 1;
5746 #ifdef FEAT_FOLDING
5747 if (hasAnyFolding(curwin))
5749 /* Count a fold for one screen line. */
5750 lnum = curwin->w_topline;
5751 while (n-- > 0 && lnum < curwin->w_botline - 1)
5753 hasFolding(lnum, NULL, &lnum);
5754 ++lnum;
5756 n = lnum - curwin->w_topline;
5758 #endif
5760 curwin->w_cursor.lnum = curwin->w_topline + n;
5761 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5762 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5765 cursor_correct(); /* correct for 'so' */
5766 beginline(BL_SOL | BL_FIX);
5770 * Cursor right commands.
5772 static void
5773 nv_right(cap)
5774 cmdarg_T *cap;
5776 long n;
5777 #ifdef FEAT_VISUAL
5778 int PAST_LINE;
5779 #else
5780 # define PAST_LINE 0
5781 #endif
5783 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5785 /* <C-Right> and <S-Right> move a word or WORD right */
5786 if (mod_mask & MOD_MASK_CTRL)
5787 cap->arg = TRUE;
5788 nv_wordcmd(cap);
5789 return;
5792 cap->oap->motion_type = MCHAR;
5793 cap->oap->inclusive = FALSE;
5794 #ifdef FEAT_VISUAL
5795 PAST_LINE = (VIsual_active && *p_sel != 'o');
5797 # ifdef FEAT_VIRTUALEDIT
5799 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
5800 * (theoretically) infinitely long.
5802 if (virtual_active())
5803 PAST_LINE = 0;
5804 # endif
5805 #endif
5807 for (n = cap->count1; n > 0; --n)
5809 if ((!PAST_LINE && oneright() == FAIL)
5810 || (PAST_LINE && *ml_get_cursor() == NUL))
5813 * <Space> wraps to next line if 'whichwrap' has 's'.
5814 * 'l' wraps to next line if 'whichwrap' has 'l'.
5815 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
5817 if ( ((cap->cmdchar == ' '
5818 && vim_strchr(p_ww, 's') != NULL)
5819 || (cap->cmdchar == 'l'
5820 && vim_strchr(p_ww, 'l') != NULL)
5821 || (cap->cmdchar == K_RIGHT
5822 && vim_strchr(p_ww, '>') != NULL))
5823 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5825 /* When deleting we also count the NL as a character.
5826 * Set cap->oap->inclusive when last char in the line is
5827 * included, move to next line after that */
5828 if ( cap->oap->op_type != OP_NOP
5829 && !cap->oap->inclusive
5830 && !lineempty(curwin->w_cursor.lnum))
5831 cap->oap->inclusive = TRUE;
5832 else
5834 ++curwin->w_cursor.lnum;
5835 curwin->w_cursor.col = 0;
5836 #ifdef FEAT_VIRTUALEDIT
5837 curwin->w_cursor.coladd = 0;
5838 #endif
5839 curwin->w_set_curswant = TRUE;
5840 cap->oap->inclusive = FALSE;
5842 continue;
5844 if (cap->oap->op_type == OP_NOP)
5846 /* Only beep and flush if not moved at all */
5847 if (n == cap->count1)
5848 beep_flush();
5850 else
5852 if (!lineempty(curwin->w_cursor.lnum))
5853 cap->oap->inclusive = TRUE;
5855 break;
5857 #ifdef FEAT_VISUAL
5858 else if (PAST_LINE)
5860 curwin->w_set_curswant = TRUE;
5861 # ifdef FEAT_VIRTUALEDIT
5862 if (virtual_active())
5863 oneright();
5864 else
5865 # endif
5867 # ifdef FEAT_MBYTE
5868 if (has_mbyte)
5869 curwin->w_cursor.col +=
5870 (*mb_ptr2len)(ml_get_cursor());
5871 else
5872 # endif
5873 ++curwin->w_cursor.col;
5876 #endif
5878 #ifdef FEAT_FOLDING
5879 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5880 && cap->oap->op_type == OP_NOP)
5881 foldOpenCursor();
5882 #endif
5886 * Cursor left commands.
5888 * Returns TRUE when operator end should not be adjusted.
5890 static void
5891 nv_left(cap)
5892 cmdarg_T *cap;
5894 long n;
5896 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5898 /* <C-Left> and <S-Left> move a word or WORD left */
5899 if (mod_mask & MOD_MASK_CTRL)
5900 cap->arg = 1;
5901 nv_bck_word(cap);
5902 return;
5905 cap->oap->motion_type = MCHAR;
5906 cap->oap->inclusive = FALSE;
5907 for (n = cap->count1; n > 0; --n)
5909 if (oneleft() == FAIL)
5911 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
5912 * 'h' wraps to previous line if 'whichwrap' has 'h'.
5913 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
5915 if ( (((cap->cmdchar == K_BS
5916 || cap->cmdchar == Ctrl_H)
5917 && vim_strchr(p_ww, 'b') != NULL)
5918 || (cap->cmdchar == 'h'
5919 && vim_strchr(p_ww, 'h') != NULL)
5920 || (cap->cmdchar == K_LEFT
5921 && vim_strchr(p_ww, '<') != NULL))
5922 && curwin->w_cursor.lnum > 1)
5924 --(curwin->w_cursor.lnum);
5925 coladvance((colnr_T)MAXCOL);
5926 curwin->w_set_curswant = TRUE;
5928 /* When the NL before the first char has to be deleted we
5929 * put the cursor on the NUL after the previous line.
5930 * This is a very special case, be careful!
5931 * Don't adjust op_end now, otherwise it won't work. */
5932 if ( (cap->oap->op_type == OP_DELETE
5933 || cap->oap->op_type == OP_CHANGE)
5934 && !lineempty(curwin->w_cursor.lnum))
5936 if (*ml_get_cursor() != NUL)
5937 ++curwin->w_cursor.col;
5938 cap->retval |= CA_NO_ADJ_OP_END;
5940 continue;
5942 /* Only beep and flush if not moved at all */
5943 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
5944 beep_flush();
5945 break;
5948 #ifdef FEAT_FOLDING
5949 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5950 && cap->oap->op_type == OP_NOP)
5951 foldOpenCursor();
5952 #endif
5956 * Cursor up commands.
5957 * cap->arg is TRUE for "-": Move cursor to first non-blank.
5959 static void
5960 nv_up(cap)
5961 cmdarg_T *cap;
5963 if (mod_mask & MOD_MASK_SHIFT)
5965 /* <S-Up> is page up */
5966 cap->arg = BACKWARD;
5967 nv_page(cap);
5969 else
5971 cap->oap->motion_type = MLINE;
5972 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5973 clearopbeep(cap->oap);
5974 else if (cap->arg)
5975 beginline(BL_WHITE | BL_FIX);
5980 * Cursor down commands.
5981 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
5983 static void
5984 nv_down(cap)
5985 cmdarg_T *cap;
5987 if (mod_mask & MOD_MASK_SHIFT)
5989 /* <S-Down> is page down */
5990 cap->arg = FORWARD;
5991 nv_page(cap);
5993 else
5994 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5995 /* In a quickfix window a <CR> jumps to the error under the cursor. */
5996 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
5997 if (curwin->w_llist_ref == NULL)
5998 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
5999 else
6000 do_cmdline_cmd((char_u *)".ll"); /* location list window */
6001 else
6002 #endif
6004 #ifdef FEAT_CMDWIN
6005 /* In the cmdline window a <CR> executes the command. */
6006 if (cmdwin_type != 0 && cap->cmdchar == CAR)
6007 cmdwin_result = CAR;
6008 else
6009 #endif
6011 cap->oap->motion_type = MLINE;
6012 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6013 clearopbeep(cap->oap);
6014 else if (cap->arg)
6015 beginline(BL_WHITE | BL_FIX);
6020 #ifdef FEAT_SEARCHPATH
6022 * Grab the file name under the cursor and edit it.
6024 static void
6025 nv_gotofile(cap)
6026 cmdarg_T *cap;
6028 char_u *ptr;
6029 linenr_T lnum = -1;
6031 if (text_locked())
6033 clearopbeep(cap->oap);
6034 text_locked_msg();
6035 return;
6037 #ifdef FEAT_AUTOCMD
6038 if (curbuf_locked())
6040 clearop(cap->oap);
6041 return;
6043 #endif
6045 ptr = grab_file_name(cap->count1, &lnum);
6047 if (ptr != NULL)
6049 /* do autowrite if necessary */
6050 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6051 autowrite(curbuf, FALSE);
6052 setpcmark();
6053 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
6054 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
6055 if (cap->nchar == 'F' && lnum >= 0)
6057 curwin->w_cursor.lnum = lnum;
6058 check_cursor_lnum();
6059 beginline(BL_SOL | BL_FIX);
6061 vim_free(ptr);
6063 else
6064 clearop(cap->oap);
6066 #endif
6069 * <End> command: to end of current line or last line.
6071 static void
6072 nv_end(cap)
6073 cmdarg_T *cap;
6075 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
6077 cap->arg = TRUE;
6078 nv_goto(cap);
6079 cap->count1 = 1; /* to end of current line */
6081 nv_dollar(cap);
6085 * Handle the "$" command.
6087 static void
6088 nv_dollar(cap)
6089 cmdarg_T *cap;
6091 cap->oap->motion_type = MCHAR;
6092 cap->oap->inclusive = TRUE;
6093 #ifdef FEAT_VIRTUALEDIT
6094 /* In virtual mode when off the edge of a line and an operator
6095 * is pending (whew!) keep the cursor where it is.
6096 * Otherwise, send it to the end of the line. */
6097 if (!virtual_active() || gchar_cursor() != NUL
6098 || cap->oap->op_type == OP_NOP)
6099 #endif
6100 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6101 if (cursor_down((long)(cap->count1 - 1),
6102 cap->oap->op_type == OP_NOP) == FAIL)
6103 clearopbeep(cap->oap);
6104 #ifdef FEAT_FOLDING
6105 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6106 foldOpenCursor();
6107 #endif
6111 * Implementation of '?' and '/' commands.
6112 * If cap->arg is TRUE don't set PC mark.
6114 static void
6115 nv_search(cap)
6116 cmdarg_T *cap;
6118 oparg_T *oap = cap->oap;
6120 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6122 /* Translate "g??" to "g?g?" */
6123 cap->cmdchar = 'g';
6124 cap->nchar = '?';
6125 nv_operator(cap);
6126 return;
6129 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6131 if (cap->searchbuf == NULL)
6133 clearop(oap);
6134 return;
6137 normal_search(cap, cap->cmdchar, cap->searchbuf,
6138 (cap->arg ? 0 : SEARCH_MARK));
6142 * Handle "N" and "n" commands.
6143 * cap->arg is SEARCH_REV for "N", 0 for "n".
6145 static void
6146 nv_next(cap)
6147 cmdarg_T *cap;
6149 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6153 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6154 * Uses only cap->count1 and cap->oap from "cap".
6156 static void
6157 normal_search(cap, dir, pat, opt)
6158 cmdarg_T *cap;
6159 int dir;
6160 char_u *pat;
6161 int opt; /* extra flags for do_search() */
6163 int i;
6165 cap->oap->motion_type = MCHAR;
6166 cap->oap->inclusive = FALSE;
6167 cap->oap->use_reg_one = TRUE;
6168 curwin->w_set_curswant = TRUE;
6170 i = do_search(cap->oap, dir, pat, cap->count1,
6171 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
6172 if (i == 0)
6173 clearop(cap->oap);
6174 else
6176 if (i == 2)
6177 cap->oap->motion_type = MLINE;
6178 #ifdef FEAT_VIRTUALEDIT
6179 curwin->w_cursor.coladd = 0;
6180 #endif
6181 #ifdef FEAT_FOLDING
6182 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6183 foldOpenCursor();
6184 #endif
6187 /* "/$" will put the cursor after the end of the line, may need to
6188 * correct that here */
6189 check_cursor();
6193 * Character search commands.
6194 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6195 * ',' and FALSE for ';'.
6196 * cap->nchar is NUL for ',' and ';' (repeat the search)
6198 static void
6199 nv_csearch(cap)
6200 cmdarg_T *cap;
6202 int t_cmd;
6204 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6205 t_cmd = TRUE;
6206 else
6207 t_cmd = FALSE;
6209 cap->oap->motion_type = MCHAR;
6210 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6211 clearopbeep(cap->oap);
6212 else
6214 curwin->w_set_curswant = TRUE;
6215 #ifdef FEAT_VIRTUALEDIT
6216 /* Include a Tab for "tx" and for "dfx". */
6217 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6218 && (t_cmd || cap->oap->op_type != OP_NOP))
6220 colnr_T scol, ecol;
6222 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6223 curwin->w_cursor.coladd = ecol - scol;
6225 else
6226 curwin->w_cursor.coladd = 0;
6227 #endif
6228 #ifdef FEAT_VISUAL
6229 adjust_for_sel(cap);
6230 #endif
6231 #ifdef FEAT_FOLDING
6232 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6233 foldOpenCursor();
6234 #endif
6239 * "[" and "]" commands.
6240 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6242 static void
6243 nv_brackets(cap)
6244 cmdarg_T *cap;
6246 pos_T new_pos;
6247 pos_T prev_pos;
6248 pos_T *pos = NULL; /* init for GCC */
6249 pos_T old_pos; /* cursor position before command */
6250 int flag;
6251 long n;
6252 int findc;
6253 int c;
6255 cap->oap->motion_type = MCHAR;
6256 cap->oap->inclusive = FALSE;
6257 old_pos = curwin->w_cursor;
6258 #ifdef FEAT_VIRTUALEDIT
6259 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6260 #endif
6262 #ifdef FEAT_SEARCHPATH
6264 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6266 if (cap->nchar == 'f')
6267 nv_gotofile(cap);
6268 else
6269 #endif
6271 #ifdef FEAT_FIND_ID
6273 * Find the occurrence(s) of the identifier or define under cursor
6274 * in current and included files or jump to the first occurrence.
6276 * search list jump
6277 * fwd bwd fwd bwd fwd bwd
6278 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6279 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6281 if (vim_strchr((char_u *)
6282 #ifdef EBCDIC
6283 "iI\005dD\067",
6284 #else
6285 "iI\011dD\004",
6286 #endif
6287 cap->nchar) != NULL)
6289 char_u *ptr;
6290 int len;
6292 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6293 clearop(cap->oap);
6294 else
6296 find_pattern_in_path(ptr, 0, len, TRUE,
6297 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6298 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6299 cap->count1,
6300 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6301 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6302 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6303 (linenr_T)MAXLNUM);
6304 curwin->w_set_curswant = TRUE;
6307 else
6308 #endif
6311 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6312 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6313 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6314 * "[m" or "]m" search for prev/next start of (Java) method.
6315 * "[M" or "]M" search for prev/next end of (Java) method.
6317 if ( (cap->cmdchar == '['
6318 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6319 || (cap->cmdchar == ']'
6320 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6322 if (cap->nchar == '*')
6323 cap->nchar = '/';
6324 new_pos.lnum = 0;
6325 prev_pos.lnum = 0;
6326 if (cap->nchar == 'm' || cap->nchar == 'M')
6328 if (cap->cmdchar == '[')
6329 findc = '{';
6330 else
6331 findc = '}';
6332 n = 9999;
6334 else
6336 findc = cap->nchar;
6337 n = cap->count1;
6339 for ( ; n > 0; --n)
6341 if ((pos = findmatchlimit(cap->oap, findc,
6342 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6344 if (new_pos.lnum == 0) /* nothing found */
6346 if (cap->nchar != 'm' && cap->nchar != 'M')
6347 clearopbeep(cap->oap);
6349 else
6350 pos = &new_pos; /* use last one found */
6351 break;
6353 prev_pos = new_pos;
6354 curwin->w_cursor = *pos;
6355 new_pos = *pos;
6357 curwin->w_cursor = old_pos;
6360 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6361 * brought us to the match for "[m" and "]M" when inside a method.
6362 * Try finding the '{' or '}' we want to be at.
6363 * Also repeat for the given count.
6365 if (cap->nchar == 'm' || cap->nchar == 'M')
6367 /* norm is TRUE for "]M" and "[m" */
6368 int norm = ((findc == '{') == (cap->nchar == 'm'));
6370 n = cap->count1;
6371 /* found a match: we were inside a method */
6372 if (prev_pos.lnum != 0)
6374 pos = &prev_pos;
6375 curwin->w_cursor = prev_pos;
6376 if (norm)
6377 --n;
6379 else
6380 pos = NULL;
6381 while (n > 0)
6383 for (;;)
6385 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6387 /* if not found anything, that's an error */
6388 if (pos == NULL)
6389 clearopbeep(cap->oap);
6390 n = 0;
6391 break;
6393 c = gchar_cursor();
6394 if (c == '{' || c == '}')
6396 /* Must have found end/start of class: use it.
6397 * Or found the place to be at. */
6398 if ((c == findc && norm) || (n == 1 && !norm))
6400 new_pos = curwin->w_cursor;
6401 pos = &new_pos;
6402 n = 0;
6404 /* if no match found at all, we started outside of the
6405 * class and we're inside now. Just go on. */
6406 else if (new_pos.lnum == 0)
6408 new_pos = curwin->w_cursor;
6409 pos = &new_pos;
6411 /* found start/end of other method: go to match */
6412 else if ((pos = findmatchlimit(cap->oap, findc,
6413 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6414 0)) == NULL)
6415 n = 0;
6416 else
6417 curwin->w_cursor = *pos;
6418 break;
6421 --n;
6423 curwin->w_cursor = old_pos;
6424 if (pos == NULL && new_pos.lnum != 0)
6425 clearopbeep(cap->oap);
6427 if (pos != NULL)
6429 setpcmark();
6430 curwin->w_cursor = *pos;
6431 curwin->w_set_curswant = TRUE;
6432 #ifdef FEAT_FOLDING
6433 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6434 && cap->oap->op_type == OP_NOP)
6435 foldOpenCursor();
6436 #endif
6441 * "[[", "[]", "]]" and "][": move to start or end of function
6443 else if (cap->nchar == '[' || cap->nchar == ']')
6445 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6446 flag = '{';
6447 else
6448 flag = '}'; /* "][" or "[]" */
6450 curwin->w_set_curswant = TRUE;
6452 * Imitate strange Vi behaviour: When using "]]" with an operator
6453 * we also stop at '}'.
6455 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
6456 (cap->oap->op_type != OP_NOP
6457 && cap->arg == FORWARD && flag == '{')))
6458 clearopbeep(cap->oap);
6459 else
6461 if (cap->oap->op_type == OP_NOP)
6462 beginline(BL_WHITE | BL_FIX);
6463 #ifdef FEAT_FOLDING
6464 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6465 foldOpenCursor();
6466 #endif
6471 * "[p", "[P", "]P" and "]p": put with indent adjustment
6473 else if (cap->nchar == 'p' || cap->nchar == 'P')
6475 if (!checkclearop(cap->oap))
6477 prep_redo_cmd(cap);
6478 do_put(cap->oap->regname,
6479 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6480 cap->count1, PUT_FIXINDENT);
6485 * "['", "[`", "]'" and "]`": jump to next mark
6487 else if (cap->nchar == '\'' || cap->nchar == '`')
6489 pos = &curwin->w_cursor;
6490 for (n = cap->count1; n > 0; --n)
6492 prev_pos = *pos;
6493 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6494 cap->nchar == '\'');
6495 if (pos == NULL)
6496 break;
6498 if (pos == NULL)
6499 pos = &prev_pos;
6500 nv_cursormark(cap, cap->nchar == '\'', pos);
6503 #ifdef FEAT_MOUSE
6505 * [ or ] followed by a middle mouse click: put selected text with
6506 * indent adjustment. Any other button just does as usual.
6508 else if (cap->nchar >= K_LEFTMOUSE && cap->nchar <= K_RIGHTRELEASE)
6510 (void)do_mouse(cap->oap, cap->nchar,
6511 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6512 cap->count1, PUT_FIXINDENT);
6514 #endif /* FEAT_MOUSE */
6516 #ifdef FEAT_FOLDING
6518 * "[z" and "]z": move to start or end of open fold.
6520 else if (cap->nchar == 'z')
6522 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6523 cap->count1) == FAIL)
6524 clearopbeep(cap->oap);
6526 #endif
6528 #ifdef FEAT_DIFF
6530 * "[c" and "]c": move to next or previous diff-change.
6532 else if (cap->nchar == 'c')
6534 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6535 cap->count1) == FAIL)
6536 clearopbeep(cap->oap);
6538 #endif
6540 #ifdef FEAT_SPELL
6542 * "[s", "[S", "]s" and "]S": move to next spell error.
6544 else if (cap->nchar == 's' || cap->nchar == 'S')
6546 setpcmark();
6547 for (n = 0; n < cap->count1; ++n)
6548 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6549 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
6551 clearopbeep(cap->oap);
6552 break;
6554 # ifdef FEAT_FOLDING
6555 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6556 foldOpenCursor();
6557 # endif
6559 #endif
6561 /* Not a valid cap->nchar. */
6562 else
6563 clearopbeep(cap->oap);
6567 * Handle Normal mode "%" command.
6569 static void
6570 nv_percent(cap)
6571 cmdarg_T *cap;
6573 pos_T *pos;
6574 #ifdef FEAT_FOLDING
6575 linenr_T lnum = curwin->w_cursor.lnum;
6576 #endif
6578 cap->oap->inclusive = TRUE;
6579 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6581 if (cap->count0 > 100)
6582 clearopbeep(cap->oap);
6583 else
6585 cap->oap->motion_type = MLINE;
6586 setpcmark();
6587 /* Round up, so CTRL-G will give same value. Watch out for a
6588 * large line count, the line number must not go negative! */
6589 if (curbuf->b_ml.ml_line_count > 1000000)
6590 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6591 / 100L * cap->count0;
6592 else
6593 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6594 cap->count0 + 99L) / 100L;
6595 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6596 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6597 beginline(BL_SOL | BL_FIX);
6600 else /* "%" : go to matching paren */
6602 cap->oap->motion_type = MCHAR;
6603 cap->oap->use_reg_one = TRUE;
6604 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6605 clearopbeep(cap->oap);
6606 else
6608 setpcmark();
6609 curwin->w_cursor = *pos;
6610 curwin->w_set_curswant = TRUE;
6611 #ifdef FEAT_VIRTUALEDIT
6612 curwin->w_cursor.coladd = 0;
6613 #endif
6614 #ifdef FEAT_VISUAL
6615 adjust_for_sel(cap);
6616 #endif
6619 #ifdef FEAT_FOLDING
6620 if (cap->oap->op_type == OP_NOP
6621 && lnum != curwin->w_cursor.lnum
6622 && (fdo_flags & FDO_PERCENT)
6623 && KeyTyped)
6624 foldOpenCursor();
6625 #endif
6629 * Handle "(" and ")" commands.
6630 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6632 static void
6633 nv_brace(cap)
6634 cmdarg_T *cap;
6636 cap->oap->motion_type = MCHAR;
6637 cap->oap->use_reg_one = TRUE;
6638 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6639 cap->oap->inclusive = FALSE;
6640 curwin->w_set_curswant = TRUE;
6642 if (findsent(cap->arg, cap->count1) == FAIL)
6643 clearopbeep(cap->oap);
6644 else
6646 /* Don't leave the cursor on the NUL past end of line. */
6647 adjust_cursor(cap->oap);
6648 #ifdef FEAT_VIRTUALEDIT
6649 curwin->w_cursor.coladd = 0;
6650 #endif
6651 #ifdef FEAT_FOLDING
6652 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6653 foldOpenCursor();
6654 #endif
6659 * "m" command: Mark a position.
6661 static void
6662 nv_mark(cap)
6663 cmdarg_T *cap;
6665 if (!checkclearop(cap->oap))
6667 if (setmark(cap->nchar) == FAIL)
6668 clearopbeep(cap->oap);
6673 * "{" and "}" commands.
6674 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6676 static void
6677 nv_findpar(cap)
6678 cmdarg_T *cap;
6680 cap->oap->motion_type = MCHAR;
6681 cap->oap->inclusive = FALSE;
6682 cap->oap->use_reg_one = TRUE;
6683 curwin->w_set_curswant = TRUE;
6684 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
6685 clearopbeep(cap->oap);
6686 else
6688 #ifdef FEAT_VIRTUALEDIT
6689 curwin->w_cursor.coladd = 0;
6690 #endif
6691 #ifdef FEAT_FOLDING
6692 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6693 foldOpenCursor();
6694 #endif
6699 * "u" command: Undo or make lower case.
6701 static void
6702 nv_undo(cap)
6703 cmdarg_T *cap;
6705 if (cap->oap->op_type == OP_LOWER
6706 #ifdef FEAT_VISUAL
6707 || VIsual_active
6708 #endif
6711 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6712 cap->cmdchar = 'g';
6713 cap->nchar = 'u';
6714 nv_operator(cap);
6716 else
6717 nv_kundo(cap);
6721 * <Undo> command.
6723 static void
6724 nv_kundo(cap)
6725 cmdarg_T *cap;
6727 if (!checkclearopq(cap->oap))
6729 u_undo((int)cap->count1);
6730 curwin->w_set_curswant = TRUE;
6735 * Handle the "r" command.
6737 static void
6738 nv_replace(cap)
6739 cmdarg_T *cap;
6741 char_u *ptr;
6742 int had_ctrl_v;
6743 long n;
6745 if (checkclearop(cap->oap))
6746 return;
6748 /* get another character */
6749 if (cap->nchar == Ctrl_V)
6751 had_ctrl_v = Ctrl_V;
6752 cap->nchar = get_literal();
6753 /* Don't redo a multibyte character with CTRL-V. */
6754 if (cap->nchar > DEL)
6755 had_ctrl_v = NUL;
6757 else
6758 had_ctrl_v = NUL;
6760 /* Abort if the character is a special key. */
6761 if (IS_SPECIAL(cap->nchar))
6763 clearopbeep(cap->oap);
6764 return;
6767 #ifdef FEAT_VISUAL
6768 /* Visual mode "r" */
6769 if (VIsual_active)
6771 if (got_int)
6772 reset_VIsual();
6773 nv_operator(cap);
6774 return;
6776 #endif
6778 #ifdef FEAT_VIRTUALEDIT
6779 /* Break tabs, etc. */
6780 if (virtual_active())
6782 if (u_save_cursor() == FAIL)
6783 return;
6784 if (gchar_cursor() == NUL)
6786 /* Add extra space and put the cursor on the first one. */
6787 coladvance_force((colnr_T)(getviscol() + cap->count1));
6788 curwin->w_cursor.col -= cap->count1;
6790 else if (gchar_cursor() == TAB)
6791 coladvance_force(getviscol());
6793 #endif
6795 /* Abort if not enough characters to replace. */
6796 ptr = ml_get_cursor();
6797 if (STRLEN(ptr) < (unsigned)cap->count1
6798 #ifdef FEAT_MBYTE
6799 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6800 #endif
6803 clearopbeep(cap->oap);
6804 return;
6808 * Replacing with a TAB is done by edit() when it is complicated because
6809 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6810 * Other characters are done below to avoid problems with things like
6811 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6813 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6815 stuffnumReadbuff(cap->count1);
6816 stuffcharReadbuff('R');
6817 stuffcharReadbuff('\t');
6818 stuffcharReadbuff(ESC);
6819 return;
6822 /* save line for undo */
6823 if (u_save_cursor() == FAIL)
6824 return;
6826 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6829 * Replace character(s) by a single newline.
6830 * Strange vi behaviour: Only one newline is inserted.
6831 * Delete the characters here.
6832 * Insert the newline with an insert command, takes care of
6833 * autoindent. The insert command depends on being on the last
6834 * character of a line or not.
6836 #ifdef FEAT_MBYTE
6837 (void)del_chars(cap->count1, FALSE); /* delete the characters */
6838 #else
6839 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
6840 #endif
6841 stuffcharReadbuff('\r');
6842 stuffcharReadbuff(ESC);
6844 /* Give 'r' to edit(), to get the redo command right. */
6845 invoke_edit(cap, TRUE, 'r', FALSE);
6847 else
6849 prep_redo(cap->oap->regname, cap->count1,
6850 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
6852 curbuf->b_op_start = curwin->w_cursor;
6853 #ifdef FEAT_MBYTE
6854 if (has_mbyte)
6856 int old_State = State;
6858 if (cap->ncharC1 != 0)
6859 AppendCharToRedobuff(cap->ncharC1);
6860 if (cap->ncharC2 != 0)
6861 AppendCharToRedobuff(cap->ncharC2);
6863 /* This is slow, but it handles replacing a single-byte with a
6864 * multi-byte and the other way around. Also handles adding
6865 * composing characters for utf-8. */
6866 for (n = cap->count1; n > 0; --n)
6868 State = REPLACE;
6869 ins_char(cap->nchar);
6870 State = old_State;
6871 if (cap->ncharC1 != 0)
6872 ins_char(cap->ncharC1);
6873 if (cap->ncharC2 != 0)
6874 ins_char(cap->ncharC2);
6877 else
6878 #endif
6881 * Replace the characters within one line.
6883 for (n = cap->count1; n > 0; --n)
6886 * Get ptr again, because u_save and/or showmatch() will have
6887 * released the line. At the same time we let know that the
6888 * line will be changed.
6890 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
6891 ptr[curwin->w_cursor.col] = cap->nchar;
6892 if (p_sm && msg_silent == 0)
6893 showmatch(cap->nchar);
6894 ++curwin->w_cursor.col;
6896 #ifdef FEAT_NETBEANS_INTG
6897 if (usingNetbeans)
6899 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
6901 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
6902 (long)cap->count1);
6903 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
6904 &ptr[start], (int)cap->count1);
6906 #endif
6908 /* mark the buffer as changed and prepare for displaying */
6909 changed_bytes(curwin->w_cursor.lnum,
6910 (colnr_T)(curwin->w_cursor.col - cap->count1));
6912 --curwin->w_cursor.col; /* cursor on the last replaced char */
6913 #ifdef FEAT_MBYTE
6914 /* if the character on the left of the current cursor is a multi-byte
6915 * character, move two characters left */
6916 if (has_mbyte)
6917 mb_adjust_cursor();
6918 #endif
6919 curbuf->b_op_end = curwin->w_cursor;
6920 curwin->w_set_curswant = TRUE;
6921 set_last_insert(cap->nchar);
6925 #ifdef FEAT_VISUAL
6927 * 'o': Exchange start and end of Visual area.
6928 * 'O': same, but in block mode exchange left and right corners.
6930 static void
6931 v_swap_corners(cmdchar)
6932 int cmdchar;
6934 pos_T old_cursor;
6935 colnr_T left, right;
6937 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
6939 old_cursor = curwin->w_cursor;
6940 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
6941 curwin->w_cursor.lnum = VIsual.lnum;
6942 coladvance(left);
6943 VIsual = curwin->w_cursor;
6945 curwin->w_cursor.lnum = old_cursor.lnum;
6946 curwin->w_curswant = right;
6947 /* 'selection "exclusive" and cursor at right-bottom corner: move it
6948 * right one column */
6949 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
6950 ++curwin->w_curswant;
6951 coladvance(curwin->w_curswant);
6952 if (curwin->w_cursor.col == old_cursor.col
6953 #ifdef FEAT_VIRTUALEDIT
6954 && (!virtual_active()
6955 || curwin->w_cursor.coladd == old_cursor.coladd)
6956 #endif
6959 curwin->w_cursor.lnum = VIsual.lnum;
6960 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
6961 ++right;
6962 coladvance(right);
6963 VIsual = curwin->w_cursor;
6965 curwin->w_cursor.lnum = old_cursor.lnum;
6966 coladvance(left);
6967 curwin->w_curswant = left;
6970 else
6972 old_cursor = curwin->w_cursor;
6973 curwin->w_cursor = VIsual;
6974 VIsual = old_cursor;
6975 curwin->w_set_curswant = TRUE;
6978 #endif /* FEAT_VISUAL */
6981 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
6983 static void
6984 nv_Replace(cap)
6985 cmdarg_T *cap;
6987 #ifdef FEAT_VISUAL
6988 if (VIsual_active) /* "R" is replace lines */
6990 cap->cmdchar = 'c';
6991 cap->nchar = NUL;
6992 VIsual_mode = 'V';
6993 nv_operator(cap);
6995 else
6996 #endif
6997 if (!checkclearopq(cap->oap))
6999 if (!curbuf->b_p_ma)
7000 EMSG(_(e_modifiable));
7001 else
7003 #ifdef FEAT_VIRTUALEDIT
7004 if (virtual_active())
7005 coladvance(getviscol());
7006 #endif
7007 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7012 #ifdef FEAT_VREPLACE
7014 * "gr".
7016 static void
7017 nv_vreplace(cap)
7018 cmdarg_T *cap;
7020 # ifdef FEAT_VISUAL
7021 if (VIsual_active)
7023 cap->cmdchar = 'r';
7024 cap->nchar = cap->extra_char;
7025 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7027 else
7028 # endif
7029 if (!checkclearopq(cap->oap))
7031 if (!curbuf->b_p_ma)
7032 EMSG(_(e_modifiable));
7033 else
7035 if (cap->extra_char == Ctrl_V) /* get another character */
7036 cap->extra_char = get_literal();
7037 stuffcharReadbuff(cap->extra_char);
7038 stuffcharReadbuff(ESC);
7039 # ifdef FEAT_VIRTUALEDIT
7040 if (virtual_active())
7041 coladvance(getviscol());
7042 # endif
7043 invoke_edit(cap, TRUE, 'v', FALSE);
7047 #endif
7050 * Swap case for "~" command, when it does not work like an operator.
7052 static void
7053 n_swapchar(cap)
7054 cmdarg_T *cap;
7056 long n;
7057 pos_T startpos;
7058 int did_change = 0;
7059 #ifdef FEAT_NETBEANS_INTG
7060 pos_T pos;
7061 char_u *ptr;
7062 int count;
7063 #endif
7065 if (checkclearopq(cap->oap))
7066 return;
7068 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7070 clearopbeep(cap->oap);
7071 return;
7074 prep_redo_cmd(cap);
7076 if (u_save_cursor() == FAIL)
7077 return;
7079 startpos = curwin->w_cursor;
7080 #ifdef FEAT_NETBEANS_INTG
7081 pos = startpos;
7082 #endif
7083 for (n = cap->count1; n > 0; --n)
7085 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7086 inc_cursor();
7087 if (gchar_cursor() == NUL)
7089 if (vim_strchr(p_ww, '~') != NULL
7090 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7092 #ifdef FEAT_NETBEANS_INTG
7093 if (usingNetbeans)
7095 if (did_change)
7097 ptr = ml_get(pos.lnum);
7098 count = (int)STRLEN(ptr) - pos.col;
7099 netbeans_removed(curbuf, pos.lnum, pos.col,
7100 (long)count);
7101 netbeans_inserted(curbuf, pos.lnum, pos.col,
7102 &ptr[pos.col], count);
7104 pos.col = 0;
7105 pos.lnum++;
7107 #endif
7108 ++curwin->w_cursor.lnum;
7109 curwin->w_cursor.col = 0;
7110 if (n > 1)
7112 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7113 break;
7114 u_clearline();
7117 else
7118 break;
7121 #ifdef FEAT_NETBEANS_INTG
7122 if (did_change && usingNetbeans)
7124 ptr = ml_get(pos.lnum);
7125 count = curwin->w_cursor.col - pos.col;
7126 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7127 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
7129 #endif
7132 check_cursor();
7133 curwin->w_set_curswant = TRUE;
7134 if (did_change)
7136 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7137 0L);
7138 curbuf->b_op_start = startpos;
7139 curbuf->b_op_end = curwin->w_cursor;
7140 if (curbuf->b_op_end.col > 0)
7141 --curbuf->b_op_end.col;
7146 * Move cursor to mark.
7148 static void
7149 nv_cursormark(cap, flag, pos)
7150 cmdarg_T *cap;
7151 int flag;
7152 pos_T *pos;
7154 if (check_mark(pos) == FAIL)
7155 clearop(cap->oap);
7156 else
7158 if (cap->cmdchar == '\''
7159 || cap->cmdchar == '`'
7160 || cap->cmdchar == '['
7161 || cap->cmdchar == ']')
7162 setpcmark();
7163 curwin->w_cursor = *pos;
7164 if (flag)
7165 beginline(BL_WHITE | BL_FIX);
7166 else
7167 check_cursor();
7169 cap->oap->motion_type = flag ? MLINE : MCHAR;
7170 if (cap->cmdchar == '`')
7171 cap->oap->use_reg_one = TRUE;
7172 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7173 curwin->w_set_curswant = TRUE;
7176 #ifdef FEAT_VISUAL
7178 * Handle commands that are operators in Visual mode.
7180 static void
7181 v_visop(cap)
7182 cmdarg_T *cap;
7184 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7186 /* Uppercase means linewise, except in block mode, then "D" deletes till
7187 * the end of the line, and "C" replaces til EOL */
7188 if (isupper(cap->cmdchar))
7190 if (VIsual_mode != Ctrl_V)
7191 VIsual_mode = 'V';
7192 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7193 curwin->w_curswant = MAXCOL;
7195 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7196 nv_operator(cap);
7198 #endif
7201 * "s" and "S" commands.
7203 static void
7204 nv_subst(cap)
7205 cmdarg_T *cap;
7207 #ifdef FEAT_VISUAL
7208 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7210 if (cap->cmdchar == 'S')
7211 VIsual_mode = 'V';
7212 cap->cmdchar = 'c';
7213 nv_operator(cap);
7215 else
7216 #endif
7217 nv_optrans(cap);
7221 * Abbreviated commands.
7223 static void
7224 nv_abbrev(cap)
7225 cmdarg_T *cap;
7227 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7228 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7230 #ifdef FEAT_VISUAL
7231 /* in Visual mode these commands are operators */
7232 if (VIsual_active)
7233 v_visop(cap);
7234 else
7235 #endif
7236 nv_optrans(cap);
7240 * Translate a command into another command.
7242 static void
7243 nv_optrans(cap)
7244 cmdarg_T *cap;
7246 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7247 (char_u *)"d$", (char_u *)"c$",
7248 (char_u *)"cl", (char_u *)"cc",
7249 (char_u *)"yy", (char_u *)":s\r"};
7250 static char_u *str = (char_u *)"xXDCsSY&";
7252 if (!checkclearopq(cap->oap))
7254 /* In Vi "2D" doesn't delete the next line. Can't translate it
7255 * either, because "2." should also not use the count. */
7256 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7258 cap->oap->start = curwin->w_cursor;
7259 cap->oap->op_type = OP_DELETE;
7260 #ifdef FEAT_EVAL
7261 set_op_var(OP_DELETE);
7262 #endif
7263 cap->count1 = 1;
7264 nv_dollar(cap);
7265 finish_op = TRUE;
7266 ResetRedobuff();
7267 AppendCharToRedobuff('D');
7269 else
7271 if (cap->count0)
7272 stuffnumReadbuff(cap->count0);
7273 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7276 cap->opcount = 0;
7280 * "'" and "`" commands. Also for "g'" and "g`".
7281 * cap->arg is TRUE for "'" and "g'".
7283 static void
7284 nv_gomark(cap)
7285 cmdarg_T *cap;
7287 pos_T *pos;
7288 int c;
7289 #ifdef FEAT_FOLDING
7290 linenr_T lnum = curwin->w_cursor.lnum;
7291 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7292 #endif
7294 if (cap->cmdchar == 'g')
7295 c = cap->extra_char;
7296 else
7297 c = cap->nchar;
7298 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7299 if (pos == (pos_T *)-1) /* jumped to other file */
7301 if (cap->arg)
7303 check_cursor_lnum();
7304 beginline(BL_WHITE | BL_FIX);
7306 else
7307 check_cursor();
7309 else
7310 nv_cursormark(cap, cap->arg, pos);
7312 #ifdef FEAT_VIRTUALEDIT
7313 /* May need to clear the coladd that a mark includes. */
7314 if (!virtual_active())
7315 curwin->w_cursor.coladd = 0;
7316 #endif
7317 #ifdef FEAT_FOLDING
7318 if (cap->oap->op_type == OP_NOP
7319 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7320 && (fdo_flags & FDO_MARK)
7321 && old_KeyTyped)
7322 foldOpenCursor();
7323 #endif
7327 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7329 static void
7330 nv_pcmark(cap)
7331 cmdarg_T *cap;
7333 #ifdef FEAT_JUMPLIST
7334 pos_T *pos;
7335 # ifdef FEAT_FOLDING
7336 linenr_T lnum = curwin->w_cursor.lnum;
7337 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7338 # endif
7340 if (!checkclearopq(cap->oap))
7342 if (cap->cmdchar == 'g')
7343 pos = movechangelist((int)cap->count1);
7344 else
7345 pos = movemark((int)cap->count1);
7346 if (pos == (pos_T *)-1) /* jump to other file */
7348 curwin->w_set_curswant = TRUE;
7349 check_cursor();
7351 else if (pos != NULL) /* can jump */
7352 nv_cursormark(cap, FALSE, pos);
7353 else if (cap->cmdchar == 'g')
7355 if (curbuf->b_changelistlen == 0)
7356 EMSG(_("E664: changelist is empty"));
7357 else if (cap->count1 < 0)
7358 EMSG(_("E662: At start of changelist"));
7359 else
7360 EMSG(_("E663: At end of changelist"));
7362 else
7363 clearopbeep(cap->oap);
7364 # ifdef FEAT_FOLDING
7365 if (cap->oap->op_type == OP_NOP
7366 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7367 && (fdo_flags & FDO_MARK)
7368 && old_KeyTyped)
7369 foldOpenCursor();
7370 # endif
7372 #else
7373 clearopbeep(cap->oap);
7374 #endif
7378 * Handle '"' command.
7380 static void
7381 nv_regname(cap)
7382 cmdarg_T *cap;
7384 if (checkclearop(cap->oap))
7385 return;
7386 #ifdef FEAT_EVAL
7387 if (cap->nchar == '=')
7388 cap->nchar = get_expr_register();
7389 #endif
7390 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7392 cap->oap->regname = cap->nchar;
7393 cap->opcount = cap->count0; /* remember count before '"' */
7394 #ifdef FEAT_EVAL
7395 set_reg_var(cap->oap->regname);
7396 #endif
7398 else
7399 clearopbeep(cap->oap);
7402 #ifdef FEAT_VISUAL
7404 * Handle "v", "V" and "CTRL-V" commands.
7405 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7406 * is TRUE.
7407 * Handle CTRL-Q just like CTRL-V.
7409 static void
7410 nv_visual(cap)
7411 cmdarg_T *cap;
7413 if (cap->cmdchar == Ctrl_Q)
7414 cap->cmdchar = Ctrl_V;
7416 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7417 * characterwise, linewise, or blockwise. */
7418 if (cap->oap->op_type != OP_NOP)
7420 cap->oap->motion_force = cap->cmdchar;
7421 finish_op = FALSE; /* operator doesn't finish now but later */
7422 return;
7425 VIsual_select = cap->arg;
7426 if (VIsual_active) /* change Visual mode */
7428 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7429 end_visual_mode();
7430 else /* toggle char/block mode */
7431 { /* or char/line mode */
7432 VIsual_mode = cap->cmdchar;
7433 showmode();
7435 redraw_curbuf_later(INVERTED); /* update the inversion */
7437 else /* start Visual mode */
7439 check_visual_highlight();
7440 if (cap->count0) /* use previously selected part */
7442 if (resel_VIsual_mode == NUL) /* there is none */
7444 beep_flush();
7445 return;
7447 VIsual = curwin->w_cursor;
7449 VIsual_active = TRUE;
7450 VIsual_reselect = TRUE;
7451 if (!cap->arg)
7452 /* start Select mode when 'selectmode' contains "cmd" */
7453 may_start_select('c');
7454 #ifdef FEAT_MOUSE
7455 setmouse();
7456 #endif
7457 if (p_smd && msg_silent == 0)
7458 redraw_cmdline = TRUE; /* show visual mode later */
7460 * For V and ^V, we multiply the number of lines even if there
7461 * was only one -- webb
7463 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7465 curwin->w_cursor.lnum +=
7466 resel_VIsual_line_count * cap->count0 - 1;
7467 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7468 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7470 VIsual_mode = resel_VIsual_mode;
7471 if (VIsual_mode == 'v')
7473 if (resel_VIsual_line_count <= 1)
7474 curwin->w_cursor.col += resel_VIsual_col * cap->count0 - 1;
7475 else
7476 curwin->w_cursor.col = resel_VIsual_col;
7477 check_cursor_col();
7479 if (resel_VIsual_col == MAXCOL)
7481 curwin->w_curswant = MAXCOL;
7482 coladvance((colnr_T)MAXCOL);
7484 else if (VIsual_mode == Ctrl_V)
7486 validate_virtcol();
7487 curwin->w_curswant = curwin->w_virtcol
7488 + resel_VIsual_col * cap->count0 - 1;
7489 coladvance(curwin->w_curswant);
7491 else
7492 curwin->w_set_curswant = TRUE;
7493 redraw_curbuf_later(INVERTED); /* show the inversion */
7495 else
7497 if (!cap->arg)
7498 /* start Select mode when 'selectmode' contains "cmd" */
7499 may_start_select('c');
7500 n_start_visual_mode(cap->cmdchar);
7506 * Start selection for Shift-movement keys.
7508 void
7509 start_selection()
7511 /* if 'selectmode' contains "key", start Select mode */
7512 may_start_select('k');
7513 n_start_visual_mode('v');
7517 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7519 void
7520 may_start_select(c)
7521 int c;
7523 VIsual_select = (stuff_empty() && typebuf_typed()
7524 && (vim_strchr(p_slm, c) != NULL));
7528 * Start Visual mode "c".
7529 * Should set VIsual_select before calling this.
7531 static void
7532 n_start_visual_mode(c)
7533 int c;
7535 VIsual_mode = c;
7536 VIsual_active = TRUE;
7537 VIsual_reselect = TRUE;
7538 #ifdef FEAT_VIRTUALEDIT
7539 /* Corner case: the 0 position in a tab may change when going into
7540 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7542 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7543 coladvance(curwin->w_virtcol);
7544 #endif
7545 VIsual = curwin->w_cursor;
7547 #ifdef FEAT_FOLDING
7548 foldAdjustVisual();
7549 #endif
7551 #ifdef FEAT_MOUSE
7552 setmouse();
7553 #endif
7554 if (p_smd && msg_silent == 0)
7555 redraw_cmdline = TRUE; /* show visual mode later */
7556 #ifdef FEAT_CLIPBOARD
7557 /* Make sure the clipboard gets updated. Needed because start and
7558 * end may still be the same, and the selection needs to be owned */
7559 clip_star.vmode = NUL;
7560 #endif
7562 /* Only need to redraw this line, unless still need to redraw an old
7563 * Visual area (when 'lazyredraw' is set). */
7564 if (curwin->w_redr_type < INVERTED)
7566 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7567 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7571 #endif /* FEAT_VISUAL */
7574 * CTRL-W: Window commands
7576 static void
7577 nv_window(cap)
7578 cmdarg_T *cap;
7580 #ifdef FEAT_WINDOWS
7581 if (!checkclearop(cap->oap))
7582 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7583 #else
7584 (void)checkclearop(cap->oap);
7585 #endif
7589 * CTRL-Z: Suspend
7591 static void
7592 nv_suspend(cap)
7593 cmdarg_T *cap;
7595 clearop(cap->oap);
7596 #ifdef FEAT_VISUAL
7597 if (VIsual_active)
7598 end_visual_mode(); /* stop Visual mode */
7599 #endif
7600 do_cmdline_cmd((char_u *)"st");
7604 * Commands starting with "g".
7606 static void
7607 nv_g_cmd(cap)
7608 cmdarg_T *cap;
7610 oparg_T *oap = cap->oap;
7611 #ifdef FEAT_VISUAL
7612 pos_T tpos;
7613 #endif
7614 int i;
7615 int flag = FALSE;
7617 switch (cap->nchar)
7619 #ifdef MEM_PROFILE
7621 * "g^A": dump log of used memory.
7623 case Ctrl_A:
7624 vim_mem_profile_dump();
7625 break;
7626 #endif
7628 #ifdef FEAT_VREPLACE
7630 * "gR": Enter virtual replace mode.
7632 case 'R':
7633 cap->arg = TRUE;
7634 nv_Replace(cap);
7635 break;
7637 case 'r':
7638 nv_vreplace(cap);
7639 break;
7640 #endif
7642 case '&':
7643 do_cmdline_cmd((char_u *)"%s//~/&");
7644 break;
7646 #ifdef FEAT_VISUAL
7648 * "gv": Reselect the previous Visual area. If Visual already active,
7649 * exchange previous and current Visual area.
7651 case 'v':
7652 if (checkclearop(oap))
7653 break;
7655 if ( curbuf->b_visual.vi_start.lnum == 0
7656 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7657 || curbuf->b_visual.vi_end.lnum == 0)
7658 beep_flush();
7659 else
7661 /* set w_cursor to the start of the Visual area, tpos to the end */
7662 if (VIsual_active)
7664 i = VIsual_mode;
7665 VIsual_mode = curbuf->b_visual.vi_mode;
7666 curbuf->b_visual.vi_mode = i;
7667 # ifdef FEAT_EVAL
7668 curbuf->b_visual_mode_eval = i;
7669 # endif
7670 i = curwin->w_curswant;
7671 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7672 curbuf->b_visual.vi_curswant = i;
7674 tpos = curbuf->b_visual.vi_end;
7675 curbuf->b_visual.vi_end = curwin->w_cursor;
7676 curwin->w_cursor = curbuf->b_visual.vi_start;
7677 curbuf->b_visual.vi_start = VIsual;
7679 else
7681 VIsual_mode = curbuf->b_visual.vi_mode;
7682 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7683 tpos = curbuf->b_visual.vi_end;
7684 curwin->w_cursor = curbuf->b_visual.vi_start;
7687 VIsual_active = TRUE;
7688 VIsual_reselect = TRUE;
7690 /* Set Visual to the start and w_cursor to the end of the Visual
7691 * area. Make sure they are on an existing character. */
7692 check_cursor();
7693 VIsual = curwin->w_cursor;
7694 curwin->w_cursor = tpos;
7695 check_cursor();
7696 update_topline();
7698 * When called from normal "g" command: start Select mode when
7699 * 'selectmode' contains "cmd". When called for K_SELECT, always
7700 * start Select mode.
7702 if (cap->arg)
7703 VIsual_select = TRUE;
7704 else
7705 may_start_select('c');
7706 #ifdef FEAT_MOUSE
7707 setmouse();
7708 #endif
7709 #ifdef FEAT_CLIPBOARD
7710 /* Make sure the clipboard gets updated. Needed because start and
7711 * end are still the same, and the selection needs to be owned */
7712 clip_star.vmode = NUL;
7713 #endif
7714 redraw_curbuf_later(INVERTED);
7715 showmode();
7717 break;
7719 * "gV": Don't reselect the previous Visual area after a Select mode
7720 * mapping of menu.
7722 case 'V':
7723 VIsual_reselect = FALSE;
7724 break;
7727 * "gh": start Select mode.
7728 * "gH": start Select line mode.
7729 * "g^H": start Select block mode.
7731 case K_BS:
7732 cap->nchar = Ctrl_H;
7733 /* FALLTHROUGH */
7734 case 'h':
7735 case 'H':
7736 case Ctrl_H:
7737 # ifdef EBCDIC
7738 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7739 if (cap->nchar == Ctrl_H)
7740 cap->cmdchar = Ctrl_V;
7741 else
7742 # endif
7743 cap->cmdchar = cap->nchar + ('v' - 'h');
7744 cap->arg = TRUE;
7745 nv_visual(cap);
7746 break;
7747 #endif /* FEAT_VISUAL */
7750 * "gj" and "gk" two new funny movement keys -- up and down
7751 * movement based on *screen* line rather than *file* line.
7753 case 'j':
7754 case K_DOWN:
7755 /* with 'nowrap' it works just like the normal "j" command; also when
7756 * in a closed fold */
7757 if (!curwin->w_p_wrap
7758 #ifdef FEAT_FOLDING
7759 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7760 #endif
7763 oap->motion_type = MLINE;
7764 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7766 else
7767 i = nv_screengo(oap, FORWARD, cap->count1);
7768 if (i == FAIL)
7769 clearopbeep(oap);
7770 break;
7772 case 'k':
7773 case K_UP:
7774 /* with 'nowrap' it works just like the normal "k" command; also when
7775 * in a closed fold */
7776 if (!curwin->w_p_wrap
7777 #ifdef FEAT_FOLDING
7778 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7779 #endif
7782 oap->motion_type = MLINE;
7783 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7785 else
7786 i = nv_screengo(oap, BACKWARD, cap->count1);
7787 if (i == FAIL)
7788 clearopbeep(oap);
7789 break;
7792 * "gJ": join two lines without inserting a space.
7794 case 'J':
7795 nv_join(cap);
7796 break;
7799 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7800 * "gm": middle of "g0" and "g$".
7802 case '^':
7803 flag = TRUE;
7804 /* FALLTHROUGH */
7806 case '0':
7807 case 'm':
7808 case K_HOME:
7809 case K_KHOME:
7810 oap->motion_type = MCHAR;
7811 oap->inclusive = FALSE;
7812 if (curwin->w_p_wrap
7813 #ifdef FEAT_VERTSPLIT
7814 && curwin->w_width != 0
7815 #endif
7818 int width1 = W_WIDTH(curwin) - curwin_col_off();
7819 int width2 = width1 + curwin_col_off2();
7821 validate_virtcol();
7822 i = 0;
7823 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
7824 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
7826 else
7827 i = curwin->w_leftcol;
7828 /* Go to the middle of the screen line. When 'number' is on and lines
7829 * are wrapping the middle can be more to the left. */
7830 if (cap->nchar == 'm')
7831 i += (W_WIDTH(curwin) - curwin_col_off()
7832 + ((curwin->w_p_wrap && i > 0)
7833 ? curwin_col_off2() : 0)) / 2;
7834 coladvance((colnr_T)i);
7835 if (flag)
7838 i = gchar_cursor();
7839 while (vim_iswhite(i) && oneright() == OK);
7841 curwin->w_set_curswant = TRUE;
7842 break;
7844 case '_':
7845 /* "g_": to the last non-blank character in the line or <count> lines
7846 * downward. */
7847 cap->oap->motion_type = MCHAR;
7848 cap->oap->inclusive = TRUE;
7849 curwin->w_curswant = MAXCOL;
7850 if (cursor_down((long)(cap->count1 - 1),
7851 cap->oap->op_type == OP_NOP) == FAIL)
7852 clearopbeep(cap->oap);
7853 else
7855 char_u *ptr = ml_get_curline();
7857 /* In Visual mode we may end up after the line. */
7858 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
7859 --curwin->w_cursor.col;
7861 /* Decrease the cursor column until it's on a non-blank. */
7862 while (curwin->w_cursor.col > 0
7863 && vim_iswhite(ptr[curwin->w_cursor.col]))
7864 --curwin->w_cursor.col;
7865 curwin->w_set_curswant = TRUE;
7867 break;
7869 case '$':
7870 case K_END:
7871 case K_KEND:
7873 int col_off = curwin_col_off();
7875 oap->motion_type = MCHAR;
7876 oap->inclusive = TRUE;
7877 if (curwin->w_p_wrap
7878 #ifdef FEAT_VERTSPLIT
7879 && curwin->w_width != 0
7880 #endif
7883 curwin->w_curswant = MAXCOL; /* so we stay at the end */
7884 if (cap->count1 == 1)
7886 int width1 = W_WIDTH(curwin) - col_off;
7887 int width2 = width1 + curwin_col_off2();
7889 validate_virtcol();
7890 i = width1 - 1;
7891 if (curwin->w_virtcol >= (colnr_T)width1)
7892 i += ((curwin->w_virtcol - width1) / width2 + 1)
7893 * width2;
7894 coladvance((colnr_T)i);
7895 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
7896 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
7899 * Check for landing on a character that got split at
7900 * the end of the line. We do not want to advance to
7901 * the next screen line.
7903 validate_virtcol();
7904 if (curwin->w_virtcol > (colnr_T)i)
7905 --curwin->w_cursor.col;
7907 #endif
7909 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
7910 clearopbeep(oap);
7912 else
7914 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
7915 coladvance((colnr_T)i);
7917 /* Make sure we stick in this column. */
7918 validate_virtcol();
7919 curwin->w_curswant = curwin->w_virtcol;
7920 curwin->w_set_curswant = FALSE;
7923 break;
7926 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
7928 case '*':
7929 case '#':
7930 #if POUND != '#'
7931 case POUND: /* pound sign (sometimes equal to '#') */
7932 #endif
7933 case Ctrl_RSB: /* :tag or :tselect for current identifier */
7934 case ']': /* :tselect for current identifier */
7935 nv_ident(cap);
7936 break;
7939 * ge and gE: go back to end of word
7941 case 'e':
7942 case 'E':
7943 oap->motion_type = MCHAR;
7944 curwin->w_set_curswant = TRUE;
7945 oap->inclusive = TRUE;
7946 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
7947 clearopbeep(oap);
7948 break;
7951 * "g CTRL-G": display info about cursor position
7953 case Ctrl_G:
7954 cursor_pos_info();
7955 break;
7958 * "gi": start Insert at the last position.
7960 case 'i':
7961 if (curbuf->b_last_insert.lnum != 0)
7963 curwin->w_cursor = curbuf->b_last_insert;
7964 check_cursor_lnum();
7965 i = (int)STRLEN(ml_get_curline());
7966 if (curwin->w_cursor.col > (colnr_T)i)
7968 #ifdef FEAT_VIRTUALEDIT
7969 if (virtual_active())
7970 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
7971 #endif
7972 curwin->w_cursor.col = i;
7975 cap->cmdchar = 'i';
7976 nv_edit(cap);
7977 break;
7980 * "gI": Start insert in column 1.
7982 case 'I':
7983 beginline(0);
7984 if (!checkclearopq(oap))
7985 invoke_edit(cap, FALSE, 'g', FALSE);
7986 break;
7988 #ifdef FEAT_SEARCHPATH
7990 * "gf": goto file, edit file under cursor
7991 * "]f" and "[f": can also be used.
7993 case 'f':
7994 case 'F':
7995 nv_gotofile(cap);
7996 break;
7997 #endif
7999 /* "g'm" and "g`m": jump to mark without setting pcmark */
8000 case '\'':
8001 cap->arg = TRUE;
8002 /*FALLTHROUGH*/
8003 case '`':
8004 nv_gomark(cap);
8005 break;
8008 * "gs": Goto sleep.
8010 case 's':
8011 do_sleep(cap->count1 * 1000L);
8012 break;
8015 * "ga": Display the ascii value of the character under the
8016 * cursor. It is displayed in decimal, hex, and octal. -- webb
8018 case 'a':
8019 do_ascii(NULL);
8020 break;
8022 #ifdef FEAT_MBYTE
8024 * "g8": Display the bytes used for the UTF-8 character under the
8025 * cursor. It is displayed in hex.
8026 * "8g8" finds illegal byte sequence.
8028 case '8':
8029 if (cap->count0 == 8)
8030 utf_find_illegal();
8031 else
8032 show_utf8();
8033 break;
8034 #endif
8036 case '<':
8037 show_sb_text();
8038 break;
8041 * "gg": Goto the first line in file. With a count it goes to
8042 * that line number like for "G". -- webb
8044 case 'g':
8045 cap->arg = FALSE;
8046 nv_goto(cap);
8047 break;
8050 * Two-character operators:
8051 * "gq" Format text
8052 * "gw" Format text and keep cursor position
8053 * "g~" Toggle the case of the text.
8054 * "gu" Change text to lower case.
8055 * "gU" Change text to upper case.
8056 * "g?" rot13 encoding
8057 * "g@" call 'operatorfunc'
8059 case 'q':
8060 case 'w':
8061 oap->cursor_start = curwin->w_cursor;
8062 /*FALLTHROUGH*/
8063 case '~':
8064 case 'u':
8065 case 'U':
8066 case '?':
8067 case '@':
8068 nv_operator(cap);
8069 break;
8072 * "gd": Find first occurrence of pattern under the cursor in the
8073 * current function
8074 * "gD": idem, but in the current file.
8076 case 'd':
8077 case 'D':
8078 nv_gd(oap, cap->nchar, (int)cap->count0);
8079 break;
8081 #ifdef FEAT_MOUSE
8083 * g<*Mouse> : <C-*mouse>
8085 case K_MIDDLEMOUSE:
8086 case K_MIDDLEDRAG:
8087 case K_MIDDLERELEASE:
8088 case K_LEFTMOUSE:
8089 case K_LEFTDRAG:
8090 case K_LEFTRELEASE:
8091 case K_RIGHTMOUSE:
8092 case K_RIGHTDRAG:
8093 case K_RIGHTRELEASE:
8094 case K_X1MOUSE:
8095 case K_X1DRAG:
8096 case K_X1RELEASE:
8097 case K_X2MOUSE:
8098 case K_X2DRAG:
8099 case K_X2RELEASE:
8100 mod_mask = MOD_MASK_CTRL;
8101 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8102 break;
8103 #endif
8105 case K_IGNORE:
8106 break;
8109 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8111 case 'p':
8112 case 'P':
8113 nv_put(cap);
8114 break;
8116 #ifdef FEAT_BYTEOFF
8117 /* "go": goto byte count from start of buffer */
8118 case 'o':
8119 goto_byte(cap->count0);
8120 break;
8121 #endif
8123 /* "gQ": improved Ex mode */
8124 case 'Q':
8125 if (text_locked())
8127 clearopbeep(cap->oap);
8128 text_locked_msg();
8129 break;
8132 if (!checkclearopq(oap))
8133 do_exmode(TRUE);
8134 break;
8136 #ifdef FEAT_JUMPLIST
8137 case ',':
8138 nv_pcmark(cap);
8139 break;
8141 case ';':
8142 cap->count1 = -cap->count1;
8143 nv_pcmark(cap);
8144 break;
8145 #endif
8147 #ifdef FEAT_WINDOWS
8148 case 't':
8149 goto_tabpage((int)cap->count0);
8150 break;
8151 case 'T':
8152 goto_tabpage(-(int)cap->count1);
8153 break;
8154 #endif
8156 case '+':
8157 case '-': /* "g+" and "g-": undo or redo along the timeline */
8158 if (!checkclearopq(oap))
8159 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
8160 FALSE, FALSE);
8161 break;
8163 default:
8164 clearopbeep(oap);
8165 break;
8170 * Handle "o" and "O" commands.
8172 static void
8173 n_opencmd(cap)
8174 cmdarg_T *cap;
8176 if (!checkclearopq(cap->oap))
8178 #ifdef FEAT_FOLDING
8179 if (cap->cmdchar == 'O')
8180 /* Open above the first line of a folded sequence of lines */
8181 (void)hasFolding(curwin->w_cursor.lnum,
8182 &curwin->w_cursor.lnum, NULL);
8183 else
8184 /* Open below the last line of a folded sequence of lines */
8185 (void)hasFolding(curwin->w_cursor.lnum,
8186 NULL, &curwin->w_cursor.lnum);
8187 #endif
8188 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8189 (cap->cmdchar == 'O' ? 1 : 0)),
8190 (linenr_T)(curwin->w_cursor.lnum +
8191 (cap->cmdchar == 'o' ? 1 : 0))
8192 ) == OK
8193 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8194 #ifdef FEAT_COMMENTS
8195 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8196 #endif
8197 0, 0))
8199 /* When '#' is in 'cpoptions' ignore the count. */
8200 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8201 cap->count1 = 1;
8202 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8208 * "." command: redo last change.
8210 static void
8211 nv_dot(cap)
8212 cmdarg_T *cap;
8214 if (!checkclearopq(cap->oap))
8217 * If "restart_edit" is TRUE, the last but one command is repeated
8218 * instead of the last command (inserting text). This is used for
8219 * CTRL-O <.> in insert mode.
8221 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8222 clearopbeep(cap->oap);
8227 * CTRL-R: undo undo
8229 static void
8230 nv_redo(cap)
8231 cmdarg_T *cap;
8233 if (!checkclearopq(cap->oap))
8235 u_redo((int)cap->count1);
8236 curwin->w_set_curswant = TRUE;
8241 * Handle "U" command.
8243 static void
8244 nv_Undo(cap)
8245 cmdarg_T *cap;
8247 /* In Visual mode and typing "gUU" triggers an operator */
8248 if (cap->oap->op_type == OP_UPPER
8249 #ifdef FEAT_VISUAL
8250 || VIsual_active
8251 #endif
8254 /* translate "gUU" to "gUgU" */
8255 cap->cmdchar = 'g';
8256 cap->nchar = 'U';
8257 nv_operator(cap);
8259 else if (!checkclearopq(cap->oap))
8261 u_undoline();
8262 curwin->w_set_curswant = TRUE;
8267 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8268 * single character.
8270 static void
8271 nv_tilde(cap)
8272 cmdarg_T *cap;
8274 if (!p_to
8275 #ifdef FEAT_VISUAL
8276 && !VIsual_active
8277 #endif
8278 && cap->oap->op_type != OP_TILDE)
8279 n_swapchar(cap);
8280 else
8281 nv_operator(cap);
8285 * Handle an operator command.
8286 * The actual work is done by do_pending_operator().
8288 static void
8289 nv_operator(cap)
8290 cmdarg_T *cap;
8292 int op_type;
8294 op_type = get_op_type(cap->cmdchar, cap->nchar);
8296 if (op_type == cap->oap->op_type) /* double operator works on lines */
8297 nv_lineop(cap);
8298 else if (!checkclearop(cap->oap))
8300 cap->oap->start = curwin->w_cursor;
8301 cap->oap->op_type = op_type;
8302 #ifdef FEAT_EVAL
8303 set_op_var(op_type);
8304 #endif
8308 #ifdef FEAT_EVAL
8310 * Set v:operator to the characters for "optype".
8312 static void
8313 set_op_var(optype)
8314 int optype;
8316 char_u opchars[3];
8318 if (optype == OP_NOP)
8319 set_vim_var_string(VV_OP, NULL, 0);
8320 else
8322 opchars[0] = get_op_char(optype);
8323 opchars[1] = get_extra_op_char(optype);
8324 opchars[2] = NUL;
8325 set_vim_var_string(VV_OP, opchars, -1);
8328 #endif
8331 * Handle linewise operator "dd", "yy", etc.
8333 * "_" is is a strange motion command that helps make operators more logical.
8334 * It is actually implemented, but not documented in the real Vi. This motion
8335 * command actually refers to "the current line". Commands like "dd" and "yy"
8336 * are really an alternate form of "d_" and "y_". It does accept a count, so
8337 * "d3_" works to delete 3 lines.
8339 static void
8340 nv_lineop(cap)
8341 cmdarg_T *cap;
8343 cap->oap->motion_type = MLINE;
8344 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8345 clearopbeep(cap->oap);
8346 else if ( cap->oap->op_type == OP_DELETE
8347 || cap->oap->op_type == OP_LSHIFT
8348 || cap->oap->op_type == OP_RSHIFT)
8349 beginline(BL_SOL | BL_FIX);
8350 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8351 beginline(BL_WHITE | BL_FIX);
8355 * <Home> command.
8357 static void
8358 nv_home(cap)
8359 cmdarg_T *cap;
8361 /* CTRL-HOME is like "gg" */
8362 if (mod_mask & MOD_MASK_CTRL)
8363 nv_goto(cap);
8364 else
8366 cap->count0 = 1;
8367 nv_pipe(cap);
8369 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8370 one-character line). */
8374 * "|" command.
8376 static void
8377 nv_pipe(cap)
8378 cmdarg_T *cap;
8380 cap->oap->motion_type = MCHAR;
8381 cap->oap->inclusive = FALSE;
8382 beginline(0);
8383 if (cap->count0 > 0)
8385 coladvance((colnr_T)(cap->count0 - 1));
8386 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8388 else
8389 curwin->w_curswant = 0;
8390 /* keep curswant at the column where we wanted to go, not where
8391 we ended; differs if line is too short */
8392 curwin->w_set_curswant = FALSE;
8396 * Handle back-word command "b" and "B".
8397 * cap->arg is 1 for "B"
8399 static void
8400 nv_bck_word(cap)
8401 cmdarg_T *cap;
8403 cap->oap->motion_type = MCHAR;
8404 cap->oap->inclusive = FALSE;
8405 curwin->w_set_curswant = TRUE;
8406 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8407 clearopbeep(cap->oap);
8408 #ifdef FEAT_FOLDING
8409 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8410 foldOpenCursor();
8411 #endif
8415 * Handle word motion commands "e", "E", "w" and "W".
8416 * cap->arg is TRUE for "E" and "W".
8418 static void
8419 nv_wordcmd(cap)
8420 cmdarg_T *cap;
8422 int n;
8423 int word_end;
8424 int flag = FALSE;
8425 pos_T startpos = curwin->w_cursor;
8428 * Set inclusive for the "E" and "e" command.
8430 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8431 word_end = TRUE;
8432 else
8433 word_end = FALSE;
8434 cap->oap->inclusive = word_end;
8437 * "cw" and "cW" are a special case.
8439 if (!word_end && cap->oap->op_type == OP_CHANGE)
8441 n = gchar_cursor();
8442 if (n != NUL) /* not an empty line */
8444 if (vim_iswhite(n))
8447 * Reproduce a funny Vi behaviour: "cw" on a blank only
8448 * changes one character, not all blanks until the start of
8449 * the next word. Only do this when the 'w' flag is included
8450 * in 'cpoptions'.
8452 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8454 cap->oap->inclusive = TRUE;
8455 cap->oap->motion_type = MCHAR;
8456 return;
8459 else
8462 * This is a little strange. To match what the real Vi does,
8463 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8464 * that we are not on a space or a TAB. This seems impolite
8465 * at first, but it's really more what we mean when we say
8466 * 'cw'.
8467 * Another strangeness: When standing on the end of a word
8468 * "ce" will change until the end of the next wordt, but "cw"
8469 * will change only one character! This is done by setting
8470 * flag.
8472 cap->oap->inclusive = TRUE;
8473 word_end = TRUE;
8474 flag = TRUE;
8479 cap->oap->motion_type = MCHAR;
8480 curwin->w_set_curswant = TRUE;
8481 if (word_end)
8482 n = end_word(cap->count1, cap->arg, flag, FALSE);
8483 else
8484 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8486 /* Don't leave the cursor on the NUL past the end of line. Unless we
8487 * didn't move it forward. */
8488 if (lt(startpos, curwin->w_cursor))
8489 adjust_cursor(cap->oap);
8491 if (n == FAIL && cap->oap->op_type == OP_NOP)
8492 clearopbeep(cap->oap);
8493 else
8495 #ifdef FEAT_VISUAL
8496 adjust_for_sel(cap);
8497 #endif
8498 #ifdef FEAT_FOLDING
8499 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8500 foldOpenCursor();
8501 #endif
8506 * Used after a movement command: If the cursor ends up on the NUL after the
8507 * end of the line, may move it back to the last character and make the motion
8508 * inclusive.
8510 static void
8511 adjust_cursor(oap)
8512 oparg_T *oap;
8514 /* The cursor cannot remain on the NUL when:
8515 * - the column is > 0
8516 * - not in Visual mode or 'selection' is "o"
8517 * - 'virtualedit' is not "all" and not "onemore".
8519 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
8520 #ifdef FEAT_VISUAL
8521 && (!VIsual_active || *p_sel == 'o')
8522 #endif
8523 #ifdef FEAT_VIRTUALEDIT
8524 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8525 #endif
8528 --curwin->w_cursor.col;
8529 #ifdef FEAT_MBYTE
8530 /* prevent cursor from moving on the trail byte */
8531 if (has_mbyte)
8532 mb_adjust_cursor();
8533 #endif
8534 oap->inclusive = TRUE;
8539 * "0" and "^" commands.
8540 * cap->arg is the argument for beginline().
8542 static void
8543 nv_beginline(cap)
8544 cmdarg_T *cap;
8546 cap->oap->motion_type = MCHAR;
8547 cap->oap->inclusive = FALSE;
8548 beginline(cap->arg);
8549 #ifdef FEAT_FOLDING
8550 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8551 foldOpenCursor();
8552 #endif
8553 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8554 one-character line). */
8557 #ifdef FEAT_VISUAL
8559 * In exclusive Visual mode, may include the last character.
8561 static void
8562 adjust_for_sel(cap)
8563 cmdarg_T *cap;
8565 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8566 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8568 # ifdef FEAT_MBYTE
8569 if (has_mbyte)
8570 inc_cursor();
8571 else
8572 # endif
8573 ++curwin->w_cursor.col;
8574 cap->oap->inclusive = FALSE;
8579 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8580 * Should check VIsual_mode before calling this.
8581 * Returns TRUE when backed up to the previous line.
8583 static int
8584 unadjust_for_sel()
8586 pos_T *pp;
8588 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8590 if (lt(VIsual, curwin->w_cursor))
8591 pp = &curwin->w_cursor;
8592 else
8593 pp = &VIsual;
8594 #ifdef FEAT_VIRTUALEDIT
8595 if (pp->coladd > 0)
8596 --pp->coladd;
8597 else
8598 #endif
8599 if (pp->col > 0)
8601 --pp->col;
8602 #ifdef FEAT_MBYTE
8603 mb_adjustpos(pp);
8604 #endif
8606 else if (pp->lnum > 1)
8608 --pp->lnum;
8609 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8610 return TRUE;
8613 return FALSE;
8617 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8619 static void
8620 nv_select(cap)
8621 cmdarg_T *cap;
8623 if (VIsual_active)
8624 VIsual_select = TRUE;
8625 else if (VIsual_reselect)
8627 cap->nchar = 'v'; /* fake "gv" command */
8628 cap->arg = TRUE;
8629 nv_g_cmd(cap);
8633 #endif
8636 * "G", "gg", CTRL-END, CTRL-HOME.
8637 * cap->arg is TRUE for "G".
8639 static void
8640 nv_goto(cap)
8641 cmdarg_T *cap;
8643 linenr_T lnum;
8645 if (cap->arg)
8646 lnum = curbuf->b_ml.ml_line_count;
8647 else
8648 lnum = 1L;
8649 cap->oap->motion_type = MLINE;
8650 setpcmark();
8652 /* When a count is given, use it instead of the default lnum */
8653 if (cap->count0 != 0)
8654 lnum = cap->count0;
8655 if (lnum < 1L)
8656 lnum = 1L;
8657 else if (lnum > curbuf->b_ml.ml_line_count)
8658 lnum = curbuf->b_ml.ml_line_count;
8659 curwin->w_cursor.lnum = lnum;
8660 beginline(BL_SOL | BL_FIX);
8661 #ifdef FEAT_FOLDING
8662 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8663 foldOpenCursor();
8664 #endif
8668 * CTRL-\ in Normal mode.
8670 static void
8671 nv_normal(cap)
8672 cmdarg_T *cap;
8674 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8676 clearop(cap->oap);
8677 if (restart_edit != 0 && mode_displayed)
8678 clear_cmdline = TRUE; /* unshow mode later */
8679 restart_edit = 0;
8680 #ifdef FEAT_CMDWIN
8681 if (cmdwin_type != 0)
8682 cmdwin_result = Ctrl_C;
8683 #endif
8684 #ifdef FEAT_VISUAL
8685 if (VIsual_active)
8687 end_visual_mode(); /* stop Visual */
8688 redraw_curbuf_later(INVERTED);
8690 #endif
8691 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8692 if (cap->nchar == Ctrl_G && p_im)
8693 restart_edit = 'a';
8695 else
8696 clearopbeep(cap->oap);
8700 * ESC in Normal mode: beep, but don't flush buffers.
8701 * Don't even beep if we are canceling a command.
8703 static void
8704 nv_esc(cap)
8705 cmdarg_T *cap;
8707 int no_reason;
8709 no_reason = (cap->oap->op_type == OP_NOP
8710 && cap->opcount == 0
8711 && cap->count0 == 0
8712 && cap->oap->regname == 0
8713 && !p_im);
8715 if (cap->arg) /* TRUE for CTRL-C */
8717 if (restart_edit == 0
8718 #ifdef FEAT_CMDWIN
8719 && cmdwin_type == 0
8720 #endif
8721 #ifdef FEAT_VISUAL
8722 && !VIsual_active
8723 #endif
8724 && no_reason)
8725 MSG(_("Type :quit<Enter> to exit Vim"));
8727 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8728 * set again below when halfway a mapping. */
8729 if (!p_im)
8730 restart_edit = 0;
8731 #ifdef FEAT_CMDWIN
8732 if (cmdwin_type != 0)
8734 cmdwin_result = K_IGNORE;
8735 got_int = FALSE; /* don't stop executing autocommands et al. */
8736 return;
8738 #endif
8741 #ifdef FEAT_VISUAL
8742 if (VIsual_active)
8744 end_visual_mode(); /* stop Visual */
8745 check_cursor_col(); /* make sure cursor is not beyond EOL */
8746 curwin->w_set_curswant = TRUE;
8747 redraw_curbuf_later(INVERTED);
8749 else
8750 #endif
8751 if (no_reason)
8752 vim_beep();
8753 clearop(cap->oap);
8755 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8756 * set return to Insert mode afterwards. */
8757 if (restart_edit == 0 && goto_im()
8758 #ifdef FEAT_EX_EXTRA
8759 && ex_normal_busy == 0
8760 #endif
8762 restart_edit = 'a';
8766 * Handle "A", "a", "I", "i" and <Insert> commands.
8768 static void
8769 nv_edit(cap)
8770 cmdarg_T *cap;
8772 /* <Insert> is equal to "i" */
8773 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8774 cap->cmdchar = 'i';
8776 #ifdef FEAT_VISUAL
8777 /* in Visual mode "A" and "I" are an operator */
8778 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8779 v_visop(cap);
8781 /* in Visual mode and after an operator "a" and "i" are for text objects */
8782 else
8783 #endif
8784 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8785 && (cap->oap->op_type != OP_NOP
8786 #ifdef FEAT_VISUAL
8787 || VIsual_active
8788 #endif
8791 #ifdef FEAT_TEXTOBJ
8792 nv_object(cap);
8793 #else
8794 clearopbeep(cap->oap);
8795 #endif
8797 else if (!curbuf->b_p_ma && !p_im)
8799 /* Only give this error when 'insertmode' is off. */
8800 EMSG(_(e_modifiable));
8801 clearop(cap->oap);
8803 else if (!checkclearopq(cap->oap))
8805 switch (cap->cmdchar)
8807 case 'A': /* "A"ppend after the line */
8808 curwin->w_set_curswant = TRUE;
8809 #ifdef FEAT_VIRTUALEDIT
8810 if (ve_flags == VE_ALL)
8812 int save_State = State;
8814 /* Pretent Insert mode here to allow the cursor on the
8815 * character past the end of the line */
8816 State = INSERT;
8817 coladvance((colnr_T)MAXCOL);
8818 State = save_State;
8820 else
8821 #endif
8822 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8823 break;
8825 case 'I': /* "I"nsert before the first non-blank */
8826 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
8827 beginline(BL_WHITE);
8828 else
8829 beginline(BL_WHITE|BL_FIX);
8830 break;
8832 case 'a': /* "a"ppend is like "i"nsert on the next character. */
8833 #ifdef FEAT_VIRTUALEDIT
8834 /* increment coladd when in virtual space, increment the
8835 * column otherwise, also to append after an unprintable char */
8836 if (virtual_active()
8837 && (curwin->w_cursor.coladd > 0
8838 || *ml_get_cursor() == NUL
8839 || *ml_get_cursor() == TAB))
8840 curwin->w_cursor.coladd++;
8841 else
8842 #endif
8843 if (*ml_get_cursor() != NUL)
8844 inc_cursor();
8845 break;
8848 #ifdef FEAT_VIRTUALEDIT
8849 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
8851 int save_State = State;
8853 /* Pretent Insert mode here to allow the cursor on the
8854 * character past the end of the line */
8855 State = INSERT;
8856 coladvance(getviscol());
8857 State = save_State;
8859 #endif
8861 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
8866 * Invoke edit() and take care of "restart_edit" and the return value.
8868 static void
8869 invoke_edit(cap, repl, cmd, startln)
8870 cmdarg_T *cap;
8871 int repl; /* "r" or "gr" command */
8872 int cmd;
8873 int startln;
8875 int restart_edit_save = 0;
8877 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
8878 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
8879 * it. */
8880 if (repl || !stuff_empty())
8881 restart_edit_save = restart_edit;
8882 else
8883 restart_edit_save = 0;
8885 /* Always reset "restart_edit", this is not a restarted edit. */
8886 restart_edit = 0;
8888 if (edit(cmd, startln, cap->count1))
8889 cap->retval |= CA_COMMAND_BUSY;
8891 if (restart_edit == 0)
8892 restart_edit = restart_edit_save;
8895 #ifdef FEAT_TEXTOBJ
8897 * "a" or "i" while an operator is pending or in Visual mode: object motion.
8899 static void
8900 nv_object(cap)
8901 cmdarg_T *cap;
8903 int flag;
8904 int include;
8905 char_u *mps_save;
8907 if (cap->cmdchar == 'i')
8908 include = FALSE; /* "ix" = inner object: exclude white space */
8909 else
8910 include = TRUE; /* "ax" = an object: include white space */
8912 /* Make sure (), [], {} and <> are in 'matchpairs' */
8913 mps_save = curbuf->b_p_mps;
8914 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
8916 switch (cap->nchar)
8918 case 'w': /* "aw" = a word */
8919 flag = current_word(cap->oap, cap->count1, include, FALSE);
8920 break;
8921 case 'W': /* "aW" = a WORD */
8922 flag = current_word(cap->oap, cap->count1, include, TRUE);
8923 break;
8924 case 'b': /* "ab" = a braces block */
8925 case '(':
8926 case ')':
8927 flag = current_block(cap->oap, cap->count1, include, '(', ')');
8928 break;
8929 case 'B': /* "aB" = a Brackets block */
8930 case '{':
8931 case '}':
8932 flag = current_block(cap->oap, cap->count1, include, '{', '}');
8933 break;
8934 case '[': /* "a[" = a [] block */
8935 case ']':
8936 flag = current_block(cap->oap, cap->count1, include, '[', ']');
8937 break;
8938 case '<': /* "a<" = a <> block */
8939 case '>':
8940 flag = current_block(cap->oap, cap->count1, include, '<', '>');
8941 break;
8942 case 't': /* "at" = a tag block (xml and html) */
8943 flag = current_tagblock(cap->oap, cap->count1, include);
8944 break;
8945 case 'p': /* "ap" = a paragraph */
8946 flag = current_par(cap->oap, cap->count1, include, 'p');
8947 break;
8948 case 's': /* "as" = a sentence */
8949 flag = current_sent(cap->oap, cap->count1, include);
8950 break;
8951 case '"': /* "a"" = a double quoted string */
8952 case '\'': /* "a'" = a single quoted string */
8953 case '`': /* "a`" = a backtick quoted string */
8954 flag = current_quote(cap->oap, cap->count1, include,
8955 cap->nchar);
8956 break;
8957 #if 0 /* TODO */
8958 case 'S': /* "aS" = a section */
8959 case 'f': /* "af" = a filename */
8960 case 'u': /* "au" = a URL */
8961 #endif
8962 default:
8963 flag = FAIL;
8964 break;
8967 curbuf->b_p_mps = mps_save;
8968 if (flag == FAIL)
8969 clearopbeep(cap->oap);
8970 adjust_cursor_col();
8971 curwin->w_set_curswant = TRUE;
8973 #endif
8976 * "q" command: Start/stop recording.
8977 * "q:", "q/", "q?": edit command-line in command-line window.
8979 static void
8980 nv_record(cap)
8981 cmdarg_T *cap;
8983 if (cap->oap->op_type == OP_FORMAT)
8985 /* "gqq" is the same as "gqgq": format line */
8986 cap->cmdchar = 'g';
8987 cap->nchar = 'q';
8988 nv_operator(cap);
8990 else if (!checkclearop(cap->oap))
8992 #ifdef FEAT_CMDWIN
8993 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
8995 stuffcharReadbuff(cap->nchar);
8996 stuffcharReadbuff(K_CMDWIN);
8998 else
8999 #endif
9000 /* (stop) recording into a named register, unless executing a
9001 * register */
9002 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9003 clearopbeep(cap->oap);
9008 * Handle the "@r" command.
9010 static void
9011 nv_at(cap)
9012 cmdarg_T *cap;
9014 if (checkclearop(cap->oap))
9015 return;
9016 #ifdef FEAT_EVAL
9017 if (cap->nchar == '=')
9019 if (get_expr_register() == NUL)
9020 return;
9022 #endif
9023 while (cap->count1-- && !got_int)
9025 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
9027 clearopbeep(cap->oap);
9028 break;
9030 line_breakcheck();
9035 * Handle the CTRL-U and CTRL-D commands.
9037 static void
9038 nv_halfpage(cap)
9039 cmdarg_T *cap;
9041 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9042 || (cap->cmdchar == Ctrl_D
9043 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9044 clearopbeep(cap->oap);
9045 else if (!checkclearop(cap->oap))
9046 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9050 * Handle "J" or "gJ" command.
9052 static void
9053 nv_join(cap)
9054 cmdarg_T *cap;
9056 #ifdef FEAT_VISUAL
9057 if (VIsual_active) /* join the visual lines */
9058 nv_operator(cap);
9059 else
9060 #endif
9061 if (!checkclearop(cap->oap))
9063 if (cap->count0 <= 1)
9064 cap->count0 = 2; /* default for join is two lines! */
9065 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9066 curbuf->b_ml.ml_line_count)
9067 clearopbeep(cap->oap); /* beyond last line */
9068 else
9070 prep_redo(cap->oap->regname, cap->count0,
9071 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9072 do_do_join(cap->count0, cap->nchar == NUL);
9078 * "P", "gP", "p" and "gp" commands.
9080 static void
9081 nv_put(cap)
9082 cmdarg_T *cap;
9084 #ifdef FEAT_VISUAL
9085 int regname = 0;
9086 void *reg1 = NULL, *reg2 = NULL;
9087 int empty = FALSE;
9088 int was_visual = FALSE;
9089 #endif
9090 int dir;
9091 int flags = 0;
9093 if (cap->oap->op_type != OP_NOP)
9095 #ifdef FEAT_DIFF
9096 /* "dp" is ":diffput" */
9097 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9099 clearop(cap->oap);
9100 nv_diffgetput(TRUE);
9102 else
9103 #endif
9104 clearopbeep(cap->oap);
9106 else
9108 dir = (cap->cmdchar == 'P'
9109 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9110 ? BACKWARD : FORWARD;
9111 prep_redo_cmd(cap);
9112 if (cap->cmdchar == 'g')
9113 flags |= PUT_CURSEND;
9115 #ifdef FEAT_VISUAL
9116 if (VIsual_active)
9118 /* Putting in Visual mode: The put text replaces the selected
9119 * text. First delete the selected text, then put the new text.
9120 * Need to save and restore the registers that the delete
9121 * overwrites if the old contents is being put.
9123 was_visual = TRUE;
9124 regname = cap->oap->regname;
9125 # ifdef FEAT_CLIPBOARD
9126 adjust_clip_reg(&regname);
9127 # endif
9128 if (regname == 0 || VIM_ISDIGIT(regname)
9129 # ifdef FEAT_CLIPBOARD
9130 || (clip_unnamed && (regname == '*' || regname == '+'))
9131 # endif
9135 /* the delete is going to overwrite the register we want to
9136 * put, save it first. */
9137 reg1 = get_register(regname, TRUE);
9140 /* Now delete the selected text. */
9141 cap->cmdchar = 'd';
9142 cap->nchar = NUL;
9143 cap->oap->regname = NUL;
9144 nv_operator(cap);
9145 do_pending_operator(cap, 0, FALSE);
9146 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
9148 /* delete PUT_LINE_BACKWARD; */
9149 cap->oap->regname = regname;
9151 if (reg1 != NULL)
9153 /* Delete probably changed the register we want to put, save
9154 * it first. Then put back what was there before the delete. */
9155 reg2 = get_register(regname, FALSE);
9156 put_register(regname, reg1);
9159 /* When deleted a linewise Visual area, put the register as
9160 * lines to avoid it joined with the next line. When deletion was
9161 * characterwise, split a line when putting lines. */
9162 if (VIsual_mode == 'V')
9163 flags |= PUT_LINE;
9164 else if (VIsual_mode == 'v')
9165 flags |= PUT_LINE_SPLIT;
9166 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9167 flags |= PUT_LINE_FORWARD;
9168 dir = BACKWARD;
9169 if ((VIsual_mode != 'V'
9170 && curwin->w_cursor.col < curbuf->b_op_start.col)
9171 || (VIsual_mode == 'V'
9172 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9173 /* cursor is at the end of the line or end of file, put
9174 * forward. */
9175 dir = FORWARD;
9177 #endif
9178 do_put(cap->oap->regname, dir, cap->count1, flags);
9180 #ifdef FEAT_VISUAL
9181 /* If a register was saved, put it back now. */
9182 if (reg2 != NULL)
9183 put_register(regname, reg2);
9185 /* What to reselect with "gv"? Selecting the just put text seems to
9186 * be the most useful, since the original text was removed. */
9187 if (was_visual)
9189 curbuf->b_visual.vi_start = curbuf->b_op_start;
9190 curbuf->b_visual.vi_end = curbuf->b_op_end;
9193 /* When all lines were selected and deleted do_put() leaves an empty
9194 * line that needs to be deleted now. */
9195 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
9197 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
9199 /* If the cursor was in that line, move it to the end of the last
9200 * line. */
9201 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9203 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9204 coladvance((colnr_T)MAXCOL);
9207 #endif
9208 auto_format(FALSE, TRUE);
9213 * "o" and "O" commands.
9215 static void
9216 nv_open(cap)
9217 cmdarg_T *cap;
9219 #ifdef FEAT_DIFF
9220 /* "do" is ":diffget" */
9221 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9223 clearop(cap->oap);
9224 nv_diffgetput(FALSE);
9226 else
9227 #endif
9228 #ifdef FEAT_VISUAL
9229 if (VIsual_active) /* switch start and end of visual */
9230 v_swap_corners(cap->cmdchar);
9231 else
9232 #endif
9233 n_opencmd(cap);
9236 #ifdef FEAT_SNIFF
9237 /*ARGSUSED*/
9238 static void
9239 nv_sniff(cap)
9240 cmdarg_T *cap;
9242 ProcessSniffRequests();
9244 #endif
9246 #ifdef FEAT_NETBEANS_INTG
9247 static void
9248 nv_nbcmd(cap)
9249 cmdarg_T *cap;
9251 netbeans_keycommand(cap->nchar);
9253 #endif
9255 #ifdef FEAT_DND
9256 /*ARGSUSED*/
9257 static void
9258 nv_drop(cap)
9259 cmdarg_T *cap;
9261 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9263 #endif
9265 #ifdef FEAT_AUTOCMD
9267 * Trigger CursorHold event.
9268 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9269 * input buffer. "did_cursorhold" is set to avoid retriggering.
9271 /*ARGSUSED*/
9272 static void
9273 nv_cursorhold(cap)
9274 cmdarg_T *cap;
9276 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9277 did_cursorhold = TRUE;
9278 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
9280 #endif