blob: d3673181225024efb5c6ba58153461d25e3742f3 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
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.
13 */
14
15#include "vim.h"
16
17#ifdef FEAT_VISUAL
18/*
19 * The Visual area is remembered for reselection.
20 */
21static int resel_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
22static linenr_T resel_VIsual_line_count; /* number of lines */
23static colnr_T resel_VIsual_col; /* nr of cols or end col */
24
25static int restart_VIsual_select = 0;
26#endif
27
28static int
29# ifdef __BORLANDC__
30_RTLENTRYF
31# endif
32 nv_compare __ARGS((const void *s1, const void *s2));
33static int find_command __ARGS((int cmdchar));
34static void op_colon __ARGS((oparg_T *oap));
35#if defined(FEAT_MOUSE) && defined(FEAT_VISUAL)
36static void find_start_of_word __ARGS((pos_T *));
37static void find_end_of_word __ARGS((pos_T *));
38static int get_mouse_class __ARGS((char_u *p));
39#endif
40static void prep_redo_cmd __ARGS((cmdarg_T *cap));
41static void prep_redo __ARGS((int regname, long, int, int, int, int, int));
42static int checkclearop __ARGS((oparg_T *oap));
43static int checkclearopq __ARGS((oparg_T *oap));
44static void clearop __ARGS((oparg_T *oap));
45static void clearopbeep __ARGS((oparg_T *oap));
46#ifdef FEAT_VISUAL
47static void unshift_special __ARGS((cmdarg_T *cap));
48#endif
49#ifdef FEAT_CMDL_INFO
50static void del_from_showcmd __ARGS((int));
51#endif
52
53/*
54 * nv_*(): functions called to handle Normal and Visual mode commands.
55 * n_*(): functions called to handle Normal mode commands.
56 * v_*(): functions called to handle Visual mode commands.
57 */
58static void nv_ignore __ARGS((cmdarg_T *cap));
59static void nv_error __ARGS((cmdarg_T *cap));
60static void nv_help __ARGS((cmdarg_T *cap));
61static void nv_addsub __ARGS((cmdarg_T *cap));
62static void nv_page __ARGS((cmdarg_T *cap));
63static void nv_gd __ARGS((oparg_T *oap, int nchar));
64static int nv_screengo __ARGS((oparg_T *oap, int dir, long dist));
65#ifdef FEAT_MOUSE
66static void nv_mousescroll __ARGS((cmdarg_T *cap));
67static void nv_mouse __ARGS((cmdarg_T *cap));
68#endif
69static void nv_scroll_line __ARGS((cmdarg_T *cap));
70static void nv_zet __ARGS((cmdarg_T *cap));
71#ifdef FEAT_GUI
72static void nv_ver_scrollbar __ARGS((cmdarg_T *cap));
73static void nv_hor_scrollbar __ARGS((cmdarg_T *cap));
74#endif
75static void nv_exmode __ARGS((cmdarg_T *cap));
76static void nv_colon __ARGS((cmdarg_T *cap));
77static void nv_ctrlg __ARGS((cmdarg_T *cap));
78static void nv_ctrlh __ARGS((cmdarg_T *cap));
79static void nv_clear __ARGS((cmdarg_T *cap));
80static void nv_ctrlo __ARGS((cmdarg_T *cap));
81static void nv_hat __ARGS((cmdarg_T *cap));
82static void nv_Zet __ARGS((cmdarg_T *cap));
83static void nv_ident __ARGS((cmdarg_T *cap));
84#ifdef FEAT_VISUAL
85static int get_visual_text __ARGS((cmdarg_T *cap, char_u **pp, int *lenp));
86#endif
87static void nv_tagpop __ARGS((cmdarg_T *cap));
88static void nv_scroll __ARGS((cmdarg_T *cap));
89static void nv_right __ARGS((cmdarg_T *cap));
90static void nv_left __ARGS((cmdarg_T *cap));
91static void nv_up __ARGS((cmdarg_T *cap));
92static void nv_down __ARGS((cmdarg_T *cap));
93#ifdef FEAT_SEARCHPATH
94static void nv_gotofile __ARGS((cmdarg_T *cap));
95#endif
96static void nv_end __ARGS((cmdarg_T *cap));
97static void nv_dollar __ARGS((cmdarg_T *cap));
98static void nv_search __ARGS((cmdarg_T *cap));
99static void nv_next __ARGS((cmdarg_T *cap));
100static void normal_search __ARGS((cmdarg_T *cap, int dir, char_u *pat, int opt));
101static void nv_csearch __ARGS((cmdarg_T *cap));
102static void nv_brackets __ARGS((cmdarg_T *cap));
103static void nv_percent __ARGS((cmdarg_T *cap));
104static void nv_brace __ARGS((cmdarg_T *cap));
105static void nv_mark __ARGS((cmdarg_T *cap));
106static void nv_findpar __ARGS((cmdarg_T *cap));
107static void nv_undo __ARGS((cmdarg_T *cap));
108static void nv_kundo __ARGS((cmdarg_T *cap));
109static void nv_Replace __ARGS((cmdarg_T *cap));
110#ifdef FEAT_VREPLACE
111static void nv_vreplace __ARGS((cmdarg_T *cap));
112#endif
113#ifdef FEAT_VISUAL
114static void v_swap_corners __ARGS((int cmdchar));
115#endif
116static void nv_replace __ARGS((cmdarg_T *cap));
117static void n_swapchar __ARGS((cmdarg_T *cap));
118static void nv_cursormark __ARGS((cmdarg_T *cap, int flag, pos_T *pos));
119#ifdef FEAT_VISUAL
120static void v_visop __ARGS((cmdarg_T *cap));
121#endif
122static void nv_subst __ARGS((cmdarg_T *cap));
123static void nv_abbrev __ARGS((cmdarg_T *cap));
124static void nv_optrans __ARGS((cmdarg_T *cap));
125static void nv_gomark __ARGS((cmdarg_T *cap));
126static void nv_pcmark __ARGS((cmdarg_T *cap));
127static void nv_regname __ARGS((cmdarg_T *cap));
128#ifdef FEAT_VISUAL
129static void nv_visual __ARGS((cmdarg_T *cap));
130static void n_start_visual_mode __ARGS((int c));
131#endif
132static void nv_window __ARGS((cmdarg_T *cap));
133static void nv_suspend __ARGS((cmdarg_T *cap));
134static void nv_g_cmd __ARGS((cmdarg_T *cap));
135static void n_opencmd __ARGS((cmdarg_T *cap));
136static void nv_dot __ARGS((cmdarg_T *cap));
137static void nv_redo __ARGS((cmdarg_T *cap));
138static void nv_Undo __ARGS((cmdarg_T *cap));
139static void nv_tilde __ARGS((cmdarg_T *cap));
140static void nv_operator __ARGS((cmdarg_T *cap));
141static void nv_lineop __ARGS((cmdarg_T *cap));
142static void nv_home __ARGS((cmdarg_T *cap));
143static void nv_pipe __ARGS((cmdarg_T *cap));
144static void nv_bck_word __ARGS((cmdarg_T *cap));
145static void nv_wordcmd __ARGS((cmdarg_T *cap));
146static void nv_beginline __ARGS((cmdarg_T *cap));
147#ifdef FEAT_VISUAL
148static void adjust_for_sel __ARGS((cmdarg_T *cap));
149static int unadjust_for_sel __ARGS((void));
150static void nv_select __ARGS((cmdarg_T *cap));
151#endif
152static void nv_goto __ARGS((cmdarg_T *cap));
153static void nv_normal __ARGS((cmdarg_T *cap));
154static void nv_esc __ARGS((cmdarg_T *oap));
155static void nv_edit __ARGS((cmdarg_T *cap));
156static void invoke_edit __ARGS((cmdarg_T *cap, int repl, int cmd, int startln));
157#ifdef FEAT_TEXTOBJ
158static void nv_object __ARGS((cmdarg_T *cap));
159#endif
160static void nv_record __ARGS((cmdarg_T *cap));
161static void nv_at __ARGS((cmdarg_T *cap));
162static void nv_halfpage __ARGS((cmdarg_T *cap));
163static void nv_join __ARGS((cmdarg_T *cap));
164static void nv_put __ARGS((cmdarg_T *cap));
165static void nv_open __ARGS((cmdarg_T *cap));
166#ifdef FEAT_SNIFF
167static void nv_sniff __ARGS((cmdarg_T *cap));
168#endif
169#ifdef FEAT_NETBEANS_INTG
170static void nv_nbcmd __ARGS((cmdarg_T *cap));
171#endif
172#ifdef FEAT_DND
173static void nv_drop __ARGS((cmdarg_T *cap));
174#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000175#ifdef FEAT_AUTOCMD
176static void nv_cursorhold __ARGS((cmdarg_T *cap));
177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178
179/*
180 * Function to be called for a Normal or Visual mode command.
181 * The argument is a cmdarg_T.
182 */
183typedef void (*nv_func_T) __ARGS((cmdarg_T *cap));
184
185/* Values for cmd_flags. */
186#define NV_NCH 0x01 /* may need to get a second char */
187#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
188#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
189#define NV_LANG 0x08 /* second char needs language adjustment */
190
191#define NV_SS 0x10 /* may start selection */
192#define NV_SSS 0x20 /* may start selection with shift modifier */
193#define NV_STS 0x40 /* may stop selection without shift modif. */
194#define NV_RL 0x80 /* 'rightleft' modifies command */
195#define NV_KEEPREG 0x100 /* don't clear regname */
196#define NV_NCW 0x200 /* not allowed in command-line window */
197
198/*
199 * Generally speaking, every Normal mode command should either clear any
200 * pending operator (with *clearop*()), or set the motion type variable
201 * oap->motion_type.
202 *
203 * When a cursor motion command is made, it is marked as being a character or
204 * line oriented motion. Then, if an operator is in effect, the operation
205 * becomes character or line oriented accordingly.
206 */
207
208/*
209 * This table contains one entry for every Normal or Visual mode command.
210 * The order doesn't matter, init_normal_cmds() will create a sorted index.
211 * It is faster when all keys from zero to '~' are present.
212 */
213static const struct nv_cmd
214{
215 int cmd_char; /* (first) command character */
216 nv_func_T cmd_func; /* function for this command */
217 short_u cmd_flags; /* NV_ flags */
218 short cmd_arg; /* value for ca.arg */
219} nv_cmds[] =
220{
221 {NUL, nv_error, 0, 0},
222 {Ctrl_A, nv_addsub, 0, 0},
223 {Ctrl_B, nv_page, NV_STS, BACKWARD},
224 {Ctrl_C, nv_esc, 0, TRUE},
225 {Ctrl_D, nv_halfpage, 0, 0},
226 {Ctrl_E, nv_scroll_line, 0, TRUE},
227 {Ctrl_F, nv_page, NV_STS, FORWARD},
228 {Ctrl_G, nv_ctrlg, 0, 0},
229 {Ctrl_H, nv_ctrlh, 0, 0},
230 {Ctrl_I, nv_pcmark, 0, 0},
231 {NL, nv_down, 0, FALSE},
232 {Ctrl_K, nv_error, 0, 0},
233 {Ctrl_L, nv_clear, 0, 0},
234 {CAR, nv_down, 0, TRUE},
235 {Ctrl_N, nv_down, NV_STS, FALSE},
236 {Ctrl_O, nv_ctrlo, 0, 0},
237 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000238#ifdef FEAT_VISUAL
239 {Ctrl_Q, nv_visual, 0, FALSE},
240#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 {Ctrl_Q, nv_ignore, 0, 0},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 {Ctrl_R, nv_redo, 0, 0},
244 {Ctrl_S, nv_ignore, 0, 0},
245 {Ctrl_T, nv_tagpop, NV_NCW, 0},
246 {Ctrl_U, nv_halfpage, 0, 0},
247#ifdef FEAT_VISUAL
248 {Ctrl_V, nv_visual, 0, FALSE},
249 {'V', nv_visual, 0, FALSE},
250 {'v', nv_visual, 0, FALSE},
251#else
252 {Ctrl_V, nv_error, 0, 0},
253 {'V', nv_error, 0, 0},
254 {'v', nv_error, 0, 0},
255#endif
256 {Ctrl_W, nv_window, 0, 0},
257 {Ctrl_X, nv_addsub, 0, 0},
258 {Ctrl_Y, nv_scroll_line, 0, FALSE},
259 {Ctrl_Z, nv_suspend, 0, 0},
260 {ESC, nv_esc, 0, FALSE},
261 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
262 {Ctrl_RSB, nv_ident, NV_NCW, 0},
263 {Ctrl_HAT, nv_hat, NV_NCW, 0},
264 {Ctrl__, nv_error, 0, 0},
265 {' ', nv_right, 0, 0},
266 {'!', nv_operator, 0, 0},
267 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
268 {'#', nv_ident, 0, 0},
269 {'$', nv_dollar, 0, 0},
270 {'%', nv_percent, 0, 0},
271 {'&', nv_optrans, 0, 0},
272 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
273 {'(', nv_brace, 0, BACKWARD},
274 {')', nv_brace, 0, FORWARD},
275 {'*', nv_ident, 0, 0},
276 {'+', nv_down, 0, TRUE},
277 {',', nv_csearch, 0, TRUE},
278 {'-', nv_up, 0, TRUE},
279 {'.', nv_dot, NV_KEEPREG, 0},
280 {'/', nv_search, 0, FALSE},
281 {'0', nv_beginline, 0, 0},
282 {'1', nv_ignore, 0, 0},
283 {'2', nv_ignore, 0, 0},
284 {'3', nv_ignore, 0, 0},
285 {'4', nv_ignore, 0, 0},
286 {'5', nv_ignore, 0, 0},
287 {'6', nv_ignore, 0, 0},
288 {'7', nv_ignore, 0, 0},
289 {'8', nv_ignore, 0, 0},
290 {'9', nv_ignore, 0, 0},
291 {':', nv_colon, 0, 0},
292 {';', nv_csearch, 0, FALSE},
293 {'<', nv_operator, NV_RL, 0},
294 {'=', nv_operator, 0, 0},
295 {'>', nv_operator, NV_RL, 0},
296 {'?', nv_search, 0, FALSE},
297 {'@', nv_at, NV_NCH_NOP, FALSE},
298 {'A', nv_edit, 0, 0},
299 {'B', nv_bck_word, 0, 1},
300 {'C', nv_abbrev, NV_KEEPREG, 0},
301 {'D', nv_abbrev, NV_KEEPREG, 0},
302 {'E', nv_wordcmd, 0, TRUE},
303 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
304 {'G', nv_goto, 0, TRUE},
305 {'H', nv_scroll, 0, 0},
306 {'I', nv_edit, 0, 0},
307 {'J', nv_join, 0, 0},
308 {'K', nv_ident, 0, 0},
309 {'L', nv_scroll, 0, 0},
310 {'M', nv_scroll, 0, 0},
311 {'N', nv_next, 0, SEARCH_REV},
312 {'O', nv_open, 0, 0},
313 {'P', nv_put, 0, 0},
314 {'Q', nv_exmode, NV_NCW, 0},
315 {'R', nv_Replace, 0, FALSE},
316 {'S', nv_subst, NV_KEEPREG, 0},
317 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
318 {'U', nv_Undo, 0, 0},
319 {'W', nv_wordcmd, 0, TRUE},
320 {'X', nv_abbrev, NV_KEEPREG, 0},
321 {'Y', nv_abbrev, NV_KEEPREG, 0},
322 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
323 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
324 {'\\', nv_error, 0, 0},
325 {']', nv_brackets, NV_NCH_ALW, FORWARD},
326 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
327 {'_', nv_lineop, 0, 0},
328 {'`', nv_gomark, NV_NCH_ALW, FALSE},
329 {'a', nv_edit, NV_NCH, 0},
330 {'b', nv_bck_word, 0, 0},
331 {'c', nv_operator, 0, 0},
332 {'d', nv_operator, 0, 0},
333 {'e', nv_wordcmd, 0, FALSE},
334 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
335 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
336 {'h', nv_left, NV_RL, 0},
337 {'i', nv_edit, NV_NCH, 0},
338 {'j', nv_down, 0, FALSE},
339 {'k', nv_up, 0, FALSE},
340 {'l', nv_right, NV_RL, 0},
341 {'m', nv_mark, NV_NCH_NOP, 0},
342 {'n', nv_next, 0, 0},
343 {'o', nv_open, 0, 0},
344 {'p', nv_put, 0, 0},
345 {'q', nv_record, NV_NCH, 0},
346 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
347 {'s', nv_subst, NV_KEEPREG, 0},
348 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
349 {'u', nv_undo, 0, 0},
350 {'w', nv_wordcmd, 0, FALSE},
351 {'x', nv_abbrev, NV_KEEPREG, 0},
352 {'y', nv_operator, 0, 0},
353 {'z', nv_zet, NV_NCH_ALW, 0},
354 {'{', nv_findpar, 0, BACKWARD},
355 {'|', nv_pipe, 0, 0},
356 {'}', nv_findpar, 0, FORWARD},
357 {'~', nv_tilde, 0, 0},
358
359 /* pound sign */
360 {POUND, nv_ident, 0, 0},
361#ifdef FEAT_MOUSE
362 {K_MOUSEUP, nv_mousescroll, 0, TRUE},
363 {K_MOUSEDOWN, nv_mousescroll, 0, FALSE},
364 {K_LEFTMOUSE, nv_mouse, 0, 0},
365 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
366 {K_LEFTDRAG, nv_mouse, 0, 0},
367 {K_LEFTRELEASE, nv_mouse, 0, 0},
368 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
369 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
370 {K_MIDDLEDRAG, nv_mouse, 0, 0},
371 {K_MIDDLERELEASE, nv_mouse, 0, 0},
372 {K_RIGHTMOUSE, nv_mouse, 0, 0},
373 {K_RIGHTDRAG, nv_mouse, 0, 0},
374 {K_RIGHTRELEASE, nv_mouse, 0, 0},
375 {K_X1MOUSE, nv_mouse, 0, 0},
376 {K_X1DRAG, nv_mouse, 0, 0},
377 {K_X1RELEASE, nv_mouse, 0, 0},
378 {K_X2MOUSE, nv_mouse, 0, 0},
379 {K_X2DRAG, nv_mouse, 0, 0},
380 {K_X2RELEASE, nv_mouse, 0, 0},
381#endif
382 {K_IGNORE, nv_ignore, 0, 0},
383 {K_INS, nv_edit, 0, 0},
384 {K_KINS, nv_edit, 0, 0},
385 {K_BS, nv_ctrlh, 0, 0},
386 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000387 {K_XUP, nv_up, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 {K_S_UP, nv_page, NV_SS, BACKWARD},
389 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000390 {K_XDOWN, nv_down, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 {K_S_DOWN, nv_page, NV_SS, FORWARD},
392 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000393 {K_XLEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
395 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
396 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000397 {K_XRIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 {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_XEND, nv_end, NV_SSS|NV_STS, FALSE},
407 {K_S_END, nv_end, NV_SS, FALSE},
408 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
409 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
410 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
411 {K_XHOME, nv_home, NV_SSS|NV_STS, 0},
412 {K_S_HOME, nv_home, NV_SS, 0},
413 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
414 {K_DEL, nv_abbrev, 0, 0},
415 {K_KDEL, nv_abbrev, 0, 0},
416 {K_UNDO, nv_kundo, 0, 0},
417 {K_HELP, nv_help, NV_NCW, 0},
418 {K_F1, nv_help, NV_NCW, 0},
419 {K_XF1, nv_help, NV_NCW, 0},
420#ifdef FEAT_VISUAL
421 {K_SELECT, nv_select, 0, 0},
422#endif
423#ifdef FEAT_GUI
424 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
425 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
426#endif
427#ifdef FEAT_FKMAP
428 {K_F8, farsi_fkey, 0, 0},
429 {K_F9, farsi_fkey, 0, 0},
430#endif
431#ifdef FEAT_SNIFF
432 {K_SNIFF, nv_sniff, 0, 0},
433#endif
434#ifdef FEAT_NETBEANS_INTG
435 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
436#endif
437#ifdef FEAT_DND
438 {K_DROP, nv_drop, NV_STS, 0},
439#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000440#ifdef FEAT_AUTOCMD
441 {K_CURSORHOLD, nv_cursorhold, 0, 0},
442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443};
444
445/* Number of commands in nv_cmds[]. */
446#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
447
448/* Sorted index of commands in nv_cmds[]. */
449static short nv_cmd_idx[NV_CMDS_SIZE];
450
451/* The highest index for which
452 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
453static int nv_max_linear;
454
455/*
456 * Compare functions for qsort() below, that checks the command character
457 * through the index in nv_cmd_idx[].
458 */
459 static int
460#ifdef __BORLANDC__
461_RTLENTRYF
462#endif
463nv_compare(s1, s2)
464 const void *s1;
465 const void *s2;
466{
467 int c1, c2;
468
469 /* The commands are sorted on absolute value. */
470 c1 = nv_cmds[*(const short *)s1].cmd_char;
471 c2 = nv_cmds[*(const short *)s2].cmd_char;
472 if (c1 < 0)
473 c1 = -c1;
474 if (c2 < 0)
475 c2 = -c2;
476 return c1 - c2;
477}
478
479/*
480 * Initialize the nv_cmd_idx[] table.
481 */
482 void
483init_normal_cmds()
484{
485 int i;
486
487 /* Fill the index table with a one to one relation. */
488 for (i = 0; i < NV_CMDS_SIZE; ++i)
489 nv_cmd_idx[i] = i;
490
491 /* Sort the commands by the command character. */
492 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
493
494 /* Find the first entry that can't be indexed by the command character. */
495 for (i = 0; i < NV_CMDS_SIZE; ++i)
496 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
497 break;
498 nv_max_linear = i - 1;
499}
500
501/*
502 * Search for a command in the commands table.
503 * Returns -1 for invalid command.
504 */
505 static int
506find_command(cmdchar)
507 int cmdchar;
508{
509 int i;
510 int idx;
511 int top, bot;
512 int c;
513
514#ifdef FEAT_MBYTE
515 /* A multi-byte character is never a command. */
516 if (cmdchar >= 0x100)
517 return -1;
518#endif
519
520 /* We use the absolute value of the character. Special keys have a
521 * negative value, but are sorted on their absolute value. */
522 if (cmdchar < 0)
523 cmdchar = -cmdchar;
524
525 /* If the character is in the first part: The character is the index into
526 * nv_cmd_idx[]. */
527 if (cmdchar <= nv_max_linear)
528 return nv_cmd_idx[cmdchar];
529
530 /* Perform a binary search. */
531 bot = nv_max_linear + 1;
532 top = NV_CMDS_SIZE - 1;
533 idx = -1;
534 while (bot <= top)
535 {
536 i = (top + bot) / 2;
537 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
538 if (c < 0)
539 c = -c;
540 if (cmdchar == c)
541 {
542 idx = nv_cmd_idx[i];
543 break;
544 }
545 if (cmdchar > c)
546 bot = i + 1;
547 else
548 top = i - 1;
549 }
550 return idx;
551}
552
553/*
554 * Execute a command in Normal mode.
555 */
556/*ARGSUSED*/
557 void
558normal_cmd(oap, toplevel)
559 oparg_T *oap;
560 int toplevel; /* TRUE when called from main() */
561{
562 static long opcount = 0; /* ca.opcount saved here */
563 cmdarg_T ca; /* command arguments */
564 int c;
565 int ctrl_w = FALSE; /* got CTRL-W command */
566 int old_col = curwin->w_curswant;
567#ifdef FEAT_CMDL_INFO
568 int need_flushbuf; /* need to call out_flush() */
569#endif
570#ifdef FEAT_VISUAL
571 pos_T old_pos; /* cursor position before command */
572 int mapped_len;
573#endif
574 static int old_mapped_len = 0;
575 int idx;
576
577 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
578 ca.oap = oap;
579 ca.opcount = opcount;
580
581#ifdef FEAT_SNIFF
582 want_sniff_request = sniff_connected;
583#endif
584
585 /*
586 * If there is an operator pending, then the command we take this time
587 * will terminate it. Finish_op tells us to finish the operation before
588 * returning this time (unless the operation was cancelled).
589 */
590#ifdef CURSOR_SHAPE
591 c = finish_op;
592#endif
593 finish_op = (oap->op_type != OP_NOP);
594#ifdef CURSOR_SHAPE
595 if (finish_op != c)
596 {
597 ui_cursor_shape(); /* may show different cursor shape */
598# ifdef FEAT_MOUSESHAPE
599 update_mouseshape(-1);
600# endif
601 }
602#endif
603
604 if (!finish_op && !oap->regname)
605 ca.opcount = 0;
606
607#ifdef FEAT_VISUAL
608 mapped_len = typebuf_maplen();
609#endif
610
611 State = NORMAL_BUSY;
612#ifdef USE_ON_FLY_SCROLL
613 dont_scroll = FALSE; /* allow scrolling here */
614#endif
615
616 /*
617 * Get the command character from the user.
618 */
619 c = safe_vgetc();
620
621#ifdef FEAT_LANGMAP
622 LANGMAP_ADJUST(c, TRUE);
623#endif
624
625 /*
626 * If a mapping was started in Visual or Select mode, remember the length
627 * of the mapping. This is used below to not return to Insert mode for as
628 * long as the mapping is being executed.
629 */
630 if (restart_edit == 0)
631 old_mapped_len = 0;
632 else if (old_mapped_len
633#ifdef FEAT_VISUAL
634 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0)
635#endif
636 )
637 old_mapped_len = typebuf_maplen();
638
639 if (c == NUL)
640 c = K_ZERO;
641
642#ifdef FEAT_VISUAL
643 /*
644 * In Select mode, typed text replaces the selection.
645 */
646 if (VIsual_active
647 && VIsual_select
648 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
649 {
650# ifdef FEAT_MBYTE
651 char_u buf[MB_MAXBYTES + 1];
652
653 buf[(*mb_char2bytes)(c, buf)] = NUL;
654# else
655 char_u buf[2];
656
657 buf[0] = c;
658 buf[1] = NUL;
659# endif
660 /* Fake a "c"hange command.
661 * Insert the typed character in the typeahead buffer, so that it will
662 * be mapped in Insert mode. Required for ":lmap" to work. May cause
663 * mapping a character from ":vnoremap"... */
664 (void)ins_typebuf(buf, REMAP_YES, 0, !KeyTyped, FALSE);
665 c = 'c';
666 }
667#endif
668
669#ifdef FEAT_CMDL_INFO
670 need_flushbuf = add_to_showcmd(c);
671#endif
672
673getcount:
674#ifdef FEAT_VISUAL
675 if (!(VIsual_active && VIsual_select))
676#endif
677 {
678 /*
679 * Handle a count before a command and compute ca.count0.
680 * Note that '0' is a command and not the start of a count, but it's
681 * part of a count after other digits.
682 */
683 while ( (c >= '1' && c <= '9')
684 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
685 {
686 if (c == K_DEL || c == K_KDEL)
687 {
688 ca.count0 /= 10;
689#ifdef FEAT_CMDL_INFO
690 del_from_showcmd(4); /* delete the digit and ~@% */
691#endif
692 }
693 else
694 ca.count0 = ca.count0 * 10 + (c - '0');
695 if (ca.count0 < 0) /* got too large! */
696 ca.count0 = 999999999L;
697 if (ctrl_w)
698 {
699 ++no_mapping;
700 ++allow_keys; /* no mapping for nchar, but keys */
701 }
702 ++no_zero_mapping; /* don't map zero here */
703 c = safe_vgetc();
704#ifdef FEAT_LANGMAP
705 LANGMAP_ADJUST(c, TRUE);
706#endif
707 --no_zero_mapping;
708 if (ctrl_w)
709 {
710 --no_mapping;
711 --allow_keys;
712 }
713#ifdef FEAT_CMDL_INFO
714 need_flushbuf |= add_to_showcmd(c);
715#endif
716 }
717
718 /*
719 * If we got CTRL-W there may be a/another count
720 */
721 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
722 {
723 ctrl_w = TRUE;
724 ca.opcount = ca.count0; /* remember first count */
725 ca.count0 = 0;
726 ++no_mapping;
727 ++allow_keys; /* no mapping for nchar, but keys */
728 c = safe_vgetc(); /* get next character */
729#ifdef FEAT_LANGMAP
730 LANGMAP_ADJUST(c, TRUE);
731#endif
732 --no_mapping;
733 --allow_keys;
734#ifdef FEAT_CMDL_INFO
735 need_flushbuf |= add_to_showcmd(c);
736#endif
737 goto getcount; /* jump back */
738 }
739 }
740
741 /*
742 * If we're in the middle of an operator (including after entering a yank
743 * buffer with '"') AND we had a count before the operator, then that
744 * count overrides the current value of ca.count0.
745 * What this means effectively, is that commands like "3dw" get turned
746 * into "d3w" which makes things fall into place pretty neatly.
747 * If you give a count before AND after the operator, they are multiplied.
748 */
749 if (ca.opcount != 0)
750 {
751 if (ca.count0)
752 ca.count0 *= ca.opcount;
753 else
754 ca.count0 = ca.opcount;
755 }
756
757 /*
758 * Always remember the count. It will be set to zero (on the next call,
759 * above) when there is no pending operator.
760 * When called from main(), save the count for use by the "count" built-in
761 * variable.
762 */
763 ca.opcount = ca.count0;
764 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
765
766#ifdef FEAT_EVAL
767 /*
768 * Only set v:count when called from main() and not a stuffed command.
769 */
770 if (toplevel && stuff_empty())
771 set_vcount(ca.count0, ca.count1);
772#endif
773
774 /*
775 * Find the command character in the table of commands.
776 * For CTRL-W we already got nchar when looking for a count.
777 */
778 if (ctrl_w)
779 {
780 ca.nchar = c;
781 ca.cmdchar = Ctrl_W;
782 }
783 else
784 ca.cmdchar = c;
785 idx = find_command(ca.cmdchar);
786 if (idx < 0)
787 {
788 /* Not a known command: beep. */
789 clearopbeep(oap);
790 goto normal_end;
791 }
792#ifdef FEAT_CMDWIN
793 if (cmdwin_type != 0 && (nv_cmds[idx].cmd_flags & NV_NCW))
794 {
795 /* This command is not allowed in the cmdline window: beep. */
796 clearopbeep(oap);
797 EMSG(_(e_cmdwin));
798 goto normal_end;
799 }
800#endif
801
802#ifdef FEAT_VISUAL
803 /*
804 * In Visual/Select mode, a few keys are handled in a special way.
805 */
806 if (VIsual_active)
807 {
808 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
809 if (km_stopsel
810 && (nv_cmds[idx].cmd_flags & NV_STS)
811 && !(mod_mask & MOD_MASK_SHIFT))
812 {
813 end_visual_mode();
814 redraw_curbuf_later(INVERTED);
815 }
816
817 /* Keys that work different when 'keymodel' contains "startsel" */
818 if (km_startsel)
819 {
820 if (nv_cmds[idx].cmd_flags & NV_SS)
821 {
822 unshift_special(&ca);
823 idx = find_command(ca.cmdchar);
824 }
825 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
826 && (mod_mask & MOD_MASK_SHIFT))
827 {
828 mod_mask &= ~MOD_MASK_SHIFT;
829 }
830 }
831 }
832#endif
833
834#ifdef FEAT_RIGHTLEFT
835 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
836 && (nv_cmds[idx].cmd_flags & NV_RL))
837 {
838 /* Invert horizontal movements and operations. Only when typed by the
839 * user directly, not when the result of a mapping or "x" translated
840 * to "dl". */
841 switch (ca.cmdchar)
842 {
843 case 'l': ca.cmdchar = 'h'; break;
844 case K_RIGHT: ca.cmdchar = K_LEFT; break;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000845 case K_XRIGHT: ca.cmdchar = K_XLEFT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
847 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
848 case 'h': ca.cmdchar = 'l'; break;
849 case K_LEFT: ca.cmdchar = K_RIGHT; break;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000850 case K_XLEFT: ca.cmdchar = K_XRIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
852 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
853 case '>': ca.cmdchar = '<'; break;
854 case '<': ca.cmdchar = '>'; break;
855 }
856 idx = find_command(ca.cmdchar);
857 }
858#endif
859
860 /*
861 * Get an additional character if we need one.
862 */
863 if ((nv_cmds[idx].cmd_flags & NV_NCH)
864 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
865 && oap->op_type == OP_NOP)
866 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
867 || (ca.cmdchar == 'q'
868 && oap->op_type == OP_NOP
869 && !Recording
870 && !Exec_reg)
871 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
872 && (oap->op_type != OP_NOP
873#ifdef FEAT_VISUAL
874 || VIsual_active
875#endif
876 ))))
877 {
878 int *cp;
879 int repl = FALSE; /* get character for replace mode */
880 int lit = FALSE; /* get extra character literally */
881 int langmap_active = FALSE; /* using :lmap mappings */
882 int lang; /* getting a text character */
883#ifdef USE_IM_CONTROL
884 int save_smd; /* saved value of p_smd */
885#endif
886
887 ++no_mapping;
888 ++allow_keys; /* no mapping for nchar, but allow key codes */
889 if (ca.cmdchar == 'g')
890 {
891 /*
892 * For 'g' get the next character now, so that we can check for
893 * "gr", "g'" and "g`".
894 */
895 ca.nchar = safe_vgetc();
896#ifdef FEAT_LANGMAP
897 LANGMAP_ADJUST(ca.nchar, TRUE);
898#endif
899#ifdef FEAT_CMDL_INFO
900 need_flushbuf |= add_to_showcmd(ca.nchar);
901#endif
902 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
903 || ca.nchar == Ctrl_BSL)
904 {
905 cp = &ca.extra_char; /* need to get a third character */
906 if (ca.nchar != 'r')
907 lit = TRUE; /* get it literally */
908 else
909 repl = TRUE; /* get it in replace mode */
910 }
911 else
912 cp = NULL; /* no third character needed */
913 }
914 else
915 {
916 if (ca.cmdchar == 'r') /* get it in replace mode */
917 repl = TRUE;
918 cp = &ca.nchar;
919 }
920 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
921
922 /*
923 * Get a second or third character.
924 */
925 if (cp != NULL)
926 {
927#ifdef CURSOR_SHAPE
928 if (repl)
929 {
930 State = REPLACE; /* pretend Replace mode */
931 ui_cursor_shape(); /* show different cursor shape */
932 }
933#endif
934 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
935 {
936 /* Allow mappings defined with ":lmap". */
937 --no_mapping;
938 --allow_keys;
939 if (repl)
940 State = LREPLACE;
941 else
942 State = LANGMAP;
943 langmap_active = TRUE;
944 }
945#ifdef USE_IM_CONTROL
946 save_smd = p_smd;
947 p_smd = FALSE; /* Don't let the IM code show the mode here */
948 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
949 im_set_active(TRUE);
950#endif
951
952 *cp = safe_vgetc();
953
954 if (langmap_active)
955 {
956 /* Undo the decrement done above */
957 ++no_mapping;
958 ++allow_keys;
959 State = NORMAL_BUSY;
960 }
961#ifdef USE_IM_CONTROL
962 if (lang)
963 {
964 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
965 im_save_status(&curbuf->b_p_iminsert);
966 im_set_active(FALSE);
967 }
968 p_smd = save_smd;
969#endif
970#ifdef CURSOR_SHAPE
971 State = NORMAL_BUSY;
972#endif
973#ifdef FEAT_CMDL_INFO
974 need_flushbuf |= add_to_showcmd(*cp);
975#endif
976
977 if (!lit)
978 {
979#ifdef FEAT_DIGRAPHS
980 /* Typing CTRL-K gets a digraph. */
981 if (*cp == Ctrl_K
982 && ((nv_cmds[idx].cmd_flags & NV_LANG)
983 || cp == &ca.extra_char)
984 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
985 {
986 c = get_digraph(FALSE);
987 if (c > 0)
988 {
989 *cp = c;
990# ifdef FEAT_CMDL_INFO
991 /* Guessing how to update showcmd here... */
992 del_from_showcmd(3);
993 need_flushbuf |= add_to_showcmd(*cp);
994# endif
995 }
996 }
997#endif
998
999#ifdef FEAT_LANGMAP
1000 /* adjust chars > 127, except after "tTfFr" commands */
1001 LANGMAP_ADJUST(*cp, !lang);
1002#endif
1003#ifdef FEAT_RIGHTLEFT
1004 /* adjust Hebrew mapped char */
1005 if (p_hkmap && lang && KeyTyped)
1006 *cp = hkmap(*cp);
1007# ifdef FEAT_FKMAP
1008 /* adjust Farsi mapped char */
1009 if (p_fkmap && lang && KeyTyped)
1010 *cp = fkmap(*cp);
1011# endif
1012#endif
1013 }
1014
1015 /*
1016 * When the next character is CTRL-\ a following CTRL-N means the
1017 * command is aborted and we go to Normal mode.
1018 */
1019 if (cp == &ca.extra_char
1020 && ca.nchar == Ctrl_BSL
1021 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1022 {
1023 ca.cmdchar = Ctrl_BSL;
1024 ca.nchar = ca.extra_char;
1025 idx = find_command(ca.cmdchar);
1026 }
1027 else if (*cp == Ctrl_BSL)
1028 {
1029 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1030
1031 /* There is a busy wait here when typing "f<C-\>" and then
1032 * something different from CTRL-N. Can't be avoided. */
1033 while ((c = vpeekc()) <= 0 && towait > 0L)
1034 {
1035 do_sleep(towait > 50L ? 50L : towait);
1036 towait -= 50L;
1037 }
1038 if (c > 0)
1039 {
1040 c = safe_vgetc();
1041 if (c != Ctrl_N && c != Ctrl_G)
1042 vungetc(c);
1043 else
1044 {
1045 ca.cmdchar = Ctrl_BSL;
1046 ca.nchar = c;
1047 idx = find_command(ca.cmdchar);
1048 }
1049 }
1050 }
1051
1052#ifdef FEAT_MBYTE
1053 /* When getting a text character and the next character is a
1054 * multi-byte character, it could be a composing character.
1055 * However, don't wait for it to arrive. */
1056 while (enc_utf8 && lang && (c = vpeekc()) > 0
1057 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1058 {
1059 c = safe_vgetc();
1060 if (!utf_iscomposing(c))
1061 {
1062 vungetc(c); /* it wasn't, put it back */
1063 break;
1064 }
1065 else if (ca.ncharC1 == 0)
1066 ca.ncharC1 = c;
1067 else
1068 ca.ncharC2 = c;
1069 }
1070#endif
1071 }
1072 --no_mapping;
1073 --allow_keys;
1074 }
1075
1076#ifdef FEAT_CMDL_INFO
1077 /*
1078 * Flush the showcmd characters onto the screen so we can see them while
1079 * the command is being executed. Only do this when the shown command was
1080 * actually displayed, otherwise this will slow down a lot when executing
1081 * mappings.
1082 */
1083 if (need_flushbuf)
1084 out_flush();
1085#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00001086#ifdef FEAT_AUTOCMD
1087 did_cursorhold = FALSE;
1088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089
1090 State = NORMAL;
1091
1092 if (ca.nchar == ESC)
1093 {
1094 clearop(oap);
1095 if (restart_edit == 0 && goto_im())
1096 restart_edit = 'a';
1097 goto normal_end;
1098 }
1099
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001100 if (ca.cmdchar != K_IGNORE)
1101 {
1102 msg_didout = FALSE; /* don't scroll screen up for normal command */
1103 msg_col = 0;
1104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105
1106#ifdef FEAT_VISUAL
1107 old_pos = curwin->w_cursor; /* remember where cursor was */
1108
1109 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1110 * mode. */
1111 if (!VIsual_active && km_startsel)
1112 {
1113 if (nv_cmds[idx].cmd_flags & NV_SS)
1114 {
1115 start_selection();
1116 unshift_special(&ca);
1117 idx = find_command(ca.cmdchar);
1118 }
1119 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1120 && (mod_mask & MOD_MASK_SHIFT))
1121 {
1122 start_selection();
1123 mod_mask &= ~MOD_MASK_SHIFT;
1124 }
1125 }
1126#endif
1127
1128 /*
1129 * Execute the command!
1130 * Call the command function found in the commands table.
1131 */
1132 ca.arg = nv_cmds[idx].cmd_arg;
1133 (nv_cmds[idx].cmd_func)(&ca);
1134
1135 /*
1136 * If we didn't start or finish an operator, reset oap->regname, unless we
1137 * need it later.
1138 */
1139 if (!finish_op
1140 && !oap->op_type
1141 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1142 {
1143 clearop(oap);
1144#ifdef FEAT_EVAL
1145 set_reg_var('"');
1146#endif
1147 }
1148
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001149 /* get the length of mapped chars again after typing a count, second
1150 * character or "z333<cr>". */
1151 if (old_mapped_len > 0)
1152 old_mapped_len = typebuf_maplen();
1153
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 /*
1155 * If an operation is pending, handle it...
1156 */
1157 do_pending_operator(&ca, old_col, FALSE);
1158
1159 /*
1160 * Wait for a moment when a message is displayed that will be overwritten
1161 * by the mode message.
1162 * In Visual mode and with "^O" in Insert mode, a short message will be
1163 * overwritten by the mode message. Wait a bit, until a key is hit.
1164 * In Visual mode, it's more important to keep the Visual area updated
1165 * than keeping a message (e.g. from a /pat search).
1166 * Only do this if the command was typed, not from a mapping.
1167 * Don't wait when emsg_silent is non-zero.
1168 * Also wait a bit after an error message, e.g. for "^O:".
1169 * Don't redraw the screen, it would remove the message.
1170 */
1171 if ( ((p_smd
1172 && (restart_edit != 0
1173#ifdef FEAT_VISUAL
1174 || (VIsual_active
1175 && old_pos.lnum == curwin->w_cursor.lnum
1176 && old_pos.col == curwin->w_cursor.col)
1177#endif
1178 )
1179 && (clear_cmdline
1180 || redraw_cmdline)
1181 && (msg_didout || (msg_didany && msg_scroll))
1182 && !msg_nowait
1183 && KeyTyped)
1184 || (restart_edit != 0
1185#ifdef FEAT_VISUAL
1186 && !VIsual_active
1187#endif
1188 && (msg_scroll
1189 || emsg_on_display)))
1190 && oap->regname == 0
1191 && !(ca.retval & CA_COMMAND_BUSY)
1192 && stuff_empty()
1193 && typebuf_typed()
1194 && emsg_silent == 0
1195 && !did_wait_return
1196 && oap->op_type == OP_NOP)
1197 {
1198 int save_State = State;
1199
1200 /* Draw the cursor with the right shape here */
1201 if (restart_edit != 0)
1202 State = INSERT;
1203
1204 /* If need to redraw, and there is a "keep_msg", redraw before the
1205 * delay */
1206 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1207 {
1208 char_u *kmsg;
1209
1210 kmsg = keep_msg;
1211 keep_msg = NULL;
1212 /* showmode() will clear keep_msg, but we want to use it anyway */
1213 update_screen(0);
1214 /* now reset it, otherwise it's put in the history again */
1215 keep_msg = kmsg;
1216 msg_attr(kmsg, keep_msg_attr);
1217 vim_free(kmsg);
1218 }
1219 setcursor();
1220 cursor_on();
1221 out_flush();
1222 if (msg_scroll || emsg_on_display)
1223 ui_delay(1000L, TRUE); /* wait at least one second */
1224 ui_delay(3000L, FALSE); /* wait up to three seconds */
1225 State = save_State;
1226
1227 msg_scroll = FALSE;
1228 emsg_on_display = FALSE;
1229 }
1230
1231 /*
1232 * Finish up after executing a Normal mode command.
1233 */
1234normal_end:
1235
1236 msg_nowait = FALSE;
1237
1238 /* Reset finish_op, in case it was set */
1239#ifdef CURSOR_SHAPE
1240 c = finish_op;
1241#endif
1242 finish_op = FALSE;
1243#ifdef CURSOR_SHAPE
1244 /* Redraw the cursor with another shape, if we were in Operator-pending
1245 * mode or did a replace command. */
1246 if (c || ca.cmdchar == 'r')
1247 {
1248 ui_cursor_shape(); /* may show different cursor shape */
1249# ifdef FEAT_MOUSESHAPE
1250 update_mouseshape(-1);
1251# endif
1252 }
1253#endif
1254
1255#ifdef FEAT_CMDL_INFO
1256 if (oap->op_type == OP_NOP && oap->regname == 0)
1257 clear_showcmd();
1258#endif
1259
1260 checkpcmark(); /* check if we moved since setting pcmark */
1261 vim_free(ca.searchbuf);
1262
1263#ifdef FEAT_MBYTE
1264 if (has_mbyte)
1265 mb_adjust_cursor();
1266#endif
1267
1268#ifdef FEAT_SCROLLBIND
1269 if (curwin->w_p_scb && toplevel)
1270 {
1271 validate_cursor(); /* may need to update w_leftcol */
1272 do_check_scrollbind(TRUE);
1273 }
1274#endif
1275
1276 /*
1277 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1278 * if still inside a mapping that started in Visual mode).
1279 * May switch from Visual to Select mode after CTRL-O command.
1280 */
1281 if ( oap->op_type == OP_NOP
1282#ifdef FEAT_VISUAL
1283 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1284 || restart_VIsual_select == 1)
1285#endif
1286 && !(ca.retval & CA_COMMAND_BUSY)
1287 && stuff_empty()
1288 && oap->regname == 0)
1289 {
1290#ifdef FEAT_VISUAL
1291 if (restart_VIsual_select == 1)
1292 {
1293 VIsual_select = TRUE;
1294 showmode();
1295 restart_VIsual_select = 0;
1296 }
1297#endif
1298 if (restart_edit != 0
1299#ifdef FEAT_VISUAL
1300 && !VIsual_active
1301#endif
1302 && old_mapped_len == 0)
1303 (void)edit(restart_edit, FALSE, 1L);
1304 }
1305
1306#ifdef FEAT_VISUAL
1307 if (restart_VIsual_select == 2)
1308 restart_VIsual_select = 1;
1309#endif
1310
1311 /* Save count before an operator for next time. */
1312 opcount = ca.opcount;
1313}
1314
1315/*
1316 * Handle an operator after visual mode or when the movement is finished
1317 */
1318 void
1319do_pending_operator(cap, old_col, gui_yank)
1320 cmdarg_T *cap;
1321 int old_col;
1322 int gui_yank;
1323{
1324 oparg_T *oap = cap->oap;
1325 pos_T old_cursor;
1326 int empty_region_error;
1327 int restart_edit_save;
1328
1329#ifdef FEAT_VISUAL
1330 /* The visual area is remembered for redo */
1331 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1332 static linenr_T redo_VIsual_line_count; /* number of lines */
1333 static colnr_T redo_VIsual_col; /* number of cols or end column */
1334 static long redo_VIsual_count; /* count for Visual operator */
1335# ifdef FEAT_VIRTUALEDIT
1336 int include_line_break = FALSE;
1337# endif
1338#endif
1339
1340#if defined(FEAT_CLIPBOARD)
1341 /*
1342 * Yank the visual area into the GUI selection register before we operate
1343 * on it and lose it forever.
1344 * Don't do it if a specific register was specified, so that ""x"*P works.
1345 * This could call do_pending_operator() recursively, but that's OK
1346 * because gui_yank will be TRUE for the nested call.
1347 */
1348 if (clip_star.available
1349 && oap->op_type != OP_NOP
1350 && !gui_yank
1351# ifdef FEAT_VISUAL
1352 && VIsual_active
1353 && !redo_VIsual_busy
1354# endif
1355 && oap->regname == 0)
1356 clip_auto_select();
1357#endif
1358 old_cursor = curwin->w_cursor;
1359
1360 /*
1361 * If an operation is pending, handle it...
1362 */
1363 if ((finish_op
1364#ifdef FEAT_VISUAL
1365 || VIsual_active
1366#endif
1367 ) && oap->op_type != OP_NOP)
1368 {
1369#ifdef FEAT_VISUAL
1370 oap->is_VIsual = VIsual_active;
1371 if (oap->motion_force == 'V')
1372 oap->motion_type = MLINE;
1373 else if (oap->motion_force == 'v')
1374 {
1375 /* If the motion was linewise, "inclusive" will not have been set.
1376 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1377 if (oap->motion_type == MLINE)
1378 oap->inclusive = FALSE;
1379 /* If the motion already was characterwise, toggle "inclusive" */
1380 else if (oap->motion_type == MCHAR)
1381 oap->inclusive = !oap->inclusive;
1382 oap->motion_type = MCHAR;
1383 }
1384 else if (oap->motion_force == Ctrl_V)
1385 {
1386 /* Change line- or characterwise motion into Visual block mode. */
1387 VIsual_active = TRUE;
1388 VIsual = oap->start;
1389 VIsual_mode = Ctrl_V;
1390 VIsual_select = FALSE;
1391 VIsual_reselect = FALSE;
1392 }
1393#endif
1394
1395 /* only redo yank when 'y' flag is in 'cpoptions' */
1396 /* never redo "zf" (define fold) */
1397 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1398#ifdef FEAT_VISUAL
1399 && (!VIsual_active || oap->motion_force)
1400#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001401 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402#ifdef FEAT_FOLDING
1403 && oap->op_type != OP_FOLD
1404 && oap->op_type != OP_FOLDOPEN
1405 && oap->op_type != OP_FOLDOPENREC
1406 && oap->op_type != OP_FOLDCLOSE
1407 && oap->op_type != OP_FOLDCLOSEREC
1408 && oap->op_type != OP_FOLDDEL
1409 && oap->op_type != OP_FOLDDELREC
1410#endif
1411 )
1412 {
1413 prep_redo(oap->regname, cap->count0,
1414 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1415 oap->motion_force, cap->cmdchar, cap->nchar);
1416 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1417 {
1418 /*
1419 * If 'cpoptions' does not contain 'r', insert the search
1420 * pattern to really repeat the same command.
1421 */
1422 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
1423 AppendToRedobuffLit(cap->searchbuf);
1424 AppendToRedobuff(NL_STR);
1425 }
1426 else if (cap->cmdchar == ':')
1427 {
1428 /* do_cmdline() has stored the first typed line in
1429 * "repeat_cmdline". When several lines are typed repeating
1430 * won't be possible. */
1431 if (repeat_cmdline == NULL)
1432 ResetRedobuff();
1433 else
1434 {
1435 AppendToRedobuffLit(repeat_cmdline);
1436 AppendToRedobuff(NL_STR);
1437 vim_free(repeat_cmdline);
1438 repeat_cmdline = NULL;
1439 }
1440 }
1441 }
1442
1443#ifdef FEAT_VISUAL
1444 if (redo_VIsual_busy)
1445 {
1446 oap->start = curwin->w_cursor;
1447 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1448 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1449 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1450 VIsual_mode = redo_VIsual_mode;
1451 if (VIsual_mode == 'v')
1452 {
1453 if (redo_VIsual_line_count <= 1)
1454 curwin->w_cursor.col += redo_VIsual_col - 1;
1455 else
1456 curwin->w_cursor.col = redo_VIsual_col;
1457 }
1458 if (redo_VIsual_col == MAXCOL)
1459 {
1460 curwin->w_curswant = MAXCOL;
1461 coladvance((colnr_T)MAXCOL);
1462 }
1463 cap->count0 = redo_VIsual_count;
1464 if (redo_VIsual_count != 0)
1465 cap->count1 = redo_VIsual_count;
1466 else
1467 cap->count1 = 1;
1468 }
1469 else if (VIsual_active)
1470 {
1471 /* Save the current VIsual area for '< and '> marks, and "gv" */
1472 curbuf->b_visual_start = VIsual;
1473 curbuf->b_visual_end = curwin->w_cursor;
1474 curbuf->b_visual_mode = VIsual_mode;
1475# ifdef FEAT_EVAL
1476 curbuf->b_visual_mode_eval = VIsual_mode;
1477# endif
1478 curbuf->b_visual_curswant = curwin->w_curswant;
1479
1480 /* In Select mode, a linewise selection is operated upon like a
1481 * characterwise selection. */
1482 if (VIsual_select && VIsual_mode == 'V')
1483 {
1484 if (lt(VIsual, curwin->w_cursor))
1485 {
1486 VIsual.col = 0;
1487 curwin->w_cursor.col =
1488 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1489 }
1490 else
1491 {
1492 curwin->w_cursor.col = 0;
1493 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1494 }
1495 VIsual_mode = 'v';
1496 }
1497 /* If 'selection' is "exclusive", backup one character for
1498 * charwise selections. */
1499 else if (VIsual_mode == 'v')
1500 {
1501# ifdef FEAT_VIRTUALEDIT
1502 include_line_break =
1503# endif
1504 unadjust_for_sel();
1505 }
1506
1507 oap->start = VIsual;
1508 if (VIsual_mode == 'V')
1509 oap->start.col = 0;
1510 }
1511#endif /* FEAT_VISUAL */
1512
1513 /*
1514 * Set oap->start to the first position of the operated text, oap->end
1515 * to the end of the operated text. w_cursor is equal to oap->start.
1516 */
1517 if (lt(oap->start, curwin->w_cursor))
1518 {
1519#ifdef FEAT_FOLDING
1520 /* Include folded lines completely. */
1521 if (!VIsual_active)
1522 {
1523 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1524 oap->start.col = 0;
1525 if (hasFolding(curwin->w_cursor.lnum, NULL,
1526 &curwin->w_cursor.lnum))
1527 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1528 }
1529#endif
1530 oap->end = curwin->w_cursor;
1531 curwin->w_cursor = oap->start;
1532
1533 /* w_virtcol may have been updated; if the cursor goes back to its
1534 * previous position w_virtcol becomes invalid and isn't updated
1535 * automatically. */
1536 curwin->w_valid &= ~VALID_VIRTCOL;
1537 }
1538 else
1539 {
1540#ifdef FEAT_FOLDING
1541 /* Include folded lines completely. */
1542 if (!VIsual_active && oap->motion_type == MLINE)
1543 {
1544 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1545 NULL))
1546 curwin->w_cursor.col = 0;
1547 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1548 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1549 }
1550#endif
1551 oap->end = oap->start;
1552 oap->start = curwin->w_cursor;
1553 }
1554
1555 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1556
1557#ifdef FEAT_VIRTUALEDIT
1558 /* Set "virtual_op" before resetting VIsual_active. */
1559 virtual_op = virtual_active();
1560#endif
1561
1562#ifdef FEAT_VISUAL
1563 if (VIsual_active || redo_VIsual_busy)
1564 {
1565 if (VIsual_mode == Ctrl_V) /* block mode */
1566 {
1567 colnr_T start, end;
1568
1569 oap->block_mode = TRUE;
1570
1571 getvvcol(curwin, &(oap->start),
1572 &oap->start_vcol, NULL, &oap->end_vcol);
1573 if (!redo_VIsual_busy)
1574 {
1575 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1576
1577 if (start < oap->start_vcol)
1578 oap->start_vcol = start;
1579 if (end > oap->end_vcol)
1580 {
1581 if (*p_sel == 'e' && start >= 1
1582 && start - 1 >= oap->end_vcol)
1583 oap->end_vcol = start - 1;
1584 else
1585 oap->end_vcol = end;
1586 }
1587 }
1588
1589 /* if '$' was used, get oap->end_vcol from longest line */
1590 if (curwin->w_curswant == MAXCOL)
1591 {
1592 curwin->w_cursor.col = MAXCOL;
1593 oap->end_vcol = 0;
1594 for (curwin->w_cursor.lnum = oap->start.lnum;
1595 curwin->w_cursor.lnum <= oap->end.lnum;
1596 ++curwin->w_cursor.lnum)
1597 {
1598 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1599 if (end > oap->end_vcol)
1600 oap->end_vcol = end;
1601 }
1602 }
1603 else if (redo_VIsual_busy)
1604 oap->end_vcol = oap->start_vcol + redo_VIsual_col - 1;
1605 /*
1606 * Correct oap->end.col and oap->start.col to be the
1607 * upper-left and lower-right corner of the block area.
1608 *
1609 * (Actually, this does convert column positions into character
1610 * positions)
1611 */
1612 curwin->w_cursor.lnum = oap->end.lnum;
1613 coladvance(oap->end_vcol);
1614 oap->end = curwin->w_cursor;
1615
1616 curwin->w_cursor = oap->start;
1617 coladvance(oap->start_vcol);
1618 oap->start = curwin->w_cursor;
1619 }
1620
1621 if (!redo_VIsual_busy && !gui_yank)
1622 {
1623 /*
1624 * Prepare to reselect and redo Visual: this is based on the
1625 * size of the Visual text
1626 */
1627 resel_VIsual_mode = VIsual_mode;
1628 if (curwin->w_curswant == MAXCOL)
1629 resel_VIsual_col = MAXCOL;
1630 else if (VIsual_mode == Ctrl_V)
1631 resel_VIsual_col = oap->end_vcol - oap->start_vcol + 1;
1632 else if (oap->line_count > 1)
1633 resel_VIsual_col = oap->end.col;
1634 else
1635 resel_VIsual_col = oap->end.col - oap->start.col + 1;
1636 resel_VIsual_line_count = oap->line_count;
1637 }
1638
1639 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1640 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1641 && oap->op_type != OP_COLON
1642#ifdef FEAT_FOLDING
1643 && oap->op_type != OP_FOLD
1644 && oap->op_type != OP_FOLDOPEN
1645 && oap->op_type != OP_FOLDOPENREC
1646 && oap->op_type != OP_FOLDCLOSE
1647 && oap->op_type != OP_FOLDCLOSEREC
1648 && oap->op_type != OP_FOLDDEL
1649 && oap->op_type != OP_FOLDDELREC
1650#endif
1651 && oap->motion_force == NUL
1652 )
1653 {
1654 /* Prepare for redoing. Only use the nchar field for "r",
1655 * otherwise it might be the second char of the operator. */
1656 prep_redo(oap->regname, 0L, NUL, 'v',
1657 get_op_char(oap->op_type),
1658 get_extra_op_char(oap->op_type),
1659 oap->op_type == OP_REPLACE ? cap->nchar : NUL);
1660 if (!redo_VIsual_busy)
1661 {
1662 redo_VIsual_mode = resel_VIsual_mode;
1663 redo_VIsual_col = resel_VIsual_col;
1664 redo_VIsual_line_count = resel_VIsual_line_count;
1665 redo_VIsual_count = cap->count0;
1666 }
1667 }
1668
1669 /*
1670 * oap->inclusive defaults to TRUE.
1671 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1672 * FALSE. This makes "d}P" and "v}dP" work the same.
1673 */
1674 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1675 oap->inclusive = TRUE;
1676 if (VIsual_mode == 'V')
1677 oap->motion_type = MLINE;
1678 else
1679 {
1680 oap->motion_type = MCHAR;
1681 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1682# ifdef FEAT_VIRTUALEDIT
1683 && (include_line_break || !virtual_op)
1684# endif
1685 )
1686 {
1687 oap->inclusive = FALSE;
1688 /* Try to include the newline, unless it's an operator
1689 * that works on lines only */
1690 if (*p_sel != 'o'
1691 && !op_on_lines(oap->op_type)
1692 && oap->end.lnum < curbuf->b_ml.ml_line_count)
1693 {
1694 ++oap->end.lnum;
1695 oap->end.col = 0;
1696# ifdef FEAT_VIRTUALEDIT
1697 oap->end.coladd = 0;
1698# endif
1699 ++oap->line_count;
1700 }
1701 }
1702 }
1703
1704 redo_VIsual_busy = FALSE;
1705 /*
1706 * Switch Visual off now, so screen updating does
1707 * not show inverted text when the screen is redrawn.
1708 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1709 * no screen redraw, so it is done here to remove the inverted
1710 * part.
1711 */
1712 if (!gui_yank)
1713 {
1714 VIsual_active = FALSE;
1715# ifdef FEAT_MOUSE
1716 setmouse();
1717 mouse_dragging = 0;
1718# endif
1719 if (p_smd)
1720 clear_cmdline = TRUE; /* unshow visual mode later */
1721#ifdef FEAT_CMDL_INFO
1722 else
1723 clear_showcmd();
1724#endif
1725 if ((oap->op_type == OP_YANK
1726 || oap->op_type == OP_COLON
1727 || oap->op_type == OP_FILTER)
1728 && oap->motion_force == NUL)
1729 redraw_curbuf_later(INVERTED);
1730 }
1731 }
1732#endif
1733
1734#ifdef FEAT_MBYTE
1735 /* Include the trailing byte of a multi-byte char. */
1736 if (has_mbyte && oap->inclusive)
1737 {
1738 int l;
1739
1740 l = (*mb_ptr2len_check)(ml_get_pos(&oap->end));
1741 if (l > 1)
1742 oap->end.col += l - 1;
1743 }
1744#endif
1745 curwin->w_set_curswant = TRUE;
1746
1747 /*
1748 * oap->empty is set when start and end are the same. The inclusive
1749 * flag affects this too, unless yanking and the end is on a NUL.
1750 */
1751 oap->empty = (oap->motion_type == MCHAR
1752 && (!oap->inclusive
1753 || (oap->op_type == OP_YANK
1754 && gchar_pos(&oap->end) == NUL))
1755 && equalpos(oap->start, oap->end)
1756#ifdef FEAT_VIRTUALEDIT
1757 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1758#endif
1759 );
1760 /*
1761 * For delete, change and yank, it's an error to operate on an
1762 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1763 */
1764 empty_region_error = (oap->empty
1765 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1766
1767#ifdef FEAT_VISUAL
1768 /* Force a redraw when operating on an empty Visual region, when
1769 * 'modifiable is off or creating a fold. */
1770 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1771# ifdef FEAT_FOLDING
1772 || oap->op_type == OP_FOLD
1773# endif
1774 ))
1775 redraw_curbuf_later(INVERTED);
1776#endif
1777
1778 /*
1779 * If the end of an operator is in column one while oap->motion_type
1780 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1781 * character in the previous line. If op_start is on or before the
1782 * first non-blank in the line, the operator becomes linewise
1783 * (strange, but that's the way vi does it).
1784 */
1785 if ( oap->motion_type == MCHAR
1786 && oap->inclusive == FALSE
1787 && !(cap->retval & CA_NO_ADJ_OP_END)
1788 && oap->end.col == 0
1789#ifdef FEAT_VISUAL
1790 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001791 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792#endif
1793 && oap->line_count > 1)
1794 {
1795 oap->end_adjusted = TRUE; /* remember that we did this */
1796 --oap->line_count;
1797 --oap->end.lnum;
1798 if (inindent(0))
1799 oap->motion_type = MLINE;
1800 else
1801 {
1802 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1803 if (oap->end.col)
1804 {
1805 --oap->end.col;
1806 oap->inclusive = TRUE;
1807 }
1808 }
1809 }
1810 else
1811 oap->end_adjusted = FALSE;
1812
1813 switch (oap->op_type)
1814 {
1815 case OP_LSHIFT:
1816 case OP_RSHIFT:
1817 op_shift(oap, TRUE,
1818#ifdef FEAT_VISUAL
1819 oap->is_VIsual ? (int)cap->count1 :
1820#endif
1821 1);
1822 auto_format(FALSE, TRUE);
1823 break;
1824
1825 case OP_JOIN_NS:
1826 case OP_JOIN:
1827 if (oap->line_count < 2)
1828 oap->line_count = 2;
1829 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1830 curbuf->b_ml.ml_line_count)
1831 beep_flush();
1832 else
1833 {
1834 do_do_join(oap->line_count, oap->op_type == OP_JOIN);
1835 auto_format(FALSE, TRUE);
1836 }
1837 break;
1838
1839 case OP_DELETE:
1840#ifdef FEAT_VISUAL
1841 VIsual_reselect = FALSE; /* don't reselect now */
1842#endif
1843 if (empty_region_error)
1844 vim_beep();
1845 else
1846 {
1847 (void)op_delete(oap);
1848 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1849 u_save_cursor(); /* cursor line wasn't saved yet */
1850 auto_format(FALSE, TRUE);
1851 }
1852 break;
1853
1854 case OP_YANK:
1855 if (empty_region_error)
1856 {
1857 if (!gui_yank)
1858 vim_beep();
1859 }
1860 else
1861 (void)op_yank(oap, FALSE, !gui_yank);
1862 check_cursor_col();
1863 break;
1864
1865 case OP_CHANGE:
1866#ifdef FEAT_VISUAL
1867 VIsual_reselect = FALSE; /* don't reselect now */
1868#endif
1869 if (empty_region_error)
1870 vim_beep();
1871 else
1872 {
1873 /* This is a new edit command, not a restart. Need to
1874 * remember it to make 'insertmode' work with mappings for
1875 * Visual mode. But do this only once and not when typed and
1876 * 'insertmode' isn't set. */
1877 if (p_im || !KeyTyped)
1878 restart_edit_save = restart_edit;
1879 else
1880 restart_edit_save = 0;
1881 restart_edit = 0;
1882 /* Reset finish_op now, don't want it set inside edit(). */
1883 finish_op = FALSE;
1884 if (op_change(oap)) /* will call edit() */
1885 cap->retval |= CA_COMMAND_BUSY;
1886 if (restart_edit == 0)
1887 restart_edit = restart_edit_save;
1888 }
1889 break;
1890
1891 case OP_FILTER:
1892 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1893 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1894 else
1895 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
1896
1897 case OP_INDENT:
1898 case OP_COLON:
1899
1900#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1901 /*
1902 * If 'equalprg' is empty, do the indenting internally.
1903 */
1904 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1905 {
1906# ifdef FEAT_LISP
1907 if (curbuf->b_p_lisp)
1908 {
1909 op_reindent(oap, get_lisp_indent);
1910 break;
1911 }
1912# endif
1913# ifdef FEAT_CINDENT
1914 op_reindent(oap,
1915# ifdef FEAT_EVAL
1916 *curbuf->b_p_inde != NUL ? get_expr_indent :
1917# endif
1918 get_c_indent);
1919 break;
1920# endif
1921 }
1922#endif
1923
1924 op_colon(oap);
1925 break;
1926
1927 case OP_TILDE:
1928 case OP_UPPER:
1929 case OP_LOWER:
1930 case OP_ROT13:
1931 if (empty_region_error)
1932 vim_beep();
1933 else
1934 op_tilde(oap);
1935 check_cursor_col();
1936 break;
1937
1938 case OP_FORMAT:
1939 if (*p_fp != NUL)
1940 op_colon(oap); /* use external command */
1941 else
1942 op_format(oap, FALSE); /* use internal function */
1943 break;
1944
1945 case OP_FORMAT2:
1946 op_format(oap, TRUE); /* use internal function */
1947 break;
1948
1949 case OP_INSERT:
1950 case OP_APPEND:
1951#ifdef FEAT_VISUAL
1952 VIsual_reselect = FALSE; /* don't reselect now */
1953#endif
1954#ifdef FEAT_VISUALEXTRA
1955 if (empty_region_error)
1956 vim_beep();
1957 else
1958 {
1959 /* This is a new edit command, not a restart. Need to
1960 * remember it to make 'insertmode' work with mappings for
1961 * Visual mode. But do this only once. */
1962 restart_edit_save = restart_edit;
1963 restart_edit = 0;
1964
1965 op_insert(oap, cap->count1);
1966
1967 /* TODO: when inserting in several lines, should format all
1968 * the lines. */
1969 auto_format(FALSE, TRUE);
1970
1971 if (restart_edit == 0)
1972 restart_edit = restart_edit_save;
1973 }
1974#else
1975 vim_beep();
1976#endif
1977 break;
1978
1979 case OP_REPLACE:
1980#ifdef FEAT_VISUAL
1981 VIsual_reselect = FALSE; /* don't reselect now */
1982#endif
1983#ifdef FEAT_VISUALEXTRA
1984 if (empty_region_error)
1985#endif
1986 vim_beep();
1987#ifdef FEAT_VISUALEXTRA
1988 else
1989 op_replace(oap, cap->nchar);
1990#endif
1991 break;
1992
1993#ifdef FEAT_FOLDING
1994 case OP_FOLD:
1995 VIsual_reselect = FALSE; /* don't reselect now */
1996 foldCreate(oap->start.lnum, oap->end.lnum);
1997 break;
1998
1999 case OP_FOLDOPEN:
2000 case OP_FOLDOPENREC:
2001 case OP_FOLDCLOSE:
2002 case OP_FOLDCLOSEREC:
2003 VIsual_reselect = FALSE; /* don't reselect now */
2004 opFoldRange(oap->start.lnum, oap->end.lnum,
2005 oap->op_type == OP_FOLDOPEN
2006 || oap->op_type == OP_FOLDOPENREC,
2007 oap->op_type == OP_FOLDOPENREC
2008 || oap->op_type == OP_FOLDCLOSEREC,
2009 oap->is_VIsual);
2010 break;
2011
2012 case OP_FOLDDEL:
2013 case OP_FOLDDELREC:
2014 VIsual_reselect = FALSE; /* don't reselect now */
2015 deleteFold(oap->start.lnum, oap->end.lnum,
2016 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2017 break;
2018#endif
2019 default:
2020 clearopbeep(oap);
2021 }
2022#ifdef FEAT_VIRTUALEDIT
2023 virtual_op = MAYBE;
2024#endif
2025 if (!gui_yank)
2026 {
2027 /*
2028 * if 'sol' not set, go back to old column for some commands
2029 */
2030 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2031 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2032 || oap->op_type == OP_DELETE))
2033 coladvance(curwin->w_curswant = old_col);
2034 }
2035 else
2036 {
2037 curwin->w_cursor = old_cursor;
2038 }
2039#ifdef FEAT_VISUAL
2040 oap->block_mode = FALSE;
2041#endif
2042 clearop(oap);
2043 }
2044}
2045
2046/*
2047 * Handle indent and format operators and visual mode ":".
2048 */
2049 static void
2050op_colon(oap)
2051 oparg_T *oap;
2052{
2053 stuffcharReadbuff(':');
2054#ifdef FEAT_VISUAL
2055 if (oap->is_VIsual)
2056 stuffReadbuff((char_u *)"'<,'>");
2057 else
2058#endif
2059 {
2060 /*
2061 * Make the range look nice, so it can be repeated.
2062 */
2063 if (oap->start.lnum == curwin->w_cursor.lnum)
2064 stuffcharReadbuff('.');
2065 else
2066 stuffnumReadbuff((long)oap->start.lnum);
2067 if (oap->end.lnum != oap->start.lnum)
2068 {
2069 stuffcharReadbuff(',');
2070 if (oap->end.lnum == curwin->w_cursor.lnum)
2071 stuffcharReadbuff('.');
2072 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2073 stuffcharReadbuff('$');
2074 else if (oap->start.lnum == curwin->w_cursor.lnum)
2075 {
2076 stuffReadbuff((char_u *)".+");
2077 stuffnumReadbuff((long)oap->line_count - 1);
2078 }
2079 else
2080 stuffnumReadbuff((long)oap->end.lnum);
2081 }
2082 }
2083 if (oap->op_type != OP_COLON)
2084 stuffReadbuff((char_u *)"!");
2085 if (oap->op_type == OP_INDENT)
2086 {
2087#ifndef FEAT_CINDENT
2088 if (*get_equalprg() == NUL)
2089 stuffReadbuff((char_u *)"indent");
2090 else
2091#endif
2092 stuffReadbuff(get_equalprg());
2093 stuffReadbuff((char_u *)"\n");
2094 }
2095 else if (oap->op_type == OP_FORMAT)
2096 {
2097 if (*p_fp == NUL)
2098 stuffReadbuff((char_u *)"fmt");
2099 else
2100 stuffReadbuff(p_fp);
2101 stuffReadbuff((char_u *)"\n");
2102 }
2103
2104 /*
2105 * do_cmdline() does the rest
2106 */
2107}
2108
2109#if defined(FEAT_MOUSE) || defined(PROTO)
2110/*
2111 * Do the appropriate action for the current mouse click in the current mode.
2112 * Not used for Command-line mode.
2113 *
2114 * Normal Mode:
2115 * event modi- position visual change action
2116 * fier cursor window
2117 * left press - yes end yes
2118 * left press C yes end yes "^]" (2)
2119 * left press S yes end yes "*" (2)
2120 * left drag - yes start if moved no
2121 * left relse - yes start if moved no
2122 * middle press - yes if not active no put register
2123 * middle press - yes if active no yank and put
2124 * right press - yes start or extend yes
2125 * right press S yes no change yes "#" (2)
2126 * right drag - yes extend no
2127 * right relse - yes extend no
2128 *
2129 * Insert or Replace Mode:
2130 * event modi- position visual change action
2131 * fier cursor window
2132 * left press - yes (cannot be active) yes
2133 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2134 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2135 * left drag - yes start or extend (1) no CTRL-O (1)
2136 * left relse - yes start or extend (1) no CTRL-O (1)
2137 * middle press - no (cannot be active) no put register
2138 * right press - yes start or extend yes CTRL-O
2139 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2140 *
2141 * (1) only if mouse pointer moved since press
2142 * (2) only if click is in same buffer
2143 *
2144 * Return TRUE if start_arrow() should be called for edit mode.
2145 */
2146 int
2147do_mouse(oap, c, dir, count, fixindent)
2148 oparg_T *oap; /* operator argument, can be NULL */
2149 int c; /* K_LEFTMOUSE, etc */
2150 int dir; /* Direction to 'put' if necessary */
2151 long count;
2152 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2153{
2154 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2155 static int got_click = FALSE; /* got a click some time back */
2156
2157 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2158 int is_click; /* If FALSE it's a drag or release event */
2159 int is_drag; /* If TRUE it's a drag event */
2160 int jump_flags = 0; /* flags for jump_to_mouse() */
2161 pos_T start_visual;
2162 int moved; /* Has cursor moved? */
2163 int in_status_line; /* mouse in status line */
2164#ifdef FEAT_VERTSPLIT
2165 int in_sep_line; /* mouse in vertical separator line */
2166#endif
2167 int c1, c2;
2168#if defined(FEAT_FOLDING)
2169 pos_T save_cursor;
2170#endif
2171 win_T *old_curwin = curwin;
2172#ifdef FEAT_VISUAL
2173 static pos_T orig_cursor;
2174 colnr_T leftcol, rightcol;
2175 pos_T end_visual;
2176 int diff;
2177 int old_active = VIsual_active;
2178 int old_mode = VIsual_mode;
2179#endif
2180 int regname;
2181
2182#if defined(FEAT_FOLDING)
2183 save_cursor = curwin->w_cursor;
2184#endif
2185
2186 /*
2187 * When GUI is active, always recognize mouse events, otherwise:
2188 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2189 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2190 * - For command line and insert mode 'mouse' is checked before calling
2191 * do_mouse().
2192 */
2193 if (do_always)
2194 do_always = FALSE;
2195 else
2196#ifdef FEAT_GUI
2197 if (!gui.in_use)
2198#endif
2199 {
2200#ifdef FEAT_VISUAL
2201 if (VIsual_active)
2202 {
2203 if (!mouse_has(MOUSE_VISUAL))
2204 return FALSE;
2205 }
2206 else
2207#endif
2208 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2209 return FALSE;
2210 }
2211
2212 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2213
2214#ifdef FEAT_MOUSESHAPE
2215 /* May have stopped dragging the status or separator line. The pointer is
2216 * most likely still on the status or separator line. */
2217 if (!is_drag && drag_status_line)
2218 {
2219 drag_status_line = FALSE;
2220 update_mouseshape(SHAPE_IDX_STATUS);
2221 }
2222# ifdef FEAT_VERTSPLIT
2223 if (!is_drag && drag_sep_line)
2224 {
2225 drag_sep_line = FALSE;
2226 update_mouseshape(SHAPE_IDX_VSEP);
2227 }
2228# endif
2229#endif
2230
2231 /*
2232 * Ignore drag and release events if we didn't get a click.
2233 */
2234 if (is_click)
2235 got_click = TRUE;
2236 else
2237 {
2238 if (!got_click) /* didn't get click, ignore */
2239 return FALSE;
2240 if (!is_drag) /* release, reset got_click */
2241 got_click = FALSE;
2242 }
2243
2244 /*
2245 * ALT is currently ignored
2246 */
2247 if ((mod_mask & MOD_MASK_ALT))
2248 return FALSE;
2249
2250 /*
2251 * CTRL right mouse button does CTRL-T
2252 */
2253 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2254 {
2255 if (State & INSERT)
2256 stuffcharReadbuff(Ctrl_O);
2257 if (count > 1)
2258 stuffnumReadbuff(count);
2259 stuffcharReadbuff(Ctrl_T);
2260 got_click = FALSE; /* ignore drag&release now */
2261 return FALSE;
2262 }
2263
2264 /*
2265 * CTRL only works with left mouse button
2266 */
2267 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2268 return FALSE;
2269
2270 /*
2271 * When a modifier is down, ignore drag and release events, as well as
2272 * multiple clicks and the middle mouse button.
2273 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2274 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002275 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2276 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 && (!is_click
2278 || (mod_mask & MOD_MASK_MULTI_CLICK)
2279 || which_button == MOUSE_MIDDLE)
2280 && !((mod_mask & MOD_MASK_SHIFT)
2281 && mouse_model_popup()
2282 && which_button == MOUSE_LEFT)
2283 )
2284 return FALSE;
2285
2286 /*
2287 * If the button press was used as the movement command for an operator
2288 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2289 * drag/release events.
2290 */
2291 if (!is_click && which_button == MOUSE_MIDDLE)
2292 return FALSE;
2293
2294 if (oap != NULL)
2295 regname = oap->regname;
2296 else
2297 regname = 0;
2298
2299 /*
2300 * Middle mouse button does a 'put' of the selected text
2301 */
2302 if (which_button == MOUSE_MIDDLE)
2303 {
2304 if (State == NORMAL)
2305 {
2306 /*
2307 * If an operator was pending, we don't know what the user wanted
2308 * to do. Go back to normal mode: Clear the operator and beep().
2309 */
2310 if (oap != NULL && oap->op_type != OP_NOP)
2311 {
2312 clearopbeep(oap);
2313 return FALSE;
2314 }
2315
2316#ifdef FEAT_VISUAL
2317 /*
2318 * If visual was active, yank the highlighted text and put it
2319 * before the mouse pointer position.
2320 */
2321 if (VIsual_active)
2322 {
2323 stuffcharReadbuff('y');
2324 stuffcharReadbuff(K_MIDDLEMOUSE);
2325 do_always = TRUE; /* ignore 'mouse' setting next time */
2326 return FALSE;
2327 }
2328#endif
2329 /*
2330 * The rest is below jump_to_mouse()
2331 */
2332 }
2333
2334 else if ((State & INSERT) == 0)
2335 return FALSE;
2336
2337 /*
2338 * Middle click in insert mode doesn't move the mouse, just insert the
2339 * contents of a register. '.' register is special, can't insert that
2340 * with do_put().
2341 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2342 * happens for the GUI).
2343 */
2344 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2345 {
2346 if (regname == '.')
2347 insert_reg(regname, TRUE);
2348 else
2349 {
2350#ifdef FEAT_CLIPBOARD
2351 if (clip_star.available && regname == 0)
2352 regname = '*';
2353#endif
2354 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2355 insert_reg(regname, TRUE);
2356 else
2357 {
2358 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2359
2360 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2361 AppendCharToRedobuff(Ctrl_R);
2362 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2363 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2364 }
2365 }
2366 return FALSE;
2367 }
2368 }
2369
2370 /* When dragging or button-up stay in the same window. */
2371 if (!is_click)
2372 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2373
2374 start_visual.lnum = 0;
2375
2376 /*
2377 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2378 * right button up -> pop-up menu
2379 * shift-left button -> right button
2380 */
2381 if (mouse_model_popup())
2382 {
2383 if (which_button == MOUSE_RIGHT
2384 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2385 {
2386 /*
2387 * NOTE: Ignore right button down and drag mouse events.
2388 * Windows only shows the popup menu on the button up event.
2389 */
Bram Moolenaar843ee412004-06-30 16:16:41 +00002390#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_KDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 if (!is_click)
2392 return FALSE;
2393#endif
2394#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2395 if (is_click || is_drag)
2396 return FALSE;
2397#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00002398#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_KDE) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2400 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2401 if (gui.in_use)
2402 {
2403 jump_flags = 0;
2404 if (STRCMP(p_mousem, "popup_setpos") == 0)
2405 {
2406 /* First set the cursor position before showing the popup
2407 * menu. */
2408#ifdef FEAT_VISUAL
2409 if (VIsual_active)
2410 {
2411 pos_T m_pos;
2412
2413 /*
2414 * set MOUSE_MAY_STOP_VIS if we are outside the
2415 * selection or the current window (might have false
2416 * negative here)
2417 */
2418 if (mouse_row < W_WINROW(curwin)
2419 || mouse_row
2420 > (W_WINROW(curwin) + curwin->w_height))
2421 jump_flags = MOUSE_MAY_STOP_VIS;
2422 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2423 jump_flags = MOUSE_MAY_STOP_VIS;
2424 else
2425 {
2426 if ((lt(curwin->w_cursor, VIsual)
2427 && (lt(m_pos, curwin->w_cursor)
2428 || lt(VIsual, m_pos)))
2429 || (lt(VIsual, curwin->w_cursor)
2430 && (lt(m_pos, VIsual)
2431 || lt(curwin->w_cursor, m_pos))))
2432 {
2433 jump_flags = MOUSE_MAY_STOP_VIS;
2434 }
2435 else if (VIsual_mode == Ctrl_V)
2436 {
2437 getvcols(curwin, &curwin->w_cursor, &VIsual,
2438 &leftcol, &rightcol);
2439 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2440 if (m_pos.col < leftcol || m_pos.col > rightcol)
2441 jump_flags = MOUSE_MAY_STOP_VIS;
2442 }
2443 }
2444 }
2445 else
2446 jump_flags = MOUSE_MAY_STOP_VIS;
2447#endif
2448 }
2449 if (jump_flags)
2450 {
2451 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2452 update_curbuf(
2453#ifdef FEAT_VISUAL
2454 VIsual_active ? INVERTED :
2455#endif
2456 VALID);
2457 setcursor();
2458 out_flush(); /* Update before showing popup menu */
2459 }
2460# ifdef FEAT_MENU
2461 gui_show_popupmenu();
2462# endif
2463 return (jump_flags & CURSOR_MOVED) != 0;
2464 }
2465 else
2466 return FALSE;
2467#else
2468 return FALSE;
2469#endif
2470 }
2471 if (which_button == MOUSE_LEFT && (mod_mask & MOD_MASK_SHIFT))
2472 {
2473 which_button = MOUSE_RIGHT;
2474 mod_mask &= ~MOD_MASK_SHIFT;
2475 }
2476 }
2477
2478#ifdef FEAT_VISUAL
2479 if ((State & (NORMAL | INSERT))
2480 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2481 {
2482 if (which_button == MOUSE_LEFT)
2483 {
2484 if (is_click)
2485 {
2486 /* stop Visual mode for a left click in a window, but not when
2487 * on a status line */
2488 if (VIsual_active)
2489 jump_flags |= MOUSE_MAY_STOP_VIS;
2490 }
2491 else if (mouse_has(MOUSE_VISUAL))
2492 jump_flags |= MOUSE_MAY_VIS;
2493 }
2494 else if (which_button == MOUSE_RIGHT)
2495 {
2496 if (is_click && VIsual_active)
2497 {
2498 /*
2499 * Remember the start and end of visual before moving the
2500 * cursor.
2501 */
2502 if (lt(curwin->w_cursor, VIsual))
2503 {
2504 start_visual = curwin->w_cursor;
2505 end_visual = VIsual;
2506 }
2507 else
2508 {
2509 start_visual = VIsual;
2510 end_visual = curwin->w_cursor;
2511 }
2512 }
2513 jump_flags |= MOUSE_FOCUS;
2514 if (mouse_has(MOUSE_VISUAL))
2515 jump_flags |= MOUSE_MAY_VIS;
2516 }
2517 }
2518#endif
2519
2520 /*
2521 * If an operator is pending, ignore all drags and releases until the
2522 * next mouse click.
2523 */
2524 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2525 {
2526 got_click = FALSE;
2527 oap->motion_type = MCHAR;
2528 }
2529
2530 /* When releasing the button let jump_to_mouse() know. */
2531 if (!is_click && !is_drag)
2532 jump_flags |= MOUSE_RELEASED;
2533
2534 /*
2535 * JUMP!
2536 */
2537 jump_flags = jump_to_mouse(jump_flags,
2538 oap == NULL ? NULL : &(oap->inclusive), which_button);
2539 moved = (jump_flags & CURSOR_MOVED);
2540 in_status_line = (jump_flags & IN_STATUS_LINE);
2541#ifdef FEAT_VERTSPLIT
2542 in_sep_line = (jump_flags & IN_SEP_LINE);
2543#endif
2544
2545#ifdef FEAT_NETBEANS_INTG
2546 if (usingNetbeans && isNetbeansBuffer(curbuf)
2547 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2548 {
2549 int key = KEY2TERMCAP1(c);
2550
2551 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2552 || key == (int)KE_RIGHTRELEASE)
2553 netbeans_button_release(which_button);
2554 }
2555#endif
2556
2557 /* When jumping to another window, clear a pending operator. That's a bit
2558 * friendlier than beeping and not jumping to that window. */
2559 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2560 clearop(oap);
2561
2562#ifdef FEAT_FOLDING
2563 if (mod_mask == 0
2564 && !is_drag
2565 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2566 && which_button == MOUSE_LEFT)
2567 {
2568 /* open or close a fold at this line */
2569 if (jump_flags & MOUSE_FOLD_OPEN)
2570 openFold(curwin->w_cursor.lnum, 1L);
2571 else
2572 closeFold(curwin->w_cursor.lnum, 1L);
2573 /* don't move the cursor if still in the same window */
2574 if (curwin == old_curwin)
2575 curwin->w_cursor = save_cursor;
2576 }
2577#endif
2578
2579#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2580 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2581 {
2582 clip_modeless(which_button, is_click, is_drag);
2583 return FALSE;
2584 }
2585#endif
2586
2587#ifdef FEAT_VISUAL
2588 /* Set global flag that we are extending the Visual area with mouse
2589 * dragging; temporarily mimimize 'scrolloff'. */
2590 if (VIsual_active && is_drag && p_so)
2591 {
2592 /* In the very first line, allow scrolling one line */
2593 if (mouse_row == 0)
2594 mouse_dragging = 2;
2595 else
2596 mouse_dragging = 1;
2597 }
2598
2599 /* When dragging the mouse above the window, scroll down. */
2600 if (is_drag && mouse_row < 0 && !in_status_line)
2601 {
2602 scroll_redraw(FALSE, 1L);
2603 mouse_row = 0;
2604 }
2605
2606 if (start_visual.lnum) /* right click in visual mode */
2607 {
2608 /*
2609 * In Visual-block mode, divide the area in four, pick up the corner
2610 * that is in the quarter that the cursor is in.
2611 */
2612 if (VIsual_mode == Ctrl_V)
2613 {
2614 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2615 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2616 end_visual.col = leftcol;
2617 else
2618 end_visual.col = rightcol;
2619 if (curwin->w_cursor.lnum <
2620 (start_visual.lnum + end_visual.lnum) / 2)
2621 end_visual.lnum = end_visual.lnum;
2622 else
2623 end_visual.lnum = start_visual.lnum;
2624
2625 /* move VIsual to the right column */
2626 start_visual = curwin->w_cursor; /* save the cursor pos */
2627 curwin->w_cursor = end_visual;
2628 coladvance(end_visual.col);
2629 VIsual = curwin->w_cursor;
2630 curwin->w_cursor = start_visual; /* restore the cursor */
2631 }
2632 else
2633 {
2634 /*
2635 * If the click is before the start of visual, change the start.
2636 * If the click is after the end of visual, change the end. If
2637 * the click is inside the visual, change the closest side.
2638 */
2639 if (lt(curwin->w_cursor, start_visual))
2640 VIsual = end_visual;
2641 else if (lt(end_visual, curwin->w_cursor))
2642 VIsual = start_visual;
2643 else
2644 {
2645 /* In the same line, compare column number */
2646 if (end_visual.lnum == start_visual.lnum)
2647 {
2648 if (curwin->w_cursor.col - start_visual.col >
2649 end_visual.col - curwin->w_cursor.col)
2650 VIsual = start_visual;
2651 else
2652 VIsual = end_visual;
2653 }
2654
2655 /* In different lines, compare line number */
2656 else
2657 {
2658 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2659 (end_visual.lnum - curwin->w_cursor.lnum);
2660
2661 if (diff > 0) /* closest to end */
2662 VIsual = start_visual;
2663 else if (diff < 0) /* closest to start */
2664 VIsual = end_visual;
2665 else /* in the middle line */
2666 {
2667 if (curwin->w_cursor.col <
2668 (start_visual.col + end_visual.col) / 2)
2669 VIsual = end_visual;
2670 else
2671 VIsual = start_visual;
2672 }
2673 }
2674 }
2675 }
2676 }
2677 /*
2678 * If Visual mode started in insert mode, execute "CTRL-O"
2679 */
2680 else if ((State & INSERT) && VIsual_active)
2681 stuffcharReadbuff(Ctrl_O);
2682#endif
2683
2684 /*
2685 * Middle mouse click: Put text before cursor.
2686 */
2687 if (which_button == MOUSE_MIDDLE)
2688 {
2689#ifdef FEAT_CLIPBOARD
2690 if (clip_star.available && regname == 0)
2691 regname = '*';
2692#endif
2693 if (yank_register_mline(regname))
2694 {
2695 if (mouse_past_bottom)
2696 dir = FORWARD;
2697 }
2698 else if (mouse_past_eol)
2699 dir = FORWARD;
2700
2701 if (fixindent)
2702 {
2703 c1 = (dir == BACKWARD) ? '[' : ']';
2704 c2 = 'p';
2705 }
2706 else
2707 {
2708 c1 = (dir == FORWARD) ? 'p' : 'P';
2709 c2 = NUL;
2710 }
2711 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2712
2713 /*
2714 * Remember where the paste started, so in edit() Insstart can be set
2715 * to this position
2716 */
2717 if (restart_edit != 0)
2718 where_paste_started = curwin->w_cursor;
2719 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2720 }
2721
2722#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2723 /*
2724 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2725 * error under the mouse pointer.
2726 */
2727 else if (((mod_mask & MOD_MASK_CTRL)
2728 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2729 && bt_quickfix(curbuf))
2730 {
2731 if (State & INSERT)
2732 stuffcharReadbuff(Ctrl_O);
2733 stuffReadbuff((char_u *)":.cc\n");
2734 got_click = FALSE; /* ignore drag&release now */
2735 }
2736#endif
2737
2738 /*
2739 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2740 * under the mouse pointer.
2741 */
2742 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2743 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2744 {
2745 if (State & INSERT)
2746 stuffcharReadbuff(Ctrl_O);
2747 stuffcharReadbuff(Ctrl_RSB);
2748 got_click = FALSE; /* ignore drag&release now */
2749 }
2750
2751 /*
2752 * Shift-Mouse click searches for the next occurrence of the word under
2753 * the mouse pointer
2754 */
2755 else if ((mod_mask & MOD_MASK_SHIFT))
2756 {
2757 if (State & INSERT
2758#ifdef FEAT_VISUAL
2759 || (VIsual_active && VIsual_select)
2760#endif
2761 )
2762 stuffcharReadbuff(Ctrl_O);
2763 if (which_button == MOUSE_LEFT)
2764 stuffcharReadbuff('*');
2765 else /* MOUSE_RIGHT */
2766 stuffcharReadbuff('#');
2767 }
2768
2769 /* Handle double clicks, unless on status line */
2770 else if (in_status_line)
2771 {
2772#ifdef FEAT_MOUSESHAPE
2773 if ((is_drag || is_click) && !drag_status_line)
2774 {
2775 drag_status_line = TRUE;
2776 update_mouseshape(-1);
2777 }
2778#endif
2779 }
2780#ifdef FEAT_VERTSPLIT
2781 else if (in_sep_line)
2782 {
2783# ifdef FEAT_MOUSESHAPE
2784 if ((is_drag || is_click) && !drag_sep_line)
2785 {
2786 drag_sep_line = TRUE;
2787 update_mouseshape(-1);
2788 }
2789# endif
2790 }
2791#endif
2792#ifdef FEAT_VISUAL
2793 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
2794 && mouse_has(MOUSE_VISUAL))
2795 {
2796 if (is_click || !VIsual_active)
2797 {
2798 if (VIsual_active)
2799 orig_cursor = VIsual;
2800 else
2801 {
2802 check_visual_highlight();
2803 VIsual = curwin->w_cursor;
2804 orig_cursor = VIsual;
2805 VIsual_active = TRUE;
2806 VIsual_reselect = TRUE;
2807 /* start Select mode if 'selectmode' contains "mouse" */
2808 may_start_select('o');
2809 setmouse();
2810 }
2811 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2812 VIsual_mode = 'v';
2813 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
2814 VIsual_mode = 'V';
2815 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
2816 VIsual_mode = Ctrl_V;
2817#ifdef FEAT_CLIPBOARD
2818 /* Make sure the clipboard gets updated. Needed because start and
2819 * end may still be the same, and the selection needs to be owned */
2820 clip_star.vmode = NUL;
2821#endif
2822 }
2823 /*
2824 * A double click selects a word or a block.
2825 */
2826 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2827 {
2828 pos_T *pos = NULL;
2829
2830 if (is_click)
2831 {
2832 /* If the character under the cursor (skipping white space) is
2833 * not a word character, try finding a match and select a (),
2834 * {}, [], #if/#endif, etc. block. */
2835 end_visual = curwin->w_cursor;
2836 while (vim_iswhite(gchar_pos(&end_visual)))
2837 inc(&end_visual);
2838 if (oap != NULL)
2839 oap->motion_type = MCHAR;
2840 if (oap != NULL
2841 && VIsual_mode == 'v'
2842 && !vim_iswordc(gchar_pos(&end_visual))
2843 && equalpos(curwin->w_cursor, VIsual)
2844 && (pos = findmatch(oap, NUL)) != NULL)
2845 {
2846 curwin->w_cursor = *pos;
2847 if (oap->motion_type == MLINE)
2848 VIsual_mode = 'V';
2849 else if (*p_sel == 'e')
2850 {
2851 if (lt(curwin->w_cursor, VIsual))
2852 ++VIsual.col;
2853 else
2854 ++curwin->w_cursor.col;
2855 }
2856 }
2857 }
2858
2859 if (pos == NULL && (is_click || is_drag))
2860 {
2861 /* When not found a match or when dragging: extend to include
2862 * a word. */
2863 if (lt(curwin->w_cursor, orig_cursor))
2864 {
2865 find_start_of_word(&curwin->w_cursor);
2866 find_end_of_word(&VIsual);
2867 }
2868 else
2869 {
2870 find_start_of_word(&VIsual);
2871 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
2872#ifdef FEAT_MBYTE
2873 curwin->w_cursor.col +=
2874 (*mb_ptr2len_check)(ml_get_cursor());
2875#else
2876 ++curwin->w_cursor.col;
2877#endif
2878 find_end_of_word(&curwin->w_cursor);
2879 }
2880 }
2881 curwin->w_set_curswant = TRUE;
2882 }
2883 if (is_click)
2884 redraw_curbuf_later(INVERTED); /* update the inversion */
2885 }
2886 else if (VIsual_active && !old_active)
2887 VIsual_mode = 'v';
2888
2889 /* If Visual mode changed show it later. */
2890 if (p_smd && (VIsual_active != old_active || VIsual_mode != old_mode))
2891 redraw_cmdline = TRUE;
2892#endif
2893
2894 return moved;
2895}
2896
2897#ifdef FEAT_VISUAL
2898/*
2899 * Move "pos" back to the start of the word it's in.
2900 */
2901 static void
2902find_start_of_word(pos)
2903 pos_T *pos;
2904{
2905 char_u *line;
2906 int cclass;
2907 int col;
2908
2909 line = ml_get(pos->lnum);
2910 cclass = get_mouse_class(line + pos->col);
2911
2912 while (pos->col > 0)
2913 {
2914 col = pos->col - 1;
2915#ifdef FEAT_MBYTE
2916 col -= (*mb_head_off)(line, line + col);
2917#endif
2918 if (get_mouse_class(line + col) != cclass)
2919 break;
2920 pos->col = col;
2921 }
2922}
2923
2924/*
2925 * Move "pos" forward to the end of the word it's in.
2926 * When 'selection' is "exclusive", the position is just after the word.
2927 */
2928 static void
2929find_end_of_word(pos)
2930 pos_T *pos;
2931{
2932 char_u *line;
2933 int cclass;
2934 int col;
2935
2936 line = ml_get(pos->lnum);
2937 if (*p_sel == 'e' && pos->col > 0)
2938 {
2939 --pos->col;
2940#ifdef FEAT_MBYTE
2941 pos->col -= (*mb_head_off)(line, line + pos->col);
2942#endif
2943 }
2944 cclass = get_mouse_class(line + pos->col);
2945 while (line[pos->col] != NUL)
2946 {
2947#ifdef FEAT_MBYTE
2948 col = pos->col + (*mb_ptr2len_check)(line + pos->col);
2949#else
2950 col = pos->col + 1;
2951#endif
2952 if (get_mouse_class(line + col) != cclass)
2953 {
2954 if (*p_sel == 'e')
2955 pos->col = col;
2956 break;
2957 }
2958 pos->col = col;
2959 }
2960}
2961
2962/*
2963 * Get class of a character for selection: same class means same word.
2964 * 0: blank
2965 * 1: punctuation groups
2966 * 2: normal word character
2967 * >2: multi-byte word character.
2968 */
2969 static int
2970get_mouse_class(p)
2971 char_u *p;
2972{
2973 int c;
2974
2975#ifdef FEAT_MBYTE
2976 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
2977 return mb_get_class(p);
2978#endif
2979
2980 c = *p;
2981 if (c == ' ' || c == '\t')
2982 return 0;
2983
2984 if (vim_iswordc(c))
2985 return 2;
2986
2987 /*
2988 * There are a few special cases where we want certain combinations of
2989 * characters to be considered as a single word. These are things like
2990 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
2991 * character is in it's own class.
2992 */
2993 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
2994 return 1;
2995 return c;
2996}
2997#endif /* FEAT_VISUAL */
2998#endif /* FEAT_MOUSE */
2999
3000#if defined(FEAT_VISUAL) || defined(PROTO)
3001/*
3002 * Check if highlighting for visual mode is possible, give a warning message
3003 * if not.
3004 */
3005 void
3006check_visual_highlight()
3007{
3008 static int did_check = FALSE;
3009
3010 if (full_screen)
3011 {
3012 if (!did_check && hl_attr(HLF_V) == 0)
3013 MSG(_("Warning: terminal cannot highlight"));
3014 did_check = TRUE;
3015 }
3016}
3017
3018/*
3019 * End visual mode.
3020 * This function should ALWAYS be called to end Visual mode, except from
3021 * do_pending_operator().
3022 */
3023 void
3024end_visual_mode()
3025{
3026#ifdef FEAT_CLIPBOARD
3027 /*
3028 * If we are using the clipboard, then remember what was selected in case
3029 * we need to paste it somewhere while we still own the selection.
3030 * Only do this when the clipboard is already owned. Don't want to grab
3031 * the selection when hitting ESC.
3032 */
3033 if (clip_star.available && clip_star.owned)
3034 clip_auto_select();
3035#endif
3036
3037 VIsual_active = FALSE;
3038#ifdef FEAT_MOUSE
3039 setmouse();
3040 mouse_dragging = 0;
3041#endif
3042
3043 /* Save the current VIsual area for '< and '> marks, and "gv" */
3044 curbuf->b_visual_mode = VIsual_mode;
3045#ifdef FEAT_EVAL
3046 curbuf->b_visual_mode_eval = VIsual_mode;
3047#endif
3048 curbuf->b_visual_start = VIsual;
3049 curbuf->b_visual_end = curwin->w_cursor;
3050 curbuf->b_visual_curswant = curwin->w_curswant;
3051#ifdef FEAT_VIRTUALEDIT
3052 if (!virtual_active())
3053 curwin->w_cursor.coladd = 0;
3054#endif
3055
3056 if (p_smd)
3057 clear_cmdline = TRUE; /* unshow visual mode later */
3058#ifdef FEAT_CMDL_INFO
3059 else
3060 clear_showcmd();
3061#endif
3062
3063 /* Don't leave the cursor past the end of the line */
3064 if (curwin->w_cursor.col > 0 && *ml_get_cursor() == NUL)
3065 --curwin->w_cursor.col;
3066}
3067
3068/*
3069 * Reset VIsual_active and VIsual_reselect.
3070 */
3071 void
3072reset_VIsual_and_resel()
3073{
3074 if (VIsual_active)
3075 {
3076 end_visual_mode();
3077 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3078 }
3079 VIsual_reselect = FALSE;
3080}
3081
3082/*
3083 * Reset VIsual_active and VIsual_reselect if it's set.
3084 */
3085 void
3086reset_VIsual()
3087{
3088 if (VIsual_active)
3089 {
3090 end_visual_mode();
3091 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3092 VIsual_reselect = FALSE;
3093 }
3094}
3095#endif /* FEAT_VISUAL */
3096
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003097#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3099
3100/*
3101 * Check for a balloon-eval special item to include when searching for an
3102 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3103 * Returns TRUE if the character at "*ptr" should be included.
3104 * "dir" is FORWARD or BACKWARD, the direction of searching.
3105 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3106 * "bnp" points to a counter for square brackets.
3107 */
3108 static int
3109find_is_eval_item(ptr, colp, bnp, dir)
3110 char_u *ptr;
3111 int *colp;
3112 int *bnp;
3113 int dir;
3114{
3115 /* Accept everything inside []. */
3116 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3117 ++*bnp;
3118 if (*bnp > 0)
3119 {
3120 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3121 --*bnp;
3122 return TRUE;
3123 }
3124
3125 /* skip over "s.var" */
3126 if (*ptr == '.')
3127 return TRUE;
3128
3129 /* two-character item: s->var */
3130 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3131 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3132 {
3133 *colp += dir;
3134 return TRUE;
3135 }
3136 return FALSE;
3137}
3138#endif
3139
3140/*
3141 * Find the identifier under or to the right of the cursor.
3142 * "find_type" can have one of three values:
3143 * FIND_IDENT: find an identifier (keyword)
3144 * FIND_STRING: find any non-white string
3145 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003146 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 *
3148 * There are three steps:
3149 * 1. Search forward for the start of an identifier/string. Doesn't move if
3150 * already on one.
3151 * 2. Search backward for the start of this identifier/string.
3152 * This doesn't match the real Vi but I like it a little better and it
3153 * shouldn't bother anyone.
3154 * 3. Search forward to the end of this identifier/string.
3155 * When FIND_IDENT isn't defined, we backup until a blank.
3156 *
3157 * Returns the length of the string, or zero if no string is found.
3158 * If a string is found, a pointer to the string is put in "*string". This
3159 * string is not always NUL terminated.
3160 */
3161 int
3162find_ident_under_cursor(string, find_type)
3163 char_u **string;
3164 int find_type;
3165{
3166 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3167 curwin->w_cursor.col, string, find_type);
3168}
3169
3170/*
3171 * Like find_ident_under_cursor(), but for any window and any position.
3172 * However: Uses 'iskeyword' from the current window!.
3173 */
3174 int
3175find_ident_at_pos(wp, lnum, startcol, string, find_type)
3176 win_T *wp;
3177 linenr_T lnum;
3178 colnr_T startcol;
3179 char_u **string;
3180 int find_type;
3181{
3182 char_u *ptr;
3183 int col = 0; /* init to shut up GCC */
3184 int i;
3185#ifdef FEAT_MBYTE
3186 int this_class = 0;
3187 int prev_class;
3188 int prevcol;
3189#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003190#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 int bn = 0; /* bracket nesting */
3192#endif
3193
3194 /*
3195 * if i == 0: try to find an identifier
3196 * if i == 1: try to find any non-white string
3197 */
3198 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3199 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3200 {
3201 /*
3202 * 1. skip to start of identifier/string
3203 */
3204 col = startcol;
3205#ifdef FEAT_MBYTE
3206 if (has_mbyte)
3207 {
3208 while (ptr[col] != NUL)
3209 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003210# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 /* Stop at a ']' to evaluate "a[x]". */
3212 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3213 break;
3214# endif
3215 this_class = mb_get_class(ptr + col);
3216 if (this_class != 0 && (i == 1 || this_class != 1))
3217 break;
3218 col += (*mb_ptr2len_check)(ptr + col);
3219 }
3220 }
3221 else
3222#endif
3223 while (ptr[col] != NUL
3224 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003225# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3227# endif
3228 )
3229 ++col;
3230
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003231#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 /* When starting on a ']' count it, so that we include the '['. */
3233 bn = ptr[col] == ']';
3234#endif
3235
3236 /*
3237 * 2. Back up to start of identifier/string.
3238 */
3239#ifdef FEAT_MBYTE
3240 if (has_mbyte)
3241 {
3242 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003243# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3245 this_class = mb_get_class((char_u *)"a");
3246 else
3247# endif
3248 this_class = mb_get_class(ptr + col);
3249 while (col > 0)
3250 {
3251 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3252 prev_class = mb_get_class(ptr + prevcol);
3253 if (this_class != prev_class
3254 && (i == 0
3255 || prev_class == 0
3256 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003257# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 && (!(find_type & FIND_EVAL)
3259 || prevcol == 0
3260 || !find_is_eval_item(ptr + prevcol, &prevcol,
3261 &bn, BACKWARD))
3262# endif
3263 )
3264 break;
3265 col = prevcol;
3266 }
3267
3268 /* If we don't want just any old string, or we've found an
3269 * identifier, stop searching. */
3270 if (this_class > 2)
3271 this_class = 2;
3272 if (!(find_type & FIND_STRING) || this_class == 2)
3273 break;
3274 }
3275 else
3276#endif
3277 {
3278 while (col > 0
3279 && ((i == 0
3280 ? vim_iswordc(ptr[col - 1])
3281 : (!vim_iswhite(ptr[col - 1])
3282 && (!(find_type & FIND_IDENT)
3283 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003284#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 || ((find_type & FIND_EVAL)
3286 && col > 1
3287 && find_is_eval_item(ptr + col - 1, &col,
3288 &bn, BACKWARD))
3289#endif
3290 ))
3291 --col;
3292
3293 /* If we don't want just any old string, or we've found an
3294 * identifier, stop searching. */
3295 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3296 break;
3297 }
3298 }
3299
3300 if (ptr[col] == NUL || (i == 0 && (
3301#ifdef FEAT_MBYTE
3302 has_mbyte ? this_class != 2 :
3303#endif
3304 !vim_iswordc(ptr[col]))))
3305 {
3306 /*
3307 * didn't find an identifier or string
3308 */
3309 if (find_type & FIND_STRING)
3310 EMSG(_("E348: No string under cursor"));
3311 else
3312 EMSG(_("E349: No identifier under cursor"));
3313 return 0;
3314 }
3315 ptr += col;
3316 *string = ptr;
3317
3318 /*
3319 * 3. Find the end if the identifier/string.
3320 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003321#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 bn = 0;
3323 startcol -= col;
3324#endif
3325 col = 0;
3326#ifdef FEAT_MBYTE
3327 if (has_mbyte)
3328 {
3329 /* Search for point of changing multibyte character class. */
3330 this_class = mb_get_class(ptr);
3331 while (ptr[col] != NUL
3332 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3333 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003334# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 || ((find_type & FIND_EVAL)
3336 && col <= (int)startcol
3337 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3338# endif
3339 ))
3340 col += (*mb_ptr2len_check)(ptr + col);
3341 }
3342 else
3343#endif
3344 while ((i == 0 ? vim_iswordc(ptr[col])
3345 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003346# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 || ((find_type & FIND_EVAL)
3348 && col <= (int)startcol
3349 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3350# endif
3351 )
3352 {
3353 ++col;
3354 }
3355
3356 return col;
3357}
3358
3359/*
3360 * Prepare for redo of a normal command.
3361 */
3362 static void
3363prep_redo_cmd(cap)
3364 cmdarg_T *cap;
3365{
3366 prep_redo(cap->oap->regname, cap->count0,
3367 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3368}
3369
3370/*
3371 * Prepare for redo of any command.
3372 * Note that only the last argument can be a multi-byte char.
3373 */
3374 static void
3375prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3376 int regname;
3377 long num;
3378 int cmd1;
3379 int cmd2;
3380 int cmd3;
3381 int cmd4;
3382 int cmd5;
3383{
3384 ResetRedobuff();
3385 if (regname != 0) /* yank from specified buffer */
3386 {
3387 AppendCharToRedobuff('"');
3388 AppendCharToRedobuff(regname);
3389 }
3390 if (num)
3391 AppendNumberToRedobuff(num);
3392
3393 if (cmd1 != NUL)
3394 AppendCharToRedobuff(cmd1);
3395 if (cmd2 != NUL)
3396 AppendCharToRedobuff(cmd2);
3397 if (cmd3 != NUL)
3398 AppendCharToRedobuff(cmd3);
3399 if (cmd4 != NUL)
3400 AppendCharToRedobuff(cmd4);
3401 if (cmd5 != NUL)
3402 AppendCharToRedobuff(cmd5);
3403}
3404
3405/*
3406 * check for operator active and clear it
3407 *
3408 * return TRUE if operator was active
3409 */
3410 static int
3411checkclearop(oap)
3412 oparg_T *oap;
3413{
3414 if (oap->op_type == OP_NOP)
3415 return FALSE;
3416 clearopbeep(oap);
3417 return TRUE;
3418}
3419
3420/*
3421 * check for operator or Visual active and clear it
3422 *
3423 * return TRUE if operator was active
3424 */
3425 static int
3426checkclearopq(oap)
3427 oparg_T *oap;
3428{
3429 if (oap->op_type == OP_NOP
3430#ifdef FEAT_VISUAL
3431 && !VIsual_active
3432#endif
3433 )
3434 return FALSE;
3435 clearopbeep(oap);
3436 return TRUE;
3437}
3438
3439 static void
3440clearop(oap)
3441 oparg_T *oap;
3442{
3443 oap->op_type = OP_NOP;
3444 oap->regname = 0;
3445 oap->motion_force = NUL;
3446 oap->use_reg_one = FALSE;
3447}
3448
3449 static void
3450clearopbeep(oap)
3451 oparg_T *oap;
3452{
3453 clearop(oap);
3454 beep_flush();
3455}
3456
3457#ifdef FEAT_VISUAL
3458/*
3459 * Remove the shift modifier from a special key.
3460 */
3461 static void
3462unshift_special(cap)
3463 cmdarg_T *cap;
3464{
3465 switch (cap->cmdchar)
3466 {
3467 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3468 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3469 case K_S_UP: cap->cmdchar = K_UP; break;
3470 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3471 case K_S_HOME: cap->cmdchar = K_HOME; break;
3472 case K_S_END: cap->cmdchar = K_END; break;
3473 }
3474 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3475}
3476#endif
3477
3478#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3479/*
3480 * Routines for displaying a partly typed command
3481 */
3482
3483#ifdef FEAT_VISUAL /* need room for size of Visual area */
3484# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3485#else
3486# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3487#endif
3488static char_u showcmd_buf[SHOWCMD_BUFLEN];
3489static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3490static int showcmd_is_clear = TRUE;
3491static int showcmd_visual = FALSE;
3492
3493static void display_showcmd __ARGS((void));
3494
3495 void
3496clear_showcmd()
3497{
3498 if (!p_sc)
3499 return;
3500
3501#ifdef FEAT_VISUAL
3502 if (VIsual_active && !char_avail())
3503 {
3504 int i = lt(VIsual, curwin->w_cursor);
3505 long lines;
3506 colnr_T leftcol, rightcol;
3507 linenr_T top, bot;
3508
3509 /* Show the size of the Visual area. */
3510 if (i)
3511 {
3512 top = VIsual.lnum;
3513 bot = curwin->w_cursor.lnum;
3514 }
3515 else
3516 {
3517 top = curwin->w_cursor.lnum;
3518 bot = VIsual.lnum;
3519 }
3520# ifdef FEAT_FOLDING
3521 /* Include closed folds as a whole. */
3522 hasFolding(top, &top, NULL);
3523 hasFolding(bot, NULL, &bot);
3524# endif
3525 lines = bot - top + 1;
3526
3527 if (VIsual_mode == Ctrl_V)
3528 {
3529 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
3530 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3531 (long)(rightcol - leftcol + 1));
3532 }
3533 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3534 sprintf((char *)showcmd_buf, "%ld", lines);
3535 else
3536 sprintf((char *)showcmd_buf, "%ld", (long)(i
3537 ? curwin->w_cursor.col - VIsual.col
3538 : VIsual.col - curwin->w_cursor.col) + (*p_sel != 'e'));
3539 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3540 showcmd_visual = TRUE;
3541 }
3542 else
3543#endif
3544 {
3545 showcmd_buf[0] = NUL;
3546 showcmd_visual = FALSE;
3547
3548 /* Don't actually display something if there is nothing to clear. */
3549 if (showcmd_is_clear)
3550 return;
3551 }
3552
3553 display_showcmd();
3554}
3555
3556/*
3557 * Add 'c' to string of shown command chars.
3558 * Return TRUE if output has been written (and setcursor() has been called).
3559 */
3560 int
3561add_to_showcmd(c)
3562 int c;
3563{
3564 char_u *p;
3565 int old_len;
3566 int extra_len;
3567 int overflow;
3568#if defined(FEAT_MOUSE)
3569 int i;
3570 static int ignore[] =
3571 {
3572#ifdef FEAT_GUI
3573 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3574 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
3575#endif
3576 K_IGNORE,
3577 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3578 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3579 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
3580 K_MOUSEDOWN, K_MOUSEUP,
3581 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
3582 0
3583 };
3584#endif
3585
3586 if (!p_sc)
3587 return FALSE;
3588
3589 if (showcmd_visual)
3590 {
3591 showcmd_buf[0] = NUL;
3592 showcmd_visual = FALSE;
3593 }
3594
3595#if defined(FEAT_MOUSE)
3596 /* Ignore keys that are scrollbar updates and mouse clicks */
3597 if (IS_SPECIAL(c))
3598 for (i = 0; ignore[i] != 0; ++i)
3599 if (ignore[i] == c)
3600 return FALSE;
3601#endif
3602
3603 p = transchar(c);
3604 old_len = (int)STRLEN(showcmd_buf);
3605 extra_len = (int)STRLEN(p);
3606 overflow = old_len + extra_len - SHOWCMD_COLS;
3607 if (overflow > 0)
3608 STRCPY(showcmd_buf, showcmd_buf + overflow);
3609 STRCAT(showcmd_buf, p);
3610
3611 if (char_avail())
3612 return FALSE;
3613
3614 display_showcmd();
3615
3616 return TRUE;
3617}
3618
3619 void
3620add_to_showcmd_c(c)
3621 int c;
3622{
3623 if (!add_to_showcmd(c))
3624 setcursor();
3625}
3626
3627/*
3628 * Delete 'len' characters from the end of the shown command.
3629 */
3630 static void
3631del_from_showcmd(len)
3632 int len;
3633{
3634 int old_len;
3635
3636 if (!p_sc)
3637 return;
3638
3639 old_len = (int)STRLEN(showcmd_buf);
3640 if (len > old_len)
3641 len = old_len;
3642 showcmd_buf[old_len - len] = NUL;
3643
3644 if (!char_avail())
3645 display_showcmd();
3646}
3647
3648/*
3649 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3650 * something and there is a partial mapping.
3651 */
3652 void
3653push_showcmd()
3654{
3655 if (p_sc)
3656 STRCPY(old_showcmd_buf, showcmd_buf);
3657}
3658
3659 void
3660pop_showcmd()
3661{
3662 if (!p_sc)
3663 return;
3664
3665 STRCPY(showcmd_buf, old_showcmd_buf);
3666
3667 display_showcmd();
3668}
3669
3670 static void
3671display_showcmd()
3672{
3673 int len;
3674
3675 cursor_off();
3676
3677 len = (int)STRLEN(showcmd_buf);
3678 if (len == 0)
3679 showcmd_is_clear = TRUE;
3680 else
3681 {
3682 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3683 showcmd_is_clear = FALSE;
3684 }
3685
3686 /*
3687 * clear the rest of an old message by outputing up to SHOWCMD_COLS spaces
3688 */
3689 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3690
3691 setcursor(); /* put cursor back where it belongs */
3692}
3693#endif
3694
3695#ifdef FEAT_SCROLLBIND
3696/*
3697 * When "check" is FALSE, prepare for commands that scroll the window.
3698 * When "check" is TRUE, take care of scroll-binding after the window has
3699 * scrolled. Called from normal_cmd() and edit().
3700 */
3701 void
3702do_check_scrollbind(check)
3703 int check;
3704{
3705 static win_T *old_curwin = NULL;
3706 static linenr_T old_topline = 0;
3707#ifdef FEAT_DIFF
3708 static int old_topfill = 0;
3709#endif
3710 static buf_T *old_buf = NULL;
3711 static colnr_T old_leftcol = 0;
3712
3713 if (check && curwin->w_p_scb)
3714 {
3715 /* If a ":syncbind" command was just used, don't scroll, only reset
3716 * the values. */
3717 if (did_syncbind)
3718 did_syncbind = FALSE;
3719 else if (curwin == old_curwin)
3720 {
3721 /*
3722 * Synchronize other windows, as necessary according to
3723 * 'scrollbind'. Don't do this after an ":edit" command, except
3724 * when 'diff' is set.
3725 */
3726 if ((curwin->w_buffer == old_buf
3727#ifdef FEAT_DIFF
3728 || curwin->w_p_diff
3729#endif
3730 )
3731 && (curwin->w_topline != old_topline
3732#ifdef FEAT_DIFF
3733 || curwin->w_topfill != old_topfill
3734#endif
3735 || curwin->w_leftcol != old_leftcol))
3736 {
3737 check_scrollbind(curwin->w_topline - old_topline,
3738 (long)(curwin->w_leftcol - old_leftcol));
3739 }
3740 }
3741 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
3742 {
3743 /*
3744 * When switching between windows, make sure that the relative
3745 * vertical offset is valid for the new window. The relative
3746 * offset is invalid whenever another 'scrollbind' window has
3747 * scrolled to a point that would force the current window to
3748 * scroll past the beginning or end of its buffer. When the
3749 * resync is performed, some of the other 'scrollbind' windows may
3750 * need to jump so that the current window's relative position is
3751 * visible on-screen.
3752 */
3753 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
3754 }
3755 curwin->w_scbind_pos = curwin->w_topline;
3756 }
3757
3758 old_curwin = curwin;
3759 old_topline = curwin->w_topline;
3760#ifdef FEAT_DIFF
3761 old_topfill = curwin->w_topfill;
3762#endif
3763 old_buf = curwin->w_buffer;
3764 old_leftcol = curwin->w_leftcol;
3765}
3766
3767/*
3768 * Synchronize any windows that have "scrollbind" set, based on the
3769 * number of rows by which the current window has changed
3770 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3771 */
3772 void
3773check_scrollbind(topline_diff, leftcol_diff)
3774 linenr_T topline_diff;
3775 long leftcol_diff;
3776{
3777 int want_ver;
3778 int want_hor;
3779 win_T *old_curwin = curwin;
3780 buf_T *old_curbuf = curbuf;
3781#ifdef FEAT_VISUAL
3782 int old_VIsual_select = VIsual_select;
3783 int old_VIsual_active = VIsual_active;
3784#endif
3785 colnr_T tgt_leftcol = curwin->w_leftcol;
3786 long topline;
3787 long y;
3788
3789 /*
3790 * check 'scrollopt' string for vertical and horizontal scroll options
3791 */
3792 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
3793#ifdef FEAT_DIFF
3794 want_ver |= old_curwin->w_p_diff;
3795#endif
3796 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
3797
3798 /*
3799 * loop through the scrollbound windows and scroll accordingly
3800 */
3801#ifdef FEAT_VISUAL
3802 VIsual_select = VIsual_active = 0;
3803#endif
3804 for (curwin = firstwin; curwin; curwin = curwin->w_next)
3805 {
3806 curbuf = curwin->w_buffer;
3807 /* skip original window and windows with 'noscrollbind' */
3808 if (curwin != old_curwin && curwin->w_p_scb)
3809 {
3810 /*
3811 * do the vertical scroll
3812 */
3813 if (want_ver)
3814 {
3815#ifdef FEAT_DIFF
3816 if (old_curwin->w_p_diff && curwin->w_p_diff)
3817 {
3818 diff_set_topline(old_curwin, curwin);
3819 }
3820 else
3821#endif
3822 {
3823 curwin->w_scbind_pos += topline_diff;
3824 topline = curwin->w_scbind_pos;
3825 if (topline > curbuf->b_ml.ml_line_count)
3826 topline = curbuf->b_ml.ml_line_count;
3827 if (topline < 1)
3828 topline = 1;
3829
3830 y = topline - curwin->w_topline;
3831 if (y > 0)
3832 scrollup(y, FALSE);
3833 else
3834 scrolldown(-y, FALSE);
3835 }
3836
3837 redraw_later(VALID);
3838 cursor_correct();
3839#ifdef FEAT_WINDOWS
3840 curwin->w_redr_status = TRUE;
3841#endif
3842 }
3843
3844 /*
3845 * do the horizontal scroll
3846 */
3847 if (want_hor && curwin->w_leftcol != tgt_leftcol)
3848 {
3849 curwin->w_leftcol = tgt_leftcol;
3850 leftcol_changed();
3851 }
3852 }
3853 }
3854
3855 /*
3856 * reset current-window
3857 */
3858#ifdef FEAT_VISUAL
3859 VIsual_select = old_VIsual_select;
3860 VIsual_active = old_VIsual_active;
3861#endif
3862 curwin = old_curwin;
3863 curbuf = old_curbuf;
3864}
3865#endif /* #ifdef FEAT_SCROLLBIND */
3866
3867/*
3868 * Command character that's ignored.
3869 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
3870 * xon/xoff
3871 */
3872/*ARGSUSED */
3873 static void
3874nv_ignore(cap)
3875 cmdarg_T *cap;
3876{
3877}
3878
3879/*
3880 * Command character doesn't exist.
3881 */
3882 static void
3883nv_error(cap)
3884 cmdarg_T *cap;
3885{
3886 clearopbeep(cap->oap);
3887}
3888
3889/*
3890 * <Help> and <F1> commands.
3891 */
3892 static void
3893nv_help(cap)
3894 cmdarg_T *cap;
3895{
3896 if (!checkclearopq(cap->oap))
3897 ex_help(NULL);
3898}
3899
3900/*
3901 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
3902 */
3903 static void
3904nv_addsub(cap)
3905 cmdarg_T *cap;
3906{
3907 if (!checkclearopq(cap->oap)
3908 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
3909 prep_redo_cmd(cap);
3910}
3911
3912/*
3913 * CTRL-F, CTRL-B, etc: Scroll page up or down.
3914 */
3915 static void
3916nv_page(cap)
3917 cmdarg_T *cap;
3918{
3919 if (!checkclearop(cap->oap))
3920 (void)onepage(cap->arg, cap->count1);
3921}
3922
3923/*
3924 * Implementation of "gd" and "gD" command.
3925 */
3926 static void
3927nv_gd(oap, nchar)
3928 oparg_T *oap;
3929 int nchar;
3930{
3931 int len;
3932 char_u *pat;
3933 pos_T old_pos;
3934 int t;
3935 int save_p_ws;
3936 int save_p_scs;
3937 char_u *ptr;
3938
3939 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0 ||
3940 (pat = alloc(len + 5)) == NULL)
3941 {
3942 clearopbeep(oap);
3943 return;
3944 }
3945 sprintf((char *)pat, vim_iswordp(ptr) ? "\\<%.*s\\>" : "%.*s", len, ptr);
3946 old_pos = curwin->w_cursor;
3947 save_p_ws = p_ws;
3948 save_p_scs = p_scs;
3949 p_ws = FALSE; /* don't wrap around end of file now */
3950 p_scs = FALSE; /* don't switch ignorecase off now */
3951
3952 /*
3953 * With "gD" go to line 1.
3954 * With "gd" Search back for the start of the current function, then go
3955 * back until a blank line. If this fails go to line 1.
3956 */
3957 if (nchar == 'D' || !findpar(oap, BACKWARD, 1L, '{', FALSE))
3958 {
3959 setpcmark(); /* Set in findpar() otherwise */
3960 curwin->w_cursor.lnum = 1;
3961 }
3962 else
3963 {
3964 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
3965 --curwin->w_cursor.lnum;
3966 }
3967 curwin->w_cursor.col = 0;
3968
3969 /* Search forward for the identifier, ignore comment lines. */
3970 while ((t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD, pat, 1L, 0,
3971 RE_LAST)) != FAIL
3972#ifdef FEAT_COMMENTS
3973 && get_leader_len(ml_get_curline(), NULL, FALSE) > 0
3974#endif
3975 && old_pos.lnum > curwin->w_cursor.lnum)
3976 {
3977 /* Ignore this line, continue at start of next line. */
3978 ++curwin->w_cursor.lnum;
3979 curwin->w_cursor.col = 0;
3980 }
3981 if (t == FAIL || old_pos.lnum <= curwin->w_cursor.lnum)
3982 {
3983 clearopbeep(oap);
3984 curwin->w_cursor = old_pos;
3985 }
3986 else
3987 {
3988 curwin->w_set_curswant = TRUE;
3989#ifdef FEAT_FOLDING
3990 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
3991 foldOpenCursor();
3992#endif
3993 /* "n" searches forward now */
3994 reset_search_dir();
3995 }
3996
3997 vim_free(pat);
3998 p_ws = save_p_ws;
3999 p_scs = save_p_scs;
4000}
4001
4002/*
4003 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4004 * lines rather than lines in the file.
4005 * 'dist' must be positive.
4006 *
4007 * Return OK if able to move cursor, FAIL otherwise.
4008 */
4009 static int
4010nv_screengo(oap, dir, dist)
4011 oparg_T *oap;
4012 int dir;
4013 long dist;
4014{
4015 int linelen = linetabsize(ml_get_curline());
4016 int retval = OK;
4017 int atend = FALSE;
4018 int n;
4019 int col_off1; /* margin offset for first screen line */
4020 int col_off2; /* margin offset for wrapped screen line */
4021 int width1; /* text width for first screen line */
4022 int width2; /* test width for wrapped screen line */
4023
4024 oap->motion_type = MCHAR;
4025 oap->inclusive = FALSE;
4026
4027 col_off1 = curwin_col_off();
4028 col_off2 = col_off1 - curwin_col_off2();
4029 width1 = W_WIDTH(curwin) - col_off1;
4030 width2 = W_WIDTH(curwin) - col_off2;
4031
4032#ifdef FEAT_VERTSPLIT
4033 if (curwin->w_width != 0)
4034 {
4035#endif
4036 /*
4037 * Instead of sticking at the last character of the buffer line we
4038 * try to stick in the last column of the screen.
4039 */
4040 if (curwin->w_curswant == MAXCOL)
4041 {
4042 atend = TRUE;
4043 validate_virtcol();
4044 if (width1 <= 0)
4045 curwin->w_curswant = 0;
4046 else
4047 {
4048 curwin->w_curswant = width1 - 1;
4049 if (curwin->w_virtcol > curwin->w_curswant)
4050 curwin->w_curswant += ((curwin->w_virtcol
4051 - curwin->w_curswant - 1) / width2 + 1) * width2;
4052 }
4053 }
4054 else
4055 {
4056 if (linelen > width1)
4057 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4058 else
4059 n = width1;
4060 if (curwin->w_curswant > (colnr_T)n + 1)
4061 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4062 * width2;
4063 }
4064
4065 while (dist--)
4066 {
4067 if (dir == BACKWARD)
4068 {
4069 if ((long)curwin->w_curswant >= width2)
4070 /* move back within line */
4071 curwin->w_curswant -= width2;
4072 else
4073 {
4074 /* to previous line */
4075 if (curwin->w_cursor.lnum == 1)
4076 {
4077 retval = FAIL;
4078 break;
4079 }
4080 --curwin->w_cursor.lnum;
4081#ifdef FEAT_FOLDING
4082 /* Move to the start of a closed fold. Don't do that when
4083 * 'foldopen' contains "all": it will open in a moment. */
4084 if (!(fdo_flags & FDO_ALL))
4085 (void)hasFolding(curwin->w_cursor.lnum,
4086 &curwin->w_cursor.lnum, NULL);
4087#endif
4088 linelen = linetabsize(ml_get_curline());
4089 if (linelen > width1)
4090 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4091 + 1) * width2;
4092 }
4093 }
4094 else /* dir == FORWARD */
4095 {
4096 if (linelen > width1)
4097 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4098 else
4099 n = width1;
4100 if (curwin->w_curswant + width2 < (colnr_T)n)
4101 /* move forward within line */
4102 curwin->w_curswant += width2;
4103 else
4104 {
4105 /* to next line */
4106#ifdef FEAT_FOLDING
4107 /* Move to the end of a closed fold. */
4108 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4109 &curwin->w_cursor.lnum);
4110#endif
4111 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4112 {
4113 retval = FAIL;
4114 break;
4115 }
4116 curwin->w_cursor.lnum++;
4117 curwin->w_curswant %= width2;
4118 }
4119 }
4120 }
4121#ifdef FEAT_VERTSPLIT
4122 }
4123#endif
4124
4125 coladvance(curwin->w_curswant);
4126
4127#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4128 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4129 {
4130 /*
4131 * Check for landing on a character that got split at the end of the
4132 * last line. We want to advance a screenline, not end up in the same
4133 * screenline or move two screenlines.
4134 */
4135 validate_virtcol();
4136 if (curwin->w_virtcol > curwin->w_curswant
4137 && (curwin->w_curswant < (colnr_T)width1
4138 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4139 : ((curwin->w_curswant - width1) % width2
4140 > (colnr_T)width2 / 2)))
4141 --curwin->w_cursor.col;
4142 }
4143#endif
4144
4145 if (atend)
4146 curwin->w_curswant = MAXCOL; /* stick in the last column */
4147
4148 return retval;
4149}
4150
4151#ifdef FEAT_MOUSE
4152/*
4153 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4154 * when Shift or Ctrl is used.
4155 * K_MOUSEUP (cap->arg == TRUE) or K_MOUSEDOWN (cap->arg == FALSE)
4156 */
4157 static void
4158nv_mousescroll(cap)
4159 cmdarg_T *cap;
4160{
4161# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4162 win_T *old_curwin;
4163
4164 old_curwin = curwin;
4165
4166 /* Currently we only get the mouse coordinates in the GUI. */
4167 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4168 {
4169 int row, col;
4170
4171 row = mouse_row;
4172 col = mouse_col;
4173
4174 /* find the window at the pointer coordinates */
4175 curwin = mouse_find_win(&row, &col);
4176 curbuf = curwin->w_buffer;
4177 }
4178# endif
4179
4180 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4181 {
4182 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4183 }
4184 else
4185 {
4186 cap->count1 = 3;
4187 cap->count0 = 3;
4188 nv_scroll_line(cap);
4189 }
4190
4191# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4192 curwin->w_redr_status = TRUE;
4193
4194 curwin = old_curwin;
4195 curbuf = curwin->w_buffer;
4196# endif
4197}
4198
4199/*
4200 * Mouse clicks and drags.
4201 */
4202 static void
4203nv_mouse(cap)
4204 cmdarg_T *cap;
4205{
4206 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4207}
4208#endif
4209
4210/*
4211 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4212 * cap->arg must be TRUE for CTRL-E.
4213 */
4214 static void
4215nv_scroll_line(cap)
4216 cmdarg_T *cap;
4217{
4218 if (!checkclearop(cap->oap))
4219 scroll_redraw(cap->arg, cap->count1);
4220}
4221
4222/*
4223 * Scroll "count" lines up or down, and redraw.
4224 */
4225 void
4226scroll_redraw(up, count)
4227 int up;
4228 long count;
4229{
4230 linenr_T prev_topline = curwin->w_topline;
4231#ifdef FEAT_DIFF
4232 int prev_topfill = curwin->w_topfill;
4233#endif
4234 linenr_T prev_lnum = curwin->w_cursor.lnum;
4235
4236 if (up)
4237 scrollup(count, TRUE);
4238 else
4239 scrolldown(count, TRUE);
4240 if (p_so)
4241 {
4242 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4243 * valid, otherwise the screen jumps back at the end of the file. */
4244 cursor_correct();
4245 check_cursor_moved(curwin);
4246 curwin->w_valid |= VALID_TOPLINE;
4247
4248 /* If moved back to where we were, at least move the cursor, otherwise
4249 * we get stuck at one position. Don't move the cursor up if the
4250 * first line of the buffer is already on the screen */
4251 while (curwin->w_topline == prev_topline
4252#ifdef FEAT_DIFF
4253 && curwin->w_topfill == prev_topfill
4254#endif
4255 )
4256 {
4257 if (up)
4258 {
4259 if (curwin->w_cursor.lnum > prev_lnum
4260 || cursor_down(1L, FALSE) == FAIL)
4261 break;
4262 }
4263 else
4264 {
4265 if (curwin->w_cursor.lnum < prev_lnum
4266 || prev_topline == 1L
4267 || cursor_up(1L, FALSE) == FAIL)
4268 break;
4269 }
4270 /* Mark w_topline as valid, otherwise the screen jumps back at the
4271 * end of the file. */
4272 check_cursor_moved(curwin);
4273 curwin->w_valid |= VALID_TOPLINE;
4274 }
4275 }
4276 if (curwin->w_cursor.lnum != prev_lnum)
4277 coladvance(curwin->w_curswant);
4278 redraw_later(VALID);
4279}
4280
4281/*
4282 * Commands that start with "z".
4283 */
4284 static void
4285nv_zet(cap)
4286 cmdarg_T *cap;
4287{
4288 long n;
4289 colnr_T col;
4290 int nchar = cap->nchar;
4291#ifdef FEAT_FOLDING
4292 long old_fdl = curwin->w_p_fdl;
4293 int old_fen = curwin->w_p_fen;
4294#endif
4295
4296 if (VIM_ISDIGIT(nchar))
4297 {
4298 /*
4299 * "z123{nchar}": edit the count before obtaining {nchar}
4300 */
4301 if (checkclearop(cap->oap))
4302 return;
4303 n = nchar - '0';
4304 for (;;)
4305 {
4306#ifdef USE_ON_FLY_SCROLL
4307 dont_scroll = TRUE; /* disallow scrolling here */
4308#endif
4309 ++no_mapping;
4310 ++allow_keys; /* no mapping for nchar, but allow key codes */
4311 nchar = safe_vgetc();
4312#ifdef FEAT_LANGMAP
4313 LANGMAP_ADJUST(nchar, TRUE);
4314#endif
4315 --no_mapping;
4316 --allow_keys;
4317#ifdef FEAT_CMDL_INFO
4318 (void)add_to_showcmd(nchar);
4319#endif
4320 if (nchar == K_DEL || nchar == K_KDEL)
4321 n /= 10;
4322 else if (VIM_ISDIGIT(nchar))
4323 n = n * 10 + (nchar - '0');
4324 else if (nchar == CAR)
4325 {
4326#ifdef FEAT_GUI
4327 need_mouse_correct = TRUE;
4328#endif
4329 win_setheight((int)n);
4330 break;
4331 }
4332 else if (nchar == 'l'
4333 || nchar == 'h'
4334 || nchar == K_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004335 || nchar == K_XLEFT
4336 || nchar == K_RIGHT
4337 || nchar == K_XRIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 {
4339 cap->count1 = n ? n * cap->count1 : cap->count1;
4340 goto dozet;
4341 }
4342 else
4343 {
4344 clearopbeep(cap->oap);
4345 break;
4346 }
4347 }
4348 cap->oap->op_type = OP_NOP;
4349 return;
4350 }
4351
4352dozet:
4353 if (
4354#ifdef FEAT_FOLDING
4355 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4356 * and "zC" only in Visual mode. "zj" and "zk" are motion
4357 * commands. */
4358 cap->nchar != 'f' && cap->nchar != 'F'
4359 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4360 && cap->nchar != 'j' && cap->nchar != 'k'
4361 &&
4362#endif
4363 checkclearop(cap->oap))
4364 return;
4365
4366 /*
4367 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4368 * If line number given, set cursor.
4369 */
4370 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4371 && cap->count0
4372 && cap->count0 != curwin->w_cursor.lnum)
4373 {
4374 setpcmark();
4375 if (cap->count0 > curbuf->b_ml.ml_line_count)
4376 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4377 else
4378 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004379 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 }
4381
4382 switch (nchar)
4383 {
4384 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4385 case '+':
4386 if (cap->count0 == 0)
4387 {
4388 /* No count given: put cursor at the line below screen */
4389 validate_botline(); /* make sure w_botline is valid */
4390 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4391 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4392 else
4393 curwin->w_cursor.lnum = curwin->w_botline;
4394 }
4395 /* FALLTHROUGH */
4396 case NL:
4397 case CAR:
4398 case K_KENTER:
4399 beginline(BL_WHITE | BL_FIX);
4400 /* FALLTHROUGH */
4401
4402 case 't': scroll_cursor_top(0, TRUE);
4403 redraw_later(VALID);
4404 break;
4405
4406 /* "z." and "zz": put cursor in middle of screen */
4407 case '.': beginline(BL_WHITE | BL_FIX);
4408 /* FALLTHROUGH */
4409
4410 case 'z': scroll_cursor_halfway(TRUE);
4411 redraw_later(VALID);
4412 break;
4413
4414 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4415 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4416 * when <count> is at bottom of window, and puts that one at
4417 * bottom of window. */
4418 if (cap->count0 != 0)
4419 {
4420 scroll_cursor_bot(0, TRUE);
4421 curwin->w_cursor.lnum = curwin->w_topline;
4422 }
4423 else if (curwin->w_topline == 1)
4424 curwin->w_cursor.lnum = 1;
4425 else
4426 curwin->w_cursor.lnum = curwin->w_topline - 1;
4427 /* FALLTHROUGH */
4428 case '-':
4429 beginline(BL_WHITE | BL_FIX);
4430 /* FALLTHROUGH */
4431
4432 case 'b': scroll_cursor_bot(0, TRUE);
4433 redraw_later(VALID);
4434 break;
4435
4436 /* "zH" - scroll screen right half-page */
4437 case 'H':
4438 cap->count1 *= W_WIDTH(curwin) / 2;
4439 /* FALLTHROUGH */
4440
4441 /* "zh" - scroll screen to the right */
4442 case 'h':
4443 case K_LEFT:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004444 case K_XLEFT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 if (!curwin->w_p_wrap)
4446 {
4447 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4448 curwin->w_leftcol = 0;
4449 else
4450 curwin->w_leftcol -= (colnr_T)cap->count1;
4451 leftcol_changed();
4452 }
4453 break;
4454
4455 /* "zL" - scroll screen left half-page */
4456 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4457 /* FALLTHROUGH */
4458
4459 /* "zl" - scroll screen to the left */
4460 case 'l':
4461 case K_RIGHT:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004462 case K_XRIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 if (!curwin->w_p_wrap)
4464 {
4465 /* scroll the window left */
4466 curwin->w_leftcol += (colnr_T)cap->count1;
4467 leftcol_changed();
4468 }
4469 break;
4470
4471 /* "zs" - scroll screen, cursor at the start */
4472 case 's': if (!curwin->w_p_wrap)
4473 {
4474#ifdef FEAT_FOLDING
4475 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4476 col = 0; /* like the cursor is in col 0 */
4477 else
4478#endif
4479 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4480 if ((long)col > p_siso)
4481 col -= p_siso;
4482 else
4483 col = 0;
4484 if (curwin->w_leftcol != col)
4485 {
4486 curwin->w_leftcol = col;
4487 redraw_later(NOT_VALID);
4488 }
4489 }
4490 break;
4491
4492 /* "ze" - scroll screen, cursor at the end */
4493 case 'e': if (!curwin->w_p_wrap)
4494 {
4495#ifdef FEAT_FOLDING
4496 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4497 col = 0; /* like the cursor is in col 0 */
4498 else
4499#endif
4500 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4501 n = W_WIDTH(curwin) - curwin_col_off();
4502 if ((long)col + p_siso < n)
4503 col = 0;
4504 else
4505 col = col + p_siso - n + 1;
4506 if (curwin->w_leftcol != col)
4507 {
4508 curwin->w_leftcol = col;
4509 redraw_later(NOT_VALID);
4510 }
4511 }
4512 break;
4513
4514#ifdef FEAT_FOLDING
4515 /* "zF": create fold command */
4516 /* "zf": create fold operator */
4517 case 'F':
4518 case 'f': if (foldManualAllowed(TRUE))
4519 {
4520 cap->nchar = 'f';
4521 nv_operator(cap);
4522 curwin->w_p_fen = TRUE;
4523
4524 /* "zF" is like "zfzf" */
4525 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4526 {
4527 nv_operator(cap);
4528 finish_op = TRUE;
4529 }
4530 }
4531 else
4532 clearopbeep(cap->oap);
4533 break;
4534
4535 /* "zd": delete fold at cursor */
4536 /* "zD": delete fold at cursor recursively */
4537 case 'd':
4538 case 'D': if (foldManualAllowed(FALSE))
4539 {
4540 if (VIsual_active)
4541 nv_operator(cap);
4542 else
4543 deleteFold(curwin->w_cursor.lnum,
4544 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4545 }
4546 break;
4547
4548 /* "zE": erease all folds */
4549 case 'E': if (foldmethodIsManual(curwin))
4550 {
4551 clearFolding(curwin);
4552 changed_window_setting();
4553 }
4554 else if (foldmethodIsMarker(curwin))
4555 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4556 TRUE, FALSE);
4557 else
4558 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4559 break;
4560
4561 /* "zn": fold none: reset 'foldenable' */
4562 case 'n': curwin->w_p_fen = FALSE;
4563 break;
4564
4565 /* "zN": fold Normal: set 'foldenable' */
4566 case 'N': curwin->w_p_fen = TRUE;
4567 break;
4568
4569 /* "zi": invert folding: toggle 'foldenable' */
4570 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4571 break;
4572
4573 /* "za": open closed fold or close open fold at cursor */
4574 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4575 openFold(curwin->w_cursor.lnum, cap->count1);
4576 else
4577 {
4578 closeFold(curwin->w_cursor.lnum, cap->count1);
4579 curwin->w_p_fen = TRUE;
4580 }
4581 break;
4582
4583 /* "zA": open fold at cursor recursively */
4584 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4585 openFoldRecurse(curwin->w_cursor.lnum);
4586 else
4587 {
4588 closeFoldRecurse(curwin->w_cursor.lnum);
4589 curwin->w_p_fen = TRUE;
4590 }
4591 break;
4592
4593 /* "zo": open fold at cursor or Visual area */
4594 case 'o': if (VIsual_active)
4595 nv_operator(cap);
4596 else
4597 openFold(curwin->w_cursor.lnum, cap->count1);
4598 break;
4599
4600 /* "zO": open fold recursively */
4601 case 'O': if (VIsual_active)
4602 nv_operator(cap);
4603 else
4604 openFoldRecurse(curwin->w_cursor.lnum);
4605 break;
4606
4607 /* "zc": close fold at cursor or Visual area */
4608 case 'c': if (VIsual_active)
4609 nv_operator(cap);
4610 else
4611 closeFold(curwin->w_cursor.lnum, cap->count1);
4612 curwin->w_p_fen = TRUE;
4613 break;
4614
4615 /* "zC": close fold recursively */
4616 case 'C': if (VIsual_active)
4617 nv_operator(cap);
4618 else
4619 closeFoldRecurse(curwin->w_cursor.lnum);
4620 curwin->w_p_fen = TRUE;
4621 break;
4622
4623 /* "zv": open folds at the cursor */
4624 case 'v': foldOpenCursor();
4625 break;
4626
4627 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
4628 case 'x': curwin->w_p_fen = TRUE;
4629 newFoldLevel(); /* update right now */
4630 foldOpenCursor();
4631 break;
4632
4633 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
4634 case 'X': curwin->w_p_fen = TRUE;
4635 old_fdl = -1; /* force an update */
4636 break;
4637
4638 /* "zm": fold more */
4639 case 'm': if (curwin->w_p_fdl > 0)
4640 --curwin->w_p_fdl;
4641 old_fdl = -1; /* force an update */
4642 curwin->w_p_fen = TRUE;
4643 break;
4644
4645 /* "zM": close all folds */
4646 case 'M': curwin->w_p_fdl = 0;
4647 old_fdl = -1; /* force an update */
4648 curwin->w_p_fen = TRUE;
4649 break;
4650
4651 /* "zr": reduce folding */
4652 case 'r': ++curwin->w_p_fdl;
4653 break;
4654
4655 /* "zR": open all folds */
4656 case 'R': curwin->w_p_fdl = getDeepestNesting();
4657 old_fdl = -1; /* force an update */
4658 break;
4659
4660 case 'j': /* "zj" move to next fold downwards */
4661 case 'k': /* "zk" move to next fold upwards */
4662 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
4663 cap->count1) == FAIL)
4664 clearopbeep(cap->oap);
4665 break;
4666
4667#endif /* FEAT_FOLDING */
4668
4669 default: clearopbeep(cap->oap);
4670 }
4671
4672#ifdef FEAT_FOLDING
4673 /* Redraw when 'foldenable' changed */
4674 if (old_fen != curwin->w_p_fen)
4675 {
4676# ifdef FEAT_DIFF
4677 win_T *wp;
4678
4679 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
4680 {
4681 /* Adjust 'foldenable' in diff-synced windows. */
4682 FOR_ALL_WINDOWS(wp)
4683 {
4684 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
4685 {
4686 wp->w_p_fen = curwin->w_p_fen;
4687 changed_window_setting_win(wp);
4688 }
4689 }
4690 }
4691# endif
4692 changed_window_setting();
4693 }
4694
4695 /* Redraw when 'foldlevel' changed. */
4696 if (old_fdl != curwin->w_p_fdl)
4697 newFoldLevel();
4698#endif
4699}
4700
4701#ifdef FEAT_GUI
4702/*
4703 * Vertical scrollbar movement.
4704 */
4705 static void
4706nv_ver_scrollbar(cap)
4707 cmdarg_T *cap;
4708{
4709 if (cap->oap->op_type != OP_NOP)
4710 clearopbeep(cap->oap);
4711
4712 /* Even if an operator was pending, we still want to scroll */
4713 gui_do_scroll();
4714}
4715
4716/*
4717 * Horizontal scrollbar movement.
4718 */
4719 static void
4720nv_hor_scrollbar(cap)
4721 cmdarg_T *cap;
4722{
4723 if (cap->oap->op_type != OP_NOP)
4724 clearopbeep(cap->oap);
4725
4726 /* Even if an operator was pending, we still want to scroll */
4727 gui_do_horiz_scroll();
4728}
4729#endif
4730
4731/*
4732 * "Q" command.
4733 */
4734 static void
4735nv_exmode(cap)
4736 cmdarg_T *cap;
4737{
4738 /*
4739 * Ignore 'Q' in Visual mode, just give a beep.
4740 */
4741#ifdef FEAT_VISUAL
4742 if (VIsual_active)
4743 vim_beep();
4744 else
4745#endif
4746 if (!checkclearop(cap->oap))
4747 do_exmode(FALSE);
4748}
4749
4750/*
4751 * Handle a ":" command.
4752 */
4753 static void
4754nv_colon(cap)
4755 cmdarg_T *cap;
4756{
4757 int old_p_im;
4758
4759#ifdef FEAT_VISUAL
4760 if (VIsual_active)
4761 nv_operator(cap);
4762 else
4763#endif
4764 {
4765 if (cap->oap->op_type != OP_NOP)
4766 {
4767 /* Using ":" as a movement is characterwise exclusive. */
4768 cap->oap->motion_type = MCHAR;
4769 cap->oap->inclusive = FALSE;
4770 }
4771 else if (cap->count0)
4772 {
4773 /* translate "count:" into ":.,.+(count - 1)" */
4774 stuffcharReadbuff('.');
4775 if (cap->count0 > 1)
4776 {
4777 stuffReadbuff((char_u *)",.+");
4778 stuffnumReadbuff((long)cap->count0 - 1L);
4779 }
4780 }
4781
4782 /* When typing, don't type below an old message */
4783 if (KeyTyped)
4784 compute_cmdrow();
4785
4786 old_p_im = p_im;
4787
4788 /* get a command line and execute it */
4789 do_cmdline(NULL, getexline, NULL,
4790 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
4791
4792 /* If 'insertmode' changed, enter or exit Insert mode */
4793 if (p_im != old_p_im)
4794 {
4795 if (p_im)
4796 restart_edit = 'i';
4797 else
4798 restart_edit = 0;
4799 }
4800
4801 /* The start of the operator may have become invalid by the Ex
4802 * command. */
4803 if (cap->oap->op_type != OP_NOP
4804 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
4805 || cap->oap->start.col >
4806 STRLEN(ml_get(cap->oap->start.lnum))))
4807 clearopbeep(cap->oap);
4808 }
4809}
4810
4811/*
4812 * Handle CTRL-G command.
4813 */
4814 static void
4815nv_ctrlg(cap)
4816 cmdarg_T *cap;
4817{
4818#ifdef FEAT_VISUAL
4819 if (VIsual_active) /* toggle Selection/Visual mode */
4820 {
4821 VIsual_select = !VIsual_select;
4822 showmode();
4823 }
4824 else
4825#endif
4826 if (!checkclearop(cap->oap))
4827 /* print full name if count given or :cd used */
4828 fileinfo((int)cap->count0, FALSE, TRUE);
4829}
4830
4831/*
4832 * Handle CTRL-H <Backspace> command.
4833 */
4834 static void
4835nv_ctrlh(cap)
4836 cmdarg_T *cap;
4837{
4838#ifdef FEAT_VISUAL
4839 if (VIsual_active && VIsual_select)
4840 {
4841 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
4842 v_visop(cap);
4843 }
4844 else
4845#endif
4846 nv_left(cap);
4847}
4848
4849/*
4850 * CTRL-L: clear screen and redraw.
4851 */
4852 static void
4853nv_clear(cap)
4854 cmdarg_T *cap;
4855{
4856 if (!checkclearop(cap->oap))
4857 {
4858#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
4859 /*
4860 * Right now, the BeBox doesn't seem to have an easy way to detect
4861 * window resizing, so we cheat and make the user detect it
4862 * manually with CTRL-L instead
4863 */
4864 ui_get_shellsize();
4865#endif
4866#ifdef FEAT_SYN_HL
4867 /* Clear all syntax states to force resyncing. */
4868 syn_stack_free_all(curbuf);
4869#endif
4870 redraw_later(CLEAR);
4871 }
4872}
4873
4874/*
4875 * CTRL-O: In Select mode: switch to Visual mode for one command.
4876 * Otherwise: Go to older pcmark.
4877 */
4878 static void
4879nv_ctrlo(cap)
4880 cmdarg_T *cap;
4881{
4882#ifdef FEAT_VISUAL
4883 if (VIsual_active && VIsual_select)
4884 {
4885 VIsual_select = FALSE;
4886 showmode();
4887 restart_VIsual_select = 2; /* restart Select mode later */
4888 }
4889 else
4890#endif
4891 {
4892 cap->count1 = -cap->count1;
4893 nv_pcmark(cap);
4894 }
4895}
4896
4897/*
4898 * CTRL-^ command, short for ":e #"
4899 */
4900 static void
4901nv_hat(cap)
4902 cmdarg_T *cap;
4903{
4904 if (!checkclearopq(cap->oap))
4905 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
4906 GETF_SETMARK|GETF_ALT, FALSE);
4907}
4908
4909/*
4910 * "Z" commands.
4911 */
4912 static void
4913nv_Zet(cap)
4914 cmdarg_T *cap;
4915{
4916 if (!checkclearopq(cap->oap))
4917 {
4918 switch (cap->nchar)
4919 {
4920 /* "ZZ": equivalent to ":x". */
4921 case 'Z': do_cmdline_cmd((char_u *)"x");
4922 break;
4923
4924 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
4925 case 'Q': do_cmdline_cmd((char_u *)"q!");
4926 break;
4927
4928 default: clearopbeep(cap->oap);
4929 }
4930 }
4931}
4932
4933#if defined(FEAT_WINDOWS) || defined(PROTO)
4934/*
4935 * Call nv_ident() as if "c1" was used, with "c2" as next character.
4936 */
4937 void
4938do_nv_ident(c1, c2)
4939 int c1;
4940 int c2;
4941{
4942 oparg_T oa;
4943 cmdarg_T ca;
4944
4945 clear_oparg(&oa);
4946 vim_memset(&ca, 0, sizeof(ca));
4947 ca.oap = &oa;
4948 ca.cmdchar = c1;
4949 ca.nchar = c2;
4950 nv_ident(&ca);
4951}
4952#endif
4953
4954/*
4955 * Handle the commands that use the word under the cursor.
4956 * [g] CTRL-] :ta to current identifier
4957 * [g] 'K' run program for current identifier
4958 * [g] '*' / to current identifier or string
4959 * [g] '#' ? to current identifier or string
4960 * g ']' :tselect for current identifier
4961 */
4962 static void
4963nv_ident(cap)
4964 cmdarg_T *cap;
4965{
4966 char_u *ptr = NULL;
4967 char_u *buf;
4968 char_u *p;
4969 char_u *kp; /* value of 'keywordprg' */
4970 int kp_help; /* 'keywordprg' is ":help" */
4971 int n = 0; /* init for GCC */
4972 int cmdchar;
4973 int g_cmd; /* "g" command */
4974 char_u *aux_ptr;
4975 int isman;
4976 int isman_s;
4977
4978 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
4979 {
4980 cmdchar = cap->nchar;
4981 g_cmd = TRUE;
4982 }
4983 else
4984 {
4985 cmdchar = cap->cmdchar;
4986 g_cmd = FALSE;
4987 }
4988
4989 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
4990 cmdchar = '#';
4991
4992 /*
4993 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
4994 */
4995 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
4996 {
4997#ifdef FEAT_VISUAL
4998 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
4999 return;
5000#endif
5001 if (checkclearopq(cap->oap))
5002 return;
5003 }
5004
5005 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5006 (cmdchar == '*' || cmdchar == '#')
5007 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5008 {
5009 clearop(cap->oap);
5010 return;
5011 }
5012
5013 /* Allocate buffer to put the command in. Inserting backslashes can
5014 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5015 * and some numbers. */
5016 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5017 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5018 || STRCMP(kp, ":help") == 0);
5019 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5020 if (buf == NULL)
5021 return;
5022 buf[0] = NUL;
5023
5024 switch (cmdchar)
5025 {
5026 case '*':
5027 case '#':
5028 /*
5029 * Put cursor at start of word, makes search skip the word
5030 * under the cursor.
5031 * Call setpcmark() first, so "*``" puts the cursor back where
5032 * it was.
5033 */
5034 setpcmark();
5035 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5036
5037 if (!g_cmd && vim_iswordp(ptr))
5038 STRCPY(buf, "\\<");
5039 no_smartcase = TRUE; /* don't use 'smartcase' now */
5040 break;
5041
5042 case 'K':
5043 if (kp_help)
5044 STRCPY(buf, "he! ");
5045 else
5046 {
5047 /* When a count is given, turn it into a range. Is this
5048 * really what we want? */
5049 isman = (STRCMP(kp, "man") == 0);
5050 isman_s = (STRCMP(kp, "man -s") == 0);
5051 if (cap->count0 != 0 && !(isman || isman_s))
5052 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5053
5054 STRCAT(buf, "! ");
5055 if (cap->count0 == 0 && isman_s)
5056 STRCAT(buf, "man");
5057 else
5058 STRCAT(buf, kp);
5059 STRCAT(buf, " ");
5060 if (cap->count0 != 0 && (isman || isman_s))
5061 {
5062 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5063 STRCAT(buf, " ");
5064 }
5065 }
5066 break;
5067
5068 case ']':
5069#ifdef FEAT_CSCOPE
5070 if (p_cst)
5071 STRCPY(buf, "cstag ");
5072 else
5073#endif
5074 STRCPY(buf, "ts ");
5075 break;
5076
5077 default:
5078 if (curbuf->b_help)
5079 STRCPY(buf, "he! ");
5080 else if (g_cmd)
5081 STRCPY(buf, "tj ");
5082 else
5083 STRCPY(buf, "ta ");
5084 }
5085
5086 /*
5087 * Now grab the chars in the identifier
5088 */
5089 if (cmdchar == '*')
5090 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5091 else if (cmdchar == '#')
5092 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
5093 else if (cmdchar == 'K' && !kp_help)
5094 aux_ptr = (char_u *)" \t\\\"|!";
5095 else
5096 /* Don't escape spaces and Tabs in a tag with a backslash */
5097 aux_ptr = (char_u *)"\\|\"";
5098
5099 p = buf + STRLEN(buf);
5100 while (n-- > 0)
5101 {
5102 /* put a backslash before \ and some others */
5103 if (vim_strchr(aux_ptr, *ptr) != NULL)
5104 *p++ = '\\';
5105#ifdef FEAT_MBYTE
5106 /* When current byte is a part of multibyte character, copy all bytes
5107 * of that character. */
5108 if (has_mbyte)
5109 {
5110 int i;
5111 int len = (*mb_ptr2len_check)(ptr) - 1;
5112
5113 for (i = 0; i < len && n >= 1; ++i, --n)
5114 *p++ = *ptr++;
5115 }
5116#endif
5117 *p++ = *ptr++;
5118 }
5119 *p = NUL;
5120
5121 /*
5122 * Execute the command.
5123 */
5124 if (cmdchar == '*' || cmdchar == '#')
5125 {
5126 if (!g_cmd && (
5127#ifdef FEAT_MBYTE
5128 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5129#endif
5130 vim_iswordc(ptr[-1])))
5131 STRCAT(buf, "\\>");
5132#ifdef FEAT_CMDHIST
5133 /* put pattern in search history */
5134 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5135#endif
5136 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5137 }
5138 else
5139 do_cmdline_cmd(buf);
5140
5141 vim_free(buf);
5142}
5143
5144#ifdef FEAT_VISUAL
5145/*
5146 * Get visually selected text, within one line only.
5147 * Returns FAIL if more than one line selected.
5148 */
5149 static int
5150get_visual_text(cap, pp, lenp)
5151 cmdarg_T *cap;
5152 char_u **pp; /* return: start of selected text */
5153 int *lenp; /* return: length of selected text */
5154{
5155 if (VIsual_mode != 'V')
5156 unadjust_for_sel();
5157 if (VIsual.lnum != curwin->w_cursor.lnum)
5158 {
5159 clearopbeep(cap->oap);
5160 return FAIL;
5161 }
5162 if (VIsual_mode == 'V')
5163 {
5164 *pp = ml_get_curline();
5165 *lenp = (int)STRLEN(*pp);
5166 }
5167 else
5168 {
5169 if (lt(curwin->w_cursor, VIsual))
5170 {
5171 *pp = ml_get_pos(&curwin->w_cursor);
5172 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5173 }
5174 else
5175 {
5176 *pp = ml_get_pos(&VIsual);
5177 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5178 }
5179#ifdef FEAT_MBYTE
5180 if (has_mbyte)
5181 /* Correct the length to include the whole last character. */
5182 *lenp += (*mb_ptr2len_check)(*pp + (*lenp - 1)) - 1;
5183#endif
5184 }
5185 reset_VIsual_and_resel();
5186 return OK;
5187}
5188#endif
5189
5190/*
5191 * CTRL-T: backwards in tag stack
5192 */
5193 static void
5194nv_tagpop(cap)
5195 cmdarg_T *cap;
5196{
5197 if (!checkclearopq(cap->oap))
5198 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5199}
5200
5201/*
5202 * Handle scrolling command 'H', 'L' and 'M'.
5203 */
5204 static void
5205nv_scroll(cap)
5206 cmdarg_T *cap;
5207{
5208 int used = 0;
5209 long n;
5210#ifdef FEAT_FOLDING
5211 linenr_T lnum;
5212#endif
5213 int half;
5214
5215 cap->oap->motion_type = MLINE;
5216 setpcmark();
5217
5218 if (cap->cmdchar == 'L')
5219 {
5220 validate_botline(); /* make sure curwin->w_botline is valid */
5221 curwin->w_cursor.lnum = curwin->w_botline - 1;
5222 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5223 curwin->w_cursor.lnum = 1;
5224 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005225 {
5226#ifdef FEAT_FOLDING
5227 if (hasAnyFolding(curwin))
5228 {
5229 /* Count a fold for one screen line. */
5230 for (n = cap->count1 - 1; n > 0
5231 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5232 {
5233 (void)hasFolding(curwin->w_cursor.lnum,
5234 &curwin->w_cursor.lnum, NULL);
5235 --curwin->w_cursor.lnum;
5236 }
5237 }
5238 else
5239#endif
5240 curwin->w_cursor.lnum -= cap->count1 - 1;
5241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 }
5243 else
5244 {
5245 if (cap->cmdchar == 'M')
5246 {
5247#ifdef FEAT_DIFF
5248 /* Don't count filler lines above the window. */
5249 used -= diff_check_fill(curwin, curwin->w_topline)
5250 - curwin->w_topfill;
5251#endif
5252 validate_botline(); /* make sure w_empty_rows is valid */
5253 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5254 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5255 {
5256#ifdef FEAT_DIFF
5257 /* Count half he number of filler lines to be "below this
5258 * line" and half to be "above the next line". */
5259 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5260 + n) / 2 >= half)
5261 {
5262 --n;
5263 break;
5264 }
5265#endif
5266 used += plines(curwin->w_topline + n);
5267 if (used >= half)
5268 break;
5269#ifdef FEAT_FOLDING
5270 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5271 n = lnum - curwin->w_topline;
5272#endif
5273 }
5274 if (n > 0 && used > curwin->w_height)
5275 --n;
5276 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005277 else /* (cap->cmdchar == 'H') */
5278 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005280#ifdef FEAT_FOLDING
5281 if (hasAnyFolding(curwin))
5282 {
5283 /* Count a fold for one screen line. */
5284 lnum = curwin->w_topline;
5285 while (n-- > 0 && lnum < curwin->w_botline - 1)
5286 {
5287 hasFolding(lnum, NULL, &lnum);
5288 ++lnum;
5289 }
5290 n = lnum - curwin->w_topline;
5291 }
5292#endif
5293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 curwin->w_cursor.lnum = curwin->w_topline + n;
5295 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5296 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5297 }
5298
5299 cursor_correct(); /* correct for 'so' */
5300 beginline(BL_SOL | BL_FIX);
5301}
5302
5303/*
5304 * Cursor right commands.
5305 */
5306 static void
5307nv_right(cap)
5308 cmdarg_T *cap;
5309{
5310 long n;
5311#ifdef FEAT_VISUAL
5312 int PAST_LINE;
5313#else
5314# define PAST_LINE 0
5315#endif
5316
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005317 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5318 {
5319 /* <C-Right> and <S-Right> move a word or WORD right */
5320 if (mod_mask & MOD_MASK_CTRL)
5321 cap->arg = TRUE;
5322 nv_wordcmd(cap);
5323 return;
5324 }
5325
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 cap->oap->motion_type = MCHAR;
5327 cap->oap->inclusive = FALSE;
5328#ifdef FEAT_VISUAL
5329 PAST_LINE = (VIsual_active && *p_sel != 'o');
5330
5331# ifdef FEAT_VIRTUALEDIT
5332 /*
5333 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
5334 * (theoretically) infinitly long.
5335 */
5336 if (virtual_active())
5337 PAST_LINE = 0;
5338# endif
5339#endif
5340
5341 for (n = cap->count1; n > 0; --n)
5342 {
5343 if ((!PAST_LINE && oneright() == FAIL)
5344 || (PAST_LINE && *ml_get_cursor() == NUL))
5345 {
5346 /*
5347 * <Space> wraps to next line if 'whichwrap' bit 1 set.
5348 * 'l' wraps to next line if 'whichwrap' bit 2 set.
5349 * CURS_RIGHT wraps to next line if 'whichwrap' bit 3 set
5350 */
5351 if ( ((cap->cmdchar == ' '
5352 && vim_strchr(p_ww, 's') != NULL)
5353 || (cap->cmdchar == 'l'
5354 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005355 || ((cap->cmdchar == K_RIGHT || cap->cmdchar == K_XRIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 && vim_strchr(p_ww, '>') != NULL))
5357 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5358 {
5359 /* When deleting we also count the NL as a character.
5360 * Set cap->oap->inclusive when last char in the line is
5361 * included, move to next line after that */
5362 if ( (cap->oap->op_type == OP_DELETE
5363 || cap->oap->op_type == OP_CHANGE)
5364 && !cap->oap->inclusive
5365 && !lineempty(curwin->w_cursor.lnum))
5366 cap->oap->inclusive = TRUE;
5367 else
5368 {
5369 ++curwin->w_cursor.lnum;
5370 curwin->w_cursor.col = 0;
5371#ifdef FEAT_VIRTUALEDIT
5372 curwin->w_cursor.coladd = 0;
5373#endif
5374 curwin->w_set_curswant = TRUE;
5375 cap->oap->inclusive = FALSE;
5376 }
5377 continue;
5378 }
5379 if (cap->oap->op_type == OP_NOP)
5380 {
5381 /* Only beep and flush if not moved at all */
5382 if (n == cap->count1)
5383 beep_flush();
5384 }
5385 else
5386 {
5387 if (!lineempty(curwin->w_cursor.lnum))
5388 cap->oap->inclusive = TRUE;
5389 }
5390 break;
5391 }
5392#ifdef FEAT_VISUAL
5393 else if (PAST_LINE)
5394 {
5395 curwin->w_set_curswant = TRUE;
5396# ifdef FEAT_VIRTUALEDIT
5397 if (virtual_active())
5398 oneright();
5399 else
5400# endif
5401 {
5402# ifdef FEAT_MBYTE
5403 if (has_mbyte)
5404 curwin->w_cursor.col +=
5405 (*mb_ptr2len_check)(ml_get_cursor());
5406 else
5407# endif
5408 ++curwin->w_cursor.col;
5409 }
5410 }
5411#endif
5412 }
5413#ifdef FEAT_FOLDING
5414 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5415 && cap->oap->op_type == OP_NOP)
5416 foldOpenCursor();
5417#endif
5418}
5419
5420/*
5421 * Cursor left commands.
5422 *
5423 * Returns TRUE when operator end should not be adjusted.
5424 */
5425 static void
5426nv_left(cap)
5427 cmdarg_T *cap;
5428{
5429 long n;
5430
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005431 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5432 {
5433 /* <C-Left> and <S-Left> move a word or WORD left */
5434 if (mod_mask & MOD_MASK_CTRL)
5435 cap->arg = 1;
5436 nv_bck_word(cap);
5437 return;
5438 }
5439
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 cap->oap->motion_type = MCHAR;
5441 cap->oap->inclusive = FALSE;
5442 for (n = cap->count1; n > 0; --n)
5443 {
5444 if (oneleft() == FAIL)
5445 {
5446 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
5447 * 'h' wraps to previous line if 'whichwrap' has 'h'.
5448 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
5449 */
5450 if ( (((cap->cmdchar == K_BS
5451 || cap->cmdchar == Ctrl_H)
5452 && vim_strchr(p_ww, 'b') != NULL)
5453 || (cap->cmdchar == 'h'
5454 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005455 || ((cap->cmdchar == K_LEFT || cap->cmdchar == K_XLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 && vim_strchr(p_ww, '<') != NULL))
5457 && curwin->w_cursor.lnum > 1)
5458 {
5459 --(curwin->w_cursor.lnum);
5460 coladvance((colnr_T)MAXCOL);
5461 curwin->w_set_curswant = TRUE;
5462
5463 /* When the NL before the first char has to be deleted we
5464 * put the cursor on the NUL after the previous line.
5465 * This is a very special case, be careful!
5466 * don't adjust op_end now, otherwise it won't work */
5467 if ( (cap->oap->op_type == OP_DELETE
5468 || cap->oap->op_type == OP_CHANGE)
5469 && !lineempty(curwin->w_cursor.lnum))
5470 {
5471 ++curwin->w_cursor.col;
5472 cap->retval |= CA_NO_ADJ_OP_END;
5473 }
5474 continue;
5475 }
5476 /* Only beep and flush if not moved at all */
5477 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
5478 beep_flush();
5479 break;
5480 }
5481 }
5482#ifdef FEAT_FOLDING
5483 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5484 && cap->oap->op_type == OP_NOP)
5485 foldOpenCursor();
5486#endif
5487}
5488
5489/*
5490 * Cursor up commands.
5491 * cap->arg is TRUE for "-": Move cursor to first non-blank.
5492 */
5493 static void
5494nv_up(cap)
5495 cmdarg_T *cap;
5496{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005497 if (mod_mask & MOD_MASK_SHIFT)
5498 {
5499 /* <S-Up> is page up */
5500 cap->arg = BACKWARD;
5501 nv_page(cap);
5502 }
5503 else
5504 {
5505 cap->oap->motion_type = MLINE;
5506 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5507 clearopbeep(cap->oap);
5508 else if (cap->arg)
5509 beginline(BL_WHITE | BL_FIX);
5510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511}
5512
5513/*
5514 * Cursor down commands.
5515 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
5516 */
5517 static void
5518nv_down(cap)
5519 cmdarg_T *cap;
5520{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005521 if (mod_mask & MOD_MASK_SHIFT)
5522 {
5523 /* <S-Down> is page down */
5524 cap->arg = FORWARD;
5525 nv_page(cap);
5526 }
5527 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5529 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005530 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 do_cmdline_cmd((char_u *)".cc");
5532 else
5533#endif
5534 {
5535#ifdef FEAT_CMDWIN
5536 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005537 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 cmdwin_result = CAR;
5539 else
5540#endif
5541 {
5542 cap->oap->motion_type = MLINE;
5543 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5544 clearopbeep(cap->oap);
5545 else if (cap->arg)
5546 beginline(BL_WHITE | BL_FIX);
5547 }
5548 }
5549}
5550
5551#ifdef FEAT_SEARCHPATH
5552/*
5553 * Grab the file name under the cursor and edit it.
5554 */
5555 static void
5556nv_gotofile(cap)
5557 cmdarg_T *cap;
5558{
5559 char_u *ptr;
5560
5561#ifdef FEAT_CMDWIN
5562 if (cmdwin_type != 0)
5563 {
5564 clearopbeep(cap->oap);
5565 return;
5566 }
5567#endif
5568
5569# ifdef FEAT_VISUAL
5570 /*
5571 * In Visual mode, use the selected text as a file name.
5572 * Don't allow selection across multiple lines.
5573 */
5574 if (VIsual_active)
5575 {
5576 int len;
5577
5578 if (get_visual_text(cap, &ptr, &len) == FAIL)
5579 return;
5580 ptr = find_file_name_in_path(ptr, len,
5581 FNAME_MESS|FNAME_EXP|FNAME_REL, cap->count1, curbuf->b_ffname);
5582 }
5583 else
5584# endif
5585 ptr = file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL,
5586 cap->count1);
5587
5588 if (ptr != NULL)
5589 {
5590 /* do autowrite if necessary */
5591 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
5592 autowrite(curbuf, FALSE);
5593 setpcmark();
5594 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
5595 P_HID(curbuf) ? ECMD_HIDE : 0);
5596 vim_free(ptr);
5597 }
5598 else
5599 clearop(cap->oap);
5600}
5601#endif
5602
5603/*
5604 * <End> command: to end of current line or last line.
5605 */
5606 static void
5607nv_end(cap)
5608 cmdarg_T *cap;
5609{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005610 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005612 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 nv_goto(cap);
5614 cap->count1 = 1; /* to end of current line */
5615 }
5616 nv_dollar(cap);
5617}
5618
5619/*
5620 * Handle the "$" command.
5621 */
5622 static void
5623nv_dollar(cap)
5624 cmdarg_T *cap;
5625{
5626 cap->oap->motion_type = MCHAR;
5627 cap->oap->inclusive = TRUE;
5628#ifdef FEAT_VIRTUALEDIT
5629 /* In virtual mode when off the edge of a line and an operator
5630 * is pending (whew!) keep the cursor where it is.
5631 * Otherwise, send it to the end of the line. */
5632 if (!virtual_active() || gchar_cursor() != NUL
5633 || cap->oap->op_type == OP_NOP)
5634#endif
5635 curwin->w_curswant = MAXCOL; /* so we stay at the end */
5636 if (cursor_down((long)(cap->count1 - 1),
5637 cap->oap->op_type == OP_NOP) == FAIL)
5638 clearopbeep(cap->oap);
5639#ifdef FEAT_FOLDING
5640 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
5641 foldOpenCursor();
5642#endif
5643}
5644
5645/*
5646 * Implementation of '?' and '/' commands.
5647 * If cap->arg is TRUE don't set PC mark.
5648 */
5649 static void
5650nv_search(cap)
5651 cmdarg_T *cap;
5652{
5653 oparg_T *oap = cap->oap;
5654
5655 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
5656 {
5657 /* Translate "g??" to "g?g?" */
5658 cap->cmdchar = 'g';
5659 cap->nchar = '?';
5660 nv_operator(cap);
5661 return;
5662 }
5663
5664 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
5665
5666 if (cap->searchbuf == NULL)
5667 {
5668 clearop(oap);
5669 return;
5670 }
5671
5672 normal_search(cap, cap->cmdchar, cap->searchbuf,
5673 (cap->arg ? 0 : SEARCH_MARK));
5674}
5675
5676/*
5677 * Handle "N" and "n" commands.
5678 * cap->arg is SEARCH_REV for "N", 0 for "n".
5679 */
5680 static void
5681nv_next(cap)
5682 cmdarg_T *cap;
5683{
5684 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
5685}
5686
5687/*
5688 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
5689 * Uses only cap->count1 and cap->oap from "cap".
5690 */
5691 static void
5692normal_search(cap, dir, pat, opt)
5693 cmdarg_T *cap;
5694 int dir;
5695 char_u *pat;
5696 int opt; /* extra flags for do_search() */
5697{
5698 int i;
5699
5700 cap->oap->motion_type = MCHAR;
5701 cap->oap->inclusive = FALSE;
5702 cap->oap->use_reg_one = TRUE;
5703 curwin->w_set_curswant = TRUE;
5704
5705 i = do_search(cap->oap, dir, pat, cap->count1,
5706 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG);
5707 if (i == 0)
5708 clearop(cap->oap);
5709 else
5710 {
5711 if (i == 2)
5712 cap->oap->motion_type = MLINE;
5713#ifdef FEAT_VIRTUALEDIT
5714 curwin->w_cursor.coladd = 0;
5715#endif
5716#ifdef FEAT_FOLDING
5717 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
5718 foldOpenCursor();
5719#endif
5720 }
5721
5722 /* "/$" will put the cursor after the end of the line, may need to
5723 * correct that here */
5724 check_cursor();
5725}
5726
5727/*
5728 * Character search commands.
5729 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
5730 * ',' and FALSE for ';'.
5731 * cap->nchar is NUL for ',' and ';' (repeat the search)
5732 */
5733 static void
5734nv_csearch(cap)
5735 cmdarg_T *cap;
5736{
5737 int t_cmd;
5738
5739 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
5740 t_cmd = TRUE;
5741 else
5742 t_cmd = FALSE;
5743
5744 cap->oap->motion_type = MCHAR;
5745 if (cap->arg == BACKWARD)
5746 cap->oap->inclusive = FALSE;
5747 else
5748 cap->oap->inclusive = TRUE;
5749 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
5750 clearopbeep(cap->oap);
5751 else
5752 {
5753 curwin->w_set_curswant = TRUE;
5754#ifdef FEAT_VIRTUALEDIT
5755 /* Include a Tab for "tx" and for "dfx". */
5756 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
5757 && (t_cmd || cap->oap->op_type != OP_NOP))
5758 {
5759 colnr_T scol, ecol;
5760
5761 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
5762 curwin->w_cursor.coladd = ecol - scol;
5763 }
5764 else
5765 curwin->w_cursor.coladd = 0;
5766#endif
5767#ifdef FEAT_VISUAL
5768 adjust_for_sel(cap);
5769#endif
5770#ifdef FEAT_FOLDING
5771 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
5772 foldOpenCursor();
5773#endif
5774 }
5775}
5776
5777/*
5778 * "[" and "]" commands.
5779 * cap->arg is BACKWARD for "[" and FORWARD for "]".
5780 */
5781 static void
5782nv_brackets(cap)
5783 cmdarg_T *cap;
5784{
5785 pos_T new_pos;
5786 pos_T prev_pos;
5787 pos_T *pos = NULL; /* init for GCC */
5788 pos_T old_pos; /* cursor position before command */
5789 int flag;
5790 long n;
5791 int findc;
5792 int c;
5793
5794 cap->oap->motion_type = MCHAR;
5795 cap->oap->inclusive = FALSE;
5796 old_pos = curwin->w_cursor;
5797#ifdef FEAT_VIRTUALEDIT
5798 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
5799#endif
5800
5801#ifdef FEAT_SEARCHPATH
5802 /*
5803 * "[f" or "]f" : Edit file under the cursor (same as "gf")
5804 */
5805 if (cap->nchar == 'f')
5806 nv_gotofile(cap);
5807 else
5808#endif
5809
5810#ifdef FEAT_FIND_ID
5811 /*
5812 * Find the occurence(s) of the identifier or define under cursor
5813 * in current and included files or jump to the first occurence.
5814 *
5815 * search list jump
5816 * fwd bwd fwd bwd fwd bwd
5817 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
5818 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
5819 */
5820 if (vim_strchr((char_u *)
5821#ifdef EBCDIC
5822 "iI\005dD\067",
5823#else
5824 "iI\011dD\004",
5825#endif
5826 cap->nchar) != NULL)
5827 {
5828 char_u *ptr;
5829 int len;
5830
5831 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
5832 clearop(cap->oap);
5833 else
5834 {
5835 find_pattern_in_path(ptr, 0, len, TRUE,
5836 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
5837 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
5838 cap->count1,
5839 isupper(cap->nchar) ? ACTION_SHOW_ALL :
5840 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
5841 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
5842 (linenr_T)MAXLNUM);
5843 curwin->w_set_curswant = TRUE;
5844 }
5845 }
5846 else
5847#endif
5848
5849 /*
5850 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
5851 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
5852 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
5853 * "[m" or "]m" search for prev/next start of (Java) method.
5854 * "[M" or "]M" search for prev/next end of (Java) method.
5855 */
5856 if ( (cap->cmdchar == '['
5857 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
5858 || (cap->cmdchar == ']'
5859 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
5860 {
5861 if (cap->nchar == '*')
5862 cap->nchar = '/';
5863 new_pos.lnum = 0;
5864 prev_pos.lnum = 0;
5865 if (cap->nchar == 'm' || cap->nchar == 'M')
5866 {
5867 if (cap->cmdchar == '[')
5868 findc = '{';
5869 else
5870 findc = '}';
5871 n = 9999;
5872 }
5873 else
5874 {
5875 findc = cap->nchar;
5876 n = cap->count1;
5877 }
5878 for ( ; n > 0; --n)
5879 {
5880 if ((pos = findmatchlimit(cap->oap, findc,
5881 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
5882 {
5883 if (new_pos.lnum == 0) /* nothing found */
5884 {
5885 if (cap->nchar != 'm' && cap->nchar != 'M')
5886 clearopbeep(cap->oap);
5887 }
5888 else
5889 pos = &new_pos; /* use last one found */
5890 break;
5891 }
5892 prev_pos = new_pos;
5893 curwin->w_cursor = *pos;
5894 new_pos = *pos;
5895 }
5896 curwin->w_cursor = old_pos;
5897
5898 /*
5899 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
5900 * brought us to the match for "[m" and "]M" when inside a method.
5901 * Try finding the '{' or '}' we want to be at.
5902 * Also repeat for the given count.
5903 */
5904 if (cap->nchar == 'm' || cap->nchar == 'M')
5905 {
5906 /* norm is TRUE for "]M" and "[m" */
5907 int norm = ((findc == '{') == (cap->nchar == 'm'));
5908
5909 n = cap->count1;
5910 /* found a match: we were inside a method */
5911 if (prev_pos.lnum != 0)
5912 {
5913 pos = &prev_pos;
5914 curwin->w_cursor = prev_pos;
5915 if (norm)
5916 --n;
5917 }
5918 else
5919 pos = NULL;
5920 while (n > 0)
5921 {
5922 for (;;)
5923 {
5924 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
5925 {
5926 /* if not found anything, that's an error */
5927 if (pos == NULL)
5928 clearopbeep(cap->oap);
5929 n = 0;
5930 break;
5931 }
5932 c = gchar_cursor();
5933 if (c == '{' || c == '}')
5934 {
5935 /* Must have found end/start of class: use it.
5936 * Or found the place to be at. */
5937 if ((c == findc && norm) || (n == 1 && !norm))
5938 {
5939 new_pos = curwin->w_cursor;
5940 pos = &new_pos;
5941 n = 0;
5942 }
5943 /* if no match found at all, we started outside of the
5944 * class and we're inside now. Just go on. */
5945 else if (new_pos.lnum == 0)
5946 {
5947 new_pos = curwin->w_cursor;
5948 pos = &new_pos;
5949 }
5950 /* found start/end of other method: go to match */
5951 else if ((pos = findmatchlimit(cap->oap, findc,
5952 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
5953 0)) == NULL)
5954 n = 0;
5955 else
5956 curwin->w_cursor = *pos;
5957 break;
5958 }
5959 }
5960 --n;
5961 }
5962 curwin->w_cursor = old_pos;
5963 if (pos == NULL && new_pos.lnum != 0)
5964 clearopbeep(cap->oap);
5965 }
5966 if (pos != NULL)
5967 {
5968 setpcmark();
5969 curwin->w_cursor = *pos;
5970 curwin->w_set_curswant = TRUE;
5971#ifdef FEAT_FOLDING
5972 if ((fdo_flags & FDO_BLOCK) && KeyTyped
5973 && cap->oap->op_type == OP_NOP)
5974 foldOpenCursor();
5975#endif
5976 }
5977 }
5978
5979 /*
5980 * "[[", "[]", "]]" and "][": move to start or end of function
5981 */
5982 else if (cap->nchar == '[' || cap->nchar == ']')
5983 {
5984 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
5985 flag = '{';
5986 else
5987 flag = '}'; /* "][" or "[]" */
5988
5989 curwin->w_set_curswant = TRUE;
5990 /*
5991 * Imitate strange Vi behaviour: When using "]]" with an operator
5992 * we also stop at '}'.
5993 */
5994 if (!findpar(cap->oap, cap->arg, cap->count1, flag,
5995 (cap->oap->op_type != OP_NOP
5996 && cap->arg == FORWARD && flag == '{')))
5997 clearopbeep(cap->oap);
5998 else
5999 {
6000 if (cap->oap->op_type == OP_NOP)
6001 beginline(BL_WHITE | BL_FIX);
6002#ifdef FEAT_FOLDING
6003 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6004 foldOpenCursor();
6005#endif
6006 }
6007 }
6008
6009 /*
6010 * "[p", "[P", "]P" and "]p": put with indent adjustment
6011 */
6012 else if (cap->nchar == 'p' || cap->nchar == 'P')
6013 {
6014 if (!checkclearopq(cap->oap))
6015 {
6016 prep_redo_cmd(cap);
6017 do_put(cap->oap->regname,
6018 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6019 cap->count1, PUT_FIXINDENT);
6020 }
6021 }
6022
6023 /*
6024 * "['", "[`", "]'" and "]`": jump to next mark
6025 */
6026 else if (cap->nchar == '\'' || cap->nchar == '`')
6027 {
6028 pos = &curwin->w_cursor;
6029 for (n = cap->count1; n > 0; --n)
6030 {
6031 prev_pos = *pos;
6032 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6033 cap->nchar == '\'');
6034 if (pos == NULL)
6035 break;
6036 }
6037 if (pos == NULL)
6038 pos = &prev_pos;
6039 nv_cursormark(cap, cap->nchar == '\'', pos);
6040 }
6041
6042#ifdef FEAT_MOUSE
6043 /*
6044 * [ or ] followed by a middle mouse click: put selected text with
6045 * indent adjustment. Any other button just does as usual.
6046 */
6047 else if (cap->nchar >= K_LEFTMOUSE && cap->nchar <= K_RIGHTRELEASE)
6048 {
6049 (void)do_mouse(cap->oap, cap->nchar,
6050 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6051 cap->count1, PUT_FIXINDENT);
6052 }
6053#endif /* FEAT_MOUSE */
6054
6055#ifdef FEAT_FOLDING
6056 /*
6057 * "[z" and "]z": move to start or end of open fold.
6058 */
6059 else if (cap->nchar == 'z')
6060 {
6061 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6062 cap->count1) == FAIL)
6063 clearopbeep(cap->oap);
6064 }
6065#endif
6066
6067#ifdef FEAT_DIFF
6068 /*
6069 * "[c" and "]c": move to next or previous diff-change.
6070 */
6071 else if (cap->nchar == 'c')
6072 {
6073 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6074 cap->count1) == FAIL)
6075 clearopbeep(cap->oap);
6076 }
6077#endif
6078
6079 /* Not a valid cap->nchar. */
6080 else
6081 clearopbeep(cap->oap);
6082}
6083
6084/*
6085 * Handle Normal mode "%" command.
6086 */
6087 static void
6088nv_percent(cap)
6089 cmdarg_T *cap;
6090{
6091 pos_T *pos;
6092#ifdef FEAT_FOLDING
6093 linenr_T lnum = curwin->w_cursor.lnum;
6094#endif
6095
6096 cap->oap->inclusive = TRUE;
6097 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6098 {
6099 if (cap->count0 > 100)
6100 clearopbeep(cap->oap);
6101 else
6102 {
6103 cap->oap->motion_type = MLINE;
6104 setpcmark();
6105 /* Round up, so CTRL-G will give same value. Watch out for a
6106 * large line count, the line number must not go negative! */
6107 if (curbuf->b_ml.ml_line_count > 1000000)
6108 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6109 / 100L * cap->count0;
6110 else
6111 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6112 cap->count0 + 99L) / 100L;
6113 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6114 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6115 beginline(BL_SOL | BL_FIX);
6116 }
6117 }
6118 else /* "%" : go to matching paren */
6119 {
6120 cap->oap->motion_type = MCHAR;
6121 cap->oap->use_reg_one = TRUE;
6122 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6123 clearopbeep(cap->oap);
6124 else
6125 {
6126 setpcmark();
6127 curwin->w_cursor = *pos;
6128 curwin->w_set_curswant = TRUE;
6129#ifdef FEAT_VIRTUALEDIT
6130 curwin->w_cursor.coladd = 0;
6131#endif
6132#ifdef FEAT_VISUAL
6133 adjust_for_sel(cap);
6134#endif
6135 }
6136 }
6137#ifdef FEAT_FOLDING
6138 if (cap->oap->op_type == OP_NOP
6139 && lnum != curwin->w_cursor.lnum
6140 && (fdo_flags & FDO_PERCENT)
6141 && KeyTyped)
6142 foldOpenCursor();
6143#endif
6144}
6145
6146/*
6147 * Handle "(" and ")" commands.
6148 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6149 */
6150 static void
6151nv_brace(cap)
6152 cmdarg_T *cap;
6153{
6154 cap->oap->motion_type = MCHAR;
6155 cap->oap->use_reg_one = TRUE;
6156 if (cap->cmdchar == ')')
6157 cap->oap->inclusive = FALSE;
6158 else
6159 cap->oap->inclusive = TRUE;
6160 curwin->w_set_curswant = TRUE;
6161
6162 if (findsent(cap->arg, cap->count1) == FAIL)
6163 clearopbeep(cap->oap);
6164 else
6165 {
6166#ifdef FEAT_VIRTUALEDIT
6167 curwin->w_cursor.coladd = 0;
6168#endif
6169#ifdef FEAT_FOLDING
6170 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6171 foldOpenCursor();
6172#endif
6173 }
6174}
6175
6176/*
6177 * "m" command: Mark a position.
6178 */
6179 static void
6180nv_mark(cap)
6181 cmdarg_T *cap;
6182{
6183 if (!checkclearop(cap->oap))
6184 {
6185 if (setmark(cap->nchar) == FAIL)
6186 clearopbeep(cap->oap);
6187 }
6188}
6189
6190/*
6191 * "{" and "}" commands.
6192 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6193 */
6194 static void
6195nv_findpar(cap)
6196 cmdarg_T *cap;
6197{
6198 cap->oap->motion_type = MCHAR;
6199 cap->oap->inclusive = FALSE;
6200 cap->oap->use_reg_one = TRUE;
6201 curwin->w_set_curswant = TRUE;
6202 if (!findpar(cap->oap, cap->arg, cap->count1, NUL, FALSE))
6203 clearopbeep(cap->oap);
6204 else
6205 {
6206#ifdef FEAT_VIRTUALEDIT
6207 curwin->w_cursor.coladd = 0;
6208#endif
6209#ifdef FEAT_FOLDING
6210 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6211 foldOpenCursor();
6212#endif
6213 }
6214}
6215
6216/*
6217 * "u" command: Undo or make lower case.
6218 */
6219 static void
6220nv_undo(cap)
6221 cmdarg_T *cap;
6222{
6223 if (cap->oap->op_type == OP_LOWER
6224#ifdef FEAT_VISUAL
6225 || VIsual_active
6226#endif
6227 )
6228 {
6229 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6230 cap->cmdchar = 'g';
6231 cap->nchar = 'u';
6232 nv_operator(cap);
6233 }
6234 else
6235 nv_kundo(cap);
6236}
6237
6238/*
6239 * <Undo> command.
6240 */
6241 static void
6242nv_kundo(cap)
6243 cmdarg_T *cap;
6244{
6245 if (!checkclearopq(cap->oap))
6246 {
6247 u_undo((int)cap->count1);
6248 curwin->w_set_curswant = TRUE;
6249 }
6250}
6251
6252/*
6253 * Handle the "r" command.
6254 */
6255 static void
6256nv_replace(cap)
6257 cmdarg_T *cap;
6258{
6259 char_u *ptr;
6260 int had_ctrl_v;
6261 long n;
6262
6263 if (checkclearop(cap->oap))
6264 return;
6265
6266 /* get another character */
6267 if (cap->nchar == Ctrl_V)
6268 {
6269 had_ctrl_v = Ctrl_V;
6270 cap->nchar = get_literal();
6271 /* Don't redo a multibyte character with CTRL-V. */
6272 if (cap->nchar > DEL)
6273 had_ctrl_v = NUL;
6274 }
6275 else
6276 had_ctrl_v = NUL;
6277
6278#ifdef FEAT_VISUAL
6279 /* Visual mode "r" */
6280 if (VIsual_active)
6281 {
6282 nv_operator(cap);
6283 return;
6284 }
6285#endif
6286
6287#ifdef FEAT_VIRTUALEDIT
6288 /* Break tabs, etc. */
6289 if (virtual_active())
6290 {
6291 if (u_save_cursor() == FAIL)
6292 return;
6293 if (gchar_cursor() == NUL)
6294 {
6295 /* Add extra space and put the cursor on the first one. */
6296 coladvance_force((colnr_T)(getviscol() + cap->count1));
6297 curwin->w_cursor.col -= cap->count1;
6298 }
6299 else if (gchar_cursor() == TAB)
6300 coladvance_force(getviscol());
6301 }
6302#endif
6303
6304 /*
6305 * Check for a special key or not enough characters to replace.
6306 */
6307 ptr = ml_get_cursor();
6308 if (IS_SPECIAL(cap->nchar) || STRLEN(ptr) < (unsigned)cap->count1
6309#ifdef FEAT_MBYTE
6310 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6311#endif
6312 )
6313 {
6314 clearopbeep(cap->oap);
6315 return;
6316 }
6317
6318 /*
6319 * Replacing with a TAB is done by edit() when it is complicated because
6320 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6321 * Other characters are done below to avoid problems with things like
6322 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6323 */
6324 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6325 {
6326 stuffnumReadbuff(cap->count1);
6327 stuffcharReadbuff('R');
6328 stuffcharReadbuff('\t');
6329 stuffcharReadbuff(ESC);
6330 return;
6331 }
6332
6333 /* save line for undo */
6334 if (u_save_cursor() == FAIL)
6335 return;
6336
6337 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6338 {
6339 /*
6340 * Replace character(s) by a single newline.
6341 * Strange vi behaviour: Only one newline is inserted.
6342 * Delete the characters here.
6343 * Insert the newline with an insert command, takes care of
6344 * autoindent. The insert command depends on being on the last
6345 * character of a line or not.
6346 */
6347#ifdef FEAT_MBYTE
6348 (void)del_chars(cap->count1, FALSE); /* delete the characters */
6349#else
6350 (void)del_bytes(cap->count1, FALSE); /* delete the characters */
6351#endif
6352 stuffcharReadbuff('\r');
6353 stuffcharReadbuff(ESC);
6354
6355 /* Give 'r' to edit(), to get the redo command right. */
6356 invoke_edit(cap, TRUE, 'r', FALSE);
6357 }
6358 else
6359 {
6360 prep_redo(cap->oap->regname, cap->count1,
6361 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
6362
6363 curbuf->b_op_start = curwin->w_cursor;
6364#ifdef FEAT_MBYTE
6365 if (has_mbyte)
6366 {
6367 int old_State = State;
6368
6369 if (cap->ncharC1 != 0)
6370 AppendCharToRedobuff(cap->ncharC1);
6371 if (cap->ncharC2 != 0)
6372 AppendCharToRedobuff(cap->ncharC2);
6373
6374 /* This is slow, but it handles replacing a single-byte with a
6375 * multi-byte and the other way around. Also handles adding
6376 * composing characters for utf-8. */
6377 for (n = cap->count1; n > 0; --n)
6378 {
6379 State = REPLACE;
6380 ins_char(cap->nchar);
6381 State = old_State;
6382 if (cap->ncharC1 != 0)
6383 ins_char(cap->ncharC1);
6384 if (cap->ncharC2 != 0)
6385 ins_char(cap->ncharC2);
6386 }
6387 }
6388 else
6389#endif
6390 {
6391 /*
6392 * Replace the characters within one line.
6393 */
6394 for (n = cap->count1; n > 0; --n)
6395 {
6396 /*
6397 * Get ptr again, because u_save and/or showmatch() will have
6398 * released the line. At the same time we let know that the
6399 * line will be changed.
6400 */
6401 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
6402 ptr[curwin->w_cursor.col] = cap->nchar;
6403 if (p_sm && msg_silent == 0)
6404 showmatch(cap->nchar);
6405 ++curwin->w_cursor.col;
6406 }
6407#ifdef FEAT_NETBEANS_INTG
6408 if (usingNetbeans)
6409 {
6410 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
6411
Bram Moolenaar009b2592004-10-24 19:18:58 +00006412 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
6413 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00006415 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 }
6417#endif
6418
6419 /* mark the buffer as changed and prepare for displaying */
6420 changed_bytes(curwin->w_cursor.lnum,
6421 (colnr_T)(curwin->w_cursor.col - cap->count1));
6422 }
6423 --curwin->w_cursor.col; /* cursor on the last replaced char */
6424#ifdef FEAT_MBYTE
6425 /* if the character on the left of the current cursor is a multi-byte
6426 * character, move two characters left */
6427 if (has_mbyte)
6428 mb_adjust_cursor();
6429#endif
6430 curbuf->b_op_end = curwin->w_cursor;
6431 curwin->w_set_curswant = TRUE;
6432 set_last_insert(cap->nchar);
6433 }
6434}
6435
6436#ifdef FEAT_VISUAL
6437/*
6438 * 'o': Exchange start and end of Visual area.
6439 * 'O': same, but in block mode exchange left and right corners.
6440 */
6441 static void
6442v_swap_corners(cmdchar)
6443 int cmdchar;
6444{
6445 pos_T old_cursor;
6446 colnr_T left, right;
6447
6448 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
6449 {
6450 old_cursor = curwin->w_cursor;
6451 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
6452 curwin->w_cursor.lnum = VIsual.lnum;
6453 coladvance(left);
6454 VIsual = curwin->w_cursor;
6455
6456 curwin->w_cursor.lnum = old_cursor.lnum;
6457 curwin->w_curswant = right;
6458 /* 'selection "exclusive" and cursor at right-bottom corner: move it
6459 * right one column */
6460 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
6461 ++curwin->w_curswant;
6462 coladvance(curwin->w_curswant);
6463 if (curwin->w_cursor.col == old_cursor.col
6464#ifdef FEAT_VIRTUALEDIT
6465 && (!virtual_active()
6466 || curwin->w_cursor.coladd == old_cursor.coladd)
6467#endif
6468 )
6469 {
6470 curwin->w_cursor.lnum = VIsual.lnum;
6471 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
6472 ++right;
6473 coladvance(right);
6474 VIsual = curwin->w_cursor;
6475
6476 curwin->w_cursor.lnum = old_cursor.lnum;
6477 coladvance(left);
6478 curwin->w_curswant = left;
6479 }
6480 }
6481 else
6482 {
6483 old_cursor = curwin->w_cursor;
6484 curwin->w_cursor = VIsual;
6485 VIsual = old_cursor;
6486 curwin->w_set_curswant = TRUE;
6487 }
6488}
6489#endif /* FEAT_VISUAL */
6490
6491/*
6492 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
6493 */
6494 static void
6495nv_Replace(cap)
6496 cmdarg_T *cap;
6497{
6498#ifdef FEAT_VISUAL
6499 if (VIsual_active) /* "R" is replace lines */
6500 {
6501 cap->cmdchar = 'c';
6502 cap->nchar = NUL;
6503 VIsual_mode = 'V';
6504 nv_operator(cap);
6505 }
6506 else
6507#endif
6508 if (!checkclearopq(cap->oap))
6509 {
6510 if (!curbuf->b_p_ma)
6511 EMSG(_(e_modifiable));
6512 else
6513 {
6514#ifdef FEAT_VIRTUALEDIT
6515 if (virtual_active())
6516 coladvance(getviscol());
6517#endif
6518 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
6519 }
6520 }
6521}
6522
6523#ifdef FEAT_VREPLACE
6524/*
6525 * "gr".
6526 */
6527 static void
6528nv_vreplace(cap)
6529 cmdarg_T *cap;
6530{
6531# ifdef FEAT_VISUAL
6532 if (VIsual_active)
6533 {
6534 cap->cmdchar = 'r';
6535 cap->nchar = cap->extra_char;
6536 nv_replace(cap); /* Do same as "r" in Visual mode for now */
6537 }
6538 else
6539# endif
6540 if (!checkclearopq(cap->oap))
6541 {
6542 if (!curbuf->b_p_ma)
6543 EMSG(_(e_modifiable));
6544 else
6545 {
6546 if (cap->extra_char == Ctrl_V) /* get another character */
6547 cap->extra_char = get_literal();
6548 stuffcharReadbuff(cap->extra_char);
6549 stuffcharReadbuff(ESC);
6550# ifdef FEAT_VIRTUALEDIT
6551 if (virtual_active())
6552 coladvance(getviscol());
6553# endif
6554 invoke_edit(cap, TRUE, 'v', FALSE);
6555 }
6556 }
6557}
6558#endif
6559
6560/*
6561 * Swap case for "~" command, when it does not work like an operator.
6562 */
6563 static void
6564n_swapchar(cap)
6565 cmdarg_T *cap;
6566{
6567 long n;
6568 pos_T startpos;
6569 int did_change = 0;
6570#ifdef FEAT_NETBEANS_INTG
6571 pos_T pos;
6572 char_u *ptr;
6573 int count;
6574#endif
6575
6576 if (checkclearopq(cap->oap))
6577 return;
6578
6579 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
6580 {
6581 clearopbeep(cap->oap);
6582 return;
6583 }
6584
6585 prep_redo_cmd(cap);
6586
6587 if (u_save_cursor() == FAIL)
6588 return;
6589
6590 startpos = curwin->w_cursor;
6591#ifdef FEAT_NETBEANS_INTG
6592 pos = startpos;
6593#endif
6594 for (n = cap->count1; n > 0; --n)
6595 {
6596 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
6597 inc_cursor();
6598 if (gchar_cursor() == NUL)
6599 {
6600 if (vim_strchr(p_ww, '~') != NULL
6601 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6602 {
6603#ifdef FEAT_NETBEANS_INTG
6604 if (usingNetbeans)
6605 {
6606 if (did_change)
6607 {
6608 ptr = ml_get(pos.lnum);
6609 count = STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00006610 netbeans_removed(curbuf, pos.lnum, pos.col,
6611 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00006613 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 }
6615 pos.col = 0;
6616 pos.lnum++;
6617 }
6618#endif
6619 ++curwin->w_cursor.lnum;
6620 curwin->w_cursor.col = 0;
6621 if (n > 1)
6622 {
6623 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
6624 break;
6625 u_clearline();
6626 }
6627 }
6628 else
6629 break;
6630 }
6631 }
6632#ifdef FEAT_NETBEANS_INTG
6633 if (did_change && usingNetbeans)
6634 {
6635 ptr = ml_get(pos.lnum);
6636 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00006637 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
6638 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 }
6640#endif
6641
6642
6643 check_cursor();
6644 curwin->w_set_curswant = TRUE;
6645 if (did_change)
6646 {
6647 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
6648 0L);
6649 curbuf->b_op_start = startpos;
6650 curbuf->b_op_end = curwin->w_cursor;
6651 if (curbuf->b_op_end.col > 0)
6652 --curbuf->b_op_end.col;
6653 }
6654}
6655
6656/*
6657 * Move cursor to mark.
6658 */
6659 static void
6660nv_cursormark(cap, flag, pos)
6661 cmdarg_T *cap;
6662 int flag;
6663 pos_T *pos;
6664{
6665 if (check_mark(pos) == FAIL)
6666 clearop(cap->oap);
6667 else
6668 {
6669 if (cap->cmdchar == '\''
6670 || cap->cmdchar == '`'
6671 || cap->cmdchar == '['
6672 || cap->cmdchar == ']')
6673 setpcmark();
6674 curwin->w_cursor = *pos;
6675 if (flag)
6676 beginline(BL_WHITE | BL_FIX);
6677 else
6678 check_cursor();
6679 }
6680 cap->oap->motion_type = flag ? MLINE : MCHAR;
6681 if (cap->cmdchar == '`')
6682 cap->oap->use_reg_one = TRUE;
6683 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
6684 curwin->w_set_curswant = TRUE;
6685}
6686
6687#ifdef FEAT_VISUAL
6688/*
6689 * Handle commands that are operators in Visual mode.
6690 */
6691 static void
6692v_visop(cap)
6693 cmdarg_T *cap;
6694{
6695 static char_u trans[] = "YyDdCcxdXdAAIIrr";
6696
6697 /* Uppercase means linewise, except in block mode, then "D" deletes till
6698 * the end of the line, and "C" replaces til EOL */
6699 if (isupper(cap->cmdchar))
6700 {
6701 if (VIsual_mode != Ctrl_V)
6702 VIsual_mode = 'V';
6703 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
6704 curwin->w_curswant = MAXCOL;
6705 }
6706 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
6707 nv_operator(cap);
6708}
6709#endif
6710
6711/*
6712 * "s" and "S" commands.
6713 */
6714 static void
6715nv_subst(cap)
6716 cmdarg_T *cap;
6717{
6718#ifdef FEAT_VISUAL
6719 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
6720 {
6721 if (cap->cmdchar == 'S')
6722 VIsual_mode = 'V';
6723 cap->cmdchar = 'c';
6724 nv_operator(cap);
6725 }
6726 else
6727#endif
6728 nv_optrans(cap);
6729}
6730
6731/*
6732 * Abbreviated commands.
6733 */
6734 static void
6735nv_abbrev(cap)
6736 cmdarg_T *cap;
6737{
6738 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
6739 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
6740
6741#ifdef FEAT_VISUAL
6742 /* in Visual mode these commands are operators */
6743 if (VIsual_active)
6744 v_visop(cap);
6745 else
6746#endif
6747 nv_optrans(cap);
6748}
6749
6750/*
6751 * Translate a command into another command.
6752 */
6753 static void
6754nv_optrans(cap)
6755 cmdarg_T *cap;
6756{
6757 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
6758 (char_u *)"d$", (char_u *)"c$",
6759 (char_u *)"cl", (char_u *)"cc",
6760 (char_u *)"yy", (char_u *)":s\r"};
6761 static char_u *str = (char_u *)"xXDCsSY&";
6762
6763 if (!checkclearopq(cap->oap))
6764 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006765 /* In Vi "2D" doesn't delete the next line. Can't translate it
6766 * either, because "2." should also not use the count. */
6767 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
6768 {
6769 cap->oap->start = curwin->w_cursor;
6770 cap->oap->op_type = OP_DELETE;
6771 cap->count1 = 1;
6772 nv_dollar(cap);
6773 finish_op = TRUE;
6774 ResetRedobuff();
6775 AppendCharToRedobuff('D');
6776 }
6777 else
6778 {
6779 if (cap->count0)
6780 stuffnumReadbuff(cap->count0);
6781 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
6782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 }
6784 cap->opcount = 0;
6785}
6786
6787/*
6788 * "'" and "`" commands. Also for "g'" and "g`".
6789 * cap->arg is TRUE for "'" and "g'".
6790 */
6791 static void
6792nv_gomark(cap)
6793 cmdarg_T *cap;
6794{
6795 pos_T *pos;
6796 int c;
6797#ifdef FEAT_FOLDING
6798 linenr_T lnum = curwin->w_cursor.lnum;
6799 int old_KeyTyped = KeyTyped; /* getting file may reset it */
6800#endif
6801
6802 if (cap->cmdchar == 'g')
6803 c = cap->extra_char;
6804 else
6805 c = cap->nchar;
6806 pos = getmark(c, (cap->oap->op_type == OP_NOP));
6807 if (pos == (pos_T *)-1) /* jumped to other file */
6808 {
6809 if (cap->arg)
6810 {
6811 check_cursor_lnum();
6812 beginline(BL_WHITE | BL_FIX);
6813 }
6814 else
6815 check_cursor();
6816 }
6817 else
6818 nv_cursormark(cap, cap->arg, pos);
6819
6820#ifdef FEAT_VIRTUALEDIT
6821 /* May need to clear the coladd that a mark includes. */
6822 if (!virtual_active())
6823 curwin->w_cursor.coladd = 0;
6824#endif
6825#ifdef FEAT_FOLDING
6826 if (cap->oap->op_type == OP_NOP
6827 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
6828 && (fdo_flags & FDO_MARK)
6829 && old_KeyTyped)
6830 foldOpenCursor();
6831#endif
6832}
6833
6834/*
6835 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
6836 */
6837 static void
6838nv_pcmark(cap)
6839 cmdarg_T *cap;
6840{
6841#ifdef FEAT_JUMPLIST
6842 pos_T *pos;
6843# ifdef FEAT_FOLDING
6844 linenr_T lnum = curwin->w_cursor.lnum;
6845 int old_KeyTyped = KeyTyped; /* getting file may reset it */
6846# endif
6847
6848 if (!checkclearopq(cap->oap))
6849 {
6850 if (cap->cmdchar == 'g')
6851 pos = movechangelist((int)cap->count1);
6852 else
6853 pos = movemark((int)cap->count1);
6854 if (pos == (pos_T *)-1) /* jump to other file */
6855 {
6856 curwin->w_set_curswant = TRUE;
6857 check_cursor();
6858 }
6859 else if (pos != NULL) /* can jump */
6860 nv_cursormark(cap, FALSE, pos);
6861 else if (cap->cmdchar == 'g')
6862 {
6863 if (curbuf->b_changelistlen == 0)
6864 EMSG(_("E664: changelist is empty"));
6865 else if (cap->count1 < 0)
6866 EMSG(_("E662: At start of changelist"));
6867 else
6868 EMSG(_("E663: At end of changelist"));
6869 }
6870 else
6871 clearopbeep(cap->oap);
6872# ifdef FEAT_FOLDING
6873 if (cap->oap->op_type == OP_NOP
6874 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
6875 && (fdo_flags & FDO_MARK)
6876 && old_KeyTyped)
6877 foldOpenCursor();
6878# endif
6879 }
6880#else
6881 clearopbeep(cap->oap);
6882#endif
6883}
6884
6885/*
6886 * Handle '"' command.
6887 */
6888 static void
6889nv_regname(cap)
6890 cmdarg_T *cap;
6891{
6892 if (checkclearop(cap->oap))
6893 return;
6894#ifdef FEAT_EVAL
6895 if (cap->nchar == '=')
6896 cap->nchar = get_expr_register();
6897#endif
6898 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
6899 {
6900 cap->oap->regname = cap->nchar;
6901 cap->opcount = cap->count0; /* remember count before '"' */
6902#ifdef FEAT_EVAL
6903 set_reg_var(cap->oap->regname);
6904#endif
6905 }
6906 else
6907 clearopbeep(cap->oap);
6908}
6909
6910#ifdef FEAT_VISUAL
6911/*
6912 * Handle "v", "V" and "CTRL-V" commands.
6913 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
6914 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006915 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 */
6917 static void
6918nv_visual(cap)
6919 cmdarg_T *cap;
6920{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006921 if (cap->cmdchar == Ctrl_Q)
6922 cap->cmdchar = Ctrl_V;
6923
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
6925 * characterwise, linewise, or blockwise. */
6926 if (cap->oap->op_type != OP_NOP)
6927 {
6928 cap->oap->motion_force = cap->cmdchar;
6929 finish_op = FALSE; /* operator doesn't finish now but later */
6930 return;
6931 }
6932
6933 VIsual_select = cap->arg;
6934 if (VIsual_active) /* change Visual mode */
6935 {
6936 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
6937 end_visual_mode();
6938 else /* toggle char/block mode */
6939 { /* or char/line mode */
6940 VIsual_mode = cap->cmdchar;
6941 showmode();
6942 }
6943 redraw_curbuf_later(INVERTED); /* update the inversion */
6944 }
6945 else /* start Visual mode */
6946 {
6947 check_visual_highlight();
6948 if (cap->count0) /* use previously selected part */
6949 {
6950 if (resel_VIsual_mode == NUL) /* there is none */
6951 {
6952 beep_flush();
6953 return;
6954 }
6955 VIsual = curwin->w_cursor;
6956
6957 VIsual_active = TRUE;
6958 VIsual_reselect = TRUE;
6959 if (!cap->arg)
6960 /* start Select mode when 'selectmode' contains "cmd" */
6961 may_start_select('c');
6962#ifdef FEAT_MOUSE
6963 setmouse();
6964#endif
6965 if (p_smd)
6966 redraw_cmdline = TRUE; /* show visual mode later */
6967 /*
6968 * For V and ^V, we multiply the number of lines even if there
6969 * was only one -- webb
6970 */
6971 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
6972 {
6973 curwin->w_cursor.lnum +=
6974 resel_VIsual_line_count * cap->count0 - 1;
6975 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6976 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6977 }
6978 VIsual_mode = resel_VIsual_mode;
6979 if (VIsual_mode == 'v')
6980 {
6981 if (resel_VIsual_line_count <= 1)
6982 curwin->w_cursor.col += resel_VIsual_col * cap->count0 - 1;
6983 else
6984 curwin->w_cursor.col = resel_VIsual_col;
6985 check_cursor_col();
6986 }
6987 if (resel_VIsual_col == MAXCOL)
6988 {
6989 curwin->w_curswant = MAXCOL;
6990 coladvance((colnr_T)MAXCOL);
6991 }
6992 else if (VIsual_mode == Ctrl_V)
6993 {
6994 validate_virtcol();
6995 curwin->w_curswant = curwin->w_virtcol
6996 + resel_VIsual_col * cap->count0 - 1;
6997 coladvance(curwin->w_curswant);
6998 }
6999 else
7000 curwin->w_set_curswant = TRUE;
7001 redraw_curbuf_later(INVERTED); /* show the inversion */
7002 }
7003 else
7004 {
7005 if (!cap->arg)
7006 /* start Select mode when 'selectmode' contains "cmd" */
7007 may_start_select('c');
7008 n_start_visual_mode(cap->cmdchar);
7009 }
7010 }
7011}
7012
7013/*
7014 * Start selection for Shift-movement keys.
7015 */
7016 void
7017start_selection()
7018{
7019 /* if 'selectmode' contains "key", start Select mode */
7020 may_start_select('k');
7021 n_start_visual_mode('v');
7022}
7023
7024/*
7025 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7026 */
7027 void
7028may_start_select(c)
7029 int c;
7030{
7031 VIsual_select = (stuff_empty() && typebuf_typed()
7032 && (vim_strchr(p_slm, c) != NULL));
7033}
7034
7035/*
7036 * Start Visual mode "c".
7037 * Should set VIsual_select before calling this.
7038 */
7039 static void
7040n_start_visual_mode(c)
7041 int c;
7042{
7043 VIsual_mode = c;
7044 VIsual_active = TRUE;
7045 VIsual_reselect = TRUE;
7046#ifdef FEAT_VIRTUALEDIT
7047 /* Corner case: the 0 position in a tab may change when going into
7048 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7049 */
7050 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7051 coladvance(curwin->w_virtcol);
7052#endif
7053 VIsual = curwin->w_cursor;
7054
7055#ifdef FEAT_FOLDING
7056 foldAdjustVisual();
7057#endif
7058
7059#ifdef FEAT_MOUSE
7060 setmouse();
7061#endif
7062 if (p_smd)
7063 redraw_cmdline = TRUE; /* show visual mode later */
7064#ifdef FEAT_CLIPBOARD
7065 /* Make sure the clipboard gets updated. Needed because start and
7066 * end may still be the same, and the selection needs to be owned */
7067 clip_star.vmode = NUL;
7068#endif
7069
7070 /* Only need to redraw this line, unless still need to redraw an old
7071 * Visual area (when 'lazyredraw' is set). */
7072 if (curwin->w_redr_type < INVERTED)
7073 {
7074 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7075 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7076 }
7077}
7078
7079#endif /* FEAT_VISUAL */
7080
7081/*
7082 * CTRL-W: Window commands
7083 */
7084 static void
7085nv_window(cap)
7086 cmdarg_T *cap;
7087{
7088#ifdef FEAT_WINDOWS
7089 if (!checkclearop(cap->oap))
7090 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7091#else
7092 (void)checkclearop(cap->oap);
7093#endif
7094}
7095
7096/*
7097 * CTRL-Z: Suspend
7098 */
7099 static void
7100nv_suspend(cap)
7101 cmdarg_T *cap;
7102{
7103 clearop(cap->oap);
7104#ifdef FEAT_VISUAL
7105 if (VIsual_active)
7106 end_visual_mode(); /* stop Visual mode */
7107#endif
7108 do_cmdline_cmd((char_u *)"st");
7109}
7110
7111/*
7112 * Commands starting with "g".
7113 */
7114 static void
7115nv_g_cmd(cap)
7116 cmdarg_T *cap;
7117{
7118 oparg_T *oap = cap->oap;
7119#ifdef FEAT_VISUAL
7120 pos_T tpos;
7121#endif
7122 int i;
7123 int flag = FALSE;
7124
7125 switch (cap->nchar)
7126 {
7127#ifdef MEM_PROFILE
7128 /*
7129 * "g^A": dump log of used memory.
7130 */
7131 case Ctrl_A:
7132 vim_mem_profile_dump();
7133 break;
7134#endif
7135
7136#ifdef FEAT_VREPLACE
7137 /*
7138 * "gR": Enter virtual replace mode.
7139 */
7140 case 'R':
7141 cap->arg = TRUE;
7142 nv_Replace(cap);
7143 break;
7144
7145 case 'r':
7146 nv_vreplace(cap);
7147 break;
7148#endif
7149
7150 case '&':
7151 do_cmdline_cmd((char_u *)"%s//~/&");
7152 break;
7153
7154#ifdef FEAT_VISUAL
7155 /*
7156 * "gv": Reselect the previous Visual area. If Visual already active,
7157 * exchange previous and current Visual area.
7158 */
7159 case 'v':
7160 if (checkclearop(oap))
7161 break;
7162
7163 if ( curbuf->b_visual_start.lnum == 0
7164 || curbuf->b_visual_start.lnum > curbuf->b_ml.ml_line_count
7165 || curbuf->b_visual_end.lnum == 0)
7166 beep_flush();
7167 else
7168 {
7169 /* set w_cursor to the start of the Visual area, tpos to the end */
7170 if (VIsual_active)
7171 {
7172 i = VIsual_mode;
7173 VIsual_mode = curbuf->b_visual_mode;
7174 curbuf->b_visual_mode = i;
7175# ifdef FEAT_EVAL
7176 curbuf->b_visual_mode_eval = i;
7177# endif
7178 i = curwin->w_curswant;
7179 curwin->w_curswant = curbuf->b_visual_curswant;
7180 curbuf->b_visual_curswant = i;
7181
7182 tpos = curbuf->b_visual_end;
7183 curbuf->b_visual_end = curwin->w_cursor;
7184 curwin->w_cursor = curbuf->b_visual_start;
7185 curbuf->b_visual_start = VIsual;
7186 }
7187 else
7188 {
7189 VIsual_mode = curbuf->b_visual_mode;
7190 curwin->w_curswant = curbuf->b_visual_curswant;
7191 tpos = curbuf->b_visual_end;
7192 curwin->w_cursor = curbuf->b_visual_start;
7193 }
7194
7195 VIsual_active = TRUE;
7196 VIsual_reselect = TRUE;
7197
7198 /* Set Visual to the start and w_cursor to the end of the Visual
7199 * area. Make sure they are on an existing character. */
7200 check_cursor();
7201 VIsual = curwin->w_cursor;
7202 curwin->w_cursor = tpos;
7203 check_cursor();
7204 update_topline();
7205 /*
7206 * When called from normal "g" command: start Select mode when
7207 * 'selectmode' contains "cmd". When called for K_SELECT, always
7208 * start Select mode.
7209 */
7210 if (cap->arg)
7211 VIsual_select = TRUE;
7212 else
7213 may_start_select('c');
7214#ifdef FEAT_MOUSE
7215 setmouse();
7216#endif
7217#ifdef FEAT_CLIPBOARD
7218 /* Make sure the clipboard gets updated. Needed because start and
7219 * end are still the same, and the selection needs to be owned */
7220 clip_star.vmode = NUL;
7221#endif
7222 redraw_curbuf_later(INVERTED);
7223 showmode();
7224 }
7225 break;
7226 /*
7227 * "gV": Don't reselect the previous Visual area after a Select mode
7228 * mapping of menu.
7229 */
7230 case 'V':
7231 VIsual_reselect = FALSE;
7232 break;
7233
7234 /*
7235 * "gh": start Select mode.
7236 * "gH": start Select line mode.
7237 * "g^H": start Select block mode.
7238 */
7239 case K_BS:
7240 cap->nchar = Ctrl_H;
7241 /* FALLTHROUGH */
7242 case 'h':
7243 case 'H':
7244 case Ctrl_H:
7245# ifdef EBCDIC
7246 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7247 if (cap->nchar == Ctrl_H)
7248 cap->cmdchar = Ctrl_V;
7249 else
7250# endif
7251 cap->cmdchar = cap->nchar + ('v' - 'h');
7252 cap->arg = TRUE;
7253 nv_visual(cap);
7254 break;
7255#endif /* FEAT_VISUAL */
7256
7257 /*
7258 * "gj" and "gk" two new funny movement keys -- up and down
7259 * movement based on *screen* line rather than *file* line.
7260 */
7261 case 'j':
7262 case K_DOWN:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00007263 case K_XDOWN:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 /* with 'nowrap' it works just like the normal "j" command; also when
7265 * in a closed fold */
7266 if (!curwin->w_p_wrap
7267#ifdef FEAT_FOLDING
7268 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7269#endif
7270 )
7271 {
7272 oap->motion_type = MLINE;
7273 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7274 }
7275 else
7276 i = nv_screengo(oap, FORWARD, cap->count1);
7277 if (i == FAIL)
7278 clearopbeep(oap);
7279 break;
7280
7281 case 'k':
7282 case K_UP:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00007283 case K_XUP:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 /* with 'nowrap' it works just like the normal "k" command; also when
7285 * in a closed fold */
7286 if (!curwin->w_p_wrap
7287#ifdef FEAT_FOLDING
7288 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7289#endif
7290 )
7291 {
7292 oap->motion_type = MLINE;
7293 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7294 }
7295 else
7296 i = nv_screengo(oap, BACKWARD, cap->count1);
7297 if (i == FAIL)
7298 clearopbeep(oap);
7299 break;
7300
7301 /*
7302 * "gJ": join two lines without inserting a space.
7303 */
7304 case 'J':
7305 nv_join(cap);
7306 break;
7307
7308 /*
7309 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7310 * "gm": middle of "g0" and "g$".
7311 */
7312 case '^':
7313 flag = TRUE;
7314 /* FALLTHROUGH */
7315
7316 case '0':
7317 case 'm':
7318 case K_HOME:
7319 case K_KHOME:
7320 case K_XHOME:
7321 oap->motion_type = MCHAR;
7322 oap->inclusive = FALSE;
7323 if (curwin->w_p_wrap
7324#ifdef FEAT_VERTSPLIT
7325 && curwin->w_width != 0
7326#endif
7327 )
7328 {
7329 int width1 = W_WIDTH(curwin) - curwin_col_off();
7330 int width2 = width1 + curwin_col_off2();
7331
7332 validate_virtcol();
7333 i = 0;
7334 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
7335 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
7336 }
7337 else
7338 i = curwin->w_leftcol;
7339 /* Go to the middle of the screen line. When 'number' is on and lines
7340 * are wrapping the middle can be more to the left.*/
7341 if (cap->nchar == 'm')
7342 i += (W_WIDTH(curwin) - curwin_col_off()
7343 + ((curwin->w_p_wrap && i > 0)
7344 ? curwin_col_off2() : 0)) / 2;
7345 coladvance((colnr_T)i);
7346 if (flag)
7347 {
7348 do
7349 i = gchar_cursor();
7350 while (vim_iswhite(i) && oneright() == OK);
7351 }
7352 curwin->w_set_curswant = TRUE;
7353 break;
7354
7355 case '_':
7356 /* "g_": to the last non-blank character in the line or <count> lines
7357 * downward. */
7358 cap->oap->motion_type = MCHAR;
7359 cap->oap->inclusive = TRUE;
7360 curwin->w_curswant = MAXCOL;
7361 if (cursor_down((long)(cap->count1 - 1),
7362 cap->oap->op_type == OP_NOP) == FAIL)
7363 clearopbeep(cap->oap);
7364 else
7365 {
7366 char_u *ptr = ml_get_curline();
7367
7368 /* In Visual mode we may end up after the line. */
7369 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
7370 --curwin->w_cursor.col;
7371
7372 /* Decrease the cursor column until it's on a non-blank. */
7373 while (curwin->w_cursor.col > 0
7374 && vim_iswhite(ptr[curwin->w_cursor.col]))
7375 --curwin->w_cursor.col;
7376 curwin->w_set_curswant = TRUE;
7377 }
7378 break;
7379
7380 case '$':
7381 case K_END:
7382 case K_KEND:
7383 case K_XEND:
7384 {
7385 int col_off = curwin_col_off();
7386
7387 oap->motion_type = MCHAR;
7388 oap->inclusive = TRUE;
7389 if (curwin->w_p_wrap
7390#ifdef FEAT_VERTSPLIT
7391 && curwin->w_width != 0
7392#endif
7393 )
7394 {
7395 curwin->w_curswant = MAXCOL; /* so we stay at the end */
7396 if (cap->count1 == 1)
7397 {
7398 int width1 = W_WIDTH(curwin) - col_off;
7399 int width2 = width1 + curwin_col_off2();
7400
7401 validate_virtcol();
7402 i = width1 - 1;
7403 if (curwin->w_virtcol >= (colnr_T)width1)
7404 i += ((curwin->w_virtcol - width1) / width2 + 1)
7405 * width2;
7406 coladvance((colnr_T)i);
7407#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
7408 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
7409 {
7410 /*
7411 * Check for landing on a character that got split at
7412 * the end of the line. We do not want to advance to
7413 * the next screen line.
7414 */
7415 validate_virtcol();
7416 if (curwin->w_virtcol > (colnr_T)i)
7417 --curwin->w_cursor.col;
7418 }
7419#endif
7420 }
7421 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
7422 clearopbeep(oap);
7423 }
7424 else
7425 {
7426 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
7427 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007428
7429 /* Make sure we stick in this column. */
7430 validate_virtcol();
7431 curwin->w_curswant = curwin->w_virtcol;
7432 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 }
7434 }
7435 break;
7436
7437 /*
7438 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
7439 */
7440 case '*':
7441 case '#':
7442#if POUND != '#'
7443 case POUND: /* pound sign (sometimes equal to '#') */
7444#endif
7445 case Ctrl_RSB: /* :tag or :tselect for current identifier */
7446 case ']': /* :tselect for current identifier */
7447 nv_ident(cap);
7448 break;
7449
7450 /*
7451 * ge and gE: go back to end of word
7452 */
7453 case 'e':
7454 case 'E':
7455 oap->motion_type = MCHAR;
7456 curwin->w_set_curswant = TRUE;
7457 oap->inclusive = TRUE;
7458 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
7459 clearopbeep(oap);
7460 break;
7461
7462 /*
7463 * "g CTRL-G": display info about cursor position
7464 */
7465 case Ctrl_G:
7466 cursor_pos_info();
7467 break;
7468
7469 /*
7470 * "gi": start Insert at the last position.
7471 */
7472 case 'i':
7473 if (curbuf->b_last_insert.lnum != 0)
7474 {
7475 curwin->w_cursor = curbuf->b_last_insert;
7476 check_cursor_lnum();
7477 i = (int)STRLEN(ml_get_curline());
7478 if (curwin->w_cursor.col > (colnr_T)i)
7479 {
7480#ifdef FEAT_VIRTUALEDIT
7481 if (virtual_active())
7482 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
7483#endif
7484 curwin->w_cursor.col = i;
7485 }
7486 }
7487 cap->cmdchar = 'i';
7488 nv_edit(cap);
7489 break;
7490
7491 /*
7492 * "gI": Start insert in column 1.
7493 */
7494 case 'I':
7495 beginline(0);
7496 if (!checkclearopq(oap))
7497 invoke_edit(cap, FALSE, 'g', FALSE);
7498 break;
7499
7500#ifdef FEAT_SEARCHPATH
7501 /*
7502 * "gf": goto file, edit file under cursor
7503 * "]f" and "[f": can also be used.
7504 */
7505 case 'f':
7506 nv_gotofile(cap);
7507 break;
7508#endif
7509
7510 /* "g'm" and "g`m": jump to mark without setting pcmark */
7511 case '\'':
7512 cap->arg = TRUE;
7513 /*FALLTHROUGH*/
7514 case '`':
7515 nv_gomark(cap);
7516 break;
7517
7518 /*
7519 * "gs": Goto sleep.
7520 */
7521 case 's':
7522 do_sleep(cap->count1 * 1000L);
7523 break;
7524
7525 /*
7526 * "ga": Display the ascii value of the character under the
7527 * cursor. It is displayed in decimal, hex, and octal. -- webb
7528 */
7529 case 'a':
7530 do_ascii(NULL);
7531 break;
7532
7533#ifdef FEAT_MBYTE
7534 /*
7535 * "g8": Display the bytes used for the UTF-8 character under the
7536 * cursor. It is displayed in hex.
7537 */
7538 case '8':
7539 show_utf8();
7540 break;
7541#endif
7542
7543 /*
7544 * "gg": Goto the first line in file. With a count it goes to
7545 * that line number like for "G". -- webb
7546 */
7547 case 'g':
7548 cap->arg = FALSE;
7549 nv_goto(cap);
7550 break;
7551
7552 /*
7553 * Two-character operators:
7554 * "gq" Format text
7555 * "gw" Format text and keep cursor position
7556 * "g~" Toggle the case of the text.
7557 * "gu" Change text to lower case.
7558 * "gU" Change text to upper case.
7559 * "g?" rot13 encoding
7560 */
7561 case 'q':
7562 case 'w':
7563 oap->cursor_start = curwin->w_cursor;
7564 /*FALLTHROUGH*/
7565 case '~':
7566 case 'u':
7567 case 'U':
7568 case '?':
7569 nv_operator(cap);
7570 break;
7571
7572 /*
7573 * "gd": Find first occurence of pattern under the cursor in the
7574 * current function
7575 * "gD": idem, but in the current file.
7576 */
7577 case 'd':
7578 case 'D':
7579 nv_gd(oap, cap->nchar);
7580 break;
7581
7582#ifdef FEAT_MOUSE
7583 /*
7584 * g<*Mouse> : <C-*mouse>
7585 */
7586 case K_MIDDLEMOUSE:
7587 case K_MIDDLEDRAG:
7588 case K_MIDDLERELEASE:
7589 case K_LEFTMOUSE:
7590 case K_LEFTDRAG:
7591 case K_LEFTRELEASE:
7592 case K_RIGHTMOUSE:
7593 case K_RIGHTDRAG:
7594 case K_RIGHTRELEASE:
7595 case K_X1MOUSE:
7596 case K_X1DRAG:
7597 case K_X1RELEASE:
7598 case K_X2MOUSE:
7599 case K_X2DRAG:
7600 case K_X2RELEASE:
7601 mod_mask = MOD_MASK_CTRL;
7602 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
7603 break;
7604#endif
7605
7606 case K_IGNORE:
7607 break;
7608
7609 /*
7610 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
7611 */
7612 case 'p':
7613 case 'P':
7614 nv_put(cap);
7615 break;
7616
7617#ifdef FEAT_BYTEOFF
7618 /* "go": goto byte count from start of buffer */
7619 case 'o':
7620 goto_byte(cap->count0);
7621 break;
7622#endif
7623
7624 /* "gQ": improved Ex mode */
7625 case 'Q':
7626#ifdef FEAT_CMDWIN
7627 if (cmdwin_type != 0)
7628 {
7629 clearopbeep(cap->oap);
7630 break;
7631 }
7632#endif
7633 if (!checkclearopq(oap))
7634 do_exmode(TRUE);
7635 break;
7636
7637#ifdef FEAT_JUMPLIST
7638 case ',':
7639 nv_pcmark(cap);
7640 break;
7641
7642 case ';':
7643 cap->count1 = -cap->count1;
7644 nv_pcmark(cap);
7645 break;
7646#endif
7647
7648 default:
7649 clearopbeep(oap);
7650 break;
7651 }
7652}
7653
7654/*
7655 * Handle "o" and "O" commands.
7656 */
7657 static void
7658n_opencmd(cap)
7659 cmdarg_T *cap;
7660{
7661 if (!checkclearopq(cap->oap))
7662 {
7663#ifdef FEAT_FOLDING
7664 if (cap->cmdchar == 'O')
7665 /* Open above the first line of a folded sequence of lines */
7666 (void)hasFolding(curwin->w_cursor.lnum,
7667 &curwin->w_cursor.lnum, NULL);
7668 else
7669 /* Open below the last line of a folded sequence of lines */
7670 (void)hasFolding(curwin->w_cursor.lnum,
7671 NULL, &curwin->w_cursor.lnum);
7672#endif
7673 if (u_save((linenr_T)(curwin->w_cursor.lnum -
7674 (cap->cmdchar == 'O' ? 1 : 0)),
7675 (linenr_T)(curwin->w_cursor.lnum +
7676 (cap->cmdchar == 'o' ? 1 : 0))
7677 ) == OK
7678 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
7679#ifdef FEAT_COMMENTS
7680 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
7681#endif
7682 0, 0))
7683 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007684 /* When '#' is in 'cpoptions' ignore the count. */
7685 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
7686 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
7688 }
7689 }
7690}
7691
7692/*
7693 * "." command: redo last change.
7694 */
7695 static void
7696nv_dot(cap)
7697 cmdarg_T *cap;
7698{
7699 if (!checkclearopq(cap->oap))
7700 {
7701 /*
7702 * If "restart_edit" is TRUE, the last but one command is repeated
7703 * instead of the last command (inserting text). This is used for
7704 * CTRL-O <.> in insert mode.
7705 */
7706 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
7707 clearopbeep(cap->oap);
7708 }
7709}
7710
7711/*
7712 * CTRL-R: undo undo
7713 */
7714 static void
7715nv_redo(cap)
7716 cmdarg_T *cap;
7717{
7718 if (!checkclearopq(cap->oap))
7719 {
7720 u_redo((int)cap->count1);
7721 curwin->w_set_curswant = TRUE;
7722 }
7723}
7724
7725/*
7726 * Handle "U" command.
7727 */
7728 static void
7729nv_Undo(cap)
7730 cmdarg_T *cap;
7731{
7732 /* In Visual mode and typing "gUU" triggers an operator */
7733 if (cap->oap->op_type == OP_UPPER
7734#ifdef FEAT_VISUAL
7735 || VIsual_active
7736#endif
7737 )
7738 {
7739 /* translate "gUU" to "gUgU" */
7740 cap->cmdchar = 'g';
7741 cap->nchar = 'U';
7742 nv_operator(cap);
7743 }
7744 else if (!checkclearopq(cap->oap))
7745 {
7746 u_undoline();
7747 curwin->w_set_curswant = TRUE;
7748 }
7749}
7750
7751/*
7752 * '~' command: If tilde is not an operator and Visual is off: swap case of a
7753 * single character.
7754 */
7755 static void
7756nv_tilde(cap)
7757 cmdarg_T *cap;
7758{
7759 if (!p_to
7760#ifdef FEAT_VISUAL
7761 && !VIsual_active
7762#endif
7763 && cap->oap->op_type != OP_TILDE)
7764 n_swapchar(cap);
7765 else
7766 nv_operator(cap);
7767}
7768
7769/*
7770 * Handle an operator command.
7771 * The actual work is done by do_pending_operator().
7772 */
7773 static void
7774nv_operator(cap)
7775 cmdarg_T *cap;
7776{
7777 int op_type;
7778
7779 op_type = get_op_type(cap->cmdchar, cap->nchar);
7780
7781 if (op_type == cap->oap->op_type) /* double operator works on lines */
7782 nv_lineop(cap);
7783 else if (!checkclearop(cap->oap))
7784 {
7785 cap->oap->start = curwin->w_cursor;
7786 cap->oap->op_type = op_type;
7787 }
7788}
7789
7790/*
7791 * Handle linewise operator "dd", "yy", etc.
7792 *
7793 * "_" is is a strange motion command that helps make operators more logical.
7794 * It is actually implemented, but not documented in the real Vi. This motion
7795 * command actually refers to "the current line". Commands like "dd" and "yy"
7796 * are really an alternate form of "d_" and "y_". It does accept a count, so
7797 * "d3_" works to delete 3 lines.
7798 */
7799 static void
7800nv_lineop(cap)
7801 cmdarg_T *cap;
7802{
7803 cap->oap->motion_type = MLINE;
7804 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
7805 clearopbeep(cap->oap);
7806 else if ( cap->oap->op_type == OP_DELETE
7807 || cap->oap->op_type == OP_LSHIFT
7808 || cap->oap->op_type == OP_RSHIFT)
7809 beginline(BL_SOL | BL_FIX);
7810 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
7811 beginline(BL_WHITE | BL_FIX);
7812}
7813
7814/*
7815 * <Home> command.
7816 */
7817 static void
7818nv_home(cap)
7819 cmdarg_T *cap;
7820{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00007821 /* CTRL-HOME is like "gg" */
7822 if (mod_mask & MOD_MASK_CTRL)
7823 nv_goto(cap);
7824 else
7825 {
7826 cap->count0 = 1;
7827 nv_pipe(cap);
7828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829}
7830
7831/*
7832 * "|" command.
7833 */
7834 static void
7835nv_pipe(cap)
7836 cmdarg_T *cap;
7837{
7838 cap->oap->motion_type = MCHAR;
7839 cap->oap->inclusive = FALSE;
7840 beginline(0);
7841 if (cap->count0 > 0)
7842 {
7843 coladvance((colnr_T)(cap->count0 - 1));
7844 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
7845 }
7846 else
7847 curwin->w_curswant = 0;
7848 /* keep curswant at the column where we wanted to go, not where
7849 we ended; differs if line is too short */
7850 curwin->w_set_curswant = FALSE;
7851}
7852
7853/*
7854 * Handle back-word command "b" and "B".
7855 * cap->arg is 1 for "B"
7856 */
7857 static void
7858nv_bck_word(cap)
7859 cmdarg_T *cap;
7860{
7861 cap->oap->motion_type = MCHAR;
7862 cap->oap->inclusive = FALSE;
7863 curwin->w_set_curswant = TRUE;
7864 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
7865 clearopbeep(cap->oap);
7866#ifdef FEAT_FOLDING
7867 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
7868 foldOpenCursor();
7869#endif
7870}
7871
7872/*
7873 * Handle word motion commands "e", "E", "w" and "W".
7874 * cap->arg is TRUE for "E" and "W".
7875 */
7876 static void
7877nv_wordcmd(cap)
7878 cmdarg_T *cap;
7879{
7880 int n;
7881 int word_end;
7882 int flag = FALSE;
7883
7884 /*
7885 * Set inclusive for the "E" and "e" command.
7886 */
7887 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
7888 word_end = TRUE;
7889 else
7890 word_end = FALSE;
7891 cap->oap->inclusive = word_end;
7892
7893 /*
7894 * "cw" and "cW" are a special case.
7895 */
7896 if (!word_end && cap->oap->op_type == OP_CHANGE)
7897 {
7898 n = gchar_cursor();
7899 if (n != NUL) /* not an empty line */
7900 {
7901 if (vim_iswhite(n))
7902 {
7903 /*
7904 * Reproduce a funny Vi behaviour: "cw" on a blank only
7905 * changes one character, not all blanks until the start of
7906 * the next word. Only do this when the 'w' flag is included
7907 * in 'cpoptions'.
7908 */
7909 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
7910 {
7911 cap->oap->inclusive = TRUE;
7912 cap->oap->motion_type = MCHAR;
7913 return;
7914 }
7915 }
7916 else
7917 {
7918 /*
7919 * This is a little strange. To match what the real Vi does,
7920 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
7921 * that we are not on a space or a TAB. This seems impolite
7922 * at first, but it's really more what we mean when we say
7923 * 'cw'.
7924 * Another strangeness: When standing on the end of a word
7925 * "ce" will change until the end of the next wordt, but "cw"
7926 * will change only one character! This is done by setting
7927 * flag.
7928 */
7929 cap->oap->inclusive = TRUE;
7930 word_end = TRUE;
7931 flag = TRUE;
7932 }
7933 }
7934 }
7935
7936 cap->oap->motion_type = MCHAR;
7937 curwin->w_set_curswant = TRUE;
7938 if (word_end)
7939 n = end_word(cap->count1, cap->arg, flag, FALSE);
7940 else
7941 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
7942
7943 /* Don't leave the cursor on the NUL past a line */
7944 if (curwin->w_cursor.col && gchar_cursor() == NUL)
7945 {
7946 --curwin->w_cursor.col;
7947 cap->oap->inclusive = TRUE;
7948 }
7949
7950 if (n == FAIL && cap->oap->op_type == OP_NOP)
7951 clearopbeep(cap->oap);
7952 else
7953 {
7954#ifdef FEAT_VISUAL
7955 adjust_for_sel(cap);
7956#endif
7957#ifdef FEAT_FOLDING
7958 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
7959 foldOpenCursor();
7960#endif
7961 }
7962}
7963
7964/*
7965 * "0" and "^" commands.
7966 * cap->arg is the argument for beginline().
7967 */
7968 static void
7969nv_beginline(cap)
7970 cmdarg_T *cap;
7971{
7972 cap->oap->motion_type = MCHAR;
7973 cap->oap->inclusive = FALSE;
7974 beginline(cap->arg);
7975#ifdef FEAT_FOLDING
7976 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
7977 foldOpenCursor();
7978#endif
7979}
7980
7981#ifdef FEAT_VISUAL
7982/*
7983 * In exclusive Visual mode, may include the last character.
7984 */
7985 static void
7986adjust_for_sel(cap)
7987 cmdarg_T *cap;
7988{
7989 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
7990 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
7991 {
7992# ifdef FEAT_MBYTE
7993 if (has_mbyte)
7994 inc_cursor();
7995 else
7996# endif
7997 ++curwin->w_cursor.col;
7998 cap->oap->inclusive = FALSE;
7999 }
8000}
8001
8002/*
8003 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8004 * Should check VIsual_mode before calling this.
8005 * Returns TRUE when backed up to the previous line.
8006 */
8007 static int
8008unadjust_for_sel()
8009{
8010 pos_T *pp;
8011
8012 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8013 {
8014 if (lt(VIsual, curwin->w_cursor))
8015 pp = &curwin->w_cursor;
8016 else
8017 pp = &VIsual;
8018#ifdef FEAT_VIRTUALEDIT
8019 if (pp->coladd > 0)
8020 --pp->coladd;
8021 else
8022#endif
8023 if (pp->col > 0)
8024 {
8025 --pp->col;
8026#ifdef FEAT_MBYTE
8027 mb_adjustpos(pp);
8028#endif
8029 }
8030 else if (pp->lnum > 1)
8031 {
8032 --pp->lnum;
8033 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8034 return TRUE;
8035 }
8036 }
8037 return FALSE;
8038}
8039
8040/*
8041 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8042 */
8043 static void
8044nv_select(cap)
8045 cmdarg_T *cap;
8046{
8047 if (VIsual_active)
8048 VIsual_select = TRUE;
8049 else if (VIsual_reselect)
8050 {
8051 cap->nchar = 'v'; /* fake "gv" command */
8052 cap->arg = TRUE;
8053 nv_g_cmd(cap);
8054 }
8055}
8056
8057#endif
8058
8059/*
8060 * "G", "gg", CTRL-END, CTRL-HOME.
8061 * cap->arg is TRUE for "G".
8062 */
8063 static void
8064nv_goto(cap)
8065 cmdarg_T *cap;
8066{
8067 linenr_T lnum;
8068
8069 if (cap->arg)
8070 lnum = curbuf->b_ml.ml_line_count;
8071 else
8072 lnum = 1L;
8073 cap->oap->motion_type = MLINE;
8074 setpcmark();
8075
8076 /* When a count is given, use it instead of the default lnum */
8077 if (cap->count0 != 0)
8078 lnum = cap->count0;
8079 if (lnum < 1L)
8080 lnum = 1L;
8081 else if (lnum > curbuf->b_ml.ml_line_count)
8082 lnum = curbuf->b_ml.ml_line_count;
8083 curwin->w_cursor.lnum = lnum;
8084 beginline(BL_SOL | BL_FIX);
8085#ifdef FEAT_FOLDING
8086 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8087 foldOpenCursor();
8088#endif
8089}
8090
8091/*
8092 * CTRL-\ in Normal mode.
8093 */
8094 static void
8095nv_normal(cap)
8096 cmdarg_T *cap;
8097{
8098 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8099 {
8100 clearop(cap->oap);
8101 if (restart_edit != 0 && p_smd)
8102 clear_cmdline = TRUE; /* unshow mode later */
8103 restart_edit = 0;
8104#ifdef FEAT_CMDWIN
8105 if (cmdwin_type != 0)
8106 cmdwin_result = Ctrl_C;
8107#endif
8108#ifdef FEAT_VISUAL
8109 if (VIsual_active)
8110 {
8111 end_visual_mode(); /* stop Visual */
8112 redraw_curbuf_later(INVERTED);
8113 }
8114#endif
8115 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8116 if (cap->nchar == Ctrl_G && p_im)
8117 restart_edit = 'a';
8118 }
8119 else
8120 clearopbeep(cap->oap);
8121}
8122
8123/*
8124 * ESC in Normal mode: beep, but don't flush buffers.
8125 * Don't even beep if we are canceling a command.
8126 */
8127 static void
8128nv_esc(cap)
8129 cmdarg_T *cap;
8130{
8131 int no_reason;
8132
8133 no_reason = (cap->oap->op_type == OP_NOP
8134 && cap->opcount == 0
8135 && cap->count0 == 0
8136 && cap->oap->regname == 0
8137 && !p_im);
8138
8139 if (cap->arg) /* TRUE for CTRL-C */
8140 {
8141 if (restart_edit == 0
8142#ifdef FEAT_CMDWIN
8143 && cmdwin_type == 0
8144#endif
8145#ifdef FEAT_VISUAL
8146 && !VIsual_active
8147#endif
8148 && no_reason)
8149 MSG(_("Type :quit<Enter> to exit Vim"));
8150
8151 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8152 * set again below when halfway a mapping. */
8153 if (!p_im)
8154 restart_edit = 0;
8155#ifdef FEAT_CMDWIN
8156 if (cmdwin_type != 0)
8157 {
8158 cmdwin_result = K_IGNORE;
8159 got_int = FALSE; /* don't stop executing autocommands et al. */
8160 return;
8161 }
8162#endif
8163 }
8164
8165#ifdef FEAT_VISUAL
8166 if (VIsual_active)
8167 {
8168 end_visual_mode(); /* stop Visual */
8169 check_cursor_col(); /* make sure cursor is not beyond EOL */
8170 curwin->w_set_curswant = TRUE;
8171 redraw_curbuf_later(INVERTED);
8172 }
8173 else
8174#endif
8175 if (no_reason)
8176 vim_beep();
8177 clearop(cap->oap);
8178
8179 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8180 * set return to Insert mode afterwards. */
8181 if (restart_edit == 0 && goto_im()
8182#ifdef FEAT_EX_EXTRA
8183 && ex_normal_busy == 0
8184#endif
8185 )
8186 restart_edit = 'a';
8187}
8188
8189/*
8190 * Handle "A", "a", "I", "i" and <Insert> commands.
8191 */
8192 static void
8193nv_edit(cap)
8194 cmdarg_T *cap;
8195{
8196 /* <Insert> is equal to "i" */
8197 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8198 cap->cmdchar = 'i';
8199
8200#ifdef FEAT_VISUAL
8201 /* in Visual mode "A" and "I" are an operator */
8202 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8203 v_visop(cap);
8204
8205 /* in Visual mode and after an operator "a" and "i" are for text objects */
8206 else
8207#endif
8208 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8209 && (cap->oap->op_type != OP_NOP
8210#ifdef FEAT_VISUAL
8211 || VIsual_active
8212#endif
8213 ))
8214 {
8215#ifdef FEAT_TEXTOBJ
8216 nv_object(cap);
8217#else
8218 clearopbeep(cap->oap);
8219#endif
8220 }
8221 else if (!curbuf->b_p_ma && !p_im)
8222 {
8223 /* Only give this error when 'insertmode' is off. */
8224 EMSG(_(e_modifiable));
8225 clearop(cap->oap);
8226 }
8227 else if (!checkclearopq(cap->oap))
8228 {
8229 switch (cap->cmdchar)
8230 {
8231 case 'A': /* "A"ppend after the line */
8232 curwin->w_set_curswant = TRUE;
8233#ifdef FEAT_VIRTUALEDIT
8234 if (ve_flags == VE_ALL)
8235 {
8236 int save_State = State;
8237
8238 /* Pretent Insert mode here to allow the cursor on the
8239 * character past the end of the line */
8240 State = INSERT;
8241 coladvance((colnr_T)MAXCOL);
8242 State = save_State;
8243 }
8244 else
8245#endif
8246 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8247 break;
8248
8249 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008250 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
8251 beginline(BL_WHITE);
8252 else
8253 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 break;
8255
8256 case 'a': /* "a"ppend is like "i"nsert on the next character. */
8257#ifdef FEAT_VIRTUALEDIT
8258 /* increment coladd when in virtual space, increment the
8259 * column otherwise, also to append after an unprintable char */
8260 if (virtual_active()
8261 && (curwin->w_cursor.coladd > 0
8262 || *ml_get_cursor() == NUL
8263 || *ml_get_cursor() == TAB))
8264 curwin->w_cursor.coladd++;
8265 else
8266#endif
8267 if (*ml_get_cursor() != NUL)
8268 inc_cursor();
8269 break;
8270 }
8271
8272#ifdef FEAT_VIRTUALEDIT
8273 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
8274 {
8275 int save_State = State;
8276
8277 /* Pretent Insert mode here to allow the cursor on the
8278 * character past the end of the line */
8279 State = INSERT;
8280 coladvance(getviscol());
8281 State = save_State;
8282 }
8283#endif
8284
8285 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
8286 }
8287}
8288
8289/*
8290 * Invoke edit() and take care of "restart_edit" and the return value.
8291 */
8292 static void
8293invoke_edit(cap, repl, cmd, startln)
8294 cmdarg_T *cap;
8295 int repl; /* "r" or "gr" command */
8296 int cmd;
8297 int startln;
8298{
8299 int restart_edit_save = 0;
8300
8301 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
8302 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
8303 * it. */
8304 if (repl || !stuff_empty())
8305 restart_edit_save = restart_edit;
8306 else
8307 restart_edit_save = 0;
8308
8309 /* Always reset "restart_edit", this is not a restarted edit. */
8310 restart_edit = 0;
8311
8312 if (edit(cmd, startln, cap->count1))
8313 cap->retval |= CA_COMMAND_BUSY;
8314
8315 if (restart_edit == 0)
8316 restart_edit = restart_edit_save;
8317}
8318
8319#ifdef FEAT_TEXTOBJ
8320/*
8321 * "a" or "i" while an operator is pending or in Visual mode: object motion.
8322 */
8323 static void
8324nv_object(cap)
8325 cmdarg_T *cap;
8326{
8327 int flag;
8328 int include;
8329 char_u *mps_save;
8330
8331 if (cap->cmdchar == 'i')
8332 include = FALSE; /* "ix" = inner object: exclude white space */
8333 else
8334 include = TRUE; /* "ax" = an object: include white space */
8335
8336 /* Make sure (), [], {} and <> are in 'matchpairs' */
8337 mps_save = curbuf->b_p_mps;
8338 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
8339
8340 switch (cap->nchar)
8341 {
8342 case 'w': /* "aw" = a word */
8343 flag = current_word(cap->oap, cap->count1, include, FALSE);
8344 break;
8345 case 'W': /* "aW" = a WORD */
8346 flag = current_word(cap->oap, cap->count1, include, TRUE);
8347 break;
8348 case 'b': /* "ab" = a braces block */
8349 case '(':
8350 case ')':
8351 flag = current_block(cap->oap, cap->count1, include, '(', ')');
8352 break;
8353 case 'B': /* "aB" = a Brackets block */
8354 case '{':
8355 case '}':
8356 flag = current_block(cap->oap, cap->count1, include, '{', '}');
8357 break;
8358 case '[': /* "a[" = a [] block */
8359 case ']':
8360 flag = current_block(cap->oap, cap->count1, include, '[', ']');
8361 break;
8362 case '<': /* "a<" = a <> block */
8363 case '>':
8364 flag = current_block(cap->oap, cap->count1, include, '<', '>');
8365 break;
8366 case 'p': /* "ap" = a paragraph */
8367 flag = current_par(cap->oap, cap->count1, include, 'p');
8368 break;
8369 case 's': /* "as" = a sentence */
8370 flag = current_sent(cap->oap, cap->count1, include);
8371 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00008372 case '"': /* "a"" = a double quoted string */
8373 case '\'': /* "a'" = a single quoted string */
8374 case '`': /* "a`" = a backtick quoted string */
8375 flag = current_quote(cap->oap, cap->count1, include,
8376 cap->nchar);
8377 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378#if 0 /* TODO */
8379 case 'S': /* "aS" = a section */
8380 case 'f': /* "af" = a filename */
8381 case 'u': /* "au" = a URL */
8382#endif
8383 default:
8384 flag = FAIL;
8385 break;
8386 }
8387
8388 curbuf->b_p_mps = mps_save;
8389 if (flag == FAIL)
8390 clearopbeep(cap->oap);
8391 adjust_cursor_col();
8392 curwin->w_set_curswant = TRUE;
8393}
8394#endif
8395
8396/*
8397 * "q" command: Start/stop recording.
8398 * "q:", "q/", "q?": edit command-line in command-line window.
8399 */
8400 static void
8401nv_record(cap)
8402 cmdarg_T *cap;
8403{
8404 if (cap->oap->op_type == OP_FORMAT)
8405 {
8406 /* "gqq" is the same as "gqgq": format line */
8407 cap->cmdchar = 'g';
8408 cap->nchar = 'q';
8409 nv_operator(cap);
8410 }
8411 else if (!checkclearop(cap->oap))
8412 {
8413#ifdef FEAT_CMDWIN
8414 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
8415 {
8416 stuffcharReadbuff(cap->nchar);
8417 stuffcharReadbuff(K_CMDWIN);
8418 }
8419 else
8420#endif
8421 /* (stop) recording into a named register, unless executing a
8422 * register */
8423 if (!Exec_reg && do_record(cap->nchar) == FAIL)
8424 clearopbeep(cap->oap);
8425 }
8426}
8427
8428/*
8429 * Handle the "@r" command.
8430 */
8431 static void
8432nv_at(cap)
8433 cmdarg_T *cap;
8434{
8435 if (checkclearop(cap->oap))
8436 return;
8437#ifdef FEAT_EVAL
8438 if (cap->nchar == '=')
8439 {
8440 if (get_expr_register() == NUL)
8441 return;
8442 }
8443#endif
8444 while (cap->count1-- && !got_int)
8445 {
8446 if (do_execreg(cap->nchar, FALSE, FALSE) == FAIL)
8447 {
8448 clearopbeep(cap->oap);
8449 break;
8450 }
8451 line_breakcheck();
8452 }
8453}
8454
8455/*
8456 * Handle the CTRL-U and CTRL-D commands.
8457 */
8458 static void
8459nv_halfpage(cap)
8460 cmdarg_T *cap;
8461{
8462 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
8463 || (cap->cmdchar == Ctrl_D
8464 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
8465 clearopbeep(cap->oap);
8466 else if (!checkclearop(cap->oap))
8467 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
8468}
8469
8470/*
8471 * Handle "J" or "gJ" command.
8472 */
8473 static void
8474nv_join(cap)
8475 cmdarg_T *cap;
8476{
8477#ifdef FEAT_VISUAL
8478 if (VIsual_active) /* join the visual lines */
8479 nv_operator(cap);
8480 else
8481#endif
8482 if (!checkclearop(cap->oap))
8483 {
8484 if (cap->count0 <= 1)
8485 cap->count0 = 2; /* default for join is two lines! */
8486 if (curwin->w_cursor.lnum + cap->count0 - 1 >
8487 curbuf->b_ml.ml_line_count)
8488 clearopbeep(cap->oap); /* beyond last line */
8489 else
8490 {
8491 prep_redo(cap->oap->regname, cap->count0,
8492 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
8493 do_do_join(cap->count0, cap->nchar == NUL);
8494 }
8495 }
8496}
8497
8498/*
8499 * "P", "gP", "p" and "gp" commands.
8500 */
8501 static void
8502nv_put(cap)
8503 cmdarg_T *cap;
8504{
8505#ifdef FEAT_VISUAL
8506 int regname = 0;
8507 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008508 int empty = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509#endif
8510 int dir;
8511 int flags = 0;
8512
8513 if (cap->oap->op_type != OP_NOP)
8514 {
8515#ifdef FEAT_DIFF
8516 /* "dp" is ":diffput" */
8517 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
8518 {
8519 clearop(cap->oap);
8520 nv_diffgetput(TRUE);
8521 }
8522 else
8523#endif
8524 clearopbeep(cap->oap);
8525 }
8526 else
8527 {
8528 dir = (cap->cmdchar == 'P'
8529 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
8530 ? BACKWARD : FORWARD;
8531 prep_redo_cmd(cap);
8532 if (cap->cmdchar == 'g')
8533 flags |= PUT_CURSEND;
8534
8535#ifdef FEAT_VISUAL
8536 if (VIsual_active)
8537 {
8538 /* Putting in Visual mode: The put text replaces the selected
8539 * text. First delete the selected text, then put the new text.
8540 * Need to save and restore the registers that the delete
8541 * overwrites if the old contents is being put.
8542 */
8543 regname = cap->oap->regname;
8544# ifdef FEAT_CLIPBOARD
8545 adjust_clip_reg(&regname);
8546# endif
8547 if (regname == 0 || VIM_ISDIGIT(regname)
8548# ifdef FEAT_CLIPBOARD
8549 || (clip_unnamed && (regname == '*' || regname == '+'))
8550# endif
8551
8552 )
8553 {
8554 /* the delete is going to overwrite the register we want to
8555 * put, save it first. */
8556 reg1 = get_register(regname, TRUE);
8557 }
8558
8559 /* Now delete the selected text. */
8560 cap->cmdchar = 'd';
8561 cap->nchar = NUL;
8562 cap->oap->regname = NUL;
8563 nv_operator(cap);
8564 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008565 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566
8567 /* delete PUT_LINE_BACKWARD; */
8568 cap->oap->regname = regname;
8569
8570 if (reg1 != NULL)
8571 {
8572 /* Delete probably changed the register we want to put, save
8573 * it first. Then put back what was there before the delete. */
8574 reg2 = get_register(regname, FALSE);
8575 put_register(regname, reg1);
8576 }
8577
8578 /* When deleted a linewise Visual area, put the register as
8579 * lines to avoid it joined with the next line. When deletion was
8580 * characterwise, split a line when putting lines. */
8581 if (VIsual_mode == 'V')
8582 flags |= PUT_LINE;
8583 else if (VIsual_mode == 'v')
8584 flags |= PUT_LINE_SPLIT;
8585 if (VIsual_mode == Ctrl_V && dir == FORWARD)
8586 flags |= PUT_LINE_FORWARD;
8587 dir = BACKWARD;
8588 if ((VIsual_mode != 'V'
8589 && curwin->w_cursor.col < curbuf->b_op_start.col)
8590 || (VIsual_mode == 'V'
8591 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
8592 /* cursor is at the end of the line or end of file, put
8593 * forward. */
8594 dir = FORWARD;
8595 }
8596#endif
8597 do_put(cap->oap->regname, dir, cap->count1, flags);
8598
8599#ifdef FEAT_VISUAL
8600 /* If a register was saved, put it back now. */
8601 if (reg2 != NULL)
8602 put_register(regname, reg2);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008603 /* When all lines were selected and deleted do_put() leaves an empty
8604 * line that needs to delete now. */
8605 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
8606 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607#endif
8608 auto_format(FALSE, TRUE);
8609 }
8610}
8611
8612/*
8613 * "o" and "O" commands.
8614 */
8615 static void
8616nv_open(cap)
8617 cmdarg_T *cap;
8618{
8619#ifdef FEAT_DIFF
8620 /* "do" is ":diffget" */
8621 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
8622 {
8623 clearop(cap->oap);
8624 nv_diffgetput(FALSE);
8625 }
8626 else
8627#endif
8628#ifdef FEAT_VISUAL
8629 if (VIsual_active) /* switch start and end of visual */
8630 v_swap_corners(cap->cmdchar);
8631 else
8632#endif
8633 n_opencmd(cap);
8634}
8635
8636#ifdef FEAT_SNIFF
8637/*ARGSUSED*/
8638 static void
8639nv_sniff(cap)
8640 cmdarg_T *cap;
8641{
8642 ProcessSniffRequests();
8643}
8644#endif
8645
8646#ifdef FEAT_NETBEANS_INTG
8647 static void
8648nv_nbcmd(cap)
8649 cmdarg_T *cap;
8650{
8651 netbeans_keycommand(cap->nchar);
8652}
8653#endif
8654
8655#ifdef FEAT_DND
8656/*ARGSUSED*/
8657 static void
8658nv_drop(cap)
8659 cmdarg_T *cap;
8660{
8661 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8662}
8663#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00008664
8665#ifdef FEAT_AUTOCMD
8666/*
8667 * Trigger CursorHold event.
8668 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
8669 * input buffer. "did_cursorhold" is set to avoid retriggering.
8670 */
8671/*ARGSUSED*/
8672 static void
8673nv_cursorhold(cap)
8674 cmdarg_T *cap;
8675{
8676 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
8677 did_cursorhold = TRUE;
8678}
8679#endif
8680