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