Patch 7.0.070
[MacVim/jjgod.git] / src / normal.c
blobd1dc05f145c89b6382971b045471f0c1b7f25a0e
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 static void nv_lineop __ARGS((cmdarg_T *cap));
145 static void nv_home __ARGS((cmdarg_T *cap));
146 static void nv_pipe __ARGS((cmdarg_T *cap));
147 static void nv_bck_word __ARGS((cmdarg_T *cap));
148 static void nv_wordcmd __ARGS((cmdarg_T *cap));
149 static void nv_beginline __ARGS((cmdarg_T *cap));
150 #ifdef FEAT_VISUAL
151 static void adjust_for_sel __ARGS((cmdarg_T *cap));
152 static int unadjust_for_sel __ARGS((void));
153 static void nv_select __ARGS((cmdarg_T *cap));
154 #endif
155 static void nv_goto __ARGS((cmdarg_T *cap));
156 static void nv_normal __ARGS((cmdarg_T *cap));
157 static void nv_esc __ARGS((cmdarg_T *oap));
158 static void nv_edit __ARGS((cmdarg_T *cap));
159 static void invoke_edit __ARGS((cmdarg_T *cap, int repl, int cmd, int startln));
160 #ifdef FEAT_TEXTOBJ
161 static void nv_object __ARGS((cmdarg_T *cap));
162 #endif
163 static void nv_record __ARGS((cmdarg_T *cap));
164 static void nv_at __ARGS((cmdarg_T *cap));
165 static void nv_halfpage __ARGS((cmdarg_T *cap));
166 static void nv_join __ARGS((cmdarg_T *cap));
167 static void nv_put __ARGS((cmdarg_T *cap));
168 static void nv_open __ARGS((cmdarg_T *cap));
169 #ifdef FEAT_SNIFF
170 static void nv_sniff __ARGS((cmdarg_T *cap));
171 #endif
172 #ifdef FEAT_NETBEANS_INTG
173 static void nv_nbcmd __ARGS((cmdarg_T *cap));
174 #endif
175 #ifdef FEAT_DND
176 static void nv_drop __ARGS((cmdarg_T *cap));
177 #endif
178 #ifdef FEAT_AUTOCMD
179 static void nv_cursorhold __ARGS((cmdarg_T *cap));
180 #endif
183 * Function to be called for a Normal or Visual mode command.
184 * The argument is a cmdarg_T.
186 typedef void (*nv_func_T) __ARGS((cmdarg_T *cap));
188 /* Values for cmd_flags. */
189 #define NV_NCH 0x01 /* may need to get a second char */
190 #define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
191 #define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
192 #define NV_LANG 0x08 /* second char needs language adjustment */
194 #define NV_SS 0x10 /* may start selection */
195 #define NV_SSS 0x20 /* may start selection with shift modifier */
196 #define NV_STS 0x40 /* may stop selection without shift modif. */
197 #define NV_RL 0x80 /* 'rightleft' modifies command */
198 #define NV_KEEPREG 0x100 /* don't clear regname */
199 #define NV_NCW 0x200 /* not allowed in command-line window */
202 * Generally speaking, every Normal mode command should either clear any
203 * pending operator (with *clearop*()), or set the motion type variable
204 * oap->motion_type.
206 * When a cursor motion command is made, it is marked as being a character or
207 * line oriented motion. Then, if an operator is in effect, the operation
208 * becomes character or line oriented accordingly.
212 * This table contains one entry for every Normal or Visual mode command.
213 * The order doesn't matter, init_normal_cmds() will create a sorted index.
214 * It is faster when all keys from zero to '~' are present.
216 static const struct nv_cmd
218 int cmd_char; /* (first) command character */
219 nv_func_T cmd_func; /* function for this command */
220 short_u cmd_flags; /* NV_ flags */
221 short cmd_arg; /* value for ca.arg */
222 } nv_cmds[] =
224 {NUL, nv_error, 0, 0},
225 {Ctrl_A, nv_addsub, 0, 0},
226 {Ctrl_B, nv_page, NV_STS, BACKWARD},
227 {Ctrl_C, nv_esc, 0, TRUE},
228 {Ctrl_D, nv_halfpage, 0, 0},
229 {Ctrl_E, nv_scroll_line, 0, TRUE},
230 {Ctrl_F, nv_page, NV_STS, FORWARD},
231 {Ctrl_G, nv_ctrlg, 0, 0},
232 {Ctrl_H, nv_ctrlh, 0, 0},
233 {Ctrl_I, nv_pcmark, 0, 0},
234 {NL, nv_down, 0, FALSE},
235 {Ctrl_K, nv_error, 0, 0},
236 {Ctrl_L, nv_clear, 0, 0},
237 {Ctrl_M, nv_down, 0, TRUE},
238 {Ctrl_N, nv_down, NV_STS, FALSE},
239 {Ctrl_O, nv_ctrlo, 0, 0},
240 {Ctrl_P, nv_up, NV_STS, FALSE},
241 #ifdef FEAT_VISUAL
242 {Ctrl_Q, nv_visual, 0, FALSE},
243 #else
244 {Ctrl_Q, nv_ignore, 0, 0},
245 #endif
246 {Ctrl_R, nv_redo, 0, 0},
247 {Ctrl_S, nv_ignore, 0, 0},
248 {Ctrl_T, nv_tagpop, NV_NCW, 0},
249 {Ctrl_U, nv_halfpage, 0, 0},
250 #ifdef FEAT_VISUAL
251 {Ctrl_V, nv_visual, 0, FALSE},
252 {'V', nv_visual, 0, FALSE},
253 {'v', nv_visual, 0, FALSE},
254 #else
255 {Ctrl_V, nv_error, 0, 0},
256 {'V', nv_error, 0, 0},
257 {'v', nv_error, 0, 0},
258 #endif
259 {Ctrl_W, nv_window, 0, 0},
260 {Ctrl_X, nv_addsub, 0, 0},
261 {Ctrl_Y, nv_scroll_line, 0, FALSE},
262 {Ctrl_Z, nv_suspend, 0, 0},
263 {ESC, nv_esc, 0, FALSE},
264 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
265 {Ctrl_RSB, nv_ident, NV_NCW, 0},
266 {Ctrl_HAT, nv_hat, NV_NCW, 0},
267 {Ctrl__, nv_error, 0, 0},
268 {' ', nv_right, 0, 0},
269 {'!', nv_operator, 0, 0},
270 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
271 {'#', nv_ident, 0, 0},
272 {'$', nv_dollar, 0, 0},
273 {'%', nv_percent, 0, 0},
274 {'&', nv_optrans, 0, 0},
275 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
276 {'(', nv_brace, 0, BACKWARD},
277 {')', nv_brace, 0, FORWARD},
278 {'*', nv_ident, 0, 0},
279 {'+', nv_down, 0, TRUE},
280 {',', nv_csearch, 0, TRUE},
281 {'-', nv_up, 0, TRUE},
282 {'.', nv_dot, NV_KEEPREG, 0},
283 {'/', nv_search, 0, FALSE},
284 {'0', nv_beginline, 0, 0},
285 {'1', nv_ignore, 0, 0},
286 {'2', nv_ignore, 0, 0},
287 {'3', nv_ignore, 0, 0},
288 {'4', nv_ignore, 0, 0},
289 {'5', nv_ignore, 0, 0},
290 {'6', nv_ignore, 0, 0},
291 {'7', nv_ignore, 0, 0},
292 {'8', nv_ignore, 0, 0},
293 {'9', nv_ignore, 0, 0},
294 {':', nv_colon, 0, 0},
295 {';', nv_csearch, 0, FALSE},
296 {'<', nv_operator, NV_RL, 0},
297 {'=', nv_operator, 0, 0},
298 {'>', nv_operator, NV_RL, 0},
299 {'?', nv_search, 0, FALSE},
300 {'@', nv_at, NV_NCH_NOP, FALSE},
301 {'A', nv_edit, 0, 0},
302 {'B', nv_bck_word, 0, 1},
303 {'C', nv_abbrev, NV_KEEPREG, 0},
304 {'D', nv_abbrev, NV_KEEPREG, 0},
305 {'E', nv_wordcmd, 0, TRUE},
306 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
307 {'G', nv_goto, 0, TRUE},
308 {'H', nv_scroll, 0, 0},
309 {'I', nv_edit, 0, 0},
310 {'J', nv_join, 0, 0},
311 {'K', nv_ident, 0, 0},
312 {'L', nv_scroll, 0, 0},
313 {'M', nv_scroll, 0, 0},
314 {'N', nv_next, 0, SEARCH_REV},
315 {'O', nv_open, 0, 0},
316 {'P', nv_put, 0, 0},
317 {'Q', nv_exmode, NV_NCW, 0},
318 {'R', nv_Replace, 0, FALSE},
319 {'S', nv_subst, NV_KEEPREG, 0},
320 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
321 {'U', nv_Undo, 0, 0},
322 {'W', nv_wordcmd, 0, TRUE},
323 {'X', nv_abbrev, NV_KEEPREG, 0},
324 {'Y', nv_abbrev, NV_KEEPREG, 0},
325 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
326 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
327 {'\\', nv_error, 0, 0},
328 {']', nv_brackets, NV_NCH_ALW, FORWARD},
329 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
330 {'_', nv_lineop, 0, 0},
331 {'`', nv_gomark, NV_NCH_ALW, FALSE},
332 {'a', nv_edit, NV_NCH, 0},
333 {'b', nv_bck_word, 0, 0},
334 {'c', nv_operator, 0, 0},
335 {'d', nv_operator, 0, 0},
336 {'e', nv_wordcmd, 0, FALSE},
337 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
338 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
339 {'h', nv_left, NV_RL, 0},
340 {'i', nv_edit, NV_NCH, 0},
341 {'j', nv_down, 0, FALSE},
342 {'k', nv_up, 0, FALSE},
343 {'l', nv_right, NV_RL, 0},
344 {'m', nv_mark, NV_NCH_NOP, 0},
345 {'n', nv_next, 0, 0},
346 {'o', nv_open, 0, 0},
347 {'p', nv_put, 0, 0},
348 {'q', nv_record, NV_NCH, 0},
349 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
350 {'s', nv_subst, NV_KEEPREG, 0},
351 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
352 {'u', nv_undo, 0, 0},
353 {'w', nv_wordcmd, 0, FALSE},
354 {'x', nv_abbrev, NV_KEEPREG, 0},
355 {'y', nv_operator, 0, 0},
356 {'z', nv_zet, NV_NCH_ALW, 0},
357 {'{', nv_findpar, 0, BACKWARD},
358 {'|', nv_pipe, 0, 0},
359 {'}', nv_findpar, 0, FORWARD},
360 {'~', nv_tilde, 0, 0},
362 /* pound sign */
363 {POUND, nv_ident, 0, 0},
364 #ifdef FEAT_MOUSE
365 {K_MOUSEUP, nv_mousescroll, 0, TRUE},
366 {K_MOUSEDOWN, nv_mousescroll, 0, FALSE},
367 {K_LEFTMOUSE, nv_mouse, 0, 0},
368 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
369 {K_LEFTDRAG, nv_mouse, 0, 0},
370 {K_LEFTRELEASE, nv_mouse, 0, 0},
371 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
372 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
373 {K_MIDDLEDRAG, nv_mouse, 0, 0},
374 {K_MIDDLERELEASE, nv_mouse, 0, 0},
375 {K_RIGHTMOUSE, nv_mouse, 0, 0},
376 {K_RIGHTDRAG, nv_mouse, 0, 0},
377 {K_RIGHTRELEASE, nv_mouse, 0, 0},
378 {K_X1MOUSE, nv_mouse, 0, 0},
379 {K_X1DRAG, nv_mouse, 0, 0},
380 {K_X1RELEASE, nv_mouse, 0, 0},
381 {K_X2MOUSE, nv_mouse, 0, 0},
382 {K_X2DRAG, nv_mouse, 0, 0},
383 {K_X2RELEASE, nv_mouse, 0, 0},
384 #endif
385 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
386 {K_NOP, nv_nop, 0, 0},
387 {K_INS, nv_edit, 0, 0},
388 {K_KINS, nv_edit, 0, 0},
389 {K_BS, nv_ctrlh, 0, 0},
390 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
391 {K_S_UP, nv_page, NV_SS, BACKWARD},
392 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
393 {K_S_DOWN, nv_page, NV_SS, FORWARD},
394 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
395 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
396 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
397 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
398 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
399 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
400 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
401 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
402 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
403 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
404 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
405 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
406 {K_S_END, nv_end, NV_SS, FALSE},
407 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
408 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
409 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
410 {K_S_HOME, nv_home, NV_SS, 0},
411 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
412 {K_DEL, nv_abbrev, 0, 0},
413 {K_KDEL, nv_abbrev, 0, 0},
414 {K_UNDO, nv_kundo, 0, 0},
415 {K_HELP, nv_help, NV_NCW, 0},
416 {K_F1, nv_help, NV_NCW, 0},
417 {K_XF1, nv_help, NV_NCW, 0},
418 #ifdef FEAT_VISUAL
419 {K_SELECT, nv_select, 0, 0},
420 #endif
421 #ifdef FEAT_GUI
422 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
423 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
424 #endif
425 #ifdef FEAT_GUI_TABLINE
426 {K_TABLINE, nv_tabline, 0, 0},
427 {K_TABMENU, nv_tabmenu, 0, 0},
428 #endif
429 #ifdef FEAT_FKMAP
430 {K_F8, farsi_fkey, 0, 0},
431 {K_F9, farsi_fkey, 0, 0},
432 #endif
433 #ifdef FEAT_SNIFF
434 {K_SNIFF, nv_sniff, 0, 0},
435 #endif
436 #ifdef FEAT_NETBEANS_INTG
437 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
438 #endif
439 #ifdef FEAT_DND
440 {K_DROP, nv_drop, NV_STS, 0},
441 #endif
442 #ifdef FEAT_AUTOCMD
443 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
444 #endif
447 /* Number of commands in nv_cmds[]. */
448 #define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
450 /* Sorted index of commands in nv_cmds[]. */
451 static short nv_cmd_idx[NV_CMDS_SIZE];
453 /* The highest index for which
454 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
455 static int nv_max_linear;
458 * Compare functions for qsort() below, that checks the command character
459 * through the index in nv_cmd_idx[].
461 static int
462 #ifdef __BORLANDC__
463 _RTLENTRYF
464 #endif
465 nv_compare(s1, s2)
466 const void *s1;
467 const void *s2;
469 int c1, c2;
471 /* The commands are sorted on absolute value. */
472 c1 = nv_cmds[*(const short *)s1].cmd_char;
473 c2 = nv_cmds[*(const short *)s2].cmd_char;
474 if (c1 < 0)
475 c1 = -c1;
476 if (c2 < 0)
477 c2 = -c2;
478 return c1 - c2;
482 * Initialize the nv_cmd_idx[] table.
484 void
485 init_normal_cmds()
487 int i;
489 /* Fill the index table with a one to one relation. */
490 for (i = 0; i < NV_CMDS_SIZE; ++i)
491 nv_cmd_idx[i] = i;
493 /* Sort the commands by the command character. */
494 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
496 /* Find the first entry that can't be indexed by the command character. */
497 for (i = 0; i < NV_CMDS_SIZE; ++i)
498 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
499 break;
500 nv_max_linear = i - 1;
504 * Search for a command in the commands table.
505 * Returns -1 for invalid command.
507 static int
508 find_command(cmdchar)
509 int cmdchar;
511 int i;
512 int idx;
513 int top, bot;
514 int c;
516 #ifdef FEAT_MBYTE
517 /* A multi-byte character is never a command. */
518 if (cmdchar >= 0x100)
519 return -1;
520 #endif
522 /* We use the absolute value of the character. Special keys have a
523 * negative value, but are sorted on their absolute value. */
524 if (cmdchar < 0)
525 cmdchar = -cmdchar;
527 /* If the character is in the first part: The character is the index into
528 * nv_cmd_idx[]. */
529 if (cmdchar <= nv_max_linear)
530 return nv_cmd_idx[cmdchar];
532 /* Perform a binary search. */
533 bot = nv_max_linear + 1;
534 top = NV_CMDS_SIZE - 1;
535 idx = -1;
536 while (bot <= top)
538 i = (top + bot) / 2;
539 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
540 if (c < 0)
541 c = -c;
542 if (cmdchar == c)
544 idx = nv_cmd_idx[i];
545 break;
547 if (cmdchar > c)
548 bot = i + 1;
549 else
550 top = i - 1;
552 return idx;
556 * Execute a command in Normal mode.
558 /*ARGSUSED*/
559 void
560 normal_cmd(oap, toplevel)
561 oparg_T *oap;
562 int toplevel; /* TRUE when called from main() */
564 static long opcount = 0; /* ca.opcount saved here */
565 cmdarg_T ca; /* command arguments */
566 int c;
567 int ctrl_w = FALSE; /* got CTRL-W command */
568 int old_col = curwin->w_curswant;
569 #ifdef FEAT_CMDL_INFO
570 int need_flushbuf; /* need to call out_flush() */
571 #endif
572 #ifdef FEAT_VISUAL
573 pos_T old_pos; /* cursor position before command */
574 int mapped_len;
575 static int old_mapped_len = 0;
576 #endif
577 int idx;
579 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
580 ca.oap = oap;
581 ca.opcount = opcount;
583 #ifdef FEAT_SNIFF
584 want_sniff_request = sniff_connected;
585 #endif
588 * If there is an operator pending, then the command we take this time
589 * will terminate it. Finish_op tells us to finish the operation before
590 * returning this time (unless the operation was cancelled).
592 #ifdef CURSOR_SHAPE
593 c = finish_op;
594 #endif
595 finish_op = (oap->op_type != OP_NOP);
596 #ifdef CURSOR_SHAPE
597 if (finish_op != c)
599 ui_cursor_shape(); /* may show different cursor shape */
600 # ifdef FEAT_MOUSESHAPE
601 update_mouseshape(-1);
602 # endif
604 #endif
606 if (!finish_op && !oap->regname)
607 ca.opcount = 0;
609 #ifdef FEAT_VISUAL
610 mapped_len = typebuf_maplen();
611 #endif
613 State = NORMAL_BUSY;
614 #ifdef USE_ON_FLY_SCROLL
615 dont_scroll = FALSE; /* allow scrolling here */
616 #endif
619 * Get the command character from the user.
621 c = safe_vgetc();
623 #ifdef FEAT_LANGMAP
624 LANGMAP_ADJUST(c, TRUE);
625 #endif
627 #ifdef FEAT_VISUAL
629 * If a mapping was started in Visual or Select mode, remember the length
630 * of the mapping. This is used below to not return to Insert mode for as
631 * long as the mapping is being executed.
633 if (restart_edit == 0)
634 old_mapped_len = 0;
635 else if (old_mapped_len
636 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
637 old_mapped_len = typebuf_maplen();
638 #endif
640 if (c == NUL)
641 c = K_ZERO;
643 #ifdef FEAT_VISUAL
645 * In Select mode, typed text replaces the selection.
647 if (VIsual_active
648 && VIsual_select
649 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
651 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
652 * 'insertmode' is set) fake a "d"elete command, Insert mode will
653 * restart automatically.
654 * Insert the typed character in the typeahead buffer, so that it will
655 * be mapped in Insert mode. Required for ":lmap" to work. May cause
656 * mapping a character from ":vnoremap"... */
657 ins_char_typebuf(c);
658 if (restart_edit != 0)
659 c = 'd';
660 else
661 c = 'c';
662 msg_nowait = TRUE; /* don't delay going to insert mode */
664 #endif
666 #ifdef FEAT_CMDL_INFO
667 need_flushbuf = add_to_showcmd(c);
668 #endif
670 getcount:
671 #ifdef FEAT_VISUAL
672 if (!(VIsual_active && VIsual_select))
673 #endif
676 * Handle a count before a command and compute ca.count0.
677 * Note that '0' is a command and not the start of a count, but it's
678 * part of a count after other digits.
680 while ( (c >= '1' && c <= '9')
681 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
683 if (c == K_DEL || c == K_KDEL)
685 ca.count0 /= 10;
686 #ifdef FEAT_CMDL_INFO
687 del_from_showcmd(4); /* delete the digit and ~@% */
688 #endif
690 else
691 ca.count0 = ca.count0 * 10 + (c - '0');
692 if (ca.count0 < 0) /* got too large! */
693 ca.count0 = 999999999L;
694 if (ctrl_w)
696 ++no_mapping;
697 ++allow_keys; /* no mapping for nchar, but keys */
699 ++no_zero_mapping; /* don't map zero here */
700 c = safe_vgetc();
701 #ifdef FEAT_LANGMAP
702 LANGMAP_ADJUST(c, TRUE);
703 #endif
704 --no_zero_mapping;
705 if (ctrl_w)
707 --no_mapping;
708 --allow_keys;
710 #ifdef FEAT_CMDL_INFO
711 need_flushbuf |= add_to_showcmd(c);
712 #endif
716 * If we got CTRL-W there may be a/another count
718 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
720 ctrl_w = TRUE;
721 ca.opcount = ca.count0; /* remember first count */
722 ca.count0 = 0;
723 ++no_mapping;
724 ++allow_keys; /* no mapping for nchar, but keys */
725 c = safe_vgetc(); /* get next character */
726 #ifdef FEAT_LANGMAP
727 LANGMAP_ADJUST(c, TRUE);
728 #endif
729 --no_mapping;
730 --allow_keys;
731 #ifdef FEAT_CMDL_INFO
732 need_flushbuf |= add_to_showcmd(c);
733 #endif
734 goto getcount; /* jump back */
739 * If we're in the middle of an operator (including after entering a yank
740 * buffer with '"') AND we had a count before the operator, then that
741 * count overrides the current value of ca.count0.
742 * What this means effectively, is that commands like "3dw" get turned
743 * into "d3w" which makes things fall into place pretty neatly.
744 * If you give a count before AND after the operator, they are multiplied.
746 if (ca.opcount != 0)
748 if (ca.count0)
749 ca.count0 *= ca.opcount;
750 else
751 ca.count0 = ca.opcount;
755 * Always remember the count. It will be set to zero (on the next call,
756 * above) when there is no pending operator.
757 * When called from main(), save the count for use by the "count" built-in
758 * variable.
760 ca.opcount = ca.count0;
761 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
763 #ifdef FEAT_EVAL
765 * Only set v:count when called from main() and not a stuffed command.
767 if (toplevel && stuff_empty())
768 set_vcount(ca.count0, ca.count1);
769 #endif
772 * Find the command character in the table of commands.
773 * For CTRL-W we already got nchar when looking for a count.
775 if (ctrl_w)
777 ca.nchar = c;
778 ca.cmdchar = Ctrl_W;
780 else
781 ca.cmdchar = c;
782 idx = find_command(ca.cmdchar);
783 if (idx < 0)
785 /* Not a known command: beep. */
786 clearopbeep(oap);
787 goto normal_end;
790 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
792 /* This command is not allowed wile editing a ccmdline: beep. */
793 clearopbeep(oap);
794 text_locked_msg();
795 goto normal_end;
797 #ifdef FEAT_AUTOCMD
798 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
799 goto normal_end;
800 #endif
802 #ifdef FEAT_VISUAL
804 * In Visual/Select mode, a few keys are handled in a special way.
806 if (VIsual_active)
808 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
809 if (km_stopsel
810 && (nv_cmds[idx].cmd_flags & NV_STS)
811 && !(mod_mask & MOD_MASK_SHIFT))
813 end_visual_mode();
814 redraw_curbuf_later(INVERTED);
817 /* Keys that work different when 'keymodel' contains "startsel" */
818 if (km_startsel)
820 if (nv_cmds[idx].cmd_flags & NV_SS)
822 unshift_special(&ca);
823 idx = find_command(ca.cmdchar);
824 if (idx < 0)
826 /* Just in case */
827 clearopbeep(oap);
828 goto normal_end;
831 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
832 && (mod_mask & MOD_MASK_SHIFT))
834 mod_mask &= ~MOD_MASK_SHIFT;
838 #endif
840 #ifdef FEAT_RIGHTLEFT
841 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
842 && (nv_cmds[idx].cmd_flags & NV_RL))
844 /* Invert horizontal movements and operations. Only when typed by the
845 * user directly, not when the result of a mapping or "x" translated
846 * to "dl". */
847 switch (ca.cmdchar)
849 case 'l': ca.cmdchar = 'h'; break;
850 case K_RIGHT: ca.cmdchar = K_LEFT; break;
851 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
852 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
853 case 'h': ca.cmdchar = 'l'; break;
854 case K_LEFT: ca.cmdchar = K_RIGHT; break;
855 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
856 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
857 case '>': ca.cmdchar = '<'; break;
858 case '<': ca.cmdchar = '>'; break;
860 idx = find_command(ca.cmdchar);
862 #endif
865 * Get an additional character if we need one.
867 if ((nv_cmds[idx].cmd_flags & NV_NCH)
868 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
869 && oap->op_type == OP_NOP)
870 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
871 || (ca.cmdchar == 'q'
872 && oap->op_type == OP_NOP
873 && !Recording
874 && !Exec_reg)
875 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
876 && (oap->op_type != OP_NOP
877 #ifdef FEAT_VISUAL
878 || VIsual_active
879 #endif
880 ))))
882 int *cp;
883 int repl = FALSE; /* get character for replace mode */
884 int lit = FALSE; /* get extra character literally */
885 int langmap_active = FALSE; /* using :lmap mappings */
886 int lang; /* getting a text character */
887 #ifdef USE_IM_CONTROL
888 int save_smd; /* saved value of p_smd */
889 #endif
891 ++no_mapping;
892 ++allow_keys; /* no mapping for nchar, but allow key codes */
893 if (ca.cmdchar == 'g')
896 * For 'g' get the next character now, so that we can check for
897 * "gr", "g'" and "g`".
899 ca.nchar = safe_vgetc();
900 #ifdef FEAT_LANGMAP
901 LANGMAP_ADJUST(ca.nchar, TRUE);
902 #endif
903 #ifdef FEAT_CMDL_INFO
904 need_flushbuf |= add_to_showcmd(ca.nchar);
905 #endif
906 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
907 || ca.nchar == Ctrl_BSL)
909 cp = &ca.extra_char; /* need to get a third character */
910 if (ca.nchar != 'r')
911 lit = TRUE; /* get it literally */
912 else
913 repl = TRUE; /* get it in replace mode */
915 else
916 cp = NULL; /* no third character needed */
918 else
920 if (ca.cmdchar == 'r') /* get it in replace mode */
921 repl = TRUE;
922 cp = &ca.nchar;
924 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
927 * Get a second or third character.
929 if (cp != NULL)
931 #ifdef CURSOR_SHAPE
932 if (repl)
934 State = REPLACE; /* pretend Replace mode */
935 ui_cursor_shape(); /* show different cursor shape */
937 #endif
938 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
940 /* Allow mappings defined with ":lmap". */
941 --no_mapping;
942 --allow_keys;
943 if (repl)
944 State = LREPLACE;
945 else
946 State = LANGMAP;
947 langmap_active = TRUE;
949 #ifdef USE_IM_CONTROL
950 save_smd = p_smd;
951 p_smd = FALSE; /* Don't let the IM code show the mode here */
952 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
953 im_set_active(TRUE);
954 #endif
956 *cp = safe_vgetc();
958 if (langmap_active)
960 /* Undo the decrement done above */
961 ++no_mapping;
962 ++allow_keys;
963 State = NORMAL_BUSY;
965 #ifdef USE_IM_CONTROL
966 if (lang)
968 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
969 im_save_status(&curbuf->b_p_iminsert);
970 im_set_active(FALSE);
972 p_smd = save_smd;
973 #endif
974 #ifdef CURSOR_SHAPE
975 State = NORMAL_BUSY;
976 #endif
977 #ifdef FEAT_CMDL_INFO
978 need_flushbuf |= add_to_showcmd(*cp);
979 #endif
981 if (!lit)
983 #ifdef FEAT_DIGRAPHS
984 /* Typing CTRL-K gets a digraph. */
985 if (*cp == Ctrl_K
986 && ((nv_cmds[idx].cmd_flags & NV_LANG)
987 || cp == &ca.extra_char)
988 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
990 c = get_digraph(FALSE);
991 if (c > 0)
993 *cp = c;
994 # ifdef FEAT_CMDL_INFO
995 /* Guessing how to update showcmd here... */
996 del_from_showcmd(3);
997 need_flushbuf |= add_to_showcmd(*cp);
998 # endif
1001 #endif
1003 #ifdef FEAT_LANGMAP
1004 /* adjust chars > 127, except after "tTfFr" commands */
1005 LANGMAP_ADJUST(*cp, !lang);
1006 #endif
1007 #ifdef FEAT_RIGHTLEFT
1008 /* adjust Hebrew mapped char */
1009 if (p_hkmap && lang && KeyTyped)
1010 *cp = hkmap(*cp);
1011 # ifdef FEAT_FKMAP
1012 /* adjust Farsi mapped char */
1013 if (p_fkmap && lang && KeyTyped)
1014 *cp = fkmap(*cp);
1015 # endif
1016 #endif
1020 * When the next character is CTRL-\ a following CTRL-N means the
1021 * command is aborted and we go to Normal mode.
1023 if (cp == &ca.extra_char
1024 && ca.nchar == Ctrl_BSL
1025 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1027 ca.cmdchar = Ctrl_BSL;
1028 ca.nchar = ca.extra_char;
1029 idx = find_command(ca.cmdchar);
1031 else if (*cp == Ctrl_BSL)
1033 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1035 /* There is a busy wait here when typing "f<C-\>" and then
1036 * something different from CTRL-N. Can't be avoided. */
1037 while ((c = vpeekc()) <= 0 && towait > 0L)
1039 do_sleep(towait > 50L ? 50L : towait);
1040 towait -= 50L;
1042 if (c > 0)
1044 c = safe_vgetc();
1045 if (c != Ctrl_N && c != Ctrl_G)
1046 vungetc(c);
1047 else
1049 ca.cmdchar = Ctrl_BSL;
1050 ca.nchar = c;
1051 idx = find_command(ca.cmdchar);
1056 #ifdef FEAT_MBYTE
1057 /* When getting a text character and the next character is a
1058 * multi-byte character, it could be a composing character.
1059 * However, don't wait for it to arrive. */
1060 while (enc_utf8 && lang && (c = vpeekc()) > 0
1061 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1063 c = safe_vgetc();
1064 if (!utf_iscomposing(c))
1066 vungetc(c); /* it wasn't, put it back */
1067 break;
1069 else if (ca.ncharC1 == 0)
1070 ca.ncharC1 = c;
1071 else
1072 ca.ncharC2 = c;
1074 #endif
1076 --no_mapping;
1077 --allow_keys;
1080 #ifdef FEAT_CMDL_INFO
1082 * Flush the showcmd characters onto the screen so we can see them while
1083 * the command is being executed. Only do this when the shown command was
1084 * actually displayed, otherwise this will slow down a lot when executing
1085 * mappings.
1087 if (need_flushbuf)
1088 out_flush();
1089 #endif
1090 #ifdef FEAT_AUTOCMD
1091 did_cursorhold = FALSE;
1092 #endif
1094 State = NORMAL;
1096 if (ca.nchar == ESC)
1098 clearop(oap);
1099 if (restart_edit == 0 && goto_im())
1100 restart_edit = 'a';
1101 goto normal_end;
1104 if (ca.cmdchar != K_IGNORE)
1106 msg_didout = FALSE; /* don't scroll screen up for normal command */
1107 msg_col = 0;
1110 #ifdef FEAT_VISUAL
1111 old_pos = curwin->w_cursor; /* remember where cursor was */
1113 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1114 * mode. */
1115 if (!VIsual_active && km_startsel)
1117 if (nv_cmds[idx].cmd_flags & NV_SS)
1119 start_selection();
1120 unshift_special(&ca);
1121 idx = find_command(ca.cmdchar);
1123 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1124 && (mod_mask & MOD_MASK_SHIFT))
1126 start_selection();
1127 mod_mask &= ~MOD_MASK_SHIFT;
1130 #endif
1133 * Execute the command!
1134 * Call the command function found in the commands table.
1136 ca.arg = nv_cmds[idx].cmd_arg;
1137 (nv_cmds[idx].cmd_func)(&ca);
1140 * If we didn't start or finish an operator, reset oap->regname, unless we
1141 * need it later.
1143 if (!finish_op
1144 && !oap->op_type
1145 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1147 clearop(oap);
1148 #ifdef FEAT_EVAL
1149 set_reg_var('"');
1150 #endif
1153 #ifdef FEAT_VISUAL
1154 /* Get the length of mapped chars again after typing a count, second
1155 * character or "z333<cr>". */
1156 if (old_mapped_len > 0)
1157 old_mapped_len = typebuf_maplen();
1158 #endif
1161 * If an operation is pending, handle it...
1163 do_pending_operator(&ca, old_col, FALSE);
1166 * Wait for a moment when a message is displayed that will be overwritten
1167 * by the mode message.
1168 * In Visual mode and with "^O" in Insert mode, a short message will be
1169 * overwritten by the mode message. Wait a bit, until a key is hit.
1170 * In Visual mode, it's more important to keep the Visual area updated
1171 * than keeping a message (e.g. from a /pat search).
1172 * Only do this if the command was typed, not from a mapping.
1173 * Don't wait when emsg_silent is non-zero.
1174 * Also wait a bit after an error message, e.g. for "^O:".
1175 * Don't redraw the screen, it would remove the message.
1177 if ( ((p_smd
1178 && msg_silent == 0
1179 && (restart_edit != 0
1180 #ifdef FEAT_VISUAL
1181 || (VIsual_active
1182 && old_pos.lnum == curwin->w_cursor.lnum
1183 && old_pos.col == curwin->w_cursor.col)
1184 #endif
1186 && (clear_cmdline
1187 || redraw_cmdline)
1188 && (msg_didout || (msg_didany && msg_scroll))
1189 && !msg_nowait
1190 && KeyTyped)
1191 || (restart_edit != 0
1192 #ifdef FEAT_VISUAL
1193 && !VIsual_active
1194 #endif
1195 && (msg_scroll
1196 || emsg_on_display)))
1197 && oap->regname == 0
1198 && !(ca.retval & CA_COMMAND_BUSY)
1199 && stuff_empty()
1200 && typebuf_typed()
1201 && emsg_silent == 0
1202 && !did_wait_return
1203 && oap->op_type == OP_NOP)
1205 int save_State = State;
1207 /* Draw the cursor with the right shape here */
1208 if (restart_edit != 0)
1209 State = INSERT;
1211 /* If need to redraw, and there is a "keep_msg", redraw before the
1212 * delay */
1213 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1215 char_u *kmsg;
1217 kmsg = keep_msg;
1218 keep_msg = NULL;
1219 /* showmode() will clear keep_msg, but we want to use it anyway */
1220 update_screen(0);
1221 /* now reset it, otherwise it's put in the history again */
1222 keep_msg = kmsg;
1223 msg_attr(kmsg, keep_msg_attr);
1224 vim_free(kmsg);
1226 setcursor();
1227 cursor_on();
1228 out_flush();
1229 if (msg_scroll || emsg_on_display)
1230 ui_delay(1000L, TRUE); /* wait at least one second */
1231 ui_delay(3000L, FALSE); /* wait up to three seconds */
1232 State = save_State;
1234 msg_scroll = FALSE;
1235 emsg_on_display = FALSE;
1239 * Finish up after executing a Normal mode command.
1241 normal_end:
1243 msg_nowait = FALSE;
1245 /* Reset finish_op, in case it was set */
1246 #ifdef CURSOR_SHAPE
1247 c = finish_op;
1248 #endif
1249 finish_op = FALSE;
1250 #ifdef CURSOR_SHAPE
1251 /* Redraw the cursor with another shape, if we were in Operator-pending
1252 * mode or did a replace command. */
1253 if (c || ca.cmdchar == 'r')
1255 ui_cursor_shape(); /* may show different cursor shape */
1256 # ifdef FEAT_MOUSESHAPE
1257 update_mouseshape(-1);
1258 # endif
1260 #endif
1262 #ifdef FEAT_CMDL_INFO
1263 if (oap->op_type == OP_NOP && oap->regname == 0)
1264 clear_showcmd();
1265 #endif
1267 checkpcmark(); /* check if we moved since setting pcmark */
1268 vim_free(ca.searchbuf);
1270 #ifdef FEAT_MBYTE
1271 if (has_mbyte)
1272 mb_adjust_cursor();
1273 #endif
1275 #ifdef FEAT_SCROLLBIND
1276 if (curwin->w_p_scb && toplevel)
1278 validate_cursor(); /* may need to update w_leftcol */
1279 do_check_scrollbind(TRUE);
1281 #endif
1284 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1285 * if still inside a mapping that started in Visual mode).
1286 * May switch from Visual to Select mode after CTRL-O command.
1288 if ( oap->op_type == OP_NOP
1289 #ifdef FEAT_VISUAL
1290 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1291 || restart_VIsual_select == 1)
1292 #else
1293 && restart_edit != 0
1294 #endif
1295 && !(ca.retval & CA_COMMAND_BUSY)
1296 && stuff_empty()
1297 && oap->regname == 0)
1299 #ifdef FEAT_VISUAL
1300 if (restart_VIsual_select == 1)
1302 VIsual_select = TRUE;
1303 showmode();
1304 restart_VIsual_select = 0;
1306 #endif
1307 if (restart_edit != 0
1308 #ifdef FEAT_VISUAL
1309 && !VIsual_active && old_mapped_len == 0
1310 #endif
1312 (void)edit(restart_edit, FALSE, 1L);
1315 #ifdef FEAT_VISUAL
1316 if (restart_VIsual_select == 2)
1317 restart_VIsual_select = 1;
1318 #endif
1320 /* Save count before an operator for next time. */
1321 opcount = ca.opcount;
1325 * Handle an operator after visual mode or when the movement is finished
1327 void
1328 do_pending_operator(cap, old_col, gui_yank)
1329 cmdarg_T *cap;
1330 int old_col;
1331 int gui_yank;
1333 oparg_T *oap = cap->oap;
1334 pos_T old_cursor;
1335 int empty_region_error;
1336 int restart_edit_save;
1338 #ifdef FEAT_VISUAL
1339 /* The visual area is remembered for redo */
1340 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1341 static linenr_T redo_VIsual_line_count; /* number of lines */
1342 static colnr_T redo_VIsual_col; /* number of cols or end column */
1343 static long redo_VIsual_count; /* count for Visual operator */
1344 # ifdef FEAT_VIRTUALEDIT
1345 int include_line_break = FALSE;
1346 # endif
1347 #endif
1349 #if defined(FEAT_CLIPBOARD)
1351 * Yank the visual area into the GUI selection register before we operate
1352 * on it and lose it forever.
1353 * Don't do it if a specific register was specified, so that ""x"*P works.
1354 * This could call do_pending_operator() recursively, but that's OK
1355 * because gui_yank will be TRUE for the nested call.
1357 if (clip_star.available
1358 && oap->op_type != OP_NOP
1359 && !gui_yank
1360 # ifdef FEAT_VISUAL
1361 && VIsual_active
1362 && !redo_VIsual_busy
1363 # endif
1364 && oap->regname == 0)
1365 clip_auto_select();
1366 #endif
1367 old_cursor = curwin->w_cursor;
1370 * If an operation is pending, handle it...
1372 if ((finish_op
1373 #ifdef FEAT_VISUAL
1374 || VIsual_active
1375 #endif
1376 ) && oap->op_type != OP_NOP)
1378 #ifdef FEAT_VISUAL
1379 oap->is_VIsual = VIsual_active;
1380 if (oap->motion_force == 'V')
1381 oap->motion_type = MLINE;
1382 else if (oap->motion_force == 'v')
1384 /* If the motion was linewise, "inclusive" will not have been set.
1385 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1386 if (oap->motion_type == MLINE)
1387 oap->inclusive = FALSE;
1388 /* If the motion already was characterwise, toggle "inclusive" */
1389 else if (oap->motion_type == MCHAR)
1390 oap->inclusive = !oap->inclusive;
1391 oap->motion_type = MCHAR;
1393 else if (oap->motion_force == Ctrl_V)
1395 /* Change line- or characterwise motion into Visual block mode. */
1396 VIsual_active = TRUE;
1397 VIsual = oap->start;
1398 VIsual_mode = Ctrl_V;
1399 VIsual_select = FALSE;
1400 VIsual_reselect = FALSE;
1402 #endif
1404 /* only redo yank when 'y' flag is in 'cpoptions' */
1405 /* never redo "zf" (define fold) */
1406 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1407 #ifdef FEAT_VISUAL
1408 && (!VIsual_active || oap->motion_force)
1409 #endif
1410 && cap->cmdchar != 'D'
1411 #ifdef FEAT_FOLDING
1412 && oap->op_type != OP_FOLD
1413 && oap->op_type != OP_FOLDOPEN
1414 && oap->op_type != OP_FOLDOPENREC
1415 && oap->op_type != OP_FOLDCLOSE
1416 && oap->op_type != OP_FOLDCLOSEREC
1417 && oap->op_type != OP_FOLDDEL
1418 && oap->op_type != OP_FOLDDELREC
1419 #endif
1422 prep_redo(oap->regname, cap->count0,
1423 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1424 oap->motion_force, cap->cmdchar, cap->nchar);
1425 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1428 * If 'cpoptions' does not contain 'r', insert the search
1429 * pattern to really repeat the same command.
1431 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
1432 AppendToRedobuffLit(cap->searchbuf, -1);
1433 AppendToRedobuff(NL_STR);
1435 else if (cap->cmdchar == ':')
1437 /* do_cmdline() has stored the first typed line in
1438 * "repeat_cmdline". When several lines are typed repeating
1439 * won't be possible. */
1440 if (repeat_cmdline == NULL)
1441 ResetRedobuff();
1442 else
1444 AppendToRedobuffLit(repeat_cmdline, -1);
1445 AppendToRedobuff(NL_STR);
1446 vim_free(repeat_cmdline);
1447 repeat_cmdline = NULL;
1452 #ifdef FEAT_VISUAL
1453 if (redo_VIsual_busy)
1455 oap->start = curwin->w_cursor;
1456 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1457 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1458 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1459 VIsual_mode = redo_VIsual_mode;
1460 if (VIsual_mode == 'v')
1462 if (redo_VIsual_line_count <= 1)
1463 curwin->w_cursor.col += redo_VIsual_col - 1;
1464 else
1465 curwin->w_cursor.col = redo_VIsual_col;
1467 if (redo_VIsual_col == MAXCOL)
1469 curwin->w_curswant = MAXCOL;
1470 coladvance((colnr_T)MAXCOL);
1472 cap->count0 = redo_VIsual_count;
1473 if (redo_VIsual_count != 0)
1474 cap->count1 = redo_VIsual_count;
1475 else
1476 cap->count1 = 1;
1478 else if (VIsual_active)
1480 /* Save the current VIsual area for '< and '> marks, and "gv" */
1481 curbuf->b_visual.vi_start = VIsual;
1482 curbuf->b_visual.vi_end = curwin->w_cursor;
1483 curbuf->b_visual.vi_mode = VIsual_mode;
1484 curbuf->b_visual.vi_curswant = curwin->w_curswant;
1485 # ifdef FEAT_EVAL
1486 curbuf->b_visual_mode_eval = VIsual_mode;
1487 # endif
1489 /* In Select mode, a linewise selection is operated upon like a
1490 * characterwise selection. */
1491 if (VIsual_select && VIsual_mode == 'V')
1493 if (lt(VIsual, curwin->w_cursor))
1495 VIsual.col = 0;
1496 curwin->w_cursor.col =
1497 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1499 else
1501 curwin->w_cursor.col = 0;
1502 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1504 VIsual_mode = 'v';
1506 /* If 'selection' is "exclusive", backup one character for
1507 * charwise selections. */
1508 else if (VIsual_mode == 'v')
1510 # ifdef FEAT_VIRTUALEDIT
1511 include_line_break =
1512 # endif
1513 unadjust_for_sel();
1516 oap->start = VIsual;
1517 if (VIsual_mode == 'V')
1518 oap->start.col = 0;
1520 #endif /* FEAT_VISUAL */
1523 * Set oap->start to the first position of the operated text, oap->end
1524 * to the end of the operated text. w_cursor is equal to oap->start.
1526 if (lt(oap->start, curwin->w_cursor))
1528 #ifdef FEAT_FOLDING
1529 /* Include folded lines completely. */
1530 if (!VIsual_active)
1532 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1533 oap->start.col = 0;
1534 if (hasFolding(curwin->w_cursor.lnum, NULL,
1535 &curwin->w_cursor.lnum))
1536 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1538 #endif
1539 oap->end = curwin->w_cursor;
1540 curwin->w_cursor = oap->start;
1542 /* w_virtcol may have been updated; if the cursor goes back to its
1543 * previous position w_virtcol becomes invalid and isn't updated
1544 * automatically. */
1545 curwin->w_valid &= ~VALID_VIRTCOL;
1547 else
1549 #ifdef FEAT_FOLDING
1550 /* Include folded lines completely. */
1551 if (!VIsual_active && oap->motion_type == MLINE)
1553 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1554 NULL))
1555 curwin->w_cursor.col = 0;
1556 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1557 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1559 #endif
1560 oap->end = oap->start;
1561 oap->start = curwin->w_cursor;
1564 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1566 #ifdef FEAT_VIRTUALEDIT
1567 /* Set "virtual_op" before resetting VIsual_active. */
1568 virtual_op = virtual_active();
1569 #endif
1571 #ifdef FEAT_VISUAL
1572 if (VIsual_active || redo_VIsual_busy)
1574 if (VIsual_mode == Ctrl_V) /* block mode */
1576 colnr_T start, end;
1578 oap->block_mode = TRUE;
1580 getvvcol(curwin, &(oap->start),
1581 &oap->start_vcol, NULL, &oap->end_vcol);
1582 if (!redo_VIsual_busy)
1584 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1586 if (start < oap->start_vcol)
1587 oap->start_vcol = start;
1588 if (end > oap->end_vcol)
1590 if (*p_sel == 'e' && start >= 1
1591 && start - 1 >= oap->end_vcol)
1592 oap->end_vcol = start - 1;
1593 else
1594 oap->end_vcol = end;
1598 /* if '$' was used, get oap->end_vcol from longest line */
1599 if (curwin->w_curswant == MAXCOL)
1601 curwin->w_cursor.col = MAXCOL;
1602 oap->end_vcol = 0;
1603 for (curwin->w_cursor.lnum = oap->start.lnum;
1604 curwin->w_cursor.lnum <= oap->end.lnum;
1605 ++curwin->w_cursor.lnum)
1607 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1608 if (end > oap->end_vcol)
1609 oap->end_vcol = end;
1612 else if (redo_VIsual_busy)
1613 oap->end_vcol = oap->start_vcol + redo_VIsual_col - 1;
1615 * Correct oap->end.col and oap->start.col to be the
1616 * upper-left and lower-right corner of the block area.
1618 * (Actually, this does convert column positions into character
1619 * positions)
1621 curwin->w_cursor.lnum = oap->end.lnum;
1622 coladvance(oap->end_vcol);
1623 oap->end = curwin->w_cursor;
1625 curwin->w_cursor = oap->start;
1626 coladvance(oap->start_vcol);
1627 oap->start = curwin->w_cursor;
1630 if (!redo_VIsual_busy && !gui_yank)
1633 * Prepare to reselect and redo Visual: this is based on the
1634 * size of the Visual text
1636 resel_VIsual_mode = VIsual_mode;
1637 if (curwin->w_curswant == MAXCOL)
1638 resel_VIsual_col = MAXCOL;
1639 else if (VIsual_mode == Ctrl_V)
1640 resel_VIsual_col = oap->end_vcol - oap->start_vcol + 1;
1641 else if (oap->line_count > 1)
1642 resel_VIsual_col = oap->end.col;
1643 else
1644 resel_VIsual_col = oap->end.col - oap->start.col + 1;
1645 resel_VIsual_line_count = oap->line_count;
1648 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1649 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1650 && oap->op_type != OP_COLON
1651 #ifdef FEAT_FOLDING
1652 && oap->op_type != OP_FOLD
1653 && oap->op_type != OP_FOLDOPEN
1654 && oap->op_type != OP_FOLDOPENREC
1655 && oap->op_type != OP_FOLDCLOSE
1656 && oap->op_type != OP_FOLDCLOSEREC
1657 && oap->op_type != OP_FOLDDEL
1658 && oap->op_type != OP_FOLDDELREC
1659 #endif
1660 && oap->motion_force == NUL
1663 /* Prepare for redoing. Only use the nchar field for "r",
1664 * otherwise it might be the second char of the operator. */
1665 prep_redo(oap->regname, 0L, NUL, 'v',
1666 get_op_char(oap->op_type),
1667 get_extra_op_char(oap->op_type),
1668 oap->op_type == OP_REPLACE ? cap->nchar : NUL);
1669 if (!redo_VIsual_busy)
1671 redo_VIsual_mode = resel_VIsual_mode;
1672 redo_VIsual_col = resel_VIsual_col;
1673 redo_VIsual_line_count = resel_VIsual_line_count;
1674 redo_VIsual_count = cap->count0;
1679 * oap->inclusive defaults to TRUE.
1680 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1681 * FALSE. This makes "d}P" and "v}dP" work the same.
1683 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1684 oap->inclusive = TRUE;
1685 if (VIsual_mode == 'V')
1686 oap->motion_type = MLINE;
1687 else
1689 oap->motion_type = MCHAR;
1690 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1691 # ifdef FEAT_VIRTUALEDIT
1692 && (include_line_break || !virtual_op)
1693 # endif
1696 oap->inclusive = FALSE;
1697 /* Try to include the newline, unless it's an operator
1698 * that works on lines only */
1699 if (*p_sel != 'o'
1700 && !op_on_lines(oap->op_type)
1701 && oap->end.lnum < curbuf->b_ml.ml_line_count)
1703 ++oap->end.lnum;
1704 oap->end.col = 0;
1705 # ifdef FEAT_VIRTUALEDIT
1706 oap->end.coladd = 0;
1707 # endif
1708 ++oap->line_count;
1713 redo_VIsual_busy = FALSE;
1716 * Switch Visual off now, so screen updating does
1717 * not show inverted text when the screen is redrawn.
1718 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1719 * no screen redraw, so it is done here to remove the inverted
1720 * part.
1722 if (!gui_yank)
1724 VIsual_active = FALSE;
1725 # ifdef FEAT_MOUSE
1726 setmouse();
1727 mouse_dragging = 0;
1728 # endif
1729 if (mode_displayed)
1730 clear_cmdline = TRUE; /* unshow visual mode later */
1731 #ifdef FEAT_CMDL_INFO
1732 else
1733 clear_showcmd();
1734 #endif
1735 if ((oap->op_type == OP_YANK
1736 || oap->op_type == OP_COLON
1737 || oap->op_type == OP_FUNCTION
1738 || oap->op_type == OP_FILTER)
1739 && oap->motion_force == NUL)
1740 redraw_curbuf_later(INVERTED);
1743 #endif
1745 #ifdef FEAT_MBYTE
1746 /* Include the trailing byte of a multi-byte char. */
1747 if (has_mbyte && oap->inclusive)
1749 int l;
1751 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
1752 if (l > 1)
1753 oap->end.col += l - 1;
1755 #endif
1756 curwin->w_set_curswant = TRUE;
1759 * oap->empty is set when start and end are the same. The inclusive
1760 * flag affects this too, unless yanking and the end is on a NUL.
1762 oap->empty = (oap->motion_type == MCHAR
1763 && (!oap->inclusive
1764 || (oap->op_type == OP_YANK
1765 && gchar_pos(&oap->end) == NUL))
1766 && equalpos(oap->start, oap->end)
1767 #ifdef FEAT_VIRTUALEDIT
1768 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1769 #endif
1772 * For delete, change and yank, it's an error to operate on an
1773 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1775 empty_region_error = (oap->empty
1776 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1778 #ifdef FEAT_VISUAL
1779 /* Force a redraw when operating on an empty Visual region, when
1780 * 'modifiable is off or creating a fold. */
1781 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1782 # ifdef FEAT_FOLDING
1783 || oap->op_type == OP_FOLD
1784 # endif
1786 redraw_curbuf_later(INVERTED);
1787 #endif
1790 * If the end of an operator is in column one while oap->motion_type
1791 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1792 * character in the previous line. If op_start is on or before the
1793 * first non-blank in the line, the operator becomes linewise
1794 * (strange, but that's the way vi does it).
1796 if ( oap->motion_type == MCHAR
1797 && oap->inclusive == FALSE
1798 && !(cap->retval & CA_NO_ADJ_OP_END)
1799 && oap->end.col == 0
1800 #ifdef FEAT_VISUAL
1801 && (!oap->is_VIsual || *p_sel == 'o')
1802 && !oap->block_mode
1803 #endif
1804 && oap->line_count > 1)
1806 oap->end_adjusted = TRUE; /* remember that we did this */
1807 --oap->line_count;
1808 --oap->end.lnum;
1809 if (inindent(0))
1810 oap->motion_type = MLINE;
1811 else
1813 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1814 if (oap->end.col)
1816 --oap->end.col;
1817 oap->inclusive = TRUE;
1821 else
1822 oap->end_adjusted = FALSE;
1824 switch (oap->op_type)
1826 case OP_LSHIFT:
1827 case OP_RSHIFT:
1828 op_shift(oap, TRUE,
1829 #ifdef FEAT_VISUAL
1830 oap->is_VIsual ? (int)cap->count1 :
1831 #endif
1833 auto_format(FALSE, TRUE);
1834 break;
1836 case OP_JOIN_NS:
1837 case OP_JOIN:
1838 if (oap->line_count < 2)
1839 oap->line_count = 2;
1840 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1841 curbuf->b_ml.ml_line_count)
1842 beep_flush();
1843 else
1845 do_do_join(oap->line_count, oap->op_type == OP_JOIN);
1846 auto_format(FALSE, TRUE);
1848 break;
1850 case OP_DELETE:
1851 #ifdef FEAT_VISUAL
1852 VIsual_reselect = FALSE; /* don't reselect now */
1853 #endif
1854 if (empty_region_error)
1855 vim_beep();
1856 else
1858 (void)op_delete(oap);
1859 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1860 u_save_cursor(); /* cursor line wasn't saved yet */
1861 auto_format(FALSE, TRUE);
1863 break;
1865 case OP_YANK:
1866 if (empty_region_error)
1868 if (!gui_yank)
1869 vim_beep();
1871 else
1872 (void)op_yank(oap, FALSE, !gui_yank);
1873 check_cursor_col();
1874 break;
1876 case OP_CHANGE:
1877 #ifdef FEAT_VISUAL
1878 VIsual_reselect = FALSE; /* don't reselect now */
1879 #endif
1880 if (empty_region_error)
1881 vim_beep();
1882 else
1884 /* This is a new edit command, not a restart. Need to
1885 * remember it to make 'insertmode' work with mappings for
1886 * Visual mode. But do this only once and not when typed and
1887 * 'insertmode' isn't set. */
1888 if (p_im || !KeyTyped)
1889 restart_edit_save = restart_edit;
1890 else
1891 restart_edit_save = 0;
1892 restart_edit = 0;
1893 /* Reset finish_op now, don't want it set inside edit(). */
1894 finish_op = FALSE;
1895 if (op_change(oap)) /* will call edit() */
1896 cap->retval |= CA_COMMAND_BUSY;
1897 if (restart_edit == 0)
1898 restart_edit = restart_edit_save;
1900 break;
1902 case OP_FILTER:
1903 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1904 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1905 else
1906 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
1908 case OP_INDENT:
1909 case OP_COLON:
1911 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1913 * If 'equalprg' is empty, do the indenting internally.
1915 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1917 # ifdef FEAT_LISP
1918 if (curbuf->b_p_lisp)
1920 op_reindent(oap, get_lisp_indent);
1921 break;
1923 # endif
1924 # ifdef FEAT_CINDENT
1925 op_reindent(oap,
1926 # ifdef FEAT_EVAL
1927 *curbuf->b_p_inde != NUL ? get_expr_indent :
1928 # endif
1929 get_c_indent);
1930 break;
1931 # endif
1933 #endif
1935 op_colon(oap);
1936 break;
1938 case OP_TILDE:
1939 case OP_UPPER:
1940 case OP_LOWER:
1941 case OP_ROT13:
1942 if (empty_region_error)
1943 vim_beep();
1944 else
1945 op_tilde(oap);
1946 check_cursor_col();
1947 break;
1949 case OP_FORMAT:
1950 #if defined(FEAT_EVAL)
1951 if (*curbuf->b_p_fex != NUL)
1952 op_formatexpr(oap); /* use expression */
1953 else
1954 #endif
1955 if (*p_fp != NUL)
1956 op_colon(oap); /* use external command */
1957 else
1958 op_format(oap, FALSE); /* use internal function */
1959 break;
1961 case OP_FORMAT2:
1962 op_format(oap, TRUE); /* use internal function */
1963 break;
1965 case OP_FUNCTION:
1966 op_function(oap); /* call 'operatorfunc' */
1967 break;
1969 case OP_INSERT:
1970 case OP_APPEND:
1971 #ifdef FEAT_VISUAL
1972 VIsual_reselect = FALSE; /* don't reselect now */
1973 #endif
1974 #ifdef FEAT_VISUALEXTRA
1975 if (empty_region_error)
1976 vim_beep();
1977 else
1979 /* This is a new edit command, not a restart. Need to
1980 * remember it to make 'insertmode' work with mappings for
1981 * Visual mode. But do this only once. */
1982 restart_edit_save = restart_edit;
1983 restart_edit = 0;
1985 op_insert(oap, cap->count1);
1987 /* TODO: when inserting in several lines, should format all
1988 * the lines. */
1989 auto_format(FALSE, TRUE);
1991 if (restart_edit == 0)
1992 restart_edit = restart_edit_save;
1994 #else
1995 vim_beep();
1996 #endif
1997 break;
1999 case OP_REPLACE:
2000 #ifdef FEAT_VISUAL
2001 VIsual_reselect = FALSE; /* don't reselect now */
2002 #endif
2003 #ifdef FEAT_VISUALEXTRA
2004 if (empty_region_error)
2005 #endif
2006 vim_beep();
2007 #ifdef FEAT_VISUALEXTRA
2008 else
2009 op_replace(oap, cap->nchar);
2010 #endif
2011 break;
2013 #ifdef FEAT_FOLDING
2014 case OP_FOLD:
2015 VIsual_reselect = FALSE; /* don't reselect now */
2016 foldCreate(oap->start.lnum, oap->end.lnum);
2017 break;
2019 case OP_FOLDOPEN:
2020 case OP_FOLDOPENREC:
2021 case OP_FOLDCLOSE:
2022 case OP_FOLDCLOSEREC:
2023 VIsual_reselect = FALSE; /* don't reselect now */
2024 opFoldRange(oap->start.lnum, oap->end.lnum,
2025 oap->op_type == OP_FOLDOPEN
2026 || oap->op_type == OP_FOLDOPENREC,
2027 oap->op_type == OP_FOLDOPENREC
2028 || oap->op_type == OP_FOLDCLOSEREC,
2029 oap->is_VIsual);
2030 break;
2032 case OP_FOLDDEL:
2033 case OP_FOLDDELREC:
2034 VIsual_reselect = FALSE; /* don't reselect now */
2035 deleteFold(oap->start.lnum, oap->end.lnum,
2036 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2037 break;
2038 #endif
2039 default:
2040 clearopbeep(oap);
2042 #ifdef FEAT_VIRTUALEDIT
2043 virtual_op = MAYBE;
2044 #endif
2045 if (!gui_yank)
2048 * if 'sol' not set, go back to old column for some commands
2050 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2051 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2052 || oap->op_type == OP_DELETE))
2053 coladvance(curwin->w_curswant = old_col);
2055 else
2057 curwin->w_cursor = old_cursor;
2059 #ifdef FEAT_VISUAL
2060 oap->block_mode = FALSE;
2061 #endif
2062 clearop(oap);
2067 * Handle indent and format operators and visual mode ":".
2069 static void
2070 op_colon(oap)
2071 oparg_T *oap;
2073 stuffcharReadbuff(':');
2074 #ifdef FEAT_VISUAL
2075 if (oap->is_VIsual)
2076 stuffReadbuff((char_u *)"'<,'>");
2077 else
2078 #endif
2081 * Make the range look nice, so it can be repeated.
2083 if (oap->start.lnum == curwin->w_cursor.lnum)
2084 stuffcharReadbuff('.');
2085 else
2086 stuffnumReadbuff((long)oap->start.lnum);
2087 if (oap->end.lnum != oap->start.lnum)
2089 stuffcharReadbuff(',');
2090 if (oap->end.lnum == curwin->w_cursor.lnum)
2091 stuffcharReadbuff('.');
2092 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2093 stuffcharReadbuff('$');
2094 else if (oap->start.lnum == curwin->w_cursor.lnum)
2096 stuffReadbuff((char_u *)".+");
2097 stuffnumReadbuff((long)oap->line_count - 1);
2099 else
2100 stuffnumReadbuff((long)oap->end.lnum);
2103 if (oap->op_type != OP_COLON)
2104 stuffReadbuff((char_u *)"!");
2105 if (oap->op_type == OP_INDENT)
2107 #ifndef FEAT_CINDENT
2108 if (*get_equalprg() == NUL)
2109 stuffReadbuff((char_u *)"indent");
2110 else
2111 #endif
2112 stuffReadbuff(get_equalprg());
2113 stuffReadbuff((char_u *)"\n");
2115 else if (oap->op_type == OP_FORMAT)
2117 if (*p_fp == NUL)
2118 stuffReadbuff((char_u *)"fmt");
2119 else
2120 stuffReadbuff(p_fp);
2121 stuffReadbuff((char_u *)"\n']");
2125 * do_cmdline() does the rest
2130 * Handle the "g@" operator: call 'operatorfunc'.
2132 /*ARGSUSED*/
2133 static void
2134 op_function(oap)
2135 oparg_T *oap;
2137 #ifdef FEAT_EVAL
2138 char_u *(argv[1]);
2140 if (*p_opfunc == NUL)
2141 EMSG(_("E774: 'operatorfunc' is empty"));
2142 else
2144 /* Set '[ and '] marks to text to be operated on. */
2145 curbuf->b_op_start = oap->start;
2146 curbuf->b_op_end = oap->end;
2147 if (oap->motion_type != MLINE && !oap->inclusive)
2148 /* Exclude the end position. */
2149 decl(&curbuf->b_op_end);
2151 if (oap->block_mode)
2152 argv[0] = (char_u *)"block";
2153 else if (oap->motion_type == MLINE)
2154 argv[0] = (char_u *)"line";
2155 else
2156 argv[0] = (char_u *)"char";
2157 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
2159 #else
2160 EMSG(_("E775: Eval feature not available"));
2161 #endif
2164 #if defined(FEAT_MOUSE) || defined(PROTO)
2166 * Do the appropriate action for the current mouse click in the current mode.
2167 * Not used for Command-line mode.
2169 * Normal Mode:
2170 * event modi- position visual change action
2171 * fier cursor window
2172 * left press - yes end yes
2173 * left press C yes end yes "^]" (2)
2174 * left press S yes end yes "*" (2)
2175 * left drag - yes start if moved no
2176 * left relse - yes start if moved no
2177 * middle press - yes if not active no put register
2178 * middle press - yes if active no yank and put
2179 * right press - yes start or extend yes
2180 * right press S yes no change yes "#" (2)
2181 * right drag - yes extend no
2182 * right relse - yes extend no
2184 * Insert or Replace Mode:
2185 * event modi- position visual change action
2186 * fier cursor window
2187 * left press - yes (cannot be active) yes
2188 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2189 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2190 * left drag - yes start or extend (1) no CTRL-O (1)
2191 * left relse - yes start or extend (1) no CTRL-O (1)
2192 * middle press - no (cannot be active) no put register
2193 * right press - yes start or extend yes CTRL-O
2194 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2196 * (1) only if mouse pointer moved since press
2197 * (2) only if click is in same buffer
2199 * Return TRUE if start_arrow() should be called for edit mode.
2202 do_mouse(oap, c, dir, count, fixindent)
2203 oparg_T *oap; /* operator argument, can be NULL */
2204 int c; /* K_LEFTMOUSE, etc */
2205 int dir; /* Direction to 'put' if necessary */
2206 long count;
2207 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2209 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2210 static int got_click = FALSE; /* got a click some time back */
2212 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2213 int is_click; /* If FALSE it's a drag or release event */
2214 int is_drag; /* If TRUE it's a drag event */
2215 int jump_flags = 0; /* flags for jump_to_mouse() */
2216 pos_T start_visual;
2217 int moved; /* Has cursor moved? */
2218 int in_status_line; /* mouse in status line */
2219 #ifdef FEAT_VERTSPLIT
2220 int in_sep_line; /* mouse in vertical separator line */
2221 #endif
2222 int c1, c2;
2223 #if defined(FEAT_FOLDING)
2224 pos_T save_cursor;
2225 #endif
2226 win_T *old_curwin = curwin;
2227 #ifdef FEAT_VISUAL
2228 static pos_T orig_cursor;
2229 colnr_T leftcol, rightcol;
2230 pos_T end_visual;
2231 int diff;
2232 int old_active = VIsual_active;
2233 int old_mode = VIsual_mode;
2234 #endif
2235 int regname;
2237 #if defined(FEAT_FOLDING)
2238 save_cursor = curwin->w_cursor;
2239 #endif
2242 * When GUI is active, always recognize mouse events, otherwise:
2243 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2244 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2245 * - For command line and insert mode 'mouse' is checked before calling
2246 * do_mouse().
2248 if (do_always)
2249 do_always = FALSE;
2250 else
2251 #ifdef FEAT_GUI
2252 if (!gui.in_use)
2253 #endif
2255 #ifdef FEAT_VISUAL
2256 if (VIsual_active)
2258 if (!mouse_has(MOUSE_VISUAL))
2259 return FALSE;
2261 else
2262 #endif
2263 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2264 return FALSE;
2267 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2269 #ifdef FEAT_MOUSESHAPE
2270 /* May have stopped dragging the status or separator line. The pointer is
2271 * most likely still on the status or separator line. */
2272 if (!is_drag && drag_status_line)
2274 drag_status_line = FALSE;
2275 update_mouseshape(SHAPE_IDX_STATUS);
2277 # ifdef FEAT_VERTSPLIT
2278 if (!is_drag && drag_sep_line)
2280 drag_sep_line = FALSE;
2281 update_mouseshape(SHAPE_IDX_VSEP);
2283 # endif
2284 #endif
2287 * Ignore drag and release events if we didn't get a click.
2289 if (is_click)
2290 got_click = TRUE;
2291 else
2293 if (!got_click) /* didn't get click, ignore */
2294 return FALSE;
2295 if (!is_drag) /* release, reset got_click */
2296 got_click = FALSE;
2299 #ifndef FEAT_VISUAL
2301 * ALT is only used for starging/extending Visual mode.
2303 if ((mod_mask & MOD_MASK_ALT))
2304 return FALSE;
2305 #endif
2308 * CTRL right mouse button does CTRL-T
2310 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2312 if (State & INSERT)
2313 stuffcharReadbuff(Ctrl_O);
2314 if (count > 1)
2315 stuffnumReadbuff(count);
2316 stuffcharReadbuff(Ctrl_T);
2317 got_click = FALSE; /* ignore drag&release now */
2318 return FALSE;
2322 * CTRL only works with left mouse button
2324 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2325 return FALSE;
2328 * When a modifier is down, ignore drag and release events, as well as
2329 * multiple clicks and the middle mouse button.
2330 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2332 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2333 | MOD_MASK_META))
2334 && (!is_click
2335 || (mod_mask & MOD_MASK_MULTI_CLICK)
2336 || which_button == MOUSE_MIDDLE)
2337 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
2338 && mouse_model_popup()
2339 && which_button == MOUSE_LEFT)
2340 && !((mod_mask & MOD_MASK_ALT)
2341 && !mouse_model_popup()
2342 && which_button == MOUSE_RIGHT)
2344 return FALSE;
2347 * If the button press was used as the movement command for an operator
2348 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2349 * drag/release events.
2351 if (!is_click && which_button == MOUSE_MIDDLE)
2352 return FALSE;
2354 if (oap != NULL)
2355 regname = oap->regname;
2356 else
2357 regname = 0;
2360 * Middle mouse button does a 'put' of the selected text
2362 if (which_button == MOUSE_MIDDLE)
2364 if (State == NORMAL)
2367 * If an operator was pending, we don't know what the user wanted
2368 * to do. Go back to normal mode: Clear the operator and beep().
2370 if (oap != NULL && oap->op_type != OP_NOP)
2372 clearopbeep(oap);
2373 return FALSE;
2376 #ifdef FEAT_VISUAL
2378 * If visual was active, yank the highlighted text and put it
2379 * before the mouse pointer position.
2381 if (VIsual_active)
2383 stuffcharReadbuff('y');
2384 stuffcharReadbuff(K_MIDDLEMOUSE);
2385 do_always = TRUE; /* ignore 'mouse' setting next time */
2386 return FALSE;
2388 #endif
2390 * The rest is below jump_to_mouse()
2394 else if ((State & INSERT) == 0)
2395 return FALSE;
2398 * Middle click in insert mode doesn't move the mouse, just insert the
2399 * contents of a register. '.' register is special, can't insert that
2400 * with do_put().
2401 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2402 * happens for the GUI).
2404 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2406 if (regname == '.')
2407 insert_reg(regname, TRUE);
2408 else
2410 #ifdef FEAT_CLIPBOARD
2411 if (clip_star.available && regname == 0)
2412 regname = '*';
2413 #endif
2414 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2415 insert_reg(regname, TRUE);
2416 else
2418 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2420 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2421 AppendCharToRedobuff(Ctrl_R);
2422 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2423 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2426 return FALSE;
2430 /* When dragging or button-up stay in the same window. */
2431 if (!is_click)
2432 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2434 start_visual.lnum = 0;
2436 #ifdef FEAT_WINDOWS
2437 /* Check for clicking in the tab page line. */
2438 if (mouse_row == 0 && firstwin->w_winrow > 0)
2440 got_click = FALSE; /* ignore mouse-up and drag events */
2442 /* click in a tab selects that tab page */
2443 if (is_click
2444 # ifdef FEAT_CMDWIN
2445 && cmdwin_type == 0
2446 # endif
2447 && mouse_col < Columns)
2449 c1 = TabPageIdxs[mouse_col];
2450 if (c1 >= 0)
2452 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2454 /* double click opens new page */
2455 end_visual_mode();
2456 tabpage_new();
2457 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2459 else
2461 /* Go to specified tab page, or next one if not clicking
2462 * on a label. */
2463 goto_tabpage(c1);
2465 /* It's like clicking on the status line of a window. */
2466 if (curwin != old_curwin)
2467 end_visual_mode();
2470 else if (c1 < 0)
2472 tabpage_T *tp;
2474 /* Close the current or specified tab page. */
2475 if (c1 == -999)
2476 tp = curtab;
2477 else
2478 tp = find_tabpage(-c1);
2479 if (tp == curtab)
2481 if (first_tabpage->tp_next != NULL)
2482 tabpage_close(FALSE);
2484 else if (tp != NULL)
2485 tabpage_close_other(tp, FALSE);
2488 return TRUE;
2490 #endif
2493 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2494 * right button up -> pop-up menu
2495 * shift-left button -> right button
2496 * alt-left button -> alt-right button
2498 if (mouse_model_popup())
2500 if (which_button == MOUSE_RIGHT
2501 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2504 * NOTE: Ignore right button down and drag mouse events.
2505 * Windows only shows the popup menu on the button up event.
2507 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2508 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2509 if (!is_click)
2510 return FALSE;
2511 #endif
2512 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2513 if (is_click || is_drag)
2514 return FALSE;
2515 #endif
2516 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2517 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2518 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2519 if (gui.in_use)
2521 jump_flags = 0;
2522 if (STRCMP(p_mousem, "popup_setpos") == 0)
2524 /* First set the cursor position before showing the popup
2525 * menu. */
2526 #ifdef FEAT_VISUAL
2527 if (VIsual_active)
2529 pos_T m_pos;
2532 * set MOUSE_MAY_STOP_VIS if we are outside the
2533 * selection or the current window (might have false
2534 * negative here)
2536 if (mouse_row < W_WINROW(curwin)
2537 || mouse_row
2538 > (W_WINROW(curwin) + curwin->w_height))
2539 jump_flags = MOUSE_MAY_STOP_VIS;
2540 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2541 jump_flags = MOUSE_MAY_STOP_VIS;
2542 else
2544 if ((lt(curwin->w_cursor, VIsual)
2545 && (lt(m_pos, curwin->w_cursor)
2546 || lt(VIsual, m_pos)))
2547 || (lt(VIsual, curwin->w_cursor)
2548 && (lt(m_pos, VIsual)
2549 || lt(curwin->w_cursor, m_pos))))
2551 jump_flags = MOUSE_MAY_STOP_VIS;
2553 else if (VIsual_mode == Ctrl_V)
2555 getvcols(curwin, &curwin->w_cursor, &VIsual,
2556 &leftcol, &rightcol);
2557 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2558 if (m_pos.col < leftcol || m_pos.col > rightcol)
2559 jump_flags = MOUSE_MAY_STOP_VIS;
2563 else
2564 jump_flags = MOUSE_MAY_STOP_VIS;
2565 #endif
2567 if (jump_flags)
2569 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2570 update_curbuf(
2571 #ifdef FEAT_VISUAL
2572 VIsual_active ? INVERTED :
2573 #endif
2574 VALID);
2575 setcursor();
2576 out_flush(); /* Update before showing popup menu */
2578 # ifdef FEAT_MENU
2579 gui_show_popupmenu();
2580 # endif
2581 return (jump_flags & CURSOR_MOVED) != 0;
2583 else
2584 return FALSE;
2585 #else
2586 return FALSE;
2587 #endif
2589 if (which_button == MOUSE_LEFT
2590 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
2592 which_button = MOUSE_RIGHT;
2593 mod_mask &= ~MOD_MASK_SHIFT;
2597 #ifdef FEAT_VISUAL
2598 if ((State & (NORMAL | INSERT))
2599 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2601 if (which_button == MOUSE_LEFT)
2603 if (is_click)
2605 /* stop Visual mode for a left click in a window, but not when
2606 * on a status line */
2607 if (VIsual_active)
2608 jump_flags |= MOUSE_MAY_STOP_VIS;
2610 else if (mouse_has(MOUSE_VISUAL))
2611 jump_flags |= MOUSE_MAY_VIS;
2613 else if (which_button == MOUSE_RIGHT)
2615 if (is_click && VIsual_active)
2618 * Remember the start and end of visual before moving the
2619 * cursor.
2621 if (lt(curwin->w_cursor, VIsual))
2623 start_visual = curwin->w_cursor;
2624 end_visual = VIsual;
2626 else
2628 start_visual = VIsual;
2629 end_visual = curwin->w_cursor;
2632 jump_flags |= MOUSE_FOCUS;
2633 if (mouse_has(MOUSE_VISUAL))
2634 jump_flags |= MOUSE_MAY_VIS;
2637 #endif
2640 * If an operator is pending, ignore all drags and releases until the
2641 * next mouse click.
2643 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2645 got_click = FALSE;
2646 oap->motion_type = MCHAR;
2649 /* When releasing the button let jump_to_mouse() know. */
2650 if (!is_click && !is_drag)
2651 jump_flags |= MOUSE_RELEASED;
2654 * JUMP!
2656 jump_flags = jump_to_mouse(jump_flags,
2657 oap == NULL ? NULL : &(oap->inclusive), which_button);
2658 moved = (jump_flags & CURSOR_MOVED);
2659 in_status_line = (jump_flags & IN_STATUS_LINE);
2660 #ifdef FEAT_VERTSPLIT
2661 in_sep_line = (jump_flags & IN_SEP_LINE);
2662 #endif
2664 #ifdef FEAT_NETBEANS_INTG
2665 if (usingNetbeans && isNetbeansBuffer(curbuf)
2666 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2668 int key = KEY2TERMCAP1(c);
2670 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2671 || key == (int)KE_RIGHTRELEASE)
2672 netbeans_button_release(which_button);
2674 #endif
2676 /* When jumping to another window, clear a pending operator. That's a bit
2677 * friendlier than beeping and not jumping to that window. */
2678 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2679 clearop(oap);
2681 #ifdef FEAT_FOLDING
2682 if (mod_mask == 0
2683 && !is_drag
2684 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2685 && which_button == MOUSE_LEFT)
2687 /* open or close a fold at this line */
2688 if (jump_flags & MOUSE_FOLD_OPEN)
2689 openFold(curwin->w_cursor.lnum, 1L);
2690 else
2691 closeFold(curwin->w_cursor.lnum, 1L);
2692 /* don't move the cursor if still in the same window */
2693 if (curwin == old_curwin)
2694 curwin->w_cursor = save_cursor;
2696 #endif
2698 #if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2699 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2701 clip_modeless(which_button, is_click, is_drag);
2702 return FALSE;
2704 #endif
2706 #ifdef FEAT_VISUAL
2707 /* Set global flag that we are extending the Visual area with mouse
2708 * dragging; temporarily mimimize 'scrolloff'. */
2709 if (VIsual_active && is_drag && p_so)
2711 /* In the very first line, allow scrolling one line */
2712 if (mouse_row == 0)
2713 mouse_dragging = 2;
2714 else
2715 mouse_dragging = 1;
2718 /* When dragging the mouse above the window, scroll down. */
2719 if (is_drag && mouse_row < 0 && !in_status_line)
2721 scroll_redraw(FALSE, 1L);
2722 mouse_row = 0;
2725 if (start_visual.lnum) /* right click in visual mode */
2727 /* When ALT is pressed make Visual mode blockwise. */
2728 if (mod_mask & MOD_MASK_ALT)
2729 VIsual_mode = Ctrl_V;
2732 * In Visual-block mode, divide the area in four, pick up the corner
2733 * that is in the quarter that the cursor is in.
2735 if (VIsual_mode == Ctrl_V)
2737 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2738 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2739 end_visual.col = leftcol;
2740 else
2741 end_visual.col = rightcol;
2742 if (curwin->w_cursor.lnum <
2743 (start_visual.lnum + end_visual.lnum) / 2)
2744 end_visual.lnum = end_visual.lnum;
2745 else
2746 end_visual.lnum = start_visual.lnum;
2748 /* move VIsual to the right column */
2749 start_visual = curwin->w_cursor; /* save the cursor pos */
2750 curwin->w_cursor = end_visual;
2751 coladvance(end_visual.col);
2752 VIsual = curwin->w_cursor;
2753 curwin->w_cursor = start_visual; /* restore the cursor */
2755 else
2758 * If the click is before the start of visual, change the start.
2759 * If the click is after the end of visual, change the end. If
2760 * the click is inside the visual, change the closest side.
2762 if (lt(curwin->w_cursor, start_visual))
2763 VIsual = end_visual;
2764 else if (lt(end_visual, curwin->w_cursor))
2765 VIsual = start_visual;
2766 else
2768 /* In the same line, compare column number */
2769 if (end_visual.lnum == start_visual.lnum)
2771 if (curwin->w_cursor.col - start_visual.col >
2772 end_visual.col - curwin->w_cursor.col)
2773 VIsual = start_visual;
2774 else
2775 VIsual = end_visual;
2778 /* In different lines, compare line number */
2779 else
2781 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2782 (end_visual.lnum - curwin->w_cursor.lnum);
2784 if (diff > 0) /* closest to end */
2785 VIsual = start_visual;
2786 else if (diff < 0) /* closest to start */
2787 VIsual = end_visual;
2788 else /* in the middle line */
2790 if (curwin->w_cursor.col <
2791 (start_visual.col + end_visual.col) / 2)
2792 VIsual = end_visual;
2793 else
2794 VIsual = start_visual;
2801 * If Visual mode started in insert mode, execute "CTRL-O"
2803 else if ((State & INSERT) && VIsual_active)
2804 stuffcharReadbuff(Ctrl_O);
2805 #endif
2808 * Middle mouse click: Put text before cursor.
2810 if (which_button == MOUSE_MIDDLE)
2812 #ifdef FEAT_CLIPBOARD
2813 if (clip_star.available && regname == 0)
2814 regname = '*';
2815 #endif
2816 if (yank_register_mline(regname))
2818 if (mouse_past_bottom)
2819 dir = FORWARD;
2821 else if (mouse_past_eol)
2822 dir = FORWARD;
2824 if (fixindent)
2826 c1 = (dir == BACKWARD) ? '[' : ']';
2827 c2 = 'p';
2829 else
2831 c1 = (dir == FORWARD) ? 'p' : 'P';
2832 c2 = NUL;
2834 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2837 * Remember where the paste started, so in edit() Insstart can be set
2838 * to this position
2840 if (restart_edit != 0)
2841 where_paste_started = curwin->w_cursor;
2842 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2845 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2847 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2848 * error under the mouse pointer.
2850 else if (((mod_mask & MOD_MASK_CTRL)
2851 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2852 && bt_quickfix(curbuf))
2854 if (State & INSERT)
2855 stuffcharReadbuff(Ctrl_O);
2856 if (curwin->w_llist_ref == NULL) /* quickfix window */
2857 stuffReadbuff((char_u *)":.cc\n");
2858 else /* location list window */
2859 stuffReadbuff((char_u *)":.ll\n");
2860 got_click = FALSE; /* ignore drag&release now */
2862 #endif
2865 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2866 * under the mouse pointer.
2868 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2869 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2871 if (State & INSERT)
2872 stuffcharReadbuff(Ctrl_O);
2873 stuffcharReadbuff(Ctrl_RSB);
2874 got_click = FALSE; /* ignore drag&release now */
2878 * Shift-Mouse click searches for the next occurrence of the word under
2879 * the mouse pointer
2881 else if ((mod_mask & MOD_MASK_SHIFT))
2883 if (State & INSERT
2884 #ifdef FEAT_VISUAL
2885 || (VIsual_active && VIsual_select)
2886 #endif
2888 stuffcharReadbuff(Ctrl_O);
2889 if (which_button == MOUSE_LEFT)
2890 stuffcharReadbuff('*');
2891 else /* MOUSE_RIGHT */
2892 stuffcharReadbuff('#');
2895 /* Handle double clicks, unless on status line */
2896 else if (in_status_line)
2898 #ifdef FEAT_MOUSESHAPE
2899 if ((is_drag || is_click) && !drag_status_line)
2901 drag_status_line = TRUE;
2902 update_mouseshape(-1);
2904 #endif
2906 #ifdef FEAT_VERTSPLIT
2907 else if (in_sep_line)
2909 # ifdef FEAT_MOUSESHAPE
2910 if ((is_drag || is_click) && !drag_sep_line)
2912 drag_sep_line = TRUE;
2913 update_mouseshape(-1);
2915 # endif
2917 #endif
2918 #ifdef FEAT_VISUAL
2919 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
2920 && mouse_has(MOUSE_VISUAL))
2922 if (is_click || !VIsual_active)
2924 if (VIsual_active)
2925 orig_cursor = VIsual;
2926 else
2928 check_visual_highlight();
2929 VIsual = curwin->w_cursor;
2930 orig_cursor = VIsual;
2931 VIsual_active = TRUE;
2932 VIsual_reselect = TRUE;
2933 /* start Select mode if 'selectmode' contains "mouse" */
2934 may_start_select('o');
2935 setmouse();
2937 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2939 /* Double click with ALT pressed makes it blockwise. */
2940 if (mod_mask & MOD_MASK_ALT)
2941 VIsual_mode = Ctrl_V;
2942 else
2943 VIsual_mode = 'v';
2945 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
2946 VIsual_mode = 'V';
2947 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
2948 VIsual_mode = Ctrl_V;
2949 #ifdef FEAT_CLIPBOARD
2950 /* Make sure the clipboard gets updated. Needed because start and
2951 * end may still be the same, and the selection needs to be owned */
2952 clip_star.vmode = NUL;
2953 #endif
2956 * A double click selects a word or a block.
2958 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2960 pos_T *pos = NULL;
2961 int gc;
2963 if (is_click)
2965 /* If the character under the cursor (skipping white space) is
2966 * not a word character, try finding a match and select a (),
2967 * {}, [], #if/#endif, etc. block. */
2968 end_visual = curwin->w_cursor;
2969 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
2970 inc(&end_visual);
2971 if (oap != NULL)
2972 oap->motion_type = MCHAR;
2973 if (oap != NULL
2974 && VIsual_mode == 'v'
2975 && !vim_iswordc(gchar_pos(&end_visual))
2976 && equalpos(curwin->w_cursor, VIsual)
2977 && (pos = findmatch(oap, NUL)) != NULL)
2979 curwin->w_cursor = *pos;
2980 if (oap->motion_type == MLINE)
2981 VIsual_mode = 'V';
2982 else if (*p_sel == 'e')
2984 if (lt(curwin->w_cursor, VIsual))
2985 ++VIsual.col;
2986 else
2987 ++curwin->w_cursor.col;
2992 if (pos == NULL && (is_click || is_drag))
2994 /* When not found a match or when dragging: extend to include
2995 * a word. */
2996 if (lt(curwin->w_cursor, orig_cursor))
2998 find_start_of_word(&curwin->w_cursor);
2999 find_end_of_word(&VIsual);
3001 else
3003 find_start_of_word(&VIsual);
3004 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3005 #ifdef FEAT_MBYTE
3006 curwin->w_cursor.col +=
3007 (*mb_ptr2len)(ml_get_cursor());
3008 #else
3009 ++curwin->w_cursor.col;
3010 #endif
3011 find_end_of_word(&curwin->w_cursor);
3014 curwin->w_set_curswant = TRUE;
3016 if (is_click)
3017 redraw_curbuf_later(INVERTED); /* update the inversion */
3019 else if (VIsual_active && !old_active)
3021 if (mod_mask & MOD_MASK_ALT)
3022 VIsual_mode = Ctrl_V;
3023 else
3024 VIsual_mode = 'v';
3027 /* If Visual mode changed show it later. */
3028 if ((!VIsual_active && old_active && mode_displayed)
3029 || (VIsual_active && p_smd && msg_silent == 0
3030 && (!old_active || VIsual_mode != old_mode)))
3031 redraw_cmdline = TRUE;
3032 #endif
3034 return moved;
3037 #ifdef FEAT_VISUAL
3039 * Move "pos" back to the start of the word it's in.
3041 static void
3042 find_start_of_word(pos)
3043 pos_T *pos;
3045 char_u *line;
3046 int cclass;
3047 int col;
3049 line = ml_get(pos->lnum);
3050 cclass = get_mouse_class(line + pos->col);
3052 while (pos->col > 0)
3054 col = pos->col - 1;
3055 #ifdef FEAT_MBYTE
3056 col -= (*mb_head_off)(line, line + col);
3057 #endif
3058 if (get_mouse_class(line + col) != cclass)
3059 break;
3060 pos->col = col;
3065 * Move "pos" forward to the end of the word it's in.
3066 * When 'selection' is "exclusive", the position is just after the word.
3068 static void
3069 find_end_of_word(pos)
3070 pos_T *pos;
3072 char_u *line;
3073 int cclass;
3074 int col;
3076 line = ml_get(pos->lnum);
3077 if (*p_sel == 'e' && pos->col > 0)
3079 --pos->col;
3080 #ifdef FEAT_MBYTE
3081 pos->col -= (*mb_head_off)(line, line + pos->col);
3082 #endif
3084 cclass = get_mouse_class(line + pos->col);
3085 while (line[pos->col] != NUL)
3087 #ifdef FEAT_MBYTE
3088 col = pos->col + (*mb_ptr2len)(line + pos->col);
3089 #else
3090 col = pos->col + 1;
3091 #endif
3092 if (get_mouse_class(line + col) != cclass)
3094 if (*p_sel == 'e')
3095 pos->col = col;
3096 break;
3098 pos->col = col;
3103 * Get class of a character for selection: same class means same word.
3104 * 0: blank
3105 * 1: punctuation groups
3106 * 2: normal word character
3107 * >2: multi-byte word character.
3109 static int
3110 get_mouse_class(p)
3111 char_u *p;
3113 int c;
3115 #ifdef FEAT_MBYTE
3116 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3117 return mb_get_class(p);
3118 #endif
3120 c = *p;
3121 if (c == ' ' || c == '\t')
3122 return 0;
3124 if (vim_iswordc(c))
3125 return 2;
3128 * There are a few special cases where we want certain combinations of
3129 * characters to be considered as a single word. These are things like
3130 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
3131 * character is in it's own class.
3133 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3134 return 1;
3135 return c;
3137 #endif /* FEAT_VISUAL */
3138 #endif /* FEAT_MOUSE */
3140 #if defined(FEAT_VISUAL) || defined(PROTO)
3142 * Check if highlighting for visual mode is possible, give a warning message
3143 * if not.
3145 void
3146 check_visual_highlight()
3148 static int did_check = FALSE;
3150 if (full_screen)
3152 if (!did_check && hl_attr(HLF_V) == 0)
3153 MSG(_("Warning: terminal cannot highlight"));
3154 did_check = TRUE;
3159 * End Visual mode.
3160 * This function should ALWAYS be called to end Visual mode, except from
3161 * do_pending_operator().
3163 void
3164 end_visual_mode()
3166 #ifdef FEAT_CLIPBOARD
3168 * If we are using the clipboard, then remember what was selected in case
3169 * we need to paste it somewhere while we still own the selection.
3170 * Only do this when the clipboard is already owned. Don't want to grab
3171 * the selection when hitting ESC.
3173 if (clip_star.available && clip_star.owned)
3174 clip_auto_select();
3175 #endif
3177 VIsual_active = FALSE;
3178 #ifdef FEAT_MOUSE
3179 setmouse();
3180 mouse_dragging = 0;
3181 #endif
3183 /* Save the current VIsual area for '< and '> marks, and "gv" */
3184 curbuf->b_visual.vi_mode = VIsual_mode;
3185 curbuf->b_visual.vi_start = VIsual;
3186 curbuf->b_visual.vi_end = curwin->w_cursor;
3187 curbuf->b_visual.vi_curswant = curwin->w_curswant;
3188 #ifdef FEAT_EVAL
3189 curbuf->b_visual_mode_eval = VIsual_mode;
3190 #endif
3191 #ifdef FEAT_VIRTUALEDIT
3192 if (!virtual_active())
3193 curwin->w_cursor.coladd = 0;
3194 #endif
3196 if (mode_displayed)
3197 clear_cmdline = TRUE; /* unshow visual mode later */
3198 #ifdef FEAT_CMDL_INFO
3199 else
3200 clear_showcmd();
3201 #endif
3203 adjust_cursor_eol();
3207 * Reset VIsual_active and VIsual_reselect.
3209 void
3210 reset_VIsual_and_resel()
3212 if (VIsual_active)
3214 end_visual_mode();
3215 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3217 VIsual_reselect = FALSE;
3221 * Reset VIsual_active and VIsual_reselect if it's set.
3223 void
3224 reset_VIsual()
3226 if (VIsual_active)
3228 end_visual_mode();
3229 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3230 VIsual_reselect = FALSE;
3233 #endif /* FEAT_VISUAL */
3235 #if defined(FEAT_BEVAL)
3236 static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3239 * Check for a balloon-eval special item to include when searching for an
3240 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3241 * Returns TRUE if the character at "*ptr" should be included.
3242 * "dir" is FORWARD or BACKWARD, the direction of searching.
3243 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3244 * "bnp" points to a counter for square brackets.
3246 static int
3247 find_is_eval_item(ptr, colp, bnp, dir)
3248 char_u *ptr;
3249 int *colp;
3250 int *bnp;
3251 int dir;
3253 /* Accept everything inside []. */
3254 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3255 ++*bnp;
3256 if (*bnp > 0)
3258 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3259 --*bnp;
3260 return TRUE;
3263 /* skip over "s.var" */
3264 if (*ptr == '.')
3265 return TRUE;
3267 /* two-character item: s->var */
3268 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3269 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3271 *colp += dir;
3272 return TRUE;
3274 return FALSE;
3276 #endif
3279 * Find the identifier under or to the right of the cursor.
3280 * "find_type" can have one of three values:
3281 * FIND_IDENT: find an identifier (keyword)
3282 * FIND_STRING: find any non-white string
3283 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
3284 * FIND_EVAL: find text useful for C program debugging
3286 * There are three steps:
3287 * 1. Search forward for the start of an identifier/string. Doesn't move if
3288 * already on one.
3289 * 2. Search backward for the start of this identifier/string.
3290 * This doesn't match the real Vi but I like it a little better and it
3291 * shouldn't bother anyone.
3292 * 3. Search forward to the end of this identifier/string.
3293 * When FIND_IDENT isn't defined, we backup until a blank.
3295 * Returns the length of the string, or zero if no string is found.
3296 * If a string is found, a pointer to the string is put in "*string". This
3297 * string is not always NUL terminated.
3300 find_ident_under_cursor(string, find_type)
3301 char_u **string;
3302 int find_type;
3304 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3305 curwin->w_cursor.col, string, find_type);
3309 * Like find_ident_under_cursor(), but for any window and any position.
3310 * However: Uses 'iskeyword' from the current window!.
3313 find_ident_at_pos(wp, lnum, startcol, string, find_type)
3314 win_T *wp;
3315 linenr_T lnum;
3316 colnr_T startcol;
3317 char_u **string;
3318 int find_type;
3320 char_u *ptr;
3321 int col = 0; /* init to shut up GCC */
3322 int i;
3323 #ifdef FEAT_MBYTE
3324 int this_class = 0;
3325 int prev_class;
3326 int prevcol;
3327 #endif
3328 #if defined(FEAT_BEVAL)
3329 int bn = 0; /* bracket nesting */
3330 #endif
3333 * if i == 0: try to find an identifier
3334 * if i == 1: try to find any non-white string
3336 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3337 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3340 * 1. skip to start of identifier/string
3342 col = startcol;
3343 #ifdef FEAT_MBYTE
3344 if (has_mbyte)
3346 while (ptr[col] != NUL)
3348 # if defined(FEAT_BEVAL)
3349 /* Stop at a ']' to evaluate "a[x]". */
3350 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3351 break;
3352 # endif
3353 this_class = mb_get_class(ptr + col);
3354 if (this_class != 0 && (i == 1 || this_class != 1))
3355 break;
3356 col += (*mb_ptr2len)(ptr + col);
3359 else
3360 #endif
3361 while (ptr[col] != NUL
3362 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
3363 # if defined(FEAT_BEVAL)
3364 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3365 # endif
3367 ++col;
3369 #if defined(FEAT_BEVAL)
3370 /* When starting on a ']' count it, so that we include the '['. */
3371 bn = ptr[col] == ']';
3372 #endif
3375 * 2. Back up to start of identifier/string.
3377 #ifdef FEAT_MBYTE
3378 if (has_mbyte)
3380 /* Remember class of character under cursor. */
3381 # if defined(FEAT_BEVAL)
3382 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3383 this_class = mb_get_class((char_u *)"a");
3384 else
3385 # endif
3386 this_class = mb_get_class(ptr + col);
3387 while (col > 0 && this_class != 0)
3389 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3390 prev_class = mb_get_class(ptr + prevcol);
3391 if (this_class != prev_class
3392 && (i == 0
3393 || prev_class == 0
3394 || (find_type & FIND_IDENT))
3395 # if defined(FEAT_BEVAL)
3396 && (!(find_type & FIND_EVAL)
3397 || prevcol == 0
3398 || !find_is_eval_item(ptr + prevcol, &prevcol,
3399 &bn, BACKWARD))
3400 # endif
3402 break;
3403 col = prevcol;
3406 /* If we don't want just any old string, or we've found an
3407 * identifier, stop searching. */
3408 if (this_class > 2)
3409 this_class = 2;
3410 if (!(find_type & FIND_STRING) || this_class == 2)
3411 break;
3413 else
3414 #endif
3416 while (col > 0
3417 && ((i == 0
3418 ? vim_iswordc(ptr[col - 1])
3419 : (!vim_iswhite(ptr[col - 1])
3420 && (!(find_type & FIND_IDENT)
3421 || !vim_iswordc(ptr[col - 1]))))
3422 #if defined(FEAT_BEVAL)
3423 || ((find_type & FIND_EVAL)
3424 && col > 1
3425 && find_is_eval_item(ptr + col - 1, &col,
3426 &bn, BACKWARD))
3427 #endif
3429 --col;
3431 /* If we don't want just any old string, or we've found an
3432 * identifier, stop searching. */
3433 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3434 break;
3438 if (ptr[col] == NUL || (i == 0 && (
3439 #ifdef FEAT_MBYTE
3440 has_mbyte ? this_class != 2 :
3441 #endif
3442 !vim_iswordc(ptr[col]))))
3445 * didn't find an identifier or string
3447 if (find_type & FIND_STRING)
3448 EMSG(_("E348: No string under cursor"));
3449 else
3450 EMSG(_("E349: No identifier under cursor"));
3451 return 0;
3453 ptr += col;
3454 *string = ptr;
3457 * 3. Find the end if the identifier/string.
3459 #if defined(FEAT_BEVAL)
3460 bn = 0;
3461 startcol -= col;
3462 #endif
3463 col = 0;
3464 #ifdef FEAT_MBYTE
3465 if (has_mbyte)
3467 /* Search for point of changing multibyte character class. */
3468 this_class = mb_get_class(ptr);
3469 while (ptr[col] != NUL
3470 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3471 : mb_get_class(ptr + col) != 0)
3472 # if defined(FEAT_BEVAL)
3473 || ((find_type & FIND_EVAL)
3474 && col <= (int)startcol
3475 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3476 # endif
3478 col += (*mb_ptr2len)(ptr + col);
3480 else
3481 #endif
3482 while ((i == 0 ? vim_iswordc(ptr[col])
3483 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
3484 # if defined(FEAT_BEVAL)
3485 || ((find_type & FIND_EVAL)
3486 && col <= (int)startcol
3487 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3488 # endif
3491 ++col;
3494 return col;
3498 * Prepare for redo of a normal command.
3500 static void
3501 prep_redo_cmd(cap)
3502 cmdarg_T *cap;
3504 prep_redo(cap->oap->regname, cap->count0,
3505 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3509 * Prepare for redo of any command.
3510 * Note that only the last argument can be a multi-byte char.
3512 static void
3513 prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3514 int regname;
3515 long num;
3516 int cmd1;
3517 int cmd2;
3518 int cmd3;
3519 int cmd4;
3520 int cmd5;
3522 ResetRedobuff();
3523 if (regname != 0) /* yank from specified buffer */
3525 AppendCharToRedobuff('"');
3526 AppendCharToRedobuff(regname);
3528 if (num)
3529 AppendNumberToRedobuff(num);
3531 if (cmd1 != NUL)
3532 AppendCharToRedobuff(cmd1);
3533 if (cmd2 != NUL)
3534 AppendCharToRedobuff(cmd2);
3535 if (cmd3 != NUL)
3536 AppendCharToRedobuff(cmd3);
3537 if (cmd4 != NUL)
3538 AppendCharToRedobuff(cmd4);
3539 if (cmd5 != NUL)
3540 AppendCharToRedobuff(cmd5);
3544 * check for operator active and clear it
3546 * return TRUE if operator was active
3548 static int
3549 checkclearop(oap)
3550 oparg_T *oap;
3552 if (oap->op_type == OP_NOP)
3553 return FALSE;
3554 clearopbeep(oap);
3555 return TRUE;
3559 * check for operator or Visual active and clear it
3561 * return TRUE if operator was active
3563 static int
3564 checkclearopq(oap)
3565 oparg_T *oap;
3567 if (oap->op_type == OP_NOP
3568 #ifdef FEAT_VISUAL
3569 && !VIsual_active
3570 #endif
3572 return FALSE;
3573 clearopbeep(oap);
3574 return TRUE;
3577 static void
3578 clearop(oap)
3579 oparg_T *oap;
3581 oap->op_type = OP_NOP;
3582 oap->regname = 0;
3583 oap->motion_force = NUL;
3584 oap->use_reg_one = FALSE;
3587 static void
3588 clearopbeep(oap)
3589 oparg_T *oap;
3591 clearop(oap);
3592 beep_flush();
3595 #ifdef FEAT_VISUAL
3597 * Remove the shift modifier from a special key.
3599 static void
3600 unshift_special(cap)
3601 cmdarg_T *cap;
3603 switch (cap->cmdchar)
3605 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3606 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3607 case K_S_UP: cap->cmdchar = K_UP; break;
3608 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3609 case K_S_HOME: cap->cmdchar = K_HOME; break;
3610 case K_S_END: cap->cmdchar = K_END; break;
3612 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3614 #endif
3616 #if defined(FEAT_CMDL_INFO) || defined(PROTO)
3618 * Routines for displaying a partly typed command
3621 #ifdef FEAT_VISUAL /* need room for size of Visual area */
3622 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3623 #else
3624 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3625 #endif
3626 static char_u showcmd_buf[SHOWCMD_BUFLEN];
3627 static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3628 static int showcmd_is_clear = TRUE;
3629 static int showcmd_visual = FALSE;
3631 static void display_showcmd __ARGS((void));
3633 void
3634 clear_showcmd()
3636 if (!p_sc)
3637 return;
3639 #ifdef FEAT_VISUAL
3640 if (VIsual_active && !char_avail())
3642 int i = lt(VIsual, curwin->w_cursor);
3643 long lines;
3644 colnr_T leftcol, rightcol;
3645 linenr_T top, bot;
3647 /* Show the size of the Visual area. */
3648 if (i)
3650 top = VIsual.lnum;
3651 bot = curwin->w_cursor.lnum;
3653 else
3655 top = curwin->w_cursor.lnum;
3656 bot = VIsual.lnum;
3658 # ifdef FEAT_FOLDING
3659 /* Include closed folds as a whole. */
3660 hasFolding(top, &top, NULL);
3661 hasFolding(bot, NULL, &bot);
3662 # endif
3663 lines = bot - top + 1;
3665 if (VIsual_mode == Ctrl_V)
3667 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
3668 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3669 (long)(rightcol - leftcol + 1));
3671 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3672 sprintf((char *)showcmd_buf, "%ld", lines);
3673 else
3674 sprintf((char *)showcmd_buf, "%ld", (long)(i
3675 ? curwin->w_cursor.col - VIsual.col
3676 : VIsual.col - curwin->w_cursor.col) + (*p_sel != 'e'));
3677 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3678 showcmd_visual = TRUE;
3680 else
3681 #endif
3683 showcmd_buf[0] = NUL;
3684 showcmd_visual = FALSE;
3686 /* Don't actually display something if there is nothing to clear. */
3687 if (showcmd_is_clear)
3688 return;
3691 display_showcmd();
3695 * Add 'c' to string of shown command chars.
3696 * Return TRUE if output has been written (and setcursor() has been called).
3699 add_to_showcmd(c)
3700 int c;
3702 char_u *p;
3703 int old_len;
3704 int extra_len;
3705 int overflow;
3706 #if defined(FEAT_MOUSE)
3707 int i;
3708 static int ignore[] =
3710 # ifdef FEAT_GUI
3711 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3712 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
3713 # endif
3714 K_IGNORE,
3715 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3716 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3717 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
3718 K_MOUSEDOWN, K_MOUSEUP,
3719 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
3720 K_CURSORHOLD,
3723 #endif
3725 if (!p_sc || msg_silent != 0)
3726 return FALSE;
3728 if (showcmd_visual)
3730 showcmd_buf[0] = NUL;
3731 showcmd_visual = FALSE;
3734 #if defined(FEAT_MOUSE)
3735 /* Ignore keys that are scrollbar updates and mouse clicks */
3736 if (IS_SPECIAL(c))
3737 for (i = 0; ignore[i] != 0; ++i)
3738 if (ignore[i] == c)
3739 return FALSE;
3740 #endif
3742 p = transchar(c);
3743 old_len = (int)STRLEN(showcmd_buf);
3744 extra_len = (int)STRLEN(p);
3745 overflow = old_len + extra_len - SHOWCMD_COLS;
3746 if (overflow > 0)
3747 STRCPY(showcmd_buf, showcmd_buf + overflow);
3748 STRCAT(showcmd_buf, p);
3750 if (char_avail())
3751 return FALSE;
3753 display_showcmd();
3755 return TRUE;
3758 void
3759 add_to_showcmd_c(c)
3760 int c;
3762 if (!add_to_showcmd(c))
3763 setcursor();
3767 * Delete 'len' characters from the end of the shown command.
3769 static void
3770 del_from_showcmd(len)
3771 int len;
3773 int old_len;
3775 if (!p_sc)
3776 return;
3778 old_len = (int)STRLEN(showcmd_buf);
3779 if (len > old_len)
3780 len = old_len;
3781 showcmd_buf[old_len - len] = NUL;
3783 if (!char_avail())
3784 display_showcmd();
3788 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3789 * something and there is a partial mapping.
3791 void
3792 push_showcmd()
3794 if (p_sc)
3795 STRCPY(old_showcmd_buf, showcmd_buf);
3798 void
3799 pop_showcmd()
3801 if (!p_sc)
3802 return;
3804 STRCPY(showcmd_buf, old_showcmd_buf);
3806 display_showcmd();
3809 static void
3810 display_showcmd()
3812 int len;
3814 cursor_off();
3816 len = (int)STRLEN(showcmd_buf);
3817 if (len == 0)
3818 showcmd_is_clear = TRUE;
3819 else
3821 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3822 showcmd_is_clear = FALSE;
3826 * clear the rest of an old message by outputing up to SHOWCMD_COLS spaces
3828 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3830 setcursor(); /* put cursor back where it belongs */
3832 #endif
3834 #ifdef FEAT_SCROLLBIND
3836 * When "check" is FALSE, prepare for commands that scroll the window.
3837 * When "check" is TRUE, take care of scroll-binding after the window has
3838 * scrolled. Called from normal_cmd() and edit().
3840 void
3841 do_check_scrollbind(check)
3842 int check;
3844 static win_T *old_curwin = NULL;
3845 static linenr_T old_topline = 0;
3846 #ifdef FEAT_DIFF
3847 static int old_topfill = 0;
3848 #endif
3849 static buf_T *old_buf = NULL;
3850 static colnr_T old_leftcol = 0;
3852 if (check && curwin->w_p_scb)
3854 /* If a ":syncbind" command was just used, don't scroll, only reset
3855 * the values. */
3856 if (did_syncbind)
3857 did_syncbind = FALSE;
3858 else if (curwin == old_curwin)
3861 * Synchronize other windows, as necessary according to
3862 * 'scrollbind'. Don't do this after an ":edit" command, except
3863 * when 'diff' is set.
3865 if ((curwin->w_buffer == old_buf
3866 #ifdef FEAT_DIFF
3867 || curwin->w_p_diff
3868 #endif
3870 && (curwin->w_topline != old_topline
3871 #ifdef FEAT_DIFF
3872 || curwin->w_topfill != old_topfill
3873 #endif
3874 || curwin->w_leftcol != old_leftcol))
3876 check_scrollbind(curwin->w_topline - old_topline,
3877 (long)(curwin->w_leftcol - old_leftcol));
3880 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
3883 * When switching between windows, make sure that the relative
3884 * vertical offset is valid for the new window. The relative
3885 * offset is invalid whenever another 'scrollbind' window has
3886 * scrolled to a point that would force the current window to
3887 * scroll past the beginning or end of its buffer. When the
3888 * resync is performed, some of the other 'scrollbind' windows may
3889 * need to jump so that the current window's relative position is
3890 * visible on-screen.
3892 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
3894 curwin->w_scbind_pos = curwin->w_topline;
3897 old_curwin = curwin;
3898 old_topline = curwin->w_topline;
3899 #ifdef FEAT_DIFF
3900 old_topfill = curwin->w_topfill;
3901 #endif
3902 old_buf = curwin->w_buffer;
3903 old_leftcol = curwin->w_leftcol;
3907 * Synchronize any windows that have "scrollbind" set, based on the
3908 * number of rows by which the current window has changed
3909 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3911 void
3912 check_scrollbind(topline_diff, leftcol_diff)
3913 linenr_T topline_diff;
3914 long leftcol_diff;
3916 int want_ver;
3917 int want_hor;
3918 win_T *old_curwin = curwin;
3919 buf_T *old_curbuf = curbuf;
3920 #ifdef FEAT_VISUAL
3921 int old_VIsual_select = VIsual_select;
3922 int old_VIsual_active = VIsual_active;
3923 #endif
3924 colnr_T tgt_leftcol = curwin->w_leftcol;
3925 long topline;
3926 long y;
3929 * check 'scrollopt' string for vertical and horizontal scroll options
3931 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
3932 #ifdef FEAT_DIFF
3933 want_ver |= old_curwin->w_p_diff;
3934 #endif
3935 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
3938 * loop through the scrollbound windows and scroll accordingly
3940 #ifdef FEAT_VISUAL
3941 VIsual_select = VIsual_active = 0;
3942 #endif
3943 for (curwin = firstwin; curwin; curwin = curwin->w_next)
3945 curbuf = curwin->w_buffer;
3946 /* skip original window and windows with 'noscrollbind' */
3947 if (curwin != old_curwin && curwin->w_p_scb)
3950 * do the vertical scroll
3952 if (want_ver)
3954 #ifdef FEAT_DIFF
3955 if (old_curwin->w_p_diff && curwin->w_p_diff)
3957 diff_set_topline(old_curwin, curwin);
3959 else
3960 #endif
3962 curwin->w_scbind_pos += topline_diff;
3963 topline = curwin->w_scbind_pos;
3964 if (topline > curbuf->b_ml.ml_line_count)
3965 topline = curbuf->b_ml.ml_line_count;
3966 if (topline < 1)
3967 topline = 1;
3969 y = topline - curwin->w_topline;
3970 if (y > 0)
3971 scrollup(y, FALSE);
3972 else
3973 scrolldown(-y, FALSE);
3976 redraw_later(VALID);
3977 cursor_correct();
3978 #ifdef FEAT_WINDOWS
3979 curwin->w_redr_status = TRUE;
3980 #endif
3984 * do the horizontal scroll
3986 if (want_hor && curwin->w_leftcol != tgt_leftcol)
3988 curwin->w_leftcol = tgt_leftcol;
3989 leftcol_changed();
3995 * reset current-window
3997 #ifdef FEAT_VISUAL
3998 VIsual_select = old_VIsual_select;
3999 VIsual_active = old_VIsual_active;
4000 #endif
4001 curwin = old_curwin;
4002 curbuf = old_curbuf;
4004 #endif /* #ifdef FEAT_SCROLLBIND */
4007 * Command character that's ignored.
4008 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
4009 * xon/xoff
4011 static void
4012 nv_ignore(cap)
4013 cmdarg_T *cap;
4015 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
4019 * Command character that doesn't do anything, but unlike nv_ignore() does
4020 * start edit(). Used for "startinsert" executed while starting up.
4022 /*ARGSUSED */
4023 static void
4024 nv_nop(cap)
4025 cmdarg_T *cap;
4030 * Command character doesn't exist.
4032 static void
4033 nv_error(cap)
4034 cmdarg_T *cap;
4036 clearopbeep(cap->oap);
4040 * <Help> and <F1> commands.
4042 static void
4043 nv_help(cap)
4044 cmdarg_T *cap;
4046 if (!checkclearopq(cap->oap))
4047 ex_help(NULL);
4051 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4053 static void
4054 nv_addsub(cap)
4055 cmdarg_T *cap;
4057 if (!checkclearopq(cap->oap)
4058 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4059 prep_redo_cmd(cap);
4063 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4065 static void
4066 nv_page(cap)
4067 cmdarg_T *cap;
4069 if (!checkclearop(cap->oap))
4071 #ifdef FEAT_WINDOWS
4072 if (mod_mask & MOD_MASK_CTRL)
4074 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4075 if (cap->arg == BACKWARD)
4076 goto_tabpage(-(int)cap->count1);
4077 else
4078 goto_tabpage((int)cap->count0);
4080 else
4081 #endif
4082 (void)onepage(cap->arg, cap->count1);
4087 * Implementation of "gd" and "gD" command.
4089 static void
4090 nv_gd(oap, nchar, thisblock)
4091 oparg_T *oap;
4092 int nchar;
4093 int thisblock; /* 1 for "1gd" and "1gD" */
4095 int len;
4096 char_u *ptr;
4098 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
4099 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
4100 clearopbeep(oap);
4101 #ifdef FEAT_FOLDING
4102 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4103 foldOpenCursor();
4104 #endif
4108 * Search for variable declaration of "ptr[len]".
4109 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4110 * current file ("gD").
4111 * When "thisblock" is TRUE check the {} block scope.
4112 * Return FAIL when not found.
4115 find_decl(ptr, len, locally, thisblock, searchflags)
4116 char_u *ptr;
4117 int len;
4118 int locally;
4119 int thisblock;
4120 int searchflags; /* flags passed to searchit() */
4122 char_u *pat;
4123 pos_T old_pos;
4124 pos_T par_pos;
4125 pos_T found_pos;
4126 int t;
4127 int save_p_ws;
4128 int save_p_scs;
4129 int retval = OK;
4130 int incll;
4132 if ((pat = alloc(len + 7)) == NULL)
4133 return FAIL;
4135 /* Put "\V" before the pattern to avoid that the special meaning of "."
4136 * and "~" causes trouble. */
4137 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4138 len, ptr);
4139 old_pos = curwin->w_cursor;
4140 save_p_ws = p_ws;
4141 save_p_scs = p_scs;
4142 p_ws = FALSE; /* don't wrap around end of file now */
4143 p_scs = FALSE; /* don't switch ignorecase off now */
4146 * With "gD" go to line 1.
4147 * With "gd" Search back for the start of the current function, then go
4148 * back until a blank line. If this fails go to line 1.
4150 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
4152 setpcmark(); /* Set in findpar() otherwise */
4153 curwin->w_cursor.lnum = 1;
4154 par_pos = curwin->w_cursor;
4156 else
4158 par_pos = curwin->w_cursor;
4159 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4160 --curwin->w_cursor.lnum;
4162 curwin->w_cursor.col = 0;
4164 /* Search forward for the identifier, ignore comment lines. */
4165 clearpos(&found_pos);
4166 for (;;)
4168 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
4169 pat, 1L, searchflags, RE_LAST, (linenr_T)0);
4170 if (curwin->w_cursor.lnum >= old_pos.lnum)
4171 t = FAIL; /* match after start is failure too */
4173 if (thisblock && t != FAIL)
4175 pos_T *pos;
4177 /* Check that the block the match is in doesn't end before the
4178 * position where we started the search from. */
4179 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4180 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4181 && pos->lnum < old_pos.lnum)
4182 continue;
4185 if (t == FAIL)
4187 /* If we previously found a valid position, use it. */
4188 if (found_pos.lnum != 0)
4190 curwin->w_cursor = found_pos;
4191 t = OK;
4193 break;
4195 #ifdef FEAT_COMMENTS
4196 if (get_leader_len(ml_get_curline(), NULL, FALSE) > 0)
4198 /* Ignore this line, continue at start of next line. */
4199 ++curwin->w_cursor.lnum;
4200 curwin->w_cursor.col = 0;
4201 continue;
4203 #endif
4204 if (!locally) /* global search: use first match found */
4205 break;
4206 if (curwin->w_cursor.lnum >= par_pos.lnum)
4208 /* If we previously found a valid position, use it. */
4209 if (found_pos.lnum != 0)
4210 curwin->w_cursor = found_pos;
4211 break;
4214 /* For finding a local variable and the match is before the "{" search
4215 * to find a later match. For K&R style function declarations this
4216 * skips the function header without types. */
4217 found_pos = curwin->w_cursor;
4220 if (t == FAIL)
4222 retval = FAIL;
4223 curwin->w_cursor = old_pos;
4225 else
4227 curwin->w_set_curswant = TRUE;
4228 /* "n" searches forward now */
4229 reset_search_dir();
4232 vim_free(pat);
4233 p_ws = save_p_ws;
4234 p_scs = save_p_scs;
4236 return retval;
4240 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4241 * lines rather than lines in the file.
4242 * 'dist' must be positive.
4244 * Return OK if able to move cursor, FAIL otherwise.
4246 static int
4247 nv_screengo(oap, dir, dist)
4248 oparg_T *oap;
4249 int dir;
4250 long dist;
4252 int linelen = linetabsize(ml_get_curline());
4253 int retval = OK;
4254 int atend = FALSE;
4255 int n;
4256 int col_off1; /* margin offset for first screen line */
4257 int col_off2; /* margin offset for wrapped screen line */
4258 int width1; /* text width for first screen line */
4259 int width2; /* test width for wrapped screen line */
4261 oap->motion_type = MCHAR;
4262 oap->inclusive = FALSE;
4264 col_off1 = curwin_col_off();
4265 col_off2 = col_off1 - curwin_col_off2();
4266 width1 = W_WIDTH(curwin) - col_off1;
4267 width2 = W_WIDTH(curwin) - col_off2;
4269 #ifdef FEAT_VERTSPLIT
4270 if (curwin->w_width != 0)
4272 #endif
4274 * Instead of sticking at the last character of the buffer line we
4275 * try to stick in the last column of the screen.
4277 if (curwin->w_curswant == MAXCOL)
4279 atend = TRUE;
4280 validate_virtcol();
4281 if (width1 <= 0)
4282 curwin->w_curswant = 0;
4283 else
4285 curwin->w_curswant = width1 - 1;
4286 if (curwin->w_virtcol > curwin->w_curswant)
4287 curwin->w_curswant += ((curwin->w_virtcol
4288 - curwin->w_curswant - 1) / width2 + 1) * width2;
4291 else
4293 if (linelen > width1)
4294 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4295 else
4296 n = width1;
4297 if (curwin->w_curswant > (colnr_T)n + 1)
4298 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4299 * width2;
4302 while (dist--)
4304 if (dir == BACKWARD)
4306 if ((long)curwin->w_curswant >= width2)
4307 /* move back within line */
4308 curwin->w_curswant -= width2;
4309 else
4311 /* to previous line */
4312 if (curwin->w_cursor.lnum == 1)
4314 retval = FAIL;
4315 break;
4317 --curwin->w_cursor.lnum;
4318 #ifdef FEAT_FOLDING
4319 /* Move to the start of a closed fold. Don't do that when
4320 * 'foldopen' contains "all": it will open in a moment. */
4321 if (!(fdo_flags & FDO_ALL))
4322 (void)hasFolding(curwin->w_cursor.lnum,
4323 &curwin->w_cursor.lnum, NULL);
4324 #endif
4325 linelen = linetabsize(ml_get_curline());
4326 if (linelen > width1)
4327 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4328 + 1) * width2;
4331 else /* dir == FORWARD */
4333 if (linelen > width1)
4334 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4335 else
4336 n = width1;
4337 if (curwin->w_curswant + width2 < (colnr_T)n)
4338 /* move forward within line */
4339 curwin->w_curswant += width2;
4340 else
4342 /* to next line */
4343 #ifdef FEAT_FOLDING
4344 /* Move to the end of a closed fold. */
4345 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4346 &curwin->w_cursor.lnum);
4347 #endif
4348 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4350 retval = FAIL;
4351 break;
4353 curwin->w_cursor.lnum++;
4354 curwin->w_curswant %= width2;
4358 #ifdef FEAT_VERTSPLIT
4360 #endif
4362 coladvance(curwin->w_curswant);
4364 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4365 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4368 * Check for landing on a character that got split at the end of the
4369 * last line. We want to advance a screenline, not end up in the same
4370 * screenline or move two screenlines.
4372 validate_virtcol();
4373 if (curwin->w_virtcol > curwin->w_curswant
4374 && (curwin->w_curswant < (colnr_T)width1
4375 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4376 : ((curwin->w_curswant - width1) % width2
4377 > (colnr_T)width2 / 2)))
4378 --curwin->w_cursor.col;
4380 #endif
4382 if (atend)
4383 curwin->w_curswant = MAXCOL; /* stick in the last column */
4385 return retval;
4388 #ifdef FEAT_MOUSE
4390 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4391 * when Shift or Ctrl is used.
4392 * K_MOUSEUP (cap->arg == TRUE) or K_MOUSEDOWN (cap->arg == FALSE)
4394 static void
4395 nv_mousescroll(cap)
4396 cmdarg_T *cap;
4398 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4399 win_T *old_curwin = curwin;
4401 /* Currently we only get the mouse coordinates in the GUI. */
4402 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4404 int row, col;
4406 row = mouse_row;
4407 col = mouse_col;
4409 /* find the window at the pointer coordinates */
4410 curwin = mouse_find_win(&row, &col);
4411 curbuf = curwin->w_buffer;
4413 # endif
4415 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4417 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4419 else
4421 cap->count1 = 3;
4422 cap->count0 = 3;
4423 nv_scroll_line(cap);
4426 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4427 curwin->w_redr_status = TRUE;
4429 curwin = old_curwin;
4430 curbuf = curwin->w_buffer;
4431 # endif
4435 * Mouse clicks and drags.
4437 static void
4438 nv_mouse(cap)
4439 cmdarg_T *cap;
4441 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4443 #endif
4446 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4447 * cap->arg must be TRUE for CTRL-E.
4449 static void
4450 nv_scroll_line(cap)
4451 cmdarg_T *cap;
4453 if (!checkclearop(cap->oap))
4454 scroll_redraw(cap->arg, cap->count1);
4458 * Scroll "count" lines up or down, and redraw.
4460 void
4461 scroll_redraw(up, count)
4462 int up;
4463 long count;
4465 linenr_T prev_topline = curwin->w_topline;
4466 #ifdef FEAT_DIFF
4467 int prev_topfill = curwin->w_topfill;
4468 #endif
4469 linenr_T prev_lnum = curwin->w_cursor.lnum;
4471 if (up)
4472 scrollup(count, TRUE);
4473 else
4474 scrolldown(count, TRUE);
4475 if (p_so)
4477 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4478 * valid, otherwise the screen jumps back at the end of the file. */
4479 cursor_correct();
4480 check_cursor_moved(curwin);
4481 curwin->w_valid |= VALID_TOPLINE;
4483 /* If moved back to where we were, at least move the cursor, otherwise
4484 * we get stuck at one position. Don't move the cursor up if the
4485 * first line of the buffer is already on the screen */
4486 while (curwin->w_topline == prev_topline
4487 #ifdef FEAT_DIFF
4488 && curwin->w_topfill == prev_topfill
4489 #endif
4492 if (up)
4494 if (curwin->w_cursor.lnum > prev_lnum
4495 || cursor_down(1L, FALSE) == FAIL)
4496 break;
4498 else
4500 if (curwin->w_cursor.lnum < prev_lnum
4501 || prev_topline == 1L
4502 || cursor_up(1L, FALSE) == FAIL)
4503 break;
4505 /* Mark w_topline as valid, otherwise the screen jumps back at the
4506 * end of the file. */
4507 check_cursor_moved(curwin);
4508 curwin->w_valid |= VALID_TOPLINE;
4511 if (curwin->w_cursor.lnum != prev_lnum)
4512 coladvance(curwin->w_curswant);
4513 redraw_later(VALID);
4517 * Commands that start with "z".
4519 static void
4520 nv_zet(cap)
4521 cmdarg_T *cap;
4523 long n;
4524 colnr_T col;
4525 int nchar = cap->nchar;
4526 #ifdef FEAT_FOLDING
4527 long old_fdl = curwin->w_p_fdl;
4528 int old_fen = curwin->w_p_fen;
4529 #endif
4530 #ifdef FEAT_SPELL
4531 int undo = FALSE;
4532 #endif
4534 if (VIM_ISDIGIT(nchar))
4537 * "z123{nchar}": edit the count before obtaining {nchar}
4539 if (checkclearop(cap->oap))
4540 return;
4541 n = nchar - '0';
4542 for (;;)
4544 #ifdef USE_ON_FLY_SCROLL
4545 dont_scroll = TRUE; /* disallow scrolling here */
4546 #endif
4547 ++no_mapping;
4548 ++allow_keys; /* no mapping for nchar, but allow key codes */
4549 nchar = safe_vgetc();
4550 #ifdef FEAT_LANGMAP
4551 LANGMAP_ADJUST(nchar, TRUE);
4552 #endif
4553 --no_mapping;
4554 --allow_keys;
4555 #ifdef FEAT_CMDL_INFO
4556 (void)add_to_showcmd(nchar);
4557 #endif
4558 if (nchar == K_DEL || nchar == K_KDEL)
4559 n /= 10;
4560 else if (VIM_ISDIGIT(nchar))
4561 n = n * 10 + (nchar - '0');
4562 else if (nchar == CAR)
4564 #ifdef FEAT_GUI
4565 need_mouse_correct = TRUE;
4566 #endif
4567 win_setheight((int)n);
4568 break;
4570 else if (nchar == 'l'
4571 || nchar == 'h'
4572 || nchar == K_LEFT
4573 || nchar == K_RIGHT)
4575 cap->count1 = n ? n * cap->count1 : cap->count1;
4576 goto dozet;
4578 else
4580 clearopbeep(cap->oap);
4581 break;
4584 cap->oap->op_type = OP_NOP;
4585 return;
4588 dozet:
4589 if (
4590 #ifdef FEAT_FOLDING
4591 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4592 * and "zC" only in Visual mode. "zj" and "zk" are motion
4593 * commands. */
4594 cap->nchar != 'f' && cap->nchar != 'F'
4595 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4596 && cap->nchar != 'j' && cap->nchar != 'k'
4598 #endif
4599 checkclearop(cap->oap))
4600 return;
4603 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4604 * If line number given, set cursor.
4606 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4607 && cap->count0
4608 && cap->count0 != curwin->w_cursor.lnum)
4610 setpcmark();
4611 if (cap->count0 > curbuf->b_ml.ml_line_count)
4612 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4613 else
4614 curwin->w_cursor.lnum = cap->count0;
4615 check_cursor_col();
4618 switch (nchar)
4620 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4621 case '+':
4622 if (cap->count0 == 0)
4624 /* No count given: put cursor at the line below screen */
4625 validate_botline(); /* make sure w_botline is valid */
4626 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4627 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4628 else
4629 curwin->w_cursor.lnum = curwin->w_botline;
4631 /* FALLTHROUGH */
4632 case NL:
4633 case CAR:
4634 case K_KENTER:
4635 beginline(BL_WHITE | BL_FIX);
4636 /* FALLTHROUGH */
4638 case 't': scroll_cursor_top(0, TRUE);
4639 redraw_later(VALID);
4640 break;
4642 /* "z." and "zz": put cursor in middle of screen */
4643 case '.': beginline(BL_WHITE | BL_FIX);
4644 /* FALLTHROUGH */
4646 case 'z': scroll_cursor_halfway(TRUE);
4647 redraw_later(VALID);
4648 break;
4650 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4651 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4652 * when <count> is at bottom of window, and puts that one at
4653 * bottom of window. */
4654 if (cap->count0 != 0)
4656 scroll_cursor_bot(0, TRUE);
4657 curwin->w_cursor.lnum = curwin->w_topline;
4659 else if (curwin->w_topline == 1)
4660 curwin->w_cursor.lnum = 1;
4661 else
4662 curwin->w_cursor.lnum = curwin->w_topline - 1;
4663 /* FALLTHROUGH */
4664 case '-':
4665 beginline(BL_WHITE | BL_FIX);
4666 /* FALLTHROUGH */
4668 case 'b': scroll_cursor_bot(0, TRUE);
4669 redraw_later(VALID);
4670 break;
4672 /* "zH" - scroll screen right half-page */
4673 case 'H':
4674 cap->count1 *= W_WIDTH(curwin) / 2;
4675 /* FALLTHROUGH */
4677 /* "zh" - scroll screen to the right */
4678 case 'h':
4679 case K_LEFT:
4680 if (!curwin->w_p_wrap)
4682 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4683 curwin->w_leftcol = 0;
4684 else
4685 curwin->w_leftcol -= (colnr_T)cap->count1;
4686 leftcol_changed();
4688 break;
4690 /* "zL" - scroll screen left half-page */
4691 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4692 /* FALLTHROUGH */
4694 /* "zl" - scroll screen to the left */
4695 case 'l':
4696 case K_RIGHT:
4697 if (!curwin->w_p_wrap)
4699 /* scroll the window left */
4700 curwin->w_leftcol += (colnr_T)cap->count1;
4701 leftcol_changed();
4703 break;
4705 /* "zs" - scroll screen, cursor at the start */
4706 case 's': if (!curwin->w_p_wrap)
4708 #ifdef FEAT_FOLDING
4709 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4710 col = 0; /* like the cursor is in col 0 */
4711 else
4712 #endif
4713 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4714 if ((long)col > p_siso)
4715 col -= p_siso;
4716 else
4717 col = 0;
4718 if (curwin->w_leftcol != col)
4720 curwin->w_leftcol = col;
4721 redraw_later(NOT_VALID);
4724 break;
4726 /* "ze" - scroll screen, cursor at the end */
4727 case 'e': if (!curwin->w_p_wrap)
4729 #ifdef FEAT_FOLDING
4730 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4731 col = 0; /* like the cursor is in col 0 */
4732 else
4733 #endif
4734 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4735 n = W_WIDTH(curwin) - curwin_col_off();
4736 if ((long)col + p_siso < n)
4737 col = 0;
4738 else
4739 col = col + p_siso - n + 1;
4740 if (curwin->w_leftcol != col)
4742 curwin->w_leftcol = col;
4743 redraw_later(NOT_VALID);
4746 break;
4748 #ifdef FEAT_FOLDING
4749 /* "zF": create fold command */
4750 /* "zf": create fold operator */
4751 case 'F':
4752 case 'f': if (foldManualAllowed(TRUE))
4754 cap->nchar = 'f';
4755 nv_operator(cap);
4756 curwin->w_p_fen = TRUE;
4758 /* "zF" is like "zfzf" */
4759 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4761 nv_operator(cap);
4762 finish_op = TRUE;
4765 else
4766 clearopbeep(cap->oap);
4767 break;
4769 /* "zd": delete fold at cursor */
4770 /* "zD": delete fold at cursor recursively */
4771 case 'd':
4772 case 'D': if (foldManualAllowed(FALSE))
4774 if (VIsual_active)
4775 nv_operator(cap);
4776 else
4777 deleteFold(curwin->w_cursor.lnum,
4778 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4780 break;
4782 /* "zE": erease all folds */
4783 case 'E': if (foldmethodIsManual(curwin))
4785 clearFolding(curwin);
4786 changed_window_setting();
4788 else if (foldmethodIsMarker(curwin))
4789 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4790 TRUE, FALSE);
4791 else
4792 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4793 break;
4795 /* "zn": fold none: reset 'foldenable' */
4796 case 'n': curwin->w_p_fen = FALSE;
4797 break;
4799 /* "zN": fold Normal: set 'foldenable' */
4800 case 'N': curwin->w_p_fen = TRUE;
4801 break;
4803 /* "zi": invert folding: toggle 'foldenable' */
4804 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4805 break;
4807 /* "za": open closed fold or close open fold at cursor */
4808 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4809 openFold(curwin->w_cursor.lnum, cap->count1);
4810 else
4812 closeFold(curwin->w_cursor.lnum, cap->count1);
4813 curwin->w_p_fen = TRUE;
4815 break;
4817 /* "zA": open fold at cursor recursively */
4818 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4819 openFoldRecurse(curwin->w_cursor.lnum);
4820 else
4822 closeFoldRecurse(curwin->w_cursor.lnum);
4823 curwin->w_p_fen = TRUE;
4825 break;
4827 /* "zo": open fold at cursor or Visual area */
4828 case 'o': if (VIsual_active)
4829 nv_operator(cap);
4830 else
4831 openFold(curwin->w_cursor.lnum, cap->count1);
4832 break;
4834 /* "zO": open fold recursively */
4835 case 'O': if (VIsual_active)
4836 nv_operator(cap);
4837 else
4838 openFoldRecurse(curwin->w_cursor.lnum);
4839 break;
4841 /* "zc": close fold at cursor or Visual area */
4842 case 'c': if (VIsual_active)
4843 nv_operator(cap);
4844 else
4845 closeFold(curwin->w_cursor.lnum, cap->count1);
4846 curwin->w_p_fen = TRUE;
4847 break;
4849 /* "zC": close fold recursively */
4850 case 'C': if (VIsual_active)
4851 nv_operator(cap);
4852 else
4853 closeFoldRecurse(curwin->w_cursor.lnum);
4854 curwin->w_p_fen = TRUE;
4855 break;
4857 /* "zv": open folds at the cursor */
4858 case 'v': foldOpenCursor();
4859 break;
4861 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
4862 case 'x': curwin->w_p_fen = TRUE;
4863 newFoldLevel(); /* update right now */
4864 foldOpenCursor();
4865 break;
4867 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
4868 case 'X': curwin->w_p_fen = TRUE;
4869 old_fdl = -1; /* force an update */
4870 break;
4872 /* "zm": fold more */
4873 case 'm': if (curwin->w_p_fdl > 0)
4874 --curwin->w_p_fdl;
4875 old_fdl = -1; /* force an update */
4876 curwin->w_p_fen = TRUE;
4877 break;
4879 /* "zM": close all folds */
4880 case 'M': curwin->w_p_fdl = 0;
4881 old_fdl = -1; /* force an update */
4882 curwin->w_p_fen = TRUE;
4883 break;
4885 /* "zr": reduce folding */
4886 case 'r': ++curwin->w_p_fdl;
4887 break;
4889 /* "zR": open all folds */
4890 case 'R': curwin->w_p_fdl = getDeepestNesting();
4891 old_fdl = -1; /* force an update */
4892 break;
4894 case 'j': /* "zj" move to next fold downwards */
4895 case 'k': /* "zk" move to next fold upwards */
4896 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
4897 cap->count1) == FAIL)
4898 clearopbeep(cap->oap);
4899 break;
4901 #endif /* FEAT_FOLDING */
4903 #ifdef FEAT_SPELL
4904 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
4905 ++no_mapping;
4906 ++allow_keys; /* no mapping for nchar, but allow key codes */
4907 nchar = safe_vgetc();
4908 #ifdef FEAT_LANGMAP
4909 LANGMAP_ADJUST(nchar, TRUE);
4910 #endif
4911 --no_mapping;
4912 --allow_keys;
4913 #ifdef FEAT_CMDL_INFO
4914 (void)add_to_showcmd(nchar);
4915 #endif
4916 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
4918 clearopbeep(cap->oap);
4919 break;
4921 undo = TRUE;
4922 /*FALLTHROUGH*/
4924 case 'g': /* "zg": add good word to word list */
4925 case 'w': /* "zw": add wrong word to word list */
4926 case 'G': /* "zG": add good word to temp word list */
4927 case 'W': /* "zW": add wrong word to temp word list */
4929 char_u *ptr = NULL;
4930 int len;
4932 if (checkclearop(cap->oap))
4933 break;
4934 # ifdef FEAT_VISUAL
4935 if (VIsual_active && get_visual_text(cap, &ptr, &len)
4936 == FAIL)
4937 return;
4938 # endif
4939 if (ptr == NULL)
4941 pos_T pos = curwin->w_cursor;
4943 /* Find bad word under the cursor. */
4944 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
4945 if (len != 0 && curwin->w_cursor.col <= pos.col)
4946 ptr = ml_get_pos(&curwin->w_cursor);
4947 curwin->w_cursor = pos;
4950 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
4951 FIND_IDENT)) == 0)
4952 return;
4953 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
4954 (nchar == 'G' || nchar == 'W')
4955 ? 0 : (int)cap->count1,
4956 undo);
4958 break;
4960 case '=': /* "z=": suggestions for a badly spelled word */
4961 if (!checkclearop(cap->oap))
4962 spell_suggest((int)cap->count0);
4963 break;
4964 #endif
4966 default: clearopbeep(cap->oap);
4969 #ifdef FEAT_FOLDING
4970 /* Redraw when 'foldenable' changed */
4971 if (old_fen != curwin->w_p_fen)
4973 # ifdef FEAT_DIFF
4974 win_T *wp;
4976 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
4978 /* Adjust 'foldenable' in diff-synced windows. */
4979 FOR_ALL_WINDOWS(wp)
4981 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
4983 wp->w_p_fen = curwin->w_p_fen;
4984 changed_window_setting_win(wp);
4988 # endif
4989 changed_window_setting();
4992 /* Redraw when 'foldlevel' changed. */
4993 if (old_fdl != curwin->w_p_fdl)
4994 newFoldLevel();
4995 #endif
4998 #ifdef FEAT_GUI
5000 * Vertical scrollbar movement.
5002 static void
5003 nv_ver_scrollbar(cap)
5004 cmdarg_T *cap;
5006 if (cap->oap->op_type != OP_NOP)
5007 clearopbeep(cap->oap);
5009 /* Even if an operator was pending, we still want to scroll */
5010 gui_do_scroll();
5014 * Horizontal scrollbar movement.
5016 static void
5017 nv_hor_scrollbar(cap)
5018 cmdarg_T *cap;
5020 if (cap->oap->op_type != OP_NOP)
5021 clearopbeep(cap->oap);
5023 /* Even if an operator was pending, we still want to scroll */
5024 gui_do_horiz_scroll();
5026 #endif
5028 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
5030 * Click in GUI tab.
5032 static void
5033 nv_tabline(cap)
5034 cmdarg_T *cap;
5036 if (cap->oap->op_type != OP_NOP)
5037 clearopbeep(cap->oap);
5039 /* Even if an operator was pending, we still want to jump tabs. */
5040 goto_tabpage(current_tab);
5044 * Selected item in tab line menu.
5046 static void
5047 nv_tabmenu(cap)
5048 cmdarg_T *cap;
5050 if (cap->oap->op_type != OP_NOP)
5051 clearopbeep(cap->oap);
5053 /* Even if an operator was pending, we still want to jump tabs. */
5054 handle_tabmenu();
5058 * Handle selecting an item of the GUI tab line menu.
5059 * Used in Normal and Insert mode.
5061 void
5062 handle_tabmenu()
5064 switch (current_tabmenu)
5066 case TABLINE_MENU_CLOSE:
5067 if (current_tab == 0)
5068 do_cmdline_cmd((char_u *)"tabclose");
5069 else
5071 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5072 current_tab);
5073 do_cmdline_cmd(IObuff);
5075 break;
5077 case TABLINE_MENU_NEW:
5078 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5079 current_tab > 0 ? current_tab - 1 : 999);
5080 do_cmdline_cmd(IObuff);
5081 break;
5083 case TABLINE_MENU_OPEN:
5084 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5085 current_tab > 0 ? current_tab - 1 : 999);
5086 do_cmdline_cmd(IObuff);
5087 break;
5090 #endif
5093 * "Q" command.
5095 static void
5096 nv_exmode(cap)
5097 cmdarg_T *cap;
5100 * Ignore 'Q' in Visual mode, just give a beep.
5102 #ifdef FEAT_VISUAL
5103 if (VIsual_active)
5104 vim_beep();
5105 else
5106 #endif
5107 if (!checkclearop(cap->oap))
5108 do_exmode(FALSE);
5112 * Handle a ":" command.
5114 static void
5115 nv_colon(cap)
5116 cmdarg_T *cap;
5118 int old_p_im;
5120 #ifdef FEAT_VISUAL
5121 if (VIsual_active)
5122 nv_operator(cap);
5123 else
5124 #endif
5126 if (cap->oap->op_type != OP_NOP)
5128 /* Using ":" as a movement is characterwise exclusive. */
5129 cap->oap->motion_type = MCHAR;
5130 cap->oap->inclusive = FALSE;
5132 else if (cap->count0)
5134 /* translate "count:" into ":.,.+(count - 1)" */
5135 stuffcharReadbuff('.');
5136 if (cap->count0 > 1)
5138 stuffReadbuff((char_u *)",.+");
5139 stuffnumReadbuff((long)cap->count0 - 1L);
5143 /* When typing, don't type below an old message */
5144 if (KeyTyped)
5145 compute_cmdrow();
5147 old_p_im = p_im;
5149 /* get a command line and execute it */
5150 do_cmdline(NULL, getexline, NULL,
5151 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5153 /* If 'insertmode' changed, enter or exit Insert mode */
5154 if (p_im != old_p_im)
5156 if (p_im)
5157 restart_edit = 'i';
5158 else
5159 restart_edit = 0;
5162 /* The start of the operator may have become invalid by the Ex
5163 * command. */
5164 if (cap->oap->op_type != OP_NOP
5165 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5166 || cap->oap->start.col >
5167 STRLEN(ml_get(cap->oap->start.lnum))))
5168 clearopbeep(cap->oap);
5173 * Handle CTRL-G command.
5175 static void
5176 nv_ctrlg(cap)
5177 cmdarg_T *cap;
5179 #ifdef FEAT_VISUAL
5180 if (VIsual_active) /* toggle Selection/Visual mode */
5182 VIsual_select = !VIsual_select;
5183 showmode();
5185 else
5186 #endif
5187 if (!checkclearop(cap->oap))
5188 /* print full name if count given or :cd used */
5189 fileinfo((int)cap->count0, FALSE, TRUE);
5193 * Handle CTRL-H <Backspace> command.
5195 static void
5196 nv_ctrlh(cap)
5197 cmdarg_T *cap;
5199 #ifdef FEAT_VISUAL
5200 if (VIsual_active && VIsual_select)
5202 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5203 v_visop(cap);
5205 else
5206 #endif
5207 nv_left(cap);
5211 * CTRL-L: clear screen and redraw.
5213 static void
5214 nv_clear(cap)
5215 cmdarg_T *cap;
5217 if (!checkclearop(cap->oap))
5219 #if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5221 * Right now, the BeBox doesn't seem to have an easy way to detect
5222 * window resizing, so we cheat and make the user detect it
5223 * manually with CTRL-L instead
5225 ui_get_shellsize();
5226 #endif
5227 #ifdef FEAT_SYN_HL
5228 /* Clear all syntax states to force resyncing. */
5229 syn_stack_free_all(curbuf);
5230 #endif
5231 redraw_later(CLEAR);
5236 * CTRL-O: In Select mode: switch to Visual mode for one command.
5237 * Otherwise: Go to older pcmark.
5239 static void
5240 nv_ctrlo(cap)
5241 cmdarg_T *cap;
5243 #ifdef FEAT_VISUAL
5244 if (VIsual_active && VIsual_select)
5246 VIsual_select = FALSE;
5247 showmode();
5248 restart_VIsual_select = 2; /* restart Select mode later */
5250 else
5251 #endif
5253 cap->count1 = -cap->count1;
5254 nv_pcmark(cap);
5259 * CTRL-^ command, short for ":e #"
5261 static void
5262 nv_hat(cap)
5263 cmdarg_T *cap;
5265 if (!checkclearopq(cap->oap))
5266 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5267 GETF_SETMARK|GETF_ALT, FALSE);
5271 * "Z" commands.
5273 static void
5274 nv_Zet(cap)
5275 cmdarg_T *cap;
5277 if (!checkclearopq(cap->oap))
5279 switch (cap->nchar)
5281 /* "ZZ": equivalent to ":x". */
5282 case 'Z': do_cmdline_cmd((char_u *)"x");
5283 break;
5285 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5286 case 'Q': do_cmdline_cmd((char_u *)"q!");
5287 break;
5289 default: clearopbeep(cap->oap);
5294 #if defined(FEAT_WINDOWS) || defined(PROTO)
5296 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5298 void
5299 do_nv_ident(c1, c2)
5300 int c1;
5301 int c2;
5303 oparg_T oa;
5304 cmdarg_T ca;
5306 clear_oparg(&oa);
5307 vim_memset(&ca, 0, sizeof(ca));
5308 ca.oap = &oa;
5309 ca.cmdchar = c1;
5310 ca.nchar = c2;
5311 nv_ident(&ca);
5313 #endif
5316 * Handle the commands that use the word under the cursor.
5317 * [g] CTRL-] :ta to current identifier
5318 * [g] 'K' run program for current identifier
5319 * [g] '*' / to current identifier or string
5320 * [g] '#' ? to current identifier or string
5321 * g ']' :tselect for current identifier
5323 static void
5324 nv_ident(cap)
5325 cmdarg_T *cap;
5327 char_u *ptr = NULL;
5328 char_u *buf;
5329 char_u *p;
5330 char_u *kp; /* value of 'keywordprg' */
5331 int kp_help; /* 'keywordprg' is ":help" */
5332 int n = 0; /* init for GCC */
5333 int cmdchar;
5334 int g_cmd; /* "g" command */
5335 char_u *aux_ptr;
5336 int isman;
5337 int isman_s;
5339 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5341 cmdchar = cap->nchar;
5342 g_cmd = TRUE;
5344 else
5346 cmdchar = cap->cmdchar;
5347 g_cmd = FALSE;
5350 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5351 cmdchar = '#';
5354 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5356 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5358 #ifdef FEAT_VISUAL
5359 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5360 return;
5361 #endif
5362 if (checkclearopq(cap->oap))
5363 return;
5366 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5367 (cmdchar == '*' || cmdchar == '#')
5368 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5370 clearop(cap->oap);
5371 return;
5374 /* Allocate buffer to put the command in. Inserting backslashes can
5375 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5376 * and some numbers. */
5377 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5378 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5379 || STRCMP(kp, ":help") == 0);
5380 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5381 if (buf == NULL)
5382 return;
5383 buf[0] = NUL;
5385 switch (cmdchar)
5387 case '*':
5388 case '#':
5390 * Put cursor at start of word, makes search skip the word
5391 * under the cursor.
5392 * Call setpcmark() first, so "*``" puts the cursor back where
5393 * it was.
5395 setpcmark();
5396 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5398 if (!g_cmd && vim_iswordp(ptr))
5399 STRCPY(buf, "\\<");
5400 no_smartcase = TRUE; /* don't use 'smartcase' now */
5401 break;
5403 case 'K':
5404 if (kp_help)
5405 STRCPY(buf, "he! ");
5406 else
5408 /* When a count is given, turn it into a range. Is this
5409 * really what we want? */
5410 isman = (STRCMP(kp, "man") == 0);
5411 isman_s = (STRCMP(kp, "man -s") == 0);
5412 if (cap->count0 != 0 && !(isman || isman_s))
5413 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5415 STRCAT(buf, "! ");
5416 if (cap->count0 == 0 && isman_s)
5417 STRCAT(buf, "man");
5418 else
5419 STRCAT(buf, kp);
5420 STRCAT(buf, " ");
5421 if (cap->count0 != 0 && (isman || isman_s))
5423 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5424 STRCAT(buf, " ");
5427 break;
5429 case ']':
5430 #ifdef FEAT_CSCOPE
5431 if (p_cst)
5432 STRCPY(buf, "cstag ");
5433 else
5434 #endif
5435 STRCPY(buf, "ts ");
5436 break;
5438 default:
5439 if (curbuf->b_help)
5440 STRCPY(buf, "he! ");
5441 else if (g_cmd)
5442 STRCPY(buf, "tj ");
5443 else
5444 sprintf((char *)buf, "%ldta ", cap->count0);
5448 * Now grab the chars in the identifier
5450 if (cmdchar == '*')
5451 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5452 else if (cmdchar == '#')
5453 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
5454 else if (cmdchar == 'K' && !kp_help)
5455 aux_ptr = (char_u *)" \t\\\"|!";
5456 else
5457 /* Don't escape spaces and Tabs in a tag with a backslash */
5458 aux_ptr = (char_u *)"\\|\"";
5460 p = buf + STRLEN(buf);
5461 while (n-- > 0)
5463 /* put a backslash before \ and some others */
5464 if (vim_strchr(aux_ptr, *ptr) != NULL)
5465 *p++ = '\\';
5466 #ifdef FEAT_MBYTE
5467 /* When current byte is a part of multibyte character, copy all bytes
5468 * of that character. */
5469 if (has_mbyte)
5471 int i;
5472 int len = (*mb_ptr2len)(ptr) - 1;
5474 for (i = 0; i < len && n >= 1; ++i, --n)
5475 *p++ = *ptr++;
5477 #endif
5478 *p++ = *ptr++;
5480 *p = NUL;
5483 * Execute the command.
5485 if (cmdchar == '*' || cmdchar == '#')
5487 if (!g_cmd && (
5488 #ifdef FEAT_MBYTE
5489 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5490 #endif
5491 vim_iswordc(ptr[-1])))
5492 STRCAT(buf, "\\>");
5493 #ifdef FEAT_CMDHIST
5494 /* put pattern in search history */
5495 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5496 #endif
5497 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5499 else
5500 do_cmdline_cmd(buf);
5502 vim_free(buf);
5505 #if defined(FEAT_VISUAL) || defined(PROTO)
5507 * Get visually selected text, within one line only.
5508 * Returns FAIL if more than one line selected.
5511 get_visual_text(cap, pp, lenp)
5512 cmdarg_T *cap;
5513 char_u **pp; /* return: start of selected text */
5514 int *lenp; /* return: length of selected text */
5516 if (VIsual_mode != 'V')
5517 unadjust_for_sel();
5518 if (VIsual.lnum != curwin->w_cursor.lnum)
5520 if (cap != NULL)
5521 clearopbeep(cap->oap);
5522 return FAIL;
5524 if (VIsual_mode == 'V')
5526 *pp = ml_get_curline();
5527 *lenp = (int)STRLEN(*pp);
5529 else
5531 if (lt(curwin->w_cursor, VIsual))
5533 *pp = ml_get_pos(&curwin->w_cursor);
5534 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5536 else
5538 *pp = ml_get_pos(&VIsual);
5539 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5541 #ifdef FEAT_MBYTE
5542 if (has_mbyte)
5543 /* Correct the length to include the whole last character. */
5544 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
5545 #endif
5547 reset_VIsual_and_resel();
5548 return OK;
5550 #endif
5553 * CTRL-T: backwards in tag stack
5555 static void
5556 nv_tagpop(cap)
5557 cmdarg_T *cap;
5559 if (!checkclearopq(cap->oap))
5560 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5564 * Handle scrolling command 'H', 'L' and 'M'.
5566 static void
5567 nv_scroll(cap)
5568 cmdarg_T *cap;
5570 int used = 0;
5571 long n;
5572 #ifdef FEAT_FOLDING
5573 linenr_T lnum;
5574 #endif
5575 int half;
5577 cap->oap->motion_type = MLINE;
5578 setpcmark();
5580 if (cap->cmdchar == 'L')
5582 validate_botline(); /* make sure curwin->w_botline is valid */
5583 curwin->w_cursor.lnum = curwin->w_botline - 1;
5584 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5585 curwin->w_cursor.lnum = 1;
5586 else
5588 #ifdef FEAT_FOLDING
5589 if (hasAnyFolding(curwin))
5591 /* Count a fold for one screen line. */
5592 for (n = cap->count1 - 1; n > 0
5593 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5595 (void)hasFolding(curwin->w_cursor.lnum,
5596 &curwin->w_cursor.lnum, NULL);
5597 --curwin->w_cursor.lnum;
5600 else
5601 #endif
5602 curwin->w_cursor.lnum -= cap->count1 - 1;
5605 else
5607 if (cap->cmdchar == 'M')
5609 #ifdef FEAT_DIFF
5610 /* Don't count filler lines above the window. */
5611 used -= diff_check_fill(curwin, curwin->w_topline)
5612 - curwin->w_topfill;
5613 #endif
5614 validate_botline(); /* make sure w_empty_rows is valid */
5615 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5616 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5618 #ifdef FEAT_DIFF
5619 /* Count half he number of filler lines to be "below this
5620 * line" and half to be "above the next line". */
5621 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5622 + n) / 2 >= half)
5624 --n;
5625 break;
5627 #endif
5628 used += plines(curwin->w_topline + n);
5629 if (used >= half)
5630 break;
5631 #ifdef FEAT_FOLDING
5632 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5633 n = lnum - curwin->w_topline;
5634 #endif
5636 if (n > 0 && used > curwin->w_height)
5637 --n;
5639 else /* (cap->cmdchar == 'H') */
5641 n = cap->count1 - 1;
5642 #ifdef FEAT_FOLDING
5643 if (hasAnyFolding(curwin))
5645 /* Count a fold for one screen line. */
5646 lnum = curwin->w_topline;
5647 while (n-- > 0 && lnum < curwin->w_botline - 1)
5649 hasFolding(lnum, NULL, &lnum);
5650 ++lnum;
5652 n = lnum - curwin->w_topline;
5654 #endif
5656 curwin->w_cursor.lnum = curwin->w_topline + n;
5657 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5658 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5661 cursor_correct(); /* correct for 'so' */
5662 beginline(BL_SOL | BL_FIX);
5666 * Cursor right commands.
5668 static void
5669 nv_right(cap)
5670 cmdarg_T *cap;
5672 long n;
5673 #ifdef FEAT_VISUAL
5674 int PAST_LINE;
5675 #else
5676 # define PAST_LINE 0
5677 #endif
5679 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5681 /* <C-Right> and <S-Right> move a word or WORD right */
5682 if (mod_mask & MOD_MASK_CTRL)
5683 cap->arg = TRUE;
5684 nv_wordcmd(cap);
5685 return;
5688 cap->oap->motion_type = MCHAR;
5689 cap->oap->inclusive = FALSE;
5690 #ifdef FEAT_VISUAL
5691 PAST_LINE = (VIsual_active && *p_sel != 'o');
5693 # ifdef FEAT_VIRTUALEDIT
5695 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
5696 * (theoretically) infinitly long.
5698 if (virtual_active())
5699 PAST_LINE = 0;
5700 # endif
5701 #endif
5703 for (n = cap->count1; n > 0; --n)
5705 if ((!PAST_LINE && oneright() == FAIL)
5706 || (PAST_LINE && *ml_get_cursor() == NUL))
5709 * <Space> wraps to next line if 'whichwrap' has 's'.
5710 * 'l' wraps to next line if 'whichwrap' has 'l'.
5711 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
5713 if ( ((cap->cmdchar == ' '
5714 && vim_strchr(p_ww, 's') != NULL)
5715 || (cap->cmdchar == 'l'
5716 && vim_strchr(p_ww, 'l') != NULL)
5717 || (cap->cmdchar == K_RIGHT
5718 && vim_strchr(p_ww, '>') != NULL))
5719 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5721 /* When deleting we also count the NL as a character.
5722 * Set cap->oap->inclusive when last char in the line is
5723 * included, move to next line after that */
5724 if ( cap->oap->op_type != OP_NOP
5725 && !cap->oap->inclusive
5726 && !lineempty(curwin->w_cursor.lnum))
5727 cap->oap->inclusive = TRUE;
5728 else
5730 ++curwin->w_cursor.lnum;
5731 curwin->w_cursor.col = 0;
5732 #ifdef FEAT_VIRTUALEDIT
5733 curwin->w_cursor.coladd = 0;
5734 #endif
5735 curwin->w_set_curswant = TRUE;
5736 cap->oap->inclusive = FALSE;
5738 continue;
5740 if (cap->oap->op_type == OP_NOP)
5742 /* Only beep and flush if not moved at all */
5743 if (n == cap->count1)
5744 beep_flush();
5746 else
5748 if (!lineempty(curwin->w_cursor.lnum))
5749 cap->oap->inclusive = TRUE;
5751 break;
5753 #ifdef FEAT_VISUAL
5754 else if (PAST_LINE)
5756 curwin->w_set_curswant = TRUE;
5757 # ifdef FEAT_VIRTUALEDIT
5758 if (virtual_active())
5759 oneright();
5760 else
5761 # endif
5763 # ifdef FEAT_MBYTE
5764 if (has_mbyte)
5765 curwin->w_cursor.col +=
5766 (*mb_ptr2len)(ml_get_cursor());
5767 else
5768 # endif
5769 ++curwin->w_cursor.col;
5772 #endif
5774 #ifdef FEAT_FOLDING
5775 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5776 && cap->oap->op_type == OP_NOP)
5777 foldOpenCursor();
5778 #endif
5782 * Cursor left commands.
5784 * Returns TRUE when operator end should not be adjusted.
5786 static void
5787 nv_left(cap)
5788 cmdarg_T *cap;
5790 long n;
5792 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5794 /* <C-Left> and <S-Left> move a word or WORD left */
5795 if (mod_mask & MOD_MASK_CTRL)
5796 cap->arg = 1;
5797 nv_bck_word(cap);
5798 return;
5801 cap->oap->motion_type = MCHAR;
5802 cap->oap->inclusive = FALSE;
5803 for (n = cap->count1; n > 0; --n)
5805 if (oneleft() == FAIL)
5807 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
5808 * 'h' wraps to previous line if 'whichwrap' has 'h'.
5809 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
5811 if ( (((cap->cmdchar == K_BS
5812 || cap->cmdchar == Ctrl_H)
5813 && vim_strchr(p_ww, 'b') != NULL)
5814 || (cap->cmdchar == 'h'
5815 && vim_strchr(p_ww, 'h') != NULL)
5816 || (cap->cmdchar == K_LEFT
5817 && vim_strchr(p_ww, '<') != NULL))
5818 && curwin->w_cursor.lnum > 1)
5820 --(curwin->w_cursor.lnum);
5821 coladvance((colnr_T)MAXCOL);
5822 curwin->w_set_curswant = TRUE;
5824 /* When the NL before the first char has to be deleted we
5825 * put the cursor on the NUL after the previous line.
5826 * This is a very special case, be careful!
5827 * don't adjust op_end now, otherwise it won't work */
5828 if ( (cap->oap->op_type == OP_DELETE
5829 || cap->oap->op_type == OP_CHANGE)
5830 && !lineempty(curwin->w_cursor.lnum))
5832 ++curwin->w_cursor.col;
5833 cap->retval |= CA_NO_ADJ_OP_END;
5835 continue;
5837 /* Only beep and flush if not moved at all */
5838 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
5839 beep_flush();
5840 break;
5843 #ifdef FEAT_FOLDING
5844 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5845 && cap->oap->op_type == OP_NOP)
5846 foldOpenCursor();
5847 #endif
5851 * Cursor up commands.
5852 * cap->arg is TRUE for "-": Move cursor to first non-blank.
5854 static void
5855 nv_up(cap)
5856 cmdarg_T *cap;
5858 if (mod_mask & MOD_MASK_SHIFT)
5860 /* <S-Up> is page up */
5861 cap->arg = BACKWARD;
5862 nv_page(cap);
5864 else
5866 cap->oap->motion_type = MLINE;
5867 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5868 clearopbeep(cap->oap);
5869 else if (cap->arg)
5870 beginline(BL_WHITE | BL_FIX);
5875 * Cursor down commands.
5876 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
5878 static void
5879 nv_down(cap)
5880 cmdarg_T *cap;
5882 if (mod_mask & MOD_MASK_SHIFT)
5884 /* <S-Down> is page down */
5885 cap->arg = FORWARD;
5886 nv_page(cap);
5888 else
5889 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5890 /* In a quickfix window a <CR> jumps to the error under the cursor. */
5891 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
5892 if (curwin->w_llist_ref == NULL)
5893 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
5894 else
5895 do_cmdline_cmd((char_u *)".ll"); /* location list window */
5896 else
5897 #endif
5899 #ifdef FEAT_CMDWIN
5900 /* In the cmdline window a <CR> executes the command. */
5901 if (cmdwin_type != 0 && cap->cmdchar == CAR)
5902 cmdwin_result = CAR;
5903 else
5904 #endif
5906 cap->oap->motion_type = MLINE;
5907 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5908 clearopbeep(cap->oap);
5909 else if (cap->arg)
5910 beginline(BL_WHITE | BL_FIX);
5915 #ifdef FEAT_SEARCHPATH
5917 * Grab the file name under the cursor and edit it.
5919 static void
5920 nv_gotofile(cap)
5921 cmdarg_T *cap;
5923 char_u *ptr;
5924 linenr_T lnum = -1;
5926 if (text_locked())
5928 clearopbeep(cap->oap);
5929 text_locked_msg();
5930 return;
5932 #ifdef FEAT_AUTOCMD
5933 if (curbuf_locked())
5935 clearop(cap->oap);
5936 return;
5938 #endif
5940 ptr = grab_file_name(cap->count1, &lnum);
5942 if (ptr != NULL)
5944 /* do autowrite if necessary */
5945 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
5946 autowrite(curbuf, FALSE);
5947 setpcmark();
5948 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
5949 P_HID(curbuf) ? ECMD_HIDE : 0);
5950 if (cap->nchar == 'F' && lnum >= 0)
5952 curwin->w_cursor.lnum = lnum;
5953 check_cursor_lnum();
5954 beginline(BL_SOL | BL_FIX);
5956 vim_free(ptr);
5958 else
5959 clearop(cap->oap);
5961 #endif
5964 * <End> command: to end of current line or last line.
5966 static void
5967 nv_end(cap)
5968 cmdarg_T *cap;
5970 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
5972 cap->arg = TRUE;
5973 nv_goto(cap);
5974 cap->count1 = 1; /* to end of current line */
5976 nv_dollar(cap);
5980 * Handle the "$" command.
5982 static void
5983 nv_dollar(cap)
5984 cmdarg_T *cap;
5986 cap->oap->motion_type = MCHAR;
5987 cap->oap->inclusive = TRUE;
5988 #ifdef FEAT_VIRTUALEDIT
5989 /* In virtual mode when off the edge of a line and an operator
5990 * is pending (whew!) keep the cursor where it is.
5991 * Otherwise, send it to the end of the line. */
5992 if (!virtual_active() || gchar_cursor() != NUL
5993 || cap->oap->op_type == OP_NOP)
5994 #endif
5995 curwin->w_curswant = MAXCOL; /* so we stay at the end */
5996 if (cursor_down((long)(cap->count1 - 1),
5997 cap->oap->op_type == OP_NOP) == FAIL)
5998 clearopbeep(cap->oap);
5999 #ifdef FEAT_FOLDING
6000 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6001 foldOpenCursor();
6002 #endif
6006 * Implementation of '?' and '/' commands.
6007 * If cap->arg is TRUE don't set PC mark.
6009 static void
6010 nv_search(cap)
6011 cmdarg_T *cap;
6013 oparg_T *oap = cap->oap;
6015 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6017 /* Translate "g??" to "g?g?" */
6018 cap->cmdchar = 'g';
6019 cap->nchar = '?';
6020 nv_operator(cap);
6021 return;
6024 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6026 if (cap->searchbuf == NULL)
6028 clearop(oap);
6029 return;
6032 normal_search(cap, cap->cmdchar, cap->searchbuf,
6033 (cap->arg ? 0 : SEARCH_MARK));
6037 * Handle "N" and "n" commands.
6038 * cap->arg is SEARCH_REV for "N", 0 for "n".
6040 static void
6041 nv_next(cap)
6042 cmdarg_T *cap;
6044 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6048 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6049 * Uses only cap->count1 and cap->oap from "cap".
6051 static void
6052 normal_search(cap, dir, pat, opt)
6053 cmdarg_T *cap;
6054 int dir;
6055 char_u *pat;
6056 int opt; /* extra flags for do_search() */
6058 int i;
6060 cap->oap->motion_type = MCHAR;
6061 cap->oap->inclusive = FALSE;
6062 cap->oap->use_reg_one = TRUE;
6063 curwin->w_set_curswant = TRUE;
6065 i = do_search(cap->oap, dir, pat, cap->count1,
6066 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG);
6067 if (i == 0)
6068 clearop(cap->oap);
6069 else
6071 if (i == 2)
6072 cap->oap->motion_type = MLINE;
6073 #ifdef FEAT_VIRTUALEDIT
6074 curwin->w_cursor.coladd = 0;
6075 #endif
6076 #ifdef FEAT_FOLDING
6077 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6078 foldOpenCursor();
6079 #endif
6082 /* "/$" will put the cursor after the end of the line, may need to
6083 * correct that here */
6084 check_cursor();
6088 * Character search commands.
6089 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6090 * ',' and FALSE for ';'.
6091 * cap->nchar is NUL for ',' and ';' (repeat the search)
6093 static void
6094 nv_csearch(cap)
6095 cmdarg_T *cap;
6097 int t_cmd;
6099 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6100 t_cmd = TRUE;
6101 else
6102 t_cmd = FALSE;
6104 cap->oap->motion_type = MCHAR;
6105 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6106 clearopbeep(cap->oap);
6107 else
6109 curwin->w_set_curswant = TRUE;
6110 #ifdef FEAT_VIRTUALEDIT
6111 /* Include a Tab for "tx" and for "dfx". */
6112 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6113 && (t_cmd || cap->oap->op_type != OP_NOP))
6115 colnr_T scol, ecol;
6117 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6118 curwin->w_cursor.coladd = ecol - scol;
6120 else
6121 curwin->w_cursor.coladd = 0;
6122 #endif
6123 #ifdef FEAT_VISUAL
6124 adjust_for_sel(cap);
6125 #endif
6126 #ifdef FEAT_FOLDING
6127 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6128 foldOpenCursor();
6129 #endif
6134 * "[" and "]" commands.
6135 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6137 static void
6138 nv_brackets(cap)
6139 cmdarg_T *cap;
6141 pos_T new_pos;
6142 pos_T prev_pos;
6143 pos_T *pos = NULL; /* init for GCC */
6144 pos_T old_pos; /* cursor position before command */
6145 int flag;
6146 long n;
6147 int findc;
6148 int c;
6150 cap->oap->motion_type = MCHAR;
6151 cap->oap->inclusive = FALSE;
6152 old_pos = curwin->w_cursor;
6153 #ifdef FEAT_VIRTUALEDIT
6154 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6155 #endif
6157 #ifdef FEAT_SEARCHPATH
6159 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6161 if (cap->nchar == 'f')
6162 nv_gotofile(cap);
6163 else
6164 #endif
6166 #ifdef FEAT_FIND_ID
6168 * Find the occurence(s) of the identifier or define under cursor
6169 * in current and included files or jump to the first occurence.
6171 * search list jump
6172 * fwd bwd fwd bwd fwd bwd
6173 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6174 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6176 if (vim_strchr((char_u *)
6177 #ifdef EBCDIC
6178 "iI\005dD\067",
6179 #else
6180 "iI\011dD\004",
6181 #endif
6182 cap->nchar) != NULL)
6184 char_u *ptr;
6185 int len;
6187 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6188 clearop(cap->oap);
6189 else
6191 find_pattern_in_path(ptr, 0, len, TRUE,
6192 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6193 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6194 cap->count1,
6195 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6196 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6197 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6198 (linenr_T)MAXLNUM);
6199 curwin->w_set_curswant = TRUE;
6202 else
6203 #endif
6206 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6207 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6208 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6209 * "[m" or "]m" search for prev/next start of (Java) method.
6210 * "[M" or "]M" search for prev/next end of (Java) method.
6212 if ( (cap->cmdchar == '['
6213 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6214 || (cap->cmdchar == ']'
6215 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6217 if (cap->nchar == '*')
6218 cap->nchar = '/';
6219 new_pos.lnum = 0;
6220 prev_pos.lnum = 0;
6221 if (cap->nchar == 'm' || cap->nchar == 'M')
6223 if (cap->cmdchar == '[')
6224 findc = '{';
6225 else
6226 findc = '}';
6227 n = 9999;
6229 else
6231 findc = cap->nchar;
6232 n = cap->count1;
6234 for ( ; n > 0; --n)
6236 if ((pos = findmatchlimit(cap->oap, findc,
6237 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6239 if (new_pos.lnum == 0) /* nothing found */
6241 if (cap->nchar != 'm' && cap->nchar != 'M')
6242 clearopbeep(cap->oap);
6244 else
6245 pos = &new_pos; /* use last one found */
6246 break;
6248 prev_pos = new_pos;
6249 curwin->w_cursor = *pos;
6250 new_pos = *pos;
6252 curwin->w_cursor = old_pos;
6255 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6256 * brought us to the match for "[m" and "]M" when inside a method.
6257 * Try finding the '{' or '}' we want to be at.
6258 * Also repeat for the given count.
6260 if (cap->nchar == 'm' || cap->nchar == 'M')
6262 /* norm is TRUE for "]M" and "[m" */
6263 int norm = ((findc == '{') == (cap->nchar == 'm'));
6265 n = cap->count1;
6266 /* found a match: we were inside a method */
6267 if (prev_pos.lnum != 0)
6269 pos = &prev_pos;
6270 curwin->w_cursor = prev_pos;
6271 if (norm)
6272 --n;
6274 else
6275 pos = NULL;
6276 while (n > 0)
6278 for (;;)
6280 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6282 /* if not found anything, that's an error */
6283 if (pos == NULL)
6284 clearopbeep(cap->oap);
6285 n = 0;
6286 break;
6288 c = gchar_cursor();
6289 if (c == '{' || c == '}')
6291 /* Must have found end/start of class: use it.
6292 * Or found the place to be at. */
6293 if ((c == findc && norm) || (n == 1 && !norm))
6295 new_pos = curwin->w_cursor;
6296 pos = &new_pos;
6297 n = 0;
6299 /* if no match found at all, we started outside of the
6300 * class and we're inside now. Just go on. */
6301 else if (new_pos.lnum == 0)
6303 new_pos = curwin->w_cursor;
6304 pos = &new_pos;
6306 /* found start/end of other method: go to match */
6307 else if ((pos = findmatchlimit(cap->oap, findc,
6308 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6309 0)) == NULL)
6310 n = 0;
6311 else
6312 curwin->w_cursor = *pos;
6313 break;
6316 --n;
6318 curwin->w_cursor = old_pos;
6319 if (pos == NULL && new_pos.lnum != 0)
6320 clearopbeep(cap->oap);
6322 if (pos != NULL)
6324 setpcmark();
6325 curwin->w_cursor = *pos;
6326 curwin->w_set_curswant = TRUE;
6327 #ifdef FEAT_FOLDING
6328 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6329 && cap->oap->op_type == OP_NOP)
6330 foldOpenCursor();
6331 #endif
6336 * "[[", "[]", "]]" and "][": move to start or end of function
6338 else if (cap->nchar == '[' || cap->nchar == ']')
6340 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6341 flag = '{';
6342 else
6343 flag = '}'; /* "][" or "[]" */
6345 curwin->w_set_curswant = TRUE;
6347 * Imitate strange Vi behaviour: When using "]]" with an operator
6348 * we also stop at '}'.
6350 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
6351 (cap->oap->op_type != OP_NOP
6352 && cap->arg == FORWARD && flag == '{')))
6353 clearopbeep(cap->oap);
6354 else
6356 if (cap->oap->op_type == OP_NOP)
6357 beginline(BL_WHITE | BL_FIX);
6358 #ifdef FEAT_FOLDING
6359 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6360 foldOpenCursor();
6361 #endif
6366 * "[p", "[P", "]P" and "]p": put with indent adjustment
6368 else if (cap->nchar == 'p' || cap->nchar == 'P')
6370 if (!checkclearopq(cap->oap))
6372 prep_redo_cmd(cap);
6373 do_put(cap->oap->regname,
6374 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6375 cap->count1, PUT_FIXINDENT);
6380 * "['", "[`", "]'" and "]`": jump to next mark
6382 else if (cap->nchar == '\'' || cap->nchar == '`')
6384 pos = &curwin->w_cursor;
6385 for (n = cap->count1; n > 0; --n)
6387 prev_pos = *pos;
6388 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6389 cap->nchar == '\'');
6390 if (pos == NULL)
6391 break;
6393 if (pos == NULL)
6394 pos = &prev_pos;
6395 nv_cursormark(cap, cap->nchar == '\'', pos);
6398 #ifdef FEAT_MOUSE
6400 * [ or ] followed by a middle mouse click: put selected text with
6401 * indent adjustment. Any other button just does as usual.
6403 else if (cap->nchar >= K_LEFTMOUSE && cap->nchar <= K_RIGHTRELEASE)
6405 (void)do_mouse(cap->oap, cap->nchar,
6406 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6407 cap->count1, PUT_FIXINDENT);
6409 #endif /* FEAT_MOUSE */
6411 #ifdef FEAT_FOLDING
6413 * "[z" and "]z": move to start or end of open fold.
6415 else if (cap->nchar == 'z')
6417 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6418 cap->count1) == FAIL)
6419 clearopbeep(cap->oap);
6421 #endif
6423 #ifdef FEAT_DIFF
6425 * "[c" and "]c": move to next or previous diff-change.
6427 else if (cap->nchar == 'c')
6429 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6430 cap->count1) == FAIL)
6431 clearopbeep(cap->oap);
6433 #endif
6435 #ifdef FEAT_SPELL
6437 * "[s", "[S", "]s" and "]S": move to next spell error.
6439 else if (cap->nchar == 's' || cap->nchar == 'S')
6441 setpcmark();
6442 for (n = 0; n < cap->count1; ++n)
6443 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6444 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
6446 clearopbeep(cap->oap);
6447 break;
6449 # ifdef FEAT_FOLDING
6450 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6451 foldOpenCursor();
6452 # endif
6454 #endif
6456 /* Not a valid cap->nchar. */
6457 else
6458 clearopbeep(cap->oap);
6462 * Handle Normal mode "%" command.
6464 static void
6465 nv_percent(cap)
6466 cmdarg_T *cap;
6468 pos_T *pos;
6469 #ifdef FEAT_FOLDING
6470 linenr_T lnum = curwin->w_cursor.lnum;
6471 #endif
6473 cap->oap->inclusive = TRUE;
6474 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6476 if (cap->count0 > 100)
6477 clearopbeep(cap->oap);
6478 else
6480 cap->oap->motion_type = MLINE;
6481 setpcmark();
6482 /* Round up, so CTRL-G will give same value. Watch out for a
6483 * large line count, the line number must not go negative! */
6484 if (curbuf->b_ml.ml_line_count > 1000000)
6485 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6486 / 100L * cap->count0;
6487 else
6488 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6489 cap->count0 + 99L) / 100L;
6490 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6491 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6492 beginline(BL_SOL | BL_FIX);
6495 else /* "%" : go to matching paren */
6497 cap->oap->motion_type = MCHAR;
6498 cap->oap->use_reg_one = TRUE;
6499 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6500 clearopbeep(cap->oap);
6501 else
6503 setpcmark();
6504 curwin->w_cursor = *pos;
6505 curwin->w_set_curswant = TRUE;
6506 #ifdef FEAT_VIRTUALEDIT
6507 curwin->w_cursor.coladd = 0;
6508 #endif
6509 #ifdef FEAT_VISUAL
6510 adjust_for_sel(cap);
6511 #endif
6514 #ifdef FEAT_FOLDING
6515 if (cap->oap->op_type == OP_NOP
6516 && lnum != curwin->w_cursor.lnum
6517 && (fdo_flags & FDO_PERCENT)
6518 && KeyTyped)
6519 foldOpenCursor();
6520 #endif
6524 * Handle "(" and ")" commands.
6525 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6527 static void
6528 nv_brace(cap)
6529 cmdarg_T *cap;
6531 cap->oap->motion_type = MCHAR;
6532 cap->oap->use_reg_one = TRUE;
6533 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6534 cap->oap->inclusive = FALSE;
6535 curwin->w_set_curswant = TRUE;
6537 if (findsent(cap->arg, cap->count1) == FAIL)
6538 clearopbeep(cap->oap);
6539 else
6541 #ifdef FEAT_VIRTUALEDIT
6542 curwin->w_cursor.coladd = 0;
6543 #endif
6544 #ifdef FEAT_FOLDING
6545 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6546 foldOpenCursor();
6547 #endif
6552 * "m" command: Mark a position.
6554 static void
6555 nv_mark(cap)
6556 cmdarg_T *cap;
6558 if (!checkclearop(cap->oap))
6560 if (setmark(cap->nchar) == FAIL)
6561 clearopbeep(cap->oap);
6566 * "{" and "}" commands.
6567 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6569 static void
6570 nv_findpar(cap)
6571 cmdarg_T *cap;
6573 cap->oap->motion_type = MCHAR;
6574 cap->oap->inclusive = FALSE;
6575 cap->oap->use_reg_one = TRUE;
6576 curwin->w_set_curswant = TRUE;
6577 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
6578 clearopbeep(cap->oap);
6579 else
6581 #ifdef FEAT_VIRTUALEDIT
6582 curwin->w_cursor.coladd = 0;
6583 #endif
6584 #ifdef FEAT_FOLDING
6585 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6586 foldOpenCursor();
6587 #endif
6592 * "u" command: Undo or make lower case.
6594 static void
6595 nv_undo(cap)
6596 cmdarg_T *cap;
6598 if (cap->oap->op_type == OP_LOWER
6599 #ifdef FEAT_VISUAL
6600 || VIsual_active
6601 #endif
6604 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6605 cap->cmdchar = 'g';
6606 cap->nchar = 'u';
6607 nv_operator(cap);
6609 else
6610 nv_kundo(cap);
6614 * <Undo> command.
6616 static void
6617 nv_kundo(cap)
6618 cmdarg_T *cap;
6620 if (!checkclearopq(cap->oap))
6622 u_undo((int)cap->count1);
6623 curwin->w_set_curswant = TRUE;
6628 * Handle the "r" command.
6630 static void
6631 nv_replace(cap)
6632 cmdarg_T *cap;
6634 char_u *ptr;
6635 int had_ctrl_v;
6636 long n;
6638 if (checkclearop(cap->oap))
6639 return;
6641 /* get another character */
6642 if (cap->nchar == Ctrl_V)
6644 had_ctrl_v = Ctrl_V;
6645 cap->nchar = get_literal();
6646 /* Don't redo a multibyte character with CTRL-V. */
6647 if (cap->nchar > DEL)
6648 had_ctrl_v = NUL;
6650 else
6651 had_ctrl_v = NUL;
6653 #ifdef FEAT_VISUAL
6654 /* Visual mode "r" */
6655 if (VIsual_active)
6657 nv_operator(cap);
6658 return;
6660 #endif
6662 #ifdef FEAT_VIRTUALEDIT
6663 /* Break tabs, etc. */
6664 if (virtual_active())
6666 if (u_save_cursor() == FAIL)
6667 return;
6668 if (gchar_cursor() == NUL)
6670 /* Add extra space and put the cursor on the first one. */
6671 coladvance_force((colnr_T)(getviscol() + cap->count1));
6672 curwin->w_cursor.col -= cap->count1;
6674 else if (gchar_cursor() == TAB)
6675 coladvance_force(getviscol());
6677 #endif
6680 * Check for a special key or not enough characters to replace.
6682 ptr = ml_get_cursor();
6683 if (IS_SPECIAL(cap->nchar) || STRLEN(ptr) < (unsigned)cap->count1
6684 #ifdef FEAT_MBYTE
6685 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6686 #endif
6689 clearopbeep(cap->oap);
6690 return;
6694 * Replacing with a TAB is done by edit() when it is complicated because
6695 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6696 * Other characters are done below to avoid problems with things like
6697 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6699 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6701 stuffnumReadbuff(cap->count1);
6702 stuffcharReadbuff('R');
6703 stuffcharReadbuff('\t');
6704 stuffcharReadbuff(ESC);
6705 return;
6708 /* save line for undo */
6709 if (u_save_cursor() == FAIL)
6710 return;
6712 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6715 * Replace character(s) by a single newline.
6716 * Strange vi behaviour: Only one newline is inserted.
6717 * Delete the characters here.
6718 * Insert the newline with an insert command, takes care of
6719 * autoindent. The insert command depends on being on the last
6720 * character of a line or not.
6722 #ifdef FEAT_MBYTE
6723 (void)del_chars(cap->count1, FALSE); /* delete the characters */
6724 #else
6725 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
6726 #endif
6727 stuffcharReadbuff('\r');
6728 stuffcharReadbuff(ESC);
6730 /* Give 'r' to edit(), to get the redo command right. */
6731 invoke_edit(cap, TRUE, 'r', FALSE);
6733 else
6735 prep_redo(cap->oap->regname, cap->count1,
6736 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
6738 curbuf->b_op_start = curwin->w_cursor;
6739 #ifdef FEAT_MBYTE
6740 if (has_mbyte)
6742 int old_State = State;
6744 if (cap->ncharC1 != 0)
6745 AppendCharToRedobuff(cap->ncharC1);
6746 if (cap->ncharC2 != 0)
6747 AppendCharToRedobuff(cap->ncharC2);
6749 /* This is slow, but it handles replacing a single-byte with a
6750 * multi-byte and the other way around. Also handles adding
6751 * composing characters for utf-8. */
6752 for (n = cap->count1; n > 0; --n)
6754 State = REPLACE;
6755 ins_char(cap->nchar);
6756 State = old_State;
6757 if (cap->ncharC1 != 0)
6758 ins_char(cap->ncharC1);
6759 if (cap->ncharC2 != 0)
6760 ins_char(cap->ncharC2);
6763 else
6764 #endif
6767 * Replace the characters within one line.
6769 for (n = cap->count1; n > 0; --n)
6772 * Get ptr again, because u_save and/or showmatch() will have
6773 * released the line. At the same time we let know that the
6774 * line will be changed.
6776 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
6777 ptr[curwin->w_cursor.col] = cap->nchar;
6778 if (p_sm && msg_silent == 0)
6779 showmatch(cap->nchar);
6780 ++curwin->w_cursor.col;
6782 #ifdef FEAT_NETBEANS_INTG
6783 if (usingNetbeans)
6785 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
6787 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
6788 (long)cap->count1);
6789 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
6790 &ptr[start], (int)cap->count1);
6792 #endif
6794 /* mark the buffer as changed and prepare for displaying */
6795 changed_bytes(curwin->w_cursor.lnum,
6796 (colnr_T)(curwin->w_cursor.col - cap->count1));
6798 --curwin->w_cursor.col; /* cursor on the last replaced char */
6799 #ifdef FEAT_MBYTE
6800 /* if the character on the left of the current cursor is a multi-byte
6801 * character, move two characters left */
6802 if (has_mbyte)
6803 mb_adjust_cursor();
6804 #endif
6805 curbuf->b_op_end = curwin->w_cursor;
6806 curwin->w_set_curswant = TRUE;
6807 set_last_insert(cap->nchar);
6811 #ifdef FEAT_VISUAL
6813 * 'o': Exchange start and end of Visual area.
6814 * 'O': same, but in block mode exchange left and right corners.
6816 static void
6817 v_swap_corners(cmdchar)
6818 int cmdchar;
6820 pos_T old_cursor;
6821 colnr_T left, right;
6823 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
6825 old_cursor = curwin->w_cursor;
6826 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
6827 curwin->w_cursor.lnum = VIsual.lnum;
6828 coladvance(left);
6829 VIsual = curwin->w_cursor;
6831 curwin->w_cursor.lnum = old_cursor.lnum;
6832 curwin->w_curswant = right;
6833 /* 'selection "exclusive" and cursor at right-bottom corner: move it
6834 * right one column */
6835 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
6836 ++curwin->w_curswant;
6837 coladvance(curwin->w_curswant);
6838 if (curwin->w_cursor.col == old_cursor.col
6839 #ifdef FEAT_VIRTUALEDIT
6840 && (!virtual_active()
6841 || curwin->w_cursor.coladd == old_cursor.coladd)
6842 #endif
6845 curwin->w_cursor.lnum = VIsual.lnum;
6846 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
6847 ++right;
6848 coladvance(right);
6849 VIsual = curwin->w_cursor;
6851 curwin->w_cursor.lnum = old_cursor.lnum;
6852 coladvance(left);
6853 curwin->w_curswant = left;
6856 else
6858 old_cursor = curwin->w_cursor;
6859 curwin->w_cursor = VIsual;
6860 VIsual = old_cursor;
6861 curwin->w_set_curswant = TRUE;
6864 #endif /* FEAT_VISUAL */
6867 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
6869 static void
6870 nv_Replace(cap)
6871 cmdarg_T *cap;
6873 #ifdef FEAT_VISUAL
6874 if (VIsual_active) /* "R" is replace lines */
6876 cap->cmdchar = 'c';
6877 cap->nchar = NUL;
6878 VIsual_mode = 'V';
6879 nv_operator(cap);
6881 else
6882 #endif
6883 if (!checkclearopq(cap->oap))
6885 if (!curbuf->b_p_ma)
6886 EMSG(_(e_modifiable));
6887 else
6889 #ifdef FEAT_VIRTUALEDIT
6890 if (virtual_active())
6891 coladvance(getviscol());
6892 #endif
6893 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
6898 #ifdef FEAT_VREPLACE
6900 * "gr".
6902 static void
6903 nv_vreplace(cap)
6904 cmdarg_T *cap;
6906 # ifdef FEAT_VISUAL
6907 if (VIsual_active)
6909 cap->cmdchar = 'r';
6910 cap->nchar = cap->extra_char;
6911 nv_replace(cap); /* Do same as "r" in Visual mode for now */
6913 else
6914 # endif
6915 if (!checkclearopq(cap->oap))
6917 if (!curbuf->b_p_ma)
6918 EMSG(_(e_modifiable));
6919 else
6921 if (cap->extra_char == Ctrl_V) /* get another character */
6922 cap->extra_char = get_literal();
6923 stuffcharReadbuff(cap->extra_char);
6924 stuffcharReadbuff(ESC);
6925 # ifdef FEAT_VIRTUALEDIT
6926 if (virtual_active())
6927 coladvance(getviscol());
6928 # endif
6929 invoke_edit(cap, TRUE, 'v', FALSE);
6933 #endif
6936 * Swap case for "~" command, when it does not work like an operator.
6938 static void
6939 n_swapchar(cap)
6940 cmdarg_T *cap;
6942 long n;
6943 pos_T startpos;
6944 int did_change = 0;
6945 #ifdef FEAT_NETBEANS_INTG
6946 pos_T pos;
6947 char_u *ptr;
6948 int count;
6949 #endif
6951 if (checkclearopq(cap->oap))
6952 return;
6954 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
6956 clearopbeep(cap->oap);
6957 return;
6960 prep_redo_cmd(cap);
6962 if (u_save_cursor() == FAIL)
6963 return;
6965 startpos = curwin->w_cursor;
6966 #ifdef FEAT_NETBEANS_INTG
6967 pos = startpos;
6968 #endif
6969 for (n = cap->count1; n > 0; --n)
6971 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
6972 inc_cursor();
6973 if (gchar_cursor() == NUL)
6975 if (vim_strchr(p_ww, '~') != NULL
6976 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6978 #ifdef FEAT_NETBEANS_INTG
6979 if (usingNetbeans)
6981 if (did_change)
6983 ptr = ml_get(pos.lnum);
6984 count = (int)STRLEN(ptr) - pos.col;
6985 netbeans_removed(curbuf, pos.lnum, pos.col,
6986 (long)count);
6987 netbeans_inserted(curbuf, pos.lnum, pos.col,
6988 &ptr[pos.col], count);
6990 pos.col = 0;
6991 pos.lnum++;
6993 #endif
6994 ++curwin->w_cursor.lnum;
6995 curwin->w_cursor.col = 0;
6996 if (n > 1)
6998 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
6999 break;
7000 u_clearline();
7003 else
7004 break;
7007 #ifdef FEAT_NETBEANS_INTG
7008 if (did_change && usingNetbeans)
7010 ptr = ml_get(pos.lnum);
7011 count = curwin->w_cursor.col - pos.col;
7012 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7013 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
7015 #endif
7018 check_cursor();
7019 curwin->w_set_curswant = TRUE;
7020 if (did_change)
7022 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7023 0L);
7024 curbuf->b_op_start = startpos;
7025 curbuf->b_op_end = curwin->w_cursor;
7026 if (curbuf->b_op_end.col > 0)
7027 --curbuf->b_op_end.col;
7032 * Move cursor to mark.
7034 static void
7035 nv_cursormark(cap, flag, pos)
7036 cmdarg_T *cap;
7037 int flag;
7038 pos_T *pos;
7040 if (check_mark(pos) == FAIL)
7041 clearop(cap->oap);
7042 else
7044 if (cap->cmdchar == '\''
7045 || cap->cmdchar == '`'
7046 || cap->cmdchar == '['
7047 || cap->cmdchar == ']')
7048 setpcmark();
7049 curwin->w_cursor = *pos;
7050 if (flag)
7051 beginline(BL_WHITE | BL_FIX);
7052 else
7053 check_cursor();
7055 cap->oap->motion_type = flag ? MLINE : MCHAR;
7056 if (cap->cmdchar == '`')
7057 cap->oap->use_reg_one = TRUE;
7058 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7059 curwin->w_set_curswant = TRUE;
7062 #ifdef FEAT_VISUAL
7064 * Handle commands that are operators in Visual mode.
7066 static void
7067 v_visop(cap)
7068 cmdarg_T *cap;
7070 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7072 /* Uppercase means linewise, except in block mode, then "D" deletes till
7073 * the end of the line, and "C" replaces til EOL */
7074 if (isupper(cap->cmdchar))
7076 if (VIsual_mode != Ctrl_V)
7077 VIsual_mode = 'V';
7078 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7079 curwin->w_curswant = MAXCOL;
7081 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7082 nv_operator(cap);
7084 #endif
7087 * "s" and "S" commands.
7089 static void
7090 nv_subst(cap)
7091 cmdarg_T *cap;
7093 #ifdef FEAT_VISUAL
7094 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7096 if (cap->cmdchar == 'S')
7097 VIsual_mode = 'V';
7098 cap->cmdchar = 'c';
7099 nv_operator(cap);
7101 else
7102 #endif
7103 nv_optrans(cap);
7107 * Abbreviated commands.
7109 static void
7110 nv_abbrev(cap)
7111 cmdarg_T *cap;
7113 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7114 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7116 #ifdef FEAT_VISUAL
7117 /* in Visual mode these commands are operators */
7118 if (VIsual_active)
7119 v_visop(cap);
7120 else
7121 #endif
7122 nv_optrans(cap);
7126 * Translate a command into another command.
7128 static void
7129 nv_optrans(cap)
7130 cmdarg_T *cap;
7132 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7133 (char_u *)"d$", (char_u *)"c$",
7134 (char_u *)"cl", (char_u *)"cc",
7135 (char_u *)"yy", (char_u *)":s\r"};
7136 static char_u *str = (char_u *)"xXDCsSY&";
7138 if (!checkclearopq(cap->oap))
7140 /* In Vi "2D" doesn't delete the next line. Can't translate it
7141 * either, because "2." should also not use the count. */
7142 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7144 cap->oap->start = curwin->w_cursor;
7145 cap->oap->op_type = OP_DELETE;
7146 cap->count1 = 1;
7147 nv_dollar(cap);
7148 finish_op = TRUE;
7149 ResetRedobuff();
7150 AppendCharToRedobuff('D');
7152 else
7154 if (cap->count0)
7155 stuffnumReadbuff(cap->count0);
7156 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7159 cap->opcount = 0;
7163 * "'" and "`" commands. Also for "g'" and "g`".
7164 * cap->arg is TRUE for "'" and "g'".
7166 static void
7167 nv_gomark(cap)
7168 cmdarg_T *cap;
7170 pos_T *pos;
7171 int c;
7172 #ifdef FEAT_FOLDING
7173 linenr_T lnum = curwin->w_cursor.lnum;
7174 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7175 #endif
7177 if (cap->cmdchar == 'g')
7178 c = cap->extra_char;
7179 else
7180 c = cap->nchar;
7181 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7182 if (pos == (pos_T *)-1) /* jumped to other file */
7184 if (cap->arg)
7186 check_cursor_lnum();
7187 beginline(BL_WHITE | BL_FIX);
7189 else
7190 check_cursor();
7192 else
7193 nv_cursormark(cap, cap->arg, pos);
7195 #ifdef FEAT_VIRTUALEDIT
7196 /* May need to clear the coladd that a mark includes. */
7197 if (!virtual_active())
7198 curwin->w_cursor.coladd = 0;
7199 #endif
7200 #ifdef FEAT_FOLDING
7201 if (cap->oap->op_type == OP_NOP
7202 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7203 && (fdo_flags & FDO_MARK)
7204 && old_KeyTyped)
7205 foldOpenCursor();
7206 #endif
7210 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7212 static void
7213 nv_pcmark(cap)
7214 cmdarg_T *cap;
7216 #ifdef FEAT_JUMPLIST
7217 pos_T *pos;
7218 # ifdef FEAT_FOLDING
7219 linenr_T lnum = curwin->w_cursor.lnum;
7220 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7221 # endif
7223 if (!checkclearopq(cap->oap))
7225 if (cap->cmdchar == 'g')
7226 pos = movechangelist((int)cap->count1);
7227 else
7228 pos = movemark((int)cap->count1);
7229 if (pos == (pos_T *)-1) /* jump to other file */
7231 curwin->w_set_curswant = TRUE;
7232 check_cursor();
7234 else if (pos != NULL) /* can jump */
7235 nv_cursormark(cap, FALSE, pos);
7236 else if (cap->cmdchar == 'g')
7238 if (curbuf->b_changelistlen == 0)
7239 EMSG(_("E664: changelist is empty"));
7240 else if (cap->count1 < 0)
7241 EMSG(_("E662: At start of changelist"));
7242 else
7243 EMSG(_("E663: At end of changelist"));
7245 else
7246 clearopbeep(cap->oap);
7247 # ifdef FEAT_FOLDING
7248 if (cap->oap->op_type == OP_NOP
7249 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7250 && (fdo_flags & FDO_MARK)
7251 && old_KeyTyped)
7252 foldOpenCursor();
7253 # endif
7255 #else
7256 clearopbeep(cap->oap);
7257 #endif
7261 * Handle '"' command.
7263 static void
7264 nv_regname(cap)
7265 cmdarg_T *cap;
7267 if (checkclearop(cap->oap))
7268 return;
7269 #ifdef FEAT_EVAL
7270 if (cap->nchar == '=')
7271 cap->nchar = get_expr_register();
7272 #endif
7273 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7275 cap->oap->regname = cap->nchar;
7276 cap->opcount = cap->count0; /* remember count before '"' */
7277 #ifdef FEAT_EVAL
7278 set_reg_var(cap->oap->regname);
7279 #endif
7281 else
7282 clearopbeep(cap->oap);
7285 #ifdef FEAT_VISUAL
7287 * Handle "v", "V" and "CTRL-V" commands.
7288 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7289 * is TRUE.
7290 * Handle CTRL-Q just like CTRL-V.
7292 static void
7293 nv_visual(cap)
7294 cmdarg_T *cap;
7296 if (cap->cmdchar == Ctrl_Q)
7297 cap->cmdchar = Ctrl_V;
7299 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7300 * characterwise, linewise, or blockwise. */
7301 if (cap->oap->op_type != OP_NOP)
7303 cap->oap->motion_force = cap->cmdchar;
7304 finish_op = FALSE; /* operator doesn't finish now but later */
7305 return;
7308 VIsual_select = cap->arg;
7309 if (VIsual_active) /* change Visual mode */
7311 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7312 end_visual_mode();
7313 else /* toggle char/block mode */
7314 { /* or char/line mode */
7315 VIsual_mode = cap->cmdchar;
7316 showmode();
7318 redraw_curbuf_later(INVERTED); /* update the inversion */
7320 else /* start Visual mode */
7322 check_visual_highlight();
7323 if (cap->count0) /* use previously selected part */
7325 if (resel_VIsual_mode == NUL) /* there is none */
7327 beep_flush();
7328 return;
7330 VIsual = curwin->w_cursor;
7332 VIsual_active = TRUE;
7333 VIsual_reselect = TRUE;
7334 if (!cap->arg)
7335 /* start Select mode when 'selectmode' contains "cmd" */
7336 may_start_select('c');
7337 #ifdef FEAT_MOUSE
7338 setmouse();
7339 #endif
7340 if (p_smd && msg_silent == 0)
7341 redraw_cmdline = TRUE; /* show visual mode later */
7343 * For V and ^V, we multiply the number of lines even if there
7344 * was only one -- webb
7346 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7348 curwin->w_cursor.lnum +=
7349 resel_VIsual_line_count * cap->count0 - 1;
7350 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7351 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7353 VIsual_mode = resel_VIsual_mode;
7354 if (VIsual_mode == 'v')
7356 if (resel_VIsual_line_count <= 1)
7357 curwin->w_cursor.col += resel_VIsual_col * cap->count0 - 1;
7358 else
7359 curwin->w_cursor.col = resel_VIsual_col;
7360 check_cursor_col();
7362 if (resel_VIsual_col == MAXCOL)
7364 curwin->w_curswant = MAXCOL;
7365 coladvance((colnr_T)MAXCOL);
7367 else if (VIsual_mode == Ctrl_V)
7369 validate_virtcol();
7370 curwin->w_curswant = curwin->w_virtcol
7371 + resel_VIsual_col * cap->count0 - 1;
7372 coladvance(curwin->w_curswant);
7374 else
7375 curwin->w_set_curswant = TRUE;
7376 redraw_curbuf_later(INVERTED); /* show the inversion */
7378 else
7380 if (!cap->arg)
7381 /* start Select mode when 'selectmode' contains "cmd" */
7382 may_start_select('c');
7383 n_start_visual_mode(cap->cmdchar);
7389 * Start selection for Shift-movement keys.
7391 void
7392 start_selection()
7394 /* if 'selectmode' contains "key", start Select mode */
7395 may_start_select('k');
7396 n_start_visual_mode('v');
7400 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7402 void
7403 may_start_select(c)
7404 int c;
7406 VIsual_select = (stuff_empty() && typebuf_typed()
7407 && (vim_strchr(p_slm, c) != NULL));
7411 * Start Visual mode "c".
7412 * Should set VIsual_select before calling this.
7414 static void
7415 n_start_visual_mode(c)
7416 int c;
7418 VIsual_mode = c;
7419 VIsual_active = TRUE;
7420 VIsual_reselect = TRUE;
7421 #ifdef FEAT_VIRTUALEDIT
7422 /* Corner case: the 0 position in a tab may change when going into
7423 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7425 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7426 coladvance(curwin->w_virtcol);
7427 #endif
7428 VIsual = curwin->w_cursor;
7430 #ifdef FEAT_FOLDING
7431 foldAdjustVisual();
7432 #endif
7434 #ifdef FEAT_MOUSE
7435 setmouse();
7436 #endif
7437 if (p_smd && msg_silent == 0)
7438 redraw_cmdline = TRUE; /* show visual mode later */
7439 #ifdef FEAT_CLIPBOARD
7440 /* Make sure the clipboard gets updated. Needed because start and
7441 * end may still be the same, and the selection needs to be owned */
7442 clip_star.vmode = NUL;
7443 #endif
7445 /* Only need to redraw this line, unless still need to redraw an old
7446 * Visual area (when 'lazyredraw' is set). */
7447 if (curwin->w_redr_type < INVERTED)
7449 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7450 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7454 #endif /* FEAT_VISUAL */
7457 * CTRL-W: Window commands
7459 static void
7460 nv_window(cap)
7461 cmdarg_T *cap;
7463 #ifdef FEAT_WINDOWS
7464 if (!checkclearop(cap->oap))
7465 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7466 #else
7467 (void)checkclearop(cap->oap);
7468 #endif
7472 * CTRL-Z: Suspend
7474 static void
7475 nv_suspend(cap)
7476 cmdarg_T *cap;
7478 clearop(cap->oap);
7479 #ifdef FEAT_VISUAL
7480 if (VIsual_active)
7481 end_visual_mode(); /* stop Visual mode */
7482 #endif
7483 do_cmdline_cmd((char_u *)"st");
7487 * Commands starting with "g".
7489 static void
7490 nv_g_cmd(cap)
7491 cmdarg_T *cap;
7493 oparg_T *oap = cap->oap;
7494 #ifdef FEAT_VISUAL
7495 pos_T tpos;
7496 #endif
7497 int i;
7498 int flag = FALSE;
7500 switch (cap->nchar)
7502 #ifdef MEM_PROFILE
7504 * "g^A": dump log of used memory.
7506 case Ctrl_A:
7507 vim_mem_profile_dump();
7508 break;
7509 #endif
7511 #ifdef FEAT_VREPLACE
7513 * "gR": Enter virtual replace mode.
7515 case 'R':
7516 cap->arg = TRUE;
7517 nv_Replace(cap);
7518 break;
7520 case 'r':
7521 nv_vreplace(cap);
7522 break;
7523 #endif
7525 case '&':
7526 do_cmdline_cmd((char_u *)"%s//~/&");
7527 break;
7529 #ifdef FEAT_VISUAL
7531 * "gv": Reselect the previous Visual area. If Visual already active,
7532 * exchange previous and current Visual area.
7534 case 'v':
7535 if (checkclearop(oap))
7536 break;
7538 if ( curbuf->b_visual.vi_start.lnum == 0
7539 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7540 || curbuf->b_visual.vi_end.lnum == 0)
7541 beep_flush();
7542 else
7544 /* set w_cursor to the start of the Visual area, tpos to the end */
7545 if (VIsual_active)
7547 i = VIsual_mode;
7548 VIsual_mode = curbuf->b_visual.vi_mode;
7549 curbuf->b_visual.vi_mode = i;
7550 # ifdef FEAT_EVAL
7551 curbuf->b_visual_mode_eval = i;
7552 # endif
7553 i = curwin->w_curswant;
7554 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7555 curbuf->b_visual.vi_curswant = i;
7557 tpos = curbuf->b_visual.vi_end;
7558 curbuf->b_visual.vi_end = curwin->w_cursor;
7559 curwin->w_cursor = curbuf->b_visual.vi_start;
7560 curbuf->b_visual.vi_start = VIsual;
7562 else
7564 VIsual_mode = curbuf->b_visual.vi_mode;
7565 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7566 tpos = curbuf->b_visual.vi_end;
7567 curwin->w_cursor = curbuf->b_visual.vi_start;
7570 VIsual_active = TRUE;
7571 VIsual_reselect = TRUE;
7573 /* Set Visual to the start and w_cursor to the end of the Visual
7574 * area. Make sure they are on an existing character. */
7575 check_cursor();
7576 VIsual = curwin->w_cursor;
7577 curwin->w_cursor = tpos;
7578 check_cursor();
7579 update_topline();
7581 * When called from normal "g" command: start Select mode when
7582 * 'selectmode' contains "cmd". When called for K_SELECT, always
7583 * start Select mode.
7585 if (cap->arg)
7586 VIsual_select = TRUE;
7587 else
7588 may_start_select('c');
7589 #ifdef FEAT_MOUSE
7590 setmouse();
7591 #endif
7592 #ifdef FEAT_CLIPBOARD
7593 /* Make sure the clipboard gets updated. Needed because start and
7594 * end are still the same, and the selection needs to be owned */
7595 clip_star.vmode = NUL;
7596 #endif
7597 redraw_curbuf_later(INVERTED);
7598 showmode();
7600 break;
7602 * "gV": Don't reselect the previous Visual area after a Select mode
7603 * mapping of menu.
7605 case 'V':
7606 VIsual_reselect = FALSE;
7607 break;
7610 * "gh": start Select mode.
7611 * "gH": start Select line mode.
7612 * "g^H": start Select block mode.
7614 case K_BS:
7615 cap->nchar = Ctrl_H;
7616 /* FALLTHROUGH */
7617 case 'h':
7618 case 'H':
7619 case Ctrl_H:
7620 # ifdef EBCDIC
7621 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7622 if (cap->nchar == Ctrl_H)
7623 cap->cmdchar = Ctrl_V;
7624 else
7625 # endif
7626 cap->cmdchar = cap->nchar + ('v' - 'h');
7627 cap->arg = TRUE;
7628 nv_visual(cap);
7629 break;
7630 #endif /* FEAT_VISUAL */
7633 * "gj" and "gk" two new funny movement keys -- up and down
7634 * movement based on *screen* line rather than *file* line.
7636 case 'j':
7637 case K_DOWN:
7638 /* with 'nowrap' it works just like the normal "j" command; also when
7639 * in a closed fold */
7640 if (!curwin->w_p_wrap
7641 #ifdef FEAT_FOLDING
7642 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7643 #endif
7646 oap->motion_type = MLINE;
7647 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7649 else
7650 i = nv_screengo(oap, FORWARD, cap->count1);
7651 if (i == FAIL)
7652 clearopbeep(oap);
7653 break;
7655 case 'k':
7656 case K_UP:
7657 /* with 'nowrap' it works just like the normal "k" command; also when
7658 * in a closed fold */
7659 if (!curwin->w_p_wrap
7660 #ifdef FEAT_FOLDING
7661 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7662 #endif
7665 oap->motion_type = MLINE;
7666 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7668 else
7669 i = nv_screengo(oap, BACKWARD, cap->count1);
7670 if (i == FAIL)
7671 clearopbeep(oap);
7672 break;
7675 * "gJ": join two lines without inserting a space.
7677 case 'J':
7678 nv_join(cap);
7679 break;
7682 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7683 * "gm": middle of "g0" and "g$".
7685 case '^':
7686 flag = TRUE;
7687 /* FALLTHROUGH */
7689 case '0':
7690 case 'm':
7691 case K_HOME:
7692 case K_KHOME:
7693 oap->motion_type = MCHAR;
7694 oap->inclusive = FALSE;
7695 if (curwin->w_p_wrap
7696 #ifdef FEAT_VERTSPLIT
7697 && curwin->w_width != 0
7698 #endif
7701 int width1 = W_WIDTH(curwin) - curwin_col_off();
7702 int width2 = width1 + curwin_col_off2();
7704 validate_virtcol();
7705 i = 0;
7706 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
7707 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
7709 else
7710 i = curwin->w_leftcol;
7711 /* Go to the middle of the screen line. When 'number' is on and lines
7712 * are wrapping the middle can be more to the left.*/
7713 if (cap->nchar == 'm')
7714 i += (W_WIDTH(curwin) - curwin_col_off()
7715 + ((curwin->w_p_wrap && i > 0)
7716 ? curwin_col_off2() : 0)) / 2;
7717 coladvance((colnr_T)i);
7718 if (flag)
7721 i = gchar_cursor();
7722 while (vim_iswhite(i) && oneright() == OK);
7724 curwin->w_set_curswant = TRUE;
7725 break;
7727 case '_':
7728 /* "g_": to the last non-blank character in the line or <count> lines
7729 * downward. */
7730 cap->oap->motion_type = MCHAR;
7731 cap->oap->inclusive = TRUE;
7732 curwin->w_curswant = MAXCOL;
7733 if (cursor_down((long)(cap->count1 - 1),
7734 cap->oap->op_type == OP_NOP) == FAIL)
7735 clearopbeep(cap->oap);
7736 else
7738 char_u *ptr = ml_get_curline();
7740 /* In Visual mode we may end up after the line. */
7741 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
7742 --curwin->w_cursor.col;
7744 /* Decrease the cursor column until it's on a non-blank. */
7745 while (curwin->w_cursor.col > 0
7746 && vim_iswhite(ptr[curwin->w_cursor.col]))
7747 --curwin->w_cursor.col;
7748 curwin->w_set_curswant = TRUE;
7750 break;
7752 case '$':
7753 case K_END:
7754 case K_KEND:
7756 int col_off = curwin_col_off();
7758 oap->motion_type = MCHAR;
7759 oap->inclusive = TRUE;
7760 if (curwin->w_p_wrap
7761 #ifdef FEAT_VERTSPLIT
7762 && curwin->w_width != 0
7763 #endif
7766 curwin->w_curswant = MAXCOL; /* so we stay at the end */
7767 if (cap->count1 == 1)
7769 int width1 = W_WIDTH(curwin) - col_off;
7770 int width2 = width1 + curwin_col_off2();
7772 validate_virtcol();
7773 i = width1 - 1;
7774 if (curwin->w_virtcol >= (colnr_T)width1)
7775 i += ((curwin->w_virtcol - width1) / width2 + 1)
7776 * width2;
7777 coladvance((colnr_T)i);
7778 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
7779 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
7782 * Check for landing on a character that got split at
7783 * the end of the line. We do not want to advance to
7784 * the next screen line.
7786 validate_virtcol();
7787 if (curwin->w_virtcol > (colnr_T)i)
7788 --curwin->w_cursor.col;
7790 #endif
7792 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
7793 clearopbeep(oap);
7795 else
7797 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
7798 coladvance((colnr_T)i);
7800 /* Make sure we stick in this column. */
7801 validate_virtcol();
7802 curwin->w_curswant = curwin->w_virtcol;
7803 curwin->w_set_curswant = FALSE;
7806 break;
7809 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
7811 case '*':
7812 case '#':
7813 #if POUND != '#'
7814 case POUND: /* pound sign (sometimes equal to '#') */
7815 #endif
7816 case Ctrl_RSB: /* :tag or :tselect for current identifier */
7817 case ']': /* :tselect for current identifier */
7818 nv_ident(cap);
7819 break;
7822 * ge and gE: go back to end of word
7824 case 'e':
7825 case 'E':
7826 oap->motion_type = MCHAR;
7827 curwin->w_set_curswant = TRUE;
7828 oap->inclusive = TRUE;
7829 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
7830 clearopbeep(oap);
7831 break;
7834 * "g CTRL-G": display info about cursor position
7836 case Ctrl_G:
7837 cursor_pos_info();
7838 break;
7841 * "gi": start Insert at the last position.
7843 case 'i':
7844 if (curbuf->b_last_insert.lnum != 0)
7846 curwin->w_cursor = curbuf->b_last_insert;
7847 check_cursor_lnum();
7848 i = (int)STRLEN(ml_get_curline());
7849 if (curwin->w_cursor.col > (colnr_T)i)
7851 #ifdef FEAT_VIRTUALEDIT
7852 if (virtual_active())
7853 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
7854 #endif
7855 curwin->w_cursor.col = i;
7858 cap->cmdchar = 'i';
7859 nv_edit(cap);
7860 break;
7863 * "gI": Start insert in column 1.
7865 case 'I':
7866 beginline(0);
7867 if (!checkclearopq(oap))
7868 invoke_edit(cap, FALSE, 'g', FALSE);
7869 break;
7871 #ifdef FEAT_SEARCHPATH
7873 * "gf": goto file, edit file under cursor
7874 * "]f" and "[f": can also be used.
7876 case 'f':
7877 case 'F':
7878 nv_gotofile(cap);
7879 break;
7880 #endif
7882 /* "g'm" and "g`m": jump to mark without setting pcmark */
7883 case '\'':
7884 cap->arg = TRUE;
7885 /*FALLTHROUGH*/
7886 case '`':
7887 nv_gomark(cap);
7888 break;
7891 * "gs": Goto sleep.
7893 case 's':
7894 do_sleep(cap->count1 * 1000L);
7895 break;
7898 * "ga": Display the ascii value of the character under the
7899 * cursor. It is displayed in decimal, hex, and octal. -- webb
7901 case 'a':
7902 do_ascii(NULL);
7903 break;
7905 #ifdef FEAT_MBYTE
7907 * "g8": Display the bytes used for the UTF-8 character under the
7908 * cursor. It is displayed in hex.
7909 * "8g8" finds illegal byte sequence.
7911 case '8':
7912 if (cap->count0 == 8)
7913 utf_find_illegal();
7914 else
7915 show_utf8();
7916 break;
7917 #endif
7919 case '<':
7920 show_sb_text();
7921 break;
7924 * "gg": Goto the first line in file. With a count it goes to
7925 * that line number like for "G". -- webb
7927 case 'g':
7928 cap->arg = FALSE;
7929 nv_goto(cap);
7930 break;
7933 * Two-character operators:
7934 * "gq" Format text
7935 * "gw" Format text and keep cursor position
7936 * "g~" Toggle the case of the text.
7937 * "gu" Change text to lower case.
7938 * "gU" Change text to upper case.
7939 * "g?" rot13 encoding
7940 * "g@" call 'operatorfunc'
7942 case 'q':
7943 case 'w':
7944 oap->cursor_start = curwin->w_cursor;
7945 /*FALLTHROUGH*/
7946 case '~':
7947 case 'u':
7948 case 'U':
7949 case '?':
7950 case '@':
7951 nv_operator(cap);
7952 break;
7955 * "gd": Find first occurence of pattern under the cursor in the
7956 * current function
7957 * "gD": idem, but in the current file.
7959 case 'd':
7960 case 'D':
7961 nv_gd(oap, cap->nchar, (int)cap->count0);
7962 break;
7964 #ifdef FEAT_MOUSE
7966 * g<*Mouse> : <C-*mouse>
7968 case K_MIDDLEMOUSE:
7969 case K_MIDDLEDRAG:
7970 case K_MIDDLERELEASE:
7971 case K_LEFTMOUSE:
7972 case K_LEFTDRAG:
7973 case K_LEFTRELEASE:
7974 case K_RIGHTMOUSE:
7975 case K_RIGHTDRAG:
7976 case K_RIGHTRELEASE:
7977 case K_X1MOUSE:
7978 case K_X1DRAG:
7979 case K_X1RELEASE:
7980 case K_X2MOUSE:
7981 case K_X2DRAG:
7982 case K_X2RELEASE:
7983 mod_mask = MOD_MASK_CTRL;
7984 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
7985 break;
7986 #endif
7988 case K_IGNORE:
7989 break;
7992 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
7994 case 'p':
7995 case 'P':
7996 nv_put(cap);
7997 break;
7999 #ifdef FEAT_BYTEOFF
8000 /* "go": goto byte count from start of buffer */
8001 case 'o':
8002 goto_byte(cap->count0);
8003 break;
8004 #endif
8006 /* "gQ": improved Ex mode */
8007 case 'Q':
8008 if (text_locked())
8010 clearopbeep(cap->oap);
8011 text_locked_msg();
8012 break;
8015 if (!checkclearopq(oap))
8016 do_exmode(TRUE);
8017 break;
8019 #ifdef FEAT_JUMPLIST
8020 case ',':
8021 nv_pcmark(cap);
8022 break;
8024 case ';':
8025 cap->count1 = -cap->count1;
8026 nv_pcmark(cap);
8027 break;
8028 #endif
8030 #ifdef FEAT_WINDOWS
8031 case 't':
8032 goto_tabpage((int)cap->count0);
8033 break;
8034 case 'T':
8035 goto_tabpage(-(int)cap->count1);
8036 break;
8037 #endif
8039 case '+':
8040 case '-': /* "g+" and "g-": undo or redo along the timeline */
8041 if (!checkclearopq(oap))
8042 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
8043 FALSE, FALSE);
8044 break;
8046 default:
8047 clearopbeep(oap);
8048 break;
8053 * Handle "o" and "O" commands.
8055 static void
8056 n_opencmd(cap)
8057 cmdarg_T *cap;
8059 if (!checkclearopq(cap->oap))
8061 #ifdef FEAT_FOLDING
8062 if (cap->cmdchar == 'O')
8063 /* Open above the first line of a folded sequence of lines */
8064 (void)hasFolding(curwin->w_cursor.lnum,
8065 &curwin->w_cursor.lnum, NULL);
8066 else
8067 /* Open below the last line of a folded sequence of lines */
8068 (void)hasFolding(curwin->w_cursor.lnum,
8069 NULL, &curwin->w_cursor.lnum);
8070 #endif
8071 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8072 (cap->cmdchar == 'O' ? 1 : 0)),
8073 (linenr_T)(curwin->w_cursor.lnum +
8074 (cap->cmdchar == 'o' ? 1 : 0))
8075 ) == OK
8076 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8077 #ifdef FEAT_COMMENTS
8078 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8079 #endif
8080 0, 0))
8082 /* When '#' is in 'cpoptions' ignore the count. */
8083 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8084 cap->count1 = 1;
8085 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8091 * "." command: redo last change.
8093 static void
8094 nv_dot(cap)
8095 cmdarg_T *cap;
8097 if (!checkclearopq(cap->oap))
8100 * If "restart_edit" is TRUE, the last but one command is repeated
8101 * instead of the last command (inserting text). This is used for
8102 * CTRL-O <.> in insert mode.
8104 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8105 clearopbeep(cap->oap);
8110 * CTRL-R: undo undo
8112 static void
8113 nv_redo(cap)
8114 cmdarg_T *cap;
8116 if (!checkclearopq(cap->oap))
8118 u_redo((int)cap->count1);
8119 curwin->w_set_curswant = TRUE;
8124 * Handle "U" command.
8126 static void
8127 nv_Undo(cap)
8128 cmdarg_T *cap;
8130 /* In Visual mode and typing "gUU" triggers an operator */
8131 if (cap->oap->op_type == OP_UPPER
8132 #ifdef FEAT_VISUAL
8133 || VIsual_active
8134 #endif
8137 /* translate "gUU" to "gUgU" */
8138 cap->cmdchar = 'g';
8139 cap->nchar = 'U';
8140 nv_operator(cap);
8142 else if (!checkclearopq(cap->oap))
8144 u_undoline();
8145 curwin->w_set_curswant = TRUE;
8150 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8151 * single character.
8153 static void
8154 nv_tilde(cap)
8155 cmdarg_T *cap;
8157 if (!p_to
8158 #ifdef FEAT_VISUAL
8159 && !VIsual_active
8160 #endif
8161 && cap->oap->op_type != OP_TILDE)
8162 n_swapchar(cap);
8163 else
8164 nv_operator(cap);
8168 * Handle an operator command.
8169 * The actual work is done by do_pending_operator().
8171 static void
8172 nv_operator(cap)
8173 cmdarg_T *cap;
8175 int op_type;
8177 op_type = get_op_type(cap->cmdchar, cap->nchar);
8179 if (op_type == cap->oap->op_type) /* double operator works on lines */
8180 nv_lineop(cap);
8181 else if (!checkclearop(cap->oap))
8183 cap->oap->start = curwin->w_cursor;
8184 cap->oap->op_type = op_type;
8189 * Handle linewise operator "dd", "yy", etc.
8191 * "_" is is a strange motion command that helps make operators more logical.
8192 * It is actually implemented, but not documented in the real Vi. This motion
8193 * command actually refers to "the current line". Commands like "dd" and "yy"
8194 * are really an alternate form of "d_" and "y_". It does accept a count, so
8195 * "d3_" works to delete 3 lines.
8197 static void
8198 nv_lineop(cap)
8199 cmdarg_T *cap;
8201 cap->oap->motion_type = MLINE;
8202 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8203 clearopbeep(cap->oap);
8204 else if ( cap->oap->op_type == OP_DELETE
8205 || cap->oap->op_type == OP_LSHIFT
8206 || cap->oap->op_type == OP_RSHIFT)
8207 beginline(BL_SOL | BL_FIX);
8208 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8209 beginline(BL_WHITE | BL_FIX);
8213 * <Home> command.
8215 static void
8216 nv_home(cap)
8217 cmdarg_T *cap;
8219 /* CTRL-HOME is like "gg" */
8220 if (mod_mask & MOD_MASK_CTRL)
8221 nv_goto(cap);
8222 else
8224 cap->count0 = 1;
8225 nv_pipe(cap);
8227 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8228 one-character line). */
8232 * "|" command.
8234 static void
8235 nv_pipe(cap)
8236 cmdarg_T *cap;
8238 cap->oap->motion_type = MCHAR;
8239 cap->oap->inclusive = FALSE;
8240 beginline(0);
8241 if (cap->count0 > 0)
8243 coladvance((colnr_T)(cap->count0 - 1));
8244 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8246 else
8247 curwin->w_curswant = 0;
8248 /* keep curswant at the column where we wanted to go, not where
8249 we ended; differs if line is too short */
8250 curwin->w_set_curswant = FALSE;
8254 * Handle back-word command "b" and "B".
8255 * cap->arg is 1 for "B"
8257 static void
8258 nv_bck_word(cap)
8259 cmdarg_T *cap;
8261 cap->oap->motion_type = MCHAR;
8262 cap->oap->inclusive = FALSE;
8263 curwin->w_set_curswant = TRUE;
8264 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8265 clearopbeep(cap->oap);
8266 #ifdef FEAT_FOLDING
8267 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8268 foldOpenCursor();
8269 #endif
8273 * Handle word motion commands "e", "E", "w" and "W".
8274 * cap->arg is TRUE for "E" and "W".
8276 static void
8277 nv_wordcmd(cap)
8278 cmdarg_T *cap;
8280 int n;
8281 int word_end;
8282 int flag = FALSE;
8285 * Set inclusive for the "E" and "e" command.
8287 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8288 word_end = TRUE;
8289 else
8290 word_end = FALSE;
8291 cap->oap->inclusive = word_end;
8294 * "cw" and "cW" are a special case.
8296 if (!word_end && cap->oap->op_type == OP_CHANGE)
8298 n = gchar_cursor();
8299 if (n != NUL) /* not an empty line */
8301 if (vim_iswhite(n))
8304 * Reproduce a funny Vi behaviour: "cw" on a blank only
8305 * changes one character, not all blanks until the start of
8306 * the next word. Only do this when the 'w' flag is included
8307 * in 'cpoptions'.
8309 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8311 cap->oap->inclusive = TRUE;
8312 cap->oap->motion_type = MCHAR;
8313 return;
8316 else
8319 * This is a little strange. To match what the real Vi does,
8320 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8321 * that we are not on a space or a TAB. This seems impolite
8322 * at first, but it's really more what we mean when we say
8323 * 'cw'.
8324 * Another strangeness: When standing on the end of a word
8325 * "ce" will change until the end of the next wordt, but "cw"
8326 * will change only one character! This is done by setting
8327 * flag.
8329 cap->oap->inclusive = TRUE;
8330 word_end = TRUE;
8331 flag = TRUE;
8336 cap->oap->motion_type = MCHAR;
8337 curwin->w_set_curswant = TRUE;
8338 if (word_end)
8339 n = end_word(cap->count1, cap->arg, flag, FALSE);
8340 else
8341 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8343 /* Don't leave the cursor on the NUL past a line */
8344 if (curwin->w_cursor.col && gchar_cursor() == NUL)
8346 --curwin->w_cursor.col;
8347 cap->oap->inclusive = TRUE;
8350 if (n == FAIL && cap->oap->op_type == OP_NOP)
8351 clearopbeep(cap->oap);
8352 else
8354 #ifdef FEAT_VISUAL
8355 adjust_for_sel(cap);
8356 #endif
8357 #ifdef FEAT_FOLDING
8358 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8359 foldOpenCursor();
8360 #endif
8365 * "0" and "^" commands.
8366 * cap->arg is the argument for beginline().
8368 static void
8369 nv_beginline(cap)
8370 cmdarg_T *cap;
8372 cap->oap->motion_type = MCHAR;
8373 cap->oap->inclusive = FALSE;
8374 beginline(cap->arg);
8375 #ifdef FEAT_FOLDING
8376 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8377 foldOpenCursor();
8378 #endif
8379 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8380 one-character line). */
8383 #ifdef FEAT_VISUAL
8385 * In exclusive Visual mode, may include the last character.
8387 static void
8388 adjust_for_sel(cap)
8389 cmdarg_T *cap;
8391 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8392 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8394 # ifdef FEAT_MBYTE
8395 if (has_mbyte)
8396 inc_cursor();
8397 else
8398 # endif
8399 ++curwin->w_cursor.col;
8400 cap->oap->inclusive = FALSE;
8405 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8406 * Should check VIsual_mode before calling this.
8407 * Returns TRUE when backed up to the previous line.
8409 static int
8410 unadjust_for_sel()
8412 pos_T *pp;
8414 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8416 if (lt(VIsual, curwin->w_cursor))
8417 pp = &curwin->w_cursor;
8418 else
8419 pp = &VIsual;
8420 #ifdef FEAT_VIRTUALEDIT
8421 if (pp->coladd > 0)
8422 --pp->coladd;
8423 else
8424 #endif
8425 if (pp->col > 0)
8427 --pp->col;
8428 #ifdef FEAT_MBYTE
8429 mb_adjustpos(pp);
8430 #endif
8432 else if (pp->lnum > 1)
8434 --pp->lnum;
8435 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8436 return TRUE;
8439 return FALSE;
8443 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8445 static void
8446 nv_select(cap)
8447 cmdarg_T *cap;
8449 if (VIsual_active)
8450 VIsual_select = TRUE;
8451 else if (VIsual_reselect)
8453 cap->nchar = 'v'; /* fake "gv" command */
8454 cap->arg = TRUE;
8455 nv_g_cmd(cap);
8459 #endif
8462 * "G", "gg", CTRL-END, CTRL-HOME.
8463 * cap->arg is TRUE for "G".
8465 static void
8466 nv_goto(cap)
8467 cmdarg_T *cap;
8469 linenr_T lnum;
8471 if (cap->arg)
8472 lnum = curbuf->b_ml.ml_line_count;
8473 else
8474 lnum = 1L;
8475 cap->oap->motion_type = MLINE;
8476 setpcmark();
8478 /* When a count is given, use it instead of the default lnum */
8479 if (cap->count0 != 0)
8480 lnum = cap->count0;
8481 if (lnum < 1L)
8482 lnum = 1L;
8483 else if (lnum > curbuf->b_ml.ml_line_count)
8484 lnum = curbuf->b_ml.ml_line_count;
8485 curwin->w_cursor.lnum = lnum;
8486 beginline(BL_SOL | BL_FIX);
8487 #ifdef FEAT_FOLDING
8488 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8489 foldOpenCursor();
8490 #endif
8494 * CTRL-\ in Normal mode.
8496 static void
8497 nv_normal(cap)
8498 cmdarg_T *cap;
8500 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8502 clearop(cap->oap);
8503 if (restart_edit != 0 && mode_displayed)
8504 clear_cmdline = TRUE; /* unshow mode later */
8505 restart_edit = 0;
8506 #ifdef FEAT_CMDWIN
8507 if (cmdwin_type != 0)
8508 cmdwin_result = Ctrl_C;
8509 #endif
8510 #ifdef FEAT_VISUAL
8511 if (VIsual_active)
8513 end_visual_mode(); /* stop Visual */
8514 redraw_curbuf_later(INVERTED);
8516 #endif
8517 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8518 if (cap->nchar == Ctrl_G && p_im)
8519 restart_edit = 'a';
8521 else
8522 clearopbeep(cap->oap);
8526 * ESC in Normal mode: beep, but don't flush buffers.
8527 * Don't even beep if we are canceling a command.
8529 static void
8530 nv_esc(cap)
8531 cmdarg_T *cap;
8533 int no_reason;
8535 no_reason = (cap->oap->op_type == OP_NOP
8536 && cap->opcount == 0
8537 && cap->count0 == 0
8538 && cap->oap->regname == 0
8539 && !p_im);
8541 if (cap->arg) /* TRUE for CTRL-C */
8543 if (restart_edit == 0
8544 #ifdef FEAT_CMDWIN
8545 && cmdwin_type == 0
8546 #endif
8547 #ifdef FEAT_VISUAL
8548 && !VIsual_active
8549 #endif
8550 && no_reason)
8551 MSG(_("Type :quit<Enter> to exit Vim"));
8553 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8554 * set again below when halfway a mapping. */
8555 if (!p_im)
8556 restart_edit = 0;
8557 #ifdef FEAT_CMDWIN
8558 if (cmdwin_type != 0)
8560 cmdwin_result = K_IGNORE;
8561 got_int = FALSE; /* don't stop executing autocommands et al. */
8562 return;
8564 #endif
8567 #ifdef FEAT_VISUAL
8568 if (VIsual_active)
8570 end_visual_mode(); /* stop Visual */
8571 check_cursor_col(); /* make sure cursor is not beyond EOL */
8572 curwin->w_set_curswant = TRUE;
8573 redraw_curbuf_later(INVERTED);
8575 else
8576 #endif
8577 if (no_reason)
8578 vim_beep();
8579 clearop(cap->oap);
8581 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8582 * set return to Insert mode afterwards. */
8583 if (restart_edit == 0 && goto_im()
8584 #ifdef FEAT_EX_EXTRA
8585 && ex_normal_busy == 0
8586 #endif
8588 restart_edit = 'a';
8592 * Handle "A", "a", "I", "i" and <Insert> commands.
8594 static void
8595 nv_edit(cap)
8596 cmdarg_T *cap;
8598 /* <Insert> is equal to "i" */
8599 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8600 cap->cmdchar = 'i';
8602 #ifdef FEAT_VISUAL
8603 /* in Visual mode "A" and "I" are an operator */
8604 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8605 v_visop(cap);
8607 /* in Visual mode and after an operator "a" and "i" are for text objects */
8608 else
8609 #endif
8610 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8611 && (cap->oap->op_type != OP_NOP
8612 #ifdef FEAT_VISUAL
8613 || VIsual_active
8614 #endif
8617 #ifdef FEAT_TEXTOBJ
8618 nv_object(cap);
8619 #else
8620 clearopbeep(cap->oap);
8621 #endif
8623 else if (!curbuf->b_p_ma && !p_im)
8625 /* Only give this error when 'insertmode' is off. */
8626 EMSG(_(e_modifiable));
8627 clearop(cap->oap);
8629 else if (!checkclearopq(cap->oap))
8631 switch (cap->cmdchar)
8633 case 'A': /* "A"ppend after the line */
8634 curwin->w_set_curswant = TRUE;
8635 #ifdef FEAT_VIRTUALEDIT
8636 if (ve_flags == VE_ALL)
8638 int save_State = State;
8640 /* Pretent Insert mode here to allow the cursor on the
8641 * character past the end of the line */
8642 State = INSERT;
8643 coladvance((colnr_T)MAXCOL);
8644 State = save_State;
8646 else
8647 #endif
8648 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8649 break;
8651 case 'I': /* "I"nsert before the first non-blank */
8652 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
8653 beginline(BL_WHITE);
8654 else
8655 beginline(BL_WHITE|BL_FIX);
8656 break;
8658 case 'a': /* "a"ppend is like "i"nsert on the next character. */
8659 #ifdef FEAT_VIRTUALEDIT
8660 /* increment coladd when in virtual space, increment the
8661 * column otherwise, also to append after an unprintable char */
8662 if (virtual_active()
8663 && (curwin->w_cursor.coladd > 0
8664 || *ml_get_cursor() == NUL
8665 || *ml_get_cursor() == TAB))
8666 curwin->w_cursor.coladd++;
8667 else
8668 #endif
8669 if (*ml_get_cursor() != NUL)
8670 inc_cursor();
8671 break;
8674 #ifdef FEAT_VIRTUALEDIT
8675 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
8677 int save_State = State;
8679 /* Pretent Insert mode here to allow the cursor on the
8680 * character past the end of the line */
8681 State = INSERT;
8682 coladvance(getviscol());
8683 State = save_State;
8685 #endif
8687 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
8692 * Invoke edit() and take care of "restart_edit" and the return value.
8694 static void
8695 invoke_edit(cap, repl, cmd, startln)
8696 cmdarg_T *cap;
8697 int repl; /* "r" or "gr" command */
8698 int cmd;
8699 int startln;
8701 int restart_edit_save = 0;
8703 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
8704 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
8705 * it. */
8706 if (repl || !stuff_empty())
8707 restart_edit_save = restart_edit;
8708 else
8709 restart_edit_save = 0;
8711 /* Always reset "restart_edit", this is not a restarted edit. */
8712 restart_edit = 0;
8714 if (edit(cmd, startln, cap->count1))
8715 cap->retval |= CA_COMMAND_BUSY;
8717 if (restart_edit == 0)
8718 restart_edit = restart_edit_save;
8721 #ifdef FEAT_TEXTOBJ
8723 * "a" or "i" while an operator is pending or in Visual mode: object motion.
8725 static void
8726 nv_object(cap)
8727 cmdarg_T *cap;
8729 int flag;
8730 int include;
8731 char_u *mps_save;
8733 if (cap->cmdchar == 'i')
8734 include = FALSE; /* "ix" = inner object: exclude white space */
8735 else
8736 include = TRUE; /* "ax" = an object: include white space */
8738 /* Make sure (), [], {} and <> are in 'matchpairs' */
8739 mps_save = curbuf->b_p_mps;
8740 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
8742 switch (cap->nchar)
8744 case 'w': /* "aw" = a word */
8745 flag = current_word(cap->oap, cap->count1, include, FALSE);
8746 break;
8747 case 'W': /* "aW" = a WORD */
8748 flag = current_word(cap->oap, cap->count1, include, TRUE);
8749 break;
8750 case 'b': /* "ab" = a braces block */
8751 case '(':
8752 case ')':
8753 flag = current_block(cap->oap, cap->count1, include, '(', ')');
8754 break;
8755 case 'B': /* "aB" = a Brackets block */
8756 case '{':
8757 case '}':
8758 flag = current_block(cap->oap, cap->count1, include, '{', '}');
8759 break;
8760 case '[': /* "a[" = a [] block */
8761 case ']':
8762 flag = current_block(cap->oap, cap->count1, include, '[', ']');
8763 break;
8764 case '<': /* "a<" = a <> block */
8765 case '>':
8766 flag = current_block(cap->oap, cap->count1, include, '<', '>');
8767 break;
8768 case 't': /* "at" = a tag block (xml and html) */
8769 flag = current_tagblock(cap->oap, cap->count1, include);
8770 break;
8771 case 'p': /* "ap" = a paragraph */
8772 flag = current_par(cap->oap, cap->count1, include, 'p');
8773 break;
8774 case 's': /* "as" = a sentence */
8775 flag = current_sent(cap->oap, cap->count1, include);
8776 break;
8777 case '"': /* "a"" = a double quoted string */
8778 case '\'': /* "a'" = a single quoted string */
8779 case '`': /* "a`" = a backtick quoted string */
8780 flag = current_quote(cap->oap, cap->count1, include,
8781 cap->nchar);
8782 break;
8783 #if 0 /* TODO */
8784 case 'S': /* "aS" = a section */
8785 case 'f': /* "af" = a filename */
8786 case 'u': /* "au" = a URL */
8787 #endif
8788 default:
8789 flag = FAIL;
8790 break;
8793 curbuf->b_p_mps = mps_save;
8794 if (flag == FAIL)
8795 clearopbeep(cap->oap);
8796 adjust_cursor_col();
8797 curwin->w_set_curswant = TRUE;
8799 #endif
8802 * "q" command: Start/stop recording.
8803 * "q:", "q/", "q?": edit command-line in command-line window.
8805 static void
8806 nv_record(cap)
8807 cmdarg_T *cap;
8809 if (cap->oap->op_type == OP_FORMAT)
8811 /* "gqq" is the same as "gqgq": format line */
8812 cap->cmdchar = 'g';
8813 cap->nchar = 'q';
8814 nv_operator(cap);
8816 else if (!checkclearop(cap->oap))
8818 #ifdef FEAT_CMDWIN
8819 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
8821 stuffcharReadbuff(cap->nchar);
8822 stuffcharReadbuff(K_CMDWIN);
8824 else
8825 #endif
8826 /* (stop) recording into a named register, unless executing a
8827 * register */
8828 if (!Exec_reg && do_record(cap->nchar) == FAIL)
8829 clearopbeep(cap->oap);
8834 * Handle the "@r" command.
8836 static void
8837 nv_at(cap)
8838 cmdarg_T *cap;
8840 if (checkclearop(cap->oap))
8841 return;
8842 #ifdef FEAT_EVAL
8843 if (cap->nchar == '=')
8845 if (get_expr_register() == NUL)
8846 return;
8848 #endif
8849 while (cap->count1-- && !got_int)
8851 if (do_execreg(cap->nchar, FALSE, FALSE) == FAIL)
8853 clearopbeep(cap->oap);
8854 break;
8856 line_breakcheck();
8861 * Handle the CTRL-U and CTRL-D commands.
8863 static void
8864 nv_halfpage(cap)
8865 cmdarg_T *cap;
8867 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
8868 || (cap->cmdchar == Ctrl_D
8869 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
8870 clearopbeep(cap->oap);
8871 else if (!checkclearop(cap->oap))
8872 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
8876 * Handle "J" or "gJ" command.
8878 static void
8879 nv_join(cap)
8880 cmdarg_T *cap;
8882 #ifdef FEAT_VISUAL
8883 if (VIsual_active) /* join the visual lines */
8884 nv_operator(cap);
8885 else
8886 #endif
8887 if (!checkclearop(cap->oap))
8889 if (cap->count0 <= 1)
8890 cap->count0 = 2; /* default for join is two lines! */
8891 if (curwin->w_cursor.lnum + cap->count0 - 1 >
8892 curbuf->b_ml.ml_line_count)
8893 clearopbeep(cap->oap); /* beyond last line */
8894 else
8896 prep_redo(cap->oap->regname, cap->count0,
8897 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
8898 do_do_join(cap->count0, cap->nchar == NUL);
8904 * "P", "gP", "p" and "gp" commands.
8906 static void
8907 nv_put(cap)
8908 cmdarg_T *cap;
8910 #ifdef FEAT_VISUAL
8911 int regname = 0;
8912 void *reg1 = NULL, *reg2 = NULL;
8913 int empty = FALSE;
8914 int was_visual = FALSE;
8915 #endif
8916 int dir;
8917 int flags = 0;
8919 if (cap->oap->op_type != OP_NOP)
8921 #ifdef FEAT_DIFF
8922 /* "dp" is ":diffput" */
8923 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
8925 clearop(cap->oap);
8926 nv_diffgetput(TRUE);
8928 else
8929 #endif
8930 clearopbeep(cap->oap);
8932 else
8934 dir = (cap->cmdchar == 'P'
8935 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
8936 ? BACKWARD : FORWARD;
8937 prep_redo_cmd(cap);
8938 if (cap->cmdchar == 'g')
8939 flags |= PUT_CURSEND;
8941 #ifdef FEAT_VISUAL
8942 if (VIsual_active)
8944 /* Putting in Visual mode: The put text replaces the selected
8945 * text. First delete the selected text, then put the new text.
8946 * Need to save and restore the registers that the delete
8947 * overwrites if the old contents is being put.
8949 was_visual = TRUE;
8950 regname = cap->oap->regname;
8951 # ifdef FEAT_CLIPBOARD
8952 adjust_clip_reg(&regname);
8953 # endif
8954 if (regname == 0 || VIM_ISDIGIT(regname)
8955 # ifdef FEAT_CLIPBOARD
8956 || (clip_unnamed && (regname == '*' || regname == '+'))
8957 # endif
8961 /* the delete is going to overwrite the register we want to
8962 * put, save it first. */
8963 reg1 = get_register(regname, TRUE);
8966 /* Now delete the selected text. */
8967 cap->cmdchar = 'd';
8968 cap->nchar = NUL;
8969 cap->oap->regname = NUL;
8970 nv_operator(cap);
8971 do_pending_operator(cap, 0, FALSE);
8972 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8974 /* delete PUT_LINE_BACKWARD; */
8975 cap->oap->regname = regname;
8977 if (reg1 != NULL)
8979 /* Delete probably changed the register we want to put, save
8980 * it first. Then put back what was there before the delete. */
8981 reg2 = get_register(regname, FALSE);
8982 put_register(regname, reg1);
8985 /* When deleted a linewise Visual area, put the register as
8986 * lines to avoid it joined with the next line. When deletion was
8987 * characterwise, split a line when putting lines. */
8988 if (VIsual_mode == 'V')
8989 flags |= PUT_LINE;
8990 else if (VIsual_mode == 'v')
8991 flags |= PUT_LINE_SPLIT;
8992 if (VIsual_mode == Ctrl_V && dir == FORWARD)
8993 flags |= PUT_LINE_FORWARD;
8994 dir = BACKWARD;
8995 if ((VIsual_mode != 'V'
8996 && curwin->w_cursor.col < curbuf->b_op_start.col)
8997 || (VIsual_mode == 'V'
8998 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
8999 /* cursor is at the end of the line or end of file, put
9000 * forward. */
9001 dir = FORWARD;
9003 #endif
9004 do_put(cap->oap->regname, dir, cap->count1, flags);
9006 #ifdef FEAT_VISUAL
9007 /* If a register was saved, put it back now. */
9008 if (reg2 != NULL)
9009 put_register(regname, reg2);
9011 /* What to reselect with "gv"? Selecting the just put text seems to
9012 * be the most useful, since the original text was removed. */
9013 if (was_visual)
9015 curbuf->b_visual.vi_start = curbuf->b_op_start;
9016 curbuf->b_visual.vi_end = curbuf->b_op_end;
9019 /* When all lines were selected and deleted do_put() leaves an empty
9020 * line that needs to be deleted now. */
9021 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
9023 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
9025 /* If the cursor was in that line, move it to the end of the last
9026 * line. */
9027 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9029 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9030 coladvance((colnr_T)MAXCOL);
9033 #endif
9034 auto_format(FALSE, TRUE);
9039 * "o" and "O" commands.
9041 static void
9042 nv_open(cap)
9043 cmdarg_T *cap;
9045 #ifdef FEAT_DIFF
9046 /* "do" is ":diffget" */
9047 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9049 clearop(cap->oap);
9050 nv_diffgetput(FALSE);
9052 else
9053 #endif
9054 #ifdef FEAT_VISUAL
9055 if (VIsual_active) /* switch start and end of visual */
9056 v_swap_corners(cap->cmdchar);
9057 else
9058 #endif
9059 n_opencmd(cap);
9062 #ifdef FEAT_SNIFF
9063 /*ARGSUSED*/
9064 static void
9065 nv_sniff(cap)
9066 cmdarg_T *cap;
9068 ProcessSniffRequests();
9070 #endif
9072 #ifdef FEAT_NETBEANS_INTG
9073 static void
9074 nv_nbcmd(cap)
9075 cmdarg_T *cap;
9077 netbeans_keycommand(cap->nchar);
9079 #endif
9081 #ifdef FEAT_DND
9082 /*ARGSUSED*/
9083 static void
9084 nv_drop(cap)
9085 cmdarg_T *cap;
9087 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9089 #endif
9091 #ifdef FEAT_AUTOCMD
9093 * Trigger CursorHold event.
9094 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9095 * input buffer. "did_cursorhold" is set to avoid retriggering.
9097 /*ARGSUSED*/
9098 static void
9099 nv_cursorhold(cap)
9100 cmdarg_T *cap;
9102 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9103 did_cursorhold = TRUE;
9104 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
9106 #endif