Merged from the latest developing branch.
[MacVim.git] / src / normal.c
blobd33a636745e00c740172c4a6d29c9f5adb526ea7
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9 /*
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
12 * the operators.
15 #include "vim.h"
17 #ifdef FEAT_VISUAL
19 * The Visual area is remembered for reselection.
21 static int resel_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
22 static linenr_T resel_VIsual_line_count; /* number of lines */
23 static colnr_T resel_VIsual_col; /* nr of cols or end col */
25 static int restart_VIsual_select = 0;
26 #endif
28 static int
29 # ifdef __BORLANDC__
30 _RTLENTRYF
31 # endif
32 nv_compare __ARGS((const void *s1, const void *s2));
33 static int find_command __ARGS((int cmdchar));
34 static void op_colon __ARGS((oparg_T *oap));
35 static void op_function __ARGS((oparg_T *oap));
36 #if defined(FEAT_MOUSE) && defined(FEAT_VISUAL)
37 static void find_start_of_word __ARGS((pos_T *));
38 static void find_end_of_word __ARGS((pos_T *));
39 static int get_mouse_class __ARGS((char_u *p));
40 #endif
41 static void prep_redo_cmd __ARGS((cmdarg_T *cap));
42 static void prep_redo __ARGS((int regname, long, int, int, int, int, int));
43 static int checkclearop __ARGS((oparg_T *oap));
44 static int checkclearopq __ARGS((oparg_T *oap));
45 static void clearop __ARGS((oparg_T *oap));
46 static void clearopbeep __ARGS((oparg_T *oap));
47 #ifdef FEAT_VISUAL
48 static void unshift_special __ARGS((cmdarg_T *cap));
49 #endif
50 #ifdef FEAT_CMDL_INFO
51 static void del_from_showcmd __ARGS((int));
52 #endif
55 * nv_*(): functions called to handle Normal and Visual mode commands.
56 * n_*(): functions called to handle Normal mode commands.
57 * v_*(): functions called to handle Visual mode commands.
59 static void nv_ignore __ARGS((cmdarg_T *cap));
60 static void nv_nop __ARGS((cmdarg_T *cap));
61 static void nv_error __ARGS((cmdarg_T *cap));
62 static void nv_help __ARGS((cmdarg_T *cap));
63 static void nv_addsub __ARGS((cmdarg_T *cap));
64 static void nv_page __ARGS((cmdarg_T *cap));
65 static void nv_gd __ARGS((oparg_T *oap, int nchar, int thisblock));
66 static int nv_screengo __ARGS((oparg_T *oap, int dir, long dist));
67 #ifdef FEAT_MOUSE
68 static void nv_mousescroll __ARGS((cmdarg_T *cap));
69 static void nv_mouse __ARGS((cmdarg_T *cap));
70 #endif
71 static void nv_scroll_line __ARGS((cmdarg_T *cap));
72 static void nv_zet __ARGS((cmdarg_T *cap));
73 #ifdef FEAT_GUI
74 static void nv_ver_scrollbar __ARGS((cmdarg_T *cap));
75 static void nv_hor_scrollbar __ARGS((cmdarg_T *cap));
76 #endif
77 #ifdef FEAT_GUI_TABLINE
78 static void nv_tabline __ARGS((cmdarg_T *cap));
79 static void nv_tabmenu __ARGS((cmdarg_T *cap));
80 #endif
81 static void nv_exmode __ARGS((cmdarg_T *cap));
82 static void nv_colon __ARGS((cmdarg_T *cap));
83 static void nv_ctrlg __ARGS((cmdarg_T *cap));
84 static void nv_ctrlh __ARGS((cmdarg_T *cap));
85 static void nv_clear __ARGS((cmdarg_T *cap));
86 static void nv_ctrlo __ARGS((cmdarg_T *cap));
87 static void nv_hat __ARGS((cmdarg_T *cap));
88 static void nv_Zet __ARGS((cmdarg_T *cap));
89 static void nv_ident __ARGS((cmdarg_T *cap));
90 static void nv_tagpop __ARGS((cmdarg_T *cap));
91 static void nv_scroll __ARGS((cmdarg_T *cap));
92 static void nv_right __ARGS((cmdarg_T *cap));
93 static void nv_left __ARGS((cmdarg_T *cap));
94 static void nv_up __ARGS((cmdarg_T *cap));
95 static void nv_down __ARGS((cmdarg_T *cap));
96 #ifdef FEAT_SEARCHPATH
97 static void nv_gotofile __ARGS((cmdarg_T *cap));
98 #endif
99 static void nv_end __ARGS((cmdarg_T *cap));
100 static void nv_dollar __ARGS((cmdarg_T *cap));
101 static void nv_search __ARGS((cmdarg_T *cap));
102 static void nv_next __ARGS((cmdarg_T *cap));
103 static void normal_search __ARGS((cmdarg_T *cap, int dir, char_u *pat, int opt));
104 static void nv_csearch __ARGS((cmdarg_T *cap));
105 static void nv_brackets __ARGS((cmdarg_T *cap));
106 static void nv_percent __ARGS((cmdarg_T *cap));
107 static void nv_brace __ARGS((cmdarg_T *cap));
108 static void nv_mark __ARGS((cmdarg_T *cap));
109 static void nv_findpar __ARGS((cmdarg_T *cap));
110 static void nv_undo __ARGS((cmdarg_T *cap));
111 static void nv_kundo __ARGS((cmdarg_T *cap));
112 static void nv_Replace __ARGS((cmdarg_T *cap));
113 #ifdef FEAT_VREPLACE
114 static void nv_vreplace __ARGS((cmdarg_T *cap));
115 #endif
116 #ifdef FEAT_VISUAL
117 static void v_swap_corners __ARGS((int cmdchar));
118 #endif
119 static void nv_replace __ARGS((cmdarg_T *cap));
120 static void n_swapchar __ARGS((cmdarg_T *cap));
121 static void nv_cursormark __ARGS((cmdarg_T *cap, int flag, pos_T *pos));
122 #ifdef FEAT_VISUAL
123 static void v_visop __ARGS((cmdarg_T *cap));
124 #endif
125 static void nv_subst __ARGS((cmdarg_T *cap));
126 static void nv_abbrev __ARGS((cmdarg_T *cap));
127 static void nv_optrans __ARGS((cmdarg_T *cap));
128 static void nv_gomark __ARGS((cmdarg_T *cap));
129 static void nv_pcmark __ARGS((cmdarg_T *cap));
130 static void nv_regname __ARGS((cmdarg_T *cap));
131 #ifdef FEAT_VISUAL
132 static void nv_visual __ARGS((cmdarg_T *cap));
133 static void n_start_visual_mode __ARGS((int c));
134 #endif
135 static void nv_window __ARGS((cmdarg_T *cap));
136 static void nv_suspend __ARGS((cmdarg_T *cap));
137 static void nv_g_cmd __ARGS((cmdarg_T *cap));
138 static void n_opencmd __ARGS((cmdarg_T *cap));
139 static void nv_dot __ARGS((cmdarg_T *cap));
140 static void nv_redo __ARGS((cmdarg_T *cap));
141 static void nv_Undo __ARGS((cmdarg_T *cap));
142 static void nv_tilde __ARGS((cmdarg_T *cap));
143 static void nv_operator __ARGS((cmdarg_T *cap));
144 #ifdef FEAT_EVAL
145 static void set_op_var __ARGS((int optype));
146 #endif
147 static void nv_lineop __ARGS((cmdarg_T *cap));
148 static void nv_home __ARGS((cmdarg_T *cap));
149 static void nv_pipe __ARGS((cmdarg_T *cap));
150 static void nv_bck_word __ARGS((cmdarg_T *cap));
151 static void nv_wordcmd __ARGS((cmdarg_T *cap));
152 static void nv_beginline __ARGS((cmdarg_T *cap));
153 static void adjust_cursor __ARGS((oparg_T *oap));
154 #ifdef FEAT_VISUAL
155 static void adjust_for_sel __ARGS((cmdarg_T *cap));
156 static int unadjust_for_sel __ARGS((void));
157 static void nv_select __ARGS((cmdarg_T *cap));
158 #endif
159 static void nv_goto __ARGS((cmdarg_T *cap));
160 static void nv_normal __ARGS((cmdarg_T *cap));
161 static void nv_esc __ARGS((cmdarg_T *oap));
162 static void nv_edit __ARGS((cmdarg_T *cap));
163 static void invoke_edit __ARGS((cmdarg_T *cap, int repl, int cmd, int startln));
164 #ifdef FEAT_TEXTOBJ
165 static void nv_object __ARGS((cmdarg_T *cap));
166 #endif
167 static void nv_record __ARGS((cmdarg_T *cap));
168 static void nv_at __ARGS((cmdarg_T *cap));
169 static void nv_halfpage __ARGS((cmdarg_T *cap));
170 static void nv_join __ARGS((cmdarg_T *cap));
171 static void nv_put __ARGS((cmdarg_T *cap));
172 static void nv_open __ARGS((cmdarg_T *cap));
173 #ifdef FEAT_SNIFF
174 static void nv_sniff __ARGS((cmdarg_T *cap));
175 #endif
176 #ifdef FEAT_NETBEANS_INTG
177 static void nv_nbcmd __ARGS((cmdarg_T *cap));
178 #endif
179 #ifdef FEAT_DND
180 static void nv_drop __ARGS((cmdarg_T *cap));
181 #endif
182 #ifdef FEAT_AUTOCMD
183 static void nv_cursorhold __ARGS((cmdarg_T *cap));
184 #endif
186 static char *e_noident = N_("E349: No identifier under cursor");
189 * Function to be called for a Normal or Visual mode command.
190 * The argument is a cmdarg_T.
192 typedef void (*nv_func_T) __ARGS((cmdarg_T *cap));
194 /* Values for cmd_flags. */
195 #define NV_NCH 0x01 /* may need to get a second char */
196 #define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
197 #define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
198 #define NV_LANG 0x08 /* second char needs language adjustment */
200 #define NV_SS 0x10 /* may start selection */
201 #define NV_SSS 0x20 /* may start selection with shift modifier */
202 #define NV_STS 0x40 /* may stop selection without shift modif. */
203 #define NV_RL 0x80 /* 'rightleft' modifies command */
204 #define NV_KEEPREG 0x100 /* don't clear regname */
205 #define NV_NCW 0x200 /* not allowed in command-line window */
208 * Generally speaking, every Normal mode command should either clear any
209 * pending operator (with *clearop*()), or set the motion type variable
210 * oap->motion_type.
212 * When a cursor motion command is made, it is marked as being a character or
213 * line oriented motion. Then, if an operator is in effect, the operation
214 * becomes character or line oriented accordingly.
218 * This table contains one entry for every Normal or Visual mode command.
219 * The order doesn't matter, init_normal_cmds() will create a sorted index.
220 * It is faster when all keys from zero to '~' are present.
222 static const struct nv_cmd
224 int cmd_char; /* (first) command character */
225 nv_func_T cmd_func; /* function for this command */
226 short_u cmd_flags; /* NV_ flags */
227 short cmd_arg; /* value for ca.arg */
228 } nv_cmds[] =
230 {NUL, nv_error, 0, 0},
231 {Ctrl_A, nv_addsub, 0, 0},
232 {Ctrl_B, nv_page, NV_STS, BACKWARD},
233 {Ctrl_C, nv_esc, 0, TRUE},
234 {Ctrl_D, nv_halfpage, 0, 0},
235 {Ctrl_E, nv_scroll_line, 0, TRUE},
236 {Ctrl_F, nv_page, NV_STS, FORWARD},
237 {Ctrl_G, nv_ctrlg, 0, 0},
238 {Ctrl_H, nv_ctrlh, 0, 0},
239 {Ctrl_I, nv_pcmark, 0, 0},
240 {NL, nv_down, 0, FALSE},
241 {Ctrl_K, nv_error, 0, 0},
242 {Ctrl_L, nv_clear, 0, 0},
243 {Ctrl_M, nv_down, 0, TRUE},
244 {Ctrl_N, nv_down, NV_STS, FALSE},
245 {Ctrl_O, nv_ctrlo, 0, 0},
246 {Ctrl_P, nv_up, NV_STS, FALSE},
247 #ifdef FEAT_VISUAL
248 {Ctrl_Q, nv_visual, 0, FALSE},
249 #else
250 {Ctrl_Q, nv_ignore, 0, 0},
251 #endif
252 {Ctrl_R, nv_redo, 0, 0},
253 {Ctrl_S, nv_ignore, 0, 0},
254 {Ctrl_T, nv_tagpop, NV_NCW, 0},
255 {Ctrl_U, nv_halfpage, 0, 0},
256 #ifdef FEAT_VISUAL
257 {Ctrl_V, nv_visual, 0, FALSE},
258 {'V', nv_visual, 0, FALSE},
259 {'v', nv_visual, 0, FALSE},
260 #else
261 {Ctrl_V, nv_error, 0, 0},
262 {'V', nv_error, 0, 0},
263 {'v', nv_error, 0, 0},
264 #endif
265 {Ctrl_W, nv_window, 0, 0},
266 {Ctrl_X, nv_addsub, 0, 0},
267 {Ctrl_Y, nv_scroll_line, 0, FALSE},
268 {Ctrl_Z, nv_suspend, 0, 0},
269 {ESC, nv_esc, 0, FALSE},
270 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
271 {Ctrl_RSB, nv_ident, NV_NCW, 0},
272 {Ctrl_HAT, nv_hat, NV_NCW, 0},
273 {Ctrl__, nv_error, 0, 0},
274 {' ', nv_right, 0, 0},
275 {'!', nv_operator, 0, 0},
276 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
277 {'#', nv_ident, 0, 0},
278 {'$', nv_dollar, 0, 0},
279 {'%', nv_percent, 0, 0},
280 {'&', nv_optrans, 0, 0},
281 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
282 {'(', nv_brace, 0, BACKWARD},
283 {')', nv_brace, 0, FORWARD},
284 {'*', nv_ident, 0, 0},
285 {'+', nv_down, 0, TRUE},
286 {',', nv_csearch, 0, TRUE},
287 {'-', nv_up, 0, TRUE},
288 {'.', nv_dot, NV_KEEPREG, 0},
289 {'/', nv_search, 0, FALSE},
290 {'0', nv_beginline, 0, 0},
291 {'1', nv_ignore, 0, 0},
292 {'2', nv_ignore, 0, 0},
293 {'3', nv_ignore, 0, 0},
294 {'4', nv_ignore, 0, 0},
295 {'5', nv_ignore, 0, 0},
296 {'6', nv_ignore, 0, 0},
297 {'7', nv_ignore, 0, 0},
298 {'8', nv_ignore, 0, 0},
299 {'9', nv_ignore, 0, 0},
300 {':', nv_colon, 0, 0},
301 {';', nv_csearch, 0, FALSE},
302 {'<', nv_operator, NV_RL, 0},
303 {'=', nv_operator, 0, 0},
304 {'>', nv_operator, NV_RL, 0},
305 {'?', nv_search, 0, FALSE},
306 {'@', nv_at, NV_NCH_NOP, FALSE},
307 {'A', nv_edit, 0, 0},
308 {'B', nv_bck_word, 0, 1},
309 {'C', nv_abbrev, NV_KEEPREG, 0},
310 {'D', nv_abbrev, NV_KEEPREG, 0},
311 {'E', nv_wordcmd, 0, TRUE},
312 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
313 {'G', nv_goto, 0, TRUE},
314 {'H', nv_scroll, 0, 0},
315 {'I', nv_edit, 0, 0},
316 {'J', nv_join, 0, 0},
317 {'K', nv_ident, 0, 0},
318 {'L', nv_scroll, 0, 0},
319 {'M', nv_scroll, 0, 0},
320 {'N', nv_next, 0, SEARCH_REV},
321 {'O', nv_open, 0, 0},
322 {'P', nv_put, 0, 0},
323 {'Q', nv_exmode, NV_NCW, 0},
324 {'R', nv_Replace, 0, FALSE},
325 {'S', nv_subst, NV_KEEPREG, 0},
326 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
327 {'U', nv_Undo, 0, 0},
328 {'W', nv_wordcmd, 0, TRUE},
329 {'X', nv_abbrev, NV_KEEPREG, 0},
330 {'Y', nv_abbrev, NV_KEEPREG, 0},
331 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
332 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
333 {'\\', nv_error, 0, 0},
334 {']', nv_brackets, NV_NCH_ALW, FORWARD},
335 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
336 {'_', nv_lineop, 0, 0},
337 {'`', nv_gomark, NV_NCH_ALW, FALSE},
338 {'a', nv_edit, NV_NCH, 0},
339 {'b', nv_bck_word, 0, 0},
340 {'c', nv_operator, 0, 0},
341 {'d', nv_operator, 0, 0},
342 {'e', nv_wordcmd, 0, FALSE},
343 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
344 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
345 {'h', nv_left, NV_RL, 0},
346 {'i', nv_edit, NV_NCH, 0},
347 {'j', nv_down, 0, FALSE},
348 {'k', nv_up, 0, FALSE},
349 {'l', nv_right, NV_RL, 0},
350 {'m', nv_mark, NV_NCH_NOP, 0},
351 {'n', nv_next, 0, 0},
352 {'o', nv_open, 0, 0},
353 {'p', nv_put, 0, 0},
354 {'q', nv_record, NV_NCH, 0},
355 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
356 {'s', nv_subst, NV_KEEPREG, 0},
357 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
358 {'u', nv_undo, 0, 0},
359 {'w', nv_wordcmd, 0, FALSE},
360 {'x', nv_abbrev, NV_KEEPREG, 0},
361 {'y', nv_operator, 0, 0},
362 {'z', nv_zet, NV_NCH_ALW, 0},
363 {'{', nv_findpar, 0, BACKWARD},
364 {'|', nv_pipe, 0, 0},
365 {'}', nv_findpar, 0, FORWARD},
366 {'~', nv_tilde, 0, 0},
368 /* pound sign */
369 {POUND, nv_ident, 0, 0},
370 #ifdef FEAT_MOUSE
371 {K_MOUSEUP, nv_mousescroll, 0, TRUE},
372 {K_MOUSEDOWN, nv_mousescroll, 0, FALSE},
373 {K_LEFTMOUSE, nv_mouse, 0, 0},
374 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
375 {K_LEFTDRAG, nv_mouse, 0, 0},
376 {K_LEFTRELEASE, nv_mouse, 0, 0},
377 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
378 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
379 {K_MIDDLEDRAG, nv_mouse, 0, 0},
380 {K_MIDDLERELEASE, nv_mouse, 0, 0},
381 {K_RIGHTMOUSE, nv_mouse, 0, 0},
382 {K_RIGHTDRAG, nv_mouse, 0, 0},
383 {K_RIGHTRELEASE, nv_mouse, 0, 0},
384 {K_X1MOUSE, nv_mouse, 0, 0},
385 {K_X1DRAG, nv_mouse, 0, 0},
386 {K_X1RELEASE, nv_mouse, 0, 0},
387 {K_X2MOUSE, nv_mouse, 0, 0},
388 {K_X2DRAG, nv_mouse, 0, 0},
389 {K_X2RELEASE, nv_mouse, 0, 0},
390 #endif
391 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
392 {K_NOP, nv_nop, 0, 0},
393 {K_INS, nv_edit, 0, 0},
394 {K_KINS, nv_edit, 0, 0},
395 {K_BS, nv_ctrlh, 0, 0},
396 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
397 {K_S_UP, nv_page, NV_SS, BACKWARD},
398 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
399 {K_S_DOWN, nv_page, NV_SS, FORWARD},
400 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
401 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
402 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
403 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
404 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
405 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
406 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
407 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
408 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
409 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
410 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
411 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
412 {K_S_END, nv_end, NV_SS, FALSE},
413 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
414 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
415 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
416 {K_S_HOME, nv_home, NV_SS, 0},
417 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
418 {K_DEL, nv_abbrev, 0, 0},
419 {K_KDEL, nv_abbrev, 0, 0},
420 {K_UNDO, nv_kundo, 0, 0},
421 {K_HELP, nv_help, NV_NCW, 0},
422 {K_F1, nv_help, NV_NCW, 0},
423 {K_XF1, nv_help, NV_NCW, 0},
424 #ifdef FEAT_VISUAL
425 {K_SELECT, nv_select, 0, 0},
426 #endif
427 #ifdef FEAT_GUI
428 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
429 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
430 #endif
431 #ifdef FEAT_GUI_TABLINE
432 {K_TABLINE, nv_tabline, 0, 0},
433 {K_TABMENU, nv_tabmenu, 0, 0},
434 #endif
435 #ifdef FEAT_FKMAP
436 {K_F8, farsi_fkey, 0, 0},
437 {K_F9, farsi_fkey, 0, 0},
438 #endif
439 #ifdef FEAT_SNIFF
440 {K_SNIFF, nv_sniff, 0, 0},
441 #endif
442 #ifdef FEAT_NETBEANS_INTG
443 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
444 #endif
445 #ifdef FEAT_DND
446 {K_DROP, nv_drop, NV_STS, 0},
447 #endif
448 #ifdef FEAT_AUTOCMD
449 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
450 #endif
453 /* Number of commands in nv_cmds[]. */
454 #define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
456 /* Sorted index of commands in nv_cmds[]. */
457 static short nv_cmd_idx[NV_CMDS_SIZE];
459 /* The highest index for which
460 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
461 static int nv_max_linear;
464 * Compare functions for qsort() below, that checks the command character
465 * through the index in nv_cmd_idx[].
467 static int
468 #ifdef __BORLANDC__
469 _RTLENTRYF
470 #endif
471 nv_compare(s1, s2)
472 const void *s1;
473 const void *s2;
475 int c1, c2;
477 /* The commands are sorted on absolute value. */
478 c1 = nv_cmds[*(const short *)s1].cmd_char;
479 c2 = nv_cmds[*(const short *)s2].cmd_char;
480 if (c1 < 0)
481 c1 = -c1;
482 if (c2 < 0)
483 c2 = -c2;
484 return c1 - c2;
488 * Initialize the nv_cmd_idx[] table.
490 void
491 init_normal_cmds()
493 int i;
495 /* Fill the index table with a one to one relation. */
496 for (i = 0; i < NV_CMDS_SIZE; ++i)
497 nv_cmd_idx[i] = i;
499 /* Sort the commands by the command character. */
500 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
502 /* Find the first entry that can't be indexed by the command character. */
503 for (i = 0; i < NV_CMDS_SIZE; ++i)
504 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
505 break;
506 nv_max_linear = i - 1;
510 * Search for a command in the commands table.
511 * Returns -1 for invalid command.
513 static int
514 find_command(cmdchar)
515 int cmdchar;
517 int i;
518 int idx;
519 int top, bot;
520 int c;
522 #ifdef FEAT_MBYTE
523 /* A multi-byte character is never a command. */
524 if (cmdchar >= 0x100)
525 return -1;
526 #endif
528 /* We use the absolute value of the character. Special keys have a
529 * negative value, but are sorted on their absolute value. */
530 if (cmdchar < 0)
531 cmdchar = -cmdchar;
533 /* If the character is in the first part: The character is the index into
534 * nv_cmd_idx[]. */
535 if (cmdchar <= nv_max_linear)
536 return nv_cmd_idx[cmdchar];
538 /* Perform a binary search. */
539 bot = nv_max_linear + 1;
540 top = NV_CMDS_SIZE - 1;
541 idx = -1;
542 while (bot <= top)
544 i = (top + bot) / 2;
545 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
546 if (c < 0)
547 c = -c;
548 if (cmdchar == c)
550 idx = nv_cmd_idx[i];
551 break;
553 if (cmdchar > c)
554 bot = i + 1;
555 else
556 top = i - 1;
558 return idx;
562 * Execute a command in Normal mode.
564 /*ARGSUSED*/
565 void
566 normal_cmd(oap, toplevel)
567 oparg_T *oap;
568 int toplevel; /* TRUE when called from main() */
570 cmdarg_T ca; /* command arguments */
571 int c;
572 int ctrl_w = FALSE; /* got CTRL-W command */
573 int old_col = curwin->w_curswant;
574 #ifdef FEAT_CMDL_INFO
575 int need_flushbuf; /* need to call out_flush() */
576 #endif
577 #ifdef FEAT_VISUAL
578 pos_T old_pos; /* cursor position before command */
579 int mapped_len;
580 static int old_mapped_len = 0;
581 #endif
582 int idx;
584 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
585 ca.oap = oap;
587 /* Use a count remembered from before entering an operator. After typing
588 * "3d" we return from normal_cmd() and come back here, the "3" is
589 * remembered in "opcount". */
590 ca.opcount = opcount;
592 #ifdef FEAT_SNIFF
593 want_sniff_request = sniff_connected;
594 #endif
597 * If there is an operator pending, then the command we take this time
598 * will terminate it. Finish_op tells us to finish the operation before
599 * returning this time (unless the operation was cancelled).
601 #ifdef CURSOR_SHAPE
602 c = finish_op;
603 #endif
604 finish_op = (oap->op_type != OP_NOP);
605 #ifdef CURSOR_SHAPE
606 if (finish_op != c)
608 ui_cursor_shape(); /* may show different cursor shape */
609 # ifdef FEAT_MOUSESHAPE
610 update_mouseshape(-1);
611 # endif
613 #endif
615 /* When not finishing an operator and no register name typed, reset the
616 * count. */
617 if (!finish_op && !oap->regname)
618 ca.opcount = 0;
620 #ifdef FEAT_AUTOCMD
621 /* Restore counts from before receiving K_CURSORHOLD. This means after
622 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
623 * "3 * 2". */
624 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
626 ca.opcount = oap->prev_opcount;
627 ca.count0 = oap->prev_count0;
628 oap->prev_opcount = 0;
629 oap->prev_count0 = 0;
631 #endif
633 #ifdef FEAT_VISUAL
634 mapped_len = typebuf_maplen();
635 #endif
637 State = NORMAL_BUSY;
638 #ifdef USE_ON_FLY_SCROLL
639 dont_scroll = FALSE; /* allow scrolling here */
640 #endif
643 * Get the command character from the user.
645 c = safe_vgetc();
647 #ifdef FEAT_LANGMAP
648 LANGMAP_ADJUST(c, TRUE);
649 #endif
651 #ifdef FEAT_VISUAL
653 * If a mapping was started in Visual or Select mode, remember the length
654 * of the mapping. This is used below to not return to Insert mode for as
655 * long as the mapping is being executed.
657 if (restart_edit == 0)
658 old_mapped_len = 0;
659 else if (old_mapped_len
660 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
661 old_mapped_len = typebuf_maplen();
662 #endif
664 if (c == NUL)
665 c = K_ZERO;
667 #ifdef FEAT_VISUAL
669 * In Select mode, typed text replaces the selection.
671 if (VIsual_active
672 && VIsual_select
673 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
675 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
676 * 'insertmode' is set) fake a "d"elete command, Insert mode will
677 * restart automatically.
678 * Insert the typed character in the typeahead buffer, so that it can
679 * be mapped in Insert mode. Required for ":lmap" to work. */
680 ins_char_typebuf(c);
681 if (restart_edit != 0)
682 c = 'd';
683 else
684 c = 'c';
685 msg_nowait = TRUE; /* don't delay going to insert mode */
687 #endif
689 #ifdef FEAT_CMDL_INFO
690 need_flushbuf = add_to_showcmd(c);
691 #endif
693 getcount:
694 #ifdef FEAT_VISUAL
695 if (!(VIsual_active && VIsual_select))
696 #endif
699 * Handle a count before a command and compute ca.count0.
700 * Note that '0' is a command and not the start of a count, but it's
701 * part of a count after other digits.
703 while ( (c >= '1' && c <= '9')
704 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
706 if (c == K_DEL || c == K_KDEL)
708 ca.count0 /= 10;
709 #ifdef FEAT_CMDL_INFO
710 del_from_showcmd(4); /* delete the digit and ~@% */
711 #endif
713 else
714 ca.count0 = ca.count0 * 10 + (c - '0');
715 if (ca.count0 < 0) /* got too large! */
716 ca.count0 = 999999999L;
717 #ifdef FEAT_EVAL
718 /* Set v:count here, when called from main() and not a stuffed
719 * command, so that v:count can be used in an expression mapping
720 * right after the count. */
721 if (toplevel && stuff_empty())
722 set_vcount(ca.count0, ca.count0 == 0 ? 1 : ca.count0);
723 #endif
724 if (ctrl_w)
726 ++no_mapping;
727 ++allow_keys; /* no mapping for nchar, but keys */
729 ++no_zero_mapping; /* don't map zero here */
730 c = plain_vgetc();
731 #ifdef FEAT_LANGMAP
732 LANGMAP_ADJUST(c, TRUE);
733 #endif
734 --no_zero_mapping;
735 if (ctrl_w)
737 --no_mapping;
738 --allow_keys;
740 #ifdef FEAT_CMDL_INFO
741 need_flushbuf |= add_to_showcmd(c);
742 #endif
746 * If we got CTRL-W there may be a/another count
748 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
750 ctrl_w = TRUE;
751 ca.opcount = ca.count0; /* remember first count */
752 ca.count0 = 0;
753 ++no_mapping;
754 ++allow_keys; /* no mapping for nchar, but keys */
755 c = plain_vgetc(); /* get next character */
756 #ifdef FEAT_LANGMAP
757 LANGMAP_ADJUST(c, TRUE);
758 #endif
759 --no_mapping;
760 --allow_keys;
761 #ifdef FEAT_CMDL_INFO
762 need_flushbuf |= add_to_showcmd(c);
763 #endif
764 goto getcount; /* jump back */
768 #ifdef FEAT_AUTOCMD
769 if (c == K_CURSORHOLD)
771 /* Save the count values so that ca.opcount and ca.count0 are exactly
772 * the same when coming back here after handling K_CURSORHOLD. */
773 oap->prev_opcount = ca.opcount;
774 oap->prev_count0 = ca.count0;
776 else
777 #endif
778 if (ca.opcount != 0)
781 * If we're in the middle of an operator (including after entering a
782 * yank buffer with '"') AND we had a count before the operator, then
783 * that count overrides the current value of ca.count0.
784 * What this means effectively, is that commands like "3dw" get turned
785 * into "d3w" which makes things fall into place pretty neatly.
786 * If you give a count before AND after the operator, they are
787 * multiplied.
789 if (ca.count0)
790 ca.count0 *= ca.opcount;
791 else
792 ca.count0 = ca.opcount;
796 * Always remember the count. It will be set to zero (on the next call,
797 * above) when there is no pending operator.
798 * When called from main(), save the count for use by the "count" built-in
799 * variable.
801 ca.opcount = ca.count0;
802 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
804 #ifdef FEAT_EVAL
806 * Only set v:count when called from main() and not a stuffed command.
808 if (toplevel && stuff_empty())
809 set_vcount(ca.count0, ca.count1);
810 #endif
813 * Find the command character in the table of commands.
814 * For CTRL-W we already got nchar when looking for a count.
816 if (ctrl_w)
818 ca.nchar = c;
819 ca.cmdchar = Ctrl_W;
821 else
822 ca.cmdchar = c;
823 idx = find_command(ca.cmdchar);
824 if (idx < 0)
826 /* Not a known command: beep. */
827 clearopbeep(oap);
828 goto normal_end;
831 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
833 /* This command is not allowed while editing a ccmdline: beep. */
834 clearopbeep(oap);
835 text_locked_msg();
836 goto normal_end;
838 #ifdef FEAT_AUTOCMD
839 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
840 goto normal_end;
841 #endif
843 #ifdef FEAT_VISUAL
845 * In Visual/Select mode, a few keys are handled in a special way.
847 if (VIsual_active)
849 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
850 if (km_stopsel
851 && (nv_cmds[idx].cmd_flags & NV_STS)
852 && !(mod_mask & MOD_MASK_SHIFT))
854 end_visual_mode();
855 redraw_curbuf_later(INVERTED);
858 /* Keys that work different when 'keymodel' contains "startsel" */
859 if (km_startsel)
861 if (nv_cmds[idx].cmd_flags & NV_SS)
863 unshift_special(&ca);
864 idx = find_command(ca.cmdchar);
865 if (idx < 0)
867 /* Just in case */
868 clearopbeep(oap);
869 goto normal_end;
872 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
873 && (mod_mask & MOD_MASK_SHIFT))
875 mod_mask &= ~MOD_MASK_SHIFT;
879 #endif
881 #ifdef FEAT_RIGHTLEFT
882 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
883 && (nv_cmds[idx].cmd_flags & NV_RL))
885 /* Invert horizontal movements and operations. Only when typed by the
886 * user directly, not when the result of a mapping or "x" translated
887 * to "dl". */
888 switch (ca.cmdchar)
890 case 'l': ca.cmdchar = 'h'; break;
891 case K_RIGHT: ca.cmdchar = K_LEFT; break;
892 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
893 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
894 case 'h': ca.cmdchar = 'l'; break;
895 case K_LEFT: ca.cmdchar = K_RIGHT; break;
896 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
897 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
898 case '>': ca.cmdchar = '<'; break;
899 case '<': ca.cmdchar = '>'; break;
901 idx = find_command(ca.cmdchar);
903 #endif
906 * Get an additional character if we need one.
908 if ((nv_cmds[idx].cmd_flags & NV_NCH)
909 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
910 && oap->op_type == OP_NOP)
911 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
912 || (ca.cmdchar == 'q'
913 && oap->op_type == OP_NOP
914 && !Recording
915 && !Exec_reg)
916 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
917 && (oap->op_type != OP_NOP
918 #ifdef FEAT_VISUAL
919 || VIsual_active
920 #endif
921 ))))
923 int *cp;
924 int repl = FALSE; /* get character for replace mode */
925 int lit = FALSE; /* get extra character literally */
926 int langmap_active = FALSE; /* using :lmap mappings */
927 int lang; /* getting a text character */
928 #ifdef USE_IM_CONTROL
929 int save_smd; /* saved value of p_smd */
930 #endif
932 ++no_mapping;
933 ++allow_keys; /* no mapping for nchar, but allow key codes */
934 #ifdef FEAT_AUTOCMD
935 /* Don't generate a CursorHold event here, most commands can't handle
936 * it, e.g., nv_replace(), nv_csearch(). */
937 did_cursorhold = TRUE;
938 #endif
939 if (ca.cmdchar == 'g')
942 * For 'g' get the next character now, so that we can check for
943 * "gr", "g'" and "g`".
945 ca.nchar = plain_vgetc();
946 #ifdef FEAT_LANGMAP
947 LANGMAP_ADJUST(ca.nchar, TRUE);
948 #endif
949 #ifdef FEAT_CMDL_INFO
950 need_flushbuf |= add_to_showcmd(ca.nchar);
951 #endif
952 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
953 || ca.nchar == Ctrl_BSL)
955 cp = &ca.extra_char; /* need to get a third character */
956 if (ca.nchar != 'r')
957 lit = TRUE; /* get it literally */
958 else
959 repl = TRUE; /* get it in replace mode */
961 else
962 cp = NULL; /* no third character needed */
964 else
966 if (ca.cmdchar == 'r') /* get it in replace mode */
967 repl = TRUE;
968 cp = &ca.nchar;
970 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
973 * Get a second or third character.
975 if (cp != NULL)
977 #ifdef CURSOR_SHAPE
978 if (repl)
980 State = REPLACE; /* pretend Replace mode */
981 ui_cursor_shape(); /* show different cursor shape */
983 #endif
984 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
986 /* Allow mappings defined with ":lmap". */
987 --no_mapping;
988 --allow_keys;
989 if (repl)
990 State = LREPLACE;
991 else
992 State = LANGMAP;
993 langmap_active = TRUE;
995 #ifdef USE_IM_CONTROL
996 save_smd = p_smd;
997 p_smd = FALSE; /* Don't let the IM code show the mode here */
998 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
999 im_set_active(TRUE);
1000 #endif
1002 *cp = plain_vgetc();
1004 if (langmap_active)
1006 /* Undo the decrement done above */
1007 ++no_mapping;
1008 ++allow_keys;
1009 State = NORMAL_BUSY;
1011 #ifdef USE_IM_CONTROL
1012 if (lang)
1014 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
1015 im_save_status(&curbuf->b_p_iminsert);
1016 im_set_active(FALSE);
1018 p_smd = save_smd;
1019 #endif
1020 #ifdef CURSOR_SHAPE
1021 State = NORMAL_BUSY;
1022 #endif
1023 #ifdef FEAT_CMDL_INFO
1024 need_flushbuf |= add_to_showcmd(*cp);
1025 #endif
1027 if (!lit)
1029 #ifdef FEAT_DIGRAPHS
1030 /* Typing CTRL-K gets a digraph. */
1031 if (*cp == Ctrl_K
1032 && ((nv_cmds[idx].cmd_flags & NV_LANG)
1033 || cp == &ca.extra_char)
1034 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1036 c = get_digraph(FALSE);
1037 if (c > 0)
1039 *cp = c;
1040 # ifdef FEAT_CMDL_INFO
1041 /* Guessing how to update showcmd here... */
1042 del_from_showcmd(3);
1043 need_flushbuf |= add_to_showcmd(*cp);
1044 # endif
1047 #endif
1049 #ifdef FEAT_LANGMAP
1050 /* adjust chars > 127, except after "tTfFr" commands */
1051 LANGMAP_ADJUST(*cp, !lang);
1052 #endif
1053 #ifdef FEAT_RIGHTLEFT
1054 /* adjust Hebrew mapped char */
1055 if (p_hkmap && lang && KeyTyped)
1056 *cp = hkmap(*cp);
1057 # ifdef FEAT_FKMAP
1058 /* adjust Farsi mapped char */
1059 if (p_fkmap && lang && KeyTyped)
1060 *cp = fkmap(*cp);
1061 # endif
1062 #endif
1066 * When the next character is CTRL-\ a following CTRL-N means the
1067 * command is aborted and we go to Normal mode.
1069 if (cp == &ca.extra_char
1070 && ca.nchar == Ctrl_BSL
1071 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1073 ca.cmdchar = Ctrl_BSL;
1074 ca.nchar = ca.extra_char;
1075 idx = find_command(ca.cmdchar);
1077 else if (*cp == Ctrl_BSL)
1079 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1081 /* There is a busy wait here when typing "f<C-\>" and then
1082 * something different from CTRL-N. Can't be avoided. */
1083 while ((c = vpeekc()) <= 0 && towait > 0L)
1085 do_sleep(towait > 50L ? 50L : towait);
1086 towait -= 50L;
1088 if (c > 0)
1090 c = plain_vgetc();
1091 if (c != Ctrl_N && c != Ctrl_G)
1092 vungetc(c);
1093 else
1095 ca.cmdchar = Ctrl_BSL;
1096 ca.nchar = c;
1097 idx = find_command(ca.cmdchar);
1102 #ifdef FEAT_MBYTE
1103 /* When getting a text character and the next character is a
1104 * multi-byte character, it could be a composing character.
1105 * However, don't wait for it to arrive. */
1106 while (enc_utf8 && lang && (c = vpeekc()) > 0
1107 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1109 c = plain_vgetc();
1110 if (!utf_iscomposing(c))
1112 vungetc(c); /* it wasn't, put it back */
1113 break;
1115 else if (ca.ncharC1 == 0)
1116 ca.ncharC1 = c;
1117 else
1118 ca.ncharC2 = c;
1120 #endif
1122 --no_mapping;
1123 --allow_keys;
1126 #ifdef FEAT_CMDL_INFO
1128 * Flush the showcmd characters onto the screen so we can see them while
1129 * the command is being executed. Only do this when the shown command was
1130 * actually displayed, otherwise this will slow down a lot when executing
1131 * mappings.
1133 if (need_flushbuf)
1134 out_flush();
1135 #endif
1136 #ifdef FEAT_AUTOCMD
1137 if (ca.cmdchar != K_IGNORE)
1138 did_cursorhold = FALSE;
1139 #endif
1141 State = NORMAL;
1143 if (ca.nchar == ESC)
1145 clearop(oap);
1146 if (restart_edit == 0 && goto_im())
1147 restart_edit = 'a';
1148 goto normal_end;
1151 if (ca.cmdchar != K_IGNORE)
1153 msg_didout = FALSE; /* don't scroll screen up for normal command */
1154 msg_col = 0;
1157 #ifdef FEAT_VISUAL
1158 old_pos = curwin->w_cursor; /* remember where cursor was */
1160 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1161 * mode. */
1162 if (!VIsual_active && km_startsel)
1164 if (nv_cmds[idx].cmd_flags & NV_SS)
1166 start_selection();
1167 unshift_special(&ca);
1168 idx = find_command(ca.cmdchar);
1170 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1171 && (mod_mask & MOD_MASK_SHIFT))
1173 start_selection();
1174 mod_mask &= ~MOD_MASK_SHIFT;
1177 #endif
1180 * Execute the command!
1181 * Call the command function found in the commands table.
1183 ca.arg = nv_cmds[idx].cmd_arg;
1184 (nv_cmds[idx].cmd_func)(&ca);
1187 * If we didn't start or finish an operator, reset oap->regname, unless we
1188 * need it later.
1190 if (!finish_op
1191 && !oap->op_type
1192 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1194 clearop(oap);
1195 #ifdef FEAT_EVAL
1196 set_reg_var('"');
1197 #endif
1200 #ifdef FEAT_VISUAL
1201 /* Get the length of mapped chars again after typing a count, second
1202 * character or "z333<cr>". */
1203 if (old_mapped_len > 0)
1204 old_mapped_len = typebuf_maplen();
1205 #endif
1208 * If an operation is pending, handle it...
1210 do_pending_operator(&ca, old_col, FALSE);
1213 * Wait for a moment when a message is displayed that will be overwritten
1214 * by the mode message.
1215 * In Visual mode and with "^O" in Insert mode, a short message will be
1216 * overwritten by the mode message. Wait a bit, until a key is hit.
1217 * In Visual mode, it's more important to keep the Visual area updated
1218 * than keeping a message (e.g. from a /pat search).
1219 * Only do this if the command was typed, not from a mapping.
1220 * Don't wait when emsg_silent is non-zero.
1221 * Also wait a bit after an error message, e.g. for "^O:".
1222 * Don't redraw the screen, it would remove the message.
1224 if ( ((p_smd
1225 && msg_silent == 0
1226 && (restart_edit != 0
1227 #ifdef FEAT_VISUAL
1228 || (VIsual_active
1229 && old_pos.lnum == curwin->w_cursor.lnum
1230 && old_pos.col == curwin->w_cursor.col)
1231 #endif
1233 && (clear_cmdline
1234 || redraw_cmdline)
1235 && (msg_didout || (msg_didany && msg_scroll))
1236 && !msg_nowait
1237 && KeyTyped)
1238 || (restart_edit != 0
1239 #ifdef FEAT_VISUAL
1240 && !VIsual_active
1241 #endif
1242 && (msg_scroll
1243 || emsg_on_display)))
1244 && oap->regname == 0
1245 && !(ca.retval & CA_COMMAND_BUSY)
1246 && stuff_empty()
1247 && typebuf_typed()
1248 && emsg_silent == 0
1249 && !did_wait_return
1250 && oap->op_type == OP_NOP)
1252 int save_State = State;
1254 /* Draw the cursor with the right shape here */
1255 if (restart_edit != 0)
1256 State = INSERT;
1258 /* If need to redraw, and there is a "keep_msg", redraw before the
1259 * delay */
1260 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1262 char_u *kmsg;
1264 kmsg = keep_msg;
1265 keep_msg = NULL;
1266 /* showmode() will clear keep_msg, but we want to use it anyway */
1267 update_screen(0);
1268 /* now reset it, otherwise it's put in the history again */
1269 keep_msg = kmsg;
1270 msg_attr(kmsg, keep_msg_attr);
1271 vim_free(kmsg);
1273 setcursor();
1274 cursor_on();
1275 out_flush();
1276 if (msg_scroll || emsg_on_display)
1277 ui_delay(1000L, TRUE); /* wait at least one second */
1278 ui_delay(3000L, FALSE); /* wait up to three seconds */
1279 State = save_State;
1281 msg_scroll = FALSE;
1282 emsg_on_display = FALSE;
1286 * Finish up after executing a Normal mode command.
1288 normal_end:
1290 msg_nowait = FALSE;
1292 /* Reset finish_op, in case it was set */
1293 #ifdef CURSOR_SHAPE
1294 c = finish_op;
1295 #endif
1296 finish_op = FALSE;
1297 #ifdef CURSOR_SHAPE
1298 /* Redraw the cursor with another shape, if we were in Operator-pending
1299 * mode or did a replace command. */
1300 if (c || ca.cmdchar == 'r')
1302 ui_cursor_shape(); /* may show different cursor shape */
1303 # ifdef FEAT_MOUSESHAPE
1304 update_mouseshape(-1);
1305 # endif
1307 #endif
1309 #ifdef FEAT_CMDL_INFO
1310 if (oap->op_type == OP_NOP && oap->regname == 0
1311 # ifdef FEAT_AUTOCMD
1312 && ca.cmdchar != K_CURSORHOLD
1313 # endif
1315 clear_showcmd();
1316 #endif
1318 checkpcmark(); /* check if we moved since setting pcmark */
1319 vim_free(ca.searchbuf);
1321 #ifdef FEAT_MBYTE
1322 if (has_mbyte)
1323 mb_adjust_cursor();
1324 #endif
1326 #ifdef FEAT_SCROLLBIND
1327 if (curwin->w_p_scb && toplevel)
1329 validate_cursor(); /* may need to update w_leftcol */
1330 do_check_scrollbind(TRUE);
1332 #endif
1335 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1336 * if still inside a mapping that started in Visual mode).
1337 * May switch from Visual to Select mode after CTRL-O command.
1339 if ( oap->op_type == OP_NOP
1340 #ifdef FEAT_VISUAL
1341 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1342 || restart_VIsual_select == 1)
1343 #else
1344 && restart_edit != 0
1345 #endif
1346 && !(ca.retval & CA_COMMAND_BUSY)
1347 && stuff_empty()
1348 && oap->regname == 0)
1350 #ifdef FEAT_VISUAL
1351 if (restart_VIsual_select == 1)
1353 VIsual_select = TRUE;
1354 showmode();
1355 restart_VIsual_select = 0;
1357 #endif
1358 if (restart_edit != 0
1359 #ifdef FEAT_VISUAL
1360 && !VIsual_active && old_mapped_len == 0
1361 #endif
1363 (void)edit(restart_edit, FALSE, 1L);
1366 #ifdef FEAT_VISUAL
1367 if (restart_VIsual_select == 2)
1368 restart_VIsual_select = 1;
1369 #endif
1371 /* Save count before an operator for next time. */
1372 opcount = ca.opcount;
1376 * Handle an operator after visual mode or when the movement is finished
1378 void
1379 do_pending_operator(cap, old_col, gui_yank)
1380 cmdarg_T *cap;
1381 int old_col;
1382 int gui_yank;
1384 oparg_T *oap = cap->oap;
1385 pos_T old_cursor;
1386 int empty_region_error;
1387 int restart_edit_save;
1389 #ifdef FEAT_VISUAL
1390 /* The visual area is remembered for redo */
1391 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1392 static linenr_T redo_VIsual_line_count; /* number of lines */
1393 static colnr_T redo_VIsual_col; /* number of cols or end column */
1394 static long redo_VIsual_count; /* count for Visual operator */
1395 # ifdef FEAT_VIRTUALEDIT
1396 int include_line_break = FALSE;
1397 # endif
1398 #endif
1400 #if defined(FEAT_CLIPBOARD)
1402 * Yank the visual area into the GUI selection register before we operate
1403 * on it and lose it forever.
1404 * Don't do it if a specific register was specified, so that ""x"*P works.
1405 * This could call do_pending_operator() recursively, but that's OK
1406 * because gui_yank will be TRUE for the nested call.
1408 if (clip_star.available
1409 && oap->op_type != OP_NOP
1410 && !gui_yank
1411 # ifdef FEAT_VISUAL
1412 && VIsual_active
1413 && !redo_VIsual_busy
1414 # endif
1415 && oap->regname == 0)
1416 clip_auto_select();
1417 #endif
1418 old_cursor = curwin->w_cursor;
1421 * If an operation is pending, handle it...
1423 if ((finish_op
1424 #ifdef FEAT_VISUAL
1425 || VIsual_active
1426 #endif
1427 ) && oap->op_type != OP_NOP)
1429 #ifdef FEAT_VISUAL
1430 oap->is_VIsual = VIsual_active;
1431 if (oap->motion_force == 'V')
1432 oap->motion_type = MLINE;
1433 else if (oap->motion_force == 'v')
1435 /* If the motion was linewise, "inclusive" will not have been set.
1436 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1437 if (oap->motion_type == MLINE)
1438 oap->inclusive = FALSE;
1439 /* If the motion already was characterwise, toggle "inclusive" */
1440 else if (oap->motion_type == MCHAR)
1441 oap->inclusive = !oap->inclusive;
1442 oap->motion_type = MCHAR;
1444 else if (oap->motion_force == Ctrl_V)
1446 /* Change line- or characterwise motion into Visual block mode. */
1447 VIsual_active = TRUE;
1448 VIsual = oap->start;
1449 VIsual_mode = Ctrl_V;
1450 VIsual_select = FALSE;
1451 VIsual_reselect = FALSE;
1453 #endif
1455 /* only redo yank when 'y' flag is in 'cpoptions' */
1456 /* never redo "zf" (define fold) */
1457 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1458 #ifdef FEAT_VISUAL
1459 && (!VIsual_active || oap->motion_force)
1460 #endif
1461 && cap->cmdchar != 'D'
1462 #ifdef FEAT_FOLDING
1463 && oap->op_type != OP_FOLD
1464 && oap->op_type != OP_FOLDOPEN
1465 && oap->op_type != OP_FOLDOPENREC
1466 && oap->op_type != OP_FOLDCLOSE
1467 && oap->op_type != OP_FOLDCLOSEREC
1468 && oap->op_type != OP_FOLDDEL
1469 && oap->op_type != OP_FOLDDELREC
1470 #endif
1473 prep_redo(oap->regname, cap->count0,
1474 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1475 oap->motion_force, cap->cmdchar, cap->nchar);
1476 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1479 * If 'cpoptions' does not contain 'r', insert the search
1480 * pattern to really repeat the same command.
1482 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
1483 AppendToRedobuffLit(cap->searchbuf, -1);
1484 AppendToRedobuff(NL_STR);
1486 else if (cap->cmdchar == ':')
1488 /* do_cmdline() has stored the first typed line in
1489 * "repeat_cmdline". When several lines are typed repeating
1490 * won't be possible. */
1491 if (repeat_cmdline == NULL)
1492 ResetRedobuff();
1493 else
1495 AppendToRedobuffLit(repeat_cmdline, -1);
1496 AppendToRedobuff(NL_STR);
1497 vim_free(repeat_cmdline);
1498 repeat_cmdline = NULL;
1503 #ifdef FEAT_VISUAL
1504 if (redo_VIsual_busy)
1506 oap->start = curwin->w_cursor;
1507 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1508 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1509 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1510 VIsual_mode = redo_VIsual_mode;
1511 if (VIsual_mode == 'v')
1513 if (redo_VIsual_line_count <= 1)
1514 curwin->w_cursor.col += redo_VIsual_col - 1;
1515 else
1516 curwin->w_cursor.col = redo_VIsual_col;
1518 if (redo_VIsual_col == MAXCOL)
1520 curwin->w_curswant = MAXCOL;
1521 coladvance((colnr_T)MAXCOL);
1523 cap->count0 = redo_VIsual_count;
1524 if (redo_VIsual_count != 0)
1525 cap->count1 = redo_VIsual_count;
1526 else
1527 cap->count1 = 1;
1529 else if (VIsual_active)
1531 if (!gui_yank)
1533 /* Save the current VIsual area for '< and '> marks, and "gv" */
1534 curbuf->b_visual.vi_start = VIsual;
1535 curbuf->b_visual.vi_end = curwin->w_cursor;
1536 curbuf->b_visual.vi_mode = VIsual_mode;
1537 curbuf->b_visual.vi_curswant = curwin->w_curswant;
1538 # ifdef FEAT_EVAL
1539 curbuf->b_visual_mode_eval = VIsual_mode;
1540 # endif
1543 /* In Select mode, a linewise selection is operated upon like a
1544 * characterwise selection. */
1545 if (VIsual_select && VIsual_mode == 'V')
1547 if (lt(VIsual, curwin->w_cursor))
1549 VIsual.col = 0;
1550 curwin->w_cursor.col =
1551 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1553 else
1555 curwin->w_cursor.col = 0;
1556 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1558 VIsual_mode = 'v';
1560 /* If 'selection' is "exclusive", backup one character for
1561 * charwise selections. */
1562 else if (VIsual_mode == 'v')
1564 # ifdef FEAT_VIRTUALEDIT
1565 include_line_break =
1566 # endif
1567 unadjust_for_sel();
1570 oap->start = VIsual;
1571 if (VIsual_mode == 'V')
1572 oap->start.col = 0;
1574 #endif /* FEAT_VISUAL */
1577 * Set oap->start to the first position of the operated text, oap->end
1578 * to the end of the operated text. w_cursor is equal to oap->start.
1580 if (lt(oap->start, curwin->w_cursor))
1582 #ifdef FEAT_FOLDING
1583 /* Include folded lines completely. */
1584 if (!VIsual_active)
1586 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1587 oap->start.col = 0;
1588 if (hasFolding(curwin->w_cursor.lnum, NULL,
1589 &curwin->w_cursor.lnum))
1590 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1592 #endif
1593 oap->end = curwin->w_cursor;
1594 curwin->w_cursor = oap->start;
1596 /* w_virtcol may have been updated; if the cursor goes back to its
1597 * previous position w_virtcol becomes invalid and isn't updated
1598 * automatically. */
1599 curwin->w_valid &= ~VALID_VIRTCOL;
1601 else
1603 #ifdef FEAT_FOLDING
1604 /* Include folded lines completely. */
1605 if (!VIsual_active && oap->motion_type == MLINE)
1607 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1608 NULL))
1609 curwin->w_cursor.col = 0;
1610 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1611 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1613 #endif
1614 oap->end = oap->start;
1615 oap->start = curwin->w_cursor;
1618 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1620 #ifdef FEAT_VIRTUALEDIT
1621 /* Set "virtual_op" before resetting VIsual_active. */
1622 virtual_op = virtual_active();
1623 #endif
1625 #ifdef FEAT_VISUAL
1626 if (VIsual_active || redo_VIsual_busy)
1628 if (VIsual_mode == Ctrl_V) /* block mode */
1630 colnr_T start, end;
1632 oap->block_mode = TRUE;
1634 getvvcol(curwin, &(oap->start),
1635 &oap->start_vcol, NULL, &oap->end_vcol);
1636 if (!redo_VIsual_busy)
1638 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1640 if (start < oap->start_vcol)
1641 oap->start_vcol = start;
1642 if (end > oap->end_vcol)
1644 if (*p_sel == 'e' && start >= 1
1645 && start - 1 >= oap->end_vcol)
1646 oap->end_vcol = start - 1;
1647 else
1648 oap->end_vcol = end;
1652 /* if '$' was used, get oap->end_vcol from longest line */
1653 if (curwin->w_curswant == MAXCOL)
1655 curwin->w_cursor.col = MAXCOL;
1656 oap->end_vcol = 0;
1657 for (curwin->w_cursor.lnum = oap->start.lnum;
1658 curwin->w_cursor.lnum <= oap->end.lnum;
1659 ++curwin->w_cursor.lnum)
1661 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1662 if (end > oap->end_vcol)
1663 oap->end_vcol = end;
1666 else if (redo_VIsual_busy)
1667 oap->end_vcol = oap->start_vcol + redo_VIsual_col - 1;
1669 * Correct oap->end.col and oap->start.col to be the
1670 * upper-left and lower-right corner of the block area.
1672 * (Actually, this does convert column positions into character
1673 * positions)
1675 curwin->w_cursor.lnum = oap->end.lnum;
1676 coladvance(oap->end_vcol);
1677 oap->end = curwin->w_cursor;
1679 curwin->w_cursor = oap->start;
1680 coladvance(oap->start_vcol);
1681 oap->start = curwin->w_cursor;
1684 if (!redo_VIsual_busy && !gui_yank)
1687 * Prepare to reselect and redo Visual: this is based on the
1688 * size of the Visual text
1690 resel_VIsual_mode = VIsual_mode;
1691 if (curwin->w_curswant == MAXCOL)
1692 resel_VIsual_col = MAXCOL;
1693 else if (VIsual_mode == Ctrl_V)
1694 resel_VIsual_col = oap->end_vcol - oap->start_vcol + 1;
1695 else if (oap->line_count > 1)
1696 resel_VIsual_col = oap->end.col;
1697 else
1698 resel_VIsual_col = oap->end.col - oap->start.col + 1;
1699 resel_VIsual_line_count = oap->line_count;
1702 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1703 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1704 && oap->op_type != OP_COLON
1705 #ifdef FEAT_FOLDING
1706 && oap->op_type != OP_FOLD
1707 && oap->op_type != OP_FOLDOPEN
1708 && oap->op_type != OP_FOLDOPENREC
1709 && oap->op_type != OP_FOLDCLOSE
1710 && oap->op_type != OP_FOLDCLOSEREC
1711 && oap->op_type != OP_FOLDDEL
1712 && oap->op_type != OP_FOLDDELREC
1713 #endif
1714 && oap->motion_force == NUL
1717 /* Prepare for redoing. Only use the nchar field for "r",
1718 * otherwise it might be the second char of the operator. */
1719 prep_redo(oap->regname, 0L, NUL, 'v',
1720 get_op_char(oap->op_type),
1721 get_extra_op_char(oap->op_type),
1722 oap->op_type == OP_REPLACE ? cap->nchar : NUL);
1723 if (!redo_VIsual_busy)
1725 redo_VIsual_mode = resel_VIsual_mode;
1726 redo_VIsual_col = resel_VIsual_col;
1727 redo_VIsual_line_count = resel_VIsual_line_count;
1728 redo_VIsual_count = cap->count0;
1733 * oap->inclusive defaults to TRUE.
1734 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1735 * FALSE. This makes "d}P" and "v}dP" work the same.
1737 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1738 oap->inclusive = TRUE;
1739 if (VIsual_mode == 'V')
1740 oap->motion_type = MLINE;
1741 else
1743 oap->motion_type = MCHAR;
1744 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1745 # ifdef FEAT_VIRTUALEDIT
1746 && (include_line_break || !virtual_op)
1747 # endif
1750 oap->inclusive = FALSE;
1751 /* Try to include the newline, unless it's an operator
1752 * that works on lines only */
1753 if (*p_sel != 'o'
1754 && !op_on_lines(oap->op_type)
1755 && oap->end.lnum < curbuf->b_ml.ml_line_count)
1757 ++oap->end.lnum;
1758 oap->end.col = 0;
1759 # ifdef FEAT_VIRTUALEDIT
1760 oap->end.coladd = 0;
1761 # endif
1762 ++oap->line_count;
1767 redo_VIsual_busy = FALSE;
1770 * Switch Visual off now, so screen updating does
1771 * not show inverted text when the screen is redrawn.
1772 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1773 * no screen redraw, so it is done here to remove the inverted
1774 * part.
1776 if (!gui_yank)
1778 VIsual_active = FALSE;
1779 # ifdef FEAT_MOUSE
1780 setmouse();
1781 mouse_dragging = 0;
1782 # endif
1783 if (mode_displayed)
1784 clear_cmdline = TRUE; /* unshow visual mode later */
1785 #ifdef FEAT_CMDL_INFO
1786 else
1787 clear_showcmd();
1788 #endif
1789 if ((oap->op_type == OP_YANK
1790 || oap->op_type == OP_COLON
1791 || oap->op_type == OP_FUNCTION
1792 || oap->op_type == OP_FILTER)
1793 && oap->motion_force == NUL)
1794 redraw_curbuf_later(INVERTED);
1797 #endif
1799 #ifdef FEAT_MBYTE
1800 /* Include the trailing byte of a multi-byte char. */
1801 if (has_mbyte && oap->inclusive)
1803 int l;
1805 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
1806 if (l > 1)
1807 oap->end.col += l - 1;
1809 #endif
1810 curwin->w_set_curswant = TRUE;
1813 * oap->empty is set when start and end are the same. The inclusive
1814 * flag affects this too, unless yanking and the end is on a NUL.
1816 oap->empty = (oap->motion_type == MCHAR
1817 && (!oap->inclusive
1818 || (oap->op_type == OP_YANK
1819 && gchar_pos(&oap->end) == NUL))
1820 && equalpos(oap->start, oap->end)
1821 #ifdef FEAT_VIRTUALEDIT
1822 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1823 #endif
1826 * For delete, change and yank, it's an error to operate on an
1827 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1829 empty_region_error = (oap->empty
1830 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1832 #ifdef FEAT_VISUAL
1833 /* Force a redraw when operating on an empty Visual region, when
1834 * 'modifiable is off or creating a fold. */
1835 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1836 # ifdef FEAT_FOLDING
1837 || oap->op_type == OP_FOLD
1838 # endif
1840 redraw_curbuf_later(INVERTED);
1841 #endif
1844 * If the end of an operator is in column one while oap->motion_type
1845 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1846 * character in the previous line. If op_start is on or before the
1847 * first non-blank in the line, the operator becomes linewise
1848 * (strange, but that's the way vi does it).
1850 if ( oap->motion_type == MCHAR
1851 && oap->inclusive == FALSE
1852 && !(cap->retval & CA_NO_ADJ_OP_END)
1853 && oap->end.col == 0
1854 #ifdef FEAT_VISUAL
1855 && (!oap->is_VIsual || *p_sel == 'o')
1856 && !oap->block_mode
1857 #endif
1858 && oap->line_count > 1)
1860 oap->end_adjusted = TRUE; /* remember that we did this */
1861 --oap->line_count;
1862 --oap->end.lnum;
1863 if (inindent(0))
1864 oap->motion_type = MLINE;
1865 else
1867 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1868 if (oap->end.col)
1870 --oap->end.col;
1871 oap->inclusive = TRUE;
1875 else
1876 oap->end_adjusted = FALSE;
1878 switch (oap->op_type)
1880 case OP_LSHIFT:
1881 case OP_RSHIFT:
1882 op_shift(oap, TRUE,
1883 #ifdef FEAT_VISUAL
1884 oap->is_VIsual ? (int)cap->count1 :
1885 #endif
1887 auto_format(FALSE, TRUE);
1888 break;
1890 case OP_JOIN_NS:
1891 case OP_JOIN:
1892 if (oap->line_count < 2)
1893 oap->line_count = 2;
1894 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1895 curbuf->b_ml.ml_line_count)
1896 beep_flush();
1897 else
1899 do_do_join(oap->line_count, oap->op_type == OP_JOIN);
1900 auto_format(FALSE, TRUE);
1902 break;
1904 case OP_DELETE:
1905 #ifdef FEAT_VISUAL
1906 VIsual_reselect = FALSE; /* don't reselect now */
1907 #endif
1908 if (empty_region_error)
1909 vim_beep();
1910 else
1912 (void)op_delete(oap);
1913 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1914 u_save_cursor(); /* cursor line wasn't saved yet */
1915 auto_format(FALSE, TRUE);
1917 break;
1919 case OP_YANK:
1920 if (empty_region_error)
1922 if (!gui_yank)
1923 vim_beep();
1925 else
1926 (void)op_yank(oap, FALSE, !gui_yank);
1927 check_cursor_col();
1928 break;
1930 case OP_CHANGE:
1931 #ifdef FEAT_VISUAL
1932 VIsual_reselect = FALSE; /* don't reselect now */
1933 #endif
1934 if (empty_region_error)
1935 vim_beep();
1936 else
1938 /* This is a new edit command, not a restart. Need to
1939 * remember it to make 'insertmode' work with mappings for
1940 * Visual mode. But do this only once and not when typed and
1941 * 'insertmode' isn't set. */
1942 if (p_im || !KeyTyped)
1943 restart_edit_save = restart_edit;
1944 else
1945 restart_edit_save = 0;
1946 restart_edit = 0;
1947 /* Reset finish_op now, don't want it set inside edit(). */
1948 finish_op = FALSE;
1949 if (op_change(oap)) /* will call edit() */
1950 cap->retval |= CA_COMMAND_BUSY;
1951 if (restart_edit == 0)
1952 restart_edit = restart_edit_save;
1954 break;
1956 case OP_FILTER:
1957 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1958 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1959 else
1960 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
1962 case OP_INDENT:
1963 case OP_COLON:
1965 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1967 * If 'equalprg' is empty, do the indenting internally.
1969 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1971 # ifdef FEAT_LISP
1972 if (curbuf->b_p_lisp)
1974 op_reindent(oap, get_lisp_indent);
1975 break;
1977 # endif
1978 # ifdef FEAT_CINDENT
1979 op_reindent(oap,
1980 # ifdef FEAT_EVAL
1981 *curbuf->b_p_inde != NUL ? get_expr_indent :
1982 # endif
1983 get_c_indent);
1984 break;
1985 # endif
1987 #endif
1989 op_colon(oap);
1990 break;
1992 case OP_TILDE:
1993 case OP_UPPER:
1994 case OP_LOWER:
1995 case OP_ROT13:
1996 if (empty_region_error)
1997 vim_beep();
1998 else
1999 op_tilde(oap);
2000 check_cursor_col();
2001 break;
2003 case OP_FORMAT:
2004 #if defined(FEAT_EVAL)
2005 if (*curbuf->b_p_fex != NUL)
2006 op_formatexpr(oap); /* use expression */
2007 else
2008 #endif
2009 if (*p_fp != NUL)
2010 op_colon(oap); /* use external command */
2011 else
2012 op_format(oap, FALSE); /* use internal function */
2013 break;
2015 case OP_FORMAT2:
2016 op_format(oap, TRUE); /* use internal function */
2017 break;
2019 case OP_FUNCTION:
2020 op_function(oap); /* call 'operatorfunc' */
2021 break;
2023 case OP_INSERT:
2024 case OP_APPEND:
2025 #ifdef FEAT_VISUAL
2026 VIsual_reselect = FALSE; /* don't reselect now */
2027 #endif
2028 #ifdef FEAT_VISUALEXTRA
2029 if (empty_region_error)
2030 vim_beep();
2031 else
2033 /* This is a new edit command, not a restart. Need to
2034 * remember it to make 'insertmode' work with mappings for
2035 * Visual mode. But do this only once. */
2036 restart_edit_save = restart_edit;
2037 restart_edit = 0;
2039 op_insert(oap, cap->count1);
2041 /* TODO: when inserting in several lines, should format all
2042 * the lines. */
2043 auto_format(FALSE, TRUE);
2045 if (restart_edit == 0)
2046 restart_edit = restart_edit_save;
2048 #else
2049 vim_beep();
2050 #endif
2051 break;
2053 case OP_REPLACE:
2054 #ifdef FEAT_VISUAL
2055 VIsual_reselect = FALSE; /* don't reselect now */
2056 #endif
2057 #ifdef FEAT_VISUALEXTRA
2058 if (empty_region_error)
2059 #endif
2060 vim_beep();
2061 #ifdef FEAT_VISUALEXTRA
2062 else
2063 op_replace(oap, cap->nchar);
2064 #endif
2065 break;
2067 #ifdef FEAT_FOLDING
2068 case OP_FOLD:
2069 VIsual_reselect = FALSE; /* don't reselect now */
2070 foldCreate(oap->start.lnum, oap->end.lnum);
2071 break;
2073 case OP_FOLDOPEN:
2074 case OP_FOLDOPENREC:
2075 case OP_FOLDCLOSE:
2076 case OP_FOLDCLOSEREC:
2077 VIsual_reselect = FALSE; /* don't reselect now */
2078 opFoldRange(oap->start.lnum, oap->end.lnum,
2079 oap->op_type == OP_FOLDOPEN
2080 || oap->op_type == OP_FOLDOPENREC,
2081 oap->op_type == OP_FOLDOPENREC
2082 || oap->op_type == OP_FOLDCLOSEREC,
2083 oap->is_VIsual);
2084 break;
2086 case OP_FOLDDEL:
2087 case OP_FOLDDELREC:
2088 VIsual_reselect = FALSE; /* don't reselect now */
2089 deleteFold(oap->start.lnum, oap->end.lnum,
2090 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2091 break;
2092 #endif
2093 default:
2094 clearopbeep(oap);
2096 #ifdef FEAT_VIRTUALEDIT
2097 virtual_op = MAYBE;
2098 #endif
2099 if (!gui_yank)
2102 * if 'sol' not set, go back to old column for some commands
2104 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2105 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2106 || oap->op_type == OP_DELETE))
2107 coladvance(curwin->w_curswant = old_col);
2109 else
2111 curwin->w_cursor = old_cursor;
2113 #ifdef FEAT_VISUAL
2114 oap->block_mode = FALSE;
2115 #endif
2116 clearop(oap);
2121 * Handle indent and format operators and visual mode ":".
2123 static void
2124 op_colon(oap)
2125 oparg_T *oap;
2127 stuffcharReadbuff(':');
2128 #ifdef FEAT_VISUAL
2129 if (oap->is_VIsual)
2130 stuffReadbuff((char_u *)"'<,'>");
2131 else
2132 #endif
2135 * Make the range look nice, so it can be repeated.
2137 if (oap->start.lnum == curwin->w_cursor.lnum)
2138 stuffcharReadbuff('.');
2139 else
2140 stuffnumReadbuff((long)oap->start.lnum);
2141 if (oap->end.lnum != oap->start.lnum)
2143 stuffcharReadbuff(',');
2144 if (oap->end.lnum == curwin->w_cursor.lnum)
2145 stuffcharReadbuff('.');
2146 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2147 stuffcharReadbuff('$');
2148 else if (oap->start.lnum == curwin->w_cursor.lnum)
2150 stuffReadbuff((char_u *)".+");
2151 stuffnumReadbuff((long)oap->line_count - 1);
2153 else
2154 stuffnumReadbuff((long)oap->end.lnum);
2157 if (oap->op_type != OP_COLON)
2158 stuffReadbuff((char_u *)"!");
2159 if (oap->op_type == OP_INDENT)
2161 #ifndef FEAT_CINDENT
2162 if (*get_equalprg() == NUL)
2163 stuffReadbuff((char_u *)"indent");
2164 else
2165 #endif
2166 stuffReadbuff(get_equalprg());
2167 stuffReadbuff((char_u *)"\n");
2169 else if (oap->op_type == OP_FORMAT)
2171 if (*p_fp == NUL)
2172 stuffReadbuff((char_u *)"fmt");
2173 else
2174 stuffReadbuff(p_fp);
2175 stuffReadbuff((char_u *)"\n']");
2179 * do_cmdline() does the rest
2184 * Handle the "g@" operator: call 'operatorfunc'.
2186 /*ARGSUSED*/
2187 static void
2188 op_function(oap)
2189 oparg_T *oap;
2191 #ifdef FEAT_EVAL
2192 char_u *(argv[1]);
2194 if (*p_opfunc == NUL)
2195 EMSG(_("E774: 'operatorfunc' is empty"));
2196 else
2198 /* Set '[ and '] marks to text to be operated on. */
2199 curbuf->b_op_start = oap->start;
2200 curbuf->b_op_end = oap->end;
2201 if (oap->motion_type != MLINE && !oap->inclusive)
2202 /* Exclude the end position. */
2203 decl(&curbuf->b_op_end);
2205 if (oap->block_mode)
2206 argv[0] = (char_u *)"block";
2207 else if (oap->motion_type == MLINE)
2208 argv[0] = (char_u *)"line";
2209 else
2210 argv[0] = (char_u *)"char";
2211 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
2213 #else
2214 EMSG(_("E775: Eval feature not available"));
2215 #endif
2218 #if defined(FEAT_MOUSE) || defined(PROTO)
2220 * Do the appropriate action for the current mouse click in the current mode.
2221 * Not used for Command-line mode.
2223 * Normal Mode:
2224 * event modi- position visual change action
2225 * fier cursor window
2226 * left press - yes end yes
2227 * left press C yes end yes "^]" (2)
2228 * left press S yes end yes "*" (2)
2229 * left drag - yes start if moved no
2230 * left relse - yes start if moved no
2231 * middle press - yes if not active no put register
2232 * middle press - yes if active no yank and put
2233 * right press - yes start or extend yes
2234 * right press S yes no change yes "#" (2)
2235 * right drag - yes extend no
2236 * right relse - yes extend no
2238 * Insert or Replace Mode:
2239 * event modi- position visual change action
2240 * fier cursor window
2241 * left press - yes (cannot be active) yes
2242 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2243 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2244 * left drag - yes start or extend (1) no CTRL-O (1)
2245 * left relse - yes start or extend (1) no CTRL-O (1)
2246 * middle press - no (cannot be active) no put register
2247 * right press - yes start or extend yes CTRL-O
2248 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2250 * (1) only if mouse pointer moved since press
2251 * (2) only if click is in same buffer
2253 * Return TRUE if start_arrow() should be called for edit mode.
2256 do_mouse(oap, c, dir, count, fixindent)
2257 oparg_T *oap; /* operator argument, can be NULL */
2258 int c; /* K_LEFTMOUSE, etc */
2259 int dir; /* Direction to 'put' if necessary */
2260 long count;
2261 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2263 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2264 static int got_click = FALSE; /* got a click some time back */
2266 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2267 int is_click; /* If FALSE it's a drag or release event */
2268 int is_drag; /* If TRUE it's a drag event */
2269 int jump_flags = 0; /* flags for jump_to_mouse() */
2270 pos_T start_visual;
2271 int moved; /* Has cursor moved? */
2272 int in_status_line; /* mouse in status line */
2273 #ifdef FEAT_VERTSPLIT
2274 int in_sep_line; /* mouse in vertical separator line */
2275 #endif
2276 int c1, c2;
2277 #if defined(FEAT_FOLDING)
2278 pos_T save_cursor;
2279 #endif
2280 win_T *old_curwin = curwin;
2281 #ifdef FEAT_VISUAL
2282 static pos_T orig_cursor;
2283 colnr_T leftcol, rightcol;
2284 pos_T end_visual;
2285 int diff;
2286 int old_active = VIsual_active;
2287 int old_mode = VIsual_mode;
2288 #endif
2289 int regname;
2291 #if defined(FEAT_FOLDING)
2292 save_cursor = curwin->w_cursor;
2293 #endif
2296 * When GUI is active, always recognize mouse events, otherwise:
2297 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2298 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2299 * - For command line and insert mode 'mouse' is checked before calling
2300 * do_mouse().
2302 if (do_always)
2303 do_always = FALSE;
2304 else
2305 #ifdef FEAT_GUI
2306 if (!gui.in_use)
2307 #endif
2309 #ifdef FEAT_VISUAL
2310 if (VIsual_active)
2312 if (!mouse_has(MOUSE_VISUAL))
2313 return FALSE;
2315 else
2316 #endif
2317 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2318 return FALSE;
2321 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2323 #ifdef FEAT_MOUSESHAPE
2324 /* May have stopped dragging the status or separator line. The pointer is
2325 * most likely still on the status or separator line. */
2326 if (!is_drag && drag_status_line)
2328 drag_status_line = FALSE;
2329 update_mouseshape(SHAPE_IDX_STATUS);
2331 # ifdef FEAT_VERTSPLIT
2332 if (!is_drag && drag_sep_line)
2334 drag_sep_line = FALSE;
2335 update_mouseshape(SHAPE_IDX_VSEP);
2337 # endif
2338 #endif
2341 * Ignore drag and release events if we didn't get a click.
2343 if (is_click)
2344 got_click = TRUE;
2345 else
2347 if (!got_click) /* didn't get click, ignore */
2348 return FALSE;
2349 if (!is_drag) /* release, reset got_click */
2350 got_click = FALSE;
2353 #ifndef FEAT_VISUAL
2355 * ALT is only used for starging/extending Visual mode.
2357 if ((mod_mask & MOD_MASK_ALT))
2358 return FALSE;
2359 #endif
2362 * CTRL right mouse button does CTRL-T
2364 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2366 if (State & INSERT)
2367 stuffcharReadbuff(Ctrl_O);
2368 if (count > 1)
2369 stuffnumReadbuff(count);
2370 stuffcharReadbuff(Ctrl_T);
2371 got_click = FALSE; /* ignore drag&release now */
2372 return FALSE;
2376 * CTRL only works with left mouse button
2378 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2379 return FALSE;
2382 * When a modifier is down, ignore drag and release events, as well as
2383 * multiple clicks and the middle mouse button.
2384 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2386 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2387 | MOD_MASK_META))
2388 && (!is_click
2389 || (mod_mask & MOD_MASK_MULTI_CLICK)
2390 || which_button == MOUSE_MIDDLE)
2391 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
2392 && mouse_model_popup()
2393 && which_button == MOUSE_LEFT)
2394 && !((mod_mask & MOD_MASK_ALT)
2395 && !mouse_model_popup()
2396 && which_button == MOUSE_RIGHT)
2398 return FALSE;
2401 * If the button press was used as the movement command for an operator
2402 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2403 * drag/release events.
2405 if (!is_click && which_button == MOUSE_MIDDLE)
2406 return FALSE;
2408 if (oap != NULL)
2409 regname = oap->regname;
2410 else
2411 regname = 0;
2414 * Middle mouse button does a 'put' of the selected text
2416 if (which_button == MOUSE_MIDDLE)
2418 if (State == NORMAL)
2421 * If an operator was pending, we don't know what the user wanted
2422 * to do. Go back to normal mode: Clear the operator and beep().
2424 if (oap != NULL && oap->op_type != OP_NOP)
2426 clearopbeep(oap);
2427 return FALSE;
2430 #ifdef FEAT_VISUAL
2432 * If visual was active, yank the highlighted text and put it
2433 * before the mouse pointer position.
2434 * In Select mode replace the highlighted text with the clipboard.
2436 if (VIsual_active)
2438 if (VIsual_select)
2440 stuffcharReadbuff(Ctrl_G);
2441 stuffReadbuff((char_u *)"\"+p");
2443 else
2445 stuffcharReadbuff('y');
2446 stuffcharReadbuff(K_MIDDLEMOUSE);
2448 do_always = TRUE; /* ignore 'mouse' setting next time */
2449 return FALSE;
2451 #endif
2453 * The rest is below jump_to_mouse()
2457 else if ((State & INSERT) == 0)
2458 return FALSE;
2461 * Middle click in insert mode doesn't move the mouse, just insert the
2462 * contents of a register. '.' register is special, can't insert that
2463 * with do_put().
2464 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2465 * happens for the GUI).
2467 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2469 if (regname == '.')
2470 insert_reg(regname, TRUE);
2471 else
2473 #ifdef FEAT_CLIPBOARD
2474 if (clip_star.available && regname == 0)
2475 regname = '*';
2476 #endif
2477 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2478 insert_reg(regname, TRUE);
2479 else
2481 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2483 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2484 AppendCharToRedobuff(Ctrl_R);
2485 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2486 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2489 return FALSE;
2493 /* When dragging or button-up stay in the same window. */
2494 if (!is_click)
2495 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2497 start_visual.lnum = 0;
2499 #ifdef FEAT_WINDOWS
2500 /* Check for clicking in the tab page line. */
2501 if (mouse_row == 0 && firstwin->w_winrow > 0)
2503 if (is_drag)
2504 return FALSE;
2505 got_click = FALSE; /* ignore mouse-up and drag events */
2507 /* click in a tab selects that tab page */
2508 if (is_click
2509 # ifdef FEAT_CMDWIN
2510 && cmdwin_type == 0
2511 # endif
2512 && mouse_col < Columns)
2514 c1 = TabPageIdxs[mouse_col];
2515 if (c1 >= 0)
2517 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2519 /* double click opens new page */
2520 end_visual_mode();
2521 tabpage_new();
2522 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2524 else
2526 /* Go to specified tab page, or next one if not clicking
2527 * on a label. */
2528 goto_tabpage(c1);
2530 /* It's like clicking on the status line of a window. */
2531 if (curwin != old_curwin)
2532 end_visual_mode();
2535 else if (c1 < 0)
2537 tabpage_T *tp;
2539 /* Close the current or specified tab page. */
2540 if (c1 == -999)
2541 tp = curtab;
2542 else
2543 tp = find_tabpage(-c1);
2544 if (tp == curtab)
2546 if (first_tabpage->tp_next != NULL)
2547 tabpage_close(FALSE);
2549 else if (tp != NULL)
2550 tabpage_close_other(tp, FALSE);
2553 return TRUE;
2555 #endif
2558 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2559 * right button up -> pop-up menu
2560 * shift-left button -> right button
2561 * alt-left button -> alt-right button
2563 if (mouse_model_popup())
2565 if (which_button == MOUSE_RIGHT
2566 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2569 * NOTE: Ignore right button down and drag mouse events.
2570 * Windows only shows the popup menu on the button up event.
2572 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2573 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2574 if (!is_click)
2575 return FALSE;
2576 #endif
2577 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2578 if (is_click || is_drag)
2579 return FALSE;
2580 #endif
2581 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2582 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2583 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2584 if (gui.in_use)
2586 jump_flags = 0;
2587 if (STRCMP(p_mousem, "popup_setpos") == 0)
2589 /* First set the cursor position before showing the popup
2590 * menu. */
2591 #ifdef FEAT_VISUAL
2592 if (VIsual_active)
2594 pos_T m_pos;
2597 * set MOUSE_MAY_STOP_VIS if we are outside the
2598 * selection or the current window (might have false
2599 * negative here)
2601 if (mouse_row < W_WINROW(curwin)
2602 || mouse_row
2603 > (W_WINROW(curwin) + curwin->w_height))
2604 jump_flags = MOUSE_MAY_STOP_VIS;
2605 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2606 jump_flags = MOUSE_MAY_STOP_VIS;
2607 else
2609 if ((lt(curwin->w_cursor, VIsual)
2610 && (lt(m_pos, curwin->w_cursor)
2611 || lt(VIsual, m_pos)))
2612 || (lt(VIsual, curwin->w_cursor)
2613 && (lt(m_pos, VIsual)
2614 || lt(curwin->w_cursor, m_pos))))
2616 jump_flags = MOUSE_MAY_STOP_VIS;
2618 else if (VIsual_mode == Ctrl_V)
2620 getvcols(curwin, &curwin->w_cursor, &VIsual,
2621 &leftcol, &rightcol);
2622 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2623 if (m_pos.col < leftcol || m_pos.col > rightcol)
2624 jump_flags = MOUSE_MAY_STOP_VIS;
2628 else
2629 jump_flags = MOUSE_MAY_STOP_VIS;
2630 #endif
2632 if (jump_flags)
2634 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2635 update_curbuf(
2636 #ifdef FEAT_VISUAL
2637 VIsual_active ? INVERTED :
2638 #endif
2639 VALID);
2640 setcursor();
2641 out_flush(); /* Update before showing popup menu */
2643 # ifdef FEAT_MENU
2644 gui_show_popupmenu();
2645 # endif
2646 return (jump_flags & CURSOR_MOVED) != 0;
2648 else
2649 return FALSE;
2650 #else
2651 return FALSE;
2652 #endif
2654 if (which_button == MOUSE_LEFT
2655 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
2657 which_button = MOUSE_RIGHT;
2658 mod_mask &= ~MOD_MASK_SHIFT;
2662 #ifdef FEAT_VISUAL
2663 if ((State & (NORMAL | INSERT))
2664 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2666 if (which_button == MOUSE_LEFT)
2668 if (is_click)
2670 /* stop Visual mode for a left click in a window, but not when
2671 * on a status line */
2672 if (VIsual_active)
2673 jump_flags |= MOUSE_MAY_STOP_VIS;
2675 else if (mouse_has(MOUSE_VISUAL))
2676 jump_flags |= MOUSE_MAY_VIS;
2678 else if (which_button == MOUSE_RIGHT)
2680 if (is_click && VIsual_active)
2683 * Remember the start and end of visual before moving the
2684 * cursor.
2686 if (lt(curwin->w_cursor, VIsual))
2688 start_visual = curwin->w_cursor;
2689 end_visual = VIsual;
2691 else
2693 start_visual = VIsual;
2694 end_visual = curwin->w_cursor;
2697 jump_flags |= MOUSE_FOCUS;
2698 if (mouse_has(MOUSE_VISUAL))
2699 jump_flags |= MOUSE_MAY_VIS;
2702 #endif
2705 * If an operator is pending, ignore all drags and releases until the
2706 * next mouse click.
2708 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2710 got_click = FALSE;
2711 oap->motion_type = MCHAR;
2714 /* When releasing the button let jump_to_mouse() know. */
2715 if (!is_click && !is_drag)
2716 jump_flags |= MOUSE_RELEASED;
2719 * JUMP!
2721 jump_flags = jump_to_mouse(jump_flags,
2722 oap == NULL ? NULL : &(oap->inclusive), which_button);
2723 moved = (jump_flags & CURSOR_MOVED);
2724 in_status_line = (jump_flags & IN_STATUS_LINE);
2725 #ifdef FEAT_VERTSPLIT
2726 in_sep_line = (jump_flags & IN_SEP_LINE);
2727 #endif
2729 #ifdef FEAT_NETBEANS_INTG
2730 if (usingNetbeans && isNetbeansBuffer(curbuf)
2731 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2733 int key = KEY2TERMCAP1(c);
2735 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2736 || key == (int)KE_RIGHTRELEASE)
2737 netbeans_button_release(which_button);
2739 #endif
2741 /* When jumping to another window, clear a pending operator. That's a bit
2742 * friendlier than beeping and not jumping to that window. */
2743 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2744 clearop(oap);
2746 #ifdef FEAT_FOLDING
2747 if (mod_mask == 0
2748 && !is_drag
2749 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2750 && which_button == MOUSE_LEFT)
2752 /* open or close a fold at this line */
2753 if (jump_flags & MOUSE_FOLD_OPEN)
2754 openFold(curwin->w_cursor.lnum, 1L);
2755 else
2756 closeFold(curwin->w_cursor.lnum, 1L);
2757 /* don't move the cursor if still in the same window */
2758 if (curwin == old_curwin)
2759 curwin->w_cursor = save_cursor;
2761 #endif
2763 #if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2764 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2766 clip_modeless(which_button, is_click, is_drag);
2767 return FALSE;
2769 #endif
2771 #ifdef FEAT_VISUAL
2772 /* Set global flag that we are extending the Visual area with mouse
2773 * dragging; temporarily minimize 'scrolloff'. */
2774 if (VIsual_active && is_drag && p_so)
2776 /* In the very first line, allow scrolling one line */
2777 if (mouse_row == 0)
2778 mouse_dragging = 2;
2779 else
2780 mouse_dragging = 1;
2783 /* When dragging the mouse above the window, scroll down. */
2784 if (is_drag && mouse_row < 0 && !in_status_line)
2786 scroll_redraw(FALSE, 1L);
2787 mouse_row = 0;
2790 if (start_visual.lnum) /* right click in visual mode */
2792 /* When ALT is pressed make Visual mode blockwise. */
2793 if (mod_mask & MOD_MASK_ALT)
2794 VIsual_mode = Ctrl_V;
2797 * In Visual-block mode, divide the area in four, pick up the corner
2798 * that is in the quarter that the cursor is in.
2800 if (VIsual_mode == Ctrl_V)
2802 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2803 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2804 end_visual.col = leftcol;
2805 else
2806 end_visual.col = rightcol;
2807 if (curwin->w_cursor.lnum <
2808 (start_visual.lnum + end_visual.lnum) / 2)
2809 end_visual.lnum = end_visual.lnum;
2810 else
2811 end_visual.lnum = start_visual.lnum;
2813 /* move VIsual to the right column */
2814 start_visual = curwin->w_cursor; /* save the cursor pos */
2815 curwin->w_cursor = end_visual;
2816 coladvance(end_visual.col);
2817 VIsual = curwin->w_cursor;
2818 curwin->w_cursor = start_visual; /* restore the cursor */
2820 else
2823 * If the click is before the start of visual, change the start.
2824 * If the click is after the end of visual, change the end. If
2825 * the click is inside the visual, change the closest side.
2827 if (lt(curwin->w_cursor, start_visual))
2828 VIsual = end_visual;
2829 else if (lt(end_visual, curwin->w_cursor))
2830 VIsual = start_visual;
2831 else
2833 /* In the same line, compare column number */
2834 if (end_visual.lnum == start_visual.lnum)
2836 if (curwin->w_cursor.col - start_visual.col >
2837 end_visual.col - curwin->w_cursor.col)
2838 VIsual = start_visual;
2839 else
2840 VIsual = end_visual;
2843 /* In different lines, compare line number */
2844 else
2846 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2847 (end_visual.lnum - curwin->w_cursor.lnum);
2849 if (diff > 0) /* closest to end */
2850 VIsual = start_visual;
2851 else if (diff < 0) /* closest to start */
2852 VIsual = end_visual;
2853 else /* in the middle line */
2855 if (curwin->w_cursor.col <
2856 (start_visual.col + end_visual.col) / 2)
2857 VIsual = end_visual;
2858 else
2859 VIsual = start_visual;
2866 * If Visual mode started in insert mode, execute "CTRL-O"
2868 else if ((State & INSERT) && VIsual_active)
2869 stuffcharReadbuff(Ctrl_O);
2870 #endif
2873 * Middle mouse click: Put text before cursor.
2875 if (which_button == MOUSE_MIDDLE)
2877 #ifdef FEAT_CLIPBOARD
2878 if (clip_star.available && regname == 0)
2879 regname = '*';
2880 #endif
2881 if (yank_register_mline(regname))
2883 if (mouse_past_bottom)
2884 dir = FORWARD;
2886 else if (mouse_past_eol)
2887 dir = FORWARD;
2889 if (fixindent)
2891 c1 = (dir == BACKWARD) ? '[' : ']';
2892 c2 = 'p';
2894 else
2896 c1 = (dir == FORWARD) ? 'p' : 'P';
2897 c2 = NUL;
2899 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2902 * Remember where the paste started, so in edit() Insstart can be set
2903 * to this position
2905 if (restart_edit != 0)
2906 where_paste_started = curwin->w_cursor;
2907 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2910 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2912 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2913 * error under the mouse pointer.
2915 else if (((mod_mask & MOD_MASK_CTRL)
2916 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2917 && bt_quickfix(curbuf))
2919 if (State & INSERT)
2920 stuffcharReadbuff(Ctrl_O);
2921 if (curwin->w_llist_ref == NULL) /* quickfix window */
2922 stuffReadbuff((char_u *)":.cc\n");
2923 else /* location list window */
2924 stuffReadbuff((char_u *)":.ll\n");
2925 got_click = FALSE; /* ignore drag&release now */
2927 #endif
2930 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2931 * under the mouse pointer.
2933 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2934 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2936 if (State & INSERT)
2937 stuffcharReadbuff(Ctrl_O);
2938 stuffcharReadbuff(Ctrl_RSB);
2939 got_click = FALSE; /* ignore drag&release now */
2943 * Shift-Mouse click searches for the next occurrence of the word under
2944 * the mouse pointer
2946 else if ((mod_mask & MOD_MASK_SHIFT))
2948 if (State & INSERT
2949 #ifdef FEAT_VISUAL
2950 || (VIsual_active && VIsual_select)
2951 #endif
2953 stuffcharReadbuff(Ctrl_O);
2954 if (which_button == MOUSE_LEFT)
2955 stuffcharReadbuff('*');
2956 else /* MOUSE_RIGHT */
2957 stuffcharReadbuff('#');
2960 /* Handle double clicks, unless on status line */
2961 else if (in_status_line)
2963 #ifdef FEAT_MOUSESHAPE
2964 if ((is_drag || is_click) && !drag_status_line)
2966 drag_status_line = TRUE;
2967 update_mouseshape(-1);
2969 #endif
2971 #ifdef FEAT_VERTSPLIT
2972 else if (in_sep_line)
2974 # ifdef FEAT_MOUSESHAPE
2975 if ((is_drag || is_click) && !drag_sep_line)
2977 drag_sep_line = TRUE;
2978 update_mouseshape(-1);
2980 # endif
2982 #endif
2983 #ifdef FEAT_VISUAL
2984 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
2985 && mouse_has(MOUSE_VISUAL))
2987 if (is_click || !VIsual_active)
2989 if (VIsual_active)
2990 orig_cursor = VIsual;
2991 else
2993 check_visual_highlight();
2994 VIsual = curwin->w_cursor;
2995 orig_cursor = VIsual;
2996 VIsual_active = TRUE;
2997 VIsual_reselect = TRUE;
2998 /* start Select mode if 'selectmode' contains "mouse" */
2999 may_start_select('o');
3000 setmouse();
3002 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3004 /* Double click with ALT pressed makes it blockwise. */
3005 if (mod_mask & MOD_MASK_ALT)
3006 VIsual_mode = Ctrl_V;
3007 else
3008 VIsual_mode = 'v';
3010 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3011 VIsual_mode = 'V';
3012 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3013 VIsual_mode = Ctrl_V;
3014 #ifdef FEAT_CLIPBOARD
3015 /* Make sure the clipboard gets updated. Needed because start and
3016 * end may still be the same, and the selection needs to be owned */
3017 clip_star.vmode = NUL;
3018 #endif
3021 * A double click selects a word or a block.
3023 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3025 pos_T *pos = NULL;
3026 int gc;
3028 if (is_click)
3030 /* If the character under the cursor (skipping white space) is
3031 * not a word character, try finding a match and select a (),
3032 * {}, [], #if/#endif, etc. block. */
3033 end_visual = curwin->w_cursor;
3034 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
3035 inc(&end_visual);
3036 if (oap != NULL)
3037 oap->motion_type = MCHAR;
3038 if (oap != NULL
3039 && VIsual_mode == 'v'
3040 && !vim_iswordc(gchar_pos(&end_visual))
3041 && equalpos(curwin->w_cursor, VIsual)
3042 && (pos = findmatch(oap, NUL)) != NULL)
3044 curwin->w_cursor = *pos;
3045 if (oap->motion_type == MLINE)
3046 VIsual_mode = 'V';
3047 else if (*p_sel == 'e')
3049 if (lt(curwin->w_cursor, VIsual))
3050 ++VIsual.col;
3051 else
3052 ++curwin->w_cursor.col;
3057 if (pos == NULL && (is_click || is_drag))
3059 /* When not found a match or when dragging: extend to include
3060 * a word. */
3061 if (lt(curwin->w_cursor, orig_cursor))
3063 find_start_of_word(&curwin->w_cursor);
3064 find_end_of_word(&VIsual);
3066 else
3068 find_start_of_word(&VIsual);
3069 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3070 #ifdef FEAT_MBYTE
3071 curwin->w_cursor.col +=
3072 (*mb_ptr2len)(ml_get_cursor());
3073 #else
3074 ++curwin->w_cursor.col;
3075 #endif
3076 find_end_of_word(&curwin->w_cursor);
3079 curwin->w_set_curswant = TRUE;
3081 if (is_click)
3082 redraw_curbuf_later(INVERTED); /* update the inversion */
3084 else if (VIsual_active && !old_active)
3086 if (mod_mask & MOD_MASK_ALT)
3087 VIsual_mode = Ctrl_V;
3088 else
3089 VIsual_mode = 'v';
3092 /* If Visual mode changed show it later. */
3093 if ((!VIsual_active && old_active && mode_displayed)
3094 || (VIsual_active && p_smd && msg_silent == 0
3095 && (!old_active || VIsual_mode != old_mode)))
3096 redraw_cmdline = TRUE;
3097 #endif
3099 return moved;
3102 #ifdef FEAT_VISUAL
3104 * Move "pos" back to the start of the word it's in.
3106 static void
3107 find_start_of_word(pos)
3108 pos_T *pos;
3110 char_u *line;
3111 int cclass;
3112 int col;
3114 line = ml_get(pos->lnum);
3115 cclass = get_mouse_class(line + pos->col);
3117 while (pos->col > 0)
3119 col = pos->col - 1;
3120 #ifdef FEAT_MBYTE
3121 col -= (*mb_head_off)(line, line + col);
3122 #endif
3123 if (get_mouse_class(line + col) != cclass)
3124 break;
3125 pos->col = col;
3130 * Move "pos" forward to the end of the word it's in.
3131 * When 'selection' is "exclusive", the position is just after the word.
3133 static void
3134 find_end_of_word(pos)
3135 pos_T *pos;
3137 char_u *line;
3138 int cclass;
3139 int col;
3141 line = ml_get(pos->lnum);
3142 if (*p_sel == 'e' && pos->col > 0)
3144 --pos->col;
3145 #ifdef FEAT_MBYTE
3146 pos->col -= (*mb_head_off)(line, line + pos->col);
3147 #endif
3149 cclass = get_mouse_class(line + pos->col);
3150 while (line[pos->col] != NUL)
3152 #ifdef FEAT_MBYTE
3153 col = pos->col + (*mb_ptr2len)(line + pos->col);
3154 #else
3155 col = pos->col + 1;
3156 #endif
3157 if (get_mouse_class(line + col) != cclass)
3159 if (*p_sel == 'e')
3160 pos->col = col;
3161 break;
3163 pos->col = col;
3168 * Get class of a character for selection: same class means same word.
3169 * 0: blank
3170 * 1: punctuation groups
3171 * 2: normal word character
3172 * >2: multi-byte word character.
3174 static int
3175 get_mouse_class(p)
3176 char_u *p;
3178 int c;
3180 #ifdef FEAT_MBYTE
3181 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3182 return mb_get_class(p);
3183 #endif
3185 c = *p;
3186 if (c == ' ' || c == '\t')
3187 return 0;
3189 if (vim_iswordc(c))
3190 return 2;
3193 * There are a few special cases where we want certain combinations of
3194 * characters to be considered as a single word. These are things like
3195 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
3196 * character is in it's own class.
3198 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3199 return 1;
3200 return c;
3202 #endif /* FEAT_VISUAL */
3203 #endif /* FEAT_MOUSE */
3205 #if defined(FEAT_VISUAL) || defined(PROTO)
3207 * Check if highlighting for visual mode is possible, give a warning message
3208 * if not.
3210 void
3211 check_visual_highlight()
3213 static int did_check = FALSE;
3215 if (full_screen)
3217 if (!did_check && hl_attr(HLF_V) == 0)
3218 MSG(_("Warning: terminal cannot highlight"));
3219 did_check = TRUE;
3224 * End Visual mode.
3225 * This function should ALWAYS be called to end Visual mode, except from
3226 * do_pending_operator().
3228 void
3229 end_visual_mode()
3231 #ifdef FEAT_CLIPBOARD
3233 * If we are using the clipboard, then remember what was selected in case
3234 * we need to paste it somewhere while we still own the selection.
3235 * Only do this when the clipboard is already owned. Don't want to grab
3236 * the selection when hitting ESC.
3238 if (clip_star.available && clip_star.owned)
3239 clip_auto_select();
3240 #endif
3242 VIsual_active = FALSE;
3243 #ifdef FEAT_MOUSE
3244 setmouse();
3245 mouse_dragging = 0;
3246 #endif
3248 /* Save the current VIsual area for '< and '> marks, and "gv" */
3249 curbuf->b_visual.vi_mode = VIsual_mode;
3250 curbuf->b_visual.vi_start = VIsual;
3251 curbuf->b_visual.vi_end = curwin->w_cursor;
3252 curbuf->b_visual.vi_curswant = curwin->w_curswant;
3253 #ifdef FEAT_EVAL
3254 curbuf->b_visual_mode_eval = VIsual_mode;
3255 #endif
3256 #ifdef FEAT_VIRTUALEDIT
3257 if (!virtual_active())
3258 curwin->w_cursor.coladd = 0;
3259 #endif
3261 if (mode_displayed)
3262 clear_cmdline = TRUE; /* unshow visual mode later */
3263 #ifdef FEAT_CMDL_INFO
3264 else
3265 clear_showcmd();
3266 #endif
3268 adjust_cursor_eol();
3272 * Reset VIsual_active and VIsual_reselect.
3274 void
3275 reset_VIsual_and_resel()
3277 if (VIsual_active)
3279 end_visual_mode();
3280 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3282 VIsual_reselect = FALSE;
3286 * Reset VIsual_active and VIsual_reselect if it's set.
3288 void
3289 reset_VIsual()
3291 if (VIsual_active)
3293 end_visual_mode();
3294 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3295 VIsual_reselect = FALSE;
3298 #endif /* FEAT_VISUAL */
3300 #if defined(FEAT_BEVAL)
3301 static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3304 * Check for a balloon-eval special item to include when searching for an
3305 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3306 * Returns TRUE if the character at "*ptr" should be included.
3307 * "dir" is FORWARD or BACKWARD, the direction of searching.
3308 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3309 * "bnp" points to a counter for square brackets.
3311 static int
3312 find_is_eval_item(ptr, colp, bnp, dir)
3313 char_u *ptr;
3314 int *colp;
3315 int *bnp;
3316 int dir;
3318 /* Accept everything inside []. */
3319 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3320 ++*bnp;
3321 if (*bnp > 0)
3323 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3324 --*bnp;
3325 return TRUE;
3328 /* skip over "s.var" */
3329 if (*ptr == '.')
3330 return TRUE;
3332 /* two-character item: s->var */
3333 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3334 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3336 *colp += dir;
3337 return TRUE;
3339 return FALSE;
3341 #endif
3344 * Find the identifier under or to the right of the cursor.
3345 * "find_type" can have one of three values:
3346 * FIND_IDENT: find an identifier (keyword)
3347 * FIND_STRING: find any non-white string
3348 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
3349 * FIND_EVAL: find text useful for C program debugging
3351 * There are three steps:
3352 * 1. Search forward for the start of an identifier/string. Doesn't move if
3353 * already on one.
3354 * 2. Search backward for the start of this identifier/string.
3355 * This doesn't match the real Vi but I like it a little better and it
3356 * shouldn't bother anyone.
3357 * 3. Search forward to the end of this identifier/string.
3358 * When FIND_IDENT isn't defined, we backup until a blank.
3360 * Returns the length of the string, or zero if no string is found.
3361 * If a string is found, a pointer to the string is put in "*string". This
3362 * string is not always NUL terminated.
3365 find_ident_under_cursor(string, find_type)
3366 char_u **string;
3367 int find_type;
3369 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3370 curwin->w_cursor.col, string, find_type);
3374 * Like find_ident_under_cursor(), but for any window and any position.
3375 * However: Uses 'iskeyword' from the current window!.
3378 find_ident_at_pos(wp, lnum, startcol, string, find_type)
3379 win_T *wp;
3380 linenr_T lnum;
3381 colnr_T startcol;
3382 char_u **string;
3383 int find_type;
3385 char_u *ptr;
3386 int col = 0; /* init to shut up GCC */
3387 int i;
3388 #ifdef FEAT_MBYTE
3389 int this_class = 0;
3390 int prev_class;
3391 int prevcol;
3392 #endif
3393 #if defined(FEAT_BEVAL)
3394 int bn = 0; /* bracket nesting */
3395 #endif
3398 * if i == 0: try to find an identifier
3399 * if i == 1: try to find any non-white string
3401 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3402 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3405 * 1. skip to start of identifier/string
3407 col = startcol;
3408 #ifdef FEAT_MBYTE
3409 if (has_mbyte)
3411 while (ptr[col] != NUL)
3413 # if defined(FEAT_BEVAL)
3414 /* Stop at a ']' to evaluate "a[x]". */
3415 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3416 break;
3417 # endif
3418 this_class = mb_get_class(ptr + col);
3419 if (this_class != 0 && (i == 1 || this_class != 1))
3420 break;
3421 col += (*mb_ptr2len)(ptr + col);
3424 else
3425 #endif
3426 while (ptr[col] != NUL
3427 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
3428 # if defined(FEAT_BEVAL)
3429 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3430 # endif
3432 ++col;
3434 #if defined(FEAT_BEVAL)
3435 /* When starting on a ']' count it, so that we include the '['. */
3436 bn = ptr[col] == ']';
3437 #endif
3440 * 2. Back up to start of identifier/string.
3442 #ifdef FEAT_MBYTE
3443 if (has_mbyte)
3445 /* Remember class of character under cursor. */
3446 # if defined(FEAT_BEVAL)
3447 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3448 this_class = mb_get_class((char_u *)"a");
3449 else
3450 # endif
3451 this_class = mb_get_class(ptr + col);
3452 while (col > 0 && this_class != 0)
3454 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3455 prev_class = mb_get_class(ptr + prevcol);
3456 if (this_class != prev_class
3457 && (i == 0
3458 || prev_class == 0
3459 || (find_type & FIND_IDENT))
3460 # if defined(FEAT_BEVAL)
3461 && (!(find_type & FIND_EVAL)
3462 || prevcol == 0
3463 || !find_is_eval_item(ptr + prevcol, &prevcol,
3464 &bn, BACKWARD))
3465 # endif
3467 break;
3468 col = prevcol;
3471 /* If we don't want just any old string, or we've found an
3472 * identifier, stop searching. */
3473 if (this_class > 2)
3474 this_class = 2;
3475 if (!(find_type & FIND_STRING) || this_class == 2)
3476 break;
3478 else
3479 #endif
3481 while (col > 0
3482 && ((i == 0
3483 ? vim_iswordc(ptr[col - 1])
3484 : (!vim_iswhite(ptr[col - 1])
3485 && (!(find_type & FIND_IDENT)
3486 || !vim_iswordc(ptr[col - 1]))))
3487 #if defined(FEAT_BEVAL)
3488 || ((find_type & FIND_EVAL)
3489 && col > 1
3490 && find_is_eval_item(ptr + col - 1, &col,
3491 &bn, BACKWARD))
3492 #endif
3494 --col;
3496 /* If we don't want just any old string, or we've found an
3497 * identifier, stop searching. */
3498 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3499 break;
3503 if (ptr[col] == NUL || (i == 0 && (
3504 #ifdef FEAT_MBYTE
3505 has_mbyte ? this_class != 2 :
3506 #endif
3507 !vim_iswordc(ptr[col]))))
3510 * didn't find an identifier or string
3512 if (find_type & FIND_STRING)
3513 EMSG(_("E348: No string under cursor"));
3514 else
3515 EMSG(_(e_noident));
3516 return 0;
3518 ptr += col;
3519 *string = ptr;
3522 * 3. Find the end if the identifier/string.
3524 #if defined(FEAT_BEVAL)
3525 bn = 0;
3526 startcol -= col;
3527 #endif
3528 col = 0;
3529 #ifdef FEAT_MBYTE
3530 if (has_mbyte)
3532 /* Search for point of changing multibyte character class. */
3533 this_class = mb_get_class(ptr);
3534 while (ptr[col] != NUL
3535 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3536 : mb_get_class(ptr + col) != 0)
3537 # if defined(FEAT_BEVAL)
3538 || ((find_type & FIND_EVAL)
3539 && col <= (int)startcol
3540 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3541 # endif
3543 col += (*mb_ptr2len)(ptr + col);
3545 else
3546 #endif
3547 while ((i == 0 ? vim_iswordc(ptr[col])
3548 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
3549 # if defined(FEAT_BEVAL)
3550 || ((find_type & FIND_EVAL)
3551 && col <= (int)startcol
3552 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3553 # endif
3556 ++col;
3559 return col;
3563 * Prepare for redo of a normal command.
3565 static void
3566 prep_redo_cmd(cap)
3567 cmdarg_T *cap;
3569 prep_redo(cap->oap->regname, cap->count0,
3570 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3574 * Prepare for redo of any command.
3575 * Note that only the last argument can be a multi-byte char.
3577 static void
3578 prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3579 int regname;
3580 long num;
3581 int cmd1;
3582 int cmd2;
3583 int cmd3;
3584 int cmd4;
3585 int cmd5;
3587 ResetRedobuff();
3588 if (regname != 0) /* yank from specified buffer */
3590 AppendCharToRedobuff('"');
3591 AppendCharToRedobuff(regname);
3593 if (num)
3594 AppendNumberToRedobuff(num);
3596 if (cmd1 != NUL)
3597 AppendCharToRedobuff(cmd1);
3598 if (cmd2 != NUL)
3599 AppendCharToRedobuff(cmd2);
3600 if (cmd3 != NUL)
3601 AppendCharToRedobuff(cmd3);
3602 if (cmd4 != NUL)
3603 AppendCharToRedobuff(cmd4);
3604 if (cmd5 != NUL)
3605 AppendCharToRedobuff(cmd5);
3609 * check for operator active and clear it
3611 * return TRUE if operator was active
3613 static int
3614 checkclearop(oap)
3615 oparg_T *oap;
3617 if (oap->op_type == OP_NOP)
3618 return FALSE;
3619 clearopbeep(oap);
3620 return TRUE;
3624 * Check for operator or Visual active. Clear active operator.
3626 * Return TRUE if operator or Visual was active.
3628 static int
3629 checkclearopq(oap)
3630 oparg_T *oap;
3632 if (oap->op_type == OP_NOP
3633 #ifdef FEAT_VISUAL
3634 && !VIsual_active
3635 #endif
3637 return FALSE;
3638 clearopbeep(oap);
3639 return TRUE;
3642 static void
3643 clearop(oap)
3644 oparg_T *oap;
3646 oap->op_type = OP_NOP;
3647 oap->regname = 0;
3648 oap->motion_force = NUL;
3649 oap->use_reg_one = FALSE;
3652 static void
3653 clearopbeep(oap)
3654 oparg_T *oap;
3656 clearop(oap);
3657 beep_flush();
3660 #ifdef FEAT_VISUAL
3662 * Remove the shift modifier from a special key.
3664 static void
3665 unshift_special(cap)
3666 cmdarg_T *cap;
3668 switch (cap->cmdchar)
3670 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3671 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3672 case K_S_UP: cap->cmdchar = K_UP; break;
3673 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3674 case K_S_HOME: cap->cmdchar = K_HOME; break;
3675 case K_S_END: cap->cmdchar = K_END; break;
3677 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3679 #endif
3681 #if defined(FEAT_CMDL_INFO) || defined(PROTO)
3683 * Routines for displaying a partly typed command
3686 #ifdef FEAT_VISUAL /* need room for size of Visual area */
3687 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3688 #else
3689 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3690 #endif
3691 static char_u showcmd_buf[SHOWCMD_BUFLEN];
3692 static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3693 static int showcmd_is_clear = TRUE;
3694 static int showcmd_visual = FALSE;
3696 static void display_showcmd __ARGS((void));
3698 void
3699 clear_showcmd()
3701 if (!p_sc)
3702 return;
3704 #ifdef FEAT_VISUAL
3705 if (VIsual_active && !char_avail())
3707 int i = lt(VIsual, curwin->w_cursor);
3708 long lines;
3709 colnr_T leftcol, rightcol;
3710 linenr_T top, bot;
3712 /* Show the size of the Visual area. */
3713 if (i)
3715 top = VIsual.lnum;
3716 bot = curwin->w_cursor.lnum;
3718 else
3720 top = curwin->w_cursor.lnum;
3721 bot = VIsual.lnum;
3723 # ifdef FEAT_FOLDING
3724 /* Include closed folds as a whole. */
3725 hasFolding(top, &top, NULL);
3726 hasFolding(bot, NULL, &bot);
3727 # endif
3728 lines = bot - top + 1;
3730 if (VIsual_mode == Ctrl_V)
3732 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
3733 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3734 (long)(rightcol - leftcol + 1));
3736 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3737 sprintf((char *)showcmd_buf, "%ld", lines);
3738 else
3739 sprintf((char *)showcmd_buf, "%ld", (long)(i
3740 ? curwin->w_cursor.col - VIsual.col
3741 : VIsual.col - curwin->w_cursor.col) + (*p_sel != 'e'));
3742 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3743 showcmd_visual = TRUE;
3745 else
3746 #endif
3748 showcmd_buf[0] = NUL;
3749 showcmd_visual = FALSE;
3751 /* Don't actually display something if there is nothing to clear. */
3752 if (showcmd_is_clear)
3753 return;
3756 display_showcmd();
3760 * Add 'c' to string of shown command chars.
3761 * Return TRUE if output has been written (and setcursor() has been called).
3764 add_to_showcmd(c)
3765 int c;
3767 char_u *p;
3768 int old_len;
3769 int extra_len;
3770 int overflow;
3771 #if defined(FEAT_MOUSE)
3772 int i;
3773 static int ignore[] =
3775 # ifdef FEAT_GUI
3776 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3777 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
3778 # endif
3779 K_IGNORE,
3780 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3781 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3782 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
3783 K_MOUSEDOWN, K_MOUSEUP,
3784 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
3785 K_CURSORHOLD,
3788 #endif
3790 if (!p_sc || msg_silent != 0)
3791 return FALSE;
3793 if (showcmd_visual)
3795 showcmd_buf[0] = NUL;
3796 showcmd_visual = FALSE;
3799 #if defined(FEAT_MOUSE)
3800 /* Ignore keys that are scrollbar updates and mouse clicks */
3801 if (IS_SPECIAL(c))
3802 for (i = 0; ignore[i] != 0; ++i)
3803 if (ignore[i] == c)
3804 return FALSE;
3805 #endif
3807 p = transchar(c);
3808 old_len = (int)STRLEN(showcmd_buf);
3809 extra_len = (int)STRLEN(p);
3810 overflow = old_len + extra_len - SHOWCMD_COLS;
3811 if (overflow > 0)
3812 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3813 old_len - overflow + 1);
3814 STRCAT(showcmd_buf, p);
3816 if (char_avail())
3817 return FALSE;
3819 display_showcmd();
3821 return TRUE;
3824 void
3825 add_to_showcmd_c(c)
3826 int c;
3828 if (!add_to_showcmd(c))
3829 setcursor();
3833 * Delete 'len' characters from the end of the shown command.
3835 static void
3836 del_from_showcmd(len)
3837 int len;
3839 int old_len;
3841 if (!p_sc)
3842 return;
3844 old_len = (int)STRLEN(showcmd_buf);
3845 if (len > old_len)
3846 len = old_len;
3847 showcmd_buf[old_len - len] = NUL;
3849 if (!char_avail())
3850 display_showcmd();
3854 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3855 * something and there is a partial mapping.
3857 void
3858 push_showcmd()
3860 if (p_sc)
3861 STRCPY(old_showcmd_buf, showcmd_buf);
3864 void
3865 pop_showcmd()
3867 if (!p_sc)
3868 return;
3870 STRCPY(showcmd_buf, old_showcmd_buf);
3872 display_showcmd();
3875 static void
3876 display_showcmd()
3878 int len;
3880 cursor_off();
3882 len = (int)STRLEN(showcmd_buf);
3883 if (len == 0)
3884 showcmd_is_clear = TRUE;
3885 else
3887 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3888 showcmd_is_clear = FALSE;
3892 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3893 * spaces
3895 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3897 setcursor(); /* put cursor back where it belongs */
3899 #endif
3901 #ifdef FEAT_SCROLLBIND
3903 * When "check" is FALSE, prepare for commands that scroll the window.
3904 * When "check" is TRUE, take care of scroll-binding after the window has
3905 * scrolled. Called from normal_cmd() and edit().
3907 void
3908 do_check_scrollbind(check)
3909 int check;
3911 static win_T *old_curwin = NULL;
3912 static linenr_T old_topline = 0;
3913 #ifdef FEAT_DIFF
3914 static int old_topfill = 0;
3915 #endif
3916 static buf_T *old_buf = NULL;
3917 static colnr_T old_leftcol = 0;
3919 if (check && curwin->w_p_scb)
3921 /* If a ":syncbind" command was just used, don't scroll, only reset
3922 * the values. */
3923 if (did_syncbind)
3924 did_syncbind = FALSE;
3925 else if (curwin == old_curwin)
3928 * Synchronize other windows, as necessary according to
3929 * 'scrollbind'. Don't do this after an ":edit" command, except
3930 * when 'diff' is set.
3932 if ((curwin->w_buffer == old_buf
3933 #ifdef FEAT_DIFF
3934 || curwin->w_p_diff
3935 #endif
3937 && (curwin->w_topline != old_topline
3938 #ifdef FEAT_DIFF
3939 || curwin->w_topfill != old_topfill
3940 #endif
3941 || curwin->w_leftcol != old_leftcol))
3943 check_scrollbind(curwin->w_topline - old_topline,
3944 (long)(curwin->w_leftcol - old_leftcol));
3947 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
3950 * When switching between windows, make sure that the relative
3951 * vertical offset is valid for the new window. The relative
3952 * offset is invalid whenever another 'scrollbind' window has
3953 * scrolled to a point that would force the current window to
3954 * scroll past the beginning or end of its buffer. When the
3955 * resync is performed, some of the other 'scrollbind' windows may
3956 * need to jump so that the current window's relative position is
3957 * visible on-screen.
3959 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
3961 curwin->w_scbind_pos = curwin->w_topline;
3964 old_curwin = curwin;
3965 old_topline = curwin->w_topline;
3966 #ifdef FEAT_DIFF
3967 old_topfill = curwin->w_topfill;
3968 #endif
3969 old_buf = curwin->w_buffer;
3970 old_leftcol = curwin->w_leftcol;
3974 * Synchronize any windows that have "scrollbind" set, based on the
3975 * number of rows by which the current window has changed
3976 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3978 void
3979 check_scrollbind(topline_diff, leftcol_diff)
3980 linenr_T topline_diff;
3981 long leftcol_diff;
3983 int want_ver;
3984 int want_hor;
3985 win_T *old_curwin = curwin;
3986 buf_T *old_curbuf = curbuf;
3987 #ifdef FEAT_VISUAL
3988 int old_VIsual_select = VIsual_select;
3989 int old_VIsual_active = VIsual_active;
3990 #endif
3991 colnr_T tgt_leftcol = curwin->w_leftcol;
3992 long topline;
3993 long y;
3996 * check 'scrollopt' string for vertical and horizontal scroll options
3998 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
3999 #ifdef FEAT_DIFF
4000 want_ver |= old_curwin->w_p_diff;
4001 #endif
4002 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4005 * loop through the scrollbound windows and scroll accordingly
4007 #ifdef FEAT_VISUAL
4008 VIsual_select = VIsual_active = 0;
4009 #endif
4010 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4012 curbuf = curwin->w_buffer;
4013 /* skip original window and windows with 'noscrollbind' */
4014 if (curwin != old_curwin && curwin->w_p_scb)
4017 * do the vertical scroll
4019 if (want_ver)
4021 #ifdef FEAT_DIFF
4022 if (old_curwin->w_p_diff && curwin->w_p_diff)
4024 diff_set_topline(old_curwin, curwin);
4026 else
4027 #endif
4029 curwin->w_scbind_pos += topline_diff;
4030 topline = curwin->w_scbind_pos;
4031 if (topline > curbuf->b_ml.ml_line_count)
4032 topline = curbuf->b_ml.ml_line_count;
4033 if (topline < 1)
4034 topline = 1;
4036 y = topline - curwin->w_topline;
4037 if (y > 0)
4038 scrollup(y, FALSE);
4039 else
4040 scrolldown(-y, FALSE);
4043 redraw_later(VALID);
4044 cursor_correct();
4045 #ifdef FEAT_WINDOWS
4046 curwin->w_redr_status = TRUE;
4047 #endif
4051 * do the horizontal scroll
4053 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4055 curwin->w_leftcol = tgt_leftcol;
4056 leftcol_changed();
4062 * reset current-window
4064 #ifdef FEAT_VISUAL
4065 VIsual_select = old_VIsual_select;
4066 VIsual_active = old_VIsual_active;
4067 #endif
4068 curwin = old_curwin;
4069 curbuf = old_curbuf;
4071 #endif /* #ifdef FEAT_SCROLLBIND */
4074 * Command character that's ignored.
4075 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
4076 * xon/xoff
4078 static void
4079 nv_ignore(cap)
4080 cmdarg_T *cap;
4082 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
4086 * Command character that doesn't do anything, but unlike nv_ignore() does
4087 * start edit(). Used for "startinsert" executed while starting up.
4089 /*ARGSUSED */
4090 static void
4091 nv_nop(cap)
4092 cmdarg_T *cap;
4097 * Command character doesn't exist.
4099 static void
4100 nv_error(cap)
4101 cmdarg_T *cap;
4103 clearopbeep(cap->oap);
4107 * <Help> and <F1> commands.
4109 static void
4110 nv_help(cap)
4111 cmdarg_T *cap;
4113 if (!checkclearopq(cap->oap))
4114 ex_help(NULL);
4118 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4120 static void
4121 nv_addsub(cap)
4122 cmdarg_T *cap;
4124 if (!checkclearopq(cap->oap)
4125 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4126 prep_redo_cmd(cap);
4130 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4132 static void
4133 nv_page(cap)
4134 cmdarg_T *cap;
4136 if (!checkclearop(cap->oap))
4138 #ifdef FEAT_WINDOWS
4139 if (mod_mask & MOD_MASK_CTRL)
4141 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4142 if (cap->arg == BACKWARD)
4143 goto_tabpage(-(int)cap->count1);
4144 else
4145 goto_tabpage((int)cap->count0);
4147 else
4148 #endif
4149 (void)onepage(cap->arg, cap->count1);
4154 * Implementation of "gd" and "gD" command.
4156 static void
4157 nv_gd(oap, nchar, thisblock)
4158 oparg_T *oap;
4159 int nchar;
4160 int thisblock; /* 1 for "1gd" and "1gD" */
4162 int len;
4163 char_u *ptr;
4165 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
4166 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
4167 clearopbeep(oap);
4168 #ifdef FEAT_FOLDING
4169 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4170 foldOpenCursor();
4171 #endif
4175 * Search for variable declaration of "ptr[len]".
4176 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4177 * current file ("gD").
4178 * When "thisblock" is TRUE check the {} block scope.
4179 * Return FAIL when not found.
4182 find_decl(ptr, len, locally, thisblock, searchflags)
4183 char_u *ptr;
4184 int len;
4185 int locally;
4186 int thisblock;
4187 int searchflags; /* flags passed to searchit() */
4189 char_u *pat;
4190 pos_T old_pos;
4191 pos_T par_pos;
4192 pos_T found_pos;
4193 int t;
4194 int save_p_ws;
4195 int save_p_scs;
4196 int retval = OK;
4197 int incll;
4199 if ((pat = alloc(len + 7)) == NULL)
4200 return FAIL;
4202 /* Put "\V" before the pattern to avoid that the special meaning of "."
4203 * and "~" causes trouble. */
4204 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4205 len, ptr);
4206 old_pos = curwin->w_cursor;
4207 save_p_ws = p_ws;
4208 save_p_scs = p_scs;
4209 p_ws = FALSE; /* don't wrap around end of file now */
4210 p_scs = FALSE; /* don't switch ignorecase off now */
4213 * With "gD" go to line 1.
4214 * With "gd" Search back for the start of the current function, then go
4215 * back until a blank line. If this fails go to line 1.
4217 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
4219 setpcmark(); /* Set in findpar() otherwise */
4220 curwin->w_cursor.lnum = 1;
4221 par_pos = curwin->w_cursor;
4223 else
4225 par_pos = curwin->w_cursor;
4226 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4227 --curwin->w_cursor.lnum;
4229 curwin->w_cursor.col = 0;
4231 /* Search forward for the identifier, ignore comment lines. */
4232 clearpos(&found_pos);
4233 for (;;)
4235 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
4236 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
4237 if (curwin->w_cursor.lnum >= old_pos.lnum)
4238 t = FAIL; /* match after start is failure too */
4240 if (thisblock && t != FAIL)
4242 pos_T *pos;
4244 /* Check that the block the match is in doesn't end before the
4245 * position where we started the search from. */
4246 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4247 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4248 && pos->lnum < old_pos.lnum)
4249 continue;
4252 if (t == FAIL)
4254 /* If we previously found a valid position, use it. */
4255 if (found_pos.lnum != 0)
4257 curwin->w_cursor = found_pos;
4258 t = OK;
4260 break;
4262 #ifdef FEAT_COMMENTS
4263 if (get_leader_len(ml_get_curline(), NULL, FALSE) > 0)
4265 /* Ignore this line, continue at start of next line. */
4266 ++curwin->w_cursor.lnum;
4267 curwin->w_cursor.col = 0;
4268 continue;
4270 #endif
4271 if (!locally) /* global search: use first match found */
4272 break;
4273 if (curwin->w_cursor.lnum >= par_pos.lnum)
4275 /* If we previously found a valid position, use it. */
4276 if (found_pos.lnum != 0)
4277 curwin->w_cursor = found_pos;
4278 break;
4281 /* For finding a local variable and the match is before the "{" search
4282 * to find a later match. For K&R style function declarations this
4283 * skips the function header without types. */
4284 found_pos = curwin->w_cursor;
4287 if (t == FAIL)
4289 retval = FAIL;
4290 curwin->w_cursor = old_pos;
4292 else
4294 curwin->w_set_curswant = TRUE;
4295 /* "n" searches forward now */
4296 reset_search_dir();
4299 vim_free(pat);
4300 p_ws = save_p_ws;
4301 p_scs = save_p_scs;
4303 return retval;
4307 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4308 * lines rather than lines in the file.
4309 * 'dist' must be positive.
4311 * Return OK if able to move cursor, FAIL otherwise.
4313 static int
4314 nv_screengo(oap, dir, dist)
4315 oparg_T *oap;
4316 int dir;
4317 long dist;
4319 int linelen = linetabsize(ml_get_curline());
4320 int retval = OK;
4321 int atend = FALSE;
4322 int n;
4323 int col_off1; /* margin offset for first screen line */
4324 int col_off2; /* margin offset for wrapped screen line */
4325 int width1; /* text width for first screen line */
4326 int width2; /* test width for wrapped screen line */
4328 oap->motion_type = MCHAR;
4329 oap->inclusive = FALSE;
4331 col_off1 = curwin_col_off();
4332 col_off2 = col_off1 - curwin_col_off2();
4333 width1 = W_WIDTH(curwin) - col_off1;
4334 width2 = W_WIDTH(curwin) - col_off2;
4336 #ifdef FEAT_VERTSPLIT
4337 if (curwin->w_width != 0)
4339 #endif
4341 * Instead of sticking at the last character of the buffer line we
4342 * try to stick in the last column of the screen.
4344 if (curwin->w_curswant == MAXCOL)
4346 atend = TRUE;
4347 validate_virtcol();
4348 if (width1 <= 0)
4349 curwin->w_curswant = 0;
4350 else
4352 curwin->w_curswant = width1 - 1;
4353 if (curwin->w_virtcol > curwin->w_curswant)
4354 curwin->w_curswant += ((curwin->w_virtcol
4355 - curwin->w_curswant - 1) / width2 + 1) * width2;
4358 else
4360 if (linelen > width1)
4361 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4362 else
4363 n = width1;
4364 if (curwin->w_curswant > (colnr_T)n + 1)
4365 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4366 * width2;
4369 while (dist--)
4371 if (dir == BACKWARD)
4373 if ((long)curwin->w_curswant >= width2)
4374 /* move back within line */
4375 curwin->w_curswant -= width2;
4376 else
4378 /* to previous line */
4379 if (curwin->w_cursor.lnum == 1)
4381 retval = FAIL;
4382 break;
4384 --curwin->w_cursor.lnum;
4385 #ifdef FEAT_FOLDING
4386 /* Move to the start of a closed fold. Don't do that when
4387 * 'foldopen' contains "all": it will open in a moment. */
4388 if (!(fdo_flags & FDO_ALL))
4389 (void)hasFolding(curwin->w_cursor.lnum,
4390 &curwin->w_cursor.lnum, NULL);
4391 #endif
4392 linelen = linetabsize(ml_get_curline());
4393 if (linelen > width1)
4394 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4395 + 1) * width2;
4398 else /* dir == FORWARD */
4400 if (linelen > width1)
4401 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4402 else
4403 n = width1;
4404 if (curwin->w_curswant + width2 < (colnr_T)n)
4405 /* move forward within line */
4406 curwin->w_curswant += width2;
4407 else
4409 /* to next line */
4410 #ifdef FEAT_FOLDING
4411 /* Move to the end of a closed fold. */
4412 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4413 &curwin->w_cursor.lnum);
4414 #endif
4415 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4417 retval = FAIL;
4418 break;
4420 curwin->w_cursor.lnum++;
4421 curwin->w_curswant %= width2;
4425 #ifdef FEAT_VERTSPLIT
4427 #endif
4429 coladvance(curwin->w_curswant);
4431 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4432 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4435 * Check for landing on a character that got split at the end of the
4436 * last line. We want to advance a screenline, not end up in the same
4437 * screenline or move two screenlines.
4439 validate_virtcol();
4440 if (curwin->w_virtcol > curwin->w_curswant
4441 && (curwin->w_curswant < (colnr_T)width1
4442 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4443 : ((curwin->w_curswant - width1) % width2
4444 > (colnr_T)width2 / 2)))
4445 --curwin->w_cursor.col;
4447 #endif
4449 if (atend)
4450 curwin->w_curswant = MAXCOL; /* stick in the last column */
4452 return retval;
4455 #ifdef FEAT_MOUSE
4457 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4458 * when Shift or Ctrl is used.
4459 * K_MOUSEUP (cap->arg == TRUE) or K_MOUSEDOWN (cap->arg == FALSE)
4461 static void
4462 nv_mousescroll(cap)
4463 cmdarg_T *cap;
4465 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4466 win_T *old_curwin = curwin;
4468 /* Currently we only get the mouse coordinates in the GUI. */
4469 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4471 int row, col;
4473 row = mouse_row;
4474 col = mouse_col;
4476 /* find the window at the pointer coordinates */
4477 curwin = mouse_find_win(&row, &col);
4478 curbuf = curwin->w_buffer;
4480 # endif
4482 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4484 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4486 else
4488 cap->count1 = 3;
4489 cap->count0 = 3;
4490 nv_scroll_line(cap);
4493 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4494 curwin->w_redr_status = TRUE;
4496 curwin = old_curwin;
4497 curbuf = curwin->w_buffer;
4498 # endif
4502 * Mouse clicks and drags.
4504 static void
4505 nv_mouse(cap)
4506 cmdarg_T *cap;
4508 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4510 #endif
4513 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4514 * cap->arg must be TRUE for CTRL-E.
4516 static void
4517 nv_scroll_line(cap)
4518 cmdarg_T *cap;
4520 if (!checkclearop(cap->oap))
4521 scroll_redraw(cap->arg, cap->count1);
4525 * Scroll "count" lines up or down, and redraw.
4527 void
4528 scroll_redraw(up, count)
4529 int up;
4530 long count;
4532 linenr_T prev_topline = curwin->w_topline;
4533 #ifdef FEAT_DIFF
4534 int prev_topfill = curwin->w_topfill;
4535 #endif
4536 linenr_T prev_lnum = curwin->w_cursor.lnum;
4538 if (up)
4539 scrollup(count, TRUE);
4540 else
4541 scrolldown(count, TRUE);
4542 if (p_so)
4544 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4545 * valid, otherwise the screen jumps back at the end of the file. */
4546 cursor_correct();
4547 check_cursor_moved(curwin);
4548 curwin->w_valid |= VALID_TOPLINE;
4550 /* If moved back to where we were, at least move the cursor, otherwise
4551 * we get stuck at one position. Don't move the cursor up if the
4552 * first line of the buffer is already on the screen */
4553 while (curwin->w_topline == prev_topline
4554 #ifdef FEAT_DIFF
4555 && curwin->w_topfill == prev_topfill
4556 #endif
4559 if (up)
4561 if (curwin->w_cursor.lnum > prev_lnum
4562 || cursor_down(1L, FALSE) == FAIL)
4563 break;
4565 else
4567 if (curwin->w_cursor.lnum < prev_lnum
4568 || prev_topline == 1L
4569 || cursor_up(1L, FALSE) == FAIL)
4570 break;
4572 /* Mark w_topline as valid, otherwise the screen jumps back at the
4573 * end of the file. */
4574 check_cursor_moved(curwin);
4575 curwin->w_valid |= VALID_TOPLINE;
4578 if (curwin->w_cursor.lnum != prev_lnum)
4579 coladvance(curwin->w_curswant);
4580 redraw_later(VALID);
4584 * Commands that start with "z".
4586 static void
4587 nv_zet(cap)
4588 cmdarg_T *cap;
4590 long n;
4591 colnr_T col;
4592 int nchar = cap->nchar;
4593 #ifdef FEAT_FOLDING
4594 long old_fdl = curwin->w_p_fdl;
4595 int old_fen = curwin->w_p_fen;
4596 #endif
4597 #ifdef FEAT_SPELL
4598 int undo = FALSE;
4599 #endif
4601 if (VIM_ISDIGIT(nchar))
4604 * "z123{nchar}": edit the count before obtaining {nchar}
4606 if (checkclearop(cap->oap))
4607 return;
4608 n = nchar - '0';
4609 for (;;)
4611 #ifdef USE_ON_FLY_SCROLL
4612 dont_scroll = TRUE; /* disallow scrolling here */
4613 #endif
4614 ++no_mapping;
4615 ++allow_keys; /* no mapping for nchar, but allow key codes */
4616 nchar = plain_vgetc();
4617 #ifdef FEAT_LANGMAP
4618 LANGMAP_ADJUST(nchar, TRUE);
4619 #endif
4620 --no_mapping;
4621 --allow_keys;
4622 #ifdef FEAT_CMDL_INFO
4623 (void)add_to_showcmd(nchar);
4624 #endif
4625 if (nchar == K_DEL || nchar == K_KDEL)
4626 n /= 10;
4627 else if (VIM_ISDIGIT(nchar))
4628 n = n * 10 + (nchar - '0');
4629 else if (nchar == CAR)
4631 #ifdef FEAT_GUI
4632 need_mouse_correct = TRUE;
4633 #endif
4634 win_setheight((int)n);
4635 break;
4637 else if (nchar == 'l'
4638 || nchar == 'h'
4639 || nchar == K_LEFT
4640 || nchar == K_RIGHT)
4642 cap->count1 = n ? n * cap->count1 : cap->count1;
4643 goto dozet;
4645 else
4647 clearopbeep(cap->oap);
4648 break;
4651 cap->oap->op_type = OP_NOP;
4652 return;
4655 dozet:
4656 if (
4657 #ifdef FEAT_FOLDING
4658 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4659 * and "zC" only in Visual mode. "zj" and "zk" are motion
4660 * commands. */
4661 cap->nchar != 'f' && cap->nchar != 'F'
4662 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4663 && cap->nchar != 'j' && cap->nchar != 'k'
4665 #endif
4666 checkclearop(cap->oap))
4667 return;
4670 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4671 * If line number given, set cursor.
4673 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4674 && cap->count0
4675 && cap->count0 != curwin->w_cursor.lnum)
4677 setpcmark();
4678 if (cap->count0 > curbuf->b_ml.ml_line_count)
4679 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4680 else
4681 curwin->w_cursor.lnum = cap->count0;
4682 check_cursor_col();
4685 switch (nchar)
4687 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4688 case '+':
4689 if (cap->count0 == 0)
4691 /* No count given: put cursor at the line below screen */
4692 validate_botline(); /* make sure w_botline is valid */
4693 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4694 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4695 else
4696 curwin->w_cursor.lnum = curwin->w_botline;
4698 /* FALLTHROUGH */
4699 case NL:
4700 case CAR:
4701 case K_KENTER:
4702 beginline(BL_WHITE | BL_FIX);
4703 /* FALLTHROUGH */
4705 case 't': scroll_cursor_top(0, TRUE);
4706 redraw_later(VALID);
4707 break;
4709 /* "z." and "zz": put cursor in middle of screen */
4710 case '.': beginline(BL_WHITE | BL_FIX);
4711 /* FALLTHROUGH */
4713 case 'z': scroll_cursor_halfway(TRUE);
4714 redraw_later(VALID);
4715 break;
4717 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4718 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4719 * when <count> is at bottom of window, and puts that one at
4720 * bottom of window. */
4721 if (cap->count0 != 0)
4723 scroll_cursor_bot(0, TRUE);
4724 curwin->w_cursor.lnum = curwin->w_topline;
4726 else if (curwin->w_topline == 1)
4727 curwin->w_cursor.lnum = 1;
4728 else
4729 curwin->w_cursor.lnum = curwin->w_topline - 1;
4730 /* FALLTHROUGH */
4731 case '-':
4732 beginline(BL_WHITE | BL_FIX);
4733 /* FALLTHROUGH */
4735 case 'b': scroll_cursor_bot(0, TRUE);
4736 redraw_later(VALID);
4737 break;
4739 /* "zH" - scroll screen right half-page */
4740 case 'H':
4741 cap->count1 *= W_WIDTH(curwin) / 2;
4742 /* FALLTHROUGH */
4744 /* "zh" - scroll screen to the right */
4745 case 'h':
4746 case K_LEFT:
4747 if (!curwin->w_p_wrap)
4749 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4750 curwin->w_leftcol = 0;
4751 else
4752 curwin->w_leftcol -= (colnr_T)cap->count1;
4753 leftcol_changed();
4755 break;
4757 /* "zL" - scroll screen left half-page */
4758 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4759 /* FALLTHROUGH */
4761 /* "zl" - scroll screen to the left */
4762 case 'l':
4763 case K_RIGHT:
4764 if (!curwin->w_p_wrap)
4766 /* scroll the window left */
4767 curwin->w_leftcol += (colnr_T)cap->count1;
4768 leftcol_changed();
4770 break;
4772 /* "zs" - scroll screen, cursor at the start */
4773 case 's': if (!curwin->w_p_wrap)
4775 #ifdef FEAT_FOLDING
4776 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4777 col = 0; /* like the cursor is in col 0 */
4778 else
4779 #endif
4780 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4781 if ((long)col > p_siso)
4782 col -= p_siso;
4783 else
4784 col = 0;
4785 if (curwin->w_leftcol != col)
4787 curwin->w_leftcol = col;
4788 redraw_later(NOT_VALID);
4791 break;
4793 /* "ze" - scroll screen, cursor at the end */
4794 case 'e': if (!curwin->w_p_wrap)
4796 #ifdef FEAT_FOLDING
4797 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4798 col = 0; /* like the cursor is in col 0 */
4799 else
4800 #endif
4801 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4802 n = W_WIDTH(curwin) - curwin_col_off();
4803 if ((long)col + p_siso < n)
4804 col = 0;
4805 else
4806 col = col + p_siso - n + 1;
4807 if (curwin->w_leftcol != col)
4809 curwin->w_leftcol = col;
4810 redraw_later(NOT_VALID);
4813 break;
4815 #ifdef FEAT_FOLDING
4816 /* "zF": create fold command */
4817 /* "zf": create fold operator */
4818 case 'F':
4819 case 'f': if (foldManualAllowed(TRUE))
4821 cap->nchar = 'f';
4822 nv_operator(cap);
4823 curwin->w_p_fen = TRUE;
4825 /* "zF" is like "zfzf" */
4826 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4828 nv_operator(cap);
4829 finish_op = TRUE;
4832 else
4833 clearopbeep(cap->oap);
4834 break;
4836 /* "zd": delete fold at cursor */
4837 /* "zD": delete fold at cursor recursively */
4838 case 'd':
4839 case 'D': if (foldManualAllowed(FALSE))
4841 if (VIsual_active)
4842 nv_operator(cap);
4843 else
4844 deleteFold(curwin->w_cursor.lnum,
4845 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4847 break;
4849 /* "zE": erease all folds */
4850 case 'E': if (foldmethodIsManual(curwin))
4852 clearFolding(curwin);
4853 changed_window_setting();
4855 else if (foldmethodIsMarker(curwin))
4856 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4857 TRUE, FALSE);
4858 else
4859 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4860 break;
4862 /* "zn": fold none: reset 'foldenable' */
4863 case 'n': curwin->w_p_fen = FALSE;
4864 break;
4866 /* "zN": fold Normal: set 'foldenable' */
4867 case 'N': curwin->w_p_fen = TRUE;
4868 break;
4870 /* "zi": invert folding: toggle 'foldenable' */
4871 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4872 break;
4874 /* "za": open closed fold or close open fold at cursor */
4875 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4876 openFold(curwin->w_cursor.lnum, cap->count1);
4877 else
4879 closeFold(curwin->w_cursor.lnum, cap->count1);
4880 curwin->w_p_fen = TRUE;
4882 break;
4884 /* "zA": open fold at cursor recursively */
4885 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4886 openFoldRecurse(curwin->w_cursor.lnum);
4887 else
4889 closeFoldRecurse(curwin->w_cursor.lnum);
4890 curwin->w_p_fen = TRUE;
4892 break;
4894 /* "zo": open fold at cursor or Visual area */
4895 case 'o': if (VIsual_active)
4896 nv_operator(cap);
4897 else
4898 openFold(curwin->w_cursor.lnum, cap->count1);
4899 break;
4901 /* "zO": open fold recursively */
4902 case 'O': if (VIsual_active)
4903 nv_operator(cap);
4904 else
4905 openFoldRecurse(curwin->w_cursor.lnum);
4906 break;
4908 /* "zc": close fold at cursor or Visual area */
4909 case 'c': if (VIsual_active)
4910 nv_operator(cap);
4911 else
4912 closeFold(curwin->w_cursor.lnum, cap->count1);
4913 curwin->w_p_fen = TRUE;
4914 break;
4916 /* "zC": close fold recursively */
4917 case 'C': if (VIsual_active)
4918 nv_operator(cap);
4919 else
4920 closeFoldRecurse(curwin->w_cursor.lnum);
4921 curwin->w_p_fen = TRUE;
4922 break;
4924 /* "zv": open folds at the cursor */
4925 case 'v': foldOpenCursor();
4926 break;
4928 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
4929 case 'x': curwin->w_p_fen = TRUE;
4930 newFoldLevel(); /* update right now */
4931 foldOpenCursor();
4932 break;
4934 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
4935 case 'X': curwin->w_p_fen = TRUE;
4936 old_fdl = -1; /* force an update */
4937 break;
4939 /* "zm": fold more */
4940 case 'm': if (curwin->w_p_fdl > 0)
4941 --curwin->w_p_fdl;
4942 old_fdl = -1; /* force an update */
4943 curwin->w_p_fen = TRUE;
4944 break;
4946 /* "zM": close all folds */
4947 case 'M': curwin->w_p_fdl = 0;
4948 old_fdl = -1; /* force an update */
4949 curwin->w_p_fen = TRUE;
4950 break;
4952 /* "zr": reduce folding */
4953 case 'r': ++curwin->w_p_fdl;
4954 break;
4956 /* "zR": open all folds */
4957 case 'R': curwin->w_p_fdl = getDeepestNesting();
4958 old_fdl = -1; /* force an update */
4959 break;
4961 case 'j': /* "zj" move to next fold downwards */
4962 case 'k': /* "zk" move to next fold upwards */
4963 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
4964 cap->count1) == FAIL)
4965 clearopbeep(cap->oap);
4966 break;
4968 #endif /* FEAT_FOLDING */
4970 #ifdef FEAT_SPELL
4971 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
4972 ++no_mapping;
4973 ++allow_keys; /* no mapping for nchar, but allow key codes */
4974 nchar = plain_vgetc();
4975 #ifdef FEAT_LANGMAP
4976 LANGMAP_ADJUST(nchar, TRUE);
4977 #endif
4978 --no_mapping;
4979 --allow_keys;
4980 #ifdef FEAT_CMDL_INFO
4981 (void)add_to_showcmd(nchar);
4982 #endif
4983 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
4985 clearopbeep(cap->oap);
4986 break;
4988 undo = TRUE;
4989 /*FALLTHROUGH*/
4991 case 'g': /* "zg": add good word to word list */
4992 case 'w': /* "zw": add wrong word to word list */
4993 case 'G': /* "zG": add good word to temp word list */
4994 case 'W': /* "zW": add wrong word to temp word list */
4996 char_u *ptr = NULL;
4997 int len;
4999 if (checkclearop(cap->oap))
5000 break;
5001 # ifdef FEAT_VISUAL
5002 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5003 == FAIL)
5004 return;
5005 # endif
5006 if (ptr == NULL)
5008 pos_T pos = curwin->w_cursor;
5010 /* Find bad word under the cursor. */
5011 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
5012 if (len != 0 && curwin->w_cursor.col <= pos.col)
5013 ptr = ml_get_pos(&curwin->w_cursor);
5014 curwin->w_cursor = pos;
5017 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5018 FIND_IDENT)) == 0)
5019 return;
5020 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
5021 (nchar == 'G' || nchar == 'W')
5022 ? 0 : (int)cap->count1,
5023 undo);
5025 break;
5027 case '=': /* "z=": suggestions for a badly spelled word */
5028 if (!checkclearop(cap->oap))
5029 spell_suggest((int)cap->count0);
5030 break;
5031 #endif
5033 default: clearopbeep(cap->oap);
5036 #ifdef FEAT_FOLDING
5037 /* Redraw when 'foldenable' changed */
5038 if (old_fen != curwin->w_p_fen)
5040 # ifdef FEAT_DIFF
5041 win_T *wp;
5043 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5045 /* Adjust 'foldenable' in diff-synced windows. */
5046 FOR_ALL_WINDOWS(wp)
5048 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5050 wp->w_p_fen = curwin->w_p_fen;
5051 changed_window_setting_win(wp);
5055 # endif
5056 changed_window_setting();
5059 /* Redraw when 'foldlevel' changed. */
5060 if (old_fdl != curwin->w_p_fdl)
5061 newFoldLevel();
5062 #endif
5065 #ifdef FEAT_GUI
5067 * Vertical scrollbar movement.
5069 static void
5070 nv_ver_scrollbar(cap)
5071 cmdarg_T *cap;
5073 if (cap->oap->op_type != OP_NOP)
5074 clearopbeep(cap->oap);
5076 /* Even if an operator was pending, we still want to scroll */
5077 gui_do_scroll();
5081 * Horizontal scrollbar movement.
5083 static void
5084 nv_hor_scrollbar(cap)
5085 cmdarg_T *cap;
5087 if (cap->oap->op_type != OP_NOP)
5088 clearopbeep(cap->oap);
5090 /* Even if an operator was pending, we still want to scroll */
5091 gui_do_horiz_scroll();
5093 #endif
5095 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
5097 * Click in GUI tab.
5099 static void
5100 nv_tabline(cap)
5101 cmdarg_T *cap;
5103 if (cap->oap->op_type != OP_NOP)
5104 clearopbeep(cap->oap);
5106 /* Even if an operator was pending, we still want to jump tabs. */
5107 goto_tabpage(current_tab);
5111 * Selected item in tab line menu.
5113 static void
5114 nv_tabmenu(cap)
5115 cmdarg_T *cap;
5117 if (cap->oap->op_type != OP_NOP)
5118 clearopbeep(cap->oap);
5120 /* Even if an operator was pending, we still want to jump tabs. */
5121 handle_tabmenu();
5125 * Handle selecting an item of the GUI tab line menu.
5126 * Used in Normal and Insert mode.
5128 void
5129 handle_tabmenu()
5131 switch (current_tabmenu)
5133 case TABLINE_MENU_CLOSE:
5134 if (current_tab == 0)
5135 do_cmdline_cmd((char_u *)"tabclose");
5136 else
5138 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5139 current_tab);
5140 do_cmdline_cmd(IObuff);
5142 break;
5144 case TABLINE_MENU_NEW:
5145 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5146 current_tab > 0 ? current_tab - 1 : 999);
5147 do_cmdline_cmd(IObuff);
5148 break;
5150 case TABLINE_MENU_OPEN:
5151 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5152 current_tab > 0 ? current_tab - 1 : 999);
5153 do_cmdline_cmd(IObuff);
5154 break;
5157 #endif
5160 * "Q" command.
5162 static void
5163 nv_exmode(cap)
5164 cmdarg_T *cap;
5167 * Ignore 'Q' in Visual mode, just give a beep.
5169 #ifdef FEAT_VISUAL
5170 if (VIsual_active)
5171 vim_beep();
5172 else
5173 #endif
5174 if (!checkclearop(cap->oap))
5175 do_exmode(FALSE);
5179 * Handle a ":" command.
5181 static void
5182 nv_colon(cap)
5183 cmdarg_T *cap;
5185 int old_p_im;
5187 #ifdef FEAT_VISUAL
5188 if (VIsual_active)
5189 nv_operator(cap);
5190 else
5191 #endif
5193 if (cap->oap->op_type != OP_NOP)
5195 /* Using ":" as a movement is characterwise exclusive. */
5196 cap->oap->motion_type = MCHAR;
5197 cap->oap->inclusive = FALSE;
5199 else if (cap->count0)
5201 /* translate "count:" into ":.,.+(count - 1)" */
5202 stuffcharReadbuff('.');
5203 if (cap->count0 > 1)
5205 stuffReadbuff((char_u *)",.+");
5206 stuffnumReadbuff((long)cap->count0 - 1L);
5210 /* When typing, don't type below an old message */
5211 if (KeyTyped)
5212 compute_cmdrow();
5214 old_p_im = p_im;
5216 /* get a command line and execute it */
5217 do_cmdline(NULL, getexline, NULL,
5218 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5220 /* If 'insertmode' changed, enter or exit Insert mode */
5221 if (p_im != old_p_im)
5223 if (p_im)
5224 restart_edit = 'i';
5225 else
5226 restart_edit = 0;
5229 /* The start of the operator may have become invalid by the Ex
5230 * command. */
5231 if (cap->oap->op_type != OP_NOP
5232 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5233 || cap->oap->start.col >
5234 STRLEN(ml_get(cap->oap->start.lnum))))
5235 clearopbeep(cap->oap);
5240 * Handle CTRL-G command.
5242 static void
5243 nv_ctrlg(cap)
5244 cmdarg_T *cap;
5246 #ifdef FEAT_VISUAL
5247 if (VIsual_active) /* toggle Selection/Visual mode */
5249 VIsual_select = !VIsual_select;
5250 showmode();
5252 else
5253 #endif
5254 if (!checkclearop(cap->oap))
5255 /* print full name if count given or :cd used */
5256 fileinfo((int)cap->count0, FALSE, TRUE);
5260 * Handle CTRL-H <Backspace> command.
5262 static void
5263 nv_ctrlh(cap)
5264 cmdarg_T *cap;
5266 #ifdef FEAT_VISUAL
5267 if (VIsual_active && VIsual_select)
5269 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5270 v_visop(cap);
5272 else
5273 #endif
5274 nv_left(cap);
5278 * CTRL-L: clear screen and redraw.
5280 static void
5281 nv_clear(cap)
5282 cmdarg_T *cap;
5284 if (!checkclearop(cap->oap))
5286 #if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5288 * Right now, the BeBox doesn't seem to have an easy way to detect
5289 * window resizing, so we cheat and make the user detect it
5290 * manually with CTRL-L instead
5292 ui_get_shellsize();
5293 #endif
5294 #ifdef FEAT_SYN_HL
5295 /* Clear all syntax states to force resyncing. */
5296 syn_stack_free_all(curbuf);
5297 #endif
5298 redraw_later(CLEAR);
5303 * CTRL-O: In Select mode: switch to Visual mode for one command.
5304 * Otherwise: Go to older pcmark.
5306 static void
5307 nv_ctrlo(cap)
5308 cmdarg_T *cap;
5310 #ifdef FEAT_VISUAL
5311 if (VIsual_active && VIsual_select)
5313 VIsual_select = FALSE;
5314 showmode();
5315 restart_VIsual_select = 2; /* restart Select mode later */
5317 else
5318 #endif
5320 cap->count1 = -cap->count1;
5321 nv_pcmark(cap);
5326 * CTRL-^ command, short for ":e #"
5328 static void
5329 nv_hat(cap)
5330 cmdarg_T *cap;
5332 if (!checkclearopq(cap->oap))
5333 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5334 GETF_SETMARK|GETF_ALT, FALSE);
5338 * "Z" commands.
5340 static void
5341 nv_Zet(cap)
5342 cmdarg_T *cap;
5344 if (!checkclearopq(cap->oap))
5346 switch (cap->nchar)
5348 /* "ZZ": equivalent to ":x". */
5349 case 'Z': do_cmdline_cmd((char_u *)"x");
5350 break;
5352 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5353 case 'Q': do_cmdline_cmd((char_u *)"q!");
5354 break;
5356 default: clearopbeep(cap->oap);
5361 #if defined(FEAT_WINDOWS) || defined(PROTO)
5363 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5365 void
5366 do_nv_ident(c1, c2)
5367 int c1;
5368 int c2;
5370 oparg_T oa;
5371 cmdarg_T ca;
5373 clear_oparg(&oa);
5374 vim_memset(&ca, 0, sizeof(ca));
5375 ca.oap = &oa;
5376 ca.cmdchar = c1;
5377 ca.nchar = c2;
5378 nv_ident(&ca);
5380 #endif
5383 * Handle the commands that use the word under the cursor.
5384 * [g] CTRL-] :ta to current identifier
5385 * [g] 'K' run program for current identifier
5386 * [g] '*' / to current identifier or string
5387 * [g] '#' ? to current identifier or string
5388 * g ']' :tselect for current identifier
5390 static void
5391 nv_ident(cap)
5392 cmdarg_T *cap;
5394 char_u *ptr = NULL;
5395 char_u *buf;
5396 char_u *p;
5397 char_u *kp; /* value of 'keywordprg' */
5398 int kp_help; /* 'keywordprg' is ":help" */
5399 int n = 0; /* init for GCC */
5400 int cmdchar;
5401 int g_cmd; /* "g" command */
5402 char_u *aux_ptr;
5403 int isman;
5404 int isman_s;
5406 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5408 cmdchar = cap->nchar;
5409 g_cmd = TRUE;
5411 else
5413 cmdchar = cap->cmdchar;
5414 g_cmd = FALSE;
5417 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5418 cmdchar = '#';
5421 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5423 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5425 #ifdef FEAT_VISUAL
5426 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5427 return;
5428 #endif
5429 if (checkclearopq(cap->oap))
5430 return;
5433 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5434 (cmdchar == '*' || cmdchar == '#')
5435 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5437 clearop(cap->oap);
5438 return;
5441 /* Allocate buffer to put the command in. Inserting backslashes can
5442 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5443 * and some numbers. */
5444 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5445 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5446 || STRCMP(kp, ":help") == 0);
5447 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5448 if (buf == NULL)
5449 return;
5450 buf[0] = NUL;
5452 switch (cmdchar)
5454 case '*':
5455 case '#':
5457 * Put cursor at start of word, makes search skip the word
5458 * under the cursor.
5459 * Call setpcmark() first, so "*``" puts the cursor back where
5460 * it was.
5462 setpcmark();
5463 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5465 if (!g_cmd && vim_iswordp(ptr))
5466 STRCPY(buf, "\\<");
5467 no_smartcase = TRUE; /* don't use 'smartcase' now */
5468 break;
5470 case 'K':
5471 if (kp_help)
5472 STRCPY(buf, "he! ");
5473 else
5475 /* An external command will probably use an argument starting
5476 * with "-" as an option. To avoid trouble we skip the "-". */
5477 while (*ptr == '-' && n > 0)
5479 ++ptr;
5480 --n;
5482 if (n == 0)
5484 EMSG(_(e_noident)); /* found dashes only */
5485 vim_free(buf);
5486 return;
5489 /* When a count is given, turn it into a range. Is this
5490 * really what we want? */
5491 isman = (STRCMP(kp, "man") == 0);
5492 isman_s = (STRCMP(kp, "man -s") == 0);
5493 if (cap->count0 != 0 && !(isman || isman_s))
5494 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5496 STRCAT(buf, "! ");
5497 if (cap->count0 == 0 && isman_s)
5498 STRCAT(buf, "man");
5499 else
5500 STRCAT(buf, kp);
5501 STRCAT(buf, " ");
5502 if (cap->count0 != 0 && (isman || isman_s))
5504 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5505 STRCAT(buf, " ");
5508 break;
5510 case ']':
5511 #ifdef FEAT_CSCOPE
5512 if (p_cst)
5513 STRCPY(buf, "cstag ");
5514 else
5515 #endif
5516 STRCPY(buf, "ts ");
5517 break;
5519 default:
5520 if (curbuf->b_help)
5521 STRCPY(buf, "he! ");
5522 else if (g_cmd)
5523 STRCPY(buf, "tj ");
5524 else
5525 sprintf((char *)buf, "%ldta ", cap->count0);
5529 * Now grab the chars in the identifier
5531 if (cmdchar == 'K' && !kp_help)
5533 /* Escape the argument properly for a shell command */
5534 ptr = vim_strnsave(ptr, n);
5535 p = vim_strsave_shellescape(ptr, TRUE);
5536 vim_free(ptr);
5537 if (p == NULL)
5539 vim_free(buf);
5540 return;
5542 buf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5543 if (buf == NULL)
5545 vim_free(buf);
5546 vim_free(p);
5547 return;
5549 STRCAT(buf, p);
5550 vim_free(p);
5552 else
5554 if (cmdchar == '*')
5555 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5556 else if (cmdchar == '#')
5557 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
5558 else
5559 /* Don't escape spaces and Tabs in a tag with a backslash */
5560 aux_ptr = (char_u *)"\\|\"\n*?[";
5562 p = buf + STRLEN(buf);
5563 while (n-- > 0)
5565 /* put a backslash before \ and some others */
5566 if (vim_strchr(aux_ptr, *ptr) != NULL)
5567 *p++ = '\\';
5568 #ifdef FEAT_MBYTE
5569 /* When current byte is a part of multibyte character, copy all
5570 * bytes of that character. */
5571 if (has_mbyte)
5573 int i;
5574 int len = (*mb_ptr2len)(ptr) - 1;
5576 for (i = 0; i < len && n >= 1; ++i, --n)
5577 *p++ = *ptr++;
5579 #endif
5580 *p++ = *ptr++;
5582 *p = NUL;
5586 * Execute the command.
5588 if (cmdchar == '*' || cmdchar == '#')
5590 if (!g_cmd && (
5591 #ifdef FEAT_MBYTE
5592 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5593 #endif
5594 vim_iswordc(ptr[-1])))
5595 STRCAT(buf, "\\>");
5596 #ifdef FEAT_CMDHIST
5597 /* put pattern in search history */
5598 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5599 #endif
5600 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5602 else
5603 do_cmdline_cmd(buf);
5605 vim_free(buf);
5608 #if defined(FEAT_VISUAL) || defined(PROTO)
5610 * Get visually selected text, within one line only.
5611 * Returns FAIL if more than one line selected.
5614 get_visual_text(cap, pp, lenp)
5615 cmdarg_T *cap;
5616 char_u **pp; /* return: start of selected text */
5617 int *lenp; /* return: length of selected text */
5619 if (VIsual_mode != 'V')
5620 unadjust_for_sel();
5621 if (VIsual.lnum != curwin->w_cursor.lnum)
5623 if (cap != NULL)
5624 clearopbeep(cap->oap);
5625 return FAIL;
5627 if (VIsual_mode == 'V')
5629 *pp = ml_get_curline();
5630 *lenp = (int)STRLEN(*pp);
5632 else
5634 if (lt(curwin->w_cursor, VIsual))
5636 *pp = ml_get_pos(&curwin->w_cursor);
5637 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5639 else
5641 *pp = ml_get_pos(&VIsual);
5642 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5644 #ifdef FEAT_MBYTE
5645 if (has_mbyte)
5646 /* Correct the length to include the whole last character. */
5647 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
5648 #endif
5650 reset_VIsual_and_resel();
5651 return OK;
5653 #endif
5656 * CTRL-T: backwards in tag stack
5658 static void
5659 nv_tagpop(cap)
5660 cmdarg_T *cap;
5662 if (!checkclearopq(cap->oap))
5663 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5667 * Handle scrolling command 'H', 'L' and 'M'.
5669 static void
5670 nv_scroll(cap)
5671 cmdarg_T *cap;
5673 int used = 0;
5674 long n;
5675 #ifdef FEAT_FOLDING
5676 linenr_T lnum;
5677 #endif
5678 int half;
5680 cap->oap->motion_type = MLINE;
5681 setpcmark();
5683 if (cap->cmdchar == 'L')
5685 validate_botline(); /* make sure curwin->w_botline is valid */
5686 curwin->w_cursor.lnum = curwin->w_botline - 1;
5687 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5688 curwin->w_cursor.lnum = 1;
5689 else
5691 #ifdef FEAT_FOLDING
5692 if (hasAnyFolding(curwin))
5694 /* Count a fold for one screen line. */
5695 for (n = cap->count1 - 1; n > 0
5696 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5698 (void)hasFolding(curwin->w_cursor.lnum,
5699 &curwin->w_cursor.lnum, NULL);
5700 --curwin->w_cursor.lnum;
5703 else
5704 #endif
5705 curwin->w_cursor.lnum -= cap->count1 - 1;
5708 else
5710 if (cap->cmdchar == 'M')
5712 #ifdef FEAT_DIFF
5713 /* Don't count filler lines above the window. */
5714 used -= diff_check_fill(curwin, curwin->w_topline)
5715 - curwin->w_topfill;
5716 #endif
5717 validate_botline(); /* make sure w_empty_rows is valid */
5718 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5719 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5721 #ifdef FEAT_DIFF
5722 /* Count half he number of filler lines to be "below this
5723 * line" and half to be "above the next line". */
5724 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5725 + n) / 2 >= half)
5727 --n;
5728 break;
5730 #endif
5731 used += plines(curwin->w_topline + n);
5732 if (used >= half)
5733 break;
5734 #ifdef FEAT_FOLDING
5735 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5736 n = lnum - curwin->w_topline;
5737 #endif
5739 if (n > 0 && used > curwin->w_height)
5740 --n;
5742 else /* (cap->cmdchar == 'H') */
5744 n = cap->count1 - 1;
5745 #ifdef FEAT_FOLDING
5746 if (hasAnyFolding(curwin))
5748 /* Count a fold for one screen line. */
5749 lnum = curwin->w_topline;
5750 while (n-- > 0 && lnum < curwin->w_botline - 1)
5752 hasFolding(lnum, NULL, &lnum);
5753 ++lnum;
5755 n = lnum - curwin->w_topline;
5757 #endif
5759 curwin->w_cursor.lnum = curwin->w_topline + n;
5760 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5761 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5764 cursor_correct(); /* correct for 'so' */
5765 beginline(BL_SOL | BL_FIX);
5769 * Cursor right commands.
5771 static void
5772 nv_right(cap)
5773 cmdarg_T *cap;
5775 long n;
5776 #ifdef FEAT_VISUAL
5777 int PAST_LINE;
5778 #else
5779 # define PAST_LINE 0
5780 #endif
5782 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5784 /* <C-Right> and <S-Right> move a word or WORD right */
5785 if (mod_mask & MOD_MASK_CTRL)
5786 cap->arg = TRUE;
5787 nv_wordcmd(cap);
5788 return;
5791 cap->oap->motion_type = MCHAR;
5792 cap->oap->inclusive = FALSE;
5793 #ifdef FEAT_VISUAL
5794 PAST_LINE = (VIsual_active && *p_sel != 'o');
5796 # ifdef FEAT_VIRTUALEDIT
5798 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
5799 * (theoretically) infinitely long.
5801 if (virtual_active())
5802 PAST_LINE = 0;
5803 # endif
5804 #endif
5806 for (n = cap->count1; n > 0; --n)
5808 if ((!PAST_LINE && oneright() == FAIL)
5809 || (PAST_LINE && *ml_get_cursor() == NUL))
5812 * <Space> wraps to next line if 'whichwrap' has 's'.
5813 * 'l' wraps to next line if 'whichwrap' has 'l'.
5814 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
5816 if ( ((cap->cmdchar == ' '
5817 && vim_strchr(p_ww, 's') != NULL)
5818 || (cap->cmdchar == 'l'
5819 && vim_strchr(p_ww, 'l') != NULL)
5820 || (cap->cmdchar == K_RIGHT
5821 && vim_strchr(p_ww, '>') != NULL))
5822 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5824 /* When deleting we also count the NL as a character.
5825 * Set cap->oap->inclusive when last char in the line is
5826 * included, move to next line after that */
5827 if ( cap->oap->op_type != OP_NOP
5828 && !cap->oap->inclusive
5829 && !lineempty(curwin->w_cursor.lnum))
5830 cap->oap->inclusive = TRUE;
5831 else
5833 ++curwin->w_cursor.lnum;
5834 curwin->w_cursor.col = 0;
5835 #ifdef FEAT_VIRTUALEDIT
5836 curwin->w_cursor.coladd = 0;
5837 #endif
5838 curwin->w_set_curswant = TRUE;
5839 cap->oap->inclusive = FALSE;
5841 continue;
5843 if (cap->oap->op_type == OP_NOP)
5845 /* Only beep and flush if not moved at all */
5846 if (n == cap->count1)
5847 beep_flush();
5849 else
5851 if (!lineempty(curwin->w_cursor.lnum))
5852 cap->oap->inclusive = TRUE;
5854 break;
5856 #ifdef FEAT_VISUAL
5857 else if (PAST_LINE)
5859 curwin->w_set_curswant = TRUE;
5860 # ifdef FEAT_VIRTUALEDIT
5861 if (virtual_active())
5862 oneright();
5863 else
5864 # endif
5866 # ifdef FEAT_MBYTE
5867 if (has_mbyte)
5868 curwin->w_cursor.col +=
5869 (*mb_ptr2len)(ml_get_cursor());
5870 else
5871 # endif
5872 ++curwin->w_cursor.col;
5875 #endif
5877 #ifdef FEAT_FOLDING
5878 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5879 && cap->oap->op_type == OP_NOP)
5880 foldOpenCursor();
5881 #endif
5885 * Cursor left commands.
5887 * Returns TRUE when operator end should not be adjusted.
5889 static void
5890 nv_left(cap)
5891 cmdarg_T *cap;
5893 long n;
5895 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5897 /* <C-Left> and <S-Left> move a word or WORD left */
5898 if (mod_mask & MOD_MASK_CTRL)
5899 cap->arg = 1;
5900 nv_bck_word(cap);
5901 return;
5904 cap->oap->motion_type = MCHAR;
5905 cap->oap->inclusive = FALSE;
5906 for (n = cap->count1; n > 0; --n)
5908 if (oneleft() == FAIL)
5910 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
5911 * 'h' wraps to previous line if 'whichwrap' has 'h'.
5912 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
5914 if ( (((cap->cmdchar == K_BS
5915 || cap->cmdchar == Ctrl_H)
5916 && vim_strchr(p_ww, 'b') != NULL)
5917 || (cap->cmdchar == 'h'
5918 && vim_strchr(p_ww, 'h') != NULL)
5919 || (cap->cmdchar == K_LEFT
5920 && vim_strchr(p_ww, '<') != NULL))
5921 && curwin->w_cursor.lnum > 1)
5923 --(curwin->w_cursor.lnum);
5924 coladvance((colnr_T)MAXCOL);
5925 curwin->w_set_curswant = TRUE;
5927 /* When the NL before the first char has to be deleted we
5928 * put the cursor on the NUL after the previous line.
5929 * This is a very special case, be careful!
5930 * Don't adjust op_end now, otherwise it won't work. */
5931 if ( (cap->oap->op_type == OP_DELETE
5932 || cap->oap->op_type == OP_CHANGE)
5933 && !lineempty(curwin->w_cursor.lnum))
5935 if (*ml_get_cursor() != NUL)
5936 ++curwin->w_cursor.col;
5937 cap->retval |= CA_NO_ADJ_OP_END;
5939 continue;
5941 /* Only beep and flush if not moved at all */
5942 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
5943 beep_flush();
5944 break;
5947 #ifdef FEAT_FOLDING
5948 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5949 && cap->oap->op_type == OP_NOP)
5950 foldOpenCursor();
5951 #endif
5955 * Cursor up commands.
5956 * cap->arg is TRUE for "-": Move cursor to first non-blank.
5958 static void
5959 nv_up(cap)
5960 cmdarg_T *cap;
5962 if (mod_mask & MOD_MASK_SHIFT)
5964 /* <S-Up> is page up */
5965 cap->arg = BACKWARD;
5966 nv_page(cap);
5968 else
5970 cap->oap->motion_type = MLINE;
5971 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5972 clearopbeep(cap->oap);
5973 else if (cap->arg)
5974 beginline(BL_WHITE | BL_FIX);
5979 * Cursor down commands.
5980 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
5982 static void
5983 nv_down(cap)
5984 cmdarg_T *cap;
5986 if (mod_mask & MOD_MASK_SHIFT)
5988 /* <S-Down> is page down */
5989 cap->arg = FORWARD;
5990 nv_page(cap);
5992 else
5993 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5994 /* In a quickfix window a <CR> jumps to the error under the cursor. */
5995 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
5996 if (curwin->w_llist_ref == NULL)
5997 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
5998 else
5999 do_cmdline_cmd((char_u *)".ll"); /* location list window */
6000 else
6001 #endif
6003 #ifdef FEAT_CMDWIN
6004 /* In the cmdline window a <CR> executes the command. */
6005 if (cmdwin_type != 0 && cap->cmdchar == CAR)
6006 cmdwin_result = CAR;
6007 else
6008 #endif
6010 cap->oap->motion_type = MLINE;
6011 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6012 clearopbeep(cap->oap);
6013 else if (cap->arg)
6014 beginline(BL_WHITE | BL_FIX);
6019 #ifdef FEAT_SEARCHPATH
6021 * Grab the file name under the cursor and edit it.
6023 static void
6024 nv_gotofile(cap)
6025 cmdarg_T *cap;
6027 char_u *ptr;
6028 linenr_T lnum = -1;
6030 if (text_locked())
6032 clearopbeep(cap->oap);
6033 text_locked_msg();
6034 return;
6036 #ifdef FEAT_AUTOCMD
6037 if (curbuf_locked())
6039 clearop(cap->oap);
6040 return;
6042 #endif
6044 ptr = grab_file_name(cap->count1, &lnum);
6046 if (ptr != NULL)
6048 /* do autowrite if necessary */
6049 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6050 autowrite(curbuf, FALSE);
6051 setpcmark();
6052 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
6053 P_HID(curbuf) ? ECMD_HIDE : 0);
6054 if (cap->nchar == 'F' && lnum >= 0)
6056 curwin->w_cursor.lnum = lnum;
6057 check_cursor_lnum();
6058 beginline(BL_SOL | BL_FIX);
6060 vim_free(ptr);
6062 else
6063 clearop(cap->oap);
6065 #endif
6068 * <End> command: to end of current line or last line.
6070 static void
6071 nv_end(cap)
6072 cmdarg_T *cap;
6074 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
6076 cap->arg = TRUE;
6077 nv_goto(cap);
6078 cap->count1 = 1; /* to end of current line */
6080 nv_dollar(cap);
6084 * Handle the "$" command.
6086 static void
6087 nv_dollar(cap)
6088 cmdarg_T *cap;
6090 cap->oap->motion_type = MCHAR;
6091 cap->oap->inclusive = TRUE;
6092 #ifdef FEAT_VIRTUALEDIT
6093 /* In virtual mode when off the edge of a line and an operator
6094 * is pending (whew!) keep the cursor where it is.
6095 * Otherwise, send it to the end of the line. */
6096 if (!virtual_active() || gchar_cursor() != NUL
6097 || cap->oap->op_type == OP_NOP)
6098 #endif
6099 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6100 if (cursor_down((long)(cap->count1 - 1),
6101 cap->oap->op_type == OP_NOP) == FAIL)
6102 clearopbeep(cap->oap);
6103 #ifdef FEAT_FOLDING
6104 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6105 foldOpenCursor();
6106 #endif
6110 * Implementation of '?' and '/' commands.
6111 * If cap->arg is TRUE don't set PC mark.
6113 static void
6114 nv_search(cap)
6115 cmdarg_T *cap;
6117 oparg_T *oap = cap->oap;
6119 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6121 /* Translate "g??" to "g?g?" */
6122 cap->cmdchar = 'g';
6123 cap->nchar = '?';
6124 nv_operator(cap);
6125 return;
6128 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6130 if (cap->searchbuf == NULL)
6132 clearop(oap);
6133 return;
6136 normal_search(cap, cap->cmdchar, cap->searchbuf,
6137 (cap->arg ? 0 : SEARCH_MARK));
6141 * Handle "N" and "n" commands.
6142 * cap->arg is SEARCH_REV for "N", 0 for "n".
6144 static void
6145 nv_next(cap)
6146 cmdarg_T *cap;
6148 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6152 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6153 * Uses only cap->count1 and cap->oap from "cap".
6155 static void
6156 normal_search(cap, dir, pat, opt)
6157 cmdarg_T *cap;
6158 int dir;
6159 char_u *pat;
6160 int opt; /* extra flags for do_search() */
6162 int i;
6164 cap->oap->motion_type = MCHAR;
6165 cap->oap->inclusive = FALSE;
6166 cap->oap->use_reg_one = TRUE;
6167 curwin->w_set_curswant = TRUE;
6169 i = do_search(cap->oap, dir, pat, cap->count1,
6170 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
6171 if (i == 0)
6172 clearop(cap->oap);
6173 else
6175 if (i == 2)
6176 cap->oap->motion_type = MLINE;
6177 #ifdef FEAT_VIRTUALEDIT
6178 curwin->w_cursor.coladd = 0;
6179 #endif
6180 #ifdef FEAT_FOLDING
6181 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6182 foldOpenCursor();
6183 #endif
6186 /* "/$" will put the cursor after the end of the line, may need to
6187 * correct that here */
6188 check_cursor();
6192 * Character search commands.
6193 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6194 * ',' and FALSE for ';'.
6195 * cap->nchar is NUL for ',' and ';' (repeat the search)
6197 static void
6198 nv_csearch(cap)
6199 cmdarg_T *cap;
6201 int t_cmd;
6203 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6204 t_cmd = TRUE;
6205 else
6206 t_cmd = FALSE;
6208 cap->oap->motion_type = MCHAR;
6209 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6210 clearopbeep(cap->oap);
6211 else
6213 curwin->w_set_curswant = TRUE;
6214 #ifdef FEAT_VIRTUALEDIT
6215 /* Include a Tab for "tx" and for "dfx". */
6216 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6217 && (t_cmd || cap->oap->op_type != OP_NOP))
6219 colnr_T scol, ecol;
6221 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6222 curwin->w_cursor.coladd = ecol - scol;
6224 else
6225 curwin->w_cursor.coladd = 0;
6226 #endif
6227 #ifdef FEAT_VISUAL
6228 adjust_for_sel(cap);
6229 #endif
6230 #ifdef FEAT_FOLDING
6231 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6232 foldOpenCursor();
6233 #endif
6238 * "[" and "]" commands.
6239 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6241 static void
6242 nv_brackets(cap)
6243 cmdarg_T *cap;
6245 pos_T new_pos;
6246 pos_T prev_pos;
6247 pos_T *pos = NULL; /* init for GCC */
6248 pos_T old_pos; /* cursor position before command */
6249 int flag;
6250 long n;
6251 int findc;
6252 int c;
6254 cap->oap->motion_type = MCHAR;
6255 cap->oap->inclusive = FALSE;
6256 old_pos = curwin->w_cursor;
6257 #ifdef FEAT_VIRTUALEDIT
6258 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6259 #endif
6261 #ifdef FEAT_SEARCHPATH
6263 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6265 if (cap->nchar == 'f')
6266 nv_gotofile(cap);
6267 else
6268 #endif
6270 #ifdef FEAT_FIND_ID
6272 * Find the occurrence(s) of the identifier or define under cursor
6273 * in current and included files or jump to the first occurrence.
6275 * search list jump
6276 * fwd bwd fwd bwd fwd bwd
6277 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6278 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6280 if (vim_strchr((char_u *)
6281 #ifdef EBCDIC
6282 "iI\005dD\067",
6283 #else
6284 "iI\011dD\004",
6285 #endif
6286 cap->nchar) != NULL)
6288 char_u *ptr;
6289 int len;
6291 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6292 clearop(cap->oap);
6293 else
6295 find_pattern_in_path(ptr, 0, len, TRUE,
6296 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6297 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6298 cap->count1,
6299 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6300 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6301 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6302 (linenr_T)MAXLNUM);
6303 curwin->w_set_curswant = TRUE;
6306 else
6307 #endif
6310 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6311 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6312 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6313 * "[m" or "]m" search for prev/next start of (Java) method.
6314 * "[M" or "]M" search for prev/next end of (Java) method.
6316 if ( (cap->cmdchar == '['
6317 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6318 || (cap->cmdchar == ']'
6319 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6321 if (cap->nchar == '*')
6322 cap->nchar = '/';
6323 new_pos.lnum = 0;
6324 prev_pos.lnum = 0;
6325 if (cap->nchar == 'm' || cap->nchar == 'M')
6327 if (cap->cmdchar == '[')
6328 findc = '{';
6329 else
6330 findc = '}';
6331 n = 9999;
6333 else
6335 findc = cap->nchar;
6336 n = cap->count1;
6338 for ( ; n > 0; --n)
6340 if ((pos = findmatchlimit(cap->oap, findc,
6341 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6343 if (new_pos.lnum == 0) /* nothing found */
6345 if (cap->nchar != 'm' && cap->nchar != 'M')
6346 clearopbeep(cap->oap);
6348 else
6349 pos = &new_pos; /* use last one found */
6350 break;
6352 prev_pos = new_pos;
6353 curwin->w_cursor = *pos;
6354 new_pos = *pos;
6356 curwin->w_cursor = old_pos;
6359 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6360 * brought us to the match for "[m" and "]M" when inside a method.
6361 * Try finding the '{' or '}' we want to be at.
6362 * Also repeat for the given count.
6364 if (cap->nchar == 'm' || cap->nchar == 'M')
6366 /* norm is TRUE for "]M" and "[m" */
6367 int norm = ((findc == '{') == (cap->nchar == 'm'));
6369 n = cap->count1;
6370 /* found a match: we were inside a method */
6371 if (prev_pos.lnum != 0)
6373 pos = &prev_pos;
6374 curwin->w_cursor = prev_pos;
6375 if (norm)
6376 --n;
6378 else
6379 pos = NULL;
6380 while (n > 0)
6382 for (;;)
6384 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6386 /* if not found anything, that's an error */
6387 if (pos == NULL)
6388 clearopbeep(cap->oap);
6389 n = 0;
6390 break;
6392 c = gchar_cursor();
6393 if (c == '{' || c == '}')
6395 /* Must have found end/start of class: use it.
6396 * Or found the place to be at. */
6397 if ((c == findc && norm) || (n == 1 && !norm))
6399 new_pos = curwin->w_cursor;
6400 pos = &new_pos;
6401 n = 0;
6403 /* if no match found at all, we started outside of the
6404 * class and we're inside now. Just go on. */
6405 else if (new_pos.lnum == 0)
6407 new_pos = curwin->w_cursor;
6408 pos = &new_pos;
6410 /* found start/end of other method: go to match */
6411 else if ((pos = findmatchlimit(cap->oap, findc,
6412 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6413 0)) == NULL)
6414 n = 0;
6415 else
6416 curwin->w_cursor = *pos;
6417 break;
6420 --n;
6422 curwin->w_cursor = old_pos;
6423 if (pos == NULL && new_pos.lnum != 0)
6424 clearopbeep(cap->oap);
6426 if (pos != NULL)
6428 setpcmark();
6429 curwin->w_cursor = *pos;
6430 curwin->w_set_curswant = TRUE;
6431 #ifdef FEAT_FOLDING
6432 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6433 && cap->oap->op_type == OP_NOP)
6434 foldOpenCursor();
6435 #endif
6440 * "[[", "[]", "]]" and "][": move to start or end of function
6442 else if (cap->nchar == '[' || cap->nchar == ']')
6444 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6445 flag = '{';
6446 else
6447 flag = '}'; /* "][" or "[]" */
6449 curwin->w_set_curswant = TRUE;
6451 * Imitate strange Vi behaviour: When using "]]" with an operator
6452 * we also stop at '}'.
6454 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
6455 (cap->oap->op_type != OP_NOP
6456 && cap->arg == FORWARD && flag == '{')))
6457 clearopbeep(cap->oap);
6458 else
6460 if (cap->oap->op_type == OP_NOP)
6461 beginline(BL_WHITE | BL_FIX);
6462 #ifdef FEAT_FOLDING
6463 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6464 foldOpenCursor();
6465 #endif
6470 * "[p", "[P", "]P" and "]p": put with indent adjustment
6472 else if (cap->nchar == 'p' || cap->nchar == 'P')
6474 if (!checkclearop(cap->oap))
6476 prep_redo_cmd(cap);
6477 do_put(cap->oap->regname,
6478 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6479 cap->count1, PUT_FIXINDENT);
6484 * "['", "[`", "]'" and "]`": jump to next mark
6486 else if (cap->nchar == '\'' || cap->nchar == '`')
6488 pos = &curwin->w_cursor;
6489 for (n = cap->count1; n > 0; --n)
6491 prev_pos = *pos;
6492 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6493 cap->nchar == '\'');
6494 if (pos == NULL)
6495 break;
6497 if (pos == NULL)
6498 pos = &prev_pos;
6499 nv_cursormark(cap, cap->nchar == '\'', pos);
6502 #ifdef FEAT_MOUSE
6504 * [ or ] followed by a middle mouse click: put selected text with
6505 * indent adjustment. Any other button just does as usual.
6507 else if (cap->nchar >= K_LEFTMOUSE && cap->nchar <= K_RIGHTRELEASE)
6509 (void)do_mouse(cap->oap, cap->nchar,
6510 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6511 cap->count1, PUT_FIXINDENT);
6513 #endif /* FEAT_MOUSE */
6515 #ifdef FEAT_FOLDING
6517 * "[z" and "]z": move to start or end of open fold.
6519 else if (cap->nchar == 'z')
6521 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6522 cap->count1) == FAIL)
6523 clearopbeep(cap->oap);
6525 #endif
6527 #ifdef FEAT_DIFF
6529 * "[c" and "]c": move to next or previous diff-change.
6531 else if (cap->nchar == 'c')
6533 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6534 cap->count1) == FAIL)
6535 clearopbeep(cap->oap);
6537 #endif
6539 #ifdef FEAT_SPELL
6541 * "[s", "[S", "]s" and "]S": move to next spell error.
6543 else if (cap->nchar == 's' || cap->nchar == 'S')
6545 setpcmark();
6546 for (n = 0; n < cap->count1; ++n)
6547 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6548 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
6550 clearopbeep(cap->oap);
6551 break;
6553 # ifdef FEAT_FOLDING
6554 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6555 foldOpenCursor();
6556 # endif
6558 #endif
6560 /* Not a valid cap->nchar. */
6561 else
6562 clearopbeep(cap->oap);
6566 * Handle Normal mode "%" command.
6568 static void
6569 nv_percent(cap)
6570 cmdarg_T *cap;
6572 pos_T *pos;
6573 #ifdef FEAT_FOLDING
6574 linenr_T lnum = curwin->w_cursor.lnum;
6575 #endif
6577 cap->oap->inclusive = TRUE;
6578 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6580 if (cap->count0 > 100)
6581 clearopbeep(cap->oap);
6582 else
6584 cap->oap->motion_type = MLINE;
6585 setpcmark();
6586 /* Round up, so CTRL-G will give same value. Watch out for a
6587 * large line count, the line number must not go negative! */
6588 if (curbuf->b_ml.ml_line_count > 1000000)
6589 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6590 / 100L * cap->count0;
6591 else
6592 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6593 cap->count0 + 99L) / 100L;
6594 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6595 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6596 beginline(BL_SOL | BL_FIX);
6599 else /* "%" : go to matching paren */
6601 cap->oap->motion_type = MCHAR;
6602 cap->oap->use_reg_one = TRUE;
6603 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6604 clearopbeep(cap->oap);
6605 else
6607 setpcmark();
6608 curwin->w_cursor = *pos;
6609 curwin->w_set_curswant = TRUE;
6610 #ifdef FEAT_VIRTUALEDIT
6611 curwin->w_cursor.coladd = 0;
6612 #endif
6613 #ifdef FEAT_VISUAL
6614 adjust_for_sel(cap);
6615 #endif
6618 #ifdef FEAT_FOLDING
6619 if (cap->oap->op_type == OP_NOP
6620 && lnum != curwin->w_cursor.lnum
6621 && (fdo_flags & FDO_PERCENT)
6622 && KeyTyped)
6623 foldOpenCursor();
6624 #endif
6628 * Handle "(" and ")" commands.
6629 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6631 static void
6632 nv_brace(cap)
6633 cmdarg_T *cap;
6635 cap->oap->motion_type = MCHAR;
6636 cap->oap->use_reg_one = TRUE;
6637 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6638 cap->oap->inclusive = FALSE;
6639 curwin->w_set_curswant = TRUE;
6641 if (findsent(cap->arg, cap->count1) == FAIL)
6642 clearopbeep(cap->oap);
6643 else
6645 /* Don't leave the cursor on the NUL past end of line. */
6646 adjust_cursor(cap->oap);
6647 #ifdef FEAT_VIRTUALEDIT
6648 curwin->w_cursor.coladd = 0;
6649 #endif
6650 #ifdef FEAT_FOLDING
6651 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6652 foldOpenCursor();
6653 #endif
6658 * "m" command: Mark a position.
6660 static void
6661 nv_mark(cap)
6662 cmdarg_T *cap;
6664 if (!checkclearop(cap->oap))
6666 if (setmark(cap->nchar) == FAIL)
6667 clearopbeep(cap->oap);
6672 * "{" and "}" commands.
6673 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6675 static void
6676 nv_findpar(cap)
6677 cmdarg_T *cap;
6679 cap->oap->motion_type = MCHAR;
6680 cap->oap->inclusive = FALSE;
6681 cap->oap->use_reg_one = TRUE;
6682 curwin->w_set_curswant = TRUE;
6683 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
6684 clearopbeep(cap->oap);
6685 else
6687 #ifdef FEAT_VIRTUALEDIT
6688 curwin->w_cursor.coladd = 0;
6689 #endif
6690 #ifdef FEAT_FOLDING
6691 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6692 foldOpenCursor();
6693 #endif
6698 * "u" command: Undo or make lower case.
6700 static void
6701 nv_undo(cap)
6702 cmdarg_T *cap;
6704 if (cap->oap->op_type == OP_LOWER
6705 #ifdef FEAT_VISUAL
6706 || VIsual_active
6707 #endif
6710 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6711 cap->cmdchar = 'g';
6712 cap->nchar = 'u';
6713 nv_operator(cap);
6715 else
6716 nv_kundo(cap);
6720 * <Undo> command.
6722 static void
6723 nv_kundo(cap)
6724 cmdarg_T *cap;
6726 if (!checkclearopq(cap->oap))
6728 u_undo((int)cap->count1);
6729 curwin->w_set_curswant = TRUE;
6734 * Handle the "r" command.
6736 static void
6737 nv_replace(cap)
6738 cmdarg_T *cap;
6740 char_u *ptr;
6741 int had_ctrl_v;
6742 long n;
6744 if (checkclearop(cap->oap))
6745 return;
6747 /* get another character */
6748 if (cap->nchar == Ctrl_V)
6750 had_ctrl_v = Ctrl_V;
6751 cap->nchar = get_literal();
6752 /* Don't redo a multibyte character with CTRL-V. */
6753 if (cap->nchar > DEL)
6754 had_ctrl_v = NUL;
6756 else
6757 had_ctrl_v = NUL;
6759 /* Abort if the character is a special key. */
6760 if (IS_SPECIAL(cap->nchar))
6762 clearopbeep(cap->oap);
6763 return;
6766 #ifdef FEAT_VISUAL
6767 /* Visual mode "r" */
6768 if (VIsual_active)
6770 nv_operator(cap);
6771 return;
6773 #endif
6775 #ifdef FEAT_VIRTUALEDIT
6776 /* Break tabs, etc. */
6777 if (virtual_active())
6779 if (u_save_cursor() == FAIL)
6780 return;
6781 if (gchar_cursor() == NUL)
6783 /* Add extra space and put the cursor on the first one. */
6784 coladvance_force((colnr_T)(getviscol() + cap->count1));
6785 curwin->w_cursor.col -= cap->count1;
6787 else if (gchar_cursor() == TAB)
6788 coladvance_force(getviscol());
6790 #endif
6792 /* Abort if not enough characters to replace. */
6793 ptr = ml_get_cursor();
6794 if (STRLEN(ptr) < (unsigned)cap->count1
6795 #ifdef FEAT_MBYTE
6796 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6797 #endif
6800 clearopbeep(cap->oap);
6801 return;
6805 * Replacing with a TAB is done by edit() when it is complicated because
6806 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6807 * Other characters are done below to avoid problems with things like
6808 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6810 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6812 stuffnumReadbuff(cap->count1);
6813 stuffcharReadbuff('R');
6814 stuffcharReadbuff('\t');
6815 stuffcharReadbuff(ESC);
6816 return;
6819 /* save line for undo */
6820 if (u_save_cursor() == FAIL)
6821 return;
6823 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6826 * Replace character(s) by a single newline.
6827 * Strange vi behaviour: Only one newline is inserted.
6828 * Delete the characters here.
6829 * Insert the newline with an insert command, takes care of
6830 * autoindent. The insert command depends on being on the last
6831 * character of a line or not.
6833 #ifdef FEAT_MBYTE
6834 (void)del_chars(cap->count1, FALSE); /* delete the characters */
6835 #else
6836 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
6837 #endif
6838 stuffcharReadbuff('\r');
6839 stuffcharReadbuff(ESC);
6841 /* Give 'r' to edit(), to get the redo command right. */
6842 invoke_edit(cap, TRUE, 'r', FALSE);
6844 else
6846 prep_redo(cap->oap->regname, cap->count1,
6847 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
6849 curbuf->b_op_start = curwin->w_cursor;
6850 #ifdef FEAT_MBYTE
6851 if (has_mbyte)
6853 int old_State = State;
6855 if (cap->ncharC1 != 0)
6856 AppendCharToRedobuff(cap->ncharC1);
6857 if (cap->ncharC2 != 0)
6858 AppendCharToRedobuff(cap->ncharC2);
6860 /* This is slow, but it handles replacing a single-byte with a
6861 * multi-byte and the other way around. Also handles adding
6862 * composing characters for utf-8. */
6863 for (n = cap->count1; n > 0; --n)
6865 State = REPLACE;
6866 ins_char(cap->nchar);
6867 State = old_State;
6868 if (cap->ncharC1 != 0)
6869 ins_char(cap->ncharC1);
6870 if (cap->ncharC2 != 0)
6871 ins_char(cap->ncharC2);
6874 else
6875 #endif
6878 * Replace the characters within one line.
6880 for (n = cap->count1; n > 0; --n)
6883 * Get ptr again, because u_save and/or showmatch() will have
6884 * released the line. At the same time we let know that the
6885 * line will be changed.
6887 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
6888 ptr[curwin->w_cursor.col] = cap->nchar;
6889 if (p_sm && msg_silent == 0)
6890 showmatch(cap->nchar);
6891 ++curwin->w_cursor.col;
6893 #ifdef FEAT_NETBEANS_INTG
6894 if (usingNetbeans)
6896 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
6898 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
6899 (long)cap->count1);
6900 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
6901 &ptr[start], (int)cap->count1);
6903 #endif
6905 /* mark the buffer as changed and prepare for displaying */
6906 changed_bytes(curwin->w_cursor.lnum,
6907 (colnr_T)(curwin->w_cursor.col - cap->count1));
6909 --curwin->w_cursor.col; /* cursor on the last replaced char */
6910 #ifdef FEAT_MBYTE
6911 /* if the character on the left of the current cursor is a multi-byte
6912 * character, move two characters left */
6913 if (has_mbyte)
6914 mb_adjust_cursor();
6915 #endif
6916 curbuf->b_op_end = curwin->w_cursor;
6917 curwin->w_set_curswant = TRUE;
6918 set_last_insert(cap->nchar);
6922 #ifdef FEAT_VISUAL
6924 * 'o': Exchange start and end of Visual area.
6925 * 'O': same, but in block mode exchange left and right corners.
6927 static void
6928 v_swap_corners(cmdchar)
6929 int cmdchar;
6931 pos_T old_cursor;
6932 colnr_T left, right;
6934 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
6936 old_cursor = curwin->w_cursor;
6937 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
6938 curwin->w_cursor.lnum = VIsual.lnum;
6939 coladvance(left);
6940 VIsual = curwin->w_cursor;
6942 curwin->w_cursor.lnum = old_cursor.lnum;
6943 curwin->w_curswant = right;
6944 /* 'selection "exclusive" and cursor at right-bottom corner: move it
6945 * right one column */
6946 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
6947 ++curwin->w_curswant;
6948 coladvance(curwin->w_curswant);
6949 if (curwin->w_cursor.col == old_cursor.col
6950 #ifdef FEAT_VIRTUALEDIT
6951 && (!virtual_active()
6952 || curwin->w_cursor.coladd == old_cursor.coladd)
6953 #endif
6956 curwin->w_cursor.lnum = VIsual.lnum;
6957 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
6958 ++right;
6959 coladvance(right);
6960 VIsual = curwin->w_cursor;
6962 curwin->w_cursor.lnum = old_cursor.lnum;
6963 coladvance(left);
6964 curwin->w_curswant = left;
6967 else
6969 old_cursor = curwin->w_cursor;
6970 curwin->w_cursor = VIsual;
6971 VIsual = old_cursor;
6972 curwin->w_set_curswant = TRUE;
6975 #endif /* FEAT_VISUAL */
6978 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
6980 static void
6981 nv_Replace(cap)
6982 cmdarg_T *cap;
6984 #ifdef FEAT_VISUAL
6985 if (VIsual_active) /* "R" is replace lines */
6987 cap->cmdchar = 'c';
6988 cap->nchar = NUL;
6989 VIsual_mode = 'V';
6990 nv_operator(cap);
6992 else
6993 #endif
6994 if (!checkclearopq(cap->oap))
6996 if (!curbuf->b_p_ma)
6997 EMSG(_(e_modifiable));
6998 else
7000 #ifdef FEAT_VIRTUALEDIT
7001 if (virtual_active())
7002 coladvance(getviscol());
7003 #endif
7004 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7009 #ifdef FEAT_VREPLACE
7011 * "gr".
7013 static void
7014 nv_vreplace(cap)
7015 cmdarg_T *cap;
7017 # ifdef FEAT_VISUAL
7018 if (VIsual_active)
7020 cap->cmdchar = 'r';
7021 cap->nchar = cap->extra_char;
7022 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7024 else
7025 # endif
7026 if (!checkclearopq(cap->oap))
7028 if (!curbuf->b_p_ma)
7029 EMSG(_(e_modifiable));
7030 else
7032 if (cap->extra_char == Ctrl_V) /* get another character */
7033 cap->extra_char = get_literal();
7034 stuffcharReadbuff(cap->extra_char);
7035 stuffcharReadbuff(ESC);
7036 # ifdef FEAT_VIRTUALEDIT
7037 if (virtual_active())
7038 coladvance(getviscol());
7039 # endif
7040 invoke_edit(cap, TRUE, 'v', FALSE);
7044 #endif
7047 * Swap case for "~" command, when it does not work like an operator.
7049 static void
7050 n_swapchar(cap)
7051 cmdarg_T *cap;
7053 long n;
7054 pos_T startpos;
7055 int did_change = 0;
7056 #ifdef FEAT_NETBEANS_INTG
7057 pos_T pos;
7058 char_u *ptr;
7059 int count;
7060 #endif
7062 if (checkclearopq(cap->oap))
7063 return;
7065 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7067 clearopbeep(cap->oap);
7068 return;
7071 prep_redo_cmd(cap);
7073 if (u_save_cursor() == FAIL)
7074 return;
7076 startpos = curwin->w_cursor;
7077 #ifdef FEAT_NETBEANS_INTG
7078 pos = startpos;
7079 #endif
7080 for (n = cap->count1; n > 0; --n)
7082 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7083 inc_cursor();
7084 if (gchar_cursor() == NUL)
7086 if (vim_strchr(p_ww, '~') != NULL
7087 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7089 #ifdef FEAT_NETBEANS_INTG
7090 if (usingNetbeans)
7092 if (did_change)
7094 ptr = ml_get(pos.lnum);
7095 count = (int)STRLEN(ptr) - pos.col;
7096 netbeans_removed(curbuf, pos.lnum, pos.col,
7097 (long)count);
7098 netbeans_inserted(curbuf, pos.lnum, pos.col,
7099 &ptr[pos.col], count);
7101 pos.col = 0;
7102 pos.lnum++;
7104 #endif
7105 ++curwin->w_cursor.lnum;
7106 curwin->w_cursor.col = 0;
7107 if (n > 1)
7109 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7110 break;
7111 u_clearline();
7114 else
7115 break;
7118 #ifdef FEAT_NETBEANS_INTG
7119 if (did_change && usingNetbeans)
7121 ptr = ml_get(pos.lnum);
7122 count = curwin->w_cursor.col - pos.col;
7123 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7124 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
7126 #endif
7129 check_cursor();
7130 curwin->w_set_curswant = TRUE;
7131 if (did_change)
7133 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7134 0L);
7135 curbuf->b_op_start = startpos;
7136 curbuf->b_op_end = curwin->w_cursor;
7137 if (curbuf->b_op_end.col > 0)
7138 --curbuf->b_op_end.col;
7143 * Move cursor to mark.
7145 static void
7146 nv_cursormark(cap, flag, pos)
7147 cmdarg_T *cap;
7148 int flag;
7149 pos_T *pos;
7151 if (check_mark(pos) == FAIL)
7152 clearop(cap->oap);
7153 else
7155 if (cap->cmdchar == '\''
7156 || cap->cmdchar == '`'
7157 || cap->cmdchar == '['
7158 || cap->cmdchar == ']')
7159 setpcmark();
7160 curwin->w_cursor = *pos;
7161 if (flag)
7162 beginline(BL_WHITE | BL_FIX);
7163 else
7164 check_cursor();
7166 cap->oap->motion_type = flag ? MLINE : MCHAR;
7167 if (cap->cmdchar == '`')
7168 cap->oap->use_reg_one = TRUE;
7169 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7170 curwin->w_set_curswant = TRUE;
7173 #ifdef FEAT_VISUAL
7175 * Handle commands that are operators in Visual mode.
7177 static void
7178 v_visop(cap)
7179 cmdarg_T *cap;
7181 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7183 /* Uppercase means linewise, except in block mode, then "D" deletes till
7184 * the end of the line, and "C" replaces til EOL */
7185 if (isupper(cap->cmdchar))
7187 if (VIsual_mode != Ctrl_V)
7188 VIsual_mode = 'V';
7189 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7190 curwin->w_curswant = MAXCOL;
7192 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7193 nv_operator(cap);
7195 #endif
7198 * "s" and "S" commands.
7200 static void
7201 nv_subst(cap)
7202 cmdarg_T *cap;
7204 #ifdef FEAT_VISUAL
7205 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7207 if (cap->cmdchar == 'S')
7208 VIsual_mode = 'V';
7209 cap->cmdchar = 'c';
7210 nv_operator(cap);
7212 else
7213 #endif
7214 nv_optrans(cap);
7218 * Abbreviated commands.
7220 static void
7221 nv_abbrev(cap)
7222 cmdarg_T *cap;
7224 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7225 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7227 #ifdef FEAT_VISUAL
7228 /* in Visual mode these commands are operators */
7229 if (VIsual_active)
7230 v_visop(cap);
7231 else
7232 #endif
7233 nv_optrans(cap);
7237 * Translate a command into another command.
7239 static void
7240 nv_optrans(cap)
7241 cmdarg_T *cap;
7243 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7244 (char_u *)"d$", (char_u *)"c$",
7245 (char_u *)"cl", (char_u *)"cc",
7246 (char_u *)"yy", (char_u *)":s\r"};
7247 static char_u *str = (char_u *)"xXDCsSY&";
7249 if (!checkclearopq(cap->oap))
7251 /* In Vi "2D" doesn't delete the next line. Can't translate it
7252 * either, because "2." should also not use the count. */
7253 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7255 cap->oap->start = curwin->w_cursor;
7256 cap->oap->op_type = OP_DELETE;
7257 #ifdef FEAT_EVAL
7258 set_op_var(OP_DELETE);
7259 #endif
7260 cap->count1 = 1;
7261 nv_dollar(cap);
7262 finish_op = TRUE;
7263 ResetRedobuff();
7264 AppendCharToRedobuff('D');
7266 else
7268 if (cap->count0)
7269 stuffnumReadbuff(cap->count0);
7270 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7273 cap->opcount = 0;
7277 * "'" and "`" commands. Also for "g'" and "g`".
7278 * cap->arg is TRUE for "'" and "g'".
7280 static void
7281 nv_gomark(cap)
7282 cmdarg_T *cap;
7284 pos_T *pos;
7285 int c;
7286 #ifdef FEAT_FOLDING
7287 linenr_T lnum = curwin->w_cursor.lnum;
7288 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7289 #endif
7291 if (cap->cmdchar == 'g')
7292 c = cap->extra_char;
7293 else
7294 c = cap->nchar;
7295 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7296 if (pos == (pos_T *)-1) /* jumped to other file */
7298 if (cap->arg)
7300 check_cursor_lnum();
7301 beginline(BL_WHITE | BL_FIX);
7303 else
7304 check_cursor();
7306 else
7307 nv_cursormark(cap, cap->arg, pos);
7309 #ifdef FEAT_VIRTUALEDIT
7310 /* May need to clear the coladd that a mark includes. */
7311 if (!virtual_active())
7312 curwin->w_cursor.coladd = 0;
7313 #endif
7314 #ifdef FEAT_FOLDING
7315 if (cap->oap->op_type == OP_NOP
7316 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7317 && (fdo_flags & FDO_MARK)
7318 && old_KeyTyped)
7319 foldOpenCursor();
7320 #endif
7324 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7326 static void
7327 nv_pcmark(cap)
7328 cmdarg_T *cap;
7330 #ifdef FEAT_JUMPLIST
7331 pos_T *pos;
7332 # ifdef FEAT_FOLDING
7333 linenr_T lnum = curwin->w_cursor.lnum;
7334 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7335 # endif
7337 if (!checkclearopq(cap->oap))
7339 if (cap->cmdchar == 'g')
7340 pos = movechangelist((int)cap->count1);
7341 else
7342 pos = movemark((int)cap->count1);
7343 if (pos == (pos_T *)-1) /* jump to other file */
7345 curwin->w_set_curswant = TRUE;
7346 check_cursor();
7348 else if (pos != NULL) /* can jump */
7349 nv_cursormark(cap, FALSE, pos);
7350 else if (cap->cmdchar == 'g')
7352 if (curbuf->b_changelistlen == 0)
7353 EMSG(_("E664: changelist is empty"));
7354 else if (cap->count1 < 0)
7355 EMSG(_("E662: At start of changelist"));
7356 else
7357 EMSG(_("E663: At end of changelist"));
7359 else
7360 clearopbeep(cap->oap);
7361 # ifdef FEAT_FOLDING
7362 if (cap->oap->op_type == OP_NOP
7363 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7364 && (fdo_flags & FDO_MARK)
7365 && old_KeyTyped)
7366 foldOpenCursor();
7367 # endif
7369 #else
7370 clearopbeep(cap->oap);
7371 #endif
7375 * Handle '"' command.
7377 static void
7378 nv_regname(cap)
7379 cmdarg_T *cap;
7381 if (checkclearop(cap->oap))
7382 return;
7383 #ifdef FEAT_EVAL
7384 if (cap->nchar == '=')
7385 cap->nchar = get_expr_register();
7386 #endif
7387 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7389 cap->oap->regname = cap->nchar;
7390 cap->opcount = cap->count0; /* remember count before '"' */
7391 #ifdef FEAT_EVAL
7392 set_reg_var(cap->oap->regname);
7393 #endif
7395 else
7396 clearopbeep(cap->oap);
7399 #ifdef FEAT_VISUAL
7401 * Handle "v", "V" and "CTRL-V" commands.
7402 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7403 * is TRUE.
7404 * Handle CTRL-Q just like CTRL-V.
7406 static void
7407 nv_visual(cap)
7408 cmdarg_T *cap;
7410 if (cap->cmdchar == Ctrl_Q)
7411 cap->cmdchar = Ctrl_V;
7413 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7414 * characterwise, linewise, or blockwise. */
7415 if (cap->oap->op_type != OP_NOP)
7417 cap->oap->motion_force = cap->cmdchar;
7418 finish_op = FALSE; /* operator doesn't finish now but later */
7419 return;
7422 VIsual_select = cap->arg;
7423 if (VIsual_active) /* change Visual mode */
7425 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7426 end_visual_mode();
7427 else /* toggle char/block mode */
7428 { /* or char/line mode */
7429 VIsual_mode = cap->cmdchar;
7430 showmode();
7432 redraw_curbuf_later(INVERTED); /* update the inversion */
7434 else /* start Visual mode */
7436 check_visual_highlight();
7437 if (cap->count0) /* use previously selected part */
7439 if (resel_VIsual_mode == NUL) /* there is none */
7441 beep_flush();
7442 return;
7444 VIsual = curwin->w_cursor;
7446 VIsual_active = TRUE;
7447 VIsual_reselect = TRUE;
7448 if (!cap->arg)
7449 /* start Select mode when 'selectmode' contains "cmd" */
7450 may_start_select('c');
7451 #ifdef FEAT_MOUSE
7452 setmouse();
7453 #endif
7454 if (p_smd && msg_silent == 0)
7455 redraw_cmdline = TRUE; /* show visual mode later */
7457 * For V and ^V, we multiply the number of lines even if there
7458 * was only one -- webb
7460 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7462 curwin->w_cursor.lnum +=
7463 resel_VIsual_line_count * cap->count0 - 1;
7464 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7465 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7467 VIsual_mode = resel_VIsual_mode;
7468 if (VIsual_mode == 'v')
7470 if (resel_VIsual_line_count <= 1)
7471 curwin->w_cursor.col += resel_VIsual_col * cap->count0 - 1;
7472 else
7473 curwin->w_cursor.col = resel_VIsual_col;
7474 check_cursor_col();
7476 if (resel_VIsual_col == MAXCOL)
7478 curwin->w_curswant = MAXCOL;
7479 coladvance((colnr_T)MAXCOL);
7481 else if (VIsual_mode == Ctrl_V)
7483 validate_virtcol();
7484 curwin->w_curswant = curwin->w_virtcol
7485 + resel_VIsual_col * cap->count0 - 1;
7486 coladvance(curwin->w_curswant);
7488 else
7489 curwin->w_set_curswant = TRUE;
7490 redraw_curbuf_later(INVERTED); /* show the inversion */
7492 else
7494 if (!cap->arg)
7495 /* start Select mode when 'selectmode' contains "cmd" */
7496 may_start_select('c');
7497 n_start_visual_mode(cap->cmdchar);
7503 * Start selection for Shift-movement keys.
7505 void
7506 start_selection()
7508 /* if 'selectmode' contains "key", start Select mode */
7509 may_start_select('k');
7510 n_start_visual_mode('v');
7514 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7516 void
7517 may_start_select(c)
7518 int c;
7520 VIsual_select = (stuff_empty() && typebuf_typed()
7521 && (vim_strchr(p_slm, c) != NULL));
7525 * Start Visual mode "c".
7526 * Should set VIsual_select before calling this.
7528 static void
7529 n_start_visual_mode(c)
7530 int c;
7532 VIsual_mode = c;
7533 VIsual_active = TRUE;
7534 VIsual_reselect = TRUE;
7535 #ifdef FEAT_VIRTUALEDIT
7536 /* Corner case: the 0 position in a tab may change when going into
7537 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7539 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7540 coladvance(curwin->w_virtcol);
7541 #endif
7542 VIsual = curwin->w_cursor;
7544 #ifdef FEAT_FOLDING
7545 foldAdjustVisual();
7546 #endif
7548 #ifdef FEAT_MOUSE
7549 setmouse();
7550 #endif
7551 if (p_smd && msg_silent == 0)
7552 redraw_cmdline = TRUE; /* show visual mode later */
7553 #ifdef FEAT_CLIPBOARD
7554 /* Make sure the clipboard gets updated. Needed because start and
7555 * end may still be the same, and the selection needs to be owned */
7556 clip_star.vmode = NUL;
7557 #endif
7559 /* Only need to redraw this line, unless still need to redraw an old
7560 * Visual area (when 'lazyredraw' is set). */
7561 if (curwin->w_redr_type < INVERTED)
7563 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7564 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7568 #endif /* FEAT_VISUAL */
7571 * CTRL-W: Window commands
7573 static void
7574 nv_window(cap)
7575 cmdarg_T *cap;
7577 #ifdef FEAT_WINDOWS
7578 if (!checkclearop(cap->oap))
7579 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7580 #else
7581 (void)checkclearop(cap->oap);
7582 #endif
7586 * CTRL-Z: Suspend
7588 static void
7589 nv_suspend(cap)
7590 cmdarg_T *cap;
7592 clearop(cap->oap);
7593 #ifdef FEAT_VISUAL
7594 if (VIsual_active)
7595 end_visual_mode(); /* stop Visual mode */
7596 #endif
7597 do_cmdline_cmd((char_u *)"st");
7601 * Commands starting with "g".
7603 static void
7604 nv_g_cmd(cap)
7605 cmdarg_T *cap;
7607 oparg_T *oap = cap->oap;
7608 #ifdef FEAT_VISUAL
7609 pos_T tpos;
7610 #endif
7611 int i;
7612 int flag = FALSE;
7614 switch (cap->nchar)
7616 #ifdef MEM_PROFILE
7618 * "g^A": dump log of used memory.
7620 case Ctrl_A:
7621 vim_mem_profile_dump();
7622 break;
7623 #endif
7625 #ifdef FEAT_VREPLACE
7627 * "gR": Enter virtual replace mode.
7629 case 'R':
7630 cap->arg = TRUE;
7631 nv_Replace(cap);
7632 break;
7634 case 'r':
7635 nv_vreplace(cap);
7636 break;
7637 #endif
7639 case '&':
7640 do_cmdline_cmd((char_u *)"%s//~/&");
7641 break;
7643 #ifdef FEAT_VISUAL
7645 * "gv": Reselect the previous Visual area. If Visual already active,
7646 * exchange previous and current Visual area.
7648 case 'v':
7649 if (checkclearop(oap))
7650 break;
7652 if ( curbuf->b_visual.vi_start.lnum == 0
7653 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7654 || curbuf->b_visual.vi_end.lnum == 0)
7655 beep_flush();
7656 else
7658 /* set w_cursor to the start of the Visual area, tpos to the end */
7659 if (VIsual_active)
7661 i = VIsual_mode;
7662 VIsual_mode = curbuf->b_visual.vi_mode;
7663 curbuf->b_visual.vi_mode = i;
7664 # ifdef FEAT_EVAL
7665 curbuf->b_visual_mode_eval = i;
7666 # endif
7667 i = curwin->w_curswant;
7668 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7669 curbuf->b_visual.vi_curswant = i;
7671 tpos = curbuf->b_visual.vi_end;
7672 curbuf->b_visual.vi_end = curwin->w_cursor;
7673 curwin->w_cursor = curbuf->b_visual.vi_start;
7674 curbuf->b_visual.vi_start = VIsual;
7676 else
7678 VIsual_mode = curbuf->b_visual.vi_mode;
7679 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7680 tpos = curbuf->b_visual.vi_end;
7681 curwin->w_cursor = curbuf->b_visual.vi_start;
7684 VIsual_active = TRUE;
7685 VIsual_reselect = TRUE;
7687 /* Set Visual to the start and w_cursor to the end of the Visual
7688 * area. Make sure they are on an existing character. */
7689 check_cursor();
7690 VIsual = curwin->w_cursor;
7691 curwin->w_cursor = tpos;
7692 check_cursor();
7693 update_topline();
7695 * When called from normal "g" command: start Select mode when
7696 * 'selectmode' contains "cmd". When called for K_SELECT, always
7697 * start Select mode.
7699 if (cap->arg)
7700 VIsual_select = TRUE;
7701 else
7702 may_start_select('c');
7703 #ifdef FEAT_MOUSE
7704 setmouse();
7705 #endif
7706 #ifdef FEAT_CLIPBOARD
7707 /* Make sure the clipboard gets updated. Needed because start and
7708 * end are still the same, and the selection needs to be owned */
7709 clip_star.vmode = NUL;
7710 #endif
7711 redraw_curbuf_later(INVERTED);
7712 showmode();
7714 break;
7716 * "gV": Don't reselect the previous Visual area after a Select mode
7717 * mapping of menu.
7719 case 'V':
7720 VIsual_reselect = FALSE;
7721 break;
7724 * "gh": start Select mode.
7725 * "gH": start Select line mode.
7726 * "g^H": start Select block mode.
7728 case K_BS:
7729 cap->nchar = Ctrl_H;
7730 /* FALLTHROUGH */
7731 case 'h':
7732 case 'H':
7733 case Ctrl_H:
7734 # ifdef EBCDIC
7735 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7736 if (cap->nchar == Ctrl_H)
7737 cap->cmdchar = Ctrl_V;
7738 else
7739 # endif
7740 cap->cmdchar = cap->nchar + ('v' - 'h');
7741 cap->arg = TRUE;
7742 nv_visual(cap);
7743 break;
7744 #endif /* FEAT_VISUAL */
7747 * "gj" and "gk" two new funny movement keys -- up and down
7748 * movement based on *screen* line rather than *file* line.
7750 case 'j':
7751 case K_DOWN:
7752 /* with 'nowrap' it works just like the normal "j" command; also when
7753 * in a closed fold */
7754 if (!curwin->w_p_wrap
7755 #ifdef FEAT_FOLDING
7756 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7757 #endif
7760 oap->motion_type = MLINE;
7761 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7763 else
7764 i = nv_screengo(oap, FORWARD, cap->count1);
7765 if (i == FAIL)
7766 clearopbeep(oap);
7767 break;
7769 case 'k':
7770 case K_UP:
7771 /* with 'nowrap' it works just like the normal "k" command; also when
7772 * in a closed fold */
7773 if (!curwin->w_p_wrap
7774 #ifdef FEAT_FOLDING
7775 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7776 #endif
7779 oap->motion_type = MLINE;
7780 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7782 else
7783 i = nv_screengo(oap, BACKWARD, cap->count1);
7784 if (i == FAIL)
7785 clearopbeep(oap);
7786 break;
7789 * "gJ": join two lines without inserting a space.
7791 case 'J':
7792 nv_join(cap);
7793 break;
7796 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7797 * "gm": middle of "g0" and "g$".
7799 case '^':
7800 flag = TRUE;
7801 /* FALLTHROUGH */
7803 case '0':
7804 case 'm':
7805 case K_HOME:
7806 case K_KHOME:
7807 oap->motion_type = MCHAR;
7808 oap->inclusive = FALSE;
7809 if (curwin->w_p_wrap
7810 #ifdef FEAT_VERTSPLIT
7811 && curwin->w_width != 0
7812 #endif
7815 int width1 = W_WIDTH(curwin) - curwin_col_off();
7816 int width2 = width1 + curwin_col_off2();
7818 validate_virtcol();
7819 i = 0;
7820 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
7821 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
7823 else
7824 i = curwin->w_leftcol;
7825 /* Go to the middle of the screen line. When 'number' is on and lines
7826 * are wrapping the middle can be more to the left.*/
7827 if (cap->nchar == 'm')
7828 i += (W_WIDTH(curwin) - curwin_col_off()
7829 + ((curwin->w_p_wrap && i > 0)
7830 ? curwin_col_off2() : 0)) / 2;
7831 coladvance((colnr_T)i);
7832 if (flag)
7835 i = gchar_cursor();
7836 while (vim_iswhite(i) && oneright() == OK);
7838 curwin->w_set_curswant = TRUE;
7839 break;
7841 case '_':
7842 /* "g_": to the last non-blank character in the line or <count> lines
7843 * downward. */
7844 cap->oap->motion_type = MCHAR;
7845 cap->oap->inclusive = TRUE;
7846 curwin->w_curswant = MAXCOL;
7847 if (cursor_down((long)(cap->count1 - 1),
7848 cap->oap->op_type == OP_NOP) == FAIL)
7849 clearopbeep(cap->oap);
7850 else
7852 char_u *ptr = ml_get_curline();
7854 /* In Visual mode we may end up after the line. */
7855 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
7856 --curwin->w_cursor.col;
7858 /* Decrease the cursor column until it's on a non-blank. */
7859 while (curwin->w_cursor.col > 0
7860 && vim_iswhite(ptr[curwin->w_cursor.col]))
7861 --curwin->w_cursor.col;
7862 curwin->w_set_curswant = TRUE;
7864 break;
7866 case '$':
7867 case K_END:
7868 case K_KEND:
7870 int col_off = curwin_col_off();
7872 oap->motion_type = MCHAR;
7873 oap->inclusive = TRUE;
7874 if (curwin->w_p_wrap
7875 #ifdef FEAT_VERTSPLIT
7876 && curwin->w_width != 0
7877 #endif
7880 curwin->w_curswant = MAXCOL; /* so we stay at the end */
7881 if (cap->count1 == 1)
7883 int width1 = W_WIDTH(curwin) - col_off;
7884 int width2 = width1 + curwin_col_off2();
7886 validate_virtcol();
7887 i = width1 - 1;
7888 if (curwin->w_virtcol >= (colnr_T)width1)
7889 i += ((curwin->w_virtcol - width1) / width2 + 1)
7890 * width2;
7891 coladvance((colnr_T)i);
7892 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
7893 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
7896 * Check for landing on a character that got split at
7897 * the end of the line. We do not want to advance to
7898 * the next screen line.
7900 validate_virtcol();
7901 if (curwin->w_virtcol > (colnr_T)i)
7902 --curwin->w_cursor.col;
7904 #endif
7906 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
7907 clearopbeep(oap);
7909 else
7911 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
7912 coladvance((colnr_T)i);
7914 /* Make sure we stick in this column. */
7915 validate_virtcol();
7916 curwin->w_curswant = curwin->w_virtcol;
7917 curwin->w_set_curswant = FALSE;
7920 break;
7923 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
7925 case '*':
7926 case '#':
7927 #if POUND != '#'
7928 case POUND: /* pound sign (sometimes equal to '#') */
7929 #endif
7930 case Ctrl_RSB: /* :tag or :tselect for current identifier */
7931 case ']': /* :tselect for current identifier */
7932 nv_ident(cap);
7933 break;
7936 * ge and gE: go back to end of word
7938 case 'e':
7939 case 'E':
7940 oap->motion_type = MCHAR;
7941 curwin->w_set_curswant = TRUE;
7942 oap->inclusive = TRUE;
7943 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
7944 clearopbeep(oap);
7945 break;
7948 * "g CTRL-G": display info about cursor position
7950 case Ctrl_G:
7951 cursor_pos_info();
7952 break;
7955 * "gi": start Insert at the last position.
7957 case 'i':
7958 if (curbuf->b_last_insert.lnum != 0)
7960 curwin->w_cursor = curbuf->b_last_insert;
7961 check_cursor_lnum();
7962 i = (int)STRLEN(ml_get_curline());
7963 if (curwin->w_cursor.col > (colnr_T)i)
7965 #ifdef FEAT_VIRTUALEDIT
7966 if (virtual_active())
7967 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
7968 #endif
7969 curwin->w_cursor.col = i;
7972 cap->cmdchar = 'i';
7973 nv_edit(cap);
7974 break;
7977 * "gI": Start insert in column 1.
7979 case 'I':
7980 beginline(0);
7981 if (!checkclearopq(oap))
7982 invoke_edit(cap, FALSE, 'g', FALSE);
7983 break;
7985 #ifdef FEAT_SEARCHPATH
7987 * "gf": goto file, edit file under cursor
7988 * "]f" and "[f": can also be used.
7990 case 'f':
7991 case 'F':
7992 nv_gotofile(cap);
7993 break;
7994 #endif
7996 /* "g'm" and "g`m": jump to mark without setting pcmark */
7997 case '\'':
7998 cap->arg = TRUE;
7999 /*FALLTHROUGH*/
8000 case '`':
8001 nv_gomark(cap);
8002 break;
8005 * "gs": Goto sleep.
8007 case 's':
8008 do_sleep(cap->count1 * 1000L);
8009 break;
8012 * "ga": Display the ascii value of the character under the
8013 * cursor. It is displayed in decimal, hex, and octal. -- webb
8015 case 'a':
8016 do_ascii(NULL);
8017 break;
8019 #ifdef FEAT_MBYTE
8021 * "g8": Display the bytes used for the UTF-8 character under the
8022 * cursor. It is displayed in hex.
8023 * "8g8" finds illegal byte sequence.
8025 case '8':
8026 if (cap->count0 == 8)
8027 utf_find_illegal();
8028 else
8029 show_utf8();
8030 break;
8031 #endif
8033 case '<':
8034 show_sb_text();
8035 break;
8038 * "gg": Goto the first line in file. With a count it goes to
8039 * that line number like for "G". -- webb
8041 case 'g':
8042 cap->arg = FALSE;
8043 nv_goto(cap);
8044 break;
8047 * Two-character operators:
8048 * "gq" Format text
8049 * "gw" Format text and keep cursor position
8050 * "g~" Toggle the case of the text.
8051 * "gu" Change text to lower case.
8052 * "gU" Change text to upper case.
8053 * "g?" rot13 encoding
8054 * "g@" call 'operatorfunc'
8056 case 'q':
8057 case 'w':
8058 oap->cursor_start = curwin->w_cursor;
8059 /*FALLTHROUGH*/
8060 case '~':
8061 case 'u':
8062 case 'U':
8063 case '?':
8064 case '@':
8065 nv_operator(cap);
8066 break;
8069 * "gd": Find first occurrence of pattern under the cursor in the
8070 * current function
8071 * "gD": idem, but in the current file.
8073 case 'd':
8074 case 'D':
8075 nv_gd(oap, cap->nchar, (int)cap->count0);
8076 break;
8078 #ifdef FEAT_MOUSE
8080 * g<*Mouse> : <C-*mouse>
8082 case K_MIDDLEMOUSE:
8083 case K_MIDDLEDRAG:
8084 case K_MIDDLERELEASE:
8085 case K_LEFTMOUSE:
8086 case K_LEFTDRAG:
8087 case K_LEFTRELEASE:
8088 case K_RIGHTMOUSE:
8089 case K_RIGHTDRAG:
8090 case K_RIGHTRELEASE:
8091 case K_X1MOUSE:
8092 case K_X1DRAG:
8093 case K_X1RELEASE:
8094 case K_X2MOUSE:
8095 case K_X2DRAG:
8096 case K_X2RELEASE:
8097 mod_mask = MOD_MASK_CTRL;
8098 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8099 break;
8100 #endif
8102 case K_IGNORE:
8103 break;
8106 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8108 case 'p':
8109 case 'P':
8110 nv_put(cap);
8111 break;
8113 #ifdef FEAT_BYTEOFF
8114 /* "go": goto byte count from start of buffer */
8115 case 'o':
8116 goto_byte(cap->count0);
8117 break;
8118 #endif
8120 /* "gQ": improved Ex mode */
8121 case 'Q':
8122 if (text_locked())
8124 clearopbeep(cap->oap);
8125 text_locked_msg();
8126 break;
8129 if (!checkclearopq(oap))
8130 do_exmode(TRUE);
8131 break;
8133 #ifdef FEAT_JUMPLIST
8134 case ',':
8135 nv_pcmark(cap);
8136 break;
8138 case ';':
8139 cap->count1 = -cap->count1;
8140 nv_pcmark(cap);
8141 break;
8142 #endif
8144 #ifdef FEAT_WINDOWS
8145 case 't':
8146 goto_tabpage((int)cap->count0);
8147 break;
8148 case 'T':
8149 goto_tabpage(-(int)cap->count1);
8150 break;
8151 #endif
8153 case '+':
8154 case '-': /* "g+" and "g-": undo or redo along the timeline */
8155 if (!checkclearopq(oap))
8156 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
8157 FALSE, FALSE);
8158 break;
8160 default:
8161 clearopbeep(oap);
8162 break;
8167 * Handle "o" and "O" commands.
8169 static void
8170 n_opencmd(cap)
8171 cmdarg_T *cap;
8173 if (!checkclearopq(cap->oap))
8175 #ifdef FEAT_FOLDING
8176 if (cap->cmdchar == 'O')
8177 /* Open above the first line of a folded sequence of lines */
8178 (void)hasFolding(curwin->w_cursor.lnum,
8179 &curwin->w_cursor.lnum, NULL);
8180 else
8181 /* Open below the last line of a folded sequence of lines */
8182 (void)hasFolding(curwin->w_cursor.lnum,
8183 NULL, &curwin->w_cursor.lnum);
8184 #endif
8185 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8186 (cap->cmdchar == 'O' ? 1 : 0)),
8187 (linenr_T)(curwin->w_cursor.lnum +
8188 (cap->cmdchar == 'o' ? 1 : 0))
8189 ) == OK
8190 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8191 #ifdef FEAT_COMMENTS
8192 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8193 #endif
8194 0, 0))
8196 /* When '#' is in 'cpoptions' ignore the count. */
8197 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8198 cap->count1 = 1;
8199 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8205 * "." command: redo last change.
8207 static void
8208 nv_dot(cap)
8209 cmdarg_T *cap;
8211 if (!checkclearopq(cap->oap))
8214 * If "restart_edit" is TRUE, the last but one command is repeated
8215 * instead of the last command (inserting text). This is used for
8216 * CTRL-O <.> in insert mode.
8218 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8219 clearopbeep(cap->oap);
8224 * CTRL-R: undo undo
8226 static void
8227 nv_redo(cap)
8228 cmdarg_T *cap;
8230 if (!checkclearopq(cap->oap))
8232 u_redo((int)cap->count1);
8233 curwin->w_set_curswant = TRUE;
8238 * Handle "U" command.
8240 static void
8241 nv_Undo(cap)
8242 cmdarg_T *cap;
8244 /* In Visual mode and typing "gUU" triggers an operator */
8245 if (cap->oap->op_type == OP_UPPER
8246 #ifdef FEAT_VISUAL
8247 || VIsual_active
8248 #endif
8251 /* translate "gUU" to "gUgU" */
8252 cap->cmdchar = 'g';
8253 cap->nchar = 'U';
8254 nv_operator(cap);
8256 else if (!checkclearopq(cap->oap))
8258 u_undoline();
8259 curwin->w_set_curswant = TRUE;
8264 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8265 * single character.
8267 static void
8268 nv_tilde(cap)
8269 cmdarg_T *cap;
8271 if (!p_to
8272 #ifdef FEAT_VISUAL
8273 && !VIsual_active
8274 #endif
8275 && cap->oap->op_type != OP_TILDE)
8276 n_swapchar(cap);
8277 else
8278 nv_operator(cap);
8282 * Handle an operator command.
8283 * The actual work is done by do_pending_operator().
8285 static void
8286 nv_operator(cap)
8287 cmdarg_T *cap;
8289 int op_type;
8291 op_type = get_op_type(cap->cmdchar, cap->nchar);
8293 if (op_type == cap->oap->op_type) /* double operator works on lines */
8294 nv_lineop(cap);
8295 else if (!checkclearop(cap->oap))
8297 cap->oap->start = curwin->w_cursor;
8298 cap->oap->op_type = op_type;
8299 #ifdef FEAT_EVAL
8300 set_op_var(op_type);
8301 #endif
8305 #ifdef FEAT_EVAL
8307 * Set v:operator to the characters for "optype".
8309 static void
8310 set_op_var(optype)
8311 int optype;
8313 char_u opchars[3];
8315 if (optype == OP_NOP)
8316 set_vim_var_string(VV_OP, NULL, 0);
8317 else
8319 opchars[0] = get_op_char(optype);
8320 opchars[1] = get_extra_op_char(optype);
8321 opchars[2] = NUL;
8322 set_vim_var_string(VV_OP, opchars, -1);
8325 #endif
8328 * Handle linewise operator "dd", "yy", etc.
8330 * "_" is is a strange motion command that helps make operators more logical.
8331 * It is actually implemented, but not documented in the real Vi. This motion
8332 * command actually refers to "the current line". Commands like "dd" and "yy"
8333 * are really an alternate form of "d_" and "y_". It does accept a count, so
8334 * "d3_" works to delete 3 lines.
8336 static void
8337 nv_lineop(cap)
8338 cmdarg_T *cap;
8340 cap->oap->motion_type = MLINE;
8341 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8342 clearopbeep(cap->oap);
8343 else if ( cap->oap->op_type == OP_DELETE
8344 || cap->oap->op_type == OP_LSHIFT
8345 || cap->oap->op_type == OP_RSHIFT)
8346 beginline(BL_SOL | BL_FIX);
8347 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8348 beginline(BL_WHITE | BL_FIX);
8352 * <Home> command.
8354 static void
8355 nv_home(cap)
8356 cmdarg_T *cap;
8358 /* CTRL-HOME is like "gg" */
8359 if (mod_mask & MOD_MASK_CTRL)
8360 nv_goto(cap);
8361 else
8363 cap->count0 = 1;
8364 nv_pipe(cap);
8366 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8367 one-character line). */
8371 * "|" command.
8373 static void
8374 nv_pipe(cap)
8375 cmdarg_T *cap;
8377 cap->oap->motion_type = MCHAR;
8378 cap->oap->inclusive = FALSE;
8379 beginline(0);
8380 if (cap->count0 > 0)
8382 coladvance((colnr_T)(cap->count0 - 1));
8383 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8385 else
8386 curwin->w_curswant = 0;
8387 /* keep curswant at the column where we wanted to go, not where
8388 we ended; differs if line is too short */
8389 curwin->w_set_curswant = FALSE;
8393 * Handle back-word command "b" and "B".
8394 * cap->arg is 1 for "B"
8396 static void
8397 nv_bck_word(cap)
8398 cmdarg_T *cap;
8400 cap->oap->motion_type = MCHAR;
8401 cap->oap->inclusive = FALSE;
8402 curwin->w_set_curswant = TRUE;
8403 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8404 clearopbeep(cap->oap);
8405 #ifdef FEAT_FOLDING
8406 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8407 foldOpenCursor();
8408 #endif
8412 * Handle word motion commands "e", "E", "w" and "W".
8413 * cap->arg is TRUE for "E" and "W".
8415 static void
8416 nv_wordcmd(cap)
8417 cmdarg_T *cap;
8419 int n;
8420 int word_end;
8421 int flag = FALSE;
8422 pos_T startpos = curwin->w_cursor;
8425 * Set inclusive for the "E" and "e" command.
8427 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8428 word_end = TRUE;
8429 else
8430 word_end = FALSE;
8431 cap->oap->inclusive = word_end;
8434 * "cw" and "cW" are a special case.
8436 if (!word_end && cap->oap->op_type == OP_CHANGE)
8438 n = gchar_cursor();
8439 if (n != NUL) /* not an empty line */
8441 if (vim_iswhite(n))
8444 * Reproduce a funny Vi behaviour: "cw" on a blank only
8445 * changes one character, not all blanks until the start of
8446 * the next word. Only do this when the 'w' flag is included
8447 * in 'cpoptions'.
8449 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8451 cap->oap->inclusive = TRUE;
8452 cap->oap->motion_type = MCHAR;
8453 return;
8456 else
8459 * This is a little strange. To match what the real Vi does,
8460 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8461 * that we are not on a space or a TAB. This seems impolite
8462 * at first, but it's really more what we mean when we say
8463 * 'cw'.
8464 * Another strangeness: When standing on the end of a word
8465 * "ce" will change until the end of the next wordt, but "cw"
8466 * will change only one character! This is done by setting
8467 * flag.
8469 cap->oap->inclusive = TRUE;
8470 word_end = TRUE;
8471 flag = TRUE;
8476 cap->oap->motion_type = MCHAR;
8477 curwin->w_set_curswant = TRUE;
8478 if (word_end)
8479 n = end_word(cap->count1, cap->arg, flag, FALSE);
8480 else
8481 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8483 /* Don't leave the cursor on the NUL past the end of line. Unless we
8484 * didn't move it forward. */
8485 if (lt(startpos, curwin->w_cursor))
8486 adjust_cursor(cap->oap);
8488 if (n == FAIL && cap->oap->op_type == OP_NOP)
8489 clearopbeep(cap->oap);
8490 else
8492 #ifdef FEAT_VISUAL
8493 adjust_for_sel(cap);
8494 #endif
8495 #ifdef FEAT_FOLDING
8496 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8497 foldOpenCursor();
8498 #endif
8503 * Used after a movement command: If the cursor ends up on the NUL after the
8504 * end of the line, may move it back to the last character and make the motion
8505 * inclusive.
8507 static void
8508 adjust_cursor(oap)
8509 oparg_T *oap;
8511 /* The cursor cannot remain on the NUL when:
8512 * - the column is > 0
8513 * - not in Visual mode or 'selection' is "o"
8514 * - 'virtualedit' is not "all" and not "onemore".
8516 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
8517 #ifdef FEAT_VISUAL
8518 && (!VIsual_active || *p_sel == 'o')
8519 #endif
8520 #ifdef FEAT_VIRTUALEDIT
8521 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8522 #endif
8525 --curwin->w_cursor.col;
8526 #ifdef FEAT_MBYTE
8527 /* prevent cursor from moving on the trail byte */
8528 if (has_mbyte)
8529 mb_adjust_cursor();
8530 #endif
8531 oap->inclusive = TRUE;
8536 * "0" and "^" commands.
8537 * cap->arg is the argument for beginline().
8539 static void
8540 nv_beginline(cap)
8541 cmdarg_T *cap;
8543 cap->oap->motion_type = MCHAR;
8544 cap->oap->inclusive = FALSE;
8545 beginline(cap->arg);
8546 #ifdef FEAT_FOLDING
8547 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8548 foldOpenCursor();
8549 #endif
8550 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8551 one-character line). */
8554 #ifdef FEAT_VISUAL
8556 * In exclusive Visual mode, may include the last character.
8558 static void
8559 adjust_for_sel(cap)
8560 cmdarg_T *cap;
8562 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8563 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8565 # ifdef FEAT_MBYTE
8566 if (has_mbyte)
8567 inc_cursor();
8568 else
8569 # endif
8570 ++curwin->w_cursor.col;
8571 cap->oap->inclusive = FALSE;
8576 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8577 * Should check VIsual_mode before calling this.
8578 * Returns TRUE when backed up to the previous line.
8580 static int
8581 unadjust_for_sel()
8583 pos_T *pp;
8585 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8587 if (lt(VIsual, curwin->w_cursor))
8588 pp = &curwin->w_cursor;
8589 else
8590 pp = &VIsual;
8591 #ifdef FEAT_VIRTUALEDIT
8592 if (pp->coladd > 0)
8593 --pp->coladd;
8594 else
8595 #endif
8596 if (pp->col > 0)
8598 --pp->col;
8599 #ifdef FEAT_MBYTE
8600 mb_adjustpos(pp);
8601 #endif
8603 else if (pp->lnum > 1)
8605 --pp->lnum;
8606 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8607 return TRUE;
8610 return FALSE;
8614 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8616 static void
8617 nv_select(cap)
8618 cmdarg_T *cap;
8620 if (VIsual_active)
8621 VIsual_select = TRUE;
8622 else if (VIsual_reselect)
8624 cap->nchar = 'v'; /* fake "gv" command */
8625 cap->arg = TRUE;
8626 nv_g_cmd(cap);
8630 #endif
8633 * "G", "gg", CTRL-END, CTRL-HOME.
8634 * cap->arg is TRUE for "G".
8636 static void
8637 nv_goto(cap)
8638 cmdarg_T *cap;
8640 linenr_T lnum;
8642 if (cap->arg)
8643 lnum = curbuf->b_ml.ml_line_count;
8644 else
8645 lnum = 1L;
8646 cap->oap->motion_type = MLINE;
8647 setpcmark();
8649 /* When a count is given, use it instead of the default lnum */
8650 if (cap->count0 != 0)
8651 lnum = cap->count0;
8652 if (lnum < 1L)
8653 lnum = 1L;
8654 else if (lnum > curbuf->b_ml.ml_line_count)
8655 lnum = curbuf->b_ml.ml_line_count;
8656 curwin->w_cursor.lnum = lnum;
8657 beginline(BL_SOL | BL_FIX);
8658 #ifdef FEAT_FOLDING
8659 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8660 foldOpenCursor();
8661 #endif
8665 * CTRL-\ in Normal mode.
8667 static void
8668 nv_normal(cap)
8669 cmdarg_T *cap;
8671 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8673 clearop(cap->oap);
8674 if (restart_edit != 0 && mode_displayed)
8675 clear_cmdline = TRUE; /* unshow mode later */
8676 restart_edit = 0;
8677 #ifdef FEAT_CMDWIN
8678 if (cmdwin_type != 0)
8679 cmdwin_result = Ctrl_C;
8680 #endif
8681 #ifdef FEAT_VISUAL
8682 if (VIsual_active)
8684 end_visual_mode(); /* stop Visual */
8685 redraw_curbuf_later(INVERTED);
8687 #endif
8688 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8689 if (cap->nchar == Ctrl_G && p_im)
8690 restart_edit = 'a';
8692 else
8693 clearopbeep(cap->oap);
8697 * ESC in Normal mode: beep, but don't flush buffers.
8698 * Don't even beep if we are canceling a command.
8700 static void
8701 nv_esc(cap)
8702 cmdarg_T *cap;
8704 int no_reason;
8706 no_reason = (cap->oap->op_type == OP_NOP
8707 && cap->opcount == 0
8708 && cap->count0 == 0
8709 && cap->oap->regname == 0
8710 && !p_im);
8712 if (cap->arg) /* TRUE for CTRL-C */
8714 if (restart_edit == 0
8715 #ifdef FEAT_CMDWIN
8716 && cmdwin_type == 0
8717 #endif
8718 #ifdef FEAT_VISUAL
8719 && !VIsual_active
8720 #endif
8721 && no_reason)
8722 MSG(_("Type :quit<Enter> to exit Vim"));
8724 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8725 * set again below when halfway a mapping. */
8726 if (!p_im)
8727 restart_edit = 0;
8728 #ifdef FEAT_CMDWIN
8729 if (cmdwin_type != 0)
8731 cmdwin_result = K_IGNORE;
8732 got_int = FALSE; /* don't stop executing autocommands et al. */
8733 return;
8735 #endif
8738 #ifdef FEAT_VISUAL
8739 if (VIsual_active)
8741 end_visual_mode(); /* stop Visual */
8742 check_cursor_col(); /* make sure cursor is not beyond EOL */
8743 curwin->w_set_curswant = TRUE;
8744 redraw_curbuf_later(INVERTED);
8746 else
8747 #endif
8748 if (no_reason)
8749 vim_beep();
8750 clearop(cap->oap);
8752 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8753 * set return to Insert mode afterwards. */
8754 if (restart_edit == 0 && goto_im()
8755 #ifdef FEAT_EX_EXTRA
8756 && ex_normal_busy == 0
8757 #endif
8759 restart_edit = 'a';
8763 * Handle "A", "a", "I", "i" and <Insert> commands.
8765 static void
8766 nv_edit(cap)
8767 cmdarg_T *cap;
8769 /* <Insert> is equal to "i" */
8770 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8771 cap->cmdchar = 'i';
8773 #ifdef FEAT_VISUAL
8774 /* in Visual mode "A" and "I" are an operator */
8775 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8776 v_visop(cap);
8778 /* in Visual mode and after an operator "a" and "i" are for text objects */
8779 else
8780 #endif
8781 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8782 && (cap->oap->op_type != OP_NOP
8783 #ifdef FEAT_VISUAL
8784 || VIsual_active
8785 #endif
8788 #ifdef FEAT_TEXTOBJ
8789 nv_object(cap);
8790 #else
8791 clearopbeep(cap->oap);
8792 #endif
8794 else if (!curbuf->b_p_ma && !p_im)
8796 /* Only give this error when 'insertmode' is off. */
8797 EMSG(_(e_modifiable));
8798 clearop(cap->oap);
8800 else if (!checkclearopq(cap->oap))
8802 switch (cap->cmdchar)
8804 case 'A': /* "A"ppend after the line */
8805 curwin->w_set_curswant = TRUE;
8806 #ifdef FEAT_VIRTUALEDIT
8807 if (ve_flags == VE_ALL)
8809 int save_State = State;
8811 /* Pretent Insert mode here to allow the cursor on the
8812 * character past the end of the line */
8813 State = INSERT;
8814 coladvance((colnr_T)MAXCOL);
8815 State = save_State;
8817 else
8818 #endif
8819 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8820 break;
8822 case 'I': /* "I"nsert before the first non-blank */
8823 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
8824 beginline(BL_WHITE);
8825 else
8826 beginline(BL_WHITE|BL_FIX);
8827 break;
8829 case 'a': /* "a"ppend is like "i"nsert on the next character. */
8830 #ifdef FEAT_VIRTUALEDIT
8831 /* increment coladd when in virtual space, increment the
8832 * column otherwise, also to append after an unprintable char */
8833 if (virtual_active()
8834 && (curwin->w_cursor.coladd > 0
8835 || *ml_get_cursor() == NUL
8836 || *ml_get_cursor() == TAB))
8837 curwin->w_cursor.coladd++;
8838 else
8839 #endif
8840 if (*ml_get_cursor() != NUL)
8841 inc_cursor();
8842 break;
8845 #ifdef FEAT_VIRTUALEDIT
8846 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
8848 int save_State = State;
8850 /* Pretent Insert mode here to allow the cursor on the
8851 * character past the end of the line */
8852 State = INSERT;
8853 coladvance(getviscol());
8854 State = save_State;
8856 #endif
8858 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
8863 * Invoke edit() and take care of "restart_edit" and the return value.
8865 static void
8866 invoke_edit(cap, repl, cmd, startln)
8867 cmdarg_T *cap;
8868 int repl; /* "r" or "gr" command */
8869 int cmd;
8870 int startln;
8872 int restart_edit_save = 0;
8874 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
8875 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
8876 * it. */
8877 if (repl || !stuff_empty())
8878 restart_edit_save = restart_edit;
8879 else
8880 restart_edit_save = 0;
8882 /* Always reset "restart_edit", this is not a restarted edit. */
8883 restart_edit = 0;
8885 if (edit(cmd, startln, cap->count1))
8886 cap->retval |= CA_COMMAND_BUSY;
8888 if (restart_edit == 0)
8889 restart_edit = restart_edit_save;
8892 #ifdef FEAT_TEXTOBJ
8894 * "a" or "i" while an operator is pending or in Visual mode: object motion.
8896 static void
8897 nv_object(cap)
8898 cmdarg_T *cap;
8900 int flag;
8901 int include;
8902 char_u *mps_save;
8904 if (cap->cmdchar == 'i')
8905 include = FALSE; /* "ix" = inner object: exclude white space */
8906 else
8907 include = TRUE; /* "ax" = an object: include white space */
8909 /* Make sure (), [], {} and <> are in 'matchpairs' */
8910 mps_save = curbuf->b_p_mps;
8911 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
8913 switch (cap->nchar)
8915 case 'w': /* "aw" = a word */
8916 flag = current_word(cap->oap, cap->count1, include, FALSE);
8917 break;
8918 case 'W': /* "aW" = a WORD */
8919 flag = current_word(cap->oap, cap->count1, include, TRUE);
8920 break;
8921 case 'b': /* "ab" = a braces block */
8922 case '(':
8923 case ')':
8924 flag = current_block(cap->oap, cap->count1, include, '(', ')');
8925 break;
8926 case 'B': /* "aB" = a Brackets block */
8927 case '{':
8928 case '}':
8929 flag = current_block(cap->oap, cap->count1, include, '{', '}');
8930 break;
8931 case '[': /* "a[" = a [] block */
8932 case ']':
8933 flag = current_block(cap->oap, cap->count1, include, '[', ']');
8934 break;
8935 case '<': /* "a<" = a <> block */
8936 case '>':
8937 flag = current_block(cap->oap, cap->count1, include, '<', '>');
8938 break;
8939 case 't': /* "at" = a tag block (xml and html) */
8940 flag = current_tagblock(cap->oap, cap->count1, include);
8941 break;
8942 case 'p': /* "ap" = a paragraph */
8943 flag = current_par(cap->oap, cap->count1, include, 'p');
8944 break;
8945 case 's': /* "as" = a sentence */
8946 flag = current_sent(cap->oap, cap->count1, include);
8947 break;
8948 case '"': /* "a"" = a double quoted string */
8949 case '\'': /* "a'" = a single quoted string */
8950 case '`': /* "a`" = a backtick quoted string */
8951 flag = current_quote(cap->oap, cap->count1, include,
8952 cap->nchar);
8953 break;
8954 #if 0 /* TODO */
8955 case 'S': /* "aS" = a section */
8956 case 'f': /* "af" = a filename */
8957 case 'u': /* "au" = a URL */
8958 #endif
8959 default:
8960 flag = FAIL;
8961 break;
8964 curbuf->b_p_mps = mps_save;
8965 if (flag == FAIL)
8966 clearopbeep(cap->oap);
8967 adjust_cursor_col();
8968 curwin->w_set_curswant = TRUE;
8970 #endif
8973 * "q" command: Start/stop recording.
8974 * "q:", "q/", "q?": edit command-line in command-line window.
8976 static void
8977 nv_record(cap)
8978 cmdarg_T *cap;
8980 if (cap->oap->op_type == OP_FORMAT)
8982 /* "gqq" is the same as "gqgq": format line */
8983 cap->cmdchar = 'g';
8984 cap->nchar = 'q';
8985 nv_operator(cap);
8987 else if (!checkclearop(cap->oap))
8989 #ifdef FEAT_CMDWIN
8990 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
8992 stuffcharReadbuff(cap->nchar);
8993 stuffcharReadbuff(K_CMDWIN);
8995 else
8996 #endif
8997 /* (stop) recording into a named register, unless executing a
8998 * register */
8999 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9000 clearopbeep(cap->oap);
9005 * Handle the "@r" command.
9007 static void
9008 nv_at(cap)
9009 cmdarg_T *cap;
9011 if (checkclearop(cap->oap))
9012 return;
9013 #ifdef FEAT_EVAL
9014 if (cap->nchar == '=')
9016 if (get_expr_register() == NUL)
9017 return;
9019 #endif
9020 while (cap->count1-- && !got_int)
9022 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
9024 clearopbeep(cap->oap);
9025 break;
9027 line_breakcheck();
9032 * Handle the CTRL-U and CTRL-D commands.
9034 static void
9035 nv_halfpage(cap)
9036 cmdarg_T *cap;
9038 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9039 || (cap->cmdchar == Ctrl_D
9040 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9041 clearopbeep(cap->oap);
9042 else if (!checkclearop(cap->oap))
9043 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9047 * Handle "J" or "gJ" command.
9049 static void
9050 nv_join(cap)
9051 cmdarg_T *cap;
9053 #ifdef FEAT_VISUAL
9054 if (VIsual_active) /* join the visual lines */
9055 nv_operator(cap);
9056 else
9057 #endif
9058 if (!checkclearop(cap->oap))
9060 if (cap->count0 <= 1)
9061 cap->count0 = 2; /* default for join is two lines! */
9062 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9063 curbuf->b_ml.ml_line_count)
9064 clearopbeep(cap->oap); /* beyond last line */
9065 else
9067 prep_redo(cap->oap->regname, cap->count0,
9068 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9069 do_do_join(cap->count0, cap->nchar == NUL);
9075 * "P", "gP", "p" and "gp" commands.
9077 static void
9078 nv_put(cap)
9079 cmdarg_T *cap;
9081 #ifdef FEAT_VISUAL
9082 int regname = 0;
9083 void *reg1 = NULL, *reg2 = NULL;
9084 int empty = FALSE;
9085 int was_visual = FALSE;
9086 #endif
9087 int dir;
9088 int flags = 0;
9090 if (cap->oap->op_type != OP_NOP)
9092 #ifdef FEAT_DIFF
9093 /* "dp" is ":diffput" */
9094 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9096 clearop(cap->oap);
9097 nv_diffgetput(TRUE);
9099 else
9100 #endif
9101 clearopbeep(cap->oap);
9103 else
9105 dir = (cap->cmdchar == 'P'
9106 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9107 ? BACKWARD : FORWARD;
9108 prep_redo_cmd(cap);
9109 if (cap->cmdchar == 'g')
9110 flags |= PUT_CURSEND;
9112 #ifdef FEAT_VISUAL
9113 if (VIsual_active)
9115 /* Putting in Visual mode: The put text replaces the selected
9116 * text. First delete the selected text, then put the new text.
9117 * Need to save and restore the registers that the delete
9118 * overwrites if the old contents is being put.
9120 was_visual = TRUE;
9121 regname = cap->oap->regname;
9122 # ifdef FEAT_CLIPBOARD
9123 adjust_clip_reg(&regname);
9124 # endif
9125 if (regname == 0 || VIM_ISDIGIT(regname)
9126 # ifdef FEAT_CLIPBOARD
9127 || (clip_unnamed && (regname == '*' || regname == '+'))
9128 # endif
9132 /* the delete is going to overwrite the register we want to
9133 * put, save it first. */
9134 reg1 = get_register(regname, TRUE);
9137 /* Now delete the selected text. */
9138 cap->cmdchar = 'd';
9139 cap->nchar = NUL;
9140 cap->oap->regname = NUL;
9141 nv_operator(cap);
9142 do_pending_operator(cap, 0, FALSE);
9143 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
9145 /* delete PUT_LINE_BACKWARD; */
9146 cap->oap->regname = regname;
9148 if (reg1 != NULL)
9150 /* Delete probably changed the register we want to put, save
9151 * it first. Then put back what was there before the delete. */
9152 reg2 = get_register(regname, FALSE);
9153 put_register(regname, reg1);
9156 /* When deleted a linewise Visual area, put the register as
9157 * lines to avoid it joined with the next line. When deletion was
9158 * characterwise, split a line when putting lines. */
9159 if (VIsual_mode == 'V')
9160 flags |= PUT_LINE;
9161 else if (VIsual_mode == 'v')
9162 flags |= PUT_LINE_SPLIT;
9163 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9164 flags |= PUT_LINE_FORWARD;
9165 dir = BACKWARD;
9166 if ((VIsual_mode != 'V'
9167 && curwin->w_cursor.col < curbuf->b_op_start.col)
9168 || (VIsual_mode == 'V'
9169 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9170 /* cursor is at the end of the line or end of file, put
9171 * forward. */
9172 dir = FORWARD;
9174 #endif
9175 do_put(cap->oap->regname, dir, cap->count1, flags);
9177 #ifdef FEAT_VISUAL
9178 /* If a register was saved, put it back now. */
9179 if (reg2 != NULL)
9180 put_register(regname, reg2);
9182 /* What to reselect with "gv"? Selecting the just put text seems to
9183 * be the most useful, since the original text was removed. */
9184 if (was_visual)
9186 curbuf->b_visual.vi_start = curbuf->b_op_start;
9187 curbuf->b_visual.vi_end = curbuf->b_op_end;
9190 /* When all lines were selected and deleted do_put() leaves an empty
9191 * line that needs to be deleted now. */
9192 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
9194 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
9196 /* If the cursor was in that line, move it to the end of the last
9197 * line. */
9198 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9200 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9201 coladvance((colnr_T)MAXCOL);
9204 #endif
9205 auto_format(FALSE, TRUE);
9210 * "o" and "O" commands.
9212 static void
9213 nv_open(cap)
9214 cmdarg_T *cap;
9216 #ifdef FEAT_DIFF
9217 /* "do" is ":diffget" */
9218 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9220 clearop(cap->oap);
9221 nv_diffgetput(FALSE);
9223 else
9224 #endif
9225 #ifdef FEAT_VISUAL
9226 if (VIsual_active) /* switch start and end of visual */
9227 v_swap_corners(cap->cmdchar);
9228 else
9229 #endif
9230 n_opencmd(cap);
9233 #ifdef FEAT_SNIFF
9234 /*ARGSUSED*/
9235 static void
9236 nv_sniff(cap)
9237 cmdarg_T *cap;
9239 ProcessSniffRequests();
9241 #endif
9243 #ifdef FEAT_NETBEANS_INTG
9244 static void
9245 nv_nbcmd(cap)
9246 cmdarg_T *cap;
9248 netbeans_keycommand(cap->nchar);
9250 #endif
9252 #ifdef FEAT_DND
9253 /*ARGSUSED*/
9254 static void
9255 nv_drop(cap)
9256 cmdarg_T *cap;
9258 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9260 #endif
9262 #ifdef FEAT_AUTOCMD
9264 * Trigger CursorHold event.
9265 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9266 * input buffer. "did_cursorhold" is set to avoid retriggering.
9268 /*ARGSUSED*/
9269 static void
9270 nv_cursorhold(cap)
9271 cmdarg_T *cap;
9273 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9274 did_cursorhold = TRUE;
9275 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
9277 #endif