blob: fb6f8f98196063219b346a827d053dcfe6e831e9 [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
3932 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0 ||
3933 (pat = alloc(len + 5)) == NULL)
3934 {
3935 clearopbeep(oap);
3936 return;
3937 }
3938 sprintf((char *)pat, vim_iswordp(ptr) ? "\\<%.*s\\>" : "%.*s", len, ptr);
3939 old_pos = curwin->w_cursor;
3940 save_p_ws = p_ws;
3941 save_p_scs = p_scs;
3942 p_ws = FALSE; /* don't wrap around end of file now */
3943 p_scs = FALSE; /* don't switch ignorecase off now */
3944
3945 /*
3946 * With "gD" go to line 1.
3947 * With "gd" Search back for the start of the current function, then go
3948 * back until a blank line. If this fails go to line 1.
3949 */
3950 if (nchar == 'D' || !findpar(oap, BACKWARD, 1L, '{', FALSE))
3951 {
3952 setpcmark(); /* Set in findpar() otherwise */
3953 curwin->w_cursor.lnum = 1;
3954 }
3955 else
3956 {
3957 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
3958 --curwin->w_cursor.lnum;
3959 }
3960 curwin->w_cursor.col = 0;
3961
3962 /* Search forward for the identifier, ignore comment lines. */
3963 while ((t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD, pat, 1L, 0,
3964 RE_LAST)) != FAIL
3965#ifdef FEAT_COMMENTS
3966 && get_leader_len(ml_get_curline(), NULL, FALSE) > 0
3967#endif
3968 && old_pos.lnum > curwin->w_cursor.lnum)
3969 {
3970 /* Ignore this line, continue at start of next line. */
3971 ++curwin->w_cursor.lnum;
3972 curwin->w_cursor.col = 0;
3973 }
3974 if (t == FAIL || old_pos.lnum <= curwin->w_cursor.lnum)
3975 {
3976 clearopbeep(oap);
3977 curwin->w_cursor = old_pos;
3978 }
3979 else
3980 {
3981 curwin->w_set_curswant = TRUE;
3982#ifdef FEAT_FOLDING
3983 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
3984 foldOpenCursor();
3985#endif
3986 /* "n" searches forward now */
3987 reset_search_dir();
3988 }
3989
3990 vim_free(pat);
3991 p_ws = save_p_ws;
3992 p_scs = save_p_scs;
3993}
3994
3995/*
3996 * Move 'dist' lines in direction 'dir', counting lines by *screen*
3997 * lines rather than lines in the file.
3998 * 'dist' must be positive.
3999 *
4000 * Return OK if able to move cursor, FAIL otherwise.
4001 */
4002 static int
4003nv_screengo(oap, dir, dist)
4004 oparg_T *oap;
4005 int dir;
4006 long dist;
4007{
4008 int linelen = linetabsize(ml_get_curline());
4009 int retval = OK;
4010 int atend = FALSE;
4011 int n;
4012 int col_off1; /* margin offset for first screen line */
4013 int col_off2; /* margin offset for wrapped screen line */
4014 int width1; /* text width for first screen line */
4015 int width2; /* test width for wrapped screen line */
4016
4017 oap->motion_type = MCHAR;
4018 oap->inclusive = FALSE;
4019
4020 col_off1 = curwin_col_off();
4021 col_off2 = col_off1 - curwin_col_off2();
4022 width1 = W_WIDTH(curwin) - col_off1;
4023 width2 = W_WIDTH(curwin) - col_off2;
4024
4025#ifdef FEAT_VERTSPLIT
4026 if (curwin->w_width != 0)
4027 {
4028#endif
4029 /*
4030 * Instead of sticking at the last character of the buffer line we
4031 * try to stick in the last column of the screen.
4032 */
4033 if (curwin->w_curswant == MAXCOL)
4034 {
4035 atend = TRUE;
4036 validate_virtcol();
4037 if (width1 <= 0)
4038 curwin->w_curswant = 0;
4039 else
4040 {
4041 curwin->w_curswant = width1 - 1;
4042 if (curwin->w_virtcol > curwin->w_curswant)
4043 curwin->w_curswant += ((curwin->w_virtcol
4044 - curwin->w_curswant - 1) / width2 + 1) * width2;
4045 }
4046 }
4047 else
4048 {
4049 if (linelen > width1)
4050 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4051 else
4052 n = width1;
4053 if (curwin->w_curswant > (colnr_T)n + 1)
4054 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4055 * width2;
4056 }
4057
4058 while (dist--)
4059 {
4060 if (dir == BACKWARD)
4061 {
4062 if ((long)curwin->w_curswant >= width2)
4063 /* move back within line */
4064 curwin->w_curswant -= width2;
4065 else
4066 {
4067 /* to previous line */
4068 if (curwin->w_cursor.lnum == 1)
4069 {
4070 retval = FAIL;
4071 break;
4072 }
4073 --curwin->w_cursor.lnum;
4074#ifdef FEAT_FOLDING
4075 /* Move to the start of a closed fold. Don't do that when
4076 * 'foldopen' contains "all": it will open in a moment. */
4077 if (!(fdo_flags & FDO_ALL))
4078 (void)hasFolding(curwin->w_cursor.lnum,
4079 &curwin->w_cursor.lnum, NULL);
4080#endif
4081 linelen = linetabsize(ml_get_curline());
4082 if (linelen > width1)
4083 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4084 + 1) * width2;
4085 }
4086 }
4087 else /* dir == FORWARD */
4088 {
4089 if (linelen > width1)
4090 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4091 else
4092 n = width1;
4093 if (curwin->w_curswant + width2 < (colnr_T)n)
4094 /* move forward within line */
4095 curwin->w_curswant += width2;
4096 else
4097 {
4098 /* to next line */
4099#ifdef FEAT_FOLDING
4100 /* Move to the end of a closed fold. */
4101 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4102 &curwin->w_cursor.lnum);
4103#endif
4104 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4105 {
4106 retval = FAIL;
4107 break;
4108 }
4109 curwin->w_cursor.lnum++;
4110 curwin->w_curswant %= width2;
4111 }
4112 }
4113 }
4114#ifdef FEAT_VERTSPLIT
4115 }
4116#endif
4117
4118 coladvance(curwin->w_curswant);
4119
4120#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4121 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4122 {
4123 /*
4124 * Check for landing on a character that got split at the end of the
4125 * last line. We want to advance a screenline, not end up in the same
4126 * screenline or move two screenlines.
4127 */
4128 validate_virtcol();
4129 if (curwin->w_virtcol > curwin->w_curswant
4130 && (curwin->w_curswant < (colnr_T)width1
4131 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4132 : ((curwin->w_curswant - width1) % width2
4133 > (colnr_T)width2 / 2)))
4134 --curwin->w_cursor.col;
4135 }
4136#endif
4137
4138 if (atend)
4139 curwin->w_curswant = MAXCOL; /* stick in the last column */
4140
4141 return retval;
4142}
4143
4144#ifdef FEAT_MOUSE
4145/*
4146 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4147 * when Shift or Ctrl is used.
4148 * K_MOUSEUP (cap->arg == TRUE) or K_MOUSEDOWN (cap->arg == FALSE)
4149 */
4150 static void
4151nv_mousescroll(cap)
4152 cmdarg_T *cap;
4153{
4154# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4155 win_T *old_curwin;
4156
4157 old_curwin = curwin;
4158
4159 /* Currently we only get the mouse coordinates in the GUI. */
4160 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4161 {
4162 int row, col;
4163
4164 row = mouse_row;
4165 col = mouse_col;
4166
4167 /* find the window at the pointer coordinates */
4168 curwin = mouse_find_win(&row, &col);
4169 curbuf = curwin->w_buffer;
4170 }
4171# endif
4172
4173 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4174 {
4175 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4176 }
4177 else
4178 {
4179 cap->count1 = 3;
4180 cap->count0 = 3;
4181 nv_scroll_line(cap);
4182 }
4183
4184# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4185 curwin->w_redr_status = TRUE;
4186
4187 curwin = old_curwin;
4188 curbuf = curwin->w_buffer;
4189# endif
4190}
4191
4192/*
4193 * Mouse clicks and drags.
4194 */
4195 static void
4196nv_mouse(cap)
4197 cmdarg_T *cap;
4198{
4199 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4200}
4201#endif
4202
4203/*
4204 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4205 * cap->arg must be TRUE for CTRL-E.
4206 */
4207 static void
4208nv_scroll_line(cap)
4209 cmdarg_T *cap;
4210{
4211 if (!checkclearop(cap->oap))
4212 scroll_redraw(cap->arg, cap->count1);
4213}
4214
4215/*
4216 * Scroll "count" lines up or down, and redraw.
4217 */
4218 void
4219scroll_redraw(up, count)
4220 int up;
4221 long count;
4222{
4223 linenr_T prev_topline = curwin->w_topline;
4224#ifdef FEAT_DIFF
4225 int prev_topfill = curwin->w_topfill;
4226#endif
4227 linenr_T prev_lnum = curwin->w_cursor.lnum;
4228
4229 if (up)
4230 scrollup(count, TRUE);
4231 else
4232 scrolldown(count, TRUE);
4233 if (p_so)
4234 {
4235 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4236 * valid, otherwise the screen jumps back at the end of the file. */
4237 cursor_correct();
4238 check_cursor_moved(curwin);
4239 curwin->w_valid |= VALID_TOPLINE;
4240
4241 /* If moved back to where we were, at least move the cursor, otherwise
4242 * we get stuck at one position. Don't move the cursor up if the
4243 * first line of the buffer is already on the screen */
4244 while (curwin->w_topline == prev_topline
4245#ifdef FEAT_DIFF
4246 && curwin->w_topfill == prev_topfill
4247#endif
4248 )
4249 {
4250 if (up)
4251 {
4252 if (curwin->w_cursor.lnum > prev_lnum
4253 || cursor_down(1L, FALSE) == FAIL)
4254 break;
4255 }
4256 else
4257 {
4258 if (curwin->w_cursor.lnum < prev_lnum
4259 || prev_topline == 1L
4260 || cursor_up(1L, FALSE) == FAIL)
4261 break;
4262 }
4263 /* Mark w_topline as valid, otherwise the screen jumps back at the
4264 * end of the file. */
4265 check_cursor_moved(curwin);
4266 curwin->w_valid |= VALID_TOPLINE;
4267 }
4268 }
4269 if (curwin->w_cursor.lnum != prev_lnum)
4270 coladvance(curwin->w_curswant);
4271 redraw_later(VALID);
4272}
4273
4274/*
4275 * Commands that start with "z".
4276 */
4277 static void
4278nv_zet(cap)
4279 cmdarg_T *cap;
4280{
4281 long n;
4282 colnr_T col;
4283 int nchar = cap->nchar;
4284#ifdef FEAT_FOLDING
4285 long old_fdl = curwin->w_p_fdl;
4286 int old_fen = curwin->w_p_fen;
4287#endif
4288
4289 if (VIM_ISDIGIT(nchar))
4290 {
4291 /*
4292 * "z123{nchar}": edit the count before obtaining {nchar}
4293 */
4294 if (checkclearop(cap->oap))
4295 return;
4296 n = nchar - '0';
4297 for (;;)
4298 {
4299#ifdef USE_ON_FLY_SCROLL
4300 dont_scroll = TRUE; /* disallow scrolling here */
4301#endif
4302 ++no_mapping;
4303 ++allow_keys; /* no mapping for nchar, but allow key codes */
4304 nchar = safe_vgetc();
4305#ifdef FEAT_LANGMAP
4306 LANGMAP_ADJUST(nchar, TRUE);
4307#endif
4308 --no_mapping;
4309 --allow_keys;
4310#ifdef FEAT_CMDL_INFO
4311 (void)add_to_showcmd(nchar);
4312#endif
4313 if (nchar == K_DEL || nchar == K_KDEL)
4314 n /= 10;
4315 else if (VIM_ISDIGIT(nchar))
4316 n = n * 10 + (nchar - '0');
4317 else if (nchar == CAR)
4318 {
4319#ifdef FEAT_GUI
4320 need_mouse_correct = TRUE;
4321#endif
4322 win_setheight((int)n);
4323 break;
4324 }
4325 else if (nchar == 'l'
4326 || nchar == 'h'
4327 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004328 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 {
4330 cap->count1 = n ? n * cap->count1 : cap->count1;
4331 goto dozet;
4332 }
4333 else
4334 {
4335 clearopbeep(cap->oap);
4336 break;
4337 }
4338 }
4339 cap->oap->op_type = OP_NOP;
4340 return;
4341 }
4342
4343dozet:
4344 if (
4345#ifdef FEAT_FOLDING
4346 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4347 * and "zC" only in Visual mode. "zj" and "zk" are motion
4348 * commands. */
4349 cap->nchar != 'f' && cap->nchar != 'F'
4350 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4351 && cap->nchar != 'j' && cap->nchar != 'k'
4352 &&
4353#endif
4354 checkclearop(cap->oap))
4355 return;
4356
4357 /*
4358 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4359 * If line number given, set cursor.
4360 */
4361 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4362 && cap->count0
4363 && cap->count0 != curwin->w_cursor.lnum)
4364 {
4365 setpcmark();
4366 if (cap->count0 > curbuf->b_ml.ml_line_count)
4367 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4368 else
4369 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004370 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 }
4372
4373 switch (nchar)
4374 {
4375 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4376 case '+':
4377 if (cap->count0 == 0)
4378 {
4379 /* No count given: put cursor at the line below screen */
4380 validate_botline(); /* make sure w_botline is valid */
4381 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4382 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4383 else
4384 curwin->w_cursor.lnum = curwin->w_botline;
4385 }
4386 /* FALLTHROUGH */
4387 case NL:
4388 case CAR:
4389 case K_KENTER:
4390 beginline(BL_WHITE | BL_FIX);
4391 /* FALLTHROUGH */
4392
4393 case 't': scroll_cursor_top(0, TRUE);
4394 redraw_later(VALID);
4395 break;
4396
4397 /* "z." and "zz": put cursor in middle of screen */
4398 case '.': beginline(BL_WHITE | BL_FIX);
4399 /* FALLTHROUGH */
4400
4401 case 'z': scroll_cursor_halfway(TRUE);
4402 redraw_later(VALID);
4403 break;
4404
4405 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4406 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4407 * when <count> is at bottom of window, and puts that one at
4408 * bottom of window. */
4409 if (cap->count0 != 0)
4410 {
4411 scroll_cursor_bot(0, TRUE);
4412 curwin->w_cursor.lnum = curwin->w_topline;
4413 }
4414 else if (curwin->w_topline == 1)
4415 curwin->w_cursor.lnum = 1;
4416 else
4417 curwin->w_cursor.lnum = curwin->w_topline - 1;
4418 /* FALLTHROUGH */
4419 case '-':
4420 beginline(BL_WHITE | BL_FIX);
4421 /* FALLTHROUGH */
4422
4423 case 'b': scroll_cursor_bot(0, TRUE);
4424 redraw_later(VALID);
4425 break;
4426
4427 /* "zH" - scroll screen right half-page */
4428 case 'H':
4429 cap->count1 *= W_WIDTH(curwin) / 2;
4430 /* FALLTHROUGH */
4431
4432 /* "zh" - scroll screen to the right */
4433 case 'h':
4434 case K_LEFT:
4435 if (!curwin->w_p_wrap)
4436 {
4437 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4438 curwin->w_leftcol = 0;
4439 else
4440 curwin->w_leftcol -= (colnr_T)cap->count1;
4441 leftcol_changed();
4442 }
4443 break;
4444
4445 /* "zL" - scroll screen left half-page */
4446 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4447 /* FALLTHROUGH */
4448
4449 /* "zl" - scroll screen to the left */
4450 case 'l':
4451 case K_RIGHT:
4452 if (!curwin->w_p_wrap)
4453 {
4454 /* scroll the window left */
4455 curwin->w_leftcol += (colnr_T)cap->count1;
4456 leftcol_changed();
4457 }
4458 break;
4459
4460 /* "zs" - scroll screen, cursor at the start */
4461 case 's': if (!curwin->w_p_wrap)
4462 {
4463#ifdef FEAT_FOLDING
4464 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4465 col = 0; /* like the cursor is in col 0 */
4466 else
4467#endif
4468 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4469 if ((long)col > p_siso)
4470 col -= p_siso;
4471 else
4472 col = 0;
4473 if (curwin->w_leftcol != col)
4474 {
4475 curwin->w_leftcol = col;
4476 redraw_later(NOT_VALID);
4477 }
4478 }
4479 break;
4480
4481 /* "ze" - scroll screen, cursor at the end */
4482 case 'e': if (!curwin->w_p_wrap)
4483 {
4484#ifdef FEAT_FOLDING
4485 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4486 col = 0; /* like the cursor is in col 0 */
4487 else
4488#endif
4489 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4490 n = W_WIDTH(curwin) - curwin_col_off();
4491 if ((long)col + p_siso < n)
4492 col = 0;
4493 else
4494 col = col + p_siso - n + 1;
4495 if (curwin->w_leftcol != col)
4496 {
4497 curwin->w_leftcol = col;
4498 redraw_later(NOT_VALID);
4499 }
4500 }
4501 break;
4502
4503#ifdef FEAT_FOLDING
4504 /* "zF": create fold command */
4505 /* "zf": create fold operator */
4506 case 'F':
4507 case 'f': if (foldManualAllowed(TRUE))
4508 {
4509 cap->nchar = 'f';
4510 nv_operator(cap);
4511 curwin->w_p_fen = TRUE;
4512
4513 /* "zF" is like "zfzf" */
4514 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4515 {
4516 nv_operator(cap);
4517 finish_op = TRUE;
4518 }
4519 }
4520 else
4521 clearopbeep(cap->oap);
4522 break;
4523
4524 /* "zd": delete fold at cursor */
4525 /* "zD": delete fold at cursor recursively */
4526 case 'd':
4527 case 'D': if (foldManualAllowed(FALSE))
4528 {
4529 if (VIsual_active)
4530 nv_operator(cap);
4531 else
4532 deleteFold(curwin->w_cursor.lnum,
4533 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4534 }
4535 break;
4536
4537 /* "zE": erease all folds */
4538 case 'E': if (foldmethodIsManual(curwin))
4539 {
4540 clearFolding(curwin);
4541 changed_window_setting();
4542 }
4543 else if (foldmethodIsMarker(curwin))
4544 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4545 TRUE, FALSE);
4546 else
4547 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4548 break;
4549
4550 /* "zn": fold none: reset 'foldenable' */
4551 case 'n': curwin->w_p_fen = FALSE;
4552 break;
4553
4554 /* "zN": fold Normal: set 'foldenable' */
4555 case 'N': curwin->w_p_fen = TRUE;
4556 break;
4557
4558 /* "zi": invert folding: toggle 'foldenable' */
4559 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4560 break;
4561
4562 /* "za": open closed fold or close open fold at cursor */
4563 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4564 openFold(curwin->w_cursor.lnum, cap->count1);
4565 else
4566 {
4567 closeFold(curwin->w_cursor.lnum, cap->count1);
4568 curwin->w_p_fen = TRUE;
4569 }
4570 break;
4571
4572 /* "zA": open fold at cursor recursively */
4573 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4574 openFoldRecurse(curwin->w_cursor.lnum);
4575 else
4576 {
4577 closeFoldRecurse(curwin->w_cursor.lnum);
4578 curwin->w_p_fen = TRUE;
4579 }
4580 break;
4581
4582 /* "zo": open fold at cursor or Visual area */
4583 case 'o': if (VIsual_active)
4584 nv_operator(cap);
4585 else
4586 openFold(curwin->w_cursor.lnum, cap->count1);
4587 break;
4588
4589 /* "zO": open fold recursively */
4590 case 'O': if (VIsual_active)
4591 nv_operator(cap);
4592 else
4593 openFoldRecurse(curwin->w_cursor.lnum);
4594 break;
4595
4596 /* "zc": close fold at cursor or Visual area */
4597 case 'c': if (VIsual_active)
4598 nv_operator(cap);
4599 else
4600 closeFold(curwin->w_cursor.lnum, cap->count1);
4601 curwin->w_p_fen = TRUE;
4602 break;
4603
4604 /* "zC": close fold recursively */
4605 case 'C': if (VIsual_active)
4606 nv_operator(cap);
4607 else
4608 closeFoldRecurse(curwin->w_cursor.lnum);
4609 curwin->w_p_fen = TRUE;
4610 break;
4611
4612 /* "zv": open folds at the cursor */
4613 case 'v': foldOpenCursor();
4614 break;
4615
4616 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
4617 case 'x': curwin->w_p_fen = TRUE;
4618 newFoldLevel(); /* update right now */
4619 foldOpenCursor();
4620 break;
4621
4622 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
4623 case 'X': curwin->w_p_fen = TRUE;
4624 old_fdl = -1; /* force an update */
4625 break;
4626
4627 /* "zm": fold more */
4628 case 'm': if (curwin->w_p_fdl > 0)
4629 --curwin->w_p_fdl;
4630 old_fdl = -1; /* force an update */
4631 curwin->w_p_fen = TRUE;
4632 break;
4633
4634 /* "zM": close all folds */
4635 case 'M': curwin->w_p_fdl = 0;
4636 old_fdl = -1; /* force an update */
4637 curwin->w_p_fen = TRUE;
4638 break;
4639
4640 /* "zr": reduce folding */
4641 case 'r': ++curwin->w_p_fdl;
4642 break;
4643
4644 /* "zR": open all folds */
4645 case 'R': curwin->w_p_fdl = getDeepestNesting();
4646 old_fdl = -1; /* force an update */
4647 break;
4648
4649 case 'j': /* "zj" move to next fold downwards */
4650 case 'k': /* "zk" move to next fold upwards */
4651 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
4652 cap->count1) == FAIL)
4653 clearopbeep(cap->oap);
4654 break;
4655
4656#endif /* FEAT_FOLDING */
4657
4658 default: clearopbeep(cap->oap);
4659 }
4660
4661#ifdef FEAT_FOLDING
4662 /* Redraw when 'foldenable' changed */
4663 if (old_fen != curwin->w_p_fen)
4664 {
4665# ifdef FEAT_DIFF
4666 win_T *wp;
4667
4668 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
4669 {
4670 /* Adjust 'foldenable' in diff-synced windows. */
4671 FOR_ALL_WINDOWS(wp)
4672 {
4673 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
4674 {
4675 wp->w_p_fen = curwin->w_p_fen;
4676 changed_window_setting_win(wp);
4677 }
4678 }
4679 }
4680# endif
4681 changed_window_setting();
4682 }
4683
4684 /* Redraw when 'foldlevel' changed. */
4685 if (old_fdl != curwin->w_p_fdl)
4686 newFoldLevel();
4687#endif
4688}
4689
4690#ifdef FEAT_GUI
4691/*
4692 * Vertical scrollbar movement.
4693 */
4694 static void
4695nv_ver_scrollbar(cap)
4696 cmdarg_T *cap;
4697{
4698 if (cap->oap->op_type != OP_NOP)
4699 clearopbeep(cap->oap);
4700
4701 /* Even if an operator was pending, we still want to scroll */
4702 gui_do_scroll();
4703}
4704
4705/*
4706 * Horizontal scrollbar movement.
4707 */
4708 static void
4709nv_hor_scrollbar(cap)
4710 cmdarg_T *cap;
4711{
4712 if (cap->oap->op_type != OP_NOP)
4713 clearopbeep(cap->oap);
4714
4715 /* Even if an operator was pending, we still want to scroll */
4716 gui_do_horiz_scroll();
4717}
4718#endif
4719
4720/*
4721 * "Q" command.
4722 */
4723 static void
4724nv_exmode(cap)
4725 cmdarg_T *cap;
4726{
4727 /*
4728 * Ignore 'Q' in Visual mode, just give a beep.
4729 */
4730#ifdef FEAT_VISUAL
4731 if (VIsual_active)
4732 vim_beep();
4733 else
4734#endif
4735 if (!checkclearop(cap->oap))
4736 do_exmode(FALSE);
4737}
4738
4739/*
4740 * Handle a ":" command.
4741 */
4742 static void
4743nv_colon(cap)
4744 cmdarg_T *cap;
4745{
4746 int old_p_im;
4747
4748#ifdef FEAT_VISUAL
4749 if (VIsual_active)
4750 nv_operator(cap);
4751 else
4752#endif
4753 {
4754 if (cap->oap->op_type != OP_NOP)
4755 {
4756 /* Using ":" as a movement is characterwise exclusive. */
4757 cap->oap->motion_type = MCHAR;
4758 cap->oap->inclusive = FALSE;
4759 }
4760 else if (cap->count0)
4761 {
4762 /* translate "count:" into ":.,.+(count - 1)" */
4763 stuffcharReadbuff('.');
4764 if (cap->count0 > 1)
4765 {
4766 stuffReadbuff((char_u *)",.+");
4767 stuffnumReadbuff((long)cap->count0 - 1L);
4768 }
4769 }
4770
4771 /* When typing, don't type below an old message */
4772 if (KeyTyped)
4773 compute_cmdrow();
4774
4775 old_p_im = p_im;
4776
4777 /* get a command line and execute it */
4778 do_cmdline(NULL, getexline, NULL,
4779 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
4780
4781 /* If 'insertmode' changed, enter or exit Insert mode */
4782 if (p_im != old_p_im)
4783 {
4784 if (p_im)
4785 restart_edit = 'i';
4786 else
4787 restart_edit = 0;
4788 }
4789
4790 /* The start of the operator may have become invalid by the Ex
4791 * command. */
4792 if (cap->oap->op_type != OP_NOP
4793 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
4794 || cap->oap->start.col >
4795 STRLEN(ml_get(cap->oap->start.lnum))))
4796 clearopbeep(cap->oap);
4797 }
4798}
4799
4800/*
4801 * Handle CTRL-G command.
4802 */
4803 static void
4804nv_ctrlg(cap)
4805 cmdarg_T *cap;
4806{
4807#ifdef FEAT_VISUAL
4808 if (VIsual_active) /* toggle Selection/Visual mode */
4809 {
4810 VIsual_select = !VIsual_select;
4811 showmode();
4812 }
4813 else
4814#endif
4815 if (!checkclearop(cap->oap))
4816 /* print full name if count given or :cd used */
4817 fileinfo((int)cap->count0, FALSE, TRUE);
4818}
4819
4820/*
4821 * Handle CTRL-H <Backspace> command.
4822 */
4823 static void
4824nv_ctrlh(cap)
4825 cmdarg_T *cap;
4826{
4827#ifdef FEAT_VISUAL
4828 if (VIsual_active && VIsual_select)
4829 {
4830 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
4831 v_visop(cap);
4832 }
4833 else
4834#endif
4835 nv_left(cap);
4836}
4837
4838/*
4839 * CTRL-L: clear screen and redraw.
4840 */
4841 static void
4842nv_clear(cap)
4843 cmdarg_T *cap;
4844{
4845 if (!checkclearop(cap->oap))
4846 {
4847#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
4848 /*
4849 * Right now, the BeBox doesn't seem to have an easy way to detect
4850 * window resizing, so we cheat and make the user detect it
4851 * manually with CTRL-L instead
4852 */
4853 ui_get_shellsize();
4854#endif
4855#ifdef FEAT_SYN_HL
4856 /* Clear all syntax states to force resyncing. */
4857 syn_stack_free_all(curbuf);
4858#endif
4859 redraw_later(CLEAR);
4860 }
4861}
4862
4863/*
4864 * CTRL-O: In Select mode: switch to Visual mode for one command.
4865 * Otherwise: Go to older pcmark.
4866 */
4867 static void
4868nv_ctrlo(cap)
4869 cmdarg_T *cap;
4870{
4871#ifdef FEAT_VISUAL
4872 if (VIsual_active && VIsual_select)
4873 {
4874 VIsual_select = FALSE;
4875 showmode();
4876 restart_VIsual_select = 2; /* restart Select mode later */
4877 }
4878 else
4879#endif
4880 {
4881 cap->count1 = -cap->count1;
4882 nv_pcmark(cap);
4883 }
4884}
4885
4886/*
4887 * CTRL-^ command, short for ":e #"
4888 */
4889 static void
4890nv_hat(cap)
4891 cmdarg_T *cap;
4892{
4893 if (!checkclearopq(cap->oap))
4894 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
4895 GETF_SETMARK|GETF_ALT, FALSE);
4896}
4897
4898/*
4899 * "Z" commands.
4900 */
4901 static void
4902nv_Zet(cap)
4903 cmdarg_T *cap;
4904{
4905 if (!checkclearopq(cap->oap))
4906 {
4907 switch (cap->nchar)
4908 {
4909 /* "ZZ": equivalent to ":x". */
4910 case 'Z': do_cmdline_cmd((char_u *)"x");
4911 break;
4912
4913 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
4914 case 'Q': do_cmdline_cmd((char_u *)"q!");
4915 break;
4916
4917 default: clearopbeep(cap->oap);
4918 }
4919 }
4920}
4921
4922#if defined(FEAT_WINDOWS) || defined(PROTO)
4923/*
4924 * Call nv_ident() as if "c1" was used, with "c2" as next character.
4925 */
4926 void
4927do_nv_ident(c1, c2)
4928 int c1;
4929 int c2;
4930{
4931 oparg_T oa;
4932 cmdarg_T ca;
4933
4934 clear_oparg(&oa);
4935 vim_memset(&ca, 0, sizeof(ca));
4936 ca.oap = &oa;
4937 ca.cmdchar = c1;
4938 ca.nchar = c2;
4939 nv_ident(&ca);
4940}
4941#endif
4942
4943/*
4944 * Handle the commands that use the word under the cursor.
4945 * [g] CTRL-] :ta to current identifier
4946 * [g] 'K' run program for current identifier
4947 * [g] '*' / to current identifier or string
4948 * [g] '#' ? to current identifier or string
4949 * g ']' :tselect for current identifier
4950 */
4951 static void
4952nv_ident(cap)
4953 cmdarg_T *cap;
4954{
4955 char_u *ptr = NULL;
4956 char_u *buf;
4957 char_u *p;
4958 char_u *kp; /* value of 'keywordprg' */
4959 int kp_help; /* 'keywordprg' is ":help" */
4960 int n = 0; /* init for GCC */
4961 int cmdchar;
4962 int g_cmd; /* "g" command */
4963 char_u *aux_ptr;
4964 int isman;
4965 int isman_s;
4966
4967 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
4968 {
4969 cmdchar = cap->nchar;
4970 g_cmd = TRUE;
4971 }
4972 else
4973 {
4974 cmdchar = cap->cmdchar;
4975 g_cmd = FALSE;
4976 }
4977
4978 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
4979 cmdchar = '#';
4980
4981 /*
4982 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
4983 */
4984 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
4985 {
4986#ifdef FEAT_VISUAL
4987 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
4988 return;
4989#endif
4990 if (checkclearopq(cap->oap))
4991 return;
4992 }
4993
4994 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
4995 (cmdchar == '*' || cmdchar == '#')
4996 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
4997 {
4998 clearop(cap->oap);
4999 return;
5000 }
5001
5002 /* Allocate buffer to put the command in. Inserting backslashes can
5003 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5004 * and some numbers. */
5005 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5006 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5007 || STRCMP(kp, ":help") == 0);
5008 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5009 if (buf == NULL)
5010 return;
5011 buf[0] = NUL;
5012
5013 switch (cmdchar)
5014 {
5015 case '*':
5016 case '#':
5017 /*
5018 * Put cursor at start of word, makes search skip the word
5019 * under the cursor.
5020 * Call setpcmark() first, so "*``" puts the cursor back where
5021 * it was.
5022 */
5023 setpcmark();
5024 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5025
5026 if (!g_cmd && vim_iswordp(ptr))
5027 STRCPY(buf, "\\<");
5028 no_smartcase = TRUE; /* don't use 'smartcase' now */
5029 break;
5030
5031 case 'K':
5032 if (kp_help)
5033 STRCPY(buf, "he! ");
5034 else
5035 {
5036 /* When a count is given, turn it into a range. Is this
5037 * really what we want? */
5038 isman = (STRCMP(kp, "man") == 0);
5039 isman_s = (STRCMP(kp, "man -s") == 0);
5040 if (cap->count0 != 0 && !(isman || isman_s))
5041 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5042
5043 STRCAT(buf, "! ");
5044 if (cap->count0 == 0 && isman_s)
5045 STRCAT(buf, "man");
5046 else
5047 STRCAT(buf, kp);
5048 STRCAT(buf, " ");
5049 if (cap->count0 != 0 && (isman || isman_s))
5050 {
5051 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5052 STRCAT(buf, " ");
5053 }
5054 }
5055 break;
5056
5057 case ']':
5058#ifdef FEAT_CSCOPE
5059 if (p_cst)
5060 STRCPY(buf, "cstag ");
5061 else
5062#endif
5063 STRCPY(buf, "ts ");
5064 break;
5065
5066 default:
5067 if (curbuf->b_help)
5068 STRCPY(buf, "he! ");
5069 else if (g_cmd)
5070 STRCPY(buf, "tj ");
5071 else
5072 STRCPY(buf, "ta ");
5073 }
5074
5075 /*
5076 * Now grab the chars in the identifier
5077 */
5078 if (cmdchar == '*')
5079 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5080 else if (cmdchar == '#')
5081 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
5082 else if (cmdchar == 'K' && !kp_help)
5083 aux_ptr = (char_u *)" \t\\\"|!";
5084 else
5085 /* Don't escape spaces and Tabs in a tag with a backslash */
5086 aux_ptr = (char_u *)"\\|\"";
5087
5088 p = buf + STRLEN(buf);
5089 while (n-- > 0)
5090 {
5091 /* put a backslash before \ and some others */
5092 if (vim_strchr(aux_ptr, *ptr) != NULL)
5093 *p++ = '\\';
5094#ifdef FEAT_MBYTE
5095 /* When current byte is a part of multibyte character, copy all bytes
5096 * of that character. */
5097 if (has_mbyte)
5098 {
5099 int i;
5100 int len = (*mb_ptr2len_check)(ptr) - 1;
5101
5102 for (i = 0; i < len && n >= 1; ++i, --n)
5103 *p++ = *ptr++;
5104 }
5105#endif
5106 *p++ = *ptr++;
5107 }
5108 *p = NUL;
5109
5110 /*
5111 * Execute the command.
5112 */
5113 if (cmdchar == '*' || cmdchar == '#')
5114 {
5115 if (!g_cmd && (
5116#ifdef FEAT_MBYTE
5117 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5118#endif
5119 vim_iswordc(ptr[-1])))
5120 STRCAT(buf, "\\>");
5121#ifdef FEAT_CMDHIST
5122 /* put pattern in search history */
5123 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5124#endif
5125 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5126 }
5127 else
5128 do_cmdline_cmd(buf);
5129
5130 vim_free(buf);
5131}
5132
5133#ifdef FEAT_VISUAL
5134/*
5135 * Get visually selected text, within one line only.
5136 * Returns FAIL if more than one line selected.
5137 */
5138 static int
5139get_visual_text(cap, pp, lenp)
5140 cmdarg_T *cap;
5141 char_u **pp; /* return: start of selected text */
5142 int *lenp; /* return: length of selected text */
5143{
5144 if (VIsual_mode != 'V')
5145 unadjust_for_sel();
5146 if (VIsual.lnum != curwin->w_cursor.lnum)
5147 {
5148 clearopbeep(cap->oap);
5149 return FAIL;
5150 }
5151 if (VIsual_mode == 'V')
5152 {
5153 *pp = ml_get_curline();
5154 *lenp = (int)STRLEN(*pp);
5155 }
5156 else
5157 {
5158 if (lt(curwin->w_cursor, VIsual))
5159 {
5160 *pp = ml_get_pos(&curwin->w_cursor);
5161 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5162 }
5163 else
5164 {
5165 *pp = ml_get_pos(&VIsual);
5166 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5167 }
5168#ifdef FEAT_MBYTE
5169 if (has_mbyte)
5170 /* Correct the length to include the whole last character. */
5171 *lenp += (*mb_ptr2len_check)(*pp + (*lenp - 1)) - 1;
5172#endif
5173 }
5174 reset_VIsual_and_resel();
5175 return OK;
5176}
5177#endif
5178
5179/*
5180 * CTRL-T: backwards in tag stack
5181 */
5182 static void
5183nv_tagpop(cap)
5184 cmdarg_T *cap;
5185{
5186 if (!checkclearopq(cap->oap))
5187 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5188}
5189
5190/*
5191 * Handle scrolling command 'H', 'L' and 'M'.
5192 */
5193 static void
5194nv_scroll(cap)
5195 cmdarg_T *cap;
5196{
5197 int used = 0;
5198 long n;
5199#ifdef FEAT_FOLDING
5200 linenr_T lnum;
5201#endif
5202 int half;
5203
5204 cap->oap->motion_type = MLINE;
5205 setpcmark();
5206
5207 if (cap->cmdchar == 'L')
5208 {
5209 validate_botline(); /* make sure curwin->w_botline is valid */
5210 curwin->w_cursor.lnum = curwin->w_botline - 1;
5211 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5212 curwin->w_cursor.lnum = 1;
5213 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005214 {
5215#ifdef FEAT_FOLDING
5216 if (hasAnyFolding(curwin))
5217 {
5218 /* Count a fold for one screen line. */
5219 for (n = cap->count1 - 1; n > 0
5220 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5221 {
5222 (void)hasFolding(curwin->w_cursor.lnum,
5223 &curwin->w_cursor.lnum, NULL);
5224 --curwin->w_cursor.lnum;
5225 }
5226 }
5227 else
5228#endif
5229 curwin->w_cursor.lnum -= cap->count1 - 1;
5230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 }
5232 else
5233 {
5234 if (cap->cmdchar == 'M')
5235 {
5236#ifdef FEAT_DIFF
5237 /* Don't count filler lines above the window. */
5238 used -= diff_check_fill(curwin, curwin->w_topline)
5239 - curwin->w_topfill;
5240#endif
5241 validate_botline(); /* make sure w_empty_rows is valid */
5242 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5243 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5244 {
5245#ifdef FEAT_DIFF
5246 /* Count half he number of filler lines to be "below this
5247 * line" and half to be "above the next line". */
5248 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5249 + n) / 2 >= half)
5250 {
5251 --n;
5252 break;
5253 }
5254#endif
5255 used += plines(curwin->w_topline + n);
5256 if (used >= half)
5257 break;
5258#ifdef FEAT_FOLDING
5259 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5260 n = lnum - curwin->w_topline;
5261#endif
5262 }
5263 if (n > 0 && used > curwin->w_height)
5264 --n;
5265 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005266 else /* (cap->cmdchar == 'H') */
5267 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005269#ifdef FEAT_FOLDING
5270 if (hasAnyFolding(curwin))
5271 {
5272 /* Count a fold for one screen line. */
5273 lnum = curwin->w_topline;
5274 while (n-- > 0 && lnum < curwin->w_botline - 1)
5275 {
5276 hasFolding(lnum, NULL, &lnum);
5277 ++lnum;
5278 }
5279 n = lnum - curwin->w_topline;
5280 }
5281#endif
5282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 curwin->w_cursor.lnum = curwin->w_topline + n;
5284 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5285 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5286 }
5287
5288 cursor_correct(); /* correct for 'so' */
5289 beginline(BL_SOL | BL_FIX);
5290}
5291
5292/*
5293 * Cursor right commands.
5294 */
5295 static void
5296nv_right(cap)
5297 cmdarg_T *cap;
5298{
5299 long n;
5300#ifdef FEAT_VISUAL
5301 int PAST_LINE;
5302#else
5303# define PAST_LINE 0
5304#endif
5305
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005306 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5307 {
5308 /* <C-Right> and <S-Right> move a word or WORD right */
5309 if (mod_mask & MOD_MASK_CTRL)
5310 cap->arg = TRUE;
5311 nv_wordcmd(cap);
5312 return;
5313 }
5314
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 cap->oap->motion_type = MCHAR;
5316 cap->oap->inclusive = FALSE;
5317#ifdef FEAT_VISUAL
5318 PAST_LINE = (VIsual_active && *p_sel != 'o');
5319
5320# ifdef FEAT_VIRTUALEDIT
5321 /*
5322 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
5323 * (theoretically) infinitly long.
5324 */
5325 if (virtual_active())
5326 PAST_LINE = 0;
5327# endif
5328#endif
5329
5330 for (n = cap->count1; n > 0; --n)
5331 {
5332 if ((!PAST_LINE && oneright() == FAIL)
5333 || (PAST_LINE && *ml_get_cursor() == NUL))
5334 {
5335 /*
5336 * <Space> wraps to next line if 'whichwrap' bit 1 set.
5337 * 'l' wraps to next line if 'whichwrap' bit 2 set.
5338 * CURS_RIGHT wraps to next line if 'whichwrap' bit 3 set
5339 */
5340 if ( ((cap->cmdchar == ' '
5341 && vim_strchr(p_ww, 's') != NULL)
5342 || (cap->cmdchar == 'l'
5343 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005344 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 && vim_strchr(p_ww, '>') != NULL))
5346 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5347 {
5348 /* When deleting we also count the NL as a character.
5349 * Set cap->oap->inclusive when last char in the line is
5350 * included, move to next line after that */
5351 if ( (cap->oap->op_type == OP_DELETE
5352 || cap->oap->op_type == OP_CHANGE)
5353 && !cap->oap->inclusive
5354 && !lineempty(curwin->w_cursor.lnum))
5355 cap->oap->inclusive = TRUE;
5356 else
5357 {
5358 ++curwin->w_cursor.lnum;
5359 curwin->w_cursor.col = 0;
5360#ifdef FEAT_VIRTUALEDIT
5361 curwin->w_cursor.coladd = 0;
5362#endif
5363 curwin->w_set_curswant = TRUE;
5364 cap->oap->inclusive = FALSE;
5365 }
5366 continue;
5367 }
5368 if (cap->oap->op_type == OP_NOP)
5369 {
5370 /* Only beep and flush if not moved at all */
5371 if (n == cap->count1)
5372 beep_flush();
5373 }
5374 else
5375 {
5376 if (!lineempty(curwin->w_cursor.lnum))
5377 cap->oap->inclusive = TRUE;
5378 }
5379 break;
5380 }
5381#ifdef FEAT_VISUAL
5382 else if (PAST_LINE)
5383 {
5384 curwin->w_set_curswant = TRUE;
5385# ifdef FEAT_VIRTUALEDIT
5386 if (virtual_active())
5387 oneright();
5388 else
5389# endif
5390 {
5391# ifdef FEAT_MBYTE
5392 if (has_mbyte)
5393 curwin->w_cursor.col +=
5394 (*mb_ptr2len_check)(ml_get_cursor());
5395 else
5396# endif
5397 ++curwin->w_cursor.col;
5398 }
5399 }
5400#endif
5401 }
5402#ifdef FEAT_FOLDING
5403 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5404 && cap->oap->op_type == OP_NOP)
5405 foldOpenCursor();
5406#endif
5407}
5408
5409/*
5410 * Cursor left commands.
5411 *
5412 * Returns TRUE when operator end should not be adjusted.
5413 */
5414 static void
5415nv_left(cap)
5416 cmdarg_T *cap;
5417{
5418 long n;
5419
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005420 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5421 {
5422 /* <C-Left> and <S-Left> move a word or WORD left */
5423 if (mod_mask & MOD_MASK_CTRL)
5424 cap->arg = 1;
5425 nv_bck_word(cap);
5426 return;
5427 }
5428
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 cap->oap->motion_type = MCHAR;
5430 cap->oap->inclusive = FALSE;
5431 for (n = cap->count1; n > 0; --n)
5432 {
5433 if (oneleft() == FAIL)
5434 {
5435 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
5436 * 'h' wraps to previous line if 'whichwrap' has 'h'.
5437 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
5438 */
5439 if ( (((cap->cmdchar == K_BS
5440 || cap->cmdchar == Ctrl_H)
5441 && vim_strchr(p_ww, 'b') != NULL)
5442 || (cap->cmdchar == 'h'
5443 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005444 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 && vim_strchr(p_ww, '<') != NULL))
5446 && curwin->w_cursor.lnum > 1)
5447 {
5448 --(curwin->w_cursor.lnum);
5449 coladvance((colnr_T)MAXCOL);
5450 curwin->w_set_curswant = TRUE;
5451
5452 /* When the NL before the first char has to be deleted we
5453 * put the cursor on the NUL after the previous line.
5454 * This is a very special case, be careful!
5455 * don't adjust op_end now, otherwise it won't work */
5456 if ( (cap->oap->op_type == OP_DELETE
5457 || cap->oap->op_type == OP_CHANGE)
5458 && !lineempty(curwin->w_cursor.lnum))
5459 {
5460 ++curwin->w_cursor.col;
5461 cap->retval |= CA_NO_ADJ_OP_END;
5462 }
5463 continue;
5464 }
5465 /* Only beep and flush if not moved at all */
5466 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
5467 beep_flush();
5468 break;
5469 }
5470 }
5471#ifdef FEAT_FOLDING
5472 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5473 && cap->oap->op_type == OP_NOP)
5474 foldOpenCursor();
5475#endif
5476}
5477
5478/*
5479 * Cursor up commands.
5480 * cap->arg is TRUE for "-": Move cursor to first non-blank.
5481 */
5482 static void
5483nv_up(cap)
5484 cmdarg_T *cap;
5485{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005486 if (mod_mask & MOD_MASK_SHIFT)
5487 {
5488 /* <S-Up> is page up */
5489 cap->arg = BACKWARD;
5490 nv_page(cap);
5491 }
5492 else
5493 {
5494 cap->oap->motion_type = MLINE;
5495 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5496 clearopbeep(cap->oap);
5497 else if (cap->arg)
5498 beginline(BL_WHITE | BL_FIX);
5499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500}
5501
5502/*
5503 * Cursor down commands.
5504 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
5505 */
5506 static void
5507nv_down(cap)
5508 cmdarg_T *cap;
5509{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005510 if (mod_mask & MOD_MASK_SHIFT)
5511 {
5512 /* <S-Down> is page down */
5513 cap->arg = FORWARD;
5514 nv_page(cap);
5515 }
5516 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5518 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005519 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 do_cmdline_cmd((char_u *)".cc");
5521 else
5522#endif
5523 {
5524#ifdef FEAT_CMDWIN
5525 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005526 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 cmdwin_result = CAR;
5528 else
5529#endif
5530 {
5531 cap->oap->motion_type = MLINE;
5532 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5533 clearopbeep(cap->oap);
5534 else if (cap->arg)
5535 beginline(BL_WHITE | BL_FIX);
5536 }
5537 }
5538}
5539
5540#ifdef FEAT_SEARCHPATH
5541/*
5542 * Grab the file name under the cursor and edit it.
5543 */
5544 static void
5545nv_gotofile(cap)
5546 cmdarg_T *cap;
5547{
5548 char_u *ptr;
5549
5550#ifdef FEAT_CMDWIN
5551 if (cmdwin_type != 0)
5552 {
5553 clearopbeep(cap->oap);
5554 return;
5555 }
5556#endif
5557
5558# ifdef FEAT_VISUAL
5559 /*
5560 * In Visual mode, use the selected text as a file name.
5561 * Don't allow selection across multiple lines.
5562 */
5563 if (VIsual_active)
5564 {
5565 int len;
5566
5567 if (get_visual_text(cap, &ptr, &len) == FAIL)
5568 return;
5569 ptr = find_file_name_in_path(ptr, len,
5570 FNAME_MESS|FNAME_EXP|FNAME_REL, cap->count1, curbuf->b_ffname);
5571 }
5572 else
5573# endif
5574 ptr = file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL,
5575 cap->count1);
5576
5577 if (ptr != NULL)
5578 {
5579 /* do autowrite if necessary */
5580 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
5581 autowrite(curbuf, FALSE);
5582 setpcmark();
5583 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
5584 P_HID(curbuf) ? ECMD_HIDE : 0);
5585 vim_free(ptr);
5586 }
5587 else
5588 clearop(cap->oap);
5589}
5590#endif
5591
5592/*
5593 * <End> command: to end of current line or last line.
5594 */
5595 static void
5596nv_end(cap)
5597 cmdarg_T *cap;
5598{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005599 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005601 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 nv_goto(cap);
5603 cap->count1 = 1; /* to end of current line */
5604 }
5605 nv_dollar(cap);
5606}
5607
5608/*
5609 * Handle the "$" command.
5610 */
5611 static void
5612nv_dollar(cap)
5613 cmdarg_T *cap;
5614{
5615 cap->oap->motion_type = MCHAR;
5616 cap->oap->inclusive = TRUE;
5617#ifdef FEAT_VIRTUALEDIT
5618 /* In virtual mode when off the edge of a line and an operator
5619 * is pending (whew!) keep the cursor where it is.
5620 * Otherwise, send it to the end of the line. */
5621 if (!virtual_active() || gchar_cursor() != NUL
5622 || cap->oap->op_type == OP_NOP)
5623#endif
5624 curwin->w_curswant = MAXCOL; /* so we stay at the end */
5625 if (cursor_down((long)(cap->count1 - 1),
5626 cap->oap->op_type == OP_NOP) == FAIL)
5627 clearopbeep(cap->oap);
5628#ifdef FEAT_FOLDING
5629 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
5630 foldOpenCursor();
5631#endif
5632}
5633
5634/*
5635 * Implementation of '?' and '/' commands.
5636 * If cap->arg is TRUE don't set PC mark.
5637 */
5638 static void
5639nv_search(cap)
5640 cmdarg_T *cap;
5641{
5642 oparg_T *oap = cap->oap;
5643
5644 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
5645 {
5646 /* Translate "g??" to "g?g?" */
5647 cap->cmdchar = 'g';
5648 cap->nchar = '?';
5649 nv_operator(cap);
5650 return;
5651 }
5652
5653 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
5654
5655 if (cap->searchbuf == NULL)
5656 {
5657 clearop(oap);
5658 return;
5659 }
5660
5661 normal_search(cap, cap->cmdchar, cap->searchbuf,
5662 (cap->arg ? 0 : SEARCH_MARK));
5663}
5664
5665/*
5666 * Handle "N" and "n" commands.
5667 * cap->arg is SEARCH_REV for "N", 0 for "n".
5668 */
5669 static void
5670nv_next(cap)
5671 cmdarg_T *cap;
5672{
5673 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
5674}
5675
5676/*
5677 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
5678 * Uses only cap->count1 and cap->oap from "cap".
5679 */
5680 static void
5681normal_search(cap, dir, pat, opt)
5682 cmdarg_T *cap;
5683 int dir;
5684 char_u *pat;
5685 int opt; /* extra flags for do_search() */
5686{
5687 int i;
5688
5689 cap->oap->motion_type = MCHAR;
5690 cap->oap->inclusive = FALSE;
5691 cap->oap->use_reg_one = TRUE;
5692 curwin->w_set_curswant = TRUE;
5693
5694 i = do_search(cap->oap, dir, pat, cap->count1,
5695 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG);
5696 if (i == 0)
5697 clearop(cap->oap);
5698 else
5699 {
5700 if (i == 2)
5701 cap->oap->motion_type = MLINE;
5702#ifdef FEAT_VIRTUALEDIT
5703 curwin->w_cursor.coladd = 0;
5704#endif
5705#ifdef FEAT_FOLDING
5706 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
5707 foldOpenCursor();
5708#endif
5709 }
5710
5711 /* "/$" will put the cursor after the end of the line, may need to
5712 * correct that here */
5713 check_cursor();
5714}
5715
5716/*
5717 * Character search commands.
5718 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
5719 * ',' and FALSE for ';'.
5720 * cap->nchar is NUL for ',' and ';' (repeat the search)
5721 */
5722 static void
5723nv_csearch(cap)
5724 cmdarg_T *cap;
5725{
5726 int t_cmd;
5727
5728 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
5729 t_cmd = TRUE;
5730 else
5731 t_cmd = FALSE;
5732
5733 cap->oap->motion_type = MCHAR;
5734 if (cap->arg == BACKWARD)
5735 cap->oap->inclusive = FALSE;
5736 else
5737 cap->oap->inclusive = TRUE;
5738 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
5739 clearopbeep(cap->oap);
5740 else
5741 {
5742 curwin->w_set_curswant = TRUE;
5743#ifdef FEAT_VIRTUALEDIT
5744 /* Include a Tab for "tx" and for "dfx". */
5745 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
5746 && (t_cmd || cap->oap->op_type != OP_NOP))
5747 {
5748 colnr_T scol, ecol;
5749
5750 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
5751 curwin->w_cursor.coladd = ecol - scol;
5752 }
5753 else
5754 curwin->w_cursor.coladd = 0;
5755#endif
5756#ifdef FEAT_VISUAL
5757 adjust_for_sel(cap);
5758#endif
5759#ifdef FEAT_FOLDING
5760 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
5761 foldOpenCursor();
5762#endif
5763 }
5764}
5765
5766/*
5767 * "[" and "]" commands.
5768 * cap->arg is BACKWARD for "[" and FORWARD for "]".
5769 */
5770 static void
5771nv_brackets(cap)
5772 cmdarg_T *cap;
5773{
5774 pos_T new_pos;
5775 pos_T prev_pos;
5776 pos_T *pos = NULL; /* init for GCC */
5777 pos_T old_pos; /* cursor position before command */
5778 int flag;
5779 long n;
5780 int findc;
5781 int c;
5782
5783 cap->oap->motion_type = MCHAR;
5784 cap->oap->inclusive = FALSE;
5785 old_pos = curwin->w_cursor;
5786#ifdef FEAT_VIRTUALEDIT
5787 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
5788#endif
5789
5790#ifdef FEAT_SEARCHPATH
5791 /*
5792 * "[f" or "]f" : Edit file under the cursor (same as "gf")
5793 */
5794 if (cap->nchar == 'f')
5795 nv_gotofile(cap);
5796 else
5797#endif
5798
5799#ifdef FEAT_FIND_ID
5800 /*
5801 * Find the occurence(s) of the identifier or define under cursor
5802 * in current and included files or jump to the first occurence.
5803 *
5804 * search list jump
5805 * fwd bwd fwd bwd fwd bwd
5806 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
5807 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
5808 */
5809 if (vim_strchr((char_u *)
5810#ifdef EBCDIC
5811 "iI\005dD\067",
5812#else
5813 "iI\011dD\004",
5814#endif
5815 cap->nchar) != NULL)
5816 {
5817 char_u *ptr;
5818 int len;
5819
5820 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
5821 clearop(cap->oap);
5822 else
5823 {
5824 find_pattern_in_path(ptr, 0, len, TRUE,
5825 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
5826 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
5827 cap->count1,
5828 isupper(cap->nchar) ? ACTION_SHOW_ALL :
5829 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
5830 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
5831 (linenr_T)MAXLNUM);
5832 curwin->w_set_curswant = TRUE;
5833 }
5834 }
5835 else
5836#endif
5837
5838 /*
5839 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
5840 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
5841 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
5842 * "[m" or "]m" search for prev/next start of (Java) method.
5843 * "[M" or "]M" search for prev/next end of (Java) method.
5844 */
5845 if ( (cap->cmdchar == '['
5846 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
5847 || (cap->cmdchar == ']'
5848 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
5849 {
5850 if (cap->nchar == '*')
5851 cap->nchar = '/';
5852 new_pos.lnum = 0;
5853 prev_pos.lnum = 0;
5854 if (cap->nchar == 'm' || cap->nchar == 'M')
5855 {
5856 if (cap->cmdchar == '[')
5857 findc = '{';
5858 else
5859 findc = '}';
5860 n = 9999;
5861 }
5862 else
5863 {
5864 findc = cap->nchar;
5865 n = cap->count1;
5866 }
5867 for ( ; n > 0; --n)
5868 {
5869 if ((pos = findmatchlimit(cap->oap, findc,
5870 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
5871 {
5872 if (new_pos.lnum == 0) /* nothing found */
5873 {
5874 if (cap->nchar != 'm' && cap->nchar != 'M')
5875 clearopbeep(cap->oap);
5876 }
5877 else
5878 pos = &new_pos; /* use last one found */
5879 break;
5880 }
5881 prev_pos = new_pos;
5882 curwin->w_cursor = *pos;
5883 new_pos = *pos;
5884 }
5885 curwin->w_cursor = old_pos;
5886
5887 /*
5888 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
5889 * brought us to the match for "[m" and "]M" when inside a method.
5890 * Try finding the '{' or '}' we want to be at.
5891 * Also repeat for the given count.
5892 */
5893 if (cap->nchar == 'm' || cap->nchar == 'M')
5894 {
5895 /* norm is TRUE for "]M" and "[m" */
5896 int norm = ((findc == '{') == (cap->nchar == 'm'));
5897
5898 n = cap->count1;
5899 /* found a match: we were inside a method */
5900 if (prev_pos.lnum != 0)
5901 {
5902 pos = &prev_pos;
5903 curwin->w_cursor = prev_pos;
5904 if (norm)
5905 --n;
5906 }
5907 else
5908 pos = NULL;
5909 while (n > 0)
5910 {
5911 for (;;)
5912 {
5913 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
5914 {
5915 /* if not found anything, that's an error */
5916 if (pos == NULL)
5917 clearopbeep(cap->oap);
5918 n = 0;
5919 break;
5920 }
5921 c = gchar_cursor();
5922 if (c == '{' || c == '}')
5923 {
5924 /* Must have found end/start of class: use it.
5925 * Or found the place to be at. */
5926 if ((c == findc && norm) || (n == 1 && !norm))
5927 {
5928 new_pos = curwin->w_cursor;
5929 pos = &new_pos;
5930 n = 0;
5931 }
5932 /* if no match found at all, we started outside of the
5933 * class and we're inside now. Just go on. */
5934 else if (new_pos.lnum == 0)
5935 {
5936 new_pos = curwin->w_cursor;
5937 pos = &new_pos;
5938 }
5939 /* found start/end of other method: go to match */
5940 else if ((pos = findmatchlimit(cap->oap, findc,
5941 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
5942 0)) == NULL)
5943 n = 0;
5944 else
5945 curwin->w_cursor = *pos;
5946 break;
5947 }
5948 }
5949 --n;
5950 }
5951 curwin->w_cursor = old_pos;
5952 if (pos == NULL && new_pos.lnum != 0)
5953 clearopbeep(cap->oap);
5954 }
5955 if (pos != NULL)
5956 {
5957 setpcmark();
5958 curwin->w_cursor = *pos;
5959 curwin->w_set_curswant = TRUE;
5960#ifdef FEAT_FOLDING
5961 if ((fdo_flags & FDO_BLOCK) && KeyTyped
5962 && cap->oap->op_type == OP_NOP)
5963 foldOpenCursor();
5964#endif
5965 }
5966 }
5967
5968 /*
5969 * "[[", "[]", "]]" and "][": move to start or end of function
5970 */
5971 else if (cap->nchar == '[' || cap->nchar == ']')
5972 {
5973 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
5974 flag = '{';
5975 else
5976 flag = '}'; /* "][" or "[]" */
5977
5978 curwin->w_set_curswant = TRUE;
5979 /*
5980 * Imitate strange Vi behaviour: When using "]]" with an operator
5981 * we also stop at '}'.
5982 */
5983 if (!findpar(cap->oap, cap->arg, cap->count1, flag,
5984 (cap->oap->op_type != OP_NOP
5985 && cap->arg == FORWARD && flag == '{')))
5986 clearopbeep(cap->oap);
5987 else
5988 {
5989 if (cap->oap->op_type == OP_NOP)
5990 beginline(BL_WHITE | BL_FIX);
5991#ifdef FEAT_FOLDING
5992 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
5993 foldOpenCursor();
5994#endif
5995 }
5996 }
5997
5998 /*
5999 * "[p", "[P", "]P" and "]p": put with indent adjustment
6000 */
6001 else if (cap->nchar == 'p' || cap->nchar == 'P')
6002 {
6003 if (!checkclearopq(cap->oap))
6004 {
6005 prep_redo_cmd(cap);
6006 do_put(cap->oap->regname,
6007 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6008 cap->count1, PUT_FIXINDENT);
6009 }
6010 }
6011
6012 /*
6013 * "['", "[`", "]'" and "]`": jump to next mark
6014 */
6015 else if (cap->nchar == '\'' || cap->nchar == '`')
6016 {
6017 pos = &curwin->w_cursor;
6018 for (n = cap->count1; n > 0; --n)
6019 {
6020 prev_pos = *pos;
6021 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6022 cap->nchar == '\'');
6023 if (pos == NULL)
6024 break;
6025 }
6026 if (pos == NULL)
6027 pos = &prev_pos;
6028 nv_cursormark(cap, cap->nchar == '\'', pos);
6029 }
6030
6031#ifdef FEAT_MOUSE
6032 /*
6033 * [ or ] followed by a middle mouse click: put selected text with
6034 * indent adjustment. Any other button just does as usual.
6035 */
6036 else if (cap->nchar >= K_LEFTMOUSE && cap->nchar <= K_RIGHTRELEASE)
6037 {
6038 (void)do_mouse(cap->oap, cap->nchar,
6039 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6040 cap->count1, PUT_FIXINDENT);
6041 }
6042#endif /* FEAT_MOUSE */
6043
6044#ifdef FEAT_FOLDING
6045 /*
6046 * "[z" and "]z": move to start or end of open fold.
6047 */
6048 else if (cap->nchar == 'z')
6049 {
6050 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6051 cap->count1) == FAIL)
6052 clearopbeep(cap->oap);
6053 }
6054#endif
6055
6056#ifdef FEAT_DIFF
6057 /*
6058 * "[c" and "]c": move to next or previous diff-change.
6059 */
6060 else if (cap->nchar == 'c')
6061 {
6062 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6063 cap->count1) == FAIL)
6064 clearopbeep(cap->oap);
6065 }
6066#endif
6067
6068 /* Not a valid cap->nchar. */
6069 else
6070 clearopbeep(cap->oap);
6071}
6072
6073/*
6074 * Handle Normal mode "%" command.
6075 */
6076 static void
6077nv_percent(cap)
6078 cmdarg_T *cap;
6079{
6080 pos_T *pos;
6081#ifdef FEAT_FOLDING
6082 linenr_T lnum = curwin->w_cursor.lnum;
6083#endif
6084
6085 cap->oap->inclusive = TRUE;
6086 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6087 {
6088 if (cap->count0 > 100)
6089 clearopbeep(cap->oap);
6090 else
6091 {
6092 cap->oap->motion_type = MLINE;
6093 setpcmark();
6094 /* Round up, so CTRL-G will give same value. Watch out for a
6095 * large line count, the line number must not go negative! */
6096 if (curbuf->b_ml.ml_line_count > 1000000)
6097 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6098 / 100L * cap->count0;
6099 else
6100 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6101 cap->count0 + 99L) / 100L;
6102 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6103 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6104 beginline(BL_SOL | BL_FIX);
6105 }
6106 }
6107 else /* "%" : go to matching paren */
6108 {
6109 cap->oap->motion_type = MCHAR;
6110 cap->oap->use_reg_one = TRUE;
6111 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6112 clearopbeep(cap->oap);
6113 else
6114 {
6115 setpcmark();
6116 curwin->w_cursor = *pos;
6117 curwin->w_set_curswant = TRUE;
6118#ifdef FEAT_VIRTUALEDIT
6119 curwin->w_cursor.coladd = 0;
6120#endif
6121#ifdef FEAT_VISUAL
6122 adjust_for_sel(cap);
6123#endif
6124 }
6125 }
6126#ifdef FEAT_FOLDING
6127 if (cap->oap->op_type == OP_NOP
6128 && lnum != curwin->w_cursor.lnum
6129 && (fdo_flags & FDO_PERCENT)
6130 && KeyTyped)
6131 foldOpenCursor();
6132#endif
6133}
6134
6135/*
6136 * Handle "(" and ")" commands.
6137 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6138 */
6139 static void
6140nv_brace(cap)
6141 cmdarg_T *cap;
6142{
6143 cap->oap->motion_type = MCHAR;
6144 cap->oap->use_reg_one = TRUE;
6145 if (cap->cmdchar == ')')
6146 cap->oap->inclusive = FALSE;
6147 else
6148 cap->oap->inclusive = TRUE;
6149 curwin->w_set_curswant = TRUE;
6150
6151 if (findsent(cap->arg, cap->count1) == FAIL)
6152 clearopbeep(cap->oap);
6153 else
6154 {
6155#ifdef FEAT_VIRTUALEDIT
6156 curwin->w_cursor.coladd = 0;
6157#endif
6158#ifdef FEAT_FOLDING
6159 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6160 foldOpenCursor();
6161#endif
6162 }
6163}
6164
6165/*
6166 * "m" command: Mark a position.
6167 */
6168 static void
6169nv_mark(cap)
6170 cmdarg_T *cap;
6171{
6172 if (!checkclearop(cap->oap))
6173 {
6174 if (setmark(cap->nchar) == FAIL)
6175 clearopbeep(cap->oap);
6176 }
6177}
6178
6179/*
6180 * "{" and "}" commands.
6181 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6182 */
6183 static void
6184nv_findpar(cap)
6185 cmdarg_T *cap;
6186{
6187 cap->oap->motion_type = MCHAR;
6188 cap->oap->inclusive = FALSE;
6189 cap->oap->use_reg_one = TRUE;
6190 curwin->w_set_curswant = TRUE;
6191 if (!findpar(cap->oap, cap->arg, cap->count1, NUL, FALSE))
6192 clearopbeep(cap->oap);
6193 else
6194 {
6195#ifdef FEAT_VIRTUALEDIT
6196 curwin->w_cursor.coladd = 0;
6197#endif
6198#ifdef FEAT_FOLDING
6199 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6200 foldOpenCursor();
6201#endif
6202 }
6203}
6204
6205/*
6206 * "u" command: Undo or make lower case.
6207 */
6208 static void
6209nv_undo(cap)
6210 cmdarg_T *cap;
6211{
6212 if (cap->oap->op_type == OP_LOWER
6213#ifdef FEAT_VISUAL
6214 || VIsual_active
6215#endif
6216 )
6217 {
6218 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6219 cap->cmdchar = 'g';
6220 cap->nchar = 'u';
6221 nv_operator(cap);
6222 }
6223 else
6224 nv_kundo(cap);
6225}
6226
6227/*
6228 * <Undo> command.
6229 */
6230 static void
6231nv_kundo(cap)
6232 cmdarg_T *cap;
6233{
6234 if (!checkclearopq(cap->oap))
6235 {
6236 u_undo((int)cap->count1);
6237 curwin->w_set_curswant = TRUE;
6238 }
6239}
6240
6241/*
6242 * Handle the "r" command.
6243 */
6244 static void
6245nv_replace(cap)
6246 cmdarg_T *cap;
6247{
6248 char_u *ptr;
6249 int had_ctrl_v;
6250 long n;
6251
6252 if (checkclearop(cap->oap))
6253 return;
6254
6255 /* get another character */
6256 if (cap->nchar == Ctrl_V)
6257 {
6258 had_ctrl_v = Ctrl_V;
6259 cap->nchar = get_literal();
6260 /* Don't redo a multibyte character with CTRL-V. */
6261 if (cap->nchar > DEL)
6262 had_ctrl_v = NUL;
6263 }
6264 else
6265 had_ctrl_v = NUL;
6266
6267#ifdef FEAT_VISUAL
6268 /* Visual mode "r" */
6269 if (VIsual_active)
6270 {
6271 nv_operator(cap);
6272 return;
6273 }
6274#endif
6275
6276#ifdef FEAT_VIRTUALEDIT
6277 /* Break tabs, etc. */
6278 if (virtual_active())
6279 {
6280 if (u_save_cursor() == FAIL)
6281 return;
6282 if (gchar_cursor() == NUL)
6283 {
6284 /* Add extra space and put the cursor on the first one. */
6285 coladvance_force((colnr_T)(getviscol() + cap->count1));
6286 curwin->w_cursor.col -= cap->count1;
6287 }
6288 else if (gchar_cursor() == TAB)
6289 coladvance_force(getviscol());
6290 }
6291#endif
6292
6293 /*
6294 * Check for a special key or not enough characters to replace.
6295 */
6296 ptr = ml_get_cursor();
6297 if (IS_SPECIAL(cap->nchar) || STRLEN(ptr) < (unsigned)cap->count1
6298#ifdef FEAT_MBYTE
6299 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6300#endif
6301 )
6302 {
6303 clearopbeep(cap->oap);
6304 return;
6305 }
6306
6307 /*
6308 * Replacing with a TAB is done by edit() when it is complicated because
6309 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6310 * Other characters are done below to avoid problems with things like
6311 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6312 */
6313 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6314 {
6315 stuffnumReadbuff(cap->count1);
6316 stuffcharReadbuff('R');
6317 stuffcharReadbuff('\t');
6318 stuffcharReadbuff(ESC);
6319 return;
6320 }
6321
6322 /* save line for undo */
6323 if (u_save_cursor() == FAIL)
6324 return;
6325
6326 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6327 {
6328 /*
6329 * Replace character(s) by a single newline.
6330 * Strange vi behaviour: Only one newline is inserted.
6331 * Delete the characters here.
6332 * Insert the newline with an insert command, takes care of
6333 * autoindent. The insert command depends on being on the last
6334 * character of a line or not.
6335 */
6336#ifdef FEAT_MBYTE
6337 (void)del_chars(cap->count1, FALSE); /* delete the characters */
6338#else
6339 (void)del_bytes(cap->count1, FALSE); /* delete the characters */
6340#endif
6341 stuffcharReadbuff('\r');
6342 stuffcharReadbuff(ESC);
6343
6344 /* Give 'r' to edit(), to get the redo command right. */
6345 invoke_edit(cap, TRUE, 'r', FALSE);
6346 }
6347 else
6348 {
6349 prep_redo(cap->oap->regname, cap->count1,
6350 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
6351
6352 curbuf->b_op_start = curwin->w_cursor;
6353#ifdef FEAT_MBYTE
6354 if (has_mbyte)
6355 {
6356 int old_State = State;
6357
6358 if (cap->ncharC1 != 0)
6359 AppendCharToRedobuff(cap->ncharC1);
6360 if (cap->ncharC2 != 0)
6361 AppendCharToRedobuff(cap->ncharC2);
6362
6363 /* This is slow, but it handles replacing a single-byte with a
6364 * multi-byte and the other way around. Also handles adding
6365 * composing characters for utf-8. */
6366 for (n = cap->count1; n > 0; --n)
6367 {
6368 State = REPLACE;
6369 ins_char(cap->nchar);
6370 State = old_State;
6371 if (cap->ncharC1 != 0)
6372 ins_char(cap->ncharC1);
6373 if (cap->ncharC2 != 0)
6374 ins_char(cap->ncharC2);
6375 }
6376 }
6377 else
6378#endif
6379 {
6380 /*
6381 * Replace the characters within one line.
6382 */
6383 for (n = cap->count1; n > 0; --n)
6384 {
6385 /*
6386 * Get ptr again, because u_save and/or showmatch() will have
6387 * released the line. At the same time we let know that the
6388 * line will be changed.
6389 */
6390 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
6391 ptr[curwin->w_cursor.col] = cap->nchar;
6392 if (p_sm && msg_silent == 0)
6393 showmatch(cap->nchar);
6394 ++curwin->w_cursor.col;
6395 }
6396#ifdef FEAT_NETBEANS_INTG
6397 if (usingNetbeans)
6398 {
6399 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
6400
Bram Moolenaar009b2592004-10-24 19:18:58 +00006401 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
6402 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00006404 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 }
6406#endif
6407
6408 /* mark the buffer as changed and prepare for displaying */
6409 changed_bytes(curwin->w_cursor.lnum,
6410 (colnr_T)(curwin->w_cursor.col - cap->count1));
6411 }
6412 --curwin->w_cursor.col; /* cursor on the last replaced char */
6413#ifdef FEAT_MBYTE
6414 /* if the character on the left of the current cursor is a multi-byte
6415 * character, move two characters left */
6416 if (has_mbyte)
6417 mb_adjust_cursor();
6418#endif
6419 curbuf->b_op_end = curwin->w_cursor;
6420 curwin->w_set_curswant = TRUE;
6421 set_last_insert(cap->nchar);
6422 }
6423}
6424
6425#ifdef FEAT_VISUAL
6426/*
6427 * 'o': Exchange start and end of Visual area.
6428 * 'O': same, but in block mode exchange left and right corners.
6429 */
6430 static void
6431v_swap_corners(cmdchar)
6432 int cmdchar;
6433{
6434 pos_T old_cursor;
6435 colnr_T left, right;
6436
6437 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
6438 {
6439 old_cursor = curwin->w_cursor;
6440 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
6441 curwin->w_cursor.lnum = VIsual.lnum;
6442 coladvance(left);
6443 VIsual = curwin->w_cursor;
6444
6445 curwin->w_cursor.lnum = old_cursor.lnum;
6446 curwin->w_curswant = right;
6447 /* 'selection "exclusive" and cursor at right-bottom corner: move it
6448 * right one column */
6449 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
6450 ++curwin->w_curswant;
6451 coladvance(curwin->w_curswant);
6452 if (curwin->w_cursor.col == old_cursor.col
6453#ifdef FEAT_VIRTUALEDIT
6454 && (!virtual_active()
6455 || curwin->w_cursor.coladd == old_cursor.coladd)
6456#endif
6457 )
6458 {
6459 curwin->w_cursor.lnum = VIsual.lnum;
6460 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
6461 ++right;
6462 coladvance(right);
6463 VIsual = curwin->w_cursor;
6464
6465 curwin->w_cursor.lnum = old_cursor.lnum;
6466 coladvance(left);
6467 curwin->w_curswant = left;
6468 }
6469 }
6470 else
6471 {
6472 old_cursor = curwin->w_cursor;
6473 curwin->w_cursor = VIsual;
6474 VIsual = old_cursor;
6475 curwin->w_set_curswant = TRUE;
6476 }
6477}
6478#endif /* FEAT_VISUAL */
6479
6480/*
6481 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
6482 */
6483 static void
6484nv_Replace(cap)
6485 cmdarg_T *cap;
6486{
6487#ifdef FEAT_VISUAL
6488 if (VIsual_active) /* "R" is replace lines */
6489 {
6490 cap->cmdchar = 'c';
6491 cap->nchar = NUL;
6492 VIsual_mode = 'V';
6493 nv_operator(cap);
6494 }
6495 else
6496#endif
6497 if (!checkclearopq(cap->oap))
6498 {
6499 if (!curbuf->b_p_ma)
6500 EMSG(_(e_modifiable));
6501 else
6502 {
6503#ifdef FEAT_VIRTUALEDIT
6504 if (virtual_active())
6505 coladvance(getviscol());
6506#endif
6507 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
6508 }
6509 }
6510}
6511
6512#ifdef FEAT_VREPLACE
6513/*
6514 * "gr".
6515 */
6516 static void
6517nv_vreplace(cap)
6518 cmdarg_T *cap;
6519{
6520# ifdef FEAT_VISUAL
6521 if (VIsual_active)
6522 {
6523 cap->cmdchar = 'r';
6524 cap->nchar = cap->extra_char;
6525 nv_replace(cap); /* Do same as "r" in Visual mode for now */
6526 }
6527 else
6528# endif
6529 if (!checkclearopq(cap->oap))
6530 {
6531 if (!curbuf->b_p_ma)
6532 EMSG(_(e_modifiable));
6533 else
6534 {
6535 if (cap->extra_char == Ctrl_V) /* get another character */
6536 cap->extra_char = get_literal();
6537 stuffcharReadbuff(cap->extra_char);
6538 stuffcharReadbuff(ESC);
6539# ifdef FEAT_VIRTUALEDIT
6540 if (virtual_active())
6541 coladvance(getviscol());
6542# endif
6543 invoke_edit(cap, TRUE, 'v', FALSE);
6544 }
6545 }
6546}
6547#endif
6548
6549/*
6550 * Swap case for "~" command, when it does not work like an operator.
6551 */
6552 static void
6553n_swapchar(cap)
6554 cmdarg_T *cap;
6555{
6556 long n;
6557 pos_T startpos;
6558 int did_change = 0;
6559#ifdef FEAT_NETBEANS_INTG
6560 pos_T pos;
6561 char_u *ptr;
6562 int count;
6563#endif
6564
6565 if (checkclearopq(cap->oap))
6566 return;
6567
6568 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
6569 {
6570 clearopbeep(cap->oap);
6571 return;
6572 }
6573
6574 prep_redo_cmd(cap);
6575
6576 if (u_save_cursor() == FAIL)
6577 return;
6578
6579 startpos = curwin->w_cursor;
6580#ifdef FEAT_NETBEANS_INTG
6581 pos = startpos;
6582#endif
6583 for (n = cap->count1; n > 0; --n)
6584 {
6585 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
6586 inc_cursor();
6587 if (gchar_cursor() == NUL)
6588 {
6589 if (vim_strchr(p_ww, '~') != NULL
6590 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6591 {
6592#ifdef FEAT_NETBEANS_INTG
6593 if (usingNetbeans)
6594 {
6595 if (did_change)
6596 {
6597 ptr = ml_get(pos.lnum);
6598 count = STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00006599 netbeans_removed(curbuf, pos.lnum, pos.col,
6600 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00006602 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 }
6604 pos.col = 0;
6605 pos.lnum++;
6606 }
6607#endif
6608 ++curwin->w_cursor.lnum;
6609 curwin->w_cursor.col = 0;
6610 if (n > 1)
6611 {
6612 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
6613 break;
6614 u_clearline();
6615 }
6616 }
6617 else
6618 break;
6619 }
6620 }
6621#ifdef FEAT_NETBEANS_INTG
6622 if (did_change && usingNetbeans)
6623 {
6624 ptr = ml_get(pos.lnum);
6625 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00006626 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
6627 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 }
6629#endif
6630
6631
6632 check_cursor();
6633 curwin->w_set_curswant = TRUE;
6634 if (did_change)
6635 {
6636 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
6637 0L);
6638 curbuf->b_op_start = startpos;
6639 curbuf->b_op_end = curwin->w_cursor;
6640 if (curbuf->b_op_end.col > 0)
6641 --curbuf->b_op_end.col;
6642 }
6643}
6644
6645/*
6646 * Move cursor to mark.
6647 */
6648 static void
6649nv_cursormark(cap, flag, pos)
6650 cmdarg_T *cap;
6651 int flag;
6652 pos_T *pos;
6653{
6654 if (check_mark(pos) == FAIL)
6655 clearop(cap->oap);
6656 else
6657 {
6658 if (cap->cmdchar == '\''
6659 || cap->cmdchar == '`'
6660 || cap->cmdchar == '['
6661 || cap->cmdchar == ']')
6662 setpcmark();
6663 curwin->w_cursor = *pos;
6664 if (flag)
6665 beginline(BL_WHITE | BL_FIX);
6666 else
6667 check_cursor();
6668 }
6669 cap->oap->motion_type = flag ? MLINE : MCHAR;
6670 if (cap->cmdchar == '`')
6671 cap->oap->use_reg_one = TRUE;
6672 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
6673 curwin->w_set_curswant = TRUE;
6674}
6675
6676#ifdef FEAT_VISUAL
6677/*
6678 * Handle commands that are operators in Visual mode.
6679 */
6680 static void
6681v_visop(cap)
6682 cmdarg_T *cap;
6683{
6684 static char_u trans[] = "YyDdCcxdXdAAIIrr";
6685
6686 /* Uppercase means linewise, except in block mode, then "D" deletes till
6687 * the end of the line, and "C" replaces til EOL */
6688 if (isupper(cap->cmdchar))
6689 {
6690 if (VIsual_mode != Ctrl_V)
6691 VIsual_mode = 'V';
6692 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
6693 curwin->w_curswant = MAXCOL;
6694 }
6695 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
6696 nv_operator(cap);
6697}
6698#endif
6699
6700/*
6701 * "s" and "S" commands.
6702 */
6703 static void
6704nv_subst(cap)
6705 cmdarg_T *cap;
6706{
6707#ifdef FEAT_VISUAL
6708 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
6709 {
6710 if (cap->cmdchar == 'S')
6711 VIsual_mode = 'V';
6712 cap->cmdchar = 'c';
6713 nv_operator(cap);
6714 }
6715 else
6716#endif
6717 nv_optrans(cap);
6718}
6719
6720/*
6721 * Abbreviated commands.
6722 */
6723 static void
6724nv_abbrev(cap)
6725 cmdarg_T *cap;
6726{
6727 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
6728 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
6729
6730#ifdef FEAT_VISUAL
6731 /* in Visual mode these commands are operators */
6732 if (VIsual_active)
6733 v_visop(cap);
6734 else
6735#endif
6736 nv_optrans(cap);
6737}
6738
6739/*
6740 * Translate a command into another command.
6741 */
6742 static void
6743nv_optrans(cap)
6744 cmdarg_T *cap;
6745{
6746 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
6747 (char_u *)"d$", (char_u *)"c$",
6748 (char_u *)"cl", (char_u *)"cc",
6749 (char_u *)"yy", (char_u *)":s\r"};
6750 static char_u *str = (char_u *)"xXDCsSY&";
6751
6752 if (!checkclearopq(cap->oap))
6753 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006754 /* In Vi "2D" doesn't delete the next line. Can't translate it
6755 * either, because "2." should also not use the count. */
6756 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
6757 {
6758 cap->oap->start = curwin->w_cursor;
6759 cap->oap->op_type = OP_DELETE;
6760 cap->count1 = 1;
6761 nv_dollar(cap);
6762 finish_op = TRUE;
6763 ResetRedobuff();
6764 AppendCharToRedobuff('D');
6765 }
6766 else
6767 {
6768 if (cap->count0)
6769 stuffnumReadbuff(cap->count0);
6770 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
6771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 }
6773 cap->opcount = 0;
6774}
6775
6776/*
6777 * "'" and "`" commands. Also for "g'" and "g`".
6778 * cap->arg is TRUE for "'" and "g'".
6779 */
6780 static void
6781nv_gomark(cap)
6782 cmdarg_T *cap;
6783{
6784 pos_T *pos;
6785 int c;
6786#ifdef FEAT_FOLDING
6787 linenr_T lnum = curwin->w_cursor.lnum;
6788 int old_KeyTyped = KeyTyped; /* getting file may reset it */
6789#endif
6790
6791 if (cap->cmdchar == 'g')
6792 c = cap->extra_char;
6793 else
6794 c = cap->nchar;
6795 pos = getmark(c, (cap->oap->op_type == OP_NOP));
6796 if (pos == (pos_T *)-1) /* jumped to other file */
6797 {
6798 if (cap->arg)
6799 {
6800 check_cursor_lnum();
6801 beginline(BL_WHITE | BL_FIX);
6802 }
6803 else
6804 check_cursor();
6805 }
6806 else
6807 nv_cursormark(cap, cap->arg, pos);
6808
6809#ifdef FEAT_VIRTUALEDIT
6810 /* May need to clear the coladd that a mark includes. */
6811 if (!virtual_active())
6812 curwin->w_cursor.coladd = 0;
6813#endif
6814#ifdef FEAT_FOLDING
6815 if (cap->oap->op_type == OP_NOP
6816 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
6817 && (fdo_flags & FDO_MARK)
6818 && old_KeyTyped)
6819 foldOpenCursor();
6820#endif
6821}
6822
6823/*
6824 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
6825 */
6826 static void
6827nv_pcmark(cap)
6828 cmdarg_T *cap;
6829{
6830#ifdef FEAT_JUMPLIST
6831 pos_T *pos;
6832# ifdef FEAT_FOLDING
6833 linenr_T lnum = curwin->w_cursor.lnum;
6834 int old_KeyTyped = KeyTyped; /* getting file may reset it */
6835# endif
6836
6837 if (!checkclearopq(cap->oap))
6838 {
6839 if (cap->cmdchar == 'g')
6840 pos = movechangelist((int)cap->count1);
6841 else
6842 pos = movemark((int)cap->count1);
6843 if (pos == (pos_T *)-1) /* jump to other file */
6844 {
6845 curwin->w_set_curswant = TRUE;
6846 check_cursor();
6847 }
6848 else if (pos != NULL) /* can jump */
6849 nv_cursormark(cap, FALSE, pos);
6850 else if (cap->cmdchar == 'g')
6851 {
6852 if (curbuf->b_changelistlen == 0)
6853 EMSG(_("E664: changelist is empty"));
6854 else if (cap->count1 < 0)
6855 EMSG(_("E662: At start of changelist"));
6856 else
6857 EMSG(_("E663: At end of changelist"));
6858 }
6859 else
6860 clearopbeep(cap->oap);
6861# ifdef FEAT_FOLDING
6862 if (cap->oap->op_type == OP_NOP
6863 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
6864 && (fdo_flags & FDO_MARK)
6865 && old_KeyTyped)
6866 foldOpenCursor();
6867# endif
6868 }
6869#else
6870 clearopbeep(cap->oap);
6871#endif
6872}
6873
6874/*
6875 * Handle '"' command.
6876 */
6877 static void
6878nv_regname(cap)
6879 cmdarg_T *cap;
6880{
6881 if (checkclearop(cap->oap))
6882 return;
6883#ifdef FEAT_EVAL
6884 if (cap->nchar == '=')
6885 cap->nchar = get_expr_register();
6886#endif
6887 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
6888 {
6889 cap->oap->regname = cap->nchar;
6890 cap->opcount = cap->count0; /* remember count before '"' */
6891#ifdef FEAT_EVAL
6892 set_reg_var(cap->oap->regname);
6893#endif
6894 }
6895 else
6896 clearopbeep(cap->oap);
6897}
6898
6899#ifdef FEAT_VISUAL
6900/*
6901 * Handle "v", "V" and "CTRL-V" commands.
6902 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
6903 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006904 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 */
6906 static void
6907nv_visual(cap)
6908 cmdarg_T *cap;
6909{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006910 if (cap->cmdchar == Ctrl_Q)
6911 cap->cmdchar = Ctrl_V;
6912
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
6914 * characterwise, linewise, or blockwise. */
6915 if (cap->oap->op_type != OP_NOP)
6916 {
6917 cap->oap->motion_force = cap->cmdchar;
6918 finish_op = FALSE; /* operator doesn't finish now but later */
6919 return;
6920 }
6921
6922 VIsual_select = cap->arg;
6923 if (VIsual_active) /* change Visual mode */
6924 {
6925 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
6926 end_visual_mode();
6927 else /* toggle char/block mode */
6928 { /* or char/line mode */
6929 VIsual_mode = cap->cmdchar;
6930 showmode();
6931 }
6932 redraw_curbuf_later(INVERTED); /* update the inversion */
6933 }
6934 else /* start Visual mode */
6935 {
6936 check_visual_highlight();
6937 if (cap->count0) /* use previously selected part */
6938 {
6939 if (resel_VIsual_mode == NUL) /* there is none */
6940 {
6941 beep_flush();
6942 return;
6943 }
6944 VIsual = curwin->w_cursor;
6945
6946 VIsual_active = TRUE;
6947 VIsual_reselect = TRUE;
6948 if (!cap->arg)
6949 /* start Select mode when 'selectmode' contains "cmd" */
6950 may_start_select('c');
6951#ifdef FEAT_MOUSE
6952 setmouse();
6953#endif
6954 if (p_smd)
6955 redraw_cmdline = TRUE; /* show visual mode later */
6956 /*
6957 * For V and ^V, we multiply the number of lines even if there
6958 * was only one -- webb
6959 */
6960 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
6961 {
6962 curwin->w_cursor.lnum +=
6963 resel_VIsual_line_count * cap->count0 - 1;
6964 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6965 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6966 }
6967 VIsual_mode = resel_VIsual_mode;
6968 if (VIsual_mode == 'v')
6969 {
6970 if (resel_VIsual_line_count <= 1)
6971 curwin->w_cursor.col += resel_VIsual_col * cap->count0 - 1;
6972 else
6973 curwin->w_cursor.col = resel_VIsual_col;
6974 check_cursor_col();
6975 }
6976 if (resel_VIsual_col == MAXCOL)
6977 {
6978 curwin->w_curswant = MAXCOL;
6979 coladvance((colnr_T)MAXCOL);
6980 }
6981 else if (VIsual_mode == Ctrl_V)
6982 {
6983 validate_virtcol();
6984 curwin->w_curswant = curwin->w_virtcol
6985 + resel_VIsual_col * cap->count0 - 1;
6986 coladvance(curwin->w_curswant);
6987 }
6988 else
6989 curwin->w_set_curswant = TRUE;
6990 redraw_curbuf_later(INVERTED); /* show the inversion */
6991 }
6992 else
6993 {
6994 if (!cap->arg)
6995 /* start Select mode when 'selectmode' contains "cmd" */
6996 may_start_select('c');
6997 n_start_visual_mode(cap->cmdchar);
6998 }
6999 }
7000}
7001
7002/*
7003 * Start selection for Shift-movement keys.
7004 */
7005 void
7006start_selection()
7007{
7008 /* if 'selectmode' contains "key", start Select mode */
7009 may_start_select('k');
7010 n_start_visual_mode('v');
7011}
7012
7013/*
7014 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7015 */
7016 void
7017may_start_select(c)
7018 int c;
7019{
7020 VIsual_select = (stuff_empty() && typebuf_typed()
7021 && (vim_strchr(p_slm, c) != NULL));
7022}
7023
7024/*
7025 * Start Visual mode "c".
7026 * Should set VIsual_select before calling this.
7027 */
7028 static void
7029n_start_visual_mode(c)
7030 int c;
7031{
7032 VIsual_mode = c;
7033 VIsual_active = TRUE;
7034 VIsual_reselect = TRUE;
7035#ifdef FEAT_VIRTUALEDIT
7036 /* Corner case: the 0 position in a tab may change when going into
7037 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7038 */
7039 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7040 coladvance(curwin->w_virtcol);
7041#endif
7042 VIsual = curwin->w_cursor;
7043
7044#ifdef FEAT_FOLDING
7045 foldAdjustVisual();
7046#endif
7047
7048#ifdef FEAT_MOUSE
7049 setmouse();
7050#endif
7051 if (p_smd)
7052 redraw_cmdline = TRUE; /* show visual mode later */
7053#ifdef FEAT_CLIPBOARD
7054 /* Make sure the clipboard gets updated. Needed because start and
7055 * end may still be the same, and the selection needs to be owned */
7056 clip_star.vmode = NUL;
7057#endif
7058
7059 /* Only need to redraw this line, unless still need to redraw an old
7060 * Visual area (when 'lazyredraw' is set). */
7061 if (curwin->w_redr_type < INVERTED)
7062 {
7063 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7064 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7065 }
7066}
7067
7068#endif /* FEAT_VISUAL */
7069
7070/*
7071 * CTRL-W: Window commands
7072 */
7073 static void
7074nv_window(cap)
7075 cmdarg_T *cap;
7076{
7077#ifdef FEAT_WINDOWS
7078 if (!checkclearop(cap->oap))
7079 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7080#else
7081 (void)checkclearop(cap->oap);
7082#endif
7083}
7084
7085/*
7086 * CTRL-Z: Suspend
7087 */
7088 static void
7089nv_suspend(cap)
7090 cmdarg_T *cap;
7091{
7092 clearop(cap->oap);
7093#ifdef FEAT_VISUAL
7094 if (VIsual_active)
7095 end_visual_mode(); /* stop Visual mode */
7096#endif
7097 do_cmdline_cmd((char_u *)"st");
7098}
7099
7100/*
7101 * Commands starting with "g".
7102 */
7103 static void
7104nv_g_cmd(cap)
7105 cmdarg_T *cap;
7106{
7107 oparg_T *oap = cap->oap;
7108#ifdef FEAT_VISUAL
7109 pos_T tpos;
7110#endif
7111 int i;
7112 int flag = FALSE;
7113
7114 switch (cap->nchar)
7115 {
7116#ifdef MEM_PROFILE
7117 /*
7118 * "g^A": dump log of used memory.
7119 */
7120 case Ctrl_A:
7121 vim_mem_profile_dump();
7122 break;
7123#endif
7124
7125#ifdef FEAT_VREPLACE
7126 /*
7127 * "gR": Enter virtual replace mode.
7128 */
7129 case 'R':
7130 cap->arg = TRUE;
7131 nv_Replace(cap);
7132 break;
7133
7134 case 'r':
7135 nv_vreplace(cap);
7136 break;
7137#endif
7138
7139 case '&':
7140 do_cmdline_cmd((char_u *)"%s//~/&");
7141 break;
7142
7143#ifdef FEAT_VISUAL
7144 /*
7145 * "gv": Reselect the previous Visual area. If Visual already active,
7146 * exchange previous and current Visual area.
7147 */
7148 case 'v':
7149 if (checkclearop(oap))
7150 break;
7151
7152 if ( curbuf->b_visual_start.lnum == 0
7153 || curbuf->b_visual_start.lnum > curbuf->b_ml.ml_line_count
7154 || curbuf->b_visual_end.lnum == 0)
7155 beep_flush();
7156 else
7157 {
7158 /* set w_cursor to the start of the Visual area, tpos to the end */
7159 if (VIsual_active)
7160 {
7161 i = VIsual_mode;
7162 VIsual_mode = curbuf->b_visual_mode;
7163 curbuf->b_visual_mode = i;
7164# ifdef FEAT_EVAL
7165 curbuf->b_visual_mode_eval = i;
7166# endif
7167 i = curwin->w_curswant;
7168 curwin->w_curswant = curbuf->b_visual_curswant;
7169 curbuf->b_visual_curswant = i;
7170
7171 tpos = curbuf->b_visual_end;
7172 curbuf->b_visual_end = curwin->w_cursor;
7173 curwin->w_cursor = curbuf->b_visual_start;
7174 curbuf->b_visual_start = VIsual;
7175 }
7176 else
7177 {
7178 VIsual_mode = curbuf->b_visual_mode;
7179 curwin->w_curswant = curbuf->b_visual_curswant;
7180 tpos = curbuf->b_visual_end;
7181 curwin->w_cursor = curbuf->b_visual_start;
7182 }
7183
7184 VIsual_active = TRUE;
7185 VIsual_reselect = TRUE;
7186
7187 /* Set Visual to the start and w_cursor to the end of the Visual
7188 * area. Make sure they are on an existing character. */
7189 check_cursor();
7190 VIsual = curwin->w_cursor;
7191 curwin->w_cursor = tpos;
7192 check_cursor();
7193 update_topline();
7194 /*
7195 * When called from normal "g" command: start Select mode when
7196 * 'selectmode' contains "cmd". When called for K_SELECT, always
7197 * start Select mode.
7198 */
7199 if (cap->arg)
7200 VIsual_select = TRUE;
7201 else
7202 may_start_select('c');
7203#ifdef FEAT_MOUSE
7204 setmouse();
7205#endif
7206#ifdef FEAT_CLIPBOARD
7207 /* Make sure the clipboard gets updated. Needed because start and
7208 * end are still the same, and the selection needs to be owned */
7209 clip_star.vmode = NUL;
7210#endif
7211 redraw_curbuf_later(INVERTED);
7212 showmode();
7213 }
7214 break;
7215 /*
7216 * "gV": Don't reselect the previous Visual area after a Select mode
7217 * mapping of menu.
7218 */
7219 case 'V':
7220 VIsual_reselect = FALSE;
7221 break;
7222
7223 /*
7224 * "gh": start Select mode.
7225 * "gH": start Select line mode.
7226 * "g^H": start Select block mode.
7227 */
7228 case K_BS:
7229 cap->nchar = Ctrl_H;
7230 /* FALLTHROUGH */
7231 case 'h':
7232 case 'H':
7233 case Ctrl_H:
7234# ifdef EBCDIC
7235 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7236 if (cap->nchar == Ctrl_H)
7237 cap->cmdchar = Ctrl_V;
7238 else
7239# endif
7240 cap->cmdchar = cap->nchar + ('v' - 'h');
7241 cap->arg = TRUE;
7242 nv_visual(cap);
7243 break;
7244#endif /* FEAT_VISUAL */
7245
7246 /*
7247 * "gj" and "gk" two new funny movement keys -- up and down
7248 * movement based on *screen* line rather than *file* line.
7249 */
7250 case 'j':
7251 case K_DOWN:
7252 /* with 'nowrap' it works just like the normal "j" command; also when
7253 * in a closed fold */
7254 if (!curwin->w_p_wrap
7255#ifdef FEAT_FOLDING
7256 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7257#endif
7258 )
7259 {
7260 oap->motion_type = MLINE;
7261 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7262 }
7263 else
7264 i = nv_screengo(oap, FORWARD, cap->count1);
7265 if (i == FAIL)
7266 clearopbeep(oap);
7267 break;
7268
7269 case 'k':
7270 case K_UP:
7271 /* with 'nowrap' it works just like the normal "k" command; also when
7272 * in a closed fold */
7273 if (!curwin->w_p_wrap
7274#ifdef FEAT_FOLDING
7275 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7276#endif
7277 )
7278 {
7279 oap->motion_type = MLINE;
7280 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7281 }
7282 else
7283 i = nv_screengo(oap, BACKWARD, cap->count1);
7284 if (i == FAIL)
7285 clearopbeep(oap);
7286 break;
7287
7288 /*
7289 * "gJ": join two lines without inserting a space.
7290 */
7291 case 'J':
7292 nv_join(cap);
7293 break;
7294
7295 /*
7296 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7297 * "gm": middle of "g0" and "g$".
7298 */
7299 case '^':
7300 flag = TRUE;
7301 /* FALLTHROUGH */
7302
7303 case '0':
7304 case 'm':
7305 case K_HOME:
7306 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 oap->motion_type = MCHAR;
7308 oap->inclusive = FALSE;
7309 if (curwin->w_p_wrap
7310#ifdef FEAT_VERTSPLIT
7311 && curwin->w_width != 0
7312#endif
7313 )
7314 {
7315 int width1 = W_WIDTH(curwin) - curwin_col_off();
7316 int width2 = width1 + curwin_col_off2();
7317
7318 validate_virtcol();
7319 i = 0;
7320 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
7321 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
7322 }
7323 else
7324 i = curwin->w_leftcol;
7325 /* Go to the middle of the screen line. When 'number' is on and lines
7326 * are wrapping the middle can be more to the left.*/
7327 if (cap->nchar == 'm')
7328 i += (W_WIDTH(curwin) - curwin_col_off()
7329 + ((curwin->w_p_wrap && i > 0)
7330 ? curwin_col_off2() : 0)) / 2;
7331 coladvance((colnr_T)i);
7332 if (flag)
7333 {
7334 do
7335 i = gchar_cursor();
7336 while (vim_iswhite(i) && oneright() == OK);
7337 }
7338 curwin->w_set_curswant = TRUE;
7339 break;
7340
7341 case '_':
7342 /* "g_": to the last non-blank character in the line or <count> lines
7343 * downward. */
7344 cap->oap->motion_type = MCHAR;
7345 cap->oap->inclusive = TRUE;
7346 curwin->w_curswant = MAXCOL;
7347 if (cursor_down((long)(cap->count1 - 1),
7348 cap->oap->op_type == OP_NOP) == FAIL)
7349 clearopbeep(cap->oap);
7350 else
7351 {
7352 char_u *ptr = ml_get_curline();
7353
7354 /* In Visual mode we may end up after the line. */
7355 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
7356 --curwin->w_cursor.col;
7357
7358 /* Decrease the cursor column until it's on a non-blank. */
7359 while (curwin->w_cursor.col > 0
7360 && vim_iswhite(ptr[curwin->w_cursor.col]))
7361 --curwin->w_cursor.col;
7362 curwin->w_set_curswant = TRUE;
7363 }
7364 break;
7365
7366 case '$':
7367 case K_END:
7368 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 {
7370 int col_off = curwin_col_off();
7371
7372 oap->motion_type = MCHAR;
7373 oap->inclusive = TRUE;
7374 if (curwin->w_p_wrap
7375#ifdef FEAT_VERTSPLIT
7376 && curwin->w_width != 0
7377#endif
7378 )
7379 {
7380 curwin->w_curswant = MAXCOL; /* so we stay at the end */
7381 if (cap->count1 == 1)
7382 {
7383 int width1 = W_WIDTH(curwin) - col_off;
7384 int width2 = width1 + curwin_col_off2();
7385
7386 validate_virtcol();
7387 i = width1 - 1;
7388 if (curwin->w_virtcol >= (colnr_T)width1)
7389 i += ((curwin->w_virtcol - width1) / width2 + 1)
7390 * width2;
7391 coladvance((colnr_T)i);
7392#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
7393 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
7394 {
7395 /*
7396 * Check for landing on a character that got split at
7397 * the end of the line. We do not want to advance to
7398 * the next screen line.
7399 */
7400 validate_virtcol();
7401 if (curwin->w_virtcol > (colnr_T)i)
7402 --curwin->w_cursor.col;
7403 }
7404#endif
7405 }
7406 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
7407 clearopbeep(oap);
7408 }
7409 else
7410 {
7411 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
7412 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007413
7414 /* Make sure we stick in this column. */
7415 validate_virtcol();
7416 curwin->w_curswant = curwin->w_virtcol;
7417 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 }
7419 }
7420 break;
7421
7422 /*
7423 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
7424 */
7425 case '*':
7426 case '#':
7427#if POUND != '#'
7428 case POUND: /* pound sign (sometimes equal to '#') */
7429#endif
7430 case Ctrl_RSB: /* :tag or :tselect for current identifier */
7431 case ']': /* :tselect for current identifier */
7432 nv_ident(cap);
7433 break;
7434
7435 /*
7436 * ge and gE: go back to end of word
7437 */
7438 case 'e':
7439 case 'E':
7440 oap->motion_type = MCHAR;
7441 curwin->w_set_curswant = TRUE;
7442 oap->inclusive = TRUE;
7443 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
7444 clearopbeep(oap);
7445 break;
7446
7447 /*
7448 * "g CTRL-G": display info about cursor position
7449 */
7450 case Ctrl_G:
7451 cursor_pos_info();
7452 break;
7453
7454 /*
7455 * "gi": start Insert at the last position.
7456 */
7457 case 'i':
7458 if (curbuf->b_last_insert.lnum != 0)
7459 {
7460 curwin->w_cursor = curbuf->b_last_insert;
7461 check_cursor_lnum();
7462 i = (int)STRLEN(ml_get_curline());
7463 if (curwin->w_cursor.col > (colnr_T)i)
7464 {
7465#ifdef FEAT_VIRTUALEDIT
7466 if (virtual_active())
7467 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
7468#endif
7469 curwin->w_cursor.col = i;
7470 }
7471 }
7472 cap->cmdchar = 'i';
7473 nv_edit(cap);
7474 break;
7475
7476 /*
7477 * "gI": Start insert in column 1.
7478 */
7479 case 'I':
7480 beginline(0);
7481 if (!checkclearopq(oap))
7482 invoke_edit(cap, FALSE, 'g', FALSE);
7483 break;
7484
7485#ifdef FEAT_SEARCHPATH
7486 /*
7487 * "gf": goto file, edit file under cursor
7488 * "]f" and "[f": can also be used.
7489 */
7490 case 'f':
7491 nv_gotofile(cap);
7492 break;
7493#endif
7494
7495 /* "g'm" and "g`m": jump to mark without setting pcmark */
7496 case '\'':
7497 cap->arg = TRUE;
7498 /*FALLTHROUGH*/
7499 case '`':
7500 nv_gomark(cap);
7501 break;
7502
7503 /*
7504 * "gs": Goto sleep.
7505 */
7506 case 's':
7507 do_sleep(cap->count1 * 1000L);
7508 break;
7509
7510 /*
7511 * "ga": Display the ascii value of the character under the
7512 * cursor. It is displayed in decimal, hex, and octal. -- webb
7513 */
7514 case 'a':
7515 do_ascii(NULL);
7516 break;
7517
7518#ifdef FEAT_MBYTE
7519 /*
7520 * "g8": Display the bytes used for the UTF-8 character under the
7521 * cursor. It is displayed in hex.
7522 */
7523 case '8':
7524 show_utf8();
7525 break;
7526#endif
7527
7528 /*
7529 * "gg": Goto the first line in file. With a count it goes to
7530 * that line number like for "G". -- webb
7531 */
7532 case 'g':
7533 cap->arg = FALSE;
7534 nv_goto(cap);
7535 break;
7536
7537 /*
7538 * Two-character operators:
7539 * "gq" Format text
7540 * "gw" Format text and keep cursor position
7541 * "g~" Toggle the case of the text.
7542 * "gu" Change text to lower case.
7543 * "gU" Change text to upper case.
7544 * "g?" rot13 encoding
7545 */
7546 case 'q':
7547 case 'w':
7548 oap->cursor_start = curwin->w_cursor;
7549 /*FALLTHROUGH*/
7550 case '~':
7551 case 'u':
7552 case 'U':
7553 case '?':
7554 nv_operator(cap);
7555 break;
7556
7557 /*
7558 * "gd": Find first occurence of pattern under the cursor in the
7559 * current function
7560 * "gD": idem, but in the current file.
7561 */
7562 case 'd':
7563 case 'D':
7564 nv_gd(oap, cap->nchar);
7565 break;
7566
7567#ifdef FEAT_MOUSE
7568 /*
7569 * g<*Mouse> : <C-*mouse>
7570 */
7571 case K_MIDDLEMOUSE:
7572 case K_MIDDLEDRAG:
7573 case K_MIDDLERELEASE:
7574 case K_LEFTMOUSE:
7575 case K_LEFTDRAG:
7576 case K_LEFTRELEASE:
7577 case K_RIGHTMOUSE:
7578 case K_RIGHTDRAG:
7579 case K_RIGHTRELEASE:
7580 case K_X1MOUSE:
7581 case K_X1DRAG:
7582 case K_X1RELEASE:
7583 case K_X2MOUSE:
7584 case K_X2DRAG:
7585 case K_X2RELEASE:
7586 mod_mask = MOD_MASK_CTRL;
7587 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
7588 break;
7589#endif
7590
7591 case K_IGNORE:
7592 break;
7593
7594 /*
7595 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
7596 */
7597 case 'p':
7598 case 'P':
7599 nv_put(cap);
7600 break;
7601
7602#ifdef FEAT_BYTEOFF
7603 /* "go": goto byte count from start of buffer */
7604 case 'o':
7605 goto_byte(cap->count0);
7606 break;
7607#endif
7608
7609 /* "gQ": improved Ex mode */
7610 case 'Q':
7611#ifdef FEAT_CMDWIN
7612 if (cmdwin_type != 0)
7613 {
7614 clearopbeep(cap->oap);
7615 break;
7616 }
7617#endif
7618 if (!checkclearopq(oap))
7619 do_exmode(TRUE);
7620 break;
7621
7622#ifdef FEAT_JUMPLIST
7623 case ',':
7624 nv_pcmark(cap);
7625 break;
7626
7627 case ';':
7628 cap->count1 = -cap->count1;
7629 nv_pcmark(cap);
7630 break;
7631#endif
7632
7633 default:
7634 clearopbeep(oap);
7635 break;
7636 }
7637}
7638
7639/*
7640 * Handle "o" and "O" commands.
7641 */
7642 static void
7643n_opencmd(cap)
7644 cmdarg_T *cap;
7645{
7646 if (!checkclearopq(cap->oap))
7647 {
7648#ifdef FEAT_FOLDING
7649 if (cap->cmdchar == 'O')
7650 /* Open above the first line of a folded sequence of lines */
7651 (void)hasFolding(curwin->w_cursor.lnum,
7652 &curwin->w_cursor.lnum, NULL);
7653 else
7654 /* Open below the last line of a folded sequence of lines */
7655 (void)hasFolding(curwin->w_cursor.lnum,
7656 NULL, &curwin->w_cursor.lnum);
7657#endif
7658 if (u_save((linenr_T)(curwin->w_cursor.lnum -
7659 (cap->cmdchar == 'O' ? 1 : 0)),
7660 (linenr_T)(curwin->w_cursor.lnum +
7661 (cap->cmdchar == 'o' ? 1 : 0))
7662 ) == OK
7663 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
7664#ifdef FEAT_COMMENTS
7665 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
7666#endif
7667 0, 0))
7668 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007669 /* When '#' is in 'cpoptions' ignore the count. */
7670 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
7671 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
7673 }
7674 }
7675}
7676
7677/*
7678 * "." command: redo last change.
7679 */
7680 static void
7681nv_dot(cap)
7682 cmdarg_T *cap;
7683{
7684 if (!checkclearopq(cap->oap))
7685 {
7686 /*
7687 * If "restart_edit" is TRUE, the last but one command is repeated
7688 * instead of the last command (inserting text). This is used for
7689 * CTRL-O <.> in insert mode.
7690 */
7691 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
7692 clearopbeep(cap->oap);
7693 }
7694}
7695
7696/*
7697 * CTRL-R: undo undo
7698 */
7699 static void
7700nv_redo(cap)
7701 cmdarg_T *cap;
7702{
7703 if (!checkclearopq(cap->oap))
7704 {
7705 u_redo((int)cap->count1);
7706 curwin->w_set_curswant = TRUE;
7707 }
7708}
7709
7710/*
7711 * Handle "U" command.
7712 */
7713 static void
7714nv_Undo(cap)
7715 cmdarg_T *cap;
7716{
7717 /* In Visual mode and typing "gUU" triggers an operator */
7718 if (cap->oap->op_type == OP_UPPER
7719#ifdef FEAT_VISUAL
7720 || VIsual_active
7721#endif
7722 )
7723 {
7724 /* translate "gUU" to "gUgU" */
7725 cap->cmdchar = 'g';
7726 cap->nchar = 'U';
7727 nv_operator(cap);
7728 }
7729 else if (!checkclearopq(cap->oap))
7730 {
7731 u_undoline();
7732 curwin->w_set_curswant = TRUE;
7733 }
7734}
7735
7736/*
7737 * '~' command: If tilde is not an operator and Visual is off: swap case of a
7738 * single character.
7739 */
7740 static void
7741nv_tilde(cap)
7742 cmdarg_T *cap;
7743{
7744 if (!p_to
7745#ifdef FEAT_VISUAL
7746 && !VIsual_active
7747#endif
7748 && cap->oap->op_type != OP_TILDE)
7749 n_swapchar(cap);
7750 else
7751 nv_operator(cap);
7752}
7753
7754/*
7755 * Handle an operator command.
7756 * The actual work is done by do_pending_operator().
7757 */
7758 static void
7759nv_operator(cap)
7760 cmdarg_T *cap;
7761{
7762 int op_type;
7763
7764 op_type = get_op_type(cap->cmdchar, cap->nchar);
7765
7766 if (op_type == cap->oap->op_type) /* double operator works on lines */
7767 nv_lineop(cap);
7768 else if (!checkclearop(cap->oap))
7769 {
7770 cap->oap->start = curwin->w_cursor;
7771 cap->oap->op_type = op_type;
7772 }
7773}
7774
7775/*
7776 * Handle linewise operator "dd", "yy", etc.
7777 *
7778 * "_" is is a strange motion command that helps make operators more logical.
7779 * It is actually implemented, but not documented in the real Vi. This motion
7780 * command actually refers to "the current line". Commands like "dd" and "yy"
7781 * are really an alternate form of "d_" and "y_". It does accept a count, so
7782 * "d3_" works to delete 3 lines.
7783 */
7784 static void
7785nv_lineop(cap)
7786 cmdarg_T *cap;
7787{
7788 cap->oap->motion_type = MLINE;
7789 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
7790 clearopbeep(cap->oap);
7791 else if ( cap->oap->op_type == OP_DELETE
7792 || cap->oap->op_type == OP_LSHIFT
7793 || cap->oap->op_type == OP_RSHIFT)
7794 beginline(BL_SOL | BL_FIX);
7795 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
7796 beginline(BL_WHITE | BL_FIX);
7797}
7798
7799/*
7800 * <Home> command.
7801 */
7802 static void
7803nv_home(cap)
7804 cmdarg_T *cap;
7805{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00007806 /* CTRL-HOME is like "gg" */
7807 if (mod_mask & MOD_MASK_CTRL)
7808 nv_goto(cap);
7809 else
7810 {
7811 cap->count0 = 1;
7812 nv_pipe(cap);
7813 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00007814 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
7815 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816}
7817
7818/*
7819 * "|" command.
7820 */
7821 static void
7822nv_pipe(cap)
7823 cmdarg_T *cap;
7824{
7825 cap->oap->motion_type = MCHAR;
7826 cap->oap->inclusive = FALSE;
7827 beginline(0);
7828 if (cap->count0 > 0)
7829 {
7830 coladvance((colnr_T)(cap->count0 - 1));
7831 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
7832 }
7833 else
7834 curwin->w_curswant = 0;
7835 /* keep curswant at the column where we wanted to go, not where
7836 we ended; differs if line is too short */
7837 curwin->w_set_curswant = FALSE;
7838}
7839
7840/*
7841 * Handle back-word command "b" and "B".
7842 * cap->arg is 1 for "B"
7843 */
7844 static void
7845nv_bck_word(cap)
7846 cmdarg_T *cap;
7847{
7848 cap->oap->motion_type = MCHAR;
7849 cap->oap->inclusive = FALSE;
7850 curwin->w_set_curswant = TRUE;
7851 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
7852 clearopbeep(cap->oap);
7853#ifdef FEAT_FOLDING
7854 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
7855 foldOpenCursor();
7856#endif
7857}
7858
7859/*
7860 * Handle word motion commands "e", "E", "w" and "W".
7861 * cap->arg is TRUE for "E" and "W".
7862 */
7863 static void
7864nv_wordcmd(cap)
7865 cmdarg_T *cap;
7866{
7867 int n;
7868 int word_end;
7869 int flag = FALSE;
7870
7871 /*
7872 * Set inclusive for the "E" and "e" command.
7873 */
7874 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
7875 word_end = TRUE;
7876 else
7877 word_end = FALSE;
7878 cap->oap->inclusive = word_end;
7879
7880 /*
7881 * "cw" and "cW" are a special case.
7882 */
7883 if (!word_end && cap->oap->op_type == OP_CHANGE)
7884 {
7885 n = gchar_cursor();
7886 if (n != NUL) /* not an empty line */
7887 {
7888 if (vim_iswhite(n))
7889 {
7890 /*
7891 * Reproduce a funny Vi behaviour: "cw" on a blank only
7892 * changes one character, not all blanks until the start of
7893 * the next word. Only do this when the 'w' flag is included
7894 * in 'cpoptions'.
7895 */
7896 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
7897 {
7898 cap->oap->inclusive = TRUE;
7899 cap->oap->motion_type = MCHAR;
7900 return;
7901 }
7902 }
7903 else
7904 {
7905 /*
7906 * This is a little strange. To match what the real Vi does,
7907 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
7908 * that we are not on a space or a TAB. This seems impolite
7909 * at first, but it's really more what we mean when we say
7910 * 'cw'.
7911 * Another strangeness: When standing on the end of a word
7912 * "ce" will change until the end of the next wordt, but "cw"
7913 * will change only one character! This is done by setting
7914 * flag.
7915 */
7916 cap->oap->inclusive = TRUE;
7917 word_end = TRUE;
7918 flag = TRUE;
7919 }
7920 }
7921 }
7922
7923 cap->oap->motion_type = MCHAR;
7924 curwin->w_set_curswant = TRUE;
7925 if (word_end)
7926 n = end_word(cap->count1, cap->arg, flag, FALSE);
7927 else
7928 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
7929
7930 /* Don't leave the cursor on the NUL past a line */
7931 if (curwin->w_cursor.col && gchar_cursor() == NUL)
7932 {
7933 --curwin->w_cursor.col;
7934 cap->oap->inclusive = TRUE;
7935 }
7936
7937 if (n == FAIL && cap->oap->op_type == OP_NOP)
7938 clearopbeep(cap->oap);
7939 else
7940 {
7941#ifdef FEAT_VISUAL
7942 adjust_for_sel(cap);
7943#endif
7944#ifdef FEAT_FOLDING
7945 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
7946 foldOpenCursor();
7947#endif
7948 }
7949}
7950
7951/*
7952 * "0" and "^" commands.
7953 * cap->arg is the argument for beginline().
7954 */
7955 static void
7956nv_beginline(cap)
7957 cmdarg_T *cap;
7958{
7959 cap->oap->motion_type = MCHAR;
7960 cap->oap->inclusive = FALSE;
7961 beginline(cap->arg);
7962#ifdef FEAT_FOLDING
7963 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
7964 foldOpenCursor();
7965#endif
7966}
7967
7968#ifdef FEAT_VISUAL
7969/*
7970 * In exclusive Visual mode, may include the last character.
7971 */
7972 static void
7973adjust_for_sel(cap)
7974 cmdarg_T *cap;
7975{
7976 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
7977 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
7978 {
7979# ifdef FEAT_MBYTE
7980 if (has_mbyte)
7981 inc_cursor();
7982 else
7983# endif
7984 ++curwin->w_cursor.col;
7985 cap->oap->inclusive = FALSE;
7986 }
7987}
7988
7989/*
7990 * Exclude last character at end of Visual area for 'selection' == "exclusive".
7991 * Should check VIsual_mode before calling this.
7992 * Returns TRUE when backed up to the previous line.
7993 */
7994 static int
7995unadjust_for_sel()
7996{
7997 pos_T *pp;
7998
7999 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8000 {
8001 if (lt(VIsual, curwin->w_cursor))
8002 pp = &curwin->w_cursor;
8003 else
8004 pp = &VIsual;
8005#ifdef FEAT_VIRTUALEDIT
8006 if (pp->coladd > 0)
8007 --pp->coladd;
8008 else
8009#endif
8010 if (pp->col > 0)
8011 {
8012 --pp->col;
8013#ifdef FEAT_MBYTE
8014 mb_adjustpos(pp);
8015#endif
8016 }
8017 else if (pp->lnum > 1)
8018 {
8019 --pp->lnum;
8020 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8021 return TRUE;
8022 }
8023 }
8024 return FALSE;
8025}
8026
8027/*
8028 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8029 */
8030 static void
8031nv_select(cap)
8032 cmdarg_T *cap;
8033{
8034 if (VIsual_active)
8035 VIsual_select = TRUE;
8036 else if (VIsual_reselect)
8037 {
8038 cap->nchar = 'v'; /* fake "gv" command */
8039 cap->arg = TRUE;
8040 nv_g_cmd(cap);
8041 }
8042}
8043
8044#endif
8045
8046/*
8047 * "G", "gg", CTRL-END, CTRL-HOME.
8048 * cap->arg is TRUE for "G".
8049 */
8050 static void
8051nv_goto(cap)
8052 cmdarg_T *cap;
8053{
8054 linenr_T lnum;
8055
8056 if (cap->arg)
8057 lnum = curbuf->b_ml.ml_line_count;
8058 else
8059 lnum = 1L;
8060 cap->oap->motion_type = MLINE;
8061 setpcmark();
8062
8063 /* When a count is given, use it instead of the default lnum */
8064 if (cap->count0 != 0)
8065 lnum = cap->count0;
8066 if (lnum < 1L)
8067 lnum = 1L;
8068 else if (lnum > curbuf->b_ml.ml_line_count)
8069 lnum = curbuf->b_ml.ml_line_count;
8070 curwin->w_cursor.lnum = lnum;
8071 beginline(BL_SOL | BL_FIX);
8072#ifdef FEAT_FOLDING
8073 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8074 foldOpenCursor();
8075#endif
8076}
8077
8078/*
8079 * CTRL-\ in Normal mode.
8080 */
8081 static void
8082nv_normal(cap)
8083 cmdarg_T *cap;
8084{
8085 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8086 {
8087 clearop(cap->oap);
8088 if (restart_edit != 0 && p_smd)
8089 clear_cmdline = TRUE; /* unshow mode later */
8090 restart_edit = 0;
8091#ifdef FEAT_CMDWIN
8092 if (cmdwin_type != 0)
8093 cmdwin_result = Ctrl_C;
8094#endif
8095#ifdef FEAT_VISUAL
8096 if (VIsual_active)
8097 {
8098 end_visual_mode(); /* stop Visual */
8099 redraw_curbuf_later(INVERTED);
8100 }
8101#endif
8102 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8103 if (cap->nchar == Ctrl_G && p_im)
8104 restart_edit = 'a';
8105 }
8106 else
8107 clearopbeep(cap->oap);
8108}
8109
8110/*
8111 * ESC in Normal mode: beep, but don't flush buffers.
8112 * Don't even beep if we are canceling a command.
8113 */
8114 static void
8115nv_esc(cap)
8116 cmdarg_T *cap;
8117{
8118 int no_reason;
8119
8120 no_reason = (cap->oap->op_type == OP_NOP
8121 && cap->opcount == 0
8122 && cap->count0 == 0
8123 && cap->oap->regname == 0
8124 && !p_im);
8125
8126 if (cap->arg) /* TRUE for CTRL-C */
8127 {
8128 if (restart_edit == 0
8129#ifdef FEAT_CMDWIN
8130 && cmdwin_type == 0
8131#endif
8132#ifdef FEAT_VISUAL
8133 && !VIsual_active
8134#endif
8135 && no_reason)
8136 MSG(_("Type :quit<Enter> to exit Vim"));
8137
8138 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8139 * set again below when halfway a mapping. */
8140 if (!p_im)
8141 restart_edit = 0;
8142#ifdef FEAT_CMDWIN
8143 if (cmdwin_type != 0)
8144 {
8145 cmdwin_result = K_IGNORE;
8146 got_int = FALSE; /* don't stop executing autocommands et al. */
8147 return;
8148 }
8149#endif
8150 }
8151
8152#ifdef FEAT_VISUAL
8153 if (VIsual_active)
8154 {
8155 end_visual_mode(); /* stop Visual */
8156 check_cursor_col(); /* make sure cursor is not beyond EOL */
8157 curwin->w_set_curswant = TRUE;
8158 redraw_curbuf_later(INVERTED);
8159 }
8160 else
8161#endif
8162 if (no_reason)
8163 vim_beep();
8164 clearop(cap->oap);
8165
8166 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8167 * set return to Insert mode afterwards. */
8168 if (restart_edit == 0 && goto_im()
8169#ifdef FEAT_EX_EXTRA
8170 && ex_normal_busy == 0
8171#endif
8172 )
8173 restart_edit = 'a';
8174}
8175
8176/*
8177 * Handle "A", "a", "I", "i" and <Insert> commands.
8178 */
8179 static void
8180nv_edit(cap)
8181 cmdarg_T *cap;
8182{
8183 /* <Insert> is equal to "i" */
8184 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8185 cap->cmdchar = 'i';
8186
8187#ifdef FEAT_VISUAL
8188 /* in Visual mode "A" and "I" are an operator */
8189 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8190 v_visop(cap);
8191
8192 /* in Visual mode and after an operator "a" and "i" are for text objects */
8193 else
8194#endif
8195 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8196 && (cap->oap->op_type != OP_NOP
8197#ifdef FEAT_VISUAL
8198 || VIsual_active
8199#endif
8200 ))
8201 {
8202#ifdef FEAT_TEXTOBJ
8203 nv_object(cap);
8204#else
8205 clearopbeep(cap->oap);
8206#endif
8207 }
8208 else if (!curbuf->b_p_ma && !p_im)
8209 {
8210 /* Only give this error when 'insertmode' is off. */
8211 EMSG(_(e_modifiable));
8212 clearop(cap->oap);
8213 }
8214 else if (!checkclearopq(cap->oap))
8215 {
8216 switch (cap->cmdchar)
8217 {
8218 case 'A': /* "A"ppend after the line */
8219 curwin->w_set_curswant = TRUE;
8220#ifdef FEAT_VIRTUALEDIT
8221 if (ve_flags == VE_ALL)
8222 {
8223 int save_State = State;
8224
8225 /* Pretent Insert mode here to allow the cursor on the
8226 * character past the end of the line */
8227 State = INSERT;
8228 coladvance((colnr_T)MAXCOL);
8229 State = save_State;
8230 }
8231 else
8232#endif
8233 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8234 break;
8235
8236 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008237 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
8238 beginline(BL_WHITE);
8239 else
8240 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 break;
8242
8243 case 'a': /* "a"ppend is like "i"nsert on the next character. */
8244#ifdef FEAT_VIRTUALEDIT
8245 /* increment coladd when in virtual space, increment the
8246 * column otherwise, also to append after an unprintable char */
8247 if (virtual_active()
8248 && (curwin->w_cursor.coladd > 0
8249 || *ml_get_cursor() == NUL
8250 || *ml_get_cursor() == TAB))
8251 curwin->w_cursor.coladd++;
8252 else
8253#endif
8254 if (*ml_get_cursor() != NUL)
8255 inc_cursor();
8256 break;
8257 }
8258
8259#ifdef FEAT_VIRTUALEDIT
8260 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
8261 {
8262 int save_State = State;
8263
8264 /* Pretent Insert mode here to allow the cursor on the
8265 * character past the end of the line */
8266 State = INSERT;
8267 coladvance(getviscol());
8268 State = save_State;
8269 }
8270#endif
8271
8272 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
8273 }
8274}
8275
8276/*
8277 * Invoke edit() and take care of "restart_edit" and the return value.
8278 */
8279 static void
8280invoke_edit(cap, repl, cmd, startln)
8281 cmdarg_T *cap;
8282 int repl; /* "r" or "gr" command */
8283 int cmd;
8284 int startln;
8285{
8286 int restart_edit_save = 0;
8287
8288 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
8289 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
8290 * it. */
8291 if (repl || !stuff_empty())
8292 restart_edit_save = restart_edit;
8293 else
8294 restart_edit_save = 0;
8295
8296 /* Always reset "restart_edit", this is not a restarted edit. */
8297 restart_edit = 0;
8298
8299 if (edit(cmd, startln, cap->count1))
8300 cap->retval |= CA_COMMAND_BUSY;
8301
8302 if (restart_edit == 0)
8303 restart_edit = restart_edit_save;
8304}
8305
8306#ifdef FEAT_TEXTOBJ
8307/*
8308 * "a" or "i" while an operator is pending or in Visual mode: object motion.
8309 */
8310 static void
8311nv_object(cap)
8312 cmdarg_T *cap;
8313{
8314 int flag;
8315 int include;
8316 char_u *mps_save;
8317
8318 if (cap->cmdchar == 'i')
8319 include = FALSE; /* "ix" = inner object: exclude white space */
8320 else
8321 include = TRUE; /* "ax" = an object: include white space */
8322
8323 /* Make sure (), [], {} and <> are in 'matchpairs' */
8324 mps_save = curbuf->b_p_mps;
8325 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
8326
8327 switch (cap->nchar)
8328 {
8329 case 'w': /* "aw" = a word */
8330 flag = current_word(cap->oap, cap->count1, include, FALSE);
8331 break;
8332 case 'W': /* "aW" = a WORD */
8333 flag = current_word(cap->oap, cap->count1, include, TRUE);
8334 break;
8335 case 'b': /* "ab" = a braces block */
8336 case '(':
8337 case ')':
8338 flag = current_block(cap->oap, cap->count1, include, '(', ')');
8339 break;
8340 case 'B': /* "aB" = a Brackets block */
8341 case '{':
8342 case '}':
8343 flag = current_block(cap->oap, cap->count1, include, '{', '}');
8344 break;
8345 case '[': /* "a[" = a [] block */
8346 case ']':
8347 flag = current_block(cap->oap, cap->count1, include, '[', ']');
8348 break;
8349 case '<': /* "a<" = a <> block */
8350 case '>':
8351 flag = current_block(cap->oap, cap->count1, include, '<', '>');
8352 break;
8353 case 'p': /* "ap" = a paragraph */
8354 flag = current_par(cap->oap, cap->count1, include, 'p');
8355 break;
8356 case 's': /* "as" = a sentence */
8357 flag = current_sent(cap->oap, cap->count1, include);
8358 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00008359 case '"': /* "a"" = a double quoted string */
8360 case '\'': /* "a'" = a single quoted string */
8361 case '`': /* "a`" = a backtick quoted string */
8362 flag = current_quote(cap->oap, cap->count1, include,
8363 cap->nchar);
8364 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365#if 0 /* TODO */
8366 case 'S': /* "aS" = a section */
8367 case 'f': /* "af" = a filename */
8368 case 'u': /* "au" = a URL */
8369#endif
8370 default:
8371 flag = FAIL;
8372 break;
8373 }
8374
8375 curbuf->b_p_mps = mps_save;
8376 if (flag == FAIL)
8377 clearopbeep(cap->oap);
8378 adjust_cursor_col();
8379 curwin->w_set_curswant = TRUE;
8380}
8381#endif
8382
8383/*
8384 * "q" command: Start/stop recording.
8385 * "q:", "q/", "q?": edit command-line in command-line window.
8386 */
8387 static void
8388nv_record(cap)
8389 cmdarg_T *cap;
8390{
8391 if (cap->oap->op_type == OP_FORMAT)
8392 {
8393 /* "gqq" is the same as "gqgq": format line */
8394 cap->cmdchar = 'g';
8395 cap->nchar = 'q';
8396 nv_operator(cap);
8397 }
8398 else if (!checkclearop(cap->oap))
8399 {
8400#ifdef FEAT_CMDWIN
8401 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
8402 {
8403 stuffcharReadbuff(cap->nchar);
8404 stuffcharReadbuff(K_CMDWIN);
8405 }
8406 else
8407#endif
8408 /* (stop) recording into a named register, unless executing a
8409 * register */
8410 if (!Exec_reg && do_record(cap->nchar) == FAIL)
8411 clearopbeep(cap->oap);
8412 }
8413}
8414
8415/*
8416 * Handle the "@r" command.
8417 */
8418 static void
8419nv_at(cap)
8420 cmdarg_T *cap;
8421{
8422 if (checkclearop(cap->oap))
8423 return;
8424#ifdef FEAT_EVAL
8425 if (cap->nchar == '=')
8426 {
8427 if (get_expr_register() == NUL)
8428 return;
8429 }
8430#endif
8431 while (cap->count1-- && !got_int)
8432 {
8433 if (do_execreg(cap->nchar, FALSE, FALSE) == FAIL)
8434 {
8435 clearopbeep(cap->oap);
8436 break;
8437 }
8438 line_breakcheck();
8439 }
8440}
8441
8442/*
8443 * Handle the CTRL-U and CTRL-D commands.
8444 */
8445 static void
8446nv_halfpage(cap)
8447 cmdarg_T *cap;
8448{
8449 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
8450 || (cap->cmdchar == Ctrl_D
8451 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
8452 clearopbeep(cap->oap);
8453 else if (!checkclearop(cap->oap))
8454 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
8455}
8456
8457/*
8458 * Handle "J" or "gJ" command.
8459 */
8460 static void
8461nv_join(cap)
8462 cmdarg_T *cap;
8463{
8464#ifdef FEAT_VISUAL
8465 if (VIsual_active) /* join the visual lines */
8466 nv_operator(cap);
8467 else
8468#endif
8469 if (!checkclearop(cap->oap))
8470 {
8471 if (cap->count0 <= 1)
8472 cap->count0 = 2; /* default for join is two lines! */
8473 if (curwin->w_cursor.lnum + cap->count0 - 1 >
8474 curbuf->b_ml.ml_line_count)
8475 clearopbeep(cap->oap); /* beyond last line */
8476 else
8477 {
8478 prep_redo(cap->oap->regname, cap->count0,
8479 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
8480 do_do_join(cap->count0, cap->nchar == NUL);
8481 }
8482 }
8483}
8484
8485/*
8486 * "P", "gP", "p" and "gp" commands.
8487 */
8488 static void
8489nv_put(cap)
8490 cmdarg_T *cap;
8491{
8492#ifdef FEAT_VISUAL
8493 int regname = 0;
8494 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008495 int empty = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496#endif
8497 int dir;
8498 int flags = 0;
8499
8500 if (cap->oap->op_type != OP_NOP)
8501 {
8502#ifdef FEAT_DIFF
8503 /* "dp" is ":diffput" */
8504 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
8505 {
8506 clearop(cap->oap);
8507 nv_diffgetput(TRUE);
8508 }
8509 else
8510#endif
8511 clearopbeep(cap->oap);
8512 }
8513 else
8514 {
8515 dir = (cap->cmdchar == 'P'
8516 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
8517 ? BACKWARD : FORWARD;
8518 prep_redo_cmd(cap);
8519 if (cap->cmdchar == 'g')
8520 flags |= PUT_CURSEND;
8521
8522#ifdef FEAT_VISUAL
8523 if (VIsual_active)
8524 {
8525 /* Putting in Visual mode: The put text replaces the selected
8526 * text. First delete the selected text, then put the new text.
8527 * Need to save and restore the registers that the delete
8528 * overwrites if the old contents is being put.
8529 */
8530 regname = cap->oap->regname;
8531# ifdef FEAT_CLIPBOARD
8532 adjust_clip_reg(&regname);
8533# endif
8534 if (regname == 0 || VIM_ISDIGIT(regname)
8535# ifdef FEAT_CLIPBOARD
8536 || (clip_unnamed && (regname == '*' || regname == '+'))
8537# endif
8538
8539 )
8540 {
8541 /* the delete is going to overwrite the register we want to
8542 * put, save it first. */
8543 reg1 = get_register(regname, TRUE);
8544 }
8545
8546 /* Now delete the selected text. */
8547 cap->cmdchar = 'd';
8548 cap->nchar = NUL;
8549 cap->oap->regname = NUL;
8550 nv_operator(cap);
8551 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008552 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553
8554 /* delete PUT_LINE_BACKWARD; */
8555 cap->oap->regname = regname;
8556
8557 if (reg1 != NULL)
8558 {
8559 /* Delete probably changed the register we want to put, save
8560 * it first. Then put back what was there before the delete. */
8561 reg2 = get_register(regname, FALSE);
8562 put_register(regname, reg1);
8563 }
8564
8565 /* When deleted a linewise Visual area, put the register as
8566 * lines to avoid it joined with the next line. When deletion was
8567 * characterwise, split a line when putting lines. */
8568 if (VIsual_mode == 'V')
8569 flags |= PUT_LINE;
8570 else if (VIsual_mode == 'v')
8571 flags |= PUT_LINE_SPLIT;
8572 if (VIsual_mode == Ctrl_V && dir == FORWARD)
8573 flags |= PUT_LINE_FORWARD;
8574 dir = BACKWARD;
8575 if ((VIsual_mode != 'V'
8576 && curwin->w_cursor.col < curbuf->b_op_start.col)
8577 || (VIsual_mode == 'V'
8578 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
8579 /* cursor is at the end of the line or end of file, put
8580 * forward. */
8581 dir = FORWARD;
8582 }
8583#endif
8584 do_put(cap->oap->regname, dir, cap->count1, flags);
8585
8586#ifdef FEAT_VISUAL
8587 /* If a register was saved, put it back now. */
8588 if (reg2 != NULL)
8589 put_register(regname, reg2);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008590 /* When all lines were selected and deleted do_put() leaves an empty
8591 * line that needs to delete now. */
8592 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
8593 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594#endif
8595 auto_format(FALSE, TRUE);
8596 }
8597}
8598
8599/*
8600 * "o" and "O" commands.
8601 */
8602 static void
8603nv_open(cap)
8604 cmdarg_T *cap;
8605{
8606#ifdef FEAT_DIFF
8607 /* "do" is ":diffget" */
8608 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
8609 {
8610 clearop(cap->oap);
8611 nv_diffgetput(FALSE);
8612 }
8613 else
8614#endif
8615#ifdef FEAT_VISUAL
8616 if (VIsual_active) /* switch start and end of visual */
8617 v_swap_corners(cap->cmdchar);
8618 else
8619#endif
8620 n_opencmd(cap);
8621}
8622
8623#ifdef FEAT_SNIFF
8624/*ARGSUSED*/
8625 static void
8626nv_sniff(cap)
8627 cmdarg_T *cap;
8628{
8629 ProcessSniffRequests();
8630}
8631#endif
8632
8633#ifdef FEAT_NETBEANS_INTG
8634 static void
8635nv_nbcmd(cap)
8636 cmdarg_T *cap;
8637{
8638 netbeans_keycommand(cap->nchar);
8639}
8640#endif
8641
8642#ifdef FEAT_DND
8643/*ARGSUSED*/
8644 static void
8645nv_drop(cap)
8646 cmdarg_T *cap;
8647{
8648 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8649}
8650#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00008651
8652#ifdef FEAT_AUTOCMD
8653/*
8654 * Trigger CursorHold event.
8655 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
8656 * input buffer. "did_cursorhold" is set to avoid retriggering.
8657 */
8658/*ARGSUSED*/
8659 static void
8660nv_cursorhold(cap)
8661 cmdarg_T *cap;
8662{
8663 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
8664 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00008665 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00008666}
8667#endif
8668