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