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