Merge branch 'master' of git://repo.or.cz/MacVim into KaoriYa
[MacVim/KaoriYa.git] / src / normal.c
blobca266b0d515d9aaedd69727419039164101d0c94
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 < (int)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 < (int)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 void
565 normal_cmd(oap, toplevel)
566 oparg_T *oap;
567 int toplevel UNUSED; /* TRUE when called from main() */
569 cmdarg_T ca; /* command arguments */
570 int c;
571 int ctrl_w = FALSE; /* got CTRL-W command */
572 int old_col = curwin->w_curswant;
573 #ifdef FEAT_CMDL_INFO
574 int need_flushbuf; /* need to call out_flush() */
575 #endif
576 #ifdef FEAT_VISUAL
577 pos_T old_pos; /* cursor position before command */
578 int mapped_len;
579 static int old_mapped_len = 0;
580 #endif
581 int idx;
582 #ifdef FEAT_EVAL
583 int set_prevcount = FALSE;
584 #endif
586 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
587 ca.oap = oap;
589 /* Use a count remembered from before entering an operator. After typing
590 * "3d" we return from normal_cmd() and come back here, the "3" is
591 * remembered in "opcount". */
592 ca.opcount = opcount;
594 #ifdef FEAT_SNIFF
595 want_sniff_request = sniff_connected;
596 #endif
599 * If there is an operator pending, then the command we take this time
600 * will terminate it. Finish_op tells us to finish the operation before
601 * returning this time (unless the operation was cancelled).
603 #ifdef CURSOR_SHAPE
604 c = finish_op;
605 #endif
606 finish_op = (oap->op_type != OP_NOP);
607 #ifdef CURSOR_SHAPE
608 if (finish_op != c)
610 ui_cursor_shape(); /* may show different cursor shape */
611 # ifdef FEAT_MOUSESHAPE
612 update_mouseshape(-1);
613 # endif
615 #endif
617 /* When not finishing an operator and no register name typed, reset the
618 * count. */
619 if (!finish_op && !oap->regname)
621 ca.opcount = 0;
622 #ifdef FEAT_EVAL
623 set_prevcount = TRUE;
624 #endif
627 #ifdef FEAT_AUTOCMD
628 /* Restore counts from before receiving K_CURSORHOLD. This means after
629 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
630 * "3 * 2". */
631 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
633 ca.opcount = oap->prev_opcount;
634 ca.count0 = oap->prev_count0;
635 oap->prev_opcount = 0;
636 oap->prev_count0 = 0;
638 #endif
640 #ifdef FEAT_VISUAL
641 mapped_len = typebuf_maplen();
642 #endif
644 State = NORMAL_BUSY;
645 #ifdef USE_ON_FLY_SCROLL
646 dont_scroll = FALSE; /* allow scrolling here */
647 #endif
650 * Get the command character from the user.
652 c = safe_vgetc();
653 LANGMAP_ADJUST(c, TRUE);
655 #ifdef FEAT_VISUAL
657 * If a mapping was started in Visual or Select mode, remember the length
658 * of the mapping. This is used below to not return to Insert mode for as
659 * long as the mapping is being executed.
661 if (restart_edit == 0)
662 old_mapped_len = 0;
663 else if (old_mapped_len
664 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
665 old_mapped_len = typebuf_maplen();
666 #endif
668 if (c == NUL)
669 c = K_ZERO;
671 #ifdef FEAT_VISUAL
673 * In Select mode, typed text replaces the selection.
675 if (VIsual_active
676 && VIsual_select
677 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
679 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
680 * 'insertmode' is set) fake a "d"elete command, Insert mode will
681 * restart automatically.
682 * Insert the typed character in the typeahead buffer, so that it can
683 * be mapped in Insert mode. Required for ":lmap" to work. */
684 ins_char_typebuf(c);
685 if (restart_edit != 0)
686 c = 'd';
687 else
688 c = 'c';
689 msg_nowait = TRUE; /* don't delay going to insert mode */
691 #endif
693 #ifdef FEAT_CMDL_INFO
694 need_flushbuf = add_to_showcmd(c);
695 #endif
697 getcount:
698 #ifdef FEAT_VISUAL
699 if (!(VIsual_active && VIsual_select))
700 #endif
703 * Handle a count before a command and compute ca.count0.
704 * Note that '0' is a command and not the start of a count, but it's
705 * part of a count after other digits.
707 while ( (c >= '1' && c <= '9')
708 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
710 if (c == K_DEL || c == K_KDEL)
712 ca.count0 /= 10;
713 #ifdef FEAT_CMDL_INFO
714 del_from_showcmd(4); /* delete the digit and ~@% */
715 #endif
717 else
718 ca.count0 = ca.count0 * 10 + (c - '0');
719 if (ca.count0 < 0) /* got too large! */
720 ca.count0 = 999999999L;
721 #ifdef FEAT_EVAL
722 /* Set v:count here, when called from main() and not a stuffed
723 * command, so that v:count can be used in an expression mapping
724 * right after the count. */
725 if (toplevel && stuff_empty())
727 long count = ca.count0;
729 /* multiply with ca.opcount the same way as below */
730 if (ca.opcount != 0)
731 count = ca.opcount * (count == 0 ? 1 : count);
732 set_vcount(count, count == 0 ? 1 : count, set_prevcount);
733 set_prevcount = FALSE; /* only set v:prevcount once */
735 #endif
736 if (ctrl_w)
738 ++no_mapping;
739 ++allow_keys; /* no mapping for nchar, but keys */
741 ++no_zero_mapping; /* don't map zero here */
742 c = plain_vgetc();
743 LANGMAP_ADJUST(c, TRUE);
744 --no_zero_mapping;
745 if (ctrl_w)
747 --no_mapping;
748 --allow_keys;
750 #ifdef FEAT_CMDL_INFO
751 need_flushbuf |= add_to_showcmd(c);
752 #endif
756 * If we got CTRL-W there may be a/another count
758 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
760 ctrl_w = TRUE;
761 ca.opcount = ca.count0; /* remember first count */
762 ca.count0 = 0;
763 ++no_mapping;
764 ++allow_keys; /* no mapping for nchar, but keys */
765 c = plain_vgetc(); /* get next character */
766 LANGMAP_ADJUST(c, TRUE);
767 --no_mapping;
768 --allow_keys;
769 #ifdef FEAT_CMDL_INFO
770 need_flushbuf |= add_to_showcmd(c);
771 #endif
772 goto getcount; /* jump back */
776 #ifdef FEAT_AUTOCMD
777 if (c == K_CURSORHOLD)
779 /* Save the count values so that ca.opcount and ca.count0 are exactly
780 * the same when coming back here after handling K_CURSORHOLD. */
781 oap->prev_opcount = ca.opcount;
782 oap->prev_count0 = ca.count0;
784 else
785 #endif
786 if (ca.opcount != 0)
789 * If we're in the middle of an operator (including after entering a
790 * yank buffer with '"') AND we had a count before the operator, then
791 * that count overrides the current value of ca.count0.
792 * What this means effectively, is that commands like "3dw" get turned
793 * into "d3w" which makes things fall into place pretty neatly.
794 * If you give a count before AND after the operator, they are
795 * multiplied.
797 if (ca.count0)
798 ca.count0 *= ca.opcount;
799 else
800 ca.count0 = ca.opcount;
804 * Always remember the count. It will be set to zero (on the next call,
805 * above) when there is no pending operator.
806 * When called from main(), save the count for use by the "count" built-in
807 * variable.
809 ca.opcount = ca.count0;
810 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
812 #ifdef FEAT_EVAL
814 * Only set v:count when called from main() and not a stuffed command.
816 if (toplevel && stuff_empty())
817 set_vcount(ca.count0, ca.count1, set_prevcount);
818 #endif
821 * Find the command character in the table of commands.
822 * For CTRL-W we already got nchar when looking for a count.
824 if (ctrl_w)
826 ca.nchar = c;
827 ca.cmdchar = Ctrl_W;
829 else
830 ca.cmdchar = c;
831 idx = find_command(ca.cmdchar);
832 if (idx < 0)
834 /* Not a known command: beep. */
835 clearopbeep(oap);
836 goto normal_end;
839 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
841 /* This command is not allowed while editing a ccmdline: beep. */
842 clearopbeep(oap);
843 text_locked_msg();
844 goto normal_end;
846 #ifdef FEAT_AUTOCMD
847 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
848 goto normal_end;
849 #endif
851 #ifdef FEAT_VISUAL
853 * In Visual/Select mode, a few keys are handled in a special way.
855 if (VIsual_active)
857 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
858 if (km_stopsel
859 && (nv_cmds[idx].cmd_flags & NV_STS)
860 && !(mod_mask & MOD_MASK_SHIFT))
862 end_visual_mode();
863 redraw_curbuf_later(INVERTED);
866 /* Keys that work different when 'keymodel' contains "startsel" */
867 if (km_startsel)
869 if (nv_cmds[idx].cmd_flags & NV_SS)
871 unshift_special(&ca);
872 idx = find_command(ca.cmdchar);
873 if (idx < 0)
875 /* Just in case */
876 clearopbeep(oap);
877 goto normal_end;
880 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
881 && (mod_mask & MOD_MASK_SHIFT))
883 mod_mask &= ~MOD_MASK_SHIFT;
887 #endif
889 #ifdef FEAT_RIGHTLEFT
890 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
891 && (nv_cmds[idx].cmd_flags & NV_RL))
893 /* Invert horizontal movements and operations. Only when typed by the
894 * user directly, not when the result of a mapping or "x" translated
895 * to "dl". */
896 switch (ca.cmdchar)
898 case 'l': ca.cmdchar = 'h'; break;
899 case K_RIGHT: ca.cmdchar = K_LEFT; break;
900 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
901 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
902 case 'h': ca.cmdchar = 'l'; break;
903 case K_LEFT: ca.cmdchar = K_RIGHT; break;
904 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
905 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
906 case '>': ca.cmdchar = '<'; break;
907 case '<': ca.cmdchar = '>'; break;
909 idx = find_command(ca.cmdchar);
911 #endif
914 * Get an additional character if we need one.
916 if ((nv_cmds[idx].cmd_flags & NV_NCH)
917 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
918 && oap->op_type == OP_NOP)
919 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
920 || (ca.cmdchar == 'q'
921 && oap->op_type == OP_NOP
922 && !Recording
923 && !Exec_reg)
924 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
925 && (oap->op_type != OP_NOP
926 #ifdef FEAT_VISUAL
927 || VIsual_active
928 #endif
929 ))))
931 int *cp;
932 int repl = FALSE; /* get character for replace mode */
933 int lit = FALSE; /* get extra character literally */
934 int langmap_active = FALSE; /* using :lmap mappings */
935 int lang; /* getting a text character */
936 #ifdef USE_IM_CONTROL
937 int save_smd; /* saved value of p_smd */
938 #endif
940 ++no_mapping;
941 ++allow_keys; /* no mapping for nchar, but allow key codes */
942 #ifdef FEAT_AUTOCMD
943 /* Don't generate a CursorHold event here, most commands can't handle
944 * it, e.g., nv_replace(), nv_csearch(). */
945 did_cursorhold = TRUE;
946 #endif
947 if (ca.cmdchar == 'g')
950 * For 'g' get the next character now, so that we can check for
951 * "gr", "g'" and "g`".
953 ca.nchar = plain_vgetc();
954 LANGMAP_ADJUST(ca.nchar, TRUE);
955 #ifdef FEAT_CMDL_INFO
956 need_flushbuf |= add_to_showcmd(ca.nchar);
957 #endif
958 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
959 || ca.nchar == Ctrl_BSL)
961 cp = &ca.extra_char; /* need to get a third character */
962 if (ca.nchar != 'r')
963 lit = TRUE; /* get it literally */
964 else
965 repl = TRUE; /* get it in replace mode */
967 else
968 cp = NULL; /* no third character needed */
970 else
972 if (ca.cmdchar == 'r') /* get it in replace mode */
973 repl = TRUE;
974 cp = &ca.nchar;
976 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
979 * Get a second or third character.
981 if (cp != NULL)
983 #ifdef CURSOR_SHAPE
984 if (repl)
986 State = REPLACE; /* pretend Replace mode */
987 ui_cursor_shape(); /* show different cursor shape */
989 #endif
990 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
992 /* Allow mappings defined with ":lmap". */
993 --no_mapping;
994 --allow_keys;
995 if (repl)
996 State = LREPLACE;
997 else
998 State = LANGMAP;
999 langmap_active = TRUE;
1001 #ifdef USE_IM_CONTROL
1002 save_smd = p_smd;
1003 p_smd = FALSE; /* Don't let the IM code show the mode here */
1004 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
1005 im_set_active(TRUE);
1006 #endif
1008 *cp = plain_vgetc();
1010 if (langmap_active)
1012 /* Undo the decrement done above */
1013 ++no_mapping;
1014 ++allow_keys;
1015 State = NORMAL_BUSY;
1017 #ifdef USE_IM_CONTROL
1018 if (lang)
1020 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
1021 im_save_status(&curbuf->b_p_iminsert);
1022 im_set_active(FALSE);
1024 p_smd = save_smd;
1025 #endif
1026 #ifdef CURSOR_SHAPE
1027 State = NORMAL_BUSY;
1028 #endif
1029 #ifdef FEAT_CMDL_INFO
1030 need_flushbuf |= add_to_showcmd(*cp);
1031 #endif
1033 if (!lit)
1035 #ifdef FEAT_DIGRAPHS
1036 /* Typing CTRL-K gets a digraph. */
1037 if (*cp == Ctrl_K
1038 && ((nv_cmds[idx].cmd_flags & NV_LANG)
1039 || cp == &ca.extra_char)
1040 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1042 c = get_digraph(FALSE);
1043 if (c > 0)
1045 *cp = c;
1046 # ifdef FEAT_CMDL_INFO
1047 /* Guessing how to update showcmd here... */
1048 del_from_showcmd(3);
1049 need_flushbuf |= add_to_showcmd(*cp);
1050 # endif
1053 #endif
1055 /* adjust chars > 127, except after "tTfFr" commands */
1056 LANGMAP_ADJUST(*cp, !lang);
1057 #ifdef FEAT_RIGHTLEFT
1058 /* adjust Hebrew mapped char */
1059 if (p_hkmap && lang && KeyTyped)
1060 *cp = hkmap(*cp);
1061 # ifdef FEAT_FKMAP
1062 /* adjust Farsi mapped char */
1063 if (p_fkmap && lang && KeyTyped)
1064 *cp = fkmap(*cp);
1065 # endif
1066 #endif
1070 * When the next character is CTRL-\ a following CTRL-N means the
1071 * command is aborted and we go to Normal mode.
1073 if (cp == &ca.extra_char
1074 && ca.nchar == Ctrl_BSL
1075 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1077 ca.cmdchar = Ctrl_BSL;
1078 ca.nchar = ca.extra_char;
1079 idx = find_command(ca.cmdchar);
1081 else if (*cp == Ctrl_BSL)
1083 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1085 /* There is a busy wait here when typing "f<C-\>" and then
1086 * something different from CTRL-N. Can't be avoided. */
1087 while ((c = vpeekc()) <= 0 && towait > 0L)
1089 do_sleep(towait > 50L ? 50L : towait);
1090 towait -= 50L;
1092 if (c > 0)
1094 c = plain_vgetc();
1095 if (c != Ctrl_N && c != Ctrl_G)
1096 vungetc(c);
1097 else
1099 ca.cmdchar = Ctrl_BSL;
1100 ca.nchar = c;
1101 idx = find_command(ca.cmdchar);
1106 #ifdef FEAT_MBYTE
1107 /* When getting a text character and the next character is a
1108 * multi-byte character, it could be a composing character.
1109 * However, don't wait for it to arrive. */
1110 while (enc_utf8 && lang && (c = vpeekc()) > 0
1111 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1113 c = plain_vgetc();
1114 if (!utf_iscomposing(c))
1116 vungetc(c); /* it wasn't, put it back */
1117 break;
1119 else if (ca.ncharC1 == 0)
1120 ca.ncharC1 = c;
1121 else
1122 ca.ncharC2 = c;
1124 #endif
1126 --no_mapping;
1127 --allow_keys;
1130 #ifdef FEAT_CMDL_INFO
1132 * Flush the showcmd characters onto the screen so we can see them while
1133 * the command is being executed. Only do this when the shown command was
1134 * actually displayed, otherwise this will slow down a lot when executing
1135 * mappings.
1137 if (need_flushbuf)
1138 out_flush();
1139 #endif
1140 #ifdef FEAT_AUTOCMD
1141 if (ca.cmdchar != K_IGNORE)
1142 did_cursorhold = FALSE;
1143 #endif
1145 State = NORMAL;
1147 if (ca.nchar == ESC)
1149 clearop(oap);
1150 if (restart_edit == 0 && goto_im())
1151 restart_edit = 'a';
1152 goto normal_end;
1155 if (ca.cmdchar != K_IGNORE)
1157 msg_didout = FALSE; /* don't scroll screen up for normal command */
1158 msg_col = 0;
1161 #ifdef FEAT_VISUAL
1162 old_pos = curwin->w_cursor; /* remember where cursor was */
1164 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1165 * mode. */
1166 if (!VIsual_active && km_startsel)
1168 if (nv_cmds[idx].cmd_flags & NV_SS)
1170 start_selection();
1171 unshift_special(&ca);
1172 idx = find_command(ca.cmdchar);
1174 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1175 && (mod_mask & MOD_MASK_SHIFT))
1177 start_selection();
1178 mod_mask &= ~MOD_MASK_SHIFT;
1181 #endif
1184 * Execute the command!
1185 * Call the command function found in the commands table.
1187 ca.arg = nv_cmds[idx].cmd_arg;
1188 (nv_cmds[idx].cmd_func)(&ca);
1191 * If we didn't start or finish an operator, reset oap->regname, unless we
1192 * need it later.
1194 if (!finish_op
1195 && !oap->op_type
1196 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1198 clearop(oap);
1199 #ifdef FEAT_EVAL
1200 set_reg_var('"');
1201 #endif
1204 #ifdef FEAT_VISUAL
1205 /* Get the length of mapped chars again after typing a count, second
1206 * character or "z333<cr>". */
1207 if (old_mapped_len > 0)
1208 old_mapped_len = typebuf_maplen();
1209 #endif
1212 * If an operation is pending, handle it...
1214 do_pending_operator(&ca, old_col, FALSE);
1217 * Wait for a moment when a message is displayed that will be overwritten
1218 * by the mode message.
1219 * In Visual mode and with "^O" in Insert mode, a short message will be
1220 * overwritten by the mode message. Wait a bit, until a key is hit.
1221 * In Visual mode, it's more important to keep the Visual area updated
1222 * than keeping a message (e.g. from a /pat search).
1223 * Only do this if the command was typed, not from a mapping.
1224 * Don't wait when emsg_silent is non-zero.
1225 * Also wait a bit after an error message, e.g. for "^O:".
1226 * Don't redraw the screen, it would remove the message.
1228 if ( ((p_smd
1229 && msg_silent == 0
1230 && (restart_edit != 0
1231 #ifdef FEAT_VISUAL
1232 || (VIsual_active
1233 && old_pos.lnum == curwin->w_cursor.lnum
1234 && old_pos.col == curwin->w_cursor.col)
1235 #endif
1237 && (clear_cmdline
1238 || redraw_cmdline)
1239 && (msg_didout || (msg_didany && msg_scroll))
1240 && !msg_nowait
1241 && KeyTyped)
1242 || (restart_edit != 0
1243 #ifdef FEAT_VISUAL
1244 && !VIsual_active
1245 #endif
1246 && (msg_scroll
1247 || emsg_on_display)))
1248 && oap->regname == 0
1249 && !(ca.retval & CA_COMMAND_BUSY)
1250 && stuff_empty()
1251 && typebuf_typed()
1252 && emsg_silent == 0
1253 && !did_wait_return
1254 && oap->op_type == OP_NOP)
1256 int save_State = State;
1258 /* Draw the cursor with the right shape here */
1259 if (restart_edit != 0)
1260 State = INSERT;
1262 /* If need to redraw, and there is a "keep_msg", redraw before the
1263 * delay */
1264 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1266 char_u *kmsg;
1268 kmsg = keep_msg;
1269 keep_msg = NULL;
1270 /* showmode() will clear keep_msg, but we want to use it anyway */
1271 update_screen(0);
1272 /* now reset it, otherwise it's put in the history again */
1273 keep_msg = kmsg;
1274 msg_attr(kmsg, keep_msg_attr);
1275 vim_free(kmsg);
1277 setcursor();
1278 cursor_on();
1279 out_flush();
1280 if (msg_scroll || emsg_on_display)
1281 ui_delay(1000L, TRUE); /* wait at least one second */
1282 ui_delay(3000L, FALSE); /* wait up to three seconds */
1283 State = save_State;
1285 msg_scroll = FALSE;
1286 emsg_on_display = FALSE;
1290 * Finish up after executing a Normal mode command.
1292 normal_end:
1294 msg_nowait = FALSE;
1296 /* Reset finish_op, in case it was set */
1297 #ifdef CURSOR_SHAPE
1298 c = finish_op;
1299 #endif
1300 finish_op = FALSE;
1301 #ifdef CURSOR_SHAPE
1302 /* Redraw the cursor with another shape, if we were in Operator-pending
1303 * mode or did a replace command. */
1304 if (c || ca.cmdchar == 'r')
1306 ui_cursor_shape(); /* may show different cursor shape */
1307 # ifdef FEAT_MOUSESHAPE
1308 update_mouseshape(-1);
1309 # endif
1311 #endif
1313 #ifdef FEAT_CMDL_INFO
1314 if (oap->op_type == OP_NOP && oap->regname == 0
1315 # ifdef FEAT_AUTOCMD
1316 && ca.cmdchar != K_CURSORHOLD
1317 # endif
1319 clear_showcmd();
1320 #endif
1322 checkpcmark(); /* check if we moved since setting pcmark */
1323 vim_free(ca.searchbuf);
1325 #ifdef FEAT_MBYTE
1326 if (has_mbyte)
1327 mb_adjust_cursor();
1328 #endif
1330 #ifdef FEAT_SCROLLBIND
1331 if (curwin->w_p_scb && toplevel)
1333 validate_cursor(); /* may need to update w_leftcol */
1334 do_check_scrollbind(TRUE);
1336 #endif
1339 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1340 * if still inside a mapping that started in Visual mode).
1341 * May switch from Visual to Select mode after CTRL-O command.
1343 if ( oap->op_type == OP_NOP
1344 #ifdef FEAT_VISUAL
1345 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1346 || restart_VIsual_select == 1)
1347 #else
1348 && restart_edit != 0
1349 #endif
1350 && !(ca.retval & CA_COMMAND_BUSY)
1351 && stuff_empty()
1352 && oap->regname == 0)
1354 #ifdef FEAT_VISUAL
1355 if (restart_VIsual_select == 1)
1357 VIsual_select = TRUE;
1358 showmode();
1359 restart_VIsual_select = 0;
1361 #endif
1362 if (restart_edit != 0
1363 #ifdef FEAT_VISUAL
1364 && !VIsual_active && old_mapped_len == 0
1365 #endif
1367 (void)edit(restart_edit, FALSE, 1L);
1370 #ifdef FEAT_VISUAL
1371 if (restart_VIsual_select == 2)
1372 restart_VIsual_select = 1;
1373 #endif
1375 /* Save count before an operator for next time. */
1376 opcount = ca.opcount;
1380 * Handle an operator after visual mode or when the movement is finished
1382 void
1383 do_pending_operator(cap, old_col, gui_yank)
1384 cmdarg_T *cap;
1385 int old_col;
1386 int gui_yank;
1388 oparg_T *oap = cap->oap;
1389 pos_T old_cursor;
1390 int empty_region_error;
1391 int restart_edit_save;
1393 #ifdef FEAT_VISUAL
1394 /* The visual area is remembered for redo */
1395 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1396 static linenr_T redo_VIsual_line_count; /* number of lines */
1397 static colnr_T redo_VIsual_col; /* number of cols or end column */
1398 static long redo_VIsual_count; /* count for Visual operator */
1399 # ifdef FEAT_VIRTUALEDIT
1400 int include_line_break = FALSE;
1401 # endif
1402 #endif
1404 #if defined(FEAT_CLIPBOARD)
1406 * Yank the visual area into the GUI selection register before we operate
1407 * on it and lose it forever.
1408 * Don't do it if a specific register was specified, so that ""x"*P works.
1409 * This could call do_pending_operator() recursively, but that's OK
1410 * because gui_yank will be TRUE for the nested call.
1412 if (clip_star.available
1413 && oap->op_type != OP_NOP
1414 && !gui_yank
1415 # ifdef FEAT_VISUAL
1416 && VIsual_active
1417 && !redo_VIsual_busy
1418 # endif
1419 && oap->regname == 0)
1420 clip_auto_select();
1421 #endif
1422 old_cursor = curwin->w_cursor;
1425 * If an operation is pending, handle it...
1427 if ((finish_op
1428 #ifdef FEAT_VISUAL
1429 || VIsual_active
1430 #endif
1431 ) && oap->op_type != OP_NOP)
1433 #ifdef FEAT_VISUAL
1434 oap->is_VIsual = VIsual_active;
1435 if (oap->motion_force == 'V')
1436 oap->motion_type = MLINE;
1437 else if (oap->motion_force == 'v')
1439 /* If the motion was linewise, "inclusive" will not have been set.
1440 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1441 if (oap->motion_type == MLINE)
1442 oap->inclusive = FALSE;
1443 /* If the motion already was characterwise, toggle "inclusive" */
1444 else if (oap->motion_type == MCHAR)
1445 oap->inclusive = !oap->inclusive;
1446 oap->motion_type = MCHAR;
1448 else if (oap->motion_force == Ctrl_V)
1450 /* Change line- or characterwise motion into Visual block mode. */
1451 VIsual_active = TRUE;
1452 VIsual = oap->start;
1453 VIsual_mode = Ctrl_V;
1454 VIsual_select = FALSE;
1455 VIsual_reselect = FALSE;
1457 #endif
1459 /* only redo yank when 'y' flag is in 'cpoptions' */
1460 /* never redo "zf" (define fold) */
1461 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1462 #ifdef FEAT_VISUAL
1463 && (!VIsual_active || oap->motion_force)
1464 #endif
1465 && cap->cmdchar != 'D'
1466 #ifdef FEAT_FOLDING
1467 && oap->op_type != OP_FOLD
1468 && oap->op_type != OP_FOLDOPEN
1469 && oap->op_type != OP_FOLDOPENREC
1470 && oap->op_type != OP_FOLDCLOSE
1471 && oap->op_type != OP_FOLDCLOSEREC
1472 && oap->op_type != OP_FOLDDEL
1473 && oap->op_type != OP_FOLDDELREC
1474 #endif
1477 prep_redo(oap->regname, cap->count0,
1478 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1479 oap->motion_force, cap->cmdchar, cap->nchar);
1480 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1483 * If 'cpoptions' does not contain 'r', insert the search
1484 * pattern to really repeat the same command.
1486 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
1487 AppendToRedobuffLit(cap->searchbuf, -1);
1488 AppendToRedobuff(NL_STR);
1490 else if (cap->cmdchar == ':')
1492 /* do_cmdline() has stored the first typed line in
1493 * "repeat_cmdline". When several lines are typed repeating
1494 * won't be possible. */
1495 if (repeat_cmdline == NULL)
1496 ResetRedobuff();
1497 else
1499 AppendToRedobuffLit(repeat_cmdline, -1);
1500 AppendToRedobuff(NL_STR);
1501 vim_free(repeat_cmdline);
1502 repeat_cmdline = NULL;
1507 #ifdef FEAT_VISUAL
1508 if (redo_VIsual_busy)
1510 oap->start = curwin->w_cursor;
1511 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1512 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1513 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1514 VIsual_mode = redo_VIsual_mode;
1515 if (VIsual_mode == 'v')
1517 if (redo_VIsual_line_count <= 1)
1518 curwin->w_cursor.col += redo_VIsual_col - 1;
1519 else
1520 curwin->w_cursor.col = redo_VIsual_col;
1522 if (redo_VIsual_col == MAXCOL)
1524 curwin->w_curswant = MAXCOL;
1525 coladvance((colnr_T)MAXCOL);
1527 cap->count0 = redo_VIsual_count;
1528 if (redo_VIsual_count != 0)
1529 cap->count1 = redo_VIsual_count;
1530 else
1531 cap->count1 = 1;
1533 else if (VIsual_active)
1535 if (!gui_yank)
1537 /* Save the current VIsual area for '< and '> marks, and "gv" */
1538 curbuf->b_visual.vi_start = VIsual;
1539 curbuf->b_visual.vi_end = curwin->w_cursor;
1540 curbuf->b_visual.vi_mode = VIsual_mode;
1541 curbuf->b_visual.vi_curswant = curwin->w_curswant;
1542 # ifdef FEAT_EVAL
1543 curbuf->b_visual_mode_eval = VIsual_mode;
1544 # endif
1547 /* In Select mode, a linewise selection is operated upon like a
1548 * characterwise selection. */
1549 if (VIsual_select && VIsual_mode == 'V')
1551 if (lt(VIsual, curwin->w_cursor))
1553 VIsual.col = 0;
1554 curwin->w_cursor.col =
1555 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1557 else
1559 curwin->w_cursor.col = 0;
1560 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1562 VIsual_mode = 'v';
1564 /* If 'selection' is "exclusive", backup one character for
1565 * charwise selections. */
1566 else if (VIsual_mode == 'v')
1568 # ifdef FEAT_VIRTUALEDIT
1569 include_line_break =
1570 # endif
1571 unadjust_for_sel();
1574 oap->start = VIsual;
1575 if (VIsual_mode == 'V')
1576 oap->start.col = 0;
1578 #endif /* FEAT_VISUAL */
1581 * Set oap->start to the first position of the operated text, oap->end
1582 * to the end of the operated text. w_cursor is equal to oap->start.
1584 if (lt(oap->start, curwin->w_cursor))
1586 #ifdef FEAT_FOLDING
1587 /* Include folded lines completely. */
1588 if (!VIsual_active)
1590 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1591 oap->start.col = 0;
1592 if (hasFolding(curwin->w_cursor.lnum, NULL,
1593 &curwin->w_cursor.lnum))
1594 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1596 #endif
1597 oap->end = curwin->w_cursor;
1598 curwin->w_cursor = oap->start;
1600 /* w_virtcol may have been updated; if the cursor goes back to its
1601 * previous position w_virtcol becomes invalid and isn't updated
1602 * automatically. */
1603 curwin->w_valid &= ~VALID_VIRTCOL;
1605 else
1607 #ifdef FEAT_FOLDING
1608 /* Include folded lines completely. */
1609 if (!VIsual_active && oap->motion_type == MLINE)
1611 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1612 NULL))
1613 curwin->w_cursor.col = 0;
1614 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1615 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1617 #endif
1618 oap->end = oap->start;
1619 oap->start = curwin->w_cursor;
1622 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1624 #ifdef FEAT_VIRTUALEDIT
1625 /* Set "virtual_op" before resetting VIsual_active. */
1626 virtual_op = virtual_active();
1627 #endif
1629 #ifdef FEAT_VISUAL
1630 if (VIsual_active || redo_VIsual_busy)
1632 if (VIsual_mode == Ctrl_V) /* block mode */
1634 colnr_T start, end;
1636 oap->block_mode = TRUE;
1638 getvvcol(curwin, &(oap->start),
1639 &oap->start_vcol, NULL, &oap->end_vcol);
1640 if (!redo_VIsual_busy)
1642 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1644 if (start < oap->start_vcol)
1645 oap->start_vcol = start;
1646 if (end > oap->end_vcol)
1648 if (*p_sel == 'e' && start >= 1
1649 && start - 1 >= oap->end_vcol)
1650 oap->end_vcol = start - 1;
1651 else
1652 oap->end_vcol = end;
1656 /* if '$' was used, get oap->end_vcol from longest line */
1657 if (curwin->w_curswant == MAXCOL)
1659 curwin->w_cursor.col = MAXCOL;
1660 oap->end_vcol = 0;
1661 for (curwin->w_cursor.lnum = oap->start.lnum;
1662 curwin->w_cursor.lnum <= oap->end.lnum;
1663 ++curwin->w_cursor.lnum)
1665 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1666 if (end > oap->end_vcol)
1667 oap->end_vcol = end;
1670 else if (redo_VIsual_busy)
1671 oap->end_vcol = oap->start_vcol + redo_VIsual_col - 1;
1673 * Correct oap->end.col and oap->start.col to be the
1674 * upper-left and lower-right corner of the block area.
1676 * (Actually, this does convert column positions into character
1677 * positions)
1679 curwin->w_cursor.lnum = oap->end.lnum;
1680 coladvance(oap->end_vcol);
1681 oap->end = curwin->w_cursor;
1683 curwin->w_cursor = oap->start;
1684 coladvance(oap->start_vcol);
1685 oap->start = curwin->w_cursor;
1688 if (!redo_VIsual_busy && !gui_yank)
1691 * Prepare to reselect and redo Visual: this is based on the
1692 * size of the Visual text
1694 resel_VIsual_mode = VIsual_mode;
1695 if (curwin->w_curswant == MAXCOL)
1696 resel_VIsual_col = MAXCOL;
1697 else if (VIsual_mode == Ctrl_V)
1698 resel_VIsual_col = oap->end_vcol - oap->start_vcol + 1;
1699 else if (oap->line_count > 1)
1700 resel_VIsual_col = oap->end.col;
1701 else
1702 resel_VIsual_col = oap->end.col - oap->start.col + 1;
1703 resel_VIsual_line_count = oap->line_count;
1706 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1707 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1708 && oap->op_type != OP_COLON
1709 #ifdef FEAT_FOLDING
1710 && oap->op_type != OP_FOLD
1711 && oap->op_type != OP_FOLDOPEN
1712 && oap->op_type != OP_FOLDOPENREC
1713 && oap->op_type != OP_FOLDCLOSE
1714 && oap->op_type != OP_FOLDCLOSEREC
1715 && oap->op_type != OP_FOLDDEL
1716 && oap->op_type != OP_FOLDDELREC
1717 #endif
1718 && oap->motion_force == NUL
1721 /* Prepare for redoing. Only use the nchar field for "r",
1722 * otherwise it might be the second char of the operator. */
1723 prep_redo(oap->regname, 0L, NUL, 'v',
1724 get_op_char(oap->op_type),
1725 get_extra_op_char(oap->op_type),
1726 oap->op_type == OP_REPLACE ? cap->nchar : NUL);
1727 if (!redo_VIsual_busy)
1729 redo_VIsual_mode = resel_VIsual_mode;
1730 redo_VIsual_col = resel_VIsual_col;
1731 redo_VIsual_line_count = resel_VIsual_line_count;
1732 redo_VIsual_count = cap->count0;
1737 * oap->inclusive defaults to TRUE.
1738 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1739 * FALSE. This makes "d}P" and "v}dP" work the same.
1741 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1742 oap->inclusive = TRUE;
1743 if (VIsual_mode == 'V')
1744 oap->motion_type = MLINE;
1745 else
1747 oap->motion_type = MCHAR;
1748 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1749 # ifdef FEAT_VIRTUALEDIT
1750 && (include_line_break || !virtual_op)
1751 # endif
1754 oap->inclusive = FALSE;
1755 /* Try to include the newline, unless it's an operator
1756 * that works on lines only */
1757 if (*p_sel != 'o'
1758 && !op_on_lines(oap->op_type)
1759 && oap->end.lnum < curbuf->b_ml.ml_line_count)
1761 ++oap->end.lnum;
1762 oap->end.col = 0;
1763 # ifdef FEAT_VIRTUALEDIT
1764 oap->end.coladd = 0;
1765 # endif
1766 ++oap->line_count;
1771 redo_VIsual_busy = FALSE;
1774 * Switch Visual off now, so screen updating does
1775 * not show inverted text when the screen is redrawn.
1776 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1777 * no screen redraw, so it is done here to remove the inverted
1778 * part.
1780 if (!gui_yank)
1782 VIsual_active = FALSE;
1783 # ifdef FEAT_MOUSE
1784 setmouse();
1785 mouse_dragging = 0;
1786 # endif
1787 if (mode_displayed)
1788 clear_cmdline = TRUE; /* unshow visual mode later */
1789 #ifdef FEAT_CMDL_INFO
1790 else
1791 clear_showcmd();
1792 #endif
1793 if ((oap->op_type == OP_YANK
1794 || oap->op_type == OP_COLON
1795 || oap->op_type == OP_FUNCTION
1796 || oap->op_type == OP_FILTER)
1797 && oap->motion_force == NUL)
1798 redraw_curbuf_later(INVERTED);
1801 #endif
1803 #ifdef FEAT_MBYTE
1804 /* Include the trailing byte of a multi-byte char. */
1805 if (has_mbyte && oap->inclusive)
1807 int l;
1809 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
1810 if (l > 1)
1811 oap->end.col += l - 1;
1813 #endif
1814 curwin->w_set_curswant = TRUE;
1817 * oap->empty is set when start and end are the same. The inclusive
1818 * flag affects this too, unless yanking and the end is on a NUL.
1820 oap->empty = (oap->motion_type == MCHAR
1821 && (!oap->inclusive
1822 || (oap->op_type == OP_YANK
1823 && gchar_pos(&oap->end) == NUL))
1824 && equalpos(oap->start, oap->end)
1825 #ifdef FEAT_VIRTUALEDIT
1826 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1827 #endif
1830 * For delete, change and yank, it's an error to operate on an
1831 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1833 empty_region_error = (oap->empty
1834 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1836 #ifdef FEAT_VISUAL
1837 /* Force a redraw when operating on an empty Visual region, when
1838 * 'modifiable is off or creating a fold. */
1839 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1840 # ifdef FEAT_FOLDING
1841 || oap->op_type == OP_FOLD
1842 # endif
1844 redraw_curbuf_later(INVERTED);
1845 #endif
1848 * If the end of an operator is in column one while oap->motion_type
1849 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1850 * character in the previous line. If op_start is on or before the
1851 * first non-blank in the line, the operator becomes linewise
1852 * (strange, but that's the way vi does it).
1854 if ( oap->motion_type == MCHAR
1855 && oap->inclusive == FALSE
1856 && !(cap->retval & CA_NO_ADJ_OP_END)
1857 && oap->end.col == 0
1858 #ifdef FEAT_VISUAL
1859 && (!oap->is_VIsual || *p_sel == 'o')
1860 && !oap->block_mode
1861 #endif
1862 && oap->line_count > 1)
1864 oap->end_adjusted = TRUE; /* remember that we did this */
1865 --oap->line_count;
1866 --oap->end.lnum;
1867 if (inindent(0))
1868 oap->motion_type = MLINE;
1869 else
1871 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1872 if (oap->end.col)
1874 --oap->end.col;
1875 oap->inclusive = TRUE;
1879 else
1880 oap->end_adjusted = FALSE;
1882 switch (oap->op_type)
1884 case OP_LSHIFT:
1885 case OP_RSHIFT:
1886 op_shift(oap, TRUE,
1887 #ifdef FEAT_VISUAL
1888 oap->is_VIsual ? (int)cap->count1 :
1889 #endif
1891 auto_format(FALSE, TRUE);
1892 break;
1894 case OP_JOIN_NS:
1895 case OP_JOIN:
1896 if (oap->line_count < 2)
1897 oap->line_count = 2;
1898 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1899 curbuf->b_ml.ml_line_count)
1900 beep_flush();
1901 else
1903 do_do_join(oap->line_count, oap->op_type == OP_JOIN);
1904 auto_format(FALSE, TRUE);
1906 break;
1908 case OP_DELETE:
1909 #ifdef FEAT_VISUAL
1910 VIsual_reselect = FALSE; /* don't reselect now */
1911 #endif
1912 if (empty_region_error)
1913 vim_beep();
1914 else
1916 (void)op_delete(oap);
1917 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1918 u_save_cursor(); /* cursor line wasn't saved yet */
1919 auto_format(FALSE, TRUE);
1921 break;
1923 case OP_YANK:
1924 if (empty_region_error)
1926 if (!gui_yank)
1927 vim_beep();
1929 else
1930 (void)op_yank(oap, FALSE, !gui_yank);
1931 check_cursor_col();
1932 break;
1934 case OP_CHANGE:
1935 #ifdef FEAT_VISUAL
1936 VIsual_reselect = FALSE; /* don't reselect now */
1937 #endif
1938 if (empty_region_error)
1939 vim_beep();
1940 else
1942 /* This is a new edit command, not a restart. Need to
1943 * remember it to make 'insertmode' work with mappings for
1944 * Visual mode. But do this only once and not when typed and
1945 * 'insertmode' isn't set. */
1946 if (p_im || !KeyTyped)
1947 restart_edit_save = restart_edit;
1948 else
1949 restart_edit_save = 0;
1950 restart_edit = 0;
1951 /* Reset finish_op now, don't want it set inside edit(). */
1952 finish_op = FALSE;
1953 if (op_change(oap)) /* will call edit() */
1954 cap->retval |= CA_COMMAND_BUSY;
1955 if (restart_edit == 0)
1956 restart_edit = restart_edit_save;
1958 break;
1960 case OP_FILTER:
1961 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1962 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1963 else
1964 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
1966 case OP_INDENT:
1967 case OP_COLON:
1969 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1971 * If 'equalprg' is empty, do the indenting internally.
1973 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1975 # ifdef FEAT_LISP
1976 if (curbuf->b_p_lisp)
1978 op_reindent(oap, get_lisp_indent);
1979 break;
1981 # endif
1982 # ifdef FEAT_CINDENT
1983 op_reindent(oap,
1984 # ifdef FEAT_EVAL
1985 *curbuf->b_p_inde != NUL ? get_expr_indent :
1986 # endif
1987 get_c_indent);
1988 break;
1989 # endif
1991 #endif
1993 op_colon(oap);
1994 break;
1996 case OP_TILDE:
1997 case OP_UPPER:
1998 case OP_LOWER:
1999 case OP_ROT13:
2000 if (empty_region_error)
2001 vim_beep();
2002 else
2003 op_tilde(oap);
2004 check_cursor_col();
2005 break;
2007 case OP_FORMAT:
2008 #if defined(FEAT_EVAL)
2009 if (*curbuf->b_p_fex != NUL)
2010 op_formatexpr(oap); /* use expression */
2011 else
2012 #endif
2013 if (*p_fp != NUL)
2014 op_colon(oap); /* use external command */
2015 else
2016 op_format(oap, FALSE); /* use internal function */
2017 break;
2019 case OP_FORMAT2:
2020 op_format(oap, TRUE); /* use internal function */
2021 break;
2023 case OP_FUNCTION:
2024 op_function(oap); /* call 'operatorfunc' */
2025 break;
2027 case OP_INSERT:
2028 case OP_APPEND:
2029 #ifdef FEAT_VISUAL
2030 VIsual_reselect = FALSE; /* don't reselect now */
2031 #endif
2032 #ifdef FEAT_VISUALEXTRA
2033 if (empty_region_error)
2034 vim_beep();
2035 else
2037 /* This is a new edit command, not a restart. Need to
2038 * remember it to make 'insertmode' work with mappings for
2039 * Visual mode. But do this only once. */
2040 restart_edit_save = restart_edit;
2041 restart_edit = 0;
2043 op_insert(oap, cap->count1);
2045 /* TODO: when inserting in several lines, should format all
2046 * the lines. */
2047 auto_format(FALSE, TRUE);
2049 if (restart_edit == 0)
2050 restart_edit = restart_edit_save;
2052 #else
2053 vim_beep();
2054 #endif
2055 break;
2057 case OP_REPLACE:
2058 #ifdef FEAT_VISUAL
2059 VIsual_reselect = FALSE; /* don't reselect now */
2060 #endif
2061 #ifdef FEAT_VISUALEXTRA
2062 if (empty_region_error)
2063 #endif
2064 vim_beep();
2065 #ifdef FEAT_VISUALEXTRA
2066 else
2067 op_replace(oap, cap->nchar);
2068 #endif
2069 break;
2071 #ifdef FEAT_FOLDING
2072 case OP_FOLD:
2073 VIsual_reselect = FALSE; /* don't reselect now */
2074 foldCreate(oap->start.lnum, oap->end.lnum);
2075 break;
2077 case OP_FOLDOPEN:
2078 case OP_FOLDOPENREC:
2079 case OP_FOLDCLOSE:
2080 case OP_FOLDCLOSEREC:
2081 VIsual_reselect = FALSE; /* don't reselect now */
2082 opFoldRange(oap->start.lnum, oap->end.lnum,
2083 oap->op_type == OP_FOLDOPEN
2084 || oap->op_type == OP_FOLDOPENREC,
2085 oap->op_type == OP_FOLDOPENREC
2086 || oap->op_type == OP_FOLDCLOSEREC,
2087 oap->is_VIsual);
2088 break;
2090 case OP_FOLDDEL:
2091 case OP_FOLDDELREC:
2092 VIsual_reselect = FALSE; /* don't reselect now */
2093 deleteFold(oap->start.lnum, oap->end.lnum,
2094 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2095 break;
2096 #endif
2097 default:
2098 clearopbeep(oap);
2100 #ifdef FEAT_VIRTUALEDIT
2101 virtual_op = MAYBE;
2102 #endif
2103 if (!gui_yank)
2106 * if 'sol' not set, go back to old column for some commands
2108 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2109 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2110 || oap->op_type == OP_DELETE))
2111 coladvance(curwin->w_curswant = old_col);
2113 else
2115 curwin->w_cursor = old_cursor;
2117 #ifdef FEAT_VISUAL
2118 oap->block_mode = FALSE;
2119 #endif
2120 clearop(oap);
2125 * Handle indent and format operators and visual mode ":".
2127 static void
2128 op_colon(oap)
2129 oparg_T *oap;
2131 stuffcharReadbuff(':');
2132 #ifdef FEAT_VISUAL
2133 if (oap->is_VIsual)
2134 stuffReadbuff((char_u *)"'<,'>");
2135 else
2136 #endif
2139 * Make the range look nice, so it can be repeated.
2141 if (oap->start.lnum == curwin->w_cursor.lnum)
2142 stuffcharReadbuff('.');
2143 else
2144 stuffnumReadbuff((long)oap->start.lnum);
2145 if (oap->end.lnum != oap->start.lnum)
2147 stuffcharReadbuff(',');
2148 if (oap->end.lnum == curwin->w_cursor.lnum)
2149 stuffcharReadbuff('.');
2150 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2151 stuffcharReadbuff('$');
2152 else if (oap->start.lnum == curwin->w_cursor.lnum)
2154 stuffReadbuff((char_u *)".+");
2155 stuffnumReadbuff((long)oap->line_count - 1);
2157 else
2158 stuffnumReadbuff((long)oap->end.lnum);
2161 if (oap->op_type != OP_COLON)
2162 stuffReadbuff((char_u *)"!");
2163 if (oap->op_type == OP_INDENT)
2165 #ifndef FEAT_CINDENT
2166 if (*get_equalprg() == NUL)
2167 stuffReadbuff((char_u *)"indent");
2168 else
2169 #endif
2170 stuffReadbuff(get_equalprg());
2171 stuffReadbuff((char_u *)"\n");
2173 else if (oap->op_type == OP_FORMAT)
2175 if (*p_fp == NUL)
2176 stuffReadbuff((char_u *)"fmt");
2177 else
2178 stuffReadbuff(p_fp);
2179 stuffReadbuff((char_u *)"\n']");
2183 * do_cmdline() does the rest
2188 * Handle the "g@" operator: call 'operatorfunc'.
2190 static void
2191 op_function(oap)
2192 oparg_T *oap UNUSED;
2194 #ifdef FEAT_EVAL
2195 char_u *(argv[1]);
2197 if (*p_opfunc == NUL)
2198 EMSG(_("E774: 'operatorfunc' is empty"));
2199 else
2201 /* Set '[ and '] marks to text to be operated on. */
2202 curbuf->b_op_start = oap->start;
2203 curbuf->b_op_end = oap->end;
2204 if (oap->motion_type != MLINE && !oap->inclusive)
2205 /* Exclude the end position. */
2206 decl(&curbuf->b_op_end);
2208 if (oap->block_mode)
2209 argv[0] = (char_u *)"block";
2210 else if (oap->motion_type == MLINE)
2211 argv[0] = (char_u *)"line";
2212 else
2213 argv[0] = (char_u *)"char";
2214 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
2216 #else
2217 EMSG(_("E775: Eval feature not available"));
2218 #endif
2221 #if defined(FEAT_MOUSE) || defined(PROTO)
2223 * Do the appropriate action for the current mouse click in the current mode.
2224 * Not used for Command-line mode.
2226 * Normal Mode:
2227 * event modi- position visual change action
2228 * fier cursor window
2229 * left press - yes end yes
2230 * left press C yes end yes "^]" (2)
2231 * left press S yes end yes "*" (2)
2232 * left drag - yes start if moved no
2233 * left relse - yes start if moved no
2234 * middle press - yes if not active no put register
2235 * middle press - yes if active no yank and put
2236 * right press - yes start or extend yes
2237 * right press S yes no change yes "#" (2)
2238 * right drag - yes extend no
2239 * right relse - yes extend no
2241 * Insert or Replace Mode:
2242 * event modi- position visual change action
2243 * fier cursor window
2244 * left press - yes (cannot be active) yes
2245 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2246 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2247 * left drag - yes start or extend (1) no CTRL-O (1)
2248 * left relse - yes start or extend (1) no CTRL-O (1)
2249 * middle press - no (cannot be active) no put register
2250 * right press - yes start or extend yes CTRL-O
2251 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2253 * (1) only if mouse pointer moved since press
2254 * (2) only if click is in same buffer
2256 * Return TRUE if start_arrow() should be called for edit mode.
2259 do_mouse(oap, c, dir, count, fixindent)
2260 oparg_T *oap; /* operator argument, can be NULL */
2261 int c; /* K_LEFTMOUSE, etc */
2262 int dir; /* Direction to 'put' if necessary */
2263 long count;
2264 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2266 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2267 static int got_click = FALSE; /* got a click some time back */
2269 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2270 int is_click; /* If FALSE it's a drag or release event */
2271 int is_drag; /* If TRUE it's a drag event */
2272 int jump_flags = 0; /* flags for jump_to_mouse() */
2273 pos_T start_visual;
2274 int moved; /* Has cursor moved? */
2275 int in_status_line; /* mouse in status line */
2276 #ifdef FEAT_VERTSPLIT
2277 int in_sep_line; /* mouse in vertical separator line */
2278 #endif
2279 int c1, c2;
2280 #if defined(FEAT_FOLDING)
2281 pos_T save_cursor;
2282 #endif
2283 win_T *old_curwin = curwin;
2284 #ifdef FEAT_VISUAL
2285 static pos_T orig_cursor;
2286 colnr_T leftcol, rightcol;
2287 pos_T end_visual;
2288 int diff;
2289 int old_active = VIsual_active;
2290 int old_mode = VIsual_mode;
2291 #endif
2292 int regname;
2294 #if defined(FEAT_FOLDING)
2295 save_cursor = curwin->w_cursor;
2296 #endif
2299 * When GUI is active, always recognize mouse events, otherwise:
2300 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2301 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2302 * - For command line and insert mode 'mouse' is checked before calling
2303 * do_mouse().
2305 if (do_always)
2306 do_always = FALSE;
2307 else
2308 #ifdef FEAT_GUI
2309 if (!gui.in_use)
2310 #endif
2312 #ifdef FEAT_VISUAL
2313 if (VIsual_active)
2315 if (!mouse_has(MOUSE_VISUAL))
2316 return FALSE;
2318 else
2319 #endif
2320 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2321 return FALSE;
2324 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2326 #ifdef FEAT_MOUSESHAPE
2327 /* May have stopped dragging the status or separator line. The pointer is
2328 * most likely still on the status or separator line. */
2329 if (!is_drag && drag_status_line)
2331 drag_status_line = FALSE;
2332 update_mouseshape(SHAPE_IDX_STATUS);
2334 # ifdef FEAT_VERTSPLIT
2335 if (!is_drag && drag_sep_line)
2337 drag_sep_line = FALSE;
2338 update_mouseshape(SHAPE_IDX_VSEP);
2340 # endif
2341 #endif
2344 * Ignore drag and release events if we didn't get a click.
2346 if (is_click)
2347 got_click = TRUE;
2348 else
2350 if (!got_click) /* didn't get click, ignore */
2351 return FALSE;
2352 if (!is_drag) /* release, reset got_click */
2353 got_click = FALSE;
2356 #ifndef FEAT_VISUAL
2358 * ALT is only used for starging/extending Visual mode.
2360 if ((mod_mask & MOD_MASK_ALT))
2361 return FALSE;
2362 #endif
2365 * CTRL right mouse button does CTRL-T
2367 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2369 if (State & INSERT)
2370 stuffcharReadbuff(Ctrl_O);
2371 if (count > 1)
2372 stuffnumReadbuff(count);
2373 stuffcharReadbuff(Ctrl_T);
2374 got_click = FALSE; /* ignore drag&release now */
2375 return FALSE;
2379 * CTRL only works with left mouse button
2381 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2382 return FALSE;
2385 * When a modifier is down, ignore drag and release events, as well as
2386 * multiple clicks and the middle mouse button.
2387 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2389 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2390 | MOD_MASK_META))
2391 && (!is_click
2392 || (mod_mask & MOD_MASK_MULTI_CLICK)
2393 || which_button == MOUSE_MIDDLE)
2394 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
2395 && mouse_model_popup()
2396 && which_button == MOUSE_LEFT)
2397 && !((mod_mask & MOD_MASK_ALT)
2398 && !mouse_model_popup()
2399 && which_button == MOUSE_RIGHT)
2401 return FALSE;
2404 * If the button press was used as the movement command for an operator
2405 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2406 * drag/release events.
2408 if (!is_click && which_button == MOUSE_MIDDLE)
2409 return FALSE;
2411 if (oap != NULL)
2412 regname = oap->regname;
2413 else
2414 regname = 0;
2417 * Middle mouse button does a 'put' of the selected text
2419 if (which_button == MOUSE_MIDDLE)
2421 if (State == NORMAL)
2424 * If an operator was pending, we don't know what the user wanted
2425 * to do. Go back to normal mode: Clear the operator and beep().
2427 if (oap != NULL && oap->op_type != OP_NOP)
2429 clearopbeep(oap);
2430 return FALSE;
2433 #ifdef FEAT_VISUAL
2435 * If visual was active, yank the highlighted text and put it
2436 * before the mouse pointer position.
2437 * In Select mode replace the highlighted text with the clipboard.
2439 if (VIsual_active)
2441 if (VIsual_select)
2443 stuffcharReadbuff(Ctrl_G);
2444 stuffReadbuff((char_u *)"\"+p");
2446 else
2448 stuffcharReadbuff('y');
2449 stuffcharReadbuff(K_MIDDLEMOUSE);
2451 do_always = TRUE; /* ignore 'mouse' setting next time */
2452 return FALSE;
2454 #endif
2456 * The rest is below jump_to_mouse()
2460 else if ((State & INSERT) == 0)
2461 return FALSE;
2464 * Middle click in insert mode doesn't move the mouse, just insert the
2465 * contents of a register. '.' register is special, can't insert that
2466 * with do_put().
2467 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2468 * happens for the GUI).
2470 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2472 if (regname == '.')
2473 insert_reg(regname, TRUE);
2474 else
2476 #ifdef FEAT_CLIPBOARD
2477 if (clip_star.available && regname == 0)
2478 regname = '*';
2479 #endif
2480 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2481 insert_reg(regname, TRUE);
2482 else
2484 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2486 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2487 AppendCharToRedobuff(Ctrl_R);
2488 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2489 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2492 return FALSE;
2496 /* When dragging or button-up stay in the same window. */
2497 if (!is_click)
2498 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2500 start_visual.lnum = 0;
2502 #ifdef FEAT_WINDOWS
2503 /* Check for clicking in the tab page line. */
2504 if (mouse_row == 0 && firstwin->w_winrow > 0)
2506 if (is_drag)
2507 return FALSE;
2508 got_click = FALSE; /* ignore mouse-up and drag events */
2510 /* click in a tab selects that tab page */
2511 if (is_click
2512 # ifdef FEAT_CMDWIN
2513 && cmdwin_type == 0
2514 # endif
2515 && mouse_col < Columns)
2517 c1 = TabPageIdxs[mouse_col];
2518 if (c1 >= 0)
2520 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2522 /* double click opens new page */
2523 end_visual_mode();
2524 tabpage_new();
2525 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2527 else
2529 /* Go to specified tab page, or next one if not clicking
2530 * on a label. */
2531 goto_tabpage(c1);
2533 /* It's like clicking on the status line of a window. */
2534 if (curwin != old_curwin)
2535 end_visual_mode();
2538 else if (c1 < 0)
2540 tabpage_T *tp;
2542 /* Close the current or specified tab page. */
2543 if (c1 == -999)
2544 tp = curtab;
2545 else
2546 tp = find_tabpage(-c1);
2547 if (tp == curtab)
2549 if (first_tabpage->tp_next != NULL)
2550 tabpage_close(FALSE);
2552 else if (tp != NULL)
2553 tabpage_close_other(tp, FALSE);
2556 return TRUE;
2558 #endif
2561 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2562 * right button up -> pop-up menu
2563 * shift-left button -> right button
2564 * alt-left button -> alt-right button
2566 if (mouse_model_popup())
2568 if (which_button == MOUSE_RIGHT
2569 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2572 * NOTE: Ignore right button down and drag mouse events.
2573 * Windows only shows the popup menu on the button up event.
2575 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2576 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) \
2577 || defined(FEAT_GUI_MACVIM)
2578 if (!is_click)
2579 return FALSE;
2580 #endif
2581 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2582 if (is_click || is_drag)
2583 return FALSE;
2584 #endif
2585 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2586 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2587 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON) \
2588 || defined(FEAT_GUI_MACVIM)
2589 if (gui.in_use)
2591 jump_flags = 0;
2592 if (STRCMP(p_mousem, "popup_setpos") == 0)
2594 /* First set the cursor position before showing the popup
2595 * menu. */
2596 #ifdef FEAT_VISUAL
2597 if (VIsual_active)
2599 pos_T m_pos;
2602 * set MOUSE_MAY_STOP_VIS if we are outside the
2603 * selection or the current window (might have false
2604 * negative here)
2606 if (mouse_row < W_WINROW(curwin)
2607 || mouse_row
2608 > (W_WINROW(curwin) + curwin->w_height))
2609 jump_flags = MOUSE_MAY_STOP_VIS;
2610 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2611 jump_flags = MOUSE_MAY_STOP_VIS;
2612 else
2614 if ((lt(curwin->w_cursor, VIsual)
2615 && (lt(m_pos, curwin->w_cursor)
2616 || lt(VIsual, m_pos)))
2617 || (lt(VIsual, curwin->w_cursor)
2618 && (lt(m_pos, VIsual)
2619 || lt(curwin->w_cursor, m_pos))))
2621 jump_flags = MOUSE_MAY_STOP_VIS;
2623 else if (VIsual_mode == Ctrl_V)
2625 getvcols(curwin, &curwin->w_cursor, &VIsual,
2626 &leftcol, &rightcol);
2627 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2628 if (m_pos.col < leftcol || m_pos.col > rightcol)
2629 jump_flags = MOUSE_MAY_STOP_VIS;
2633 else
2634 jump_flags = MOUSE_MAY_STOP_VIS;
2635 #endif
2637 if (jump_flags)
2639 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2640 update_curbuf(
2641 #ifdef FEAT_VISUAL
2642 VIsual_active ? INVERTED :
2643 #endif
2644 VALID);
2645 setcursor();
2646 out_flush(); /* Update before showing popup menu */
2648 # ifdef FEAT_MENU
2649 gui_show_popupmenu();
2650 # endif
2651 return (jump_flags & CURSOR_MOVED) != 0;
2653 else
2654 return FALSE;
2655 #else
2656 return FALSE;
2657 #endif
2659 if (which_button == MOUSE_LEFT
2660 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
2662 which_button = MOUSE_RIGHT;
2663 mod_mask &= ~MOD_MASK_SHIFT;
2667 #ifdef FEAT_VISUAL
2668 if ((State & (NORMAL | INSERT))
2669 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2671 if (which_button == MOUSE_LEFT)
2673 if (is_click)
2675 /* stop Visual mode for a left click in a window, but not when
2676 * on a status line */
2677 if (VIsual_active)
2678 jump_flags |= MOUSE_MAY_STOP_VIS;
2680 else if (mouse_has(MOUSE_VISUAL))
2681 jump_flags |= MOUSE_MAY_VIS;
2683 else if (which_button == MOUSE_RIGHT)
2685 if (is_click && VIsual_active)
2688 * Remember the start and end of visual before moving the
2689 * cursor.
2691 if (lt(curwin->w_cursor, VIsual))
2693 start_visual = curwin->w_cursor;
2694 end_visual = VIsual;
2696 else
2698 start_visual = VIsual;
2699 end_visual = curwin->w_cursor;
2702 jump_flags |= MOUSE_FOCUS;
2703 if (mouse_has(MOUSE_VISUAL))
2704 jump_flags |= MOUSE_MAY_VIS;
2707 #endif
2710 * If an operator is pending, ignore all drags and releases until the
2711 * next mouse click.
2713 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2715 got_click = FALSE;
2716 oap->motion_type = MCHAR;
2719 /* When releasing the button let jump_to_mouse() know. */
2720 if (!is_click && !is_drag)
2721 jump_flags |= MOUSE_RELEASED;
2724 * JUMP!
2726 jump_flags = jump_to_mouse(jump_flags,
2727 oap == NULL ? NULL : &(oap->inclusive), which_button);
2728 moved = (jump_flags & CURSOR_MOVED);
2729 in_status_line = (jump_flags & IN_STATUS_LINE);
2730 #ifdef FEAT_VERTSPLIT
2731 in_sep_line = (jump_flags & IN_SEP_LINE);
2732 #endif
2734 #ifdef FEAT_NETBEANS_INTG
2735 if (usingNetbeans && isNetbeansBuffer(curbuf)
2736 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2738 int key = KEY2TERMCAP1(c);
2740 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2741 || key == (int)KE_RIGHTRELEASE)
2742 netbeans_button_release(which_button);
2744 #endif
2746 /* When jumping to another window, clear a pending operator. That's a bit
2747 * friendlier than beeping and not jumping to that window. */
2748 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2749 clearop(oap);
2751 #ifdef FEAT_FOLDING
2752 if (mod_mask == 0
2753 && !is_drag
2754 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2755 && which_button == MOUSE_LEFT)
2757 /* open or close a fold at this line */
2758 if (jump_flags & MOUSE_FOLD_OPEN)
2759 openFold(curwin->w_cursor.lnum, 1L);
2760 else
2761 closeFold(curwin->w_cursor.lnum, 1L);
2762 /* don't move the cursor if still in the same window */
2763 if (curwin == old_curwin)
2764 curwin->w_cursor = save_cursor;
2766 #endif
2768 #if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2769 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2771 clip_modeless(which_button, is_click, is_drag);
2772 return FALSE;
2774 #endif
2776 #ifdef FEAT_VISUAL
2777 /* Set global flag that we are extending the Visual area with mouse
2778 * dragging; temporarily minimize 'scrolloff'. */
2779 if (VIsual_active && is_drag && p_so)
2781 /* In the very first line, allow scrolling one line */
2782 if (mouse_row == 0)
2783 mouse_dragging = 2;
2784 else
2785 mouse_dragging = 1;
2788 /* When dragging the mouse above the window, scroll down. */
2789 if (is_drag && mouse_row < 0 && !in_status_line)
2791 scroll_redraw(FALSE, 1L);
2792 mouse_row = 0;
2795 if (start_visual.lnum) /* right click in visual mode */
2797 /* When ALT is pressed make Visual mode blockwise. */
2798 if (mod_mask & MOD_MASK_ALT)
2799 VIsual_mode = Ctrl_V;
2802 * In Visual-block mode, divide the area in four, pick up the corner
2803 * that is in the quarter that the cursor is in.
2805 if (VIsual_mode == Ctrl_V)
2807 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2808 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2809 end_visual.col = leftcol;
2810 else
2811 end_visual.col = rightcol;
2812 if (curwin->w_cursor.lnum <
2813 (start_visual.lnum + end_visual.lnum) / 2)
2814 end_visual.lnum = end_visual.lnum;
2815 else
2816 end_visual.lnum = start_visual.lnum;
2818 /* move VIsual to the right column */
2819 start_visual = curwin->w_cursor; /* save the cursor pos */
2820 curwin->w_cursor = end_visual;
2821 coladvance(end_visual.col);
2822 VIsual = curwin->w_cursor;
2823 curwin->w_cursor = start_visual; /* restore the cursor */
2825 else
2828 * If the click is before the start of visual, change the start.
2829 * If the click is after the end of visual, change the end. If
2830 * the click is inside the visual, change the closest side.
2832 if (lt(curwin->w_cursor, start_visual))
2833 VIsual = end_visual;
2834 else if (lt(end_visual, curwin->w_cursor))
2835 VIsual = start_visual;
2836 else
2838 /* In the same line, compare column number */
2839 if (end_visual.lnum == start_visual.lnum)
2841 if (curwin->w_cursor.col - start_visual.col >
2842 end_visual.col - curwin->w_cursor.col)
2843 VIsual = start_visual;
2844 else
2845 VIsual = end_visual;
2848 /* In different lines, compare line number */
2849 else
2851 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2852 (end_visual.lnum - curwin->w_cursor.lnum);
2854 if (diff > 0) /* closest to end */
2855 VIsual = start_visual;
2856 else if (diff < 0) /* closest to start */
2857 VIsual = end_visual;
2858 else /* in the middle line */
2860 if (curwin->w_cursor.col <
2861 (start_visual.col + end_visual.col) / 2)
2862 VIsual = end_visual;
2863 else
2864 VIsual = start_visual;
2871 * If Visual mode started in insert mode, execute "CTRL-O"
2873 else if ((State & INSERT) && VIsual_active)
2874 stuffcharReadbuff(Ctrl_O);
2875 #endif
2878 * Middle mouse click: Put text before cursor.
2880 if (which_button == MOUSE_MIDDLE)
2882 #ifdef FEAT_CLIPBOARD
2883 if (clip_star.available && regname == 0)
2884 regname = '*';
2885 #endif
2886 if (yank_register_mline(regname))
2888 if (mouse_past_bottom)
2889 dir = FORWARD;
2891 else if (mouse_past_eol)
2892 dir = FORWARD;
2894 if (fixindent)
2896 c1 = (dir == BACKWARD) ? '[' : ']';
2897 c2 = 'p';
2899 else
2901 c1 = (dir == FORWARD) ? 'p' : 'P';
2902 c2 = NUL;
2904 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2907 * Remember where the paste started, so in edit() Insstart can be set
2908 * to this position
2910 if (restart_edit != 0)
2911 where_paste_started = curwin->w_cursor;
2912 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2915 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2917 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2918 * error under the mouse pointer.
2920 else if (((mod_mask & MOD_MASK_CTRL)
2921 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2922 && bt_quickfix(curbuf))
2924 if (State & INSERT)
2925 stuffcharReadbuff(Ctrl_O);
2926 if (curwin->w_llist_ref == NULL) /* quickfix window */
2927 stuffReadbuff((char_u *)":.cc\n");
2928 else /* location list window */
2929 stuffReadbuff((char_u *)":.ll\n");
2930 got_click = FALSE; /* ignore drag&release now */
2932 #endif
2935 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2936 * under the mouse pointer.
2938 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2939 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2941 if (State & INSERT)
2942 stuffcharReadbuff(Ctrl_O);
2943 stuffcharReadbuff(Ctrl_RSB);
2944 got_click = FALSE; /* ignore drag&release now */
2948 * Shift-Mouse click searches for the next occurrence of the word under
2949 * the mouse pointer
2951 else if ((mod_mask & MOD_MASK_SHIFT))
2953 if (State & INSERT
2954 #ifdef FEAT_VISUAL
2955 || (VIsual_active && VIsual_select)
2956 #endif
2958 stuffcharReadbuff(Ctrl_O);
2959 if (which_button == MOUSE_LEFT)
2960 stuffcharReadbuff('*');
2961 else /* MOUSE_RIGHT */
2962 stuffcharReadbuff('#');
2965 /* Handle double clicks, unless on status line */
2966 else if (in_status_line)
2968 #ifdef FEAT_MOUSESHAPE
2969 if ((is_drag || is_click) && !drag_status_line)
2971 drag_status_line = TRUE;
2972 update_mouseshape(-1);
2974 #endif
2976 #ifdef FEAT_VERTSPLIT
2977 else if (in_sep_line)
2979 # ifdef FEAT_MOUSESHAPE
2980 if ((is_drag || is_click) && !drag_sep_line)
2982 drag_sep_line = TRUE;
2983 update_mouseshape(-1);
2985 # endif
2987 #endif
2988 #ifdef FEAT_VISUAL
2989 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
2990 && mouse_has(MOUSE_VISUAL))
2992 if (is_click || !VIsual_active)
2994 if (VIsual_active)
2995 orig_cursor = VIsual;
2996 else
2998 check_visual_highlight();
2999 VIsual = curwin->w_cursor;
3000 orig_cursor = VIsual;
3001 VIsual_active = TRUE;
3002 VIsual_reselect = TRUE;
3003 /* start Select mode if 'selectmode' contains "mouse" */
3004 may_start_select('o');
3005 setmouse();
3007 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3009 /* Double click with ALT pressed makes it blockwise. */
3010 if (mod_mask & MOD_MASK_ALT)
3011 VIsual_mode = Ctrl_V;
3012 else
3013 VIsual_mode = 'v';
3015 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3016 VIsual_mode = 'V';
3017 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3018 VIsual_mode = Ctrl_V;
3019 #ifdef FEAT_CLIPBOARD
3020 /* Make sure the clipboard gets updated. Needed because start and
3021 * end may still be the same, and the selection needs to be owned */
3022 clip_star.vmode = NUL;
3023 #endif
3026 * A double click selects a word or a block.
3028 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3030 pos_T *pos = NULL;
3031 int gc;
3033 if (is_click)
3035 /* If the character under the cursor (skipping white space) is
3036 * not a word character, try finding a match and select a (),
3037 * {}, [], #if/#endif, etc. block. */
3038 end_visual = curwin->w_cursor;
3039 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
3040 inc(&end_visual);
3041 if (oap != NULL)
3042 oap->motion_type = MCHAR;
3043 if (oap != NULL
3044 && VIsual_mode == 'v'
3045 && !vim_iswordc(gchar_pos(&end_visual))
3046 && equalpos(curwin->w_cursor, VIsual)
3047 && (pos = findmatch(oap, NUL)) != NULL)
3049 curwin->w_cursor = *pos;
3050 if (oap->motion_type == MLINE)
3051 VIsual_mode = 'V';
3052 else if (*p_sel == 'e')
3054 if (lt(curwin->w_cursor, VIsual))
3055 ++VIsual.col;
3056 else
3057 ++curwin->w_cursor.col;
3062 if (pos == NULL && (is_click || is_drag))
3064 /* When not found a match or when dragging: extend to include
3065 * a word. */
3066 if (lt(curwin->w_cursor, orig_cursor))
3068 find_start_of_word(&curwin->w_cursor);
3069 find_end_of_word(&VIsual);
3071 else
3073 find_start_of_word(&VIsual);
3074 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3075 #ifdef FEAT_MBYTE
3076 curwin->w_cursor.col +=
3077 (*mb_ptr2len)(ml_get_cursor());
3078 #else
3079 ++curwin->w_cursor.col;
3080 #endif
3081 find_end_of_word(&curwin->w_cursor);
3084 curwin->w_set_curswant = TRUE;
3086 if (is_click)
3087 redraw_curbuf_later(INVERTED); /* update the inversion */
3089 else if (VIsual_active && !old_active)
3091 if (mod_mask & MOD_MASK_ALT)
3092 VIsual_mode = Ctrl_V;
3093 else
3094 VIsual_mode = 'v';
3097 /* If Visual mode changed show it later. */
3098 if ((!VIsual_active && old_active && mode_displayed)
3099 || (VIsual_active && p_smd && msg_silent == 0
3100 && (!old_active || VIsual_mode != old_mode)))
3101 redraw_cmdline = TRUE;
3102 #endif
3104 return moved;
3107 #ifdef FEAT_VISUAL
3109 * Move "pos" back to the start of the word it's in.
3111 static void
3112 find_start_of_word(pos)
3113 pos_T *pos;
3115 char_u *line;
3116 int cclass;
3117 int col;
3119 line = ml_get(pos->lnum);
3120 cclass = get_mouse_class(line + pos->col);
3122 while (pos->col > 0)
3124 col = pos->col - 1;
3125 #ifdef FEAT_MBYTE
3126 col -= (*mb_head_off)(line, line + col);
3127 #endif
3128 if (get_mouse_class(line + col) != cclass)
3129 break;
3130 pos->col = col;
3135 * Move "pos" forward to the end of the word it's in.
3136 * When 'selection' is "exclusive", the position is just after the word.
3138 static void
3139 find_end_of_word(pos)
3140 pos_T *pos;
3142 char_u *line;
3143 int cclass;
3144 int col;
3146 line = ml_get(pos->lnum);
3147 if (*p_sel == 'e' && pos->col > 0)
3149 --pos->col;
3150 #ifdef FEAT_MBYTE
3151 pos->col -= (*mb_head_off)(line, line + pos->col);
3152 #endif
3154 cclass = get_mouse_class(line + pos->col);
3155 while (line[pos->col] != NUL)
3157 #ifdef FEAT_MBYTE
3158 col = pos->col + (*mb_ptr2len)(line + pos->col);
3159 #else
3160 col = pos->col + 1;
3161 #endif
3162 if (get_mouse_class(line + col) != cclass)
3164 if (*p_sel == 'e')
3165 pos->col = col;
3166 break;
3168 pos->col = col;
3173 * Get class of a character for selection: same class means same word.
3174 * 0: blank
3175 * 1: punctuation groups
3176 * 2: normal word character
3177 * >2: multi-byte word character.
3179 static int
3180 get_mouse_class(p)
3181 char_u *p;
3183 int c;
3185 #ifdef FEAT_MBYTE
3186 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3187 return mb_get_class(p);
3188 #endif
3190 c = *p;
3191 if (c == ' ' || c == '\t')
3192 return 0;
3194 if (vim_iswordc(c))
3195 return 2;
3198 * There are a few special cases where we want certain combinations of
3199 * characters to be considered as a single word. These are things like
3200 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
3201 * character is in it's own class.
3203 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3204 return 1;
3205 return c;
3207 #endif /* FEAT_VISUAL */
3208 #endif /* FEAT_MOUSE */
3210 #if defined(FEAT_VISUAL) || defined(PROTO)
3212 * Check if highlighting for visual mode is possible, give a warning message
3213 * if not.
3215 void
3216 check_visual_highlight()
3218 static int did_check = FALSE;
3220 if (full_screen)
3222 if (!did_check && hl_attr(HLF_V) == 0)
3223 MSG(_("Warning: terminal cannot highlight"));
3224 did_check = TRUE;
3229 * End Visual mode.
3230 * This function should ALWAYS be called to end Visual mode, except from
3231 * do_pending_operator().
3233 void
3234 end_visual_mode()
3236 #ifdef FEAT_CLIPBOARD
3238 * If we are using the clipboard, then remember what was selected in case
3239 * we need to paste it somewhere while we still own the selection.
3240 * Only do this when the clipboard is already owned. Don't want to grab
3241 * the selection when hitting ESC.
3243 if (clip_star.available && clip_star.owned)
3244 clip_auto_select();
3245 #endif
3247 VIsual_active = FALSE;
3248 #ifdef FEAT_MOUSE
3249 setmouse();
3250 mouse_dragging = 0;
3251 #endif
3253 /* Save the current VIsual area for '< and '> marks, and "gv" */
3254 curbuf->b_visual.vi_mode = VIsual_mode;
3255 curbuf->b_visual.vi_start = VIsual;
3256 curbuf->b_visual.vi_end = curwin->w_cursor;
3257 curbuf->b_visual.vi_curswant = curwin->w_curswant;
3258 #ifdef FEAT_EVAL
3259 curbuf->b_visual_mode_eval = VIsual_mode;
3260 #endif
3261 #ifdef FEAT_VIRTUALEDIT
3262 if (!virtual_active())
3263 curwin->w_cursor.coladd = 0;
3264 #endif
3266 if (mode_displayed)
3267 clear_cmdline = TRUE; /* unshow visual mode later */
3268 #ifdef FEAT_CMDL_INFO
3269 else
3270 clear_showcmd();
3271 #endif
3273 adjust_cursor_eol();
3277 * Reset VIsual_active and VIsual_reselect.
3279 void
3280 reset_VIsual_and_resel()
3282 if (VIsual_active)
3284 end_visual_mode();
3285 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3287 VIsual_reselect = FALSE;
3291 * Reset VIsual_active and VIsual_reselect if it's set.
3293 void
3294 reset_VIsual()
3296 if (VIsual_active)
3298 end_visual_mode();
3299 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3300 VIsual_reselect = FALSE;
3303 #endif /* FEAT_VISUAL */
3305 #if defined(FEAT_BEVAL)
3306 static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3309 * Check for a balloon-eval special item to include when searching for an
3310 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3311 * Returns TRUE if the character at "*ptr" should be included.
3312 * "dir" is FORWARD or BACKWARD, the direction of searching.
3313 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3314 * "bnp" points to a counter for square brackets.
3316 static int
3317 find_is_eval_item(ptr, colp, bnp, dir)
3318 char_u *ptr;
3319 int *colp;
3320 int *bnp;
3321 int dir;
3323 /* Accept everything inside []. */
3324 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3325 ++*bnp;
3326 if (*bnp > 0)
3328 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3329 --*bnp;
3330 return TRUE;
3333 /* skip over "s.var" */
3334 if (*ptr == '.')
3335 return TRUE;
3337 /* two-character item: s->var */
3338 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3339 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3341 *colp += dir;
3342 return TRUE;
3344 return FALSE;
3346 #endif
3349 * Find the identifier under or to the right of the cursor.
3350 * "find_type" can have one of three values:
3351 * FIND_IDENT: find an identifier (keyword)
3352 * FIND_STRING: find any non-white string
3353 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
3354 * FIND_EVAL: find text useful for C program debugging
3356 * There are three steps:
3357 * 1. Search forward for the start of an identifier/string. Doesn't move if
3358 * already on one.
3359 * 2. Search backward for the start of this identifier/string.
3360 * This doesn't match the real Vi but I like it a little better and it
3361 * shouldn't bother anyone.
3362 * 3. Search forward to the end of this identifier/string.
3363 * When FIND_IDENT isn't defined, we backup until a blank.
3365 * Returns the length of the string, or zero if no string is found.
3366 * If a string is found, a pointer to the string is put in "*string". This
3367 * string is not always NUL terminated.
3370 find_ident_under_cursor(string, find_type)
3371 char_u **string;
3372 int find_type;
3374 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3375 curwin->w_cursor.col, string, find_type);
3379 * Like find_ident_under_cursor(), but for any window and any position.
3380 * However: Uses 'iskeyword' from the current window!.
3383 find_ident_at_pos(wp, lnum, startcol, string, find_type)
3384 win_T *wp;
3385 linenr_T lnum;
3386 colnr_T startcol;
3387 char_u **string;
3388 int find_type;
3390 char_u *ptr;
3391 int col = 0; /* init to shut up GCC */
3392 int i;
3393 #ifdef FEAT_MBYTE
3394 int this_class = 0;
3395 int prev_class;
3396 int prevcol;
3397 #endif
3398 #if defined(FEAT_BEVAL)
3399 int bn = 0; /* bracket nesting */
3400 #endif
3403 * if i == 0: try to find an identifier
3404 * if i == 1: try to find any non-white string
3406 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3407 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3410 * 1. skip to start of identifier/string
3412 col = startcol;
3413 #ifdef FEAT_MBYTE
3414 if (has_mbyte)
3416 while (ptr[col] != NUL)
3418 # if defined(FEAT_BEVAL)
3419 /* Stop at a ']' to evaluate "a[x]". */
3420 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3421 break;
3422 # endif
3423 this_class = mb_get_class(ptr + col);
3424 if (this_class != 0 && (i == 1 || this_class != 1))
3425 break;
3426 col += (*mb_ptr2len)(ptr + col);
3429 else
3430 #endif
3431 while (ptr[col] != NUL
3432 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
3433 # if defined(FEAT_BEVAL)
3434 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3435 # endif
3437 ++col;
3439 #if defined(FEAT_BEVAL)
3440 /* When starting on a ']' count it, so that we include the '['. */
3441 bn = ptr[col] == ']';
3442 #endif
3445 * 2. Back up to start of identifier/string.
3447 #ifdef FEAT_MBYTE
3448 if (has_mbyte)
3450 /* Remember class of character under cursor. */
3451 # if defined(FEAT_BEVAL)
3452 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3453 this_class = mb_get_class((char_u *)"a");
3454 else
3455 # endif
3456 this_class = mb_get_class(ptr + col);
3457 while (col > 0 && this_class != 0)
3459 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3460 prev_class = mb_get_class(ptr + prevcol);
3461 if (this_class != prev_class
3462 && (i == 0
3463 || prev_class == 0
3464 || (find_type & FIND_IDENT))
3465 # if defined(FEAT_BEVAL)
3466 && (!(find_type & FIND_EVAL)
3467 || prevcol == 0
3468 || !find_is_eval_item(ptr + prevcol, &prevcol,
3469 &bn, BACKWARD))
3470 # endif
3472 break;
3473 col = prevcol;
3476 /* If we don't want just any old string, or we've found an
3477 * identifier, stop searching. */
3478 if (this_class > 2)
3479 this_class = 2;
3480 if (!(find_type & FIND_STRING) || this_class == 2)
3481 break;
3483 else
3484 #endif
3486 while (col > 0
3487 && ((i == 0
3488 ? vim_iswordc(ptr[col - 1])
3489 : (!vim_iswhite(ptr[col - 1])
3490 && (!(find_type & FIND_IDENT)
3491 || !vim_iswordc(ptr[col - 1]))))
3492 #if defined(FEAT_BEVAL)
3493 || ((find_type & FIND_EVAL)
3494 && col > 1
3495 && find_is_eval_item(ptr + col - 1, &col,
3496 &bn, BACKWARD))
3497 #endif
3499 --col;
3501 /* If we don't want just any old string, or we've found an
3502 * identifier, stop searching. */
3503 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3504 break;
3508 if (ptr[col] == NUL || (i == 0 && (
3509 #ifdef FEAT_MBYTE
3510 has_mbyte ? this_class != 2 :
3511 #endif
3512 !vim_iswordc(ptr[col]))))
3515 * didn't find an identifier or string
3517 if (find_type & FIND_STRING)
3518 EMSG(_("E348: No string under cursor"));
3519 else
3520 EMSG(_(e_noident));
3521 return 0;
3523 ptr += col;
3524 *string = ptr;
3527 * 3. Find the end if the identifier/string.
3529 #if defined(FEAT_BEVAL)
3530 bn = 0;
3531 startcol -= col;
3532 #endif
3533 col = 0;
3534 #ifdef FEAT_MBYTE
3535 if (has_mbyte)
3537 /* Search for point of changing multibyte character class. */
3538 this_class = mb_get_class(ptr);
3539 while (ptr[col] != NUL
3540 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3541 : mb_get_class(ptr + col) != 0)
3542 # if defined(FEAT_BEVAL)
3543 || ((find_type & FIND_EVAL)
3544 && col <= (int)startcol
3545 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3546 # endif
3548 col += (*mb_ptr2len)(ptr + col);
3550 else
3551 #endif
3552 while ((i == 0 ? vim_iswordc(ptr[col])
3553 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
3554 # if defined(FEAT_BEVAL)
3555 || ((find_type & FIND_EVAL)
3556 && col <= (int)startcol
3557 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3558 # endif
3561 ++col;
3564 return col;
3568 * Prepare for redo of a normal command.
3570 static void
3571 prep_redo_cmd(cap)
3572 cmdarg_T *cap;
3574 prep_redo(cap->oap->regname, cap->count0,
3575 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3579 * Prepare for redo of any command.
3580 * Note that only the last argument can be a multi-byte char.
3582 static void
3583 prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3584 int regname;
3585 long num;
3586 int cmd1;
3587 int cmd2;
3588 int cmd3;
3589 int cmd4;
3590 int cmd5;
3592 ResetRedobuff();
3593 if (regname != 0) /* yank from specified buffer */
3595 AppendCharToRedobuff('"');
3596 AppendCharToRedobuff(regname);
3598 if (num)
3599 AppendNumberToRedobuff(num);
3601 if (cmd1 != NUL)
3602 AppendCharToRedobuff(cmd1);
3603 if (cmd2 != NUL)
3604 AppendCharToRedobuff(cmd2);
3605 if (cmd3 != NUL)
3606 AppendCharToRedobuff(cmd3);
3607 if (cmd4 != NUL)
3608 AppendCharToRedobuff(cmd4);
3609 if (cmd5 != NUL)
3610 AppendCharToRedobuff(cmd5);
3614 * check for operator active and clear it
3616 * return TRUE if operator was active
3618 static int
3619 checkclearop(oap)
3620 oparg_T *oap;
3622 if (oap->op_type == OP_NOP)
3623 return FALSE;
3624 clearopbeep(oap);
3625 return TRUE;
3629 * Check for operator or Visual active. Clear active operator.
3631 * Return TRUE if operator or Visual was active.
3633 static int
3634 checkclearopq(oap)
3635 oparg_T *oap;
3637 if (oap->op_type == OP_NOP
3638 #ifdef FEAT_VISUAL
3639 && !VIsual_active
3640 #endif
3642 return FALSE;
3643 clearopbeep(oap);
3644 return TRUE;
3647 static void
3648 clearop(oap)
3649 oparg_T *oap;
3651 oap->op_type = OP_NOP;
3652 oap->regname = 0;
3653 oap->motion_force = NUL;
3654 oap->use_reg_one = FALSE;
3657 static void
3658 clearopbeep(oap)
3659 oparg_T *oap;
3661 clearop(oap);
3662 beep_flush();
3665 #ifdef FEAT_VISUAL
3667 * Remove the shift modifier from a special key.
3669 static void
3670 unshift_special(cap)
3671 cmdarg_T *cap;
3673 switch (cap->cmdchar)
3675 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3676 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3677 case K_S_UP: cap->cmdchar = K_UP; break;
3678 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3679 case K_S_HOME: cap->cmdchar = K_HOME; break;
3680 case K_S_END: cap->cmdchar = K_END; break;
3682 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3684 #endif
3686 #if defined(FEAT_CMDL_INFO) || defined(PROTO)
3688 * Routines for displaying a partly typed command
3691 #ifdef FEAT_VISUAL /* need room for size of Visual area */
3692 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3693 #else
3694 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3695 #endif
3696 static char_u showcmd_buf[SHOWCMD_BUFLEN];
3697 static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3698 static int showcmd_is_clear = TRUE;
3699 static int showcmd_visual = FALSE;
3701 static void display_showcmd __ARGS((void));
3703 void
3704 clear_showcmd()
3706 if (!p_sc)
3707 return;
3709 #ifdef FEAT_VISUAL
3710 if (VIsual_active && !char_avail())
3712 int cursor_bot = lt(VIsual, curwin->w_cursor);
3713 long lines;
3714 colnr_T leftcol, rightcol;
3715 linenr_T top, bot;
3717 /* Show the size of the Visual area. */
3718 if (cursor_bot)
3720 top = VIsual.lnum;
3721 bot = curwin->w_cursor.lnum;
3723 else
3725 top = curwin->w_cursor.lnum;
3726 bot = VIsual.lnum;
3728 # ifdef FEAT_FOLDING
3729 /* Include closed folds as a whole. */
3730 hasFolding(top, &top, NULL);
3731 hasFolding(bot, NULL, &bot);
3732 # endif
3733 lines = bot - top + 1;
3735 if (VIsual_mode == Ctrl_V)
3737 #ifdef FEAT_LINEBREAK
3738 char_u *saved_sbr = p_sbr;
3740 /* Make 'sbr' empty for a moment to get the correct size. */
3741 p_sbr = empty_option;
3742 #endif
3743 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
3744 #ifdef FEAT_LINEBREAK
3745 p_sbr = saved_sbr;
3746 #endif
3747 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3748 (long)(rightcol - leftcol + 1));
3750 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3751 sprintf((char *)showcmd_buf, "%ld", lines);
3752 else
3753 sprintf((char *)showcmd_buf, "%ld", (long)(cursor_bot
3754 ? curwin->w_cursor.col - VIsual.col
3755 : VIsual.col - curwin->w_cursor.col) + (*p_sel != 'e'));
3756 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3757 showcmd_visual = TRUE;
3759 else
3760 #endif
3762 showcmd_buf[0] = NUL;
3763 showcmd_visual = FALSE;
3765 /* Don't actually display something if there is nothing to clear. */
3766 if (showcmd_is_clear)
3767 return;
3770 display_showcmd();
3774 * Add 'c' to string of shown command chars.
3775 * Return TRUE if output has been written (and setcursor() has been called).
3778 add_to_showcmd(c)
3779 int c;
3781 char_u *p;
3782 int old_len;
3783 int extra_len;
3784 int overflow;
3785 #if defined(FEAT_MOUSE)
3786 int i;
3787 static int ignore[] =
3789 # ifdef FEAT_GUI
3790 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3791 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
3792 # endif
3793 K_IGNORE,
3794 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3795 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3796 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
3797 K_MOUSEDOWN, K_MOUSEUP,
3798 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
3799 K_CURSORHOLD,
3802 #endif
3804 if (!p_sc || msg_silent != 0)
3805 return FALSE;
3807 if (showcmd_visual)
3809 showcmd_buf[0] = NUL;
3810 showcmd_visual = FALSE;
3813 #if defined(FEAT_MOUSE)
3814 /* Ignore keys that are scrollbar updates and mouse clicks */
3815 if (IS_SPECIAL(c))
3816 for (i = 0; ignore[i] != 0; ++i)
3817 if (ignore[i] == c)
3818 return FALSE;
3819 #endif
3821 p = transchar(c);
3822 old_len = (int)STRLEN(showcmd_buf);
3823 extra_len = (int)STRLEN(p);
3824 overflow = old_len + extra_len - SHOWCMD_COLS;
3825 if (overflow > 0)
3826 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3827 old_len - overflow + 1);
3828 STRCAT(showcmd_buf, p);
3830 if (char_avail())
3831 return FALSE;
3833 display_showcmd();
3835 return TRUE;
3838 void
3839 add_to_showcmd_c(c)
3840 int c;
3842 if (!add_to_showcmd(c))
3843 setcursor();
3847 * Delete 'len' characters from the end of the shown command.
3849 static void
3850 del_from_showcmd(len)
3851 int len;
3853 int old_len;
3855 if (!p_sc)
3856 return;
3858 old_len = (int)STRLEN(showcmd_buf);
3859 if (len > old_len)
3860 len = old_len;
3861 showcmd_buf[old_len - len] = NUL;
3863 if (!char_avail())
3864 display_showcmd();
3868 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3869 * something and there is a partial mapping.
3871 void
3872 push_showcmd()
3874 if (p_sc)
3875 STRCPY(old_showcmd_buf, showcmd_buf);
3878 void
3879 pop_showcmd()
3881 if (!p_sc)
3882 return;
3884 STRCPY(showcmd_buf, old_showcmd_buf);
3886 display_showcmd();
3889 static void
3890 display_showcmd()
3892 int len;
3894 cursor_off();
3896 len = (int)STRLEN(showcmd_buf);
3897 if (len == 0)
3898 showcmd_is_clear = TRUE;
3899 else
3901 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3902 showcmd_is_clear = FALSE;
3906 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3907 * spaces
3909 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3911 setcursor(); /* put cursor back where it belongs */
3913 #endif
3915 #ifdef FEAT_SCROLLBIND
3917 * When "check" is FALSE, prepare for commands that scroll the window.
3918 * When "check" is TRUE, take care of scroll-binding after the window has
3919 * scrolled. Called from normal_cmd() and edit().
3921 void
3922 do_check_scrollbind(check)
3923 int check;
3925 static win_T *old_curwin = NULL;
3926 static linenr_T old_topline = 0;
3927 #ifdef FEAT_DIFF
3928 static int old_topfill = 0;
3929 #endif
3930 static buf_T *old_buf = NULL;
3931 static colnr_T old_leftcol = 0;
3933 if (check && curwin->w_p_scb)
3935 /* If a ":syncbind" command was just used, don't scroll, only reset
3936 * the values. */
3937 if (did_syncbind)
3938 did_syncbind = FALSE;
3939 else if (curwin == old_curwin)
3942 * Synchronize other windows, as necessary according to
3943 * 'scrollbind'. Don't do this after an ":edit" command, except
3944 * when 'diff' is set.
3946 if ((curwin->w_buffer == old_buf
3947 #ifdef FEAT_DIFF
3948 || curwin->w_p_diff
3949 #endif
3951 && (curwin->w_topline != old_topline
3952 #ifdef FEAT_DIFF
3953 || curwin->w_topfill != old_topfill
3954 #endif
3955 || curwin->w_leftcol != old_leftcol))
3957 check_scrollbind(curwin->w_topline - old_topline,
3958 (long)(curwin->w_leftcol - old_leftcol));
3961 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
3964 * When switching between windows, make sure that the relative
3965 * vertical offset is valid for the new window. The relative
3966 * offset is invalid whenever another 'scrollbind' window has
3967 * scrolled to a point that would force the current window to
3968 * scroll past the beginning or end of its buffer. When the
3969 * resync is performed, some of the other 'scrollbind' windows may
3970 * need to jump so that the current window's relative position is
3971 * visible on-screen.
3973 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
3975 curwin->w_scbind_pos = curwin->w_topline;
3978 old_curwin = curwin;
3979 old_topline = curwin->w_topline;
3980 #ifdef FEAT_DIFF
3981 old_topfill = curwin->w_topfill;
3982 #endif
3983 old_buf = curwin->w_buffer;
3984 old_leftcol = curwin->w_leftcol;
3988 * Synchronize any windows that have "scrollbind" set, based on the
3989 * number of rows by which the current window has changed
3990 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3992 void
3993 check_scrollbind(topline_diff, leftcol_diff)
3994 linenr_T topline_diff;
3995 long leftcol_diff;
3997 int want_ver;
3998 int want_hor;
3999 win_T *old_curwin = curwin;
4000 buf_T *old_curbuf = curbuf;
4001 #ifdef FEAT_VISUAL
4002 int old_VIsual_select = VIsual_select;
4003 int old_VIsual_active = VIsual_active;
4004 #endif
4005 colnr_T tgt_leftcol = curwin->w_leftcol;
4006 long topline;
4007 long y;
4010 * check 'scrollopt' string for vertical and horizontal scroll options
4012 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4013 #ifdef FEAT_DIFF
4014 want_ver |= old_curwin->w_p_diff;
4015 #endif
4016 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4019 * loop through the scrollbound windows and scroll accordingly
4021 #ifdef FEAT_VISUAL
4022 VIsual_select = VIsual_active = 0;
4023 #endif
4024 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4026 curbuf = curwin->w_buffer;
4027 /* skip original window and windows with 'noscrollbind' */
4028 if (curwin != old_curwin && curwin->w_p_scb)
4031 * do the vertical scroll
4033 if (want_ver)
4035 #ifdef FEAT_DIFF
4036 if (old_curwin->w_p_diff && curwin->w_p_diff)
4038 diff_set_topline(old_curwin, curwin);
4040 else
4041 #endif
4043 curwin->w_scbind_pos += topline_diff;
4044 topline = curwin->w_scbind_pos;
4045 if (topline > curbuf->b_ml.ml_line_count)
4046 topline = curbuf->b_ml.ml_line_count;
4047 if (topline < 1)
4048 topline = 1;
4050 y = topline - curwin->w_topline;
4051 if (y > 0)
4052 scrollup(y, FALSE);
4053 else
4054 scrolldown(-y, FALSE);
4057 redraw_later(VALID);
4058 cursor_correct();
4059 #ifdef FEAT_WINDOWS
4060 curwin->w_redr_status = TRUE;
4061 #endif
4065 * do the horizontal scroll
4067 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4069 curwin->w_leftcol = tgt_leftcol;
4070 leftcol_changed();
4076 * reset current-window
4078 #ifdef FEAT_VISUAL
4079 VIsual_select = old_VIsual_select;
4080 VIsual_active = old_VIsual_active;
4081 #endif
4082 curwin = old_curwin;
4083 curbuf = old_curbuf;
4085 #endif /* #ifdef FEAT_SCROLLBIND */
4088 * Command character that's ignored.
4089 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
4090 * xon/xoff
4092 static void
4093 nv_ignore(cap)
4094 cmdarg_T *cap;
4096 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
4100 * Command character that doesn't do anything, but unlike nv_ignore() does
4101 * start edit(). Used for "startinsert" executed while starting up.
4103 static void
4104 nv_nop(cap)
4105 cmdarg_T *cap UNUSED;
4110 * Command character doesn't exist.
4112 static void
4113 nv_error(cap)
4114 cmdarg_T *cap;
4116 clearopbeep(cap->oap);
4120 * <Help> and <F1> commands.
4122 static void
4123 nv_help(cap)
4124 cmdarg_T *cap;
4126 if (!checkclearopq(cap->oap))
4127 ex_help(NULL);
4131 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4133 static void
4134 nv_addsub(cap)
4135 cmdarg_T *cap;
4137 if (!checkclearopq(cap->oap)
4138 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4139 prep_redo_cmd(cap);
4143 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4145 static void
4146 nv_page(cap)
4147 cmdarg_T *cap;
4149 if (!checkclearop(cap->oap))
4151 #ifdef FEAT_WINDOWS
4152 if (mod_mask & MOD_MASK_CTRL)
4154 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4155 if (cap->arg == BACKWARD)
4156 goto_tabpage(-(int)cap->count1);
4157 else
4158 goto_tabpage((int)cap->count0);
4160 else
4161 #endif
4162 (void)onepage(cap->arg, cap->count1);
4167 * Implementation of "gd" and "gD" command.
4169 static void
4170 nv_gd(oap, nchar, thisblock)
4171 oparg_T *oap;
4172 int nchar;
4173 int thisblock; /* 1 for "1gd" and "1gD" */
4175 int len;
4176 char_u *ptr;
4178 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
4179 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
4180 clearopbeep(oap);
4181 #ifdef FEAT_FOLDING
4182 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4183 foldOpenCursor();
4184 #endif
4188 * Search for variable declaration of "ptr[len]".
4189 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4190 * current file ("gD").
4191 * When "thisblock" is TRUE check the {} block scope.
4192 * Return FAIL when not found.
4195 find_decl(ptr, len, locally, thisblock, searchflags)
4196 char_u *ptr;
4197 int len;
4198 int locally;
4199 int thisblock;
4200 int searchflags; /* flags passed to searchit() */
4202 char_u *pat;
4203 pos_T old_pos;
4204 pos_T par_pos;
4205 pos_T found_pos;
4206 int t;
4207 int save_p_ws;
4208 int save_p_scs;
4209 int retval = OK;
4210 int incll;
4212 if ((pat = alloc(len + 7)) == NULL)
4213 return FAIL;
4215 /* Put "\V" before the pattern to avoid that the special meaning of "."
4216 * and "~" causes trouble. */
4217 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4218 len, ptr);
4219 old_pos = curwin->w_cursor;
4220 save_p_ws = p_ws;
4221 save_p_scs = p_scs;
4222 p_ws = FALSE; /* don't wrap around end of file now */
4223 p_scs = FALSE; /* don't switch ignorecase off now */
4226 * With "gD" go to line 1.
4227 * With "gd" Search back for the start of the current function, then go
4228 * back until a blank line. If this fails go to line 1.
4230 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
4232 setpcmark(); /* Set in findpar() otherwise */
4233 curwin->w_cursor.lnum = 1;
4234 par_pos = curwin->w_cursor;
4236 else
4238 par_pos = curwin->w_cursor;
4239 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4240 --curwin->w_cursor.lnum;
4242 curwin->w_cursor.col = 0;
4244 /* Search forward for the identifier, ignore comment lines. */
4245 clearpos(&found_pos);
4246 for (;;)
4248 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
4249 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
4250 if (curwin->w_cursor.lnum >= old_pos.lnum)
4251 t = FAIL; /* match after start is failure too */
4253 if (thisblock && t != FAIL)
4255 pos_T *pos;
4257 /* Check that the block the match is in doesn't end before the
4258 * position where we started the search from. */
4259 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4260 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4261 && pos->lnum < old_pos.lnum)
4262 continue;
4265 if (t == FAIL)
4267 /* If we previously found a valid position, use it. */
4268 if (found_pos.lnum != 0)
4270 curwin->w_cursor = found_pos;
4271 t = OK;
4273 break;
4275 #ifdef FEAT_COMMENTS
4276 if (get_leader_len(ml_get_curline(), NULL, FALSE) > 0)
4278 /* Ignore this line, continue at start of next line. */
4279 ++curwin->w_cursor.lnum;
4280 curwin->w_cursor.col = 0;
4281 continue;
4283 #endif
4284 if (!locally) /* global search: use first match found */
4285 break;
4286 if (curwin->w_cursor.lnum >= par_pos.lnum)
4288 /* If we previously found a valid position, use it. */
4289 if (found_pos.lnum != 0)
4290 curwin->w_cursor = found_pos;
4291 break;
4294 /* For finding a local variable and the match is before the "{" search
4295 * to find a later match. For K&R style function declarations this
4296 * skips the function header without types. */
4297 found_pos = curwin->w_cursor;
4300 if (t == FAIL)
4302 retval = FAIL;
4303 curwin->w_cursor = old_pos;
4305 else
4307 curwin->w_set_curswant = TRUE;
4308 /* "n" searches forward now */
4309 reset_search_dir();
4312 vim_free(pat);
4313 p_ws = save_p_ws;
4314 p_scs = save_p_scs;
4316 return retval;
4320 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4321 * lines rather than lines in the file.
4322 * 'dist' must be positive.
4324 * Return OK if able to move cursor, FAIL otherwise.
4326 static int
4327 nv_screengo(oap, dir, dist)
4328 oparg_T *oap;
4329 int dir;
4330 long dist;
4332 int linelen = linetabsize(ml_get_curline());
4333 int retval = OK;
4334 int atend = FALSE;
4335 int n;
4336 int col_off1; /* margin offset for first screen line */
4337 int col_off2; /* margin offset for wrapped screen line */
4338 int width1; /* text width for first screen line */
4339 int width2; /* test width for wrapped screen line */
4341 oap->motion_type = MCHAR;
4342 oap->inclusive = FALSE;
4344 col_off1 = curwin_col_off();
4345 col_off2 = col_off1 - curwin_col_off2();
4346 width1 = W_WIDTH(curwin) - col_off1;
4347 width2 = W_WIDTH(curwin) - col_off2;
4349 #ifdef FEAT_VERTSPLIT
4350 if (curwin->w_width != 0)
4352 #endif
4354 * Instead of sticking at the last character of the buffer line we
4355 * try to stick in the last column of the screen.
4357 if (curwin->w_curswant == MAXCOL)
4359 atend = TRUE;
4360 validate_virtcol();
4361 if (width1 <= 0)
4362 curwin->w_curswant = 0;
4363 else
4365 curwin->w_curswant = width1 - 1;
4366 if (curwin->w_virtcol > curwin->w_curswant)
4367 curwin->w_curswant += ((curwin->w_virtcol
4368 - curwin->w_curswant - 1) / width2 + 1) * width2;
4371 else
4373 if (linelen > width1)
4374 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4375 else
4376 n = width1;
4377 if (curwin->w_curswant > (colnr_T)n + 1)
4378 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4379 * width2;
4382 while (dist--)
4384 if (dir == BACKWARD)
4386 if ((long)curwin->w_curswant >= width2)
4387 /* move back within line */
4388 curwin->w_curswant -= width2;
4389 else
4391 /* to previous line */
4392 if (curwin->w_cursor.lnum == 1)
4394 retval = FAIL;
4395 break;
4397 --curwin->w_cursor.lnum;
4398 #ifdef FEAT_FOLDING
4399 /* Move to the start of a closed fold. Don't do that when
4400 * 'foldopen' contains "all": it will open in a moment. */
4401 if (!(fdo_flags & FDO_ALL))
4402 (void)hasFolding(curwin->w_cursor.lnum,
4403 &curwin->w_cursor.lnum, NULL);
4404 #endif
4405 linelen = linetabsize(ml_get_curline());
4406 if (linelen > width1)
4407 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4408 + 1) * width2;
4411 else /* dir == FORWARD */
4413 if (linelen > width1)
4414 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4415 else
4416 n = width1;
4417 if (curwin->w_curswant + width2 < (colnr_T)n)
4418 /* move forward within line */
4419 curwin->w_curswant += width2;
4420 else
4422 /* to next line */
4423 #ifdef FEAT_FOLDING
4424 /* Move to the end of a closed fold. */
4425 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4426 &curwin->w_cursor.lnum);
4427 #endif
4428 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4430 retval = FAIL;
4431 break;
4433 curwin->w_cursor.lnum++;
4434 curwin->w_curswant %= width2;
4438 #ifdef FEAT_VERTSPLIT
4440 #endif
4442 coladvance(curwin->w_curswant);
4444 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4445 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4448 * Check for landing on a character that got split at the end of the
4449 * last line. We want to advance a screenline, not end up in the same
4450 * screenline or move two screenlines.
4452 validate_virtcol();
4453 if (curwin->w_virtcol > curwin->w_curswant
4454 && (curwin->w_curswant < (colnr_T)width1
4455 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4456 : ((curwin->w_curswant - width1) % width2
4457 > (colnr_T)width2 / 2)))
4458 --curwin->w_cursor.col;
4460 #endif
4462 if (atend)
4463 curwin->w_curswant = MAXCOL; /* stick in the last column */
4465 return retval;
4468 #ifdef FEAT_MOUSE
4470 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4471 * when Shift or Ctrl is used.
4472 * K_MOUSEUP (cap->arg == TRUE) or K_MOUSEDOWN (cap->arg == FALSE)
4474 static void
4475 nv_mousescroll(cap)
4476 cmdarg_T *cap;
4478 # ifdef FEAT_GUI_SCROLL_WHEEL_FORCE
4479 int scroll_wheel_force = 0;
4480 # endif
4481 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4482 win_T *old_curwin = curwin;
4484 /* Currently we only get the mouse coordinates in the GUI. */
4485 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4487 int row, col;
4489 row = mouse_row;
4490 col = mouse_col;
4492 /* find the window at the pointer coordinates */
4493 curwin = mouse_find_win(&row, &col);
4494 curbuf = curwin->w_buffer;
4496 # endif
4498 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4500 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4502 else
4504 # ifdef FEAT_GUI_SCROLL_WHEEL_FORCE
4505 if (gui.in_use && gui.scroll_wheel_force >= 1) {
4506 scroll_wheel_force = gui.scroll_wheel_force;
4507 if (scroll_wheel_force > 1000)
4508 scroll_wheel_force = 1000;
4510 cap->count1 = scroll_wheel_force;
4511 cap->count0 = scroll_wheel_force;
4512 } else {
4513 cap->count1 = 3;
4514 cap->count0 = 3;
4516 # else
4517 cap->count1 = 3;
4518 cap->count0 = 3;
4519 # endif
4520 nv_scroll_line(cap);
4523 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4524 curwin->w_redr_status = TRUE;
4526 curwin = old_curwin;
4527 curbuf = curwin->w_buffer;
4528 # endif
4532 * Mouse clicks and drags.
4534 static void
4535 nv_mouse(cap)
4536 cmdarg_T *cap;
4538 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4540 #endif
4543 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4544 * cap->arg must be TRUE for CTRL-E.
4546 static void
4547 nv_scroll_line(cap)
4548 cmdarg_T *cap;
4550 if (!checkclearop(cap->oap))
4551 scroll_redraw(cap->arg, cap->count1);
4555 * Scroll "count" lines up or down, and redraw.
4557 void
4558 scroll_redraw(up, count)
4559 int up;
4560 long count;
4562 linenr_T prev_topline = curwin->w_topline;
4563 #ifdef FEAT_DIFF
4564 int prev_topfill = curwin->w_topfill;
4565 #endif
4566 linenr_T prev_lnum = curwin->w_cursor.lnum;
4568 if (up)
4569 scrollup(count, TRUE);
4570 else
4571 scrolldown(count, TRUE);
4572 if (p_so)
4574 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4575 * valid, otherwise the screen jumps back at the end of the file. */
4576 cursor_correct();
4577 check_cursor_moved(curwin);
4578 curwin->w_valid |= VALID_TOPLINE;
4580 /* If moved back to where we were, at least move the cursor, otherwise
4581 * we get stuck at one position. Don't move the cursor up if the
4582 * first line of the buffer is already on the screen */
4583 while (curwin->w_topline == prev_topline
4584 #ifdef FEAT_DIFF
4585 && curwin->w_topfill == prev_topfill
4586 #endif
4589 if (up)
4591 if (curwin->w_cursor.lnum > prev_lnum
4592 || cursor_down(1L, FALSE) == FAIL)
4593 break;
4595 else
4597 if (curwin->w_cursor.lnum < prev_lnum
4598 || prev_topline == 1L
4599 || cursor_up(1L, FALSE) == FAIL)
4600 break;
4602 /* Mark w_topline as valid, otherwise the screen jumps back at the
4603 * end of the file. */
4604 check_cursor_moved(curwin);
4605 curwin->w_valid |= VALID_TOPLINE;
4608 if (curwin->w_cursor.lnum != prev_lnum)
4609 coladvance(curwin->w_curswant);
4610 redraw_later(VALID);
4614 * Commands that start with "z".
4616 static void
4617 nv_zet(cap)
4618 cmdarg_T *cap;
4620 long n;
4621 colnr_T col;
4622 int nchar = cap->nchar;
4623 #ifdef FEAT_FOLDING
4624 long old_fdl = curwin->w_p_fdl;
4625 int old_fen = curwin->w_p_fen;
4626 #endif
4627 #ifdef FEAT_SPELL
4628 int undo = FALSE;
4629 #endif
4631 if (VIM_ISDIGIT(nchar))
4634 * "z123{nchar}": edit the count before obtaining {nchar}
4636 if (checkclearop(cap->oap))
4637 return;
4638 n = nchar - '0';
4639 for (;;)
4641 #ifdef USE_ON_FLY_SCROLL
4642 dont_scroll = TRUE; /* disallow scrolling here */
4643 #endif
4644 ++no_mapping;
4645 ++allow_keys; /* no mapping for nchar, but allow key codes */
4646 nchar = plain_vgetc();
4647 LANGMAP_ADJUST(nchar, TRUE);
4648 --no_mapping;
4649 --allow_keys;
4650 #ifdef FEAT_CMDL_INFO
4651 (void)add_to_showcmd(nchar);
4652 #endif
4653 if (nchar == K_DEL || nchar == K_KDEL)
4654 n /= 10;
4655 else if (VIM_ISDIGIT(nchar))
4656 n = n * 10 + (nchar - '0');
4657 else if (nchar == CAR)
4659 #ifdef FEAT_GUI
4660 need_mouse_correct = TRUE;
4661 #endif
4662 win_setheight((int)n);
4663 break;
4665 else if (nchar == 'l'
4666 || nchar == 'h'
4667 || nchar == K_LEFT
4668 || nchar == K_RIGHT)
4670 cap->count1 = n ? n * cap->count1 : cap->count1;
4671 goto dozet;
4673 else
4675 clearopbeep(cap->oap);
4676 break;
4679 cap->oap->op_type = OP_NOP;
4680 return;
4683 dozet:
4684 if (
4685 #ifdef FEAT_FOLDING
4686 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4687 * and "zC" only in Visual mode. "zj" and "zk" are motion
4688 * commands. */
4689 cap->nchar != 'f' && cap->nchar != 'F'
4690 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4691 && cap->nchar != 'j' && cap->nchar != 'k'
4693 #endif
4694 checkclearop(cap->oap))
4695 return;
4698 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4699 * If line number given, set cursor.
4701 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4702 && cap->count0
4703 && cap->count0 != curwin->w_cursor.lnum)
4705 setpcmark();
4706 if (cap->count0 > curbuf->b_ml.ml_line_count)
4707 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4708 else
4709 curwin->w_cursor.lnum = cap->count0;
4710 check_cursor_col();
4713 switch (nchar)
4715 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4716 case '+':
4717 if (cap->count0 == 0)
4719 /* No count given: put cursor at the line below screen */
4720 validate_botline(); /* make sure w_botline is valid */
4721 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4722 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4723 else
4724 curwin->w_cursor.lnum = curwin->w_botline;
4726 /* FALLTHROUGH */
4727 case NL:
4728 case CAR:
4729 case K_KENTER:
4730 beginline(BL_WHITE | BL_FIX);
4731 /* FALLTHROUGH */
4733 case 't': scroll_cursor_top(0, TRUE);
4734 redraw_later(VALID);
4735 break;
4737 /* "z." and "zz": put cursor in middle of screen */
4738 case '.': beginline(BL_WHITE | BL_FIX);
4739 /* FALLTHROUGH */
4741 case 'z': scroll_cursor_halfway(TRUE);
4742 redraw_later(VALID);
4743 break;
4745 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4746 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4747 * when <count> is at bottom of window, and puts that one at
4748 * bottom of window. */
4749 if (cap->count0 != 0)
4751 scroll_cursor_bot(0, TRUE);
4752 curwin->w_cursor.lnum = curwin->w_topline;
4754 else if (curwin->w_topline == 1)
4755 curwin->w_cursor.lnum = 1;
4756 else
4757 curwin->w_cursor.lnum = curwin->w_topline - 1;
4758 /* FALLTHROUGH */
4759 case '-':
4760 beginline(BL_WHITE | BL_FIX);
4761 /* FALLTHROUGH */
4763 case 'b': scroll_cursor_bot(0, TRUE);
4764 redraw_later(VALID);
4765 break;
4767 /* "zH" - scroll screen right half-page */
4768 case 'H':
4769 cap->count1 *= W_WIDTH(curwin) / 2;
4770 /* FALLTHROUGH */
4772 /* "zh" - scroll screen to the right */
4773 case 'h':
4774 case K_LEFT:
4775 if (!curwin->w_p_wrap)
4777 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4778 curwin->w_leftcol = 0;
4779 else
4780 curwin->w_leftcol -= (colnr_T)cap->count1;
4781 leftcol_changed();
4783 break;
4785 /* "zL" - scroll screen left half-page */
4786 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4787 /* FALLTHROUGH */
4789 /* "zl" - scroll screen to the left */
4790 case 'l':
4791 case K_RIGHT:
4792 if (!curwin->w_p_wrap)
4794 /* scroll the window left */
4795 curwin->w_leftcol += (colnr_T)cap->count1;
4796 leftcol_changed();
4798 break;
4800 /* "zs" - scroll screen, cursor at the start */
4801 case 's': if (!curwin->w_p_wrap)
4803 #ifdef FEAT_FOLDING
4804 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4805 col = 0; /* like the cursor is in col 0 */
4806 else
4807 #endif
4808 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4809 if ((long)col > p_siso)
4810 col -= p_siso;
4811 else
4812 col = 0;
4813 if (curwin->w_leftcol != col)
4815 curwin->w_leftcol = col;
4816 redraw_later(NOT_VALID);
4819 break;
4821 /* "ze" - scroll screen, cursor at the end */
4822 case 'e': if (!curwin->w_p_wrap)
4824 #ifdef FEAT_FOLDING
4825 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4826 col = 0; /* like the cursor is in col 0 */
4827 else
4828 #endif
4829 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4830 n = W_WIDTH(curwin) - curwin_col_off();
4831 if ((long)col + p_siso < n)
4832 col = 0;
4833 else
4834 col = col + p_siso - n + 1;
4835 if (curwin->w_leftcol != col)
4837 curwin->w_leftcol = col;
4838 redraw_later(NOT_VALID);
4841 break;
4843 #ifdef FEAT_FOLDING
4844 /* "zF": create fold command */
4845 /* "zf": create fold operator */
4846 case 'F':
4847 case 'f': if (foldManualAllowed(TRUE))
4849 cap->nchar = 'f';
4850 nv_operator(cap);
4851 curwin->w_p_fen = TRUE;
4853 /* "zF" is like "zfzf" */
4854 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4856 nv_operator(cap);
4857 finish_op = TRUE;
4860 else
4861 clearopbeep(cap->oap);
4862 break;
4864 /* "zd": delete fold at cursor */
4865 /* "zD": delete fold at cursor recursively */
4866 case 'd':
4867 case 'D': if (foldManualAllowed(FALSE))
4869 if (VIsual_active)
4870 nv_operator(cap);
4871 else
4872 deleteFold(curwin->w_cursor.lnum,
4873 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4875 break;
4877 /* "zE": erease all folds */
4878 case 'E': if (foldmethodIsManual(curwin))
4880 clearFolding(curwin);
4881 changed_window_setting();
4883 else if (foldmethodIsMarker(curwin))
4884 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4885 TRUE, FALSE);
4886 else
4887 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4888 break;
4890 /* "zn": fold none: reset 'foldenable' */
4891 case 'n': curwin->w_p_fen = FALSE;
4892 break;
4894 /* "zN": fold Normal: set 'foldenable' */
4895 case 'N': curwin->w_p_fen = TRUE;
4896 break;
4898 /* "zi": invert folding: toggle 'foldenable' */
4899 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4900 break;
4902 /* "za": open closed fold or close open fold at cursor */
4903 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4904 openFold(curwin->w_cursor.lnum, cap->count1);
4905 else
4907 closeFold(curwin->w_cursor.lnum, cap->count1);
4908 curwin->w_p_fen = TRUE;
4910 break;
4912 /* "zA": open fold at cursor recursively */
4913 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4914 openFoldRecurse(curwin->w_cursor.lnum);
4915 else
4917 closeFoldRecurse(curwin->w_cursor.lnum);
4918 curwin->w_p_fen = TRUE;
4920 break;
4922 /* "zo": open fold at cursor or Visual area */
4923 case 'o': if (VIsual_active)
4924 nv_operator(cap);
4925 else
4926 openFold(curwin->w_cursor.lnum, cap->count1);
4927 break;
4929 /* "zO": open fold recursively */
4930 case 'O': if (VIsual_active)
4931 nv_operator(cap);
4932 else
4933 openFoldRecurse(curwin->w_cursor.lnum);
4934 break;
4936 /* "zc": close fold at cursor or Visual area */
4937 case 'c': if (VIsual_active)
4938 nv_operator(cap);
4939 else
4940 closeFold(curwin->w_cursor.lnum, cap->count1);
4941 curwin->w_p_fen = TRUE;
4942 break;
4944 /* "zC": close fold recursively */
4945 case 'C': if (VIsual_active)
4946 nv_operator(cap);
4947 else
4948 closeFoldRecurse(curwin->w_cursor.lnum);
4949 curwin->w_p_fen = TRUE;
4950 break;
4952 /* "zv": open folds at the cursor */
4953 case 'v': foldOpenCursor();
4954 break;
4956 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
4957 case 'x': curwin->w_p_fen = TRUE;
4958 newFoldLevel(); /* update right now */
4959 foldOpenCursor();
4960 break;
4962 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
4963 case 'X': curwin->w_p_fen = TRUE;
4964 old_fdl = -1; /* force an update */
4965 break;
4967 /* "zm": fold more */
4968 case 'm': if (curwin->w_p_fdl > 0)
4969 --curwin->w_p_fdl;
4970 old_fdl = -1; /* force an update */
4971 curwin->w_p_fen = TRUE;
4972 break;
4974 /* "zM": close all folds */
4975 case 'M': curwin->w_p_fdl = 0;
4976 old_fdl = -1; /* force an update */
4977 curwin->w_p_fen = TRUE;
4978 break;
4980 /* "zr": reduce folding */
4981 case 'r': ++curwin->w_p_fdl;
4982 break;
4984 /* "zR": open all folds */
4985 case 'R': curwin->w_p_fdl = getDeepestNesting();
4986 old_fdl = -1; /* force an update */
4987 break;
4989 case 'j': /* "zj" move to next fold downwards */
4990 case 'k': /* "zk" move to next fold upwards */
4991 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
4992 cap->count1) == FAIL)
4993 clearopbeep(cap->oap);
4994 break;
4996 #endif /* FEAT_FOLDING */
4998 #ifdef FEAT_SPELL
4999 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5000 ++no_mapping;
5001 ++allow_keys; /* no mapping for nchar, but allow key codes */
5002 nchar = plain_vgetc();
5003 LANGMAP_ADJUST(nchar, TRUE);
5004 --no_mapping;
5005 --allow_keys;
5006 #ifdef FEAT_CMDL_INFO
5007 (void)add_to_showcmd(nchar);
5008 #endif
5009 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5011 clearopbeep(cap->oap);
5012 break;
5014 undo = TRUE;
5015 /*FALLTHROUGH*/
5017 case 'g': /* "zg": add good word to word list */
5018 case 'w': /* "zw": add wrong word to word list */
5019 case 'G': /* "zG": add good word to temp word list */
5020 case 'W': /* "zW": add wrong word to temp word list */
5022 char_u *ptr = NULL;
5023 int len;
5025 if (checkclearop(cap->oap))
5026 break;
5027 # ifdef FEAT_VISUAL
5028 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5029 == FAIL)
5030 return;
5031 # endif
5032 if (ptr == NULL)
5034 pos_T pos = curwin->w_cursor;
5036 /* Find bad word under the cursor. */
5037 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
5038 if (len != 0 && curwin->w_cursor.col <= pos.col)
5039 ptr = ml_get_pos(&curwin->w_cursor);
5040 curwin->w_cursor = pos;
5043 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5044 FIND_IDENT)) == 0)
5045 return;
5046 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
5047 (nchar == 'G' || nchar == 'W')
5048 ? 0 : (int)cap->count1,
5049 undo);
5051 break;
5053 case '=': /* "z=": suggestions for a badly spelled word */
5054 if (!checkclearop(cap->oap))
5055 spell_suggest((int)cap->count0);
5056 break;
5057 #endif
5059 default: clearopbeep(cap->oap);
5062 #ifdef FEAT_FOLDING
5063 /* Redraw when 'foldenable' changed */
5064 if (old_fen != curwin->w_p_fen)
5066 # ifdef FEAT_DIFF
5067 win_T *wp;
5069 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5071 /* Adjust 'foldenable' in diff-synced windows. */
5072 FOR_ALL_WINDOWS(wp)
5074 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5076 wp->w_p_fen = curwin->w_p_fen;
5077 changed_window_setting_win(wp);
5081 # endif
5082 changed_window_setting();
5085 /* Redraw when 'foldlevel' changed. */
5086 if (old_fdl != curwin->w_p_fdl)
5087 newFoldLevel();
5088 #endif
5091 #ifdef FEAT_GUI
5093 * Vertical scrollbar movement.
5095 static void
5096 nv_ver_scrollbar(cap)
5097 cmdarg_T *cap;
5099 if (cap->oap->op_type != OP_NOP)
5100 clearopbeep(cap->oap);
5102 /* Even if an operator was pending, we still want to scroll */
5103 gui_do_scroll();
5107 * Horizontal scrollbar movement.
5109 static void
5110 nv_hor_scrollbar(cap)
5111 cmdarg_T *cap;
5113 if (cap->oap->op_type != OP_NOP)
5114 clearopbeep(cap->oap);
5116 /* Even if an operator was pending, we still want to scroll */
5117 gui_do_horiz_scroll();
5119 #endif
5121 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
5123 * Click in GUI tab.
5125 static void
5126 nv_tabline(cap)
5127 cmdarg_T *cap;
5129 if (cap->oap->op_type != OP_NOP)
5130 clearopbeep(cap->oap);
5132 /* Even if an operator was pending, we still want to jump tabs. */
5133 goto_tabpage(current_tab);
5137 * Selected item in tab line menu.
5139 static void
5140 nv_tabmenu(cap)
5141 cmdarg_T *cap;
5143 if (cap->oap->op_type != OP_NOP)
5144 clearopbeep(cap->oap);
5146 /* Even if an operator was pending, we still want to jump tabs. */
5147 handle_tabmenu();
5151 * Handle selecting an item of the GUI tab line menu.
5152 * Used in Normal and Insert mode.
5154 void
5155 handle_tabmenu()
5157 switch (current_tabmenu)
5159 case TABLINE_MENU_CLOSE:
5160 if (current_tab == 0)
5161 #ifdef FEAT_GUI_MACVIM
5162 do_cmdline_cmd((char_u *)"conf tabclose");
5163 #else
5164 do_cmdline_cmd((char_u *)"tabclose");
5165 #endif
5166 else
5168 #ifdef FEAT_GUI_MACVIM
5169 vim_snprintf((char *)IObuff, IOSIZE, "conf tabclose %d",
5170 current_tab);
5171 #else
5172 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5173 current_tab);
5174 #endif
5175 do_cmdline_cmd(IObuff);
5177 break;
5179 case TABLINE_MENU_NEW:
5180 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5181 current_tab > 0 ? current_tab - 1 : 999);
5182 do_cmdline_cmd(IObuff);
5183 break;
5185 case TABLINE_MENU_OPEN:
5186 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5187 current_tab > 0 ? current_tab - 1 : 999);
5188 do_cmdline_cmd(IObuff);
5189 break;
5192 #endif
5195 * "Q" command.
5197 static void
5198 nv_exmode(cap)
5199 cmdarg_T *cap;
5202 * Ignore 'Q' in Visual mode, just give a beep.
5204 #ifdef FEAT_VISUAL
5205 if (VIsual_active)
5206 vim_beep();
5207 else
5208 #endif
5209 if (!checkclearop(cap->oap))
5210 do_exmode(FALSE);
5214 * Handle a ":" command.
5216 static void
5217 nv_colon(cap)
5218 cmdarg_T *cap;
5220 int old_p_im;
5222 #ifdef FEAT_VISUAL
5223 if (VIsual_active)
5224 nv_operator(cap);
5225 else
5226 #endif
5228 if (cap->oap->op_type != OP_NOP)
5230 /* Using ":" as a movement is characterwise exclusive. */
5231 cap->oap->motion_type = MCHAR;
5232 cap->oap->inclusive = FALSE;
5234 else if (cap->count0)
5236 /* translate "count:" into ":.,.+(count - 1)" */
5237 stuffcharReadbuff('.');
5238 if (cap->count0 > 1)
5240 stuffReadbuff((char_u *)",.+");
5241 stuffnumReadbuff((long)cap->count0 - 1L);
5245 /* When typing, don't type below an old message */
5246 if (KeyTyped)
5247 compute_cmdrow();
5249 old_p_im = p_im;
5251 /* get a command line and execute it */
5252 do_cmdline(NULL, getexline, NULL,
5253 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5255 /* If 'insertmode' changed, enter or exit Insert mode */
5256 if (p_im != old_p_im)
5258 if (p_im)
5259 restart_edit = 'i';
5260 else
5261 restart_edit = 0;
5264 /* The start of the operator may have become invalid by the Ex
5265 * command. */
5266 if (cap->oap->op_type != OP_NOP
5267 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5268 || cap->oap->start.col >
5269 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))))
5270 clearopbeep(cap->oap);
5275 * Handle CTRL-G command.
5277 static void
5278 nv_ctrlg(cap)
5279 cmdarg_T *cap;
5281 #ifdef FEAT_VISUAL
5282 if (VIsual_active) /* toggle Selection/Visual mode */
5284 VIsual_select = !VIsual_select;
5285 showmode();
5287 else
5288 #endif
5289 if (!checkclearop(cap->oap))
5290 /* print full name if count given or :cd used */
5291 fileinfo((int)cap->count0, FALSE, TRUE);
5295 * Handle CTRL-H <Backspace> command.
5297 static void
5298 nv_ctrlh(cap)
5299 cmdarg_T *cap;
5301 #ifdef FEAT_VISUAL
5302 if (VIsual_active && VIsual_select)
5304 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5305 v_visop(cap);
5307 else
5308 #endif
5309 nv_left(cap);
5313 * CTRL-L: clear screen and redraw.
5315 static void
5316 nv_clear(cap)
5317 cmdarg_T *cap;
5319 if (!checkclearop(cap->oap))
5321 #if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5323 * Right now, the BeBox doesn't seem to have an easy way to detect
5324 * window resizing, so we cheat and make the user detect it
5325 * manually with CTRL-L instead
5327 ui_get_shellsize();
5328 #endif
5329 #ifdef FEAT_SYN_HL
5330 /* Clear all syntax states to force resyncing. */
5331 syn_stack_free_all(curbuf);
5332 #endif
5333 redraw_later(CLEAR);
5338 * CTRL-O: In Select mode: switch to Visual mode for one command.
5339 * Otherwise: Go to older pcmark.
5341 static void
5342 nv_ctrlo(cap)
5343 cmdarg_T *cap;
5345 #ifdef FEAT_VISUAL
5346 if (VIsual_active && VIsual_select)
5348 VIsual_select = FALSE;
5349 showmode();
5350 restart_VIsual_select = 2; /* restart Select mode later */
5352 else
5353 #endif
5355 cap->count1 = -cap->count1;
5356 nv_pcmark(cap);
5361 * CTRL-^ command, short for ":e #"
5363 static void
5364 nv_hat(cap)
5365 cmdarg_T *cap;
5367 if (!checkclearopq(cap->oap))
5368 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5369 GETF_SETMARK|GETF_ALT, FALSE);
5373 * "Z" commands.
5375 static void
5376 nv_Zet(cap)
5377 cmdarg_T *cap;
5379 if (!checkclearopq(cap->oap))
5381 switch (cap->nchar)
5383 /* "ZZ": equivalent to ":x". */
5384 case 'Z': do_cmdline_cmd((char_u *)"x");
5385 break;
5387 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5388 case 'Q': do_cmdline_cmd((char_u *)"q!");
5389 break;
5391 default: clearopbeep(cap->oap);
5396 #if defined(FEAT_WINDOWS) || defined(PROTO)
5398 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5400 void
5401 do_nv_ident(c1, c2)
5402 int c1;
5403 int c2;
5405 oparg_T oa;
5406 cmdarg_T ca;
5408 clear_oparg(&oa);
5409 vim_memset(&ca, 0, sizeof(ca));
5410 ca.oap = &oa;
5411 ca.cmdchar = c1;
5412 ca.nchar = c2;
5413 nv_ident(&ca);
5415 #endif
5418 * Handle the commands that use the word under the cursor.
5419 * [g] CTRL-] :ta to current identifier
5420 * [g] 'K' run program for current identifier
5421 * [g] '*' / to current identifier or string
5422 * [g] '#' ? to current identifier or string
5423 * g ']' :tselect for current identifier
5425 static void
5426 nv_ident(cap)
5427 cmdarg_T *cap;
5429 char_u *ptr = NULL;
5430 char_u *buf;
5431 char_u *p;
5432 char_u *kp; /* value of 'keywordprg' */
5433 int kp_help; /* 'keywordprg' is ":help" */
5434 int n = 0; /* init for GCC */
5435 int cmdchar;
5436 int g_cmd; /* "g" command */
5437 char_u *aux_ptr;
5438 int isman;
5439 int isman_s;
5441 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5443 cmdchar = cap->nchar;
5444 g_cmd = TRUE;
5446 else
5448 cmdchar = cap->cmdchar;
5449 g_cmd = FALSE;
5452 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5453 cmdchar = '#';
5456 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5458 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5460 #ifdef FEAT_VISUAL
5461 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5462 return;
5463 #endif
5464 if (checkclearopq(cap->oap))
5465 return;
5468 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5469 (cmdchar == '*' || cmdchar == '#')
5470 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5472 clearop(cap->oap);
5473 return;
5476 /* Allocate buffer to put the command in. Inserting backslashes can
5477 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5478 * and some numbers. */
5479 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5480 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5481 || STRCMP(kp, ":help") == 0);
5482 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5483 if (buf == NULL)
5484 return;
5485 buf[0] = NUL;
5487 switch (cmdchar)
5489 case '*':
5490 case '#':
5492 * Put cursor at start of word, makes search skip the word
5493 * under the cursor.
5494 * Call setpcmark() first, so "*``" puts the cursor back where
5495 * it was.
5497 setpcmark();
5498 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5500 if (!g_cmd && vim_iswordp(ptr))
5501 STRCPY(buf, "\\<");
5502 no_smartcase = TRUE; /* don't use 'smartcase' now */
5503 break;
5505 case 'K':
5506 if (kp_help)
5507 STRCPY(buf, "he! ");
5508 else
5510 /* An external command will probably use an argument starting
5511 * with "-" as an option. To avoid trouble we skip the "-". */
5512 while (*ptr == '-' && n > 0)
5514 ++ptr;
5515 --n;
5517 if (n == 0)
5519 EMSG(_(e_noident)); /* found dashes only */
5520 vim_free(buf);
5521 return;
5524 /* When a count is given, turn it into a range. Is this
5525 * really what we want? */
5526 isman = (STRCMP(kp, "man") == 0);
5527 isman_s = (STRCMP(kp, "man -s") == 0);
5528 if (cap->count0 != 0 && !(isman || isman_s))
5529 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5531 STRCAT(buf, "! ");
5532 if (cap->count0 == 0 && isman_s)
5533 STRCAT(buf, "man");
5534 else
5535 STRCAT(buf, kp);
5536 STRCAT(buf, " ");
5537 if (cap->count0 != 0 && (isman || isman_s))
5539 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5540 STRCAT(buf, " ");
5543 break;
5545 case ']':
5546 #ifdef FEAT_CSCOPE
5547 if (p_cst)
5548 STRCPY(buf, "cstag ");
5549 else
5550 #endif
5551 STRCPY(buf, "ts ");
5552 break;
5554 default:
5555 if (curbuf->b_help)
5556 STRCPY(buf, "he! ");
5557 else if (g_cmd)
5558 STRCPY(buf, "tj ");
5559 else
5560 sprintf((char *)buf, "%ldta ", cap->count0);
5564 * Now grab the chars in the identifier
5566 if (cmdchar == 'K' && !kp_help)
5568 /* Escape the argument properly for a shell command */
5569 ptr = vim_strnsave(ptr, n);
5570 p = vim_strsave_shellescape(ptr, TRUE);
5571 vim_free(ptr);
5572 if (p == NULL)
5574 vim_free(buf);
5575 return;
5577 buf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5578 if (buf == NULL)
5580 vim_free(buf);
5581 vim_free(p);
5582 return;
5584 STRCAT(buf, p);
5585 vim_free(p);
5587 else
5589 if (cmdchar == '*')
5590 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5591 else if (cmdchar == '#')
5592 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
5593 else
5594 /* Don't escape spaces and Tabs in a tag with a backslash */
5595 aux_ptr = (char_u *)"\\|\"\n*?[";
5597 p = buf + STRLEN(buf);
5598 while (n-- > 0)
5600 /* put a backslash before \ and some others */
5601 if (vim_strchr(aux_ptr, *ptr) != NULL)
5602 *p++ = '\\';
5603 #ifdef FEAT_MBYTE
5604 /* When current byte is a part of multibyte character, copy all
5605 * bytes of that character. */
5606 if (has_mbyte)
5608 int i;
5609 int len = (*mb_ptr2len)(ptr) - 1;
5611 for (i = 0; i < len && n >= 1; ++i, --n)
5612 *p++ = *ptr++;
5614 #endif
5615 *p++ = *ptr++;
5617 *p = NUL;
5621 * Execute the command.
5623 if (cmdchar == '*' || cmdchar == '#')
5625 if (!g_cmd && (
5626 #ifdef FEAT_MBYTE
5627 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5628 #endif
5629 vim_iswordc(ptr[-1])))
5630 STRCAT(buf, "\\>");
5631 #ifdef FEAT_CMDHIST
5632 /* put pattern in search history */
5633 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5634 #endif
5635 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5637 else
5638 do_cmdline_cmd(buf);
5640 vim_free(buf);
5643 #if defined(FEAT_VISUAL) || defined(PROTO)
5645 * Get visually selected text, within one line only.
5646 * Returns FAIL if more than one line selected.
5649 get_visual_text(cap, pp, lenp)
5650 cmdarg_T *cap;
5651 char_u **pp; /* return: start of selected text */
5652 int *lenp; /* return: length of selected text */
5654 if (VIsual_mode != 'V')
5655 unadjust_for_sel();
5656 if (VIsual.lnum != curwin->w_cursor.lnum)
5658 if (cap != NULL)
5659 clearopbeep(cap->oap);
5660 return FAIL;
5662 if (VIsual_mode == 'V')
5664 *pp = ml_get_curline();
5665 *lenp = (int)STRLEN(*pp);
5667 else
5669 if (lt(curwin->w_cursor, VIsual))
5671 *pp = ml_get_pos(&curwin->w_cursor);
5672 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5674 else
5676 *pp = ml_get_pos(&VIsual);
5677 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5679 #ifdef FEAT_MBYTE
5680 if (has_mbyte)
5681 /* Correct the length to include the whole last character. */
5682 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
5683 #endif
5685 reset_VIsual_and_resel();
5686 return OK;
5688 #endif
5691 * CTRL-T: backwards in tag stack
5693 static void
5694 nv_tagpop(cap)
5695 cmdarg_T *cap;
5697 if (!checkclearopq(cap->oap))
5698 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5702 * Handle scrolling command 'H', 'L' and 'M'.
5704 static void
5705 nv_scroll(cap)
5706 cmdarg_T *cap;
5708 int used = 0;
5709 long n;
5710 #ifdef FEAT_FOLDING
5711 linenr_T lnum;
5712 #endif
5713 int half;
5715 cap->oap->motion_type = MLINE;
5716 setpcmark();
5718 if (cap->cmdchar == 'L')
5720 validate_botline(); /* make sure curwin->w_botline is valid */
5721 curwin->w_cursor.lnum = curwin->w_botline - 1;
5722 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5723 curwin->w_cursor.lnum = 1;
5724 else
5726 #ifdef FEAT_FOLDING
5727 if (hasAnyFolding(curwin))
5729 /* Count a fold for one screen line. */
5730 for (n = cap->count1 - 1; n > 0
5731 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5733 (void)hasFolding(curwin->w_cursor.lnum,
5734 &curwin->w_cursor.lnum, NULL);
5735 --curwin->w_cursor.lnum;
5738 else
5739 #endif
5740 curwin->w_cursor.lnum -= cap->count1 - 1;
5743 else
5745 if (cap->cmdchar == 'M')
5747 #ifdef FEAT_DIFF
5748 /* Don't count filler lines above the window. */
5749 used -= diff_check_fill(curwin, curwin->w_topline)
5750 - curwin->w_topfill;
5751 #endif
5752 validate_botline(); /* make sure w_empty_rows is valid */
5753 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5754 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5756 #ifdef FEAT_DIFF
5757 /* Count half he number of filler lines to be "below this
5758 * line" and half to be "above the next line". */
5759 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5760 + n) / 2 >= half)
5762 --n;
5763 break;
5765 #endif
5766 used += plines(curwin->w_topline + n);
5767 if (used >= half)
5768 break;
5769 #ifdef FEAT_FOLDING
5770 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5771 n = lnum - curwin->w_topline;
5772 #endif
5774 if (n > 0 && used > curwin->w_height)
5775 --n;
5777 else /* (cap->cmdchar == 'H') */
5779 n = cap->count1 - 1;
5780 #ifdef FEAT_FOLDING
5781 if (hasAnyFolding(curwin))
5783 /* Count a fold for one screen line. */
5784 lnum = curwin->w_topline;
5785 while (n-- > 0 && lnum < curwin->w_botline - 1)
5787 hasFolding(lnum, NULL, &lnum);
5788 ++lnum;
5790 n = lnum - curwin->w_topline;
5792 #endif
5794 curwin->w_cursor.lnum = curwin->w_topline + n;
5795 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5796 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5799 cursor_correct(); /* correct for 'so' */
5800 beginline(BL_SOL | BL_FIX);
5804 * Cursor right commands.
5806 static void
5807 nv_right(cap)
5808 cmdarg_T *cap;
5810 long n;
5811 #ifdef FEAT_VISUAL
5812 int PAST_LINE;
5813 #else
5814 # define PAST_LINE 0
5815 #endif
5817 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5819 /* <C-Right> and <S-Right> move a word or WORD right */
5820 if (mod_mask & MOD_MASK_CTRL)
5821 cap->arg = TRUE;
5822 nv_wordcmd(cap);
5823 return;
5826 cap->oap->motion_type = MCHAR;
5827 cap->oap->inclusive = FALSE;
5828 #ifdef FEAT_VISUAL
5829 PAST_LINE = (VIsual_active && *p_sel != 'o');
5831 # ifdef FEAT_VIRTUALEDIT
5833 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
5834 * (theoretically) infinitely long.
5836 if (virtual_active())
5837 PAST_LINE = 0;
5838 # endif
5839 #endif
5841 for (n = cap->count1; n > 0; --n)
5843 if ((!PAST_LINE && oneright() == FAIL)
5844 #ifdef FEAT_VISUAL
5845 || (PAST_LINE && *ml_get_cursor() == NUL)
5846 #endif
5850 * <Space> wraps to next line if 'whichwrap' has 's'.
5851 * 'l' wraps to next line if 'whichwrap' has 'l'.
5852 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
5854 if ( ((cap->cmdchar == ' '
5855 && vim_strchr(p_ww, 's') != NULL)
5856 || (cap->cmdchar == 'l'
5857 && vim_strchr(p_ww, 'l') != NULL)
5858 || (cap->cmdchar == K_RIGHT
5859 && vim_strchr(p_ww, '>') != NULL))
5860 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5862 /* When deleting we also count the NL as a character.
5863 * Set cap->oap->inclusive when last char in the line is
5864 * included, move to next line after that */
5865 if ( cap->oap->op_type != OP_NOP
5866 && !cap->oap->inclusive
5867 && !lineempty(curwin->w_cursor.lnum))
5868 cap->oap->inclusive = TRUE;
5869 else
5871 ++curwin->w_cursor.lnum;
5872 curwin->w_cursor.col = 0;
5873 #ifdef FEAT_VIRTUALEDIT
5874 curwin->w_cursor.coladd = 0;
5875 #endif
5876 curwin->w_set_curswant = TRUE;
5877 cap->oap->inclusive = FALSE;
5879 continue;
5881 if (cap->oap->op_type == OP_NOP)
5883 /* Only beep and flush if not moved at all */
5884 if (n == cap->count1)
5885 beep_flush();
5887 else
5889 if (!lineempty(curwin->w_cursor.lnum))
5890 cap->oap->inclusive = TRUE;
5892 break;
5894 #ifdef FEAT_VISUAL
5895 else if (PAST_LINE)
5897 curwin->w_set_curswant = TRUE;
5898 # ifdef FEAT_VIRTUALEDIT
5899 if (virtual_active())
5900 oneright();
5901 else
5902 # endif
5904 # ifdef FEAT_MBYTE
5905 if (has_mbyte)
5906 curwin->w_cursor.col +=
5907 (*mb_ptr2len)(ml_get_cursor());
5908 else
5909 # endif
5910 ++curwin->w_cursor.col;
5913 #endif
5915 #ifdef FEAT_FOLDING
5916 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5917 && cap->oap->op_type == OP_NOP)
5918 foldOpenCursor();
5919 #endif
5923 * Cursor left commands.
5925 * Returns TRUE when operator end should not be adjusted.
5927 static void
5928 nv_left(cap)
5929 cmdarg_T *cap;
5931 long n;
5933 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5935 /* <C-Left> and <S-Left> move a word or WORD left */
5936 if (mod_mask & MOD_MASK_CTRL)
5937 cap->arg = 1;
5938 nv_bck_word(cap);
5939 return;
5942 cap->oap->motion_type = MCHAR;
5943 cap->oap->inclusive = FALSE;
5944 for (n = cap->count1; n > 0; --n)
5946 if (oneleft() == FAIL)
5948 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
5949 * 'h' wraps to previous line if 'whichwrap' has 'h'.
5950 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
5952 if ( (((cap->cmdchar == K_BS
5953 || cap->cmdchar == Ctrl_H)
5954 && vim_strchr(p_ww, 'b') != NULL)
5955 || (cap->cmdchar == 'h'
5956 && vim_strchr(p_ww, 'h') != NULL)
5957 || (cap->cmdchar == K_LEFT
5958 && vim_strchr(p_ww, '<') != NULL))
5959 && curwin->w_cursor.lnum > 1)
5961 --(curwin->w_cursor.lnum);
5962 coladvance((colnr_T)MAXCOL);
5963 curwin->w_set_curswant = TRUE;
5965 /* When the NL before the first char has to be deleted we
5966 * put the cursor on the NUL after the previous line.
5967 * This is a very special case, be careful!
5968 * Don't adjust op_end now, otherwise it won't work. */
5969 if ( (cap->oap->op_type == OP_DELETE
5970 || cap->oap->op_type == OP_CHANGE)
5971 && !lineempty(curwin->w_cursor.lnum))
5973 if (*ml_get_cursor() != NUL)
5974 ++curwin->w_cursor.col;
5975 cap->retval |= CA_NO_ADJ_OP_END;
5977 continue;
5979 /* Only beep and flush if not moved at all */
5980 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
5981 beep_flush();
5982 break;
5985 #ifdef FEAT_FOLDING
5986 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5987 && cap->oap->op_type == OP_NOP)
5988 foldOpenCursor();
5989 #endif
5993 * Cursor up commands.
5994 * cap->arg is TRUE for "-": Move cursor to first non-blank.
5996 static void
5997 nv_up(cap)
5998 cmdarg_T *cap;
6000 if (mod_mask & MOD_MASK_SHIFT)
6002 /* <S-Up> is page up */
6003 cap->arg = BACKWARD;
6004 nv_page(cap);
6006 else
6008 cap->oap->motion_type = MLINE;
6009 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6010 clearopbeep(cap->oap);
6011 else if (cap->arg)
6012 beginline(BL_WHITE | BL_FIX);
6017 * Cursor down commands.
6018 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6020 static void
6021 nv_down(cap)
6022 cmdarg_T *cap;
6024 if (mod_mask & MOD_MASK_SHIFT)
6026 /* <S-Down> is page down */
6027 cap->arg = FORWARD;
6028 nv_page(cap);
6030 else
6031 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6032 /* In a quickfix window a <CR> jumps to the error under the cursor. */
6033 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
6034 if (curwin->w_llist_ref == NULL)
6035 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6036 else
6037 do_cmdline_cmd((char_u *)".ll"); /* location list window */
6038 else
6039 #endif
6041 #ifdef FEAT_CMDWIN
6042 /* In the cmdline window a <CR> executes the command. */
6043 if (cmdwin_type != 0 && cap->cmdchar == CAR)
6044 cmdwin_result = CAR;
6045 else
6046 #endif
6048 cap->oap->motion_type = MLINE;
6049 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6050 clearopbeep(cap->oap);
6051 else if (cap->arg)
6052 beginline(BL_WHITE | BL_FIX);
6057 #ifdef FEAT_SEARCHPATH
6059 * Grab the file name under the cursor and edit it.
6061 static void
6062 nv_gotofile(cap)
6063 cmdarg_T *cap;
6065 char_u *ptr;
6066 linenr_T lnum = -1;
6068 if (text_locked())
6070 clearopbeep(cap->oap);
6071 text_locked_msg();
6072 return;
6074 #ifdef FEAT_AUTOCMD
6075 if (curbuf_locked())
6077 clearop(cap->oap);
6078 return;
6080 #endif
6082 ptr = grab_file_name(cap->count1, &lnum);
6084 if (ptr != NULL)
6086 /* do autowrite if necessary */
6087 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6088 autowrite(curbuf, FALSE);
6089 setpcmark();
6090 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
6091 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
6092 if (cap->nchar == 'F' && lnum >= 0)
6094 curwin->w_cursor.lnum = lnum;
6095 check_cursor_lnum();
6096 beginline(BL_SOL | BL_FIX);
6098 vim_free(ptr);
6100 else
6101 clearop(cap->oap);
6103 #endif
6106 * <End> command: to end of current line or last line.
6108 static void
6109 nv_end(cap)
6110 cmdarg_T *cap;
6112 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
6114 cap->arg = TRUE;
6115 nv_goto(cap);
6116 cap->count1 = 1; /* to end of current line */
6118 nv_dollar(cap);
6122 * Handle the "$" command.
6124 static void
6125 nv_dollar(cap)
6126 cmdarg_T *cap;
6128 cap->oap->motion_type = MCHAR;
6129 cap->oap->inclusive = TRUE;
6130 #ifdef FEAT_VIRTUALEDIT
6131 /* In virtual mode when off the edge of a line and an operator
6132 * is pending (whew!) keep the cursor where it is.
6133 * Otherwise, send it to the end of the line. */
6134 if (!virtual_active() || gchar_cursor() != NUL
6135 || cap->oap->op_type == OP_NOP)
6136 #endif
6137 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6138 if (cursor_down((long)(cap->count1 - 1),
6139 cap->oap->op_type == OP_NOP) == FAIL)
6140 clearopbeep(cap->oap);
6141 #ifdef FEAT_FOLDING
6142 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6143 foldOpenCursor();
6144 #endif
6148 * Implementation of '?' and '/' commands.
6149 * If cap->arg is TRUE don't set PC mark.
6151 static void
6152 nv_search(cap)
6153 cmdarg_T *cap;
6155 oparg_T *oap = cap->oap;
6157 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6159 /* Translate "g??" to "g?g?" */
6160 cap->cmdchar = 'g';
6161 cap->nchar = '?';
6162 nv_operator(cap);
6163 return;
6166 #ifdef USE_MIGEMO
6167 cap->searchbuf = getcmdline(cap->cmdchar,
6168 (cap->nchar == 'g' ? -cap->count1 : cap->count1), 0);
6169 #else
6170 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6171 #endif
6173 if (cap->searchbuf == NULL)
6175 clearop(oap);
6176 return;
6179 normal_search(cap, cap->cmdchar, cap->searchbuf,
6180 #ifdef USE_MIGEMO
6181 (cap->nchar == 'g' ? SEARCH_MIGEMO : 0) |
6182 #endif
6183 (cap->arg ? 0 : SEARCH_MARK));
6187 * Handle "N" and "n" commands.
6188 * cap->arg is SEARCH_REV for "N", 0 for "n".
6190 static void
6191 nv_next(cap)
6192 cmdarg_T *cap;
6194 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6198 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6199 * Uses only cap->count1 and cap->oap from "cap".
6201 static void
6202 normal_search(cap, dir, pat, opt)
6203 cmdarg_T *cap;
6204 int dir;
6205 char_u *pat;
6206 int opt; /* extra flags for do_search() */
6208 int i;
6210 cap->oap->motion_type = MCHAR;
6211 cap->oap->inclusive = FALSE;
6212 cap->oap->use_reg_one = TRUE;
6213 curwin->w_set_curswant = TRUE;
6215 i = do_search(cap->oap, dir, pat, cap->count1,
6216 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
6217 if (i == 0)
6218 clearop(cap->oap);
6219 else
6221 if (i == 2)
6222 cap->oap->motion_type = MLINE;
6223 #ifdef FEAT_VIRTUALEDIT
6224 curwin->w_cursor.coladd = 0;
6225 #endif
6226 #ifdef FEAT_FOLDING
6227 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6228 foldOpenCursor();
6229 #endif
6232 /* "/$" will put the cursor after the end of the line, may need to
6233 * correct that here */
6234 check_cursor();
6238 * Character search commands.
6239 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6240 * ',' and FALSE for ';'.
6241 * cap->nchar is NUL for ',' and ';' (repeat the search)
6243 static void
6244 nv_csearch(cap)
6245 cmdarg_T *cap;
6247 int t_cmd;
6249 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6250 t_cmd = TRUE;
6251 else
6252 t_cmd = FALSE;
6254 cap->oap->motion_type = MCHAR;
6255 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6256 clearopbeep(cap->oap);
6257 else
6259 curwin->w_set_curswant = TRUE;
6260 #ifdef FEAT_VIRTUALEDIT
6261 /* Include a Tab for "tx" and for "dfx". */
6262 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6263 && (t_cmd || cap->oap->op_type != OP_NOP))
6265 colnr_T scol, ecol;
6267 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6268 curwin->w_cursor.coladd = ecol - scol;
6270 else
6271 curwin->w_cursor.coladd = 0;
6272 #endif
6273 #ifdef FEAT_VISUAL
6274 adjust_for_sel(cap);
6275 #endif
6276 #ifdef FEAT_FOLDING
6277 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6278 foldOpenCursor();
6279 #endif
6284 * "[" and "]" commands.
6285 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6287 static void
6288 nv_brackets(cap)
6289 cmdarg_T *cap;
6291 pos_T new_pos;
6292 pos_T prev_pos;
6293 pos_T *pos = NULL; /* init for GCC */
6294 pos_T old_pos; /* cursor position before command */
6295 int flag;
6296 long n;
6297 int findc;
6298 int c;
6300 cap->oap->motion_type = MCHAR;
6301 cap->oap->inclusive = FALSE;
6302 old_pos = curwin->w_cursor;
6303 #ifdef FEAT_VIRTUALEDIT
6304 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6305 #endif
6307 #ifdef FEAT_SEARCHPATH
6309 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6311 if (cap->nchar == 'f')
6312 nv_gotofile(cap);
6313 else
6314 #endif
6316 #ifdef FEAT_FIND_ID
6318 * Find the occurrence(s) of the identifier or define under cursor
6319 * in current and included files or jump to the first occurrence.
6321 * search list jump
6322 * fwd bwd fwd bwd fwd bwd
6323 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6324 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6326 if (vim_strchr((char_u *)
6327 #ifdef EBCDIC
6328 "iI\005dD\067",
6329 #else
6330 "iI\011dD\004",
6331 #endif
6332 cap->nchar) != NULL)
6334 char_u *ptr;
6335 int len;
6337 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6338 clearop(cap->oap);
6339 else
6341 find_pattern_in_path(ptr, 0, len, TRUE,
6342 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6343 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6344 cap->count1,
6345 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6346 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6347 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6348 (linenr_T)MAXLNUM);
6349 curwin->w_set_curswant = TRUE;
6352 else
6353 #endif
6356 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6357 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6358 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6359 * "[m" or "]m" search for prev/next start of (Java) method.
6360 * "[M" or "]M" search for prev/next end of (Java) method.
6362 if ( (cap->cmdchar == '['
6363 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6364 || (cap->cmdchar == ']'
6365 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6367 if (cap->nchar == '*')
6368 cap->nchar = '/';
6369 new_pos.lnum = 0;
6370 prev_pos.lnum = 0;
6371 if (cap->nchar == 'm' || cap->nchar == 'M')
6373 if (cap->cmdchar == '[')
6374 findc = '{';
6375 else
6376 findc = '}';
6377 n = 9999;
6379 else
6381 findc = cap->nchar;
6382 n = cap->count1;
6384 for ( ; n > 0; --n)
6386 if ((pos = findmatchlimit(cap->oap, findc,
6387 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6389 if (new_pos.lnum == 0) /* nothing found */
6391 if (cap->nchar != 'm' && cap->nchar != 'M')
6392 clearopbeep(cap->oap);
6394 else
6395 pos = &new_pos; /* use last one found */
6396 break;
6398 prev_pos = new_pos;
6399 curwin->w_cursor = *pos;
6400 new_pos = *pos;
6402 curwin->w_cursor = old_pos;
6405 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6406 * brought us to the match for "[m" and "]M" when inside a method.
6407 * Try finding the '{' or '}' we want to be at.
6408 * Also repeat for the given count.
6410 if (cap->nchar == 'm' || cap->nchar == 'M')
6412 /* norm is TRUE for "]M" and "[m" */
6413 int norm = ((findc == '{') == (cap->nchar == 'm'));
6415 n = cap->count1;
6416 /* found a match: we were inside a method */
6417 if (prev_pos.lnum != 0)
6419 pos = &prev_pos;
6420 curwin->w_cursor = prev_pos;
6421 if (norm)
6422 --n;
6424 else
6425 pos = NULL;
6426 while (n > 0)
6428 for (;;)
6430 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6432 /* if not found anything, that's an error */
6433 if (pos == NULL)
6434 clearopbeep(cap->oap);
6435 n = 0;
6436 break;
6438 c = gchar_cursor();
6439 if (c == '{' || c == '}')
6441 /* Must have found end/start of class: use it.
6442 * Or found the place to be at. */
6443 if ((c == findc && norm) || (n == 1 && !norm))
6445 new_pos = curwin->w_cursor;
6446 pos = &new_pos;
6447 n = 0;
6449 /* if no match found at all, we started outside of the
6450 * class and we're inside now. Just go on. */
6451 else if (new_pos.lnum == 0)
6453 new_pos = curwin->w_cursor;
6454 pos = &new_pos;
6456 /* found start/end of other method: go to match */
6457 else if ((pos = findmatchlimit(cap->oap, findc,
6458 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6459 0)) == NULL)
6460 n = 0;
6461 else
6462 curwin->w_cursor = *pos;
6463 break;
6466 --n;
6468 curwin->w_cursor = old_pos;
6469 if (pos == NULL && new_pos.lnum != 0)
6470 clearopbeep(cap->oap);
6472 if (pos != NULL)
6474 setpcmark();
6475 curwin->w_cursor = *pos;
6476 curwin->w_set_curswant = TRUE;
6477 #ifdef FEAT_FOLDING
6478 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6479 && cap->oap->op_type == OP_NOP)
6480 foldOpenCursor();
6481 #endif
6486 * "[[", "[]", "]]" and "][": move to start or end of function
6488 else if (cap->nchar == '[' || cap->nchar == ']')
6490 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6491 flag = '{';
6492 else
6493 flag = '}'; /* "][" or "[]" */
6495 curwin->w_set_curswant = TRUE;
6497 * Imitate strange Vi behaviour: When using "]]" with an operator
6498 * we also stop at '}'.
6500 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
6501 (cap->oap->op_type != OP_NOP
6502 && cap->arg == FORWARD && flag == '{')))
6503 clearopbeep(cap->oap);
6504 else
6506 if (cap->oap->op_type == OP_NOP)
6507 beginline(BL_WHITE | BL_FIX);
6508 #ifdef FEAT_FOLDING
6509 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6510 foldOpenCursor();
6511 #endif
6516 * "[p", "[P", "]P" and "]p": put with indent adjustment
6518 else if (cap->nchar == 'p' || cap->nchar == 'P')
6520 if (!checkclearop(cap->oap))
6522 prep_redo_cmd(cap);
6523 do_put(cap->oap->regname,
6524 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6525 cap->count1, PUT_FIXINDENT);
6530 * "['", "[`", "]'" and "]`": jump to next mark
6532 else if (cap->nchar == '\'' || cap->nchar == '`')
6534 pos = &curwin->w_cursor;
6535 for (n = cap->count1; n > 0; --n)
6537 prev_pos = *pos;
6538 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6539 cap->nchar == '\'');
6540 if (pos == NULL)
6541 break;
6543 if (pos == NULL)
6544 pos = &prev_pos;
6545 nv_cursormark(cap, cap->nchar == '\'', pos);
6548 #ifdef FEAT_MOUSE
6550 * [ or ] followed by a middle mouse click: put selected text with
6551 * indent adjustment. Any other button just does as usual.
6553 else if (cap->nchar >= K_LEFTMOUSE && cap->nchar <= K_RIGHTRELEASE)
6555 (void)do_mouse(cap->oap, cap->nchar,
6556 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6557 cap->count1, PUT_FIXINDENT);
6559 #endif /* FEAT_MOUSE */
6561 #ifdef FEAT_FOLDING
6563 * "[z" and "]z": move to start or end of open fold.
6565 else if (cap->nchar == 'z')
6567 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6568 cap->count1) == FAIL)
6569 clearopbeep(cap->oap);
6571 #endif
6573 #ifdef FEAT_DIFF
6575 * "[c" and "]c": move to next or previous diff-change.
6577 else if (cap->nchar == 'c')
6579 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6580 cap->count1) == FAIL)
6581 clearopbeep(cap->oap);
6583 #endif
6585 #ifdef FEAT_SPELL
6587 * "[s", "[S", "]s" and "]S": move to next spell error.
6589 else if (cap->nchar == 's' || cap->nchar == 'S')
6591 setpcmark();
6592 for (n = 0; n < cap->count1; ++n)
6593 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6594 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
6596 clearopbeep(cap->oap);
6597 break;
6599 # ifdef FEAT_FOLDING
6600 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6601 foldOpenCursor();
6602 # endif
6604 #endif
6606 /* Not a valid cap->nchar. */
6607 else
6608 clearopbeep(cap->oap);
6612 * Handle Normal mode "%" command.
6614 static void
6615 nv_percent(cap)
6616 cmdarg_T *cap;
6618 pos_T *pos;
6619 #ifdef FEAT_FOLDING
6620 linenr_T lnum = curwin->w_cursor.lnum;
6621 #endif
6623 cap->oap->inclusive = TRUE;
6624 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6626 if (cap->count0 > 100)
6627 clearopbeep(cap->oap);
6628 else
6630 cap->oap->motion_type = MLINE;
6631 setpcmark();
6632 /* Round up, so CTRL-G will give same value. Watch out for a
6633 * large line count, the line number must not go negative! */
6634 if (curbuf->b_ml.ml_line_count > 1000000)
6635 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6636 / 100L * cap->count0;
6637 else
6638 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6639 cap->count0 + 99L) / 100L;
6640 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6641 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6642 beginline(BL_SOL | BL_FIX);
6645 else /* "%" : go to matching paren */
6647 cap->oap->motion_type = MCHAR;
6648 cap->oap->use_reg_one = TRUE;
6649 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6650 clearopbeep(cap->oap);
6651 else
6653 setpcmark();
6654 curwin->w_cursor = *pos;
6655 curwin->w_set_curswant = TRUE;
6656 #ifdef FEAT_VIRTUALEDIT
6657 curwin->w_cursor.coladd = 0;
6658 #endif
6659 #ifdef FEAT_VISUAL
6660 adjust_for_sel(cap);
6661 #endif
6664 #ifdef FEAT_FOLDING
6665 if (cap->oap->op_type == OP_NOP
6666 && lnum != curwin->w_cursor.lnum
6667 && (fdo_flags & FDO_PERCENT)
6668 && KeyTyped)
6669 foldOpenCursor();
6670 #endif
6674 * Handle "(" and ")" commands.
6675 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6677 static void
6678 nv_brace(cap)
6679 cmdarg_T *cap;
6681 cap->oap->motion_type = MCHAR;
6682 cap->oap->use_reg_one = TRUE;
6683 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6684 cap->oap->inclusive = FALSE;
6685 curwin->w_set_curswant = TRUE;
6687 if (findsent(cap->arg, cap->count1) == FAIL)
6688 clearopbeep(cap->oap);
6689 else
6691 /* Don't leave the cursor on the NUL past end of line. */
6692 adjust_cursor(cap->oap);
6693 #ifdef FEAT_VIRTUALEDIT
6694 curwin->w_cursor.coladd = 0;
6695 #endif
6696 #ifdef FEAT_FOLDING
6697 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6698 foldOpenCursor();
6699 #endif
6704 * "m" command: Mark a position.
6706 static void
6707 nv_mark(cap)
6708 cmdarg_T *cap;
6710 if (!checkclearop(cap->oap))
6712 if (setmark(cap->nchar) == FAIL)
6713 clearopbeep(cap->oap);
6718 * "{" and "}" commands.
6719 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6721 static void
6722 nv_findpar(cap)
6723 cmdarg_T *cap;
6725 cap->oap->motion_type = MCHAR;
6726 cap->oap->inclusive = FALSE;
6727 cap->oap->use_reg_one = TRUE;
6728 curwin->w_set_curswant = TRUE;
6729 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
6730 clearopbeep(cap->oap);
6731 else
6733 #ifdef FEAT_VIRTUALEDIT
6734 curwin->w_cursor.coladd = 0;
6735 #endif
6736 #ifdef FEAT_FOLDING
6737 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6738 foldOpenCursor();
6739 #endif
6744 * "u" command: Undo or make lower case.
6746 static void
6747 nv_undo(cap)
6748 cmdarg_T *cap;
6750 if (cap->oap->op_type == OP_LOWER
6751 #ifdef FEAT_VISUAL
6752 || VIsual_active
6753 #endif
6756 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6757 cap->cmdchar = 'g';
6758 cap->nchar = 'u';
6759 nv_operator(cap);
6761 else
6762 nv_kundo(cap);
6766 * <Undo> command.
6768 static void
6769 nv_kundo(cap)
6770 cmdarg_T *cap;
6772 if (!checkclearopq(cap->oap))
6774 u_undo((int)cap->count1);
6775 curwin->w_set_curswant = TRUE;
6780 * Handle the "r" command.
6782 static void
6783 nv_replace(cap)
6784 cmdarg_T *cap;
6786 char_u *ptr;
6787 int had_ctrl_v;
6788 long n;
6790 if (checkclearop(cap->oap))
6791 return;
6793 /* get another character */
6794 if (cap->nchar == Ctrl_V)
6796 had_ctrl_v = Ctrl_V;
6797 cap->nchar = get_literal();
6798 /* Don't redo a multibyte character with CTRL-V. */
6799 if (cap->nchar > DEL)
6800 had_ctrl_v = NUL;
6802 else
6803 had_ctrl_v = NUL;
6805 /* Abort if the character is a special key. */
6806 if (IS_SPECIAL(cap->nchar))
6808 clearopbeep(cap->oap);
6809 return;
6812 #ifdef FEAT_VISUAL
6813 /* Visual mode "r" */
6814 if (VIsual_active)
6816 if (got_int)
6817 reset_VIsual();
6818 nv_operator(cap);
6819 return;
6821 #endif
6823 #ifdef FEAT_VIRTUALEDIT
6824 /* Break tabs, etc. */
6825 if (virtual_active())
6827 if (u_save_cursor() == FAIL)
6828 return;
6829 if (gchar_cursor() == NUL)
6831 /* Add extra space and put the cursor on the first one. */
6832 coladvance_force((colnr_T)(getviscol() + cap->count1));
6833 curwin->w_cursor.col -= cap->count1;
6835 else if (gchar_cursor() == TAB)
6836 coladvance_force(getviscol());
6838 #endif
6840 /* Abort if not enough characters to replace. */
6841 ptr = ml_get_cursor();
6842 if (STRLEN(ptr) < (unsigned)cap->count1
6843 #ifdef FEAT_MBYTE
6844 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6845 #endif
6848 clearopbeep(cap->oap);
6849 return;
6853 * Replacing with a TAB is done by edit() when it is complicated because
6854 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6855 * Other characters are done below to avoid problems with things like
6856 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6858 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6860 stuffnumReadbuff(cap->count1);
6861 stuffcharReadbuff('R');
6862 stuffcharReadbuff('\t');
6863 stuffcharReadbuff(ESC);
6864 return;
6867 /* save line for undo */
6868 if (u_save_cursor() == FAIL)
6869 return;
6871 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6874 * Replace character(s) by a single newline.
6875 * Strange vi behaviour: Only one newline is inserted.
6876 * Delete the characters here.
6877 * Insert the newline with an insert command, takes care of
6878 * autoindent. The insert command depends on being on the last
6879 * character of a line or not.
6881 #ifdef FEAT_MBYTE
6882 (void)del_chars(cap->count1, FALSE); /* delete the characters */
6883 #else
6884 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
6885 #endif
6886 stuffcharReadbuff('\r');
6887 stuffcharReadbuff(ESC);
6889 /* Give 'r' to edit(), to get the redo command right. */
6890 invoke_edit(cap, TRUE, 'r', FALSE);
6892 else
6894 prep_redo(cap->oap->regname, cap->count1,
6895 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
6897 curbuf->b_op_start = curwin->w_cursor;
6898 #ifdef FEAT_MBYTE
6899 if (has_mbyte)
6901 int old_State = State;
6903 if (cap->ncharC1 != 0)
6904 AppendCharToRedobuff(cap->ncharC1);
6905 if (cap->ncharC2 != 0)
6906 AppendCharToRedobuff(cap->ncharC2);
6908 /* This is slow, but it handles replacing a single-byte with a
6909 * multi-byte and the other way around. Also handles adding
6910 * composing characters for utf-8. */
6911 for (n = cap->count1; n > 0; --n)
6913 State = REPLACE;
6914 ins_char(cap->nchar);
6915 State = old_State;
6916 if (cap->ncharC1 != 0)
6917 ins_char(cap->ncharC1);
6918 if (cap->ncharC2 != 0)
6919 ins_char(cap->ncharC2);
6922 else
6923 #endif
6926 * Replace the characters within one line.
6928 for (n = cap->count1; n > 0; --n)
6931 * Get ptr again, because u_save and/or showmatch() will have
6932 * released the line. At the same time we let know that the
6933 * line will be changed.
6935 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
6936 ptr[curwin->w_cursor.col] = cap->nchar;
6937 if (p_sm && msg_silent == 0)
6938 showmatch(cap->nchar);
6939 ++curwin->w_cursor.col;
6941 #ifdef FEAT_NETBEANS_INTG
6942 if (usingNetbeans)
6944 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
6946 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
6947 (long)cap->count1);
6948 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
6949 &ptr[start], (int)cap->count1);
6951 #endif
6953 /* mark the buffer as changed and prepare for displaying */
6954 changed_bytes(curwin->w_cursor.lnum,
6955 (colnr_T)(curwin->w_cursor.col - cap->count1));
6957 --curwin->w_cursor.col; /* cursor on the last replaced char */
6958 #ifdef FEAT_MBYTE
6959 /* if the character on the left of the current cursor is a multi-byte
6960 * character, move two characters left */
6961 if (has_mbyte)
6962 mb_adjust_cursor();
6963 #endif
6964 curbuf->b_op_end = curwin->w_cursor;
6965 curwin->w_set_curswant = TRUE;
6966 set_last_insert(cap->nchar);
6970 #ifdef FEAT_VISUAL
6972 * 'o': Exchange start and end of Visual area.
6973 * 'O': same, but in block mode exchange left and right corners.
6975 static void
6976 v_swap_corners(cmdchar)
6977 int cmdchar;
6979 pos_T old_cursor;
6980 colnr_T left, right;
6982 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
6984 old_cursor = curwin->w_cursor;
6985 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
6986 curwin->w_cursor.lnum = VIsual.lnum;
6987 coladvance(left);
6988 VIsual = curwin->w_cursor;
6990 curwin->w_cursor.lnum = old_cursor.lnum;
6991 curwin->w_curswant = right;
6992 /* 'selection "exclusive" and cursor at right-bottom corner: move it
6993 * right one column */
6994 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
6995 ++curwin->w_curswant;
6996 coladvance(curwin->w_curswant);
6997 if (curwin->w_cursor.col == old_cursor.col
6998 #ifdef FEAT_VIRTUALEDIT
6999 && (!virtual_active()
7000 || curwin->w_cursor.coladd == old_cursor.coladd)
7001 #endif
7004 curwin->w_cursor.lnum = VIsual.lnum;
7005 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7006 ++right;
7007 coladvance(right);
7008 VIsual = curwin->w_cursor;
7010 curwin->w_cursor.lnum = old_cursor.lnum;
7011 coladvance(left);
7012 curwin->w_curswant = left;
7015 else
7017 old_cursor = curwin->w_cursor;
7018 curwin->w_cursor = VIsual;
7019 VIsual = old_cursor;
7020 curwin->w_set_curswant = TRUE;
7023 #endif /* FEAT_VISUAL */
7026 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7028 static void
7029 nv_Replace(cap)
7030 cmdarg_T *cap;
7032 #ifdef FEAT_VISUAL
7033 if (VIsual_active) /* "R" is replace lines */
7035 cap->cmdchar = 'c';
7036 cap->nchar = NUL;
7037 VIsual_mode = 'V';
7038 nv_operator(cap);
7040 else
7041 #endif
7042 if (!checkclearopq(cap->oap))
7044 if (!curbuf->b_p_ma)
7045 EMSG(_(e_modifiable));
7046 else
7048 #ifdef FEAT_VIRTUALEDIT
7049 if (virtual_active())
7050 coladvance(getviscol());
7051 #endif
7052 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7057 #ifdef FEAT_VREPLACE
7059 * "gr".
7061 static void
7062 nv_vreplace(cap)
7063 cmdarg_T *cap;
7065 # ifdef FEAT_VISUAL
7066 if (VIsual_active)
7068 cap->cmdchar = 'r';
7069 cap->nchar = cap->extra_char;
7070 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7072 else
7073 # endif
7074 if (!checkclearopq(cap->oap))
7076 if (!curbuf->b_p_ma)
7077 EMSG(_(e_modifiable));
7078 else
7080 if (cap->extra_char == Ctrl_V) /* get another character */
7081 cap->extra_char = get_literal();
7082 stuffcharReadbuff(cap->extra_char);
7083 stuffcharReadbuff(ESC);
7084 # ifdef FEAT_VIRTUALEDIT
7085 if (virtual_active())
7086 coladvance(getviscol());
7087 # endif
7088 invoke_edit(cap, TRUE, 'v', FALSE);
7092 #endif
7095 * Swap case for "~" command, when it does not work like an operator.
7097 static void
7098 n_swapchar(cap)
7099 cmdarg_T *cap;
7101 long n;
7102 pos_T startpos;
7103 int did_change = 0;
7104 #ifdef FEAT_NETBEANS_INTG
7105 pos_T pos;
7106 char_u *ptr;
7107 int count;
7108 #endif
7110 if (checkclearopq(cap->oap))
7111 return;
7113 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7115 clearopbeep(cap->oap);
7116 return;
7119 prep_redo_cmd(cap);
7121 if (u_save_cursor() == FAIL)
7122 return;
7124 startpos = curwin->w_cursor;
7125 #ifdef FEAT_NETBEANS_INTG
7126 pos = startpos;
7127 #endif
7128 for (n = cap->count1; n > 0; --n)
7130 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7131 inc_cursor();
7132 if (gchar_cursor() == NUL)
7134 if (vim_strchr(p_ww, '~') != NULL
7135 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7137 #ifdef FEAT_NETBEANS_INTG
7138 if (usingNetbeans)
7140 if (did_change)
7142 ptr = ml_get(pos.lnum);
7143 count = (int)STRLEN(ptr) - pos.col;
7144 netbeans_removed(curbuf, pos.lnum, pos.col,
7145 (long)count);
7146 netbeans_inserted(curbuf, pos.lnum, pos.col,
7147 &ptr[pos.col], count);
7149 pos.col = 0;
7150 pos.lnum++;
7152 #endif
7153 ++curwin->w_cursor.lnum;
7154 curwin->w_cursor.col = 0;
7155 if (n > 1)
7157 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7158 break;
7159 u_clearline();
7162 else
7163 break;
7166 #ifdef FEAT_NETBEANS_INTG
7167 if (did_change && usingNetbeans)
7169 ptr = ml_get(pos.lnum);
7170 count = curwin->w_cursor.col - pos.col;
7171 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7172 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
7174 #endif
7177 check_cursor();
7178 curwin->w_set_curswant = TRUE;
7179 if (did_change)
7181 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7182 0L);
7183 curbuf->b_op_start = startpos;
7184 curbuf->b_op_end = curwin->w_cursor;
7185 if (curbuf->b_op_end.col > 0)
7186 --curbuf->b_op_end.col;
7191 * Move cursor to mark.
7193 static void
7194 nv_cursormark(cap, flag, pos)
7195 cmdarg_T *cap;
7196 int flag;
7197 pos_T *pos;
7199 if (check_mark(pos) == FAIL)
7200 clearop(cap->oap);
7201 else
7203 if (cap->cmdchar == '\''
7204 || cap->cmdchar == '`'
7205 || cap->cmdchar == '['
7206 || cap->cmdchar == ']')
7207 setpcmark();
7208 curwin->w_cursor = *pos;
7209 if (flag)
7210 beginline(BL_WHITE | BL_FIX);
7211 else
7212 check_cursor();
7214 cap->oap->motion_type = flag ? MLINE : MCHAR;
7215 if (cap->cmdchar == '`')
7216 cap->oap->use_reg_one = TRUE;
7217 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7218 curwin->w_set_curswant = TRUE;
7221 #ifdef FEAT_VISUAL
7223 * Handle commands that are operators in Visual mode.
7225 static void
7226 v_visop(cap)
7227 cmdarg_T *cap;
7229 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7231 /* Uppercase means linewise, except in block mode, then "D" deletes till
7232 * the end of the line, and "C" replaces til EOL */
7233 if (isupper(cap->cmdchar))
7235 if (VIsual_mode != Ctrl_V)
7236 VIsual_mode = 'V';
7237 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7238 curwin->w_curswant = MAXCOL;
7240 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7241 nv_operator(cap);
7243 #endif
7246 * "s" and "S" commands.
7248 static void
7249 nv_subst(cap)
7250 cmdarg_T *cap;
7252 #ifdef FEAT_VISUAL
7253 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7255 if (cap->cmdchar == 'S')
7256 VIsual_mode = 'V';
7257 cap->cmdchar = 'c';
7258 nv_operator(cap);
7260 else
7261 #endif
7262 nv_optrans(cap);
7266 * Abbreviated commands.
7268 static void
7269 nv_abbrev(cap)
7270 cmdarg_T *cap;
7272 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7273 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7275 #ifdef FEAT_VISUAL
7276 /* in Visual mode these commands are operators */
7277 if (VIsual_active)
7278 v_visop(cap);
7279 else
7280 #endif
7281 nv_optrans(cap);
7285 * Translate a command into another command.
7287 static void
7288 nv_optrans(cap)
7289 cmdarg_T *cap;
7291 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7292 (char_u *)"d$", (char_u *)"c$",
7293 (char_u *)"cl", (char_u *)"cc",
7294 (char_u *)"yy", (char_u *)":s\r"};
7295 static char_u *str = (char_u *)"xXDCsSY&";
7297 if (!checkclearopq(cap->oap))
7299 /* In Vi "2D" doesn't delete the next line. Can't translate it
7300 * either, because "2." should also not use the count. */
7301 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7303 cap->oap->start = curwin->w_cursor;
7304 cap->oap->op_type = OP_DELETE;
7305 #ifdef FEAT_EVAL
7306 set_op_var(OP_DELETE);
7307 #endif
7308 cap->count1 = 1;
7309 nv_dollar(cap);
7310 finish_op = TRUE;
7311 ResetRedobuff();
7312 AppendCharToRedobuff('D');
7314 else
7316 if (cap->count0)
7317 stuffnumReadbuff(cap->count0);
7318 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7321 cap->opcount = 0;
7325 * "'" and "`" commands. Also for "g'" and "g`".
7326 * cap->arg is TRUE for "'" and "g'".
7328 static void
7329 nv_gomark(cap)
7330 cmdarg_T *cap;
7332 pos_T *pos;
7333 int c;
7334 #ifdef FEAT_FOLDING
7335 linenr_T lnum = curwin->w_cursor.lnum;
7336 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7337 #endif
7339 if (cap->cmdchar == 'g')
7340 c = cap->extra_char;
7341 else
7342 c = cap->nchar;
7343 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7344 if (pos == (pos_T *)-1) /* jumped to other file */
7346 if (cap->arg)
7348 check_cursor_lnum();
7349 beginline(BL_WHITE | BL_FIX);
7351 else
7352 check_cursor();
7354 else
7355 nv_cursormark(cap, cap->arg, pos);
7357 #ifdef FEAT_VIRTUALEDIT
7358 /* May need to clear the coladd that a mark includes. */
7359 if (!virtual_active())
7360 curwin->w_cursor.coladd = 0;
7361 #endif
7362 #ifdef FEAT_FOLDING
7363 if (cap->oap->op_type == OP_NOP
7364 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7365 && (fdo_flags & FDO_MARK)
7366 && old_KeyTyped)
7367 foldOpenCursor();
7368 #endif
7372 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7374 static void
7375 nv_pcmark(cap)
7376 cmdarg_T *cap;
7378 #ifdef FEAT_JUMPLIST
7379 pos_T *pos;
7380 # ifdef FEAT_FOLDING
7381 linenr_T lnum = curwin->w_cursor.lnum;
7382 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7383 # endif
7385 if (!checkclearopq(cap->oap))
7387 if (cap->cmdchar == 'g')
7388 pos = movechangelist((int)cap->count1);
7389 else
7390 pos = movemark((int)cap->count1);
7391 if (pos == (pos_T *)-1) /* jump to other file */
7393 curwin->w_set_curswant = TRUE;
7394 check_cursor();
7396 else if (pos != NULL) /* can jump */
7397 nv_cursormark(cap, FALSE, pos);
7398 else if (cap->cmdchar == 'g')
7400 if (curbuf->b_changelistlen == 0)
7401 EMSG(_("E664: changelist is empty"));
7402 else if (cap->count1 < 0)
7403 EMSG(_("E662: At start of changelist"));
7404 else
7405 EMSG(_("E663: At end of changelist"));
7407 else
7408 clearopbeep(cap->oap);
7409 # ifdef FEAT_FOLDING
7410 if (cap->oap->op_type == OP_NOP
7411 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7412 && (fdo_flags & FDO_MARK)
7413 && old_KeyTyped)
7414 foldOpenCursor();
7415 # endif
7417 #else
7418 clearopbeep(cap->oap);
7419 #endif
7423 * Handle '"' command.
7425 static void
7426 nv_regname(cap)
7427 cmdarg_T *cap;
7429 if (checkclearop(cap->oap))
7430 return;
7431 #ifdef FEAT_EVAL
7432 if (cap->nchar == '=')
7433 cap->nchar = get_expr_register();
7434 #endif
7435 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7437 cap->oap->regname = cap->nchar;
7438 cap->opcount = cap->count0; /* remember count before '"' */
7439 #ifdef FEAT_EVAL
7440 set_reg_var(cap->oap->regname);
7441 #endif
7443 else
7444 clearopbeep(cap->oap);
7447 #ifdef FEAT_VISUAL
7449 * Handle "v", "V" and "CTRL-V" commands.
7450 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7451 * is TRUE.
7452 * Handle CTRL-Q just like CTRL-V.
7454 static void
7455 nv_visual(cap)
7456 cmdarg_T *cap;
7458 if (cap->cmdchar == Ctrl_Q)
7459 cap->cmdchar = Ctrl_V;
7461 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7462 * characterwise, linewise, or blockwise. */
7463 if (cap->oap->op_type != OP_NOP)
7465 cap->oap->motion_force = cap->cmdchar;
7466 finish_op = FALSE; /* operator doesn't finish now but later */
7467 return;
7470 VIsual_select = cap->arg;
7471 if (VIsual_active) /* change Visual mode */
7473 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7474 end_visual_mode();
7475 else /* toggle char/block mode */
7476 { /* or char/line mode */
7477 VIsual_mode = cap->cmdchar;
7478 showmode();
7480 redraw_curbuf_later(INVERTED); /* update the inversion */
7482 else /* start Visual mode */
7484 check_visual_highlight();
7485 if (cap->count0) /* use previously selected part */
7487 if (resel_VIsual_mode == NUL) /* there is none */
7489 beep_flush();
7490 return;
7492 VIsual = curwin->w_cursor;
7494 VIsual_active = TRUE;
7495 VIsual_reselect = TRUE;
7496 if (!cap->arg)
7497 /* start Select mode when 'selectmode' contains "cmd" */
7498 may_start_select('c');
7499 #ifdef FEAT_MOUSE
7500 setmouse();
7501 #endif
7502 if (p_smd && msg_silent == 0)
7503 redraw_cmdline = TRUE; /* show visual mode later */
7505 * For V and ^V, we multiply the number of lines even if there
7506 * was only one -- webb
7508 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7510 curwin->w_cursor.lnum +=
7511 resel_VIsual_line_count * cap->count0 - 1;
7512 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7513 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7515 VIsual_mode = resel_VIsual_mode;
7516 if (VIsual_mode == 'v')
7518 if (resel_VIsual_line_count <= 1)
7519 curwin->w_cursor.col += resel_VIsual_col * cap->count0 - 1;
7520 else
7521 curwin->w_cursor.col = resel_VIsual_col;
7522 check_cursor_col();
7524 if (resel_VIsual_col == MAXCOL)
7526 curwin->w_curswant = MAXCOL;
7527 coladvance((colnr_T)MAXCOL);
7529 else if (VIsual_mode == Ctrl_V)
7531 validate_virtcol();
7532 curwin->w_curswant = curwin->w_virtcol
7533 + resel_VIsual_col * cap->count0 - 1;
7534 coladvance(curwin->w_curswant);
7536 else
7537 curwin->w_set_curswant = TRUE;
7538 redraw_curbuf_later(INVERTED); /* show the inversion */
7540 else
7542 if (!cap->arg)
7543 /* start Select mode when 'selectmode' contains "cmd" */
7544 may_start_select('c');
7545 n_start_visual_mode(cap->cmdchar);
7551 * Start selection for Shift-movement keys.
7553 void
7554 start_selection()
7556 /* if 'selectmode' contains "key", start Select mode */
7557 may_start_select('k');
7558 n_start_visual_mode('v');
7562 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7564 void
7565 may_start_select(c)
7566 int c;
7568 VIsual_select = (stuff_empty() && typebuf_typed()
7569 && (vim_strchr(p_slm, c) != NULL));
7573 * Start Visual mode "c".
7574 * Should set VIsual_select before calling this.
7576 static void
7577 n_start_visual_mode(c)
7578 int c;
7580 VIsual_mode = c;
7581 VIsual_active = TRUE;
7582 VIsual_reselect = TRUE;
7583 #ifdef FEAT_VIRTUALEDIT
7584 /* Corner case: the 0 position in a tab may change when going into
7585 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7587 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7588 coladvance(curwin->w_virtcol);
7589 #endif
7590 VIsual = curwin->w_cursor;
7592 #ifdef FEAT_FOLDING
7593 foldAdjustVisual();
7594 #endif
7596 #ifdef FEAT_MOUSE
7597 setmouse();
7598 #endif
7599 if (p_smd && msg_silent == 0)
7600 redraw_cmdline = TRUE; /* show visual mode later */
7601 #ifdef FEAT_CLIPBOARD
7602 /* Make sure the clipboard gets updated. Needed because start and
7603 * end may still be the same, and the selection needs to be owned */
7604 clip_star.vmode = NUL;
7605 #endif
7607 /* Only need to redraw this line, unless still need to redraw an old
7608 * Visual area (when 'lazyredraw' is set). */
7609 if (curwin->w_redr_type < INVERTED)
7611 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7612 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7616 #endif /* FEAT_VISUAL */
7619 * CTRL-W: Window commands
7621 static void
7622 nv_window(cap)
7623 cmdarg_T *cap;
7625 #ifdef FEAT_WINDOWS
7626 if (!checkclearop(cap->oap))
7627 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7628 #else
7629 (void)checkclearop(cap->oap);
7630 #endif
7634 * CTRL-Z: Suspend
7636 static void
7637 nv_suspend(cap)
7638 cmdarg_T *cap;
7640 clearop(cap->oap);
7641 #ifdef FEAT_VISUAL
7642 if (VIsual_active)
7643 end_visual_mode(); /* stop Visual mode */
7644 #endif
7645 do_cmdline_cmd((char_u *)"st");
7649 * Commands starting with "g".
7651 static void
7652 nv_g_cmd(cap)
7653 cmdarg_T *cap;
7655 oparg_T *oap = cap->oap;
7656 #ifdef FEAT_VISUAL
7657 pos_T tpos;
7658 #endif
7659 int i;
7660 int flag = FALSE;
7662 switch (cap->nchar)
7664 #ifdef MEM_PROFILE
7666 * "g^A": dump log of used memory.
7668 case Ctrl_A:
7669 vim_mem_profile_dump();
7670 break;
7671 #endif
7673 #ifdef FEAT_VREPLACE
7675 * "gR": Enter virtual replace mode.
7677 case 'R':
7678 cap->arg = TRUE;
7679 nv_Replace(cap);
7680 break;
7682 case 'r':
7683 nv_vreplace(cap);
7684 break;
7685 #endif
7687 case '&':
7688 do_cmdline_cmd((char_u *)"%s//~/&");
7689 break;
7691 #ifdef FEAT_VISUAL
7693 * "gv": Reselect the previous Visual area. If Visual already active,
7694 * exchange previous and current Visual area.
7696 case 'v':
7697 if (checkclearop(oap))
7698 break;
7700 if ( curbuf->b_visual.vi_start.lnum == 0
7701 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7702 || curbuf->b_visual.vi_end.lnum == 0)
7703 beep_flush();
7704 else
7706 /* set w_cursor to the start of the Visual area, tpos to the end */
7707 if (VIsual_active)
7709 i = VIsual_mode;
7710 VIsual_mode = curbuf->b_visual.vi_mode;
7711 curbuf->b_visual.vi_mode = i;
7712 # ifdef FEAT_EVAL
7713 curbuf->b_visual_mode_eval = i;
7714 # endif
7715 i = curwin->w_curswant;
7716 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7717 curbuf->b_visual.vi_curswant = i;
7719 tpos = curbuf->b_visual.vi_end;
7720 curbuf->b_visual.vi_end = curwin->w_cursor;
7721 curwin->w_cursor = curbuf->b_visual.vi_start;
7722 curbuf->b_visual.vi_start = VIsual;
7724 else
7726 VIsual_mode = curbuf->b_visual.vi_mode;
7727 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7728 tpos = curbuf->b_visual.vi_end;
7729 curwin->w_cursor = curbuf->b_visual.vi_start;
7732 VIsual_active = TRUE;
7733 VIsual_reselect = TRUE;
7735 /* Set Visual to the start and w_cursor to the end of the Visual
7736 * area. Make sure they are on an existing character. */
7737 check_cursor();
7738 VIsual = curwin->w_cursor;
7739 curwin->w_cursor = tpos;
7740 check_cursor();
7741 update_topline();
7743 * When called from normal "g" command: start Select mode when
7744 * 'selectmode' contains "cmd". When called for K_SELECT, always
7745 * start Select mode.
7747 if (cap->arg)
7748 VIsual_select = TRUE;
7749 else
7750 may_start_select('c');
7751 #ifdef FEAT_MOUSE
7752 setmouse();
7753 #endif
7754 #ifdef FEAT_CLIPBOARD
7755 /* Make sure the clipboard gets updated. Needed because start and
7756 * end are still the same, and the selection needs to be owned */
7757 clip_star.vmode = NUL;
7758 #endif
7759 redraw_curbuf_later(INVERTED);
7760 showmode();
7762 break;
7764 * "gV": Don't reselect the previous Visual area after a Select mode
7765 * mapping of menu.
7767 case 'V':
7768 VIsual_reselect = FALSE;
7769 break;
7772 * "gh": start Select mode.
7773 * "gH": start Select line mode.
7774 * "g^H": start Select block mode.
7776 case K_BS:
7777 cap->nchar = Ctrl_H;
7778 /* FALLTHROUGH */
7779 case 'h':
7780 case 'H':
7781 case Ctrl_H:
7782 # ifdef EBCDIC
7783 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7784 if (cap->nchar == Ctrl_H)
7785 cap->cmdchar = Ctrl_V;
7786 else
7787 # endif
7788 cap->cmdchar = cap->nchar + ('v' - 'h');
7789 cap->arg = TRUE;
7790 nv_visual(cap);
7791 break;
7792 #endif /* FEAT_VISUAL */
7795 * "gj" and "gk" two new funny movement keys -- up and down
7796 * movement based on *screen* line rather than *file* line.
7798 case 'j':
7799 case K_DOWN:
7800 /* with 'nowrap' it works just like the normal "j" command; also when
7801 * in a closed fold */
7802 if (!curwin->w_p_wrap
7803 #ifdef FEAT_FOLDING
7804 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7805 #endif
7808 oap->motion_type = MLINE;
7809 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7811 else
7812 i = nv_screengo(oap, FORWARD, cap->count1);
7813 if (i == FAIL)
7814 clearopbeep(oap);
7815 break;
7817 case 'k':
7818 case K_UP:
7819 /* with 'nowrap' it works just like the normal "k" command; also when
7820 * in a closed fold */
7821 if (!curwin->w_p_wrap
7822 #ifdef FEAT_FOLDING
7823 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7824 #endif
7827 oap->motion_type = MLINE;
7828 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7830 else
7831 i = nv_screengo(oap, BACKWARD, cap->count1);
7832 if (i == FAIL)
7833 clearopbeep(oap);
7834 break;
7837 * "gJ": join two lines without inserting a space.
7839 case 'J':
7840 nv_join(cap);
7841 break;
7844 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7845 * "gm": middle of "g0" and "g$".
7847 case '^':
7848 flag = TRUE;
7849 /* FALLTHROUGH */
7851 case '0':
7852 case 'm':
7853 case K_HOME:
7854 case K_KHOME:
7855 oap->motion_type = MCHAR;
7856 oap->inclusive = FALSE;
7857 if (curwin->w_p_wrap
7858 #ifdef FEAT_VERTSPLIT
7859 && curwin->w_width != 0
7860 #endif
7863 int width1 = W_WIDTH(curwin) - curwin_col_off();
7864 int width2 = width1 + curwin_col_off2();
7866 validate_virtcol();
7867 i = 0;
7868 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
7869 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
7871 else
7872 i = curwin->w_leftcol;
7873 /* Go to the middle of the screen line. When 'number' is on and lines
7874 * are wrapping the middle can be more to the left. */
7875 if (cap->nchar == 'm')
7876 i += (W_WIDTH(curwin) - curwin_col_off()
7877 + ((curwin->w_p_wrap && i > 0)
7878 ? curwin_col_off2() : 0)) / 2;
7879 coladvance((colnr_T)i);
7880 if (flag)
7883 i = gchar_cursor();
7884 while (vim_iswhite(i) && oneright() == OK);
7886 curwin->w_set_curswant = TRUE;
7887 break;
7889 case '_':
7890 /* "g_": to the last non-blank character in the line or <count> lines
7891 * downward. */
7892 cap->oap->motion_type = MCHAR;
7893 cap->oap->inclusive = TRUE;
7894 curwin->w_curswant = MAXCOL;
7895 if (cursor_down((long)(cap->count1 - 1),
7896 cap->oap->op_type == OP_NOP) == FAIL)
7897 clearopbeep(cap->oap);
7898 else
7900 char_u *ptr = ml_get_curline();
7902 /* In Visual mode we may end up after the line. */
7903 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
7904 --curwin->w_cursor.col;
7906 /* Decrease the cursor column until it's on a non-blank. */
7907 while (curwin->w_cursor.col > 0
7908 && vim_iswhite(ptr[curwin->w_cursor.col]))
7909 --curwin->w_cursor.col;
7910 curwin->w_set_curswant = TRUE;
7912 break;
7914 case '$':
7915 case K_END:
7916 case K_KEND:
7918 int col_off = curwin_col_off();
7920 oap->motion_type = MCHAR;
7921 oap->inclusive = TRUE;
7922 if (curwin->w_p_wrap
7923 #ifdef FEAT_VERTSPLIT
7924 && curwin->w_width != 0
7925 #endif
7928 curwin->w_curswant = MAXCOL; /* so we stay at the end */
7929 if (cap->count1 == 1)
7931 int width1 = W_WIDTH(curwin) - col_off;
7932 int width2 = width1 + curwin_col_off2();
7934 validate_virtcol();
7935 i = width1 - 1;
7936 if (curwin->w_virtcol >= (colnr_T)width1)
7937 i += ((curwin->w_virtcol - width1) / width2 + 1)
7938 * width2;
7939 coladvance((colnr_T)i);
7940 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
7941 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
7944 * Check for landing on a character that got split at
7945 * the end of the line. We do not want to advance to
7946 * the next screen line.
7948 validate_virtcol();
7949 if (curwin->w_virtcol > (colnr_T)i)
7950 --curwin->w_cursor.col;
7952 #endif
7954 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
7955 clearopbeep(oap);
7957 else
7959 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
7960 coladvance((colnr_T)i);
7962 /* Make sure we stick in this column. */
7963 validate_virtcol();
7964 curwin->w_curswant = curwin->w_virtcol;
7965 curwin->w_set_curswant = FALSE;
7968 break;
7971 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
7973 case '*':
7974 case '#':
7975 #if POUND != '#'
7976 case POUND: /* pound sign (sometimes equal to '#') */
7977 #endif
7978 case Ctrl_RSB: /* :tag or :tselect for current identifier */
7979 case ']': /* :tselect for current identifier */
7980 nv_ident(cap);
7981 break;
7984 * ge and gE: go back to end of word
7986 case 'e':
7987 case 'E':
7988 oap->motion_type = MCHAR;
7989 curwin->w_set_curswant = TRUE;
7990 oap->inclusive = TRUE;
7991 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
7992 clearopbeep(oap);
7993 break;
7996 * "g CTRL-G": display info about cursor position
7998 case Ctrl_G:
7999 cursor_pos_info();
8000 break;
8003 * "gi": start Insert at the last position.
8005 case 'i':
8006 if (curbuf->b_last_insert.lnum != 0)
8008 curwin->w_cursor = curbuf->b_last_insert;
8009 check_cursor_lnum();
8010 i = (int)STRLEN(ml_get_curline());
8011 if (curwin->w_cursor.col > (colnr_T)i)
8013 #ifdef FEAT_VIRTUALEDIT
8014 if (virtual_active())
8015 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8016 #endif
8017 curwin->w_cursor.col = i;
8020 cap->cmdchar = 'i';
8021 nv_edit(cap);
8022 break;
8025 * "gI": Start insert in column 1.
8027 case 'I':
8028 beginline(0);
8029 if (!checkclearopq(oap))
8030 invoke_edit(cap, FALSE, 'g', FALSE);
8031 break;
8033 #ifdef FEAT_SEARCHPATH
8035 * "gf": goto file, edit file under cursor
8036 * "]f" and "[f": can also be used.
8038 case 'f':
8039 case 'F':
8040 nv_gotofile(cap);
8041 break;
8042 #endif
8044 /* "g'm" and "g`m": jump to mark without setting pcmark */
8045 case '\'':
8046 cap->arg = TRUE;
8047 /*FALLTHROUGH*/
8048 case '`':
8049 nv_gomark(cap);
8050 break;
8053 * "gs": Goto sleep.
8055 case 's':
8056 do_sleep(cap->count1 * 1000L);
8057 break;
8060 * "ga": Display the ascii value of the character under the
8061 * cursor. It is displayed in decimal, hex, and octal. -- webb
8063 case 'a':
8064 do_ascii(NULL);
8065 break;
8067 #ifdef FEAT_MBYTE
8069 * "g8": Display the bytes used for the UTF-8 character under the
8070 * cursor. It is displayed in hex.
8071 * "8g8" finds illegal byte sequence.
8073 case '8':
8074 if (cap->count0 == 8)
8075 utf_find_illegal();
8076 else
8077 show_utf8();
8078 break;
8079 #endif
8081 case '<':
8082 show_sb_text();
8083 break;
8086 * "gg": Goto the first line in file. With a count it goes to
8087 * that line number like for "G". -- webb
8089 case 'g':
8090 cap->arg = FALSE;
8091 nv_goto(cap);
8092 break;
8095 * Two-character operators:
8096 * "gq" Format text
8097 * "gw" Format text and keep cursor position
8098 * "g~" Toggle the case of the text.
8099 * "gu" Change text to lower case.
8100 * "gU" Change text to upper case.
8101 * "g?" rot13 encoding
8102 * "g@" call 'operatorfunc'
8104 case 'q':
8105 case 'w':
8106 oap->cursor_start = curwin->w_cursor;
8107 /*FALLTHROUGH*/
8108 case '~':
8109 case 'u':
8110 case 'U':
8111 #ifndef USE_MIGEMO
8112 case '?':
8113 #endif
8114 case '@':
8115 nv_operator(cap);
8116 break;
8119 * "gd": Find first occurrence of pattern under the cursor in the
8120 * current function
8121 * "gD": idem, but in the current file.
8123 case 'd':
8124 case 'D':
8125 nv_gd(oap, cap->nchar, (int)cap->count0);
8126 break;
8128 #ifdef FEAT_MOUSE
8130 * g<*Mouse> : <C-*mouse>
8132 case K_MIDDLEMOUSE:
8133 case K_MIDDLEDRAG:
8134 case K_MIDDLERELEASE:
8135 case K_LEFTMOUSE:
8136 case K_LEFTDRAG:
8137 case K_LEFTRELEASE:
8138 case K_RIGHTMOUSE:
8139 case K_RIGHTDRAG:
8140 case K_RIGHTRELEASE:
8141 case K_X1MOUSE:
8142 case K_X1DRAG:
8143 case K_X1RELEASE:
8144 case K_X2MOUSE:
8145 case K_X2DRAG:
8146 case K_X2RELEASE:
8147 mod_mask = MOD_MASK_CTRL;
8148 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8149 break;
8150 #endif
8152 case K_IGNORE:
8153 break;
8156 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8158 case 'p':
8159 case 'P':
8160 nv_put(cap);
8161 break;
8163 #ifdef FEAT_BYTEOFF
8164 /* "go": goto byte count from start of buffer */
8165 case 'o':
8166 goto_byte(cap->count0);
8167 break;
8168 #endif
8170 /* "gQ": improved Ex mode */
8171 case 'Q':
8172 if (text_locked())
8174 clearopbeep(cap->oap);
8175 text_locked_msg();
8176 break;
8179 if (!checkclearopq(oap))
8180 do_exmode(TRUE);
8181 break;
8183 #ifdef FEAT_JUMPLIST
8184 case ',':
8185 nv_pcmark(cap);
8186 break;
8188 case ';':
8189 cap->count1 = -cap->count1;
8190 nv_pcmark(cap);
8191 break;
8192 #endif
8194 #ifdef FEAT_WINDOWS
8195 case 't':
8196 goto_tabpage((int)cap->count0);
8197 break;
8198 case 'T':
8199 goto_tabpage(-(int)cap->count1);
8200 break;
8201 #endif
8203 case '+':
8204 case '-': /* "g+" and "g-": undo or redo along the timeline */
8205 if (!checkclearopq(oap))
8206 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
8207 FALSE, FALSE);
8208 break;
8210 #ifdef USE_MIGEMO
8211 case '/':
8212 cap->cmdchar = '/';
8213 cap->nchar = 'g';
8214 nv_search(cap);
8215 break;
8217 case '?':
8218 if (curbuf->b_p_migemo)
8220 cap->cmdchar = '?';
8221 cap->nchar = 'g';
8222 nv_search(cap);
8224 else
8225 nv_operator(cap);
8226 break;
8227 #endif
8229 default:
8230 clearopbeep(oap);
8231 break;
8236 * Handle "o" and "O" commands.
8238 static void
8239 n_opencmd(cap)
8240 cmdarg_T *cap;
8242 if (!checkclearopq(cap->oap))
8244 #ifdef FEAT_FOLDING
8245 if (cap->cmdchar == 'O')
8246 /* Open above the first line of a folded sequence of lines */
8247 (void)hasFolding(curwin->w_cursor.lnum,
8248 &curwin->w_cursor.lnum, NULL);
8249 else
8250 /* Open below the last line of a folded sequence of lines */
8251 (void)hasFolding(curwin->w_cursor.lnum,
8252 NULL, &curwin->w_cursor.lnum);
8253 #endif
8254 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8255 (cap->cmdchar == 'O' ? 1 : 0)),
8256 (linenr_T)(curwin->w_cursor.lnum +
8257 (cap->cmdchar == 'o' ? 1 : 0))
8258 ) == OK
8259 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8260 #ifdef FEAT_COMMENTS
8261 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8262 #endif
8263 0, 0))
8265 /* When '#' is in 'cpoptions' ignore the count. */
8266 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8267 cap->count1 = 1;
8268 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8274 * "." command: redo last change.
8276 static void
8277 nv_dot(cap)
8278 cmdarg_T *cap;
8280 if (!checkclearopq(cap->oap))
8283 * If "restart_edit" is TRUE, the last but one command is repeated
8284 * instead of the last command (inserting text). This is used for
8285 * CTRL-O <.> in insert mode.
8287 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8288 clearopbeep(cap->oap);
8293 * CTRL-R: undo undo
8295 static void
8296 nv_redo(cap)
8297 cmdarg_T *cap;
8299 if (!checkclearopq(cap->oap))
8301 u_redo((int)cap->count1);
8302 curwin->w_set_curswant = TRUE;
8307 * Handle "U" command.
8309 static void
8310 nv_Undo(cap)
8311 cmdarg_T *cap;
8313 /* In Visual mode and typing "gUU" triggers an operator */
8314 if (cap->oap->op_type == OP_UPPER
8315 #ifdef FEAT_VISUAL
8316 || VIsual_active
8317 #endif
8320 /* translate "gUU" to "gUgU" */
8321 cap->cmdchar = 'g';
8322 cap->nchar = 'U';
8323 nv_operator(cap);
8325 else if (!checkclearopq(cap->oap))
8327 u_undoline();
8328 curwin->w_set_curswant = TRUE;
8333 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8334 * single character.
8336 static void
8337 nv_tilde(cap)
8338 cmdarg_T *cap;
8340 if (!p_to
8341 #ifdef FEAT_VISUAL
8342 && !VIsual_active
8343 #endif
8344 && cap->oap->op_type != OP_TILDE)
8345 n_swapchar(cap);
8346 else
8347 nv_operator(cap);
8351 * Handle an operator command.
8352 * The actual work is done by do_pending_operator().
8354 static void
8355 nv_operator(cap)
8356 cmdarg_T *cap;
8358 int op_type;
8360 op_type = get_op_type(cap->cmdchar, cap->nchar);
8362 if (op_type == cap->oap->op_type) /* double operator works on lines */
8363 nv_lineop(cap);
8364 else if (!checkclearop(cap->oap))
8366 cap->oap->start = curwin->w_cursor;
8367 cap->oap->op_type = op_type;
8368 #ifdef FEAT_EVAL
8369 set_op_var(op_type);
8370 #endif
8374 #ifdef FEAT_EVAL
8376 * Set v:operator to the characters for "optype".
8378 static void
8379 set_op_var(optype)
8380 int optype;
8382 char_u opchars[3];
8384 if (optype == OP_NOP)
8385 set_vim_var_string(VV_OP, NULL, 0);
8386 else
8388 opchars[0] = get_op_char(optype);
8389 opchars[1] = get_extra_op_char(optype);
8390 opchars[2] = NUL;
8391 set_vim_var_string(VV_OP, opchars, -1);
8394 #endif
8397 * Handle linewise operator "dd", "yy", etc.
8399 * "_" is is a strange motion command that helps make operators more logical.
8400 * It is actually implemented, but not documented in the real Vi. This motion
8401 * command actually refers to "the current line". Commands like "dd" and "yy"
8402 * are really an alternate form of "d_" and "y_". It does accept a count, so
8403 * "d3_" works to delete 3 lines.
8405 static void
8406 nv_lineop(cap)
8407 cmdarg_T *cap;
8409 cap->oap->motion_type = MLINE;
8410 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8411 clearopbeep(cap->oap);
8412 else if ( cap->oap->op_type == OP_DELETE
8413 || cap->oap->op_type == OP_LSHIFT
8414 || cap->oap->op_type == OP_RSHIFT)
8415 beginline(BL_SOL | BL_FIX);
8416 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8417 beginline(BL_WHITE | BL_FIX);
8421 * <Home> command.
8423 static void
8424 nv_home(cap)
8425 cmdarg_T *cap;
8427 /* CTRL-HOME is like "gg" */
8428 if (mod_mask & MOD_MASK_CTRL)
8429 nv_goto(cap);
8430 else
8432 cap->count0 = 1;
8433 nv_pipe(cap);
8435 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8436 one-character line). */
8440 * "|" command.
8442 static void
8443 nv_pipe(cap)
8444 cmdarg_T *cap;
8446 cap->oap->motion_type = MCHAR;
8447 cap->oap->inclusive = FALSE;
8448 beginline(0);
8449 if (cap->count0 > 0)
8451 coladvance((colnr_T)(cap->count0 - 1));
8452 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8454 else
8455 curwin->w_curswant = 0;
8456 /* keep curswant at the column where we wanted to go, not where
8457 we ended; differs if line is too short */
8458 curwin->w_set_curswant = FALSE;
8462 * Handle back-word command "b" and "B".
8463 * cap->arg is 1 for "B"
8465 static void
8466 nv_bck_word(cap)
8467 cmdarg_T *cap;
8469 cap->oap->motion_type = MCHAR;
8470 cap->oap->inclusive = FALSE;
8471 curwin->w_set_curswant = TRUE;
8472 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8473 clearopbeep(cap->oap);
8474 #ifdef FEAT_FOLDING
8475 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8476 foldOpenCursor();
8477 #endif
8481 * Handle word motion commands "e", "E", "w" and "W".
8482 * cap->arg is TRUE for "E" and "W".
8484 static void
8485 nv_wordcmd(cap)
8486 cmdarg_T *cap;
8488 int n;
8489 int word_end;
8490 int flag = FALSE;
8491 pos_T startpos = curwin->w_cursor;
8494 * Set inclusive for the "E" and "e" command.
8496 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8497 word_end = TRUE;
8498 else
8499 word_end = FALSE;
8500 cap->oap->inclusive = word_end;
8503 * "cw" and "cW" are a special case.
8505 if (!word_end && cap->oap->op_type == OP_CHANGE)
8507 n = gchar_cursor();
8508 if (n != NUL) /* not an empty line */
8510 if (vim_iswhite(n))
8513 * Reproduce a funny Vi behaviour: "cw" on a blank only
8514 * changes one character, not all blanks until the start of
8515 * the next word. Only do this when the 'w' flag is included
8516 * in 'cpoptions'.
8518 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8520 cap->oap->inclusive = TRUE;
8521 cap->oap->motion_type = MCHAR;
8522 return;
8525 else
8528 * This is a little strange. To match what the real Vi does,
8529 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8530 * that we are not on a space or a TAB. This seems impolite
8531 * at first, but it's really more what we mean when we say
8532 * 'cw'.
8533 * Another strangeness: When standing on the end of a word
8534 * "ce" will change until the end of the next wordt, but "cw"
8535 * will change only one character! This is done by setting
8536 * flag.
8538 cap->oap->inclusive = TRUE;
8539 word_end = TRUE;
8540 flag = TRUE;
8545 cap->oap->motion_type = MCHAR;
8546 curwin->w_set_curswant = TRUE;
8547 if (word_end)
8548 n = end_word(cap->count1, cap->arg, flag, FALSE);
8549 else
8550 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8552 /* Don't leave the cursor on the NUL past the end of line. Unless we
8553 * didn't move it forward. */
8554 if (lt(startpos, curwin->w_cursor))
8555 adjust_cursor(cap->oap);
8557 if (n == FAIL && cap->oap->op_type == OP_NOP)
8558 clearopbeep(cap->oap);
8559 else
8561 #ifdef FEAT_VISUAL
8562 adjust_for_sel(cap);
8563 #endif
8564 #ifdef FEAT_FOLDING
8565 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8566 foldOpenCursor();
8567 #endif
8572 * Used after a movement command: If the cursor ends up on the NUL after the
8573 * end of the line, may move it back to the last character and make the motion
8574 * inclusive.
8576 static void
8577 adjust_cursor(oap)
8578 oparg_T *oap;
8580 /* The cursor cannot remain on the NUL when:
8581 * - the column is > 0
8582 * - not in Visual mode or 'selection' is "o"
8583 * - 'virtualedit' is not "all" and not "onemore".
8585 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
8586 #ifdef FEAT_VISUAL
8587 && (!VIsual_active || *p_sel == 'o')
8588 #endif
8589 #ifdef FEAT_VIRTUALEDIT
8590 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8591 #endif
8594 --curwin->w_cursor.col;
8595 #ifdef FEAT_MBYTE
8596 /* prevent cursor from moving on the trail byte */
8597 if (has_mbyte)
8598 mb_adjust_cursor();
8599 #endif
8600 oap->inclusive = TRUE;
8605 * "0" and "^" commands.
8606 * cap->arg is the argument for beginline().
8608 static void
8609 nv_beginline(cap)
8610 cmdarg_T *cap;
8612 cap->oap->motion_type = MCHAR;
8613 cap->oap->inclusive = FALSE;
8614 beginline(cap->arg);
8615 #ifdef FEAT_FOLDING
8616 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8617 foldOpenCursor();
8618 #endif
8619 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8620 one-character line). */
8623 #ifdef FEAT_VISUAL
8625 * In exclusive Visual mode, may include the last character.
8627 static void
8628 adjust_for_sel(cap)
8629 cmdarg_T *cap;
8631 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8632 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8634 # ifdef FEAT_MBYTE
8635 if (has_mbyte)
8636 inc_cursor();
8637 else
8638 # endif
8639 ++curwin->w_cursor.col;
8640 cap->oap->inclusive = FALSE;
8645 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8646 * Should check VIsual_mode before calling this.
8647 * Returns TRUE when backed up to the previous line.
8649 static int
8650 unadjust_for_sel()
8652 pos_T *pp;
8654 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8656 if (lt(VIsual, curwin->w_cursor))
8657 pp = &curwin->w_cursor;
8658 else
8659 pp = &VIsual;
8660 #ifdef FEAT_VIRTUALEDIT
8661 if (pp->coladd > 0)
8662 --pp->coladd;
8663 else
8664 #endif
8665 if (pp->col > 0)
8667 --pp->col;
8668 #ifdef FEAT_MBYTE
8669 mb_adjustpos(pp);
8670 #endif
8672 else if (pp->lnum > 1)
8674 --pp->lnum;
8675 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8676 return TRUE;
8679 return FALSE;
8683 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8685 static void
8686 nv_select(cap)
8687 cmdarg_T *cap;
8689 if (VIsual_active)
8690 VIsual_select = TRUE;
8691 else if (VIsual_reselect)
8693 cap->nchar = 'v'; /* fake "gv" command */
8694 cap->arg = TRUE;
8695 nv_g_cmd(cap);
8699 #endif
8702 * "G", "gg", CTRL-END, CTRL-HOME.
8703 * cap->arg is TRUE for "G".
8705 static void
8706 nv_goto(cap)
8707 cmdarg_T *cap;
8709 linenr_T lnum;
8711 if (cap->arg)
8712 lnum = curbuf->b_ml.ml_line_count;
8713 else
8714 lnum = 1L;
8715 cap->oap->motion_type = MLINE;
8716 setpcmark();
8718 /* When a count is given, use it instead of the default lnum */
8719 if (cap->count0 != 0)
8720 lnum = cap->count0;
8721 if (lnum < 1L)
8722 lnum = 1L;
8723 else if (lnum > curbuf->b_ml.ml_line_count)
8724 lnum = curbuf->b_ml.ml_line_count;
8725 curwin->w_cursor.lnum = lnum;
8726 beginline(BL_SOL | BL_FIX);
8727 #ifdef FEAT_FOLDING
8728 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8729 foldOpenCursor();
8730 #endif
8734 * CTRL-\ in Normal mode.
8736 static void
8737 nv_normal(cap)
8738 cmdarg_T *cap;
8740 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8742 clearop(cap->oap);
8743 if (restart_edit != 0 && mode_displayed)
8744 clear_cmdline = TRUE; /* unshow mode later */
8745 restart_edit = 0;
8746 #ifdef FEAT_CMDWIN
8747 if (cmdwin_type != 0)
8748 cmdwin_result = Ctrl_C;
8749 #endif
8750 #ifdef FEAT_VISUAL
8751 if (VIsual_active)
8753 end_visual_mode(); /* stop Visual */
8754 redraw_curbuf_later(INVERTED);
8756 #endif
8757 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8758 if (cap->nchar == Ctrl_G && p_im)
8759 restart_edit = 'a';
8761 else
8762 clearopbeep(cap->oap);
8766 * ESC in Normal mode: beep, but don't flush buffers.
8767 * Don't even beep if we are canceling a command.
8769 static void
8770 nv_esc(cap)
8771 cmdarg_T *cap;
8773 int no_reason;
8775 no_reason = (cap->oap->op_type == OP_NOP
8776 && cap->opcount == 0
8777 && cap->count0 == 0
8778 && cap->oap->regname == 0
8779 && !p_im);
8781 if (cap->arg) /* TRUE for CTRL-C */
8783 if (restart_edit == 0
8784 #ifdef FEAT_CMDWIN
8785 && cmdwin_type == 0
8786 #endif
8787 #ifdef FEAT_VISUAL
8788 && !VIsual_active
8789 #endif
8790 && no_reason)
8791 MSG(_("Type :quit<Enter> to exit Vim"));
8793 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8794 * set again below when halfway a mapping. */
8795 if (!p_im)
8796 restart_edit = 0;
8797 #ifdef FEAT_CMDWIN
8798 if (cmdwin_type != 0)
8800 cmdwin_result = K_IGNORE;
8801 got_int = FALSE; /* don't stop executing autocommands et al. */
8802 return;
8804 #endif
8807 #ifdef FEAT_VISUAL
8808 if (VIsual_active)
8810 end_visual_mode(); /* stop Visual */
8811 check_cursor_col(); /* make sure cursor is not beyond EOL */
8812 curwin->w_set_curswant = TRUE;
8813 redraw_curbuf_later(INVERTED);
8815 else
8816 #endif
8817 if (no_reason)
8818 vim_beep();
8819 clearop(cap->oap);
8821 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8822 * set return to Insert mode afterwards. */
8823 if (restart_edit == 0 && goto_im()
8824 #ifdef FEAT_EX_EXTRA
8825 && ex_normal_busy == 0
8826 #endif
8828 restart_edit = 'a';
8832 * Handle "A", "a", "I", "i" and <Insert> commands.
8834 static void
8835 nv_edit(cap)
8836 cmdarg_T *cap;
8838 /* <Insert> is equal to "i" */
8839 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8840 cap->cmdchar = 'i';
8842 #ifdef FEAT_VISUAL
8843 /* in Visual mode "A" and "I" are an operator */
8844 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8845 v_visop(cap);
8847 /* in Visual mode and after an operator "a" and "i" are for text objects */
8848 else
8849 #endif
8850 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8851 && (cap->oap->op_type != OP_NOP
8852 #ifdef FEAT_VISUAL
8853 || VIsual_active
8854 #endif
8857 #ifdef FEAT_TEXTOBJ
8858 nv_object(cap);
8859 #else
8860 clearopbeep(cap->oap);
8861 #endif
8863 else if (!curbuf->b_p_ma && !p_im)
8865 /* Only give this error when 'insertmode' is off. */
8866 EMSG(_(e_modifiable));
8867 clearop(cap->oap);
8869 else if (!checkclearopq(cap->oap))
8871 switch (cap->cmdchar)
8873 case 'A': /* "A"ppend after the line */
8874 curwin->w_set_curswant = TRUE;
8875 #ifdef FEAT_VIRTUALEDIT
8876 if (ve_flags == VE_ALL)
8878 int save_State = State;
8880 /* Pretent Insert mode here to allow the cursor on the
8881 * character past the end of the line */
8882 State = INSERT;
8883 coladvance((colnr_T)MAXCOL);
8884 State = save_State;
8886 else
8887 #endif
8888 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8889 break;
8891 case 'I': /* "I"nsert before the first non-blank */
8892 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
8893 beginline(BL_WHITE);
8894 else
8895 beginline(BL_WHITE|BL_FIX);
8896 break;
8898 case 'a': /* "a"ppend is like "i"nsert on the next character. */
8899 #ifdef FEAT_VIRTUALEDIT
8900 /* increment coladd when in virtual space, increment the
8901 * column otherwise, also to append after an unprintable char */
8902 if (virtual_active()
8903 && (curwin->w_cursor.coladd > 0
8904 || *ml_get_cursor() == NUL
8905 || *ml_get_cursor() == TAB))
8906 curwin->w_cursor.coladd++;
8907 else
8908 #endif
8909 if (*ml_get_cursor() != NUL)
8910 inc_cursor();
8911 break;
8914 #ifdef FEAT_VIRTUALEDIT
8915 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
8917 int save_State = State;
8919 /* Pretent Insert mode here to allow the cursor on the
8920 * character past the end of the line */
8921 State = INSERT;
8922 coladvance(getviscol());
8923 State = save_State;
8925 #endif
8927 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
8932 * Invoke edit() and take care of "restart_edit" and the return value.
8934 static void
8935 invoke_edit(cap, repl, cmd, startln)
8936 cmdarg_T *cap;
8937 int repl; /* "r" or "gr" command */
8938 int cmd;
8939 int startln;
8941 int restart_edit_save = 0;
8943 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
8944 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
8945 * it. */
8946 if (repl || !stuff_empty())
8947 restart_edit_save = restart_edit;
8948 else
8949 restart_edit_save = 0;
8951 /* Always reset "restart_edit", this is not a restarted edit. */
8952 restart_edit = 0;
8954 if (edit(cmd, startln, cap->count1))
8955 cap->retval |= CA_COMMAND_BUSY;
8957 if (restart_edit == 0)
8958 restart_edit = restart_edit_save;
8961 #ifdef FEAT_TEXTOBJ
8963 * "a" or "i" while an operator is pending or in Visual mode: object motion.
8965 static void
8966 nv_object(cap)
8967 cmdarg_T *cap;
8969 int flag;
8970 int include;
8971 char_u *mps_save;
8973 if (cap->cmdchar == 'i')
8974 include = FALSE; /* "ix" = inner object: exclude white space */
8975 else
8976 include = TRUE; /* "ax" = an object: include white space */
8978 /* Make sure (), [], {} and <> are in 'matchpairs' */
8979 mps_save = curbuf->b_p_mps;
8980 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
8982 switch (cap->nchar)
8984 case 'w': /* "aw" = a word */
8985 flag = current_word(cap->oap, cap->count1, include, FALSE);
8986 break;
8987 case 'W': /* "aW" = a WORD */
8988 flag = current_word(cap->oap, cap->count1, include, TRUE);
8989 break;
8990 case 'b': /* "ab" = a braces block */
8991 case '(':
8992 case ')':
8993 flag = current_block(cap->oap, cap->count1, include, '(', ')');
8994 break;
8995 case 'B': /* "aB" = a Brackets block */
8996 case '{':
8997 case '}':
8998 flag = current_block(cap->oap, cap->count1, include, '{', '}');
8999 break;
9000 case '[': /* "a[" = a [] block */
9001 case ']':
9002 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9003 break;
9004 case '<': /* "a<" = a <> block */
9005 case '>':
9006 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9007 break;
9008 case 't': /* "at" = a tag block (xml and html) */
9009 flag = current_tagblock(cap->oap, cap->count1, include);
9010 break;
9011 case 'p': /* "ap" = a paragraph */
9012 flag = current_par(cap->oap, cap->count1, include, 'p');
9013 break;
9014 case 's': /* "as" = a sentence */
9015 flag = current_sent(cap->oap, cap->count1, include);
9016 break;
9017 case '"': /* "a"" = a double quoted string */
9018 case '\'': /* "a'" = a single quoted string */
9019 case '`': /* "a`" = a backtick quoted string */
9020 flag = current_quote(cap->oap, cap->count1, include,
9021 cap->nchar);
9022 break;
9023 #if 0 /* TODO */
9024 case 'S': /* "aS" = a section */
9025 case 'f': /* "af" = a filename */
9026 case 'u': /* "au" = a URL */
9027 #endif
9028 default:
9029 flag = FAIL;
9030 break;
9033 curbuf->b_p_mps = mps_save;
9034 if (flag == FAIL)
9035 clearopbeep(cap->oap);
9036 adjust_cursor_col();
9037 curwin->w_set_curswant = TRUE;
9039 #endif
9042 * "q" command: Start/stop recording.
9043 * "q:", "q/", "q?": edit command-line in command-line window.
9045 static void
9046 nv_record(cap)
9047 cmdarg_T *cap;
9049 if (cap->oap->op_type == OP_FORMAT)
9051 /* "gqq" is the same as "gqgq": format line */
9052 cap->cmdchar = 'g';
9053 cap->nchar = 'q';
9054 nv_operator(cap);
9056 else if (!checkclearop(cap->oap))
9058 #ifdef FEAT_CMDWIN
9059 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9061 stuffcharReadbuff(cap->nchar);
9062 stuffcharReadbuff(K_CMDWIN);
9064 else
9065 #endif
9066 /* (stop) recording into a named register, unless executing a
9067 * register */
9068 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9069 clearopbeep(cap->oap);
9074 * Handle the "@r" command.
9076 static void
9077 nv_at(cap)
9078 cmdarg_T *cap;
9080 if (checkclearop(cap->oap))
9081 return;
9082 #ifdef FEAT_EVAL
9083 if (cap->nchar == '=')
9085 if (get_expr_register() == NUL)
9086 return;
9088 #endif
9089 while (cap->count1-- && !got_int)
9091 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
9093 clearopbeep(cap->oap);
9094 break;
9096 line_breakcheck();
9101 * Handle the CTRL-U and CTRL-D commands.
9103 static void
9104 nv_halfpage(cap)
9105 cmdarg_T *cap;
9107 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9108 || (cap->cmdchar == Ctrl_D
9109 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9110 clearopbeep(cap->oap);
9111 else if (!checkclearop(cap->oap))
9112 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9116 * Handle "J" or "gJ" command.
9118 static void
9119 nv_join(cap)
9120 cmdarg_T *cap;
9122 #ifdef FEAT_VISUAL
9123 if (VIsual_active) /* join the visual lines */
9124 nv_operator(cap);
9125 else
9126 #endif
9127 if (!checkclearop(cap->oap))
9129 if (cap->count0 <= 1)
9130 cap->count0 = 2; /* default for join is two lines! */
9131 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9132 curbuf->b_ml.ml_line_count)
9133 clearopbeep(cap->oap); /* beyond last line */
9134 else
9136 prep_redo(cap->oap->regname, cap->count0,
9137 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9138 do_do_join(cap->count0, cap->nchar == NUL);
9144 * "P", "gP", "p" and "gp" commands.
9146 static void
9147 nv_put(cap)
9148 cmdarg_T *cap;
9150 #ifdef FEAT_VISUAL
9151 int regname = 0;
9152 void *reg1 = NULL, *reg2 = NULL;
9153 int empty = FALSE;
9154 int was_visual = FALSE;
9155 #endif
9156 int dir;
9157 int flags = 0;
9159 if (cap->oap->op_type != OP_NOP)
9161 #ifdef FEAT_DIFF
9162 /* "dp" is ":diffput" */
9163 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9165 clearop(cap->oap);
9166 nv_diffgetput(TRUE);
9168 else
9169 #endif
9170 clearopbeep(cap->oap);
9172 else
9174 dir = (cap->cmdchar == 'P'
9175 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9176 ? BACKWARD : FORWARD;
9177 prep_redo_cmd(cap);
9178 if (cap->cmdchar == 'g')
9179 flags |= PUT_CURSEND;
9181 #ifdef FEAT_VISUAL
9182 if (VIsual_active)
9184 /* Putting in Visual mode: The put text replaces the selected
9185 * text. First delete the selected text, then put the new text.
9186 * Need to save and restore the registers that the delete
9187 * overwrites if the old contents is being put.
9189 was_visual = TRUE;
9190 regname = cap->oap->regname;
9191 # ifdef FEAT_CLIPBOARD
9192 adjust_clip_reg(&regname);
9193 # endif
9194 if (regname == 0 || VIM_ISDIGIT(regname)
9195 # ifdef FEAT_CLIPBOARD
9196 || (clip_unnamed && (regname == '*' || regname == '+'))
9197 # endif
9201 /* the delete is going to overwrite the register we want to
9202 * put, save it first. */
9203 reg1 = get_register(regname, TRUE);
9206 /* Now delete the selected text. */
9207 cap->cmdchar = 'd';
9208 cap->nchar = NUL;
9209 cap->oap->regname = NUL;
9210 nv_operator(cap);
9211 do_pending_operator(cap, 0, FALSE);
9212 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
9214 /* delete PUT_LINE_BACKWARD; */
9215 cap->oap->regname = regname;
9217 if (reg1 != NULL)
9219 /* Delete probably changed the register we want to put, save
9220 * it first. Then put back what was there before the delete. */
9221 reg2 = get_register(regname, FALSE);
9222 put_register(regname, reg1);
9225 /* When deleted a linewise Visual area, put the register as
9226 * lines to avoid it joined with the next line. When deletion was
9227 * characterwise, split a line when putting lines. */
9228 if (VIsual_mode == 'V')
9229 flags |= PUT_LINE;
9230 else if (VIsual_mode == 'v')
9231 flags |= PUT_LINE_SPLIT;
9232 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9233 flags |= PUT_LINE_FORWARD;
9234 dir = BACKWARD;
9235 if ((VIsual_mode != 'V'
9236 && curwin->w_cursor.col < curbuf->b_op_start.col)
9237 || (VIsual_mode == 'V'
9238 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9239 /* cursor is at the end of the line or end of file, put
9240 * forward. */
9241 dir = FORWARD;
9243 #endif
9244 do_put(cap->oap->regname, dir, cap->count1, flags);
9246 #ifdef FEAT_VISUAL
9247 /* If a register was saved, put it back now. */
9248 if (reg2 != NULL)
9249 put_register(regname, reg2);
9251 /* What to reselect with "gv"? Selecting the just put text seems to
9252 * be the most useful, since the original text was removed. */
9253 if (was_visual)
9255 curbuf->b_visual.vi_start = curbuf->b_op_start;
9256 curbuf->b_visual.vi_end = curbuf->b_op_end;
9259 /* When all lines were selected and deleted do_put() leaves an empty
9260 * line that needs to be deleted now. */
9261 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
9263 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
9265 /* If the cursor was in that line, move it to the end of the last
9266 * line. */
9267 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9269 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9270 coladvance((colnr_T)MAXCOL);
9273 #endif
9274 auto_format(FALSE, TRUE);
9279 * "o" and "O" commands.
9281 static void
9282 nv_open(cap)
9283 cmdarg_T *cap;
9285 #ifdef FEAT_DIFF
9286 /* "do" is ":diffget" */
9287 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9289 clearop(cap->oap);
9290 nv_diffgetput(FALSE);
9292 else
9293 #endif
9294 #ifdef FEAT_VISUAL
9295 if (VIsual_active) /* switch start and end of visual */
9296 v_swap_corners(cap->cmdchar);
9297 else
9298 #endif
9299 n_opencmd(cap);
9302 #ifdef FEAT_SNIFF
9303 static void
9304 nv_sniff(cap)
9305 cmdarg_T *cap UNUSED;
9307 ProcessSniffRequests();
9309 #endif
9311 #ifdef FEAT_NETBEANS_INTG
9312 static void
9313 nv_nbcmd(cap)
9314 cmdarg_T *cap;
9316 netbeans_keycommand(cap->nchar);
9318 #endif
9320 #ifdef FEAT_DND
9321 static void
9322 nv_drop(cap)
9323 cmdarg_T *cap UNUSED;
9325 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9327 #endif
9329 #ifdef FEAT_AUTOCMD
9331 * Trigger CursorHold event.
9332 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9333 * input buffer. "did_cursorhold" is set to avoid retriggering.
9335 static void
9336 nv_cursorhold(cap)
9337 cmdarg_T *cap;
9339 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9340 did_cursorhold = TRUE;
9341 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
9343 #endif