Merge branch 'vim'
[MacVim.git] / src / normal.c
blob0e17d23ed6a3b3da06c8a656bf1eb003d753db5e
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9 /*
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
12 * the operators.
15 #include "vim.h"
17 #ifdef FEAT_VISUAL
19 * The Visual area is remembered for reselection.
21 static int resel_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
22 static linenr_T resel_VIsual_line_count; /* number of lines */
23 static colnr_T resel_VIsual_col; /* nr of cols or end col */
25 static int restart_VIsual_select = 0;
26 #endif
28 static int
29 # ifdef __BORLANDC__
30 _RTLENTRYF
31 # endif
32 nv_compare __ARGS((const void *s1, const void *s2));
33 static int find_command __ARGS((int cmdchar));
34 static void op_colon __ARGS((oparg_T *oap));
35 static void op_function __ARGS((oparg_T *oap));
36 #if defined(FEAT_MOUSE) && defined(FEAT_VISUAL)
37 static void find_start_of_word __ARGS((pos_T *));
38 static void find_end_of_word __ARGS((pos_T *));
39 static int get_mouse_class __ARGS((char_u *p));
40 #endif
41 static void prep_redo_cmd __ARGS((cmdarg_T *cap));
42 static void prep_redo __ARGS((int regname, long, int, int, int, int, int));
43 static int checkclearop __ARGS((oparg_T *oap));
44 static int checkclearopq __ARGS((oparg_T *oap));
45 static void clearop __ARGS((oparg_T *oap));
46 static void clearopbeep __ARGS((oparg_T *oap));
47 #ifdef FEAT_VISUAL
48 static void unshift_special __ARGS((cmdarg_T *cap));
49 #endif
50 #ifdef FEAT_CMDL_INFO
51 static void del_from_showcmd __ARGS((int));
52 #endif
55 * nv_*(): functions called to handle Normal and Visual mode commands.
56 * n_*(): functions called to handle Normal mode commands.
57 * v_*(): functions called to handle Visual mode commands.
59 static void nv_ignore __ARGS((cmdarg_T *cap));
60 static void nv_nop __ARGS((cmdarg_T *cap));
61 static void nv_error __ARGS((cmdarg_T *cap));
62 static void nv_help __ARGS((cmdarg_T *cap));
63 static void nv_addsub __ARGS((cmdarg_T *cap));
64 static void nv_page __ARGS((cmdarg_T *cap));
65 static void nv_gd __ARGS((oparg_T *oap, int nchar, int thisblock));
66 static int nv_screengo __ARGS((oparg_T *oap, int dir, long dist));
67 #ifdef FEAT_MOUSE
68 static void nv_mousescroll __ARGS((cmdarg_T *cap));
69 static void nv_mouse __ARGS((cmdarg_T *cap));
70 #endif
71 static void nv_scroll_line __ARGS((cmdarg_T *cap));
72 static void nv_zet __ARGS((cmdarg_T *cap));
73 #ifdef FEAT_GUI
74 static void nv_ver_scrollbar __ARGS((cmdarg_T *cap));
75 static void nv_hor_scrollbar __ARGS((cmdarg_T *cap));
76 #endif
77 #ifdef FEAT_GUI_TABLINE
78 static void nv_tabline __ARGS((cmdarg_T *cap));
79 static void nv_tabmenu __ARGS((cmdarg_T *cap));
80 #endif
81 static void nv_exmode __ARGS((cmdarg_T *cap));
82 static void nv_colon __ARGS((cmdarg_T *cap));
83 static void nv_ctrlg __ARGS((cmdarg_T *cap));
84 static void nv_ctrlh __ARGS((cmdarg_T *cap));
85 static void nv_clear __ARGS((cmdarg_T *cap));
86 static void nv_ctrlo __ARGS((cmdarg_T *cap));
87 static void nv_hat __ARGS((cmdarg_T *cap));
88 static void nv_Zet __ARGS((cmdarg_T *cap));
89 static void nv_ident __ARGS((cmdarg_T *cap));
90 static void nv_tagpop __ARGS((cmdarg_T *cap));
91 static void nv_scroll __ARGS((cmdarg_T *cap));
92 static void nv_right __ARGS((cmdarg_T *cap));
93 static void nv_left __ARGS((cmdarg_T *cap));
94 static void nv_up __ARGS((cmdarg_T *cap));
95 static void nv_down __ARGS((cmdarg_T *cap));
96 #ifdef FEAT_SEARCHPATH
97 static void nv_gotofile __ARGS((cmdarg_T *cap));
98 #endif
99 static void nv_end __ARGS((cmdarg_T *cap));
100 static void nv_dollar __ARGS((cmdarg_T *cap));
101 static void nv_search __ARGS((cmdarg_T *cap));
102 static void nv_next __ARGS((cmdarg_T *cap));
103 static void normal_search __ARGS((cmdarg_T *cap, int dir, char_u *pat, int opt));
104 static void nv_csearch __ARGS((cmdarg_T *cap));
105 static void nv_brackets __ARGS((cmdarg_T *cap));
106 static void nv_percent __ARGS((cmdarg_T *cap));
107 static void nv_brace __ARGS((cmdarg_T *cap));
108 static void nv_mark __ARGS((cmdarg_T *cap));
109 static void nv_findpar __ARGS((cmdarg_T *cap));
110 static void nv_undo __ARGS((cmdarg_T *cap));
111 static void nv_kundo __ARGS((cmdarg_T *cap));
112 static void nv_Replace __ARGS((cmdarg_T *cap));
113 #ifdef FEAT_VREPLACE
114 static void nv_vreplace __ARGS((cmdarg_T *cap));
115 #endif
116 #ifdef FEAT_VISUAL
117 static void v_swap_corners __ARGS((int cmdchar));
118 #endif
119 static void nv_replace __ARGS((cmdarg_T *cap));
120 static void n_swapchar __ARGS((cmdarg_T *cap));
121 static void nv_cursormark __ARGS((cmdarg_T *cap, int flag, pos_T *pos));
122 #ifdef FEAT_VISUAL
123 static void v_visop __ARGS((cmdarg_T *cap));
124 #endif
125 static void nv_subst __ARGS((cmdarg_T *cap));
126 static void nv_abbrev __ARGS((cmdarg_T *cap));
127 static void nv_optrans __ARGS((cmdarg_T *cap));
128 static void nv_gomark __ARGS((cmdarg_T *cap));
129 static void nv_pcmark __ARGS((cmdarg_T *cap));
130 static void nv_regname __ARGS((cmdarg_T *cap));
131 #ifdef FEAT_VISUAL
132 static void nv_visual __ARGS((cmdarg_T *cap));
133 static void n_start_visual_mode __ARGS((int c));
134 #endif
135 static void nv_window __ARGS((cmdarg_T *cap));
136 static void nv_suspend __ARGS((cmdarg_T *cap));
137 static void nv_g_cmd __ARGS((cmdarg_T *cap));
138 static void n_opencmd __ARGS((cmdarg_T *cap));
139 static void nv_dot __ARGS((cmdarg_T *cap));
140 static void nv_redo __ARGS((cmdarg_T *cap));
141 static void nv_Undo __ARGS((cmdarg_T *cap));
142 static void nv_tilde __ARGS((cmdarg_T *cap));
143 static void nv_operator __ARGS((cmdarg_T *cap));
144 #ifdef FEAT_EVAL
145 static void set_op_var __ARGS((int optype));
146 #endif
147 static void nv_lineop __ARGS((cmdarg_T *cap));
148 static void nv_home __ARGS((cmdarg_T *cap));
149 static void nv_pipe __ARGS((cmdarg_T *cap));
150 static void nv_bck_word __ARGS((cmdarg_T *cap));
151 static void nv_wordcmd __ARGS((cmdarg_T *cap));
152 static void nv_beginline __ARGS((cmdarg_T *cap));
153 static void adjust_cursor __ARGS((oparg_T *oap));
154 #ifdef FEAT_VISUAL
155 static void adjust_for_sel __ARGS((cmdarg_T *cap));
156 static int unadjust_for_sel __ARGS((void));
157 static void nv_select __ARGS((cmdarg_T *cap));
158 #endif
159 static void nv_goto __ARGS((cmdarg_T *cap));
160 static void nv_normal __ARGS((cmdarg_T *cap));
161 static void nv_esc __ARGS((cmdarg_T *oap));
162 static void nv_edit __ARGS((cmdarg_T *cap));
163 static void invoke_edit __ARGS((cmdarg_T *cap, int repl, int cmd, int startln));
164 #ifdef FEAT_TEXTOBJ
165 static void nv_object __ARGS((cmdarg_T *cap));
166 #endif
167 static void nv_record __ARGS((cmdarg_T *cap));
168 static void nv_at __ARGS((cmdarg_T *cap));
169 static void nv_halfpage __ARGS((cmdarg_T *cap));
170 static void nv_join __ARGS((cmdarg_T *cap));
171 static void nv_put __ARGS((cmdarg_T *cap));
172 static void nv_open __ARGS((cmdarg_T *cap));
173 #ifdef FEAT_SNIFF
174 static void nv_sniff __ARGS((cmdarg_T *cap));
175 #endif
176 #ifdef FEAT_NETBEANS_INTG
177 static void nv_nbcmd __ARGS((cmdarg_T *cap));
178 #endif
179 #ifdef FEAT_DND
180 static void nv_drop __ARGS((cmdarg_T *cap));
181 #endif
182 #ifdef FEAT_AUTOCMD
183 static void nv_cursorhold __ARGS((cmdarg_T *cap));
184 #endif
186 static char *e_noident = N_("E349: No identifier under cursor");
189 * Function to be called for a Normal or Visual mode command.
190 * The argument is a cmdarg_T.
192 typedef void (*nv_func_T) __ARGS((cmdarg_T *cap));
194 /* Values for cmd_flags. */
195 #define NV_NCH 0x01 /* may need to get a second char */
196 #define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
197 #define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
198 #define NV_LANG 0x08 /* second char needs language adjustment */
200 #define NV_SS 0x10 /* may start selection */
201 #define NV_SSS 0x20 /* may start selection with shift modifier */
202 #define NV_STS 0x40 /* may stop selection without shift modif. */
203 #define NV_RL 0x80 /* 'rightleft' modifies command */
204 #define NV_KEEPREG 0x100 /* don't clear regname */
205 #define NV_NCW 0x200 /* not allowed in command-line window */
208 * Generally speaking, every Normal mode command should either clear any
209 * pending operator (with *clearop*()), or set the motion type variable
210 * oap->motion_type.
212 * When a cursor motion command is made, it is marked as being a character or
213 * line oriented motion. Then, if an operator is in effect, the operation
214 * becomes character or line oriented accordingly.
218 * This table contains one entry for every Normal or Visual mode command.
219 * The order doesn't matter, init_normal_cmds() will create a sorted index.
220 * It is faster when all keys from zero to '~' are present.
222 static const struct nv_cmd
224 int cmd_char; /* (first) command character */
225 nv_func_T cmd_func; /* function for this command */
226 short_u cmd_flags; /* NV_ flags */
227 short cmd_arg; /* value for ca.arg */
228 } nv_cmds[] =
230 {NUL, nv_error, 0, 0},
231 {Ctrl_A, nv_addsub, 0, 0},
232 {Ctrl_B, nv_page, NV_STS, BACKWARD},
233 {Ctrl_C, nv_esc, 0, TRUE},
234 {Ctrl_D, nv_halfpage, 0, 0},
235 {Ctrl_E, nv_scroll_line, 0, TRUE},
236 {Ctrl_F, nv_page, NV_STS, FORWARD},
237 {Ctrl_G, nv_ctrlg, 0, 0},
238 {Ctrl_H, nv_ctrlh, 0, 0},
239 {Ctrl_I, nv_pcmark, 0, 0},
240 {NL, nv_down, 0, FALSE},
241 {Ctrl_K, nv_error, 0, 0},
242 {Ctrl_L, nv_clear, 0, 0},
243 {Ctrl_M, nv_down, 0, TRUE},
244 {Ctrl_N, nv_down, NV_STS, FALSE},
245 {Ctrl_O, nv_ctrlo, 0, 0},
246 {Ctrl_P, nv_up, NV_STS, FALSE},
247 #ifdef FEAT_VISUAL
248 {Ctrl_Q, nv_visual, 0, FALSE},
249 #else
250 {Ctrl_Q, nv_ignore, 0, 0},
251 #endif
252 {Ctrl_R, nv_redo, 0, 0},
253 {Ctrl_S, nv_ignore, 0, 0},
254 {Ctrl_T, nv_tagpop, NV_NCW, 0},
255 {Ctrl_U, nv_halfpage, 0, 0},
256 #ifdef FEAT_VISUAL
257 {Ctrl_V, nv_visual, 0, FALSE},
258 {'V', nv_visual, 0, FALSE},
259 {'v', nv_visual, 0, FALSE},
260 #else
261 {Ctrl_V, nv_error, 0, 0},
262 {'V', nv_error, 0, 0},
263 {'v', nv_error, 0, 0},
264 #endif
265 {Ctrl_W, nv_window, 0, 0},
266 {Ctrl_X, nv_addsub, 0, 0},
267 {Ctrl_Y, nv_scroll_line, 0, FALSE},
268 {Ctrl_Z, nv_suspend, 0, 0},
269 {ESC, nv_esc, 0, FALSE},
270 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
271 {Ctrl_RSB, nv_ident, NV_NCW, 0},
272 {Ctrl_HAT, nv_hat, NV_NCW, 0},
273 {Ctrl__, nv_error, 0, 0},
274 {' ', nv_right, 0, 0},
275 {'!', nv_operator, 0, 0},
276 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
277 {'#', nv_ident, 0, 0},
278 {'$', nv_dollar, 0, 0},
279 {'%', nv_percent, 0, 0},
280 {'&', nv_optrans, 0, 0},
281 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
282 {'(', nv_brace, 0, BACKWARD},
283 {')', nv_brace, 0, FORWARD},
284 {'*', nv_ident, 0, 0},
285 {'+', nv_down, 0, TRUE},
286 {',', nv_csearch, 0, TRUE},
287 {'-', nv_up, 0, TRUE},
288 {'.', nv_dot, NV_KEEPREG, 0},
289 {'/', nv_search, 0, FALSE},
290 {'0', nv_beginline, 0, 0},
291 {'1', nv_ignore, 0, 0},
292 {'2', nv_ignore, 0, 0},
293 {'3', nv_ignore, 0, 0},
294 {'4', nv_ignore, 0, 0},
295 {'5', nv_ignore, 0, 0},
296 {'6', nv_ignore, 0, 0},
297 {'7', nv_ignore, 0, 0},
298 {'8', nv_ignore, 0, 0},
299 {'9', nv_ignore, 0, 0},
300 {':', nv_colon, 0, 0},
301 {';', nv_csearch, 0, FALSE},
302 {'<', nv_operator, NV_RL, 0},
303 {'=', nv_operator, 0, 0},
304 {'>', nv_operator, NV_RL, 0},
305 {'?', nv_search, 0, FALSE},
306 {'@', nv_at, NV_NCH_NOP, FALSE},
307 {'A', nv_edit, 0, 0},
308 {'B', nv_bck_word, 0, 1},
309 {'C', nv_abbrev, NV_KEEPREG, 0},
310 {'D', nv_abbrev, NV_KEEPREG, 0},
311 {'E', nv_wordcmd, 0, TRUE},
312 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
313 {'G', nv_goto, 0, TRUE},
314 {'H', nv_scroll, 0, 0},
315 {'I', nv_edit, 0, 0},
316 {'J', nv_join, 0, 0},
317 {'K', nv_ident, 0, 0},
318 {'L', nv_scroll, 0, 0},
319 {'M', nv_scroll, 0, 0},
320 {'N', nv_next, 0, SEARCH_REV},
321 {'O', nv_open, 0, 0},
322 {'P', nv_put, 0, 0},
323 {'Q', nv_exmode, NV_NCW, 0},
324 {'R', nv_Replace, 0, FALSE},
325 {'S', nv_subst, NV_KEEPREG, 0},
326 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
327 {'U', nv_Undo, 0, 0},
328 {'W', nv_wordcmd, 0, TRUE},
329 {'X', nv_abbrev, NV_KEEPREG, 0},
330 {'Y', nv_abbrev, NV_KEEPREG, 0},
331 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
332 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
333 {'\\', nv_error, 0, 0},
334 {']', nv_brackets, NV_NCH_ALW, FORWARD},
335 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
336 {'_', nv_lineop, 0, 0},
337 {'`', nv_gomark, NV_NCH_ALW, FALSE},
338 {'a', nv_edit, NV_NCH, 0},
339 {'b', nv_bck_word, 0, 0},
340 {'c', nv_operator, 0, 0},
341 {'d', nv_operator, 0, 0},
342 {'e', nv_wordcmd, 0, FALSE},
343 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
344 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
345 {'h', nv_left, NV_RL, 0},
346 {'i', nv_edit, NV_NCH, 0},
347 {'j', nv_down, 0, FALSE},
348 {'k', nv_up, 0, FALSE},
349 {'l', nv_right, NV_RL, 0},
350 {'m', nv_mark, NV_NCH_NOP, 0},
351 {'n', nv_next, 0, 0},
352 {'o', nv_open, 0, 0},
353 {'p', nv_put, 0, 0},
354 {'q', nv_record, NV_NCH, 0},
355 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
356 {'s', nv_subst, NV_KEEPREG, 0},
357 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
358 {'u', nv_undo, 0, 0},
359 {'w', nv_wordcmd, 0, FALSE},
360 {'x', nv_abbrev, NV_KEEPREG, 0},
361 {'y', nv_operator, 0, 0},
362 {'z', nv_zet, NV_NCH_ALW, 0},
363 {'{', nv_findpar, 0, BACKWARD},
364 {'|', nv_pipe, 0, 0},
365 {'}', nv_findpar, 0, FORWARD},
366 {'~', nv_tilde, 0, 0},
368 /* pound sign */
369 {POUND, nv_ident, 0, 0},
370 #ifdef FEAT_MOUSE
371 {K_MOUSEUP, nv_mousescroll, 0, TRUE},
372 {K_MOUSEDOWN, nv_mousescroll, 0, FALSE},
373 {K_LEFTMOUSE, nv_mouse, 0, 0},
374 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
375 {K_LEFTDRAG, nv_mouse, 0, 0},
376 {K_LEFTRELEASE, nv_mouse, 0, 0},
377 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
378 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
379 {K_MIDDLEDRAG, nv_mouse, 0, 0},
380 {K_MIDDLERELEASE, nv_mouse, 0, 0},
381 {K_RIGHTMOUSE, nv_mouse, 0, 0},
382 {K_RIGHTDRAG, nv_mouse, 0, 0},
383 {K_RIGHTRELEASE, nv_mouse, 0, 0},
384 {K_X1MOUSE, nv_mouse, 0, 0},
385 {K_X1DRAG, nv_mouse, 0, 0},
386 {K_X1RELEASE, nv_mouse, 0, 0},
387 {K_X2MOUSE, nv_mouse, 0, 0},
388 {K_X2DRAG, nv_mouse, 0, 0},
389 {K_X2RELEASE, nv_mouse, 0, 0},
390 #endif
391 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
392 {K_NOP, nv_nop, 0, 0},
393 {K_INS, nv_edit, 0, 0},
394 {K_KINS, nv_edit, 0, 0},
395 {K_BS, nv_ctrlh, 0, 0},
396 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
397 {K_S_UP, nv_page, NV_SS, BACKWARD},
398 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
399 {K_S_DOWN, nv_page, NV_SS, FORWARD},
400 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
401 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
402 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
403 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
404 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
405 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
406 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
407 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
408 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
409 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
410 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
411 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
412 {K_S_END, nv_end, NV_SS, FALSE},
413 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
414 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
415 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
416 {K_S_HOME, nv_home, NV_SS, 0},
417 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
418 {K_DEL, nv_abbrev, 0, 0},
419 {K_KDEL, nv_abbrev, 0, 0},
420 {K_UNDO, nv_kundo, 0, 0},
421 {K_HELP, nv_help, NV_NCW, 0},
422 {K_F1, nv_help, NV_NCW, 0},
423 {K_XF1, nv_help, NV_NCW, 0},
424 #ifdef FEAT_VISUAL
425 {K_SELECT, nv_select, 0, 0},
426 #endif
427 #ifdef FEAT_GUI
428 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
429 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
430 #endif
431 #ifdef FEAT_GUI_TABLINE
432 {K_TABLINE, nv_tabline, 0, 0},
433 {K_TABMENU, nv_tabmenu, 0, 0},
434 #endif
435 #ifdef FEAT_FKMAP
436 {K_F8, farsi_fkey, 0, 0},
437 {K_F9, farsi_fkey, 0, 0},
438 #endif
439 #ifdef FEAT_SNIFF
440 {K_SNIFF, nv_sniff, 0, 0},
441 #endif
442 #ifdef FEAT_NETBEANS_INTG
443 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
444 #endif
445 #ifdef FEAT_DND
446 {K_DROP, nv_drop, NV_STS, 0},
447 #endif
448 #ifdef FEAT_AUTOCMD
449 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
450 #endif
453 /* Number of commands in nv_cmds[]. */
454 #define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
456 /* Sorted index of commands in nv_cmds[]. */
457 static short nv_cmd_idx[NV_CMDS_SIZE];
459 /* The highest index for which
460 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
461 static int nv_max_linear;
464 * Compare functions for qsort() below, that checks the command character
465 * through the index in nv_cmd_idx[].
467 static int
468 #ifdef __BORLANDC__
469 _RTLENTRYF
470 #endif
471 nv_compare(s1, s2)
472 const void *s1;
473 const void *s2;
475 int c1, c2;
477 /* The commands are sorted on absolute value. */
478 c1 = nv_cmds[*(const short *)s1].cmd_char;
479 c2 = nv_cmds[*(const short *)s2].cmd_char;
480 if (c1 < 0)
481 c1 = -c1;
482 if (c2 < 0)
483 c2 = -c2;
484 return c1 - c2;
488 * Initialize the nv_cmd_idx[] table.
490 void
491 init_normal_cmds()
493 int i;
495 /* Fill the index table with a one to one relation. */
496 for (i = 0; i < NV_CMDS_SIZE; ++i)
497 nv_cmd_idx[i] = i;
499 /* Sort the commands by the command character. */
500 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
502 /* Find the first entry that can't be indexed by the command character. */
503 for (i = 0; i < NV_CMDS_SIZE; ++i)
504 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
505 break;
506 nv_max_linear = i - 1;
510 * Search for a command in the commands table.
511 * Returns -1 for invalid command.
513 static int
514 find_command(cmdchar)
515 int cmdchar;
517 int i;
518 int idx;
519 int top, bot;
520 int c;
522 #ifdef FEAT_MBYTE
523 /* A multi-byte character is never a command. */
524 if (cmdchar >= 0x100)
525 return -1;
526 #endif
528 /* We use the absolute value of the character. Special keys have a
529 * negative value, but are sorted on their absolute value. */
530 if (cmdchar < 0)
531 cmdchar = -cmdchar;
533 /* If the character is in the first part: The character is the index into
534 * nv_cmd_idx[]. */
535 if (cmdchar <= nv_max_linear)
536 return nv_cmd_idx[cmdchar];
538 /* Perform a binary search. */
539 bot = nv_max_linear + 1;
540 top = NV_CMDS_SIZE - 1;
541 idx = -1;
542 while (bot <= top)
544 i = (top + bot) / 2;
545 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
546 if (c < 0)
547 c = -c;
548 if (cmdchar == c)
550 idx = nv_cmd_idx[i];
551 break;
553 if (cmdchar > c)
554 bot = i + 1;
555 else
556 top = i - 1;
558 return idx;
562 * Execute a command in Normal mode.
564 /*ARGSUSED*/
565 void
566 normal_cmd(oap, toplevel)
567 oparg_T *oap;
568 int toplevel; /* TRUE when called from main() */
570 cmdarg_T ca; /* command arguments */
571 int c;
572 int ctrl_w = FALSE; /* got CTRL-W command */
573 int old_col = curwin->w_curswant;
574 #ifdef FEAT_CMDL_INFO
575 int need_flushbuf; /* need to call out_flush() */
576 #endif
577 #ifdef FEAT_VISUAL
578 pos_T old_pos; /* cursor position before command */
579 int mapped_len;
580 static int old_mapped_len = 0;
581 #endif
582 int idx;
583 #ifdef FEAT_EVAL
584 int set_prevcount = FALSE;
585 #endif
587 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
588 ca.oap = oap;
590 /* Use a count remembered from before entering an operator. After typing
591 * "3d" we return from normal_cmd() and come back here, the "3" is
592 * remembered in "opcount". */
593 ca.opcount = opcount;
595 #ifdef FEAT_SNIFF
596 want_sniff_request = sniff_connected;
597 #endif
600 * If there is an operator pending, then the command we take this time
601 * will terminate it. Finish_op tells us to finish the operation before
602 * returning this time (unless the operation was cancelled).
604 #ifdef CURSOR_SHAPE
605 c = finish_op;
606 #endif
607 finish_op = (oap->op_type != OP_NOP);
608 #ifdef CURSOR_SHAPE
609 if (finish_op != c)
611 ui_cursor_shape(); /* may show different cursor shape */
612 # ifdef FEAT_MOUSESHAPE
613 update_mouseshape(-1);
614 # endif
616 #endif
618 /* When not finishing an operator and no register name typed, reset the
619 * count. */
620 if (!finish_op && !oap->regname)
622 ca.opcount = 0;
623 #ifdef FEAT_EVAL
624 set_prevcount = TRUE;
625 #endif
628 #ifdef FEAT_AUTOCMD
629 /* Restore counts from before receiving K_CURSORHOLD. This means after
630 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
631 * "3 * 2". */
632 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
634 ca.opcount = oap->prev_opcount;
635 ca.count0 = oap->prev_count0;
636 oap->prev_opcount = 0;
637 oap->prev_count0 = 0;
639 #endif
641 #ifdef FEAT_VISUAL
642 mapped_len = typebuf_maplen();
643 #endif
645 State = NORMAL_BUSY;
646 #ifdef USE_ON_FLY_SCROLL
647 dont_scroll = FALSE; /* allow scrolling here */
648 #endif
651 * Get the command character from the user.
653 c = safe_vgetc();
655 #ifdef FEAT_LANGMAP
656 LANGMAP_ADJUST(c, TRUE);
657 #endif
659 #ifdef FEAT_VISUAL
661 * If a mapping was started in Visual or Select mode, remember the length
662 * of the mapping. This is used below to not return to Insert mode for as
663 * long as the mapping is being executed.
665 if (restart_edit == 0)
666 old_mapped_len = 0;
667 else if (old_mapped_len
668 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
669 old_mapped_len = typebuf_maplen();
670 #endif
672 if (c == NUL)
673 c = K_ZERO;
675 #ifdef FEAT_VISUAL
677 * In Select mode, typed text replaces the selection.
679 if (VIsual_active
680 && VIsual_select
681 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
683 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
684 * 'insertmode' is set) fake a "d"elete command, Insert mode will
685 * restart automatically.
686 * Insert the typed character in the typeahead buffer, so that it can
687 * be mapped in Insert mode. Required for ":lmap" to work. */
688 ins_char_typebuf(c);
689 if (restart_edit != 0)
690 c = 'd';
691 else
692 c = 'c';
693 msg_nowait = TRUE; /* don't delay going to insert mode */
695 #endif
697 #ifdef FEAT_CMDL_INFO
698 need_flushbuf = add_to_showcmd(c);
699 #endif
701 getcount:
702 #ifdef FEAT_VISUAL
703 if (!(VIsual_active && VIsual_select))
704 #endif
707 * Handle a count before a command and compute ca.count0.
708 * Note that '0' is a command and not the start of a count, but it's
709 * part of a count after other digits.
711 while ( (c >= '1' && c <= '9')
712 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
714 if (c == K_DEL || c == K_KDEL)
716 ca.count0 /= 10;
717 #ifdef FEAT_CMDL_INFO
718 del_from_showcmd(4); /* delete the digit and ~@% */
719 #endif
721 else
722 ca.count0 = ca.count0 * 10 + (c - '0');
723 if (ca.count0 < 0) /* got too large! */
724 ca.count0 = 999999999L;
725 #ifdef FEAT_EVAL
726 /* Set v:count here, when called from main() and not a stuffed
727 * command, so that v:count can be used in an expression mapping
728 * right after the count. */
729 if (toplevel && stuff_empty())
731 long count = ca.count0;
733 /* multiply with ca.opcount the same way as below */
734 if (ca.opcount != 0)
735 count = ca.opcount * (count == 0 ? 1 : count);
736 set_vcount(count, count == 0 ? 1 : count, set_prevcount);
737 set_prevcount = FALSE; /* only set v:prevcount once */
739 #endif
740 if (ctrl_w)
742 ++no_mapping;
743 ++allow_keys; /* no mapping for nchar, but keys */
745 ++no_zero_mapping; /* don't map zero here */
746 c = plain_vgetc();
747 #ifdef FEAT_LANGMAP
748 LANGMAP_ADJUST(c, TRUE);
749 #endif
750 --no_zero_mapping;
751 if (ctrl_w)
753 --no_mapping;
754 --allow_keys;
756 #ifdef FEAT_CMDL_INFO
757 need_flushbuf |= add_to_showcmd(c);
758 #endif
762 * If we got CTRL-W there may be a/another count
764 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
766 ctrl_w = TRUE;
767 ca.opcount = ca.count0; /* remember first count */
768 ca.count0 = 0;
769 ++no_mapping;
770 ++allow_keys; /* no mapping for nchar, but keys */
771 c = plain_vgetc(); /* get next character */
772 #ifdef FEAT_LANGMAP
773 LANGMAP_ADJUST(c, TRUE);
774 #endif
775 --no_mapping;
776 --allow_keys;
777 #ifdef FEAT_CMDL_INFO
778 need_flushbuf |= add_to_showcmd(c);
779 #endif
780 goto getcount; /* jump back */
784 #ifdef FEAT_AUTOCMD
785 if (c == K_CURSORHOLD)
787 /* Save the count values so that ca.opcount and ca.count0 are exactly
788 * the same when coming back here after handling K_CURSORHOLD. */
789 oap->prev_opcount = ca.opcount;
790 oap->prev_count0 = ca.count0;
792 else
793 #endif
794 if (ca.opcount != 0)
797 * If we're in the middle of an operator (including after entering a
798 * yank buffer with '"') AND we had a count before the operator, then
799 * that count overrides the current value of ca.count0.
800 * What this means effectively, is that commands like "3dw" get turned
801 * into "d3w" which makes things fall into place pretty neatly.
802 * If you give a count before AND after the operator, they are
803 * multiplied.
805 if (ca.count0)
806 ca.count0 *= ca.opcount;
807 else
808 ca.count0 = ca.opcount;
812 * Always remember the count. It will be set to zero (on the next call,
813 * above) when there is no pending operator.
814 * When called from main(), save the count for use by the "count" built-in
815 * variable.
817 ca.opcount = ca.count0;
818 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
820 #ifdef FEAT_EVAL
822 * Only set v:count when called from main() and not a stuffed command.
824 if (toplevel && stuff_empty())
825 set_vcount(ca.count0, ca.count1, set_prevcount);
826 #endif
829 * Find the command character in the table of commands.
830 * For CTRL-W we already got nchar when looking for a count.
832 if (ctrl_w)
834 ca.nchar = c;
835 ca.cmdchar = Ctrl_W;
837 else
838 ca.cmdchar = c;
839 idx = find_command(ca.cmdchar);
840 if (idx < 0)
842 /* Not a known command: beep. */
843 clearopbeep(oap);
844 goto normal_end;
847 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
849 /* This command is not allowed while editing a ccmdline: beep. */
850 clearopbeep(oap);
851 text_locked_msg();
852 goto normal_end;
854 #ifdef FEAT_AUTOCMD
855 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
856 goto normal_end;
857 #endif
859 #ifdef FEAT_VISUAL
861 * In Visual/Select mode, a few keys are handled in a special way.
863 if (VIsual_active)
865 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
866 if (km_stopsel
867 && (nv_cmds[idx].cmd_flags & NV_STS)
868 && !(mod_mask & MOD_MASK_SHIFT))
870 end_visual_mode();
871 redraw_curbuf_later(INVERTED);
874 /* Keys that work different when 'keymodel' contains "startsel" */
875 if (km_startsel)
877 if (nv_cmds[idx].cmd_flags & NV_SS)
879 unshift_special(&ca);
880 idx = find_command(ca.cmdchar);
881 if (idx < 0)
883 /* Just in case */
884 clearopbeep(oap);
885 goto normal_end;
888 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
889 && (mod_mask & MOD_MASK_SHIFT))
891 mod_mask &= ~MOD_MASK_SHIFT;
895 #endif
897 #ifdef FEAT_RIGHTLEFT
898 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
899 && (nv_cmds[idx].cmd_flags & NV_RL))
901 /* Invert horizontal movements and operations. Only when typed by the
902 * user directly, not when the result of a mapping or "x" translated
903 * to "dl". */
904 switch (ca.cmdchar)
906 case 'l': ca.cmdchar = 'h'; break;
907 case K_RIGHT: ca.cmdchar = K_LEFT; break;
908 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
909 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
910 case 'h': ca.cmdchar = 'l'; break;
911 case K_LEFT: ca.cmdchar = K_RIGHT; break;
912 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
913 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
914 case '>': ca.cmdchar = '<'; break;
915 case '<': ca.cmdchar = '>'; break;
917 idx = find_command(ca.cmdchar);
919 #endif
922 * Get an additional character if we need one.
924 if ((nv_cmds[idx].cmd_flags & NV_NCH)
925 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
926 && oap->op_type == OP_NOP)
927 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
928 || (ca.cmdchar == 'q'
929 && oap->op_type == OP_NOP
930 && !Recording
931 && !Exec_reg)
932 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
933 && (oap->op_type != OP_NOP
934 #ifdef FEAT_VISUAL
935 || VIsual_active
936 #endif
937 ))))
939 int *cp;
940 int repl = FALSE; /* get character for replace mode */
941 int lit = FALSE; /* get extra character literally */
942 int langmap_active = FALSE; /* using :lmap mappings */
943 int lang; /* getting a text character */
944 #ifdef USE_IM_CONTROL
945 int save_smd; /* saved value of p_smd */
946 #endif
948 ++no_mapping;
949 ++allow_keys; /* no mapping for nchar, but allow key codes */
950 #ifdef FEAT_AUTOCMD
951 /* Don't generate a CursorHold event here, most commands can't handle
952 * it, e.g., nv_replace(), nv_csearch(). */
953 did_cursorhold = TRUE;
954 #endif
955 if (ca.cmdchar == 'g')
958 * For 'g' get the next character now, so that we can check for
959 * "gr", "g'" and "g`".
961 ca.nchar = plain_vgetc();
962 #ifdef FEAT_LANGMAP
963 LANGMAP_ADJUST(ca.nchar, TRUE);
964 #endif
965 #ifdef FEAT_CMDL_INFO
966 need_flushbuf |= add_to_showcmd(ca.nchar);
967 #endif
968 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
969 || ca.nchar == Ctrl_BSL)
971 cp = &ca.extra_char; /* need to get a third character */
972 if (ca.nchar != 'r')
973 lit = TRUE; /* get it literally */
974 else
975 repl = TRUE; /* get it in replace mode */
977 else
978 cp = NULL; /* no third character needed */
980 else
982 if (ca.cmdchar == 'r') /* get it in replace mode */
983 repl = TRUE;
984 cp = &ca.nchar;
986 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
989 * Get a second or third character.
991 if (cp != NULL)
993 #ifdef CURSOR_SHAPE
994 if (repl)
996 State = REPLACE; /* pretend Replace mode */
997 ui_cursor_shape(); /* show different cursor shape */
999 #endif
1000 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
1002 /* Allow mappings defined with ":lmap". */
1003 --no_mapping;
1004 --allow_keys;
1005 if (repl)
1006 State = LREPLACE;
1007 else
1008 State = LANGMAP;
1009 langmap_active = TRUE;
1011 #ifdef USE_IM_CONTROL
1012 save_smd = p_smd;
1013 p_smd = FALSE; /* Don't let the IM code show the mode here */
1014 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
1015 im_set_active(TRUE);
1016 #endif
1018 *cp = plain_vgetc();
1020 if (langmap_active)
1022 /* Undo the decrement done above */
1023 ++no_mapping;
1024 ++allow_keys;
1025 State = NORMAL_BUSY;
1027 #ifdef USE_IM_CONTROL
1028 if (lang)
1030 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
1031 im_save_status(&curbuf->b_p_iminsert);
1032 im_set_active(FALSE);
1034 p_smd = save_smd;
1035 #endif
1036 #ifdef CURSOR_SHAPE
1037 State = NORMAL_BUSY;
1038 #endif
1039 #ifdef FEAT_CMDL_INFO
1040 need_flushbuf |= add_to_showcmd(*cp);
1041 #endif
1043 if (!lit)
1045 #ifdef FEAT_DIGRAPHS
1046 /* Typing CTRL-K gets a digraph. */
1047 if (*cp == Ctrl_K
1048 && ((nv_cmds[idx].cmd_flags & NV_LANG)
1049 || cp == &ca.extra_char)
1050 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1052 c = get_digraph(FALSE);
1053 if (c > 0)
1055 *cp = c;
1056 # ifdef FEAT_CMDL_INFO
1057 /* Guessing how to update showcmd here... */
1058 del_from_showcmd(3);
1059 need_flushbuf |= add_to_showcmd(*cp);
1060 # endif
1063 #endif
1065 #ifdef FEAT_LANGMAP
1066 /* adjust chars > 127, except after "tTfFr" commands */
1067 LANGMAP_ADJUST(*cp, !lang);
1068 #endif
1069 #ifdef FEAT_RIGHTLEFT
1070 /* adjust Hebrew mapped char */
1071 if (p_hkmap && lang && KeyTyped)
1072 *cp = hkmap(*cp);
1073 # ifdef FEAT_FKMAP
1074 /* adjust Farsi mapped char */
1075 if (p_fkmap && lang && KeyTyped)
1076 *cp = fkmap(*cp);
1077 # endif
1078 #endif
1082 * When the next character is CTRL-\ a following CTRL-N means the
1083 * command is aborted and we go to Normal mode.
1085 if (cp == &ca.extra_char
1086 && ca.nchar == Ctrl_BSL
1087 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1089 ca.cmdchar = Ctrl_BSL;
1090 ca.nchar = ca.extra_char;
1091 idx = find_command(ca.cmdchar);
1093 else if (*cp == Ctrl_BSL)
1095 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1097 /* There is a busy wait here when typing "f<C-\>" and then
1098 * something different from CTRL-N. Can't be avoided. */
1099 while ((c = vpeekc()) <= 0 && towait > 0L)
1101 do_sleep(towait > 50L ? 50L : towait);
1102 towait -= 50L;
1104 if (c > 0)
1106 c = plain_vgetc();
1107 if (c != Ctrl_N && c != Ctrl_G)
1108 vungetc(c);
1109 else
1111 ca.cmdchar = Ctrl_BSL;
1112 ca.nchar = c;
1113 idx = find_command(ca.cmdchar);
1118 #ifdef FEAT_MBYTE
1119 /* When getting a text character and the next character is a
1120 * multi-byte character, it could be a composing character.
1121 * However, don't wait for it to arrive. */
1122 while (enc_utf8 && lang && (c = vpeekc()) > 0
1123 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1125 c = plain_vgetc();
1126 if (!utf_iscomposing(c))
1128 vungetc(c); /* it wasn't, put it back */
1129 break;
1131 else if (ca.ncharC1 == 0)
1132 ca.ncharC1 = c;
1133 else
1134 ca.ncharC2 = c;
1136 #endif
1138 --no_mapping;
1139 --allow_keys;
1142 #ifdef FEAT_CMDL_INFO
1144 * Flush the showcmd characters onto the screen so we can see them while
1145 * the command is being executed. Only do this when the shown command was
1146 * actually displayed, otherwise this will slow down a lot when executing
1147 * mappings.
1149 if (need_flushbuf)
1150 out_flush();
1151 #endif
1152 #ifdef FEAT_AUTOCMD
1153 if (ca.cmdchar != K_IGNORE)
1154 did_cursorhold = FALSE;
1155 #endif
1157 State = NORMAL;
1159 if (ca.nchar == ESC)
1161 clearop(oap);
1162 if (restart_edit == 0 && goto_im())
1163 restart_edit = 'a';
1164 goto normal_end;
1167 if (ca.cmdchar != K_IGNORE)
1169 msg_didout = FALSE; /* don't scroll screen up for normal command */
1170 msg_col = 0;
1173 #ifdef FEAT_VISUAL
1174 old_pos = curwin->w_cursor; /* remember where cursor was */
1176 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1177 * mode. */
1178 if (!VIsual_active && km_startsel)
1180 if (nv_cmds[idx].cmd_flags & NV_SS)
1182 start_selection();
1183 unshift_special(&ca);
1184 idx = find_command(ca.cmdchar);
1186 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1187 && (mod_mask & MOD_MASK_SHIFT))
1189 start_selection();
1190 mod_mask &= ~MOD_MASK_SHIFT;
1193 #endif
1196 * Execute the command!
1197 * Call the command function found in the commands table.
1199 ca.arg = nv_cmds[idx].cmd_arg;
1200 (nv_cmds[idx].cmd_func)(&ca);
1203 * If we didn't start or finish an operator, reset oap->regname, unless we
1204 * need it later.
1206 if (!finish_op
1207 && !oap->op_type
1208 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1210 clearop(oap);
1211 #ifdef FEAT_EVAL
1212 set_reg_var('"');
1213 #endif
1216 #ifdef FEAT_VISUAL
1217 /* Get the length of mapped chars again after typing a count, second
1218 * character or "z333<cr>". */
1219 if (old_mapped_len > 0)
1220 old_mapped_len = typebuf_maplen();
1221 #endif
1224 * If an operation is pending, handle it...
1226 do_pending_operator(&ca, old_col, FALSE);
1229 * Wait for a moment when a message is displayed that will be overwritten
1230 * by the mode message.
1231 * In Visual mode and with "^O" in Insert mode, a short message will be
1232 * overwritten by the mode message. Wait a bit, until a key is hit.
1233 * In Visual mode, it's more important to keep the Visual area updated
1234 * than keeping a message (e.g. from a /pat search).
1235 * Only do this if the command was typed, not from a mapping.
1236 * Don't wait when emsg_silent is non-zero.
1237 * Also wait a bit after an error message, e.g. for "^O:".
1238 * Don't redraw the screen, it would remove the message.
1240 if ( ((p_smd
1241 && msg_silent == 0
1242 && (restart_edit != 0
1243 #ifdef FEAT_VISUAL
1244 || (VIsual_active
1245 && old_pos.lnum == curwin->w_cursor.lnum
1246 && old_pos.col == curwin->w_cursor.col)
1247 #endif
1249 && (clear_cmdline
1250 || redraw_cmdline)
1251 && (msg_didout || (msg_didany && msg_scroll))
1252 && !msg_nowait
1253 && KeyTyped)
1254 || (restart_edit != 0
1255 #ifdef FEAT_VISUAL
1256 && !VIsual_active
1257 #endif
1258 && (msg_scroll
1259 || emsg_on_display)))
1260 && oap->regname == 0
1261 && !(ca.retval & CA_COMMAND_BUSY)
1262 && stuff_empty()
1263 && typebuf_typed()
1264 && emsg_silent == 0
1265 && !did_wait_return
1266 && oap->op_type == OP_NOP)
1268 int save_State = State;
1270 /* Draw the cursor with the right shape here */
1271 if (restart_edit != 0)
1272 State = INSERT;
1274 /* If need to redraw, and there is a "keep_msg", redraw before the
1275 * delay */
1276 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1278 char_u *kmsg;
1280 kmsg = keep_msg;
1281 keep_msg = NULL;
1282 /* showmode() will clear keep_msg, but we want to use it anyway */
1283 update_screen(0);
1284 /* now reset it, otherwise it's put in the history again */
1285 keep_msg = kmsg;
1286 msg_attr(kmsg, keep_msg_attr);
1287 vim_free(kmsg);
1289 setcursor();
1290 cursor_on();
1291 out_flush();
1292 if (msg_scroll || emsg_on_display)
1293 ui_delay(1000L, TRUE); /* wait at least one second */
1294 ui_delay(3000L, FALSE); /* wait up to three seconds */
1295 State = save_State;
1297 msg_scroll = FALSE;
1298 emsg_on_display = FALSE;
1302 * Finish up after executing a Normal mode command.
1304 normal_end:
1306 msg_nowait = FALSE;
1308 /* Reset finish_op, in case it was set */
1309 #ifdef CURSOR_SHAPE
1310 c = finish_op;
1311 #endif
1312 finish_op = FALSE;
1313 #ifdef CURSOR_SHAPE
1314 /* Redraw the cursor with another shape, if we were in Operator-pending
1315 * mode or did a replace command. */
1316 if (c || ca.cmdchar == 'r')
1318 ui_cursor_shape(); /* may show different cursor shape */
1319 # ifdef FEAT_MOUSESHAPE
1320 update_mouseshape(-1);
1321 # endif
1323 #endif
1325 #ifdef FEAT_CMDL_INFO
1326 if (oap->op_type == OP_NOP && oap->regname == 0
1327 # ifdef FEAT_AUTOCMD
1328 && ca.cmdchar != K_CURSORHOLD
1329 # endif
1331 clear_showcmd();
1332 #endif
1334 checkpcmark(); /* check if we moved since setting pcmark */
1335 vim_free(ca.searchbuf);
1337 #ifdef FEAT_MBYTE
1338 if (has_mbyte)
1339 mb_adjust_cursor();
1340 #endif
1342 #ifdef FEAT_SCROLLBIND
1343 if (curwin->w_p_scb && toplevel)
1345 validate_cursor(); /* may need to update w_leftcol */
1346 do_check_scrollbind(TRUE);
1348 #endif
1351 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1352 * if still inside a mapping that started in Visual mode).
1353 * May switch from Visual to Select mode after CTRL-O command.
1355 if ( oap->op_type == OP_NOP
1356 #ifdef FEAT_VISUAL
1357 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1358 || restart_VIsual_select == 1)
1359 #else
1360 && restart_edit != 0
1361 #endif
1362 && !(ca.retval & CA_COMMAND_BUSY)
1363 && stuff_empty()
1364 && oap->regname == 0)
1366 #ifdef FEAT_VISUAL
1367 if (restart_VIsual_select == 1)
1369 VIsual_select = TRUE;
1370 showmode();
1371 restart_VIsual_select = 0;
1373 #endif
1374 if (restart_edit != 0
1375 #ifdef FEAT_VISUAL
1376 && !VIsual_active && old_mapped_len == 0
1377 #endif
1379 (void)edit(restart_edit, FALSE, 1L);
1382 #ifdef FEAT_VISUAL
1383 if (restart_VIsual_select == 2)
1384 restart_VIsual_select = 1;
1385 #endif
1387 /* Save count before an operator for next time. */
1388 opcount = ca.opcount;
1392 * Handle an operator after visual mode or when the movement is finished
1394 void
1395 do_pending_operator(cap, old_col, gui_yank)
1396 cmdarg_T *cap;
1397 int old_col;
1398 int gui_yank;
1400 oparg_T *oap = cap->oap;
1401 pos_T old_cursor;
1402 int empty_region_error;
1403 int restart_edit_save;
1405 #ifdef FEAT_VISUAL
1406 /* The visual area is remembered for redo */
1407 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1408 static linenr_T redo_VIsual_line_count; /* number of lines */
1409 static colnr_T redo_VIsual_col; /* number of cols or end column */
1410 static long redo_VIsual_count; /* count for Visual operator */
1411 # ifdef FEAT_VIRTUALEDIT
1412 int include_line_break = FALSE;
1413 # endif
1414 #endif
1416 #if defined(FEAT_CLIPBOARD)
1418 * Yank the visual area into the GUI selection register before we operate
1419 * on it and lose it forever.
1420 * Don't do it if a specific register was specified, so that ""x"*P works.
1421 * This could call do_pending_operator() recursively, but that's OK
1422 * because gui_yank will be TRUE for the nested call.
1424 if (clip_star.available
1425 && oap->op_type != OP_NOP
1426 && !gui_yank
1427 # ifdef FEAT_VISUAL
1428 && VIsual_active
1429 && !redo_VIsual_busy
1430 # endif
1431 && oap->regname == 0)
1432 clip_auto_select();
1433 #endif
1434 old_cursor = curwin->w_cursor;
1437 * If an operation is pending, handle it...
1439 if ((finish_op
1440 #ifdef FEAT_VISUAL
1441 || VIsual_active
1442 #endif
1443 ) && oap->op_type != OP_NOP)
1445 #ifdef FEAT_VISUAL
1446 oap->is_VIsual = VIsual_active;
1447 if (oap->motion_force == 'V')
1448 oap->motion_type = MLINE;
1449 else if (oap->motion_force == 'v')
1451 /* If the motion was linewise, "inclusive" will not have been set.
1452 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1453 if (oap->motion_type == MLINE)
1454 oap->inclusive = FALSE;
1455 /* If the motion already was characterwise, toggle "inclusive" */
1456 else if (oap->motion_type == MCHAR)
1457 oap->inclusive = !oap->inclusive;
1458 oap->motion_type = MCHAR;
1460 else if (oap->motion_force == Ctrl_V)
1462 /* Change line- or characterwise motion into Visual block mode. */
1463 VIsual_active = TRUE;
1464 VIsual = oap->start;
1465 VIsual_mode = Ctrl_V;
1466 VIsual_select = FALSE;
1467 VIsual_reselect = FALSE;
1469 #endif
1471 /* only redo yank when 'y' flag is in 'cpoptions' */
1472 /* never redo "zf" (define fold) */
1473 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1474 #ifdef FEAT_VISUAL
1475 && (!VIsual_active || oap->motion_force)
1476 #endif
1477 && cap->cmdchar != 'D'
1478 #ifdef FEAT_FOLDING
1479 && oap->op_type != OP_FOLD
1480 && oap->op_type != OP_FOLDOPEN
1481 && oap->op_type != OP_FOLDOPENREC
1482 && oap->op_type != OP_FOLDCLOSE
1483 && oap->op_type != OP_FOLDCLOSEREC
1484 && oap->op_type != OP_FOLDDEL
1485 && oap->op_type != OP_FOLDDELREC
1486 #endif
1489 prep_redo(oap->regname, cap->count0,
1490 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1491 oap->motion_force, cap->cmdchar, cap->nchar);
1492 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1495 * If 'cpoptions' does not contain 'r', insert the search
1496 * pattern to really repeat the same command.
1498 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
1499 AppendToRedobuffLit(cap->searchbuf, -1);
1500 AppendToRedobuff(NL_STR);
1502 else if (cap->cmdchar == ':')
1504 /* do_cmdline() has stored the first typed line in
1505 * "repeat_cmdline". When several lines are typed repeating
1506 * won't be possible. */
1507 if (repeat_cmdline == NULL)
1508 ResetRedobuff();
1509 else
1511 AppendToRedobuffLit(repeat_cmdline, -1);
1512 AppendToRedobuff(NL_STR);
1513 vim_free(repeat_cmdline);
1514 repeat_cmdline = NULL;
1519 #ifdef FEAT_VISUAL
1520 if (redo_VIsual_busy)
1522 oap->start = curwin->w_cursor;
1523 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1524 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1525 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1526 VIsual_mode = redo_VIsual_mode;
1527 if (VIsual_mode == 'v')
1529 if (redo_VIsual_line_count <= 1)
1530 curwin->w_cursor.col += redo_VIsual_col - 1;
1531 else
1532 curwin->w_cursor.col = redo_VIsual_col;
1534 if (redo_VIsual_col == MAXCOL)
1536 curwin->w_curswant = MAXCOL;
1537 coladvance((colnr_T)MAXCOL);
1539 cap->count0 = redo_VIsual_count;
1540 if (redo_VIsual_count != 0)
1541 cap->count1 = redo_VIsual_count;
1542 else
1543 cap->count1 = 1;
1545 else if (VIsual_active)
1547 if (!gui_yank)
1549 /* Save the current VIsual area for '< and '> marks, and "gv" */
1550 curbuf->b_visual.vi_start = VIsual;
1551 curbuf->b_visual.vi_end = curwin->w_cursor;
1552 curbuf->b_visual.vi_mode = VIsual_mode;
1553 curbuf->b_visual.vi_curswant = curwin->w_curswant;
1554 # ifdef FEAT_EVAL
1555 curbuf->b_visual_mode_eval = VIsual_mode;
1556 # endif
1559 /* In Select mode, a linewise selection is operated upon like a
1560 * characterwise selection. */
1561 if (VIsual_select && VIsual_mode == 'V')
1563 if (lt(VIsual, curwin->w_cursor))
1565 VIsual.col = 0;
1566 curwin->w_cursor.col =
1567 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1569 else
1571 curwin->w_cursor.col = 0;
1572 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1574 VIsual_mode = 'v';
1576 /* If 'selection' is "exclusive", backup one character for
1577 * charwise selections. */
1578 else if (VIsual_mode == 'v')
1580 # ifdef FEAT_VIRTUALEDIT
1581 include_line_break =
1582 # endif
1583 unadjust_for_sel();
1586 oap->start = VIsual;
1587 if (VIsual_mode == 'V')
1588 oap->start.col = 0;
1590 #endif /* FEAT_VISUAL */
1593 * Set oap->start to the first position of the operated text, oap->end
1594 * to the end of the operated text. w_cursor is equal to oap->start.
1596 if (lt(oap->start, curwin->w_cursor))
1598 #ifdef FEAT_FOLDING
1599 /* Include folded lines completely. */
1600 if (!VIsual_active)
1602 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1603 oap->start.col = 0;
1604 if (hasFolding(curwin->w_cursor.lnum, NULL,
1605 &curwin->w_cursor.lnum))
1606 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1608 #endif
1609 oap->end = curwin->w_cursor;
1610 curwin->w_cursor = oap->start;
1612 /* w_virtcol may have been updated; if the cursor goes back to its
1613 * previous position w_virtcol becomes invalid and isn't updated
1614 * automatically. */
1615 curwin->w_valid &= ~VALID_VIRTCOL;
1617 else
1619 #ifdef FEAT_FOLDING
1620 /* Include folded lines completely. */
1621 if (!VIsual_active && oap->motion_type == MLINE)
1623 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1624 NULL))
1625 curwin->w_cursor.col = 0;
1626 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1627 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1629 #endif
1630 oap->end = oap->start;
1631 oap->start = curwin->w_cursor;
1634 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1636 #ifdef FEAT_VIRTUALEDIT
1637 /* Set "virtual_op" before resetting VIsual_active. */
1638 virtual_op = virtual_active();
1639 #endif
1641 #ifdef FEAT_VISUAL
1642 if (VIsual_active || redo_VIsual_busy)
1644 if (VIsual_mode == Ctrl_V) /* block mode */
1646 colnr_T start, end;
1648 oap->block_mode = TRUE;
1650 getvvcol(curwin, &(oap->start),
1651 &oap->start_vcol, NULL, &oap->end_vcol);
1652 if (!redo_VIsual_busy)
1654 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1656 if (start < oap->start_vcol)
1657 oap->start_vcol = start;
1658 if (end > oap->end_vcol)
1660 if (*p_sel == 'e' && start >= 1
1661 && start - 1 >= oap->end_vcol)
1662 oap->end_vcol = start - 1;
1663 else
1664 oap->end_vcol = end;
1668 /* if '$' was used, get oap->end_vcol from longest line */
1669 if (curwin->w_curswant == MAXCOL)
1671 curwin->w_cursor.col = MAXCOL;
1672 oap->end_vcol = 0;
1673 for (curwin->w_cursor.lnum = oap->start.lnum;
1674 curwin->w_cursor.lnum <= oap->end.lnum;
1675 ++curwin->w_cursor.lnum)
1677 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1678 if (end > oap->end_vcol)
1679 oap->end_vcol = end;
1682 else if (redo_VIsual_busy)
1683 oap->end_vcol = oap->start_vcol + redo_VIsual_col - 1;
1685 * Correct oap->end.col and oap->start.col to be the
1686 * upper-left and lower-right corner of the block area.
1688 * (Actually, this does convert column positions into character
1689 * positions)
1691 curwin->w_cursor.lnum = oap->end.lnum;
1692 coladvance(oap->end_vcol);
1693 oap->end = curwin->w_cursor;
1695 curwin->w_cursor = oap->start;
1696 coladvance(oap->start_vcol);
1697 oap->start = curwin->w_cursor;
1700 if (!redo_VIsual_busy && !gui_yank)
1703 * Prepare to reselect and redo Visual: this is based on the
1704 * size of the Visual text
1706 resel_VIsual_mode = VIsual_mode;
1707 if (curwin->w_curswant == MAXCOL)
1708 resel_VIsual_col = MAXCOL;
1709 else if (VIsual_mode == Ctrl_V)
1710 resel_VIsual_col = oap->end_vcol - oap->start_vcol + 1;
1711 else if (oap->line_count > 1)
1712 resel_VIsual_col = oap->end.col;
1713 else
1714 resel_VIsual_col = oap->end.col - oap->start.col + 1;
1715 resel_VIsual_line_count = oap->line_count;
1718 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1719 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1720 && oap->op_type != OP_COLON
1721 #ifdef FEAT_FOLDING
1722 && oap->op_type != OP_FOLD
1723 && oap->op_type != OP_FOLDOPEN
1724 && oap->op_type != OP_FOLDOPENREC
1725 && oap->op_type != OP_FOLDCLOSE
1726 && oap->op_type != OP_FOLDCLOSEREC
1727 && oap->op_type != OP_FOLDDEL
1728 && oap->op_type != OP_FOLDDELREC
1729 #endif
1730 && oap->motion_force == NUL
1733 /* Prepare for redoing. Only use the nchar field for "r",
1734 * otherwise it might be the second char of the operator. */
1735 prep_redo(oap->regname, 0L, NUL, 'v',
1736 get_op_char(oap->op_type),
1737 get_extra_op_char(oap->op_type),
1738 oap->op_type == OP_REPLACE ? cap->nchar : NUL);
1739 if (!redo_VIsual_busy)
1741 redo_VIsual_mode = resel_VIsual_mode;
1742 redo_VIsual_col = resel_VIsual_col;
1743 redo_VIsual_line_count = resel_VIsual_line_count;
1744 redo_VIsual_count = cap->count0;
1749 * oap->inclusive defaults to TRUE.
1750 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1751 * FALSE. This makes "d}P" and "v}dP" work the same.
1753 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1754 oap->inclusive = TRUE;
1755 if (VIsual_mode == 'V')
1756 oap->motion_type = MLINE;
1757 else
1759 oap->motion_type = MCHAR;
1760 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1761 # ifdef FEAT_VIRTUALEDIT
1762 && (include_line_break || !virtual_op)
1763 # endif
1766 oap->inclusive = FALSE;
1767 /* Try to include the newline, unless it's an operator
1768 * that works on lines only */
1769 if (*p_sel != 'o'
1770 && !op_on_lines(oap->op_type)
1771 && oap->end.lnum < curbuf->b_ml.ml_line_count)
1773 ++oap->end.lnum;
1774 oap->end.col = 0;
1775 # ifdef FEAT_VIRTUALEDIT
1776 oap->end.coladd = 0;
1777 # endif
1778 ++oap->line_count;
1783 redo_VIsual_busy = FALSE;
1786 * Switch Visual off now, so screen updating does
1787 * not show inverted text when the screen is redrawn.
1788 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1789 * no screen redraw, so it is done here to remove the inverted
1790 * part.
1792 if (!gui_yank)
1794 VIsual_active = FALSE;
1795 # ifdef FEAT_MOUSE
1796 setmouse();
1797 mouse_dragging = 0;
1798 # endif
1799 if (mode_displayed)
1800 clear_cmdline = TRUE; /* unshow visual mode later */
1801 #ifdef FEAT_CMDL_INFO
1802 else
1803 clear_showcmd();
1804 #endif
1805 if ((oap->op_type == OP_YANK
1806 || oap->op_type == OP_COLON
1807 || oap->op_type == OP_FUNCTION
1808 || oap->op_type == OP_FILTER)
1809 && oap->motion_force == NUL)
1810 redraw_curbuf_later(INVERTED);
1813 #endif
1815 #ifdef FEAT_MBYTE
1816 /* Include the trailing byte of a multi-byte char. */
1817 if (has_mbyte && oap->inclusive)
1819 int l;
1821 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
1822 if (l > 1)
1823 oap->end.col += l - 1;
1825 #endif
1826 curwin->w_set_curswant = TRUE;
1829 * oap->empty is set when start and end are the same. The inclusive
1830 * flag affects this too, unless yanking and the end is on a NUL.
1832 oap->empty = (oap->motion_type == MCHAR
1833 && (!oap->inclusive
1834 || (oap->op_type == OP_YANK
1835 && gchar_pos(&oap->end) == NUL))
1836 && equalpos(oap->start, oap->end)
1837 #ifdef FEAT_VIRTUALEDIT
1838 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1839 #endif
1842 * For delete, change and yank, it's an error to operate on an
1843 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1845 empty_region_error = (oap->empty
1846 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1848 #ifdef FEAT_VISUAL
1849 /* Force a redraw when operating on an empty Visual region, when
1850 * 'modifiable is off or creating a fold. */
1851 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1852 # ifdef FEAT_FOLDING
1853 || oap->op_type == OP_FOLD
1854 # endif
1856 redraw_curbuf_later(INVERTED);
1857 #endif
1860 * If the end of an operator is in column one while oap->motion_type
1861 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1862 * character in the previous line. If op_start is on or before the
1863 * first non-blank in the line, the operator becomes linewise
1864 * (strange, but that's the way vi does it).
1866 if ( oap->motion_type == MCHAR
1867 && oap->inclusive == FALSE
1868 && !(cap->retval & CA_NO_ADJ_OP_END)
1869 && oap->end.col == 0
1870 #ifdef FEAT_VISUAL
1871 && (!oap->is_VIsual || *p_sel == 'o')
1872 && !oap->block_mode
1873 #endif
1874 && oap->line_count > 1)
1876 oap->end_adjusted = TRUE; /* remember that we did this */
1877 --oap->line_count;
1878 --oap->end.lnum;
1879 if (inindent(0))
1880 oap->motion_type = MLINE;
1881 else
1883 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1884 if (oap->end.col)
1886 --oap->end.col;
1887 oap->inclusive = TRUE;
1891 else
1892 oap->end_adjusted = FALSE;
1894 switch (oap->op_type)
1896 case OP_LSHIFT:
1897 case OP_RSHIFT:
1898 op_shift(oap, TRUE,
1899 #ifdef FEAT_VISUAL
1900 oap->is_VIsual ? (int)cap->count1 :
1901 #endif
1903 auto_format(FALSE, TRUE);
1904 break;
1906 case OP_JOIN_NS:
1907 case OP_JOIN:
1908 if (oap->line_count < 2)
1909 oap->line_count = 2;
1910 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1911 curbuf->b_ml.ml_line_count)
1912 beep_flush();
1913 else
1915 do_do_join(oap->line_count, oap->op_type == OP_JOIN);
1916 auto_format(FALSE, TRUE);
1918 break;
1920 case OP_DELETE:
1921 #ifdef FEAT_VISUAL
1922 VIsual_reselect = FALSE; /* don't reselect now */
1923 #endif
1924 if (empty_region_error)
1925 vim_beep();
1926 else
1928 (void)op_delete(oap);
1929 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1930 u_save_cursor(); /* cursor line wasn't saved yet */
1931 auto_format(FALSE, TRUE);
1933 break;
1935 case OP_YANK:
1936 if (empty_region_error)
1938 if (!gui_yank)
1939 vim_beep();
1941 else
1942 (void)op_yank(oap, FALSE, !gui_yank);
1943 check_cursor_col();
1944 break;
1946 case OP_CHANGE:
1947 #ifdef FEAT_VISUAL
1948 VIsual_reselect = FALSE; /* don't reselect now */
1949 #endif
1950 if (empty_region_error)
1951 vim_beep();
1952 else
1954 /* This is a new edit command, not a restart. Need to
1955 * remember it to make 'insertmode' work with mappings for
1956 * Visual mode. But do this only once and not when typed and
1957 * 'insertmode' isn't set. */
1958 if (p_im || !KeyTyped)
1959 restart_edit_save = restart_edit;
1960 else
1961 restart_edit_save = 0;
1962 restart_edit = 0;
1963 /* Reset finish_op now, don't want it set inside edit(). */
1964 finish_op = FALSE;
1965 if (op_change(oap)) /* will call edit() */
1966 cap->retval |= CA_COMMAND_BUSY;
1967 if (restart_edit == 0)
1968 restart_edit = restart_edit_save;
1970 break;
1972 case OP_FILTER:
1973 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1974 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1975 else
1976 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
1978 case OP_INDENT:
1979 case OP_COLON:
1981 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1983 * If 'equalprg' is empty, do the indenting internally.
1985 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1987 # ifdef FEAT_LISP
1988 if (curbuf->b_p_lisp)
1990 op_reindent(oap, get_lisp_indent);
1991 break;
1993 # endif
1994 # ifdef FEAT_CINDENT
1995 op_reindent(oap,
1996 # ifdef FEAT_EVAL
1997 *curbuf->b_p_inde != NUL ? get_expr_indent :
1998 # endif
1999 get_c_indent);
2000 break;
2001 # endif
2003 #endif
2005 op_colon(oap);
2006 break;
2008 case OP_TILDE:
2009 case OP_UPPER:
2010 case OP_LOWER:
2011 case OP_ROT13:
2012 if (empty_region_error)
2013 vim_beep();
2014 else
2015 op_tilde(oap);
2016 check_cursor_col();
2017 break;
2019 case OP_FORMAT:
2020 #if defined(FEAT_EVAL)
2021 if (*curbuf->b_p_fex != NUL)
2022 op_formatexpr(oap); /* use expression */
2023 else
2024 #endif
2025 if (*p_fp != NUL)
2026 op_colon(oap); /* use external command */
2027 else
2028 op_format(oap, FALSE); /* use internal function */
2029 break;
2031 case OP_FORMAT2:
2032 op_format(oap, TRUE); /* use internal function */
2033 break;
2035 case OP_FUNCTION:
2036 op_function(oap); /* call 'operatorfunc' */
2037 break;
2039 case OP_INSERT:
2040 case OP_APPEND:
2041 #ifdef FEAT_VISUAL
2042 VIsual_reselect = FALSE; /* don't reselect now */
2043 #endif
2044 #ifdef FEAT_VISUALEXTRA
2045 if (empty_region_error)
2046 vim_beep();
2047 else
2049 /* This is a new edit command, not a restart. Need to
2050 * remember it to make 'insertmode' work with mappings for
2051 * Visual mode. But do this only once. */
2052 restart_edit_save = restart_edit;
2053 restart_edit = 0;
2055 op_insert(oap, cap->count1);
2057 /* TODO: when inserting in several lines, should format all
2058 * the lines. */
2059 auto_format(FALSE, TRUE);
2061 if (restart_edit == 0)
2062 restart_edit = restart_edit_save;
2064 #else
2065 vim_beep();
2066 #endif
2067 break;
2069 case OP_REPLACE:
2070 #ifdef FEAT_VISUAL
2071 VIsual_reselect = FALSE; /* don't reselect now */
2072 #endif
2073 #ifdef FEAT_VISUALEXTRA
2074 if (empty_region_error)
2075 #endif
2076 vim_beep();
2077 #ifdef FEAT_VISUALEXTRA
2078 else
2079 op_replace(oap, cap->nchar);
2080 #endif
2081 break;
2083 #ifdef FEAT_FOLDING
2084 case OP_FOLD:
2085 VIsual_reselect = FALSE; /* don't reselect now */
2086 foldCreate(oap->start.lnum, oap->end.lnum);
2087 break;
2089 case OP_FOLDOPEN:
2090 case OP_FOLDOPENREC:
2091 case OP_FOLDCLOSE:
2092 case OP_FOLDCLOSEREC:
2093 VIsual_reselect = FALSE; /* don't reselect now */
2094 opFoldRange(oap->start.lnum, oap->end.lnum,
2095 oap->op_type == OP_FOLDOPEN
2096 || oap->op_type == OP_FOLDOPENREC,
2097 oap->op_type == OP_FOLDOPENREC
2098 || oap->op_type == OP_FOLDCLOSEREC,
2099 oap->is_VIsual);
2100 break;
2102 case OP_FOLDDEL:
2103 case OP_FOLDDELREC:
2104 VIsual_reselect = FALSE; /* don't reselect now */
2105 deleteFold(oap->start.lnum, oap->end.lnum,
2106 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2107 break;
2108 #endif
2109 default:
2110 clearopbeep(oap);
2112 #ifdef FEAT_VIRTUALEDIT
2113 virtual_op = MAYBE;
2114 #endif
2115 if (!gui_yank)
2118 * if 'sol' not set, go back to old column for some commands
2120 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2121 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2122 || oap->op_type == OP_DELETE))
2123 coladvance(curwin->w_curswant = old_col);
2125 else
2127 curwin->w_cursor = old_cursor;
2129 #ifdef FEAT_VISUAL
2130 oap->block_mode = FALSE;
2131 #endif
2132 clearop(oap);
2137 * Handle indent and format operators and visual mode ":".
2139 static void
2140 op_colon(oap)
2141 oparg_T *oap;
2143 stuffcharReadbuff(':');
2144 #ifdef FEAT_VISUAL
2145 if (oap->is_VIsual)
2146 stuffReadbuff((char_u *)"'<,'>");
2147 else
2148 #endif
2151 * Make the range look nice, so it can be repeated.
2153 if (oap->start.lnum == curwin->w_cursor.lnum)
2154 stuffcharReadbuff('.');
2155 else
2156 stuffnumReadbuff((long)oap->start.lnum);
2157 if (oap->end.lnum != oap->start.lnum)
2159 stuffcharReadbuff(',');
2160 if (oap->end.lnum == curwin->w_cursor.lnum)
2161 stuffcharReadbuff('.');
2162 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2163 stuffcharReadbuff('$');
2164 else if (oap->start.lnum == curwin->w_cursor.lnum)
2166 stuffReadbuff((char_u *)".+");
2167 stuffnumReadbuff((long)oap->line_count - 1);
2169 else
2170 stuffnumReadbuff((long)oap->end.lnum);
2173 if (oap->op_type != OP_COLON)
2174 stuffReadbuff((char_u *)"!");
2175 if (oap->op_type == OP_INDENT)
2177 #ifndef FEAT_CINDENT
2178 if (*get_equalprg() == NUL)
2179 stuffReadbuff((char_u *)"indent");
2180 else
2181 #endif
2182 stuffReadbuff(get_equalprg());
2183 stuffReadbuff((char_u *)"\n");
2185 else if (oap->op_type == OP_FORMAT)
2187 if (*p_fp == NUL)
2188 stuffReadbuff((char_u *)"fmt");
2189 else
2190 stuffReadbuff(p_fp);
2191 stuffReadbuff((char_u *)"\n']");
2195 * do_cmdline() does the rest
2200 * Handle the "g@" operator: call 'operatorfunc'.
2202 /*ARGSUSED*/
2203 static void
2204 op_function(oap)
2205 oparg_T *oap;
2207 #ifdef FEAT_EVAL
2208 char_u *(argv[1]);
2210 if (*p_opfunc == NUL)
2211 EMSG(_("E774: 'operatorfunc' is empty"));
2212 else
2214 /* Set '[ and '] marks to text to be operated on. */
2215 curbuf->b_op_start = oap->start;
2216 curbuf->b_op_end = oap->end;
2217 if (oap->motion_type != MLINE && !oap->inclusive)
2218 /* Exclude the end position. */
2219 decl(&curbuf->b_op_end);
2221 if (oap->block_mode)
2222 argv[0] = (char_u *)"block";
2223 else if (oap->motion_type == MLINE)
2224 argv[0] = (char_u *)"line";
2225 else
2226 argv[0] = (char_u *)"char";
2227 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
2229 #else
2230 EMSG(_("E775: Eval feature not available"));
2231 #endif
2234 #if defined(FEAT_MOUSE) || defined(PROTO)
2236 * Do the appropriate action for the current mouse click in the current mode.
2237 * Not used for Command-line mode.
2239 * Normal Mode:
2240 * event modi- position visual change action
2241 * fier cursor window
2242 * left press - yes end yes
2243 * left press C yes end yes "^]" (2)
2244 * left press S yes end yes "*" (2)
2245 * left drag - yes start if moved no
2246 * left relse - yes start if moved no
2247 * middle press - yes if not active no put register
2248 * middle press - yes if active no yank and put
2249 * right press - yes start or extend yes
2250 * right press S yes no change yes "#" (2)
2251 * right drag - yes extend no
2252 * right relse - yes extend no
2254 * Insert or Replace Mode:
2255 * event modi- position visual change action
2256 * fier cursor window
2257 * left press - yes (cannot be active) yes
2258 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2259 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2260 * left drag - yes start or extend (1) no CTRL-O (1)
2261 * left relse - yes start or extend (1) no CTRL-O (1)
2262 * middle press - no (cannot be active) no put register
2263 * right press - yes start or extend yes CTRL-O
2264 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2266 * (1) only if mouse pointer moved since press
2267 * (2) only if click is in same buffer
2269 * Return TRUE if start_arrow() should be called for edit mode.
2272 do_mouse(oap, c, dir, count, fixindent)
2273 oparg_T *oap; /* operator argument, can be NULL */
2274 int c; /* K_LEFTMOUSE, etc */
2275 int dir; /* Direction to 'put' if necessary */
2276 long count;
2277 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2279 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2280 static int got_click = FALSE; /* got a click some time back */
2282 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2283 int is_click; /* If FALSE it's a drag or release event */
2284 int is_drag; /* If TRUE it's a drag event */
2285 int jump_flags = 0; /* flags for jump_to_mouse() */
2286 pos_T start_visual;
2287 int moved; /* Has cursor moved? */
2288 int in_status_line; /* mouse in status line */
2289 #ifdef FEAT_VERTSPLIT
2290 int in_sep_line; /* mouse in vertical separator line */
2291 #endif
2292 int c1, c2;
2293 #if defined(FEAT_FOLDING)
2294 pos_T save_cursor;
2295 #endif
2296 win_T *old_curwin = curwin;
2297 #ifdef FEAT_VISUAL
2298 static pos_T orig_cursor;
2299 colnr_T leftcol, rightcol;
2300 pos_T end_visual;
2301 int diff;
2302 int old_active = VIsual_active;
2303 int old_mode = VIsual_mode;
2304 #endif
2305 int regname;
2307 #if defined(FEAT_FOLDING)
2308 save_cursor = curwin->w_cursor;
2309 #endif
2312 * When GUI is active, always recognize mouse events, otherwise:
2313 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2314 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2315 * - For command line and insert mode 'mouse' is checked before calling
2316 * do_mouse().
2318 if (do_always)
2319 do_always = FALSE;
2320 else
2321 #ifdef FEAT_GUI
2322 if (!gui.in_use)
2323 #endif
2325 #ifdef FEAT_VISUAL
2326 if (VIsual_active)
2328 if (!mouse_has(MOUSE_VISUAL))
2329 return FALSE;
2331 else
2332 #endif
2333 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2334 return FALSE;
2337 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2339 #ifdef FEAT_MOUSESHAPE
2340 /* May have stopped dragging the status or separator line. The pointer is
2341 * most likely still on the status or separator line. */
2342 if (!is_drag && drag_status_line)
2344 drag_status_line = FALSE;
2345 update_mouseshape(SHAPE_IDX_STATUS);
2347 # ifdef FEAT_VERTSPLIT
2348 if (!is_drag && drag_sep_line)
2350 drag_sep_line = FALSE;
2351 update_mouseshape(SHAPE_IDX_VSEP);
2353 # endif
2354 #endif
2357 * Ignore drag and release events if we didn't get a click.
2359 if (is_click)
2360 got_click = TRUE;
2361 else
2363 if (!got_click) /* didn't get click, ignore */
2364 return FALSE;
2365 if (!is_drag) /* release, reset got_click */
2366 got_click = FALSE;
2369 #ifndef FEAT_VISUAL
2371 * ALT is only used for starging/extending Visual mode.
2373 if ((mod_mask & MOD_MASK_ALT))
2374 return FALSE;
2375 #endif
2378 * CTRL right mouse button does CTRL-T
2380 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2382 if (State & INSERT)
2383 stuffcharReadbuff(Ctrl_O);
2384 if (count > 1)
2385 stuffnumReadbuff(count);
2386 stuffcharReadbuff(Ctrl_T);
2387 got_click = FALSE; /* ignore drag&release now */
2388 return FALSE;
2392 * CTRL only works with left mouse button
2394 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2395 return FALSE;
2398 * When a modifier is down, ignore drag and release events, as well as
2399 * multiple clicks and the middle mouse button.
2400 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2402 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2403 | MOD_MASK_META))
2404 && (!is_click
2405 || (mod_mask & MOD_MASK_MULTI_CLICK)
2406 || which_button == MOUSE_MIDDLE)
2407 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
2408 && mouse_model_popup()
2409 && which_button == MOUSE_LEFT)
2410 && !((mod_mask & MOD_MASK_ALT)
2411 && !mouse_model_popup()
2412 && which_button == MOUSE_RIGHT)
2414 return FALSE;
2417 * If the button press was used as the movement command for an operator
2418 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2419 * drag/release events.
2421 if (!is_click && which_button == MOUSE_MIDDLE)
2422 return FALSE;
2424 if (oap != NULL)
2425 regname = oap->regname;
2426 else
2427 regname = 0;
2430 * Middle mouse button does a 'put' of the selected text
2432 if (which_button == MOUSE_MIDDLE)
2434 if (State == NORMAL)
2437 * If an operator was pending, we don't know what the user wanted
2438 * to do. Go back to normal mode: Clear the operator and beep().
2440 if (oap != NULL && oap->op_type != OP_NOP)
2442 clearopbeep(oap);
2443 return FALSE;
2446 #ifdef FEAT_VISUAL
2448 * If visual was active, yank the highlighted text and put it
2449 * before the mouse pointer position.
2450 * In Select mode replace the highlighted text with the clipboard.
2452 if (VIsual_active)
2454 if (VIsual_select)
2456 stuffcharReadbuff(Ctrl_G);
2457 stuffReadbuff((char_u *)"\"+p");
2459 else
2461 stuffcharReadbuff('y');
2462 stuffcharReadbuff(K_MIDDLEMOUSE);
2464 do_always = TRUE; /* ignore 'mouse' setting next time */
2465 return FALSE;
2467 #endif
2469 * The rest is below jump_to_mouse()
2473 else if ((State & INSERT) == 0)
2474 return FALSE;
2477 * Middle click in insert mode doesn't move the mouse, just insert the
2478 * contents of a register. '.' register is special, can't insert that
2479 * with do_put().
2480 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2481 * happens for the GUI).
2483 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2485 if (regname == '.')
2486 insert_reg(regname, TRUE);
2487 else
2489 #ifdef FEAT_CLIPBOARD
2490 if (clip_star.available && regname == 0)
2491 regname = '*';
2492 #endif
2493 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2494 insert_reg(regname, TRUE);
2495 else
2497 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2499 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2500 AppendCharToRedobuff(Ctrl_R);
2501 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2502 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2505 return FALSE;
2509 /* When dragging or button-up stay in the same window. */
2510 if (!is_click)
2511 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2513 start_visual.lnum = 0;
2515 #ifdef FEAT_WINDOWS
2516 /* Check for clicking in the tab page line. */
2517 if (mouse_row == 0 && firstwin->w_winrow > 0)
2519 if (is_drag)
2520 return FALSE;
2521 got_click = FALSE; /* ignore mouse-up and drag events */
2523 /* click in a tab selects that tab page */
2524 if (is_click
2525 # ifdef FEAT_CMDWIN
2526 && cmdwin_type == 0
2527 # endif
2528 && mouse_col < Columns)
2530 c1 = TabPageIdxs[mouse_col];
2531 if (c1 >= 0)
2533 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2535 /* double click opens new page */
2536 end_visual_mode();
2537 tabpage_new();
2538 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2540 else
2542 /* Go to specified tab page, or next one if not clicking
2543 * on a label. */
2544 goto_tabpage(c1);
2546 /* It's like clicking on the status line of a window. */
2547 if (curwin != old_curwin)
2548 end_visual_mode();
2551 else if (c1 < 0)
2553 tabpage_T *tp;
2555 /* Close the current or specified tab page. */
2556 if (c1 == -999)
2557 tp = curtab;
2558 else
2559 tp = find_tabpage(-c1);
2560 if (tp == curtab)
2562 if (first_tabpage->tp_next != NULL)
2563 tabpage_close(FALSE);
2565 else if (tp != NULL)
2566 tabpage_close_other(tp, FALSE);
2569 return TRUE;
2571 #endif
2574 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2575 * right button up -> pop-up menu
2576 * shift-left button -> right button
2577 * alt-left button -> alt-right button
2579 if (mouse_model_popup())
2581 if (which_button == MOUSE_RIGHT
2582 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2585 * NOTE: Ignore right button down and drag mouse events.
2586 * Windows only shows the popup menu on the button up event.
2588 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2589 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) \
2590 || defined(FEAT_GUI_MACVIM)
2591 if (!is_click)
2592 return FALSE;
2593 #endif
2594 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2595 if (is_click || is_drag)
2596 return FALSE;
2597 #endif
2598 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2599 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2600 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON) \
2601 || defined(FEAT_GUI_MACVIM)
2602 if (gui.in_use)
2604 jump_flags = 0;
2605 if (STRCMP(p_mousem, "popup_setpos") == 0)
2607 /* First set the cursor position before showing the popup
2608 * menu. */
2609 #ifdef FEAT_VISUAL
2610 if (VIsual_active)
2612 pos_T m_pos;
2615 * set MOUSE_MAY_STOP_VIS if we are outside the
2616 * selection or the current window (might have false
2617 * negative here)
2619 if (mouse_row < W_WINROW(curwin)
2620 || mouse_row
2621 > (W_WINROW(curwin) + curwin->w_height))
2622 jump_flags = MOUSE_MAY_STOP_VIS;
2623 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2624 jump_flags = MOUSE_MAY_STOP_VIS;
2625 else
2627 if ((lt(curwin->w_cursor, VIsual)
2628 && (lt(m_pos, curwin->w_cursor)
2629 || lt(VIsual, m_pos)))
2630 || (lt(VIsual, curwin->w_cursor)
2631 && (lt(m_pos, VIsual)
2632 || lt(curwin->w_cursor, m_pos))))
2634 jump_flags = MOUSE_MAY_STOP_VIS;
2636 else if (VIsual_mode == Ctrl_V)
2638 getvcols(curwin, &curwin->w_cursor, &VIsual,
2639 &leftcol, &rightcol);
2640 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2641 if (m_pos.col < leftcol || m_pos.col > rightcol)
2642 jump_flags = MOUSE_MAY_STOP_VIS;
2646 else
2647 jump_flags = MOUSE_MAY_STOP_VIS;
2648 #endif
2650 if (jump_flags)
2652 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2653 update_curbuf(
2654 #ifdef FEAT_VISUAL
2655 VIsual_active ? INVERTED :
2656 #endif
2657 VALID);
2658 setcursor();
2659 out_flush(); /* Update before showing popup menu */
2661 # ifdef FEAT_MENU
2662 gui_show_popupmenu();
2663 # endif
2664 return (jump_flags & CURSOR_MOVED) != 0;
2666 else
2667 return FALSE;
2668 #else
2669 return FALSE;
2670 #endif
2672 if (which_button == MOUSE_LEFT
2673 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
2675 which_button = MOUSE_RIGHT;
2676 mod_mask &= ~MOD_MASK_SHIFT;
2680 #ifdef FEAT_VISUAL
2681 if ((State & (NORMAL | INSERT))
2682 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2684 if (which_button == MOUSE_LEFT)
2686 if (is_click)
2688 /* stop Visual mode for a left click in a window, but not when
2689 * on a status line */
2690 if (VIsual_active)
2691 jump_flags |= MOUSE_MAY_STOP_VIS;
2693 else if (mouse_has(MOUSE_VISUAL))
2694 jump_flags |= MOUSE_MAY_VIS;
2696 else if (which_button == MOUSE_RIGHT)
2698 if (is_click && VIsual_active)
2701 * Remember the start and end of visual before moving the
2702 * cursor.
2704 if (lt(curwin->w_cursor, VIsual))
2706 start_visual = curwin->w_cursor;
2707 end_visual = VIsual;
2709 else
2711 start_visual = VIsual;
2712 end_visual = curwin->w_cursor;
2715 jump_flags |= MOUSE_FOCUS;
2716 if (mouse_has(MOUSE_VISUAL))
2717 jump_flags |= MOUSE_MAY_VIS;
2720 #endif
2723 * If an operator is pending, ignore all drags and releases until the
2724 * next mouse click.
2726 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2728 got_click = FALSE;
2729 oap->motion_type = MCHAR;
2732 /* When releasing the button let jump_to_mouse() know. */
2733 if (!is_click && !is_drag)
2734 jump_flags |= MOUSE_RELEASED;
2737 * JUMP!
2739 jump_flags = jump_to_mouse(jump_flags,
2740 oap == NULL ? NULL : &(oap->inclusive), which_button);
2741 moved = (jump_flags & CURSOR_MOVED);
2742 in_status_line = (jump_flags & IN_STATUS_LINE);
2743 #ifdef FEAT_VERTSPLIT
2744 in_sep_line = (jump_flags & IN_SEP_LINE);
2745 #endif
2747 #ifdef FEAT_NETBEANS_INTG
2748 if (usingNetbeans && isNetbeansBuffer(curbuf)
2749 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2751 int key = KEY2TERMCAP1(c);
2753 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2754 || key == (int)KE_RIGHTRELEASE)
2755 netbeans_button_release(which_button);
2757 #endif
2759 /* When jumping to another window, clear a pending operator. That's a bit
2760 * friendlier than beeping and not jumping to that window. */
2761 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2762 clearop(oap);
2764 #ifdef FEAT_FOLDING
2765 if (mod_mask == 0
2766 && !is_drag
2767 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2768 && which_button == MOUSE_LEFT)
2770 /* open or close a fold at this line */
2771 if (jump_flags & MOUSE_FOLD_OPEN)
2772 openFold(curwin->w_cursor.lnum, 1L);
2773 else
2774 closeFold(curwin->w_cursor.lnum, 1L);
2775 /* don't move the cursor if still in the same window */
2776 if (curwin == old_curwin)
2777 curwin->w_cursor = save_cursor;
2779 #endif
2781 #if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2782 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2784 clip_modeless(which_button, is_click, is_drag);
2785 return FALSE;
2787 #endif
2789 #ifdef FEAT_VISUAL
2790 /* Set global flag that we are extending the Visual area with mouse
2791 * dragging; temporarily minimize 'scrolloff'. */
2792 if (VIsual_active && is_drag && p_so)
2794 /* In the very first line, allow scrolling one line */
2795 if (mouse_row == 0)
2796 mouse_dragging = 2;
2797 else
2798 mouse_dragging = 1;
2801 /* When dragging the mouse above the window, scroll down. */
2802 if (is_drag && mouse_row < 0 && !in_status_line)
2804 scroll_redraw(FALSE, 1L);
2805 mouse_row = 0;
2808 if (start_visual.lnum) /* right click in visual mode */
2810 /* When ALT is pressed make Visual mode blockwise. */
2811 if (mod_mask & MOD_MASK_ALT)
2812 VIsual_mode = Ctrl_V;
2815 * In Visual-block mode, divide the area in four, pick up the corner
2816 * that is in the quarter that the cursor is in.
2818 if (VIsual_mode == Ctrl_V)
2820 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2821 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2822 end_visual.col = leftcol;
2823 else
2824 end_visual.col = rightcol;
2825 if (curwin->w_cursor.lnum <
2826 (start_visual.lnum + end_visual.lnum) / 2)
2827 end_visual.lnum = end_visual.lnum;
2828 else
2829 end_visual.lnum = start_visual.lnum;
2831 /* move VIsual to the right column */
2832 start_visual = curwin->w_cursor; /* save the cursor pos */
2833 curwin->w_cursor = end_visual;
2834 coladvance(end_visual.col);
2835 VIsual = curwin->w_cursor;
2836 curwin->w_cursor = start_visual; /* restore the cursor */
2838 else
2841 * If the click is before the start of visual, change the start.
2842 * If the click is after the end of visual, change the end. If
2843 * the click is inside the visual, change the closest side.
2845 if (lt(curwin->w_cursor, start_visual))
2846 VIsual = end_visual;
2847 else if (lt(end_visual, curwin->w_cursor))
2848 VIsual = start_visual;
2849 else
2851 /* In the same line, compare column number */
2852 if (end_visual.lnum == start_visual.lnum)
2854 if (curwin->w_cursor.col - start_visual.col >
2855 end_visual.col - curwin->w_cursor.col)
2856 VIsual = start_visual;
2857 else
2858 VIsual = end_visual;
2861 /* In different lines, compare line number */
2862 else
2864 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2865 (end_visual.lnum - curwin->w_cursor.lnum);
2867 if (diff > 0) /* closest to end */
2868 VIsual = start_visual;
2869 else if (diff < 0) /* closest to start */
2870 VIsual = end_visual;
2871 else /* in the middle line */
2873 if (curwin->w_cursor.col <
2874 (start_visual.col + end_visual.col) / 2)
2875 VIsual = end_visual;
2876 else
2877 VIsual = start_visual;
2884 * If Visual mode started in insert mode, execute "CTRL-O"
2886 else if ((State & INSERT) && VIsual_active)
2887 stuffcharReadbuff(Ctrl_O);
2888 #endif
2891 * Middle mouse click: Put text before cursor.
2893 if (which_button == MOUSE_MIDDLE)
2895 #ifdef FEAT_CLIPBOARD
2896 if (clip_star.available && regname == 0)
2897 regname = '*';
2898 #endif
2899 if (yank_register_mline(regname))
2901 if (mouse_past_bottom)
2902 dir = FORWARD;
2904 else if (mouse_past_eol)
2905 dir = FORWARD;
2907 if (fixindent)
2909 c1 = (dir == BACKWARD) ? '[' : ']';
2910 c2 = 'p';
2912 else
2914 c1 = (dir == FORWARD) ? 'p' : 'P';
2915 c2 = NUL;
2917 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2920 * Remember where the paste started, so in edit() Insstart can be set
2921 * to this position
2923 if (restart_edit != 0)
2924 where_paste_started = curwin->w_cursor;
2925 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2928 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2930 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2931 * error under the mouse pointer.
2933 else if (((mod_mask & MOD_MASK_CTRL)
2934 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2935 && bt_quickfix(curbuf))
2937 if (State & INSERT)
2938 stuffcharReadbuff(Ctrl_O);
2939 if (curwin->w_llist_ref == NULL) /* quickfix window */
2940 stuffReadbuff((char_u *)":.cc\n");
2941 else /* location list window */
2942 stuffReadbuff((char_u *)":.ll\n");
2943 got_click = FALSE; /* ignore drag&release now */
2945 #endif
2948 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2949 * under the mouse pointer.
2951 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2952 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2954 if (State & INSERT)
2955 stuffcharReadbuff(Ctrl_O);
2956 stuffcharReadbuff(Ctrl_RSB);
2957 got_click = FALSE; /* ignore drag&release now */
2961 * Shift-Mouse click searches for the next occurrence of the word under
2962 * the mouse pointer
2964 else if ((mod_mask & MOD_MASK_SHIFT))
2966 if (State & INSERT
2967 #ifdef FEAT_VISUAL
2968 || (VIsual_active && VIsual_select)
2969 #endif
2971 stuffcharReadbuff(Ctrl_O);
2972 if (which_button == MOUSE_LEFT)
2973 stuffcharReadbuff('*');
2974 else /* MOUSE_RIGHT */
2975 stuffcharReadbuff('#');
2978 /* Handle double clicks, unless on status line */
2979 else if (in_status_line)
2981 #ifdef FEAT_MOUSESHAPE
2982 if ((is_drag || is_click) && !drag_status_line)
2984 drag_status_line = TRUE;
2985 update_mouseshape(-1);
2987 #endif
2989 #ifdef FEAT_VERTSPLIT
2990 else if (in_sep_line)
2992 # ifdef FEAT_MOUSESHAPE
2993 if ((is_drag || is_click) && !drag_sep_line)
2995 drag_sep_line = TRUE;
2996 update_mouseshape(-1);
2998 # endif
3000 #endif
3001 #ifdef FEAT_VISUAL
3002 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3003 && mouse_has(MOUSE_VISUAL))
3005 if (is_click || !VIsual_active)
3007 if (VIsual_active)
3008 orig_cursor = VIsual;
3009 else
3011 check_visual_highlight();
3012 VIsual = curwin->w_cursor;
3013 orig_cursor = VIsual;
3014 VIsual_active = TRUE;
3015 VIsual_reselect = TRUE;
3016 /* start Select mode if 'selectmode' contains "mouse" */
3017 may_start_select('o');
3018 setmouse();
3020 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3022 /* Double click with ALT pressed makes it blockwise. */
3023 if (mod_mask & MOD_MASK_ALT)
3024 VIsual_mode = Ctrl_V;
3025 else
3026 VIsual_mode = 'v';
3028 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3029 VIsual_mode = 'V';
3030 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3031 VIsual_mode = Ctrl_V;
3032 #ifdef FEAT_CLIPBOARD
3033 /* Make sure the clipboard gets updated. Needed because start and
3034 * end may still be the same, and the selection needs to be owned */
3035 clip_star.vmode = NUL;
3036 #endif
3039 * A double click selects a word or a block.
3041 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3043 pos_T *pos = NULL;
3044 int gc;
3046 if (is_click)
3048 /* If the character under the cursor (skipping white space) is
3049 * not a word character, try finding a match and select a (),
3050 * {}, [], #if/#endif, etc. block. */
3051 end_visual = curwin->w_cursor;
3052 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
3053 inc(&end_visual);
3054 if (oap != NULL)
3055 oap->motion_type = MCHAR;
3056 if (oap != NULL
3057 && VIsual_mode == 'v'
3058 && !vim_iswordc(gchar_pos(&end_visual))
3059 && equalpos(curwin->w_cursor, VIsual)
3060 && (pos = findmatch(oap, NUL)) != NULL)
3062 curwin->w_cursor = *pos;
3063 if (oap->motion_type == MLINE)
3064 VIsual_mode = 'V';
3065 else if (*p_sel == 'e')
3067 if (lt(curwin->w_cursor, VIsual))
3068 ++VIsual.col;
3069 else
3070 ++curwin->w_cursor.col;
3075 if (pos == NULL && (is_click || is_drag))
3077 /* When not found a match or when dragging: extend to include
3078 * a word. */
3079 if (lt(curwin->w_cursor, orig_cursor))
3081 find_start_of_word(&curwin->w_cursor);
3082 find_end_of_word(&VIsual);
3084 else
3086 find_start_of_word(&VIsual);
3087 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3088 #ifdef FEAT_MBYTE
3089 curwin->w_cursor.col +=
3090 (*mb_ptr2len)(ml_get_cursor());
3091 #else
3092 ++curwin->w_cursor.col;
3093 #endif
3094 find_end_of_word(&curwin->w_cursor);
3097 curwin->w_set_curswant = TRUE;
3099 if (is_click)
3100 redraw_curbuf_later(INVERTED); /* update the inversion */
3102 else if (VIsual_active && !old_active)
3104 if (mod_mask & MOD_MASK_ALT)
3105 VIsual_mode = Ctrl_V;
3106 else
3107 VIsual_mode = 'v';
3110 /* If Visual mode changed show it later. */
3111 if ((!VIsual_active && old_active && mode_displayed)
3112 || (VIsual_active && p_smd && msg_silent == 0
3113 && (!old_active || VIsual_mode != old_mode)))
3114 redraw_cmdline = TRUE;
3115 #endif
3117 return moved;
3120 #ifdef FEAT_VISUAL
3122 * Move "pos" back to the start of the word it's in.
3124 static void
3125 find_start_of_word(pos)
3126 pos_T *pos;
3128 char_u *line;
3129 int cclass;
3130 int col;
3132 line = ml_get(pos->lnum);
3133 cclass = get_mouse_class(line + pos->col);
3135 while (pos->col > 0)
3137 col = pos->col - 1;
3138 #ifdef FEAT_MBYTE
3139 col -= (*mb_head_off)(line, line + col);
3140 #endif
3141 if (get_mouse_class(line + col) != cclass)
3142 break;
3143 pos->col = col;
3148 * Move "pos" forward to the end of the word it's in.
3149 * When 'selection' is "exclusive", the position is just after the word.
3151 static void
3152 find_end_of_word(pos)
3153 pos_T *pos;
3155 char_u *line;
3156 int cclass;
3157 int col;
3159 line = ml_get(pos->lnum);
3160 if (*p_sel == 'e' && pos->col > 0)
3162 --pos->col;
3163 #ifdef FEAT_MBYTE
3164 pos->col -= (*mb_head_off)(line, line + pos->col);
3165 #endif
3167 cclass = get_mouse_class(line + pos->col);
3168 while (line[pos->col] != NUL)
3170 #ifdef FEAT_MBYTE
3171 col = pos->col + (*mb_ptr2len)(line + pos->col);
3172 #else
3173 col = pos->col + 1;
3174 #endif
3175 if (get_mouse_class(line + col) != cclass)
3177 if (*p_sel == 'e')
3178 pos->col = col;
3179 break;
3181 pos->col = col;
3186 * Get class of a character for selection: same class means same word.
3187 * 0: blank
3188 * 1: punctuation groups
3189 * 2: normal word character
3190 * >2: multi-byte word character.
3192 static int
3193 get_mouse_class(p)
3194 char_u *p;
3196 int c;
3198 #ifdef FEAT_MBYTE
3199 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3200 return mb_get_class(p);
3201 #endif
3203 c = *p;
3204 if (c == ' ' || c == '\t')
3205 return 0;
3207 if (vim_iswordc(c))
3208 return 2;
3211 * There are a few special cases where we want certain combinations of
3212 * characters to be considered as a single word. These are things like
3213 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
3214 * character is in it's own class.
3216 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3217 return 1;
3218 return c;
3220 #endif /* FEAT_VISUAL */
3221 #endif /* FEAT_MOUSE */
3223 #if defined(FEAT_VISUAL) || defined(PROTO)
3225 * Check if highlighting for visual mode is possible, give a warning message
3226 * if not.
3228 void
3229 check_visual_highlight()
3231 static int did_check = FALSE;
3233 if (full_screen)
3235 if (!did_check && hl_attr(HLF_V) == 0)
3236 MSG(_("Warning: terminal cannot highlight"));
3237 did_check = TRUE;
3242 * End Visual mode.
3243 * This function should ALWAYS be called to end Visual mode, except from
3244 * do_pending_operator().
3246 void
3247 end_visual_mode()
3249 #ifdef FEAT_CLIPBOARD
3251 * If we are using the clipboard, then remember what was selected in case
3252 * we need to paste it somewhere while we still own the selection.
3253 * Only do this when the clipboard is already owned. Don't want to grab
3254 * the selection when hitting ESC.
3256 if (clip_star.available && clip_star.owned)
3257 clip_auto_select();
3258 #endif
3260 VIsual_active = FALSE;
3261 #ifdef FEAT_MOUSE
3262 setmouse();
3263 mouse_dragging = 0;
3264 #endif
3266 /* Save the current VIsual area for '< and '> marks, and "gv" */
3267 curbuf->b_visual.vi_mode = VIsual_mode;
3268 curbuf->b_visual.vi_start = VIsual;
3269 curbuf->b_visual.vi_end = curwin->w_cursor;
3270 curbuf->b_visual.vi_curswant = curwin->w_curswant;
3271 #ifdef FEAT_EVAL
3272 curbuf->b_visual_mode_eval = VIsual_mode;
3273 #endif
3274 #ifdef FEAT_VIRTUALEDIT
3275 if (!virtual_active())
3276 curwin->w_cursor.coladd = 0;
3277 #endif
3279 if (mode_displayed)
3280 clear_cmdline = TRUE; /* unshow visual mode later */
3281 #ifdef FEAT_CMDL_INFO
3282 else
3283 clear_showcmd();
3284 #endif
3286 adjust_cursor_eol();
3290 * Reset VIsual_active and VIsual_reselect.
3292 void
3293 reset_VIsual_and_resel()
3295 if (VIsual_active)
3297 end_visual_mode();
3298 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3300 VIsual_reselect = FALSE;
3304 * Reset VIsual_active and VIsual_reselect if it's set.
3306 void
3307 reset_VIsual()
3309 if (VIsual_active)
3311 end_visual_mode();
3312 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3313 VIsual_reselect = FALSE;
3316 #endif /* FEAT_VISUAL */
3318 #if defined(FEAT_BEVAL)
3319 static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3322 * Check for a balloon-eval special item to include when searching for an
3323 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3324 * Returns TRUE if the character at "*ptr" should be included.
3325 * "dir" is FORWARD or BACKWARD, the direction of searching.
3326 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3327 * "bnp" points to a counter for square brackets.
3329 static int
3330 find_is_eval_item(ptr, colp, bnp, dir)
3331 char_u *ptr;
3332 int *colp;
3333 int *bnp;
3334 int dir;
3336 /* Accept everything inside []. */
3337 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3338 ++*bnp;
3339 if (*bnp > 0)
3341 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3342 --*bnp;
3343 return TRUE;
3346 /* skip over "s.var" */
3347 if (*ptr == '.')
3348 return TRUE;
3350 /* two-character item: s->var */
3351 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3352 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3354 *colp += dir;
3355 return TRUE;
3357 return FALSE;
3359 #endif
3362 * Find the identifier under or to the right of the cursor.
3363 * "find_type" can have one of three values:
3364 * FIND_IDENT: find an identifier (keyword)
3365 * FIND_STRING: find any non-white string
3366 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
3367 * FIND_EVAL: find text useful for C program debugging
3369 * There are three steps:
3370 * 1. Search forward for the start of an identifier/string. Doesn't move if
3371 * already on one.
3372 * 2. Search backward for the start of this identifier/string.
3373 * This doesn't match the real Vi but I like it a little better and it
3374 * shouldn't bother anyone.
3375 * 3. Search forward to the end of this identifier/string.
3376 * When FIND_IDENT isn't defined, we backup until a blank.
3378 * Returns the length of the string, or zero if no string is found.
3379 * If a string is found, a pointer to the string is put in "*string". This
3380 * string is not always NUL terminated.
3383 find_ident_under_cursor(string, find_type)
3384 char_u **string;
3385 int find_type;
3387 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3388 curwin->w_cursor.col, string, find_type);
3392 * Like find_ident_under_cursor(), but for any window and any position.
3393 * However: Uses 'iskeyword' from the current window!.
3396 find_ident_at_pos(wp, lnum, startcol, string, find_type)
3397 win_T *wp;
3398 linenr_T lnum;
3399 colnr_T startcol;
3400 char_u **string;
3401 int find_type;
3403 char_u *ptr;
3404 int col = 0; /* init to shut up GCC */
3405 int i;
3406 #ifdef FEAT_MBYTE
3407 int this_class = 0;
3408 int prev_class;
3409 int prevcol;
3410 #endif
3411 #if defined(FEAT_BEVAL)
3412 int bn = 0; /* bracket nesting */
3413 #endif
3416 * if i == 0: try to find an identifier
3417 * if i == 1: try to find any non-white string
3419 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3420 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3423 * 1. skip to start of identifier/string
3425 col = startcol;
3426 #ifdef FEAT_MBYTE
3427 if (has_mbyte)
3429 while (ptr[col] != NUL)
3431 # if defined(FEAT_BEVAL)
3432 /* Stop at a ']' to evaluate "a[x]". */
3433 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3434 break;
3435 # endif
3436 this_class = mb_get_class(ptr + col);
3437 if (this_class != 0 && (i == 1 || this_class != 1))
3438 break;
3439 col += (*mb_ptr2len)(ptr + col);
3442 else
3443 #endif
3444 while (ptr[col] != NUL
3445 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
3446 # if defined(FEAT_BEVAL)
3447 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3448 # endif
3450 ++col;
3452 #if defined(FEAT_BEVAL)
3453 /* When starting on a ']' count it, so that we include the '['. */
3454 bn = ptr[col] == ']';
3455 #endif
3458 * 2. Back up to start of identifier/string.
3460 #ifdef FEAT_MBYTE
3461 if (has_mbyte)
3463 /* Remember class of character under cursor. */
3464 # if defined(FEAT_BEVAL)
3465 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3466 this_class = mb_get_class((char_u *)"a");
3467 else
3468 # endif
3469 this_class = mb_get_class(ptr + col);
3470 while (col > 0 && this_class != 0)
3472 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3473 prev_class = mb_get_class(ptr + prevcol);
3474 if (this_class != prev_class
3475 && (i == 0
3476 || prev_class == 0
3477 || (find_type & FIND_IDENT))
3478 # if defined(FEAT_BEVAL)
3479 && (!(find_type & FIND_EVAL)
3480 || prevcol == 0
3481 || !find_is_eval_item(ptr + prevcol, &prevcol,
3482 &bn, BACKWARD))
3483 # endif
3485 break;
3486 col = prevcol;
3489 /* If we don't want just any old string, or we've found an
3490 * identifier, stop searching. */
3491 if (this_class > 2)
3492 this_class = 2;
3493 if (!(find_type & FIND_STRING) || this_class == 2)
3494 break;
3496 else
3497 #endif
3499 while (col > 0
3500 && ((i == 0
3501 ? vim_iswordc(ptr[col - 1])
3502 : (!vim_iswhite(ptr[col - 1])
3503 && (!(find_type & FIND_IDENT)
3504 || !vim_iswordc(ptr[col - 1]))))
3505 #if defined(FEAT_BEVAL)
3506 || ((find_type & FIND_EVAL)
3507 && col > 1
3508 && find_is_eval_item(ptr + col - 1, &col,
3509 &bn, BACKWARD))
3510 #endif
3512 --col;
3514 /* If we don't want just any old string, or we've found an
3515 * identifier, stop searching. */
3516 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3517 break;
3521 if (ptr[col] == NUL || (i == 0 && (
3522 #ifdef FEAT_MBYTE
3523 has_mbyte ? this_class != 2 :
3524 #endif
3525 !vim_iswordc(ptr[col]))))
3528 * didn't find an identifier or string
3530 if (find_type & FIND_STRING)
3531 EMSG(_("E348: No string under cursor"));
3532 else
3533 EMSG(_(e_noident));
3534 return 0;
3536 ptr += col;
3537 *string = ptr;
3540 * 3. Find the end if the identifier/string.
3542 #if defined(FEAT_BEVAL)
3543 bn = 0;
3544 startcol -= col;
3545 #endif
3546 col = 0;
3547 #ifdef FEAT_MBYTE
3548 if (has_mbyte)
3550 /* Search for point of changing multibyte character class. */
3551 this_class = mb_get_class(ptr);
3552 while (ptr[col] != NUL
3553 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3554 : mb_get_class(ptr + col) != 0)
3555 # if defined(FEAT_BEVAL)
3556 || ((find_type & FIND_EVAL)
3557 && col <= (int)startcol
3558 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3559 # endif
3561 col += (*mb_ptr2len)(ptr + col);
3563 else
3564 #endif
3565 while ((i == 0 ? vim_iswordc(ptr[col])
3566 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
3567 # if defined(FEAT_BEVAL)
3568 || ((find_type & FIND_EVAL)
3569 && col <= (int)startcol
3570 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3571 # endif
3574 ++col;
3577 return col;
3581 * Prepare for redo of a normal command.
3583 static void
3584 prep_redo_cmd(cap)
3585 cmdarg_T *cap;
3587 prep_redo(cap->oap->regname, cap->count0,
3588 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3592 * Prepare for redo of any command.
3593 * Note that only the last argument can be a multi-byte char.
3595 static void
3596 prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3597 int regname;
3598 long num;
3599 int cmd1;
3600 int cmd2;
3601 int cmd3;
3602 int cmd4;
3603 int cmd5;
3605 ResetRedobuff();
3606 if (regname != 0) /* yank from specified buffer */
3608 AppendCharToRedobuff('"');
3609 AppendCharToRedobuff(regname);
3611 if (num)
3612 AppendNumberToRedobuff(num);
3614 if (cmd1 != NUL)
3615 AppendCharToRedobuff(cmd1);
3616 if (cmd2 != NUL)
3617 AppendCharToRedobuff(cmd2);
3618 if (cmd3 != NUL)
3619 AppendCharToRedobuff(cmd3);
3620 if (cmd4 != NUL)
3621 AppendCharToRedobuff(cmd4);
3622 if (cmd5 != NUL)
3623 AppendCharToRedobuff(cmd5);
3627 * check for operator active and clear it
3629 * return TRUE if operator was active
3631 static int
3632 checkclearop(oap)
3633 oparg_T *oap;
3635 if (oap->op_type == OP_NOP)
3636 return FALSE;
3637 clearopbeep(oap);
3638 return TRUE;
3642 * Check for operator or Visual active. Clear active operator.
3644 * Return TRUE if operator or Visual was active.
3646 static int
3647 checkclearopq(oap)
3648 oparg_T *oap;
3650 if (oap->op_type == OP_NOP
3651 #ifdef FEAT_VISUAL
3652 && !VIsual_active
3653 #endif
3655 return FALSE;
3656 clearopbeep(oap);
3657 return TRUE;
3660 static void
3661 clearop(oap)
3662 oparg_T *oap;
3664 oap->op_type = OP_NOP;
3665 oap->regname = 0;
3666 oap->motion_force = NUL;
3667 oap->use_reg_one = FALSE;
3670 static void
3671 clearopbeep(oap)
3672 oparg_T *oap;
3674 clearop(oap);
3675 beep_flush();
3678 #ifdef FEAT_VISUAL
3680 * Remove the shift modifier from a special key.
3682 static void
3683 unshift_special(cap)
3684 cmdarg_T *cap;
3686 switch (cap->cmdchar)
3688 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3689 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3690 case K_S_UP: cap->cmdchar = K_UP; break;
3691 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3692 case K_S_HOME: cap->cmdchar = K_HOME; break;
3693 case K_S_END: cap->cmdchar = K_END; break;
3695 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3697 #endif
3699 #if defined(FEAT_CMDL_INFO) || defined(PROTO)
3701 * Routines for displaying a partly typed command
3704 #ifdef FEAT_VISUAL /* need room for size of Visual area */
3705 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3706 #else
3707 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3708 #endif
3709 static char_u showcmd_buf[SHOWCMD_BUFLEN];
3710 static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3711 static int showcmd_is_clear = TRUE;
3712 static int showcmd_visual = FALSE;
3714 static void display_showcmd __ARGS((void));
3716 void
3717 clear_showcmd()
3719 if (!p_sc)
3720 return;
3722 #ifdef FEAT_VISUAL
3723 if (VIsual_active && !char_avail())
3725 int i = lt(VIsual, curwin->w_cursor);
3726 long lines;
3727 colnr_T leftcol, rightcol;
3728 linenr_T top, bot;
3730 /* Show the size of the Visual area. */
3731 if (i)
3733 top = VIsual.lnum;
3734 bot = curwin->w_cursor.lnum;
3736 else
3738 top = curwin->w_cursor.lnum;
3739 bot = VIsual.lnum;
3741 # ifdef FEAT_FOLDING
3742 /* Include closed folds as a whole. */
3743 hasFolding(top, &top, NULL);
3744 hasFolding(bot, NULL, &bot);
3745 # endif
3746 lines = bot - top + 1;
3748 if (VIsual_mode == Ctrl_V)
3750 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
3751 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3752 (long)(rightcol - leftcol + 1));
3754 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3755 sprintf((char *)showcmd_buf, "%ld", lines);
3756 else
3757 sprintf((char *)showcmd_buf, "%ld", (long)(i
3758 ? curwin->w_cursor.col - VIsual.col
3759 : VIsual.col - curwin->w_cursor.col) + (*p_sel != 'e'));
3760 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3761 showcmd_visual = TRUE;
3763 else
3764 #endif
3766 showcmd_buf[0] = NUL;
3767 showcmd_visual = FALSE;
3769 /* Don't actually display something if there is nothing to clear. */
3770 if (showcmd_is_clear)
3771 return;
3774 display_showcmd();
3778 * Add 'c' to string of shown command chars.
3779 * Return TRUE if output has been written (and setcursor() has been called).
3782 add_to_showcmd(c)
3783 int c;
3785 char_u *p;
3786 int old_len;
3787 int extra_len;
3788 int overflow;
3789 #if defined(FEAT_MOUSE)
3790 int i;
3791 static int ignore[] =
3793 # ifdef FEAT_GUI
3794 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3795 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
3796 # endif
3797 K_IGNORE,
3798 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3799 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3800 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
3801 K_MOUSEDOWN, K_MOUSEUP,
3802 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
3803 K_CURSORHOLD,
3806 #endif
3808 if (!p_sc || msg_silent != 0)
3809 return FALSE;
3811 if (showcmd_visual)
3813 showcmd_buf[0] = NUL;
3814 showcmd_visual = FALSE;
3817 #if defined(FEAT_MOUSE)
3818 /* Ignore keys that are scrollbar updates and mouse clicks */
3819 if (IS_SPECIAL(c))
3820 for (i = 0; ignore[i] != 0; ++i)
3821 if (ignore[i] == c)
3822 return FALSE;
3823 #endif
3825 p = transchar(c);
3826 old_len = (int)STRLEN(showcmd_buf);
3827 extra_len = (int)STRLEN(p);
3828 overflow = old_len + extra_len - SHOWCMD_COLS;
3829 if (overflow > 0)
3830 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3831 old_len - overflow + 1);
3832 STRCAT(showcmd_buf, p);
3834 if (char_avail())
3835 return FALSE;
3837 display_showcmd();
3839 return TRUE;
3842 void
3843 add_to_showcmd_c(c)
3844 int c;
3846 if (!add_to_showcmd(c))
3847 setcursor();
3851 * Delete 'len' characters from the end of the shown command.
3853 static void
3854 del_from_showcmd(len)
3855 int len;
3857 int old_len;
3859 if (!p_sc)
3860 return;
3862 old_len = (int)STRLEN(showcmd_buf);
3863 if (len > old_len)
3864 len = old_len;
3865 showcmd_buf[old_len - len] = NUL;
3867 if (!char_avail())
3868 display_showcmd();
3872 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3873 * something and there is a partial mapping.
3875 void
3876 push_showcmd()
3878 if (p_sc)
3879 STRCPY(old_showcmd_buf, showcmd_buf);
3882 void
3883 pop_showcmd()
3885 if (!p_sc)
3886 return;
3888 STRCPY(showcmd_buf, old_showcmd_buf);
3890 display_showcmd();
3893 static void
3894 display_showcmd()
3896 int len;
3898 cursor_off();
3900 len = (int)STRLEN(showcmd_buf);
3901 if (len == 0)
3902 showcmd_is_clear = TRUE;
3903 else
3905 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3906 showcmd_is_clear = FALSE;
3910 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3911 * spaces
3913 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3915 setcursor(); /* put cursor back where it belongs */
3917 #endif
3919 #ifdef FEAT_SCROLLBIND
3921 * When "check" is FALSE, prepare for commands that scroll the window.
3922 * When "check" is TRUE, take care of scroll-binding after the window has
3923 * scrolled. Called from normal_cmd() and edit().
3925 void
3926 do_check_scrollbind(check)
3927 int check;
3929 static win_T *old_curwin = NULL;
3930 static linenr_T old_topline = 0;
3931 #ifdef FEAT_DIFF
3932 static int old_topfill = 0;
3933 #endif
3934 static buf_T *old_buf = NULL;
3935 static colnr_T old_leftcol = 0;
3937 if (check && curwin->w_p_scb)
3939 /* If a ":syncbind" command was just used, don't scroll, only reset
3940 * the values. */
3941 if (did_syncbind)
3942 did_syncbind = FALSE;
3943 else if (curwin == old_curwin)
3946 * Synchronize other windows, as necessary according to
3947 * 'scrollbind'. Don't do this after an ":edit" command, except
3948 * when 'diff' is set.
3950 if ((curwin->w_buffer == old_buf
3951 #ifdef FEAT_DIFF
3952 || curwin->w_p_diff
3953 #endif
3955 && (curwin->w_topline != old_topline
3956 #ifdef FEAT_DIFF
3957 || curwin->w_topfill != old_topfill
3958 #endif
3959 || curwin->w_leftcol != old_leftcol))
3961 check_scrollbind(curwin->w_topline - old_topline,
3962 (long)(curwin->w_leftcol - old_leftcol));
3965 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
3968 * When switching between windows, make sure that the relative
3969 * vertical offset is valid for the new window. The relative
3970 * offset is invalid whenever another 'scrollbind' window has
3971 * scrolled to a point that would force the current window to
3972 * scroll past the beginning or end of its buffer. When the
3973 * resync is performed, some of the other 'scrollbind' windows may
3974 * need to jump so that the current window's relative position is
3975 * visible on-screen.
3977 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
3979 curwin->w_scbind_pos = curwin->w_topline;
3982 old_curwin = curwin;
3983 old_topline = curwin->w_topline;
3984 #ifdef FEAT_DIFF
3985 old_topfill = curwin->w_topfill;
3986 #endif
3987 old_buf = curwin->w_buffer;
3988 old_leftcol = curwin->w_leftcol;
3992 * Synchronize any windows that have "scrollbind" set, based on the
3993 * number of rows by which the current window has changed
3994 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3996 void
3997 check_scrollbind(topline_diff, leftcol_diff)
3998 linenr_T topline_diff;
3999 long leftcol_diff;
4001 int want_ver;
4002 int want_hor;
4003 win_T *old_curwin = curwin;
4004 buf_T *old_curbuf = curbuf;
4005 #ifdef FEAT_VISUAL
4006 int old_VIsual_select = VIsual_select;
4007 int old_VIsual_active = VIsual_active;
4008 #endif
4009 colnr_T tgt_leftcol = curwin->w_leftcol;
4010 long topline;
4011 long y;
4014 * check 'scrollopt' string for vertical and horizontal scroll options
4016 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4017 #ifdef FEAT_DIFF
4018 want_ver |= old_curwin->w_p_diff;
4019 #endif
4020 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4023 * loop through the scrollbound windows and scroll accordingly
4025 #ifdef FEAT_VISUAL
4026 VIsual_select = VIsual_active = 0;
4027 #endif
4028 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4030 curbuf = curwin->w_buffer;
4031 /* skip original window and windows with 'noscrollbind' */
4032 if (curwin != old_curwin && curwin->w_p_scb)
4035 * do the vertical scroll
4037 if (want_ver)
4039 #ifdef FEAT_DIFF
4040 if (old_curwin->w_p_diff && curwin->w_p_diff)
4042 diff_set_topline(old_curwin, curwin);
4044 else
4045 #endif
4047 curwin->w_scbind_pos += topline_diff;
4048 topline = curwin->w_scbind_pos;
4049 if (topline > curbuf->b_ml.ml_line_count)
4050 topline = curbuf->b_ml.ml_line_count;
4051 if (topline < 1)
4052 topline = 1;
4054 y = topline - curwin->w_topline;
4055 if (y > 0)
4056 scrollup(y, FALSE);
4057 else
4058 scrolldown(-y, FALSE);
4061 redraw_later(VALID);
4062 cursor_correct();
4063 #ifdef FEAT_WINDOWS
4064 curwin->w_redr_status = TRUE;
4065 #endif
4069 * do the horizontal scroll
4071 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4073 curwin->w_leftcol = tgt_leftcol;
4074 leftcol_changed();
4080 * reset current-window
4082 #ifdef FEAT_VISUAL
4083 VIsual_select = old_VIsual_select;
4084 VIsual_active = old_VIsual_active;
4085 #endif
4086 curwin = old_curwin;
4087 curbuf = old_curbuf;
4089 #endif /* #ifdef FEAT_SCROLLBIND */
4092 * Command character that's ignored.
4093 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
4094 * xon/xoff
4096 static void
4097 nv_ignore(cap)
4098 cmdarg_T *cap;
4100 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
4104 * Command character that doesn't do anything, but unlike nv_ignore() does
4105 * start edit(). Used for "startinsert" executed while starting up.
4107 /*ARGSUSED */
4108 static void
4109 nv_nop(cap)
4110 cmdarg_T *cap;
4115 * Command character doesn't exist.
4117 static void
4118 nv_error(cap)
4119 cmdarg_T *cap;
4121 clearopbeep(cap->oap);
4125 * <Help> and <F1> commands.
4127 static void
4128 nv_help(cap)
4129 cmdarg_T *cap;
4131 if (!checkclearopq(cap->oap))
4132 ex_help(NULL);
4136 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4138 static void
4139 nv_addsub(cap)
4140 cmdarg_T *cap;
4142 if (!checkclearopq(cap->oap)
4143 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4144 prep_redo_cmd(cap);
4148 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4150 static void
4151 nv_page(cap)
4152 cmdarg_T *cap;
4154 if (!checkclearop(cap->oap))
4156 #ifdef FEAT_WINDOWS
4157 if (mod_mask & MOD_MASK_CTRL)
4159 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4160 if (cap->arg == BACKWARD)
4161 goto_tabpage(-(int)cap->count1);
4162 else
4163 goto_tabpage((int)cap->count0);
4165 else
4166 #endif
4167 (void)onepage(cap->arg, cap->count1);
4172 * Implementation of "gd" and "gD" command.
4174 static void
4175 nv_gd(oap, nchar, thisblock)
4176 oparg_T *oap;
4177 int nchar;
4178 int thisblock; /* 1 for "1gd" and "1gD" */
4180 int len;
4181 char_u *ptr;
4183 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
4184 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
4185 clearopbeep(oap);
4186 #ifdef FEAT_FOLDING
4187 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4188 foldOpenCursor();
4189 #endif
4193 * Search for variable declaration of "ptr[len]".
4194 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4195 * current file ("gD").
4196 * When "thisblock" is TRUE check the {} block scope.
4197 * Return FAIL when not found.
4200 find_decl(ptr, len, locally, thisblock, searchflags)
4201 char_u *ptr;
4202 int len;
4203 int locally;
4204 int thisblock;
4205 int searchflags; /* flags passed to searchit() */
4207 char_u *pat;
4208 pos_T old_pos;
4209 pos_T par_pos;
4210 pos_T found_pos;
4211 int t;
4212 int save_p_ws;
4213 int save_p_scs;
4214 int retval = OK;
4215 int incll;
4217 if ((pat = alloc(len + 7)) == NULL)
4218 return FAIL;
4220 /* Put "\V" before the pattern to avoid that the special meaning of "."
4221 * and "~" causes trouble. */
4222 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4223 len, ptr);
4224 old_pos = curwin->w_cursor;
4225 save_p_ws = p_ws;
4226 save_p_scs = p_scs;
4227 p_ws = FALSE; /* don't wrap around end of file now */
4228 p_scs = FALSE; /* don't switch ignorecase off now */
4231 * With "gD" go to line 1.
4232 * With "gd" Search back for the start of the current function, then go
4233 * back until a blank line. If this fails go to line 1.
4235 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
4237 setpcmark(); /* Set in findpar() otherwise */
4238 curwin->w_cursor.lnum = 1;
4239 par_pos = curwin->w_cursor;
4241 else
4243 par_pos = curwin->w_cursor;
4244 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4245 --curwin->w_cursor.lnum;
4247 curwin->w_cursor.col = 0;
4249 /* Search forward for the identifier, ignore comment lines. */
4250 clearpos(&found_pos);
4251 for (;;)
4253 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
4254 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
4255 if (curwin->w_cursor.lnum >= old_pos.lnum)
4256 t = FAIL; /* match after start is failure too */
4258 if (thisblock && t != FAIL)
4260 pos_T *pos;
4262 /* Check that the block the match is in doesn't end before the
4263 * position where we started the search from. */
4264 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4265 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4266 && pos->lnum < old_pos.lnum)
4267 continue;
4270 if (t == FAIL)
4272 /* If we previously found a valid position, use it. */
4273 if (found_pos.lnum != 0)
4275 curwin->w_cursor = found_pos;
4276 t = OK;
4278 break;
4280 #ifdef FEAT_COMMENTS
4281 if (get_leader_len(ml_get_curline(), NULL, FALSE) > 0)
4283 /* Ignore this line, continue at start of next line. */
4284 ++curwin->w_cursor.lnum;
4285 curwin->w_cursor.col = 0;
4286 continue;
4288 #endif
4289 if (!locally) /* global search: use first match found */
4290 break;
4291 if (curwin->w_cursor.lnum >= par_pos.lnum)
4293 /* If we previously found a valid position, use it. */
4294 if (found_pos.lnum != 0)
4295 curwin->w_cursor = found_pos;
4296 break;
4299 /* For finding a local variable and the match is before the "{" search
4300 * to find a later match. For K&R style function declarations this
4301 * skips the function header without types. */
4302 found_pos = curwin->w_cursor;
4305 if (t == FAIL)
4307 retval = FAIL;
4308 curwin->w_cursor = old_pos;
4310 else
4312 curwin->w_set_curswant = TRUE;
4313 /* "n" searches forward now */
4314 reset_search_dir();
4317 vim_free(pat);
4318 p_ws = save_p_ws;
4319 p_scs = save_p_scs;
4321 return retval;
4325 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4326 * lines rather than lines in the file.
4327 * 'dist' must be positive.
4329 * Return OK if able to move cursor, FAIL otherwise.
4331 static int
4332 nv_screengo(oap, dir, dist)
4333 oparg_T *oap;
4334 int dir;
4335 long dist;
4337 int linelen = linetabsize(ml_get_curline());
4338 int retval = OK;
4339 int atend = FALSE;
4340 int n;
4341 int col_off1; /* margin offset for first screen line */
4342 int col_off2; /* margin offset for wrapped screen line */
4343 int width1; /* text width for first screen line */
4344 int width2; /* test width for wrapped screen line */
4346 oap->motion_type = MCHAR;
4347 oap->inclusive = FALSE;
4349 col_off1 = curwin_col_off();
4350 col_off2 = col_off1 - curwin_col_off2();
4351 width1 = W_WIDTH(curwin) - col_off1;
4352 width2 = W_WIDTH(curwin) - col_off2;
4354 #ifdef FEAT_VERTSPLIT
4355 if (curwin->w_width != 0)
4357 #endif
4359 * Instead of sticking at the last character of the buffer line we
4360 * try to stick in the last column of the screen.
4362 if (curwin->w_curswant == MAXCOL)
4364 atend = TRUE;
4365 validate_virtcol();
4366 if (width1 <= 0)
4367 curwin->w_curswant = 0;
4368 else
4370 curwin->w_curswant = width1 - 1;
4371 if (curwin->w_virtcol > curwin->w_curswant)
4372 curwin->w_curswant += ((curwin->w_virtcol
4373 - curwin->w_curswant - 1) / width2 + 1) * width2;
4376 else
4378 if (linelen > width1)
4379 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4380 else
4381 n = width1;
4382 if (curwin->w_curswant > (colnr_T)n + 1)
4383 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4384 * width2;
4387 while (dist--)
4389 if (dir == BACKWARD)
4391 if ((long)curwin->w_curswant >= width2)
4392 /* move back within line */
4393 curwin->w_curswant -= width2;
4394 else
4396 /* to previous line */
4397 if (curwin->w_cursor.lnum == 1)
4399 retval = FAIL;
4400 break;
4402 --curwin->w_cursor.lnum;
4403 #ifdef FEAT_FOLDING
4404 /* Move to the start of a closed fold. Don't do that when
4405 * 'foldopen' contains "all": it will open in a moment. */
4406 if (!(fdo_flags & FDO_ALL))
4407 (void)hasFolding(curwin->w_cursor.lnum,
4408 &curwin->w_cursor.lnum, NULL);
4409 #endif
4410 linelen = linetabsize(ml_get_curline());
4411 if (linelen > width1)
4412 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4413 + 1) * width2;
4416 else /* dir == FORWARD */
4418 if (linelen > width1)
4419 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4420 else
4421 n = width1;
4422 if (curwin->w_curswant + width2 < (colnr_T)n)
4423 /* move forward within line */
4424 curwin->w_curswant += width2;
4425 else
4427 /* to next line */
4428 #ifdef FEAT_FOLDING
4429 /* Move to the end of a closed fold. */
4430 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4431 &curwin->w_cursor.lnum);
4432 #endif
4433 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4435 retval = FAIL;
4436 break;
4438 curwin->w_cursor.lnum++;
4439 curwin->w_curswant %= width2;
4443 #ifdef FEAT_VERTSPLIT
4445 #endif
4447 coladvance(curwin->w_curswant);
4449 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4450 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4453 * Check for landing on a character that got split at the end of the
4454 * last line. We want to advance a screenline, not end up in the same
4455 * screenline or move two screenlines.
4457 validate_virtcol();
4458 if (curwin->w_virtcol > curwin->w_curswant
4459 && (curwin->w_curswant < (colnr_T)width1
4460 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4461 : ((curwin->w_curswant - width1) % width2
4462 > (colnr_T)width2 / 2)))
4463 --curwin->w_cursor.col;
4465 #endif
4467 if (atend)
4468 curwin->w_curswant = MAXCOL; /* stick in the last column */
4470 return retval;
4473 #ifdef FEAT_MOUSE
4475 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4476 * when Shift or Ctrl is used.
4477 * K_MOUSEUP (cap->arg == TRUE) or K_MOUSEDOWN (cap->arg == FALSE)
4479 static void
4480 nv_mousescroll(cap)
4481 cmdarg_T *cap;
4483 # ifdef FEAT_GUI_SCROLL_WHEEL_FORCE
4484 int scroll_wheel_force = 0;
4485 # endif
4486 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4487 win_T *old_curwin = curwin;
4489 /* Currently we only get the mouse coordinates in the GUI. */
4490 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4492 int row, col;
4494 row = mouse_row;
4495 col = mouse_col;
4497 /* find the window at the pointer coordinates */
4498 curwin = mouse_find_win(&row, &col);
4499 curbuf = curwin->w_buffer;
4501 # endif
4503 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4505 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4507 else
4509 # ifdef FEAT_GUI_SCROLL_WHEEL_FORCE
4510 if (gui.in_use && gui.scroll_wheel_force >= 1) {
4511 scroll_wheel_force = gui.scroll_wheel_force;
4512 if (scroll_wheel_force > 1000)
4513 scroll_wheel_force = 1000;
4515 cap->count1 = scroll_wheel_force;
4516 cap->count0 = scroll_wheel_force;
4517 } else {
4518 cap->count1 = 3;
4519 cap->count0 = 3;
4521 # else
4522 cap->count1 = 3;
4523 cap->count0 = 3;
4524 # endif
4525 nv_scroll_line(cap);
4528 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4529 curwin->w_redr_status = TRUE;
4531 curwin = old_curwin;
4532 curbuf = curwin->w_buffer;
4533 # endif
4537 * Mouse clicks and drags.
4539 static void
4540 nv_mouse(cap)
4541 cmdarg_T *cap;
4543 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4545 #endif
4548 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4549 * cap->arg must be TRUE for CTRL-E.
4551 static void
4552 nv_scroll_line(cap)
4553 cmdarg_T *cap;
4555 if (!checkclearop(cap->oap))
4556 scroll_redraw(cap->arg, cap->count1);
4560 * Scroll "count" lines up or down, and redraw.
4562 void
4563 scroll_redraw(up, count)
4564 int up;
4565 long count;
4567 linenr_T prev_topline = curwin->w_topline;
4568 #ifdef FEAT_DIFF
4569 int prev_topfill = curwin->w_topfill;
4570 #endif
4571 linenr_T prev_lnum = curwin->w_cursor.lnum;
4573 if (up)
4574 scrollup(count, TRUE);
4575 else
4576 scrolldown(count, TRUE);
4577 if (p_so)
4579 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4580 * valid, otherwise the screen jumps back at the end of the file. */
4581 cursor_correct();
4582 check_cursor_moved(curwin);
4583 curwin->w_valid |= VALID_TOPLINE;
4585 /* If moved back to where we were, at least move the cursor, otherwise
4586 * we get stuck at one position. Don't move the cursor up if the
4587 * first line of the buffer is already on the screen */
4588 while (curwin->w_topline == prev_topline
4589 #ifdef FEAT_DIFF
4590 && curwin->w_topfill == prev_topfill
4591 #endif
4594 if (up)
4596 if (curwin->w_cursor.lnum > prev_lnum
4597 || cursor_down(1L, FALSE) == FAIL)
4598 break;
4600 else
4602 if (curwin->w_cursor.lnum < prev_lnum
4603 || prev_topline == 1L
4604 || cursor_up(1L, FALSE) == FAIL)
4605 break;
4607 /* Mark w_topline as valid, otherwise the screen jumps back at the
4608 * end of the file. */
4609 check_cursor_moved(curwin);
4610 curwin->w_valid |= VALID_TOPLINE;
4613 if (curwin->w_cursor.lnum != prev_lnum)
4614 coladvance(curwin->w_curswant);
4615 redraw_later(VALID);
4619 * Commands that start with "z".
4621 static void
4622 nv_zet(cap)
4623 cmdarg_T *cap;
4625 long n;
4626 colnr_T col;
4627 int nchar = cap->nchar;
4628 #ifdef FEAT_FOLDING
4629 long old_fdl = curwin->w_p_fdl;
4630 int old_fen = curwin->w_p_fen;
4631 #endif
4632 #ifdef FEAT_SPELL
4633 int undo = FALSE;
4634 #endif
4636 if (VIM_ISDIGIT(nchar))
4639 * "z123{nchar}": edit the count before obtaining {nchar}
4641 if (checkclearop(cap->oap))
4642 return;
4643 n = nchar - '0';
4644 for (;;)
4646 #ifdef USE_ON_FLY_SCROLL
4647 dont_scroll = TRUE; /* disallow scrolling here */
4648 #endif
4649 ++no_mapping;
4650 ++allow_keys; /* no mapping for nchar, but allow key codes */
4651 nchar = plain_vgetc();
4652 #ifdef FEAT_LANGMAP
4653 LANGMAP_ADJUST(nchar, TRUE);
4654 #endif
4655 --no_mapping;
4656 --allow_keys;
4657 #ifdef FEAT_CMDL_INFO
4658 (void)add_to_showcmd(nchar);
4659 #endif
4660 if (nchar == K_DEL || nchar == K_KDEL)
4661 n /= 10;
4662 else if (VIM_ISDIGIT(nchar))
4663 n = n * 10 + (nchar - '0');
4664 else if (nchar == CAR)
4666 #ifdef FEAT_GUI
4667 need_mouse_correct = TRUE;
4668 #endif
4669 win_setheight((int)n);
4670 break;
4672 else if (nchar == 'l'
4673 || nchar == 'h'
4674 || nchar == K_LEFT
4675 || nchar == K_RIGHT)
4677 cap->count1 = n ? n * cap->count1 : cap->count1;
4678 goto dozet;
4680 else
4682 clearopbeep(cap->oap);
4683 break;
4686 cap->oap->op_type = OP_NOP;
4687 return;
4690 dozet:
4691 if (
4692 #ifdef FEAT_FOLDING
4693 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4694 * and "zC" only in Visual mode. "zj" and "zk" are motion
4695 * commands. */
4696 cap->nchar != 'f' && cap->nchar != 'F'
4697 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4698 && cap->nchar != 'j' && cap->nchar != 'k'
4700 #endif
4701 checkclearop(cap->oap))
4702 return;
4705 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4706 * If line number given, set cursor.
4708 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4709 && cap->count0
4710 && cap->count0 != curwin->w_cursor.lnum)
4712 setpcmark();
4713 if (cap->count0 > curbuf->b_ml.ml_line_count)
4714 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4715 else
4716 curwin->w_cursor.lnum = cap->count0;
4717 check_cursor_col();
4720 switch (nchar)
4722 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4723 case '+':
4724 if (cap->count0 == 0)
4726 /* No count given: put cursor at the line below screen */
4727 validate_botline(); /* make sure w_botline is valid */
4728 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4729 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4730 else
4731 curwin->w_cursor.lnum = curwin->w_botline;
4733 /* FALLTHROUGH */
4734 case NL:
4735 case CAR:
4736 case K_KENTER:
4737 beginline(BL_WHITE | BL_FIX);
4738 /* FALLTHROUGH */
4740 case 't': scroll_cursor_top(0, TRUE);
4741 redraw_later(VALID);
4742 break;
4744 /* "z." and "zz": put cursor in middle of screen */
4745 case '.': beginline(BL_WHITE | BL_FIX);
4746 /* FALLTHROUGH */
4748 case 'z': scroll_cursor_halfway(TRUE);
4749 redraw_later(VALID);
4750 break;
4752 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4753 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4754 * when <count> is at bottom of window, and puts that one at
4755 * bottom of window. */
4756 if (cap->count0 != 0)
4758 scroll_cursor_bot(0, TRUE);
4759 curwin->w_cursor.lnum = curwin->w_topline;
4761 else if (curwin->w_topline == 1)
4762 curwin->w_cursor.lnum = 1;
4763 else
4764 curwin->w_cursor.lnum = curwin->w_topline - 1;
4765 /* FALLTHROUGH */
4766 case '-':
4767 beginline(BL_WHITE | BL_FIX);
4768 /* FALLTHROUGH */
4770 case 'b': scroll_cursor_bot(0, TRUE);
4771 redraw_later(VALID);
4772 break;
4774 /* "zH" - scroll screen right half-page */
4775 case 'H':
4776 cap->count1 *= W_WIDTH(curwin) / 2;
4777 /* FALLTHROUGH */
4779 /* "zh" - scroll screen to the right */
4780 case 'h':
4781 case K_LEFT:
4782 if (!curwin->w_p_wrap)
4784 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4785 curwin->w_leftcol = 0;
4786 else
4787 curwin->w_leftcol -= (colnr_T)cap->count1;
4788 leftcol_changed();
4790 break;
4792 /* "zL" - scroll screen left half-page */
4793 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4794 /* FALLTHROUGH */
4796 /* "zl" - scroll screen to the left */
4797 case 'l':
4798 case K_RIGHT:
4799 if (!curwin->w_p_wrap)
4801 /* scroll the window left */
4802 curwin->w_leftcol += (colnr_T)cap->count1;
4803 leftcol_changed();
4805 break;
4807 /* "zs" - scroll screen, cursor at the start */
4808 case 's': if (!curwin->w_p_wrap)
4810 #ifdef FEAT_FOLDING
4811 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4812 col = 0; /* like the cursor is in col 0 */
4813 else
4814 #endif
4815 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4816 if ((long)col > p_siso)
4817 col -= p_siso;
4818 else
4819 col = 0;
4820 if (curwin->w_leftcol != col)
4822 curwin->w_leftcol = col;
4823 redraw_later(NOT_VALID);
4826 break;
4828 /* "ze" - scroll screen, cursor at the end */
4829 case 'e': if (!curwin->w_p_wrap)
4831 #ifdef FEAT_FOLDING
4832 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4833 col = 0; /* like the cursor is in col 0 */
4834 else
4835 #endif
4836 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4837 n = W_WIDTH(curwin) - curwin_col_off();
4838 if ((long)col + p_siso < n)
4839 col = 0;
4840 else
4841 col = col + p_siso - n + 1;
4842 if (curwin->w_leftcol != col)
4844 curwin->w_leftcol = col;
4845 redraw_later(NOT_VALID);
4848 break;
4850 #ifdef FEAT_FOLDING
4851 /* "zF": create fold command */
4852 /* "zf": create fold operator */
4853 case 'F':
4854 case 'f': if (foldManualAllowed(TRUE))
4856 cap->nchar = 'f';
4857 nv_operator(cap);
4858 curwin->w_p_fen = TRUE;
4860 /* "zF" is like "zfzf" */
4861 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4863 nv_operator(cap);
4864 finish_op = TRUE;
4867 else
4868 clearopbeep(cap->oap);
4869 break;
4871 /* "zd": delete fold at cursor */
4872 /* "zD": delete fold at cursor recursively */
4873 case 'd':
4874 case 'D': if (foldManualAllowed(FALSE))
4876 if (VIsual_active)
4877 nv_operator(cap);
4878 else
4879 deleteFold(curwin->w_cursor.lnum,
4880 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4882 break;
4884 /* "zE": erease all folds */
4885 case 'E': if (foldmethodIsManual(curwin))
4887 clearFolding(curwin);
4888 changed_window_setting();
4890 else if (foldmethodIsMarker(curwin))
4891 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4892 TRUE, FALSE);
4893 else
4894 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4895 break;
4897 /* "zn": fold none: reset 'foldenable' */
4898 case 'n': curwin->w_p_fen = FALSE;
4899 break;
4901 /* "zN": fold Normal: set 'foldenable' */
4902 case 'N': curwin->w_p_fen = TRUE;
4903 break;
4905 /* "zi": invert folding: toggle 'foldenable' */
4906 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4907 break;
4909 /* "za": open closed fold or close open fold at cursor */
4910 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4911 openFold(curwin->w_cursor.lnum, cap->count1);
4912 else
4914 closeFold(curwin->w_cursor.lnum, cap->count1);
4915 curwin->w_p_fen = TRUE;
4917 break;
4919 /* "zA": open fold at cursor recursively */
4920 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4921 openFoldRecurse(curwin->w_cursor.lnum);
4922 else
4924 closeFoldRecurse(curwin->w_cursor.lnum);
4925 curwin->w_p_fen = TRUE;
4927 break;
4929 /* "zo": open fold at cursor or Visual area */
4930 case 'o': if (VIsual_active)
4931 nv_operator(cap);
4932 else
4933 openFold(curwin->w_cursor.lnum, cap->count1);
4934 break;
4936 /* "zO": open fold recursively */
4937 case 'O': if (VIsual_active)
4938 nv_operator(cap);
4939 else
4940 openFoldRecurse(curwin->w_cursor.lnum);
4941 break;
4943 /* "zc": close fold at cursor or Visual area */
4944 case 'c': if (VIsual_active)
4945 nv_operator(cap);
4946 else
4947 closeFold(curwin->w_cursor.lnum, cap->count1);
4948 curwin->w_p_fen = TRUE;
4949 break;
4951 /* "zC": close fold recursively */
4952 case 'C': if (VIsual_active)
4953 nv_operator(cap);
4954 else
4955 closeFoldRecurse(curwin->w_cursor.lnum);
4956 curwin->w_p_fen = TRUE;
4957 break;
4959 /* "zv": open folds at the cursor */
4960 case 'v': foldOpenCursor();
4961 break;
4963 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
4964 case 'x': curwin->w_p_fen = TRUE;
4965 newFoldLevel(); /* update right now */
4966 foldOpenCursor();
4967 break;
4969 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
4970 case 'X': curwin->w_p_fen = TRUE;
4971 old_fdl = -1; /* force an update */
4972 break;
4974 /* "zm": fold more */
4975 case 'm': if (curwin->w_p_fdl > 0)
4976 --curwin->w_p_fdl;
4977 old_fdl = -1; /* force an update */
4978 curwin->w_p_fen = TRUE;
4979 break;
4981 /* "zM": close all folds */
4982 case 'M': curwin->w_p_fdl = 0;
4983 old_fdl = -1; /* force an update */
4984 curwin->w_p_fen = TRUE;
4985 break;
4987 /* "zr": reduce folding */
4988 case 'r': ++curwin->w_p_fdl;
4989 break;
4991 /* "zR": open all folds */
4992 case 'R': curwin->w_p_fdl = getDeepestNesting();
4993 old_fdl = -1; /* force an update */
4994 break;
4996 case 'j': /* "zj" move to next fold downwards */
4997 case 'k': /* "zk" move to next fold upwards */
4998 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
4999 cap->count1) == FAIL)
5000 clearopbeep(cap->oap);
5001 break;
5003 #endif /* FEAT_FOLDING */
5005 #ifdef FEAT_SPELL
5006 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5007 ++no_mapping;
5008 ++allow_keys; /* no mapping for nchar, but allow key codes */
5009 nchar = plain_vgetc();
5010 #ifdef FEAT_LANGMAP
5011 LANGMAP_ADJUST(nchar, TRUE);
5012 #endif
5013 --no_mapping;
5014 --allow_keys;
5015 #ifdef FEAT_CMDL_INFO
5016 (void)add_to_showcmd(nchar);
5017 #endif
5018 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5020 clearopbeep(cap->oap);
5021 break;
5023 undo = TRUE;
5024 /*FALLTHROUGH*/
5026 case 'g': /* "zg": add good word to word list */
5027 case 'w': /* "zw": add wrong word to word list */
5028 case 'G': /* "zG": add good word to temp word list */
5029 case 'W': /* "zW": add wrong word to temp word list */
5031 char_u *ptr = NULL;
5032 int len;
5034 if (checkclearop(cap->oap))
5035 break;
5036 # ifdef FEAT_VISUAL
5037 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5038 == FAIL)
5039 return;
5040 # endif
5041 if (ptr == NULL)
5043 pos_T pos = curwin->w_cursor;
5045 /* Find bad word under the cursor. */
5046 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
5047 if (len != 0 && curwin->w_cursor.col <= pos.col)
5048 ptr = ml_get_pos(&curwin->w_cursor);
5049 curwin->w_cursor = pos;
5052 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5053 FIND_IDENT)) == 0)
5054 return;
5055 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
5056 (nchar == 'G' || nchar == 'W')
5057 ? 0 : (int)cap->count1,
5058 undo);
5060 break;
5062 case '=': /* "z=": suggestions for a badly spelled word */
5063 if (!checkclearop(cap->oap))
5064 spell_suggest((int)cap->count0);
5065 break;
5066 #endif
5068 default: clearopbeep(cap->oap);
5071 #ifdef FEAT_FOLDING
5072 /* Redraw when 'foldenable' changed */
5073 if (old_fen != curwin->w_p_fen)
5075 # ifdef FEAT_DIFF
5076 win_T *wp;
5078 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5080 /* Adjust 'foldenable' in diff-synced windows. */
5081 FOR_ALL_WINDOWS(wp)
5083 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5085 wp->w_p_fen = curwin->w_p_fen;
5086 changed_window_setting_win(wp);
5090 # endif
5091 changed_window_setting();
5094 /* Redraw when 'foldlevel' changed. */
5095 if (old_fdl != curwin->w_p_fdl)
5096 newFoldLevel();
5097 #endif
5100 #ifdef FEAT_GUI
5102 * Vertical scrollbar movement.
5104 static void
5105 nv_ver_scrollbar(cap)
5106 cmdarg_T *cap;
5108 if (cap->oap->op_type != OP_NOP)
5109 clearopbeep(cap->oap);
5111 /* Even if an operator was pending, we still want to scroll */
5112 gui_do_scroll();
5116 * Horizontal scrollbar movement.
5118 static void
5119 nv_hor_scrollbar(cap)
5120 cmdarg_T *cap;
5122 if (cap->oap->op_type != OP_NOP)
5123 clearopbeep(cap->oap);
5125 /* Even if an operator was pending, we still want to scroll */
5126 gui_do_horiz_scroll();
5128 #endif
5130 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
5132 * Click in GUI tab.
5134 static void
5135 nv_tabline(cap)
5136 cmdarg_T *cap;
5138 if (cap->oap->op_type != OP_NOP)
5139 clearopbeep(cap->oap);
5141 /* Even if an operator was pending, we still want to jump tabs. */
5142 goto_tabpage(current_tab);
5146 * Selected item in tab line menu.
5148 static void
5149 nv_tabmenu(cap)
5150 cmdarg_T *cap;
5152 if (cap->oap->op_type != OP_NOP)
5153 clearopbeep(cap->oap);
5155 /* Even if an operator was pending, we still want to jump tabs. */
5156 handle_tabmenu();
5160 * Handle selecting an item of the GUI tab line menu.
5161 * Used in Normal and Insert mode.
5163 void
5164 handle_tabmenu()
5166 switch (current_tabmenu)
5168 case TABLINE_MENU_CLOSE:
5169 if (current_tab == 0)
5170 #ifdef FEAT_GUI_MACVIM
5171 do_cmdline_cmd((char_u *)"conf tabclose");
5172 #else
5173 do_cmdline_cmd((char_u *)"tabclose");
5174 #endif
5175 else
5177 #ifdef FEAT_GUI_MACVIM
5178 vim_snprintf((char *)IObuff, IOSIZE, "conf tabclose %d",
5179 current_tab);
5180 #else
5181 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5182 current_tab);
5183 #endif
5184 do_cmdline_cmd(IObuff);
5186 break;
5188 case TABLINE_MENU_NEW:
5189 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5190 current_tab > 0 ? current_tab - 1 : 999);
5191 do_cmdline_cmd(IObuff);
5192 break;
5194 case TABLINE_MENU_OPEN:
5195 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5196 current_tab > 0 ? current_tab - 1 : 999);
5197 do_cmdline_cmd(IObuff);
5198 break;
5201 #endif
5204 * "Q" command.
5206 static void
5207 nv_exmode(cap)
5208 cmdarg_T *cap;
5211 * Ignore 'Q' in Visual mode, just give a beep.
5213 #ifdef FEAT_VISUAL
5214 if (VIsual_active)
5215 vim_beep();
5216 else
5217 #endif
5218 if (!checkclearop(cap->oap))
5219 do_exmode(FALSE);
5223 * Handle a ":" command.
5225 static void
5226 nv_colon(cap)
5227 cmdarg_T *cap;
5229 int old_p_im;
5231 #ifdef FEAT_VISUAL
5232 if (VIsual_active)
5233 nv_operator(cap);
5234 else
5235 #endif
5237 if (cap->oap->op_type != OP_NOP)
5239 /* Using ":" as a movement is characterwise exclusive. */
5240 cap->oap->motion_type = MCHAR;
5241 cap->oap->inclusive = FALSE;
5243 else if (cap->count0)
5245 /* translate "count:" into ":.,.+(count - 1)" */
5246 stuffcharReadbuff('.');
5247 if (cap->count0 > 1)
5249 stuffReadbuff((char_u *)",.+");
5250 stuffnumReadbuff((long)cap->count0 - 1L);
5254 /* When typing, don't type below an old message */
5255 if (KeyTyped)
5256 compute_cmdrow();
5258 old_p_im = p_im;
5260 /* get a command line and execute it */
5261 do_cmdline(NULL, getexline, NULL,
5262 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5264 /* If 'insertmode' changed, enter or exit Insert mode */
5265 if (p_im != old_p_im)
5267 if (p_im)
5268 restart_edit = 'i';
5269 else
5270 restart_edit = 0;
5273 /* The start of the operator may have become invalid by the Ex
5274 * command. */
5275 if (cap->oap->op_type != OP_NOP
5276 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5277 || cap->oap->start.col >
5278 STRLEN(ml_get(cap->oap->start.lnum))))
5279 clearopbeep(cap->oap);
5284 * Handle CTRL-G command.
5286 static void
5287 nv_ctrlg(cap)
5288 cmdarg_T *cap;
5290 #ifdef FEAT_VISUAL
5291 if (VIsual_active) /* toggle Selection/Visual mode */
5293 VIsual_select = !VIsual_select;
5294 showmode();
5296 else
5297 #endif
5298 if (!checkclearop(cap->oap))
5299 /* print full name if count given or :cd used */
5300 fileinfo((int)cap->count0, FALSE, TRUE);
5304 * Handle CTRL-H <Backspace> command.
5306 static void
5307 nv_ctrlh(cap)
5308 cmdarg_T *cap;
5310 #ifdef FEAT_VISUAL
5311 if (VIsual_active && VIsual_select)
5313 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5314 v_visop(cap);
5316 else
5317 #endif
5318 nv_left(cap);
5322 * CTRL-L: clear screen and redraw.
5324 static void
5325 nv_clear(cap)
5326 cmdarg_T *cap;
5328 if (!checkclearop(cap->oap))
5330 #if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5332 * Right now, the BeBox doesn't seem to have an easy way to detect
5333 * window resizing, so we cheat and make the user detect it
5334 * manually with CTRL-L instead
5336 ui_get_shellsize();
5337 #endif
5338 #ifdef FEAT_SYN_HL
5339 /* Clear all syntax states to force resyncing. */
5340 syn_stack_free_all(curbuf);
5341 #endif
5342 redraw_later(CLEAR);
5347 * CTRL-O: In Select mode: switch to Visual mode for one command.
5348 * Otherwise: Go to older pcmark.
5350 static void
5351 nv_ctrlo(cap)
5352 cmdarg_T *cap;
5354 #ifdef FEAT_VISUAL
5355 if (VIsual_active && VIsual_select)
5357 VIsual_select = FALSE;
5358 showmode();
5359 restart_VIsual_select = 2; /* restart Select mode later */
5361 else
5362 #endif
5364 cap->count1 = -cap->count1;
5365 nv_pcmark(cap);
5370 * CTRL-^ command, short for ":e #"
5372 static void
5373 nv_hat(cap)
5374 cmdarg_T *cap;
5376 if (!checkclearopq(cap->oap))
5377 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5378 GETF_SETMARK|GETF_ALT, FALSE);
5382 * "Z" commands.
5384 static void
5385 nv_Zet(cap)
5386 cmdarg_T *cap;
5388 if (!checkclearopq(cap->oap))
5390 switch (cap->nchar)
5392 /* "ZZ": equivalent to ":x". */
5393 case 'Z': do_cmdline_cmd((char_u *)"x");
5394 break;
5396 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5397 case 'Q': do_cmdline_cmd((char_u *)"q!");
5398 break;
5400 default: clearopbeep(cap->oap);
5405 #if defined(FEAT_WINDOWS) || defined(PROTO)
5407 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5409 void
5410 do_nv_ident(c1, c2)
5411 int c1;
5412 int c2;
5414 oparg_T oa;
5415 cmdarg_T ca;
5417 clear_oparg(&oa);
5418 vim_memset(&ca, 0, sizeof(ca));
5419 ca.oap = &oa;
5420 ca.cmdchar = c1;
5421 ca.nchar = c2;
5422 nv_ident(&ca);
5424 #endif
5427 * Handle the commands that use the word under the cursor.
5428 * [g] CTRL-] :ta to current identifier
5429 * [g] 'K' run program for current identifier
5430 * [g] '*' / to current identifier or string
5431 * [g] '#' ? to current identifier or string
5432 * g ']' :tselect for current identifier
5434 static void
5435 nv_ident(cap)
5436 cmdarg_T *cap;
5438 char_u *ptr = NULL;
5439 char_u *buf;
5440 char_u *p;
5441 char_u *kp; /* value of 'keywordprg' */
5442 int kp_help; /* 'keywordprg' is ":help" */
5443 int n = 0; /* init for GCC */
5444 int cmdchar;
5445 int g_cmd; /* "g" command */
5446 char_u *aux_ptr;
5447 int isman;
5448 int isman_s;
5450 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5452 cmdchar = cap->nchar;
5453 g_cmd = TRUE;
5455 else
5457 cmdchar = cap->cmdchar;
5458 g_cmd = FALSE;
5461 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5462 cmdchar = '#';
5465 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5467 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5469 #ifdef FEAT_VISUAL
5470 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5471 return;
5472 #endif
5473 if (checkclearopq(cap->oap))
5474 return;
5477 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5478 (cmdchar == '*' || cmdchar == '#')
5479 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5481 clearop(cap->oap);
5482 return;
5485 /* Allocate buffer to put the command in. Inserting backslashes can
5486 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5487 * and some numbers. */
5488 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5489 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5490 || STRCMP(kp, ":help") == 0);
5491 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5492 if (buf == NULL)
5493 return;
5494 buf[0] = NUL;
5496 switch (cmdchar)
5498 case '*':
5499 case '#':
5501 * Put cursor at start of word, makes search skip the word
5502 * under the cursor.
5503 * Call setpcmark() first, so "*``" puts the cursor back where
5504 * it was.
5506 setpcmark();
5507 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5509 if (!g_cmd && vim_iswordp(ptr))
5510 STRCPY(buf, "\\<");
5511 no_smartcase = TRUE; /* don't use 'smartcase' now */
5512 break;
5514 case 'K':
5515 if (kp_help)
5516 STRCPY(buf, "he! ");
5517 else
5519 /* An external command will probably use an argument starting
5520 * with "-" as an option. To avoid trouble we skip the "-". */
5521 while (*ptr == '-' && n > 0)
5523 ++ptr;
5524 --n;
5526 if (n == 0)
5528 EMSG(_(e_noident)); /* found dashes only */
5529 vim_free(buf);
5530 return;
5533 /* When a count is given, turn it into a range. Is this
5534 * really what we want? */
5535 isman = (STRCMP(kp, "man") == 0);
5536 isman_s = (STRCMP(kp, "man -s") == 0);
5537 if (cap->count0 != 0 && !(isman || isman_s))
5538 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5540 STRCAT(buf, "! ");
5541 if (cap->count0 == 0 && isman_s)
5542 STRCAT(buf, "man");
5543 else
5544 STRCAT(buf, kp);
5545 STRCAT(buf, " ");
5546 if (cap->count0 != 0 && (isman || isman_s))
5548 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5549 STRCAT(buf, " ");
5552 break;
5554 case ']':
5555 #ifdef FEAT_CSCOPE
5556 if (p_cst)
5557 STRCPY(buf, "cstag ");
5558 else
5559 #endif
5560 STRCPY(buf, "ts ");
5561 break;
5563 default:
5564 if (curbuf->b_help)
5565 STRCPY(buf, "he! ");
5566 else if (g_cmd)
5567 STRCPY(buf, "tj ");
5568 else
5569 sprintf((char *)buf, "%ldta ", cap->count0);
5573 * Now grab the chars in the identifier
5575 if (cmdchar == 'K' && !kp_help)
5577 /* Escape the argument properly for a shell command */
5578 ptr = vim_strnsave(ptr, n);
5579 p = vim_strsave_shellescape(ptr, TRUE);
5580 vim_free(ptr);
5581 if (p == NULL)
5583 vim_free(buf);
5584 return;
5586 buf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5587 if (buf == NULL)
5589 vim_free(buf);
5590 vim_free(p);
5591 return;
5593 STRCAT(buf, p);
5594 vim_free(p);
5596 else
5598 if (cmdchar == '*')
5599 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5600 else if (cmdchar == '#')
5601 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
5602 else
5603 /* Don't escape spaces and Tabs in a tag with a backslash */
5604 aux_ptr = (char_u *)"\\|\"\n*?[";
5606 p = buf + STRLEN(buf);
5607 while (n-- > 0)
5609 /* put a backslash before \ and some others */
5610 if (vim_strchr(aux_ptr, *ptr) != NULL)
5611 *p++ = '\\';
5612 #ifdef FEAT_MBYTE
5613 /* When current byte is a part of multibyte character, copy all
5614 * bytes of that character. */
5615 if (has_mbyte)
5617 int i;
5618 int len = (*mb_ptr2len)(ptr) - 1;
5620 for (i = 0; i < len && n >= 1; ++i, --n)
5621 *p++ = *ptr++;
5623 #endif
5624 *p++ = *ptr++;
5626 *p = NUL;
5630 * Execute the command.
5632 if (cmdchar == '*' || cmdchar == '#')
5634 if (!g_cmd && (
5635 #ifdef FEAT_MBYTE
5636 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5637 #endif
5638 vim_iswordc(ptr[-1])))
5639 STRCAT(buf, "\\>");
5640 #ifdef FEAT_CMDHIST
5641 /* put pattern in search history */
5642 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5643 #endif
5644 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5646 else
5647 do_cmdline_cmd(buf);
5649 vim_free(buf);
5652 #if defined(FEAT_VISUAL) || defined(PROTO)
5654 * Get visually selected text, within one line only.
5655 * Returns FAIL if more than one line selected.
5658 get_visual_text(cap, pp, lenp)
5659 cmdarg_T *cap;
5660 char_u **pp; /* return: start of selected text */
5661 int *lenp; /* return: length of selected text */
5663 if (VIsual_mode != 'V')
5664 unadjust_for_sel();
5665 if (VIsual.lnum != curwin->w_cursor.lnum)
5667 if (cap != NULL)
5668 clearopbeep(cap->oap);
5669 return FAIL;
5671 if (VIsual_mode == 'V')
5673 *pp = ml_get_curline();
5674 *lenp = (int)STRLEN(*pp);
5676 else
5678 if (lt(curwin->w_cursor, VIsual))
5680 *pp = ml_get_pos(&curwin->w_cursor);
5681 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5683 else
5685 *pp = ml_get_pos(&VIsual);
5686 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5688 #ifdef FEAT_MBYTE
5689 if (has_mbyte)
5690 /* Correct the length to include the whole last character. */
5691 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
5692 #endif
5694 reset_VIsual_and_resel();
5695 return OK;
5697 #endif
5700 * CTRL-T: backwards in tag stack
5702 static void
5703 nv_tagpop(cap)
5704 cmdarg_T *cap;
5706 if (!checkclearopq(cap->oap))
5707 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5711 * Handle scrolling command 'H', 'L' and 'M'.
5713 static void
5714 nv_scroll(cap)
5715 cmdarg_T *cap;
5717 int used = 0;
5718 long n;
5719 #ifdef FEAT_FOLDING
5720 linenr_T lnum;
5721 #endif
5722 int half;
5724 cap->oap->motion_type = MLINE;
5725 setpcmark();
5727 if (cap->cmdchar == 'L')
5729 validate_botline(); /* make sure curwin->w_botline is valid */
5730 curwin->w_cursor.lnum = curwin->w_botline - 1;
5731 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5732 curwin->w_cursor.lnum = 1;
5733 else
5735 #ifdef FEAT_FOLDING
5736 if (hasAnyFolding(curwin))
5738 /* Count a fold for one screen line. */
5739 for (n = cap->count1 - 1; n > 0
5740 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5742 (void)hasFolding(curwin->w_cursor.lnum,
5743 &curwin->w_cursor.lnum, NULL);
5744 --curwin->w_cursor.lnum;
5747 else
5748 #endif
5749 curwin->w_cursor.lnum -= cap->count1 - 1;
5752 else
5754 if (cap->cmdchar == 'M')
5756 #ifdef FEAT_DIFF
5757 /* Don't count filler lines above the window. */
5758 used -= diff_check_fill(curwin, curwin->w_topline)
5759 - curwin->w_topfill;
5760 #endif
5761 validate_botline(); /* make sure w_empty_rows is valid */
5762 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5763 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5765 #ifdef FEAT_DIFF
5766 /* Count half he number of filler lines to be "below this
5767 * line" and half to be "above the next line". */
5768 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5769 + n) / 2 >= half)
5771 --n;
5772 break;
5774 #endif
5775 used += plines(curwin->w_topline + n);
5776 if (used >= half)
5777 break;
5778 #ifdef FEAT_FOLDING
5779 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5780 n = lnum - curwin->w_topline;
5781 #endif
5783 if (n > 0 && used > curwin->w_height)
5784 --n;
5786 else /* (cap->cmdchar == 'H') */
5788 n = cap->count1 - 1;
5789 #ifdef FEAT_FOLDING
5790 if (hasAnyFolding(curwin))
5792 /* Count a fold for one screen line. */
5793 lnum = curwin->w_topline;
5794 while (n-- > 0 && lnum < curwin->w_botline - 1)
5796 hasFolding(lnum, NULL, &lnum);
5797 ++lnum;
5799 n = lnum - curwin->w_topline;
5801 #endif
5803 curwin->w_cursor.lnum = curwin->w_topline + n;
5804 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5805 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5808 cursor_correct(); /* correct for 'so' */
5809 beginline(BL_SOL | BL_FIX);
5813 * Cursor right commands.
5815 static void
5816 nv_right(cap)
5817 cmdarg_T *cap;
5819 long n;
5820 #ifdef FEAT_VISUAL
5821 int PAST_LINE;
5822 #else
5823 # define PAST_LINE 0
5824 #endif
5826 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5828 /* <C-Right> and <S-Right> move a word or WORD right */
5829 if (mod_mask & MOD_MASK_CTRL)
5830 cap->arg = TRUE;
5831 nv_wordcmd(cap);
5832 return;
5835 cap->oap->motion_type = MCHAR;
5836 cap->oap->inclusive = FALSE;
5837 #ifdef FEAT_VISUAL
5838 PAST_LINE = (VIsual_active && *p_sel != 'o');
5840 # ifdef FEAT_VIRTUALEDIT
5842 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
5843 * (theoretically) infinitely long.
5845 if (virtual_active())
5846 PAST_LINE = 0;
5847 # endif
5848 #endif
5850 for (n = cap->count1; n > 0; --n)
5852 if ((!PAST_LINE && oneright() == FAIL)
5853 || (PAST_LINE && *ml_get_cursor() == NUL))
5856 * <Space> wraps to next line if 'whichwrap' has 's'.
5857 * 'l' wraps to next line if 'whichwrap' has 'l'.
5858 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
5860 if ( ((cap->cmdchar == ' '
5861 && vim_strchr(p_ww, 's') != NULL)
5862 || (cap->cmdchar == 'l'
5863 && vim_strchr(p_ww, 'l') != NULL)
5864 || (cap->cmdchar == K_RIGHT
5865 && vim_strchr(p_ww, '>') != NULL))
5866 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5868 /* When deleting we also count the NL as a character.
5869 * Set cap->oap->inclusive when last char in the line is
5870 * included, move to next line after that */
5871 if ( cap->oap->op_type != OP_NOP
5872 && !cap->oap->inclusive
5873 && !lineempty(curwin->w_cursor.lnum))
5874 cap->oap->inclusive = TRUE;
5875 else
5877 ++curwin->w_cursor.lnum;
5878 curwin->w_cursor.col = 0;
5879 #ifdef FEAT_VIRTUALEDIT
5880 curwin->w_cursor.coladd = 0;
5881 #endif
5882 curwin->w_set_curswant = TRUE;
5883 cap->oap->inclusive = FALSE;
5885 continue;
5887 if (cap->oap->op_type == OP_NOP)
5889 /* Only beep and flush if not moved at all */
5890 if (n == cap->count1)
5891 beep_flush();
5893 else
5895 if (!lineempty(curwin->w_cursor.lnum))
5896 cap->oap->inclusive = TRUE;
5898 break;
5900 #ifdef FEAT_VISUAL
5901 else if (PAST_LINE)
5903 curwin->w_set_curswant = TRUE;
5904 # ifdef FEAT_VIRTUALEDIT
5905 if (virtual_active())
5906 oneright();
5907 else
5908 # endif
5910 # ifdef FEAT_MBYTE
5911 if (has_mbyte)
5912 curwin->w_cursor.col +=
5913 (*mb_ptr2len)(ml_get_cursor());
5914 else
5915 # endif
5916 ++curwin->w_cursor.col;
5919 #endif
5921 #ifdef FEAT_FOLDING
5922 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5923 && cap->oap->op_type == OP_NOP)
5924 foldOpenCursor();
5925 #endif
5929 * Cursor left commands.
5931 * Returns TRUE when operator end should not be adjusted.
5933 static void
5934 nv_left(cap)
5935 cmdarg_T *cap;
5937 long n;
5939 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5941 /* <C-Left> and <S-Left> move a word or WORD left */
5942 if (mod_mask & MOD_MASK_CTRL)
5943 cap->arg = 1;
5944 nv_bck_word(cap);
5945 return;
5948 cap->oap->motion_type = MCHAR;
5949 cap->oap->inclusive = FALSE;
5950 for (n = cap->count1; n > 0; --n)
5952 if (oneleft() == FAIL)
5954 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
5955 * 'h' wraps to previous line if 'whichwrap' has 'h'.
5956 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
5958 if ( (((cap->cmdchar == K_BS
5959 || cap->cmdchar == Ctrl_H)
5960 && vim_strchr(p_ww, 'b') != NULL)
5961 || (cap->cmdchar == 'h'
5962 && vim_strchr(p_ww, 'h') != NULL)
5963 || (cap->cmdchar == K_LEFT
5964 && vim_strchr(p_ww, '<') != NULL))
5965 && curwin->w_cursor.lnum > 1)
5967 --(curwin->w_cursor.lnum);
5968 coladvance((colnr_T)MAXCOL);
5969 curwin->w_set_curswant = TRUE;
5971 /* When the NL before the first char has to be deleted we
5972 * put the cursor on the NUL after the previous line.
5973 * This is a very special case, be careful!
5974 * Don't adjust op_end now, otherwise it won't work. */
5975 if ( (cap->oap->op_type == OP_DELETE
5976 || cap->oap->op_type == OP_CHANGE)
5977 && !lineempty(curwin->w_cursor.lnum))
5979 if (*ml_get_cursor() != NUL)
5980 ++curwin->w_cursor.col;
5981 cap->retval |= CA_NO_ADJ_OP_END;
5983 continue;
5985 /* Only beep and flush if not moved at all */
5986 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
5987 beep_flush();
5988 break;
5991 #ifdef FEAT_FOLDING
5992 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5993 && cap->oap->op_type == OP_NOP)
5994 foldOpenCursor();
5995 #endif
5999 * Cursor up commands.
6000 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6002 static void
6003 nv_up(cap)
6004 cmdarg_T *cap;
6006 if (mod_mask & MOD_MASK_SHIFT)
6008 /* <S-Up> is page up */
6009 cap->arg = BACKWARD;
6010 nv_page(cap);
6012 else
6014 cap->oap->motion_type = MLINE;
6015 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6016 clearopbeep(cap->oap);
6017 else if (cap->arg)
6018 beginline(BL_WHITE | BL_FIX);
6023 * Cursor down commands.
6024 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6026 static void
6027 nv_down(cap)
6028 cmdarg_T *cap;
6030 if (mod_mask & MOD_MASK_SHIFT)
6032 /* <S-Down> is page down */
6033 cap->arg = FORWARD;
6034 nv_page(cap);
6036 else
6037 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6038 /* In a quickfix window a <CR> jumps to the error under the cursor. */
6039 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
6040 if (curwin->w_llist_ref == NULL)
6041 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6042 else
6043 do_cmdline_cmd((char_u *)".ll"); /* location list window */
6044 else
6045 #endif
6047 #ifdef FEAT_CMDWIN
6048 /* In the cmdline window a <CR> executes the command. */
6049 if (cmdwin_type != 0 && cap->cmdchar == CAR)
6050 cmdwin_result = CAR;
6051 else
6052 #endif
6054 cap->oap->motion_type = MLINE;
6055 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6056 clearopbeep(cap->oap);
6057 else if (cap->arg)
6058 beginline(BL_WHITE | BL_FIX);
6063 #ifdef FEAT_SEARCHPATH
6065 * Grab the file name under the cursor and edit it.
6067 static void
6068 nv_gotofile(cap)
6069 cmdarg_T *cap;
6071 char_u *ptr;
6072 linenr_T lnum = -1;
6074 if (text_locked())
6076 clearopbeep(cap->oap);
6077 text_locked_msg();
6078 return;
6080 #ifdef FEAT_AUTOCMD
6081 if (curbuf_locked())
6083 clearop(cap->oap);
6084 return;
6086 #endif
6088 ptr = grab_file_name(cap->count1, &lnum);
6090 if (ptr != NULL)
6092 /* do autowrite if necessary */
6093 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6094 autowrite(curbuf, FALSE);
6095 setpcmark();
6096 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
6097 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
6098 if (cap->nchar == 'F' && lnum >= 0)
6100 curwin->w_cursor.lnum = lnum;
6101 check_cursor_lnum();
6102 beginline(BL_SOL | BL_FIX);
6104 vim_free(ptr);
6106 else
6107 clearop(cap->oap);
6109 #endif
6112 * <End> command: to end of current line or last line.
6114 static void
6115 nv_end(cap)
6116 cmdarg_T *cap;
6118 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
6120 cap->arg = TRUE;
6121 nv_goto(cap);
6122 cap->count1 = 1; /* to end of current line */
6124 nv_dollar(cap);
6128 * Handle the "$" command.
6130 static void
6131 nv_dollar(cap)
6132 cmdarg_T *cap;
6134 cap->oap->motion_type = MCHAR;
6135 cap->oap->inclusive = TRUE;
6136 #ifdef FEAT_VIRTUALEDIT
6137 /* In virtual mode when off the edge of a line and an operator
6138 * is pending (whew!) keep the cursor where it is.
6139 * Otherwise, send it to the end of the line. */
6140 if (!virtual_active() || gchar_cursor() != NUL
6141 || cap->oap->op_type == OP_NOP)
6142 #endif
6143 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6144 if (cursor_down((long)(cap->count1 - 1),
6145 cap->oap->op_type == OP_NOP) == FAIL)
6146 clearopbeep(cap->oap);
6147 #ifdef FEAT_FOLDING
6148 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6149 foldOpenCursor();
6150 #endif
6154 * Implementation of '?' and '/' commands.
6155 * If cap->arg is TRUE don't set PC mark.
6157 static void
6158 nv_search(cap)
6159 cmdarg_T *cap;
6161 oparg_T *oap = cap->oap;
6163 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6165 /* Translate "g??" to "g?g?" */
6166 cap->cmdchar = 'g';
6167 cap->nchar = '?';
6168 nv_operator(cap);
6169 return;
6172 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6174 if (cap->searchbuf == NULL)
6176 clearop(oap);
6177 return;
6180 normal_search(cap, cap->cmdchar, cap->searchbuf,
6181 (cap->arg ? 0 : SEARCH_MARK));
6185 * Handle "N" and "n" commands.
6186 * cap->arg is SEARCH_REV for "N", 0 for "n".
6188 static void
6189 nv_next(cap)
6190 cmdarg_T *cap;
6192 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6196 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6197 * Uses only cap->count1 and cap->oap from "cap".
6199 static void
6200 normal_search(cap, dir, pat, opt)
6201 cmdarg_T *cap;
6202 int dir;
6203 char_u *pat;
6204 int opt; /* extra flags for do_search() */
6206 int i;
6208 cap->oap->motion_type = MCHAR;
6209 cap->oap->inclusive = FALSE;
6210 cap->oap->use_reg_one = TRUE;
6211 curwin->w_set_curswant = TRUE;
6213 i = do_search(cap->oap, dir, pat, cap->count1,
6214 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
6215 if (i == 0)
6216 clearop(cap->oap);
6217 else
6219 if (i == 2)
6220 cap->oap->motion_type = MLINE;
6221 #ifdef FEAT_VIRTUALEDIT
6222 curwin->w_cursor.coladd = 0;
6223 #endif
6224 #ifdef FEAT_FOLDING
6225 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6226 foldOpenCursor();
6227 #endif
6230 /* "/$" will put the cursor after the end of the line, may need to
6231 * correct that here */
6232 check_cursor();
6236 * Character search commands.
6237 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6238 * ',' and FALSE for ';'.
6239 * cap->nchar is NUL for ',' and ';' (repeat the search)
6241 static void
6242 nv_csearch(cap)
6243 cmdarg_T *cap;
6245 int t_cmd;
6247 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6248 t_cmd = TRUE;
6249 else
6250 t_cmd = FALSE;
6252 cap->oap->motion_type = MCHAR;
6253 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6254 clearopbeep(cap->oap);
6255 else
6257 curwin->w_set_curswant = TRUE;
6258 #ifdef FEAT_VIRTUALEDIT
6259 /* Include a Tab for "tx" and for "dfx". */
6260 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6261 && (t_cmd || cap->oap->op_type != OP_NOP))
6263 colnr_T scol, ecol;
6265 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6266 curwin->w_cursor.coladd = ecol - scol;
6268 else
6269 curwin->w_cursor.coladd = 0;
6270 #endif
6271 #ifdef FEAT_VISUAL
6272 adjust_for_sel(cap);
6273 #endif
6274 #ifdef FEAT_FOLDING
6275 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6276 foldOpenCursor();
6277 #endif
6282 * "[" and "]" commands.
6283 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6285 static void
6286 nv_brackets(cap)
6287 cmdarg_T *cap;
6289 pos_T new_pos;
6290 pos_T prev_pos;
6291 pos_T *pos = NULL; /* init for GCC */
6292 pos_T old_pos; /* cursor position before command */
6293 int flag;
6294 long n;
6295 int findc;
6296 int c;
6298 cap->oap->motion_type = MCHAR;
6299 cap->oap->inclusive = FALSE;
6300 old_pos = curwin->w_cursor;
6301 #ifdef FEAT_VIRTUALEDIT
6302 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6303 #endif
6305 #ifdef FEAT_SEARCHPATH
6307 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6309 if (cap->nchar == 'f')
6310 nv_gotofile(cap);
6311 else
6312 #endif
6314 #ifdef FEAT_FIND_ID
6316 * Find the occurrence(s) of the identifier or define under cursor
6317 * in current and included files or jump to the first occurrence.
6319 * search list jump
6320 * fwd bwd fwd bwd fwd bwd
6321 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6322 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6324 if (vim_strchr((char_u *)
6325 #ifdef EBCDIC
6326 "iI\005dD\067",
6327 #else
6328 "iI\011dD\004",
6329 #endif
6330 cap->nchar) != NULL)
6332 char_u *ptr;
6333 int len;
6335 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6336 clearop(cap->oap);
6337 else
6339 find_pattern_in_path(ptr, 0, len, TRUE,
6340 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6341 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6342 cap->count1,
6343 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6344 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6345 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6346 (linenr_T)MAXLNUM);
6347 curwin->w_set_curswant = TRUE;
6350 else
6351 #endif
6354 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6355 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6356 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6357 * "[m" or "]m" search for prev/next start of (Java) method.
6358 * "[M" or "]M" search for prev/next end of (Java) method.
6360 if ( (cap->cmdchar == '['
6361 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6362 || (cap->cmdchar == ']'
6363 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6365 if (cap->nchar == '*')
6366 cap->nchar = '/';
6367 new_pos.lnum = 0;
6368 prev_pos.lnum = 0;
6369 if (cap->nchar == 'm' || cap->nchar == 'M')
6371 if (cap->cmdchar == '[')
6372 findc = '{';
6373 else
6374 findc = '}';
6375 n = 9999;
6377 else
6379 findc = cap->nchar;
6380 n = cap->count1;
6382 for ( ; n > 0; --n)
6384 if ((pos = findmatchlimit(cap->oap, findc,
6385 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6387 if (new_pos.lnum == 0) /* nothing found */
6389 if (cap->nchar != 'm' && cap->nchar != 'M')
6390 clearopbeep(cap->oap);
6392 else
6393 pos = &new_pos; /* use last one found */
6394 break;
6396 prev_pos = new_pos;
6397 curwin->w_cursor = *pos;
6398 new_pos = *pos;
6400 curwin->w_cursor = old_pos;
6403 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6404 * brought us to the match for "[m" and "]M" when inside a method.
6405 * Try finding the '{' or '}' we want to be at.
6406 * Also repeat for the given count.
6408 if (cap->nchar == 'm' || cap->nchar == 'M')
6410 /* norm is TRUE for "]M" and "[m" */
6411 int norm = ((findc == '{') == (cap->nchar == 'm'));
6413 n = cap->count1;
6414 /* found a match: we were inside a method */
6415 if (prev_pos.lnum != 0)
6417 pos = &prev_pos;
6418 curwin->w_cursor = prev_pos;
6419 if (norm)
6420 --n;
6422 else
6423 pos = NULL;
6424 while (n > 0)
6426 for (;;)
6428 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6430 /* if not found anything, that's an error */
6431 if (pos == NULL)
6432 clearopbeep(cap->oap);
6433 n = 0;
6434 break;
6436 c = gchar_cursor();
6437 if (c == '{' || c == '}')
6439 /* Must have found end/start of class: use it.
6440 * Or found the place to be at. */
6441 if ((c == findc && norm) || (n == 1 && !norm))
6443 new_pos = curwin->w_cursor;
6444 pos = &new_pos;
6445 n = 0;
6447 /* if no match found at all, we started outside of the
6448 * class and we're inside now. Just go on. */
6449 else if (new_pos.lnum == 0)
6451 new_pos = curwin->w_cursor;
6452 pos = &new_pos;
6454 /* found start/end of other method: go to match */
6455 else if ((pos = findmatchlimit(cap->oap, findc,
6456 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6457 0)) == NULL)
6458 n = 0;
6459 else
6460 curwin->w_cursor = *pos;
6461 break;
6464 --n;
6466 curwin->w_cursor = old_pos;
6467 if (pos == NULL && new_pos.lnum != 0)
6468 clearopbeep(cap->oap);
6470 if (pos != NULL)
6472 setpcmark();
6473 curwin->w_cursor = *pos;
6474 curwin->w_set_curswant = TRUE;
6475 #ifdef FEAT_FOLDING
6476 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6477 && cap->oap->op_type == OP_NOP)
6478 foldOpenCursor();
6479 #endif
6484 * "[[", "[]", "]]" and "][": move to start or end of function
6486 else if (cap->nchar == '[' || cap->nchar == ']')
6488 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6489 flag = '{';
6490 else
6491 flag = '}'; /* "][" or "[]" */
6493 curwin->w_set_curswant = TRUE;
6495 * Imitate strange Vi behaviour: When using "]]" with an operator
6496 * we also stop at '}'.
6498 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
6499 (cap->oap->op_type != OP_NOP
6500 && cap->arg == FORWARD && flag == '{')))
6501 clearopbeep(cap->oap);
6502 else
6504 if (cap->oap->op_type == OP_NOP)
6505 beginline(BL_WHITE | BL_FIX);
6506 #ifdef FEAT_FOLDING
6507 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6508 foldOpenCursor();
6509 #endif
6514 * "[p", "[P", "]P" and "]p": put with indent adjustment
6516 else if (cap->nchar == 'p' || cap->nchar == 'P')
6518 if (!checkclearop(cap->oap))
6520 prep_redo_cmd(cap);
6521 do_put(cap->oap->regname,
6522 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6523 cap->count1, PUT_FIXINDENT);
6528 * "['", "[`", "]'" and "]`": jump to next mark
6530 else if (cap->nchar == '\'' || cap->nchar == '`')
6532 pos = &curwin->w_cursor;
6533 for (n = cap->count1; n > 0; --n)
6535 prev_pos = *pos;
6536 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6537 cap->nchar == '\'');
6538 if (pos == NULL)
6539 break;
6541 if (pos == NULL)
6542 pos = &prev_pos;
6543 nv_cursormark(cap, cap->nchar == '\'', pos);
6546 #ifdef FEAT_MOUSE
6548 * [ or ] followed by a middle mouse click: put selected text with
6549 * indent adjustment. Any other button just does as usual.
6551 else if (cap->nchar >= K_LEFTMOUSE && cap->nchar <= K_RIGHTRELEASE)
6553 (void)do_mouse(cap->oap, cap->nchar,
6554 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6555 cap->count1, PUT_FIXINDENT);
6557 #endif /* FEAT_MOUSE */
6559 #ifdef FEAT_FOLDING
6561 * "[z" and "]z": move to start or end of open fold.
6563 else if (cap->nchar == 'z')
6565 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6566 cap->count1) == FAIL)
6567 clearopbeep(cap->oap);
6569 #endif
6571 #ifdef FEAT_DIFF
6573 * "[c" and "]c": move to next or previous diff-change.
6575 else if (cap->nchar == 'c')
6577 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6578 cap->count1) == FAIL)
6579 clearopbeep(cap->oap);
6581 #endif
6583 #ifdef FEAT_SPELL
6585 * "[s", "[S", "]s" and "]S": move to next spell error.
6587 else if (cap->nchar == 's' || cap->nchar == 'S')
6589 setpcmark();
6590 for (n = 0; n < cap->count1; ++n)
6591 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6592 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
6594 clearopbeep(cap->oap);
6595 break;
6597 # ifdef FEAT_FOLDING
6598 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6599 foldOpenCursor();
6600 # endif
6602 #endif
6604 /* Not a valid cap->nchar. */
6605 else
6606 clearopbeep(cap->oap);
6610 * Handle Normal mode "%" command.
6612 static void
6613 nv_percent(cap)
6614 cmdarg_T *cap;
6616 pos_T *pos;
6617 #ifdef FEAT_FOLDING
6618 linenr_T lnum = curwin->w_cursor.lnum;
6619 #endif
6621 cap->oap->inclusive = TRUE;
6622 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6624 if (cap->count0 > 100)
6625 clearopbeep(cap->oap);
6626 else
6628 cap->oap->motion_type = MLINE;
6629 setpcmark();
6630 /* Round up, so CTRL-G will give same value. Watch out for a
6631 * large line count, the line number must not go negative! */
6632 if (curbuf->b_ml.ml_line_count > 1000000)
6633 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6634 / 100L * cap->count0;
6635 else
6636 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6637 cap->count0 + 99L) / 100L;
6638 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6639 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6640 beginline(BL_SOL | BL_FIX);
6643 else /* "%" : go to matching paren */
6645 cap->oap->motion_type = MCHAR;
6646 cap->oap->use_reg_one = TRUE;
6647 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6648 clearopbeep(cap->oap);
6649 else
6651 setpcmark();
6652 curwin->w_cursor = *pos;
6653 curwin->w_set_curswant = TRUE;
6654 #ifdef FEAT_VIRTUALEDIT
6655 curwin->w_cursor.coladd = 0;
6656 #endif
6657 #ifdef FEAT_VISUAL
6658 adjust_for_sel(cap);
6659 #endif
6662 #ifdef FEAT_FOLDING
6663 if (cap->oap->op_type == OP_NOP
6664 && lnum != curwin->w_cursor.lnum
6665 && (fdo_flags & FDO_PERCENT)
6666 && KeyTyped)
6667 foldOpenCursor();
6668 #endif
6672 * Handle "(" and ")" commands.
6673 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6675 static void
6676 nv_brace(cap)
6677 cmdarg_T *cap;
6679 cap->oap->motion_type = MCHAR;
6680 cap->oap->use_reg_one = TRUE;
6681 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6682 cap->oap->inclusive = FALSE;
6683 curwin->w_set_curswant = TRUE;
6685 if (findsent(cap->arg, cap->count1) == FAIL)
6686 clearopbeep(cap->oap);
6687 else
6689 /* Don't leave the cursor on the NUL past end of line. */
6690 adjust_cursor(cap->oap);
6691 #ifdef FEAT_VIRTUALEDIT
6692 curwin->w_cursor.coladd = 0;
6693 #endif
6694 #ifdef FEAT_FOLDING
6695 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6696 foldOpenCursor();
6697 #endif
6702 * "m" command: Mark a position.
6704 static void
6705 nv_mark(cap)
6706 cmdarg_T *cap;
6708 if (!checkclearop(cap->oap))
6710 if (setmark(cap->nchar) == FAIL)
6711 clearopbeep(cap->oap);
6716 * "{" and "}" commands.
6717 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6719 static void
6720 nv_findpar(cap)
6721 cmdarg_T *cap;
6723 cap->oap->motion_type = MCHAR;
6724 cap->oap->inclusive = FALSE;
6725 cap->oap->use_reg_one = TRUE;
6726 curwin->w_set_curswant = TRUE;
6727 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
6728 clearopbeep(cap->oap);
6729 else
6731 #ifdef FEAT_VIRTUALEDIT
6732 curwin->w_cursor.coladd = 0;
6733 #endif
6734 #ifdef FEAT_FOLDING
6735 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6736 foldOpenCursor();
6737 #endif
6742 * "u" command: Undo or make lower case.
6744 static void
6745 nv_undo(cap)
6746 cmdarg_T *cap;
6748 if (cap->oap->op_type == OP_LOWER
6749 #ifdef FEAT_VISUAL
6750 || VIsual_active
6751 #endif
6754 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6755 cap->cmdchar = 'g';
6756 cap->nchar = 'u';
6757 nv_operator(cap);
6759 else
6760 nv_kundo(cap);
6764 * <Undo> command.
6766 static void
6767 nv_kundo(cap)
6768 cmdarg_T *cap;
6770 if (!checkclearopq(cap->oap))
6772 u_undo((int)cap->count1);
6773 curwin->w_set_curswant = TRUE;
6778 * Handle the "r" command.
6780 static void
6781 nv_replace(cap)
6782 cmdarg_T *cap;
6784 char_u *ptr;
6785 int had_ctrl_v;
6786 long n;
6788 if (checkclearop(cap->oap))
6789 return;
6791 /* get another character */
6792 if (cap->nchar == Ctrl_V)
6794 had_ctrl_v = Ctrl_V;
6795 cap->nchar = get_literal();
6796 /* Don't redo a multibyte character with CTRL-V. */
6797 if (cap->nchar > DEL)
6798 had_ctrl_v = NUL;
6800 else
6801 had_ctrl_v = NUL;
6803 /* Abort if the character is a special key. */
6804 if (IS_SPECIAL(cap->nchar))
6806 clearopbeep(cap->oap);
6807 return;
6810 #ifdef FEAT_VISUAL
6811 /* Visual mode "r" */
6812 if (VIsual_active)
6814 if (got_int)
6815 reset_VIsual();
6816 nv_operator(cap);
6817 return;
6819 #endif
6821 #ifdef FEAT_VIRTUALEDIT
6822 /* Break tabs, etc. */
6823 if (virtual_active())
6825 if (u_save_cursor() == FAIL)
6826 return;
6827 if (gchar_cursor() == NUL)
6829 /* Add extra space and put the cursor on the first one. */
6830 coladvance_force((colnr_T)(getviscol() + cap->count1));
6831 curwin->w_cursor.col -= cap->count1;
6833 else if (gchar_cursor() == TAB)
6834 coladvance_force(getviscol());
6836 #endif
6838 /* Abort if not enough characters to replace. */
6839 ptr = ml_get_cursor();
6840 if (STRLEN(ptr) < (unsigned)cap->count1
6841 #ifdef FEAT_MBYTE
6842 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6843 #endif
6846 clearopbeep(cap->oap);
6847 return;
6851 * Replacing with a TAB is done by edit() when it is complicated because
6852 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6853 * Other characters are done below to avoid problems with things like
6854 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6856 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6858 stuffnumReadbuff(cap->count1);
6859 stuffcharReadbuff('R');
6860 stuffcharReadbuff('\t');
6861 stuffcharReadbuff(ESC);
6862 return;
6865 /* save line for undo */
6866 if (u_save_cursor() == FAIL)
6867 return;
6869 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6872 * Replace character(s) by a single newline.
6873 * Strange vi behaviour: Only one newline is inserted.
6874 * Delete the characters here.
6875 * Insert the newline with an insert command, takes care of
6876 * autoindent. The insert command depends on being on the last
6877 * character of a line or not.
6879 #ifdef FEAT_MBYTE
6880 (void)del_chars(cap->count1, FALSE); /* delete the characters */
6881 #else
6882 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
6883 #endif
6884 stuffcharReadbuff('\r');
6885 stuffcharReadbuff(ESC);
6887 /* Give 'r' to edit(), to get the redo command right. */
6888 invoke_edit(cap, TRUE, 'r', FALSE);
6890 else
6892 prep_redo(cap->oap->regname, cap->count1,
6893 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
6895 curbuf->b_op_start = curwin->w_cursor;
6896 #ifdef FEAT_MBYTE
6897 if (has_mbyte)
6899 int old_State = State;
6901 if (cap->ncharC1 != 0)
6902 AppendCharToRedobuff(cap->ncharC1);
6903 if (cap->ncharC2 != 0)
6904 AppendCharToRedobuff(cap->ncharC2);
6906 /* This is slow, but it handles replacing a single-byte with a
6907 * multi-byte and the other way around. Also handles adding
6908 * composing characters for utf-8. */
6909 for (n = cap->count1; n > 0; --n)
6911 State = REPLACE;
6912 ins_char(cap->nchar);
6913 State = old_State;
6914 if (cap->ncharC1 != 0)
6915 ins_char(cap->ncharC1);
6916 if (cap->ncharC2 != 0)
6917 ins_char(cap->ncharC2);
6920 else
6921 #endif
6924 * Replace the characters within one line.
6926 for (n = cap->count1; n > 0; --n)
6929 * Get ptr again, because u_save and/or showmatch() will have
6930 * released the line. At the same time we let know that the
6931 * line will be changed.
6933 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
6934 ptr[curwin->w_cursor.col] = cap->nchar;
6935 if (p_sm && msg_silent == 0)
6936 showmatch(cap->nchar);
6937 ++curwin->w_cursor.col;
6939 #ifdef FEAT_NETBEANS_INTG
6940 if (usingNetbeans)
6942 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
6944 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
6945 (long)cap->count1);
6946 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
6947 &ptr[start], (int)cap->count1);
6949 #endif
6951 /* mark the buffer as changed and prepare for displaying */
6952 changed_bytes(curwin->w_cursor.lnum,
6953 (colnr_T)(curwin->w_cursor.col - cap->count1));
6955 --curwin->w_cursor.col; /* cursor on the last replaced char */
6956 #ifdef FEAT_MBYTE
6957 /* if the character on the left of the current cursor is a multi-byte
6958 * character, move two characters left */
6959 if (has_mbyte)
6960 mb_adjust_cursor();
6961 #endif
6962 curbuf->b_op_end = curwin->w_cursor;
6963 curwin->w_set_curswant = TRUE;
6964 set_last_insert(cap->nchar);
6968 #ifdef FEAT_VISUAL
6970 * 'o': Exchange start and end of Visual area.
6971 * 'O': same, but in block mode exchange left and right corners.
6973 static void
6974 v_swap_corners(cmdchar)
6975 int cmdchar;
6977 pos_T old_cursor;
6978 colnr_T left, right;
6980 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
6982 old_cursor = curwin->w_cursor;
6983 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
6984 curwin->w_cursor.lnum = VIsual.lnum;
6985 coladvance(left);
6986 VIsual = curwin->w_cursor;
6988 curwin->w_cursor.lnum = old_cursor.lnum;
6989 curwin->w_curswant = right;
6990 /* 'selection "exclusive" and cursor at right-bottom corner: move it
6991 * right one column */
6992 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
6993 ++curwin->w_curswant;
6994 coladvance(curwin->w_curswant);
6995 if (curwin->w_cursor.col == old_cursor.col
6996 #ifdef FEAT_VIRTUALEDIT
6997 && (!virtual_active()
6998 || curwin->w_cursor.coladd == old_cursor.coladd)
6999 #endif
7002 curwin->w_cursor.lnum = VIsual.lnum;
7003 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7004 ++right;
7005 coladvance(right);
7006 VIsual = curwin->w_cursor;
7008 curwin->w_cursor.lnum = old_cursor.lnum;
7009 coladvance(left);
7010 curwin->w_curswant = left;
7013 else
7015 old_cursor = curwin->w_cursor;
7016 curwin->w_cursor = VIsual;
7017 VIsual = old_cursor;
7018 curwin->w_set_curswant = TRUE;
7021 #endif /* FEAT_VISUAL */
7024 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7026 static void
7027 nv_Replace(cap)
7028 cmdarg_T *cap;
7030 #ifdef FEAT_VISUAL
7031 if (VIsual_active) /* "R" is replace lines */
7033 cap->cmdchar = 'c';
7034 cap->nchar = NUL;
7035 VIsual_mode = 'V';
7036 nv_operator(cap);
7038 else
7039 #endif
7040 if (!checkclearopq(cap->oap))
7042 if (!curbuf->b_p_ma)
7043 EMSG(_(e_modifiable));
7044 else
7046 #ifdef FEAT_VIRTUALEDIT
7047 if (virtual_active())
7048 coladvance(getviscol());
7049 #endif
7050 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7055 #ifdef FEAT_VREPLACE
7057 * "gr".
7059 static void
7060 nv_vreplace(cap)
7061 cmdarg_T *cap;
7063 # ifdef FEAT_VISUAL
7064 if (VIsual_active)
7066 cap->cmdchar = 'r';
7067 cap->nchar = cap->extra_char;
7068 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7070 else
7071 # endif
7072 if (!checkclearopq(cap->oap))
7074 if (!curbuf->b_p_ma)
7075 EMSG(_(e_modifiable));
7076 else
7078 if (cap->extra_char == Ctrl_V) /* get another character */
7079 cap->extra_char = get_literal();
7080 stuffcharReadbuff(cap->extra_char);
7081 stuffcharReadbuff(ESC);
7082 # ifdef FEAT_VIRTUALEDIT
7083 if (virtual_active())
7084 coladvance(getviscol());
7085 # endif
7086 invoke_edit(cap, TRUE, 'v', FALSE);
7090 #endif
7093 * Swap case for "~" command, when it does not work like an operator.
7095 static void
7096 n_swapchar(cap)
7097 cmdarg_T *cap;
7099 long n;
7100 pos_T startpos;
7101 int did_change = 0;
7102 #ifdef FEAT_NETBEANS_INTG
7103 pos_T pos;
7104 char_u *ptr;
7105 int count;
7106 #endif
7108 if (checkclearopq(cap->oap))
7109 return;
7111 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7113 clearopbeep(cap->oap);
7114 return;
7117 prep_redo_cmd(cap);
7119 if (u_save_cursor() == FAIL)
7120 return;
7122 startpos = curwin->w_cursor;
7123 #ifdef FEAT_NETBEANS_INTG
7124 pos = startpos;
7125 #endif
7126 for (n = cap->count1; n > 0; --n)
7128 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7129 inc_cursor();
7130 if (gchar_cursor() == NUL)
7132 if (vim_strchr(p_ww, '~') != NULL
7133 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7135 #ifdef FEAT_NETBEANS_INTG
7136 if (usingNetbeans)
7138 if (did_change)
7140 ptr = ml_get(pos.lnum);
7141 count = (int)STRLEN(ptr) - pos.col;
7142 netbeans_removed(curbuf, pos.lnum, pos.col,
7143 (long)count);
7144 netbeans_inserted(curbuf, pos.lnum, pos.col,
7145 &ptr[pos.col], count);
7147 pos.col = 0;
7148 pos.lnum++;
7150 #endif
7151 ++curwin->w_cursor.lnum;
7152 curwin->w_cursor.col = 0;
7153 if (n > 1)
7155 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7156 break;
7157 u_clearline();
7160 else
7161 break;
7164 #ifdef FEAT_NETBEANS_INTG
7165 if (did_change && usingNetbeans)
7167 ptr = ml_get(pos.lnum);
7168 count = curwin->w_cursor.col - pos.col;
7169 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7170 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
7172 #endif
7175 check_cursor();
7176 curwin->w_set_curswant = TRUE;
7177 if (did_change)
7179 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7180 0L);
7181 curbuf->b_op_start = startpos;
7182 curbuf->b_op_end = curwin->w_cursor;
7183 if (curbuf->b_op_end.col > 0)
7184 --curbuf->b_op_end.col;
7189 * Move cursor to mark.
7191 static void
7192 nv_cursormark(cap, flag, pos)
7193 cmdarg_T *cap;
7194 int flag;
7195 pos_T *pos;
7197 if (check_mark(pos) == FAIL)
7198 clearop(cap->oap);
7199 else
7201 if (cap->cmdchar == '\''
7202 || cap->cmdchar == '`'
7203 || cap->cmdchar == '['
7204 || cap->cmdchar == ']')
7205 setpcmark();
7206 curwin->w_cursor = *pos;
7207 if (flag)
7208 beginline(BL_WHITE | BL_FIX);
7209 else
7210 check_cursor();
7212 cap->oap->motion_type = flag ? MLINE : MCHAR;
7213 if (cap->cmdchar == '`')
7214 cap->oap->use_reg_one = TRUE;
7215 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7216 curwin->w_set_curswant = TRUE;
7219 #ifdef FEAT_VISUAL
7221 * Handle commands that are operators in Visual mode.
7223 static void
7224 v_visop(cap)
7225 cmdarg_T *cap;
7227 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7229 /* Uppercase means linewise, except in block mode, then "D" deletes till
7230 * the end of the line, and "C" replaces til EOL */
7231 if (isupper(cap->cmdchar))
7233 if (VIsual_mode != Ctrl_V)
7234 VIsual_mode = 'V';
7235 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7236 curwin->w_curswant = MAXCOL;
7238 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7239 nv_operator(cap);
7241 #endif
7244 * "s" and "S" commands.
7246 static void
7247 nv_subst(cap)
7248 cmdarg_T *cap;
7250 #ifdef FEAT_VISUAL
7251 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7253 if (cap->cmdchar == 'S')
7254 VIsual_mode = 'V';
7255 cap->cmdchar = 'c';
7256 nv_operator(cap);
7258 else
7259 #endif
7260 nv_optrans(cap);
7264 * Abbreviated commands.
7266 static void
7267 nv_abbrev(cap)
7268 cmdarg_T *cap;
7270 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7271 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7273 #ifdef FEAT_VISUAL
7274 /* in Visual mode these commands are operators */
7275 if (VIsual_active)
7276 v_visop(cap);
7277 else
7278 #endif
7279 nv_optrans(cap);
7283 * Translate a command into another command.
7285 static void
7286 nv_optrans(cap)
7287 cmdarg_T *cap;
7289 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7290 (char_u *)"d$", (char_u *)"c$",
7291 (char_u *)"cl", (char_u *)"cc",
7292 (char_u *)"yy", (char_u *)":s\r"};
7293 static char_u *str = (char_u *)"xXDCsSY&";
7295 if (!checkclearopq(cap->oap))
7297 /* In Vi "2D" doesn't delete the next line. Can't translate it
7298 * either, because "2." should also not use the count. */
7299 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7301 cap->oap->start = curwin->w_cursor;
7302 cap->oap->op_type = OP_DELETE;
7303 #ifdef FEAT_EVAL
7304 set_op_var(OP_DELETE);
7305 #endif
7306 cap->count1 = 1;
7307 nv_dollar(cap);
7308 finish_op = TRUE;
7309 ResetRedobuff();
7310 AppendCharToRedobuff('D');
7312 else
7314 if (cap->count0)
7315 stuffnumReadbuff(cap->count0);
7316 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7319 cap->opcount = 0;
7323 * "'" and "`" commands. Also for "g'" and "g`".
7324 * cap->arg is TRUE for "'" and "g'".
7326 static void
7327 nv_gomark(cap)
7328 cmdarg_T *cap;
7330 pos_T *pos;
7331 int c;
7332 #ifdef FEAT_FOLDING
7333 linenr_T lnum = curwin->w_cursor.lnum;
7334 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7335 #endif
7337 if (cap->cmdchar == 'g')
7338 c = cap->extra_char;
7339 else
7340 c = cap->nchar;
7341 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7342 if (pos == (pos_T *)-1) /* jumped to other file */
7344 if (cap->arg)
7346 check_cursor_lnum();
7347 beginline(BL_WHITE | BL_FIX);
7349 else
7350 check_cursor();
7352 else
7353 nv_cursormark(cap, cap->arg, pos);
7355 #ifdef FEAT_VIRTUALEDIT
7356 /* May need to clear the coladd that a mark includes. */
7357 if (!virtual_active())
7358 curwin->w_cursor.coladd = 0;
7359 #endif
7360 #ifdef FEAT_FOLDING
7361 if (cap->oap->op_type == OP_NOP
7362 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7363 && (fdo_flags & FDO_MARK)
7364 && old_KeyTyped)
7365 foldOpenCursor();
7366 #endif
7370 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7372 static void
7373 nv_pcmark(cap)
7374 cmdarg_T *cap;
7376 #ifdef FEAT_JUMPLIST
7377 pos_T *pos;
7378 # ifdef FEAT_FOLDING
7379 linenr_T lnum = curwin->w_cursor.lnum;
7380 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7381 # endif
7383 if (!checkclearopq(cap->oap))
7385 if (cap->cmdchar == 'g')
7386 pos = movechangelist((int)cap->count1);
7387 else
7388 pos = movemark((int)cap->count1);
7389 if (pos == (pos_T *)-1) /* jump to other file */
7391 curwin->w_set_curswant = TRUE;
7392 check_cursor();
7394 else if (pos != NULL) /* can jump */
7395 nv_cursormark(cap, FALSE, pos);
7396 else if (cap->cmdchar == 'g')
7398 if (curbuf->b_changelistlen == 0)
7399 EMSG(_("E664: changelist is empty"));
7400 else if (cap->count1 < 0)
7401 EMSG(_("E662: At start of changelist"));
7402 else
7403 EMSG(_("E663: At end of changelist"));
7405 else
7406 clearopbeep(cap->oap);
7407 # ifdef FEAT_FOLDING
7408 if (cap->oap->op_type == OP_NOP
7409 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7410 && (fdo_flags & FDO_MARK)
7411 && old_KeyTyped)
7412 foldOpenCursor();
7413 # endif
7415 #else
7416 clearopbeep(cap->oap);
7417 #endif
7421 * Handle '"' command.
7423 static void
7424 nv_regname(cap)
7425 cmdarg_T *cap;
7427 if (checkclearop(cap->oap))
7428 return;
7429 #ifdef FEAT_EVAL
7430 if (cap->nchar == '=')
7431 cap->nchar = get_expr_register();
7432 #endif
7433 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7435 cap->oap->regname = cap->nchar;
7436 cap->opcount = cap->count0; /* remember count before '"' */
7437 #ifdef FEAT_EVAL
7438 set_reg_var(cap->oap->regname);
7439 #endif
7441 else
7442 clearopbeep(cap->oap);
7445 #ifdef FEAT_VISUAL
7447 * Handle "v", "V" and "CTRL-V" commands.
7448 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7449 * is TRUE.
7450 * Handle CTRL-Q just like CTRL-V.
7452 static void
7453 nv_visual(cap)
7454 cmdarg_T *cap;
7456 if (cap->cmdchar == Ctrl_Q)
7457 cap->cmdchar = Ctrl_V;
7459 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7460 * characterwise, linewise, or blockwise. */
7461 if (cap->oap->op_type != OP_NOP)
7463 cap->oap->motion_force = cap->cmdchar;
7464 finish_op = FALSE; /* operator doesn't finish now but later */
7465 return;
7468 VIsual_select = cap->arg;
7469 if (VIsual_active) /* change Visual mode */
7471 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7472 end_visual_mode();
7473 else /* toggle char/block mode */
7474 { /* or char/line mode */
7475 VIsual_mode = cap->cmdchar;
7476 showmode();
7478 redraw_curbuf_later(INVERTED); /* update the inversion */
7480 else /* start Visual mode */
7482 check_visual_highlight();
7483 if (cap->count0) /* use previously selected part */
7485 if (resel_VIsual_mode == NUL) /* there is none */
7487 beep_flush();
7488 return;
7490 VIsual = curwin->w_cursor;
7492 VIsual_active = TRUE;
7493 VIsual_reselect = TRUE;
7494 if (!cap->arg)
7495 /* start Select mode when 'selectmode' contains "cmd" */
7496 may_start_select('c');
7497 #ifdef FEAT_MOUSE
7498 setmouse();
7499 #endif
7500 if (p_smd && msg_silent == 0)
7501 redraw_cmdline = TRUE; /* show visual mode later */
7503 * For V and ^V, we multiply the number of lines even if there
7504 * was only one -- webb
7506 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7508 curwin->w_cursor.lnum +=
7509 resel_VIsual_line_count * cap->count0 - 1;
7510 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7511 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7513 VIsual_mode = resel_VIsual_mode;
7514 if (VIsual_mode == 'v')
7516 if (resel_VIsual_line_count <= 1)
7517 curwin->w_cursor.col += resel_VIsual_col * cap->count0 - 1;
7518 else
7519 curwin->w_cursor.col = resel_VIsual_col;
7520 check_cursor_col();
7522 if (resel_VIsual_col == MAXCOL)
7524 curwin->w_curswant = MAXCOL;
7525 coladvance((colnr_T)MAXCOL);
7527 else if (VIsual_mode == Ctrl_V)
7529 validate_virtcol();
7530 curwin->w_curswant = curwin->w_virtcol
7531 + resel_VIsual_col * cap->count0 - 1;
7532 coladvance(curwin->w_curswant);
7534 else
7535 curwin->w_set_curswant = TRUE;
7536 redraw_curbuf_later(INVERTED); /* show the inversion */
7538 else
7540 if (!cap->arg)
7541 /* start Select mode when 'selectmode' contains "cmd" */
7542 may_start_select('c');
7543 n_start_visual_mode(cap->cmdchar);
7549 * Start selection for Shift-movement keys.
7551 void
7552 start_selection()
7554 /* if 'selectmode' contains "key", start Select mode */
7555 may_start_select('k');
7556 n_start_visual_mode('v');
7560 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7562 void
7563 may_start_select(c)
7564 int c;
7566 VIsual_select = (stuff_empty() && typebuf_typed()
7567 && (vim_strchr(p_slm, c) != NULL));
7571 * Start Visual mode "c".
7572 * Should set VIsual_select before calling this.
7574 static void
7575 n_start_visual_mode(c)
7576 int c;
7578 VIsual_mode = c;
7579 VIsual_active = TRUE;
7580 VIsual_reselect = TRUE;
7581 #ifdef FEAT_VIRTUALEDIT
7582 /* Corner case: the 0 position in a tab may change when going into
7583 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7585 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7586 coladvance(curwin->w_virtcol);
7587 #endif
7588 VIsual = curwin->w_cursor;
7590 #ifdef FEAT_FOLDING
7591 foldAdjustVisual();
7592 #endif
7594 #ifdef FEAT_MOUSE
7595 setmouse();
7596 #endif
7597 if (p_smd && msg_silent == 0)
7598 redraw_cmdline = TRUE; /* show visual mode later */
7599 #ifdef FEAT_CLIPBOARD
7600 /* Make sure the clipboard gets updated. Needed because start and
7601 * end may still be the same, and the selection needs to be owned */
7602 clip_star.vmode = NUL;
7603 #endif
7605 /* Only need to redraw this line, unless still need to redraw an old
7606 * Visual area (when 'lazyredraw' is set). */
7607 if (curwin->w_redr_type < INVERTED)
7609 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7610 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7614 #endif /* FEAT_VISUAL */
7617 * CTRL-W: Window commands
7619 static void
7620 nv_window(cap)
7621 cmdarg_T *cap;
7623 #ifdef FEAT_WINDOWS
7624 if (!checkclearop(cap->oap))
7625 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7626 #else
7627 (void)checkclearop(cap->oap);
7628 #endif
7632 * CTRL-Z: Suspend
7634 static void
7635 nv_suspend(cap)
7636 cmdarg_T *cap;
7638 clearop(cap->oap);
7639 #ifdef FEAT_VISUAL
7640 if (VIsual_active)
7641 end_visual_mode(); /* stop Visual mode */
7642 #endif
7643 do_cmdline_cmd((char_u *)"st");
7647 * Commands starting with "g".
7649 static void
7650 nv_g_cmd(cap)
7651 cmdarg_T *cap;
7653 oparg_T *oap = cap->oap;
7654 #ifdef FEAT_VISUAL
7655 pos_T tpos;
7656 #endif
7657 int i;
7658 int flag = FALSE;
7660 switch (cap->nchar)
7662 #ifdef MEM_PROFILE
7664 * "g^A": dump log of used memory.
7666 case Ctrl_A:
7667 vim_mem_profile_dump();
7668 break;
7669 #endif
7671 #ifdef FEAT_VREPLACE
7673 * "gR": Enter virtual replace mode.
7675 case 'R':
7676 cap->arg = TRUE;
7677 nv_Replace(cap);
7678 break;
7680 case 'r':
7681 nv_vreplace(cap);
7682 break;
7683 #endif
7685 case '&':
7686 do_cmdline_cmd((char_u *)"%s//~/&");
7687 break;
7689 #ifdef FEAT_VISUAL
7691 * "gv": Reselect the previous Visual area. If Visual already active,
7692 * exchange previous and current Visual area.
7694 case 'v':
7695 if (checkclearop(oap))
7696 break;
7698 if ( curbuf->b_visual.vi_start.lnum == 0
7699 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7700 || curbuf->b_visual.vi_end.lnum == 0)
7701 beep_flush();
7702 else
7704 /* set w_cursor to the start of the Visual area, tpos to the end */
7705 if (VIsual_active)
7707 i = VIsual_mode;
7708 VIsual_mode = curbuf->b_visual.vi_mode;
7709 curbuf->b_visual.vi_mode = i;
7710 # ifdef FEAT_EVAL
7711 curbuf->b_visual_mode_eval = i;
7712 # endif
7713 i = curwin->w_curswant;
7714 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7715 curbuf->b_visual.vi_curswant = i;
7717 tpos = curbuf->b_visual.vi_end;
7718 curbuf->b_visual.vi_end = curwin->w_cursor;
7719 curwin->w_cursor = curbuf->b_visual.vi_start;
7720 curbuf->b_visual.vi_start = VIsual;
7722 else
7724 VIsual_mode = curbuf->b_visual.vi_mode;
7725 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7726 tpos = curbuf->b_visual.vi_end;
7727 curwin->w_cursor = curbuf->b_visual.vi_start;
7730 VIsual_active = TRUE;
7731 VIsual_reselect = TRUE;
7733 /* Set Visual to the start and w_cursor to the end of the Visual
7734 * area. Make sure they are on an existing character. */
7735 check_cursor();
7736 VIsual = curwin->w_cursor;
7737 curwin->w_cursor = tpos;
7738 check_cursor();
7739 update_topline();
7741 * When called from normal "g" command: start Select mode when
7742 * 'selectmode' contains "cmd". When called for K_SELECT, always
7743 * start Select mode.
7745 if (cap->arg)
7746 VIsual_select = TRUE;
7747 else
7748 may_start_select('c');
7749 #ifdef FEAT_MOUSE
7750 setmouse();
7751 #endif
7752 #ifdef FEAT_CLIPBOARD
7753 /* Make sure the clipboard gets updated. Needed because start and
7754 * end are still the same, and the selection needs to be owned */
7755 clip_star.vmode = NUL;
7756 #endif
7757 redraw_curbuf_later(INVERTED);
7758 showmode();
7760 break;
7762 * "gV": Don't reselect the previous Visual area after a Select mode
7763 * mapping of menu.
7765 case 'V':
7766 VIsual_reselect = FALSE;
7767 break;
7770 * "gh": start Select mode.
7771 * "gH": start Select line mode.
7772 * "g^H": start Select block mode.
7774 case K_BS:
7775 cap->nchar = Ctrl_H;
7776 /* FALLTHROUGH */
7777 case 'h':
7778 case 'H':
7779 case Ctrl_H:
7780 # ifdef EBCDIC
7781 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7782 if (cap->nchar == Ctrl_H)
7783 cap->cmdchar = Ctrl_V;
7784 else
7785 # endif
7786 cap->cmdchar = cap->nchar + ('v' - 'h');
7787 cap->arg = TRUE;
7788 nv_visual(cap);
7789 break;
7790 #endif /* FEAT_VISUAL */
7793 * "gj" and "gk" two new funny movement keys -- up and down
7794 * movement based on *screen* line rather than *file* line.
7796 case 'j':
7797 case K_DOWN:
7798 /* with 'nowrap' it works just like the normal "j" command; also when
7799 * in a closed fold */
7800 if (!curwin->w_p_wrap
7801 #ifdef FEAT_FOLDING
7802 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7803 #endif
7806 oap->motion_type = MLINE;
7807 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7809 else
7810 i = nv_screengo(oap, FORWARD, cap->count1);
7811 if (i == FAIL)
7812 clearopbeep(oap);
7813 break;
7815 case 'k':
7816 case K_UP:
7817 /* with 'nowrap' it works just like the normal "k" command; also when
7818 * in a closed fold */
7819 if (!curwin->w_p_wrap
7820 #ifdef FEAT_FOLDING
7821 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7822 #endif
7825 oap->motion_type = MLINE;
7826 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7828 else
7829 i = nv_screengo(oap, BACKWARD, cap->count1);
7830 if (i == FAIL)
7831 clearopbeep(oap);
7832 break;
7835 * "gJ": join two lines without inserting a space.
7837 case 'J':
7838 nv_join(cap);
7839 break;
7842 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7843 * "gm": middle of "g0" and "g$".
7845 case '^':
7846 flag = TRUE;
7847 /* FALLTHROUGH */
7849 case '0':
7850 case 'm':
7851 case K_HOME:
7852 case K_KHOME:
7853 oap->motion_type = MCHAR;
7854 oap->inclusive = FALSE;
7855 if (curwin->w_p_wrap
7856 #ifdef FEAT_VERTSPLIT
7857 && curwin->w_width != 0
7858 #endif
7861 int width1 = W_WIDTH(curwin) - curwin_col_off();
7862 int width2 = width1 + curwin_col_off2();
7864 validate_virtcol();
7865 i = 0;
7866 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
7867 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
7869 else
7870 i = curwin->w_leftcol;
7871 /* Go to the middle of the screen line. When 'number' is on and lines
7872 * are wrapping the middle can be more to the left. */
7873 if (cap->nchar == 'm')
7874 i += (W_WIDTH(curwin) - curwin_col_off()
7875 + ((curwin->w_p_wrap && i > 0)
7876 ? curwin_col_off2() : 0)) / 2;
7877 coladvance((colnr_T)i);
7878 if (flag)
7881 i = gchar_cursor();
7882 while (vim_iswhite(i) && oneright() == OK);
7884 curwin->w_set_curswant = TRUE;
7885 break;
7887 case '_':
7888 /* "g_": to the last non-blank character in the line or <count> lines
7889 * downward. */
7890 cap->oap->motion_type = MCHAR;
7891 cap->oap->inclusive = TRUE;
7892 curwin->w_curswant = MAXCOL;
7893 if (cursor_down((long)(cap->count1 - 1),
7894 cap->oap->op_type == OP_NOP) == FAIL)
7895 clearopbeep(cap->oap);
7896 else
7898 char_u *ptr = ml_get_curline();
7900 /* In Visual mode we may end up after the line. */
7901 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
7902 --curwin->w_cursor.col;
7904 /* Decrease the cursor column until it's on a non-blank. */
7905 while (curwin->w_cursor.col > 0
7906 && vim_iswhite(ptr[curwin->w_cursor.col]))
7907 --curwin->w_cursor.col;
7908 curwin->w_set_curswant = TRUE;
7910 break;
7912 case '$':
7913 case K_END:
7914 case K_KEND:
7916 int col_off = curwin_col_off();
7918 oap->motion_type = MCHAR;
7919 oap->inclusive = TRUE;
7920 if (curwin->w_p_wrap
7921 #ifdef FEAT_VERTSPLIT
7922 && curwin->w_width != 0
7923 #endif
7926 curwin->w_curswant = MAXCOL; /* so we stay at the end */
7927 if (cap->count1 == 1)
7929 int width1 = W_WIDTH(curwin) - col_off;
7930 int width2 = width1 + curwin_col_off2();
7932 validate_virtcol();
7933 i = width1 - 1;
7934 if (curwin->w_virtcol >= (colnr_T)width1)
7935 i += ((curwin->w_virtcol - width1) / width2 + 1)
7936 * width2;
7937 coladvance((colnr_T)i);
7938 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
7939 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
7942 * Check for landing on a character that got split at
7943 * the end of the line. We do not want to advance to
7944 * the next screen line.
7946 validate_virtcol();
7947 if (curwin->w_virtcol > (colnr_T)i)
7948 --curwin->w_cursor.col;
7950 #endif
7952 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
7953 clearopbeep(oap);
7955 else
7957 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
7958 coladvance((colnr_T)i);
7960 /* Make sure we stick in this column. */
7961 validate_virtcol();
7962 curwin->w_curswant = curwin->w_virtcol;
7963 curwin->w_set_curswant = FALSE;
7966 break;
7969 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
7971 case '*':
7972 case '#':
7973 #if POUND != '#'
7974 case POUND: /* pound sign (sometimes equal to '#') */
7975 #endif
7976 case Ctrl_RSB: /* :tag or :tselect for current identifier */
7977 case ']': /* :tselect for current identifier */
7978 nv_ident(cap);
7979 break;
7982 * ge and gE: go back to end of word
7984 case 'e':
7985 case 'E':
7986 oap->motion_type = MCHAR;
7987 curwin->w_set_curswant = TRUE;
7988 oap->inclusive = TRUE;
7989 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
7990 clearopbeep(oap);
7991 break;
7994 * "g CTRL-G": display info about cursor position
7996 case Ctrl_G:
7997 cursor_pos_info();
7998 break;
8001 * "gi": start Insert at the last position.
8003 case 'i':
8004 if (curbuf->b_last_insert.lnum != 0)
8006 curwin->w_cursor = curbuf->b_last_insert;
8007 check_cursor_lnum();
8008 i = (int)STRLEN(ml_get_curline());
8009 if (curwin->w_cursor.col > (colnr_T)i)
8011 #ifdef FEAT_VIRTUALEDIT
8012 if (virtual_active())
8013 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8014 #endif
8015 curwin->w_cursor.col = i;
8018 cap->cmdchar = 'i';
8019 nv_edit(cap);
8020 break;
8023 * "gI": Start insert in column 1.
8025 case 'I':
8026 beginline(0);
8027 if (!checkclearopq(oap))
8028 invoke_edit(cap, FALSE, 'g', FALSE);
8029 break;
8031 #ifdef FEAT_SEARCHPATH
8033 * "gf": goto file, edit file under cursor
8034 * "]f" and "[f": can also be used.
8036 case 'f':
8037 case 'F':
8038 nv_gotofile(cap);
8039 break;
8040 #endif
8042 /* "g'm" and "g`m": jump to mark without setting pcmark */
8043 case '\'':
8044 cap->arg = TRUE;
8045 /*FALLTHROUGH*/
8046 case '`':
8047 nv_gomark(cap);
8048 break;
8051 * "gs": Goto sleep.
8053 case 's':
8054 do_sleep(cap->count1 * 1000L);
8055 break;
8058 * "ga": Display the ascii value of the character under the
8059 * cursor. It is displayed in decimal, hex, and octal. -- webb
8061 case 'a':
8062 do_ascii(NULL);
8063 break;
8065 #ifdef FEAT_MBYTE
8067 * "g8": Display the bytes used for the UTF-8 character under the
8068 * cursor. It is displayed in hex.
8069 * "8g8" finds illegal byte sequence.
8071 case '8':
8072 if (cap->count0 == 8)
8073 utf_find_illegal();
8074 else
8075 show_utf8();
8076 break;
8077 #endif
8079 case '<':
8080 show_sb_text();
8081 break;
8084 * "gg": Goto the first line in file. With a count it goes to
8085 * that line number like for "G". -- webb
8087 case 'g':
8088 cap->arg = FALSE;
8089 nv_goto(cap);
8090 break;
8093 * Two-character operators:
8094 * "gq" Format text
8095 * "gw" Format text and keep cursor position
8096 * "g~" Toggle the case of the text.
8097 * "gu" Change text to lower case.
8098 * "gU" Change text to upper case.
8099 * "g?" rot13 encoding
8100 * "g@" call 'operatorfunc'
8102 case 'q':
8103 case 'w':
8104 oap->cursor_start = curwin->w_cursor;
8105 /*FALLTHROUGH*/
8106 case '~':
8107 case 'u':
8108 case 'U':
8109 case '?':
8110 case '@':
8111 nv_operator(cap);
8112 break;
8115 * "gd": Find first occurrence of pattern under the cursor in the
8116 * current function
8117 * "gD": idem, but in the current file.
8119 case 'd':
8120 case 'D':
8121 nv_gd(oap, cap->nchar, (int)cap->count0);
8122 break;
8124 #ifdef FEAT_MOUSE
8126 * g<*Mouse> : <C-*mouse>
8128 case K_MIDDLEMOUSE:
8129 case K_MIDDLEDRAG:
8130 case K_MIDDLERELEASE:
8131 case K_LEFTMOUSE:
8132 case K_LEFTDRAG:
8133 case K_LEFTRELEASE:
8134 case K_RIGHTMOUSE:
8135 case K_RIGHTDRAG:
8136 case K_RIGHTRELEASE:
8137 case K_X1MOUSE:
8138 case K_X1DRAG:
8139 case K_X1RELEASE:
8140 case K_X2MOUSE:
8141 case K_X2DRAG:
8142 case K_X2RELEASE:
8143 mod_mask = MOD_MASK_CTRL;
8144 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8145 break;
8146 #endif
8148 case K_IGNORE:
8149 break;
8152 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8154 case 'p':
8155 case 'P':
8156 nv_put(cap);
8157 break;
8159 #ifdef FEAT_BYTEOFF
8160 /* "go": goto byte count from start of buffer */
8161 case 'o':
8162 goto_byte(cap->count0);
8163 break;
8164 #endif
8166 /* "gQ": improved Ex mode */
8167 case 'Q':
8168 if (text_locked())
8170 clearopbeep(cap->oap);
8171 text_locked_msg();
8172 break;
8175 if (!checkclearopq(oap))
8176 do_exmode(TRUE);
8177 break;
8179 #ifdef FEAT_JUMPLIST
8180 case ',':
8181 nv_pcmark(cap);
8182 break;
8184 case ';':
8185 cap->count1 = -cap->count1;
8186 nv_pcmark(cap);
8187 break;
8188 #endif
8190 #ifdef FEAT_WINDOWS
8191 case 't':
8192 goto_tabpage((int)cap->count0);
8193 break;
8194 case 'T':
8195 goto_tabpage(-(int)cap->count1);
8196 break;
8197 #endif
8199 case '+':
8200 case '-': /* "g+" and "g-": undo or redo along the timeline */
8201 if (!checkclearopq(oap))
8202 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
8203 FALSE, FALSE);
8204 break;
8206 default:
8207 clearopbeep(oap);
8208 break;
8213 * Handle "o" and "O" commands.
8215 static void
8216 n_opencmd(cap)
8217 cmdarg_T *cap;
8219 if (!checkclearopq(cap->oap))
8221 #ifdef FEAT_FOLDING
8222 if (cap->cmdchar == 'O')
8223 /* Open above the first line of a folded sequence of lines */
8224 (void)hasFolding(curwin->w_cursor.lnum,
8225 &curwin->w_cursor.lnum, NULL);
8226 else
8227 /* Open below the last line of a folded sequence of lines */
8228 (void)hasFolding(curwin->w_cursor.lnum,
8229 NULL, &curwin->w_cursor.lnum);
8230 #endif
8231 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8232 (cap->cmdchar == 'O' ? 1 : 0)),
8233 (linenr_T)(curwin->w_cursor.lnum +
8234 (cap->cmdchar == 'o' ? 1 : 0))
8235 ) == OK
8236 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8237 #ifdef FEAT_COMMENTS
8238 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8239 #endif
8240 0, 0))
8242 /* When '#' is in 'cpoptions' ignore the count. */
8243 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8244 cap->count1 = 1;
8245 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8251 * "." command: redo last change.
8253 static void
8254 nv_dot(cap)
8255 cmdarg_T *cap;
8257 if (!checkclearopq(cap->oap))
8260 * If "restart_edit" is TRUE, the last but one command is repeated
8261 * instead of the last command (inserting text). This is used for
8262 * CTRL-O <.> in insert mode.
8264 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8265 clearopbeep(cap->oap);
8270 * CTRL-R: undo undo
8272 static void
8273 nv_redo(cap)
8274 cmdarg_T *cap;
8276 if (!checkclearopq(cap->oap))
8278 u_redo((int)cap->count1);
8279 curwin->w_set_curswant = TRUE;
8284 * Handle "U" command.
8286 static void
8287 nv_Undo(cap)
8288 cmdarg_T *cap;
8290 /* In Visual mode and typing "gUU" triggers an operator */
8291 if (cap->oap->op_type == OP_UPPER
8292 #ifdef FEAT_VISUAL
8293 || VIsual_active
8294 #endif
8297 /* translate "gUU" to "gUgU" */
8298 cap->cmdchar = 'g';
8299 cap->nchar = 'U';
8300 nv_operator(cap);
8302 else if (!checkclearopq(cap->oap))
8304 u_undoline();
8305 curwin->w_set_curswant = TRUE;
8310 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8311 * single character.
8313 static void
8314 nv_tilde(cap)
8315 cmdarg_T *cap;
8317 if (!p_to
8318 #ifdef FEAT_VISUAL
8319 && !VIsual_active
8320 #endif
8321 && cap->oap->op_type != OP_TILDE)
8322 n_swapchar(cap);
8323 else
8324 nv_operator(cap);
8328 * Handle an operator command.
8329 * The actual work is done by do_pending_operator().
8331 static void
8332 nv_operator(cap)
8333 cmdarg_T *cap;
8335 int op_type;
8337 op_type = get_op_type(cap->cmdchar, cap->nchar);
8339 if (op_type == cap->oap->op_type) /* double operator works on lines */
8340 nv_lineop(cap);
8341 else if (!checkclearop(cap->oap))
8343 cap->oap->start = curwin->w_cursor;
8344 cap->oap->op_type = op_type;
8345 #ifdef FEAT_EVAL
8346 set_op_var(op_type);
8347 #endif
8351 #ifdef FEAT_EVAL
8353 * Set v:operator to the characters for "optype".
8355 static void
8356 set_op_var(optype)
8357 int optype;
8359 char_u opchars[3];
8361 if (optype == OP_NOP)
8362 set_vim_var_string(VV_OP, NULL, 0);
8363 else
8365 opchars[0] = get_op_char(optype);
8366 opchars[1] = get_extra_op_char(optype);
8367 opchars[2] = NUL;
8368 set_vim_var_string(VV_OP, opchars, -1);
8371 #endif
8374 * Handle linewise operator "dd", "yy", etc.
8376 * "_" is is a strange motion command that helps make operators more logical.
8377 * It is actually implemented, but not documented in the real Vi. This motion
8378 * command actually refers to "the current line". Commands like "dd" and "yy"
8379 * are really an alternate form of "d_" and "y_". It does accept a count, so
8380 * "d3_" works to delete 3 lines.
8382 static void
8383 nv_lineop(cap)
8384 cmdarg_T *cap;
8386 cap->oap->motion_type = MLINE;
8387 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8388 clearopbeep(cap->oap);
8389 else if ( cap->oap->op_type == OP_DELETE
8390 || cap->oap->op_type == OP_LSHIFT
8391 || cap->oap->op_type == OP_RSHIFT)
8392 beginline(BL_SOL | BL_FIX);
8393 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8394 beginline(BL_WHITE | BL_FIX);
8398 * <Home> command.
8400 static void
8401 nv_home(cap)
8402 cmdarg_T *cap;
8404 /* CTRL-HOME is like "gg" */
8405 if (mod_mask & MOD_MASK_CTRL)
8406 nv_goto(cap);
8407 else
8409 cap->count0 = 1;
8410 nv_pipe(cap);
8412 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8413 one-character line). */
8417 * "|" command.
8419 static void
8420 nv_pipe(cap)
8421 cmdarg_T *cap;
8423 cap->oap->motion_type = MCHAR;
8424 cap->oap->inclusive = FALSE;
8425 beginline(0);
8426 if (cap->count0 > 0)
8428 coladvance((colnr_T)(cap->count0 - 1));
8429 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8431 else
8432 curwin->w_curswant = 0;
8433 /* keep curswant at the column where we wanted to go, not where
8434 we ended; differs if line is too short */
8435 curwin->w_set_curswant = FALSE;
8439 * Handle back-word command "b" and "B".
8440 * cap->arg is 1 for "B"
8442 static void
8443 nv_bck_word(cap)
8444 cmdarg_T *cap;
8446 cap->oap->motion_type = MCHAR;
8447 cap->oap->inclusive = FALSE;
8448 curwin->w_set_curswant = TRUE;
8449 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8450 clearopbeep(cap->oap);
8451 #ifdef FEAT_FOLDING
8452 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8453 foldOpenCursor();
8454 #endif
8458 * Handle word motion commands "e", "E", "w" and "W".
8459 * cap->arg is TRUE for "E" and "W".
8461 static void
8462 nv_wordcmd(cap)
8463 cmdarg_T *cap;
8465 int n;
8466 int word_end;
8467 int flag = FALSE;
8468 pos_T startpos = curwin->w_cursor;
8471 * Set inclusive for the "E" and "e" command.
8473 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8474 word_end = TRUE;
8475 else
8476 word_end = FALSE;
8477 cap->oap->inclusive = word_end;
8480 * "cw" and "cW" are a special case.
8482 if (!word_end && cap->oap->op_type == OP_CHANGE)
8484 n = gchar_cursor();
8485 if (n != NUL) /* not an empty line */
8487 if (vim_iswhite(n))
8490 * Reproduce a funny Vi behaviour: "cw" on a blank only
8491 * changes one character, not all blanks until the start of
8492 * the next word. Only do this when the 'w' flag is included
8493 * in 'cpoptions'.
8495 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8497 cap->oap->inclusive = TRUE;
8498 cap->oap->motion_type = MCHAR;
8499 return;
8502 else
8505 * This is a little strange. To match what the real Vi does,
8506 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8507 * that we are not on a space or a TAB. This seems impolite
8508 * at first, but it's really more what we mean when we say
8509 * 'cw'.
8510 * Another strangeness: When standing on the end of a word
8511 * "ce" will change until the end of the next wordt, but "cw"
8512 * will change only one character! This is done by setting
8513 * flag.
8515 cap->oap->inclusive = TRUE;
8516 word_end = TRUE;
8517 flag = TRUE;
8522 cap->oap->motion_type = MCHAR;
8523 curwin->w_set_curswant = TRUE;
8524 if (word_end)
8525 n = end_word(cap->count1, cap->arg, flag, FALSE);
8526 else
8527 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8529 /* Don't leave the cursor on the NUL past the end of line. Unless we
8530 * didn't move it forward. */
8531 if (lt(startpos, curwin->w_cursor))
8532 adjust_cursor(cap->oap);
8534 if (n == FAIL && cap->oap->op_type == OP_NOP)
8535 clearopbeep(cap->oap);
8536 else
8538 #ifdef FEAT_VISUAL
8539 adjust_for_sel(cap);
8540 #endif
8541 #ifdef FEAT_FOLDING
8542 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8543 foldOpenCursor();
8544 #endif
8549 * Used after a movement command: If the cursor ends up on the NUL after the
8550 * end of the line, may move it back to the last character and make the motion
8551 * inclusive.
8553 static void
8554 adjust_cursor(oap)
8555 oparg_T *oap;
8557 /* The cursor cannot remain on the NUL when:
8558 * - the column is > 0
8559 * - not in Visual mode or 'selection' is "o"
8560 * - 'virtualedit' is not "all" and not "onemore".
8562 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
8563 #ifdef FEAT_VISUAL
8564 && (!VIsual_active || *p_sel == 'o')
8565 #endif
8566 #ifdef FEAT_VIRTUALEDIT
8567 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8568 #endif
8571 --curwin->w_cursor.col;
8572 #ifdef FEAT_MBYTE
8573 /* prevent cursor from moving on the trail byte */
8574 if (has_mbyte)
8575 mb_adjust_cursor();
8576 #endif
8577 oap->inclusive = TRUE;
8582 * "0" and "^" commands.
8583 * cap->arg is the argument for beginline().
8585 static void
8586 nv_beginline(cap)
8587 cmdarg_T *cap;
8589 cap->oap->motion_type = MCHAR;
8590 cap->oap->inclusive = FALSE;
8591 beginline(cap->arg);
8592 #ifdef FEAT_FOLDING
8593 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8594 foldOpenCursor();
8595 #endif
8596 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8597 one-character line). */
8600 #ifdef FEAT_VISUAL
8602 * In exclusive Visual mode, may include the last character.
8604 static void
8605 adjust_for_sel(cap)
8606 cmdarg_T *cap;
8608 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8609 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8611 # ifdef FEAT_MBYTE
8612 if (has_mbyte)
8613 inc_cursor();
8614 else
8615 # endif
8616 ++curwin->w_cursor.col;
8617 cap->oap->inclusive = FALSE;
8622 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8623 * Should check VIsual_mode before calling this.
8624 * Returns TRUE when backed up to the previous line.
8626 static int
8627 unadjust_for_sel()
8629 pos_T *pp;
8631 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8633 if (lt(VIsual, curwin->w_cursor))
8634 pp = &curwin->w_cursor;
8635 else
8636 pp = &VIsual;
8637 #ifdef FEAT_VIRTUALEDIT
8638 if (pp->coladd > 0)
8639 --pp->coladd;
8640 else
8641 #endif
8642 if (pp->col > 0)
8644 --pp->col;
8645 #ifdef FEAT_MBYTE
8646 mb_adjustpos(pp);
8647 #endif
8649 else if (pp->lnum > 1)
8651 --pp->lnum;
8652 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8653 return TRUE;
8656 return FALSE;
8660 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8662 static void
8663 nv_select(cap)
8664 cmdarg_T *cap;
8666 if (VIsual_active)
8667 VIsual_select = TRUE;
8668 else if (VIsual_reselect)
8670 cap->nchar = 'v'; /* fake "gv" command */
8671 cap->arg = TRUE;
8672 nv_g_cmd(cap);
8676 #endif
8679 * "G", "gg", CTRL-END, CTRL-HOME.
8680 * cap->arg is TRUE for "G".
8682 static void
8683 nv_goto(cap)
8684 cmdarg_T *cap;
8686 linenr_T lnum;
8688 if (cap->arg)
8689 lnum = curbuf->b_ml.ml_line_count;
8690 else
8691 lnum = 1L;
8692 cap->oap->motion_type = MLINE;
8693 setpcmark();
8695 /* When a count is given, use it instead of the default lnum */
8696 if (cap->count0 != 0)
8697 lnum = cap->count0;
8698 if (lnum < 1L)
8699 lnum = 1L;
8700 else if (lnum > curbuf->b_ml.ml_line_count)
8701 lnum = curbuf->b_ml.ml_line_count;
8702 curwin->w_cursor.lnum = lnum;
8703 beginline(BL_SOL | BL_FIX);
8704 #ifdef FEAT_FOLDING
8705 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8706 foldOpenCursor();
8707 #endif
8711 * CTRL-\ in Normal mode.
8713 static void
8714 nv_normal(cap)
8715 cmdarg_T *cap;
8717 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8719 clearop(cap->oap);
8720 if (restart_edit != 0 && mode_displayed)
8721 clear_cmdline = TRUE; /* unshow mode later */
8722 restart_edit = 0;
8723 #ifdef FEAT_CMDWIN
8724 if (cmdwin_type != 0)
8725 cmdwin_result = Ctrl_C;
8726 #endif
8727 #ifdef FEAT_VISUAL
8728 if (VIsual_active)
8730 end_visual_mode(); /* stop Visual */
8731 redraw_curbuf_later(INVERTED);
8733 #endif
8734 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8735 if (cap->nchar == Ctrl_G && p_im)
8736 restart_edit = 'a';
8738 else
8739 clearopbeep(cap->oap);
8743 * ESC in Normal mode: beep, but don't flush buffers.
8744 * Don't even beep if we are canceling a command.
8746 static void
8747 nv_esc(cap)
8748 cmdarg_T *cap;
8750 int no_reason;
8752 no_reason = (cap->oap->op_type == OP_NOP
8753 && cap->opcount == 0
8754 && cap->count0 == 0
8755 && cap->oap->regname == 0
8756 && !p_im);
8758 if (cap->arg) /* TRUE for CTRL-C */
8760 if (restart_edit == 0
8761 #ifdef FEAT_CMDWIN
8762 && cmdwin_type == 0
8763 #endif
8764 #ifdef FEAT_VISUAL
8765 && !VIsual_active
8766 #endif
8767 && no_reason)
8768 MSG(_("Type :quit<Enter> to exit Vim"));
8770 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8771 * set again below when halfway a mapping. */
8772 if (!p_im)
8773 restart_edit = 0;
8774 #ifdef FEAT_CMDWIN
8775 if (cmdwin_type != 0)
8777 cmdwin_result = K_IGNORE;
8778 got_int = FALSE; /* don't stop executing autocommands et al. */
8779 return;
8781 #endif
8784 #ifdef FEAT_VISUAL
8785 if (VIsual_active)
8787 end_visual_mode(); /* stop Visual */
8788 check_cursor_col(); /* make sure cursor is not beyond EOL */
8789 curwin->w_set_curswant = TRUE;
8790 redraw_curbuf_later(INVERTED);
8792 else
8793 #endif
8794 if (no_reason)
8795 vim_beep();
8796 clearop(cap->oap);
8798 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8799 * set return to Insert mode afterwards. */
8800 if (restart_edit == 0 && goto_im()
8801 #ifdef FEAT_EX_EXTRA
8802 && ex_normal_busy == 0
8803 #endif
8805 restart_edit = 'a';
8809 * Handle "A", "a", "I", "i" and <Insert> commands.
8811 static void
8812 nv_edit(cap)
8813 cmdarg_T *cap;
8815 /* <Insert> is equal to "i" */
8816 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8817 cap->cmdchar = 'i';
8819 #ifdef FEAT_VISUAL
8820 /* in Visual mode "A" and "I" are an operator */
8821 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8822 v_visop(cap);
8824 /* in Visual mode and after an operator "a" and "i" are for text objects */
8825 else
8826 #endif
8827 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8828 && (cap->oap->op_type != OP_NOP
8829 #ifdef FEAT_VISUAL
8830 || VIsual_active
8831 #endif
8834 #ifdef FEAT_TEXTOBJ
8835 nv_object(cap);
8836 #else
8837 clearopbeep(cap->oap);
8838 #endif
8840 else if (!curbuf->b_p_ma && !p_im)
8842 /* Only give this error when 'insertmode' is off. */
8843 EMSG(_(e_modifiable));
8844 clearop(cap->oap);
8846 else if (!checkclearopq(cap->oap))
8848 switch (cap->cmdchar)
8850 case 'A': /* "A"ppend after the line */
8851 curwin->w_set_curswant = TRUE;
8852 #ifdef FEAT_VIRTUALEDIT
8853 if (ve_flags == VE_ALL)
8855 int save_State = State;
8857 /* Pretent Insert mode here to allow the cursor on the
8858 * character past the end of the line */
8859 State = INSERT;
8860 coladvance((colnr_T)MAXCOL);
8861 State = save_State;
8863 else
8864 #endif
8865 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8866 break;
8868 case 'I': /* "I"nsert before the first non-blank */
8869 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
8870 beginline(BL_WHITE);
8871 else
8872 beginline(BL_WHITE|BL_FIX);
8873 break;
8875 case 'a': /* "a"ppend is like "i"nsert on the next character. */
8876 #ifdef FEAT_VIRTUALEDIT
8877 /* increment coladd when in virtual space, increment the
8878 * column otherwise, also to append after an unprintable char */
8879 if (virtual_active()
8880 && (curwin->w_cursor.coladd > 0
8881 || *ml_get_cursor() == NUL
8882 || *ml_get_cursor() == TAB))
8883 curwin->w_cursor.coladd++;
8884 else
8885 #endif
8886 if (*ml_get_cursor() != NUL)
8887 inc_cursor();
8888 break;
8891 #ifdef FEAT_VIRTUALEDIT
8892 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
8894 int save_State = State;
8896 /* Pretent Insert mode here to allow the cursor on the
8897 * character past the end of the line */
8898 State = INSERT;
8899 coladvance(getviscol());
8900 State = save_State;
8902 #endif
8904 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
8909 * Invoke edit() and take care of "restart_edit" and the return value.
8911 static void
8912 invoke_edit(cap, repl, cmd, startln)
8913 cmdarg_T *cap;
8914 int repl; /* "r" or "gr" command */
8915 int cmd;
8916 int startln;
8918 int restart_edit_save = 0;
8920 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
8921 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
8922 * it. */
8923 if (repl || !stuff_empty())
8924 restart_edit_save = restart_edit;
8925 else
8926 restart_edit_save = 0;
8928 /* Always reset "restart_edit", this is not a restarted edit. */
8929 restart_edit = 0;
8931 if (edit(cmd, startln, cap->count1))
8932 cap->retval |= CA_COMMAND_BUSY;
8934 if (restart_edit == 0)
8935 restart_edit = restart_edit_save;
8938 #ifdef FEAT_TEXTOBJ
8940 * "a" or "i" while an operator is pending or in Visual mode: object motion.
8942 static void
8943 nv_object(cap)
8944 cmdarg_T *cap;
8946 int flag;
8947 int include;
8948 char_u *mps_save;
8950 if (cap->cmdchar == 'i')
8951 include = FALSE; /* "ix" = inner object: exclude white space */
8952 else
8953 include = TRUE; /* "ax" = an object: include white space */
8955 /* Make sure (), [], {} and <> are in 'matchpairs' */
8956 mps_save = curbuf->b_p_mps;
8957 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
8959 switch (cap->nchar)
8961 case 'w': /* "aw" = a word */
8962 flag = current_word(cap->oap, cap->count1, include, FALSE);
8963 break;
8964 case 'W': /* "aW" = a WORD */
8965 flag = current_word(cap->oap, cap->count1, include, TRUE);
8966 break;
8967 case 'b': /* "ab" = a braces block */
8968 case '(':
8969 case ')':
8970 flag = current_block(cap->oap, cap->count1, include, '(', ')');
8971 break;
8972 case 'B': /* "aB" = a Brackets block */
8973 case '{':
8974 case '}':
8975 flag = current_block(cap->oap, cap->count1, include, '{', '}');
8976 break;
8977 case '[': /* "a[" = a [] block */
8978 case ']':
8979 flag = current_block(cap->oap, cap->count1, include, '[', ']');
8980 break;
8981 case '<': /* "a<" = a <> block */
8982 case '>':
8983 flag = current_block(cap->oap, cap->count1, include, '<', '>');
8984 break;
8985 case 't': /* "at" = a tag block (xml and html) */
8986 flag = current_tagblock(cap->oap, cap->count1, include);
8987 break;
8988 case 'p': /* "ap" = a paragraph */
8989 flag = current_par(cap->oap, cap->count1, include, 'p');
8990 break;
8991 case 's': /* "as" = a sentence */
8992 flag = current_sent(cap->oap, cap->count1, include);
8993 break;
8994 case '"': /* "a"" = a double quoted string */
8995 case '\'': /* "a'" = a single quoted string */
8996 case '`': /* "a`" = a backtick quoted string */
8997 flag = current_quote(cap->oap, cap->count1, include,
8998 cap->nchar);
8999 break;
9000 #if 0 /* TODO */
9001 case 'S': /* "aS" = a section */
9002 case 'f': /* "af" = a filename */
9003 case 'u': /* "au" = a URL */
9004 #endif
9005 default:
9006 flag = FAIL;
9007 break;
9010 curbuf->b_p_mps = mps_save;
9011 if (flag == FAIL)
9012 clearopbeep(cap->oap);
9013 adjust_cursor_col();
9014 curwin->w_set_curswant = TRUE;
9016 #endif
9019 * "q" command: Start/stop recording.
9020 * "q:", "q/", "q?": edit command-line in command-line window.
9022 static void
9023 nv_record(cap)
9024 cmdarg_T *cap;
9026 if (cap->oap->op_type == OP_FORMAT)
9028 /* "gqq" is the same as "gqgq": format line */
9029 cap->cmdchar = 'g';
9030 cap->nchar = 'q';
9031 nv_operator(cap);
9033 else if (!checkclearop(cap->oap))
9035 #ifdef FEAT_CMDWIN
9036 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9038 stuffcharReadbuff(cap->nchar);
9039 stuffcharReadbuff(K_CMDWIN);
9041 else
9042 #endif
9043 /* (stop) recording into a named register, unless executing a
9044 * register */
9045 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9046 clearopbeep(cap->oap);
9051 * Handle the "@r" command.
9053 static void
9054 nv_at(cap)
9055 cmdarg_T *cap;
9057 if (checkclearop(cap->oap))
9058 return;
9059 #ifdef FEAT_EVAL
9060 if (cap->nchar == '=')
9062 if (get_expr_register() == NUL)
9063 return;
9065 #endif
9066 while (cap->count1-- && !got_int)
9068 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
9070 clearopbeep(cap->oap);
9071 break;
9073 line_breakcheck();
9078 * Handle the CTRL-U and CTRL-D commands.
9080 static void
9081 nv_halfpage(cap)
9082 cmdarg_T *cap;
9084 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9085 || (cap->cmdchar == Ctrl_D
9086 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9087 clearopbeep(cap->oap);
9088 else if (!checkclearop(cap->oap))
9089 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9093 * Handle "J" or "gJ" command.
9095 static void
9096 nv_join(cap)
9097 cmdarg_T *cap;
9099 #ifdef FEAT_VISUAL
9100 if (VIsual_active) /* join the visual lines */
9101 nv_operator(cap);
9102 else
9103 #endif
9104 if (!checkclearop(cap->oap))
9106 if (cap->count0 <= 1)
9107 cap->count0 = 2; /* default for join is two lines! */
9108 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9109 curbuf->b_ml.ml_line_count)
9110 clearopbeep(cap->oap); /* beyond last line */
9111 else
9113 prep_redo(cap->oap->regname, cap->count0,
9114 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9115 do_do_join(cap->count0, cap->nchar == NUL);
9121 * "P", "gP", "p" and "gp" commands.
9123 static void
9124 nv_put(cap)
9125 cmdarg_T *cap;
9127 #ifdef FEAT_VISUAL
9128 int regname = 0;
9129 void *reg1 = NULL, *reg2 = NULL;
9130 int empty = FALSE;
9131 int was_visual = FALSE;
9132 #endif
9133 int dir;
9134 int flags = 0;
9136 if (cap->oap->op_type != OP_NOP)
9138 #ifdef FEAT_DIFF
9139 /* "dp" is ":diffput" */
9140 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9142 clearop(cap->oap);
9143 nv_diffgetput(TRUE);
9145 else
9146 #endif
9147 clearopbeep(cap->oap);
9149 else
9151 dir = (cap->cmdchar == 'P'
9152 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9153 ? BACKWARD : FORWARD;
9154 prep_redo_cmd(cap);
9155 if (cap->cmdchar == 'g')
9156 flags |= PUT_CURSEND;
9158 #ifdef FEAT_VISUAL
9159 if (VIsual_active)
9161 /* Putting in Visual mode: The put text replaces the selected
9162 * text. First delete the selected text, then put the new text.
9163 * Need to save and restore the registers that the delete
9164 * overwrites if the old contents is being put.
9166 was_visual = TRUE;
9167 regname = cap->oap->regname;
9168 # ifdef FEAT_CLIPBOARD
9169 adjust_clip_reg(&regname);
9170 # endif
9171 if (regname == 0 || VIM_ISDIGIT(regname)
9172 # ifdef FEAT_CLIPBOARD
9173 || (clip_unnamed && (regname == '*' || regname == '+'))
9174 # endif
9178 /* the delete is going to overwrite the register we want to
9179 * put, save it first. */
9180 reg1 = get_register(regname, TRUE);
9183 /* Now delete the selected text. */
9184 cap->cmdchar = 'd';
9185 cap->nchar = NUL;
9186 cap->oap->regname = NUL;
9187 nv_operator(cap);
9188 do_pending_operator(cap, 0, FALSE);
9189 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
9191 /* delete PUT_LINE_BACKWARD; */
9192 cap->oap->regname = regname;
9194 if (reg1 != NULL)
9196 /* Delete probably changed the register we want to put, save
9197 * it first. Then put back what was there before the delete. */
9198 reg2 = get_register(regname, FALSE);
9199 put_register(regname, reg1);
9202 /* When deleted a linewise Visual area, put the register as
9203 * lines to avoid it joined with the next line. When deletion was
9204 * characterwise, split a line when putting lines. */
9205 if (VIsual_mode == 'V')
9206 flags |= PUT_LINE;
9207 else if (VIsual_mode == 'v')
9208 flags |= PUT_LINE_SPLIT;
9209 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9210 flags |= PUT_LINE_FORWARD;
9211 dir = BACKWARD;
9212 if ((VIsual_mode != 'V'
9213 && curwin->w_cursor.col < curbuf->b_op_start.col)
9214 || (VIsual_mode == 'V'
9215 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9216 /* cursor is at the end of the line or end of file, put
9217 * forward. */
9218 dir = FORWARD;
9220 #endif
9221 do_put(cap->oap->regname, dir, cap->count1, flags);
9223 #ifdef FEAT_VISUAL
9224 /* If a register was saved, put it back now. */
9225 if (reg2 != NULL)
9226 put_register(regname, reg2);
9228 /* What to reselect with "gv"? Selecting the just put text seems to
9229 * be the most useful, since the original text was removed. */
9230 if (was_visual)
9232 curbuf->b_visual.vi_start = curbuf->b_op_start;
9233 curbuf->b_visual.vi_end = curbuf->b_op_end;
9236 /* When all lines were selected and deleted do_put() leaves an empty
9237 * line that needs to be deleted now. */
9238 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
9240 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
9242 /* If the cursor was in that line, move it to the end of the last
9243 * line. */
9244 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9246 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9247 coladvance((colnr_T)MAXCOL);
9250 #endif
9251 auto_format(FALSE, TRUE);
9256 * "o" and "O" commands.
9258 static void
9259 nv_open(cap)
9260 cmdarg_T *cap;
9262 #ifdef FEAT_DIFF
9263 /* "do" is ":diffget" */
9264 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9266 clearop(cap->oap);
9267 nv_diffgetput(FALSE);
9269 else
9270 #endif
9271 #ifdef FEAT_VISUAL
9272 if (VIsual_active) /* switch start and end of visual */
9273 v_swap_corners(cap->cmdchar);
9274 else
9275 #endif
9276 n_opencmd(cap);
9279 #ifdef FEAT_SNIFF
9280 /*ARGSUSED*/
9281 static void
9282 nv_sniff(cap)
9283 cmdarg_T *cap;
9285 ProcessSniffRequests();
9287 #endif
9289 #ifdef FEAT_NETBEANS_INTG
9290 static void
9291 nv_nbcmd(cap)
9292 cmdarg_T *cap;
9294 netbeans_keycommand(cap->nchar);
9296 #endif
9298 #ifdef FEAT_DND
9299 /*ARGSUSED*/
9300 static void
9301 nv_drop(cap)
9302 cmdarg_T *cap;
9304 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9306 #endif
9308 #ifdef FEAT_AUTOCMD
9310 * Trigger CursorHold event.
9311 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9312 * input buffer. "did_cursorhold" is set to avoid retriggering.
9314 /*ARGSUSED*/
9315 static void
9316 nv_cursorhold(cap)
9317 cmdarg_T *cap;
9319 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9320 did_cursorhold = TRUE;
9321 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
9323 #endif