Enable trackpad scrolling over the black area in full-screen
[MacVim/jjgod.git] / src / normal.c
blob30a45a3e5c6df82f6af8d60a030f9b50abd4f33a
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 can
655 * be mapped in Insert mode. Required for ":lmap" to work. */
656 ins_char_typebuf(c);
657 if (restart_edit != 0)
658 c = 'd';
659 else
660 c = 'c';
661 msg_nowait = TRUE; /* don't delay going to insert mode */
663 #endif
665 #ifdef FEAT_CMDL_INFO
666 need_flushbuf = add_to_showcmd(c);
667 #endif
669 getcount:
670 #ifdef FEAT_VISUAL
671 if (!(VIsual_active && VIsual_select))
672 #endif
675 * Handle a count before a command and compute ca.count0.
676 * Note that '0' is a command and not the start of a count, but it's
677 * part of a count after other digits.
679 while ( (c >= '1' && c <= '9')
680 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
682 if (c == K_DEL || c == K_KDEL)
684 ca.count0 /= 10;
685 #ifdef FEAT_CMDL_INFO
686 del_from_showcmd(4); /* delete the digit and ~@% */
687 #endif
689 else
690 ca.count0 = ca.count0 * 10 + (c - '0');
691 if (ca.count0 < 0) /* got too large! */
692 ca.count0 = 999999999L;
693 #ifdef FEAT_EVAL
694 /* Set v:count here, when called from main() and not a stuffed
695 * command, so that v:count can be used in an expression mapping
696 * right after the count. */
697 if (toplevel && stuff_empty())
698 set_vcount(ca.count0, ca.count0 == 0 ? 1 : ca.count0);
699 #endif
700 if (ctrl_w)
702 ++no_mapping;
703 ++allow_keys; /* no mapping for nchar, but keys */
705 ++no_zero_mapping; /* don't map zero here */
706 c = plain_vgetc();
707 #ifdef FEAT_LANGMAP
708 LANGMAP_ADJUST(c, TRUE);
709 #endif
710 --no_zero_mapping;
711 if (ctrl_w)
713 --no_mapping;
714 --allow_keys;
716 #ifdef FEAT_CMDL_INFO
717 need_flushbuf |= add_to_showcmd(c);
718 #endif
722 * If we got CTRL-W there may be a/another count
724 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
726 ctrl_w = TRUE;
727 ca.opcount = ca.count0; /* remember first count */
728 ca.count0 = 0;
729 ++no_mapping;
730 ++allow_keys; /* no mapping for nchar, but keys */
731 c = plain_vgetc(); /* get next character */
732 #ifdef FEAT_LANGMAP
733 LANGMAP_ADJUST(c, TRUE);
734 #endif
735 --no_mapping;
736 --allow_keys;
737 #ifdef FEAT_CMDL_INFO
738 need_flushbuf |= add_to_showcmd(c);
739 #endif
740 goto getcount; /* jump back */
745 * If we're in the middle of an operator (including after entering a yank
746 * buffer with '"') AND we had a count before the operator, then that
747 * count overrides the current value of ca.count0.
748 * What this means effectively, is that commands like "3dw" get turned
749 * into "d3w" which makes things fall into place pretty neatly.
750 * If you give a count before AND after the operator, they are multiplied.
752 if (ca.opcount != 0)
754 if (ca.count0)
755 ca.count0 *= ca.opcount;
756 else
757 ca.count0 = ca.opcount;
761 * Always remember the count. It will be set to zero (on the next call,
762 * above) when there is no pending operator.
763 * When called from main(), save the count for use by the "count" built-in
764 * variable.
766 ca.opcount = ca.count0;
767 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
769 #ifdef FEAT_EVAL
771 * Only set v:count when called from main() and not a stuffed command.
773 if (toplevel && stuff_empty())
774 set_vcount(ca.count0, ca.count1);
775 #endif
778 * Find the command character in the table of commands.
779 * For CTRL-W we already got nchar when looking for a count.
781 if (ctrl_w)
783 ca.nchar = c;
784 ca.cmdchar = Ctrl_W;
786 else
787 ca.cmdchar = c;
788 idx = find_command(ca.cmdchar);
789 if (idx < 0)
791 /* Not a known command: beep. */
792 clearopbeep(oap);
793 goto normal_end;
796 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
798 /* This command is not allowed wile editing a ccmdline: beep. */
799 clearopbeep(oap);
800 text_locked_msg();
801 goto normal_end;
803 #ifdef FEAT_AUTOCMD
804 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
805 goto normal_end;
806 #endif
808 #ifdef FEAT_VISUAL
810 * In Visual/Select mode, a few keys are handled in a special way.
812 if (VIsual_active)
814 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
815 if (km_stopsel
816 && (nv_cmds[idx].cmd_flags & NV_STS)
817 && !(mod_mask & MOD_MASK_SHIFT))
819 end_visual_mode();
820 redraw_curbuf_later(INVERTED);
823 /* Keys that work different when 'keymodel' contains "startsel" */
824 if (km_startsel)
826 if (nv_cmds[idx].cmd_flags & NV_SS)
828 unshift_special(&ca);
829 idx = find_command(ca.cmdchar);
830 if (idx < 0)
832 /* Just in case */
833 clearopbeep(oap);
834 goto normal_end;
837 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
838 && (mod_mask & MOD_MASK_SHIFT))
840 mod_mask &= ~MOD_MASK_SHIFT;
844 #endif
846 #ifdef FEAT_RIGHTLEFT
847 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
848 && (nv_cmds[idx].cmd_flags & NV_RL))
850 /* Invert horizontal movements and operations. Only when typed by the
851 * user directly, not when the result of a mapping or "x" translated
852 * to "dl". */
853 switch (ca.cmdchar)
855 case 'l': ca.cmdchar = 'h'; break;
856 case K_RIGHT: ca.cmdchar = K_LEFT; break;
857 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
858 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
859 case 'h': ca.cmdchar = 'l'; break;
860 case K_LEFT: ca.cmdchar = K_RIGHT; break;
861 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
862 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
863 case '>': ca.cmdchar = '<'; break;
864 case '<': ca.cmdchar = '>'; break;
866 idx = find_command(ca.cmdchar);
868 #endif
871 * Get an additional character if we need one.
873 if ((nv_cmds[idx].cmd_flags & NV_NCH)
874 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
875 && oap->op_type == OP_NOP)
876 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
877 || (ca.cmdchar == 'q'
878 && oap->op_type == OP_NOP
879 && !Recording
880 && !Exec_reg)
881 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
882 && (oap->op_type != OP_NOP
883 #ifdef FEAT_VISUAL
884 || VIsual_active
885 #endif
886 ))))
888 int *cp;
889 int repl = FALSE; /* get character for replace mode */
890 int lit = FALSE; /* get extra character literally */
891 int langmap_active = FALSE; /* using :lmap mappings */
892 int lang; /* getting a text character */
893 #ifdef USE_IM_CONTROL
894 int save_smd; /* saved value of p_smd */
895 #endif
897 ++no_mapping;
898 ++allow_keys; /* no mapping for nchar, but allow key codes */
899 #ifdef FEAT_AUTOCMD
900 /* Don't generate a CursorHold event here, most commands can't handle
901 * it, e.g., nv_replace(), nv_csearch(). */
902 did_cursorhold = TRUE;
903 #endif
904 if (ca.cmdchar == 'g')
907 * For 'g' get the next character now, so that we can check for
908 * "gr", "g'" and "g`".
910 ca.nchar = plain_vgetc();
911 #ifdef FEAT_LANGMAP
912 LANGMAP_ADJUST(ca.nchar, TRUE);
913 #endif
914 #ifdef FEAT_CMDL_INFO
915 need_flushbuf |= add_to_showcmd(ca.nchar);
916 #endif
917 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
918 || ca.nchar == Ctrl_BSL)
920 cp = &ca.extra_char; /* need to get a third character */
921 if (ca.nchar != 'r')
922 lit = TRUE; /* get it literally */
923 else
924 repl = TRUE; /* get it in replace mode */
926 else
927 cp = NULL; /* no third character needed */
929 else
931 if (ca.cmdchar == 'r') /* get it in replace mode */
932 repl = TRUE;
933 cp = &ca.nchar;
935 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
938 * Get a second or third character.
940 if (cp != NULL)
942 #ifdef CURSOR_SHAPE
943 if (repl)
945 State = REPLACE; /* pretend Replace mode */
946 ui_cursor_shape(); /* show different cursor shape */
948 #endif
949 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
951 /* Allow mappings defined with ":lmap". */
952 --no_mapping;
953 --allow_keys;
954 if (repl)
955 State = LREPLACE;
956 else
957 State = LANGMAP;
958 langmap_active = TRUE;
960 #ifdef USE_IM_CONTROL
961 save_smd = p_smd;
962 p_smd = FALSE; /* Don't let the IM code show the mode here */
963 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
964 im_set_active(TRUE);
965 #endif
967 *cp = plain_vgetc();
969 if (langmap_active)
971 /* Undo the decrement done above */
972 ++no_mapping;
973 ++allow_keys;
974 State = NORMAL_BUSY;
976 #ifdef USE_IM_CONTROL
977 if (lang)
979 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
980 im_save_status(&curbuf->b_p_iminsert);
981 im_set_active(FALSE);
983 p_smd = save_smd;
984 #endif
985 #ifdef CURSOR_SHAPE
986 State = NORMAL_BUSY;
987 #endif
988 #ifdef FEAT_CMDL_INFO
989 need_flushbuf |= add_to_showcmd(*cp);
990 #endif
992 if (!lit)
994 #ifdef FEAT_DIGRAPHS
995 /* Typing CTRL-K gets a digraph. */
996 if (*cp == Ctrl_K
997 && ((nv_cmds[idx].cmd_flags & NV_LANG)
998 || cp == &ca.extra_char)
999 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1001 c = get_digraph(FALSE);
1002 if (c > 0)
1004 *cp = c;
1005 # ifdef FEAT_CMDL_INFO
1006 /* Guessing how to update showcmd here... */
1007 del_from_showcmd(3);
1008 need_flushbuf |= add_to_showcmd(*cp);
1009 # endif
1012 #endif
1014 #ifdef FEAT_LANGMAP
1015 /* adjust chars > 127, except after "tTfFr" commands */
1016 LANGMAP_ADJUST(*cp, !lang);
1017 #endif
1018 #ifdef FEAT_RIGHTLEFT
1019 /* adjust Hebrew mapped char */
1020 if (p_hkmap && lang && KeyTyped)
1021 *cp = hkmap(*cp);
1022 # ifdef FEAT_FKMAP
1023 /* adjust Farsi mapped char */
1024 if (p_fkmap && lang && KeyTyped)
1025 *cp = fkmap(*cp);
1026 # endif
1027 #endif
1031 * When the next character is CTRL-\ a following CTRL-N means the
1032 * command is aborted and we go to Normal mode.
1034 if (cp == &ca.extra_char
1035 && ca.nchar == Ctrl_BSL
1036 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1038 ca.cmdchar = Ctrl_BSL;
1039 ca.nchar = ca.extra_char;
1040 idx = find_command(ca.cmdchar);
1042 else if (*cp == Ctrl_BSL)
1044 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1046 /* There is a busy wait here when typing "f<C-\>" and then
1047 * something different from CTRL-N. Can't be avoided. */
1048 while ((c = vpeekc()) <= 0 && towait > 0L)
1050 do_sleep(towait > 50L ? 50L : towait);
1051 towait -= 50L;
1053 if (c > 0)
1055 c = plain_vgetc();
1056 if (c != Ctrl_N && c != Ctrl_G)
1057 vungetc(c);
1058 else
1060 ca.cmdchar = Ctrl_BSL;
1061 ca.nchar = c;
1062 idx = find_command(ca.cmdchar);
1067 #ifdef FEAT_MBYTE
1068 /* When getting a text character and the next character is a
1069 * multi-byte character, it could be a composing character.
1070 * However, don't wait for it to arrive. */
1071 while (enc_utf8 && lang && (c = vpeekc()) > 0
1072 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1074 c = plain_vgetc();
1075 if (!utf_iscomposing(c))
1077 vungetc(c); /* it wasn't, put it back */
1078 break;
1080 else if (ca.ncharC1 == 0)
1081 ca.ncharC1 = c;
1082 else
1083 ca.ncharC2 = c;
1085 #endif
1087 --no_mapping;
1088 --allow_keys;
1091 #ifdef FEAT_CMDL_INFO
1093 * Flush the showcmd characters onto the screen so we can see them while
1094 * the command is being executed. Only do this when the shown command was
1095 * actually displayed, otherwise this will slow down a lot when executing
1096 * mappings.
1098 if (need_flushbuf)
1099 out_flush();
1100 #endif
1101 #ifdef FEAT_AUTOCMD
1102 did_cursorhold = FALSE;
1103 #endif
1105 State = NORMAL;
1107 if (ca.nchar == ESC)
1109 clearop(oap);
1110 if (restart_edit == 0 && goto_im())
1111 restart_edit = 'a';
1112 goto normal_end;
1115 if (ca.cmdchar != K_IGNORE)
1117 msg_didout = FALSE; /* don't scroll screen up for normal command */
1118 msg_col = 0;
1121 #ifdef FEAT_VISUAL
1122 old_pos = curwin->w_cursor; /* remember where cursor was */
1124 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1125 * mode. */
1126 if (!VIsual_active && km_startsel)
1128 if (nv_cmds[idx].cmd_flags & NV_SS)
1130 start_selection();
1131 unshift_special(&ca);
1132 idx = find_command(ca.cmdchar);
1134 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1135 && (mod_mask & MOD_MASK_SHIFT))
1137 start_selection();
1138 mod_mask &= ~MOD_MASK_SHIFT;
1141 #endif
1144 * Execute the command!
1145 * Call the command function found in the commands table.
1147 ca.arg = nv_cmds[idx].cmd_arg;
1148 (nv_cmds[idx].cmd_func)(&ca);
1151 * If we didn't start or finish an operator, reset oap->regname, unless we
1152 * need it later.
1154 if (!finish_op
1155 && !oap->op_type
1156 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1158 clearop(oap);
1159 #ifdef FEAT_EVAL
1160 set_reg_var('"');
1161 #endif
1164 #ifdef FEAT_VISUAL
1165 /* Get the length of mapped chars again after typing a count, second
1166 * character or "z333<cr>". */
1167 if (old_mapped_len > 0)
1168 old_mapped_len = typebuf_maplen();
1169 #endif
1172 * If an operation is pending, handle it...
1174 do_pending_operator(&ca, old_col, FALSE);
1177 * Wait for a moment when a message is displayed that will be overwritten
1178 * by the mode message.
1179 * In Visual mode and with "^O" in Insert mode, a short message will be
1180 * overwritten by the mode message. Wait a bit, until a key is hit.
1181 * In Visual mode, it's more important to keep the Visual area updated
1182 * than keeping a message (e.g. from a /pat search).
1183 * Only do this if the command was typed, not from a mapping.
1184 * Don't wait when emsg_silent is non-zero.
1185 * Also wait a bit after an error message, e.g. for "^O:".
1186 * Don't redraw the screen, it would remove the message.
1188 if ( ((p_smd
1189 && msg_silent == 0
1190 && (restart_edit != 0
1191 #ifdef FEAT_VISUAL
1192 || (VIsual_active
1193 && old_pos.lnum == curwin->w_cursor.lnum
1194 && old_pos.col == curwin->w_cursor.col)
1195 #endif
1197 && (clear_cmdline
1198 || redraw_cmdline)
1199 && (msg_didout || (msg_didany && msg_scroll))
1200 && !msg_nowait
1201 && KeyTyped)
1202 || (restart_edit != 0
1203 #ifdef FEAT_VISUAL
1204 && !VIsual_active
1205 #endif
1206 && (msg_scroll
1207 || emsg_on_display)))
1208 && oap->regname == 0
1209 && !(ca.retval & CA_COMMAND_BUSY)
1210 && stuff_empty()
1211 && typebuf_typed()
1212 && emsg_silent == 0
1213 && !did_wait_return
1214 && oap->op_type == OP_NOP)
1216 int save_State = State;
1218 /* Draw the cursor with the right shape here */
1219 if (restart_edit != 0)
1220 State = INSERT;
1222 /* If need to redraw, and there is a "keep_msg", redraw before the
1223 * delay */
1224 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1226 char_u *kmsg;
1228 kmsg = keep_msg;
1229 keep_msg = NULL;
1230 /* showmode() will clear keep_msg, but we want to use it anyway */
1231 update_screen(0);
1232 /* now reset it, otherwise it's put in the history again */
1233 keep_msg = kmsg;
1234 msg_attr(kmsg, keep_msg_attr);
1235 vim_free(kmsg);
1237 setcursor();
1238 cursor_on();
1239 out_flush();
1240 if (msg_scroll || emsg_on_display)
1241 ui_delay(1000L, TRUE); /* wait at least one second */
1242 ui_delay(3000L, FALSE); /* wait up to three seconds */
1243 State = save_State;
1245 msg_scroll = FALSE;
1246 emsg_on_display = FALSE;
1250 * Finish up after executing a Normal mode command.
1252 normal_end:
1254 msg_nowait = FALSE;
1256 /* Reset finish_op, in case it was set */
1257 #ifdef CURSOR_SHAPE
1258 c = finish_op;
1259 #endif
1260 finish_op = FALSE;
1261 #ifdef CURSOR_SHAPE
1262 /* Redraw the cursor with another shape, if we were in Operator-pending
1263 * mode or did a replace command. */
1264 if (c || ca.cmdchar == 'r')
1266 ui_cursor_shape(); /* may show different cursor shape */
1267 # ifdef FEAT_MOUSESHAPE
1268 update_mouseshape(-1);
1269 # endif
1271 #endif
1273 #ifdef FEAT_CMDL_INFO
1274 if (oap->op_type == OP_NOP && oap->regname == 0)
1275 clear_showcmd();
1276 #endif
1278 checkpcmark(); /* check if we moved since setting pcmark */
1279 vim_free(ca.searchbuf);
1281 #ifdef FEAT_MBYTE
1282 if (has_mbyte)
1283 mb_adjust_cursor();
1284 #endif
1286 #ifdef FEAT_SCROLLBIND
1287 if (curwin->w_p_scb && toplevel)
1289 validate_cursor(); /* may need to update w_leftcol */
1290 do_check_scrollbind(TRUE);
1292 #endif
1295 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1296 * if still inside a mapping that started in Visual mode).
1297 * May switch from Visual to Select mode after CTRL-O command.
1299 if ( oap->op_type == OP_NOP
1300 #ifdef FEAT_VISUAL
1301 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1302 || restart_VIsual_select == 1)
1303 #else
1304 && restart_edit != 0
1305 #endif
1306 && !(ca.retval & CA_COMMAND_BUSY)
1307 && stuff_empty()
1308 && oap->regname == 0)
1310 #ifdef FEAT_VISUAL
1311 if (restart_VIsual_select == 1)
1313 VIsual_select = TRUE;
1314 showmode();
1315 restart_VIsual_select = 0;
1317 #endif
1318 if (restart_edit != 0
1319 #ifdef FEAT_VISUAL
1320 && !VIsual_active && old_mapped_len == 0
1321 #endif
1323 (void)edit(restart_edit, FALSE, 1L);
1326 #ifdef FEAT_VISUAL
1327 if (restart_VIsual_select == 2)
1328 restart_VIsual_select = 1;
1329 #endif
1331 /* Save count before an operator for next time. */
1332 opcount = ca.opcount;
1336 * Handle an operator after visual mode or when the movement is finished
1338 void
1339 do_pending_operator(cap, old_col, gui_yank)
1340 cmdarg_T *cap;
1341 int old_col;
1342 int gui_yank;
1344 oparg_T *oap = cap->oap;
1345 pos_T old_cursor;
1346 int empty_region_error;
1347 int restart_edit_save;
1349 #ifdef FEAT_VISUAL
1350 /* The visual area is remembered for redo */
1351 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1352 static linenr_T redo_VIsual_line_count; /* number of lines */
1353 static colnr_T redo_VIsual_col; /* number of cols or end column */
1354 static long redo_VIsual_count; /* count for Visual operator */
1355 # ifdef FEAT_VIRTUALEDIT
1356 int include_line_break = FALSE;
1357 # endif
1358 #endif
1360 #if defined(FEAT_CLIPBOARD)
1362 * Yank the visual area into the GUI selection register before we operate
1363 * on it and lose it forever.
1364 * Don't do it if a specific register was specified, so that ""x"*P works.
1365 * This could call do_pending_operator() recursively, but that's OK
1366 * because gui_yank will be TRUE for the nested call.
1368 if (clip_star.available
1369 && oap->op_type != OP_NOP
1370 && !gui_yank
1371 # ifdef FEAT_VISUAL
1372 && VIsual_active
1373 && !redo_VIsual_busy
1374 # endif
1375 && oap->regname == 0)
1376 clip_auto_select();
1377 #endif
1378 old_cursor = curwin->w_cursor;
1381 * If an operation is pending, handle it...
1383 if ((finish_op
1384 #ifdef FEAT_VISUAL
1385 || VIsual_active
1386 #endif
1387 ) && oap->op_type != OP_NOP)
1389 #ifdef FEAT_VISUAL
1390 oap->is_VIsual = VIsual_active;
1391 if (oap->motion_force == 'V')
1392 oap->motion_type = MLINE;
1393 else if (oap->motion_force == 'v')
1395 /* If the motion was linewise, "inclusive" will not have been set.
1396 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1397 if (oap->motion_type == MLINE)
1398 oap->inclusive = FALSE;
1399 /* If the motion already was characterwise, toggle "inclusive" */
1400 else if (oap->motion_type == MCHAR)
1401 oap->inclusive = !oap->inclusive;
1402 oap->motion_type = MCHAR;
1404 else if (oap->motion_force == Ctrl_V)
1406 /* Change line- or characterwise motion into Visual block mode. */
1407 VIsual_active = TRUE;
1408 VIsual = oap->start;
1409 VIsual_mode = Ctrl_V;
1410 VIsual_select = FALSE;
1411 VIsual_reselect = FALSE;
1413 #endif
1415 /* only redo yank when 'y' flag is in 'cpoptions' */
1416 /* never redo "zf" (define fold) */
1417 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1418 #ifdef FEAT_VISUAL
1419 && (!VIsual_active || oap->motion_force)
1420 #endif
1421 && cap->cmdchar != 'D'
1422 #ifdef FEAT_FOLDING
1423 && oap->op_type != OP_FOLD
1424 && oap->op_type != OP_FOLDOPEN
1425 && oap->op_type != OP_FOLDOPENREC
1426 && oap->op_type != OP_FOLDCLOSE
1427 && oap->op_type != OP_FOLDCLOSEREC
1428 && oap->op_type != OP_FOLDDEL
1429 && oap->op_type != OP_FOLDDELREC
1430 #endif
1433 prep_redo(oap->regname, cap->count0,
1434 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1435 oap->motion_force, cap->cmdchar, cap->nchar);
1436 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1439 * If 'cpoptions' does not contain 'r', insert the search
1440 * pattern to really repeat the same command.
1442 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
1443 AppendToRedobuffLit(cap->searchbuf, -1);
1444 AppendToRedobuff(NL_STR);
1446 else if (cap->cmdchar == ':')
1448 /* do_cmdline() has stored the first typed line in
1449 * "repeat_cmdline". When several lines are typed repeating
1450 * won't be possible. */
1451 if (repeat_cmdline == NULL)
1452 ResetRedobuff();
1453 else
1455 AppendToRedobuffLit(repeat_cmdline, -1);
1456 AppendToRedobuff(NL_STR);
1457 vim_free(repeat_cmdline);
1458 repeat_cmdline = NULL;
1463 #ifdef FEAT_VISUAL
1464 if (redo_VIsual_busy)
1466 oap->start = curwin->w_cursor;
1467 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1468 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1469 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1470 VIsual_mode = redo_VIsual_mode;
1471 if (VIsual_mode == 'v')
1473 if (redo_VIsual_line_count <= 1)
1474 curwin->w_cursor.col += redo_VIsual_col - 1;
1475 else
1476 curwin->w_cursor.col = redo_VIsual_col;
1478 if (redo_VIsual_col == MAXCOL)
1480 curwin->w_curswant = MAXCOL;
1481 coladvance((colnr_T)MAXCOL);
1483 cap->count0 = redo_VIsual_count;
1484 if (redo_VIsual_count != 0)
1485 cap->count1 = redo_VIsual_count;
1486 else
1487 cap->count1 = 1;
1489 else if (VIsual_active)
1491 if (!gui_yank)
1493 /* Save the current VIsual area for '< and '> marks, and "gv" */
1494 curbuf->b_visual.vi_start = VIsual;
1495 curbuf->b_visual.vi_end = curwin->w_cursor;
1496 curbuf->b_visual.vi_mode = VIsual_mode;
1497 curbuf->b_visual.vi_curswant = curwin->w_curswant;
1498 # ifdef FEAT_EVAL
1499 curbuf->b_visual_mode_eval = VIsual_mode;
1500 # endif
1503 /* In Select mode, a linewise selection is operated upon like a
1504 * characterwise selection. */
1505 if (VIsual_select && VIsual_mode == 'V')
1507 if (lt(VIsual, curwin->w_cursor))
1509 VIsual.col = 0;
1510 curwin->w_cursor.col =
1511 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1513 else
1515 curwin->w_cursor.col = 0;
1516 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1518 VIsual_mode = 'v';
1520 /* If 'selection' is "exclusive", backup one character for
1521 * charwise selections. */
1522 else if (VIsual_mode == 'v')
1524 # ifdef FEAT_VIRTUALEDIT
1525 include_line_break =
1526 # endif
1527 unadjust_for_sel();
1530 oap->start = VIsual;
1531 if (VIsual_mode == 'V')
1532 oap->start.col = 0;
1534 #endif /* FEAT_VISUAL */
1537 * Set oap->start to the first position of the operated text, oap->end
1538 * to the end of the operated text. w_cursor is equal to oap->start.
1540 if (lt(oap->start, curwin->w_cursor))
1542 #ifdef FEAT_FOLDING
1543 /* Include folded lines completely. */
1544 if (!VIsual_active)
1546 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1547 oap->start.col = 0;
1548 if (hasFolding(curwin->w_cursor.lnum, NULL,
1549 &curwin->w_cursor.lnum))
1550 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1552 #endif
1553 oap->end = curwin->w_cursor;
1554 curwin->w_cursor = oap->start;
1556 /* w_virtcol may have been updated; if the cursor goes back to its
1557 * previous position w_virtcol becomes invalid and isn't updated
1558 * automatically. */
1559 curwin->w_valid &= ~VALID_VIRTCOL;
1561 else
1563 #ifdef FEAT_FOLDING
1564 /* Include folded lines completely. */
1565 if (!VIsual_active && oap->motion_type == MLINE)
1567 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1568 NULL))
1569 curwin->w_cursor.col = 0;
1570 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1571 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1573 #endif
1574 oap->end = oap->start;
1575 oap->start = curwin->w_cursor;
1578 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1580 #ifdef FEAT_VIRTUALEDIT
1581 /* Set "virtual_op" before resetting VIsual_active. */
1582 virtual_op = virtual_active();
1583 #endif
1585 #ifdef FEAT_VISUAL
1586 if (VIsual_active || redo_VIsual_busy)
1588 if (VIsual_mode == Ctrl_V) /* block mode */
1590 colnr_T start, end;
1592 oap->block_mode = TRUE;
1594 getvvcol(curwin, &(oap->start),
1595 &oap->start_vcol, NULL, &oap->end_vcol);
1596 if (!redo_VIsual_busy)
1598 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1600 if (start < oap->start_vcol)
1601 oap->start_vcol = start;
1602 if (end > oap->end_vcol)
1604 if (*p_sel == 'e' && start >= 1
1605 && start - 1 >= oap->end_vcol)
1606 oap->end_vcol = start - 1;
1607 else
1608 oap->end_vcol = end;
1612 /* if '$' was used, get oap->end_vcol from longest line */
1613 if (curwin->w_curswant == MAXCOL)
1615 curwin->w_cursor.col = MAXCOL;
1616 oap->end_vcol = 0;
1617 for (curwin->w_cursor.lnum = oap->start.lnum;
1618 curwin->w_cursor.lnum <= oap->end.lnum;
1619 ++curwin->w_cursor.lnum)
1621 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1622 if (end > oap->end_vcol)
1623 oap->end_vcol = end;
1626 else if (redo_VIsual_busy)
1627 oap->end_vcol = oap->start_vcol + redo_VIsual_col - 1;
1629 * Correct oap->end.col and oap->start.col to be the
1630 * upper-left and lower-right corner of the block area.
1632 * (Actually, this does convert column positions into character
1633 * positions)
1635 curwin->w_cursor.lnum = oap->end.lnum;
1636 coladvance(oap->end_vcol);
1637 oap->end = curwin->w_cursor;
1639 curwin->w_cursor = oap->start;
1640 coladvance(oap->start_vcol);
1641 oap->start = curwin->w_cursor;
1644 if (!redo_VIsual_busy && !gui_yank)
1647 * Prepare to reselect and redo Visual: this is based on the
1648 * size of the Visual text
1650 resel_VIsual_mode = VIsual_mode;
1651 if (curwin->w_curswant == MAXCOL)
1652 resel_VIsual_col = MAXCOL;
1653 else if (VIsual_mode == Ctrl_V)
1654 resel_VIsual_col = oap->end_vcol - oap->start_vcol + 1;
1655 else if (oap->line_count > 1)
1656 resel_VIsual_col = oap->end.col;
1657 else
1658 resel_VIsual_col = oap->end.col - oap->start.col + 1;
1659 resel_VIsual_line_count = oap->line_count;
1662 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1663 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1664 && oap->op_type != OP_COLON
1665 #ifdef FEAT_FOLDING
1666 && oap->op_type != OP_FOLD
1667 && oap->op_type != OP_FOLDOPEN
1668 && oap->op_type != OP_FOLDOPENREC
1669 && oap->op_type != OP_FOLDCLOSE
1670 && oap->op_type != OP_FOLDCLOSEREC
1671 && oap->op_type != OP_FOLDDEL
1672 && oap->op_type != OP_FOLDDELREC
1673 #endif
1674 && oap->motion_force == NUL
1677 /* Prepare for redoing. Only use the nchar field for "r",
1678 * otherwise it might be the second char of the operator. */
1679 prep_redo(oap->regname, 0L, NUL, 'v',
1680 get_op_char(oap->op_type),
1681 get_extra_op_char(oap->op_type),
1682 oap->op_type == OP_REPLACE ? cap->nchar : NUL);
1683 if (!redo_VIsual_busy)
1685 redo_VIsual_mode = resel_VIsual_mode;
1686 redo_VIsual_col = resel_VIsual_col;
1687 redo_VIsual_line_count = resel_VIsual_line_count;
1688 redo_VIsual_count = cap->count0;
1693 * oap->inclusive defaults to TRUE.
1694 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1695 * FALSE. This makes "d}P" and "v}dP" work the same.
1697 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1698 oap->inclusive = TRUE;
1699 if (VIsual_mode == 'V')
1700 oap->motion_type = MLINE;
1701 else
1703 oap->motion_type = MCHAR;
1704 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1705 # ifdef FEAT_VIRTUALEDIT
1706 && (include_line_break || !virtual_op)
1707 # endif
1710 oap->inclusive = FALSE;
1711 /* Try to include the newline, unless it's an operator
1712 * that works on lines only */
1713 if (*p_sel != 'o'
1714 && !op_on_lines(oap->op_type)
1715 && oap->end.lnum < curbuf->b_ml.ml_line_count)
1717 ++oap->end.lnum;
1718 oap->end.col = 0;
1719 # ifdef FEAT_VIRTUALEDIT
1720 oap->end.coladd = 0;
1721 # endif
1722 ++oap->line_count;
1727 redo_VIsual_busy = FALSE;
1730 * Switch Visual off now, so screen updating does
1731 * not show inverted text when the screen is redrawn.
1732 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1733 * no screen redraw, so it is done here to remove the inverted
1734 * part.
1736 if (!gui_yank)
1738 VIsual_active = FALSE;
1739 # ifdef FEAT_MOUSE
1740 setmouse();
1741 mouse_dragging = 0;
1742 # endif
1743 if (mode_displayed)
1744 clear_cmdline = TRUE; /* unshow visual mode later */
1745 #ifdef FEAT_CMDL_INFO
1746 else
1747 clear_showcmd();
1748 #endif
1749 if ((oap->op_type == OP_YANK
1750 || oap->op_type == OP_COLON
1751 || oap->op_type == OP_FUNCTION
1752 || oap->op_type == OP_FILTER)
1753 && oap->motion_force == NUL)
1754 redraw_curbuf_later(INVERTED);
1757 #endif
1759 #ifdef FEAT_MBYTE
1760 /* Include the trailing byte of a multi-byte char. */
1761 if (has_mbyte && oap->inclusive)
1763 int l;
1765 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
1766 if (l > 1)
1767 oap->end.col += l - 1;
1769 #endif
1770 curwin->w_set_curswant = TRUE;
1773 * oap->empty is set when start and end are the same. The inclusive
1774 * flag affects this too, unless yanking and the end is on a NUL.
1776 oap->empty = (oap->motion_type == MCHAR
1777 && (!oap->inclusive
1778 || (oap->op_type == OP_YANK
1779 && gchar_pos(&oap->end) == NUL))
1780 && equalpos(oap->start, oap->end)
1781 #ifdef FEAT_VIRTUALEDIT
1782 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1783 #endif
1786 * For delete, change and yank, it's an error to operate on an
1787 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1789 empty_region_error = (oap->empty
1790 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1792 #ifdef FEAT_VISUAL
1793 /* Force a redraw when operating on an empty Visual region, when
1794 * 'modifiable is off or creating a fold. */
1795 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1796 # ifdef FEAT_FOLDING
1797 || oap->op_type == OP_FOLD
1798 # endif
1800 redraw_curbuf_later(INVERTED);
1801 #endif
1804 * If the end of an operator is in column one while oap->motion_type
1805 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1806 * character in the previous line. If op_start is on or before the
1807 * first non-blank in the line, the operator becomes linewise
1808 * (strange, but that's the way vi does it).
1810 if ( oap->motion_type == MCHAR
1811 && oap->inclusive == FALSE
1812 && !(cap->retval & CA_NO_ADJ_OP_END)
1813 && oap->end.col == 0
1814 #ifdef FEAT_VISUAL
1815 && (!oap->is_VIsual || *p_sel == 'o')
1816 && !oap->block_mode
1817 #endif
1818 && oap->line_count > 1)
1820 oap->end_adjusted = TRUE; /* remember that we did this */
1821 --oap->line_count;
1822 --oap->end.lnum;
1823 if (inindent(0))
1824 oap->motion_type = MLINE;
1825 else
1827 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1828 if (oap->end.col)
1830 --oap->end.col;
1831 oap->inclusive = TRUE;
1835 else
1836 oap->end_adjusted = FALSE;
1838 switch (oap->op_type)
1840 case OP_LSHIFT:
1841 case OP_RSHIFT:
1842 op_shift(oap, TRUE,
1843 #ifdef FEAT_VISUAL
1844 oap->is_VIsual ? (int)cap->count1 :
1845 #endif
1847 auto_format(FALSE, TRUE);
1848 break;
1850 case OP_JOIN_NS:
1851 case OP_JOIN:
1852 if (oap->line_count < 2)
1853 oap->line_count = 2;
1854 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1855 curbuf->b_ml.ml_line_count)
1856 beep_flush();
1857 else
1859 do_do_join(oap->line_count, oap->op_type == OP_JOIN);
1860 auto_format(FALSE, TRUE);
1862 break;
1864 case OP_DELETE:
1865 #ifdef FEAT_VISUAL
1866 VIsual_reselect = FALSE; /* don't reselect now */
1867 #endif
1868 if (empty_region_error)
1869 vim_beep();
1870 else
1872 (void)op_delete(oap);
1873 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1874 u_save_cursor(); /* cursor line wasn't saved yet */
1875 auto_format(FALSE, TRUE);
1877 break;
1879 case OP_YANK:
1880 if (empty_region_error)
1882 if (!gui_yank)
1883 vim_beep();
1885 else
1886 (void)op_yank(oap, FALSE, !gui_yank);
1887 check_cursor_col();
1888 break;
1890 case OP_CHANGE:
1891 #ifdef FEAT_VISUAL
1892 VIsual_reselect = FALSE; /* don't reselect now */
1893 #endif
1894 if (empty_region_error)
1895 vim_beep();
1896 else
1898 /* This is a new edit command, not a restart. Need to
1899 * remember it to make 'insertmode' work with mappings for
1900 * Visual mode. But do this only once and not when typed and
1901 * 'insertmode' isn't set. */
1902 if (p_im || !KeyTyped)
1903 restart_edit_save = restart_edit;
1904 else
1905 restart_edit_save = 0;
1906 restart_edit = 0;
1907 /* Reset finish_op now, don't want it set inside edit(). */
1908 finish_op = FALSE;
1909 if (op_change(oap)) /* will call edit() */
1910 cap->retval |= CA_COMMAND_BUSY;
1911 if (restart_edit == 0)
1912 restart_edit = restart_edit_save;
1914 break;
1916 case OP_FILTER:
1917 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1918 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1919 else
1920 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
1922 case OP_INDENT:
1923 case OP_COLON:
1925 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1927 * If 'equalprg' is empty, do the indenting internally.
1929 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1931 # ifdef FEAT_LISP
1932 if (curbuf->b_p_lisp)
1934 op_reindent(oap, get_lisp_indent);
1935 break;
1937 # endif
1938 # ifdef FEAT_CINDENT
1939 op_reindent(oap,
1940 # ifdef FEAT_EVAL
1941 *curbuf->b_p_inde != NUL ? get_expr_indent :
1942 # endif
1943 get_c_indent);
1944 break;
1945 # endif
1947 #endif
1949 op_colon(oap);
1950 break;
1952 case OP_TILDE:
1953 case OP_UPPER:
1954 case OP_LOWER:
1955 case OP_ROT13:
1956 if (empty_region_error)
1957 vim_beep();
1958 else
1959 op_tilde(oap);
1960 check_cursor_col();
1961 break;
1963 case OP_FORMAT:
1964 #if defined(FEAT_EVAL)
1965 if (*curbuf->b_p_fex != NUL)
1966 op_formatexpr(oap); /* use expression */
1967 else
1968 #endif
1969 if (*p_fp != NUL)
1970 op_colon(oap); /* use external command */
1971 else
1972 op_format(oap, FALSE); /* use internal function */
1973 break;
1975 case OP_FORMAT2:
1976 op_format(oap, TRUE); /* use internal function */
1977 break;
1979 case OP_FUNCTION:
1980 op_function(oap); /* call 'operatorfunc' */
1981 break;
1983 case OP_INSERT:
1984 case OP_APPEND:
1985 #ifdef FEAT_VISUAL
1986 VIsual_reselect = FALSE; /* don't reselect now */
1987 #endif
1988 #ifdef FEAT_VISUALEXTRA
1989 if (empty_region_error)
1990 vim_beep();
1991 else
1993 /* This is a new edit command, not a restart. Need to
1994 * remember it to make 'insertmode' work with mappings for
1995 * Visual mode. But do this only once. */
1996 restart_edit_save = restart_edit;
1997 restart_edit = 0;
1999 op_insert(oap, cap->count1);
2001 /* TODO: when inserting in several lines, should format all
2002 * the lines. */
2003 auto_format(FALSE, TRUE);
2005 if (restart_edit == 0)
2006 restart_edit = restart_edit_save;
2008 #else
2009 vim_beep();
2010 #endif
2011 break;
2013 case OP_REPLACE:
2014 #ifdef FEAT_VISUAL
2015 VIsual_reselect = FALSE; /* don't reselect now */
2016 #endif
2017 #ifdef FEAT_VISUALEXTRA
2018 if (empty_region_error)
2019 #endif
2020 vim_beep();
2021 #ifdef FEAT_VISUALEXTRA
2022 else
2023 op_replace(oap, cap->nchar);
2024 #endif
2025 break;
2027 #ifdef FEAT_FOLDING
2028 case OP_FOLD:
2029 VIsual_reselect = FALSE; /* don't reselect now */
2030 foldCreate(oap->start.lnum, oap->end.lnum);
2031 break;
2033 case OP_FOLDOPEN:
2034 case OP_FOLDOPENREC:
2035 case OP_FOLDCLOSE:
2036 case OP_FOLDCLOSEREC:
2037 VIsual_reselect = FALSE; /* don't reselect now */
2038 opFoldRange(oap->start.lnum, oap->end.lnum,
2039 oap->op_type == OP_FOLDOPEN
2040 || oap->op_type == OP_FOLDOPENREC,
2041 oap->op_type == OP_FOLDOPENREC
2042 || oap->op_type == OP_FOLDCLOSEREC,
2043 oap->is_VIsual);
2044 break;
2046 case OP_FOLDDEL:
2047 case OP_FOLDDELREC:
2048 VIsual_reselect = FALSE; /* don't reselect now */
2049 deleteFold(oap->start.lnum, oap->end.lnum,
2050 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2051 break;
2052 #endif
2053 default:
2054 clearopbeep(oap);
2056 #ifdef FEAT_VIRTUALEDIT
2057 virtual_op = MAYBE;
2058 #endif
2059 if (!gui_yank)
2062 * if 'sol' not set, go back to old column for some commands
2064 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2065 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2066 || oap->op_type == OP_DELETE))
2067 coladvance(curwin->w_curswant = old_col);
2069 else
2071 curwin->w_cursor = old_cursor;
2073 #ifdef FEAT_VISUAL
2074 oap->block_mode = FALSE;
2075 #endif
2076 clearop(oap);
2081 * Handle indent and format operators and visual mode ":".
2083 static void
2084 op_colon(oap)
2085 oparg_T *oap;
2087 stuffcharReadbuff(':');
2088 #ifdef FEAT_VISUAL
2089 if (oap->is_VIsual)
2090 stuffReadbuff((char_u *)"'<,'>");
2091 else
2092 #endif
2095 * Make the range look nice, so it can be repeated.
2097 if (oap->start.lnum == curwin->w_cursor.lnum)
2098 stuffcharReadbuff('.');
2099 else
2100 stuffnumReadbuff((long)oap->start.lnum);
2101 if (oap->end.lnum != oap->start.lnum)
2103 stuffcharReadbuff(',');
2104 if (oap->end.lnum == curwin->w_cursor.lnum)
2105 stuffcharReadbuff('.');
2106 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2107 stuffcharReadbuff('$');
2108 else if (oap->start.lnum == curwin->w_cursor.lnum)
2110 stuffReadbuff((char_u *)".+");
2111 stuffnumReadbuff((long)oap->line_count - 1);
2113 else
2114 stuffnumReadbuff((long)oap->end.lnum);
2117 if (oap->op_type != OP_COLON)
2118 stuffReadbuff((char_u *)"!");
2119 if (oap->op_type == OP_INDENT)
2121 #ifndef FEAT_CINDENT
2122 if (*get_equalprg() == NUL)
2123 stuffReadbuff((char_u *)"indent");
2124 else
2125 #endif
2126 stuffReadbuff(get_equalprg());
2127 stuffReadbuff((char_u *)"\n");
2129 else if (oap->op_type == OP_FORMAT)
2131 if (*p_fp == NUL)
2132 stuffReadbuff((char_u *)"fmt");
2133 else
2134 stuffReadbuff(p_fp);
2135 stuffReadbuff((char_u *)"\n']");
2139 * do_cmdline() does the rest
2144 * Handle the "g@" operator: call 'operatorfunc'.
2146 /*ARGSUSED*/
2147 static void
2148 op_function(oap)
2149 oparg_T *oap;
2151 #ifdef FEAT_EVAL
2152 char_u *(argv[1]);
2154 if (*p_opfunc == NUL)
2155 EMSG(_("E774: 'operatorfunc' is empty"));
2156 else
2158 /* Set '[ and '] marks to text to be operated on. */
2159 curbuf->b_op_start = oap->start;
2160 curbuf->b_op_end = oap->end;
2161 if (oap->motion_type != MLINE && !oap->inclusive)
2162 /* Exclude the end position. */
2163 decl(&curbuf->b_op_end);
2165 if (oap->block_mode)
2166 argv[0] = (char_u *)"block";
2167 else if (oap->motion_type == MLINE)
2168 argv[0] = (char_u *)"line";
2169 else
2170 argv[0] = (char_u *)"char";
2171 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
2173 #else
2174 EMSG(_("E775: Eval feature not available"));
2175 #endif
2178 #if defined(FEAT_MOUSE) || defined(PROTO)
2180 * Do the appropriate action for the current mouse click in the current mode.
2181 * Not used for Command-line mode.
2183 * Normal Mode:
2184 * event modi- position visual change action
2185 * fier cursor window
2186 * left press - yes end yes
2187 * left press C yes end yes "^]" (2)
2188 * left press S yes end yes "*" (2)
2189 * left drag - yes start if moved no
2190 * left relse - yes start if moved no
2191 * middle press - yes if not active no put register
2192 * middle press - yes if active no yank and put
2193 * right press - yes start or extend yes
2194 * right press S yes no change yes "#" (2)
2195 * right drag - yes extend no
2196 * right relse - yes extend no
2198 * Insert or Replace Mode:
2199 * event modi- position visual change action
2200 * fier cursor window
2201 * left press - yes (cannot be active) yes
2202 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2203 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2204 * left drag - yes start or extend (1) no CTRL-O (1)
2205 * left relse - yes start or extend (1) no CTRL-O (1)
2206 * middle press - no (cannot be active) no put register
2207 * right press - yes start or extend yes CTRL-O
2208 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2210 * (1) only if mouse pointer moved since press
2211 * (2) only if click is in same buffer
2213 * Return TRUE if start_arrow() should be called for edit mode.
2216 do_mouse(oap, c, dir, count, fixindent)
2217 oparg_T *oap; /* operator argument, can be NULL */
2218 int c; /* K_LEFTMOUSE, etc */
2219 int dir; /* Direction to 'put' if necessary */
2220 long count;
2221 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2223 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2224 static int got_click = FALSE; /* got a click some time back */
2226 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2227 int is_click; /* If FALSE it's a drag or release event */
2228 int is_drag; /* If TRUE it's a drag event */
2229 int jump_flags = 0; /* flags for jump_to_mouse() */
2230 pos_T start_visual;
2231 int moved; /* Has cursor moved? */
2232 int in_status_line; /* mouse in status line */
2233 #ifdef FEAT_VERTSPLIT
2234 int in_sep_line; /* mouse in vertical separator line */
2235 #endif
2236 int c1, c2;
2237 #if defined(FEAT_FOLDING)
2238 pos_T save_cursor;
2239 #endif
2240 win_T *old_curwin = curwin;
2241 #ifdef FEAT_VISUAL
2242 static pos_T orig_cursor;
2243 colnr_T leftcol, rightcol;
2244 pos_T end_visual;
2245 int diff;
2246 int old_active = VIsual_active;
2247 int old_mode = VIsual_mode;
2248 #endif
2249 int regname;
2251 #if defined(FEAT_FOLDING)
2252 save_cursor = curwin->w_cursor;
2253 #endif
2256 * When GUI is active, always recognize mouse events, otherwise:
2257 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2258 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2259 * - For command line and insert mode 'mouse' is checked before calling
2260 * do_mouse().
2262 if (do_always)
2263 do_always = FALSE;
2264 else
2265 #ifdef FEAT_GUI
2266 if (!gui.in_use)
2267 #endif
2269 #ifdef FEAT_VISUAL
2270 if (VIsual_active)
2272 if (!mouse_has(MOUSE_VISUAL))
2273 return FALSE;
2275 else
2276 #endif
2277 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2278 return FALSE;
2281 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2283 #ifdef FEAT_MOUSESHAPE
2284 /* May have stopped dragging the status or separator line. The pointer is
2285 * most likely still on the status or separator line. */
2286 if (!is_drag && drag_status_line)
2288 drag_status_line = FALSE;
2289 update_mouseshape(SHAPE_IDX_STATUS);
2291 # ifdef FEAT_VERTSPLIT
2292 if (!is_drag && drag_sep_line)
2294 drag_sep_line = FALSE;
2295 update_mouseshape(SHAPE_IDX_VSEP);
2297 # endif
2298 #endif
2301 * Ignore drag and release events if we didn't get a click.
2303 if (is_click)
2304 got_click = TRUE;
2305 else
2307 if (!got_click) /* didn't get click, ignore */
2308 return FALSE;
2309 if (!is_drag) /* release, reset got_click */
2310 got_click = FALSE;
2313 #ifndef FEAT_VISUAL
2315 * ALT is only used for starging/extending Visual mode.
2317 if ((mod_mask & MOD_MASK_ALT))
2318 return FALSE;
2319 #endif
2322 * CTRL right mouse button does CTRL-T
2324 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2326 if (State & INSERT)
2327 stuffcharReadbuff(Ctrl_O);
2328 if (count > 1)
2329 stuffnumReadbuff(count);
2330 stuffcharReadbuff(Ctrl_T);
2331 got_click = FALSE; /* ignore drag&release now */
2332 return FALSE;
2336 * CTRL only works with left mouse button
2338 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2339 return FALSE;
2342 * When a modifier is down, ignore drag and release events, as well as
2343 * multiple clicks and the middle mouse button.
2344 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2346 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2347 | MOD_MASK_META))
2348 && (!is_click
2349 || (mod_mask & MOD_MASK_MULTI_CLICK)
2350 || which_button == MOUSE_MIDDLE)
2351 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
2352 && mouse_model_popup()
2353 && which_button == MOUSE_LEFT)
2354 && !((mod_mask & MOD_MASK_ALT)
2355 && !mouse_model_popup()
2356 && which_button == MOUSE_RIGHT)
2358 return FALSE;
2361 * If the button press was used as the movement command for an operator
2362 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2363 * drag/release events.
2365 if (!is_click && which_button == MOUSE_MIDDLE)
2366 return FALSE;
2368 if (oap != NULL)
2369 regname = oap->regname;
2370 else
2371 regname = 0;
2374 * Middle mouse button does a 'put' of the selected text
2376 if (which_button == MOUSE_MIDDLE)
2378 if (State == NORMAL)
2381 * If an operator was pending, we don't know what the user wanted
2382 * to do. Go back to normal mode: Clear the operator and beep().
2384 if (oap != NULL && oap->op_type != OP_NOP)
2386 clearopbeep(oap);
2387 return FALSE;
2390 #ifdef FEAT_VISUAL
2392 * If visual was active, yank the highlighted text and put it
2393 * before the mouse pointer position.
2394 * In Select mode replace the highlighted text with the clipboard.
2396 if (VIsual_active)
2398 if (VIsual_select)
2400 stuffcharReadbuff(Ctrl_G);
2401 stuffReadbuff((char_u *)"\"+p");
2403 else
2405 stuffcharReadbuff('y');
2406 stuffcharReadbuff(K_MIDDLEMOUSE);
2408 do_always = TRUE; /* ignore 'mouse' setting next time */
2409 return FALSE;
2411 #endif
2413 * The rest is below jump_to_mouse()
2417 else if ((State & INSERT) == 0)
2418 return FALSE;
2421 * Middle click in insert mode doesn't move the mouse, just insert the
2422 * contents of a register. '.' register is special, can't insert that
2423 * with do_put().
2424 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2425 * happens for the GUI).
2427 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2429 if (regname == '.')
2430 insert_reg(regname, TRUE);
2431 else
2433 #ifdef FEAT_CLIPBOARD
2434 if (clip_star.available && regname == 0)
2435 regname = '*';
2436 #endif
2437 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2438 insert_reg(regname, TRUE);
2439 else
2441 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2443 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2444 AppendCharToRedobuff(Ctrl_R);
2445 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2446 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2449 return FALSE;
2453 /* When dragging or button-up stay in the same window. */
2454 if (!is_click)
2455 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2457 start_visual.lnum = 0;
2459 #ifdef FEAT_WINDOWS
2460 /* Check for clicking in the tab page line. */
2461 if (mouse_row == 0 && firstwin->w_winrow > 0)
2463 got_click = FALSE; /* ignore mouse-up and drag events */
2465 /* click in a tab selects that tab page */
2466 if (is_click
2467 # ifdef FEAT_CMDWIN
2468 && cmdwin_type == 0
2469 # endif
2470 && mouse_col < Columns)
2472 c1 = TabPageIdxs[mouse_col];
2473 if (c1 >= 0)
2475 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2477 /* double click opens new page */
2478 end_visual_mode();
2479 tabpage_new();
2480 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2482 else
2484 /* Go to specified tab page, or next one if not clicking
2485 * on a label. */
2486 goto_tabpage(c1);
2488 /* It's like clicking on the status line of a window. */
2489 if (curwin != old_curwin)
2490 end_visual_mode();
2493 else if (c1 < 0)
2495 tabpage_T *tp;
2497 /* Close the current or specified tab page. */
2498 if (c1 == -999)
2499 tp = curtab;
2500 else
2501 tp = find_tabpage(-c1);
2502 if (tp == curtab)
2504 if (first_tabpage->tp_next != NULL)
2505 tabpage_close(FALSE);
2507 else if (tp != NULL)
2508 tabpage_close_other(tp, FALSE);
2511 return TRUE;
2513 #endif
2516 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2517 * right button up -> pop-up menu
2518 * shift-left button -> right button
2519 * alt-left button -> alt-right button
2521 if (mouse_model_popup())
2523 if (which_button == MOUSE_RIGHT
2524 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2527 * NOTE: Ignore right button down and drag mouse events.
2528 * Windows only shows the popup menu on the button up event.
2530 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2531 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) \
2532 || defined(FEAT_GUI_MACVIM)
2533 if (!is_click)
2534 return FALSE;
2535 #endif
2536 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2537 if (is_click || is_drag)
2538 return FALSE;
2539 #endif
2540 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2541 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2542 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON) \
2543 || defined(FEAT_GUI_MACVIM)
2544 if (gui.in_use)
2546 jump_flags = 0;
2547 if (STRCMP(p_mousem, "popup_setpos") == 0)
2549 /* First set the cursor position before showing the popup
2550 * menu. */
2551 #ifdef FEAT_VISUAL
2552 if (VIsual_active)
2554 pos_T m_pos;
2557 * set MOUSE_MAY_STOP_VIS if we are outside the
2558 * selection or the current window (might have false
2559 * negative here)
2561 if (mouse_row < W_WINROW(curwin)
2562 || mouse_row
2563 > (W_WINROW(curwin) + curwin->w_height))
2564 jump_flags = MOUSE_MAY_STOP_VIS;
2565 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2566 jump_flags = MOUSE_MAY_STOP_VIS;
2567 else
2569 if ((lt(curwin->w_cursor, VIsual)
2570 && (lt(m_pos, curwin->w_cursor)
2571 || lt(VIsual, m_pos)))
2572 || (lt(VIsual, curwin->w_cursor)
2573 && (lt(m_pos, VIsual)
2574 || lt(curwin->w_cursor, m_pos))))
2576 jump_flags = MOUSE_MAY_STOP_VIS;
2578 else if (VIsual_mode == Ctrl_V)
2580 getvcols(curwin, &curwin->w_cursor, &VIsual,
2581 &leftcol, &rightcol);
2582 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2583 if (m_pos.col < leftcol || m_pos.col > rightcol)
2584 jump_flags = MOUSE_MAY_STOP_VIS;
2588 else
2589 jump_flags = MOUSE_MAY_STOP_VIS;
2590 #endif
2592 if (jump_flags)
2594 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2595 update_curbuf(
2596 #ifdef FEAT_VISUAL
2597 VIsual_active ? INVERTED :
2598 #endif
2599 VALID);
2600 setcursor();
2601 out_flush(); /* Update before showing popup menu */
2603 # ifdef FEAT_MENU
2604 gui_show_popupmenu();
2605 # endif
2606 return (jump_flags & CURSOR_MOVED) != 0;
2608 else
2609 return FALSE;
2610 #else
2611 return FALSE;
2612 #endif
2614 if (which_button == MOUSE_LEFT
2615 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
2617 which_button = MOUSE_RIGHT;
2618 mod_mask &= ~MOD_MASK_SHIFT;
2622 #ifdef FEAT_VISUAL
2623 if ((State & (NORMAL | INSERT))
2624 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2626 if (which_button == MOUSE_LEFT)
2628 if (is_click)
2630 /* stop Visual mode for a left click in a window, but not when
2631 * on a status line */
2632 if (VIsual_active)
2633 jump_flags |= MOUSE_MAY_STOP_VIS;
2635 else if (mouse_has(MOUSE_VISUAL))
2636 jump_flags |= MOUSE_MAY_VIS;
2638 else if (which_button == MOUSE_RIGHT)
2640 if (is_click && VIsual_active)
2643 * Remember the start and end of visual before moving the
2644 * cursor.
2646 if (lt(curwin->w_cursor, VIsual))
2648 start_visual = curwin->w_cursor;
2649 end_visual = VIsual;
2651 else
2653 start_visual = VIsual;
2654 end_visual = curwin->w_cursor;
2657 jump_flags |= MOUSE_FOCUS;
2658 if (mouse_has(MOUSE_VISUAL))
2659 jump_flags |= MOUSE_MAY_VIS;
2662 #endif
2665 * If an operator is pending, ignore all drags and releases until the
2666 * next mouse click.
2668 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2670 got_click = FALSE;
2671 oap->motion_type = MCHAR;
2674 /* When releasing the button let jump_to_mouse() know. */
2675 if (!is_click && !is_drag)
2676 jump_flags |= MOUSE_RELEASED;
2679 * JUMP!
2681 jump_flags = jump_to_mouse(jump_flags,
2682 oap == NULL ? NULL : &(oap->inclusive), which_button);
2683 moved = (jump_flags & CURSOR_MOVED);
2684 in_status_line = (jump_flags & IN_STATUS_LINE);
2685 #ifdef FEAT_VERTSPLIT
2686 in_sep_line = (jump_flags & IN_SEP_LINE);
2687 #endif
2689 #ifdef FEAT_NETBEANS_INTG
2690 if (usingNetbeans && isNetbeansBuffer(curbuf)
2691 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2693 int key = KEY2TERMCAP1(c);
2695 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2696 || key == (int)KE_RIGHTRELEASE)
2697 netbeans_button_release(which_button);
2699 #endif
2701 /* When jumping to another window, clear a pending operator. That's a bit
2702 * friendlier than beeping and not jumping to that window. */
2703 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2704 clearop(oap);
2706 #ifdef FEAT_FOLDING
2707 if (mod_mask == 0
2708 && !is_drag
2709 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2710 && which_button == MOUSE_LEFT)
2712 /* open or close a fold at this line */
2713 if (jump_flags & MOUSE_FOLD_OPEN)
2714 openFold(curwin->w_cursor.lnum, 1L);
2715 else
2716 closeFold(curwin->w_cursor.lnum, 1L);
2717 /* don't move the cursor if still in the same window */
2718 if (curwin == old_curwin)
2719 curwin->w_cursor = save_cursor;
2721 #endif
2723 #if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2724 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2726 clip_modeless(which_button, is_click, is_drag);
2727 return FALSE;
2729 #endif
2731 #ifdef FEAT_VISUAL
2732 /* Set global flag that we are extending the Visual area with mouse
2733 * dragging; temporarily minimize 'scrolloff'. */
2734 if (VIsual_active && is_drag && p_so)
2736 /* In the very first line, allow scrolling one line */
2737 if (mouse_row == 0)
2738 mouse_dragging = 2;
2739 else
2740 mouse_dragging = 1;
2743 /* When dragging the mouse above the window, scroll down. */
2744 if (is_drag && mouse_row < 0 && !in_status_line)
2746 scroll_redraw(FALSE, 1L);
2747 mouse_row = 0;
2750 if (start_visual.lnum) /* right click in visual mode */
2752 /* When ALT is pressed make Visual mode blockwise. */
2753 if (mod_mask & MOD_MASK_ALT)
2754 VIsual_mode = Ctrl_V;
2757 * In Visual-block mode, divide the area in four, pick up the corner
2758 * that is in the quarter that the cursor is in.
2760 if (VIsual_mode == Ctrl_V)
2762 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2763 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2764 end_visual.col = leftcol;
2765 else
2766 end_visual.col = rightcol;
2767 if (curwin->w_cursor.lnum <
2768 (start_visual.lnum + end_visual.lnum) / 2)
2769 end_visual.lnum = end_visual.lnum;
2770 else
2771 end_visual.lnum = start_visual.lnum;
2773 /* move VIsual to the right column */
2774 start_visual = curwin->w_cursor; /* save the cursor pos */
2775 curwin->w_cursor = end_visual;
2776 coladvance(end_visual.col);
2777 VIsual = curwin->w_cursor;
2778 curwin->w_cursor = start_visual; /* restore the cursor */
2780 else
2783 * If the click is before the start of visual, change the start.
2784 * If the click is after the end of visual, change the end. If
2785 * the click is inside the visual, change the closest side.
2787 if (lt(curwin->w_cursor, start_visual))
2788 VIsual = end_visual;
2789 else if (lt(end_visual, curwin->w_cursor))
2790 VIsual = start_visual;
2791 else
2793 /* In the same line, compare column number */
2794 if (end_visual.lnum == start_visual.lnum)
2796 if (curwin->w_cursor.col - start_visual.col >
2797 end_visual.col - curwin->w_cursor.col)
2798 VIsual = start_visual;
2799 else
2800 VIsual = end_visual;
2803 /* In different lines, compare line number */
2804 else
2806 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2807 (end_visual.lnum - curwin->w_cursor.lnum);
2809 if (diff > 0) /* closest to end */
2810 VIsual = start_visual;
2811 else if (diff < 0) /* closest to start */
2812 VIsual = end_visual;
2813 else /* in the middle line */
2815 if (curwin->w_cursor.col <
2816 (start_visual.col + end_visual.col) / 2)
2817 VIsual = end_visual;
2818 else
2819 VIsual = start_visual;
2826 * If Visual mode started in insert mode, execute "CTRL-O"
2828 else if ((State & INSERT) && VIsual_active)
2829 stuffcharReadbuff(Ctrl_O);
2830 #endif
2833 * Middle mouse click: Put text before cursor.
2835 if (which_button == MOUSE_MIDDLE)
2837 #ifdef FEAT_CLIPBOARD
2838 if (clip_star.available && regname == 0)
2839 regname = '*';
2840 #endif
2841 if (yank_register_mline(regname))
2843 if (mouse_past_bottom)
2844 dir = FORWARD;
2846 else if (mouse_past_eol)
2847 dir = FORWARD;
2849 if (fixindent)
2851 c1 = (dir == BACKWARD) ? '[' : ']';
2852 c2 = 'p';
2854 else
2856 c1 = (dir == FORWARD) ? 'p' : 'P';
2857 c2 = NUL;
2859 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2862 * Remember where the paste started, so in edit() Insstart can be set
2863 * to this position
2865 if (restart_edit != 0)
2866 where_paste_started = curwin->w_cursor;
2867 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2870 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2872 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2873 * error under the mouse pointer.
2875 else if (((mod_mask & MOD_MASK_CTRL)
2876 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2877 && bt_quickfix(curbuf))
2879 if (State & INSERT)
2880 stuffcharReadbuff(Ctrl_O);
2881 if (curwin->w_llist_ref == NULL) /* quickfix window */
2882 stuffReadbuff((char_u *)":.cc\n");
2883 else /* location list window */
2884 stuffReadbuff((char_u *)":.ll\n");
2885 got_click = FALSE; /* ignore drag&release now */
2887 #endif
2890 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2891 * under the mouse pointer.
2893 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2894 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2896 if (State & INSERT)
2897 stuffcharReadbuff(Ctrl_O);
2898 stuffcharReadbuff(Ctrl_RSB);
2899 got_click = FALSE; /* ignore drag&release now */
2903 * Shift-Mouse click searches for the next occurrence of the word under
2904 * the mouse pointer
2906 else if ((mod_mask & MOD_MASK_SHIFT))
2908 if (State & INSERT
2909 #ifdef FEAT_VISUAL
2910 || (VIsual_active && VIsual_select)
2911 #endif
2913 stuffcharReadbuff(Ctrl_O);
2914 if (which_button == MOUSE_LEFT)
2915 stuffcharReadbuff('*');
2916 else /* MOUSE_RIGHT */
2917 stuffcharReadbuff('#');
2920 /* Handle double clicks, unless on status line */
2921 else if (in_status_line)
2923 #ifdef FEAT_MOUSESHAPE
2924 if ((is_drag || is_click) && !drag_status_line)
2926 drag_status_line = TRUE;
2927 update_mouseshape(-1);
2929 #endif
2931 #ifdef FEAT_VERTSPLIT
2932 else if (in_sep_line)
2934 # ifdef FEAT_MOUSESHAPE
2935 if ((is_drag || is_click) && !drag_sep_line)
2937 drag_sep_line = TRUE;
2938 update_mouseshape(-1);
2940 # endif
2942 #endif
2943 #ifdef FEAT_VISUAL
2944 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
2945 && mouse_has(MOUSE_VISUAL))
2947 if (is_click || !VIsual_active)
2949 if (VIsual_active)
2950 orig_cursor = VIsual;
2951 else
2953 check_visual_highlight();
2954 VIsual = curwin->w_cursor;
2955 orig_cursor = VIsual;
2956 VIsual_active = TRUE;
2957 VIsual_reselect = TRUE;
2958 /* start Select mode if 'selectmode' contains "mouse" */
2959 may_start_select('o');
2960 setmouse();
2962 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2964 /* Double click with ALT pressed makes it blockwise. */
2965 if (mod_mask & MOD_MASK_ALT)
2966 VIsual_mode = Ctrl_V;
2967 else
2968 VIsual_mode = 'v';
2970 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
2971 VIsual_mode = 'V';
2972 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
2973 VIsual_mode = Ctrl_V;
2974 #ifdef FEAT_CLIPBOARD
2975 /* Make sure the clipboard gets updated. Needed because start and
2976 * end may still be the same, and the selection needs to be owned */
2977 clip_star.vmode = NUL;
2978 #endif
2981 * A double click selects a word or a block.
2983 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2985 pos_T *pos = NULL;
2986 int gc;
2988 if (is_click)
2990 /* If the character under the cursor (skipping white space) is
2991 * not a word character, try finding a match and select a (),
2992 * {}, [], #if/#endif, etc. block. */
2993 end_visual = curwin->w_cursor;
2994 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
2995 inc(&end_visual);
2996 if (oap != NULL)
2997 oap->motion_type = MCHAR;
2998 if (oap != NULL
2999 && VIsual_mode == 'v'
3000 && !vim_iswordc(gchar_pos(&end_visual))
3001 && equalpos(curwin->w_cursor, VIsual)
3002 && (pos = findmatch(oap, NUL)) != NULL)
3004 curwin->w_cursor = *pos;
3005 if (oap->motion_type == MLINE)
3006 VIsual_mode = 'V';
3007 else if (*p_sel == 'e')
3009 if (lt(curwin->w_cursor, VIsual))
3010 ++VIsual.col;
3011 else
3012 ++curwin->w_cursor.col;
3017 if (pos == NULL && (is_click || is_drag))
3019 /* When not found a match or when dragging: extend to include
3020 * a word. */
3021 if (lt(curwin->w_cursor, orig_cursor))
3023 find_start_of_word(&curwin->w_cursor);
3024 find_end_of_word(&VIsual);
3026 else
3028 find_start_of_word(&VIsual);
3029 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3030 #ifdef FEAT_MBYTE
3031 curwin->w_cursor.col +=
3032 (*mb_ptr2len)(ml_get_cursor());
3033 #else
3034 ++curwin->w_cursor.col;
3035 #endif
3036 find_end_of_word(&curwin->w_cursor);
3039 curwin->w_set_curswant = TRUE;
3041 if (is_click)
3042 redraw_curbuf_later(INVERTED); /* update the inversion */
3044 else if (VIsual_active && !old_active)
3046 if (mod_mask & MOD_MASK_ALT)
3047 VIsual_mode = Ctrl_V;
3048 else
3049 VIsual_mode = 'v';
3052 /* If Visual mode changed show it later. */
3053 if ((!VIsual_active && old_active && mode_displayed)
3054 || (VIsual_active && p_smd && msg_silent == 0
3055 && (!old_active || VIsual_mode != old_mode)))
3056 redraw_cmdline = TRUE;
3057 #endif
3059 return moved;
3062 #ifdef FEAT_VISUAL
3064 * Move "pos" back to the start of the word it's in.
3066 static void
3067 find_start_of_word(pos)
3068 pos_T *pos;
3070 char_u *line;
3071 int cclass;
3072 int col;
3074 line = ml_get(pos->lnum);
3075 cclass = get_mouse_class(line + pos->col);
3077 while (pos->col > 0)
3079 col = pos->col - 1;
3080 #ifdef FEAT_MBYTE
3081 col -= (*mb_head_off)(line, line + col);
3082 #endif
3083 if (get_mouse_class(line + col) != cclass)
3084 break;
3085 pos->col = col;
3090 * Move "pos" forward to the end of the word it's in.
3091 * When 'selection' is "exclusive", the position is just after the word.
3093 static void
3094 find_end_of_word(pos)
3095 pos_T *pos;
3097 char_u *line;
3098 int cclass;
3099 int col;
3101 line = ml_get(pos->lnum);
3102 if (*p_sel == 'e' && pos->col > 0)
3104 --pos->col;
3105 #ifdef FEAT_MBYTE
3106 pos->col -= (*mb_head_off)(line, line + pos->col);
3107 #endif
3109 cclass = get_mouse_class(line + pos->col);
3110 while (line[pos->col] != NUL)
3112 #ifdef FEAT_MBYTE
3113 col = pos->col + (*mb_ptr2len)(line + pos->col);
3114 #else
3115 col = pos->col + 1;
3116 #endif
3117 if (get_mouse_class(line + col) != cclass)
3119 if (*p_sel == 'e')
3120 pos->col = col;
3121 break;
3123 pos->col = col;
3128 * Get class of a character for selection: same class means same word.
3129 * 0: blank
3130 * 1: punctuation groups
3131 * 2: normal word character
3132 * >2: multi-byte word character.
3134 static int
3135 get_mouse_class(p)
3136 char_u *p;
3138 int c;
3140 #ifdef FEAT_MBYTE
3141 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3142 return mb_get_class(p);
3143 #endif
3145 c = *p;
3146 if (c == ' ' || c == '\t')
3147 return 0;
3149 if (vim_iswordc(c))
3150 return 2;
3153 * There are a few special cases where we want certain combinations of
3154 * characters to be considered as a single word. These are things like
3155 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
3156 * character is in it's own class.
3158 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3159 return 1;
3160 return c;
3162 #endif /* FEAT_VISUAL */
3163 #endif /* FEAT_MOUSE */
3165 #if defined(FEAT_VISUAL) || defined(PROTO)
3167 * Check if highlighting for visual mode is possible, give a warning message
3168 * if not.
3170 void
3171 check_visual_highlight()
3173 static int did_check = FALSE;
3175 if (full_screen)
3177 if (!did_check && hl_attr(HLF_V) == 0)
3178 MSG(_("Warning: terminal cannot highlight"));
3179 did_check = TRUE;
3184 * End Visual mode.
3185 * This function should ALWAYS be called to end Visual mode, except from
3186 * do_pending_operator().
3188 void
3189 end_visual_mode()
3191 #ifdef FEAT_CLIPBOARD
3193 * If we are using the clipboard, then remember what was selected in case
3194 * we need to paste it somewhere while we still own the selection.
3195 * Only do this when the clipboard is already owned. Don't want to grab
3196 * the selection when hitting ESC.
3198 if (clip_star.available && clip_star.owned)
3199 clip_auto_select();
3200 #endif
3202 VIsual_active = FALSE;
3203 #ifdef FEAT_MOUSE
3204 setmouse();
3205 mouse_dragging = 0;
3206 #endif
3208 /* Save the current VIsual area for '< and '> marks, and "gv" */
3209 curbuf->b_visual.vi_mode = VIsual_mode;
3210 curbuf->b_visual.vi_start = VIsual;
3211 curbuf->b_visual.vi_end = curwin->w_cursor;
3212 curbuf->b_visual.vi_curswant = curwin->w_curswant;
3213 #ifdef FEAT_EVAL
3214 curbuf->b_visual_mode_eval = VIsual_mode;
3215 #endif
3216 #ifdef FEAT_VIRTUALEDIT
3217 if (!virtual_active())
3218 curwin->w_cursor.coladd = 0;
3219 #endif
3221 if (mode_displayed)
3222 clear_cmdline = TRUE; /* unshow visual mode later */
3223 #ifdef FEAT_CMDL_INFO
3224 else
3225 clear_showcmd();
3226 #endif
3228 adjust_cursor_eol();
3232 * Reset VIsual_active and VIsual_reselect.
3234 void
3235 reset_VIsual_and_resel()
3237 if (VIsual_active)
3239 end_visual_mode();
3240 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3242 VIsual_reselect = FALSE;
3246 * Reset VIsual_active and VIsual_reselect if it's set.
3248 void
3249 reset_VIsual()
3251 if (VIsual_active)
3253 end_visual_mode();
3254 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3255 VIsual_reselect = FALSE;
3258 #endif /* FEAT_VISUAL */
3260 #if defined(FEAT_BEVAL)
3261 static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3264 * Check for a balloon-eval special item to include when searching for an
3265 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3266 * Returns TRUE if the character at "*ptr" should be included.
3267 * "dir" is FORWARD or BACKWARD, the direction of searching.
3268 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3269 * "bnp" points to a counter for square brackets.
3271 static int
3272 find_is_eval_item(ptr, colp, bnp, dir)
3273 char_u *ptr;
3274 int *colp;
3275 int *bnp;
3276 int dir;
3278 /* Accept everything inside []. */
3279 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3280 ++*bnp;
3281 if (*bnp > 0)
3283 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3284 --*bnp;
3285 return TRUE;
3288 /* skip over "s.var" */
3289 if (*ptr == '.')
3290 return TRUE;
3292 /* two-character item: s->var */
3293 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3294 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3296 *colp += dir;
3297 return TRUE;
3299 return FALSE;
3301 #endif
3304 * Find the identifier under or to the right of the cursor.
3305 * "find_type" can have one of three values:
3306 * FIND_IDENT: find an identifier (keyword)
3307 * FIND_STRING: find any non-white string
3308 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
3309 * FIND_EVAL: find text useful for C program debugging
3311 * There are three steps:
3312 * 1. Search forward for the start of an identifier/string. Doesn't move if
3313 * already on one.
3314 * 2. Search backward for the start of this identifier/string.
3315 * This doesn't match the real Vi but I like it a little better and it
3316 * shouldn't bother anyone.
3317 * 3. Search forward to the end of this identifier/string.
3318 * When FIND_IDENT isn't defined, we backup until a blank.
3320 * Returns the length of the string, or zero if no string is found.
3321 * If a string is found, a pointer to the string is put in "*string". This
3322 * string is not always NUL terminated.
3325 find_ident_under_cursor(string, find_type)
3326 char_u **string;
3327 int find_type;
3329 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3330 curwin->w_cursor.col, string, find_type);
3334 * Like find_ident_under_cursor(), but for any window and any position.
3335 * However: Uses 'iskeyword' from the current window!.
3338 find_ident_at_pos(wp, lnum, startcol, string, find_type)
3339 win_T *wp;
3340 linenr_T lnum;
3341 colnr_T startcol;
3342 char_u **string;
3343 int find_type;
3345 char_u *ptr;
3346 int col = 0; /* init to shut up GCC */
3347 int i;
3348 #ifdef FEAT_MBYTE
3349 int this_class = 0;
3350 int prev_class;
3351 int prevcol;
3352 #endif
3353 #if defined(FEAT_BEVAL)
3354 int bn = 0; /* bracket nesting */
3355 #endif
3358 * if i == 0: try to find an identifier
3359 * if i == 1: try to find any non-white string
3361 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3362 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3365 * 1. skip to start of identifier/string
3367 col = startcol;
3368 #ifdef FEAT_MBYTE
3369 if (has_mbyte)
3371 while (ptr[col] != NUL)
3373 # if defined(FEAT_BEVAL)
3374 /* Stop at a ']' to evaluate "a[x]". */
3375 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3376 break;
3377 # endif
3378 this_class = mb_get_class(ptr + col);
3379 if (this_class != 0 && (i == 1 || this_class != 1))
3380 break;
3381 col += (*mb_ptr2len)(ptr + col);
3384 else
3385 #endif
3386 while (ptr[col] != NUL
3387 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
3388 # if defined(FEAT_BEVAL)
3389 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3390 # endif
3392 ++col;
3394 #if defined(FEAT_BEVAL)
3395 /* When starting on a ']' count it, so that we include the '['. */
3396 bn = ptr[col] == ']';
3397 #endif
3400 * 2. Back up to start of identifier/string.
3402 #ifdef FEAT_MBYTE
3403 if (has_mbyte)
3405 /* Remember class of character under cursor. */
3406 # if defined(FEAT_BEVAL)
3407 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3408 this_class = mb_get_class((char_u *)"a");
3409 else
3410 # endif
3411 this_class = mb_get_class(ptr + col);
3412 while (col > 0 && this_class != 0)
3414 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3415 prev_class = mb_get_class(ptr + prevcol);
3416 if (this_class != prev_class
3417 && (i == 0
3418 || prev_class == 0
3419 || (find_type & FIND_IDENT))
3420 # if defined(FEAT_BEVAL)
3421 && (!(find_type & FIND_EVAL)
3422 || prevcol == 0
3423 || !find_is_eval_item(ptr + prevcol, &prevcol,
3424 &bn, BACKWARD))
3425 # endif
3427 break;
3428 col = prevcol;
3431 /* If we don't want just any old string, or we've found an
3432 * identifier, stop searching. */
3433 if (this_class > 2)
3434 this_class = 2;
3435 if (!(find_type & FIND_STRING) || this_class == 2)
3436 break;
3438 else
3439 #endif
3441 while (col > 0
3442 && ((i == 0
3443 ? vim_iswordc(ptr[col - 1])
3444 : (!vim_iswhite(ptr[col - 1])
3445 && (!(find_type & FIND_IDENT)
3446 || !vim_iswordc(ptr[col - 1]))))
3447 #if defined(FEAT_BEVAL)
3448 || ((find_type & FIND_EVAL)
3449 && col > 1
3450 && find_is_eval_item(ptr + col - 1, &col,
3451 &bn, BACKWARD))
3452 #endif
3454 --col;
3456 /* If we don't want just any old string, or we've found an
3457 * identifier, stop searching. */
3458 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3459 break;
3463 if (ptr[col] == NUL || (i == 0 && (
3464 #ifdef FEAT_MBYTE
3465 has_mbyte ? this_class != 2 :
3466 #endif
3467 !vim_iswordc(ptr[col]))))
3470 * didn't find an identifier or string
3472 if (find_type & FIND_STRING)
3473 EMSG(_("E348: No string under cursor"));
3474 else
3475 EMSG(_("E349: No identifier under cursor"));
3476 return 0;
3478 ptr += col;
3479 *string = ptr;
3482 * 3. Find the end if the identifier/string.
3484 #if defined(FEAT_BEVAL)
3485 bn = 0;
3486 startcol -= col;
3487 #endif
3488 col = 0;
3489 #ifdef FEAT_MBYTE
3490 if (has_mbyte)
3492 /* Search for point of changing multibyte character class. */
3493 this_class = mb_get_class(ptr);
3494 while (ptr[col] != NUL
3495 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3496 : mb_get_class(ptr + col) != 0)
3497 # if defined(FEAT_BEVAL)
3498 || ((find_type & FIND_EVAL)
3499 && col <= (int)startcol
3500 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3501 # endif
3503 col += (*mb_ptr2len)(ptr + col);
3505 else
3506 #endif
3507 while ((i == 0 ? vim_iswordc(ptr[col])
3508 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
3509 # if defined(FEAT_BEVAL)
3510 || ((find_type & FIND_EVAL)
3511 && col <= (int)startcol
3512 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3513 # endif
3516 ++col;
3519 return col;
3523 * Prepare for redo of a normal command.
3525 static void
3526 prep_redo_cmd(cap)
3527 cmdarg_T *cap;
3529 prep_redo(cap->oap->regname, cap->count0,
3530 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3534 * Prepare for redo of any command.
3535 * Note that only the last argument can be a multi-byte char.
3537 static void
3538 prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3539 int regname;
3540 long num;
3541 int cmd1;
3542 int cmd2;
3543 int cmd3;
3544 int cmd4;
3545 int cmd5;
3547 ResetRedobuff();
3548 if (regname != 0) /* yank from specified buffer */
3550 AppendCharToRedobuff('"');
3551 AppendCharToRedobuff(regname);
3553 if (num)
3554 AppendNumberToRedobuff(num);
3556 if (cmd1 != NUL)
3557 AppendCharToRedobuff(cmd1);
3558 if (cmd2 != NUL)
3559 AppendCharToRedobuff(cmd2);
3560 if (cmd3 != NUL)
3561 AppendCharToRedobuff(cmd3);
3562 if (cmd4 != NUL)
3563 AppendCharToRedobuff(cmd4);
3564 if (cmd5 != NUL)
3565 AppendCharToRedobuff(cmd5);
3569 * check for operator active and clear it
3571 * return TRUE if operator was active
3573 static int
3574 checkclearop(oap)
3575 oparg_T *oap;
3577 if (oap->op_type == OP_NOP)
3578 return FALSE;
3579 clearopbeep(oap);
3580 return TRUE;
3584 * Check for operator or Visual active. Clear active operator.
3586 * Return TRUE if operator or Visual was active.
3588 static int
3589 checkclearopq(oap)
3590 oparg_T *oap;
3592 if (oap->op_type == OP_NOP
3593 #ifdef FEAT_VISUAL
3594 && !VIsual_active
3595 #endif
3597 return FALSE;
3598 clearopbeep(oap);
3599 return TRUE;
3602 static void
3603 clearop(oap)
3604 oparg_T *oap;
3606 oap->op_type = OP_NOP;
3607 oap->regname = 0;
3608 oap->motion_force = NUL;
3609 oap->use_reg_one = FALSE;
3612 static void
3613 clearopbeep(oap)
3614 oparg_T *oap;
3616 clearop(oap);
3617 beep_flush();
3620 #ifdef FEAT_VISUAL
3622 * Remove the shift modifier from a special key.
3624 static void
3625 unshift_special(cap)
3626 cmdarg_T *cap;
3628 switch (cap->cmdchar)
3630 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3631 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3632 case K_S_UP: cap->cmdchar = K_UP; break;
3633 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3634 case K_S_HOME: cap->cmdchar = K_HOME; break;
3635 case K_S_END: cap->cmdchar = K_END; break;
3637 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3639 #endif
3641 #if defined(FEAT_CMDL_INFO) || defined(PROTO)
3643 * Routines for displaying a partly typed command
3646 #ifdef FEAT_VISUAL /* need room for size of Visual area */
3647 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3648 #else
3649 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3650 #endif
3651 static char_u showcmd_buf[SHOWCMD_BUFLEN];
3652 static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3653 static int showcmd_is_clear = TRUE;
3654 static int showcmd_visual = FALSE;
3656 static void display_showcmd __ARGS((void));
3658 void
3659 clear_showcmd()
3661 if (!p_sc)
3662 return;
3664 #ifdef FEAT_VISUAL
3665 if (VIsual_active && !char_avail())
3667 int i = lt(VIsual, curwin->w_cursor);
3668 long lines;
3669 colnr_T leftcol, rightcol;
3670 linenr_T top, bot;
3672 /* Show the size of the Visual area. */
3673 if (i)
3675 top = VIsual.lnum;
3676 bot = curwin->w_cursor.lnum;
3678 else
3680 top = curwin->w_cursor.lnum;
3681 bot = VIsual.lnum;
3683 # ifdef FEAT_FOLDING
3684 /* Include closed folds as a whole. */
3685 hasFolding(top, &top, NULL);
3686 hasFolding(bot, NULL, &bot);
3687 # endif
3688 lines = bot - top + 1;
3690 if (VIsual_mode == Ctrl_V)
3692 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
3693 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3694 (long)(rightcol - leftcol + 1));
3696 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3697 sprintf((char *)showcmd_buf, "%ld", lines);
3698 else
3699 sprintf((char *)showcmd_buf, "%ld", (long)(i
3700 ? curwin->w_cursor.col - VIsual.col
3701 : VIsual.col - curwin->w_cursor.col) + (*p_sel != 'e'));
3702 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3703 showcmd_visual = TRUE;
3705 else
3706 #endif
3708 showcmd_buf[0] = NUL;
3709 showcmd_visual = FALSE;
3711 /* Don't actually display something if there is nothing to clear. */
3712 if (showcmd_is_clear)
3713 return;
3716 display_showcmd();
3720 * Add 'c' to string of shown command chars.
3721 * Return TRUE if output has been written (and setcursor() has been called).
3724 add_to_showcmd(c)
3725 int c;
3727 char_u *p;
3728 int old_len;
3729 int extra_len;
3730 int overflow;
3731 #if defined(FEAT_MOUSE)
3732 int i;
3733 static int ignore[] =
3735 # ifdef FEAT_GUI
3736 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3737 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
3738 # endif
3739 K_IGNORE,
3740 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3741 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3742 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
3743 K_MOUSEDOWN, K_MOUSEUP,
3744 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
3745 K_CURSORHOLD,
3748 #endif
3750 if (!p_sc || msg_silent != 0)
3751 return FALSE;
3753 if (showcmd_visual)
3755 showcmd_buf[0] = NUL;
3756 showcmd_visual = FALSE;
3759 #if defined(FEAT_MOUSE)
3760 /* Ignore keys that are scrollbar updates and mouse clicks */
3761 if (IS_SPECIAL(c))
3762 for (i = 0; ignore[i] != 0; ++i)
3763 if (ignore[i] == c)
3764 return FALSE;
3765 #endif
3767 p = transchar(c);
3768 old_len = (int)STRLEN(showcmd_buf);
3769 extra_len = (int)STRLEN(p);
3770 overflow = old_len + extra_len - SHOWCMD_COLS;
3771 if (overflow > 0)
3772 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3773 old_len - overflow + 1);
3774 STRCAT(showcmd_buf, p);
3776 if (char_avail())
3777 return FALSE;
3779 display_showcmd();
3781 return TRUE;
3784 void
3785 add_to_showcmd_c(c)
3786 int c;
3788 if (!add_to_showcmd(c))
3789 setcursor();
3793 * Delete 'len' characters from the end of the shown command.
3795 static void
3796 del_from_showcmd(len)
3797 int len;
3799 int old_len;
3801 if (!p_sc)
3802 return;
3804 old_len = (int)STRLEN(showcmd_buf);
3805 if (len > old_len)
3806 len = old_len;
3807 showcmd_buf[old_len - len] = NUL;
3809 if (!char_avail())
3810 display_showcmd();
3814 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3815 * something and there is a partial mapping.
3817 void
3818 push_showcmd()
3820 if (p_sc)
3821 STRCPY(old_showcmd_buf, showcmd_buf);
3824 void
3825 pop_showcmd()
3827 if (!p_sc)
3828 return;
3830 STRCPY(showcmd_buf, old_showcmd_buf);
3832 display_showcmd();
3835 static void
3836 display_showcmd()
3838 int len;
3840 cursor_off();
3842 len = (int)STRLEN(showcmd_buf);
3843 if (len == 0)
3844 showcmd_is_clear = TRUE;
3845 else
3847 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3848 showcmd_is_clear = FALSE;
3852 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3853 * spaces
3855 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3857 setcursor(); /* put cursor back where it belongs */
3859 #endif
3861 #ifdef FEAT_SCROLLBIND
3863 * When "check" is FALSE, prepare for commands that scroll the window.
3864 * When "check" is TRUE, take care of scroll-binding after the window has
3865 * scrolled. Called from normal_cmd() and edit().
3867 void
3868 do_check_scrollbind(check)
3869 int check;
3871 static win_T *old_curwin = NULL;
3872 static linenr_T old_topline = 0;
3873 #ifdef FEAT_DIFF
3874 static int old_topfill = 0;
3875 #endif
3876 static buf_T *old_buf = NULL;
3877 static colnr_T old_leftcol = 0;
3879 if (check && curwin->w_p_scb)
3881 /* If a ":syncbind" command was just used, don't scroll, only reset
3882 * the values. */
3883 if (did_syncbind)
3884 did_syncbind = FALSE;
3885 else if (curwin == old_curwin)
3888 * Synchronize other windows, as necessary according to
3889 * 'scrollbind'. Don't do this after an ":edit" command, except
3890 * when 'diff' is set.
3892 if ((curwin->w_buffer == old_buf
3893 #ifdef FEAT_DIFF
3894 || curwin->w_p_diff
3895 #endif
3897 && (curwin->w_topline != old_topline
3898 #ifdef FEAT_DIFF
3899 || curwin->w_topfill != old_topfill
3900 #endif
3901 || curwin->w_leftcol != old_leftcol))
3903 check_scrollbind(curwin->w_topline - old_topline,
3904 (long)(curwin->w_leftcol - old_leftcol));
3907 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
3910 * When switching between windows, make sure that the relative
3911 * vertical offset is valid for the new window. The relative
3912 * offset is invalid whenever another 'scrollbind' window has
3913 * scrolled to a point that would force the current window to
3914 * scroll past the beginning or end of its buffer. When the
3915 * resync is performed, some of the other 'scrollbind' windows may
3916 * need to jump so that the current window's relative position is
3917 * visible on-screen.
3919 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
3921 curwin->w_scbind_pos = curwin->w_topline;
3924 old_curwin = curwin;
3925 old_topline = curwin->w_topline;
3926 #ifdef FEAT_DIFF
3927 old_topfill = curwin->w_topfill;
3928 #endif
3929 old_buf = curwin->w_buffer;
3930 old_leftcol = curwin->w_leftcol;
3934 * Synchronize any windows that have "scrollbind" set, based on the
3935 * number of rows by which the current window has changed
3936 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3938 void
3939 check_scrollbind(topline_diff, leftcol_diff)
3940 linenr_T topline_diff;
3941 long leftcol_diff;
3943 int want_ver;
3944 int want_hor;
3945 win_T *old_curwin = curwin;
3946 buf_T *old_curbuf = curbuf;
3947 #ifdef FEAT_VISUAL
3948 int old_VIsual_select = VIsual_select;
3949 int old_VIsual_active = VIsual_active;
3950 #endif
3951 colnr_T tgt_leftcol = curwin->w_leftcol;
3952 long topline;
3953 long y;
3956 * check 'scrollopt' string for vertical and horizontal scroll options
3958 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
3959 #ifdef FEAT_DIFF
3960 want_ver |= old_curwin->w_p_diff;
3961 #endif
3962 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
3965 * loop through the scrollbound windows and scroll accordingly
3967 #ifdef FEAT_VISUAL
3968 VIsual_select = VIsual_active = 0;
3969 #endif
3970 for (curwin = firstwin; curwin; curwin = curwin->w_next)
3972 curbuf = curwin->w_buffer;
3973 /* skip original window and windows with 'noscrollbind' */
3974 if (curwin != old_curwin && curwin->w_p_scb)
3977 * do the vertical scroll
3979 if (want_ver)
3981 #ifdef FEAT_DIFF
3982 if (old_curwin->w_p_diff && curwin->w_p_diff)
3984 diff_set_topline(old_curwin, curwin);
3986 else
3987 #endif
3989 curwin->w_scbind_pos += topline_diff;
3990 topline = curwin->w_scbind_pos;
3991 if (topline > curbuf->b_ml.ml_line_count)
3992 topline = curbuf->b_ml.ml_line_count;
3993 if (topline < 1)
3994 topline = 1;
3996 y = topline - curwin->w_topline;
3997 if (y > 0)
3998 scrollup(y, FALSE);
3999 else
4000 scrolldown(-y, FALSE);
4003 redraw_later(VALID);
4004 cursor_correct();
4005 #ifdef FEAT_WINDOWS
4006 curwin->w_redr_status = TRUE;
4007 #endif
4011 * do the horizontal scroll
4013 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4015 curwin->w_leftcol = tgt_leftcol;
4016 leftcol_changed();
4022 * reset current-window
4024 #ifdef FEAT_VISUAL
4025 VIsual_select = old_VIsual_select;
4026 VIsual_active = old_VIsual_active;
4027 #endif
4028 curwin = old_curwin;
4029 curbuf = old_curbuf;
4031 #endif /* #ifdef FEAT_SCROLLBIND */
4034 * Command character that's ignored.
4035 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
4036 * xon/xoff
4038 static void
4039 nv_ignore(cap)
4040 cmdarg_T *cap;
4042 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
4046 * Command character that doesn't do anything, but unlike nv_ignore() does
4047 * start edit(). Used for "startinsert" executed while starting up.
4049 /*ARGSUSED */
4050 static void
4051 nv_nop(cap)
4052 cmdarg_T *cap;
4057 * Command character doesn't exist.
4059 static void
4060 nv_error(cap)
4061 cmdarg_T *cap;
4063 clearopbeep(cap->oap);
4067 * <Help> and <F1> commands.
4069 static void
4070 nv_help(cap)
4071 cmdarg_T *cap;
4073 if (!checkclearopq(cap->oap))
4074 ex_help(NULL);
4078 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4080 static void
4081 nv_addsub(cap)
4082 cmdarg_T *cap;
4084 if (!checkclearopq(cap->oap)
4085 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4086 prep_redo_cmd(cap);
4090 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4092 static void
4093 nv_page(cap)
4094 cmdarg_T *cap;
4096 if (!checkclearop(cap->oap))
4098 #ifdef FEAT_WINDOWS
4099 if (mod_mask & MOD_MASK_CTRL)
4101 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4102 if (cap->arg == BACKWARD)
4103 goto_tabpage(-(int)cap->count1);
4104 else
4105 goto_tabpage((int)cap->count0);
4107 else
4108 #endif
4109 (void)onepage(cap->arg, cap->count1);
4114 * Implementation of "gd" and "gD" command.
4116 static void
4117 nv_gd(oap, nchar, thisblock)
4118 oparg_T *oap;
4119 int nchar;
4120 int thisblock; /* 1 for "1gd" and "1gD" */
4122 int len;
4123 char_u *ptr;
4125 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
4126 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
4127 clearopbeep(oap);
4128 #ifdef FEAT_FOLDING
4129 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4130 foldOpenCursor();
4131 #endif
4135 * Search for variable declaration of "ptr[len]".
4136 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4137 * current file ("gD").
4138 * When "thisblock" is TRUE check the {} block scope.
4139 * Return FAIL when not found.
4142 find_decl(ptr, len, locally, thisblock, searchflags)
4143 char_u *ptr;
4144 int len;
4145 int locally;
4146 int thisblock;
4147 int searchflags; /* flags passed to searchit() */
4149 char_u *pat;
4150 pos_T old_pos;
4151 pos_T par_pos;
4152 pos_T found_pos;
4153 int t;
4154 int save_p_ws;
4155 int save_p_scs;
4156 int retval = OK;
4157 int incll;
4159 if ((pat = alloc(len + 7)) == NULL)
4160 return FAIL;
4162 /* Put "\V" before the pattern to avoid that the special meaning of "."
4163 * and "~" causes trouble. */
4164 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4165 len, ptr);
4166 old_pos = curwin->w_cursor;
4167 save_p_ws = p_ws;
4168 save_p_scs = p_scs;
4169 p_ws = FALSE; /* don't wrap around end of file now */
4170 p_scs = FALSE; /* don't switch ignorecase off now */
4173 * With "gD" go to line 1.
4174 * With "gd" Search back for the start of the current function, then go
4175 * back until a blank line. If this fails go to line 1.
4177 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
4179 setpcmark(); /* Set in findpar() otherwise */
4180 curwin->w_cursor.lnum = 1;
4181 par_pos = curwin->w_cursor;
4183 else
4185 par_pos = curwin->w_cursor;
4186 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4187 --curwin->w_cursor.lnum;
4189 curwin->w_cursor.col = 0;
4191 /* Search forward for the identifier, ignore comment lines. */
4192 clearpos(&found_pos);
4193 for (;;)
4195 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
4196 pat, 1L, searchflags, RE_LAST, (linenr_T)0);
4197 if (curwin->w_cursor.lnum >= old_pos.lnum)
4198 t = FAIL; /* match after start is failure too */
4200 if (thisblock && t != FAIL)
4202 pos_T *pos;
4204 /* Check that the block the match is in doesn't end before the
4205 * position where we started the search from. */
4206 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4207 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4208 && pos->lnum < old_pos.lnum)
4209 continue;
4212 if (t == FAIL)
4214 /* If we previously found a valid position, use it. */
4215 if (found_pos.lnum != 0)
4217 curwin->w_cursor = found_pos;
4218 t = OK;
4220 break;
4222 #ifdef FEAT_COMMENTS
4223 if (get_leader_len(ml_get_curline(), NULL, FALSE) > 0)
4225 /* Ignore this line, continue at start of next line. */
4226 ++curwin->w_cursor.lnum;
4227 curwin->w_cursor.col = 0;
4228 continue;
4230 #endif
4231 if (!locally) /* global search: use first match found */
4232 break;
4233 if (curwin->w_cursor.lnum >= par_pos.lnum)
4235 /* If we previously found a valid position, use it. */
4236 if (found_pos.lnum != 0)
4237 curwin->w_cursor = found_pos;
4238 break;
4241 /* For finding a local variable and the match is before the "{" search
4242 * to find a later match. For K&R style function declarations this
4243 * skips the function header without types. */
4244 found_pos = curwin->w_cursor;
4247 if (t == FAIL)
4249 retval = FAIL;
4250 curwin->w_cursor = old_pos;
4252 else
4254 curwin->w_set_curswant = TRUE;
4255 /* "n" searches forward now */
4256 reset_search_dir();
4259 vim_free(pat);
4260 p_ws = save_p_ws;
4261 p_scs = save_p_scs;
4263 return retval;
4267 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4268 * lines rather than lines in the file.
4269 * 'dist' must be positive.
4271 * Return OK if able to move cursor, FAIL otherwise.
4273 static int
4274 nv_screengo(oap, dir, dist)
4275 oparg_T *oap;
4276 int dir;
4277 long dist;
4279 int linelen = linetabsize(ml_get_curline());
4280 int retval = OK;
4281 int atend = FALSE;
4282 int n;
4283 int col_off1; /* margin offset for first screen line */
4284 int col_off2; /* margin offset for wrapped screen line */
4285 int width1; /* text width for first screen line */
4286 int width2; /* test width for wrapped screen line */
4288 oap->motion_type = MCHAR;
4289 oap->inclusive = FALSE;
4291 col_off1 = curwin_col_off();
4292 col_off2 = col_off1 - curwin_col_off2();
4293 width1 = W_WIDTH(curwin) - col_off1;
4294 width2 = W_WIDTH(curwin) - col_off2;
4296 #ifdef FEAT_VERTSPLIT
4297 if (curwin->w_width != 0)
4299 #endif
4301 * Instead of sticking at the last character of the buffer line we
4302 * try to stick in the last column of the screen.
4304 if (curwin->w_curswant == MAXCOL)
4306 atend = TRUE;
4307 validate_virtcol();
4308 if (width1 <= 0)
4309 curwin->w_curswant = 0;
4310 else
4312 curwin->w_curswant = width1 - 1;
4313 if (curwin->w_virtcol > curwin->w_curswant)
4314 curwin->w_curswant += ((curwin->w_virtcol
4315 - curwin->w_curswant - 1) / width2 + 1) * width2;
4318 else
4320 if (linelen > width1)
4321 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4322 else
4323 n = width1;
4324 if (curwin->w_curswant > (colnr_T)n + 1)
4325 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4326 * width2;
4329 while (dist--)
4331 if (dir == BACKWARD)
4333 if ((long)curwin->w_curswant >= width2)
4334 /* move back within line */
4335 curwin->w_curswant -= width2;
4336 else
4338 /* to previous line */
4339 if (curwin->w_cursor.lnum == 1)
4341 retval = FAIL;
4342 break;
4344 --curwin->w_cursor.lnum;
4345 #ifdef FEAT_FOLDING
4346 /* Move to the start of a closed fold. Don't do that when
4347 * 'foldopen' contains "all": it will open in a moment. */
4348 if (!(fdo_flags & FDO_ALL))
4349 (void)hasFolding(curwin->w_cursor.lnum,
4350 &curwin->w_cursor.lnum, NULL);
4351 #endif
4352 linelen = linetabsize(ml_get_curline());
4353 if (linelen > width1)
4354 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4355 + 1) * width2;
4358 else /* dir == FORWARD */
4360 if (linelen > width1)
4361 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4362 else
4363 n = width1;
4364 if (curwin->w_curswant + width2 < (colnr_T)n)
4365 /* move forward within line */
4366 curwin->w_curswant += width2;
4367 else
4369 /* to next line */
4370 #ifdef FEAT_FOLDING
4371 /* Move to the end of a closed fold. */
4372 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4373 &curwin->w_cursor.lnum);
4374 #endif
4375 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4377 retval = FAIL;
4378 break;
4380 curwin->w_cursor.lnum++;
4381 curwin->w_curswant %= width2;
4385 #ifdef FEAT_VERTSPLIT
4387 #endif
4389 coladvance(curwin->w_curswant);
4391 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4392 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4395 * Check for landing on a character that got split at the end of the
4396 * last line. We want to advance a screenline, not end up in the same
4397 * screenline or move two screenlines.
4399 validate_virtcol();
4400 if (curwin->w_virtcol > curwin->w_curswant
4401 && (curwin->w_curswant < (colnr_T)width1
4402 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4403 : ((curwin->w_curswant - width1) % width2
4404 > (colnr_T)width2 / 2)))
4405 --curwin->w_cursor.col;
4407 #endif
4409 if (atend)
4410 curwin->w_curswant = MAXCOL; /* stick in the last column */
4412 return retval;
4415 #ifdef FEAT_MOUSE
4417 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4418 * when Shift or Ctrl is used.
4419 * K_MOUSEUP (cap->arg == TRUE) or K_MOUSEDOWN (cap->arg == FALSE)
4421 static void
4422 nv_mousescroll(cap)
4423 cmdarg_T *cap;
4425 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4426 win_T *old_curwin = curwin;
4428 /* Currently we only get the mouse coordinates in the GUI. */
4429 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4431 int row, col;
4433 row = mouse_row;
4434 col = mouse_col;
4436 /* find the window at the pointer coordinates */
4437 curwin = mouse_find_win(&row, &col);
4438 curbuf = curwin->w_buffer;
4440 # endif
4442 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4444 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4446 else
4448 cap->count1 = 3;
4449 cap->count0 = 3;
4450 nv_scroll_line(cap);
4453 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4454 curwin->w_redr_status = TRUE;
4456 curwin = old_curwin;
4457 curbuf = curwin->w_buffer;
4458 # endif
4462 * Mouse clicks and drags.
4464 static void
4465 nv_mouse(cap)
4466 cmdarg_T *cap;
4468 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4470 #endif
4473 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4474 * cap->arg must be TRUE for CTRL-E.
4476 static void
4477 nv_scroll_line(cap)
4478 cmdarg_T *cap;
4480 if (!checkclearop(cap->oap))
4481 scroll_redraw(cap->arg, cap->count1);
4485 * Scroll "count" lines up or down, and redraw.
4487 void
4488 scroll_redraw(up, count)
4489 int up;
4490 long count;
4492 linenr_T prev_topline = curwin->w_topline;
4493 #ifdef FEAT_DIFF
4494 int prev_topfill = curwin->w_topfill;
4495 #endif
4496 linenr_T prev_lnum = curwin->w_cursor.lnum;
4498 if (up)
4499 scrollup(count, TRUE);
4500 else
4501 scrolldown(count, TRUE);
4502 if (p_so)
4504 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4505 * valid, otherwise the screen jumps back at the end of the file. */
4506 cursor_correct();
4507 check_cursor_moved(curwin);
4508 curwin->w_valid |= VALID_TOPLINE;
4510 /* If moved back to where we were, at least move the cursor, otherwise
4511 * we get stuck at one position. Don't move the cursor up if the
4512 * first line of the buffer is already on the screen */
4513 while (curwin->w_topline == prev_topline
4514 #ifdef FEAT_DIFF
4515 && curwin->w_topfill == prev_topfill
4516 #endif
4519 if (up)
4521 if (curwin->w_cursor.lnum > prev_lnum
4522 || cursor_down(1L, FALSE) == FAIL)
4523 break;
4525 else
4527 if (curwin->w_cursor.lnum < prev_lnum
4528 || prev_topline == 1L
4529 || cursor_up(1L, FALSE) == FAIL)
4530 break;
4532 /* Mark w_topline as valid, otherwise the screen jumps back at the
4533 * end of the file. */
4534 check_cursor_moved(curwin);
4535 curwin->w_valid |= VALID_TOPLINE;
4538 if (curwin->w_cursor.lnum != prev_lnum)
4539 coladvance(curwin->w_curswant);
4540 redraw_later(VALID);
4544 * Commands that start with "z".
4546 static void
4547 nv_zet(cap)
4548 cmdarg_T *cap;
4550 long n;
4551 colnr_T col;
4552 int nchar = cap->nchar;
4553 #ifdef FEAT_FOLDING
4554 long old_fdl = curwin->w_p_fdl;
4555 int old_fen = curwin->w_p_fen;
4556 #endif
4557 #ifdef FEAT_SPELL
4558 int undo = FALSE;
4559 #endif
4561 if (VIM_ISDIGIT(nchar))
4564 * "z123{nchar}": edit the count before obtaining {nchar}
4566 if (checkclearop(cap->oap))
4567 return;
4568 n = nchar - '0';
4569 for (;;)
4571 #ifdef USE_ON_FLY_SCROLL
4572 dont_scroll = TRUE; /* disallow scrolling here */
4573 #endif
4574 ++no_mapping;
4575 ++allow_keys; /* no mapping for nchar, but allow key codes */
4576 nchar = plain_vgetc();
4577 #ifdef FEAT_LANGMAP
4578 LANGMAP_ADJUST(nchar, TRUE);
4579 #endif
4580 --no_mapping;
4581 --allow_keys;
4582 #ifdef FEAT_CMDL_INFO
4583 (void)add_to_showcmd(nchar);
4584 #endif
4585 if (nchar == K_DEL || nchar == K_KDEL)
4586 n /= 10;
4587 else if (VIM_ISDIGIT(nchar))
4588 n = n * 10 + (nchar - '0');
4589 else if (nchar == CAR)
4591 #ifdef FEAT_GUI
4592 need_mouse_correct = TRUE;
4593 #endif
4594 win_setheight((int)n);
4595 break;
4597 else if (nchar == 'l'
4598 || nchar == 'h'
4599 || nchar == K_LEFT
4600 || nchar == K_RIGHT)
4602 cap->count1 = n ? n * cap->count1 : cap->count1;
4603 goto dozet;
4605 else
4607 clearopbeep(cap->oap);
4608 break;
4611 cap->oap->op_type = OP_NOP;
4612 return;
4615 dozet:
4616 if (
4617 #ifdef FEAT_FOLDING
4618 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4619 * and "zC" only in Visual mode. "zj" and "zk" are motion
4620 * commands. */
4621 cap->nchar != 'f' && cap->nchar != 'F'
4622 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4623 && cap->nchar != 'j' && cap->nchar != 'k'
4625 #endif
4626 checkclearop(cap->oap))
4627 return;
4630 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4631 * If line number given, set cursor.
4633 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4634 && cap->count0
4635 && cap->count0 != curwin->w_cursor.lnum)
4637 setpcmark();
4638 if (cap->count0 > curbuf->b_ml.ml_line_count)
4639 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4640 else
4641 curwin->w_cursor.lnum = cap->count0;
4642 check_cursor_col();
4645 switch (nchar)
4647 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4648 case '+':
4649 if (cap->count0 == 0)
4651 /* No count given: put cursor at the line below screen */
4652 validate_botline(); /* make sure w_botline is valid */
4653 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4654 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4655 else
4656 curwin->w_cursor.lnum = curwin->w_botline;
4658 /* FALLTHROUGH */
4659 case NL:
4660 case CAR:
4661 case K_KENTER:
4662 beginline(BL_WHITE | BL_FIX);
4663 /* FALLTHROUGH */
4665 case 't': scroll_cursor_top(0, TRUE);
4666 redraw_later(VALID);
4667 break;
4669 /* "z." and "zz": put cursor in middle of screen */
4670 case '.': beginline(BL_WHITE | BL_FIX);
4671 /* FALLTHROUGH */
4673 case 'z': scroll_cursor_halfway(TRUE);
4674 redraw_later(VALID);
4675 break;
4677 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4678 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4679 * when <count> is at bottom of window, and puts that one at
4680 * bottom of window. */
4681 if (cap->count0 != 0)
4683 scroll_cursor_bot(0, TRUE);
4684 curwin->w_cursor.lnum = curwin->w_topline;
4686 else if (curwin->w_topline == 1)
4687 curwin->w_cursor.lnum = 1;
4688 else
4689 curwin->w_cursor.lnum = curwin->w_topline - 1;
4690 /* FALLTHROUGH */
4691 case '-':
4692 beginline(BL_WHITE | BL_FIX);
4693 /* FALLTHROUGH */
4695 case 'b': scroll_cursor_bot(0, TRUE);
4696 redraw_later(VALID);
4697 break;
4699 /* "zH" - scroll screen right half-page */
4700 case 'H':
4701 cap->count1 *= W_WIDTH(curwin) / 2;
4702 /* FALLTHROUGH */
4704 /* "zh" - scroll screen to the right */
4705 case 'h':
4706 case K_LEFT:
4707 if (!curwin->w_p_wrap)
4709 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4710 curwin->w_leftcol = 0;
4711 else
4712 curwin->w_leftcol -= (colnr_T)cap->count1;
4713 leftcol_changed();
4715 break;
4717 /* "zL" - scroll screen left half-page */
4718 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4719 /* FALLTHROUGH */
4721 /* "zl" - scroll screen to the left */
4722 case 'l':
4723 case K_RIGHT:
4724 if (!curwin->w_p_wrap)
4726 /* scroll the window left */
4727 curwin->w_leftcol += (colnr_T)cap->count1;
4728 leftcol_changed();
4730 break;
4732 /* "zs" - scroll screen, cursor at the start */
4733 case 's': if (!curwin->w_p_wrap)
4735 #ifdef FEAT_FOLDING
4736 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4737 col = 0; /* like the cursor is in col 0 */
4738 else
4739 #endif
4740 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4741 if ((long)col > p_siso)
4742 col -= p_siso;
4743 else
4744 col = 0;
4745 if (curwin->w_leftcol != col)
4747 curwin->w_leftcol = col;
4748 redraw_later(NOT_VALID);
4751 break;
4753 /* "ze" - scroll screen, cursor at the end */
4754 case 'e': if (!curwin->w_p_wrap)
4756 #ifdef FEAT_FOLDING
4757 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4758 col = 0; /* like the cursor is in col 0 */
4759 else
4760 #endif
4761 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4762 n = W_WIDTH(curwin) - curwin_col_off();
4763 if ((long)col + p_siso < n)
4764 col = 0;
4765 else
4766 col = col + p_siso - n + 1;
4767 if (curwin->w_leftcol != col)
4769 curwin->w_leftcol = col;
4770 redraw_later(NOT_VALID);
4773 break;
4775 #ifdef FEAT_FOLDING
4776 /* "zF": create fold command */
4777 /* "zf": create fold operator */
4778 case 'F':
4779 case 'f': if (foldManualAllowed(TRUE))
4781 cap->nchar = 'f';
4782 nv_operator(cap);
4783 curwin->w_p_fen = TRUE;
4785 /* "zF" is like "zfzf" */
4786 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4788 nv_operator(cap);
4789 finish_op = TRUE;
4792 else
4793 clearopbeep(cap->oap);
4794 break;
4796 /* "zd": delete fold at cursor */
4797 /* "zD": delete fold at cursor recursively */
4798 case 'd':
4799 case 'D': if (foldManualAllowed(FALSE))
4801 if (VIsual_active)
4802 nv_operator(cap);
4803 else
4804 deleteFold(curwin->w_cursor.lnum,
4805 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4807 break;
4809 /* "zE": erease all folds */
4810 case 'E': if (foldmethodIsManual(curwin))
4812 clearFolding(curwin);
4813 changed_window_setting();
4815 else if (foldmethodIsMarker(curwin))
4816 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4817 TRUE, FALSE);
4818 else
4819 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4820 break;
4822 /* "zn": fold none: reset 'foldenable' */
4823 case 'n': curwin->w_p_fen = FALSE;
4824 break;
4826 /* "zN": fold Normal: set 'foldenable' */
4827 case 'N': curwin->w_p_fen = TRUE;
4828 break;
4830 /* "zi": invert folding: toggle 'foldenable' */
4831 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4832 break;
4834 /* "za": open closed fold or close open fold at cursor */
4835 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4836 openFold(curwin->w_cursor.lnum, cap->count1);
4837 else
4839 closeFold(curwin->w_cursor.lnum, cap->count1);
4840 curwin->w_p_fen = TRUE;
4842 break;
4844 /* "zA": open fold at cursor recursively */
4845 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4846 openFoldRecurse(curwin->w_cursor.lnum);
4847 else
4849 closeFoldRecurse(curwin->w_cursor.lnum);
4850 curwin->w_p_fen = TRUE;
4852 break;
4854 /* "zo": open fold at cursor or Visual area */
4855 case 'o': if (VIsual_active)
4856 nv_operator(cap);
4857 else
4858 openFold(curwin->w_cursor.lnum, cap->count1);
4859 break;
4861 /* "zO": open fold recursively */
4862 case 'O': if (VIsual_active)
4863 nv_operator(cap);
4864 else
4865 openFoldRecurse(curwin->w_cursor.lnum);
4866 break;
4868 /* "zc": close fold at cursor or Visual area */
4869 case 'c': if (VIsual_active)
4870 nv_operator(cap);
4871 else
4872 closeFold(curwin->w_cursor.lnum, cap->count1);
4873 curwin->w_p_fen = TRUE;
4874 break;
4876 /* "zC": close fold recursively */
4877 case 'C': if (VIsual_active)
4878 nv_operator(cap);
4879 else
4880 closeFoldRecurse(curwin->w_cursor.lnum);
4881 curwin->w_p_fen = TRUE;
4882 break;
4884 /* "zv": open folds at the cursor */
4885 case 'v': foldOpenCursor();
4886 break;
4888 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
4889 case 'x': curwin->w_p_fen = TRUE;
4890 newFoldLevel(); /* update right now */
4891 foldOpenCursor();
4892 break;
4894 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
4895 case 'X': curwin->w_p_fen = TRUE;
4896 old_fdl = -1; /* force an update */
4897 break;
4899 /* "zm": fold more */
4900 case 'm': if (curwin->w_p_fdl > 0)
4901 --curwin->w_p_fdl;
4902 old_fdl = -1; /* force an update */
4903 curwin->w_p_fen = TRUE;
4904 break;
4906 /* "zM": close all folds */
4907 case 'M': curwin->w_p_fdl = 0;
4908 old_fdl = -1; /* force an update */
4909 curwin->w_p_fen = TRUE;
4910 break;
4912 /* "zr": reduce folding */
4913 case 'r': ++curwin->w_p_fdl;
4914 break;
4916 /* "zR": open all folds */
4917 case 'R': curwin->w_p_fdl = getDeepestNesting();
4918 old_fdl = -1; /* force an update */
4919 break;
4921 case 'j': /* "zj" move to next fold downwards */
4922 case 'k': /* "zk" move to next fold upwards */
4923 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
4924 cap->count1) == FAIL)
4925 clearopbeep(cap->oap);
4926 break;
4928 #endif /* FEAT_FOLDING */
4930 #ifdef FEAT_SPELL
4931 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
4932 ++no_mapping;
4933 ++allow_keys; /* no mapping for nchar, but allow key codes */
4934 nchar = plain_vgetc();
4935 #ifdef FEAT_LANGMAP
4936 LANGMAP_ADJUST(nchar, TRUE);
4937 #endif
4938 --no_mapping;
4939 --allow_keys;
4940 #ifdef FEAT_CMDL_INFO
4941 (void)add_to_showcmd(nchar);
4942 #endif
4943 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
4945 clearopbeep(cap->oap);
4946 break;
4948 undo = TRUE;
4949 /*FALLTHROUGH*/
4951 case 'g': /* "zg": add good word to word list */
4952 case 'w': /* "zw": add wrong word to word list */
4953 case 'G': /* "zG": add good word to temp word list */
4954 case 'W': /* "zW": add wrong word to temp word list */
4956 char_u *ptr = NULL;
4957 int len;
4959 if (checkclearop(cap->oap))
4960 break;
4961 # ifdef FEAT_VISUAL
4962 if (VIsual_active && get_visual_text(cap, &ptr, &len)
4963 == FAIL)
4964 return;
4965 # endif
4966 if (ptr == NULL)
4968 pos_T pos = curwin->w_cursor;
4970 /* Find bad word under the cursor. */
4971 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
4972 if (len != 0 && curwin->w_cursor.col <= pos.col)
4973 ptr = ml_get_pos(&curwin->w_cursor);
4974 curwin->w_cursor = pos;
4977 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
4978 FIND_IDENT)) == 0)
4979 return;
4980 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
4981 (nchar == 'G' || nchar == 'W')
4982 ? 0 : (int)cap->count1,
4983 undo);
4985 break;
4987 case '=': /* "z=": suggestions for a badly spelled word */
4988 if (!checkclearop(cap->oap))
4989 spell_suggest((int)cap->count0);
4990 break;
4991 #endif
4993 default: clearopbeep(cap->oap);
4996 #ifdef FEAT_FOLDING
4997 /* Redraw when 'foldenable' changed */
4998 if (old_fen != curwin->w_p_fen)
5000 # ifdef FEAT_DIFF
5001 win_T *wp;
5003 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5005 /* Adjust 'foldenable' in diff-synced windows. */
5006 FOR_ALL_WINDOWS(wp)
5008 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5010 wp->w_p_fen = curwin->w_p_fen;
5011 changed_window_setting_win(wp);
5015 # endif
5016 changed_window_setting();
5019 /* Redraw when 'foldlevel' changed. */
5020 if (old_fdl != curwin->w_p_fdl)
5021 newFoldLevel();
5022 #endif
5025 #ifdef FEAT_GUI
5027 * Vertical scrollbar movement.
5029 static void
5030 nv_ver_scrollbar(cap)
5031 cmdarg_T *cap;
5033 if (cap->oap->op_type != OP_NOP)
5034 clearopbeep(cap->oap);
5036 /* Even if an operator was pending, we still want to scroll */
5037 gui_do_scroll();
5041 * Horizontal scrollbar movement.
5043 static void
5044 nv_hor_scrollbar(cap)
5045 cmdarg_T *cap;
5047 if (cap->oap->op_type != OP_NOP)
5048 clearopbeep(cap->oap);
5050 /* Even if an operator was pending, we still want to scroll */
5051 gui_do_horiz_scroll();
5053 #endif
5055 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
5057 * Click in GUI tab.
5059 static void
5060 nv_tabline(cap)
5061 cmdarg_T *cap;
5063 if (cap->oap->op_type != OP_NOP)
5064 clearopbeep(cap->oap);
5066 /* Even if an operator was pending, we still want to jump tabs. */
5067 goto_tabpage(current_tab);
5071 * Selected item in tab line menu.
5073 static void
5074 nv_tabmenu(cap)
5075 cmdarg_T *cap;
5077 if (cap->oap->op_type != OP_NOP)
5078 clearopbeep(cap->oap);
5080 /* Even if an operator was pending, we still want to jump tabs. */
5081 handle_tabmenu();
5085 * Handle selecting an item of the GUI tab line menu.
5086 * Used in Normal and Insert mode.
5088 void
5089 handle_tabmenu()
5091 switch (current_tabmenu)
5093 case TABLINE_MENU_CLOSE:
5094 if (current_tab == 0)
5095 do_cmdline_cmd((char_u *)"tabclose");
5096 else
5098 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5099 current_tab);
5100 do_cmdline_cmd(IObuff);
5102 break;
5104 case TABLINE_MENU_NEW:
5105 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5106 current_tab > 0 ? current_tab - 1 : 999);
5107 do_cmdline_cmd(IObuff);
5108 break;
5110 case TABLINE_MENU_OPEN:
5111 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5112 current_tab > 0 ? current_tab - 1 : 999);
5113 do_cmdline_cmd(IObuff);
5114 break;
5117 #endif
5120 * "Q" command.
5122 static void
5123 nv_exmode(cap)
5124 cmdarg_T *cap;
5127 * Ignore 'Q' in Visual mode, just give a beep.
5129 #ifdef FEAT_VISUAL
5130 if (VIsual_active)
5131 vim_beep();
5132 else
5133 #endif
5134 if (!checkclearop(cap->oap))
5135 do_exmode(FALSE);
5139 * Handle a ":" command.
5141 static void
5142 nv_colon(cap)
5143 cmdarg_T *cap;
5145 int old_p_im;
5147 #ifdef FEAT_VISUAL
5148 if (VIsual_active)
5149 nv_operator(cap);
5150 else
5151 #endif
5153 if (cap->oap->op_type != OP_NOP)
5155 /* Using ":" as a movement is characterwise exclusive. */
5156 cap->oap->motion_type = MCHAR;
5157 cap->oap->inclusive = FALSE;
5159 else if (cap->count0)
5161 /* translate "count:" into ":.,.+(count - 1)" */
5162 stuffcharReadbuff('.');
5163 if (cap->count0 > 1)
5165 stuffReadbuff((char_u *)",.+");
5166 stuffnumReadbuff((long)cap->count0 - 1L);
5170 /* When typing, don't type below an old message */
5171 if (KeyTyped)
5172 compute_cmdrow();
5174 old_p_im = p_im;
5176 /* get a command line and execute it */
5177 do_cmdline(NULL, getexline, NULL,
5178 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5180 /* If 'insertmode' changed, enter or exit Insert mode */
5181 if (p_im != old_p_im)
5183 if (p_im)
5184 restart_edit = 'i';
5185 else
5186 restart_edit = 0;
5189 /* The start of the operator may have become invalid by the Ex
5190 * command. */
5191 if (cap->oap->op_type != OP_NOP
5192 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5193 || cap->oap->start.col >
5194 STRLEN(ml_get(cap->oap->start.lnum))))
5195 clearopbeep(cap->oap);
5200 * Handle CTRL-G command.
5202 static void
5203 nv_ctrlg(cap)
5204 cmdarg_T *cap;
5206 #ifdef FEAT_VISUAL
5207 if (VIsual_active) /* toggle Selection/Visual mode */
5209 VIsual_select = !VIsual_select;
5210 showmode();
5212 else
5213 #endif
5214 if (!checkclearop(cap->oap))
5215 /* print full name if count given or :cd used */
5216 fileinfo((int)cap->count0, FALSE, TRUE);
5220 * Handle CTRL-H <Backspace> command.
5222 static void
5223 nv_ctrlh(cap)
5224 cmdarg_T *cap;
5226 #ifdef FEAT_VISUAL
5227 if (VIsual_active && VIsual_select)
5229 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5230 v_visop(cap);
5232 else
5233 #endif
5234 nv_left(cap);
5238 * CTRL-L: clear screen and redraw.
5240 static void
5241 nv_clear(cap)
5242 cmdarg_T *cap;
5244 if (!checkclearop(cap->oap))
5246 #if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5248 * Right now, the BeBox doesn't seem to have an easy way to detect
5249 * window resizing, so we cheat and make the user detect it
5250 * manually with CTRL-L instead
5252 ui_get_shellsize();
5253 #endif
5254 #ifdef FEAT_SYN_HL
5255 /* Clear all syntax states to force resyncing. */
5256 syn_stack_free_all(curbuf);
5257 #endif
5258 redraw_later(CLEAR);
5263 * CTRL-O: In Select mode: switch to Visual mode for one command.
5264 * Otherwise: Go to older pcmark.
5266 static void
5267 nv_ctrlo(cap)
5268 cmdarg_T *cap;
5270 #ifdef FEAT_VISUAL
5271 if (VIsual_active && VIsual_select)
5273 VIsual_select = FALSE;
5274 showmode();
5275 restart_VIsual_select = 2; /* restart Select mode later */
5277 else
5278 #endif
5280 cap->count1 = -cap->count1;
5281 nv_pcmark(cap);
5286 * CTRL-^ command, short for ":e #"
5288 static void
5289 nv_hat(cap)
5290 cmdarg_T *cap;
5292 if (!checkclearopq(cap->oap))
5293 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5294 GETF_SETMARK|GETF_ALT, FALSE);
5298 * "Z" commands.
5300 static void
5301 nv_Zet(cap)
5302 cmdarg_T *cap;
5304 if (!checkclearopq(cap->oap))
5306 switch (cap->nchar)
5308 /* "ZZ": equivalent to ":x". */
5309 case 'Z': do_cmdline_cmd((char_u *)"x");
5310 break;
5312 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5313 case 'Q': do_cmdline_cmd((char_u *)"q!");
5314 break;
5316 default: clearopbeep(cap->oap);
5321 #if defined(FEAT_WINDOWS) || defined(PROTO)
5323 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5325 void
5326 do_nv_ident(c1, c2)
5327 int c1;
5328 int c2;
5330 oparg_T oa;
5331 cmdarg_T ca;
5333 clear_oparg(&oa);
5334 vim_memset(&ca, 0, sizeof(ca));
5335 ca.oap = &oa;
5336 ca.cmdchar = c1;
5337 ca.nchar = c2;
5338 nv_ident(&ca);
5340 #endif
5343 * Handle the commands that use the word under the cursor.
5344 * [g] CTRL-] :ta to current identifier
5345 * [g] 'K' run program for current identifier
5346 * [g] '*' / to current identifier or string
5347 * [g] '#' ? to current identifier or string
5348 * g ']' :tselect for current identifier
5350 static void
5351 nv_ident(cap)
5352 cmdarg_T *cap;
5354 char_u *ptr = NULL;
5355 char_u *buf;
5356 char_u *p;
5357 char_u *kp; /* value of 'keywordprg' */
5358 int kp_help; /* 'keywordprg' is ":help" */
5359 int n = 0; /* init for GCC */
5360 int cmdchar;
5361 int g_cmd; /* "g" command */
5362 char_u *aux_ptr;
5363 int isman;
5364 int isman_s;
5366 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5368 cmdchar = cap->nchar;
5369 g_cmd = TRUE;
5371 else
5373 cmdchar = cap->cmdchar;
5374 g_cmd = FALSE;
5377 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5378 cmdchar = '#';
5381 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5383 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5385 #ifdef FEAT_VISUAL
5386 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5387 return;
5388 #endif
5389 if (checkclearopq(cap->oap))
5390 return;
5393 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5394 (cmdchar == '*' || cmdchar == '#')
5395 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5397 clearop(cap->oap);
5398 return;
5401 /* Allocate buffer to put the command in. Inserting backslashes can
5402 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5403 * and some numbers. */
5404 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5405 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5406 || STRCMP(kp, ":help") == 0);
5407 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5408 if (buf == NULL)
5409 return;
5410 buf[0] = NUL;
5412 switch (cmdchar)
5414 case '*':
5415 case '#':
5417 * Put cursor at start of word, makes search skip the word
5418 * under the cursor.
5419 * Call setpcmark() first, so "*``" puts the cursor back where
5420 * it was.
5422 setpcmark();
5423 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5425 if (!g_cmd && vim_iswordp(ptr))
5426 STRCPY(buf, "\\<");
5427 no_smartcase = TRUE; /* don't use 'smartcase' now */
5428 break;
5430 case 'K':
5431 if (kp_help)
5432 STRCPY(buf, "he! ");
5433 else
5435 /* When a count is given, turn it into a range. Is this
5436 * really what we want? */
5437 isman = (STRCMP(kp, "man") == 0);
5438 isman_s = (STRCMP(kp, "man -s") == 0);
5439 if (cap->count0 != 0 && !(isman || isman_s))
5440 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5442 STRCAT(buf, "! ");
5443 if (cap->count0 == 0 && isman_s)
5444 STRCAT(buf, "man");
5445 else
5446 STRCAT(buf, kp);
5447 STRCAT(buf, " ");
5448 if (cap->count0 != 0 && (isman || isman_s))
5450 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5451 STRCAT(buf, " ");
5454 break;
5456 case ']':
5457 #ifdef FEAT_CSCOPE
5458 if (p_cst)
5459 STRCPY(buf, "cstag ");
5460 else
5461 #endif
5462 STRCPY(buf, "ts ");
5463 break;
5465 default:
5466 if (curbuf->b_help)
5467 STRCPY(buf, "he! ");
5468 else if (g_cmd)
5469 STRCPY(buf, "tj ");
5470 else
5471 sprintf((char *)buf, "%ldta ", cap->count0);
5475 * Now grab the chars in the identifier
5477 if (cmdchar == '*')
5478 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5479 else if (cmdchar == '#')
5480 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
5481 else if (cmdchar == 'K' && !kp_help)
5482 aux_ptr = (char_u *)" \t\\\"|!";
5483 else
5484 /* Don't escape spaces and Tabs in a tag with a backslash */
5485 aux_ptr = (char_u *)"\\|\"";
5487 p = buf + STRLEN(buf);
5488 while (n-- > 0)
5490 /* put a backslash before \ and some others */
5491 if (vim_strchr(aux_ptr, *ptr) != NULL)
5492 *p++ = '\\';
5493 #ifdef FEAT_MBYTE
5494 /* When current byte is a part of multibyte character, copy all bytes
5495 * of that character. */
5496 if (has_mbyte)
5498 int i;
5499 int len = (*mb_ptr2len)(ptr) - 1;
5501 for (i = 0; i < len && n >= 1; ++i, --n)
5502 *p++ = *ptr++;
5504 #endif
5505 *p++ = *ptr++;
5507 *p = NUL;
5510 * Execute the command.
5512 if (cmdchar == '*' || cmdchar == '#')
5514 if (!g_cmd && (
5515 #ifdef FEAT_MBYTE
5516 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5517 #endif
5518 vim_iswordc(ptr[-1])))
5519 STRCAT(buf, "\\>");
5520 #ifdef FEAT_CMDHIST
5521 /* put pattern in search history */
5522 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5523 #endif
5524 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5526 else
5527 do_cmdline_cmd(buf);
5529 vim_free(buf);
5532 #if defined(FEAT_VISUAL) || defined(PROTO)
5534 * Get visually selected text, within one line only.
5535 * Returns FAIL if more than one line selected.
5538 get_visual_text(cap, pp, lenp)
5539 cmdarg_T *cap;
5540 char_u **pp; /* return: start of selected text */
5541 int *lenp; /* return: length of selected text */
5543 if (VIsual_mode != 'V')
5544 unadjust_for_sel();
5545 if (VIsual.lnum != curwin->w_cursor.lnum)
5547 if (cap != NULL)
5548 clearopbeep(cap->oap);
5549 return FAIL;
5551 if (VIsual_mode == 'V')
5553 *pp = ml_get_curline();
5554 *lenp = (int)STRLEN(*pp);
5556 else
5558 if (lt(curwin->w_cursor, VIsual))
5560 *pp = ml_get_pos(&curwin->w_cursor);
5561 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5563 else
5565 *pp = ml_get_pos(&VIsual);
5566 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5568 #ifdef FEAT_MBYTE
5569 if (has_mbyte)
5570 /* Correct the length to include the whole last character. */
5571 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
5572 #endif
5574 reset_VIsual_and_resel();
5575 return OK;
5577 #endif
5580 * CTRL-T: backwards in tag stack
5582 static void
5583 nv_tagpop(cap)
5584 cmdarg_T *cap;
5586 if (!checkclearopq(cap->oap))
5587 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5591 * Handle scrolling command 'H', 'L' and 'M'.
5593 static void
5594 nv_scroll(cap)
5595 cmdarg_T *cap;
5597 int used = 0;
5598 long n;
5599 #ifdef FEAT_FOLDING
5600 linenr_T lnum;
5601 #endif
5602 int half;
5604 cap->oap->motion_type = MLINE;
5605 setpcmark();
5607 if (cap->cmdchar == 'L')
5609 validate_botline(); /* make sure curwin->w_botline is valid */
5610 curwin->w_cursor.lnum = curwin->w_botline - 1;
5611 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5612 curwin->w_cursor.lnum = 1;
5613 else
5615 #ifdef FEAT_FOLDING
5616 if (hasAnyFolding(curwin))
5618 /* Count a fold for one screen line. */
5619 for (n = cap->count1 - 1; n > 0
5620 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5622 (void)hasFolding(curwin->w_cursor.lnum,
5623 &curwin->w_cursor.lnum, NULL);
5624 --curwin->w_cursor.lnum;
5627 else
5628 #endif
5629 curwin->w_cursor.lnum -= cap->count1 - 1;
5632 else
5634 if (cap->cmdchar == 'M')
5636 #ifdef FEAT_DIFF
5637 /* Don't count filler lines above the window. */
5638 used -= diff_check_fill(curwin, curwin->w_topline)
5639 - curwin->w_topfill;
5640 #endif
5641 validate_botline(); /* make sure w_empty_rows is valid */
5642 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5643 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5645 #ifdef FEAT_DIFF
5646 /* Count half he number of filler lines to be "below this
5647 * line" and half to be "above the next line". */
5648 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5649 + n) / 2 >= half)
5651 --n;
5652 break;
5654 #endif
5655 used += plines(curwin->w_topline + n);
5656 if (used >= half)
5657 break;
5658 #ifdef FEAT_FOLDING
5659 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5660 n = lnum - curwin->w_topline;
5661 #endif
5663 if (n > 0 && used > curwin->w_height)
5664 --n;
5666 else /* (cap->cmdchar == 'H') */
5668 n = cap->count1 - 1;
5669 #ifdef FEAT_FOLDING
5670 if (hasAnyFolding(curwin))
5672 /* Count a fold for one screen line. */
5673 lnum = curwin->w_topline;
5674 while (n-- > 0 && lnum < curwin->w_botline - 1)
5676 hasFolding(lnum, NULL, &lnum);
5677 ++lnum;
5679 n = lnum - curwin->w_topline;
5681 #endif
5683 curwin->w_cursor.lnum = curwin->w_topline + n;
5684 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5685 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5688 cursor_correct(); /* correct for 'so' */
5689 beginline(BL_SOL | BL_FIX);
5693 * Cursor right commands.
5695 static void
5696 nv_right(cap)
5697 cmdarg_T *cap;
5699 long n;
5700 #ifdef FEAT_VISUAL
5701 int PAST_LINE;
5702 #else
5703 # define PAST_LINE 0
5704 #endif
5706 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5708 /* <C-Right> and <S-Right> move a word or WORD right */
5709 if (mod_mask & MOD_MASK_CTRL)
5710 cap->arg = TRUE;
5711 nv_wordcmd(cap);
5712 return;
5715 cap->oap->motion_type = MCHAR;
5716 cap->oap->inclusive = FALSE;
5717 #ifdef FEAT_VISUAL
5718 PAST_LINE = (VIsual_active && *p_sel != 'o');
5720 # ifdef FEAT_VIRTUALEDIT
5722 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
5723 * (theoretically) infinitely long.
5725 if (virtual_active())
5726 PAST_LINE = 0;
5727 # endif
5728 #endif
5730 for (n = cap->count1; n > 0; --n)
5732 if ((!PAST_LINE && oneright() == FAIL)
5733 || (PAST_LINE && *ml_get_cursor() == NUL))
5736 * <Space> wraps to next line if 'whichwrap' has 's'.
5737 * 'l' wraps to next line if 'whichwrap' has 'l'.
5738 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
5740 if ( ((cap->cmdchar == ' '
5741 && vim_strchr(p_ww, 's') != NULL)
5742 || (cap->cmdchar == 'l'
5743 && vim_strchr(p_ww, 'l') != NULL)
5744 || (cap->cmdchar == K_RIGHT
5745 && vim_strchr(p_ww, '>') != NULL))
5746 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5748 /* When deleting we also count the NL as a character.
5749 * Set cap->oap->inclusive when last char in the line is
5750 * included, move to next line after that */
5751 if ( cap->oap->op_type != OP_NOP
5752 && !cap->oap->inclusive
5753 && !lineempty(curwin->w_cursor.lnum))
5754 cap->oap->inclusive = TRUE;
5755 else
5757 ++curwin->w_cursor.lnum;
5758 curwin->w_cursor.col = 0;
5759 #ifdef FEAT_VIRTUALEDIT
5760 curwin->w_cursor.coladd = 0;
5761 #endif
5762 curwin->w_set_curswant = TRUE;
5763 cap->oap->inclusive = FALSE;
5765 continue;
5767 if (cap->oap->op_type == OP_NOP)
5769 /* Only beep and flush if not moved at all */
5770 if (n == cap->count1)
5771 beep_flush();
5773 else
5775 if (!lineempty(curwin->w_cursor.lnum))
5776 cap->oap->inclusive = TRUE;
5778 break;
5780 #ifdef FEAT_VISUAL
5781 else if (PAST_LINE)
5783 curwin->w_set_curswant = TRUE;
5784 # ifdef FEAT_VIRTUALEDIT
5785 if (virtual_active())
5786 oneright();
5787 else
5788 # endif
5790 # ifdef FEAT_MBYTE
5791 if (has_mbyte)
5792 curwin->w_cursor.col +=
5793 (*mb_ptr2len)(ml_get_cursor());
5794 else
5795 # endif
5796 ++curwin->w_cursor.col;
5799 #endif
5801 #ifdef FEAT_FOLDING
5802 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5803 && cap->oap->op_type == OP_NOP)
5804 foldOpenCursor();
5805 #endif
5809 * Cursor left commands.
5811 * Returns TRUE when operator end should not be adjusted.
5813 static void
5814 nv_left(cap)
5815 cmdarg_T *cap;
5817 long n;
5819 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5821 /* <C-Left> and <S-Left> move a word or WORD left */
5822 if (mod_mask & MOD_MASK_CTRL)
5823 cap->arg = 1;
5824 nv_bck_word(cap);
5825 return;
5828 cap->oap->motion_type = MCHAR;
5829 cap->oap->inclusive = FALSE;
5830 for (n = cap->count1; n > 0; --n)
5832 if (oneleft() == FAIL)
5834 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
5835 * 'h' wraps to previous line if 'whichwrap' has 'h'.
5836 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
5838 if ( (((cap->cmdchar == K_BS
5839 || cap->cmdchar == Ctrl_H)
5840 && vim_strchr(p_ww, 'b') != NULL)
5841 || (cap->cmdchar == 'h'
5842 && vim_strchr(p_ww, 'h') != NULL)
5843 || (cap->cmdchar == K_LEFT
5844 && vim_strchr(p_ww, '<') != NULL))
5845 && curwin->w_cursor.lnum > 1)
5847 --(curwin->w_cursor.lnum);
5848 coladvance((colnr_T)MAXCOL);
5849 curwin->w_set_curswant = TRUE;
5851 /* When the NL before the first char has to be deleted we
5852 * put the cursor on the NUL after the previous line.
5853 * This is a very special case, be careful!
5854 * don't adjust op_end now, otherwise it won't work */
5855 if ( (cap->oap->op_type == OP_DELETE
5856 || cap->oap->op_type == OP_CHANGE)
5857 && !lineempty(curwin->w_cursor.lnum))
5859 ++curwin->w_cursor.col;
5860 cap->retval |= CA_NO_ADJ_OP_END;
5862 continue;
5864 /* Only beep and flush if not moved at all */
5865 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
5866 beep_flush();
5867 break;
5870 #ifdef FEAT_FOLDING
5871 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5872 && cap->oap->op_type == OP_NOP)
5873 foldOpenCursor();
5874 #endif
5878 * Cursor up commands.
5879 * cap->arg is TRUE for "-": Move cursor to first non-blank.
5881 static void
5882 nv_up(cap)
5883 cmdarg_T *cap;
5885 if (mod_mask & MOD_MASK_SHIFT)
5887 /* <S-Up> is page up */
5888 cap->arg = BACKWARD;
5889 nv_page(cap);
5891 else
5893 cap->oap->motion_type = MLINE;
5894 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5895 clearopbeep(cap->oap);
5896 else if (cap->arg)
5897 beginline(BL_WHITE | BL_FIX);
5902 * Cursor down commands.
5903 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
5905 static void
5906 nv_down(cap)
5907 cmdarg_T *cap;
5909 if (mod_mask & MOD_MASK_SHIFT)
5911 /* <S-Down> is page down */
5912 cap->arg = FORWARD;
5913 nv_page(cap);
5915 else
5916 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5917 /* In a quickfix window a <CR> jumps to the error under the cursor. */
5918 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
5919 if (curwin->w_llist_ref == NULL)
5920 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
5921 else
5922 do_cmdline_cmd((char_u *)".ll"); /* location list window */
5923 else
5924 #endif
5926 #ifdef FEAT_CMDWIN
5927 /* In the cmdline window a <CR> executes the command. */
5928 if (cmdwin_type != 0 && cap->cmdchar == CAR)
5929 cmdwin_result = CAR;
5930 else
5931 #endif
5933 cap->oap->motion_type = MLINE;
5934 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5935 clearopbeep(cap->oap);
5936 else if (cap->arg)
5937 beginline(BL_WHITE | BL_FIX);
5942 #ifdef FEAT_SEARCHPATH
5944 * Grab the file name under the cursor and edit it.
5946 static void
5947 nv_gotofile(cap)
5948 cmdarg_T *cap;
5950 char_u *ptr;
5951 linenr_T lnum = -1;
5953 if (text_locked())
5955 clearopbeep(cap->oap);
5956 text_locked_msg();
5957 return;
5959 #ifdef FEAT_AUTOCMD
5960 if (curbuf_locked())
5962 clearop(cap->oap);
5963 return;
5965 #endif
5967 ptr = grab_file_name(cap->count1, &lnum);
5969 if (ptr != NULL)
5971 /* do autowrite if necessary */
5972 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
5973 autowrite(curbuf, FALSE);
5974 setpcmark();
5975 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
5976 P_HID(curbuf) ? ECMD_HIDE : 0);
5977 if (cap->nchar == 'F' && lnum >= 0)
5979 curwin->w_cursor.lnum = lnum;
5980 check_cursor_lnum();
5981 beginline(BL_SOL | BL_FIX);
5983 vim_free(ptr);
5985 else
5986 clearop(cap->oap);
5988 #endif
5991 * <End> command: to end of current line or last line.
5993 static void
5994 nv_end(cap)
5995 cmdarg_T *cap;
5997 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
5999 cap->arg = TRUE;
6000 nv_goto(cap);
6001 cap->count1 = 1; /* to end of current line */
6003 nv_dollar(cap);
6007 * Handle the "$" command.
6009 static void
6010 nv_dollar(cap)
6011 cmdarg_T *cap;
6013 cap->oap->motion_type = MCHAR;
6014 cap->oap->inclusive = TRUE;
6015 #ifdef FEAT_VIRTUALEDIT
6016 /* In virtual mode when off the edge of a line and an operator
6017 * is pending (whew!) keep the cursor where it is.
6018 * Otherwise, send it to the end of the line. */
6019 if (!virtual_active() || gchar_cursor() != NUL
6020 || cap->oap->op_type == OP_NOP)
6021 #endif
6022 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6023 if (cursor_down((long)(cap->count1 - 1),
6024 cap->oap->op_type == OP_NOP) == FAIL)
6025 clearopbeep(cap->oap);
6026 #ifdef FEAT_FOLDING
6027 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6028 foldOpenCursor();
6029 #endif
6033 * Implementation of '?' and '/' commands.
6034 * If cap->arg is TRUE don't set PC mark.
6036 static void
6037 nv_search(cap)
6038 cmdarg_T *cap;
6040 oparg_T *oap = cap->oap;
6042 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6044 /* Translate "g??" to "g?g?" */
6045 cap->cmdchar = 'g';
6046 cap->nchar = '?';
6047 nv_operator(cap);
6048 return;
6051 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6053 if (cap->searchbuf == NULL)
6055 clearop(oap);
6056 return;
6059 normal_search(cap, cap->cmdchar, cap->searchbuf,
6060 (cap->arg ? 0 : SEARCH_MARK));
6064 * Handle "N" and "n" commands.
6065 * cap->arg is SEARCH_REV for "N", 0 for "n".
6067 static void
6068 nv_next(cap)
6069 cmdarg_T *cap;
6071 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6075 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6076 * Uses only cap->count1 and cap->oap from "cap".
6078 static void
6079 normal_search(cap, dir, pat, opt)
6080 cmdarg_T *cap;
6081 int dir;
6082 char_u *pat;
6083 int opt; /* extra flags for do_search() */
6085 int i;
6087 cap->oap->motion_type = MCHAR;
6088 cap->oap->inclusive = FALSE;
6089 cap->oap->use_reg_one = TRUE;
6090 curwin->w_set_curswant = TRUE;
6092 i = do_search(cap->oap, dir, pat, cap->count1,
6093 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG);
6094 if (i == 0)
6095 clearop(cap->oap);
6096 else
6098 if (i == 2)
6099 cap->oap->motion_type = MLINE;
6100 #ifdef FEAT_VIRTUALEDIT
6101 curwin->w_cursor.coladd = 0;
6102 #endif
6103 #ifdef FEAT_FOLDING
6104 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6105 foldOpenCursor();
6106 #endif
6109 /* "/$" will put the cursor after the end of the line, may need to
6110 * correct that here */
6111 check_cursor();
6115 * Character search commands.
6116 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6117 * ',' and FALSE for ';'.
6118 * cap->nchar is NUL for ',' and ';' (repeat the search)
6120 static void
6121 nv_csearch(cap)
6122 cmdarg_T *cap;
6124 int t_cmd;
6126 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6127 t_cmd = TRUE;
6128 else
6129 t_cmd = FALSE;
6131 cap->oap->motion_type = MCHAR;
6132 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6133 clearopbeep(cap->oap);
6134 else
6136 curwin->w_set_curswant = TRUE;
6137 #ifdef FEAT_VIRTUALEDIT
6138 /* Include a Tab for "tx" and for "dfx". */
6139 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6140 && (t_cmd || cap->oap->op_type != OP_NOP))
6142 colnr_T scol, ecol;
6144 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6145 curwin->w_cursor.coladd = ecol - scol;
6147 else
6148 curwin->w_cursor.coladd = 0;
6149 #endif
6150 #ifdef FEAT_VISUAL
6151 adjust_for_sel(cap);
6152 #endif
6153 #ifdef FEAT_FOLDING
6154 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6155 foldOpenCursor();
6156 #endif
6161 * "[" and "]" commands.
6162 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6164 static void
6165 nv_brackets(cap)
6166 cmdarg_T *cap;
6168 pos_T new_pos;
6169 pos_T prev_pos;
6170 pos_T *pos = NULL; /* init for GCC */
6171 pos_T old_pos; /* cursor position before command */
6172 int flag;
6173 long n;
6174 int findc;
6175 int c;
6177 cap->oap->motion_type = MCHAR;
6178 cap->oap->inclusive = FALSE;
6179 old_pos = curwin->w_cursor;
6180 #ifdef FEAT_VIRTUALEDIT
6181 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6182 #endif
6184 #ifdef FEAT_SEARCHPATH
6186 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6188 if (cap->nchar == 'f')
6189 nv_gotofile(cap);
6190 else
6191 #endif
6193 #ifdef FEAT_FIND_ID
6195 * Find the occurrence(s) of the identifier or define under cursor
6196 * in current and included files or jump to the first occurrence.
6198 * search list jump
6199 * fwd bwd fwd bwd fwd bwd
6200 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6201 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6203 if (vim_strchr((char_u *)
6204 #ifdef EBCDIC
6205 "iI\005dD\067",
6206 #else
6207 "iI\011dD\004",
6208 #endif
6209 cap->nchar) != NULL)
6211 char_u *ptr;
6212 int len;
6214 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6215 clearop(cap->oap);
6216 else
6218 find_pattern_in_path(ptr, 0, len, TRUE,
6219 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6220 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6221 cap->count1,
6222 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6223 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6224 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6225 (linenr_T)MAXLNUM);
6226 curwin->w_set_curswant = TRUE;
6229 else
6230 #endif
6233 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6234 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6235 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6236 * "[m" or "]m" search for prev/next start of (Java) method.
6237 * "[M" or "]M" search for prev/next end of (Java) method.
6239 if ( (cap->cmdchar == '['
6240 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6241 || (cap->cmdchar == ']'
6242 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6244 if (cap->nchar == '*')
6245 cap->nchar = '/';
6246 new_pos.lnum = 0;
6247 prev_pos.lnum = 0;
6248 if (cap->nchar == 'm' || cap->nchar == 'M')
6250 if (cap->cmdchar == '[')
6251 findc = '{';
6252 else
6253 findc = '}';
6254 n = 9999;
6256 else
6258 findc = cap->nchar;
6259 n = cap->count1;
6261 for ( ; n > 0; --n)
6263 if ((pos = findmatchlimit(cap->oap, findc,
6264 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6266 if (new_pos.lnum == 0) /* nothing found */
6268 if (cap->nchar != 'm' && cap->nchar != 'M')
6269 clearopbeep(cap->oap);
6271 else
6272 pos = &new_pos; /* use last one found */
6273 break;
6275 prev_pos = new_pos;
6276 curwin->w_cursor = *pos;
6277 new_pos = *pos;
6279 curwin->w_cursor = old_pos;
6282 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6283 * brought us to the match for "[m" and "]M" when inside a method.
6284 * Try finding the '{' or '}' we want to be at.
6285 * Also repeat for the given count.
6287 if (cap->nchar == 'm' || cap->nchar == 'M')
6289 /* norm is TRUE for "]M" and "[m" */
6290 int norm = ((findc == '{') == (cap->nchar == 'm'));
6292 n = cap->count1;
6293 /* found a match: we were inside a method */
6294 if (prev_pos.lnum != 0)
6296 pos = &prev_pos;
6297 curwin->w_cursor = prev_pos;
6298 if (norm)
6299 --n;
6301 else
6302 pos = NULL;
6303 while (n > 0)
6305 for (;;)
6307 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6309 /* if not found anything, that's an error */
6310 if (pos == NULL)
6311 clearopbeep(cap->oap);
6312 n = 0;
6313 break;
6315 c = gchar_cursor();
6316 if (c == '{' || c == '}')
6318 /* Must have found end/start of class: use it.
6319 * Or found the place to be at. */
6320 if ((c == findc && norm) || (n == 1 && !norm))
6322 new_pos = curwin->w_cursor;
6323 pos = &new_pos;
6324 n = 0;
6326 /* if no match found at all, we started outside of the
6327 * class and we're inside now. Just go on. */
6328 else if (new_pos.lnum == 0)
6330 new_pos = curwin->w_cursor;
6331 pos = &new_pos;
6333 /* found start/end of other method: go to match */
6334 else if ((pos = findmatchlimit(cap->oap, findc,
6335 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6336 0)) == NULL)
6337 n = 0;
6338 else
6339 curwin->w_cursor = *pos;
6340 break;
6343 --n;
6345 curwin->w_cursor = old_pos;
6346 if (pos == NULL && new_pos.lnum != 0)
6347 clearopbeep(cap->oap);
6349 if (pos != NULL)
6351 setpcmark();
6352 curwin->w_cursor = *pos;
6353 curwin->w_set_curswant = TRUE;
6354 #ifdef FEAT_FOLDING
6355 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6356 && cap->oap->op_type == OP_NOP)
6357 foldOpenCursor();
6358 #endif
6363 * "[[", "[]", "]]" and "][": move to start or end of function
6365 else if (cap->nchar == '[' || cap->nchar == ']')
6367 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6368 flag = '{';
6369 else
6370 flag = '}'; /* "][" or "[]" */
6372 curwin->w_set_curswant = TRUE;
6374 * Imitate strange Vi behaviour: When using "]]" with an operator
6375 * we also stop at '}'.
6377 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
6378 (cap->oap->op_type != OP_NOP
6379 && cap->arg == FORWARD && flag == '{')))
6380 clearopbeep(cap->oap);
6381 else
6383 if (cap->oap->op_type == OP_NOP)
6384 beginline(BL_WHITE | BL_FIX);
6385 #ifdef FEAT_FOLDING
6386 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6387 foldOpenCursor();
6388 #endif
6393 * "[p", "[P", "]P" and "]p": put with indent adjustment
6395 else if (cap->nchar == 'p' || cap->nchar == 'P')
6397 if (!checkclearop(cap->oap))
6399 prep_redo_cmd(cap);
6400 do_put(cap->oap->regname,
6401 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6402 cap->count1, PUT_FIXINDENT);
6407 * "['", "[`", "]'" and "]`": jump to next mark
6409 else if (cap->nchar == '\'' || cap->nchar == '`')
6411 pos = &curwin->w_cursor;
6412 for (n = cap->count1; n > 0; --n)
6414 prev_pos = *pos;
6415 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6416 cap->nchar == '\'');
6417 if (pos == NULL)
6418 break;
6420 if (pos == NULL)
6421 pos = &prev_pos;
6422 nv_cursormark(cap, cap->nchar == '\'', pos);
6425 #ifdef FEAT_MOUSE
6427 * [ or ] followed by a middle mouse click: put selected text with
6428 * indent adjustment. Any other button just does as usual.
6430 else if (cap->nchar >= K_LEFTMOUSE && cap->nchar <= K_RIGHTRELEASE)
6432 (void)do_mouse(cap->oap, cap->nchar,
6433 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6434 cap->count1, PUT_FIXINDENT);
6436 #endif /* FEAT_MOUSE */
6438 #ifdef FEAT_FOLDING
6440 * "[z" and "]z": move to start or end of open fold.
6442 else if (cap->nchar == 'z')
6444 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6445 cap->count1) == FAIL)
6446 clearopbeep(cap->oap);
6448 #endif
6450 #ifdef FEAT_DIFF
6452 * "[c" and "]c": move to next or previous diff-change.
6454 else if (cap->nchar == 'c')
6456 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6457 cap->count1) == FAIL)
6458 clearopbeep(cap->oap);
6460 #endif
6462 #ifdef FEAT_SPELL
6464 * "[s", "[S", "]s" and "]S": move to next spell error.
6466 else if (cap->nchar == 's' || cap->nchar == 'S')
6468 setpcmark();
6469 for (n = 0; n < cap->count1; ++n)
6470 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6471 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
6473 clearopbeep(cap->oap);
6474 break;
6476 # ifdef FEAT_FOLDING
6477 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6478 foldOpenCursor();
6479 # endif
6481 #endif
6483 /* Not a valid cap->nchar. */
6484 else
6485 clearopbeep(cap->oap);
6489 * Handle Normal mode "%" command.
6491 static void
6492 nv_percent(cap)
6493 cmdarg_T *cap;
6495 pos_T *pos;
6496 #ifdef FEAT_FOLDING
6497 linenr_T lnum = curwin->w_cursor.lnum;
6498 #endif
6500 cap->oap->inclusive = TRUE;
6501 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6503 if (cap->count0 > 100)
6504 clearopbeep(cap->oap);
6505 else
6507 cap->oap->motion_type = MLINE;
6508 setpcmark();
6509 /* Round up, so CTRL-G will give same value. Watch out for a
6510 * large line count, the line number must not go negative! */
6511 if (curbuf->b_ml.ml_line_count > 1000000)
6512 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6513 / 100L * cap->count0;
6514 else
6515 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6516 cap->count0 + 99L) / 100L;
6517 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6518 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6519 beginline(BL_SOL | BL_FIX);
6522 else /* "%" : go to matching paren */
6524 cap->oap->motion_type = MCHAR;
6525 cap->oap->use_reg_one = TRUE;
6526 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6527 clearopbeep(cap->oap);
6528 else
6530 setpcmark();
6531 curwin->w_cursor = *pos;
6532 curwin->w_set_curswant = TRUE;
6533 #ifdef FEAT_VIRTUALEDIT
6534 curwin->w_cursor.coladd = 0;
6535 #endif
6536 #ifdef FEAT_VISUAL
6537 adjust_for_sel(cap);
6538 #endif
6541 #ifdef FEAT_FOLDING
6542 if (cap->oap->op_type == OP_NOP
6543 && lnum != curwin->w_cursor.lnum
6544 && (fdo_flags & FDO_PERCENT)
6545 && KeyTyped)
6546 foldOpenCursor();
6547 #endif
6551 * Handle "(" and ")" commands.
6552 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6554 static void
6555 nv_brace(cap)
6556 cmdarg_T *cap;
6558 cap->oap->motion_type = MCHAR;
6559 cap->oap->use_reg_one = TRUE;
6560 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6561 cap->oap->inclusive = FALSE;
6562 curwin->w_set_curswant = TRUE;
6564 if (findsent(cap->arg, cap->count1) == FAIL)
6565 clearopbeep(cap->oap);
6566 else
6568 #ifdef FEAT_VIRTUALEDIT
6569 curwin->w_cursor.coladd = 0;
6570 #endif
6571 #ifdef FEAT_FOLDING
6572 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6573 foldOpenCursor();
6574 #endif
6579 * "m" command: Mark a position.
6581 static void
6582 nv_mark(cap)
6583 cmdarg_T *cap;
6585 if (!checkclearop(cap->oap))
6587 if (setmark(cap->nchar) == FAIL)
6588 clearopbeep(cap->oap);
6593 * "{" and "}" commands.
6594 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6596 static void
6597 nv_findpar(cap)
6598 cmdarg_T *cap;
6600 cap->oap->motion_type = MCHAR;
6601 cap->oap->inclusive = FALSE;
6602 cap->oap->use_reg_one = TRUE;
6603 curwin->w_set_curswant = TRUE;
6604 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
6605 clearopbeep(cap->oap);
6606 else
6608 #ifdef FEAT_VIRTUALEDIT
6609 curwin->w_cursor.coladd = 0;
6610 #endif
6611 #ifdef FEAT_FOLDING
6612 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6613 foldOpenCursor();
6614 #endif
6619 * "u" command: Undo or make lower case.
6621 static void
6622 nv_undo(cap)
6623 cmdarg_T *cap;
6625 if (cap->oap->op_type == OP_LOWER
6626 #ifdef FEAT_VISUAL
6627 || VIsual_active
6628 #endif
6631 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6632 cap->cmdchar = 'g';
6633 cap->nchar = 'u';
6634 nv_operator(cap);
6636 else
6637 nv_kundo(cap);
6641 * <Undo> command.
6643 static void
6644 nv_kundo(cap)
6645 cmdarg_T *cap;
6647 if (!checkclearopq(cap->oap))
6649 u_undo((int)cap->count1);
6650 curwin->w_set_curswant = TRUE;
6655 * Handle the "r" command.
6657 static void
6658 nv_replace(cap)
6659 cmdarg_T *cap;
6661 char_u *ptr;
6662 int had_ctrl_v;
6663 long n;
6665 if (checkclearop(cap->oap))
6666 return;
6668 /* get another character */
6669 if (cap->nchar == Ctrl_V)
6671 had_ctrl_v = Ctrl_V;
6672 cap->nchar = get_literal();
6673 /* Don't redo a multibyte character with CTRL-V. */
6674 if (cap->nchar > DEL)
6675 had_ctrl_v = NUL;
6677 else
6678 had_ctrl_v = NUL;
6680 /* Abort if the character is a special key. */
6681 if (IS_SPECIAL(cap->nchar))
6683 clearopbeep(cap->oap);
6684 return;
6687 #ifdef FEAT_VISUAL
6688 /* Visual mode "r" */
6689 if (VIsual_active)
6691 nv_operator(cap);
6692 return;
6694 #endif
6696 #ifdef FEAT_VIRTUALEDIT
6697 /* Break tabs, etc. */
6698 if (virtual_active())
6700 if (u_save_cursor() == FAIL)
6701 return;
6702 if (gchar_cursor() == NUL)
6704 /* Add extra space and put the cursor on the first one. */
6705 coladvance_force((colnr_T)(getviscol() + cap->count1));
6706 curwin->w_cursor.col -= cap->count1;
6708 else if (gchar_cursor() == TAB)
6709 coladvance_force(getviscol());
6711 #endif
6713 /* Abort if not enough characters to replace. */
6714 ptr = ml_get_cursor();
6715 if (STRLEN(ptr) < (unsigned)cap->count1
6716 #ifdef FEAT_MBYTE
6717 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6718 #endif
6721 clearopbeep(cap->oap);
6722 return;
6726 * Replacing with a TAB is done by edit() when it is complicated because
6727 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6728 * Other characters are done below to avoid problems with things like
6729 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6731 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6733 stuffnumReadbuff(cap->count1);
6734 stuffcharReadbuff('R');
6735 stuffcharReadbuff('\t');
6736 stuffcharReadbuff(ESC);
6737 return;
6740 /* save line for undo */
6741 if (u_save_cursor() == FAIL)
6742 return;
6744 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6747 * Replace character(s) by a single newline.
6748 * Strange vi behaviour: Only one newline is inserted.
6749 * Delete the characters here.
6750 * Insert the newline with an insert command, takes care of
6751 * autoindent. The insert command depends on being on the last
6752 * character of a line or not.
6754 #ifdef FEAT_MBYTE
6755 (void)del_chars(cap->count1, FALSE); /* delete the characters */
6756 #else
6757 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
6758 #endif
6759 stuffcharReadbuff('\r');
6760 stuffcharReadbuff(ESC);
6762 /* Give 'r' to edit(), to get the redo command right. */
6763 invoke_edit(cap, TRUE, 'r', FALSE);
6765 else
6767 prep_redo(cap->oap->regname, cap->count1,
6768 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
6770 curbuf->b_op_start = curwin->w_cursor;
6771 #ifdef FEAT_MBYTE
6772 if (has_mbyte)
6774 int old_State = State;
6776 if (cap->ncharC1 != 0)
6777 AppendCharToRedobuff(cap->ncharC1);
6778 if (cap->ncharC2 != 0)
6779 AppendCharToRedobuff(cap->ncharC2);
6781 /* This is slow, but it handles replacing a single-byte with a
6782 * multi-byte and the other way around. Also handles adding
6783 * composing characters for utf-8. */
6784 for (n = cap->count1; n > 0; --n)
6786 State = REPLACE;
6787 ins_char(cap->nchar);
6788 State = old_State;
6789 if (cap->ncharC1 != 0)
6790 ins_char(cap->ncharC1);
6791 if (cap->ncharC2 != 0)
6792 ins_char(cap->ncharC2);
6795 else
6796 #endif
6799 * Replace the characters within one line.
6801 for (n = cap->count1; n > 0; --n)
6804 * Get ptr again, because u_save and/or showmatch() will have
6805 * released the line. At the same time we let know that the
6806 * line will be changed.
6808 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
6809 ptr[curwin->w_cursor.col] = cap->nchar;
6810 if (p_sm && msg_silent == 0)
6811 showmatch(cap->nchar);
6812 ++curwin->w_cursor.col;
6814 #ifdef FEAT_NETBEANS_INTG
6815 if (usingNetbeans)
6817 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
6819 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
6820 (long)cap->count1);
6821 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
6822 &ptr[start], (int)cap->count1);
6824 #endif
6826 /* mark the buffer as changed and prepare for displaying */
6827 changed_bytes(curwin->w_cursor.lnum,
6828 (colnr_T)(curwin->w_cursor.col - cap->count1));
6830 --curwin->w_cursor.col; /* cursor on the last replaced char */
6831 #ifdef FEAT_MBYTE
6832 /* if the character on the left of the current cursor is a multi-byte
6833 * character, move two characters left */
6834 if (has_mbyte)
6835 mb_adjust_cursor();
6836 #endif
6837 curbuf->b_op_end = curwin->w_cursor;
6838 curwin->w_set_curswant = TRUE;
6839 set_last_insert(cap->nchar);
6843 #ifdef FEAT_VISUAL
6845 * 'o': Exchange start and end of Visual area.
6846 * 'O': same, but in block mode exchange left and right corners.
6848 static void
6849 v_swap_corners(cmdchar)
6850 int cmdchar;
6852 pos_T old_cursor;
6853 colnr_T left, right;
6855 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
6857 old_cursor = curwin->w_cursor;
6858 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
6859 curwin->w_cursor.lnum = VIsual.lnum;
6860 coladvance(left);
6861 VIsual = curwin->w_cursor;
6863 curwin->w_cursor.lnum = old_cursor.lnum;
6864 curwin->w_curswant = right;
6865 /* 'selection "exclusive" and cursor at right-bottom corner: move it
6866 * right one column */
6867 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
6868 ++curwin->w_curswant;
6869 coladvance(curwin->w_curswant);
6870 if (curwin->w_cursor.col == old_cursor.col
6871 #ifdef FEAT_VIRTUALEDIT
6872 && (!virtual_active()
6873 || curwin->w_cursor.coladd == old_cursor.coladd)
6874 #endif
6877 curwin->w_cursor.lnum = VIsual.lnum;
6878 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
6879 ++right;
6880 coladvance(right);
6881 VIsual = curwin->w_cursor;
6883 curwin->w_cursor.lnum = old_cursor.lnum;
6884 coladvance(left);
6885 curwin->w_curswant = left;
6888 else
6890 old_cursor = curwin->w_cursor;
6891 curwin->w_cursor = VIsual;
6892 VIsual = old_cursor;
6893 curwin->w_set_curswant = TRUE;
6896 #endif /* FEAT_VISUAL */
6899 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
6901 static void
6902 nv_Replace(cap)
6903 cmdarg_T *cap;
6905 #ifdef FEAT_VISUAL
6906 if (VIsual_active) /* "R" is replace lines */
6908 cap->cmdchar = 'c';
6909 cap->nchar = NUL;
6910 VIsual_mode = 'V';
6911 nv_operator(cap);
6913 else
6914 #endif
6915 if (!checkclearopq(cap->oap))
6917 if (!curbuf->b_p_ma)
6918 EMSG(_(e_modifiable));
6919 else
6921 #ifdef FEAT_VIRTUALEDIT
6922 if (virtual_active())
6923 coladvance(getviscol());
6924 #endif
6925 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
6930 #ifdef FEAT_VREPLACE
6932 * "gr".
6934 static void
6935 nv_vreplace(cap)
6936 cmdarg_T *cap;
6938 # ifdef FEAT_VISUAL
6939 if (VIsual_active)
6941 cap->cmdchar = 'r';
6942 cap->nchar = cap->extra_char;
6943 nv_replace(cap); /* Do same as "r" in Visual mode for now */
6945 else
6946 # endif
6947 if (!checkclearopq(cap->oap))
6949 if (!curbuf->b_p_ma)
6950 EMSG(_(e_modifiable));
6951 else
6953 if (cap->extra_char == Ctrl_V) /* get another character */
6954 cap->extra_char = get_literal();
6955 stuffcharReadbuff(cap->extra_char);
6956 stuffcharReadbuff(ESC);
6957 # ifdef FEAT_VIRTUALEDIT
6958 if (virtual_active())
6959 coladvance(getviscol());
6960 # endif
6961 invoke_edit(cap, TRUE, 'v', FALSE);
6965 #endif
6968 * Swap case for "~" command, when it does not work like an operator.
6970 static void
6971 n_swapchar(cap)
6972 cmdarg_T *cap;
6974 long n;
6975 pos_T startpos;
6976 int did_change = 0;
6977 #ifdef FEAT_NETBEANS_INTG
6978 pos_T pos;
6979 char_u *ptr;
6980 int count;
6981 #endif
6983 if (checkclearopq(cap->oap))
6984 return;
6986 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
6988 clearopbeep(cap->oap);
6989 return;
6992 prep_redo_cmd(cap);
6994 if (u_save_cursor() == FAIL)
6995 return;
6997 startpos = curwin->w_cursor;
6998 #ifdef FEAT_NETBEANS_INTG
6999 pos = startpos;
7000 #endif
7001 for (n = cap->count1; n > 0; --n)
7003 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7004 inc_cursor();
7005 if (gchar_cursor() == NUL)
7007 if (vim_strchr(p_ww, '~') != NULL
7008 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7010 #ifdef FEAT_NETBEANS_INTG
7011 if (usingNetbeans)
7013 if (did_change)
7015 ptr = ml_get(pos.lnum);
7016 count = (int)STRLEN(ptr) - pos.col;
7017 netbeans_removed(curbuf, pos.lnum, pos.col,
7018 (long)count);
7019 netbeans_inserted(curbuf, pos.lnum, pos.col,
7020 &ptr[pos.col], count);
7022 pos.col = 0;
7023 pos.lnum++;
7025 #endif
7026 ++curwin->w_cursor.lnum;
7027 curwin->w_cursor.col = 0;
7028 if (n > 1)
7030 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7031 break;
7032 u_clearline();
7035 else
7036 break;
7039 #ifdef FEAT_NETBEANS_INTG
7040 if (did_change && usingNetbeans)
7042 ptr = ml_get(pos.lnum);
7043 count = curwin->w_cursor.col - pos.col;
7044 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7045 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
7047 #endif
7050 check_cursor();
7051 curwin->w_set_curswant = TRUE;
7052 if (did_change)
7054 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7055 0L);
7056 curbuf->b_op_start = startpos;
7057 curbuf->b_op_end = curwin->w_cursor;
7058 if (curbuf->b_op_end.col > 0)
7059 --curbuf->b_op_end.col;
7064 * Move cursor to mark.
7066 static void
7067 nv_cursormark(cap, flag, pos)
7068 cmdarg_T *cap;
7069 int flag;
7070 pos_T *pos;
7072 if (check_mark(pos) == FAIL)
7073 clearop(cap->oap);
7074 else
7076 if (cap->cmdchar == '\''
7077 || cap->cmdchar == '`'
7078 || cap->cmdchar == '['
7079 || cap->cmdchar == ']')
7080 setpcmark();
7081 curwin->w_cursor = *pos;
7082 if (flag)
7083 beginline(BL_WHITE | BL_FIX);
7084 else
7085 check_cursor();
7087 cap->oap->motion_type = flag ? MLINE : MCHAR;
7088 if (cap->cmdchar == '`')
7089 cap->oap->use_reg_one = TRUE;
7090 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7091 curwin->w_set_curswant = TRUE;
7094 #ifdef FEAT_VISUAL
7096 * Handle commands that are operators in Visual mode.
7098 static void
7099 v_visop(cap)
7100 cmdarg_T *cap;
7102 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7104 /* Uppercase means linewise, except in block mode, then "D" deletes till
7105 * the end of the line, and "C" replaces til EOL */
7106 if (isupper(cap->cmdchar))
7108 if (VIsual_mode != Ctrl_V)
7109 VIsual_mode = 'V';
7110 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7111 curwin->w_curswant = MAXCOL;
7113 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7114 nv_operator(cap);
7116 #endif
7119 * "s" and "S" commands.
7121 static void
7122 nv_subst(cap)
7123 cmdarg_T *cap;
7125 #ifdef FEAT_VISUAL
7126 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7128 if (cap->cmdchar == 'S')
7129 VIsual_mode = 'V';
7130 cap->cmdchar = 'c';
7131 nv_operator(cap);
7133 else
7134 #endif
7135 nv_optrans(cap);
7139 * Abbreviated commands.
7141 static void
7142 nv_abbrev(cap)
7143 cmdarg_T *cap;
7145 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7146 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7148 #ifdef FEAT_VISUAL
7149 /* in Visual mode these commands are operators */
7150 if (VIsual_active)
7151 v_visop(cap);
7152 else
7153 #endif
7154 nv_optrans(cap);
7158 * Translate a command into another command.
7160 static void
7161 nv_optrans(cap)
7162 cmdarg_T *cap;
7164 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7165 (char_u *)"d$", (char_u *)"c$",
7166 (char_u *)"cl", (char_u *)"cc",
7167 (char_u *)"yy", (char_u *)":s\r"};
7168 static char_u *str = (char_u *)"xXDCsSY&";
7170 if (!checkclearopq(cap->oap))
7172 /* In Vi "2D" doesn't delete the next line. Can't translate it
7173 * either, because "2." should also not use the count. */
7174 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7176 cap->oap->start = curwin->w_cursor;
7177 cap->oap->op_type = OP_DELETE;
7178 cap->count1 = 1;
7179 nv_dollar(cap);
7180 finish_op = TRUE;
7181 ResetRedobuff();
7182 AppendCharToRedobuff('D');
7184 else
7186 if (cap->count0)
7187 stuffnumReadbuff(cap->count0);
7188 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7191 cap->opcount = 0;
7195 * "'" and "`" commands. Also for "g'" and "g`".
7196 * cap->arg is TRUE for "'" and "g'".
7198 static void
7199 nv_gomark(cap)
7200 cmdarg_T *cap;
7202 pos_T *pos;
7203 int c;
7204 #ifdef FEAT_FOLDING
7205 linenr_T lnum = curwin->w_cursor.lnum;
7206 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7207 #endif
7209 if (cap->cmdchar == 'g')
7210 c = cap->extra_char;
7211 else
7212 c = cap->nchar;
7213 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7214 if (pos == (pos_T *)-1) /* jumped to other file */
7216 if (cap->arg)
7218 check_cursor_lnum();
7219 beginline(BL_WHITE | BL_FIX);
7221 else
7222 check_cursor();
7224 else
7225 nv_cursormark(cap, cap->arg, pos);
7227 #ifdef FEAT_VIRTUALEDIT
7228 /* May need to clear the coladd that a mark includes. */
7229 if (!virtual_active())
7230 curwin->w_cursor.coladd = 0;
7231 #endif
7232 #ifdef FEAT_FOLDING
7233 if (cap->oap->op_type == OP_NOP
7234 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7235 && (fdo_flags & FDO_MARK)
7236 && old_KeyTyped)
7237 foldOpenCursor();
7238 #endif
7242 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7244 static void
7245 nv_pcmark(cap)
7246 cmdarg_T *cap;
7248 #ifdef FEAT_JUMPLIST
7249 pos_T *pos;
7250 # ifdef FEAT_FOLDING
7251 linenr_T lnum = curwin->w_cursor.lnum;
7252 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7253 # endif
7255 if (!checkclearopq(cap->oap))
7257 if (cap->cmdchar == 'g')
7258 pos = movechangelist((int)cap->count1);
7259 else
7260 pos = movemark((int)cap->count1);
7261 if (pos == (pos_T *)-1) /* jump to other file */
7263 curwin->w_set_curswant = TRUE;
7264 check_cursor();
7266 else if (pos != NULL) /* can jump */
7267 nv_cursormark(cap, FALSE, pos);
7268 else if (cap->cmdchar == 'g')
7270 if (curbuf->b_changelistlen == 0)
7271 EMSG(_("E664: changelist is empty"));
7272 else if (cap->count1 < 0)
7273 EMSG(_("E662: At start of changelist"));
7274 else
7275 EMSG(_("E663: At end of changelist"));
7277 else
7278 clearopbeep(cap->oap);
7279 # ifdef FEAT_FOLDING
7280 if (cap->oap->op_type == OP_NOP
7281 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7282 && (fdo_flags & FDO_MARK)
7283 && old_KeyTyped)
7284 foldOpenCursor();
7285 # endif
7287 #else
7288 clearopbeep(cap->oap);
7289 #endif
7293 * Handle '"' command.
7295 static void
7296 nv_regname(cap)
7297 cmdarg_T *cap;
7299 if (checkclearop(cap->oap))
7300 return;
7301 #ifdef FEAT_EVAL
7302 if (cap->nchar == '=')
7303 cap->nchar = get_expr_register();
7304 #endif
7305 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7307 cap->oap->regname = cap->nchar;
7308 cap->opcount = cap->count0; /* remember count before '"' */
7309 #ifdef FEAT_EVAL
7310 set_reg_var(cap->oap->regname);
7311 #endif
7313 else
7314 clearopbeep(cap->oap);
7317 #ifdef FEAT_VISUAL
7319 * Handle "v", "V" and "CTRL-V" commands.
7320 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7321 * is TRUE.
7322 * Handle CTRL-Q just like CTRL-V.
7324 static void
7325 nv_visual(cap)
7326 cmdarg_T *cap;
7328 if (cap->cmdchar == Ctrl_Q)
7329 cap->cmdchar = Ctrl_V;
7331 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7332 * characterwise, linewise, or blockwise. */
7333 if (cap->oap->op_type != OP_NOP)
7335 cap->oap->motion_force = cap->cmdchar;
7336 finish_op = FALSE; /* operator doesn't finish now but later */
7337 return;
7340 VIsual_select = cap->arg;
7341 if (VIsual_active) /* change Visual mode */
7343 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7344 end_visual_mode();
7345 else /* toggle char/block mode */
7346 { /* or char/line mode */
7347 VIsual_mode = cap->cmdchar;
7348 showmode();
7350 redraw_curbuf_later(INVERTED); /* update the inversion */
7352 else /* start Visual mode */
7354 check_visual_highlight();
7355 if (cap->count0) /* use previously selected part */
7357 if (resel_VIsual_mode == NUL) /* there is none */
7359 beep_flush();
7360 return;
7362 VIsual = curwin->w_cursor;
7364 VIsual_active = TRUE;
7365 VIsual_reselect = TRUE;
7366 if (!cap->arg)
7367 /* start Select mode when 'selectmode' contains "cmd" */
7368 may_start_select('c');
7369 #ifdef FEAT_MOUSE
7370 setmouse();
7371 #endif
7372 if (p_smd && msg_silent == 0)
7373 redraw_cmdline = TRUE; /* show visual mode later */
7375 * For V and ^V, we multiply the number of lines even if there
7376 * was only one -- webb
7378 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7380 curwin->w_cursor.lnum +=
7381 resel_VIsual_line_count * cap->count0 - 1;
7382 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7383 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7385 VIsual_mode = resel_VIsual_mode;
7386 if (VIsual_mode == 'v')
7388 if (resel_VIsual_line_count <= 1)
7389 curwin->w_cursor.col += resel_VIsual_col * cap->count0 - 1;
7390 else
7391 curwin->w_cursor.col = resel_VIsual_col;
7392 check_cursor_col();
7394 if (resel_VIsual_col == MAXCOL)
7396 curwin->w_curswant = MAXCOL;
7397 coladvance((colnr_T)MAXCOL);
7399 else if (VIsual_mode == Ctrl_V)
7401 validate_virtcol();
7402 curwin->w_curswant = curwin->w_virtcol
7403 + resel_VIsual_col * cap->count0 - 1;
7404 coladvance(curwin->w_curswant);
7406 else
7407 curwin->w_set_curswant = TRUE;
7408 redraw_curbuf_later(INVERTED); /* show the inversion */
7410 else
7412 if (!cap->arg)
7413 /* start Select mode when 'selectmode' contains "cmd" */
7414 may_start_select('c');
7415 n_start_visual_mode(cap->cmdchar);
7421 * Start selection for Shift-movement keys.
7423 void
7424 start_selection()
7426 /* if 'selectmode' contains "key", start Select mode */
7427 may_start_select('k');
7428 n_start_visual_mode('v');
7432 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7434 void
7435 may_start_select(c)
7436 int c;
7438 VIsual_select = (stuff_empty() && typebuf_typed()
7439 && (vim_strchr(p_slm, c) != NULL));
7443 * Start Visual mode "c".
7444 * Should set VIsual_select before calling this.
7446 static void
7447 n_start_visual_mode(c)
7448 int c;
7450 VIsual_mode = c;
7451 VIsual_active = TRUE;
7452 VIsual_reselect = TRUE;
7453 #ifdef FEAT_VIRTUALEDIT
7454 /* Corner case: the 0 position in a tab may change when going into
7455 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7457 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7458 coladvance(curwin->w_virtcol);
7459 #endif
7460 VIsual = curwin->w_cursor;
7462 #ifdef FEAT_FOLDING
7463 foldAdjustVisual();
7464 #endif
7466 #ifdef FEAT_MOUSE
7467 setmouse();
7468 #endif
7469 if (p_smd && msg_silent == 0)
7470 redraw_cmdline = TRUE; /* show visual mode later */
7471 #ifdef FEAT_CLIPBOARD
7472 /* Make sure the clipboard gets updated. Needed because start and
7473 * end may still be the same, and the selection needs to be owned */
7474 clip_star.vmode = NUL;
7475 #endif
7477 /* Only need to redraw this line, unless still need to redraw an old
7478 * Visual area (when 'lazyredraw' is set). */
7479 if (curwin->w_redr_type < INVERTED)
7481 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7482 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7486 #endif /* FEAT_VISUAL */
7489 * CTRL-W: Window commands
7491 static void
7492 nv_window(cap)
7493 cmdarg_T *cap;
7495 #ifdef FEAT_WINDOWS
7496 if (!checkclearop(cap->oap))
7497 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7498 #else
7499 (void)checkclearop(cap->oap);
7500 #endif
7504 * CTRL-Z: Suspend
7506 static void
7507 nv_suspend(cap)
7508 cmdarg_T *cap;
7510 clearop(cap->oap);
7511 #ifdef FEAT_VISUAL
7512 if (VIsual_active)
7513 end_visual_mode(); /* stop Visual mode */
7514 #endif
7515 do_cmdline_cmd((char_u *)"st");
7519 * Commands starting with "g".
7521 static void
7522 nv_g_cmd(cap)
7523 cmdarg_T *cap;
7525 oparg_T *oap = cap->oap;
7526 #ifdef FEAT_VISUAL
7527 pos_T tpos;
7528 #endif
7529 int i;
7530 int flag = FALSE;
7532 switch (cap->nchar)
7534 #ifdef MEM_PROFILE
7536 * "g^A": dump log of used memory.
7538 case Ctrl_A:
7539 vim_mem_profile_dump();
7540 break;
7541 #endif
7543 #ifdef FEAT_VREPLACE
7545 * "gR": Enter virtual replace mode.
7547 case 'R':
7548 cap->arg = TRUE;
7549 nv_Replace(cap);
7550 break;
7552 case 'r':
7553 nv_vreplace(cap);
7554 break;
7555 #endif
7557 case '&':
7558 do_cmdline_cmd((char_u *)"%s//~/&");
7559 break;
7561 #ifdef FEAT_VISUAL
7563 * "gv": Reselect the previous Visual area. If Visual already active,
7564 * exchange previous and current Visual area.
7566 case 'v':
7567 if (checkclearop(oap))
7568 break;
7570 if ( curbuf->b_visual.vi_start.lnum == 0
7571 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7572 || curbuf->b_visual.vi_end.lnum == 0)
7573 beep_flush();
7574 else
7576 /* set w_cursor to the start of the Visual area, tpos to the end */
7577 if (VIsual_active)
7579 i = VIsual_mode;
7580 VIsual_mode = curbuf->b_visual.vi_mode;
7581 curbuf->b_visual.vi_mode = i;
7582 # ifdef FEAT_EVAL
7583 curbuf->b_visual_mode_eval = i;
7584 # endif
7585 i = curwin->w_curswant;
7586 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7587 curbuf->b_visual.vi_curswant = i;
7589 tpos = curbuf->b_visual.vi_end;
7590 curbuf->b_visual.vi_end = curwin->w_cursor;
7591 curwin->w_cursor = curbuf->b_visual.vi_start;
7592 curbuf->b_visual.vi_start = VIsual;
7594 else
7596 VIsual_mode = curbuf->b_visual.vi_mode;
7597 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7598 tpos = curbuf->b_visual.vi_end;
7599 curwin->w_cursor = curbuf->b_visual.vi_start;
7602 VIsual_active = TRUE;
7603 VIsual_reselect = TRUE;
7605 /* Set Visual to the start and w_cursor to the end of the Visual
7606 * area. Make sure they are on an existing character. */
7607 check_cursor();
7608 VIsual = curwin->w_cursor;
7609 curwin->w_cursor = tpos;
7610 check_cursor();
7611 update_topline();
7613 * When called from normal "g" command: start Select mode when
7614 * 'selectmode' contains "cmd". When called for K_SELECT, always
7615 * start Select mode.
7617 if (cap->arg)
7618 VIsual_select = TRUE;
7619 else
7620 may_start_select('c');
7621 #ifdef FEAT_MOUSE
7622 setmouse();
7623 #endif
7624 #ifdef FEAT_CLIPBOARD
7625 /* Make sure the clipboard gets updated. Needed because start and
7626 * end are still the same, and the selection needs to be owned */
7627 clip_star.vmode = NUL;
7628 #endif
7629 redraw_curbuf_later(INVERTED);
7630 showmode();
7632 break;
7634 * "gV": Don't reselect the previous Visual area after a Select mode
7635 * mapping of menu.
7637 case 'V':
7638 VIsual_reselect = FALSE;
7639 break;
7642 * "gh": start Select mode.
7643 * "gH": start Select line mode.
7644 * "g^H": start Select block mode.
7646 case K_BS:
7647 cap->nchar = Ctrl_H;
7648 /* FALLTHROUGH */
7649 case 'h':
7650 case 'H':
7651 case Ctrl_H:
7652 # ifdef EBCDIC
7653 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7654 if (cap->nchar == Ctrl_H)
7655 cap->cmdchar = Ctrl_V;
7656 else
7657 # endif
7658 cap->cmdchar = cap->nchar + ('v' - 'h');
7659 cap->arg = TRUE;
7660 nv_visual(cap);
7661 break;
7662 #endif /* FEAT_VISUAL */
7665 * "gj" and "gk" two new funny movement keys -- up and down
7666 * movement based on *screen* line rather than *file* line.
7668 case 'j':
7669 case K_DOWN:
7670 /* with 'nowrap' it works just like the normal "j" command; also when
7671 * in a closed fold */
7672 if (!curwin->w_p_wrap
7673 #ifdef FEAT_FOLDING
7674 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7675 #endif
7678 oap->motion_type = MLINE;
7679 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7681 else
7682 i = nv_screengo(oap, FORWARD, cap->count1);
7683 if (i == FAIL)
7684 clearopbeep(oap);
7685 break;
7687 case 'k':
7688 case K_UP:
7689 /* with 'nowrap' it works just like the normal "k" command; also when
7690 * in a closed fold */
7691 if (!curwin->w_p_wrap
7692 #ifdef FEAT_FOLDING
7693 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7694 #endif
7697 oap->motion_type = MLINE;
7698 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7700 else
7701 i = nv_screengo(oap, BACKWARD, cap->count1);
7702 if (i == FAIL)
7703 clearopbeep(oap);
7704 break;
7707 * "gJ": join two lines without inserting a space.
7709 case 'J':
7710 nv_join(cap);
7711 break;
7714 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7715 * "gm": middle of "g0" and "g$".
7717 case '^':
7718 flag = TRUE;
7719 /* FALLTHROUGH */
7721 case '0':
7722 case 'm':
7723 case K_HOME:
7724 case K_KHOME:
7725 oap->motion_type = MCHAR;
7726 oap->inclusive = FALSE;
7727 if (curwin->w_p_wrap
7728 #ifdef FEAT_VERTSPLIT
7729 && curwin->w_width != 0
7730 #endif
7733 int width1 = W_WIDTH(curwin) - curwin_col_off();
7734 int width2 = width1 + curwin_col_off2();
7736 validate_virtcol();
7737 i = 0;
7738 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
7739 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
7741 else
7742 i = curwin->w_leftcol;
7743 /* Go to the middle of the screen line. When 'number' is on and lines
7744 * are wrapping the middle can be more to the left.*/
7745 if (cap->nchar == 'm')
7746 i += (W_WIDTH(curwin) - curwin_col_off()
7747 + ((curwin->w_p_wrap && i > 0)
7748 ? curwin_col_off2() : 0)) / 2;
7749 coladvance((colnr_T)i);
7750 if (flag)
7753 i = gchar_cursor();
7754 while (vim_iswhite(i) && oneright() == OK);
7756 curwin->w_set_curswant = TRUE;
7757 break;
7759 case '_':
7760 /* "g_": to the last non-blank character in the line or <count> lines
7761 * downward. */
7762 cap->oap->motion_type = MCHAR;
7763 cap->oap->inclusive = TRUE;
7764 curwin->w_curswant = MAXCOL;
7765 if (cursor_down((long)(cap->count1 - 1),
7766 cap->oap->op_type == OP_NOP) == FAIL)
7767 clearopbeep(cap->oap);
7768 else
7770 char_u *ptr = ml_get_curline();
7772 /* In Visual mode we may end up after the line. */
7773 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
7774 --curwin->w_cursor.col;
7776 /* Decrease the cursor column until it's on a non-blank. */
7777 while (curwin->w_cursor.col > 0
7778 && vim_iswhite(ptr[curwin->w_cursor.col]))
7779 --curwin->w_cursor.col;
7780 curwin->w_set_curswant = TRUE;
7782 break;
7784 case '$':
7785 case K_END:
7786 case K_KEND:
7788 int col_off = curwin_col_off();
7790 oap->motion_type = MCHAR;
7791 oap->inclusive = TRUE;
7792 if (curwin->w_p_wrap
7793 #ifdef FEAT_VERTSPLIT
7794 && curwin->w_width != 0
7795 #endif
7798 curwin->w_curswant = MAXCOL; /* so we stay at the end */
7799 if (cap->count1 == 1)
7801 int width1 = W_WIDTH(curwin) - col_off;
7802 int width2 = width1 + curwin_col_off2();
7804 validate_virtcol();
7805 i = width1 - 1;
7806 if (curwin->w_virtcol >= (colnr_T)width1)
7807 i += ((curwin->w_virtcol - width1) / width2 + 1)
7808 * width2;
7809 coladvance((colnr_T)i);
7810 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
7811 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
7814 * Check for landing on a character that got split at
7815 * the end of the line. We do not want to advance to
7816 * the next screen line.
7818 validate_virtcol();
7819 if (curwin->w_virtcol > (colnr_T)i)
7820 --curwin->w_cursor.col;
7822 #endif
7824 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
7825 clearopbeep(oap);
7827 else
7829 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
7830 coladvance((colnr_T)i);
7832 /* Make sure we stick in this column. */
7833 validate_virtcol();
7834 curwin->w_curswant = curwin->w_virtcol;
7835 curwin->w_set_curswant = FALSE;
7838 break;
7841 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
7843 case '*':
7844 case '#':
7845 #if POUND != '#'
7846 case POUND: /* pound sign (sometimes equal to '#') */
7847 #endif
7848 case Ctrl_RSB: /* :tag or :tselect for current identifier */
7849 case ']': /* :tselect for current identifier */
7850 nv_ident(cap);
7851 break;
7854 * ge and gE: go back to end of word
7856 case 'e':
7857 case 'E':
7858 oap->motion_type = MCHAR;
7859 curwin->w_set_curswant = TRUE;
7860 oap->inclusive = TRUE;
7861 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
7862 clearopbeep(oap);
7863 break;
7866 * "g CTRL-G": display info about cursor position
7868 case Ctrl_G:
7869 cursor_pos_info();
7870 break;
7873 * "gi": start Insert at the last position.
7875 case 'i':
7876 if (curbuf->b_last_insert.lnum != 0)
7878 curwin->w_cursor = curbuf->b_last_insert;
7879 check_cursor_lnum();
7880 i = (int)STRLEN(ml_get_curline());
7881 if (curwin->w_cursor.col > (colnr_T)i)
7883 #ifdef FEAT_VIRTUALEDIT
7884 if (virtual_active())
7885 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
7886 #endif
7887 curwin->w_cursor.col = i;
7890 cap->cmdchar = 'i';
7891 nv_edit(cap);
7892 break;
7895 * "gI": Start insert in column 1.
7897 case 'I':
7898 beginline(0);
7899 if (!checkclearopq(oap))
7900 invoke_edit(cap, FALSE, 'g', FALSE);
7901 break;
7903 #ifdef FEAT_SEARCHPATH
7905 * "gf": goto file, edit file under cursor
7906 * "]f" and "[f": can also be used.
7908 case 'f':
7909 case 'F':
7910 nv_gotofile(cap);
7911 break;
7912 #endif
7914 /* "g'm" and "g`m": jump to mark without setting pcmark */
7915 case '\'':
7916 cap->arg = TRUE;
7917 /*FALLTHROUGH*/
7918 case '`':
7919 nv_gomark(cap);
7920 break;
7923 * "gs": Goto sleep.
7925 case 's':
7926 do_sleep(cap->count1 * 1000L);
7927 break;
7930 * "ga": Display the ascii value of the character under the
7931 * cursor. It is displayed in decimal, hex, and octal. -- webb
7933 case 'a':
7934 do_ascii(NULL);
7935 break;
7937 #ifdef FEAT_MBYTE
7939 * "g8": Display the bytes used for the UTF-8 character under the
7940 * cursor. It is displayed in hex.
7941 * "8g8" finds illegal byte sequence.
7943 case '8':
7944 if (cap->count0 == 8)
7945 utf_find_illegal();
7946 else
7947 show_utf8();
7948 break;
7949 #endif
7951 case '<':
7952 show_sb_text();
7953 break;
7956 * "gg": Goto the first line in file. With a count it goes to
7957 * that line number like for "G". -- webb
7959 case 'g':
7960 cap->arg = FALSE;
7961 nv_goto(cap);
7962 break;
7965 * Two-character operators:
7966 * "gq" Format text
7967 * "gw" Format text and keep cursor position
7968 * "g~" Toggle the case of the text.
7969 * "gu" Change text to lower case.
7970 * "gU" Change text to upper case.
7971 * "g?" rot13 encoding
7972 * "g@" call 'operatorfunc'
7974 case 'q':
7975 case 'w':
7976 oap->cursor_start = curwin->w_cursor;
7977 /*FALLTHROUGH*/
7978 case '~':
7979 case 'u':
7980 case 'U':
7981 case '?':
7982 case '@':
7983 nv_operator(cap);
7984 break;
7987 * "gd": Find first occurrence of pattern under the cursor in the
7988 * current function
7989 * "gD": idem, but in the current file.
7991 case 'd':
7992 case 'D':
7993 nv_gd(oap, cap->nchar, (int)cap->count0);
7994 break;
7996 #ifdef FEAT_MOUSE
7998 * g<*Mouse> : <C-*mouse>
8000 case K_MIDDLEMOUSE:
8001 case K_MIDDLEDRAG:
8002 case K_MIDDLERELEASE:
8003 case K_LEFTMOUSE:
8004 case K_LEFTDRAG:
8005 case K_LEFTRELEASE:
8006 case K_RIGHTMOUSE:
8007 case K_RIGHTDRAG:
8008 case K_RIGHTRELEASE:
8009 case K_X1MOUSE:
8010 case K_X1DRAG:
8011 case K_X1RELEASE:
8012 case K_X2MOUSE:
8013 case K_X2DRAG:
8014 case K_X2RELEASE:
8015 mod_mask = MOD_MASK_CTRL;
8016 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8017 break;
8018 #endif
8020 case K_IGNORE:
8021 break;
8024 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8026 case 'p':
8027 case 'P':
8028 nv_put(cap);
8029 break;
8031 #ifdef FEAT_BYTEOFF
8032 /* "go": goto byte count from start of buffer */
8033 case 'o':
8034 goto_byte(cap->count0);
8035 break;
8036 #endif
8038 /* "gQ": improved Ex mode */
8039 case 'Q':
8040 if (text_locked())
8042 clearopbeep(cap->oap);
8043 text_locked_msg();
8044 break;
8047 if (!checkclearopq(oap))
8048 do_exmode(TRUE);
8049 break;
8051 #ifdef FEAT_JUMPLIST
8052 case ',':
8053 nv_pcmark(cap);
8054 break;
8056 case ';':
8057 cap->count1 = -cap->count1;
8058 nv_pcmark(cap);
8059 break;
8060 #endif
8062 #ifdef FEAT_WINDOWS
8063 case 't':
8064 goto_tabpage((int)cap->count0);
8065 break;
8066 case 'T':
8067 goto_tabpage(-(int)cap->count1);
8068 break;
8069 #endif
8071 case '+':
8072 case '-': /* "g+" and "g-": undo or redo along the timeline */
8073 if (!checkclearopq(oap))
8074 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
8075 FALSE, FALSE);
8076 break;
8078 default:
8079 clearopbeep(oap);
8080 break;
8085 * Handle "o" and "O" commands.
8087 static void
8088 n_opencmd(cap)
8089 cmdarg_T *cap;
8091 if (!checkclearopq(cap->oap))
8093 #ifdef FEAT_FOLDING
8094 if (cap->cmdchar == 'O')
8095 /* Open above the first line of a folded sequence of lines */
8096 (void)hasFolding(curwin->w_cursor.lnum,
8097 &curwin->w_cursor.lnum, NULL);
8098 else
8099 /* Open below the last line of a folded sequence of lines */
8100 (void)hasFolding(curwin->w_cursor.lnum,
8101 NULL, &curwin->w_cursor.lnum);
8102 #endif
8103 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8104 (cap->cmdchar == 'O' ? 1 : 0)),
8105 (linenr_T)(curwin->w_cursor.lnum +
8106 (cap->cmdchar == 'o' ? 1 : 0))
8107 ) == OK
8108 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8109 #ifdef FEAT_COMMENTS
8110 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8111 #endif
8112 0, 0))
8114 /* When '#' is in 'cpoptions' ignore the count. */
8115 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8116 cap->count1 = 1;
8117 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8123 * "." command: redo last change.
8125 static void
8126 nv_dot(cap)
8127 cmdarg_T *cap;
8129 if (!checkclearopq(cap->oap))
8132 * If "restart_edit" is TRUE, the last but one command is repeated
8133 * instead of the last command (inserting text). This is used for
8134 * CTRL-O <.> in insert mode.
8136 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8137 clearopbeep(cap->oap);
8142 * CTRL-R: undo undo
8144 static void
8145 nv_redo(cap)
8146 cmdarg_T *cap;
8148 if (!checkclearopq(cap->oap))
8150 u_redo((int)cap->count1);
8151 curwin->w_set_curswant = TRUE;
8156 * Handle "U" command.
8158 static void
8159 nv_Undo(cap)
8160 cmdarg_T *cap;
8162 /* In Visual mode and typing "gUU" triggers an operator */
8163 if (cap->oap->op_type == OP_UPPER
8164 #ifdef FEAT_VISUAL
8165 || VIsual_active
8166 #endif
8169 /* translate "gUU" to "gUgU" */
8170 cap->cmdchar = 'g';
8171 cap->nchar = 'U';
8172 nv_operator(cap);
8174 else if (!checkclearopq(cap->oap))
8176 u_undoline();
8177 curwin->w_set_curswant = TRUE;
8182 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8183 * single character.
8185 static void
8186 nv_tilde(cap)
8187 cmdarg_T *cap;
8189 if (!p_to
8190 #ifdef FEAT_VISUAL
8191 && !VIsual_active
8192 #endif
8193 && cap->oap->op_type != OP_TILDE)
8194 n_swapchar(cap);
8195 else
8196 nv_operator(cap);
8200 * Handle an operator command.
8201 * The actual work is done by do_pending_operator().
8203 static void
8204 nv_operator(cap)
8205 cmdarg_T *cap;
8207 int op_type;
8209 op_type = get_op_type(cap->cmdchar, cap->nchar);
8211 if (op_type == cap->oap->op_type) /* double operator works on lines */
8212 nv_lineop(cap);
8213 else if (!checkclearop(cap->oap))
8215 cap->oap->start = curwin->w_cursor;
8216 cap->oap->op_type = op_type;
8221 * Handle linewise operator "dd", "yy", etc.
8223 * "_" is is a strange motion command that helps make operators more logical.
8224 * It is actually implemented, but not documented in the real Vi. This motion
8225 * command actually refers to "the current line". Commands like "dd" and "yy"
8226 * are really an alternate form of "d_" and "y_". It does accept a count, so
8227 * "d3_" works to delete 3 lines.
8229 static void
8230 nv_lineop(cap)
8231 cmdarg_T *cap;
8233 cap->oap->motion_type = MLINE;
8234 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8235 clearopbeep(cap->oap);
8236 else if ( cap->oap->op_type == OP_DELETE
8237 || cap->oap->op_type == OP_LSHIFT
8238 || cap->oap->op_type == OP_RSHIFT)
8239 beginline(BL_SOL | BL_FIX);
8240 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8241 beginline(BL_WHITE | BL_FIX);
8245 * <Home> command.
8247 static void
8248 nv_home(cap)
8249 cmdarg_T *cap;
8251 /* CTRL-HOME is like "gg" */
8252 if (mod_mask & MOD_MASK_CTRL)
8253 nv_goto(cap);
8254 else
8256 cap->count0 = 1;
8257 nv_pipe(cap);
8259 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8260 one-character line). */
8264 * "|" command.
8266 static void
8267 nv_pipe(cap)
8268 cmdarg_T *cap;
8270 cap->oap->motion_type = MCHAR;
8271 cap->oap->inclusive = FALSE;
8272 beginline(0);
8273 if (cap->count0 > 0)
8275 coladvance((colnr_T)(cap->count0 - 1));
8276 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8278 else
8279 curwin->w_curswant = 0;
8280 /* keep curswant at the column where we wanted to go, not where
8281 we ended; differs if line is too short */
8282 curwin->w_set_curswant = FALSE;
8286 * Handle back-word command "b" and "B".
8287 * cap->arg is 1 for "B"
8289 static void
8290 nv_bck_word(cap)
8291 cmdarg_T *cap;
8293 cap->oap->motion_type = MCHAR;
8294 cap->oap->inclusive = FALSE;
8295 curwin->w_set_curswant = TRUE;
8296 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8297 clearopbeep(cap->oap);
8298 #ifdef FEAT_FOLDING
8299 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8300 foldOpenCursor();
8301 #endif
8305 * Handle word motion commands "e", "E", "w" and "W".
8306 * cap->arg is TRUE for "E" and "W".
8308 static void
8309 nv_wordcmd(cap)
8310 cmdarg_T *cap;
8312 int n;
8313 int word_end;
8314 int flag = FALSE;
8317 * Set inclusive for the "E" and "e" command.
8319 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8320 word_end = TRUE;
8321 else
8322 word_end = FALSE;
8323 cap->oap->inclusive = word_end;
8326 * "cw" and "cW" are a special case.
8328 if (!word_end && cap->oap->op_type == OP_CHANGE)
8330 n = gchar_cursor();
8331 if (n != NUL) /* not an empty line */
8333 if (vim_iswhite(n))
8336 * Reproduce a funny Vi behaviour: "cw" on a blank only
8337 * changes one character, not all blanks until the start of
8338 * the next word. Only do this when the 'w' flag is included
8339 * in 'cpoptions'.
8341 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8343 cap->oap->inclusive = TRUE;
8344 cap->oap->motion_type = MCHAR;
8345 return;
8348 else
8351 * This is a little strange. To match what the real Vi does,
8352 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8353 * that we are not on a space or a TAB. This seems impolite
8354 * at first, but it's really more what we mean when we say
8355 * 'cw'.
8356 * Another strangeness: When standing on the end of a word
8357 * "ce" will change until the end of the next wordt, but "cw"
8358 * will change only one character! This is done by setting
8359 * flag.
8361 cap->oap->inclusive = TRUE;
8362 word_end = TRUE;
8363 flag = TRUE;
8368 cap->oap->motion_type = MCHAR;
8369 curwin->w_set_curswant = TRUE;
8370 if (word_end)
8371 n = end_word(cap->count1, cap->arg, flag, FALSE);
8372 else
8373 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8375 /* Don't leave the cursor on the NUL past a line */
8376 if (n != FAIL && curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
8378 --curwin->w_cursor.col;
8379 cap->oap->inclusive = TRUE;
8382 if (n == FAIL && cap->oap->op_type == OP_NOP)
8383 clearopbeep(cap->oap);
8384 else
8386 #ifdef FEAT_VISUAL
8387 adjust_for_sel(cap);
8388 #endif
8389 #ifdef FEAT_FOLDING
8390 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8391 foldOpenCursor();
8392 #endif
8397 * "0" and "^" commands.
8398 * cap->arg is the argument for beginline().
8400 static void
8401 nv_beginline(cap)
8402 cmdarg_T *cap;
8404 cap->oap->motion_type = MCHAR;
8405 cap->oap->inclusive = FALSE;
8406 beginline(cap->arg);
8407 #ifdef FEAT_FOLDING
8408 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8409 foldOpenCursor();
8410 #endif
8411 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8412 one-character line). */
8415 #ifdef FEAT_VISUAL
8417 * In exclusive Visual mode, may include the last character.
8419 static void
8420 adjust_for_sel(cap)
8421 cmdarg_T *cap;
8423 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8424 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8426 # ifdef FEAT_MBYTE
8427 if (has_mbyte)
8428 inc_cursor();
8429 else
8430 # endif
8431 ++curwin->w_cursor.col;
8432 cap->oap->inclusive = FALSE;
8437 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8438 * Should check VIsual_mode before calling this.
8439 * Returns TRUE when backed up to the previous line.
8441 static int
8442 unadjust_for_sel()
8444 pos_T *pp;
8446 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8448 if (lt(VIsual, curwin->w_cursor))
8449 pp = &curwin->w_cursor;
8450 else
8451 pp = &VIsual;
8452 #ifdef FEAT_VIRTUALEDIT
8453 if (pp->coladd > 0)
8454 --pp->coladd;
8455 else
8456 #endif
8457 if (pp->col > 0)
8459 --pp->col;
8460 #ifdef FEAT_MBYTE
8461 mb_adjustpos(pp);
8462 #endif
8464 else if (pp->lnum > 1)
8466 --pp->lnum;
8467 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8468 return TRUE;
8471 return FALSE;
8475 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8477 static void
8478 nv_select(cap)
8479 cmdarg_T *cap;
8481 if (VIsual_active)
8482 VIsual_select = TRUE;
8483 else if (VIsual_reselect)
8485 cap->nchar = 'v'; /* fake "gv" command */
8486 cap->arg = TRUE;
8487 nv_g_cmd(cap);
8491 #endif
8494 * "G", "gg", CTRL-END, CTRL-HOME.
8495 * cap->arg is TRUE for "G".
8497 static void
8498 nv_goto(cap)
8499 cmdarg_T *cap;
8501 linenr_T lnum;
8503 if (cap->arg)
8504 lnum = curbuf->b_ml.ml_line_count;
8505 else
8506 lnum = 1L;
8507 cap->oap->motion_type = MLINE;
8508 setpcmark();
8510 /* When a count is given, use it instead of the default lnum */
8511 if (cap->count0 != 0)
8512 lnum = cap->count0;
8513 if (lnum < 1L)
8514 lnum = 1L;
8515 else if (lnum > curbuf->b_ml.ml_line_count)
8516 lnum = curbuf->b_ml.ml_line_count;
8517 curwin->w_cursor.lnum = lnum;
8518 beginline(BL_SOL | BL_FIX);
8519 #ifdef FEAT_FOLDING
8520 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8521 foldOpenCursor();
8522 #endif
8526 * CTRL-\ in Normal mode.
8528 static void
8529 nv_normal(cap)
8530 cmdarg_T *cap;
8532 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8534 clearop(cap->oap);
8535 if (restart_edit != 0 && mode_displayed)
8536 clear_cmdline = TRUE; /* unshow mode later */
8537 restart_edit = 0;
8538 #ifdef FEAT_CMDWIN
8539 if (cmdwin_type != 0)
8540 cmdwin_result = Ctrl_C;
8541 #endif
8542 #ifdef FEAT_VISUAL
8543 if (VIsual_active)
8545 end_visual_mode(); /* stop Visual */
8546 redraw_curbuf_later(INVERTED);
8548 #endif
8549 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8550 if (cap->nchar == Ctrl_G && p_im)
8551 restart_edit = 'a';
8553 else
8554 clearopbeep(cap->oap);
8558 * ESC in Normal mode: beep, but don't flush buffers.
8559 * Don't even beep if we are canceling a command.
8561 static void
8562 nv_esc(cap)
8563 cmdarg_T *cap;
8565 int no_reason;
8567 no_reason = (cap->oap->op_type == OP_NOP
8568 && cap->opcount == 0
8569 && cap->count0 == 0
8570 && cap->oap->regname == 0
8571 && !p_im);
8573 if (cap->arg) /* TRUE for CTRL-C */
8575 if (restart_edit == 0
8576 #ifdef FEAT_CMDWIN
8577 && cmdwin_type == 0
8578 #endif
8579 #ifdef FEAT_VISUAL
8580 && !VIsual_active
8581 #endif
8582 && no_reason)
8583 MSG(_("Type :quit<Enter> to exit Vim"));
8585 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8586 * set again below when halfway a mapping. */
8587 if (!p_im)
8588 restart_edit = 0;
8589 #ifdef FEAT_CMDWIN
8590 if (cmdwin_type != 0)
8592 cmdwin_result = K_IGNORE;
8593 got_int = FALSE; /* don't stop executing autocommands et al. */
8594 return;
8596 #endif
8599 #ifdef FEAT_VISUAL
8600 if (VIsual_active)
8602 end_visual_mode(); /* stop Visual */
8603 check_cursor_col(); /* make sure cursor is not beyond EOL */
8604 curwin->w_set_curswant = TRUE;
8605 redraw_curbuf_later(INVERTED);
8607 else
8608 #endif
8609 if (no_reason)
8610 vim_beep();
8611 clearop(cap->oap);
8613 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8614 * set return to Insert mode afterwards. */
8615 if (restart_edit == 0 && goto_im()
8616 #ifdef FEAT_EX_EXTRA
8617 && ex_normal_busy == 0
8618 #endif
8620 restart_edit = 'a';
8624 * Handle "A", "a", "I", "i" and <Insert> commands.
8626 static void
8627 nv_edit(cap)
8628 cmdarg_T *cap;
8630 /* <Insert> is equal to "i" */
8631 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8632 cap->cmdchar = 'i';
8634 #ifdef FEAT_VISUAL
8635 /* in Visual mode "A" and "I" are an operator */
8636 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8637 v_visop(cap);
8639 /* in Visual mode and after an operator "a" and "i" are for text objects */
8640 else
8641 #endif
8642 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8643 && (cap->oap->op_type != OP_NOP
8644 #ifdef FEAT_VISUAL
8645 || VIsual_active
8646 #endif
8649 #ifdef FEAT_TEXTOBJ
8650 nv_object(cap);
8651 #else
8652 clearopbeep(cap->oap);
8653 #endif
8655 else if (!curbuf->b_p_ma && !p_im)
8657 /* Only give this error when 'insertmode' is off. */
8658 EMSG(_(e_modifiable));
8659 clearop(cap->oap);
8661 else if (!checkclearopq(cap->oap))
8663 switch (cap->cmdchar)
8665 case 'A': /* "A"ppend after the line */
8666 curwin->w_set_curswant = TRUE;
8667 #ifdef FEAT_VIRTUALEDIT
8668 if (ve_flags == VE_ALL)
8670 int save_State = State;
8672 /* Pretent Insert mode here to allow the cursor on the
8673 * character past the end of the line */
8674 State = INSERT;
8675 coladvance((colnr_T)MAXCOL);
8676 State = save_State;
8678 else
8679 #endif
8680 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8681 break;
8683 case 'I': /* "I"nsert before the first non-blank */
8684 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
8685 beginline(BL_WHITE);
8686 else
8687 beginline(BL_WHITE|BL_FIX);
8688 break;
8690 case 'a': /* "a"ppend is like "i"nsert on the next character. */
8691 #ifdef FEAT_VIRTUALEDIT
8692 /* increment coladd when in virtual space, increment the
8693 * column otherwise, also to append after an unprintable char */
8694 if (virtual_active()
8695 && (curwin->w_cursor.coladd > 0
8696 || *ml_get_cursor() == NUL
8697 || *ml_get_cursor() == TAB))
8698 curwin->w_cursor.coladd++;
8699 else
8700 #endif
8701 if (*ml_get_cursor() != NUL)
8702 inc_cursor();
8703 break;
8706 #ifdef FEAT_VIRTUALEDIT
8707 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
8709 int save_State = State;
8711 /* Pretent Insert mode here to allow the cursor on the
8712 * character past the end of the line */
8713 State = INSERT;
8714 coladvance(getviscol());
8715 State = save_State;
8717 #endif
8719 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
8724 * Invoke edit() and take care of "restart_edit" and the return value.
8726 static void
8727 invoke_edit(cap, repl, cmd, startln)
8728 cmdarg_T *cap;
8729 int repl; /* "r" or "gr" command */
8730 int cmd;
8731 int startln;
8733 int restart_edit_save = 0;
8735 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
8736 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
8737 * it. */
8738 if (repl || !stuff_empty())
8739 restart_edit_save = restart_edit;
8740 else
8741 restart_edit_save = 0;
8743 /* Always reset "restart_edit", this is not a restarted edit. */
8744 restart_edit = 0;
8746 if (edit(cmd, startln, cap->count1))
8747 cap->retval |= CA_COMMAND_BUSY;
8749 if (restart_edit == 0)
8750 restart_edit = restart_edit_save;
8753 #ifdef FEAT_TEXTOBJ
8755 * "a" or "i" while an operator is pending or in Visual mode: object motion.
8757 static void
8758 nv_object(cap)
8759 cmdarg_T *cap;
8761 int flag;
8762 int include;
8763 char_u *mps_save;
8765 if (cap->cmdchar == 'i')
8766 include = FALSE; /* "ix" = inner object: exclude white space */
8767 else
8768 include = TRUE; /* "ax" = an object: include white space */
8770 /* Make sure (), [], {} and <> are in 'matchpairs' */
8771 mps_save = curbuf->b_p_mps;
8772 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
8774 switch (cap->nchar)
8776 case 'w': /* "aw" = a word */
8777 flag = current_word(cap->oap, cap->count1, include, FALSE);
8778 break;
8779 case 'W': /* "aW" = a WORD */
8780 flag = current_word(cap->oap, cap->count1, include, TRUE);
8781 break;
8782 case 'b': /* "ab" = a braces block */
8783 case '(':
8784 case ')':
8785 flag = current_block(cap->oap, cap->count1, include, '(', ')');
8786 break;
8787 case 'B': /* "aB" = a Brackets block */
8788 case '{':
8789 case '}':
8790 flag = current_block(cap->oap, cap->count1, include, '{', '}');
8791 break;
8792 case '[': /* "a[" = a [] block */
8793 case ']':
8794 flag = current_block(cap->oap, cap->count1, include, '[', ']');
8795 break;
8796 case '<': /* "a<" = a <> block */
8797 case '>':
8798 flag = current_block(cap->oap, cap->count1, include, '<', '>');
8799 break;
8800 case 't': /* "at" = a tag block (xml and html) */
8801 flag = current_tagblock(cap->oap, cap->count1, include);
8802 break;
8803 case 'p': /* "ap" = a paragraph */
8804 flag = current_par(cap->oap, cap->count1, include, 'p');
8805 break;
8806 case 's': /* "as" = a sentence */
8807 flag = current_sent(cap->oap, cap->count1, include);
8808 break;
8809 case '"': /* "a"" = a double quoted string */
8810 case '\'': /* "a'" = a single quoted string */
8811 case '`': /* "a`" = a backtick quoted string */
8812 flag = current_quote(cap->oap, cap->count1, include,
8813 cap->nchar);
8814 break;
8815 #if 0 /* TODO */
8816 case 'S': /* "aS" = a section */
8817 case 'f': /* "af" = a filename */
8818 case 'u': /* "au" = a URL */
8819 #endif
8820 default:
8821 flag = FAIL;
8822 break;
8825 curbuf->b_p_mps = mps_save;
8826 if (flag == FAIL)
8827 clearopbeep(cap->oap);
8828 adjust_cursor_col();
8829 curwin->w_set_curswant = TRUE;
8831 #endif
8834 * "q" command: Start/stop recording.
8835 * "q:", "q/", "q?": edit command-line in command-line window.
8837 static void
8838 nv_record(cap)
8839 cmdarg_T *cap;
8841 if (cap->oap->op_type == OP_FORMAT)
8843 /* "gqq" is the same as "gqgq": format line */
8844 cap->cmdchar = 'g';
8845 cap->nchar = 'q';
8846 nv_operator(cap);
8848 else if (!checkclearop(cap->oap))
8850 #ifdef FEAT_CMDWIN
8851 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
8853 stuffcharReadbuff(cap->nchar);
8854 stuffcharReadbuff(K_CMDWIN);
8856 else
8857 #endif
8858 /* (stop) recording into a named register, unless executing a
8859 * register */
8860 if (!Exec_reg && do_record(cap->nchar) == FAIL)
8861 clearopbeep(cap->oap);
8866 * Handle the "@r" command.
8868 static void
8869 nv_at(cap)
8870 cmdarg_T *cap;
8872 if (checkclearop(cap->oap))
8873 return;
8874 #ifdef FEAT_EVAL
8875 if (cap->nchar == '=')
8877 if (get_expr_register() == NUL)
8878 return;
8880 #endif
8881 while (cap->count1-- && !got_int)
8883 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
8885 clearopbeep(cap->oap);
8886 break;
8888 line_breakcheck();
8893 * Handle the CTRL-U and CTRL-D commands.
8895 static void
8896 nv_halfpage(cap)
8897 cmdarg_T *cap;
8899 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
8900 || (cap->cmdchar == Ctrl_D
8901 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
8902 clearopbeep(cap->oap);
8903 else if (!checkclearop(cap->oap))
8904 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
8908 * Handle "J" or "gJ" command.
8910 static void
8911 nv_join(cap)
8912 cmdarg_T *cap;
8914 #ifdef FEAT_VISUAL
8915 if (VIsual_active) /* join the visual lines */
8916 nv_operator(cap);
8917 else
8918 #endif
8919 if (!checkclearop(cap->oap))
8921 if (cap->count0 <= 1)
8922 cap->count0 = 2; /* default for join is two lines! */
8923 if (curwin->w_cursor.lnum + cap->count0 - 1 >
8924 curbuf->b_ml.ml_line_count)
8925 clearopbeep(cap->oap); /* beyond last line */
8926 else
8928 prep_redo(cap->oap->regname, cap->count0,
8929 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
8930 do_do_join(cap->count0, cap->nchar == NUL);
8936 * "P", "gP", "p" and "gp" commands.
8938 static void
8939 nv_put(cap)
8940 cmdarg_T *cap;
8942 #ifdef FEAT_VISUAL
8943 int regname = 0;
8944 void *reg1 = NULL, *reg2 = NULL;
8945 int empty = FALSE;
8946 int was_visual = FALSE;
8947 #endif
8948 int dir;
8949 int flags = 0;
8951 if (cap->oap->op_type != OP_NOP)
8953 #ifdef FEAT_DIFF
8954 /* "dp" is ":diffput" */
8955 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
8957 clearop(cap->oap);
8958 nv_diffgetput(TRUE);
8960 else
8961 #endif
8962 clearopbeep(cap->oap);
8964 else
8966 dir = (cap->cmdchar == 'P'
8967 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
8968 ? BACKWARD : FORWARD;
8969 prep_redo_cmd(cap);
8970 if (cap->cmdchar == 'g')
8971 flags |= PUT_CURSEND;
8973 #ifdef FEAT_VISUAL
8974 if (VIsual_active)
8976 /* Putting in Visual mode: The put text replaces the selected
8977 * text. First delete the selected text, then put the new text.
8978 * Need to save and restore the registers that the delete
8979 * overwrites if the old contents is being put.
8981 was_visual = TRUE;
8982 regname = cap->oap->regname;
8983 # ifdef FEAT_CLIPBOARD
8984 adjust_clip_reg(&regname);
8985 # endif
8986 if (regname == 0 || VIM_ISDIGIT(regname)
8987 # ifdef FEAT_CLIPBOARD
8988 || (clip_unnamed && (regname == '*' || regname == '+'))
8989 # endif
8993 /* the delete is going to overwrite the register we want to
8994 * put, save it first. */
8995 reg1 = get_register(regname, TRUE);
8998 /* Now delete the selected text. */
8999 cap->cmdchar = 'd';
9000 cap->nchar = NUL;
9001 cap->oap->regname = NUL;
9002 nv_operator(cap);
9003 do_pending_operator(cap, 0, FALSE);
9004 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
9006 /* delete PUT_LINE_BACKWARD; */
9007 cap->oap->regname = regname;
9009 if (reg1 != NULL)
9011 /* Delete probably changed the register we want to put, save
9012 * it first. Then put back what was there before the delete. */
9013 reg2 = get_register(regname, FALSE);
9014 put_register(regname, reg1);
9017 /* When deleted a linewise Visual area, put the register as
9018 * lines to avoid it joined with the next line. When deletion was
9019 * characterwise, split a line when putting lines. */
9020 if (VIsual_mode == 'V')
9021 flags |= PUT_LINE;
9022 else if (VIsual_mode == 'v')
9023 flags |= PUT_LINE_SPLIT;
9024 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9025 flags |= PUT_LINE_FORWARD;
9026 dir = BACKWARD;
9027 if ((VIsual_mode != 'V'
9028 && curwin->w_cursor.col < curbuf->b_op_start.col)
9029 || (VIsual_mode == 'V'
9030 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9031 /* cursor is at the end of the line or end of file, put
9032 * forward. */
9033 dir = FORWARD;
9035 #endif
9036 do_put(cap->oap->regname, dir, cap->count1, flags);
9038 #ifdef FEAT_VISUAL
9039 /* If a register was saved, put it back now. */
9040 if (reg2 != NULL)
9041 put_register(regname, reg2);
9043 /* What to reselect with "gv"? Selecting the just put text seems to
9044 * be the most useful, since the original text was removed. */
9045 if (was_visual)
9047 curbuf->b_visual.vi_start = curbuf->b_op_start;
9048 curbuf->b_visual.vi_end = curbuf->b_op_end;
9051 /* When all lines were selected and deleted do_put() leaves an empty
9052 * line that needs to be deleted now. */
9053 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
9055 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
9057 /* If the cursor was in that line, move it to the end of the last
9058 * line. */
9059 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9061 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9062 coladvance((colnr_T)MAXCOL);
9065 #endif
9066 auto_format(FALSE, TRUE);
9071 * "o" and "O" commands.
9073 static void
9074 nv_open(cap)
9075 cmdarg_T *cap;
9077 #ifdef FEAT_DIFF
9078 /* "do" is ":diffget" */
9079 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9081 clearop(cap->oap);
9082 nv_diffgetput(FALSE);
9084 else
9085 #endif
9086 #ifdef FEAT_VISUAL
9087 if (VIsual_active) /* switch start and end of visual */
9088 v_swap_corners(cap->cmdchar);
9089 else
9090 #endif
9091 n_opencmd(cap);
9094 #ifdef FEAT_SNIFF
9095 /*ARGSUSED*/
9096 static void
9097 nv_sniff(cap)
9098 cmdarg_T *cap;
9100 ProcessSniffRequests();
9102 #endif
9104 #ifdef FEAT_NETBEANS_INTG
9105 static void
9106 nv_nbcmd(cap)
9107 cmdarg_T *cap;
9109 netbeans_keycommand(cap->nchar);
9111 #endif
9113 #ifdef FEAT_DND
9114 /*ARGSUSED*/
9115 static void
9116 nv_drop(cap)
9117 cmdarg_T *cap;
9119 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9121 #endif
9123 #ifdef FEAT_AUTOCMD
9125 * Trigger CursorHold event.
9126 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9127 * input buffer. "did_cursorhold" is set to avoid retriggering.
9129 /*ARGSUSED*/
9130 static void
9131 nv_cursorhold(cap)
9132 cmdarg_T *cap;
9134 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9135 did_cursorhold = TRUE;
9136 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
9138 #endif