Snapshot 44
[MacVim.git] / src / normal.c
bloba07008936a6671d84efa42de263b53fa6eade84a
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9 /*
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
12 * the operators.
15 #include "vim.h"
17 #ifdef FEAT_VISUAL
19 * The Visual area is remembered for reselection.
21 static int resel_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
22 static linenr_T resel_VIsual_line_count; /* number of lines */
23 static colnr_T resel_VIsual_col; /* nr of cols or end col */
25 static int restart_VIsual_select = 0;
26 #endif
28 static int
29 # ifdef __BORLANDC__
30 _RTLENTRYF
31 # endif
32 nv_compare __ARGS((const void *s1, const void *s2));
33 static int find_command __ARGS((int cmdchar));
34 static void op_colon __ARGS((oparg_T *oap));
35 static void op_function __ARGS((oparg_T *oap));
36 #if defined(FEAT_MOUSE) && defined(FEAT_VISUAL)
37 static void find_start_of_word __ARGS((pos_T *));
38 static void find_end_of_word __ARGS((pos_T *));
39 static int get_mouse_class __ARGS((char_u *p));
40 #endif
41 static void prep_redo_cmd __ARGS((cmdarg_T *cap));
42 static void prep_redo __ARGS((int regname, long, int, int, int, int, int));
43 static int checkclearop __ARGS((oparg_T *oap));
44 static int checkclearopq __ARGS((oparg_T *oap));
45 static void clearop __ARGS((oparg_T *oap));
46 static void clearopbeep __ARGS((oparg_T *oap));
47 #ifdef FEAT_VISUAL
48 static void unshift_special __ARGS((cmdarg_T *cap));
49 #endif
50 #ifdef FEAT_CMDL_INFO
51 static void del_from_showcmd __ARGS((int));
52 #endif
55 * nv_*(): functions called to handle Normal and Visual mode commands.
56 * n_*(): functions called to handle Normal mode commands.
57 * v_*(): functions called to handle Visual mode commands.
59 static void nv_ignore __ARGS((cmdarg_T *cap));
60 static void nv_nop __ARGS((cmdarg_T *cap));
61 static void nv_error __ARGS((cmdarg_T *cap));
62 static void nv_help __ARGS((cmdarg_T *cap));
63 static void nv_addsub __ARGS((cmdarg_T *cap));
64 static void nv_page __ARGS((cmdarg_T *cap));
65 static void nv_gd __ARGS((oparg_T *oap, int nchar, int thisblock));
66 static int nv_screengo __ARGS((oparg_T *oap, int dir, long dist));
67 #ifdef FEAT_MOUSE
68 static void nv_mousescroll __ARGS((cmdarg_T *cap));
69 static void nv_mouse __ARGS((cmdarg_T *cap));
70 #endif
71 static void nv_scroll_line __ARGS((cmdarg_T *cap));
72 static void nv_zet __ARGS((cmdarg_T *cap));
73 #ifdef FEAT_GUI
74 static void nv_ver_scrollbar __ARGS((cmdarg_T *cap));
75 static void nv_hor_scrollbar __ARGS((cmdarg_T *cap));
76 #endif
77 #ifdef FEAT_GUI_TABLINE
78 static void nv_tabline __ARGS((cmdarg_T *cap));
79 static void nv_tabmenu __ARGS((cmdarg_T *cap));
80 #endif
81 static void nv_exmode __ARGS((cmdarg_T *cap));
82 static void nv_colon __ARGS((cmdarg_T *cap));
83 static void nv_ctrlg __ARGS((cmdarg_T *cap));
84 static void nv_ctrlh __ARGS((cmdarg_T *cap));
85 static void nv_clear __ARGS((cmdarg_T *cap));
86 static void nv_ctrlo __ARGS((cmdarg_T *cap));
87 static void nv_hat __ARGS((cmdarg_T *cap));
88 static void nv_Zet __ARGS((cmdarg_T *cap));
89 static void nv_ident __ARGS((cmdarg_T *cap));
90 static void nv_tagpop __ARGS((cmdarg_T *cap));
91 static void nv_scroll __ARGS((cmdarg_T *cap));
92 static void nv_right __ARGS((cmdarg_T *cap));
93 static void nv_left __ARGS((cmdarg_T *cap));
94 static void nv_up __ARGS((cmdarg_T *cap));
95 static void nv_down __ARGS((cmdarg_T *cap));
96 #ifdef FEAT_SEARCHPATH
97 static void nv_gotofile __ARGS((cmdarg_T *cap));
98 #endif
99 static void nv_end __ARGS((cmdarg_T *cap));
100 static void nv_dollar __ARGS((cmdarg_T *cap));
101 static void nv_search __ARGS((cmdarg_T *cap));
102 static void nv_next __ARGS((cmdarg_T *cap));
103 static void normal_search __ARGS((cmdarg_T *cap, int dir, char_u *pat, int opt));
104 static void nv_csearch __ARGS((cmdarg_T *cap));
105 static void nv_brackets __ARGS((cmdarg_T *cap));
106 static void nv_percent __ARGS((cmdarg_T *cap));
107 static void nv_brace __ARGS((cmdarg_T *cap));
108 static void nv_mark __ARGS((cmdarg_T *cap));
109 static void nv_findpar __ARGS((cmdarg_T *cap));
110 static void nv_undo __ARGS((cmdarg_T *cap));
111 static void nv_kundo __ARGS((cmdarg_T *cap));
112 static void nv_Replace __ARGS((cmdarg_T *cap));
113 #ifdef FEAT_VREPLACE
114 static void nv_vreplace __ARGS((cmdarg_T *cap));
115 #endif
116 #ifdef FEAT_VISUAL
117 static void v_swap_corners __ARGS((int cmdchar));
118 #endif
119 static void nv_replace __ARGS((cmdarg_T *cap));
120 static void n_swapchar __ARGS((cmdarg_T *cap));
121 static void nv_cursormark __ARGS((cmdarg_T *cap, int flag, pos_T *pos));
122 #ifdef FEAT_VISUAL
123 static void v_visop __ARGS((cmdarg_T *cap));
124 #endif
125 static void nv_subst __ARGS((cmdarg_T *cap));
126 static void nv_abbrev __ARGS((cmdarg_T *cap));
127 static void nv_optrans __ARGS((cmdarg_T *cap));
128 static void nv_gomark __ARGS((cmdarg_T *cap));
129 static void nv_pcmark __ARGS((cmdarg_T *cap));
130 static void nv_regname __ARGS((cmdarg_T *cap));
131 #ifdef FEAT_VISUAL
132 static void nv_visual __ARGS((cmdarg_T *cap));
133 static void n_start_visual_mode __ARGS((int c));
134 #endif
135 static void nv_window __ARGS((cmdarg_T *cap));
136 static void nv_suspend __ARGS((cmdarg_T *cap));
137 static void nv_g_cmd __ARGS((cmdarg_T *cap));
138 static void n_opencmd __ARGS((cmdarg_T *cap));
139 static void nv_dot __ARGS((cmdarg_T *cap));
140 static void nv_redo __ARGS((cmdarg_T *cap));
141 static void nv_Undo __ARGS((cmdarg_T *cap));
142 static void nv_tilde __ARGS((cmdarg_T *cap));
143 static void nv_operator __ARGS((cmdarg_T *cap));
144 #ifdef FEAT_EVAL
145 static void set_op_var __ARGS((int optype));
146 #endif
147 static void nv_lineop __ARGS((cmdarg_T *cap));
148 static void nv_home __ARGS((cmdarg_T *cap));
149 static void nv_pipe __ARGS((cmdarg_T *cap));
150 static void nv_bck_word __ARGS((cmdarg_T *cap));
151 static void nv_wordcmd __ARGS((cmdarg_T *cap));
152 static void nv_beginline __ARGS((cmdarg_T *cap));
153 static void adjust_cursor __ARGS((oparg_T *oap));
154 #ifdef FEAT_VISUAL
155 static void adjust_for_sel __ARGS((cmdarg_T *cap));
156 static int unadjust_for_sel __ARGS((void));
157 static void nv_select __ARGS((cmdarg_T *cap));
158 #endif
159 static void nv_goto __ARGS((cmdarg_T *cap));
160 static void nv_normal __ARGS((cmdarg_T *cap));
161 static void nv_esc __ARGS((cmdarg_T *oap));
162 static void nv_edit __ARGS((cmdarg_T *cap));
163 static void invoke_edit __ARGS((cmdarg_T *cap, int repl, int cmd, int startln));
164 #ifdef FEAT_TEXTOBJ
165 static void nv_object __ARGS((cmdarg_T *cap));
166 #endif
167 static void nv_record __ARGS((cmdarg_T *cap));
168 static void nv_at __ARGS((cmdarg_T *cap));
169 static void nv_halfpage __ARGS((cmdarg_T *cap));
170 static void nv_join __ARGS((cmdarg_T *cap));
171 static void nv_put __ARGS((cmdarg_T *cap));
172 static void nv_open __ARGS((cmdarg_T *cap));
173 #ifdef FEAT_SNIFF
174 static void nv_sniff __ARGS((cmdarg_T *cap));
175 #endif
176 #ifdef FEAT_NETBEANS_INTG
177 static void nv_nbcmd __ARGS((cmdarg_T *cap));
178 #endif
179 #ifdef FEAT_DND
180 static void nv_drop __ARGS((cmdarg_T *cap));
181 #endif
182 #ifdef FEAT_AUTOCMD
183 static void nv_cursorhold __ARGS((cmdarg_T *cap));
184 #endif
186 static char *e_noident = N_("E349: No identifier under cursor");
189 * Function to be called for a Normal or Visual mode command.
190 * The argument is a cmdarg_T.
192 typedef void (*nv_func_T) __ARGS((cmdarg_T *cap));
194 /* Values for cmd_flags. */
195 #define NV_NCH 0x01 /* may need to get a second char */
196 #define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
197 #define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
198 #define NV_LANG 0x08 /* second char needs language adjustment */
200 #define NV_SS 0x10 /* may start selection */
201 #define NV_SSS 0x20 /* may start selection with shift modifier */
202 #define NV_STS 0x40 /* may stop selection without shift modif. */
203 #define NV_RL 0x80 /* 'rightleft' modifies command */
204 #define NV_KEEPREG 0x100 /* don't clear regname */
205 #define NV_NCW 0x200 /* not allowed in command-line window */
208 * Generally speaking, every Normal mode command should either clear any
209 * pending operator (with *clearop*()), or set the motion type variable
210 * oap->motion_type.
212 * When a cursor motion command is made, it is marked as being a character or
213 * line oriented motion. Then, if an operator is in effect, the operation
214 * becomes character or line oriented accordingly.
218 * This table contains one entry for every Normal or Visual mode command.
219 * The order doesn't matter, init_normal_cmds() will create a sorted index.
220 * It is faster when all keys from zero to '~' are present.
222 static const struct nv_cmd
224 int cmd_char; /* (first) command character */
225 nv_func_T cmd_func; /* function for this command */
226 short_u cmd_flags; /* NV_ flags */
227 short cmd_arg; /* value for ca.arg */
228 } nv_cmds[] =
230 {NUL, nv_error, 0, 0},
231 {Ctrl_A, nv_addsub, 0, 0},
232 {Ctrl_B, nv_page, NV_STS, BACKWARD},
233 {Ctrl_C, nv_esc, 0, TRUE},
234 {Ctrl_D, nv_halfpage, 0, 0},
235 {Ctrl_E, nv_scroll_line, 0, TRUE},
236 {Ctrl_F, nv_page, NV_STS, FORWARD},
237 {Ctrl_G, nv_ctrlg, 0, 0},
238 {Ctrl_H, nv_ctrlh, 0, 0},
239 {Ctrl_I, nv_pcmark, 0, 0},
240 {NL, nv_down, 0, FALSE},
241 {Ctrl_K, nv_error, 0, 0},
242 {Ctrl_L, nv_clear, 0, 0},
243 {Ctrl_M, nv_down, 0, TRUE},
244 {Ctrl_N, nv_down, NV_STS, FALSE},
245 {Ctrl_O, nv_ctrlo, 0, 0},
246 {Ctrl_P, nv_up, NV_STS, FALSE},
247 #ifdef FEAT_VISUAL
248 {Ctrl_Q, nv_visual, 0, FALSE},
249 #else
250 {Ctrl_Q, nv_ignore, 0, 0},
251 #endif
252 {Ctrl_R, nv_redo, 0, 0},
253 {Ctrl_S, nv_ignore, 0, 0},
254 {Ctrl_T, nv_tagpop, NV_NCW, 0},
255 {Ctrl_U, nv_halfpage, 0, 0},
256 #ifdef FEAT_VISUAL
257 {Ctrl_V, nv_visual, 0, FALSE},
258 {'V', nv_visual, 0, FALSE},
259 {'v', nv_visual, 0, FALSE},
260 #else
261 {Ctrl_V, nv_error, 0, 0},
262 {'V', nv_error, 0, 0},
263 {'v', nv_error, 0, 0},
264 #endif
265 {Ctrl_W, nv_window, 0, 0},
266 {Ctrl_X, nv_addsub, 0, 0},
267 {Ctrl_Y, nv_scroll_line, 0, FALSE},
268 {Ctrl_Z, nv_suspend, 0, 0},
269 {ESC, nv_esc, 0, FALSE},
270 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
271 {Ctrl_RSB, nv_ident, NV_NCW, 0},
272 {Ctrl_HAT, nv_hat, NV_NCW, 0},
273 {Ctrl__, nv_error, 0, 0},
274 {' ', nv_right, 0, 0},
275 {'!', nv_operator, 0, 0},
276 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
277 {'#', nv_ident, 0, 0},
278 {'$', nv_dollar, 0, 0},
279 {'%', nv_percent, 0, 0},
280 {'&', nv_optrans, 0, 0},
281 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
282 {'(', nv_brace, 0, BACKWARD},
283 {')', nv_brace, 0, FORWARD},
284 {'*', nv_ident, 0, 0},
285 {'+', nv_down, 0, TRUE},
286 {',', nv_csearch, 0, TRUE},
287 {'-', nv_up, 0, TRUE},
288 {'.', nv_dot, NV_KEEPREG, 0},
289 {'/', nv_search, 0, FALSE},
290 {'0', nv_beginline, 0, 0},
291 {'1', nv_ignore, 0, 0},
292 {'2', nv_ignore, 0, 0},
293 {'3', nv_ignore, 0, 0},
294 {'4', nv_ignore, 0, 0},
295 {'5', nv_ignore, 0, 0},
296 {'6', nv_ignore, 0, 0},
297 {'7', nv_ignore, 0, 0},
298 {'8', nv_ignore, 0, 0},
299 {'9', nv_ignore, 0, 0},
300 {':', nv_colon, 0, 0},
301 {';', nv_csearch, 0, FALSE},
302 {'<', nv_operator, NV_RL, 0},
303 {'=', nv_operator, 0, 0},
304 {'>', nv_operator, NV_RL, 0},
305 {'?', nv_search, 0, FALSE},
306 {'@', nv_at, NV_NCH_NOP, FALSE},
307 {'A', nv_edit, 0, 0},
308 {'B', nv_bck_word, 0, 1},
309 {'C', nv_abbrev, NV_KEEPREG, 0},
310 {'D', nv_abbrev, NV_KEEPREG, 0},
311 {'E', nv_wordcmd, 0, TRUE},
312 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
313 {'G', nv_goto, 0, TRUE},
314 {'H', nv_scroll, 0, 0},
315 {'I', nv_edit, 0, 0},
316 {'J', nv_join, 0, 0},
317 {'K', nv_ident, 0, 0},
318 {'L', nv_scroll, 0, 0},
319 {'M', nv_scroll, 0, 0},
320 {'N', nv_next, 0, SEARCH_REV},
321 {'O', nv_open, 0, 0},
322 {'P', nv_put, 0, 0},
323 {'Q', nv_exmode, NV_NCW, 0},
324 {'R', nv_Replace, 0, FALSE},
325 {'S', nv_subst, NV_KEEPREG, 0},
326 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
327 {'U', nv_Undo, 0, 0},
328 {'W', nv_wordcmd, 0, TRUE},
329 {'X', nv_abbrev, NV_KEEPREG, 0},
330 {'Y', nv_abbrev, NV_KEEPREG, 0},
331 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
332 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
333 {'\\', nv_error, 0, 0},
334 {']', nv_brackets, NV_NCH_ALW, FORWARD},
335 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
336 {'_', nv_lineop, 0, 0},
337 {'`', nv_gomark, NV_NCH_ALW, FALSE},
338 {'a', nv_edit, NV_NCH, 0},
339 {'b', nv_bck_word, 0, 0},
340 {'c', nv_operator, 0, 0},
341 {'d', nv_operator, 0, 0},
342 {'e', nv_wordcmd, 0, FALSE},
343 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
344 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
345 {'h', nv_left, NV_RL, 0},
346 {'i', nv_edit, NV_NCH, 0},
347 {'j', nv_down, 0, FALSE},
348 {'k', nv_up, 0, FALSE},
349 {'l', nv_right, NV_RL, 0},
350 {'m', nv_mark, NV_NCH_NOP, 0},
351 {'n', nv_next, 0, 0},
352 {'o', nv_open, 0, 0},
353 {'p', nv_put, 0, 0},
354 {'q', nv_record, NV_NCH, 0},
355 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
356 {'s', nv_subst, NV_KEEPREG, 0},
357 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
358 {'u', nv_undo, 0, 0},
359 {'w', nv_wordcmd, 0, FALSE},
360 {'x', nv_abbrev, NV_KEEPREG, 0},
361 {'y', nv_operator, 0, 0},
362 {'z', nv_zet, NV_NCH_ALW, 0},
363 {'{', nv_findpar, 0, BACKWARD},
364 {'|', nv_pipe, 0, 0},
365 {'}', nv_findpar, 0, FORWARD},
366 {'~', nv_tilde, 0, 0},
368 /* pound sign */
369 {POUND, nv_ident, 0, 0},
370 #ifdef FEAT_MOUSE
371 {K_MOUSEUP, nv_mousescroll, 0, TRUE},
372 {K_MOUSEDOWN, nv_mousescroll, 0, FALSE},
373 {K_LEFTMOUSE, nv_mouse, 0, 0},
374 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
375 {K_LEFTDRAG, nv_mouse, 0, 0},
376 {K_LEFTRELEASE, nv_mouse, 0, 0},
377 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
378 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
379 {K_MIDDLEDRAG, nv_mouse, 0, 0},
380 {K_MIDDLERELEASE, nv_mouse, 0, 0},
381 {K_RIGHTMOUSE, nv_mouse, 0, 0},
382 {K_RIGHTDRAG, nv_mouse, 0, 0},
383 {K_RIGHTRELEASE, nv_mouse, 0, 0},
384 {K_X1MOUSE, nv_mouse, 0, 0},
385 {K_X1DRAG, nv_mouse, 0, 0},
386 {K_X1RELEASE, nv_mouse, 0, 0},
387 {K_X2MOUSE, nv_mouse, 0, 0},
388 {K_X2DRAG, nv_mouse, 0, 0},
389 {K_X2RELEASE, nv_mouse, 0, 0},
390 #endif
391 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
392 {K_NOP, nv_nop, 0, 0},
393 {K_INS, nv_edit, 0, 0},
394 {K_KINS, nv_edit, 0, 0},
395 {K_BS, nv_ctrlh, 0, 0},
396 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
397 {K_S_UP, nv_page, NV_SS, BACKWARD},
398 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
399 {K_S_DOWN, nv_page, NV_SS, FORWARD},
400 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
401 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
402 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
403 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
404 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
405 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
406 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
407 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
408 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
409 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
410 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
411 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
412 {K_S_END, nv_end, NV_SS, FALSE},
413 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
414 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
415 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
416 {K_S_HOME, nv_home, NV_SS, 0},
417 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
418 {K_DEL, nv_abbrev, 0, 0},
419 {K_KDEL, nv_abbrev, 0, 0},
420 {K_UNDO, nv_kundo, 0, 0},
421 {K_HELP, nv_help, NV_NCW, 0},
422 {K_F1, nv_help, NV_NCW, 0},
423 {K_XF1, nv_help, NV_NCW, 0},
424 #ifdef FEAT_VISUAL
425 {K_SELECT, nv_select, 0, 0},
426 #endif
427 #ifdef FEAT_GUI
428 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
429 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
430 #endif
431 #ifdef FEAT_GUI_TABLINE
432 {K_TABLINE, nv_tabline, 0, 0},
433 {K_TABMENU, nv_tabmenu, 0, 0},
434 #endif
435 #ifdef FEAT_FKMAP
436 {K_F8, farsi_fkey, 0, 0},
437 {K_F9, farsi_fkey, 0, 0},
438 #endif
439 #ifdef FEAT_SNIFF
440 {K_SNIFF, nv_sniff, 0, 0},
441 #endif
442 #ifdef FEAT_NETBEANS_INTG
443 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
444 #endif
445 #ifdef FEAT_DND
446 {K_DROP, nv_drop, NV_STS, 0},
447 #endif
448 #ifdef FEAT_AUTOCMD
449 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
450 #endif
453 /* Number of commands in nv_cmds[]. */
454 #define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
456 /* Sorted index of commands in nv_cmds[]. */
457 static short nv_cmd_idx[NV_CMDS_SIZE];
459 /* The highest index for which
460 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
461 static int nv_max_linear;
464 * Compare functions for qsort() below, that checks the command character
465 * through the index in nv_cmd_idx[].
467 static int
468 #ifdef __BORLANDC__
469 _RTLENTRYF
470 #endif
471 nv_compare(s1, s2)
472 const void *s1;
473 const void *s2;
475 int c1, c2;
477 /* The commands are sorted on absolute value. */
478 c1 = nv_cmds[*(const short *)s1].cmd_char;
479 c2 = nv_cmds[*(const short *)s2].cmd_char;
480 if (c1 < 0)
481 c1 = -c1;
482 if (c2 < 0)
483 c2 = -c2;
484 return c1 - c2;
488 * Initialize the nv_cmd_idx[] table.
490 void
491 init_normal_cmds()
493 int i;
495 /* Fill the index table with a one to one relation. */
496 for (i = 0; i < NV_CMDS_SIZE; ++i)
497 nv_cmd_idx[i] = i;
499 /* Sort the commands by the command character. */
500 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
502 /* Find the first entry that can't be indexed by the command character. */
503 for (i = 0; i < NV_CMDS_SIZE; ++i)
504 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
505 break;
506 nv_max_linear = i - 1;
510 * Search for a command in the commands table.
511 * Returns -1 for invalid command.
513 static int
514 find_command(cmdchar)
515 int cmdchar;
517 int i;
518 int idx;
519 int top, bot;
520 int c;
522 #ifdef FEAT_MBYTE
523 /* A multi-byte character is never a command. */
524 if (cmdchar >= 0x100)
525 return -1;
526 #endif
528 /* We use the absolute value of the character. Special keys have a
529 * negative value, but are sorted on their absolute value. */
530 if (cmdchar < 0)
531 cmdchar = -cmdchar;
533 /* If the character is in the first part: The character is the index into
534 * nv_cmd_idx[]. */
535 if (cmdchar <= nv_max_linear)
536 return nv_cmd_idx[cmdchar];
538 /* Perform a binary search. */
539 bot = nv_max_linear + 1;
540 top = NV_CMDS_SIZE - 1;
541 idx = -1;
542 while (bot <= top)
544 i = (top + bot) / 2;
545 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
546 if (c < 0)
547 c = -c;
548 if (cmdchar == c)
550 idx = nv_cmd_idx[i];
551 break;
553 if (cmdchar > c)
554 bot = i + 1;
555 else
556 top = i - 1;
558 return idx;
562 * Execute a command in Normal mode.
564 /*ARGSUSED*/
565 void
566 normal_cmd(oap, toplevel)
567 oparg_T *oap;
568 int toplevel; /* TRUE when called from main() */
570 cmdarg_T ca; /* command arguments */
571 int c;
572 int ctrl_w = FALSE; /* got CTRL-W command */
573 int old_col = curwin->w_curswant;
574 #ifdef FEAT_CMDL_INFO
575 int need_flushbuf; /* need to call out_flush() */
576 #endif
577 #ifdef FEAT_VISUAL
578 pos_T old_pos; /* cursor position before command */
579 int mapped_len;
580 static int old_mapped_len = 0;
581 #endif
582 int idx;
583 #ifdef FEAT_EVAL
584 int set_prevcount = FALSE;
585 #endif
587 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
588 ca.oap = oap;
590 /* Use a count remembered from before entering an operator. After typing
591 * "3d" we return from normal_cmd() and come back here, the "3" is
592 * remembered in "opcount". */
593 ca.opcount = opcount;
595 #ifdef FEAT_SNIFF
596 want_sniff_request = sniff_connected;
597 #endif
600 * If there is an operator pending, then the command we take this time
601 * will terminate it. Finish_op tells us to finish the operation before
602 * returning this time (unless the operation was cancelled).
604 #ifdef CURSOR_SHAPE
605 c = finish_op;
606 #endif
607 finish_op = (oap->op_type != OP_NOP);
608 #ifdef CURSOR_SHAPE
609 if (finish_op != c)
611 ui_cursor_shape(); /* may show different cursor shape */
612 # ifdef FEAT_MOUSESHAPE
613 update_mouseshape(-1);
614 # endif
616 #endif
618 /* When not finishing an operator and no register name typed, reset the
619 * count. */
620 if (!finish_op && !oap->regname)
622 ca.opcount = 0;
623 #ifdef FEAT_EVAL
624 set_prevcount = TRUE;
625 #endif
628 #ifdef FEAT_AUTOCMD
629 /* Restore counts from before receiving K_CURSORHOLD. This means after
630 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
631 * "3 * 2". */
632 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
634 ca.opcount = oap->prev_opcount;
635 ca.count0 = oap->prev_count0;
636 oap->prev_opcount = 0;
637 oap->prev_count0 = 0;
639 #endif
641 #ifdef FEAT_VISUAL
642 mapped_len = typebuf_maplen();
643 #endif
645 State = NORMAL_BUSY;
646 #ifdef USE_ON_FLY_SCROLL
647 dont_scroll = FALSE; /* allow scrolling here */
648 #endif
651 * Get the command character from the user.
653 c = safe_vgetc();
654 LANGMAP_ADJUST(c, TRUE);
656 #ifdef FEAT_VISUAL
658 * If a mapping was started in Visual or Select mode, remember the length
659 * of the mapping. This is used below to not return to Insert mode for as
660 * long as the mapping is being executed.
662 if (restart_edit == 0)
663 old_mapped_len = 0;
664 else if (old_mapped_len
665 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
666 old_mapped_len = typebuf_maplen();
667 #endif
669 if (c == NUL)
670 c = K_ZERO;
672 #ifdef FEAT_VISUAL
674 * In Select mode, typed text replaces the selection.
676 if (VIsual_active
677 && VIsual_select
678 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
680 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
681 * 'insertmode' is set) fake a "d"elete command, Insert mode will
682 * restart automatically.
683 * Insert the typed character in the typeahead buffer, so that it can
684 * be mapped in Insert mode. Required for ":lmap" to work. */
685 ins_char_typebuf(c);
686 if (restart_edit != 0)
687 c = 'd';
688 else
689 c = 'c';
690 msg_nowait = TRUE; /* don't delay going to insert mode */
692 #endif
694 #ifdef FEAT_CMDL_INFO
695 need_flushbuf = add_to_showcmd(c);
696 #endif
698 getcount:
699 #ifdef FEAT_VISUAL
700 if (!(VIsual_active && VIsual_select))
701 #endif
704 * Handle a count before a command and compute ca.count0.
705 * Note that '0' is a command and not the start of a count, but it's
706 * part of a count after other digits.
708 while ( (c >= '1' && c <= '9')
709 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
711 if (c == K_DEL || c == K_KDEL)
713 ca.count0 /= 10;
714 #ifdef FEAT_CMDL_INFO
715 del_from_showcmd(4); /* delete the digit and ~@% */
716 #endif
718 else
719 ca.count0 = ca.count0 * 10 + (c - '0');
720 if (ca.count0 < 0) /* got too large! */
721 ca.count0 = 999999999L;
722 #ifdef FEAT_EVAL
723 /* Set v:count here, when called from main() and not a stuffed
724 * command, so that v:count can be used in an expression mapping
725 * right after the count. */
726 if (toplevel && stuff_empty())
728 long count = ca.count0;
730 /* multiply with ca.opcount the same way as below */
731 if (ca.opcount != 0)
732 count = ca.opcount * (count == 0 ? 1 : count);
733 set_vcount(count, count == 0 ? 1 : count, set_prevcount);
734 set_prevcount = FALSE; /* only set v:prevcount once */
736 #endif
737 if (ctrl_w)
739 ++no_mapping;
740 ++allow_keys; /* no mapping for nchar, but keys */
742 ++no_zero_mapping; /* don't map zero here */
743 c = plain_vgetc();
744 LANGMAP_ADJUST(c, TRUE);
745 --no_zero_mapping;
746 if (ctrl_w)
748 --no_mapping;
749 --allow_keys;
751 #ifdef FEAT_CMDL_INFO
752 need_flushbuf |= add_to_showcmd(c);
753 #endif
757 * If we got CTRL-W there may be a/another count
759 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
761 ctrl_w = TRUE;
762 ca.opcount = ca.count0; /* remember first count */
763 ca.count0 = 0;
764 ++no_mapping;
765 ++allow_keys; /* no mapping for nchar, but keys */
766 c = plain_vgetc(); /* get next character */
767 LANGMAP_ADJUST(c, TRUE);
768 --no_mapping;
769 --allow_keys;
770 #ifdef FEAT_CMDL_INFO
771 need_flushbuf |= add_to_showcmd(c);
772 #endif
773 goto getcount; /* jump back */
777 #ifdef FEAT_AUTOCMD
778 if (c == K_CURSORHOLD)
780 /* Save the count values so that ca.opcount and ca.count0 are exactly
781 * the same when coming back here after handling K_CURSORHOLD. */
782 oap->prev_opcount = ca.opcount;
783 oap->prev_count0 = ca.count0;
785 else
786 #endif
787 if (ca.opcount != 0)
790 * If we're in the middle of an operator (including after entering a
791 * yank buffer with '"') AND we had a count before the operator, then
792 * that count overrides the current value of ca.count0.
793 * What this means effectively, is that commands like "3dw" get turned
794 * into "d3w" which makes things fall into place pretty neatly.
795 * If you give a count before AND after the operator, they are
796 * multiplied.
798 if (ca.count0)
799 ca.count0 *= ca.opcount;
800 else
801 ca.count0 = ca.opcount;
805 * Always remember the count. It will be set to zero (on the next call,
806 * above) when there is no pending operator.
807 * When called from main(), save the count for use by the "count" built-in
808 * variable.
810 ca.opcount = ca.count0;
811 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
813 #ifdef FEAT_EVAL
815 * Only set v:count when called from main() and not a stuffed command.
817 if (toplevel && stuff_empty())
818 set_vcount(ca.count0, ca.count1, set_prevcount);
819 #endif
822 * Find the command character in the table of commands.
823 * For CTRL-W we already got nchar when looking for a count.
825 if (ctrl_w)
827 ca.nchar = c;
828 ca.cmdchar = Ctrl_W;
830 else
831 ca.cmdchar = c;
832 idx = find_command(ca.cmdchar);
833 if (idx < 0)
835 /* Not a known command: beep. */
836 clearopbeep(oap);
837 goto normal_end;
840 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
842 /* This command is not allowed while editing a ccmdline: beep. */
843 clearopbeep(oap);
844 text_locked_msg();
845 goto normal_end;
847 #ifdef FEAT_AUTOCMD
848 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
849 goto normal_end;
850 #endif
852 #ifdef FEAT_VISUAL
854 * In Visual/Select mode, a few keys are handled in a special way.
856 if (VIsual_active)
858 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
859 if (km_stopsel
860 && (nv_cmds[idx].cmd_flags & NV_STS)
861 && !(mod_mask & MOD_MASK_SHIFT))
863 end_visual_mode();
864 redraw_curbuf_later(INVERTED);
867 /* Keys that work different when 'keymodel' contains "startsel" */
868 if (km_startsel)
870 if (nv_cmds[idx].cmd_flags & NV_SS)
872 unshift_special(&ca);
873 idx = find_command(ca.cmdchar);
874 if (idx < 0)
876 /* Just in case */
877 clearopbeep(oap);
878 goto normal_end;
881 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
882 && (mod_mask & MOD_MASK_SHIFT))
884 mod_mask &= ~MOD_MASK_SHIFT;
888 #endif
890 #ifdef FEAT_RIGHTLEFT
891 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
892 && (nv_cmds[idx].cmd_flags & NV_RL))
894 /* Invert horizontal movements and operations. Only when typed by the
895 * user directly, not when the result of a mapping or "x" translated
896 * to "dl". */
897 switch (ca.cmdchar)
899 case 'l': ca.cmdchar = 'h'; break;
900 case K_RIGHT: ca.cmdchar = K_LEFT; break;
901 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
902 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
903 case 'h': ca.cmdchar = 'l'; break;
904 case K_LEFT: ca.cmdchar = K_RIGHT; break;
905 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
906 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
907 case '>': ca.cmdchar = '<'; break;
908 case '<': ca.cmdchar = '>'; break;
910 idx = find_command(ca.cmdchar);
912 #endif
915 * Get an additional character if we need one.
917 if ((nv_cmds[idx].cmd_flags & NV_NCH)
918 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
919 && oap->op_type == OP_NOP)
920 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
921 || (ca.cmdchar == 'q'
922 && oap->op_type == OP_NOP
923 && !Recording
924 && !Exec_reg)
925 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
926 && (oap->op_type != OP_NOP
927 #ifdef FEAT_VISUAL
928 || VIsual_active
929 #endif
930 ))))
932 int *cp;
933 int repl = FALSE; /* get character for replace mode */
934 int lit = FALSE; /* get extra character literally */
935 int langmap_active = FALSE; /* using :lmap mappings */
936 int lang; /* getting a text character */
937 #ifdef USE_IM_CONTROL
938 int save_smd; /* saved value of p_smd */
939 #endif
941 ++no_mapping;
942 ++allow_keys; /* no mapping for nchar, but allow key codes */
943 #ifdef FEAT_AUTOCMD
944 /* Don't generate a CursorHold event here, most commands can't handle
945 * it, e.g., nv_replace(), nv_csearch(). */
946 did_cursorhold = TRUE;
947 #endif
948 if (ca.cmdchar == 'g')
951 * For 'g' get the next character now, so that we can check for
952 * "gr", "g'" and "g`".
954 ca.nchar = plain_vgetc();
955 LANGMAP_ADJUST(ca.nchar, TRUE);
956 #ifdef FEAT_CMDL_INFO
957 need_flushbuf |= add_to_showcmd(ca.nchar);
958 #endif
959 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
960 || ca.nchar == Ctrl_BSL)
962 cp = &ca.extra_char; /* need to get a third character */
963 if (ca.nchar != 'r')
964 lit = TRUE; /* get it literally */
965 else
966 repl = TRUE; /* get it in replace mode */
968 else
969 cp = NULL; /* no third character needed */
971 else
973 if (ca.cmdchar == 'r') /* get it in replace mode */
974 repl = TRUE;
975 cp = &ca.nchar;
977 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
980 * Get a second or third character.
982 if (cp != NULL)
984 #ifdef CURSOR_SHAPE
985 if (repl)
987 State = REPLACE; /* pretend Replace mode */
988 ui_cursor_shape(); /* show different cursor shape */
990 #endif
991 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
993 /* Allow mappings defined with ":lmap". */
994 --no_mapping;
995 --allow_keys;
996 if (repl)
997 State = LREPLACE;
998 else
999 State = LANGMAP;
1000 langmap_active = TRUE;
1002 #ifdef USE_IM_CONTROL
1003 save_smd = p_smd;
1004 p_smd = FALSE; /* Don't let the IM code show the mode here */
1005 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
1006 im_set_active(TRUE);
1007 #endif
1009 *cp = plain_vgetc();
1011 if (langmap_active)
1013 /* Undo the decrement done above */
1014 ++no_mapping;
1015 ++allow_keys;
1016 State = NORMAL_BUSY;
1018 #ifdef USE_IM_CONTROL
1019 if (lang)
1021 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
1022 im_save_status(&curbuf->b_p_iminsert);
1023 im_set_active(FALSE);
1025 p_smd = save_smd;
1026 #endif
1027 #ifdef CURSOR_SHAPE
1028 State = NORMAL_BUSY;
1029 #endif
1030 #ifdef FEAT_CMDL_INFO
1031 need_flushbuf |= add_to_showcmd(*cp);
1032 #endif
1034 if (!lit)
1036 #ifdef FEAT_DIGRAPHS
1037 /* Typing CTRL-K gets a digraph. */
1038 if (*cp == Ctrl_K
1039 && ((nv_cmds[idx].cmd_flags & NV_LANG)
1040 || cp == &ca.extra_char)
1041 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1043 c = get_digraph(FALSE);
1044 if (c > 0)
1046 *cp = c;
1047 # ifdef FEAT_CMDL_INFO
1048 /* Guessing how to update showcmd here... */
1049 del_from_showcmd(3);
1050 need_flushbuf |= add_to_showcmd(*cp);
1051 # endif
1054 #endif
1056 /* adjust chars > 127, except after "tTfFr" commands */
1057 LANGMAP_ADJUST(*cp, !lang);
1058 #ifdef FEAT_RIGHTLEFT
1059 /* adjust Hebrew mapped char */
1060 if (p_hkmap && lang && KeyTyped)
1061 *cp = hkmap(*cp);
1062 # ifdef FEAT_FKMAP
1063 /* adjust Farsi mapped char */
1064 if (p_fkmap && lang && KeyTyped)
1065 *cp = fkmap(*cp);
1066 # endif
1067 #endif
1071 * When the next character is CTRL-\ a following CTRL-N means the
1072 * command is aborted and we go to Normal mode.
1074 if (cp == &ca.extra_char
1075 && ca.nchar == Ctrl_BSL
1076 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1078 ca.cmdchar = Ctrl_BSL;
1079 ca.nchar = ca.extra_char;
1080 idx = find_command(ca.cmdchar);
1082 else if (*cp == Ctrl_BSL)
1084 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1086 /* There is a busy wait here when typing "f<C-\>" and then
1087 * something different from CTRL-N. Can't be avoided. */
1088 while ((c = vpeekc()) <= 0 && towait > 0L)
1090 do_sleep(towait > 50L ? 50L : towait);
1091 towait -= 50L;
1093 if (c > 0)
1095 c = plain_vgetc();
1096 if (c != Ctrl_N && c != Ctrl_G)
1097 vungetc(c);
1098 else
1100 ca.cmdchar = Ctrl_BSL;
1101 ca.nchar = c;
1102 idx = find_command(ca.cmdchar);
1107 #ifdef FEAT_MBYTE
1108 /* When getting a text character and the next character is a
1109 * multi-byte character, it could be a composing character.
1110 * However, don't wait for it to arrive. */
1111 while (enc_utf8 && lang && (c = vpeekc()) > 0
1112 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1114 c = plain_vgetc();
1115 if (!utf_iscomposing(c))
1117 vungetc(c); /* it wasn't, put it back */
1118 break;
1120 else if (ca.ncharC1 == 0)
1121 ca.ncharC1 = c;
1122 else
1123 ca.ncharC2 = c;
1125 #endif
1127 --no_mapping;
1128 --allow_keys;
1131 #ifdef FEAT_CMDL_INFO
1133 * Flush the showcmd characters onto the screen so we can see them while
1134 * the command is being executed. Only do this when the shown command was
1135 * actually displayed, otherwise this will slow down a lot when executing
1136 * mappings.
1138 if (need_flushbuf)
1139 out_flush();
1140 #endif
1141 #ifdef FEAT_AUTOCMD
1142 if (ca.cmdchar != K_IGNORE)
1143 did_cursorhold = FALSE;
1144 #endif
1146 State = NORMAL;
1148 if (ca.nchar == ESC)
1150 clearop(oap);
1151 if (restart_edit == 0 && goto_im())
1152 restart_edit = 'a';
1153 goto normal_end;
1156 if (ca.cmdchar != K_IGNORE)
1158 msg_didout = FALSE; /* don't scroll screen up for normal command */
1159 msg_col = 0;
1162 #ifdef FEAT_VISUAL
1163 old_pos = curwin->w_cursor; /* remember where cursor was */
1165 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1166 * mode. */
1167 if (!VIsual_active && km_startsel)
1169 if (nv_cmds[idx].cmd_flags & NV_SS)
1171 start_selection();
1172 unshift_special(&ca);
1173 idx = find_command(ca.cmdchar);
1175 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1176 && (mod_mask & MOD_MASK_SHIFT))
1178 start_selection();
1179 mod_mask &= ~MOD_MASK_SHIFT;
1182 #endif
1185 * Execute the command!
1186 * Call the command function found in the commands table.
1188 ca.arg = nv_cmds[idx].cmd_arg;
1189 (nv_cmds[idx].cmd_func)(&ca);
1192 * If we didn't start or finish an operator, reset oap->regname, unless we
1193 * need it later.
1195 if (!finish_op
1196 && !oap->op_type
1197 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1199 clearop(oap);
1200 #ifdef FEAT_EVAL
1201 set_reg_var('"');
1202 #endif
1205 #ifdef FEAT_VISUAL
1206 /* Get the length of mapped chars again after typing a count, second
1207 * character or "z333<cr>". */
1208 if (old_mapped_len > 0)
1209 old_mapped_len = typebuf_maplen();
1210 #endif
1213 * If an operation is pending, handle it...
1215 do_pending_operator(&ca, old_col, FALSE);
1218 * Wait for a moment when a message is displayed that will be overwritten
1219 * by the mode message.
1220 * In Visual mode and with "^O" in Insert mode, a short message will be
1221 * overwritten by the mode message. Wait a bit, until a key is hit.
1222 * In Visual mode, it's more important to keep the Visual area updated
1223 * than keeping a message (e.g. from a /pat search).
1224 * Only do this if the command was typed, not from a mapping.
1225 * Don't wait when emsg_silent is non-zero.
1226 * Also wait a bit after an error message, e.g. for "^O:".
1227 * Don't redraw the screen, it would remove the message.
1229 if ( ((p_smd
1230 && msg_silent == 0
1231 && (restart_edit != 0
1232 #ifdef FEAT_VISUAL
1233 || (VIsual_active
1234 && old_pos.lnum == curwin->w_cursor.lnum
1235 && old_pos.col == curwin->w_cursor.col)
1236 #endif
1238 && (clear_cmdline
1239 || redraw_cmdline)
1240 && (msg_didout || (msg_didany && msg_scroll))
1241 && !msg_nowait
1242 && KeyTyped)
1243 || (restart_edit != 0
1244 #ifdef FEAT_VISUAL
1245 && !VIsual_active
1246 #endif
1247 && (msg_scroll
1248 || emsg_on_display)))
1249 && oap->regname == 0
1250 && !(ca.retval & CA_COMMAND_BUSY)
1251 && stuff_empty()
1252 && typebuf_typed()
1253 && emsg_silent == 0
1254 && !did_wait_return
1255 && oap->op_type == OP_NOP)
1257 int save_State = State;
1259 /* Draw the cursor with the right shape here */
1260 if (restart_edit != 0)
1261 State = INSERT;
1263 /* If need to redraw, and there is a "keep_msg", redraw before the
1264 * delay */
1265 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1267 char_u *kmsg;
1269 kmsg = keep_msg;
1270 keep_msg = NULL;
1271 /* showmode() will clear keep_msg, but we want to use it anyway */
1272 update_screen(0);
1273 /* now reset it, otherwise it's put in the history again */
1274 keep_msg = kmsg;
1275 msg_attr(kmsg, keep_msg_attr);
1276 vim_free(kmsg);
1278 setcursor();
1279 cursor_on();
1280 out_flush();
1281 if (msg_scroll || emsg_on_display)
1282 ui_delay(1000L, TRUE); /* wait at least one second */
1283 ui_delay(3000L, FALSE); /* wait up to three seconds */
1284 State = save_State;
1286 msg_scroll = FALSE;
1287 emsg_on_display = FALSE;
1291 * Finish up after executing a Normal mode command.
1293 normal_end:
1295 msg_nowait = FALSE;
1297 /* Reset finish_op, in case it was set */
1298 #ifdef CURSOR_SHAPE
1299 c = finish_op;
1300 #endif
1301 finish_op = FALSE;
1302 #ifdef CURSOR_SHAPE
1303 /* Redraw the cursor with another shape, if we were in Operator-pending
1304 * mode or did a replace command. */
1305 if (c || ca.cmdchar == 'r')
1307 ui_cursor_shape(); /* may show different cursor shape */
1308 # ifdef FEAT_MOUSESHAPE
1309 update_mouseshape(-1);
1310 # endif
1312 #endif
1314 #ifdef FEAT_CMDL_INFO
1315 if (oap->op_type == OP_NOP && oap->regname == 0
1316 # ifdef FEAT_AUTOCMD
1317 && ca.cmdchar != K_CURSORHOLD
1318 # endif
1320 clear_showcmd();
1321 #endif
1323 checkpcmark(); /* check if we moved since setting pcmark */
1324 vim_free(ca.searchbuf);
1326 #ifdef FEAT_MBYTE
1327 if (has_mbyte)
1328 mb_adjust_cursor();
1329 #endif
1331 #ifdef FEAT_SCROLLBIND
1332 if (curwin->w_p_scb && toplevel)
1334 validate_cursor(); /* may need to update w_leftcol */
1335 do_check_scrollbind(TRUE);
1337 #endif
1340 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1341 * if still inside a mapping that started in Visual mode).
1342 * May switch from Visual to Select mode after CTRL-O command.
1344 if ( oap->op_type == OP_NOP
1345 #ifdef FEAT_VISUAL
1346 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1347 || restart_VIsual_select == 1)
1348 #else
1349 && restart_edit != 0
1350 #endif
1351 && !(ca.retval & CA_COMMAND_BUSY)
1352 && stuff_empty()
1353 && oap->regname == 0)
1355 #ifdef FEAT_VISUAL
1356 if (restart_VIsual_select == 1)
1358 VIsual_select = TRUE;
1359 showmode();
1360 restart_VIsual_select = 0;
1362 #endif
1363 if (restart_edit != 0
1364 #ifdef FEAT_VISUAL
1365 && !VIsual_active && old_mapped_len == 0
1366 #endif
1368 (void)edit(restart_edit, FALSE, 1L);
1371 #ifdef FEAT_VISUAL
1372 if (restart_VIsual_select == 2)
1373 restart_VIsual_select = 1;
1374 #endif
1376 /* Save count before an operator for next time. */
1377 opcount = ca.opcount;
1381 * Handle an operator after visual mode or when the movement is finished
1383 void
1384 do_pending_operator(cap, old_col, gui_yank)
1385 cmdarg_T *cap;
1386 int old_col;
1387 int gui_yank;
1389 oparg_T *oap = cap->oap;
1390 pos_T old_cursor;
1391 int empty_region_error;
1392 int restart_edit_save;
1394 #ifdef FEAT_VISUAL
1395 /* The visual area is remembered for redo */
1396 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1397 static linenr_T redo_VIsual_line_count; /* number of lines */
1398 static colnr_T redo_VIsual_col; /* number of cols or end column */
1399 static long redo_VIsual_count; /* count for Visual operator */
1400 # ifdef FEAT_VIRTUALEDIT
1401 int include_line_break = FALSE;
1402 # endif
1403 #endif
1405 #if defined(FEAT_CLIPBOARD)
1407 * Yank the visual area into the GUI selection register before we operate
1408 * on it and lose it forever.
1409 * Don't do it if a specific register was specified, so that ""x"*P works.
1410 * This could call do_pending_operator() recursively, but that's OK
1411 * because gui_yank will be TRUE for the nested call.
1413 if (clip_star.available
1414 && oap->op_type != OP_NOP
1415 && !gui_yank
1416 # ifdef FEAT_VISUAL
1417 && VIsual_active
1418 && !redo_VIsual_busy
1419 # endif
1420 && oap->regname == 0)
1421 clip_auto_select();
1422 #endif
1423 old_cursor = curwin->w_cursor;
1426 * If an operation is pending, handle it...
1428 if ((finish_op
1429 #ifdef FEAT_VISUAL
1430 || VIsual_active
1431 #endif
1432 ) && oap->op_type != OP_NOP)
1434 #ifdef FEAT_VISUAL
1435 oap->is_VIsual = VIsual_active;
1436 if (oap->motion_force == 'V')
1437 oap->motion_type = MLINE;
1438 else if (oap->motion_force == 'v')
1440 /* If the motion was linewise, "inclusive" will not have been set.
1441 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1442 if (oap->motion_type == MLINE)
1443 oap->inclusive = FALSE;
1444 /* If the motion already was characterwise, toggle "inclusive" */
1445 else if (oap->motion_type == MCHAR)
1446 oap->inclusive = !oap->inclusive;
1447 oap->motion_type = MCHAR;
1449 else if (oap->motion_force == Ctrl_V)
1451 /* Change line- or characterwise motion into Visual block mode. */
1452 VIsual_active = TRUE;
1453 VIsual = oap->start;
1454 VIsual_mode = Ctrl_V;
1455 VIsual_select = FALSE;
1456 VIsual_reselect = FALSE;
1458 #endif
1460 /* only redo yank when 'y' flag is in 'cpoptions' */
1461 /* never redo "zf" (define fold) */
1462 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1463 #ifdef FEAT_VISUAL
1464 && (!VIsual_active || oap->motion_force)
1465 #endif
1466 && cap->cmdchar != 'D'
1467 #ifdef FEAT_FOLDING
1468 && oap->op_type != OP_FOLD
1469 && oap->op_type != OP_FOLDOPEN
1470 && oap->op_type != OP_FOLDOPENREC
1471 && oap->op_type != OP_FOLDCLOSE
1472 && oap->op_type != OP_FOLDCLOSEREC
1473 && oap->op_type != OP_FOLDDEL
1474 && oap->op_type != OP_FOLDDELREC
1475 #endif
1478 prep_redo(oap->regname, cap->count0,
1479 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1480 oap->motion_force, cap->cmdchar, cap->nchar);
1481 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1484 * If 'cpoptions' does not contain 'r', insert the search
1485 * pattern to really repeat the same command.
1487 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
1488 AppendToRedobuffLit(cap->searchbuf, -1);
1489 AppendToRedobuff(NL_STR);
1491 else if (cap->cmdchar == ':')
1493 /* do_cmdline() has stored the first typed line in
1494 * "repeat_cmdline". When several lines are typed repeating
1495 * won't be possible. */
1496 if (repeat_cmdline == NULL)
1497 ResetRedobuff();
1498 else
1500 AppendToRedobuffLit(repeat_cmdline, -1);
1501 AppendToRedobuff(NL_STR);
1502 vim_free(repeat_cmdline);
1503 repeat_cmdline = NULL;
1508 #ifdef FEAT_VISUAL
1509 if (redo_VIsual_busy)
1511 oap->start = curwin->w_cursor;
1512 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1513 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1514 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1515 VIsual_mode = redo_VIsual_mode;
1516 if (VIsual_mode == 'v')
1518 if (redo_VIsual_line_count <= 1)
1519 curwin->w_cursor.col += redo_VIsual_col - 1;
1520 else
1521 curwin->w_cursor.col = redo_VIsual_col;
1523 if (redo_VIsual_col == MAXCOL)
1525 curwin->w_curswant = MAXCOL;
1526 coladvance((colnr_T)MAXCOL);
1528 cap->count0 = redo_VIsual_count;
1529 if (redo_VIsual_count != 0)
1530 cap->count1 = redo_VIsual_count;
1531 else
1532 cap->count1 = 1;
1534 else if (VIsual_active)
1536 if (!gui_yank)
1538 /* Save the current VIsual area for '< and '> marks, and "gv" */
1539 curbuf->b_visual.vi_start = VIsual;
1540 curbuf->b_visual.vi_end = curwin->w_cursor;
1541 curbuf->b_visual.vi_mode = VIsual_mode;
1542 curbuf->b_visual.vi_curswant = curwin->w_curswant;
1543 # ifdef FEAT_EVAL
1544 curbuf->b_visual_mode_eval = VIsual_mode;
1545 # endif
1548 /* In Select mode, a linewise selection is operated upon like a
1549 * characterwise selection. */
1550 if (VIsual_select && VIsual_mode == 'V')
1552 if (lt(VIsual, curwin->w_cursor))
1554 VIsual.col = 0;
1555 curwin->w_cursor.col =
1556 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1558 else
1560 curwin->w_cursor.col = 0;
1561 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1563 VIsual_mode = 'v';
1565 /* If 'selection' is "exclusive", backup one character for
1566 * charwise selections. */
1567 else if (VIsual_mode == 'v')
1569 # ifdef FEAT_VIRTUALEDIT
1570 include_line_break =
1571 # endif
1572 unadjust_for_sel();
1575 oap->start = VIsual;
1576 if (VIsual_mode == 'V')
1577 oap->start.col = 0;
1579 #endif /* FEAT_VISUAL */
1582 * Set oap->start to the first position of the operated text, oap->end
1583 * to the end of the operated text. w_cursor is equal to oap->start.
1585 if (lt(oap->start, curwin->w_cursor))
1587 #ifdef FEAT_FOLDING
1588 /* Include folded lines completely. */
1589 if (!VIsual_active)
1591 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1592 oap->start.col = 0;
1593 if (hasFolding(curwin->w_cursor.lnum, NULL,
1594 &curwin->w_cursor.lnum))
1595 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1597 #endif
1598 oap->end = curwin->w_cursor;
1599 curwin->w_cursor = oap->start;
1601 /* w_virtcol may have been updated; if the cursor goes back to its
1602 * previous position w_virtcol becomes invalid and isn't updated
1603 * automatically. */
1604 curwin->w_valid &= ~VALID_VIRTCOL;
1606 else
1608 #ifdef FEAT_FOLDING
1609 /* Include folded lines completely. */
1610 if (!VIsual_active && oap->motion_type == MLINE)
1612 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1613 NULL))
1614 curwin->w_cursor.col = 0;
1615 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1616 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1618 #endif
1619 oap->end = oap->start;
1620 oap->start = curwin->w_cursor;
1623 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1625 #ifdef FEAT_VIRTUALEDIT
1626 /* Set "virtual_op" before resetting VIsual_active. */
1627 virtual_op = virtual_active();
1628 #endif
1630 #ifdef FEAT_VISUAL
1631 if (VIsual_active || redo_VIsual_busy)
1633 if (VIsual_mode == Ctrl_V) /* block mode */
1635 colnr_T start, end;
1637 oap->block_mode = TRUE;
1639 getvvcol(curwin, &(oap->start),
1640 &oap->start_vcol, NULL, &oap->end_vcol);
1641 if (!redo_VIsual_busy)
1643 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1645 if (start < oap->start_vcol)
1646 oap->start_vcol = start;
1647 if (end > oap->end_vcol)
1649 if (*p_sel == 'e' && start >= 1
1650 && start - 1 >= oap->end_vcol)
1651 oap->end_vcol = start - 1;
1652 else
1653 oap->end_vcol = end;
1657 /* if '$' was used, get oap->end_vcol from longest line */
1658 if (curwin->w_curswant == MAXCOL)
1660 curwin->w_cursor.col = MAXCOL;
1661 oap->end_vcol = 0;
1662 for (curwin->w_cursor.lnum = oap->start.lnum;
1663 curwin->w_cursor.lnum <= oap->end.lnum;
1664 ++curwin->w_cursor.lnum)
1666 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1667 if (end > oap->end_vcol)
1668 oap->end_vcol = end;
1671 else if (redo_VIsual_busy)
1672 oap->end_vcol = oap->start_vcol + redo_VIsual_col - 1;
1674 * Correct oap->end.col and oap->start.col to be the
1675 * upper-left and lower-right corner of the block area.
1677 * (Actually, this does convert column positions into character
1678 * positions)
1680 curwin->w_cursor.lnum = oap->end.lnum;
1681 coladvance(oap->end_vcol);
1682 oap->end = curwin->w_cursor;
1684 curwin->w_cursor = oap->start;
1685 coladvance(oap->start_vcol);
1686 oap->start = curwin->w_cursor;
1689 if (!redo_VIsual_busy && !gui_yank)
1692 * Prepare to reselect and redo Visual: this is based on the
1693 * size of the Visual text
1695 resel_VIsual_mode = VIsual_mode;
1696 if (curwin->w_curswant == MAXCOL)
1697 resel_VIsual_col = MAXCOL;
1698 else if (VIsual_mode == Ctrl_V)
1699 resel_VIsual_col = oap->end_vcol - oap->start_vcol + 1;
1700 else if (oap->line_count > 1)
1701 resel_VIsual_col = oap->end.col;
1702 else
1703 resel_VIsual_col = oap->end.col - oap->start.col + 1;
1704 resel_VIsual_line_count = oap->line_count;
1707 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1708 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1709 && oap->op_type != OP_COLON
1710 #ifdef FEAT_FOLDING
1711 && oap->op_type != OP_FOLD
1712 && oap->op_type != OP_FOLDOPEN
1713 && oap->op_type != OP_FOLDOPENREC
1714 && oap->op_type != OP_FOLDCLOSE
1715 && oap->op_type != OP_FOLDCLOSEREC
1716 && oap->op_type != OP_FOLDDEL
1717 && oap->op_type != OP_FOLDDELREC
1718 #endif
1719 && oap->motion_force == NUL
1722 /* Prepare for redoing. Only use the nchar field for "r",
1723 * otherwise it might be the second char of the operator. */
1724 prep_redo(oap->regname, 0L, NUL, 'v',
1725 get_op_char(oap->op_type),
1726 get_extra_op_char(oap->op_type),
1727 oap->op_type == OP_REPLACE ? cap->nchar : NUL);
1728 if (!redo_VIsual_busy)
1730 redo_VIsual_mode = resel_VIsual_mode;
1731 redo_VIsual_col = resel_VIsual_col;
1732 redo_VIsual_line_count = resel_VIsual_line_count;
1733 redo_VIsual_count = cap->count0;
1738 * oap->inclusive defaults to TRUE.
1739 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1740 * FALSE. This makes "d}P" and "v}dP" work the same.
1742 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1743 oap->inclusive = TRUE;
1744 if (VIsual_mode == 'V')
1745 oap->motion_type = MLINE;
1746 else
1748 oap->motion_type = MCHAR;
1749 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1750 # ifdef FEAT_VIRTUALEDIT
1751 && (include_line_break || !virtual_op)
1752 # endif
1755 oap->inclusive = FALSE;
1756 /* Try to include the newline, unless it's an operator
1757 * that works on lines only */
1758 if (*p_sel != 'o'
1759 && !op_on_lines(oap->op_type)
1760 && oap->end.lnum < curbuf->b_ml.ml_line_count)
1762 ++oap->end.lnum;
1763 oap->end.col = 0;
1764 # ifdef FEAT_VIRTUALEDIT
1765 oap->end.coladd = 0;
1766 # endif
1767 ++oap->line_count;
1772 redo_VIsual_busy = FALSE;
1775 * Switch Visual off now, so screen updating does
1776 * not show inverted text when the screen is redrawn.
1777 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1778 * no screen redraw, so it is done here to remove the inverted
1779 * part.
1781 if (!gui_yank)
1783 VIsual_active = FALSE;
1784 # ifdef FEAT_MOUSE
1785 setmouse();
1786 mouse_dragging = 0;
1787 # endif
1788 if (mode_displayed)
1789 clear_cmdline = TRUE; /* unshow visual mode later */
1790 #ifdef FEAT_CMDL_INFO
1791 else
1792 clear_showcmd();
1793 #endif
1794 if ((oap->op_type == OP_YANK
1795 || oap->op_type == OP_COLON
1796 || oap->op_type == OP_FUNCTION
1797 || oap->op_type == OP_FILTER)
1798 && oap->motion_force == NUL)
1799 redraw_curbuf_later(INVERTED);
1802 #endif
1804 #ifdef FEAT_MBYTE
1805 /* Include the trailing byte of a multi-byte char. */
1806 if (has_mbyte && oap->inclusive)
1808 int l;
1810 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
1811 if (l > 1)
1812 oap->end.col += l - 1;
1814 #endif
1815 curwin->w_set_curswant = TRUE;
1818 * oap->empty is set when start and end are the same. The inclusive
1819 * flag affects this too, unless yanking and the end is on a NUL.
1821 oap->empty = (oap->motion_type == MCHAR
1822 && (!oap->inclusive
1823 || (oap->op_type == OP_YANK
1824 && gchar_pos(&oap->end) == NUL))
1825 && equalpos(oap->start, oap->end)
1826 #ifdef FEAT_VIRTUALEDIT
1827 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1828 #endif
1831 * For delete, change and yank, it's an error to operate on an
1832 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1834 empty_region_error = (oap->empty
1835 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1837 #ifdef FEAT_VISUAL
1838 /* Force a redraw when operating on an empty Visual region, when
1839 * 'modifiable is off or creating a fold. */
1840 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1841 # ifdef FEAT_FOLDING
1842 || oap->op_type == OP_FOLD
1843 # endif
1845 redraw_curbuf_later(INVERTED);
1846 #endif
1849 * If the end of an operator is in column one while oap->motion_type
1850 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1851 * character in the previous line. If op_start is on or before the
1852 * first non-blank in the line, the operator becomes linewise
1853 * (strange, but that's the way vi does it).
1855 if ( oap->motion_type == MCHAR
1856 && oap->inclusive == FALSE
1857 && !(cap->retval & CA_NO_ADJ_OP_END)
1858 && oap->end.col == 0
1859 #ifdef FEAT_VISUAL
1860 && (!oap->is_VIsual || *p_sel == 'o')
1861 && !oap->block_mode
1862 #endif
1863 && oap->line_count > 1)
1865 oap->end_adjusted = TRUE; /* remember that we did this */
1866 --oap->line_count;
1867 --oap->end.lnum;
1868 if (inindent(0))
1869 oap->motion_type = MLINE;
1870 else
1872 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1873 if (oap->end.col)
1875 --oap->end.col;
1876 oap->inclusive = TRUE;
1880 else
1881 oap->end_adjusted = FALSE;
1883 switch (oap->op_type)
1885 case OP_LSHIFT:
1886 case OP_RSHIFT:
1887 op_shift(oap, TRUE,
1888 #ifdef FEAT_VISUAL
1889 oap->is_VIsual ? (int)cap->count1 :
1890 #endif
1892 auto_format(FALSE, TRUE);
1893 break;
1895 case OP_JOIN_NS:
1896 case OP_JOIN:
1897 if (oap->line_count < 2)
1898 oap->line_count = 2;
1899 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1900 curbuf->b_ml.ml_line_count)
1901 beep_flush();
1902 else
1904 do_do_join(oap->line_count, oap->op_type == OP_JOIN);
1905 auto_format(FALSE, TRUE);
1907 break;
1909 case OP_DELETE:
1910 #ifdef FEAT_VISUAL
1911 VIsual_reselect = FALSE; /* don't reselect now */
1912 #endif
1913 if (empty_region_error)
1914 vim_beep();
1915 else
1917 (void)op_delete(oap);
1918 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1919 u_save_cursor(); /* cursor line wasn't saved yet */
1920 auto_format(FALSE, TRUE);
1922 break;
1924 case OP_YANK:
1925 if (empty_region_error)
1927 if (!gui_yank)
1928 vim_beep();
1930 else
1931 (void)op_yank(oap, FALSE, !gui_yank);
1932 check_cursor_col();
1933 break;
1935 case OP_CHANGE:
1936 #ifdef FEAT_VISUAL
1937 VIsual_reselect = FALSE; /* don't reselect now */
1938 #endif
1939 if (empty_region_error)
1940 vim_beep();
1941 else
1943 /* This is a new edit command, not a restart. Need to
1944 * remember it to make 'insertmode' work with mappings for
1945 * Visual mode. But do this only once and not when typed and
1946 * 'insertmode' isn't set. */
1947 if (p_im || !KeyTyped)
1948 restart_edit_save = restart_edit;
1949 else
1950 restart_edit_save = 0;
1951 restart_edit = 0;
1952 /* Reset finish_op now, don't want it set inside edit(). */
1953 finish_op = FALSE;
1954 if (op_change(oap)) /* will call edit() */
1955 cap->retval |= CA_COMMAND_BUSY;
1956 if (restart_edit == 0)
1957 restart_edit = restart_edit_save;
1959 break;
1961 case OP_FILTER:
1962 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1963 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1964 else
1965 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
1967 case OP_INDENT:
1968 case OP_COLON:
1970 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1972 * If 'equalprg' is empty, do the indenting internally.
1974 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1976 # ifdef FEAT_LISP
1977 if (curbuf->b_p_lisp)
1979 op_reindent(oap, get_lisp_indent);
1980 break;
1982 # endif
1983 # ifdef FEAT_CINDENT
1984 op_reindent(oap,
1985 # ifdef FEAT_EVAL
1986 *curbuf->b_p_inde != NUL ? get_expr_indent :
1987 # endif
1988 get_c_indent);
1989 break;
1990 # endif
1992 #endif
1994 op_colon(oap);
1995 break;
1997 case OP_TILDE:
1998 case OP_UPPER:
1999 case OP_LOWER:
2000 case OP_ROT13:
2001 if (empty_region_error)
2002 vim_beep();
2003 else
2004 op_tilde(oap);
2005 check_cursor_col();
2006 break;
2008 case OP_FORMAT:
2009 #if defined(FEAT_EVAL)
2010 if (*curbuf->b_p_fex != NUL)
2011 op_formatexpr(oap); /* use expression */
2012 else
2013 #endif
2014 if (*p_fp != NUL)
2015 op_colon(oap); /* use external command */
2016 else
2017 op_format(oap, FALSE); /* use internal function */
2018 break;
2020 case OP_FORMAT2:
2021 op_format(oap, TRUE); /* use internal function */
2022 break;
2024 case OP_FUNCTION:
2025 op_function(oap); /* call 'operatorfunc' */
2026 break;
2028 case OP_INSERT:
2029 case OP_APPEND:
2030 #ifdef FEAT_VISUAL
2031 VIsual_reselect = FALSE; /* don't reselect now */
2032 #endif
2033 #ifdef FEAT_VISUALEXTRA
2034 if (empty_region_error)
2035 vim_beep();
2036 else
2038 /* This is a new edit command, not a restart. Need to
2039 * remember it to make 'insertmode' work with mappings for
2040 * Visual mode. But do this only once. */
2041 restart_edit_save = restart_edit;
2042 restart_edit = 0;
2044 op_insert(oap, cap->count1);
2046 /* TODO: when inserting in several lines, should format all
2047 * the lines. */
2048 auto_format(FALSE, TRUE);
2050 if (restart_edit == 0)
2051 restart_edit = restart_edit_save;
2053 #else
2054 vim_beep();
2055 #endif
2056 break;
2058 case OP_REPLACE:
2059 #ifdef FEAT_VISUAL
2060 VIsual_reselect = FALSE; /* don't reselect now */
2061 #endif
2062 #ifdef FEAT_VISUALEXTRA
2063 if (empty_region_error)
2064 #endif
2065 vim_beep();
2066 #ifdef FEAT_VISUALEXTRA
2067 else
2068 op_replace(oap, cap->nchar);
2069 #endif
2070 break;
2072 #ifdef FEAT_FOLDING
2073 case OP_FOLD:
2074 VIsual_reselect = FALSE; /* don't reselect now */
2075 foldCreate(oap->start.lnum, oap->end.lnum);
2076 break;
2078 case OP_FOLDOPEN:
2079 case OP_FOLDOPENREC:
2080 case OP_FOLDCLOSE:
2081 case OP_FOLDCLOSEREC:
2082 VIsual_reselect = FALSE; /* don't reselect now */
2083 opFoldRange(oap->start.lnum, oap->end.lnum,
2084 oap->op_type == OP_FOLDOPEN
2085 || oap->op_type == OP_FOLDOPENREC,
2086 oap->op_type == OP_FOLDOPENREC
2087 || oap->op_type == OP_FOLDCLOSEREC,
2088 oap->is_VIsual);
2089 break;
2091 case OP_FOLDDEL:
2092 case OP_FOLDDELREC:
2093 VIsual_reselect = FALSE; /* don't reselect now */
2094 deleteFold(oap->start.lnum, oap->end.lnum,
2095 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2096 break;
2097 #endif
2098 default:
2099 clearopbeep(oap);
2101 #ifdef FEAT_VIRTUALEDIT
2102 virtual_op = MAYBE;
2103 #endif
2104 if (!gui_yank)
2107 * if 'sol' not set, go back to old column for some commands
2109 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2110 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2111 || oap->op_type == OP_DELETE))
2112 coladvance(curwin->w_curswant = old_col);
2114 else
2116 curwin->w_cursor = old_cursor;
2118 #ifdef FEAT_VISUAL
2119 oap->block_mode = FALSE;
2120 #endif
2121 clearop(oap);
2126 * Handle indent and format operators and visual mode ":".
2128 static void
2129 op_colon(oap)
2130 oparg_T *oap;
2132 stuffcharReadbuff(':');
2133 #ifdef FEAT_VISUAL
2134 if (oap->is_VIsual)
2135 stuffReadbuff((char_u *)"'<,'>");
2136 else
2137 #endif
2140 * Make the range look nice, so it can be repeated.
2142 if (oap->start.lnum == curwin->w_cursor.lnum)
2143 stuffcharReadbuff('.');
2144 else
2145 stuffnumReadbuff((long)oap->start.lnum);
2146 if (oap->end.lnum != oap->start.lnum)
2148 stuffcharReadbuff(',');
2149 if (oap->end.lnum == curwin->w_cursor.lnum)
2150 stuffcharReadbuff('.');
2151 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2152 stuffcharReadbuff('$');
2153 else if (oap->start.lnum == curwin->w_cursor.lnum)
2155 stuffReadbuff((char_u *)".+");
2156 stuffnumReadbuff((long)oap->line_count - 1);
2158 else
2159 stuffnumReadbuff((long)oap->end.lnum);
2162 if (oap->op_type != OP_COLON)
2163 stuffReadbuff((char_u *)"!");
2164 if (oap->op_type == OP_INDENT)
2166 #ifndef FEAT_CINDENT
2167 if (*get_equalprg() == NUL)
2168 stuffReadbuff((char_u *)"indent");
2169 else
2170 #endif
2171 stuffReadbuff(get_equalprg());
2172 stuffReadbuff((char_u *)"\n");
2174 else if (oap->op_type == OP_FORMAT)
2176 if (*p_fp == NUL)
2177 stuffReadbuff((char_u *)"fmt");
2178 else
2179 stuffReadbuff(p_fp);
2180 stuffReadbuff((char_u *)"\n']");
2184 * do_cmdline() does the rest
2189 * Handle the "g@" operator: call 'operatorfunc'.
2191 /*ARGSUSED*/
2192 static void
2193 op_function(oap)
2194 oparg_T *oap;
2196 #ifdef FEAT_EVAL
2197 char_u *(argv[1]);
2199 if (*p_opfunc == NUL)
2200 EMSG(_("E774: 'operatorfunc' is empty"));
2201 else
2203 /* Set '[ and '] marks to text to be operated on. */
2204 curbuf->b_op_start = oap->start;
2205 curbuf->b_op_end = oap->end;
2206 if (oap->motion_type != MLINE && !oap->inclusive)
2207 /* Exclude the end position. */
2208 decl(&curbuf->b_op_end);
2210 if (oap->block_mode)
2211 argv[0] = (char_u *)"block";
2212 else if (oap->motion_type == MLINE)
2213 argv[0] = (char_u *)"line";
2214 else
2215 argv[0] = (char_u *)"char";
2216 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
2218 #else
2219 EMSG(_("E775: Eval feature not available"));
2220 #endif
2223 #if defined(FEAT_MOUSE) || defined(PROTO)
2225 * Do the appropriate action for the current mouse click in the current mode.
2226 * Not used for Command-line mode.
2228 * Normal Mode:
2229 * event modi- position visual change action
2230 * fier cursor window
2231 * left press - yes end yes
2232 * left press C yes end yes "^]" (2)
2233 * left press S yes end yes "*" (2)
2234 * left drag - yes start if moved no
2235 * left relse - yes start if moved no
2236 * middle press - yes if not active no put register
2237 * middle press - yes if active no yank and put
2238 * right press - yes start or extend yes
2239 * right press S yes no change yes "#" (2)
2240 * right drag - yes extend no
2241 * right relse - yes extend no
2243 * Insert or Replace Mode:
2244 * event modi- position visual change action
2245 * fier cursor window
2246 * left press - yes (cannot be active) yes
2247 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2248 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2249 * left drag - yes start or extend (1) no CTRL-O (1)
2250 * left relse - yes start or extend (1) no CTRL-O (1)
2251 * middle press - no (cannot be active) no put register
2252 * right press - yes start or extend yes CTRL-O
2253 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2255 * (1) only if mouse pointer moved since press
2256 * (2) only if click is in same buffer
2258 * Return TRUE if start_arrow() should be called for edit mode.
2261 do_mouse(oap, c, dir, count, fixindent)
2262 oparg_T *oap; /* operator argument, can be NULL */
2263 int c; /* K_LEFTMOUSE, etc */
2264 int dir; /* Direction to 'put' if necessary */
2265 long count;
2266 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2268 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2269 static int got_click = FALSE; /* got a click some time back */
2271 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2272 int is_click; /* If FALSE it's a drag or release event */
2273 int is_drag; /* If TRUE it's a drag event */
2274 int jump_flags = 0; /* flags for jump_to_mouse() */
2275 pos_T start_visual;
2276 int moved; /* Has cursor moved? */
2277 int in_status_line; /* mouse in status line */
2278 #ifdef FEAT_VERTSPLIT
2279 int in_sep_line; /* mouse in vertical separator line */
2280 #endif
2281 int c1, c2;
2282 #if defined(FEAT_FOLDING)
2283 pos_T save_cursor;
2284 #endif
2285 win_T *old_curwin = curwin;
2286 #ifdef FEAT_VISUAL
2287 static pos_T orig_cursor;
2288 colnr_T leftcol, rightcol;
2289 pos_T end_visual;
2290 int diff;
2291 int old_active = VIsual_active;
2292 int old_mode = VIsual_mode;
2293 #endif
2294 int regname;
2296 #if defined(FEAT_FOLDING)
2297 save_cursor = curwin->w_cursor;
2298 #endif
2301 * When GUI is active, always recognize mouse events, otherwise:
2302 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2303 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2304 * - For command line and insert mode 'mouse' is checked before calling
2305 * do_mouse().
2307 if (do_always)
2308 do_always = FALSE;
2309 else
2310 #ifdef FEAT_GUI
2311 if (!gui.in_use)
2312 #endif
2314 #ifdef FEAT_VISUAL
2315 if (VIsual_active)
2317 if (!mouse_has(MOUSE_VISUAL))
2318 return FALSE;
2320 else
2321 #endif
2322 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2323 return FALSE;
2326 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2328 #ifdef FEAT_MOUSESHAPE
2329 /* May have stopped dragging the status or separator line. The pointer is
2330 * most likely still on the status or separator line. */
2331 if (!is_drag && drag_status_line)
2333 drag_status_line = FALSE;
2334 update_mouseshape(SHAPE_IDX_STATUS);
2336 # ifdef FEAT_VERTSPLIT
2337 if (!is_drag && drag_sep_line)
2339 drag_sep_line = FALSE;
2340 update_mouseshape(SHAPE_IDX_VSEP);
2342 # endif
2343 #endif
2346 * Ignore drag and release events if we didn't get a click.
2348 if (is_click)
2349 got_click = TRUE;
2350 else
2352 if (!got_click) /* didn't get click, ignore */
2353 return FALSE;
2354 if (!is_drag) /* release, reset got_click */
2355 got_click = FALSE;
2358 #ifndef FEAT_VISUAL
2360 * ALT is only used for starging/extending Visual mode.
2362 if ((mod_mask & MOD_MASK_ALT))
2363 return FALSE;
2364 #endif
2367 * CTRL right mouse button does CTRL-T
2369 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2371 if (State & INSERT)
2372 stuffcharReadbuff(Ctrl_O);
2373 if (count > 1)
2374 stuffnumReadbuff(count);
2375 stuffcharReadbuff(Ctrl_T);
2376 got_click = FALSE; /* ignore drag&release now */
2377 return FALSE;
2381 * CTRL only works with left mouse button
2383 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2384 return FALSE;
2387 * When a modifier is down, ignore drag and release events, as well as
2388 * multiple clicks and the middle mouse button.
2389 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2391 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2392 | MOD_MASK_META))
2393 && (!is_click
2394 || (mod_mask & MOD_MASK_MULTI_CLICK)
2395 || which_button == MOUSE_MIDDLE)
2396 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
2397 && mouse_model_popup()
2398 && which_button == MOUSE_LEFT)
2399 && !((mod_mask & MOD_MASK_ALT)
2400 && !mouse_model_popup()
2401 && which_button == MOUSE_RIGHT)
2403 return FALSE;
2406 * If the button press was used as the movement command for an operator
2407 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2408 * drag/release events.
2410 if (!is_click && which_button == MOUSE_MIDDLE)
2411 return FALSE;
2413 if (oap != NULL)
2414 regname = oap->regname;
2415 else
2416 regname = 0;
2419 * Middle mouse button does a 'put' of the selected text
2421 if (which_button == MOUSE_MIDDLE)
2423 if (State == NORMAL)
2426 * If an operator was pending, we don't know what the user wanted
2427 * to do. Go back to normal mode: Clear the operator and beep().
2429 if (oap != NULL && oap->op_type != OP_NOP)
2431 clearopbeep(oap);
2432 return FALSE;
2435 #ifdef FEAT_VISUAL
2437 * If visual was active, yank the highlighted text and put it
2438 * before the mouse pointer position.
2439 * In Select mode replace the highlighted text with the clipboard.
2441 if (VIsual_active)
2443 if (VIsual_select)
2445 stuffcharReadbuff(Ctrl_G);
2446 stuffReadbuff((char_u *)"\"+p");
2448 else
2450 stuffcharReadbuff('y');
2451 stuffcharReadbuff(K_MIDDLEMOUSE);
2453 do_always = TRUE; /* ignore 'mouse' setting next time */
2454 return FALSE;
2456 #endif
2458 * The rest is below jump_to_mouse()
2462 else if ((State & INSERT) == 0)
2463 return FALSE;
2466 * Middle click in insert mode doesn't move the mouse, just insert the
2467 * contents of a register. '.' register is special, can't insert that
2468 * with do_put().
2469 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2470 * happens for the GUI).
2472 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2474 if (regname == '.')
2475 insert_reg(regname, TRUE);
2476 else
2478 #ifdef FEAT_CLIPBOARD
2479 if (clip_star.available && regname == 0)
2480 regname = '*';
2481 #endif
2482 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2483 insert_reg(regname, TRUE);
2484 else
2486 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2488 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2489 AppendCharToRedobuff(Ctrl_R);
2490 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2491 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2494 return FALSE;
2498 /* When dragging or button-up stay in the same window. */
2499 if (!is_click)
2500 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2502 start_visual.lnum = 0;
2504 #ifdef FEAT_WINDOWS
2505 /* Check for clicking in the tab page line. */
2506 if (mouse_row == 0 && firstwin->w_winrow > 0)
2508 if (is_drag)
2509 return FALSE;
2510 got_click = FALSE; /* ignore mouse-up and drag events */
2512 /* click in a tab selects that tab page */
2513 if (is_click
2514 # ifdef FEAT_CMDWIN
2515 && cmdwin_type == 0
2516 # endif
2517 && mouse_col < Columns)
2519 c1 = TabPageIdxs[mouse_col];
2520 if (c1 >= 0)
2522 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2524 /* double click opens new page */
2525 end_visual_mode();
2526 tabpage_new();
2527 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2529 else
2531 /* Go to specified tab page, or next one if not clicking
2532 * on a label. */
2533 goto_tabpage(c1);
2535 /* It's like clicking on the status line of a window. */
2536 if (curwin != old_curwin)
2537 end_visual_mode();
2540 else if (c1 < 0)
2542 tabpage_T *tp;
2544 /* Close the current or specified tab page. */
2545 if (c1 == -999)
2546 tp = curtab;
2547 else
2548 tp = find_tabpage(-c1);
2549 if (tp == curtab)
2551 if (first_tabpage->tp_next != NULL)
2552 tabpage_close(FALSE);
2554 else if (tp != NULL)
2555 tabpage_close_other(tp, FALSE);
2558 return TRUE;
2560 #endif
2563 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2564 * right button up -> pop-up menu
2565 * shift-left button -> right button
2566 * alt-left button -> alt-right button
2568 if (mouse_model_popup())
2570 if (which_button == MOUSE_RIGHT
2571 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2574 * NOTE: Ignore right button down and drag mouse events.
2575 * Windows only shows the popup menu on the button up event.
2577 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2578 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) \
2579 || defined(FEAT_GUI_MACVIM)
2580 if (!is_click)
2581 return FALSE;
2582 #endif
2583 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2584 if (is_click || is_drag)
2585 return FALSE;
2586 #endif
2587 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2588 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2589 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON) \
2590 || defined(FEAT_GUI_MACVIM)
2591 if (gui.in_use)
2593 jump_flags = 0;
2594 if (STRCMP(p_mousem, "popup_setpos") == 0)
2596 /* First set the cursor position before showing the popup
2597 * menu. */
2598 #ifdef FEAT_VISUAL
2599 if (VIsual_active)
2601 pos_T m_pos;
2604 * set MOUSE_MAY_STOP_VIS if we are outside the
2605 * selection or the current window (might have false
2606 * negative here)
2608 if (mouse_row < W_WINROW(curwin)
2609 || mouse_row
2610 > (W_WINROW(curwin) + curwin->w_height))
2611 jump_flags = MOUSE_MAY_STOP_VIS;
2612 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2613 jump_flags = MOUSE_MAY_STOP_VIS;
2614 else
2616 if ((lt(curwin->w_cursor, VIsual)
2617 && (lt(m_pos, curwin->w_cursor)
2618 || lt(VIsual, m_pos)))
2619 || (lt(VIsual, curwin->w_cursor)
2620 && (lt(m_pos, VIsual)
2621 || lt(curwin->w_cursor, m_pos))))
2623 jump_flags = MOUSE_MAY_STOP_VIS;
2625 else if (VIsual_mode == Ctrl_V)
2627 getvcols(curwin, &curwin->w_cursor, &VIsual,
2628 &leftcol, &rightcol);
2629 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2630 if (m_pos.col < leftcol || m_pos.col > rightcol)
2631 jump_flags = MOUSE_MAY_STOP_VIS;
2635 else
2636 jump_flags = MOUSE_MAY_STOP_VIS;
2637 #endif
2639 if (jump_flags)
2641 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2642 update_curbuf(
2643 #ifdef FEAT_VISUAL
2644 VIsual_active ? INVERTED :
2645 #endif
2646 VALID);
2647 setcursor();
2648 out_flush(); /* Update before showing popup menu */
2650 # ifdef FEAT_MENU
2651 gui_show_popupmenu();
2652 # endif
2653 return (jump_flags & CURSOR_MOVED) != 0;
2655 else
2656 return FALSE;
2657 #else
2658 return FALSE;
2659 #endif
2661 if (which_button == MOUSE_LEFT
2662 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
2664 which_button = MOUSE_RIGHT;
2665 mod_mask &= ~MOD_MASK_SHIFT;
2669 #ifdef FEAT_VISUAL
2670 if ((State & (NORMAL | INSERT))
2671 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2673 if (which_button == MOUSE_LEFT)
2675 if (is_click)
2677 /* stop Visual mode for a left click in a window, but not when
2678 * on a status line */
2679 if (VIsual_active)
2680 jump_flags |= MOUSE_MAY_STOP_VIS;
2682 else if (mouse_has(MOUSE_VISUAL))
2683 jump_flags |= MOUSE_MAY_VIS;
2685 else if (which_button == MOUSE_RIGHT)
2687 if (is_click && VIsual_active)
2690 * Remember the start and end of visual before moving the
2691 * cursor.
2693 if (lt(curwin->w_cursor, VIsual))
2695 start_visual = curwin->w_cursor;
2696 end_visual = VIsual;
2698 else
2700 start_visual = VIsual;
2701 end_visual = curwin->w_cursor;
2704 jump_flags |= MOUSE_FOCUS;
2705 if (mouse_has(MOUSE_VISUAL))
2706 jump_flags |= MOUSE_MAY_VIS;
2709 #endif
2712 * If an operator is pending, ignore all drags and releases until the
2713 * next mouse click.
2715 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2717 got_click = FALSE;
2718 oap->motion_type = MCHAR;
2721 /* When releasing the button let jump_to_mouse() know. */
2722 if (!is_click && !is_drag)
2723 jump_flags |= MOUSE_RELEASED;
2726 * JUMP!
2728 jump_flags = jump_to_mouse(jump_flags,
2729 oap == NULL ? NULL : &(oap->inclusive), which_button);
2730 moved = (jump_flags & CURSOR_MOVED);
2731 in_status_line = (jump_flags & IN_STATUS_LINE);
2732 #ifdef FEAT_VERTSPLIT
2733 in_sep_line = (jump_flags & IN_SEP_LINE);
2734 #endif
2736 #ifdef FEAT_NETBEANS_INTG
2737 if (usingNetbeans && isNetbeansBuffer(curbuf)
2738 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2740 int key = KEY2TERMCAP1(c);
2742 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2743 || key == (int)KE_RIGHTRELEASE)
2744 netbeans_button_release(which_button);
2746 #endif
2748 /* When jumping to another window, clear a pending operator. That's a bit
2749 * friendlier than beeping and not jumping to that window. */
2750 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2751 clearop(oap);
2753 #ifdef FEAT_FOLDING
2754 if (mod_mask == 0
2755 && !is_drag
2756 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2757 && which_button == MOUSE_LEFT)
2759 /* open or close a fold at this line */
2760 if (jump_flags & MOUSE_FOLD_OPEN)
2761 openFold(curwin->w_cursor.lnum, 1L);
2762 else
2763 closeFold(curwin->w_cursor.lnum, 1L);
2764 /* don't move the cursor if still in the same window */
2765 if (curwin == old_curwin)
2766 curwin->w_cursor = save_cursor;
2768 #endif
2770 #if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2771 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2773 clip_modeless(which_button, is_click, is_drag);
2774 return FALSE;
2776 #endif
2778 #ifdef FEAT_VISUAL
2779 /* Set global flag that we are extending the Visual area with mouse
2780 * dragging; temporarily minimize 'scrolloff'. */
2781 if (VIsual_active && is_drag && p_so)
2783 /* In the very first line, allow scrolling one line */
2784 if (mouse_row == 0)
2785 mouse_dragging = 2;
2786 else
2787 mouse_dragging = 1;
2790 /* When dragging the mouse above the window, scroll down. */
2791 if (is_drag && mouse_row < 0 && !in_status_line)
2793 scroll_redraw(FALSE, 1L);
2794 mouse_row = 0;
2797 if (start_visual.lnum) /* right click in visual mode */
2799 /* When ALT is pressed make Visual mode blockwise. */
2800 if (mod_mask & MOD_MASK_ALT)
2801 VIsual_mode = Ctrl_V;
2804 * In Visual-block mode, divide the area in four, pick up the corner
2805 * that is in the quarter that the cursor is in.
2807 if (VIsual_mode == Ctrl_V)
2809 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2810 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2811 end_visual.col = leftcol;
2812 else
2813 end_visual.col = rightcol;
2814 if (curwin->w_cursor.lnum <
2815 (start_visual.lnum + end_visual.lnum) / 2)
2816 end_visual.lnum = end_visual.lnum;
2817 else
2818 end_visual.lnum = start_visual.lnum;
2820 /* move VIsual to the right column */
2821 start_visual = curwin->w_cursor; /* save the cursor pos */
2822 curwin->w_cursor = end_visual;
2823 coladvance(end_visual.col);
2824 VIsual = curwin->w_cursor;
2825 curwin->w_cursor = start_visual; /* restore the cursor */
2827 else
2830 * If the click is before the start of visual, change the start.
2831 * If the click is after the end of visual, change the end. If
2832 * the click is inside the visual, change the closest side.
2834 if (lt(curwin->w_cursor, start_visual))
2835 VIsual = end_visual;
2836 else if (lt(end_visual, curwin->w_cursor))
2837 VIsual = start_visual;
2838 else
2840 /* In the same line, compare column number */
2841 if (end_visual.lnum == start_visual.lnum)
2843 if (curwin->w_cursor.col - start_visual.col >
2844 end_visual.col - curwin->w_cursor.col)
2845 VIsual = start_visual;
2846 else
2847 VIsual = end_visual;
2850 /* In different lines, compare line number */
2851 else
2853 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2854 (end_visual.lnum - curwin->w_cursor.lnum);
2856 if (diff > 0) /* closest to end */
2857 VIsual = start_visual;
2858 else if (diff < 0) /* closest to start */
2859 VIsual = end_visual;
2860 else /* in the middle line */
2862 if (curwin->w_cursor.col <
2863 (start_visual.col + end_visual.col) / 2)
2864 VIsual = end_visual;
2865 else
2866 VIsual = start_visual;
2873 * If Visual mode started in insert mode, execute "CTRL-O"
2875 else if ((State & INSERT) && VIsual_active)
2876 stuffcharReadbuff(Ctrl_O);
2877 #endif
2880 * Middle mouse click: Put text before cursor.
2882 if (which_button == MOUSE_MIDDLE)
2884 #ifdef FEAT_CLIPBOARD
2885 if (clip_star.available && regname == 0)
2886 regname = '*';
2887 #endif
2888 if (yank_register_mline(regname))
2890 if (mouse_past_bottom)
2891 dir = FORWARD;
2893 else if (mouse_past_eol)
2894 dir = FORWARD;
2896 if (fixindent)
2898 c1 = (dir == BACKWARD) ? '[' : ']';
2899 c2 = 'p';
2901 else
2903 c1 = (dir == FORWARD) ? 'p' : 'P';
2904 c2 = NUL;
2906 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2909 * Remember where the paste started, so in edit() Insstart can be set
2910 * to this position
2912 if (restart_edit != 0)
2913 where_paste_started = curwin->w_cursor;
2914 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2917 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2919 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2920 * error under the mouse pointer.
2922 else if (((mod_mask & MOD_MASK_CTRL)
2923 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2924 && bt_quickfix(curbuf))
2926 if (State & INSERT)
2927 stuffcharReadbuff(Ctrl_O);
2928 if (curwin->w_llist_ref == NULL) /* quickfix window */
2929 stuffReadbuff((char_u *)":.cc\n");
2930 else /* location list window */
2931 stuffReadbuff((char_u *)":.ll\n");
2932 got_click = FALSE; /* ignore drag&release now */
2934 #endif
2937 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2938 * under the mouse pointer.
2940 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2941 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2943 if (State & INSERT)
2944 stuffcharReadbuff(Ctrl_O);
2945 stuffcharReadbuff(Ctrl_RSB);
2946 got_click = FALSE; /* ignore drag&release now */
2950 * Shift-Mouse click searches for the next occurrence of the word under
2951 * the mouse pointer
2953 else if ((mod_mask & MOD_MASK_SHIFT))
2955 if (State & INSERT
2956 #ifdef FEAT_VISUAL
2957 || (VIsual_active && VIsual_select)
2958 #endif
2960 stuffcharReadbuff(Ctrl_O);
2961 if (which_button == MOUSE_LEFT)
2962 stuffcharReadbuff('*');
2963 else /* MOUSE_RIGHT */
2964 stuffcharReadbuff('#');
2967 /* Handle double clicks, unless on status line */
2968 else if (in_status_line)
2970 #ifdef FEAT_MOUSESHAPE
2971 if ((is_drag || is_click) && !drag_status_line)
2973 drag_status_line = TRUE;
2974 update_mouseshape(-1);
2976 #endif
2978 #ifdef FEAT_VERTSPLIT
2979 else if (in_sep_line)
2981 # ifdef FEAT_MOUSESHAPE
2982 if ((is_drag || is_click) && !drag_sep_line)
2984 drag_sep_line = TRUE;
2985 update_mouseshape(-1);
2987 # endif
2989 #endif
2990 #ifdef FEAT_VISUAL
2991 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
2992 && mouse_has(MOUSE_VISUAL))
2994 if (is_click || !VIsual_active)
2996 if (VIsual_active)
2997 orig_cursor = VIsual;
2998 else
3000 check_visual_highlight();
3001 VIsual = curwin->w_cursor;
3002 orig_cursor = VIsual;
3003 VIsual_active = TRUE;
3004 VIsual_reselect = TRUE;
3005 /* start Select mode if 'selectmode' contains "mouse" */
3006 may_start_select('o');
3007 setmouse();
3009 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3011 /* Double click with ALT pressed makes it blockwise. */
3012 if (mod_mask & MOD_MASK_ALT)
3013 VIsual_mode = Ctrl_V;
3014 else
3015 VIsual_mode = 'v';
3017 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3018 VIsual_mode = 'V';
3019 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3020 VIsual_mode = Ctrl_V;
3021 #ifdef FEAT_CLIPBOARD
3022 /* Make sure the clipboard gets updated. Needed because start and
3023 * end may still be the same, and the selection needs to be owned */
3024 clip_star.vmode = NUL;
3025 #endif
3028 * A double click selects a word or a block.
3030 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3032 pos_T *pos = NULL;
3033 int gc;
3035 if (is_click)
3037 /* If the character under the cursor (skipping white space) is
3038 * not a word character, try finding a match and select a (),
3039 * {}, [], #if/#endif, etc. block. */
3040 end_visual = curwin->w_cursor;
3041 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
3042 inc(&end_visual);
3043 if (oap != NULL)
3044 oap->motion_type = MCHAR;
3045 if (oap != NULL
3046 && VIsual_mode == 'v'
3047 && !vim_iswordc(gchar_pos(&end_visual))
3048 && equalpos(curwin->w_cursor, VIsual)
3049 && (pos = findmatch(oap, NUL)) != NULL)
3051 curwin->w_cursor = *pos;
3052 if (oap->motion_type == MLINE)
3053 VIsual_mode = 'V';
3054 else if (*p_sel == 'e')
3056 if (lt(curwin->w_cursor, VIsual))
3057 ++VIsual.col;
3058 else
3059 ++curwin->w_cursor.col;
3064 if (pos == NULL && (is_click || is_drag))
3066 /* When not found a match or when dragging: extend to include
3067 * a word. */
3068 if (lt(curwin->w_cursor, orig_cursor))
3070 find_start_of_word(&curwin->w_cursor);
3071 find_end_of_word(&VIsual);
3073 else
3075 find_start_of_word(&VIsual);
3076 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3077 #ifdef FEAT_MBYTE
3078 curwin->w_cursor.col +=
3079 (*mb_ptr2len)(ml_get_cursor());
3080 #else
3081 ++curwin->w_cursor.col;
3082 #endif
3083 find_end_of_word(&curwin->w_cursor);
3086 curwin->w_set_curswant = TRUE;
3088 if (is_click)
3089 redraw_curbuf_later(INVERTED); /* update the inversion */
3091 else if (VIsual_active && !old_active)
3093 if (mod_mask & MOD_MASK_ALT)
3094 VIsual_mode = Ctrl_V;
3095 else
3096 VIsual_mode = 'v';
3099 /* If Visual mode changed show it later. */
3100 if ((!VIsual_active && old_active && mode_displayed)
3101 || (VIsual_active && p_smd && msg_silent == 0
3102 && (!old_active || VIsual_mode != old_mode)))
3103 redraw_cmdline = TRUE;
3104 #endif
3106 return moved;
3109 #ifdef FEAT_VISUAL
3111 * Move "pos" back to the start of the word it's in.
3113 static void
3114 find_start_of_word(pos)
3115 pos_T *pos;
3117 char_u *line;
3118 int cclass;
3119 int col;
3121 line = ml_get(pos->lnum);
3122 cclass = get_mouse_class(line + pos->col);
3124 while (pos->col > 0)
3126 col = pos->col - 1;
3127 #ifdef FEAT_MBYTE
3128 col -= (*mb_head_off)(line, line + col);
3129 #endif
3130 if (get_mouse_class(line + col) != cclass)
3131 break;
3132 pos->col = col;
3137 * Move "pos" forward to the end of the word it's in.
3138 * When 'selection' is "exclusive", the position is just after the word.
3140 static void
3141 find_end_of_word(pos)
3142 pos_T *pos;
3144 char_u *line;
3145 int cclass;
3146 int col;
3148 line = ml_get(pos->lnum);
3149 if (*p_sel == 'e' && pos->col > 0)
3151 --pos->col;
3152 #ifdef FEAT_MBYTE
3153 pos->col -= (*mb_head_off)(line, line + pos->col);
3154 #endif
3156 cclass = get_mouse_class(line + pos->col);
3157 while (line[pos->col] != NUL)
3159 #ifdef FEAT_MBYTE
3160 col = pos->col + (*mb_ptr2len)(line + pos->col);
3161 #else
3162 col = pos->col + 1;
3163 #endif
3164 if (get_mouse_class(line + col) != cclass)
3166 if (*p_sel == 'e')
3167 pos->col = col;
3168 break;
3170 pos->col = col;
3175 * Get class of a character for selection: same class means same word.
3176 * 0: blank
3177 * 1: punctuation groups
3178 * 2: normal word character
3179 * >2: multi-byte word character.
3181 static int
3182 get_mouse_class(p)
3183 char_u *p;
3185 int c;
3187 #ifdef FEAT_MBYTE
3188 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3189 return mb_get_class(p);
3190 #endif
3192 c = *p;
3193 if (c == ' ' || c == '\t')
3194 return 0;
3196 if (vim_iswordc(c))
3197 return 2;
3200 * There are a few special cases where we want certain combinations of
3201 * characters to be considered as a single word. These are things like
3202 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
3203 * character is in it's own class.
3205 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3206 return 1;
3207 return c;
3209 #endif /* FEAT_VISUAL */
3210 #endif /* FEAT_MOUSE */
3212 #if defined(FEAT_VISUAL) || defined(PROTO)
3214 * Check if highlighting for visual mode is possible, give a warning message
3215 * if not.
3217 void
3218 check_visual_highlight()
3220 static int did_check = FALSE;
3222 if (full_screen)
3224 if (!did_check && hl_attr(HLF_V) == 0)
3225 MSG(_("Warning: terminal cannot highlight"));
3226 did_check = TRUE;
3231 * End Visual mode.
3232 * This function should ALWAYS be called to end Visual mode, except from
3233 * do_pending_operator().
3235 void
3236 end_visual_mode()
3238 #ifdef FEAT_CLIPBOARD
3240 * If we are using the clipboard, then remember what was selected in case
3241 * we need to paste it somewhere while we still own the selection.
3242 * Only do this when the clipboard is already owned. Don't want to grab
3243 * the selection when hitting ESC.
3245 if (clip_star.available && clip_star.owned)
3246 clip_auto_select();
3247 #endif
3249 VIsual_active = FALSE;
3250 #ifdef FEAT_MOUSE
3251 setmouse();
3252 mouse_dragging = 0;
3253 #endif
3255 /* Save the current VIsual area for '< and '> marks, and "gv" */
3256 curbuf->b_visual.vi_mode = VIsual_mode;
3257 curbuf->b_visual.vi_start = VIsual;
3258 curbuf->b_visual.vi_end = curwin->w_cursor;
3259 curbuf->b_visual.vi_curswant = curwin->w_curswant;
3260 #ifdef FEAT_EVAL
3261 curbuf->b_visual_mode_eval = VIsual_mode;
3262 #endif
3263 #ifdef FEAT_VIRTUALEDIT
3264 if (!virtual_active())
3265 curwin->w_cursor.coladd = 0;
3266 #endif
3268 if (mode_displayed)
3269 clear_cmdline = TRUE; /* unshow visual mode later */
3270 #ifdef FEAT_CMDL_INFO
3271 else
3272 clear_showcmd();
3273 #endif
3275 adjust_cursor_eol();
3279 * Reset VIsual_active and VIsual_reselect.
3281 void
3282 reset_VIsual_and_resel()
3284 if (VIsual_active)
3286 end_visual_mode();
3287 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3289 VIsual_reselect = FALSE;
3293 * Reset VIsual_active and VIsual_reselect if it's set.
3295 void
3296 reset_VIsual()
3298 if (VIsual_active)
3300 end_visual_mode();
3301 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3302 VIsual_reselect = FALSE;
3305 #endif /* FEAT_VISUAL */
3307 #if defined(FEAT_BEVAL)
3308 static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3311 * Check for a balloon-eval special item to include when searching for an
3312 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3313 * Returns TRUE if the character at "*ptr" should be included.
3314 * "dir" is FORWARD or BACKWARD, the direction of searching.
3315 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3316 * "bnp" points to a counter for square brackets.
3318 static int
3319 find_is_eval_item(ptr, colp, bnp, dir)
3320 char_u *ptr;
3321 int *colp;
3322 int *bnp;
3323 int dir;
3325 /* Accept everything inside []. */
3326 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3327 ++*bnp;
3328 if (*bnp > 0)
3330 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3331 --*bnp;
3332 return TRUE;
3335 /* skip over "s.var" */
3336 if (*ptr == '.')
3337 return TRUE;
3339 /* two-character item: s->var */
3340 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3341 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3343 *colp += dir;
3344 return TRUE;
3346 return FALSE;
3348 #endif
3351 * Find the identifier under or to the right of the cursor.
3352 * "find_type" can have one of three values:
3353 * FIND_IDENT: find an identifier (keyword)
3354 * FIND_STRING: find any non-white string
3355 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
3356 * FIND_EVAL: find text useful for C program debugging
3358 * There are three steps:
3359 * 1. Search forward for the start of an identifier/string. Doesn't move if
3360 * already on one.
3361 * 2. Search backward for the start of this identifier/string.
3362 * This doesn't match the real Vi but I like it a little better and it
3363 * shouldn't bother anyone.
3364 * 3. Search forward to the end of this identifier/string.
3365 * When FIND_IDENT isn't defined, we backup until a blank.
3367 * Returns the length of the string, or zero if no string is found.
3368 * If a string is found, a pointer to the string is put in "*string". This
3369 * string is not always NUL terminated.
3372 find_ident_under_cursor(string, find_type)
3373 char_u **string;
3374 int find_type;
3376 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3377 curwin->w_cursor.col, string, find_type);
3381 * Like find_ident_under_cursor(), but for any window and any position.
3382 * However: Uses 'iskeyword' from the current window!.
3385 find_ident_at_pos(wp, lnum, startcol, string, find_type)
3386 win_T *wp;
3387 linenr_T lnum;
3388 colnr_T startcol;
3389 char_u **string;
3390 int find_type;
3392 char_u *ptr;
3393 int col = 0; /* init to shut up GCC */
3394 int i;
3395 #ifdef FEAT_MBYTE
3396 int this_class = 0;
3397 int prev_class;
3398 int prevcol;
3399 #endif
3400 #if defined(FEAT_BEVAL)
3401 int bn = 0; /* bracket nesting */
3402 #endif
3405 * if i == 0: try to find an identifier
3406 * if i == 1: try to find any non-white string
3408 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3409 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3412 * 1. skip to start of identifier/string
3414 col = startcol;
3415 #ifdef FEAT_MBYTE
3416 if (has_mbyte)
3418 while (ptr[col] != NUL)
3420 # if defined(FEAT_BEVAL)
3421 /* Stop at a ']' to evaluate "a[x]". */
3422 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3423 break;
3424 # endif
3425 this_class = mb_get_class(ptr + col);
3426 if (this_class != 0 && (i == 1 || this_class != 1))
3427 break;
3428 col += (*mb_ptr2len)(ptr + col);
3431 else
3432 #endif
3433 while (ptr[col] != NUL
3434 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
3435 # if defined(FEAT_BEVAL)
3436 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3437 # endif
3439 ++col;
3441 #if defined(FEAT_BEVAL)
3442 /* When starting on a ']' count it, so that we include the '['. */
3443 bn = ptr[col] == ']';
3444 #endif
3447 * 2. Back up to start of identifier/string.
3449 #ifdef FEAT_MBYTE
3450 if (has_mbyte)
3452 /* Remember class of character under cursor. */
3453 # if defined(FEAT_BEVAL)
3454 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3455 this_class = mb_get_class((char_u *)"a");
3456 else
3457 # endif
3458 this_class = mb_get_class(ptr + col);
3459 while (col > 0 && this_class != 0)
3461 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3462 prev_class = mb_get_class(ptr + prevcol);
3463 if (this_class != prev_class
3464 && (i == 0
3465 || prev_class == 0
3466 || (find_type & FIND_IDENT))
3467 # if defined(FEAT_BEVAL)
3468 && (!(find_type & FIND_EVAL)
3469 || prevcol == 0
3470 || !find_is_eval_item(ptr + prevcol, &prevcol,
3471 &bn, BACKWARD))
3472 # endif
3474 break;
3475 col = prevcol;
3478 /* If we don't want just any old string, or we've found an
3479 * identifier, stop searching. */
3480 if (this_class > 2)
3481 this_class = 2;
3482 if (!(find_type & FIND_STRING) || this_class == 2)
3483 break;
3485 else
3486 #endif
3488 while (col > 0
3489 && ((i == 0
3490 ? vim_iswordc(ptr[col - 1])
3491 : (!vim_iswhite(ptr[col - 1])
3492 && (!(find_type & FIND_IDENT)
3493 || !vim_iswordc(ptr[col - 1]))))
3494 #if defined(FEAT_BEVAL)
3495 || ((find_type & FIND_EVAL)
3496 && col > 1
3497 && find_is_eval_item(ptr + col - 1, &col,
3498 &bn, BACKWARD))
3499 #endif
3501 --col;
3503 /* If we don't want just any old string, or we've found an
3504 * identifier, stop searching. */
3505 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3506 break;
3510 if (ptr[col] == NUL || (i == 0 && (
3511 #ifdef FEAT_MBYTE
3512 has_mbyte ? this_class != 2 :
3513 #endif
3514 !vim_iswordc(ptr[col]))))
3517 * didn't find an identifier or string
3519 if (find_type & FIND_STRING)
3520 EMSG(_("E348: No string under cursor"));
3521 else
3522 EMSG(_(e_noident));
3523 return 0;
3525 ptr += col;
3526 *string = ptr;
3529 * 3. Find the end if the identifier/string.
3531 #if defined(FEAT_BEVAL)
3532 bn = 0;
3533 startcol -= col;
3534 #endif
3535 col = 0;
3536 #ifdef FEAT_MBYTE
3537 if (has_mbyte)
3539 /* Search for point of changing multibyte character class. */
3540 this_class = mb_get_class(ptr);
3541 while (ptr[col] != NUL
3542 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3543 : mb_get_class(ptr + col) != 0)
3544 # if defined(FEAT_BEVAL)
3545 || ((find_type & FIND_EVAL)
3546 && col <= (int)startcol
3547 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3548 # endif
3550 col += (*mb_ptr2len)(ptr + col);
3552 else
3553 #endif
3554 while ((i == 0 ? vim_iswordc(ptr[col])
3555 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
3556 # if defined(FEAT_BEVAL)
3557 || ((find_type & FIND_EVAL)
3558 && col <= (int)startcol
3559 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3560 # endif
3563 ++col;
3566 return col;
3570 * Prepare for redo of a normal command.
3572 static void
3573 prep_redo_cmd(cap)
3574 cmdarg_T *cap;
3576 prep_redo(cap->oap->regname, cap->count0,
3577 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3581 * Prepare for redo of any command.
3582 * Note that only the last argument can be a multi-byte char.
3584 static void
3585 prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3586 int regname;
3587 long num;
3588 int cmd1;
3589 int cmd2;
3590 int cmd3;
3591 int cmd4;
3592 int cmd5;
3594 ResetRedobuff();
3595 if (regname != 0) /* yank from specified buffer */
3597 AppendCharToRedobuff('"');
3598 AppendCharToRedobuff(regname);
3600 if (num)
3601 AppendNumberToRedobuff(num);
3603 if (cmd1 != NUL)
3604 AppendCharToRedobuff(cmd1);
3605 if (cmd2 != NUL)
3606 AppendCharToRedobuff(cmd2);
3607 if (cmd3 != NUL)
3608 AppendCharToRedobuff(cmd3);
3609 if (cmd4 != NUL)
3610 AppendCharToRedobuff(cmd4);
3611 if (cmd5 != NUL)
3612 AppendCharToRedobuff(cmd5);
3616 * check for operator active and clear it
3618 * return TRUE if operator was active
3620 static int
3621 checkclearop(oap)
3622 oparg_T *oap;
3624 if (oap->op_type == OP_NOP)
3625 return FALSE;
3626 clearopbeep(oap);
3627 return TRUE;
3631 * Check for operator or Visual active. Clear active operator.
3633 * Return TRUE if operator or Visual was active.
3635 static int
3636 checkclearopq(oap)
3637 oparg_T *oap;
3639 if (oap->op_type == OP_NOP
3640 #ifdef FEAT_VISUAL
3641 && !VIsual_active
3642 #endif
3644 return FALSE;
3645 clearopbeep(oap);
3646 return TRUE;
3649 static void
3650 clearop(oap)
3651 oparg_T *oap;
3653 oap->op_type = OP_NOP;
3654 oap->regname = 0;
3655 oap->motion_force = NUL;
3656 oap->use_reg_one = FALSE;
3659 static void
3660 clearopbeep(oap)
3661 oparg_T *oap;
3663 clearop(oap);
3664 beep_flush();
3667 #ifdef FEAT_VISUAL
3669 * Remove the shift modifier from a special key.
3671 static void
3672 unshift_special(cap)
3673 cmdarg_T *cap;
3675 switch (cap->cmdchar)
3677 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3678 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3679 case K_S_UP: cap->cmdchar = K_UP; break;
3680 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3681 case K_S_HOME: cap->cmdchar = K_HOME; break;
3682 case K_S_END: cap->cmdchar = K_END; break;
3684 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3686 #endif
3688 #if defined(FEAT_CMDL_INFO) || defined(PROTO)
3690 * Routines for displaying a partly typed command
3693 #ifdef FEAT_VISUAL /* need room for size of Visual area */
3694 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3695 #else
3696 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3697 #endif
3698 static char_u showcmd_buf[SHOWCMD_BUFLEN];
3699 static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3700 static int showcmd_is_clear = TRUE;
3701 static int showcmd_visual = FALSE;
3703 static void display_showcmd __ARGS((void));
3705 void
3706 clear_showcmd()
3708 if (!p_sc)
3709 return;
3711 #ifdef FEAT_VISUAL
3712 if (VIsual_active && !char_avail())
3714 int i = lt(VIsual, curwin->w_cursor);
3715 long lines;
3716 colnr_T leftcol, rightcol;
3717 linenr_T top, bot;
3719 /* Show the size of the Visual area. */
3720 if (i)
3722 top = VIsual.lnum;
3723 bot = curwin->w_cursor.lnum;
3725 else
3727 top = curwin->w_cursor.lnum;
3728 bot = VIsual.lnum;
3730 # ifdef FEAT_FOLDING
3731 /* Include closed folds as a whole. */
3732 hasFolding(top, &top, NULL);
3733 hasFolding(bot, NULL, &bot);
3734 # endif
3735 lines = bot - top + 1;
3737 if (VIsual_mode == Ctrl_V)
3739 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
3740 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3741 (long)(rightcol - leftcol + 1));
3743 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3744 sprintf((char *)showcmd_buf, "%ld", lines);
3745 else
3746 sprintf((char *)showcmd_buf, "%ld", (long)(i
3747 ? curwin->w_cursor.col - VIsual.col
3748 : VIsual.col - curwin->w_cursor.col) + (*p_sel != 'e'));
3749 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3750 showcmd_visual = TRUE;
3752 else
3753 #endif
3755 showcmd_buf[0] = NUL;
3756 showcmd_visual = FALSE;
3758 /* Don't actually display something if there is nothing to clear. */
3759 if (showcmd_is_clear)
3760 return;
3763 display_showcmd();
3767 * Add 'c' to string of shown command chars.
3768 * Return TRUE if output has been written (and setcursor() has been called).
3771 add_to_showcmd(c)
3772 int c;
3774 char_u *p;
3775 int old_len;
3776 int extra_len;
3777 int overflow;
3778 #if defined(FEAT_MOUSE)
3779 int i;
3780 static int ignore[] =
3782 # ifdef FEAT_GUI
3783 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3784 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
3785 # endif
3786 K_IGNORE,
3787 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3788 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3789 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
3790 K_MOUSEDOWN, K_MOUSEUP,
3791 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
3792 K_CURSORHOLD,
3795 #endif
3797 if (!p_sc || msg_silent != 0)
3798 return FALSE;
3800 if (showcmd_visual)
3802 showcmd_buf[0] = NUL;
3803 showcmd_visual = FALSE;
3806 #if defined(FEAT_MOUSE)
3807 /* Ignore keys that are scrollbar updates and mouse clicks */
3808 if (IS_SPECIAL(c))
3809 for (i = 0; ignore[i] != 0; ++i)
3810 if (ignore[i] == c)
3811 return FALSE;
3812 #endif
3814 p = transchar(c);
3815 old_len = (int)STRLEN(showcmd_buf);
3816 extra_len = (int)STRLEN(p);
3817 overflow = old_len + extra_len - SHOWCMD_COLS;
3818 if (overflow > 0)
3819 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3820 old_len - overflow + 1);
3821 STRCAT(showcmd_buf, p);
3823 if (char_avail())
3824 return FALSE;
3826 display_showcmd();
3828 return TRUE;
3831 void
3832 add_to_showcmd_c(c)
3833 int c;
3835 if (!add_to_showcmd(c))
3836 setcursor();
3840 * Delete 'len' characters from the end of the shown command.
3842 static void
3843 del_from_showcmd(len)
3844 int len;
3846 int old_len;
3848 if (!p_sc)
3849 return;
3851 old_len = (int)STRLEN(showcmd_buf);
3852 if (len > old_len)
3853 len = old_len;
3854 showcmd_buf[old_len - len] = NUL;
3856 if (!char_avail())
3857 display_showcmd();
3861 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3862 * something and there is a partial mapping.
3864 void
3865 push_showcmd()
3867 if (p_sc)
3868 STRCPY(old_showcmd_buf, showcmd_buf);
3871 void
3872 pop_showcmd()
3874 if (!p_sc)
3875 return;
3877 STRCPY(showcmd_buf, old_showcmd_buf);
3879 display_showcmd();
3882 static void
3883 display_showcmd()
3885 int len;
3887 cursor_off();
3889 len = (int)STRLEN(showcmd_buf);
3890 if (len == 0)
3891 showcmd_is_clear = TRUE;
3892 else
3894 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3895 showcmd_is_clear = FALSE;
3899 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3900 * spaces
3902 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3904 setcursor(); /* put cursor back where it belongs */
3906 #endif
3908 #ifdef FEAT_SCROLLBIND
3910 * When "check" is FALSE, prepare for commands that scroll the window.
3911 * When "check" is TRUE, take care of scroll-binding after the window has
3912 * scrolled. Called from normal_cmd() and edit().
3914 void
3915 do_check_scrollbind(check)
3916 int check;
3918 static win_T *old_curwin = NULL;
3919 static linenr_T old_topline = 0;
3920 #ifdef FEAT_DIFF
3921 static int old_topfill = 0;
3922 #endif
3923 static buf_T *old_buf = NULL;
3924 static colnr_T old_leftcol = 0;
3926 if (check && curwin->w_p_scb)
3928 /* If a ":syncbind" command was just used, don't scroll, only reset
3929 * the values. */
3930 if (did_syncbind)
3931 did_syncbind = FALSE;
3932 else if (curwin == old_curwin)
3935 * Synchronize other windows, as necessary according to
3936 * 'scrollbind'. Don't do this after an ":edit" command, except
3937 * when 'diff' is set.
3939 if ((curwin->w_buffer == old_buf
3940 #ifdef FEAT_DIFF
3941 || curwin->w_p_diff
3942 #endif
3944 && (curwin->w_topline != old_topline
3945 #ifdef FEAT_DIFF
3946 || curwin->w_topfill != old_topfill
3947 #endif
3948 || curwin->w_leftcol != old_leftcol))
3950 check_scrollbind(curwin->w_topline - old_topline,
3951 (long)(curwin->w_leftcol - old_leftcol));
3954 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
3957 * When switching between windows, make sure that the relative
3958 * vertical offset is valid for the new window. The relative
3959 * offset is invalid whenever another 'scrollbind' window has
3960 * scrolled to a point that would force the current window to
3961 * scroll past the beginning or end of its buffer. When the
3962 * resync is performed, some of the other 'scrollbind' windows may
3963 * need to jump so that the current window's relative position is
3964 * visible on-screen.
3966 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
3968 curwin->w_scbind_pos = curwin->w_topline;
3971 old_curwin = curwin;
3972 old_topline = curwin->w_topline;
3973 #ifdef FEAT_DIFF
3974 old_topfill = curwin->w_topfill;
3975 #endif
3976 old_buf = curwin->w_buffer;
3977 old_leftcol = curwin->w_leftcol;
3981 * Synchronize any windows that have "scrollbind" set, based on the
3982 * number of rows by which the current window has changed
3983 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3985 void
3986 check_scrollbind(topline_diff, leftcol_diff)
3987 linenr_T topline_diff;
3988 long leftcol_diff;
3990 int want_ver;
3991 int want_hor;
3992 win_T *old_curwin = curwin;
3993 buf_T *old_curbuf = curbuf;
3994 #ifdef FEAT_VISUAL
3995 int old_VIsual_select = VIsual_select;
3996 int old_VIsual_active = VIsual_active;
3997 #endif
3998 colnr_T tgt_leftcol = curwin->w_leftcol;
3999 long topline;
4000 long y;
4003 * check 'scrollopt' string for vertical and horizontal scroll options
4005 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4006 #ifdef FEAT_DIFF
4007 want_ver |= old_curwin->w_p_diff;
4008 #endif
4009 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4012 * loop through the scrollbound windows and scroll accordingly
4014 #ifdef FEAT_VISUAL
4015 VIsual_select = VIsual_active = 0;
4016 #endif
4017 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4019 curbuf = curwin->w_buffer;
4020 /* skip original window and windows with 'noscrollbind' */
4021 if (curwin != old_curwin && curwin->w_p_scb)
4024 * do the vertical scroll
4026 if (want_ver)
4028 #ifdef FEAT_DIFF
4029 if (old_curwin->w_p_diff && curwin->w_p_diff)
4031 diff_set_topline(old_curwin, curwin);
4033 else
4034 #endif
4036 curwin->w_scbind_pos += topline_diff;
4037 topline = curwin->w_scbind_pos;
4038 if (topline > curbuf->b_ml.ml_line_count)
4039 topline = curbuf->b_ml.ml_line_count;
4040 if (topline < 1)
4041 topline = 1;
4043 y = topline - curwin->w_topline;
4044 if (y > 0)
4045 scrollup(y, FALSE);
4046 else
4047 scrolldown(-y, FALSE);
4050 redraw_later(VALID);
4051 cursor_correct();
4052 #ifdef FEAT_WINDOWS
4053 curwin->w_redr_status = TRUE;
4054 #endif
4058 * do the horizontal scroll
4060 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4062 curwin->w_leftcol = tgt_leftcol;
4063 leftcol_changed();
4069 * reset current-window
4071 #ifdef FEAT_VISUAL
4072 VIsual_select = old_VIsual_select;
4073 VIsual_active = old_VIsual_active;
4074 #endif
4075 curwin = old_curwin;
4076 curbuf = old_curbuf;
4078 #endif /* #ifdef FEAT_SCROLLBIND */
4081 * Command character that's ignored.
4082 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
4083 * xon/xoff
4085 static void
4086 nv_ignore(cap)
4087 cmdarg_T *cap;
4089 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
4093 * Command character that doesn't do anything, but unlike nv_ignore() does
4094 * start edit(). Used for "startinsert" executed while starting up.
4096 /*ARGSUSED */
4097 static void
4098 nv_nop(cap)
4099 cmdarg_T *cap;
4104 * Command character doesn't exist.
4106 static void
4107 nv_error(cap)
4108 cmdarg_T *cap;
4110 clearopbeep(cap->oap);
4114 * <Help> and <F1> commands.
4116 static void
4117 nv_help(cap)
4118 cmdarg_T *cap;
4120 if (!checkclearopq(cap->oap))
4121 ex_help(NULL);
4125 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4127 static void
4128 nv_addsub(cap)
4129 cmdarg_T *cap;
4131 if (!checkclearopq(cap->oap)
4132 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4133 prep_redo_cmd(cap);
4137 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4139 static void
4140 nv_page(cap)
4141 cmdarg_T *cap;
4143 if (!checkclearop(cap->oap))
4145 #ifdef FEAT_WINDOWS
4146 if (mod_mask & MOD_MASK_CTRL)
4148 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4149 if (cap->arg == BACKWARD)
4150 goto_tabpage(-(int)cap->count1);
4151 else
4152 goto_tabpage((int)cap->count0);
4154 else
4155 #endif
4156 (void)onepage(cap->arg, cap->count1);
4161 * Implementation of "gd" and "gD" command.
4163 static void
4164 nv_gd(oap, nchar, thisblock)
4165 oparg_T *oap;
4166 int nchar;
4167 int thisblock; /* 1 for "1gd" and "1gD" */
4169 int len;
4170 char_u *ptr;
4172 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
4173 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
4174 clearopbeep(oap);
4175 #ifdef FEAT_FOLDING
4176 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4177 foldOpenCursor();
4178 #endif
4182 * Search for variable declaration of "ptr[len]".
4183 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4184 * current file ("gD").
4185 * When "thisblock" is TRUE check the {} block scope.
4186 * Return FAIL when not found.
4189 find_decl(ptr, len, locally, thisblock, searchflags)
4190 char_u *ptr;
4191 int len;
4192 int locally;
4193 int thisblock;
4194 int searchflags; /* flags passed to searchit() */
4196 char_u *pat;
4197 pos_T old_pos;
4198 pos_T par_pos;
4199 pos_T found_pos;
4200 int t;
4201 int save_p_ws;
4202 int save_p_scs;
4203 int retval = OK;
4204 int incll;
4206 if ((pat = alloc(len + 7)) == NULL)
4207 return FAIL;
4209 /* Put "\V" before the pattern to avoid that the special meaning of "."
4210 * and "~" causes trouble. */
4211 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4212 len, ptr);
4213 old_pos = curwin->w_cursor;
4214 save_p_ws = p_ws;
4215 save_p_scs = p_scs;
4216 p_ws = FALSE; /* don't wrap around end of file now */
4217 p_scs = FALSE; /* don't switch ignorecase off now */
4220 * With "gD" go to line 1.
4221 * With "gd" Search back for the start of the current function, then go
4222 * back until a blank line. If this fails go to line 1.
4224 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
4226 setpcmark(); /* Set in findpar() otherwise */
4227 curwin->w_cursor.lnum = 1;
4228 par_pos = curwin->w_cursor;
4230 else
4232 par_pos = curwin->w_cursor;
4233 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4234 --curwin->w_cursor.lnum;
4236 curwin->w_cursor.col = 0;
4238 /* Search forward for the identifier, ignore comment lines. */
4239 clearpos(&found_pos);
4240 for (;;)
4242 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
4243 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
4244 if (curwin->w_cursor.lnum >= old_pos.lnum)
4245 t = FAIL; /* match after start is failure too */
4247 if (thisblock && t != FAIL)
4249 pos_T *pos;
4251 /* Check that the block the match is in doesn't end before the
4252 * position where we started the search from. */
4253 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4254 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4255 && pos->lnum < old_pos.lnum)
4256 continue;
4259 if (t == FAIL)
4261 /* If we previously found a valid position, use it. */
4262 if (found_pos.lnum != 0)
4264 curwin->w_cursor = found_pos;
4265 t = OK;
4267 break;
4269 #ifdef FEAT_COMMENTS
4270 if (get_leader_len(ml_get_curline(), NULL, FALSE) > 0)
4272 /* Ignore this line, continue at start of next line. */
4273 ++curwin->w_cursor.lnum;
4274 curwin->w_cursor.col = 0;
4275 continue;
4277 #endif
4278 if (!locally) /* global search: use first match found */
4279 break;
4280 if (curwin->w_cursor.lnum >= par_pos.lnum)
4282 /* If we previously found a valid position, use it. */
4283 if (found_pos.lnum != 0)
4284 curwin->w_cursor = found_pos;
4285 break;
4288 /* For finding a local variable and the match is before the "{" search
4289 * to find a later match. For K&R style function declarations this
4290 * skips the function header without types. */
4291 found_pos = curwin->w_cursor;
4294 if (t == FAIL)
4296 retval = FAIL;
4297 curwin->w_cursor = old_pos;
4299 else
4301 curwin->w_set_curswant = TRUE;
4302 /* "n" searches forward now */
4303 reset_search_dir();
4306 vim_free(pat);
4307 p_ws = save_p_ws;
4308 p_scs = save_p_scs;
4310 return retval;
4314 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4315 * lines rather than lines in the file.
4316 * 'dist' must be positive.
4318 * Return OK if able to move cursor, FAIL otherwise.
4320 static int
4321 nv_screengo(oap, dir, dist)
4322 oparg_T *oap;
4323 int dir;
4324 long dist;
4326 int linelen = linetabsize(ml_get_curline());
4327 int retval = OK;
4328 int atend = FALSE;
4329 int n;
4330 int col_off1; /* margin offset for first screen line */
4331 int col_off2; /* margin offset for wrapped screen line */
4332 int width1; /* text width for first screen line */
4333 int width2; /* test width for wrapped screen line */
4335 oap->motion_type = MCHAR;
4336 oap->inclusive = FALSE;
4338 col_off1 = curwin_col_off();
4339 col_off2 = col_off1 - curwin_col_off2();
4340 width1 = W_WIDTH(curwin) - col_off1;
4341 width2 = W_WIDTH(curwin) - col_off2;
4343 #ifdef FEAT_VERTSPLIT
4344 if (curwin->w_width != 0)
4346 #endif
4348 * Instead of sticking at the last character of the buffer line we
4349 * try to stick in the last column of the screen.
4351 if (curwin->w_curswant == MAXCOL)
4353 atend = TRUE;
4354 validate_virtcol();
4355 if (width1 <= 0)
4356 curwin->w_curswant = 0;
4357 else
4359 curwin->w_curswant = width1 - 1;
4360 if (curwin->w_virtcol > curwin->w_curswant)
4361 curwin->w_curswant += ((curwin->w_virtcol
4362 - curwin->w_curswant - 1) / width2 + 1) * width2;
4365 else
4367 if (linelen > width1)
4368 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4369 else
4370 n = width1;
4371 if (curwin->w_curswant > (colnr_T)n + 1)
4372 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4373 * width2;
4376 while (dist--)
4378 if (dir == BACKWARD)
4380 if ((long)curwin->w_curswant >= width2)
4381 /* move back within line */
4382 curwin->w_curswant -= width2;
4383 else
4385 /* to previous line */
4386 if (curwin->w_cursor.lnum == 1)
4388 retval = FAIL;
4389 break;
4391 --curwin->w_cursor.lnum;
4392 #ifdef FEAT_FOLDING
4393 /* Move to the start of a closed fold. Don't do that when
4394 * 'foldopen' contains "all": it will open in a moment. */
4395 if (!(fdo_flags & FDO_ALL))
4396 (void)hasFolding(curwin->w_cursor.lnum,
4397 &curwin->w_cursor.lnum, NULL);
4398 #endif
4399 linelen = linetabsize(ml_get_curline());
4400 if (linelen > width1)
4401 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4402 + 1) * width2;
4405 else /* dir == FORWARD */
4407 if (linelen > width1)
4408 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4409 else
4410 n = width1;
4411 if (curwin->w_curswant + width2 < (colnr_T)n)
4412 /* move forward within line */
4413 curwin->w_curswant += width2;
4414 else
4416 /* to next line */
4417 #ifdef FEAT_FOLDING
4418 /* Move to the end of a closed fold. */
4419 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4420 &curwin->w_cursor.lnum);
4421 #endif
4422 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4424 retval = FAIL;
4425 break;
4427 curwin->w_cursor.lnum++;
4428 curwin->w_curswant %= width2;
4432 #ifdef FEAT_VERTSPLIT
4434 #endif
4436 coladvance(curwin->w_curswant);
4438 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4439 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4442 * Check for landing on a character that got split at the end of the
4443 * last line. We want to advance a screenline, not end up in the same
4444 * screenline or move two screenlines.
4446 validate_virtcol();
4447 if (curwin->w_virtcol > curwin->w_curswant
4448 && (curwin->w_curswant < (colnr_T)width1
4449 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4450 : ((curwin->w_curswant - width1) % width2
4451 > (colnr_T)width2 / 2)))
4452 --curwin->w_cursor.col;
4454 #endif
4456 if (atend)
4457 curwin->w_curswant = MAXCOL; /* stick in the last column */
4459 return retval;
4462 #ifdef FEAT_MOUSE
4464 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4465 * when Shift or Ctrl is used.
4466 * K_MOUSEUP (cap->arg == TRUE) or K_MOUSEDOWN (cap->arg == FALSE)
4468 static void
4469 nv_mousescroll(cap)
4470 cmdarg_T *cap;
4472 # ifdef FEAT_GUI_SCROLL_WHEEL_FORCE
4473 int scroll_wheel_force = 0;
4474 # endif
4475 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4476 win_T *old_curwin = curwin;
4478 /* Currently we only get the mouse coordinates in the GUI. */
4479 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4481 int row, col;
4483 row = mouse_row;
4484 col = mouse_col;
4486 /* find the window at the pointer coordinates */
4487 curwin = mouse_find_win(&row, &col);
4488 curbuf = curwin->w_buffer;
4490 # endif
4492 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4494 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4496 else
4498 # ifdef FEAT_GUI_SCROLL_WHEEL_FORCE
4499 if (gui.in_use && gui.scroll_wheel_force >= 1) {
4500 scroll_wheel_force = gui.scroll_wheel_force;
4501 if (scroll_wheel_force > 1000)
4502 scroll_wheel_force = 1000;
4504 cap->count1 = scroll_wheel_force;
4505 cap->count0 = scroll_wheel_force;
4506 } else {
4507 cap->count1 = 3;
4508 cap->count0 = 3;
4510 # else
4511 cap->count1 = 3;
4512 cap->count0 = 3;
4513 # endif
4514 nv_scroll_line(cap);
4517 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4518 curwin->w_redr_status = TRUE;
4520 curwin = old_curwin;
4521 curbuf = curwin->w_buffer;
4522 # endif
4526 * Mouse clicks and drags.
4528 static void
4529 nv_mouse(cap)
4530 cmdarg_T *cap;
4532 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4534 #endif
4537 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4538 * cap->arg must be TRUE for CTRL-E.
4540 static void
4541 nv_scroll_line(cap)
4542 cmdarg_T *cap;
4544 if (!checkclearop(cap->oap))
4545 scroll_redraw(cap->arg, cap->count1);
4549 * Scroll "count" lines up or down, and redraw.
4551 void
4552 scroll_redraw(up, count)
4553 int up;
4554 long count;
4556 linenr_T prev_topline = curwin->w_topline;
4557 #ifdef FEAT_DIFF
4558 int prev_topfill = curwin->w_topfill;
4559 #endif
4560 linenr_T prev_lnum = curwin->w_cursor.lnum;
4562 if (up)
4563 scrollup(count, TRUE);
4564 else
4565 scrolldown(count, TRUE);
4566 if (p_so)
4568 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4569 * valid, otherwise the screen jumps back at the end of the file. */
4570 cursor_correct();
4571 check_cursor_moved(curwin);
4572 curwin->w_valid |= VALID_TOPLINE;
4574 /* If moved back to where we were, at least move the cursor, otherwise
4575 * we get stuck at one position. Don't move the cursor up if the
4576 * first line of the buffer is already on the screen */
4577 while (curwin->w_topline == prev_topline
4578 #ifdef FEAT_DIFF
4579 && curwin->w_topfill == prev_topfill
4580 #endif
4583 if (up)
4585 if (curwin->w_cursor.lnum > prev_lnum
4586 || cursor_down(1L, FALSE) == FAIL)
4587 break;
4589 else
4591 if (curwin->w_cursor.lnum < prev_lnum
4592 || prev_topline == 1L
4593 || cursor_up(1L, FALSE) == FAIL)
4594 break;
4596 /* Mark w_topline as valid, otherwise the screen jumps back at the
4597 * end of the file. */
4598 check_cursor_moved(curwin);
4599 curwin->w_valid |= VALID_TOPLINE;
4602 if (curwin->w_cursor.lnum != prev_lnum)
4603 coladvance(curwin->w_curswant);
4604 redraw_later(VALID);
4608 * Commands that start with "z".
4610 static void
4611 nv_zet(cap)
4612 cmdarg_T *cap;
4614 long n;
4615 colnr_T col;
4616 int nchar = cap->nchar;
4617 #ifdef FEAT_FOLDING
4618 long old_fdl = curwin->w_p_fdl;
4619 int old_fen = curwin->w_p_fen;
4620 #endif
4621 #ifdef FEAT_SPELL
4622 int undo = FALSE;
4623 #endif
4625 if (VIM_ISDIGIT(nchar))
4628 * "z123{nchar}": edit the count before obtaining {nchar}
4630 if (checkclearop(cap->oap))
4631 return;
4632 n = nchar - '0';
4633 for (;;)
4635 #ifdef USE_ON_FLY_SCROLL
4636 dont_scroll = TRUE; /* disallow scrolling here */
4637 #endif
4638 ++no_mapping;
4639 ++allow_keys; /* no mapping for nchar, but allow key codes */
4640 nchar = plain_vgetc();
4641 LANGMAP_ADJUST(nchar, TRUE);
4642 --no_mapping;
4643 --allow_keys;
4644 #ifdef FEAT_CMDL_INFO
4645 (void)add_to_showcmd(nchar);
4646 #endif
4647 if (nchar == K_DEL || nchar == K_KDEL)
4648 n /= 10;
4649 else if (VIM_ISDIGIT(nchar))
4650 n = n * 10 + (nchar - '0');
4651 else if (nchar == CAR)
4653 #ifdef FEAT_GUI
4654 need_mouse_correct = TRUE;
4655 #endif
4656 win_setheight((int)n);
4657 break;
4659 else if (nchar == 'l'
4660 || nchar == 'h'
4661 || nchar == K_LEFT
4662 || nchar == K_RIGHT)
4664 cap->count1 = n ? n * cap->count1 : cap->count1;
4665 goto dozet;
4667 else
4669 clearopbeep(cap->oap);
4670 break;
4673 cap->oap->op_type = OP_NOP;
4674 return;
4677 dozet:
4678 if (
4679 #ifdef FEAT_FOLDING
4680 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4681 * and "zC" only in Visual mode. "zj" and "zk" are motion
4682 * commands. */
4683 cap->nchar != 'f' && cap->nchar != 'F'
4684 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4685 && cap->nchar != 'j' && cap->nchar != 'k'
4687 #endif
4688 checkclearop(cap->oap))
4689 return;
4692 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4693 * If line number given, set cursor.
4695 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4696 && cap->count0
4697 && cap->count0 != curwin->w_cursor.lnum)
4699 setpcmark();
4700 if (cap->count0 > curbuf->b_ml.ml_line_count)
4701 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4702 else
4703 curwin->w_cursor.lnum = cap->count0;
4704 check_cursor_col();
4707 switch (nchar)
4709 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4710 case '+':
4711 if (cap->count0 == 0)
4713 /* No count given: put cursor at the line below screen */
4714 validate_botline(); /* make sure w_botline is valid */
4715 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4716 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4717 else
4718 curwin->w_cursor.lnum = curwin->w_botline;
4720 /* FALLTHROUGH */
4721 case NL:
4722 case CAR:
4723 case K_KENTER:
4724 beginline(BL_WHITE | BL_FIX);
4725 /* FALLTHROUGH */
4727 case 't': scroll_cursor_top(0, TRUE);
4728 redraw_later(VALID);
4729 break;
4731 /* "z." and "zz": put cursor in middle of screen */
4732 case '.': beginline(BL_WHITE | BL_FIX);
4733 /* FALLTHROUGH */
4735 case 'z': scroll_cursor_halfway(TRUE);
4736 redraw_later(VALID);
4737 break;
4739 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4740 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4741 * when <count> is at bottom of window, and puts that one at
4742 * bottom of window. */
4743 if (cap->count0 != 0)
4745 scroll_cursor_bot(0, TRUE);
4746 curwin->w_cursor.lnum = curwin->w_topline;
4748 else if (curwin->w_topline == 1)
4749 curwin->w_cursor.lnum = 1;
4750 else
4751 curwin->w_cursor.lnum = curwin->w_topline - 1;
4752 /* FALLTHROUGH */
4753 case '-':
4754 beginline(BL_WHITE | BL_FIX);
4755 /* FALLTHROUGH */
4757 case 'b': scroll_cursor_bot(0, TRUE);
4758 redraw_later(VALID);
4759 break;
4761 /* "zH" - scroll screen right half-page */
4762 case 'H':
4763 cap->count1 *= W_WIDTH(curwin) / 2;
4764 /* FALLTHROUGH */
4766 /* "zh" - scroll screen to the right */
4767 case 'h':
4768 case K_LEFT:
4769 if (!curwin->w_p_wrap)
4771 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4772 curwin->w_leftcol = 0;
4773 else
4774 curwin->w_leftcol -= (colnr_T)cap->count1;
4775 leftcol_changed();
4777 break;
4779 /* "zL" - scroll screen left half-page */
4780 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4781 /* FALLTHROUGH */
4783 /* "zl" - scroll screen to the left */
4784 case 'l':
4785 case K_RIGHT:
4786 if (!curwin->w_p_wrap)
4788 /* scroll the window left */
4789 curwin->w_leftcol += (colnr_T)cap->count1;
4790 leftcol_changed();
4792 break;
4794 /* "zs" - scroll screen, cursor at the start */
4795 case 's': if (!curwin->w_p_wrap)
4797 #ifdef FEAT_FOLDING
4798 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4799 col = 0; /* like the cursor is in col 0 */
4800 else
4801 #endif
4802 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4803 if ((long)col > p_siso)
4804 col -= p_siso;
4805 else
4806 col = 0;
4807 if (curwin->w_leftcol != col)
4809 curwin->w_leftcol = col;
4810 redraw_later(NOT_VALID);
4813 break;
4815 /* "ze" - scroll screen, cursor at the end */
4816 case 'e': if (!curwin->w_p_wrap)
4818 #ifdef FEAT_FOLDING
4819 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4820 col = 0; /* like the cursor is in col 0 */
4821 else
4822 #endif
4823 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4824 n = W_WIDTH(curwin) - curwin_col_off();
4825 if ((long)col + p_siso < n)
4826 col = 0;
4827 else
4828 col = col + p_siso - n + 1;
4829 if (curwin->w_leftcol != col)
4831 curwin->w_leftcol = col;
4832 redraw_later(NOT_VALID);
4835 break;
4837 #ifdef FEAT_FOLDING
4838 /* "zF": create fold command */
4839 /* "zf": create fold operator */
4840 case 'F':
4841 case 'f': if (foldManualAllowed(TRUE))
4843 cap->nchar = 'f';
4844 nv_operator(cap);
4845 curwin->w_p_fen = TRUE;
4847 /* "zF" is like "zfzf" */
4848 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4850 nv_operator(cap);
4851 finish_op = TRUE;
4854 else
4855 clearopbeep(cap->oap);
4856 break;
4858 /* "zd": delete fold at cursor */
4859 /* "zD": delete fold at cursor recursively */
4860 case 'd':
4861 case 'D': if (foldManualAllowed(FALSE))
4863 if (VIsual_active)
4864 nv_operator(cap);
4865 else
4866 deleteFold(curwin->w_cursor.lnum,
4867 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4869 break;
4871 /* "zE": erease all folds */
4872 case 'E': if (foldmethodIsManual(curwin))
4874 clearFolding(curwin);
4875 changed_window_setting();
4877 else if (foldmethodIsMarker(curwin))
4878 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4879 TRUE, FALSE);
4880 else
4881 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4882 break;
4884 /* "zn": fold none: reset 'foldenable' */
4885 case 'n': curwin->w_p_fen = FALSE;
4886 break;
4888 /* "zN": fold Normal: set 'foldenable' */
4889 case 'N': curwin->w_p_fen = TRUE;
4890 break;
4892 /* "zi": invert folding: toggle 'foldenable' */
4893 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4894 break;
4896 /* "za": open closed fold or close open fold at cursor */
4897 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4898 openFold(curwin->w_cursor.lnum, cap->count1);
4899 else
4901 closeFold(curwin->w_cursor.lnum, cap->count1);
4902 curwin->w_p_fen = TRUE;
4904 break;
4906 /* "zA": open fold at cursor recursively */
4907 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4908 openFoldRecurse(curwin->w_cursor.lnum);
4909 else
4911 closeFoldRecurse(curwin->w_cursor.lnum);
4912 curwin->w_p_fen = TRUE;
4914 break;
4916 /* "zo": open fold at cursor or Visual area */
4917 case 'o': if (VIsual_active)
4918 nv_operator(cap);
4919 else
4920 openFold(curwin->w_cursor.lnum, cap->count1);
4921 break;
4923 /* "zO": open fold recursively */
4924 case 'O': if (VIsual_active)
4925 nv_operator(cap);
4926 else
4927 openFoldRecurse(curwin->w_cursor.lnum);
4928 break;
4930 /* "zc": close fold at cursor or Visual area */
4931 case 'c': if (VIsual_active)
4932 nv_operator(cap);
4933 else
4934 closeFold(curwin->w_cursor.lnum, cap->count1);
4935 curwin->w_p_fen = TRUE;
4936 break;
4938 /* "zC": close fold recursively */
4939 case 'C': if (VIsual_active)
4940 nv_operator(cap);
4941 else
4942 closeFoldRecurse(curwin->w_cursor.lnum);
4943 curwin->w_p_fen = TRUE;
4944 break;
4946 /* "zv": open folds at the cursor */
4947 case 'v': foldOpenCursor();
4948 break;
4950 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
4951 case 'x': curwin->w_p_fen = TRUE;
4952 newFoldLevel(); /* update right now */
4953 foldOpenCursor();
4954 break;
4956 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
4957 case 'X': curwin->w_p_fen = TRUE;
4958 old_fdl = -1; /* force an update */
4959 break;
4961 /* "zm": fold more */
4962 case 'm': if (curwin->w_p_fdl > 0)
4963 --curwin->w_p_fdl;
4964 old_fdl = -1; /* force an update */
4965 curwin->w_p_fen = TRUE;
4966 break;
4968 /* "zM": close all folds */
4969 case 'M': curwin->w_p_fdl = 0;
4970 old_fdl = -1; /* force an update */
4971 curwin->w_p_fen = TRUE;
4972 break;
4974 /* "zr": reduce folding */
4975 case 'r': ++curwin->w_p_fdl;
4976 break;
4978 /* "zR": open all folds */
4979 case 'R': curwin->w_p_fdl = getDeepestNesting();
4980 old_fdl = -1; /* force an update */
4981 break;
4983 case 'j': /* "zj" move to next fold downwards */
4984 case 'k': /* "zk" move to next fold upwards */
4985 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
4986 cap->count1) == FAIL)
4987 clearopbeep(cap->oap);
4988 break;
4990 #endif /* FEAT_FOLDING */
4992 #ifdef FEAT_SPELL
4993 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
4994 ++no_mapping;
4995 ++allow_keys; /* no mapping for nchar, but allow key codes */
4996 nchar = plain_vgetc();
4997 LANGMAP_ADJUST(nchar, TRUE);
4998 --no_mapping;
4999 --allow_keys;
5000 #ifdef FEAT_CMDL_INFO
5001 (void)add_to_showcmd(nchar);
5002 #endif
5003 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5005 clearopbeep(cap->oap);
5006 break;
5008 undo = TRUE;
5009 /*FALLTHROUGH*/
5011 case 'g': /* "zg": add good word to word list */
5012 case 'w': /* "zw": add wrong word to word list */
5013 case 'G': /* "zG": add good word to temp word list */
5014 case 'W': /* "zW": add wrong word to temp word list */
5016 char_u *ptr = NULL;
5017 int len;
5019 if (checkclearop(cap->oap))
5020 break;
5021 # ifdef FEAT_VISUAL
5022 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5023 == FAIL)
5024 return;
5025 # endif
5026 if (ptr == NULL)
5028 pos_T pos = curwin->w_cursor;
5030 /* Find bad word under the cursor. */
5031 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
5032 if (len != 0 && curwin->w_cursor.col <= pos.col)
5033 ptr = ml_get_pos(&curwin->w_cursor);
5034 curwin->w_cursor = pos;
5037 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5038 FIND_IDENT)) == 0)
5039 return;
5040 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
5041 (nchar == 'G' || nchar == 'W')
5042 ? 0 : (int)cap->count1,
5043 undo);
5045 break;
5047 case '=': /* "z=": suggestions for a badly spelled word */
5048 if (!checkclearop(cap->oap))
5049 spell_suggest((int)cap->count0);
5050 break;
5051 #endif
5053 default: clearopbeep(cap->oap);
5056 #ifdef FEAT_FOLDING
5057 /* Redraw when 'foldenable' changed */
5058 if (old_fen != curwin->w_p_fen)
5060 # ifdef FEAT_DIFF
5061 win_T *wp;
5063 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5065 /* Adjust 'foldenable' in diff-synced windows. */
5066 FOR_ALL_WINDOWS(wp)
5068 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5070 wp->w_p_fen = curwin->w_p_fen;
5071 changed_window_setting_win(wp);
5075 # endif
5076 changed_window_setting();
5079 /* Redraw when 'foldlevel' changed. */
5080 if (old_fdl != curwin->w_p_fdl)
5081 newFoldLevel();
5082 #endif
5085 #ifdef FEAT_GUI
5087 * Vertical scrollbar movement.
5089 static void
5090 nv_ver_scrollbar(cap)
5091 cmdarg_T *cap;
5093 if (cap->oap->op_type != OP_NOP)
5094 clearopbeep(cap->oap);
5096 /* Even if an operator was pending, we still want to scroll */
5097 gui_do_scroll();
5101 * Horizontal scrollbar movement.
5103 static void
5104 nv_hor_scrollbar(cap)
5105 cmdarg_T *cap;
5107 if (cap->oap->op_type != OP_NOP)
5108 clearopbeep(cap->oap);
5110 /* Even if an operator was pending, we still want to scroll */
5111 gui_do_horiz_scroll();
5113 #endif
5115 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
5117 * Click in GUI tab.
5119 static void
5120 nv_tabline(cap)
5121 cmdarg_T *cap;
5123 if (cap->oap->op_type != OP_NOP)
5124 clearopbeep(cap->oap);
5126 /* Even if an operator was pending, we still want to jump tabs. */
5127 goto_tabpage(current_tab);
5131 * Selected item in tab line menu.
5133 static void
5134 nv_tabmenu(cap)
5135 cmdarg_T *cap;
5137 if (cap->oap->op_type != OP_NOP)
5138 clearopbeep(cap->oap);
5140 /* Even if an operator was pending, we still want to jump tabs. */
5141 handle_tabmenu();
5145 * Handle selecting an item of the GUI tab line menu.
5146 * Used in Normal and Insert mode.
5148 void
5149 handle_tabmenu()
5151 switch (current_tabmenu)
5153 case TABLINE_MENU_CLOSE:
5154 if (current_tab == 0)
5155 #ifdef FEAT_GUI_MACVIM
5156 do_cmdline_cmd((char_u *)"conf tabclose");
5157 #else
5158 do_cmdline_cmd((char_u *)"tabclose");
5159 #endif
5160 else
5162 #ifdef FEAT_GUI_MACVIM
5163 vim_snprintf((char *)IObuff, IOSIZE, "conf tabclose %d",
5164 current_tab);
5165 #else
5166 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5167 current_tab);
5168 #endif
5169 do_cmdline_cmd(IObuff);
5171 break;
5173 case TABLINE_MENU_NEW:
5174 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5175 current_tab > 0 ? current_tab - 1 : 999);
5176 do_cmdline_cmd(IObuff);
5177 break;
5179 case TABLINE_MENU_OPEN:
5180 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5181 current_tab > 0 ? current_tab - 1 : 999);
5182 do_cmdline_cmd(IObuff);
5183 break;
5186 #endif
5189 * "Q" command.
5191 static void
5192 nv_exmode(cap)
5193 cmdarg_T *cap;
5196 * Ignore 'Q' in Visual mode, just give a beep.
5198 #ifdef FEAT_VISUAL
5199 if (VIsual_active)
5200 vim_beep();
5201 else
5202 #endif
5203 if (!checkclearop(cap->oap))
5204 do_exmode(FALSE);
5208 * Handle a ":" command.
5210 static void
5211 nv_colon(cap)
5212 cmdarg_T *cap;
5214 int old_p_im;
5216 #ifdef FEAT_VISUAL
5217 if (VIsual_active)
5218 nv_operator(cap);
5219 else
5220 #endif
5222 if (cap->oap->op_type != OP_NOP)
5224 /* Using ":" as a movement is characterwise exclusive. */
5225 cap->oap->motion_type = MCHAR;
5226 cap->oap->inclusive = FALSE;
5228 else if (cap->count0)
5230 /* translate "count:" into ":.,.+(count - 1)" */
5231 stuffcharReadbuff('.');
5232 if (cap->count0 > 1)
5234 stuffReadbuff((char_u *)",.+");
5235 stuffnumReadbuff((long)cap->count0 - 1L);
5239 /* When typing, don't type below an old message */
5240 if (KeyTyped)
5241 compute_cmdrow();
5243 old_p_im = p_im;
5245 /* get a command line and execute it */
5246 do_cmdline(NULL, getexline, NULL,
5247 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5249 /* If 'insertmode' changed, enter or exit Insert mode */
5250 if (p_im != old_p_im)
5252 if (p_im)
5253 restart_edit = 'i';
5254 else
5255 restart_edit = 0;
5258 /* The start of the operator may have become invalid by the Ex
5259 * command. */
5260 if (cap->oap->op_type != OP_NOP
5261 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5262 || cap->oap->start.col >
5263 STRLEN(ml_get(cap->oap->start.lnum))))
5264 clearopbeep(cap->oap);
5269 * Handle CTRL-G command.
5271 static void
5272 nv_ctrlg(cap)
5273 cmdarg_T *cap;
5275 #ifdef FEAT_VISUAL
5276 if (VIsual_active) /* toggle Selection/Visual mode */
5278 VIsual_select = !VIsual_select;
5279 showmode();
5281 else
5282 #endif
5283 if (!checkclearop(cap->oap))
5284 /* print full name if count given or :cd used */
5285 fileinfo((int)cap->count0, FALSE, TRUE);
5289 * Handle CTRL-H <Backspace> command.
5291 static void
5292 nv_ctrlh(cap)
5293 cmdarg_T *cap;
5295 #ifdef FEAT_VISUAL
5296 if (VIsual_active && VIsual_select)
5298 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5299 v_visop(cap);
5301 else
5302 #endif
5303 nv_left(cap);
5307 * CTRL-L: clear screen and redraw.
5309 static void
5310 nv_clear(cap)
5311 cmdarg_T *cap;
5313 if (!checkclearop(cap->oap))
5315 #if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5317 * Right now, the BeBox doesn't seem to have an easy way to detect
5318 * window resizing, so we cheat and make the user detect it
5319 * manually with CTRL-L instead
5321 ui_get_shellsize();
5322 #endif
5323 #ifdef FEAT_SYN_HL
5324 /* Clear all syntax states to force resyncing. */
5325 syn_stack_free_all(curbuf);
5326 #endif
5327 redraw_later(CLEAR);
5332 * CTRL-O: In Select mode: switch to Visual mode for one command.
5333 * Otherwise: Go to older pcmark.
5335 static void
5336 nv_ctrlo(cap)
5337 cmdarg_T *cap;
5339 #ifdef FEAT_VISUAL
5340 if (VIsual_active && VIsual_select)
5342 VIsual_select = FALSE;
5343 showmode();
5344 restart_VIsual_select = 2; /* restart Select mode later */
5346 else
5347 #endif
5349 cap->count1 = -cap->count1;
5350 nv_pcmark(cap);
5355 * CTRL-^ command, short for ":e #"
5357 static void
5358 nv_hat(cap)
5359 cmdarg_T *cap;
5361 if (!checkclearopq(cap->oap))
5362 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5363 GETF_SETMARK|GETF_ALT, FALSE);
5367 * "Z" commands.
5369 static void
5370 nv_Zet(cap)
5371 cmdarg_T *cap;
5373 if (!checkclearopq(cap->oap))
5375 switch (cap->nchar)
5377 /* "ZZ": equivalent to ":x". */
5378 case 'Z': do_cmdline_cmd((char_u *)"x");
5379 break;
5381 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5382 case 'Q': do_cmdline_cmd((char_u *)"q!");
5383 break;
5385 default: clearopbeep(cap->oap);
5390 #if defined(FEAT_WINDOWS) || defined(PROTO)
5392 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5394 void
5395 do_nv_ident(c1, c2)
5396 int c1;
5397 int c2;
5399 oparg_T oa;
5400 cmdarg_T ca;
5402 clear_oparg(&oa);
5403 vim_memset(&ca, 0, sizeof(ca));
5404 ca.oap = &oa;
5405 ca.cmdchar = c1;
5406 ca.nchar = c2;
5407 nv_ident(&ca);
5409 #endif
5412 * Handle the commands that use the word under the cursor.
5413 * [g] CTRL-] :ta to current identifier
5414 * [g] 'K' run program for current identifier
5415 * [g] '*' / to current identifier or string
5416 * [g] '#' ? to current identifier or string
5417 * g ']' :tselect for current identifier
5419 static void
5420 nv_ident(cap)
5421 cmdarg_T *cap;
5423 char_u *ptr = NULL;
5424 char_u *buf;
5425 char_u *p;
5426 char_u *kp; /* value of 'keywordprg' */
5427 int kp_help; /* 'keywordprg' is ":help" */
5428 int n = 0; /* init for GCC */
5429 int cmdchar;
5430 int g_cmd; /* "g" command */
5431 char_u *aux_ptr;
5432 int isman;
5433 int isman_s;
5435 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5437 cmdchar = cap->nchar;
5438 g_cmd = TRUE;
5440 else
5442 cmdchar = cap->cmdchar;
5443 g_cmd = FALSE;
5446 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5447 cmdchar = '#';
5450 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5452 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5454 #ifdef FEAT_VISUAL
5455 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5456 return;
5457 #endif
5458 if (checkclearopq(cap->oap))
5459 return;
5462 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5463 (cmdchar == '*' || cmdchar == '#')
5464 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5466 clearop(cap->oap);
5467 return;
5470 /* Allocate buffer to put the command in. Inserting backslashes can
5471 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5472 * and some numbers. */
5473 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5474 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5475 || STRCMP(kp, ":help") == 0);
5476 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5477 if (buf == NULL)
5478 return;
5479 buf[0] = NUL;
5481 switch (cmdchar)
5483 case '*':
5484 case '#':
5486 * Put cursor at start of word, makes search skip the word
5487 * under the cursor.
5488 * Call setpcmark() first, so "*``" puts the cursor back where
5489 * it was.
5491 setpcmark();
5492 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5494 if (!g_cmd && vim_iswordp(ptr))
5495 STRCPY(buf, "\\<");
5496 no_smartcase = TRUE; /* don't use 'smartcase' now */
5497 break;
5499 case 'K':
5500 if (kp_help)
5501 STRCPY(buf, "he! ");
5502 else
5504 /* An external command will probably use an argument starting
5505 * with "-" as an option. To avoid trouble we skip the "-". */
5506 while (*ptr == '-' && n > 0)
5508 ++ptr;
5509 --n;
5511 if (n == 0)
5513 EMSG(_(e_noident)); /* found dashes only */
5514 vim_free(buf);
5515 return;
5518 /* When a count is given, turn it into a range. Is this
5519 * really what we want? */
5520 isman = (STRCMP(kp, "man") == 0);
5521 isman_s = (STRCMP(kp, "man -s") == 0);
5522 if (cap->count0 != 0 && !(isman || isman_s))
5523 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5525 STRCAT(buf, "! ");
5526 if (cap->count0 == 0 && isman_s)
5527 STRCAT(buf, "man");
5528 else
5529 STRCAT(buf, kp);
5530 STRCAT(buf, " ");
5531 if (cap->count0 != 0 && (isman || isman_s))
5533 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5534 STRCAT(buf, " ");
5537 break;
5539 case ']':
5540 #ifdef FEAT_CSCOPE
5541 if (p_cst)
5542 STRCPY(buf, "cstag ");
5543 else
5544 #endif
5545 STRCPY(buf, "ts ");
5546 break;
5548 default:
5549 if (curbuf->b_help)
5550 STRCPY(buf, "he! ");
5551 else if (g_cmd)
5552 STRCPY(buf, "tj ");
5553 else
5554 sprintf((char *)buf, "%ldta ", cap->count0);
5558 * Now grab the chars in the identifier
5560 if (cmdchar == 'K' && !kp_help)
5562 /* Escape the argument properly for a shell command */
5563 ptr = vim_strnsave(ptr, n);
5564 p = vim_strsave_shellescape(ptr, TRUE);
5565 vim_free(ptr);
5566 if (p == NULL)
5568 vim_free(buf);
5569 return;
5571 buf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5572 if (buf == NULL)
5574 vim_free(buf);
5575 vim_free(p);
5576 return;
5578 STRCAT(buf, p);
5579 vim_free(p);
5581 else
5583 if (cmdchar == '*')
5584 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5585 else if (cmdchar == '#')
5586 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
5587 else
5588 /* Don't escape spaces and Tabs in a tag with a backslash */
5589 aux_ptr = (char_u *)"\\|\"\n*?[";
5591 p = buf + STRLEN(buf);
5592 while (n-- > 0)
5594 /* put a backslash before \ and some others */
5595 if (vim_strchr(aux_ptr, *ptr) != NULL)
5596 *p++ = '\\';
5597 #ifdef FEAT_MBYTE
5598 /* When current byte is a part of multibyte character, copy all
5599 * bytes of that character. */
5600 if (has_mbyte)
5602 int i;
5603 int len = (*mb_ptr2len)(ptr) - 1;
5605 for (i = 0; i < len && n >= 1; ++i, --n)
5606 *p++ = *ptr++;
5608 #endif
5609 *p++ = *ptr++;
5611 *p = NUL;
5615 * Execute the command.
5617 if (cmdchar == '*' || cmdchar == '#')
5619 if (!g_cmd && (
5620 #ifdef FEAT_MBYTE
5621 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5622 #endif
5623 vim_iswordc(ptr[-1])))
5624 STRCAT(buf, "\\>");
5625 #ifdef FEAT_CMDHIST
5626 /* put pattern in search history */
5627 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5628 #endif
5629 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5631 else
5632 do_cmdline_cmd(buf);
5634 vim_free(buf);
5637 #if defined(FEAT_VISUAL) || defined(PROTO)
5639 * Get visually selected text, within one line only.
5640 * Returns FAIL if more than one line selected.
5643 get_visual_text(cap, pp, lenp)
5644 cmdarg_T *cap;
5645 char_u **pp; /* return: start of selected text */
5646 int *lenp; /* return: length of selected text */
5648 if (VIsual_mode != 'V')
5649 unadjust_for_sel();
5650 if (VIsual.lnum != curwin->w_cursor.lnum)
5652 if (cap != NULL)
5653 clearopbeep(cap->oap);
5654 return FAIL;
5656 if (VIsual_mode == 'V')
5658 *pp = ml_get_curline();
5659 *lenp = (int)STRLEN(*pp);
5661 else
5663 if (lt(curwin->w_cursor, VIsual))
5665 *pp = ml_get_pos(&curwin->w_cursor);
5666 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5668 else
5670 *pp = ml_get_pos(&VIsual);
5671 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5673 #ifdef FEAT_MBYTE
5674 if (has_mbyte)
5675 /* Correct the length to include the whole last character. */
5676 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
5677 #endif
5679 reset_VIsual_and_resel();
5680 return OK;
5682 #endif
5685 * CTRL-T: backwards in tag stack
5687 static void
5688 nv_tagpop(cap)
5689 cmdarg_T *cap;
5691 if (!checkclearopq(cap->oap))
5692 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5696 * Handle scrolling command 'H', 'L' and 'M'.
5698 static void
5699 nv_scroll(cap)
5700 cmdarg_T *cap;
5702 int used = 0;
5703 long n;
5704 #ifdef FEAT_FOLDING
5705 linenr_T lnum;
5706 #endif
5707 int half;
5709 cap->oap->motion_type = MLINE;
5710 setpcmark();
5712 if (cap->cmdchar == 'L')
5714 validate_botline(); /* make sure curwin->w_botline is valid */
5715 curwin->w_cursor.lnum = curwin->w_botline - 1;
5716 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5717 curwin->w_cursor.lnum = 1;
5718 else
5720 #ifdef FEAT_FOLDING
5721 if (hasAnyFolding(curwin))
5723 /* Count a fold for one screen line. */
5724 for (n = cap->count1 - 1; n > 0
5725 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5727 (void)hasFolding(curwin->w_cursor.lnum,
5728 &curwin->w_cursor.lnum, NULL);
5729 --curwin->w_cursor.lnum;
5732 else
5733 #endif
5734 curwin->w_cursor.lnum -= cap->count1 - 1;
5737 else
5739 if (cap->cmdchar == 'M')
5741 #ifdef FEAT_DIFF
5742 /* Don't count filler lines above the window. */
5743 used -= diff_check_fill(curwin, curwin->w_topline)
5744 - curwin->w_topfill;
5745 #endif
5746 validate_botline(); /* make sure w_empty_rows is valid */
5747 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5748 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5750 #ifdef FEAT_DIFF
5751 /* Count half he number of filler lines to be "below this
5752 * line" and half to be "above the next line". */
5753 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5754 + n) / 2 >= half)
5756 --n;
5757 break;
5759 #endif
5760 used += plines(curwin->w_topline + n);
5761 if (used >= half)
5762 break;
5763 #ifdef FEAT_FOLDING
5764 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5765 n = lnum - curwin->w_topline;
5766 #endif
5768 if (n > 0 && used > curwin->w_height)
5769 --n;
5771 else /* (cap->cmdchar == 'H') */
5773 n = cap->count1 - 1;
5774 #ifdef FEAT_FOLDING
5775 if (hasAnyFolding(curwin))
5777 /* Count a fold for one screen line. */
5778 lnum = curwin->w_topline;
5779 while (n-- > 0 && lnum < curwin->w_botline - 1)
5781 hasFolding(lnum, NULL, &lnum);
5782 ++lnum;
5784 n = lnum - curwin->w_topline;
5786 #endif
5788 curwin->w_cursor.lnum = curwin->w_topline + n;
5789 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5790 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5793 cursor_correct(); /* correct for 'so' */
5794 beginline(BL_SOL | BL_FIX);
5798 * Cursor right commands.
5800 static void
5801 nv_right(cap)
5802 cmdarg_T *cap;
5804 long n;
5805 #ifdef FEAT_VISUAL
5806 int PAST_LINE;
5807 #else
5808 # define PAST_LINE 0
5809 #endif
5811 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5813 /* <C-Right> and <S-Right> move a word or WORD right */
5814 if (mod_mask & MOD_MASK_CTRL)
5815 cap->arg = TRUE;
5816 nv_wordcmd(cap);
5817 return;
5820 cap->oap->motion_type = MCHAR;
5821 cap->oap->inclusive = FALSE;
5822 #ifdef FEAT_VISUAL
5823 PAST_LINE = (VIsual_active && *p_sel != 'o');
5825 # ifdef FEAT_VIRTUALEDIT
5827 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
5828 * (theoretically) infinitely long.
5830 if (virtual_active())
5831 PAST_LINE = 0;
5832 # endif
5833 #endif
5835 for (n = cap->count1; n > 0; --n)
5837 if ((!PAST_LINE && oneright() == FAIL)
5838 || (PAST_LINE && *ml_get_cursor() == NUL))
5841 * <Space> wraps to next line if 'whichwrap' has 's'.
5842 * 'l' wraps to next line if 'whichwrap' has 'l'.
5843 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
5845 if ( ((cap->cmdchar == ' '
5846 && vim_strchr(p_ww, 's') != NULL)
5847 || (cap->cmdchar == 'l'
5848 && vim_strchr(p_ww, 'l') != NULL)
5849 || (cap->cmdchar == K_RIGHT
5850 && vim_strchr(p_ww, '>') != NULL))
5851 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5853 /* When deleting we also count the NL as a character.
5854 * Set cap->oap->inclusive when last char in the line is
5855 * included, move to next line after that */
5856 if ( cap->oap->op_type != OP_NOP
5857 && !cap->oap->inclusive
5858 && !lineempty(curwin->w_cursor.lnum))
5859 cap->oap->inclusive = TRUE;
5860 else
5862 ++curwin->w_cursor.lnum;
5863 curwin->w_cursor.col = 0;
5864 #ifdef FEAT_VIRTUALEDIT
5865 curwin->w_cursor.coladd = 0;
5866 #endif
5867 curwin->w_set_curswant = TRUE;
5868 cap->oap->inclusive = FALSE;
5870 continue;
5872 if (cap->oap->op_type == OP_NOP)
5874 /* Only beep and flush if not moved at all */
5875 if (n == cap->count1)
5876 beep_flush();
5878 else
5880 if (!lineempty(curwin->w_cursor.lnum))
5881 cap->oap->inclusive = TRUE;
5883 break;
5885 #ifdef FEAT_VISUAL
5886 else if (PAST_LINE)
5888 curwin->w_set_curswant = TRUE;
5889 # ifdef FEAT_VIRTUALEDIT
5890 if (virtual_active())
5891 oneright();
5892 else
5893 # endif
5895 # ifdef FEAT_MBYTE
5896 if (has_mbyte)
5897 curwin->w_cursor.col +=
5898 (*mb_ptr2len)(ml_get_cursor());
5899 else
5900 # endif
5901 ++curwin->w_cursor.col;
5904 #endif
5906 #ifdef FEAT_FOLDING
5907 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5908 && cap->oap->op_type == OP_NOP)
5909 foldOpenCursor();
5910 #endif
5914 * Cursor left commands.
5916 * Returns TRUE when operator end should not be adjusted.
5918 static void
5919 nv_left(cap)
5920 cmdarg_T *cap;
5922 long n;
5924 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5926 /* <C-Left> and <S-Left> move a word or WORD left */
5927 if (mod_mask & MOD_MASK_CTRL)
5928 cap->arg = 1;
5929 nv_bck_word(cap);
5930 return;
5933 cap->oap->motion_type = MCHAR;
5934 cap->oap->inclusive = FALSE;
5935 for (n = cap->count1; n > 0; --n)
5937 if (oneleft() == FAIL)
5939 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
5940 * 'h' wraps to previous line if 'whichwrap' has 'h'.
5941 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
5943 if ( (((cap->cmdchar == K_BS
5944 || cap->cmdchar == Ctrl_H)
5945 && vim_strchr(p_ww, 'b') != NULL)
5946 || (cap->cmdchar == 'h'
5947 && vim_strchr(p_ww, 'h') != NULL)
5948 || (cap->cmdchar == K_LEFT
5949 && vim_strchr(p_ww, '<') != NULL))
5950 && curwin->w_cursor.lnum > 1)
5952 --(curwin->w_cursor.lnum);
5953 coladvance((colnr_T)MAXCOL);
5954 curwin->w_set_curswant = TRUE;
5956 /* When the NL before the first char has to be deleted we
5957 * put the cursor on the NUL after the previous line.
5958 * This is a very special case, be careful!
5959 * Don't adjust op_end now, otherwise it won't work. */
5960 if ( (cap->oap->op_type == OP_DELETE
5961 || cap->oap->op_type == OP_CHANGE)
5962 && !lineempty(curwin->w_cursor.lnum))
5964 if (*ml_get_cursor() != NUL)
5965 ++curwin->w_cursor.col;
5966 cap->retval |= CA_NO_ADJ_OP_END;
5968 continue;
5970 /* Only beep and flush if not moved at all */
5971 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
5972 beep_flush();
5973 break;
5976 #ifdef FEAT_FOLDING
5977 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5978 && cap->oap->op_type == OP_NOP)
5979 foldOpenCursor();
5980 #endif
5984 * Cursor up commands.
5985 * cap->arg is TRUE for "-": Move cursor to first non-blank.
5987 static void
5988 nv_up(cap)
5989 cmdarg_T *cap;
5991 if (mod_mask & MOD_MASK_SHIFT)
5993 /* <S-Up> is page up */
5994 cap->arg = BACKWARD;
5995 nv_page(cap);
5997 else
5999 cap->oap->motion_type = MLINE;
6000 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6001 clearopbeep(cap->oap);
6002 else if (cap->arg)
6003 beginline(BL_WHITE | BL_FIX);
6008 * Cursor down commands.
6009 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6011 static void
6012 nv_down(cap)
6013 cmdarg_T *cap;
6015 if (mod_mask & MOD_MASK_SHIFT)
6017 /* <S-Down> is page down */
6018 cap->arg = FORWARD;
6019 nv_page(cap);
6021 else
6022 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6023 /* In a quickfix window a <CR> jumps to the error under the cursor. */
6024 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
6025 if (curwin->w_llist_ref == NULL)
6026 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6027 else
6028 do_cmdline_cmd((char_u *)".ll"); /* location list window */
6029 else
6030 #endif
6032 #ifdef FEAT_CMDWIN
6033 /* In the cmdline window a <CR> executes the command. */
6034 if (cmdwin_type != 0 && cap->cmdchar == CAR)
6035 cmdwin_result = CAR;
6036 else
6037 #endif
6039 cap->oap->motion_type = MLINE;
6040 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6041 clearopbeep(cap->oap);
6042 else if (cap->arg)
6043 beginline(BL_WHITE | BL_FIX);
6048 #ifdef FEAT_SEARCHPATH
6050 * Grab the file name under the cursor and edit it.
6052 static void
6053 nv_gotofile(cap)
6054 cmdarg_T *cap;
6056 char_u *ptr;
6057 linenr_T lnum = -1;
6059 if (text_locked())
6061 clearopbeep(cap->oap);
6062 text_locked_msg();
6063 return;
6065 #ifdef FEAT_AUTOCMD
6066 if (curbuf_locked())
6068 clearop(cap->oap);
6069 return;
6071 #endif
6073 ptr = grab_file_name(cap->count1, &lnum);
6075 if (ptr != NULL)
6077 /* do autowrite if necessary */
6078 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6079 autowrite(curbuf, FALSE);
6080 setpcmark();
6081 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
6082 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
6083 if (cap->nchar == 'F' && lnum >= 0)
6085 curwin->w_cursor.lnum = lnum;
6086 check_cursor_lnum();
6087 beginline(BL_SOL | BL_FIX);
6089 vim_free(ptr);
6091 else
6092 clearop(cap->oap);
6094 #endif
6097 * <End> command: to end of current line or last line.
6099 static void
6100 nv_end(cap)
6101 cmdarg_T *cap;
6103 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
6105 cap->arg = TRUE;
6106 nv_goto(cap);
6107 cap->count1 = 1; /* to end of current line */
6109 nv_dollar(cap);
6113 * Handle the "$" command.
6115 static void
6116 nv_dollar(cap)
6117 cmdarg_T *cap;
6119 cap->oap->motion_type = MCHAR;
6120 cap->oap->inclusive = TRUE;
6121 #ifdef FEAT_VIRTUALEDIT
6122 /* In virtual mode when off the edge of a line and an operator
6123 * is pending (whew!) keep the cursor where it is.
6124 * Otherwise, send it to the end of the line. */
6125 if (!virtual_active() || gchar_cursor() != NUL
6126 || cap->oap->op_type == OP_NOP)
6127 #endif
6128 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6129 if (cursor_down((long)(cap->count1 - 1),
6130 cap->oap->op_type == OP_NOP) == FAIL)
6131 clearopbeep(cap->oap);
6132 #ifdef FEAT_FOLDING
6133 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6134 foldOpenCursor();
6135 #endif
6139 * Implementation of '?' and '/' commands.
6140 * If cap->arg is TRUE don't set PC mark.
6142 static void
6143 nv_search(cap)
6144 cmdarg_T *cap;
6146 oparg_T *oap = cap->oap;
6148 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6150 /* Translate "g??" to "g?g?" */
6151 cap->cmdchar = 'g';
6152 cap->nchar = '?';
6153 nv_operator(cap);
6154 return;
6157 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6159 if (cap->searchbuf == NULL)
6161 clearop(oap);
6162 return;
6165 normal_search(cap, cap->cmdchar, cap->searchbuf,
6166 (cap->arg ? 0 : SEARCH_MARK));
6170 * Handle "N" and "n" commands.
6171 * cap->arg is SEARCH_REV for "N", 0 for "n".
6173 static void
6174 nv_next(cap)
6175 cmdarg_T *cap;
6177 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6181 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6182 * Uses only cap->count1 and cap->oap from "cap".
6184 static void
6185 normal_search(cap, dir, pat, opt)
6186 cmdarg_T *cap;
6187 int dir;
6188 char_u *pat;
6189 int opt; /* extra flags for do_search() */
6191 int i;
6193 cap->oap->motion_type = MCHAR;
6194 cap->oap->inclusive = FALSE;
6195 cap->oap->use_reg_one = TRUE;
6196 curwin->w_set_curswant = TRUE;
6198 i = do_search(cap->oap, dir, pat, cap->count1,
6199 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
6200 if (i == 0)
6201 clearop(cap->oap);
6202 else
6204 if (i == 2)
6205 cap->oap->motion_type = MLINE;
6206 #ifdef FEAT_VIRTUALEDIT
6207 curwin->w_cursor.coladd = 0;
6208 #endif
6209 #ifdef FEAT_FOLDING
6210 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6211 foldOpenCursor();
6212 #endif
6215 /* "/$" will put the cursor after the end of the line, may need to
6216 * correct that here */
6217 check_cursor();
6221 * Character search commands.
6222 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6223 * ',' and FALSE for ';'.
6224 * cap->nchar is NUL for ',' and ';' (repeat the search)
6226 static void
6227 nv_csearch(cap)
6228 cmdarg_T *cap;
6230 int t_cmd;
6232 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6233 t_cmd = TRUE;
6234 else
6235 t_cmd = FALSE;
6237 cap->oap->motion_type = MCHAR;
6238 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6239 clearopbeep(cap->oap);
6240 else
6242 curwin->w_set_curswant = TRUE;
6243 #ifdef FEAT_VIRTUALEDIT
6244 /* Include a Tab for "tx" and for "dfx". */
6245 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6246 && (t_cmd || cap->oap->op_type != OP_NOP))
6248 colnr_T scol, ecol;
6250 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6251 curwin->w_cursor.coladd = ecol - scol;
6253 else
6254 curwin->w_cursor.coladd = 0;
6255 #endif
6256 #ifdef FEAT_VISUAL
6257 adjust_for_sel(cap);
6258 #endif
6259 #ifdef FEAT_FOLDING
6260 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6261 foldOpenCursor();
6262 #endif
6267 * "[" and "]" commands.
6268 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6270 static void
6271 nv_brackets(cap)
6272 cmdarg_T *cap;
6274 pos_T new_pos;
6275 pos_T prev_pos;
6276 pos_T *pos = NULL; /* init for GCC */
6277 pos_T old_pos; /* cursor position before command */
6278 int flag;
6279 long n;
6280 int findc;
6281 int c;
6283 cap->oap->motion_type = MCHAR;
6284 cap->oap->inclusive = FALSE;
6285 old_pos = curwin->w_cursor;
6286 #ifdef FEAT_VIRTUALEDIT
6287 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6288 #endif
6290 #ifdef FEAT_SEARCHPATH
6292 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6294 if (cap->nchar == 'f')
6295 nv_gotofile(cap);
6296 else
6297 #endif
6299 #ifdef FEAT_FIND_ID
6301 * Find the occurrence(s) of the identifier or define under cursor
6302 * in current and included files or jump to the first occurrence.
6304 * search list jump
6305 * fwd bwd fwd bwd fwd bwd
6306 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6307 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6309 if (vim_strchr((char_u *)
6310 #ifdef EBCDIC
6311 "iI\005dD\067",
6312 #else
6313 "iI\011dD\004",
6314 #endif
6315 cap->nchar) != NULL)
6317 char_u *ptr;
6318 int len;
6320 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6321 clearop(cap->oap);
6322 else
6324 find_pattern_in_path(ptr, 0, len, TRUE,
6325 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6326 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6327 cap->count1,
6328 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6329 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6330 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6331 (linenr_T)MAXLNUM);
6332 curwin->w_set_curswant = TRUE;
6335 else
6336 #endif
6339 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6340 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6341 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6342 * "[m" or "]m" search for prev/next start of (Java) method.
6343 * "[M" or "]M" search for prev/next end of (Java) method.
6345 if ( (cap->cmdchar == '['
6346 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6347 || (cap->cmdchar == ']'
6348 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6350 if (cap->nchar == '*')
6351 cap->nchar = '/';
6352 new_pos.lnum = 0;
6353 prev_pos.lnum = 0;
6354 if (cap->nchar == 'm' || cap->nchar == 'M')
6356 if (cap->cmdchar == '[')
6357 findc = '{';
6358 else
6359 findc = '}';
6360 n = 9999;
6362 else
6364 findc = cap->nchar;
6365 n = cap->count1;
6367 for ( ; n > 0; --n)
6369 if ((pos = findmatchlimit(cap->oap, findc,
6370 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6372 if (new_pos.lnum == 0) /* nothing found */
6374 if (cap->nchar != 'm' && cap->nchar != 'M')
6375 clearopbeep(cap->oap);
6377 else
6378 pos = &new_pos; /* use last one found */
6379 break;
6381 prev_pos = new_pos;
6382 curwin->w_cursor = *pos;
6383 new_pos = *pos;
6385 curwin->w_cursor = old_pos;
6388 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6389 * brought us to the match for "[m" and "]M" when inside a method.
6390 * Try finding the '{' or '}' we want to be at.
6391 * Also repeat for the given count.
6393 if (cap->nchar == 'm' || cap->nchar == 'M')
6395 /* norm is TRUE for "]M" and "[m" */
6396 int norm = ((findc == '{') == (cap->nchar == 'm'));
6398 n = cap->count1;
6399 /* found a match: we were inside a method */
6400 if (prev_pos.lnum != 0)
6402 pos = &prev_pos;
6403 curwin->w_cursor = prev_pos;
6404 if (norm)
6405 --n;
6407 else
6408 pos = NULL;
6409 while (n > 0)
6411 for (;;)
6413 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6415 /* if not found anything, that's an error */
6416 if (pos == NULL)
6417 clearopbeep(cap->oap);
6418 n = 0;
6419 break;
6421 c = gchar_cursor();
6422 if (c == '{' || c == '}')
6424 /* Must have found end/start of class: use it.
6425 * Or found the place to be at. */
6426 if ((c == findc && norm) || (n == 1 && !norm))
6428 new_pos = curwin->w_cursor;
6429 pos = &new_pos;
6430 n = 0;
6432 /* if no match found at all, we started outside of the
6433 * class and we're inside now. Just go on. */
6434 else if (new_pos.lnum == 0)
6436 new_pos = curwin->w_cursor;
6437 pos = &new_pos;
6439 /* found start/end of other method: go to match */
6440 else if ((pos = findmatchlimit(cap->oap, findc,
6441 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6442 0)) == NULL)
6443 n = 0;
6444 else
6445 curwin->w_cursor = *pos;
6446 break;
6449 --n;
6451 curwin->w_cursor = old_pos;
6452 if (pos == NULL && new_pos.lnum != 0)
6453 clearopbeep(cap->oap);
6455 if (pos != NULL)
6457 setpcmark();
6458 curwin->w_cursor = *pos;
6459 curwin->w_set_curswant = TRUE;
6460 #ifdef FEAT_FOLDING
6461 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6462 && cap->oap->op_type == OP_NOP)
6463 foldOpenCursor();
6464 #endif
6469 * "[[", "[]", "]]" and "][": move to start or end of function
6471 else if (cap->nchar == '[' || cap->nchar == ']')
6473 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6474 flag = '{';
6475 else
6476 flag = '}'; /* "][" or "[]" */
6478 curwin->w_set_curswant = TRUE;
6480 * Imitate strange Vi behaviour: When using "]]" with an operator
6481 * we also stop at '}'.
6483 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
6484 (cap->oap->op_type != OP_NOP
6485 && cap->arg == FORWARD && flag == '{')))
6486 clearopbeep(cap->oap);
6487 else
6489 if (cap->oap->op_type == OP_NOP)
6490 beginline(BL_WHITE | BL_FIX);
6491 #ifdef FEAT_FOLDING
6492 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6493 foldOpenCursor();
6494 #endif
6499 * "[p", "[P", "]P" and "]p": put with indent adjustment
6501 else if (cap->nchar == 'p' || cap->nchar == 'P')
6503 if (!checkclearop(cap->oap))
6505 prep_redo_cmd(cap);
6506 do_put(cap->oap->regname,
6507 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6508 cap->count1, PUT_FIXINDENT);
6513 * "['", "[`", "]'" and "]`": jump to next mark
6515 else if (cap->nchar == '\'' || cap->nchar == '`')
6517 pos = &curwin->w_cursor;
6518 for (n = cap->count1; n > 0; --n)
6520 prev_pos = *pos;
6521 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6522 cap->nchar == '\'');
6523 if (pos == NULL)
6524 break;
6526 if (pos == NULL)
6527 pos = &prev_pos;
6528 nv_cursormark(cap, cap->nchar == '\'', pos);
6531 #ifdef FEAT_MOUSE
6533 * [ or ] followed by a middle mouse click: put selected text with
6534 * indent adjustment. Any other button just does as usual.
6536 else if (cap->nchar >= K_LEFTMOUSE && cap->nchar <= K_RIGHTRELEASE)
6538 (void)do_mouse(cap->oap, cap->nchar,
6539 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6540 cap->count1, PUT_FIXINDENT);
6542 #endif /* FEAT_MOUSE */
6544 #ifdef FEAT_FOLDING
6546 * "[z" and "]z": move to start or end of open fold.
6548 else if (cap->nchar == 'z')
6550 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6551 cap->count1) == FAIL)
6552 clearopbeep(cap->oap);
6554 #endif
6556 #ifdef FEAT_DIFF
6558 * "[c" and "]c": move to next or previous diff-change.
6560 else if (cap->nchar == 'c')
6562 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6563 cap->count1) == FAIL)
6564 clearopbeep(cap->oap);
6566 #endif
6568 #ifdef FEAT_SPELL
6570 * "[s", "[S", "]s" and "]S": move to next spell error.
6572 else if (cap->nchar == 's' || cap->nchar == 'S')
6574 setpcmark();
6575 for (n = 0; n < cap->count1; ++n)
6576 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6577 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
6579 clearopbeep(cap->oap);
6580 break;
6582 # ifdef FEAT_FOLDING
6583 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6584 foldOpenCursor();
6585 # endif
6587 #endif
6589 /* Not a valid cap->nchar. */
6590 else
6591 clearopbeep(cap->oap);
6595 * Handle Normal mode "%" command.
6597 static void
6598 nv_percent(cap)
6599 cmdarg_T *cap;
6601 pos_T *pos;
6602 #ifdef FEAT_FOLDING
6603 linenr_T lnum = curwin->w_cursor.lnum;
6604 #endif
6606 cap->oap->inclusive = TRUE;
6607 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6609 if (cap->count0 > 100)
6610 clearopbeep(cap->oap);
6611 else
6613 cap->oap->motion_type = MLINE;
6614 setpcmark();
6615 /* Round up, so CTRL-G will give same value. Watch out for a
6616 * large line count, the line number must not go negative! */
6617 if (curbuf->b_ml.ml_line_count > 1000000)
6618 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6619 / 100L * cap->count0;
6620 else
6621 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6622 cap->count0 + 99L) / 100L;
6623 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6624 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6625 beginline(BL_SOL | BL_FIX);
6628 else /* "%" : go to matching paren */
6630 cap->oap->motion_type = MCHAR;
6631 cap->oap->use_reg_one = TRUE;
6632 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6633 clearopbeep(cap->oap);
6634 else
6636 setpcmark();
6637 curwin->w_cursor = *pos;
6638 curwin->w_set_curswant = TRUE;
6639 #ifdef FEAT_VIRTUALEDIT
6640 curwin->w_cursor.coladd = 0;
6641 #endif
6642 #ifdef FEAT_VISUAL
6643 adjust_for_sel(cap);
6644 #endif
6647 #ifdef FEAT_FOLDING
6648 if (cap->oap->op_type == OP_NOP
6649 && lnum != curwin->w_cursor.lnum
6650 && (fdo_flags & FDO_PERCENT)
6651 && KeyTyped)
6652 foldOpenCursor();
6653 #endif
6657 * Handle "(" and ")" commands.
6658 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6660 static void
6661 nv_brace(cap)
6662 cmdarg_T *cap;
6664 cap->oap->motion_type = MCHAR;
6665 cap->oap->use_reg_one = TRUE;
6666 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6667 cap->oap->inclusive = FALSE;
6668 curwin->w_set_curswant = TRUE;
6670 if (findsent(cap->arg, cap->count1) == FAIL)
6671 clearopbeep(cap->oap);
6672 else
6674 /* Don't leave the cursor on the NUL past end of line. */
6675 adjust_cursor(cap->oap);
6676 #ifdef FEAT_VIRTUALEDIT
6677 curwin->w_cursor.coladd = 0;
6678 #endif
6679 #ifdef FEAT_FOLDING
6680 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6681 foldOpenCursor();
6682 #endif
6687 * "m" command: Mark a position.
6689 static void
6690 nv_mark(cap)
6691 cmdarg_T *cap;
6693 if (!checkclearop(cap->oap))
6695 if (setmark(cap->nchar) == FAIL)
6696 clearopbeep(cap->oap);
6701 * "{" and "}" commands.
6702 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6704 static void
6705 nv_findpar(cap)
6706 cmdarg_T *cap;
6708 cap->oap->motion_type = MCHAR;
6709 cap->oap->inclusive = FALSE;
6710 cap->oap->use_reg_one = TRUE;
6711 curwin->w_set_curswant = TRUE;
6712 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
6713 clearopbeep(cap->oap);
6714 else
6716 #ifdef FEAT_VIRTUALEDIT
6717 curwin->w_cursor.coladd = 0;
6718 #endif
6719 #ifdef FEAT_FOLDING
6720 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6721 foldOpenCursor();
6722 #endif
6727 * "u" command: Undo or make lower case.
6729 static void
6730 nv_undo(cap)
6731 cmdarg_T *cap;
6733 if (cap->oap->op_type == OP_LOWER
6734 #ifdef FEAT_VISUAL
6735 || VIsual_active
6736 #endif
6739 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6740 cap->cmdchar = 'g';
6741 cap->nchar = 'u';
6742 nv_operator(cap);
6744 else
6745 nv_kundo(cap);
6749 * <Undo> command.
6751 static void
6752 nv_kundo(cap)
6753 cmdarg_T *cap;
6755 if (!checkclearopq(cap->oap))
6757 u_undo((int)cap->count1);
6758 curwin->w_set_curswant = TRUE;
6763 * Handle the "r" command.
6765 static void
6766 nv_replace(cap)
6767 cmdarg_T *cap;
6769 char_u *ptr;
6770 int had_ctrl_v;
6771 long n;
6773 if (checkclearop(cap->oap))
6774 return;
6776 /* get another character */
6777 if (cap->nchar == Ctrl_V)
6779 had_ctrl_v = Ctrl_V;
6780 cap->nchar = get_literal();
6781 /* Don't redo a multibyte character with CTRL-V. */
6782 if (cap->nchar > DEL)
6783 had_ctrl_v = NUL;
6785 else
6786 had_ctrl_v = NUL;
6788 /* Abort if the character is a special key. */
6789 if (IS_SPECIAL(cap->nchar))
6791 clearopbeep(cap->oap);
6792 return;
6795 #ifdef FEAT_VISUAL
6796 /* Visual mode "r" */
6797 if (VIsual_active)
6799 if (got_int)
6800 reset_VIsual();
6801 nv_operator(cap);
6802 return;
6804 #endif
6806 #ifdef FEAT_VIRTUALEDIT
6807 /* Break tabs, etc. */
6808 if (virtual_active())
6810 if (u_save_cursor() == FAIL)
6811 return;
6812 if (gchar_cursor() == NUL)
6814 /* Add extra space and put the cursor on the first one. */
6815 coladvance_force((colnr_T)(getviscol() + cap->count1));
6816 curwin->w_cursor.col -= cap->count1;
6818 else if (gchar_cursor() == TAB)
6819 coladvance_force(getviscol());
6821 #endif
6823 /* Abort if not enough characters to replace. */
6824 ptr = ml_get_cursor();
6825 if (STRLEN(ptr) < (unsigned)cap->count1
6826 #ifdef FEAT_MBYTE
6827 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6828 #endif
6831 clearopbeep(cap->oap);
6832 return;
6836 * Replacing with a TAB is done by edit() when it is complicated because
6837 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6838 * Other characters are done below to avoid problems with things like
6839 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6841 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6843 stuffnumReadbuff(cap->count1);
6844 stuffcharReadbuff('R');
6845 stuffcharReadbuff('\t');
6846 stuffcharReadbuff(ESC);
6847 return;
6850 /* save line for undo */
6851 if (u_save_cursor() == FAIL)
6852 return;
6854 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6857 * Replace character(s) by a single newline.
6858 * Strange vi behaviour: Only one newline is inserted.
6859 * Delete the characters here.
6860 * Insert the newline with an insert command, takes care of
6861 * autoindent. The insert command depends on being on the last
6862 * character of a line or not.
6864 #ifdef FEAT_MBYTE
6865 (void)del_chars(cap->count1, FALSE); /* delete the characters */
6866 #else
6867 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
6868 #endif
6869 stuffcharReadbuff('\r');
6870 stuffcharReadbuff(ESC);
6872 /* Give 'r' to edit(), to get the redo command right. */
6873 invoke_edit(cap, TRUE, 'r', FALSE);
6875 else
6877 prep_redo(cap->oap->regname, cap->count1,
6878 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
6880 curbuf->b_op_start = curwin->w_cursor;
6881 #ifdef FEAT_MBYTE
6882 if (has_mbyte)
6884 int old_State = State;
6886 if (cap->ncharC1 != 0)
6887 AppendCharToRedobuff(cap->ncharC1);
6888 if (cap->ncharC2 != 0)
6889 AppendCharToRedobuff(cap->ncharC2);
6891 /* This is slow, but it handles replacing a single-byte with a
6892 * multi-byte and the other way around. Also handles adding
6893 * composing characters for utf-8. */
6894 for (n = cap->count1; n > 0; --n)
6896 State = REPLACE;
6897 ins_char(cap->nchar);
6898 State = old_State;
6899 if (cap->ncharC1 != 0)
6900 ins_char(cap->ncharC1);
6901 if (cap->ncharC2 != 0)
6902 ins_char(cap->ncharC2);
6905 else
6906 #endif
6909 * Replace the characters within one line.
6911 for (n = cap->count1; n > 0; --n)
6914 * Get ptr again, because u_save and/or showmatch() will have
6915 * released the line. At the same time we let know that the
6916 * line will be changed.
6918 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
6919 ptr[curwin->w_cursor.col] = cap->nchar;
6920 if (p_sm && msg_silent == 0)
6921 showmatch(cap->nchar);
6922 ++curwin->w_cursor.col;
6924 #ifdef FEAT_NETBEANS_INTG
6925 if (usingNetbeans)
6927 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
6929 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
6930 (long)cap->count1);
6931 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
6932 &ptr[start], (int)cap->count1);
6934 #endif
6936 /* mark the buffer as changed and prepare for displaying */
6937 changed_bytes(curwin->w_cursor.lnum,
6938 (colnr_T)(curwin->w_cursor.col - cap->count1));
6940 --curwin->w_cursor.col; /* cursor on the last replaced char */
6941 #ifdef FEAT_MBYTE
6942 /* if the character on the left of the current cursor is a multi-byte
6943 * character, move two characters left */
6944 if (has_mbyte)
6945 mb_adjust_cursor();
6946 #endif
6947 curbuf->b_op_end = curwin->w_cursor;
6948 curwin->w_set_curswant = TRUE;
6949 set_last_insert(cap->nchar);
6953 #ifdef FEAT_VISUAL
6955 * 'o': Exchange start and end of Visual area.
6956 * 'O': same, but in block mode exchange left and right corners.
6958 static void
6959 v_swap_corners(cmdchar)
6960 int cmdchar;
6962 pos_T old_cursor;
6963 colnr_T left, right;
6965 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
6967 old_cursor = curwin->w_cursor;
6968 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
6969 curwin->w_cursor.lnum = VIsual.lnum;
6970 coladvance(left);
6971 VIsual = curwin->w_cursor;
6973 curwin->w_cursor.lnum = old_cursor.lnum;
6974 curwin->w_curswant = right;
6975 /* 'selection "exclusive" and cursor at right-bottom corner: move it
6976 * right one column */
6977 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
6978 ++curwin->w_curswant;
6979 coladvance(curwin->w_curswant);
6980 if (curwin->w_cursor.col == old_cursor.col
6981 #ifdef FEAT_VIRTUALEDIT
6982 && (!virtual_active()
6983 || curwin->w_cursor.coladd == old_cursor.coladd)
6984 #endif
6987 curwin->w_cursor.lnum = VIsual.lnum;
6988 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
6989 ++right;
6990 coladvance(right);
6991 VIsual = curwin->w_cursor;
6993 curwin->w_cursor.lnum = old_cursor.lnum;
6994 coladvance(left);
6995 curwin->w_curswant = left;
6998 else
7000 old_cursor = curwin->w_cursor;
7001 curwin->w_cursor = VIsual;
7002 VIsual = old_cursor;
7003 curwin->w_set_curswant = TRUE;
7006 #endif /* FEAT_VISUAL */
7009 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7011 static void
7012 nv_Replace(cap)
7013 cmdarg_T *cap;
7015 #ifdef FEAT_VISUAL
7016 if (VIsual_active) /* "R" is replace lines */
7018 cap->cmdchar = 'c';
7019 cap->nchar = NUL;
7020 VIsual_mode = 'V';
7021 nv_operator(cap);
7023 else
7024 #endif
7025 if (!checkclearopq(cap->oap))
7027 if (!curbuf->b_p_ma)
7028 EMSG(_(e_modifiable));
7029 else
7031 #ifdef FEAT_VIRTUALEDIT
7032 if (virtual_active())
7033 coladvance(getviscol());
7034 #endif
7035 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7040 #ifdef FEAT_VREPLACE
7042 * "gr".
7044 static void
7045 nv_vreplace(cap)
7046 cmdarg_T *cap;
7048 # ifdef FEAT_VISUAL
7049 if (VIsual_active)
7051 cap->cmdchar = 'r';
7052 cap->nchar = cap->extra_char;
7053 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7055 else
7056 # endif
7057 if (!checkclearopq(cap->oap))
7059 if (!curbuf->b_p_ma)
7060 EMSG(_(e_modifiable));
7061 else
7063 if (cap->extra_char == Ctrl_V) /* get another character */
7064 cap->extra_char = get_literal();
7065 stuffcharReadbuff(cap->extra_char);
7066 stuffcharReadbuff(ESC);
7067 # ifdef FEAT_VIRTUALEDIT
7068 if (virtual_active())
7069 coladvance(getviscol());
7070 # endif
7071 invoke_edit(cap, TRUE, 'v', FALSE);
7075 #endif
7078 * Swap case for "~" command, when it does not work like an operator.
7080 static void
7081 n_swapchar(cap)
7082 cmdarg_T *cap;
7084 long n;
7085 pos_T startpos;
7086 int did_change = 0;
7087 #ifdef FEAT_NETBEANS_INTG
7088 pos_T pos;
7089 char_u *ptr;
7090 int count;
7091 #endif
7093 if (checkclearopq(cap->oap))
7094 return;
7096 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7098 clearopbeep(cap->oap);
7099 return;
7102 prep_redo_cmd(cap);
7104 if (u_save_cursor() == FAIL)
7105 return;
7107 startpos = curwin->w_cursor;
7108 #ifdef FEAT_NETBEANS_INTG
7109 pos = startpos;
7110 #endif
7111 for (n = cap->count1; n > 0; --n)
7113 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7114 inc_cursor();
7115 if (gchar_cursor() == NUL)
7117 if (vim_strchr(p_ww, '~') != NULL
7118 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7120 #ifdef FEAT_NETBEANS_INTG
7121 if (usingNetbeans)
7123 if (did_change)
7125 ptr = ml_get(pos.lnum);
7126 count = (int)STRLEN(ptr) - pos.col;
7127 netbeans_removed(curbuf, pos.lnum, pos.col,
7128 (long)count);
7129 netbeans_inserted(curbuf, pos.lnum, pos.col,
7130 &ptr[pos.col], count);
7132 pos.col = 0;
7133 pos.lnum++;
7135 #endif
7136 ++curwin->w_cursor.lnum;
7137 curwin->w_cursor.col = 0;
7138 if (n > 1)
7140 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7141 break;
7142 u_clearline();
7145 else
7146 break;
7149 #ifdef FEAT_NETBEANS_INTG
7150 if (did_change && usingNetbeans)
7152 ptr = ml_get(pos.lnum);
7153 count = curwin->w_cursor.col - pos.col;
7154 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7155 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
7157 #endif
7160 check_cursor();
7161 curwin->w_set_curswant = TRUE;
7162 if (did_change)
7164 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7165 0L);
7166 curbuf->b_op_start = startpos;
7167 curbuf->b_op_end = curwin->w_cursor;
7168 if (curbuf->b_op_end.col > 0)
7169 --curbuf->b_op_end.col;
7174 * Move cursor to mark.
7176 static void
7177 nv_cursormark(cap, flag, pos)
7178 cmdarg_T *cap;
7179 int flag;
7180 pos_T *pos;
7182 if (check_mark(pos) == FAIL)
7183 clearop(cap->oap);
7184 else
7186 if (cap->cmdchar == '\''
7187 || cap->cmdchar == '`'
7188 || cap->cmdchar == '['
7189 || cap->cmdchar == ']')
7190 setpcmark();
7191 curwin->w_cursor = *pos;
7192 if (flag)
7193 beginline(BL_WHITE | BL_FIX);
7194 else
7195 check_cursor();
7197 cap->oap->motion_type = flag ? MLINE : MCHAR;
7198 if (cap->cmdchar == '`')
7199 cap->oap->use_reg_one = TRUE;
7200 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7201 curwin->w_set_curswant = TRUE;
7204 #ifdef FEAT_VISUAL
7206 * Handle commands that are operators in Visual mode.
7208 static void
7209 v_visop(cap)
7210 cmdarg_T *cap;
7212 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7214 /* Uppercase means linewise, except in block mode, then "D" deletes till
7215 * the end of the line, and "C" replaces til EOL */
7216 if (isupper(cap->cmdchar))
7218 if (VIsual_mode != Ctrl_V)
7219 VIsual_mode = 'V';
7220 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7221 curwin->w_curswant = MAXCOL;
7223 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7224 nv_operator(cap);
7226 #endif
7229 * "s" and "S" commands.
7231 static void
7232 nv_subst(cap)
7233 cmdarg_T *cap;
7235 #ifdef FEAT_VISUAL
7236 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7238 if (cap->cmdchar == 'S')
7239 VIsual_mode = 'V';
7240 cap->cmdchar = 'c';
7241 nv_operator(cap);
7243 else
7244 #endif
7245 nv_optrans(cap);
7249 * Abbreviated commands.
7251 static void
7252 nv_abbrev(cap)
7253 cmdarg_T *cap;
7255 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7256 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7258 #ifdef FEAT_VISUAL
7259 /* in Visual mode these commands are operators */
7260 if (VIsual_active)
7261 v_visop(cap);
7262 else
7263 #endif
7264 nv_optrans(cap);
7268 * Translate a command into another command.
7270 static void
7271 nv_optrans(cap)
7272 cmdarg_T *cap;
7274 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7275 (char_u *)"d$", (char_u *)"c$",
7276 (char_u *)"cl", (char_u *)"cc",
7277 (char_u *)"yy", (char_u *)":s\r"};
7278 static char_u *str = (char_u *)"xXDCsSY&";
7280 if (!checkclearopq(cap->oap))
7282 /* In Vi "2D" doesn't delete the next line. Can't translate it
7283 * either, because "2." should also not use the count. */
7284 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7286 cap->oap->start = curwin->w_cursor;
7287 cap->oap->op_type = OP_DELETE;
7288 #ifdef FEAT_EVAL
7289 set_op_var(OP_DELETE);
7290 #endif
7291 cap->count1 = 1;
7292 nv_dollar(cap);
7293 finish_op = TRUE;
7294 ResetRedobuff();
7295 AppendCharToRedobuff('D');
7297 else
7299 if (cap->count0)
7300 stuffnumReadbuff(cap->count0);
7301 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7304 cap->opcount = 0;
7308 * "'" and "`" commands. Also for "g'" and "g`".
7309 * cap->arg is TRUE for "'" and "g'".
7311 static void
7312 nv_gomark(cap)
7313 cmdarg_T *cap;
7315 pos_T *pos;
7316 int c;
7317 #ifdef FEAT_FOLDING
7318 linenr_T lnum = curwin->w_cursor.lnum;
7319 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7320 #endif
7322 if (cap->cmdchar == 'g')
7323 c = cap->extra_char;
7324 else
7325 c = cap->nchar;
7326 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7327 if (pos == (pos_T *)-1) /* jumped to other file */
7329 if (cap->arg)
7331 check_cursor_lnum();
7332 beginline(BL_WHITE | BL_FIX);
7334 else
7335 check_cursor();
7337 else
7338 nv_cursormark(cap, cap->arg, pos);
7340 #ifdef FEAT_VIRTUALEDIT
7341 /* May need to clear the coladd that a mark includes. */
7342 if (!virtual_active())
7343 curwin->w_cursor.coladd = 0;
7344 #endif
7345 #ifdef FEAT_FOLDING
7346 if (cap->oap->op_type == OP_NOP
7347 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7348 && (fdo_flags & FDO_MARK)
7349 && old_KeyTyped)
7350 foldOpenCursor();
7351 #endif
7355 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7357 static void
7358 nv_pcmark(cap)
7359 cmdarg_T *cap;
7361 #ifdef FEAT_JUMPLIST
7362 pos_T *pos;
7363 # ifdef FEAT_FOLDING
7364 linenr_T lnum = curwin->w_cursor.lnum;
7365 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7366 # endif
7368 if (!checkclearopq(cap->oap))
7370 if (cap->cmdchar == 'g')
7371 pos = movechangelist((int)cap->count1);
7372 else
7373 pos = movemark((int)cap->count1);
7374 if (pos == (pos_T *)-1) /* jump to other file */
7376 curwin->w_set_curswant = TRUE;
7377 check_cursor();
7379 else if (pos != NULL) /* can jump */
7380 nv_cursormark(cap, FALSE, pos);
7381 else if (cap->cmdchar == 'g')
7383 if (curbuf->b_changelistlen == 0)
7384 EMSG(_("E664: changelist is empty"));
7385 else if (cap->count1 < 0)
7386 EMSG(_("E662: At start of changelist"));
7387 else
7388 EMSG(_("E663: At end of changelist"));
7390 else
7391 clearopbeep(cap->oap);
7392 # ifdef FEAT_FOLDING
7393 if (cap->oap->op_type == OP_NOP
7394 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7395 && (fdo_flags & FDO_MARK)
7396 && old_KeyTyped)
7397 foldOpenCursor();
7398 # endif
7400 #else
7401 clearopbeep(cap->oap);
7402 #endif
7406 * Handle '"' command.
7408 static void
7409 nv_regname(cap)
7410 cmdarg_T *cap;
7412 if (checkclearop(cap->oap))
7413 return;
7414 #ifdef FEAT_EVAL
7415 if (cap->nchar == '=')
7416 cap->nchar = get_expr_register();
7417 #endif
7418 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7420 cap->oap->regname = cap->nchar;
7421 cap->opcount = cap->count0; /* remember count before '"' */
7422 #ifdef FEAT_EVAL
7423 set_reg_var(cap->oap->regname);
7424 #endif
7426 else
7427 clearopbeep(cap->oap);
7430 #ifdef FEAT_VISUAL
7432 * Handle "v", "V" and "CTRL-V" commands.
7433 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7434 * is TRUE.
7435 * Handle CTRL-Q just like CTRL-V.
7437 static void
7438 nv_visual(cap)
7439 cmdarg_T *cap;
7441 if (cap->cmdchar == Ctrl_Q)
7442 cap->cmdchar = Ctrl_V;
7444 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7445 * characterwise, linewise, or blockwise. */
7446 if (cap->oap->op_type != OP_NOP)
7448 cap->oap->motion_force = cap->cmdchar;
7449 finish_op = FALSE; /* operator doesn't finish now but later */
7450 return;
7453 VIsual_select = cap->arg;
7454 if (VIsual_active) /* change Visual mode */
7456 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7457 end_visual_mode();
7458 else /* toggle char/block mode */
7459 { /* or char/line mode */
7460 VIsual_mode = cap->cmdchar;
7461 showmode();
7463 redraw_curbuf_later(INVERTED); /* update the inversion */
7465 else /* start Visual mode */
7467 check_visual_highlight();
7468 if (cap->count0) /* use previously selected part */
7470 if (resel_VIsual_mode == NUL) /* there is none */
7472 beep_flush();
7473 return;
7475 VIsual = curwin->w_cursor;
7477 VIsual_active = TRUE;
7478 VIsual_reselect = TRUE;
7479 if (!cap->arg)
7480 /* start Select mode when 'selectmode' contains "cmd" */
7481 may_start_select('c');
7482 #ifdef FEAT_MOUSE
7483 setmouse();
7484 #endif
7485 if (p_smd && msg_silent == 0)
7486 redraw_cmdline = TRUE; /* show visual mode later */
7488 * For V and ^V, we multiply the number of lines even if there
7489 * was only one -- webb
7491 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7493 curwin->w_cursor.lnum +=
7494 resel_VIsual_line_count * cap->count0 - 1;
7495 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7496 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7498 VIsual_mode = resel_VIsual_mode;
7499 if (VIsual_mode == 'v')
7501 if (resel_VIsual_line_count <= 1)
7502 curwin->w_cursor.col += resel_VIsual_col * cap->count0 - 1;
7503 else
7504 curwin->w_cursor.col = resel_VIsual_col;
7505 check_cursor_col();
7507 if (resel_VIsual_col == MAXCOL)
7509 curwin->w_curswant = MAXCOL;
7510 coladvance((colnr_T)MAXCOL);
7512 else if (VIsual_mode == Ctrl_V)
7514 validate_virtcol();
7515 curwin->w_curswant = curwin->w_virtcol
7516 + resel_VIsual_col * cap->count0 - 1;
7517 coladvance(curwin->w_curswant);
7519 else
7520 curwin->w_set_curswant = TRUE;
7521 redraw_curbuf_later(INVERTED); /* show the inversion */
7523 else
7525 if (!cap->arg)
7526 /* start Select mode when 'selectmode' contains "cmd" */
7527 may_start_select('c');
7528 n_start_visual_mode(cap->cmdchar);
7534 * Start selection for Shift-movement keys.
7536 void
7537 start_selection()
7539 /* if 'selectmode' contains "key", start Select mode */
7540 may_start_select('k');
7541 n_start_visual_mode('v');
7545 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7547 void
7548 may_start_select(c)
7549 int c;
7551 VIsual_select = (stuff_empty() && typebuf_typed()
7552 && (vim_strchr(p_slm, c) != NULL));
7556 * Start Visual mode "c".
7557 * Should set VIsual_select before calling this.
7559 static void
7560 n_start_visual_mode(c)
7561 int c;
7563 VIsual_mode = c;
7564 VIsual_active = TRUE;
7565 VIsual_reselect = TRUE;
7566 #ifdef FEAT_VIRTUALEDIT
7567 /* Corner case: the 0 position in a tab may change when going into
7568 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7570 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7571 coladvance(curwin->w_virtcol);
7572 #endif
7573 VIsual = curwin->w_cursor;
7575 #ifdef FEAT_FOLDING
7576 foldAdjustVisual();
7577 #endif
7579 #ifdef FEAT_MOUSE
7580 setmouse();
7581 #endif
7582 if (p_smd && msg_silent == 0)
7583 redraw_cmdline = TRUE; /* show visual mode later */
7584 #ifdef FEAT_CLIPBOARD
7585 /* Make sure the clipboard gets updated. Needed because start and
7586 * end may still be the same, and the selection needs to be owned */
7587 clip_star.vmode = NUL;
7588 #endif
7590 /* Only need to redraw this line, unless still need to redraw an old
7591 * Visual area (when 'lazyredraw' is set). */
7592 if (curwin->w_redr_type < INVERTED)
7594 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7595 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7599 #endif /* FEAT_VISUAL */
7602 * CTRL-W: Window commands
7604 static void
7605 nv_window(cap)
7606 cmdarg_T *cap;
7608 #ifdef FEAT_WINDOWS
7609 if (!checkclearop(cap->oap))
7610 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7611 #else
7612 (void)checkclearop(cap->oap);
7613 #endif
7617 * CTRL-Z: Suspend
7619 static void
7620 nv_suspend(cap)
7621 cmdarg_T *cap;
7623 clearop(cap->oap);
7624 #ifdef FEAT_VISUAL
7625 if (VIsual_active)
7626 end_visual_mode(); /* stop Visual mode */
7627 #endif
7628 do_cmdline_cmd((char_u *)"st");
7632 * Commands starting with "g".
7634 static void
7635 nv_g_cmd(cap)
7636 cmdarg_T *cap;
7638 oparg_T *oap = cap->oap;
7639 #ifdef FEAT_VISUAL
7640 pos_T tpos;
7641 #endif
7642 int i;
7643 int flag = FALSE;
7645 switch (cap->nchar)
7647 #ifdef MEM_PROFILE
7649 * "g^A": dump log of used memory.
7651 case Ctrl_A:
7652 vim_mem_profile_dump();
7653 break;
7654 #endif
7656 #ifdef FEAT_VREPLACE
7658 * "gR": Enter virtual replace mode.
7660 case 'R':
7661 cap->arg = TRUE;
7662 nv_Replace(cap);
7663 break;
7665 case 'r':
7666 nv_vreplace(cap);
7667 break;
7668 #endif
7670 case '&':
7671 do_cmdline_cmd((char_u *)"%s//~/&");
7672 break;
7674 #ifdef FEAT_VISUAL
7676 * "gv": Reselect the previous Visual area. If Visual already active,
7677 * exchange previous and current Visual area.
7679 case 'v':
7680 if (checkclearop(oap))
7681 break;
7683 if ( curbuf->b_visual.vi_start.lnum == 0
7684 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7685 || curbuf->b_visual.vi_end.lnum == 0)
7686 beep_flush();
7687 else
7689 /* set w_cursor to the start of the Visual area, tpos to the end */
7690 if (VIsual_active)
7692 i = VIsual_mode;
7693 VIsual_mode = curbuf->b_visual.vi_mode;
7694 curbuf->b_visual.vi_mode = i;
7695 # ifdef FEAT_EVAL
7696 curbuf->b_visual_mode_eval = i;
7697 # endif
7698 i = curwin->w_curswant;
7699 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7700 curbuf->b_visual.vi_curswant = i;
7702 tpos = curbuf->b_visual.vi_end;
7703 curbuf->b_visual.vi_end = curwin->w_cursor;
7704 curwin->w_cursor = curbuf->b_visual.vi_start;
7705 curbuf->b_visual.vi_start = VIsual;
7707 else
7709 VIsual_mode = curbuf->b_visual.vi_mode;
7710 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7711 tpos = curbuf->b_visual.vi_end;
7712 curwin->w_cursor = curbuf->b_visual.vi_start;
7715 VIsual_active = TRUE;
7716 VIsual_reselect = TRUE;
7718 /* Set Visual to the start and w_cursor to the end of the Visual
7719 * area. Make sure they are on an existing character. */
7720 check_cursor();
7721 VIsual = curwin->w_cursor;
7722 curwin->w_cursor = tpos;
7723 check_cursor();
7724 update_topline();
7726 * When called from normal "g" command: start Select mode when
7727 * 'selectmode' contains "cmd". When called for K_SELECT, always
7728 * start Select mode.
7730 if (cap->arg)
7731 VIsual_select = TRUE;
7732 else
7733 may_start_select('c');
7734 #ifdef FEAT_MOUSE
7735 setmouse();
7736 #endif
7737 #ifdef FEAT_CLIPBOARD
7738 /* Make sure the clipboard gets updated. Needed because start and
7739 * end are still the same, and the selection needs to be owned */
7740 clip_star.vmode = NUL;
7741 #endif
7742 redraw_curbuf_later(INVERTED);
7743 showmode();
7745 break;
7747 * "gV": Don't reselect the previous Visual area after a Select mode
7748 * mapping of menu.
7750 case 'V':
7751 VIsual_reselect = FALSE;
7752 break;
7755 * "gh": start Select mode.
7756 * "gH": start Select line mode.
7757 * "g^H": start Select block mode.
7759 case K_BS:
7760 cap->nchar = Ctrl_H;
7761 /* FALLTHROUGH */
7762 case 'h':
7763 case 'H':
7764 case Ctrl_H:
7765 # ifdef EBCDIC
7766 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7767 if (cap->nchar == Ctrl_H)
7768 cap->cmdchar = Ctrl_V;
7769 else
7770 # endif
7771 cap->cmdchar = cap->nchar + ('v' - 'h');
7772 cap->arg = TRUE;
7773 nv_visual(cap);
7774 break;
7775 #endif /* FEAT_VISUAL */
7778 * "gj" and "gk" two new funny movement keys -- up and down
7779 * movement based on *screen* line rather than *file* line.
7781 case 'j':
7782 case K_DOWN:
7783 /* with 'nowrap' it works just like the normal "j" command; also when
7784 * in a closed fold */
7785 if (!curwin->w_p_wrap
7786 #ifdef FEAT_FOLDING
7787 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7788 #endif
7791 oap->motion_type = MLINE;
7792 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7794 else
7795 i = nv_screengo(oap, FORWARD, cap->count1);
7796 if (i == FAIL)
7797 clearopbeep(oap);
7798 break;
7800 case 'k':
7801 case K_UP:
7802 /* with 'nowrap' it works just like the normal "k" command; also when
7803 * in a closed fold */
7804 if (!curwin->w_p_wrap
7805 #ifdef FEAT_FOLDING
7806 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7807 #endif
7810 oap->motion_type = MLINE;
7811 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7813 else
7814 i = nv_screengo(oap, BACKWARD, cap->count1);
7815 if (i == FAIL)
7816 clearopbeep(oap);
7817 break;
7820 * "gJ": join two lines without inserting a space.
7822 case 'J':
7823 nv_join(cap);
7824 break;
7827 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7828 * "gm": middle of "g0" and "g$".
7830 case '^':
7831 flag = TRUE;
7832 /* FALLTHROUGH */
7834 case '0':
7835 case 'm':
7836 case K_HOME:
7837 case K_KHOME:
7838 oap->motion_type = MCHAR;
7839 oap->inclusive = FALSE;
7840 if (curwin->w_p_wrap
7841 #ifdef FEAT_VERTSPLIT
7842 && curwin->w_width != 0
7843 #endif
7846 int width1 = W_WIDTH(curwin) - curwin_col_off();
7847 int width2 = width1 + curwin_col_off2();
7849 validate_virtcol();
7850 i = 0;
7851 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
7852 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
7854 else
7855 i = curwin->w_leftcol;
7856 /* Go to the middle of the screen line. When 'number' is on and lines
7857 * are wrapping the middle can be more to the left. */
7858 if (cap->nchar == 'm')
7859 i += (W_WIDTH(curwin) - curwin_col_off()
7860 + ((curwin->w_p_wrap && i > 0)
7861 ? curwin_col_off2() : 0)) / 2;
7862 coladvance((colnr_T)i);
7863 if (flag)
7866 i = gchar_cursor();
7867 while (vim_iswhite(i) && oneright() == OK);
7869 curwin->w_set_curswant = TRUE;
7870 break;
7872 case '_':
7873 /* "g_": to the last non-blank character in the line or <count> lines
7874 * downward. */
7875 cap->oap->motion_type = MCHAR;
7876 cap->oap->inclusive = TRUE;
7877 curwin->w_curswant = MAXCOL;
7878 if (cursor_down((long)(cap->count1 - 1),
7879 cap->oap->op_type == OP_NOP) == FAIL)
7880 clearopbeep(cap->oap);
7881 else
7883 char_u *ptr = ml_get_curline();
7885 /* In Visual mode we may end up after the line. */
7886 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
7887 --curwin->w_cursor.col;
7889 /* Decrease the cursor column until it's on a non-blank. */
7890 while (curwin->w_cursor.col > 0
7891 && vim_iswhite(ptr[curwin->w_cursor.col]))
7892 --curwin->w_cursor.col;
7893 curwin->w_set_curswant = TRUE;
7895 break;
7897 case '$':
7898 case K_END:
7899 case K_KEND:
7901 int col_off = curwin_col_off();
7903 oap->motion_type = MCHAR;
7904 oap->inclusive = TRUE;
7905 if (curwin->w_p_wrap
7906 #ifdef FEAT_VERTSPLIT
7907 && curwin->w_width != 0
7908 #endif
7911 curwin->w_curswant = MAXCOL; /* so we stay at the end */
7912 if (cap->count1 == 1)
7914 int width1 = W_WIDTH(curwin) - col_off;
7915 int width2 = width1 + curwin_col_off2();
7917 validate_virtcol();
7918 i = width1 - 1;
7919 if (curwin->w_virtcol >= (colnr_T)width1)
7920 i += ((curwin->w_virtcol - width1) / width2 + 1)
7921 * width2;
7922 coladvance((colnr_T)i);
7923 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
7924 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
7927 * Check for landing on a character that got split at
7928 * the end of the line. We do not want to advance to
7929 * the next screen line.
7931 validate_virtcol();
7932 if (curwin->w_virtcol > (colnr_T)i)
7933 --curwin->w_cursor.col;
7935 #endif
7937 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
7938 clearopbeep(oap);
7940 else
7942 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
7943 coladvance((colnr_T)i);
7945 /* Make sure we stick in this column. */
7946 validate_virtcol();
7947 curwin->w_curswant = curwin->w_virtcol;
7948 curwin->w_set_curswant = FALSE;
7951 break;
7954 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
7956 case '*':
7957 case '#':
7958 #if POUND != '#'
7959 case POUND: /* pound sign (sometimes equal to '#') */
7960 #endif
7961 case Ctrl_RSB: /* :tag or :tselect for current identifier */
7962 case ']': /* :tselect for current identifier */
7963 nv_ident(cap);
7964 break;
7967 * ge and gE: go back to end of word
7969 case 'e':
7970 case 'E':
7971 oap->motion_type = MCHAR;
7972 curwin->w_set_curswant = TRUE;
7973 oap->inclusive = TRUE;
7974 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
7975 clearopbeep(oap);
7976 break;
7979 * "g CTRL-G": display info about cursor position
7981 case Ctrl_G:
7982 cursor_pos_info();
7983 break;
7986 * "gi": start Insert at the last position.
7988 case 'i':
7989 if (curbuf->b_last_insert.lnum != 0)
7991 curwin->w_cursor = curbuf->b_last_insert;
7992 check_cursor_lnum();
7993 i = (int)STRLEN(ml_get_curline());
7994 if (curwin->w_cursor.col > (colnr_T)i)
7996 #ifdef FEAT_VIRTUALEDIT
7997 if (virtual_active())
7998 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
7999 #endif
8000 curwin->w_cursor.col = i;
8003 cap->cmdchar = 'i';
8004 nv_edit(cap);
8005 break;
8008 * "gI": Start insert in column 1.
8010 case 'I':
8011 beginline(0);
8012 if (!checkclearopq(oap))
8013 invoke_edit(cap, FALSE, 'g', FALSE);
8014 break;
8016 #ifdef FEAT_SEARCHPATH
8018 * "gf": goto file, edit file under cursor
8019 * "]f" and "[f": can also be used.
8021 case 'f':
8022 case 'F':
8023 nv_gotofile(cap);
8024 break;
8025 #endif
8027 /* "g'm" and "g`m": jump to mark without setting pcmark */
8028 case '\'':
8029 cap->arg = TRUE;
8030 /*FALLTHROUGH*/
8031 case '`':
8032 nv_gomark(cap);
8033 break;
8036 * "gs": Goto sleep.
8038 case 's':
8039 do_sleep(cap->count1 * 1000L);
8040 break;
8043 * "ga": Display the ascii value of the character under the
8044 * cursor. It is displayed in decimal, hex, and octal. -- webb
8046 case 'a':
8047 do_ascii(NULL);
8048 break;
8050 #ifdef FEAT_MBYTE
8052 * "g8": Display the bytes used for the UTF-8 character under the
8053 * cursor. It is displayed in hex.
8054 * "8g8" finds illegal byte sequence.
8056 case '8':
8057 if (cap->count0 == 8)
8058 utf_find_illegal();
8059 else
8060 show_utf8();
8061 break;
8062 #endif
8064 case '<':
8065 show_sb_text();
8066 break;
8069 * "gg": Goto the first line in file. With a count it goes to
8070 * that line number like for "G". -- webb
8072 case 'g':
8073 cap->arg = FALSE;
8074 nv_goto(cap);
8075 break;
8078 * Two-character operators:
8079 * "gq" Format text
8080 * "gw" Format text and keep cursor position
8081 * "g~" Toggle the case of the text.
8082 * "gu" Change text to lower case.
8083 * "gU" Change text to upper case.
8084 * "g?" rot13 encoding
8085 * "g@" call 'operatorfunc'
8087 case 'q':
8088 case 'w':
8089 oap->cursor_start = curwin->w_cursor;
8090 /*FALLTHROUGH*/
8091 case '~':
8092 case 'u':
8093 case 'U':
8094 case '?':
8095 case '@':
8096 nv_operator(cap);
8097 break;
8100 * "gd": Find first occurrence of pattern under the cursor in the
8101 * current function
8102 * "gD": idem, but in the current file.
8104 case 'd':
8105 case 'D':
8106 nv_gd(oap, cap->nchar, (int)cap->count0);
8107 break;
8109 #ifdef FEAT_MOUSE
8111 * g<*Mouse> : <C-*mouse>
8113 case K_MIDDLEMOUSE:
8114 case K_MIDDLEDRAG:
8115 case K_MIDDLERELEASE:
8116 case K_LEFTMOUSE:
8117 case K_LEFTDRAG:
8118 case K_LEFTRELEASE:
8119 case K_RIGHTMOUSE:
8120 case K_RIGHTDRAG:
8121 case K_RIGHTRELEASE:
8122 case K_X1MOUSE:
8123 case K_X1DRAG:
8124 case K_X1RELEASE:
8125 case K_X2MOUSE:
8126 case K_X2DRAG:
8127 case K_X2RELEASE:
8128 mod_mask = MOD_MASK_CTRL;
8129 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8130 break;
8131 #endif
8133 case K_IGNORE:
8134 break;
8137 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8139 case 'p':
8140 case 'P':
8141 nv_put(cap);
8142 break;
8144 #ifdef FEAT_BYTEOFF
8145 /* "go": goto byte count from start of buffer */
8146 case 'o':
8147 goto_byte(cap->count0);
8148 break;
8149 #endif
8151 /* "gQ": improved Ex mode */
8152 case 'Q':
8153 if (text_locked())
8155 clearopbeep(cap->oap);
8156 text_locked_msg();
8157 break;
8160 if (!checkclearopq(oap))
8161 do_exmode(TRUE);
8162 break;
8164 #ifdef FEAT_JUMPLIST
8165 case ',':
8166 nv_pcmark(cap);
8167 break;
8169 case ';':
8170 cap->count1 = -cap->count1;
8171 nv_pcmark(cap);
8172 break;
8173 #endif
8175 #ifdef FEAT_WINDOWS
8176 case 't':
8177 goto_tabpage((int)cap->count0);
8178 break;
8179 case 'T':
8180 goto_tabpage(-(int)cap->count1);
8181 break;
8182 #endif
8184 case '+':
8185 case '-': /* "g+" and "g-": undo or redo along the timeline */
8186 if (!checkclearopq(oap))
8187 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
8188 FALSE, FALSE);
8189 break;
8191 default:
8192 clearopbeep(oap);
8193 break;
8198 * Handle "o" and "O" commands.
8200 static void
8201 n_opencmd(cap)
8202 cmdarg_T *cap;
8204 if (!checkclearopq(cap->oap))
8206 #ifdef FEAT_FOLDING
8207 if (cap->cmdchar == 'O')
8208 /* Open above the first line of a folded sequence of lines */
8209 (void)hasFolding(curwin->w_cursor.lnum,
8210 &curwin->w_cursor.lnum, NULL);
8211 else
8212 /* Open below the last line of a folded sequence of lines */
8213 (void)hasFolding(curwin->w_cursor.lnum,
8214 NULL, &curwin->w_cursor.lnum);
8215 #endif
8216 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8217 (cap->cmdchar == 'O' ? 1 : 0)),
8218 (linenr_T)(curwin->w_cursor.lnum +
8219 (cap->cmdchar == 'o' ? 1 : 0))
8220 ) == OK
8221 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8222 #ifdef FEAT_COMMENTS
8223 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8224 #endif
8225 0, 0))
8227 /* When '#' is in 'cpoptions' ignore the count. */
8228 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8229 cap->count1 = 1;
8230 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8236 * "." command: redo last change.
8238 static void
8239 nv_dot(cap)
8240 cmdarg_T *cap;
8242 if (!checkclearopq(cap->oap))
8245 * If "restart_edit" is TRUE, the last but one command is repeated
8246 * instead of the last command (inserting text). This is used for
8247 * CTRL-O <.> in insert mode.
8249 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8250 clearopbeep(cap->oap);
8255 * CTRL-R: undo undo
8257 static void
8258 nv_redo(cap)
8259 cmdarg_T *cap;
8261 if (!checkclearopq(cap->oap))
8263 u_redo((int)cap->count1);
8264 curwin->w_set_curswant = TRUE;
8269 * Handle "U" command.
8271 static void
8272 nv_Undo(cap)
8273 cmdarg_T *cap;
8275 /* In Visual mode and typing "gUU" triggers an operator */
8276 if (cap->oap->op_type == OP_UPPER
8277 #ifdef FEAT_VISUAL
8278 || VIsual_active
8279 #endif
8282 /* translate "gUU" to "gUgU" */
8283 cap->cmdchar = 'g';
8284 cap->nchar = 'U';
8285 nv_operator(cap);
8287 else if (!checkclearopq(cap->oap))
8289 u_undoline();
8290 curwin->w_set_curswant = TRUE;
8295 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8296 * single character.
8298 static void
8299 nv_tilde(cap)
8300 cmdarg_T *cap;
8302 if (!p_to
8303 #ifdef FEAT_VISUAL
8304 && !VIsual_active
8305 #endif
8306 && cap->oap->op_type != OP_TILDE)
8307 n_swapchar(cap);
8308 else
8309 nv_operator(cap);
8313 * Handle an operator command.
8314 * The actual work is done by do_pending_operator().
8316 static void
8317 nv_operator(cap)
8318 cmdarg_T *cap;
8320 int op_type;
8322 op_type = get_op_type(cap->cmdchar, cap->nchar);
8324 if (op_type == cap->oap->op_type) /* double operator works on lines */
8325 nv_lineop(cap);
8326 else if (!checkclearop(cap->oap))
8328 cap->oap->start = curwin->w_cursor;
8329 cap->oap->op_type = op_type;
8330 #ifdef FEAT_EVAL
8331 set_op_var(op_type);
8332 #endif
8336 #ifdef FEAT_EVAL
8338 * Set v:operator to the characters for "optype".
8340 static void
8341 set_op_var(optype)
8342 int optype;
8344 char_u opchars[3];
8346 if (optype == OP_NOP)
8347 set_vim_var_string(VV_OP, NULL, 0);
8348 else
8350 opchars[0] = get_op_char(optype);
8351 opchars[1] = get_extra_op_char(optype);
8352 opchars[2] = NUL;
8353 set_vim_var_string(VV_OP, opchars, -1);
8356 #endif
8359 * Handle linewise operator "dd", "yy", etc.
8361 * "_" is is a strange motion command that helps make operators more logical.
8362 * It is actually implemented, but not documented in the real Vi. This motion
8363 * command actually refers to "the current line". Commands like "dd" and "yy"
8364 * are really an alternate form of "d_" and "y_". It does accept a count, so
8365 * "d3_" works to delete 3 lines.
8367 static void
8368 nv_lineop(cap)
8369 cmdarg_T *cap;
8371 cap->oap->motion_type = MLINE;
8372 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8373 clearopbeep(cap->oap);
8374 else if ( cap->oap->op_type == OP_DELETE
8375 || cap->oap->op_type == OP_LSHIFT
8376 || cap->oap->op_type == OP_RSHIFT)
8377 beginline(BL_SOL | BL_FIX);
8378 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8379 beginline(BL_WHITE | BL_FIX);
8383 * <Home> command.
8385 static void
8386 nv_home(cap)
8387 cmdarg_T *cap;
8389 /* CTRL-HOME is like "gg" */
8390 if (mod_mask & MOD_MASK_CTRL)
8391 nv_goto(cap);
8392 else
8394 cap->count0 = 1;
8395 nv_pipe(cap);
8397 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8398 one-character line). */
8402 * "|" command.
8404 static void
8405 nv_pipe(cap)
8406 cmdarg_T *cap;
8408 cap->oap->motion_type = MCHAR;
8409 cap->oap->inclusive = FALSE;
8410 beginline(0);
8411 if (cap->count0 > 0)
8413 coladvance((colnr_T)(cap->count0 - 1));
8414 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8416 else
8417 curwin->w_curswant = 0;
8418 /* keep curswant at the column where we wanted to go, not where
8419 we ended; differs if line is too short */
8420 curwin->w_set_curswant = FALSE;
8424 * Handle back-word command "b" and "B".
8425 * cap->arg is 1 for "B"
8427 static void
8428 nv_bck_word(cap)
8429 cmdarg_T *cap;
8431 cap->oap->motion_type = MCHAR;
8432 cap->oap->inclusive = FALSE;
8433 curwin->w_set_curswant = TRUE;
8434 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8435 clearopbeep(cap->oap);
8436 #ifdef FEAT_FOLDING
8437 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8438 foldOpenCursor();
8439 #endif
8443 * Handle word motion commands "e", "E", "w" and "W".
8444 * cap->arg is TRUE for "E" and "W".
8446 static void
8447 nv_wordcmd(cap)
8448 cmdarg_T *cap;
8450 int n;
8451 int word_end;
8452 int flag = FALSE;
8453 pos_T startpos = curwin->w_cursor;
8456 * Set inclusive for the "E" and "e" command.
8458 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8459 word_end = TRUE;
8460 else
8461 word_end = FALSE;
8462 cap->oap->inclusive = word_end;
8465 * "cw" and "cW" are a special case.
8467 if (!word_end && cap->oap->op_type == OP_CHANGE)
8469 n = gchar_cursor();
8470 if (n != NUL) /* not an empty line */
8472 if (vim_iswhite(n))
8475 * Reproduce a funny Vi behaviour: "cw" on a blank only
8476 * changes one character, not all blanks until the start of
8477 * the next word. Only do this when the 'w' flag is included
8478 * in 'cpoptions'.
8480 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8482 cap->oap->inclusive = TRUE;
8483 cap->oap->motion_type = MCHAR;
8484 return;
8487 else
8490 * This is a little strange. To match what the real Vi does,
8491 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8492 * that we are not on a space or a TAB. This seems impolite
8493 * at first, but it's really more what we mean when we say
8494 * 'cw'.
8495 * Another strangeness: When standing on the end of a word
8496 * "ce" will change until the end of the next wordt, but "cw"
8497 * will change only one character! This is done by setting
8498 * flag.
8500 cap->oap->inclusive = TRUE;
8501 word_end = TRUE;
8502 flag = TRUE;
8507 cap->oap->motion_type = MCHAR;
8508 curwin->w_set_curswant = TRUE;
8509 if (word_end)
8510 n = end_word(cap->count1, cap->arg, flag, FALSE);
8511 else
8512 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8514 /* Don't leave the cursor on the NUL past the end of line. Unless we
8515 * didn't move it forward. */
8516 if (lt(startpos, curwin->w_cursor))
8517 adjust_cursor(cap->oap);
8519 if (n == FAIL && cap->oap->op_type == OP_NOP)
8520 clearopbeep(cap->oap);
8521 else
8523 #ifdef FEAT_VISUAL
8524 adjust_for_sel(cap);
8525 #endif
8526 #ifdef FEAT_FOLDING
8527 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8528 foldOpenCursor();
8529 #endif
8534 * Used after a movement command: If the cursor ends up on the NUL after the
8535 * end of the line, may move it back to the last character and make the motion
8536 * inclusive.
8538 static void
8539 adjust_cursor(oap)
8540 oparg_T *oap;
8542 /* The cursor cannot remain on the NUL when:
8543 * - the column is > 0
8544 * - not in Visual mode or 'selection' is "o"
8545 * - 'virtualedit' is not "all" and not "onemore".
8547 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
8548 #ifdef FEAT_VISUAL
8549 && (!VIsual_active || *p_sel == 'o')
8550 #endif
8551 #ifdef FEAT_VIRTUALEDIT
8552 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8553 #endif
8556 --curwin->w_cursor.col;
8557 #ifdef FEAT_MBYTE
8558 /* prevent cursor from moving on the trail byte */
8559 if (has_mbyte)
8560 mb_adjust_cursor();
8561 #endif
8562 oap->inclusive = TRUE;
8567 * "0" and "^" commands.
8568 * cap->arg is the argument for beginline().
8570 static void
8571 nv_beginline(cap)
8572 cmdarg_T *cap;
8574 cap->oap->motion_type = MCHAR;
8575 cap->oap->inclusive = FALSE;
8576 beginline(cap->arg);
8577 #ifdef FEAT_FOLDING
8578 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8579 foldOpenCursor();
8580 #endif
8581 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8582 one-character line). */
8585 #ifdef FEAT_VISUAL
8587 * In exclusive Visual mode, may include the last character.
8589 static void
8590 adjust_for_sel(cap)
8591 cmdarg_T *cap;
8593 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8594 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8596 # ifdef FEAT_MBYTE
8597 if (has_mbyte)
8598 inc_cursor();
8599 else
8600 # endif
8601 ++curwin->w_cursor.col;
8602 cap->oap->inclusive = FALSE;
8607 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8608 * Should check VIsual_mode before calling this.
8609 * Returns TRUE when backed up to the previous line.
8611 static int
8612 unadjust_for_sel()
8614 pos_T *pp;
8616 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8618 if (lt(VIsual, curwin->w_cursor))
8619 pp = &curwin->w_cursor;
8620 else
8621 pp = &VIsual;
8622 #ifdef FEAT_VIRTUALEDIT
8623 if (pp->coladd > 0)
8624 --pp->coladd;
8625 else
8626 #endif
8627 if (pp->col > 0)
8629 --pp->col;
8630 #ifdef FEAT_MBYTE
8631 mb_adjustpos(pp);
8632 #endif
8634 else if (pp->lnum > 1)
8636 --pp->lnum;
8637 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8638 return TRUE;
8641 return FALSE;
8645 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8647 static void
8648 nv_select(cap)
8649 cmdarg_T *cap;
8651 if (VIsual_active)
8652 VIsual_select = TRUE;
8653 else if (VIsual_reselect)
8655 cap->nchar = 'v'; /* fake "gv" command */
8656 cap->arg = TRUE;
8657 nv_g_cmd(cap);
8661 #endif
8664 * "G", "gg", CTRL-END, CTRL-HOME.
8665 * cap->arg is TRUE for "G".
8667 static void
8668 nv_goto(cap)
8669 cmdarg_T *cap;
8671 linenr_T lnum;
8673 if (cap->arg)
8674 lnum = curbuf->b_ml.ml_line_count;
8675 else
8676 lnum = 1L;
8677 cap->oap->motion_type = MLINE;
8678 setpcmark();
8680 /* When a count is given, use it instead of the default lnum */
8681 if (cap->count0 != 0)
8682 lnum = cap->count0;
8683 if (lnum < 1L)
8684 lnum = 1L;
8685 else if (lnum > curbuf->b_ml.ml_line_count)
8686 lnum = curbuf->b_ml.ml_line_count;
8687 curwin->w_cursor.lnum = lnum;
8688 beginline(BL_SOL | BL_FIX);
8689 #ifdef FEAT_FOLDING
8690 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8691 foldOpenCursor();
8692 #endif
8696 * CTRL-\ in Normal mode.
8698 static void
8699 nv_normal(cap)
8700 cmdarg_T *cap;
8702 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8704 clearop(cap->oap);
8705 if (restart_edit != 0 && mode_displayed)
8706 clear_cmdline = TRUE; /* unshow mode later */
8707 restart_edit = 0;
8708 #ifdef FEAT_CMDWIN
8709 if (cmdwin_type != 0)
8710 cmdwin_result = Ctrl_C;
8711 #endif
8712 #ifdef FEAT_VISUAL
8713 if (VIsual_active)
8715 end_visual_mode(); /* stop Visual */
8716 redraw_curbuf_later(INVERTED);
8718 #endif
8719 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8720 if (cap->nchar == Ctrl_G && p_im)
8721 restart_edit = 'a';
8723 else
8724 clearopbeep(cap->oap);
8728 * ESC in Normal mode: beep, but don't flush buffers.
8729 * Don't even beep if we are canceling a command.
8731 static void
8732 nv_esc(cap)
8733 cmdarg_T *cap;
8735 int no_reason;
8737 no_reason = (cap->oap->op_type == OP_NOP
8738 && cap->opcount == 0
8739 && cap->count0 == 0
8740 && cap->oap->regname == 0
8741 && !p_im);
8743 if (cap->arg) /* TRUE for CTRL-C */
8745 if (restart_edit == 0
8746 #ifdef FEAT_CMDWIN
8747 && cmdwin_type == 0
8748 #endif
8749 #ifdef FEAT_VISUAL
8750 && !VIsual_active
8751 #endif
8752 && no_reason)
8753 MSG(_("Type :quit<Enter> to exit Vim"));
8755 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8756 * set again below when halfway a mapping. */
8757 if (!p_im)
8758 restart_edit = 0;
8759 #ifdef FEAT_CMDWIN
8760 if (cmdwin_type != 0)
8762 cmdwin_result = K_IGNORE;
8763 got_int = FALSE; /* don't stop executing autocommands et al. */
8764 return;
8766 #endif
8769 #ifdef FEAT_VISUAL
8770 if (VIsual_active)
8772 end_visual_mode(); /* stop Visual */
8773 check_cursor_col(); /* make sure cursor is not beyond EOL */
8774 curwin->w_set_curswant = TRUE;
8775 redraw_curbuf_later(INVERTED);
8777 else
8778 #endif
8779 if (no_reason)
8780 vim_beep();
8781 clearop(cap->oap);
8783 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8784 * set return to Insert mode afterwards. */
8785 if (restart_edit == 0 && goto_im()
8786 #ifdef FEAT_EX_EXTRA
8787 && ex_normal_busy == 0
8788 #endif
8790 restart_edit = 'a';
8794 * Handle "A", "a", "I", "i" and <Insert> commands.
8796 static void
8797 nv_edit(cap)
8798 cmdarg_T *cap;
8800 /* <Insert> is equal to "i" */
8801 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8802 cap->cmdchar = 'i';
8804 #ifdef FEAT_VISUAL
8805 /* in Visual mode "A" and "I" are an operator */
8806 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8807 v_visop(cap);
8809 /* in Visual mode and after an operator "a" and "i" are for text objects */
8810 else
8811 #endif
8812 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8813 && (cap->oap->op_type != OP_NOP
8814 #ifdef FEAT_VISUAL
8815 || VIsual_active
8816 #endif
8819 #ifdef FEAT_TEXTOBJ
8820 nv_object(cap);
8821 #else
8822 clearopbeep(cap->oap);
8823 #endif
8825 else if (!curbuf->b_p_ma && !p_im)
8827 /* Only give this error when 'insertmode' is off. */
8828 EMSG(_(e_modifiable));
8829 clearop(cap->oap);
8831 else if (!checkclearopq(cap->oap))
8833 switch (cap->cmdchar)
8835 case 'A': /* "A"ppend after the line */
8836 curwin->w_set_curswant = TRUE;
8837 #ifdef FEAT_VIRTUALEDIT
8838 if (ve_flags == VE_ALL)
8840 int save_State = State;
8842 /* Pretent Insert mode here to allow the cursor on the
8843 * character past the end of the line */
8844 State = INSERT;
8845 coladvance((colnr_T)MAXCOL);
8846 State = save_State;
8848 else
8849 #endif
8850 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8851 break;
8853 case 'I': /* "I"nsert before the first non-blank */
8854 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
8855 beginline(BL_WHITE);
8856 else
8857 beginline(BL_WHITE|BL_FIX);
8858 break;
8860 case 'a': /* "a"ppend is like "i"nsert on the next character. */
8861 #ifdef FEAT_VIRTUALEDIT
8862 /* increment coladd when in virtual space, increment the
8863 * column otherwise, also to append after an unprintable char */
8864 if (virtual_active()
8865 && (curwin->w_cursor.coladd > 0
8866 || *ml_get_cursor() == NUL
8867 || *ml_get_cursor() == TAB))
8868 curwin->w_cursor.coladd++;
8869 else
8870 #endif
8871 if (*ml_get_cursor() != NUL)
8872 inc_cursor();
8873 break;
8876 #ifdef FEAT_VIRTUALEDIT
8877 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
8879 int save_State = State;
8881 /* Pretent Insert mode here to allow the cursor on the
8882 * character past the end of the line */
8883 State = INSERT;
8884 coladvance(getviscol());
8885 State = save_State;
8887 #endif
8889 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
8894 * Invoke edit() and take care of "restart_edit" and the return value.
8896 static void
8897 invoke_edit(cap, repl, cmd, startln)
8898 cmdarg_T *cap;
8899 int repl; /* "r" or "gr" command */
8900 int cmd;
8901 int startln;
8903 int restart_edit_save = 0;
8905 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
8906 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
8907 * it. */
8908 if (repl || !stuff_empty())
8909 restart_edit_save = restart_edit;
8910 else
8911 restart_edit_save = 0;
8913 /* Always reset "restart_edit", this is not a restarted edit. */
8914 restart_edit = 0;
8916 if (edit(cmd, startln, cap->count1))
8917 cap->retval |= CA_COMMAND_BUSY;
8919 if (restart_edit == 0)
8920 restart_edit = restart_edit_save;
8923 #ifdef FEAT_TEXTOBJ
8925 * "a" or "i" while an operator is pending or in Visual mode: object motion.
8927 static void
8928 nv_object(cap)
8929 cmdarg_T *cap;
8931 int flag;
8932 int include;
8933 char_u *mps_save;
8935 if (cap->cmdchar == 'i')
8936 include = FALSE; /* "ix" = inner object: exclude white space */
8937 else
8938 include = TRUE; /* "ax" = an object: include white space */
8940 /* Make sure (), [], {} and <> are in 'matchpairs' */
8941 mps_save = curbuf->b_p_mps;
8942 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
8944 switch (cap->nchar)
8946 case 'w': /* "aw" = a word */
8947 flag = current_word(cap->oap, cap->count1, include, FALSE);
8948 break;
8949 case 'W': /* "aW" = a WORD */
8950 flag = current_word(cap->oap, cap->count1, include, TRUE);
8951 break;
8952 case 'b': /* "ab" = a braces block */
8953 case '(':
8954 case ')':
8955 flag = current_block(cap->oap, cap->count1, include, '(', ')');
8956 break;
8957 case 'B': /* "aB" = a Brackets block */
8958 case '{':
8959 case '}':
8960 flag = current_block(cap->oap, cap->count1, include, '{', '}');
8961 break;
8962 case '[': /* "a[" = a [] block */
8963 case ']':
8964 flag = current_block(cap->oap, cap->count1, include, '[', ']');
8965 break;
8966 case '<': /* "a<" = a <> block */
8967 case '>':
8968 flag = current_block(cap->oap, cap->count1, include, '<', '>');
8969 break;
8970 case 't': /* "at" = a tag block (xml and html) */
8971 flag = current_tagblock(cap->oap, cap->count1, include);
8972 break;
8973 case 'p': /* "ap" = a paragraph */
8974 flag = current_par(cap->oap, cap->count1, include, 'p');
8975 break;
8976 case 's': /* "as" = a sentence */
8977 flag = current_sent(cap->oap, cap->count1, include);
8978 break;
8979 case '"': /* "a"" = a double quoted string */
8980 case '\'': /* "a'" = a single quoted string */
8981 case '`': /* "a`" = a backtick quoted string */
8982 flag = current_quote(cap->oap, cap->count1, include,
8983 cap->nchar);
8984 break;
8985 #if 0 /* TODO */
8986 case 'S': /* "aS" = a section */
8987 case 'f': /* "af" = a filename */
8988 case 'u': /* "au" = a URL */
8989 #endif
8990 default:
8991 flag = FAIL;
8992 break;
8995 curbuf->b_p_mps = mps_save;
8996 if (flag == FAIL)
8997 clearopbeep(cap->oap);
8998 adjust_cursor_col();
8999 curwin->w_set_curswant = TRUE;
9001 #endif
9004 * "q" command: Start/stop recording.
9005 * "q:", "q/", "q?": edit command-line in command-line window.
9007 static void
9008 nv_record(cap)
9009 cmdarg_T *cap;
9011 if (cap->oap->op_type == OP_FORMAT)
9013 /* "gqq" is the same as "gqgq": format line */
9014 cap->cmdchar = 'g';
9015 cap->nchar = 'q';
9016 nv_operator(cap);
9018 else if (!checkclearop(cap->oap))
9020 #ifdef FEAT_CMDWIN
9021 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9023 stuffcharReadbuff(cap->nchar);
9024 stuffcharReadbuff(K_CMDWIN);
9026 else
9027 #endif
9028 /* (stop) recording into a named register, unless executing a
9029 * register */
9030 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9031 clearopbeep(cap->oap);
9036 * Handle the "@r" command.
9038 static void
9039 nv_at(cap)
9040 cmdarg_T *cap;
9042 if (checkclearop(cap->oap))
9043 return;
9044 #ifdef FEAT_EVAL
9045 if (cap->nchar == '=')
9047 if (get_expr_register() == NUL)
9048 return;
9050 #endif
9051 while (cap->count1-- && !got_int)
9053 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
9055 clearopbeep(cap->oap);
9056 break;
9058 line_breakcheck();
9063 * Handle the CTRL-U and CTRL-D commands.
9065 static void
9066 nv_halfpage(cap)
9067 cmdarg_T *cap;
9069 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9070 || (cap->cmdchar == Ctrl_D
9071 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9072 clearopbeep(cap->oap);
9073 else if (!checkclearop(cap->oap))
9074 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9078 * Handle "J" or "gJ" command.
9080 static void
9081 nv_join(cap)
9082 cmdarg_T *cap;
9084 #ifdef FEAT_VISUAL
9085 if (VIsual_active) /* join the visual lines */
9086 nv_operator(cap);
9087 else
9088 #endif
9089 if (!checkclearop(cap->oap))
9091 if (cap->count0 <= 1)
9092 cap->count0 = 2; /* default for join is two lines! */
9093 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9094 curbuf->b_ml.ml_line_count)
9095 clearopbeep(cap->oap); /* beyond last line */
9096 else
9098 prep_redo(cap->oap->regname, cap->count0,
9099 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9100 do_do_join(cap->count0, cap->nchar == NUL);
9106 * "P", "gP", "p" and "gp" commands.
9108 static void
9109 nv_put(cap)
9110 cmdarg_T *cap;
9112 #ifdef FEAT_VISUAL
9113 int regname = 0;
9114 void *reg1 = NULL, *reg2 = NULL;
9115 int empty = FALSE;
9116 int was_visual = FALSE;
9117 #endif
9118 int dir;
9119 int flags = 0;
9121 if (cap->oap->op_type != OP_NOP)
9123 #ifdef FEAT_DIFF
9124 /* "dp" is ":diffput" */
9125 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9127 clearop(cap->oap);
9128 nv_diffgetput(TRUE);
9130 else
9131 #endif
9132 clearopbeep(cap->oap);
9134 else
9136 dir = (cap->cmdchar == 'P'
9137 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9138 ? BACKWARD : FORWARD;
9139 prep_redo_cmd(cap);
9140 if (cap->cmdchar == 'g')
9141 flags |= PUT_CURSEND;
9143 #ifdef FEAT_VISUAL
9144 if (VIsual_active)
9146 /* Putting in Visual mode: The put text replaces the selected
9147 * text. First delete the selected text, then put the new text.
9148 * Need to save and restore the registers that the delete
9149 * overwrites if the old contents is being put.
9151 was_visual = TRUE;
9152 regname = cap->oap->regname;
9153 # ifdef FEAT_CLIPBOARD
9154 adjust_clip_reg(&regname);
9155 # endif
9156 if (regname == 0 || VIM_ISDIGIT(regname)
9157 # ifdef FEAT_CLIPBOARD
9158 || (clip_unnamed && (regname == '*' || regname == '+'))
9159 # endif
9163 /* the delete is going to overwrite the register we want to
9164 * put, save it first. */
9165 reg1 = get_register(regname, TRUE);
9168 /* Now delete the selected text. */
9169 cap->cmdchar = 'd';
9170 cap->nchar = NUL;
9171 cap->oap->regname = NUL;
9172 nv_operator(cap);
9173 do_pending_operator(cap, 0, FALSE);
9174 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
9176 /* delete PUT_LINE_BACKWARD; */
9177 cap->oap->regname = regname;
9179 if (reg1 != NULL)
9181 /* Delete probably changed the register we want to put, save
9182 * it first. Then put back what was there before the delete. */
9183 reg2 = get_register(regname, FALSE);
9184 put_register(regname, reg1);
9187 /* When deleted a linewise Visual area, put the register as
9188 * lines to avoid it joined with the next line. When deletion was
9189 * characterwise, split a line when putting lines. */
9190 if (VIsual_mode == 'V')
9191 flags |= PUT_LINE;
9192 else if (VIsual_mode == 'v')
9193 flags |= PUT_LINE_SPLIT;
9194 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9195 flags |= PUT_LINE_FORWARD;
9196 dir = BACKWARD;
9197 if ((VIsual_mode != 'V'
9198 && curwin->w_cursor.col < curbuf->b_op_start.col)
9199 || (VIsual_mode == 'V'
9200 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9201 /* cursor is at the end of the line or end of file, put
9202 * forward. */
9203 dir = FORWARD;
9205 #endif
9206 do_put(cap->oap->regname, dir, cap->count1, flags);
9208 #ifdef FEAT_VISUAL
9209 /* If a register was saved, put it back now. */
9210 if (reg2 != NULL)
9211 put_register(regname, reg2);
9213 /* What to reselect with "gv"? Selecting the just put text seems to
9214 * be the most useful, since the original text was removed. */
9215 if (was_visual)
9217 curbuf->b_visual.vi_start = curbuf->b_op_start;
9218 curbuf->b_visual.vi_end = curbuf->b_op_end;
9221 /* When all lines were selected and deleted do_put() leaves an empty
9222 * line that needs to be deleted now. */
9223 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
9225 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
9227 /* If the cursor was in that line, move it to the end of the last
9228 * line. */
9229 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9231 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9232 coladvance((colnr_T)MAXCOL);
9235 #endif
9236 auto_format(FALSE, TRUE);
9241 * "o" and "O" commands.
9243 static void
9244 nv_open(cap)
9245 cmdarg_T *cap;
9247 #ifdef FEAT_DIFF
9248 /* "do" is ":diffget" */
9249 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9251 clearop(cap->oap);
9252 nv_diffgetput(FALSE);
9254 else
9255 #endif
9256 #ifdef FEAT_VISUAL
9257 if (VIsual_active) /* switch start and end of visual */
9258 v_swap_corners(cap->cmdchar);
9259 else
9260 #endif
9261 n_opencmd(cap);
9264 #ifdef FEAT_SNIFF
9265 /*ARGSUSED*/
9266 static void
9267 nv_sniff(cap)
9268 cmdarg_T *cap;
9270 ProcessSniffRequests();
9272 #endif
9274 #ifdef FEAT_NETBEANS_INTG
9275 static void
9276 nv_nbcmd(cap)
9277 cmdarg_T *cap;
9279 netbeans_keycommand(cap->nchar);
9281 #endif
9283 #ifdef FEAT_DND
9284 /*ARGSUSED*/
9285 static void
9286 nv_drop(cap)
9287 cmdarg_T *cap;
9289 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9291 #endif
9293 #ifdef FEAT_AUTOCMD
9295 * Trigger CursorHold event.
9296 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9297 * input buffer. "did_cursorhold" is set to avoid retriggering.
9299 /*ARGSUSED*/
9300 static void
9301 nv_cursorhold(cap)
9302 cmdarg_T *cap;
9304 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9305 did_cursorhold = TRUE;
9306 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
9308 #endif