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