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