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