blob: fe306675b0a3e95d718c0c76b6d2c56666740fb5 [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
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010028#ifdef FEAT_EVAL
29static void set_vcount_ca __ARGS((cmdarg_T *cap, int *set_prevcount));
30#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000031static int
32# ifdef __BORLANDC__
33_RTLENTRYF
34# endif
35 nv_compare __ARGS((const void *s1, const void *s2));
36static int find_command __ARGS((int cmdchar));
37static void op_colon __ARGS((oparg_T *oap));
Bram Moolenaar5b962cf2005-12-12 21:58:40 +000038static void op_function __ARGS((oparg_T *oap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#if defined(FEAT_MOUSE) && defined(FEAT_VISUAL)
40static void find_start_of_word __ARGS((pos_T *));
41static void find_end_of_word __ARGS((pos_T *));
42static int get_mouse_class __ARGS((char_u *p));
43#endif
44static void prep_redo_cmd __ARGS((cmdarg_T *cap));
45static void prep_redo __ARGS((int regname, long, int, int, int, int, int));
46static int checkclearop __ARGS((oparg_T *oap));
47static int checkclearopq __ARGS((oparg_T *oap));
48static void clearop __ARGS((oparg_T *oap));
49static void clearopbeep __ARGS((oparg_T *oap));
50#ifdef FEAT_VISUAL
51static void unshift_special __ARGS((cmdarg_T *cap));
52#endif
53#ifdef FEAT_CMDL_INFO
54static void del_from_showcmd __ARGS((int));
55#endif
56
57/*
58 * nv_*(): functions called to handle Normal and Visual mode commands.
59 * n_*(): functions called to handle Normal mode commands.
60 * v_*(): functions called to handle Visual mode commands.
61 */
62static void nv_ignore __ARGS((cmdarg_T *cap));
Bram Moolenaarebefac62005-12-28 22:39:57 +000063static void nv_nop __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000064static void nv_error __ARGS((cmdarg_T *cap));
65static void nv_help __ARGS((cmdarg_T *cap));
66static void nv_addsub __ARGS((cmdarg_T *cap));
67static void nv_page __ARGS((cmdarg_T *cap));
Bram Moolenaarf75a9632005-09-13 21:20:47 +000068static void nv_gd __ARGS((oparg_T *oap, int nchar, int thisblock));
Bram Moolenaar071d4272004-06-13 20:20:40 +000069static int nv_screengo __ARGS((oparg_T *oap, int dir, long dist));
70#ifdef FEAT_MOUSE
71static void nv_mousescroll __ARGS((cmdarg_T *cap));
72static void nv_mouse __ARGS((cmdarg_T *cap));
73#endif
74static void nv_scroll_line __ARGS((cmdarg_T *cap));
75static void nv_zet __ARGS((cmdarg_T *cap));
76#ifdef FEAT_GUI
77static void nv_ver_scrollbar __ARGS((cmdarg_T *cap));
78static void nv_hor_scrollbar __ARGS((cmdarg_T *cap));
79#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000080#ifdef FEAT_GUI_TABLINE
81static void nv_tabline __ARGS((cmdarg_T *cap));
Bram Moolenaarba6c0522006-02-25 21:45:02 +000082static void nv_tabmenu __ARGS((cmdarg_T *cap));
Bram Moolenaar32466aa2006-02-24 23:53:04 +000083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000084static void nv_exmode __ARGS((cmdarg_T *cap));
85static void nv_colon __ARGS((cmdarg_T *cap));
86static void nv_ctrlg __ARGS((cmdarg_T *cap));
87static void nv_ctrlh __ARGS((cmdarg_T *cap));
88static void nv_clear __ARGS((cmdarg_T *cap));
89static void nv_ctrlo __ARGS((cmdarg_T *cap));
90static void nv_hat __ARGS((cmdarg_T *cap));
91static void nv_Zet __ARGS((cmdarg_T *cap));
92static void nv_ident __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000093static void nv_tagpop __ARGS((cmdarg_T *cap));
94static void nv_scroll __ARGS((cmdarg_T *cap));
95static void nv_right __ARGS((cmdarg_T *cap));
96static void nv_left __ARGS((cmdarg_T *cap));
97static void nv_up __ARGS((cmdarg_T *cap));
98static void nv_down __ARGS((cmdarg_T *cap));
99#ifdef FEAT_SEARCHPATH
100static void nv_gotofile __ARGS((cmdarg_T *cap));
101#endif
102static void nv_end __ARGS((cmdarg_T *cap));
103static void nv_dollar __ARGS((cmdarg_T *cap));
104static void nv_search __ARGS((cmdarg_T *cap));
105static void nv_next __ARGS((cmdarg_T *cap));
106static void normal_search __ARGS((cmdarg_T *cap, int dir, char_u *pat, int opt));
107static void nv_csearch __ARGS((cmdarg_T *cap));
108static void nv_brackets __ARGS((cmdarg_T *cap));
109static void nv_percent __ARGS((cmdarg_T *cap));
110static void nv_brace __ARGS((cmdarg_T *cap));
111static void nv_mark __ARGS((cmdarg_T *cap));
112static void nv_findpar __ARGS((cmdarg_T *cap));
113static void nv_undo __ARGS((cmdarg_T *cap));
114static void nv_kundo __ARGS((cmdarg_T *cap));
115static void nv_Replace __ARGS((cmdarg_T *cap));
116#ifdef FEAT_VREPLACE
117static void nv_vreplace __ARGS((cmdarg_T *cap));
118#endif
119#ifdef FEAT_VISUAL
120static void v_swap_corners __ARGS((int cmdchar));
121#endif
122static void nv_replace __ARGS((cmdarg_T *cap));
123static void n_swapchar __ARGS((cmdarg_T *cap));
124static void nv_cursormark __ARGS((cmdarg_T *cap, int flag, pos_T *pos));
125#ifdef FEAT_VISUAL
126static void v_visop __ARGS((cmdarg_T *cap));
127#endif
128static void nv_subst __ARGS((cmdarg_T *cap));
129static void nv_abbrev __ARGS((cmdarg_T *cap));
130static void nv_optrans __ARGS((cmdarg_T *cap));
131static void nv_gomark __ARGS((cmdarg_T *cap));
132static void nv_pcmark __ARGS((cmdarg_T *cap));
133static void nv_regname __ARGS((cmdarg_T *cap));
134#ifdef FEAT_VISUAL
135static void nv_visual __ARGS((cmdarg_T *cap));
136static void n_start_visual_mode __ARGS((int c));
137#endif
138static void nv_window __ARGS((cmdarg_T *cap));
139static void nv_suspend __ARGS((cmdarg_T *cap));
140static void nv_g_cmd __ARGS((cmdarg_T *cap));
141static void n_opencmd __ARGS((cmdarg_T *cap));
142static void nv_dot __ARGS((cmdarg_T *cap));
143static void nv_redo __ARGS((cmdarg_T *cap));
144static void nv_Undo __ARGS((cmdarg_T *cap));
145static void nv_tilde __ARGS((cmdarg_T *cap));
146static void nv_operator __ARGS((cmdarg_T *cap));
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000147#ifdef FEAT_EVAL
148static void set_op_var __ARGS((int optype));
149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150static void nv_lineop __ARGS((cmdarg_T *cap));
151static void nv_home __ARGS((cmdarg_T *cap));
152static void nv_pipe __ARGS((cmdarg_T *cap));
153static void nv_bck_word __ARGS((cmdarg_T *cap));
154static void nv_wordcmd __ARGS((cmdarg_T *cap));
155static void nv_beginline __ARGS((cmdarg_T *cap));
Bram Moolenaar1f14d572008-01-12 16:12:10 +0000156static void adjust_cursor __ARGS((oparg_T *oap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#ifdef FEAT_VISUAL
158static void adjust_for_sel __ARGS((cmdarg_T *cap));
159static int unadjust_for_sel __ARGS((void));
160static void nv_select __ARGS((cmdarg_T *cap));
161#endif
162static void nv_goto __ARGS((cmdarg_T *cap));
163static void nv_normal __ARGS((cmdarg_T *cap));
164static void nv_esc __ARGS((cmdarg_T *oap));
165static void nv_edit __ARGS((cmdarg_T *cap));
166static void invoke_edit __ARGS((cmdarg_T *cap, int repl, int cmd, int startln));
167#ifdef FEAT_TEXTOBJ
168static void nv_object __ARGS((cmdarg_T *cap));
169#endif
170static void nv_record __ARGS((cmdarg_T *cap));
171static void nv_at __ARGS((cmdarg_T *cap));
172static void nv_halfpage __ARGS((cmdarg_T *cap));
173static void nv_join __ARGS((cmdarg_T *cap));
174static void nv_put __ARGS((cmdarg_T *cap));
175static void nv_open __ARGS((cmdarg_T *cap));
176#ifdef FEAT_SNIFF
177static void nv_sniff __ARGS((cmdarg_T *cap));
178#endif
179#ifdef FEAT_NETBEANS_INTG
180static void nv_nbcmd __ARGS((cmdarg_T *cap));
181#endif
182#ifdef FEAT_DND
183static void nv_drop __ARGS((cmdarg_T *cap));
184#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000185#ifdef FEAT_AUTOCMD
186static void nv_cursorhold __ARGS((cmdarg_T *cap));
187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000189static char *e_noident = N_("E349: No identifier under cursor");
190
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191/*
192 * Function to be called for a Normal or Visual mode command.
193 * The argument is a cmdarg_T.
194 */
195typedef void (*nv_func_T) __ARGS((cmdarg_T *cap));
196
197/* Values for cmd_flags. */
198#define NV_NCH 0x01 /* may need to get a second char */
199#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
200#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
201#define NV_LANG 0x08 /* second char needs language adjustment */
202
203#define NV_SS 0x10 /* may start selection */
204#define NV_SSS 0x20 /* may start selection with shift modifier */
205#define NV_STS 0x40 /* may stop selection without shift modif. */
206#define NV_RL 0x80 /* 'rightleft' modifies command */
207#define NV_KEEPREG 0x100 /* don't clear regname */
208#define NV_NCW 0x200 /* not allowed in command-line window */
209
210/*
211 * Generally speaking, every Normal mode command should either clear any
212 * pending operator (with *clearop*()), or set the motion type variable
213 * oap->motion_type.
214 *
215 * When a cursor motion command is made, it is marked as being a character or
216 * line oriented motion. Then, if an operator is in effect, the operation
217 * becomes character or line oriented accordingly.
218 */
219
220/*
221 * This table contains one entry for every Normal or Visual mode command.
222 * The order doesn't matter, init_normal_cmds() will create a sorted index.
223 * It is faster when all keys from zero to '~' are present.
224 */
225static const struct nv_cmd
226{
227 int cmd_char; /* (first) command character */
228 nv_func_T cmd_func; /* function for this command */
229 short_u cmd_flags; /* NV_ flags */
230 short cmd_arg; /* value for ca.arg */
231} nv_cmds[] =
232{
233 {NUL, nv_error, 0, 0},
234 {Ctrl_A, nv_addsub, 0, 0},
235 {Ctrl_B, nv_page, NV_STS, BACKWARD},
236 {Ctrl_C, nv_esc, 0, TRUE},
237 {Ctrl_D, nv_halfpage, 0, 0},
238 {Ctrl_E, nv_scroll_line, 0, TRUE},
239 {Ctrl_F, nv_page, NV_STS, FORWARD},
240 {Ctrl_G, nv_ctrlg, 0, 0},
241 {Ctrl_H, nv_ctrlh, 0, 0},
242 {Ctrl_I, nv_pcmark, 0, 0},
243 {NL, nv_down, 0, FALSE},
244 {Ctrl_K, nv_error, 0, 0},
245 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000246 {Ctrl_M, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 {Ctrl_N, nv_down, NV_STS, FALSE},
248 {Ctrl_O, nv_ctrlo, 0, 0},
249 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000250#ifdef FEAT_VISUAL
251 {Ctrl_Q, nv_visual, 0, FALSE},
252#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 {Ctrl_Q, nv_ignore, 0, 0},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 {Ctrl_R, nv_redo, 0, 0},
256 {Ctrl_S, nv_ignore, 0, 0},
257 {Ctrl_T, nv_tagpop, NV_NCW, 0},
258 {Ctrl_U, nv_halfpage, 0, 0},
259#ifdef FEAT_VISUAL
260 {Ctrl_V, nv_visual, 0, FALSE},
261 {'V', nv_visual, 0, FALSE},
262 {'v', nv_visual, 0, FALSE},
263#else
264 {Ctrl_V, nv_error, 0, 0},
265 {'V', nv_error, 0, 0},
266 {'v', nv_error, 0, 0},
267#endif
268 {Ctrl_W, nv_window, 0, 0},
269 {Ctrl_X, nv_addsub, 0, 0},
270 {Ctrl_Y, nv_scroll_line, 0, FALSE},
271 {Ctrl_Z, nv_suspend, 0, 0},
272 {ESC, nv_esc, 0, FALSE},
273 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
274 {Ctrl_RSB, nv_ident, NV_NCW, 0},
275 {Ctrl_HAT, nv_hat, NV_NCW, 0},
276 {Ctrl__, nv_error, 0, 0},
277 {' ', nv_right, 0, 0},
278 {'!', nv_operator, 0, 0},
279 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
280 {'#', nv_ident, 0, 0},
281 {'$', nv_dollar, 0, 0},
282 {'%', nv_percent, 0, 0},
283 {'&', nv_optrans, 0, 0},
284 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
285 {'(', nv_brace, 0, BACKWARD},
286 {')', nv_brace, 0, FORWARD},
287 {'*', nv_ident, 0, 0},
288 {'+', nv_down, 0, TRUE},
289 {',', nv_csearch, 0, TRUE},
290 {'-', nv_up, 0, TRUE},
291 {'.', nv_dot, NV_KEEPREG, 0},
292 {'/', nv_search, 0, FALSE},
293 {'0', nv_beginline, 0, 0},
294 {'1', nv_ignore, 0, 0},
295 {'2', nv_ignore, 0, 0},
296 {'3', nv_ignore, 0, 0},
297 {'4', nv_ignore, 0, 0},
298 {'5', nv_ignore, 0, 0},
299 {'6', nv_ignore, 0, 0},
300 {'7', nv_ignore, 0, 0},
301 {'8', nv_ignore, 0, 0},
302 {'9', nv_ignore, 0, 0},
303 {':', nv_colon, 0, 0},
304 {';', nv_csearch, 0, FALSE},
305 {'<', nv_operator, NV_RL, 0},
306 {'=', nv_operator, 0, 0},
307 {'>', nv_operator, NV_RL, 0},
308 {'?', nv_search, 0, FALSE},
309 {'@', nv_at, NV_NCH_NOP, FALSE},
310 {'A', nv_edit, 0, 0},
311 {'B', nv_bck_word, 0, 1},
312 {'C', nv_abbrev, NV_KEEPREG, 0},
313 {'D', nv_abbrev, NV_KEEPREG, 0},
314 {'E', nv_wordcmd, 0, TRUE},
315 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
316 {'G', nv_goto, 0, TRUE},
317 {'H', nv_scroll, 0, 0},
318 {'I', nv_edit, 0, 0},
319 {'J', nv_join, 0, 0},
320 {'K', nv_ident, 0, 0},
321 {'L', nv_scroll, 0, 0},
322 {'M', nv_scroll, 0, 0},
323 {'N', nv_next, 0, SEARCH_REV},
324 {'O', nv_open, 0, 0},
325 {'P', nv_put, 0, 0},
326 {'Q', nv_exmode, NV_NCW, 0},
327 {'R', nv_Replace, 0, FALSE},
328 {'S', nv_subst, NV_KEEPREG, 0},
329 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
330 {'U', nv_Undo, 0, 0},
331 {'W', nv_wordcmd, 0, TRUE},
332 {'X', nv_abbrev, NV_KEEPREG, 0},
333 {'Y', nv_abbrev, NV_KEEPREG, 0},
334 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
335 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
336 {'\\', nv_error, 0, 0},
337 {']', nv_brackets, NV_NCH_ALW, FORWARD},
338 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
339 {'_', nv_lineop, 0, 0},
340 {'`', nv_gomark, NV_NCH_ALW, FALSE},
341 {'a', nv_edit, NV_NCH, 0},
342 {'b', nv_bck_word, 0, 0},
343 {'c', nv_operator, 0, 0},
344 {'d', nv_operator, 0, 0},
345 {'e', nv_wordcmd, 0, FALSE},
346 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
347 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
348 {'h', nv_left, NV_RL, 0},
349 {'i', nv_edit, NV_NCH, 0},
350 {'j', nv_down, 0, FALSE},
351 {'k', nv_up, 0, FALSE},
352 {'l', nv_right, NV_RL, 0},
353 {'m', nv_mark, NV_NCH_NOP, 0},
354 {'n', nv_next, 0, 0},
355 {'o', nv_open, 0, 0},
356 {'p', nv_put, 0, 0},
357 {'q', nv_record, NV_NCH, 0},
358 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
359 {'s', nv_subst, NV_KEEPREG, 0},
360 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
361 {'u', nv_undo, 0, 0},
362 {'w', nv_wordcmd, 0, FALSE},
363 {'x', nv_abbrev, NV_KEEPREG, 0},
364 {'y', nv_operator, 0, 0},
365 {'z', nv_zet, NV_NCH_ALW, 0},
366 {'{', nv_findpar, 0, BACKWARD},
367 {'|', nv_pipe, 0, 0},
368 {'}', nv_findpar, 0, FORWARD},
369 {'~', nv_tilde, 0, 0},
370
371 /* pound sign */
372 {POUND, nv_ident, 0, 0},
373#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200374 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
375 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
376 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
377 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 {K_LEFTMOUSE, nv_mouse, 0, 0},
379 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
380 {K_LEFTDRAG, nv_mouse, 0, 0},
381 {K_LEFTRELEASE, nv_mouse, 0, 0},
382 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
383 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
384 {K_MIDDLEDRAG, nv_mouse, 0, 0},
385 {K_MIDDLERELEASE, nv_mouse, 0, 0},
386 {K_RIGHTMOUSE, nv_mouse, 0, 0},
387 {K_RIGHTDRAG, nv_mouse, 0, 0},
388 {K_RIGHTRELEASE, nv_mouse, 0, 0},
389 {K_X1MOUSE, nv_mouse, 0, 0},
390 {K_X1DRAG, nv_mouse, 0, 0},
391 {K_X1RELEASE, nv_mouse, 0, 0},
392 {K_X2MOUSE, nv_mouse, 0, 0},
393 {K_X2DRAG, nv_mouse, 0, 0},
394 {K_X2RELEASE, nv_mouse, 0, 0},
395#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000396 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000397 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 {K_INS, nv_edit, 0, 0},
399 {K_KINS, nv_edit, 0, 0},
400 {K_BS, nv_ctrlh, 0, 0},
401 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
402 {K_S_UP, nv_page, NV_SS, BACKWARD},
403 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
404 {K_S_DOWN, nv_page, NV_SS, FORWARD},
405 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
406 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
407 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
408 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
409 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
410 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
411 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
412 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
413 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
414 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
415 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
416 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 {K_S_END, nv_end, NV_SS, FALSE},
418 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
419 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
420 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 {K_S_HOME, nv_home, NV_SS, 0},
422 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
423 {K_DEL, nv_abbrev, 0, 0},
424 {K_KDEL, nv_abbrev, 0, 0},
425 {K_UNDO, nv_kundo, 0, 0},
426 {K_HELP, nv_help, NV_NCW, 0},
427 {K_F1, nv_help, NV_NCW, 0},
428 {K_XF1, nv_help, NV_NCW, 0},
429#ifdef FEAT_VISUAL
430 {K_SELECT, nv_select, 0, 0},
431#endif
432#ifdef FEAT_GUI
433 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
434 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
435#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000436#ifdef FEAT_GUI_TABLINE
437 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000438 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440#ifdef FEAT_FKMAP
441 {K_F8, farsi_fkey, 0, 0},
442 {K_F9, farsi_fkey, 0, 0},
443#endif
444#ifdef FEAT_SNIFF
445 {K_SNIFF, nv_sniff, 0, 0},
446#endif
447#ifdef FEAT_NETBEANS_INTG
448 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
449#endif
450#ifdef FEAT_DND
451 {K_DROP, nv_drop, NV_STS, 0},
452#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000453#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000454 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaar3918c952005-03-15 22:34:55 +0000455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456};
457
458/* Number of commands in nv_cmds[]. */
459#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
460
461/* Sorted index of commands in nv_cmds[]. */
462static short nv_cmd_idx[NV_CMDS_SIZE];
463
464/* The highest index for which
465 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
466static int nv_max_linear;
467
468/*
469 * Compare functions for qsort() below, that checks the command character
470 * through the index in nv_cmd_idx[].
471 */
472 static int
473#ifdef __BORLANDC__
474_RTLENTRYF
475#endif
476nv_compare(s1, s2)
477 const void *s1;
478 const void *s2;
479{
480 int c1, c2;
481
482 /* The commands are sorted on absolute value. */
483 c1 = nv_cmds[*(const short *)s1].cmd_char;
484 c2 = nv_cmds[*(const short *)s2].cmd_char;
485 if (c1 < 0)
486 c1 = -c1;
487 if (c2 < 0)
488 c2 = -c2;
489 return c1 - c2;
490}
491
492/*
493 * Initialize the nv_cmd_idx[] table.
494 */
495 void
496init_normal_cmds()
497{
498 int i;
499
500 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000501 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 nv_cmd_idx[i] = i;
503
504 /* Sort the commands by the command character. */
505 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
506
507 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000508 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
510 break;
511 nv_max_linear = i - 1;
512}
513
514/*
515 * Search for a command in the commands table.
516 * Returns -1 for invalid command.
517 */
518 static int
519find_command(cmdchar)
520 int cmdchar;
521{
522 int i;
523 int idx;
524 int top, bot;
525 int c;
526
527#ifdef FEAT_MBYTE
528 /* A multi-byte character is never a command. */
529 if (cmdchar >= 0x100)
530 return -1;
531#endif
532
533 /* We use the absolute value of the character. Special keys have a
534 * negative value, but are sorted on their absolute value. */
535 if (cmdchar < 0)
536 cmdchar = -cmdchar;
537
538 /* If the character is in the first part: The character is the index into
539 * nv_cmd_idx[]. */
540 if (cmdchar <= nv_max_linear)
541 return nv_cmd_idx[cmdchar];
542
543 /* Perform a binary search. */
544 bot = nv_max_linear + 1;
545 top = NV_CMDS_SIZE - 1;
546 idx = -1;
547 while (bot <= top)
548 {
549 i = (top + bot) / 2;
550 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
551 if (c < 0)
552 c = -c;
553 if (cmdchar == c)
554 {
555 idx = nv_cmd_idx[i];
556 break;
557 }
558 if (cmdchar > c)
559 bot = i + 1;
560 else
561 top = i - 1;
562 }
563 return idx;
564}
565
566/*
567 * Execute a command in Normal mode.
568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 void
570normal_cmd(oap, toplevel)
571 oparg_T *oap;
Bram Moolenaar78a15312009-05-15 19:33:18 +0000572 int toplevel UNUSED; /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 cmdarg_T ca; /* command arguments */
575 int c;
576 int ctrl_w = FALSE; /* got CTRL-W command */
577 int old_col = curwin->w_curswant;
578#ifdef FEAT_CMDL_INFO
579 int need_flushbuf; /* need to call out_flush() */
580#endif
581#ifdef FEAT_VISUAL
582 pos_T old_pos; /* cursor position before command */
583 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 static int old_mapped_len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000587#ifdef FEAT_EVAL
588 int set_prevcount = FALSE;
589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590
591 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
592 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000593
594 /* Use a count remembered from before entering an operator. After typing
595 * "3d" we return from normal_cmd() and come back here, the "3" is
596 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 ca.opcount = opcount;
598
599#ifdef FEAT_SNIFF
600 want_sniff_request = sniff_connected;
601#endif
602
603 /*
604 * If there is an operator pending, then the command we take this time
605 * will terminate it. Finish_op tells us to finish the operation before
606 * returning this time (unless the operation was cancelled).
607 */
608#ifdef CURSOR_SHAPE
609 c = finish_op;
610#endif
611 finish_op = (oap->op_type != OP_NOP);
612#ifdef CURSOR_SHAPE
613 if (finish_op != c)
614 {
615 ui_cursor_shape(); /* may show different cursor shape */
616# ifdef FEAT_MOUSESHAPE
617 update_mouseshape(-1);
618# endif
619 }
620#endif
621
Bram Moolenaara983fe92008-07-31 20:04:27 +0000622 /* When not finishing an operator and no register name typed, reset the
623 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000625 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000627#ifdef FEAT_EVAL
628 set_prevcount = TRUE;
629#endif
630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
Bram Moolenaara983fe92008-07-31 20:04:27 +0000632#ifdef FEAT_AUTOCMD
633 /* Restore counts from before receiving K_CURSORHOLD. This means after
634 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
635 * "3 * 2". */
636 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
637 {
638 ca.opcount = oap->prev_opcount;
639 ca.count0 = oap->prev_count0;
640 oap->prev_opcount = 0;
641 oap->prev_count0 = 0;
642 }
643#endif
644
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645#ifdef FEAT_VISUAL
646 mapped_len = typebuf_maplen();
647#endif
648
649 State = NORMAL_BUSY;
650#ifdef USE_ON_FLY_SCROLL
651 dont_scroll = FALSE; /* allow scrolling here */
652#endif
653
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100654#ifdef FEAT_EVAL
655 /* Set v:count here, when called from main() and not a stuffed
656 * command, so that v:count can be used in an expression mapping
657 * when there is no count. */
658 if (toplevel && stuff_empty())
659 set_vcount_ca(&ca, &set_prevcount);
660#endif
661
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 /*
663 * Get the command character from the user.
664 */
665 c = safe_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000668#ifdef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 /*
670 * If a mapping was started in Visual or Select mode, remember the length
671 * of the mapping. This is used below to not return to Insert mode for as
672 * long as the mapping is being executed.
673 */
674 if (restart_edit == 0)
675 old_mapped_len = 0;
676 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000677 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 old_mapped_len = typebuf_maplen();
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680
681 if (c == NUL)
682 c = K_ZERO;
683
684#ifdef FEAT_VISUAL
685 /*
686 * In Select mode, typed text replaces the selection.
687 */
688 if (VIsual_active
689 && VIsual_select
690 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
691 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000692 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
693 * 'insertmode' is set) fake a "d"elete command, Insert mode will
694 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000695 * Insert the typed character in the typeahead buffer, so that it can
696 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000697 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000698 if (restart_edit != 0)
699 c = 'd';
700 else
701 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000702 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 }
704#endif
705
706#ifdef FEAT_CMDL_INFO
707 need_flushbuf = add_to_showcmd(c);
708#endif
709
710getcount:
711#ifdef FEAT_VISUAL
712 if (!(VIsual_active && VIsual_select))
713#endif
714 {
715 /*
716 * Handle a count before a command and compute ca.count0.
717 * Note that '0' is a command and not the start of a count, but it's
718 * part of a count after other digits.
719 */
720 while ( (c >= '1' && c <= '9')
721 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
722 {
723 if (c == K_DEL || c == K_KDEL)
724 {
725 ca.count0 /= 10;
726#ifdef FEAT_CMDL_INFO
727 del_from_showcmd(4); /* delete the digit and ~@% */
728#endif
729 }
730 else
731 ca.count0 = ca.count0 * 10 + (c - '0');
732 if (ca.count0 < 0) /* got too large! */
733 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000734#ifdef FEAT_EVAL
735 /* Set v:count here, when called from main() and not a stuffed
736 * command, so that v:count can be used in an expression mapping
737 * right after the count. */
738 if (toplevel && stuff_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100739 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 if (ctrl_w)
742 {
743 ++no_mapping;
744 ++allow_keys; /* no mapping for nchar, but keys */
745 }
746 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000747 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 --no_zero_mapping;
750 if (ctrl_w)
751 {
752 --no_mapping;
753 --allow_keys;
754 }
755#ifdef FEAT_CMDL_INFO
756 need_flushbuf |= add_to_showcmd(c);
757#endif
758 }
759
760 /*
761 * If we got CTRL-W there may be a/another count
762 */
763 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
764 {
765 ctrl_w = TRUE;
766 ca.opcount = ca.count0; /* remember first count */
767 ca.count0 = 0;
768 ++no_mapping;
769 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000770 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 --no_mapping;
773 --allow_keys;
774#ifdef FEAT_CMDL_INFO
775 need_flushbuf |= add_to_showcmd(c);
776#endif
777 goto getcount; /* jump back */
778 }
779 }
780
Bram Moolenaara983fe92008-07-31 20:04:27 +0000781#ifdef FEAT_AUTOCMD
782 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000784 /* Save the count values so that ca.opcount and ca.count0 are exactly
785 * the same when coming back here after handling K_CURSORHOLD. */
786 oap->prev_opcount = ca.opcount;
787 oap->prev_count0 = ca.count0;
788 }
789 else
790#endif
791 if (ca.opcount != 0)
792 {
793 /*
794 * If we're in the middle of an operator (including after entering a
795 * yank buffer with '"') AND we had a count before the operator, then
796 * that count overrides the current value of ca.count0.
797 * What this means effectively, is that commands like "3dw" get turned
798 * into "d3w" which makes things fall into place pretty neatly.
799 * If you give a count before AND after the operator, they are
800 * multiplied.
801 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 if (ca.count0)
803 ca.count0 *= ca.opcount;
804 else
805 ca.count0 = ca.opcount;
806 }
807
808 /*
809 * Always remember the count. It will be set to zero (on the next call,
810 * above) when there is no pending operator.
811 * When called from main(), save the count for use by the "count" built-in
812 * variable.
813 */
814 ca.opcount = ca.count0;
815 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
816
817#ifdef FEAT_EVAL
818 /*
819 * Only set v:count when called from main() and not a stuffed command.
820 */
821 if (toplevel && stuff_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000822 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823#endif
824
825 /*
826 * Find the command character in the table of commands.
827 * For CTRL-W we already got nchar when looking for a count.
828 */
829 if (ctrl_w)
830 {
831 ca.nchar = c;
832 ca.cmdchar = Ctrl_W;
833 }
834 else
835 ca.cmdchar = c;
836 idx = find_command(ca.cmdchar);
837 if (idx < 0)
838 {
839 /* Not a known command: beep. */
840 clearopbeep(oap);
841 goto normal_end;
842 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000843
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000844 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000846 /* This command is not allowed while editing a ccmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000848 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 goto normal_end;
850 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000851#ifdef FEAT_AUTOCMD
852 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
853 goto normal_end;
854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
856#ifdef FEAT_VISUAL
857 /*
858 * In Visual/Select mode, a few keys are handled in a special way.
859 */
860 if (VIsual_active)
861 {
862 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
863 if (km_stopsel
864 && (nv_cmds[idx].cmd_flags & NV_STS)
865 && !(mod_mask & MOD_MASK_SHIFT))
866 {
867 end_visual_mode();
868 redraw_curbuf_later(INVERTED);
869 }
870
871 /* Keys that work different when 'keymodel' contains "startsel" */
872 if (km_startsel)
873 {
874 if (nv_cmds[idx].cmd_flags & NV_SS)
875 {
876 unshift_special(&ca);
877 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000878 if (idx < 0)
879 {
880 /* Just in case */
881 clearopbeep(oap);
882 goto normal_end;
883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 }
885 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
886 && (mod_mask & MOD_MASK_SHIFT))
887 {
888 mod_mask &= ~MOD_MASK_SHIFT;
889 }
890 }
891 }
892#endif
893
894#ifdef FEAT_RIGHTLEFT
895 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
896 && (nv_cmds[idx].cmd_flags & NV_RL))
897 {
898 /* Invert horizontal movements and operations. Only when typed by the
899 * user directly, not when the result of a mapping or "x" translated
900 * to "dl". */
901 switch (ca.cmdchar)
902 {
903 case 'l': ca.cmdchar = 'h'; break;
904 case K_RIGHT: ca.cmdchar = K_LEFT; break;
905 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
906 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
907 case 'h': ca.cmdchar = 'l'; break;
908 case K_LEFT: ca.cmdchar = K_RIGHT; break;
909 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
910 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
911 case '>': ca.cmdchar = '<'; break;
912 case '<': ca.cmdchar = '>'; break;
913 }
914 idx = find_command(ca.cmdchar);
915 }
916#endif
917
918 /*
919 * Get an additional character if we need one.
920 */
921 if ((nv_cmds[idx].cmd_flags & NV_NCH)
922 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
923 && oap->op_type == OP_NOP)
924 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
925 || (ca.cmdchar == 'q'
926 && oap->op_type == OP_NOP
927 && !Recording
928 && !Exec_reg)
929 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
930 && (oap->op_type != OP_NOP
931#ifdef FEAT_VISUAL
932 || VIsual_active
933#endif
934 ))))
935 {
936 int *cp;
937 int repl = FALSE; /* get character for replace mode */
938 int lit = FALSE; /* get extra character literally */
939 int langmap_active = FALSE; /* using :lmap mappings */
940 int lang; /* getting a text character */
941#ifdef USE_IM_CONTROL
942 int save_smd; /* saved value of p_smd */
943#endif
944
945 ++no_mapping;
946 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000947#ifdef FEAT_AUTOCMD
948 /* Don't generate a CursorHold event here, most commands can't handle
949 * it, e.g., nv_replace(), nv_csearch(). */
950 did_cursorhold = TRUE;
951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 if (ca.cmdchar == 'g')
953 {
954 /*
955 * For 'g' get the next character now, so that we can check for
956 * "gr", "g'" and "g`".
957 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000958 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960#ifdef FEAT_CMDL_INFO
961 need_flushbuf |= add_to_showcmd(ca.nchar);
962#endif
963 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
964 || ca.nchar == Ctrl_BSL)
965 {
966 cp = &ca.extra_char; /* need to get a third character */
967 if (ca.nchar != 'r')
968 lit = TRUE; /* get it literally */
969 else
970 repl = TRUE; /* get it in replace mode */
971 }
972 else
973 cp = NULL; /* no third character needed */
974 }
975 else
976 {
977 if (ca.cmdchar == 'r') /* get it in replace mode */
978 repl = TRUE;
979 cp = &ca.nchar;
980 }
981 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
982
983 /*
984 * Get a second or third character.
985 */
986 if (cp != NULL)
987 {
988#ifdef CURSOR_SHAPE
989 if (repl)
990 {
991 State = REPLACE; /* pretend Replace mode */
992 ui_cursor_shape(); /* show different cursor shape */
993 }
994#endif
995 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
996 {
997 /* Allow mappings defined with ":lmap". */
998 --no_mapping;
999 --allow_keys;
1000 if (repl)
1001 State = LREPLACE;
1002 else
1003 State = LANGMAP;
1004 langmap_active = TRUE;
1005 }
1006#ifdef USE_IM_CONTROL
1007 save_smd = p_smd;
1008 p_smd = FALSE; /* Don't let the IM code show the mode here */
1009 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
1010 im_set_active(TRUE);
1011#endif
1012
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001013 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014
1015 if (langmap_active)
1016 {
1017 /* Undo the decrement done above */
1018 ++no_mapping;
1019 ++allow_keys;
1020 State = NORMAL_BUSY;
1021 }
1022#ifdef USE_IM_CONTROL
1023 if (lang)
1024 {
1025 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
1026 im_save_status(&curbuf->b_p_iminsert);
1027 im_set_active(FALSE);
1028 }
1029 p_smd = save_smd;
1030#endif
1031#ifdef CURSOR_SHAPE
1032 State = NORMAL_BUSY;
1033#endif
1034#ifdef FEAT_CMDL_INFO
1035 need_flushbuf |= add_to_showcmd(*cp);
1036#endif
1037
1038 if (!lit)
1039 {
1040#ifdef FEAT_DIGRAPHS
1041 /* Typing CTRL-K gets a digraph. */
1042 if (*cp == Ctrl_K
1043 && ((nv_cmds[idx].cmd_flags & NV_LANG)
1044 || cp == &ca.extra_char)
1045 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1046 {
1047 c = get_digraph(FALSE);
1048 if (c > 0)
1049 {
1050 *cp = c;
1051# ifdef FEAT_CMDL_INFO
1052 /* Guessing how to update showcmd here... */
1053 del_from_showcmd(3);
1054 need_flushbuf |= add_to_showcmd(*cp);
1055# endif
1056 }
1057 }
1058#endif
1059
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 /* adjust chars > 127, except after "tTfFr" commands */
1061 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062#ifdef FEAT_RIGHTLEFT
1063 /* adjust Hebrew mapped char */
1064 if (p_hkmap && lang && KeyTyped)
1065 *cp = hkmap(*cp);
1066# ifdef FEAT_FKMAP
1067 /* adjust Farsi mapped char */
1068 if (p_fkmap && lang && KeyTyped)
1069 *cp = fkmap(*cp);
1070# endif
1071#endif
1072 }
1073
1074 /*
1075 * When the next character is CTRL-\ a following CTRL-N means the
1076 * command is aborted and we go to Normal mode.
1077 */
1078 if (cp == &ca.extra_char
1079 && ca.nchar == Ctrl_BSL
1080 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1081 {
1082 ca.cmdchar = Ctrl_BSL;
1083 ca.nchar = ca.extra_char;
1084 idx = find_command(ca.cmdchar);
1085 }
1086 else if (*cp == Ctrl_BSL)
1087 {
1088 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1089
1090 /* There is a busy wait here when typing "f<C-\>" and then
1091 * something different from CTRL-N. Can't be avoided. */
1092 while ((c = vpeekc()) <= 0 && towait > 0L)
1093 {
1094 do_sleep(towait > 50L ? 50L : towait);
1095 towait -= 50L;
1096 }
1097 if (c > 0)
1098 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001099 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 if (c != Ctrl_N && c != Ctrl_G)
1101 vungetc(c);
1102 else
1103 {
1104 ca.cmdchar = Ctrl_BSL;
1105 ca.nchar = c;
1106 idx = find_command(ca.cmdchar);
1107 }
1108 }
1109 }
1110
1111#ifdef FEAT_MBYTE
1112 /* When getting a text character and the next character is a
1113 * multi-byte character, it could be a composing character.
1114 * However, don't wait for it to arrive. */
1115 while (enc_utf8 && lang && (c = vpeekc()) > 0
1116 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1117 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001118 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 if (!utf_iscomposing(c))
1120 {
1121 vungetc(c); /* it wasn't, put it back */
1122 break;
1123 }
1124 else if (ca.ncharC1 == 0)
1125 ca.ncharC1 = c;
1126 else
1127 ca.ncharC2 = c;
1128 }
1129#endif
1130 }
1131 --no_mapping;
1132 --allow_keys;
1133 }
1134
1135#ifdef FEAT_CMDL_INFO
1136 /*
1137 * Flush the showcmd characters onto the screen so we can see them while
1138 * the command is being executed. Only do this when the shown command was
1139 * actually displayed, otherwise this will slow down a lot when executing
1140 * mappings.
1141 */
1142 if (need_flushbuf)
1143 out_flush();
1144#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00001145#ifdef FEAT_AUTOCMD
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001146 if (ca.cmdchar != K_IGNORE)
1147 did_cursorhold = FALSE;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149
1150 State = NORMAL;
1151
1152 if (ca.nchar == ESC)
1153 {
1154 clearop(oap);
1155 if (restart_edit == 0 && goto_im())
1156 restart_edit = 'a';
1157 goto normal_end;
1158 }
1159
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001160 if (ca.cmdchar != K_IGNORE)
1161 {
1162 msg_didout = FALSE; /* don't scroll screen up for normal command */
1163 msg_col = 0;
1164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165
1166#ifdef FEAT_VISUAL
1167 old_pos = curwin->w_cursor; /* remember where cursor was */
1168
1169 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1170 * mode. */
1171 if (!VIsual_active && km_startsel)
1172 {
1173 if (nv_cmds[idx].cmd_flags & NV_SS)
1174 {
1175 start_selection();
1176 unshift_special(&ca);
1177 idx = find_command(ca.cmdchar);
1178 }
1179 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1180 && (mod_mask & MOD_MASK_SHIFT))
1181 {
1182 start_selection();
1183 mod_mask &= ~MOD_MASK_SHIFT;
1184 }
1185 }
1186#endif
1187
1188 /*
1189 * Execute the command!
1190 * Call the command function found in the commands table.
1191 */
1192 ca.arg = nv_cmds[idx].cmd_arg;
1193 (nv_cmds[idx].cmd_func)(&ca);
1194
1195 /*
1196 * If we didn't start or finish an operator, reset oap->regname, unless we
1197 * need it later.
1198 */
1199 if (!finish_op
1200 && !oap->op_type
1201 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1202 {
1203 clearop(oap);
1204#ifdef FEAT_EVAL
1205 set_reg_var('"');
1206#endif
1207 }
1208
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001209#ifdef FEAT_VISUAL
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001210 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001211 * character or "z333<cr>". */
1212 if (old_mapped_len > 0)
1213 old_mapped_len = typebuf_maplen();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001214#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001215
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 /*
1217 * If an operation is pending, handle it...
1218 */
1219 do_pending_operator(&ca, old_col, FALSE);
1220
1221 /*
1222 * Wait for a moment when a message is displayed that will be overwritten
1223 * by the mode message.
1224 * In Visual mode and with "^O" in Insert mode, a short message will be
1225 * overwritten by the mode message. Wait a bit, until a key is hit.
1226 * In Visual mode, it's more important to keep the Visual area updated
1227 * than keeping a message (e.g. from a /pat search).
1228 * Only do this if the command was typed, not from a mapping.
1229 * Don't wait when emsg_silent is non-zero.
1230 * Also wait a bit after an error message, e.g. for "^O:".
1231 * Don't redraw the screen, it would remove the message.
1232 */
1233 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001234 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 && (restart_edit != 0
1236#ifdef FEAT_VISUAL
1237 || (VIsual_active
1238 && old_pos.lnum == curwin->w_cursor.lnum
1239 && old_pos.col == curwin->w_cursor.col)
1240#endif
1241 )
1242 && (clear_cmdline
1243 || redraw_cmdline)
1244 && (msg_didout || (msg_didany && msg_scroll))
1245 && !msg_nowait
1246 && KeyTyped)
1247 || (restart_edit != 0
1248#ifdef FEAT_VISUAL
1249 && !VIsual_active
1250#endif
1251 && (msg_scroll
1252 || emsg_on_display)))
1253 && oap->regname == 0
1254 && !(ca.retval & CA_COMMAND_BUSY)
1255 && stuff_empty()
1256 && typebuf_typed()
1257 && emsg_silent == 0
1258 && !did_wait_return
1259 && oap->op_type == OP_NOP)
1260 {
1261 int save_State = State;
1262
1263 /* Draw the cursor with the right shape here */
1264 if (restart_edit != 0)
1265 State = INSERT;
1266
1267 /* If need to redraw, and there is a "keep_msg", redraw before the
1268 * delay */
1269 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1270 {
1271 char_u *kmsg;
1272
1273 kmsg = keep_msg;
1274 keep_msg = NULL;
1275 /* showmode() will clear keep_msg, but we want to use it anyway */
1276 update_screen(0);
1277 /* now reset it, otherwise it's put in the history again */
1278 keep_msg = kmsg;
1279 msg_attr(kmsg, keep_msg_attr);
1280 vim_free(kmsg);
1281 }
1282 setcursor();
1283 cursor_on();
1284 out_flush();
1285 if (msg_scroll || emsg_on_display)
1286 ui_delay(1000L, TRUE); /* wait at least one second */
1287 ui_delay(3000L, FALSE); /* wait up to three seconds */
1288 State = save_State;
1289
1290 msg_scroll = FALSE;
1291 emsg_on_display = FALSE;
1292 }
1293
1294 /*
1295 * Finish up after executing a Normal mode command.
1296 */
1297normal_end:
1298
1299 msg_nowait = FALSE;
1300
1301 /* Reset finish_op, in case it was set */
1302#ifdef CURSOR_SHAPE
1303 c = finish_op;
1304#endif
1305 finish_op = FALSE;
1306#ifdef CURSOR_SHAPE
1307 /* Redraw the cursor with another shape, if we were in Operator-pending
1308 * mode or did a replace command. */
1309 if (c || ca.cmdchar == 'r')
1310 {
1311 ui_cursor_shape(); /* may show different cursor shape */
1312# ifdef FEAT_MOUSESHAPE
1313 update_mouseshape(-1);
1314# endif
1315 }
1316#endif
1317
1318#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001319 if (oap->op_type == OP_NOP && oap->regname == 0
1320# ifdef FEAT_AUTOCMD
1321 && ca.cmdchar != K_CURSORHOLD
1322# endif
1323 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 clear_showcmd();
1325#endif
1326
1327 checkpcmark(); /* check if we moved since setting pcmark */
1328 vim_free(ca.searchbuf);
1329
1330#ifdef FEAT_MBYTE
1331 if (has_mbyte)
1332 mb_adjust_cursor();
1333#endif
1334
1335#ifdef FEAT_SCROLLBIND
1336 if (curwin->w_p_scb && toplevel)
1337 {
1338 validate_cursor(); /* may need to update w_leftcol */
1339 do_check_scrollbind(TRUE);
1340 }
1341#endif
1342
Bram Moolenaar860cae12010-06-05 23:22:07 +02001343#ifdef FEAT_CURSORBIND
1344 if (curwin->w_p_crb && toplevel)
1345 {
1346 validate_cursor(); /* may need to update w_leftcol */
1347 do_check_cursorbind();
1348 }
1349#endif
1350
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 /*
1352 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1353 * if still inside a mapping that started in Visual mode).
1354 * May switch from Visual to Select mode after CTRL-O command.
1355 */
1356 if ( oap->op_type == OP_NOP
1357#ifdef FEAT_VISUAL
1358 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1359 || restart_VIsual_select == 1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001360#else
1361 && restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362#endif
1363 && !(ca.retval & CA_COMMAND_BUSY)
1364 && stuff_empty()
1365 && oap->regname == 0)
1366 {
1367#ifdef FEAT_VISUAL
1368 if (restart_VIsual_select == 1)
1369 {
1370 VIsual_select = TRUE;
1371 showmode();
1372 restart_VIsual_select = 0;
1373 }
1374#endif
1375 if (restart_edit != 0
1376#ifdef FEAT_VISUAL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001377 && !VIsual_active && old_mapped_len == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001379 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 (void)edit(restart_edit, FALSE, 1L);
1381 }
1382
1383#ifdef FEAT_VISUAL
1384 if (restart_VIsual_select == 2)
1385 restart_VIsual_select = 1;
1386#endif
1387
1388 /* Save count before an operator for next time. */
1389 opcount = ca.opcount;
1390}
1391
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001392#ifdef FEAT_EVAL
1393/*
1394 * Set v:count and v:count1 according to "cap".
1395 * Set v:prevcount only when "set_prevcount" is TRUE.
1396 */
1397 static void
1398set_vcount_ca(cap, set_prevcount)
1399 cmdarg_T *cap;
1400 int *set_prevcount;
1401{
1402 long count = cap->count0;
1403
1404 /* multiply with cap->opcount the same way as above */
1405 if (cap->opcount != 0)
1406 count = cap->opcount * (count == 0 ? 1 : count);
1407 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1408 *set_prevcount = FALSE; /* only set v:prevcount once */
1409}
1410#endif
1411
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412/*
1413 * Handle an operator after visual mode or when the movement is finished
1414 */
1415 void
1416do_pending_operator(cap, old_col, gui_yank)
1417 cmdarg_T *cap;
1418 int old_col;
1419 int gui_yank;
1420{
1421 oparg_T *oap = cap->oap;
1422 pos_T old_cursor;
1423 int empty_region_error;
1424 int restart_edit_save;
1425
1426#ifdef FEAT_VISUAL
1427 /* The visual area is remembered for redo */
1428 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1429 static linenr_T redo_VIsual_line_count; /* number of lines */
1430 static colnr_T redo_VIsual_col; /* number of cols or end column */
1431 static long redo_VIsual_count; /* count for Visual operator */
1432# ifdef FEAT_VIRTUALEDIT
1433 int include_line_break = FALSE;
1434# endif
1435#endif
1436
1437#if defined(FEAT_CLIPBOARD)
1438 /*
1439 * Yank the visual area into the GUI selection register before we operate
1440 * on it and lose it forever.
1441 * Don't do it if a specific register was specified, so that ""x"*P works.
1442 * This could call do_pending_operator() recursively, but that's OK
1443 * because gui_yank will be TRUE for the nested call.
1444 */
1445 if (clip_star.available
1446 && oap->op_type != OP_NOP
1447 && !gui_yank
1448# ifdef FEAT_VISUAL
1449 && VIsual_active
1450 && !redo_VIsual_busy
1451# endif
1452 && oap->regname == 0)
1453 clip_auto_select();
1454#endif
1455 old_cursor = curwin->w_cursor;
1456
1457 /*
1458 * If an operation is pending, handle it...
1459 */
1460 if ((finish_op
1461#ifdef FEAT_VISUAL
1462 || VIsual_active
1463#endif
1464 ) && oap->op_type != OP_NOP)
1465 {
1466#ifdef FEAT_VISUAL
1467 oap->is_VIsual = VIsual_active;
1468 if (oap->motion_force == 'V')
1469 oap->motion_type = MLINE;
1470 else if (oap->motion_force == 'v')
1471 {
1472 /* If the motion was linewise, "inclusive" will not have been set.
1473 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1474 if (oap->motion_type == MLINE)
1475 oap->inclusive = FALSE;
1476 /* If the motion already was characterwise, toggle "inclusive" */
1477 else if (oap->motion_type == MCHAR)
1478 oap->inclusive = !oap->inclusive;
1479 oap->motion_type = MCHAR;
1480 }
1481 else if (oap->motion_force == Ctrl_V)
1482 {
1483 /* Change line- or characterwise motion into Visual block mode. */
1484 VIsual_active = TRUE;
1485 VIsual = oap->start;
1486 VIsual_mode = Ctrl_V;
1487 VIsual_select = FALSE;
1488 VIsual_reselect = FALSE;
1489 }
1490#endif
1491
1492 /* only redo yank when 'y' flag is in 'cpoptions' */
1493 /* never redo "zf" (define fold) */
1494 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1495#ifdef FEAT_VISUAL
1496 && (!VIsual_active || oap->motion_force)
1497#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001498 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499#ifdef FEAT_FOLDING
1500 && oap->op_type != OP_FOLD
1501 && oap->op_type != OP_FOLDOPEN
1502 && oap->op_type != OP_FOLDOPENREC
1503 && oap->op_type != OP_FOLDCLOSE
1504 && oap->op_type != OP_FOLDCLOSEREC
1505 && oap->op_type != OP_FOLDDEL
1506 && oap->op_type != OP_FOLDDELREC
1507#endif
1508 )
1509 {
1510 prep_redo(oap->regname, cap->count0,
1511 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1512 oap->motion_force, cap->cmdchar, cap->nchar);
1513 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1514 {
1515 /*
1516 * If 'cpoptions' does not contain 'r', insert the search
1517 * pattern to really repeat the same command.
1518 */
1519 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001520 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 AppendToRedobuff(NL_STR);
1522 }
1523 else if (cap->cmdchar == ':')
1524 {
1525 /* do_cmdline() has stored the first typed line in
1526 * "repeat_cmdline". When several lines are typed repeating
1527 * won't be possible. */
1528 if (repeat_cmdline == NULL)
1529 ResetRedobuff();
1530 else
1531 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001532 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 AppendToRedobuff(NL_STR);
1534 vim_free(repeat_cmdline);
1535 repeat_cmdline = NULL;
1536 }
1537 }
1538 }
1539
1540#ifdef FEAT_VISUAL
1541 if (redo_VIsual_busy)
1542 {
1543 oap->start = curwin->w_cursor;
1544 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1545 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1546 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1547 VIsual_mode = redo_VIsual_mode;
1548 if (VIsual_mode == 'v')
1549 {
1550 if (redo_VIsual_line_count <= 1)
1551 curwin->w_cursor.col += redo_VIsual_col - 1;
1552 else
1553 curwin->w_cursor.col = redo_VIsual_col;
1554 }
1555 if (redo_VIsual_col == MAXCOL)
1556 {
1557 curwin->w_curswant = MAXCOL;
1558 coladvance((colnr_T)MAXCOL);
1559 }
1560 cap->count0 = redo_VIsual_count;
1561 if (redo_VIsual_count != 0)
1562 cap->count1 = redo_VIsual_count;
1563 else
1564 cap->count1 = 1;
1565 }
1566 else if (VIsual_active)
1567 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001568 if (!gui_yank)
1569 {
1570 /* Save the current VIsual area for '< and '> marks, and "gv" */
1571 curbuf->b_visual.vi_start = VIsual;
1572 curbuf->b_visual.vi_end = curwin->w_cursor;
1573 curbuf->b_visual.vi_mode = VIsual_mode;
1574 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001576 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
1580 /* In Select mode, a linewise selection is operated upon like a
1581 * characterwise selection. */
1582 if (VIsual_select && VIsual_mode == 'V')
1583 {
1584 if (lt(VIsual, curwin->w_cursor))
1585 {
1586 VIsual.col = 0;
1587 curwin->w_cursor.col =
1588 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1589 }
1590 else
1591 {
1592 curwin->w_cursor.col = 0;
1593 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1594 }
1595 VIsual_mode = 'v';
1596 }
1597 /* If 'selection' is "exclusive", backup one character for
1598 * charwise selections. */
1599 else if (VIsual_mode == 'v')
1600 {
1601# ifdef FEAT_VIRTUALEDIT
1602 include_line_break =
1603# endif
1604 unadjust_for_sel();
1605 }
1606
1607 oap->start = VIsual;
1608 if (VIsual_mode == 'V')
1609 oap->start.col = 0;
1610 }
1611#endif /* FEAT_VISUAL */
1612
1613 /*
1614 * Set oap->start to the first position of the operated text, oap->end
1615 * to the end of the operated text. w_cursor is equal to oap->start.
1616 */
1617 if (lt(oap->start, curwin->w_cursor))
1618 {
1619#ifdef FEAT_FOLDING
1620 /* Include folded lines completely. */
1621 if (!VIsual_active)
1622 {
1623 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1624 oap->start.col = 0;
1625 if (hasFolding(curwin->w_cursor.lnum, NULL,
1626 &curwin->w_cursor.lnum))
1627 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1628 }
1629#endif
1630 oap->end = curwin->w_cursor;
1631 curwin->w_cursor = oap->start;
1632
1633 /* w_virtcol may have been updated; if the cursor goes back to its
1634 * previous position w_virtcol becomes invalid and isn't updated
1635 * automatically. */
1636 curwin->w_valid &= ~VALID_VIRTCOL;
1637 }
1638 else
1639 {
1640#ifdef FEAT_FOLDING
1641 /* Include folded lines completely. */
1642 if (!VIsual_active && oap->motion_type == MLINE)
1643 {
1644 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1645 NULL))
1646 curwin->w_cursor.col = 0;
1647 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1648 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1649 }
1650#endif
1651 oap->end = oap->start;
1652 oap->start = curwin->w_cursor;
1653 }
1654
1655 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1656
1657#ifdef FEAT_VIRTUALEDIT
1658 /* Set "virtual_op" before resetting VIsual_active. */
1659 virtual_op = virtual_active();
1660#endif
1661
1662#ifdef FEAT_VISUAL
1663 if (VIsual_active || redo_VIsual_busy)
1664 {
1665 if (VIsual_mode == Ctrl_V) /* block mode */
1666 {
1667 colnr_T start, end;
1668
1669 oap->block_mode = TRUE;
1670
1671 getvvcol(curwin, &(oap->start),
1672 &oap->start_vcol, NULL, &oap->end_vcol);
1673 if (!redo_VIsual_busy)
1674 {
1675 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1676
1677 if (start < oap->start_vcol)
1678 oap->start_vcol = start;
1679 if (end > oap->end_vcol)
1680 {
1681 if (*p_sel == 'e' && start >= 1
1682 && start - 1 >= oap->end_vcol)
1683 oap->end_vcol = start - 1;
1684 else
1685 oap->end_vcol = end;
1686 }
1687 }
1688
1689 /* if '$' was used, get oap->end_vcol from longest line */
1690 if (curwin->w_curswant == MAXCOL)
1691 {
1692 curwin->w_cursor.col = MAXCOL;
1693 oap->end_vcol = 0;
1694 for (curwin->w_cursor.lnum = oap->start.lnum;
1695 curwin->w_cursor.lnum <= oap->end.lnum;
1696 ++curwin->w_cursor.lnum)
1697 {
1698 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1699 if (end > oap->end_vcol)
1700 oap->end_vcol = end;
1701 }
1702 }
1703 else if (redo_VIsual_busy)
1704 oap->end_vcol = oap->start_vcol + redo_VIsual_col - 1;
1705 /*
1706 * Correct oap->end.col and oap->start.col to be the
1707 * upper-left and lower-right corner of the block area.
1708 *
1709 * (Actually, this does convert column positions into character
1710 * positions)
1711 */
1712 curwin->w_cursor.lnum = oap->end.lnum;
1713 coladvance(oap->end_vcol);
1714 oap->end = curwin->w_cursor;
1715
1716 curwin->w_cursor = oap->start;
1717 coladvance(oap->start_vcol);
1718 oap->start = curwin->w_cursor;
1719 }
1720
1721 if (!redo_VIsual_busy && !gui_yank)
1722 {
1723 /*
1724 * Prepare to reselect and redo Visual: this is based on the
1725 * size of the Visual text
1726 */
1727 resel_VIsual_mode = VIsual_mode;
1728 if (curwin->w_curswant == MAXCOL)
1729 resel_VIsual_col = MAXCOL;
1730 else if (VIsual_mode == Ctrl_V)
1731 resel_VIsual_col = oap->end_vcol - oap->start_vcol + 1;
1732 else if (oap->line_count > 1)
1733 resel_VIsual_col = oap->end.col;
1734 else
1735 resel_VIsual_col = oap->end.col - oap->start.col + 1;
1736 resel_VIsual_line_count = oap->line_count;
1737 }
1738
1739 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1740 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1741 && oap->op_type != OP_COLON
1742#ifdef FEAT_FOLDING
1743 && oap->op_type != OP_FOLD
1744 && oap->op_type != OP_FOLDOPEN
1745 && oap->op_type != OP_FOLDOPENREC
1746 && oap->op_type != OP_FOLDCLOSE
1747 && oap->op_type != OP_FOLDCLOSEREC
1748 && oap->op_type != OP_FOLDDEL
1749 && oap->op_type != OP_FOLDDELREC
1750#endif
1751 && oap->motion_force == NUL
1752 )
1753 {
1754 /* Prepare for redoing. Only use the nchar field for "r",
1755 * otherwise it might be the second char of the operator. */
1756 prep_redo(oap->regname, 0L, NUL, 'v',
1757 get_op_char(oap->op_type),
1758 get_extra_op_char(oap->op_type),
1759 oap->op_type == OP_REPLACE ? cap->nchar : NUL);
1760 if (!redo_VIsual_busy)
1761 {
1762 redo_VIsual_mode = resel_VIsual_mode;
1763 redo_VIsual_col = resel_VIsual_col;
1764 redo_VIsual_line_count = resel_VIsual_line_count;
1765 redo_VIsual_count = cap->count0;
1766 }
1767 }
1768
1769 /*
1770 * oap->inclusive defaults to TRUE.
1771 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1772 * FALSE. This makes "d}P" and "v}dP" work the same.
1773 */
1774 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1775 oap->inclusive = TRUE;
1776 if (VIsual_mode == 'V')
1777 oap->motion_type = MLINE;
1778 else
1779 {
1780 oap->motion_type = MCHAR;
1781 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1782# ifdef FEAT_VIRTUALEDIT
1783 && (include_line_break || !virtual_op)
1784# endif
1785 )
1786 {
1787 oap->inclusive = FALSE;
1788 /* Try to include the newline, unless it's an operator
1789 * that works on lines only */
1790 if (*p_sel != 'o'
1791 && !op_on_lines(oap->op_type)
1792 && oap->end.lnum < curbuf->b_ml.ml_line_count)
1793 {
1794 ++oap->end.lnum;
1795 oap->end.col = 0;
1796# ifdef FEAT_VIRTUALEDIT
1797 oap->end.coladd = 0;
1798# endif
1799 ++oap->line_count;
1800 }
1801 }
1802 }
1803
1804 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001805
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 /*
1807 * Switch Visual off now, so screen updating does
1808 * not show inverted text when the screen is redrawn.
1809 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1810 * no screen redraw, so it is done here to remove the inverted
1811 * part.
1812 */
1813 if (!gui_yank)
1814 {
1815 VIsual_active = FALSE;
1816# ifdef FEAT_MOUSE
1817 setmouse();
1818 mouse_dragging = 0;
1819# endif
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001820 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 clear_cmdline = TRUE; /* unshow visual mode later */
1822#ifdef FEAT_CMDL_INFO
1823 else
1824 clear_showcmd();
1825#endif
1826 if ((oap->op_type == OP_YANK
1827 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001828 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 || oap->op_type == OP_FILTER)
1830 && oap->motion_force == NUL)
1831 redraw_curbuf_later(INVERTED);
1832 }
1833 }
1834#endif
1835
1836#ifdef FEAT_MBYTE
1837 /* Include the trailing byte of a multi-byte char. */
1838 if (has_mbyte && oap->inclusive)
1839 {
1840 int l;
1841
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001842 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 if (l > 1)
1844 oap->end.col += l - 1;
1845 }
1846#endif
1847 curwin->w_set_curswant = TRUE;
1848
1849 /*
1850 * oap->empty is set when start and end are the same. The inclusive
1851 * flag affects this too, unless yanking and the end is on a NUL.
1852 */
1853 oap->empty = (oap->motion_type == MCHAR
1854 && (!oap->inclusive
1855 || (oap->op_type == OP_YANK
1856 && gchar_pos(&oap->end) == NUL))
1857 && equalpos(oap->start, oap->end)
1858#ifdef FEAT_VIRTUALEDIT
1859 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1860#endif
1861 );
1862 /*
1863 * For delete, change and yank, it's an error to operate on an
1864 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1865 */
1866 empty_region_error = (oap->empty
1867 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1868
1869#ifdef FEAT_VISUAL
1870 /* Force a redraw when operating on an empty Visual region, when
1871 * 'modifiable is off or creating a fold. */
1872 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1873# ifdef FEAT_FOLDING
1874 || oap->op_type == OP_FOLD
1875# endif
1876 ))
1877 redraw_curbuf_later(INVERTED);
1878#endif
1879
1880 /*
1881 * If the end of an operator is in column one while oap->motion_type
1882 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1883 * character in the previous line. If op_start is on or before the
1884 * first non-blank in the line, the operator becomes linewise
1885 * (strange, but that's the way vi does it).
1886 */
1887 if ( oap->motion_type == MCHAR
1888 && oap->inclusive == FALSE
1889 && !(cap->retval & CA_NO_ADJ_OP_END)
1890 && oap->end.col == 0
1891#ifdef FEAT_VISUAL
1892 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001893 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894#endif
1895 && oap->line_count > 1)
1896 {
1897 oap->end_adjusted = TRUE; /* remember that we did this */
1898 --oap->line_count;
1899 --oap->end.lnum;
1900 if (inindent(0))
1901 oap->motion_type = MLINE;
1902 else
1903 {
1904 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1905 if (oap->end.col)
1906 {
1907 --oap->end.col;
1908 oap->inclusive = TRUE;
1909 }
1910 }
1911 }
1912 else
1913 oap->end_adjusted = FALSE;
1914
1915 switch (oap->op_type)
1916 {
1917 case OP_LSHIFT:
1918 case OP_RSHIFT:
1919 op_shift(oap, TRUE,
1920#ifdef FEAT_VISUAL
1921 oap->is_VIsual ? (int)cap->count1 :
1922#endif
1923 1);
1924 auto_format(FALSE, TRUE);
1925 break;
1926
1927 case OP_JOIN_NS:
1928 case OP_JOIN:
1929 if (oap->line_count < 2)
1930 oap->line_count = 2;
1931 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1932 curbuf->b_ml.ml_line_count)
1933 beep_flush();
1934 else
1935 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001936 (void)do_join(oap->line_count, oap->op_type == OP_JOIN, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 auto_format(FALSE, TRUE);
1938 }
1939 break;
1940
1941 case OP_DELETE:
1942#ifdef FEAT_VISUAL
1943 VIsual_reselect = FALSE; /* don't reselect now */
1944#endif
1945 if (empty_region_error)
1946 vim_beep();
1947 else
1948 {
1949 (void)op_delete(oap);
1950 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1951 u_save_cursor(); /* cursor line wasn't saved yet */
1952 auto_format(FALSE, TRUE);
1953 }
1954 break;
1955
1956 case OP_YANK:
1957 if (empty_region_error)
1958 {
1959 if (!gui_yank)
1960 vim_beep();
1961 }
1962 else
1963 (void)op_yank(oap, FALSE, !gui_yank);
1964 check_cursor_col();
1965 break;
1966
1967 case OP_CHANGE:
1968#ifdef FEAT_VISUAL
1969 VIsual_reselect = FALSE; /* don't reselect now */
1970#endif
1971 if (empty_region_error)
1972 vim_beep();
1973 else
1974 {
1975 /* This is a new edit command, not a restart. Need to
1976 * remember it to make 'insertmode' work with mappings for
1977 * Visual mode. But do this only once and not when typed and
1978 * 'insertmode' isn't set. */
1979 if (p_im || !KeyTyped)
1980 restart_edit_save = restart_edit;
1981 else
1982 restart_edit_save = 0;
1983 restart_edit = 0;
1984 /* Reset finish_op now, don't want it set inside edit(). */
1985 finish_op = FALSE;
1986 if (op_change(oap)) /* will call edit() */
1987 cap->retval |= CA_COMMAND_BUSY;
1988 if (restart_edit == 0)
1989 restart_edit = restart_edit_save;
1990 }
1991 break;
1992
1993 case OP_FILTER:
1994 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1995 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1996 else
1997 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
1998
1999 case OP_INDENT:
2000 case OP_COLON:
2001
2002#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2003 /*
2004 * If 'equalprg' is empty, do the indenting internally.
2005 */
2006 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
2007 {
2008# ifdef FEAT_LISP
2009 if (curbuf->b_p_lisp)
2010 {
2011 op_reindent(oap, get_lisp_indent);
2012 break;
2013 }
2014# endif
2015# ifdef FEAT_CINDENT
2016 op_reindent(oap,
2017# ifdef FEAT_EVAL
2018 *curbuf->b_p_inde != NUL ? get_expr_indent :
2019# endif
2020 get_c_indent);
2021 break;
2022# endif
2023 }
2024#endif
2025
2026 op_colon(oap);
2027 break;
2028
2029 case OP_TILDE:
2030 case OP_UPPER:
2031 case OP_LOWER:
2032 case OP_ROT13:
2033 if (empty_region_error)
2034 vim_beep();
2035 else
2036 op_tilde(oap);
2037 check_cursor_col();
2038 break;
2039
2040 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002041#if defined(FEAT_EVAL)
2042 if (*curbuf->b_p_fex != NUL)
2043 op_formatexpr(oap); /* use expression */
2044 else
2045#endif
2046 if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 op_colon(oap); /* use external command */
2048 else
2049 op_format(oap, FALSE); /* use internal function */
2050 break;
2051
2052 case OP_FORMAT2:
2053 op_format(oap, TRUE); /* use internal function */
2054 break;
2055
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002056 case OP_FUNCTION:
2057 op_function(oap); /* call 'operatorfunc' */
2058 break;
2059
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 case OP_INSERT:
2061 case OP_APPEND:
2062#ifdef FEAT_VISUAL
2063 VIsual_reselect = FALSE; /* don't reselect now */
2064#endif
2065#ifdef FEAT_VISUALEXTRA
2066 if (empty_region_error)
2067 vim_beep();
2068 else
2069 {
2070 /* This is a new edit command, not a restart. Need to
2071 * remember it to make 'insertmode' work with mappings for
2072 * Visual mode. But do this only once. */
2073 restart_edit_save = restart_edit;
2074 restart_edit = 0;
2075
2076 op_insert(oap, cap->count1);
2077
2078 /* TODO: when inserting in several lines, should format all
2079 * the lines. */
2080 auto_format(FALSE, TRUE);
2081
2082 if (restart_edit == 0)
2083 restart_edit = restart_edit_save;
2084 }
2085#else
2086 vim_beep();
2087#endif
2088 break;
2089
2090 case OP_REPLACE:
2091#ifdef FEAT_VISUAL
2092 VIsual_reselect = FALSE; /* don't reselect now */
2093#endif
2094#ifdef FEAT_VISUALEXTRA
2095 if (empty_region_error)
2096#endif
2097 vim_beep();
2098#ifdef FEAT_VISUALEXTRA
2099 else
2100 op_replace(oap, cap->nchar);
2101#endif
2102 break;
2103
2104#ifdef FEAT_FOLDING
2105 case OP_FOLD:
2106 VIsual_reselect = FALSE; /* don't reselect now */
2107 foldCreate(oap->start.lnum, oap->end.lnum);
2108 break;
2109
2110 case OP_FOLDOPEN:
2111 case OP_FOLDOPENREC:
2112 case OP_FOLDCLOSE:
2113 case OP_FOLDCLOSEREC:
2114 VIsual_reselect = FALSE; /* don't reselect now */
2115 opFoldRange(oap->start.lnum, oap->end.lnum,
2116 oap->op_type == OP_FOLDOPEN
2117 || oap->op_type == OP_FOLDOPENREC,
2118 oap->op_type == OP_FOLDOPENREC
2119 || oap->op_type == OP_FOLDCLOSEREC,
2120 oap->is_VIsual);
2121 break;
2122
2123 case OP_FOLDDEL:
2124 case OP_FOLDDELREC:
2125 VIsual_reselect = FALSE; /* don't reselect now */
2126 deleteFold(oap->start.lnum, oap->end.lnum,
2127 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2128 break;
2129#endif
2130 default:
2131 clearopbeep(oap);
2132 }
2133#ifdef FEAT_VIRTUALEDIT
2134 virtual_op = MAYBE;
2135#endif
2136 if (!gui_yank)
2137 {
2138 /*
2139 * if 'sol' not set, go back to old column for some commands
2140 */
2141 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2142 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2143 || oap->op_type == OP_DELETE))
2144 coladvance(curwin->w_curswant = old_col);
2145 }
2146 else
2147 {
2148 curwin->w_cursor = old_cursor;
2149 }
2150#ifdef FEAT_VISUAL
2151 oap->block_mode = FALSE;
2152#endif
2153 clearop(oap);
2154 }
2155}
2156
2157/*
2158 * Handle indent and format operators and visual mode ":".
2159 */
2160 static void
2161op_colon(oap)
2162 oparg_T *oap;
2163{
2164 stuffcharReadbuff(':');
2165#ifdef FEAT_VISUAL
2166 if (oap->is_VIsual)
2167 stuffReadbuff((char_u *)"'<,'>");
2168 else
2169#endif
2170 {
2171 /*
2172 * Make the range look nice, so it can be repeated.
2173 */
2174 if (oap->start.lnum == curwin->w_cursor.lnum)
2175 stuffcharReadbuff('.');
2176 else
2177 stuffnumReadbuff((long)oap->start.lnum);
2178 if (oap->end.lnum != oap->start.lnum)
2179 {
2180 stuffcharReadbuff(',');
2181 if (oap->end.lnum == curwin->w_cursor.lnum)
2182 stuffcharReadbuff('.');
2183 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2184 stuffcharReadbuff('$');
2185 else if (oap->start.lnum == curwin->w_cursor.lnum)
2186 {
2187 stuffReadbuff((char_u *)".+");
2188 stuffnumReadbuff((long)oap->line_count - 1);
2189 }
2190 else
2191 stuffnumReadbuff((long)oap->end.lnum);
2192 }
2193 }
2194 if (oap->op_type != OP_COLON)
2195 stuffReadbuff((char_u *)"!");
2196 if (oap->op_type == OP_INDENT)
2197 {
2198#ifndef FEAT_CINDENT
2199 if (*get_equalprg() == NUL)
2200 stuffReadbuff((char_u *)"indent");
2201 else
2202#endif
2203 stuffReadbuff(get_equalprg());
2204 stuffReadbuff((char_u *)"\n");
2205 }
2206 else if (oap->op_type == OP_FORMAT)
2207 {
2208 if (*p_fp == NUL)
2209 stuffReadbuff((char_u *)"fmt");
2210 else
2211 stuffReadbuff(p_fp);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002212 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 }
2214
2215 /*
2216 * do_cmdline() does the rest
2217 */
2218}
2219
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002220/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002221 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002222 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002223 static void
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002224op_function(oap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002225 oparg_T *oap UNUSED;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002226{
2227#ifdef FEAT_EVAL
2228 char_u *(argv[1]);
2229
2230 if (*p_opfunc == NUL)
2231 EMSG(_("E774: 'operatorfunc' is empty"));
2232 else
2233 {
2234 /* Set '[ and '] marks to text to be operated on. */
2235 curbuf->b_op_start = oap->start;
2236 curbuf->b_op_end = oap->end;
2237 if (oap->motion_type != MLINE && !oap->inclusive)
2238 /* Exclude the end position. */
2239 decl(&curbuf->b_op_end);
2240
2241 if (oap->block_mode)
2242 argv[0] = (char_u *)"block";
2243 else if (oap->motion_type == MLINE)
2244 argv[0] = (char_u *)"line";
2245 else
2246 argv[0] = (char_u *)"char";
2247 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
2248 }
2249#else
2250 EMSG(_("E775: Eval feature not available"));
2251#endif
2252}
2253
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254#if defined(FEAT_MOUSE) || defined(PROTO)
2255/*
2256 * Do the appropriate action for the current mouse click in the current mode.
2257 * Not used for Command-line mode.
2258 *
2259 * Normal Mode:
2260 * event modi- position visual change action
2261 * fier cursor window
2262 * left press - yes end yes
2263 * left press C yes end yes "^]" (2)
2264 * left press S yes end yes "*" (2)
2265 * left drag - yes start if moved no
2266 * left relse - yes start if moved no
2267 * middle press - yes if not active no put register
2268 * middle press - yes if active no yank and put
2269 * right press - yes start or extend yes
2270 * right press S yes no change yes "#" (2)
2271 * right drag - yes extend no
2272 * right relse - yes extend no
2273 *
2274 * Insert or Replace Mode:
2275 * event modi- position visual change action
2276 * fier cursor window
2277 * left press - yes (cannot be active) yes
2278 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2279 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2280 * left drag - yes start or extend (1) no CTRL-O (1)
2281 * left relse - yes start or extend (1) no CTRL-O (1)
2282 * middle press - no (cannot be active) no put register
2283 * right press - yes start or extend yes CTRL-O
2284 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2285 *
2286 * (1) only if mouse pointer moved since press
2287 * (2) only if click is in same buffer
2288 *
2289 * Return TRUE if start_arrow() should be called for edit mode.
2290 */
2291 int
2292do_mouse(oap, c, dir, count, fixindent)
2293 oparg_T *oap; /* operator argument, can be NULL */
2294 int c; /* K_LEFTMOUSE, etc */
2295 int dir; /* Direction to 'put' if necessary */
2296 long count;
2297 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2298{
2299 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2300 static int got_click = FALSE; /* got a click some time back */
2301
2302 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2303 int is_click; /* If FALSE it's a drag or release event */
2304 int is_drag; /* If TRUE it's a drag event */
2305 int jump_flags = 0; /* flags for jump_to_mouse() */
2306 pos_T start_visual;
2307 int moved; /* Has cursor moved? */
2308 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002309#ifdef FEAT_WINDOWS
2310 static int in_tab_line = FALSE; /* mouse clicked in tab line */
2311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312#ifdef FEAT_VERTSPLIT
2313 int in_sep_line; /* mouse in vertical separator line */
2314#endif
2315 int c1, c2;
2316#if defined(FEAT_FOLDING)
2317 pos_T save_cursor;
2318#endif
2319 win_T *old_curwin = curwin;
2320#ifdef FEAT_VISUAL
2321 static pos_T orig_cursor;
2322 colnr_T leftcol, rightcol;
2323 pos_T end_visual;
2324 int diff;
2325 int old_active = VIsual_active;
2326 int old_mode = VIsual_mode;
2327#endif
2328 int regname;
2329
2330#if defined(FEAT_FOLDING)
2331 save_cursor = curwin->w_cursor;
2332#endif
2333
2334 /*
2335 * When GUI is active, always recognize mouse events, otherwise:
2336 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2337 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2338 * - For command line and insert mode 'mouse' is checked before calling
2339 * do_mouse().
2340 */
2341 if (do_always)
2342 do_always = FALSE;
2343 else
2344#ifdef FEAT_GUI
2345 if (!gui.in_use)
2346#endif
2347 {
2348#ifdef FEAT_VISUAL
2349 if (VIsual_active)
2350 {
2351 if (!mouse_has(MOUSE_VISUAL))
2352 return FALSE;
2353 }
2354 else
2355#endif
2356 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2357 return FALSE;
2358 }
2359
2360 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2361
2362#ifdef FEAT_MOUSESHAPE
2363 /* May have stopped dragging the status or separator line. The pointer is
2364 * most likely still on the status or separator line. */
2365 if (!is_drag && drag_status_line)
2366 {
2367 drag_status_line = FALSE;
2368 update_mouseshape(SHAPE_IDX_STATUS);
2369 }
2370# ifdef FEAT_VERTSPLIT
2371 if (!is_drag && drag_sep_line)
2372 {
2373 drag_sep_line = FALSE;
2374 update_mouseshape(SHAPE_IDX_VSEP);
2375 }
2376# endif
2377#endif
2378
2379 /*
2380 * Ignore drag and release events if we didn't get a click.
2381 */
2382 if (is_click)
2383 got_click = TRUE;
2384 else
2385 {
2386 if (!got_click) /* didn't get click, ignore */
2387 return FALSE;
2388 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002389 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002391#ifdef FEAT_WINDOWS
2392 if (in_tab_line)
2393 {
2394 in_tab_line = FALSE;
2395 return FALSE;
2396 }
2397#endif
2398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
2400
Bram Moolenaar64969662005-12-14 21:59:55 +00002401#ifndef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 /*
Bram Moolenaar64969662005-12-14 21:59:55 +00002403 * ALT is only used for starging/extending Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 */
2405 if ((mod_mask & MOD_MASK_ALT))
2406 return FALSE;
Bram Moolenaar64969662005-12-14 21:59:55 +00002407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
2409 /*
2410 * CTRL right mouse button does CTRL-T
2411 */
2412 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2413 {
2414 if (State & INSERT)
2415 stuffcharReadbuff(Ctrl_O);
2416 if (count > 1)
2417 stuffnumReadbuff(count);
2418 stuffcharReadbuff(Ctrl_T);
2419 got_click = FALSE; /* ignore drag&release now */
2420 return FALSE;
2421 }
2422
2423 /*
2424 * CTRL only works with left mouse button
2425 */
2426 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2427 return FALSE;
2428
2429 /*
2430 * When a modifier is down, ignore drag and release events, as well as
2431 * multiple clicks and the middle mouse button.
2432 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2433 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002434 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2435 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 && (!is_click
2437 || (mod_mask & MOD_MASK_MULTI_CLICK)
2438 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002439 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 && mouse_model_popup()
2441 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002442 && !((mod_mask & MOD_MASK_ALT)
2443 && !mouse_model_popup()
2444 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 )
2446 return FALSE;
2447
2448 /*
2449 * If the button press was used as the movement command for an operator
2450 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2451 * drag/release events.
2452 */
2453 if (!is_click && which_button == MOUSE_MIDDLE)
2454 return FALSE;
2455
2456 if (oap != NULL)
2457 regname = oap->regname;
2458 else
2459 regname = 0;
2460
2461 /*
2462 * Middle mouse button does a 'put' of the selected text
2463 */
2464 if (which_button == MOUSE_MIDDLE)
2465 {
2466 if (State == NORMAL)
2467 {
2468 /*
2469 * If an operator was pending, we don't know what the user wanted
2470 * to do. Go back to normal mode: Clear the operator and beep().
2471 */
2472 if (oap != NULL && oap->op_type != OP_NOP)
2473 {
2474 clearopbeep(oap);
2475 return FALSE;
2476 }
2477
2478#ifdef FEAT_VISUAL
2479 /*
2480 * If visual was active, yank the highlighted text and put it
2481 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002482 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 */
2484 if (VIsual_active)
2485 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002486 if (VIsual_select)
2487 {
2488 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002489 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002490 }
2491 else
2492 {
2493 stuffcharReadbuff('y');
2494 stuffcharReadbuff(K_MIDDLEMOUSE);
2495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 do_always = TRUE; /* ignore 'mouse' setting next time */
2497 return FALSE;
2498 }
2499#endif
2500 /*
2501 * The rest is below jump_to_mouse()
2502 */
2503 }
2504
2505 else if ((State & INSERT) == 0)
2506 return FALSE;
2507
2508 /*
2509 * Middle click in insert mode doesn't move the mouse, just insert the
2510 * contents of a register. '.' register is special, can't insert that
2511 * with do_put().
2512 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2513 * happens for the GUI).
2514 */
2515 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2516 {
2517 if (regname == '.')
2518 insert_reg(regname, TRUE);
2519 else
2520 {
2521#ifdef FEAT_CLIPBOARD
2522 if (clip_star.available && regname == 0)
2523 regname = '*';
2524#endif
2525 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2526 insert_reg(regname, TRUE);
2527 else
2528 {
2529 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2530
2531 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2532 AppendCharToRedobuff(Ctrl_R);
2533 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2534 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2535 }
2536 }
2537 return FALSE;
2538 }
2539 }
2540
2541 /* When dragging or button-up stay in the same window. */
2542 if (!is_click)
2543 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2544
2545 start_visual.lnum = 0;
2546
Bram Moolenaarf740b292006-02-16 22:11:02 +00002547#ifdef FEAT_WINDOWS
2548 /* Check for clicking in the tab page line. */
2549 if (mouse_row == 0 && firstwin->w_winrow > 0)
2550 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002551 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002552 {
2553 if (in_tab_line)
2554 {
2555 c1 = TabPageIdxs[mouse_col];
2556 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2557 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002558 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002559 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002560
Bram Moolenaarf740b292006-02-16 22:11:02 +00002561 /* click in a tab selects that tab page */
2562 if (is_click
2563# ifdef FEAT_CMDWIN
2564 && cmdwin_type == 0
2565# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002566 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002567 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002568 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002569 c1 = TabPageIdxs[mouse_col];
2570 if (c1 >= 0)
2571 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002572 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2573 {
2574 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002575 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002576 tabpage_new();
2577 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2578 }
2579 else
2580 {
2581 /* Go to specified tab page, or next one if not clicking
2582 * on a label. */
2583 goto_tabpage(c1);
2584
2585 /* It's like clicking on the status line of a window. */
2586 if (curwin != old_curwin)
2587 end_visual_mode();
2588 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002589 }
2590 else if (c1 < 0)
2591 {
2592 tabpage_T *tp;
2593
2594 /* Close the current or specified tab page. */
2595 if (c1 == -999)
2596 tp = curtab;
2597 else
2598 tp = find_tabpage(-c1);
2599 if (tp == curtab)
2600 {
2601 if (first_tabpage->tp_next != NULL)
2602 tabpage_close(FALSE);
2603 }
2604 else if (tp != NULL)
2605 tabpage_close_other(tp, FALSE);
2606 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002607 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002608 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002609 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002610 else if (is_drag && in_tab_line)
2611 {
2612 c1 = TabPageIdxs[mouse_col];
2613 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2614 return FALSE;
2615 }
2616
Bram Moolenaarf740b292006-02-16 22:11:02 +00002617#endif
2618
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 /*
2620 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2621 * right button up -> pop-up menu
2622 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002623 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 */
2625 if (mouse_model_popup())
2626 {
2627 if (which_button == MOUSE_RIGHT
2628 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2629 {
2630 /*
2631 * NOTE: Ignore right button down and drag mouse events.
2632 * Windows only shows the popup menu on the button up event.
2633 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002634#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2635 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 if (!is_click)
2637 return FALSE;
2638#endif
2639#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2640 if (is_click || is_drag)
2641 return FALSE;
2642#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002643#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2645 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2646 if (gui.in_use)
2647 {
2648 jump_flags = 0;
2649 if (STRCMP(p_mousem, "popup_setpos") == 0)
2650 {
2651 /* First set the cursor position before showing the popup
2652 * menu. */
2653#ifdef FEAT_VISUAL
2654 if (VIsual_active)
2655 {
2656 pos_T m_pos;
2657
2658 /*
2659 * set MOUSE_MAY_STOP_VIS if we are outside the
2660 * selection or the current window (might have false
2661 * negative here)
2662 */
2663 if (mouse_row < W_WINROW(curwin)
2664 || mouse_row
2665 > (W_WINROW(curwin) + curwin->w_height))
2666 jump_flags = MOUSE_MAY_STOP_VIS;
2667 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2668 jump_flags = MOUSE_MAY_STOP_VIS;
2669 else
2670 {
2671 if ((lt(curwin->w_cursor, VIsual)
2672 && (lt(m_pos, curwin->w_cursor)
2673 || lt(VIsual, m_pos)))
2674 || (lt(VIsual, curwin->w_cursor)
2675 && (lt(m_pos, VIsual)
2676 || lt(curwin->w_cursor, m_pos))))
2677 {
2678 jump_flags = MOUSE_MAY_STOP_VIS;
2679 }
2680 else if (VIsual_mode == Ctrl_V)
2681 {
2682 getvcols(curwin, &curwin->w_cursor, &VIsual,
2683 &leftcol, &rightcol);
2684 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2685 if (m_pos.col < leftcol || m_pos.col > rightcol)
2686 jump_flags = MOUSE_MAY_STOP_VIS;
2687 }
2688 }
2689 }
2690 else
2691 jump_flags = MOUSE_MAY_STOP_VIS;
2692#endif
2693 }
2694 if (jump_flags)
2695 {
2696 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2697 update_curbuf(
2698#ifdef FEAT_VISUAL
2699 VIsual_active ? INVERTED :
2700#endif
2701 VALID);
2702 setcursor();
2703 out_flush(); /* Update before showing popup menu */
2704 }
2705# ifdef FEAT_MENU
2706 gui_show_popupmenu();
2707# endif
2708 return (jump_flags & CURSOR_MOVED) != 0;
2709 }
2710 else
2711 return FALSE;
2712#else
2713 return FALSE;
2714#endif
2715 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002716 if (which_button == MOUSE_LEFT
2717 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 {
2719 which_button = MOUSE_RIGHT;
2720 mod_mask &= ~MOD_MASK_SHIFT;
2721 }
2722 }
2723
2724#ifdef FEAT_VISUAL
2725 if ((State & (NORMAL | INSERT))
2726 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2727 {
2728 if (which_button == MOUSE_LEFT)
2729 {
2730 if (is_click)
2731 {
2732 /* stop Visual mode for a left click in a window, but not when
2733 * on a status line */
2734 if (VIsual_active)
2735 jump_flags |= MOUSE_MAY_STOP_VIS;
2736 }
2737 else if (mouse_has(MOUSE_VISUAL))
2738 jump_flags |= MOUSE_MAY_VIS;
2739 }
2740 else if (which_button == MOUSE_RIGHT)
2741 {
2742 if (is_click && VIsual_active)
2743 {
2744 /*
2745 * Remember the start and end of visual before moving the
2746 * cursor.
2747 */
2748 if (lt(curwin->w_cursor, VIsual))
2749 {
2750 start_visual = curwin->w_cursor;
2751 end_visual = VIsual;
2752 }
2753 else
2754 {
2755 start_visual = VIsual;
2756 end_visual = curwin->w_cursor;
2757 }
2758 }
2759 jump_flags |= MOUSE_FOCUS;
2760 if (mouse_has(MOUSE_VISUAL))
2761 jump_flags |= MOUSE_MAY_VIS;
2762 }
2763 }
2764#endif
2765
2766 /*
2767 * If an operator is pending, ignore all drags and releases until the
2768 * next mouse click.
2769 */
2770 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2771 {
2772 got_click = FALSE;
2773 oap->motion_type = MCHAR;
2774 }
2775
2776 /* When releasing the button let jump_to_mouse() know. */
2777 if (!is_click && !is_drag)
2778 jump_flags |= MOUSE_RELEASED;
2779
2780 /*
2781 * JUMP!
2782 */
2783 jump_flags = jump_to_mouse(jump_flags,
2784 oap == NULL ? NULL : &(oap->inclusive), which_button);
2785 moved = (jump_flags & CURSOR_MOVED);
2786 in_status_line = (jump_flags & IN_STATUS_LINE);
2787#ifdef FEAT_VERTSPLIT
2788 in_sep_line = (jump_flags & IN_SEP_LINE);
2789#endif
2790
2791#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002792 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2794 {
2795 int key = KEY2TERMCAP1(c);
2796
2797 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2798 || key == (int)KE_RIGHTRELEASE)
2799 netbeans_button_release(which_button);
2800 }
2801#endif
2802
2803 /* When jumping to another window, clear a pending operator. That's a bit
2804 * friendlier than beeping and not jumping to that window. */
2805 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2806 clearop(oap);
2807
2808#ifdef FEAT_FOLDING
2809 if (mod_mask == 0
2810 && !is_drag
2811 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2812 && which_button == MOUSE_LEFT)
2813 {
2814 /* open or close a fold at this line */
2815 if (jump_flags & MOUSE_FOLD_OPEN)
2816 openFold(curwin->w_cursor.lnum, 1L);
2817 else
2818 closeFold(curwin->w_cursor.lnum, 1L);
2819 /* don't move the cursor if still in the same window */
2820 if (curwin == old_curwin)
2821 curwin->w_cursor = save_cursor;
2822 }
2823#endif
2824
2825#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2826 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2827 {
2828 clip_modeless(which_button, is_click, is_drag);
2829 return FALSE;
2830 }
2831#endif
2832
2833#ifdef FEAT_VISUAL
2834 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002835 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 if (VIsual_active && is_drag && p_so)
2837 {
2838 /* In the very first line, allow scrolling one line */
2839 if (mouse_row == 0)
2840 mouse_dragging = 2;
2841 else
2842 mouse_dragging = 1;
2843 }
2844
2845 /* When dragging the mouse above the window, scroll down. */
2846 if (is_drag && mouse_row < 0 && !in_status_line)
2847 {
2848 scroll_redraw(FALSE, 1L);
2849 mouse_row = 0;
2850 }
2851
2852 if (start_visual.lnum) /* right click in visual mode */
2853 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002854 /* When ALT is pressed make Visual mode blockwise. */
2855 if (mod_mask & MOD_MASK_ALT)
2856 VIsual_mode = Ctrl_V;
2857
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 /*
2859 * In Visual-block mode, divide the area in four, pick up the corner
2860 * that is in the quarter that the cursor is in.
2861 */
2862 if (VIsual_mode == Ctrl_V)
2863 {
2864 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2865 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2866 end_visual.col = leftcol;
2867 else
2868 end_visual.col = rightcol;
2869 if (curwin->w_cursor.lnum <
2870 (start_visual.lnum + end_visual.lnum) / 2)
2871 end_visual.lnum = end_visual.lnum;
2872 else
2873 end_visual.lnum = start_visual.lnum;
2874
2875 /* move VIsual to the right column */
2876 start_visual = curwin->w_cursor; /* save the cursor pos */
2877 curwin->w_cursor = end_visual;
2878 coladvance(end_visual.col);
2879 VIsual = curwin->w_cursor;
2880 curwin->w_cursor = start_visual; /* restore the cursor */
2881 }
2882 else
2883 {
2884 /*
2885 * If the click is before the start of visual, change the start.
2886 * If the click is after the end of visual, change the end. If
2887 * the click is inside the visual, change the closest side.
2888 */
2889 if (lt(curwin->w_cursor, start_visual))
2890 VIsual = end_visual;
2891 else if (lt(end_visual, curwin->w_cursor))
2892 VIsual = start_visual;
2893 else
2894 {
2895 /* In the same line, compare column number */
2896 if (end_visual.lnum == start_visual.lnum)
2897 {
2898 if (curwin->w_cursor.col - start_visual.col >
2899 end_visual.col - curwin->w_cursor.col)
2900 VIsual = start_visual;
2901 else
2902 VIsual = end_visual;
2903 }
2904
2905 /* In different lines, compare line number */
2906 else
2907 {
2908 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2909 (end_visual.lnum - curwin->w_cursor.lnum);
2910
2911 if (diff > 0) /* closest to end */
2912 VIsual = start_visual;
2913 else if (diff < 0) /* closest to start */
2914 VIsual = end_visual;
2915 else /* in the middle line */
2916 {
2917 if (curwin->w_cursor.col <
2918 (start_visual.col + end_visual.col) / 2)
2919 VIsual = end_visual;
2920 else
2921 VIsual = start_visual;
2922 }
2923 }
2924 }
2925 }
2926 }
2927 /*
2928 * If Visual mode started in insert mode, execute "CTRL-O"
2929 */
2930 else if ((State & INSERT) && VIsual_active)
2931 stuffcharReadbuff(Ctrl_O);
2932#endif
2933
2934 /*
2935 * Middle mouse click: Put text before cursor.
2936 */
2937 if (which_button == MOUSE_MIDDLE)
2938 {
2939#ifdef FEAT_CLIPBOARD
2940 if (clip_star.available && regname == 0)
2941 regname = '*';
2942#endif
2943 if (yank_register_mline(regname))
2944 {
2945 if (mouse_past_bottom)
2946 dir = FORWARD;
2947 }
2948 else if (mouse_past_eol)
2949 dir = FORWARD;
2950
2951 if (fixindent)
2952 {
2953 c1 = (dir == BACKWARD) ? '[' : ']';
2954 c2 = 'p';
2955 }
2956 else
2957 {
2958 c1 = (dir == FORWARD) ? 'p' : 'P';
2959 c2 = NUL;
2960 }
2961 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2962
2963 /*
2964 * Remember where the paste started, so in edit() Insstart can be set
2965 * to this position
2966 */
2967 if (restart_edit != 0)
2968 where_paste_started = curwin->w_cursor;
2969 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2970 }
2971
2972#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2973 /*
2974 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2975 * error under the mouse pointer.
2976 */
2977 else if (((mod_mask & MOD_MASK_CTRL)
2978 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2979 && bt_quickfix(curbuf))
2980 {
2981 if (State & INSERT)
2982 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002983 if (curwin->w_llist_ref == NULL) /* quickfix window */
2984 stuffReadbuff((char_u *)":.cc\n");
2985 else /* location list window */
2986 stuffReadbuff((char_u *)":.ll\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 got_click = FALSE; /* ignore drag&release now */
2988 }
2989#endif
2990
2991 /*
2992 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2993 * under the mouse pointer.
2994 */
2995 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2996 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2997 {
2998 if (State & INSERT)
2999 stuffcharReadbuff(Ctrl_O);
3000 stuffcharReadbuff(Ctrl_RSB);
3001 got_click = FALSE; /* ignore drag&release now */
3002 }
3003
3004 /*
3005 * Shift-Mouse click searches for the next occurrence of the word under
3006 * the mouse pointer
3007 */
3008 else if ((mod_mask & MOD_MASK_SHIFT))
3009 {
3010 if (State & INSERT
3011#ifdef FEAT_VISUAL
3012 || (VIsual_active && VIsual_select)
3013#endif
3014 )
3015 stuffcharReadbuff(Ctrl_O);
3016 if (which_button == MOUSE_LEFT)
3017 stuffcharReadbuff('*');
3018 else /* MOUSE_RIGHT */
3019 stuffcharReadbuff('#');
3020 }
3021
3022 /* Handle double clicks, unless on status line */
3023 else if (in_status_line)
3024 {
3025#ifdef FEAT_MOUSESHAPE
3026 if ((is_drag || is_click) && !drag_status_line)
3027 {
3028 drag_status_line = TRUE;
3029 update_mouseshape(-1);
3030 }
3031#endif
3032 }
3033#ifdef FEAT_VERTSPLIT
3034 else if (in_sep_line)
3035 {
3036# ifdef FEAT_MOUSESHAPE
3037 if ((is_drag || is_click) && !drag_sep_line)
3038 {
3039 drag_sep_line = TRUE;
3040 update_mouseshape(-1);
3041 }
3042# endif
3043 }
3044#endif
3045#ifdef FEAT_VISUAL
3046 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3047 && mouse_has(MOUSE_VISUAL))
3048 {
3049 if (is_click || !VIsual_active)
3050 {
3051 if (VIsual_active)
3052 orig_cursor = VIsual;
3053 else
3054 {
3055 check_visual_highlight();
3056 VIsual = curwin->w_cursor;
3057 orig_cursor = VIsual;
3058 VIsual_active = TRUE;
3059 VIsual_reselect = TRUE;
3060 /* start Select mode if 'selectmode' contains "mouse" */
3061 may_start_select('o');
3062 setmouse();
3063 }
3064 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003065 {
3066 /* Double click with ALT pressed makes it blockwise. */
3067 if (mod_mask & MOD_MASK_ALT)
3068 VIsual_mode = Ctrl_V;
3069 else
3070 VIsual_mode = 'v';
3071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3073 VIsual_mode = 'V';
3074 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3075 VIsual_mode = Ctrl_V;
3076#ifdef FEAT_CLIPBOARD
3077 /* Make sure the clipboard gets updated. Needed because start and
3078 * end may still be the same, and the selection needs to be owned */
3079 clip_star.vmode = NUL;
3080#endif
3081 }
3082 /*
3083 * A double click selects a word or a block.
3084 */
3085 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3086 {
3087 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003088 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089
3090 if (is_click)
3091 {
3092 /* If the character under the cursor (skipping white space) is
3093 * not a word character, try finding a match and select a (),
3094 * {}, [], #if/#endif, etc. block. */
3095 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003096 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 inc(&end_visual);
3098 if (oap != NULL)
3099 oap->motion_type = MCHAR;
3100 if (oap != NULL
3101 && VIsual_mode == 'v'
3102 && !vim_iswordc(gchar_pos(&end_visual))
3103 && equalpos(curwin->w_cursor, VIsual)
3104 && (pos = findmatch(oap, NUL)) != NULL)
3105 {
3106 curwin->w_cursor = *pos;
3107 if (oap->motion_type == MLINE)
3108 VIsual_mode = 'V';
3109 else if (*p_sel == 'e')
3110 {
3111 if (lt(curwin->w_cursor, VIsual))
3112 ++VIsual.col;
3113 else
3114 ++curwin->w_cursor.col;
3115 }
3116 }
3117 }
3118
3119 if (pos == NULL && (is_click || is_drag))
3120 {
3121 /* When not found a match or when dragging: extend to include
3122 * a word. */
3123 if (lt(curwin->w_cursor, orig_cursor))
3124 {
3125 find_start_of_word(&curwin->w_cursor);
3126 find_end_of_word(&VIsual);
3127 }
3128 else
3129 {
3130 find_start_of_word(&VIsual);
3131 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3132#ifdef FEAT_MBYTE
3133 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003134 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135#else
3136 ++curwin->w_cursor.col;
3137#endif
3138 find_end_of_word(&curwin->w_cursor);
3139 }
3140 }
3141 curwin->w_set_curswant = TRUE;
3142 }
3143 if (is_click)
3144 redraw_curbuf_later(INVERTED); /* update the inversion */
3145 }
3146 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003147 {
3148 if (mod_mask & MOD_MASK_ALT)
3149 VIsual_mode = Ctrl_V;
3150 else
3151 VIsual_mode = 'v';
3152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153
3154 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003155 if ((!VIsual_active && old_active && mode_displayed)
3156 || (VIsual_active && p_smd && msg_silent == 0
3157 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 redraw_cmdline = TRUE;
3159#endif
3160
3161 return moved;
3162}
3163
3164#ifdef FEAT_VISUAL
3165/*
3166 * Move "pos" back to the start of the word it's in.
3167 */
3168 static void
3169find_start_of_word(pos)
3170 pos_T *pos;
3171{
3172 char_u *line;
3173 int cclass;
3174 int col;
3175
3176 line = ml_get(pos->lnum);
3177 cclass = get_mouse_class(line + pos->col);
3178
3179 while (pos->col > 0)
3180 {
3181 col = pos->col - 1;
3182#ifdef FEAT_MBYTE
3183 col -= (*mb_head_off)(line, line + col);
3184#endif
3185 if (get_mouse_class(line + col) != cclass)
3186 break;
3187 pos->col = col;
3188 }
3189}
3190
3191/*
3192 * Move "pos" forward to the end of the word it's in.
3193 * When 'selection' is "exclusive", the position is just after the word.
3194 */
3195 static void
3196find_end_of_word(pos)
3197 pos_T *pos;
3198{
3199 char_u *line;
3200 int cclass;
3201 int col;
3202
3203 line = ml_get(pos->lnum);
3204 if (*p_sel == 'e' && pos->col > 0)
3205 {
3206 --pos->col;
3207#ifdef FEAT_MBYTE
3208 pos->col -= (*mb_head_off)(line, line + pos->col);
3209#endif
3210 }
3211 cclass = get_mouse_class(line + pos->col);
3212 while (line[pos->col] != NUL)
3213 {
3214#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003215 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216#else
3217 col = pos->col + 1;
3218#endif
3219 if (get_mouse_class(line + col) != cclass)
3220 {
3221 if (*p_sel == 'e')
3222 pos->col = col;
3223 break;
3224 }
3225 pos->col = col;
3226 }
3227}
3228
3229/*
3230 * Get class of a character for selection: same class means same word.
3231 * 0: blank
3232 * 1: punctuation groups
3233 * 2: normal word character
3234 * >2: multi-byte word character.
3235 */
3236 static int
3237get_mouse_class(p)
3238 char_u *p;
3239{
3240 int c;
3241
3242#ifdef FEAT_MBYTE
3243 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3244 return mb_get_class(p);
3245#endif
3246
3247 c = *p;
3248 if (c == ' ' || c == '\t')
3249 return 0;
3250
3251 if (vim_iswordc(c))
3252 return 2;
3253
3254 /*
3255 * There are a few special cases where we want certain combinations of
3256 * characters to be considered as a single word. These are things like
3257 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003258 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 */
3260 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3261 return 1;
3262 return c;
3263}
3264#endif /* FEAT_VISUAL */
3265#endif /* FEAT_MOUSE */
3266
3267#if defined(FEAT_VISUAL) || defined(PROTO)
3268/*
3269 * Check if highlighting for visual mode is possible, give a warning message
3270 * if not.
3271 */
3272 void
3273check_visual_highlight()
3274{
3275 static int did_check = FALSE;
3276
3277 if (full_screen)
3278 {
3279 if (!did_check && hl_attr(HLF_V) == 0)
3280 MSG(_("Warning: terminal cannot highlight"));
3281 did_check = TRUE;
3282 }
3283}
3284
3285/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003286 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 * This function should ALWAYS be called to end Visual mode, except from
3288 * do_pending_operator().
3289 */
3290 void
3291end_visual_mode()
3292{
3293#ifdef FEAT_CLIPBOARD
3294 /*
3295 * If we are using the clipboard, then remember what was selected in case
3296 * we need to paste it somewhere while we still own the selection.
3297 * Only do this when the clipboard is already owned. Don't want to grab
3298 * the selection when hitting ESC.
3299 */
3300 if (clip_star.available && clip_star.owned)
3301 clip_auto_select();
3302#endif
3303
3304 VIsual_active = FALSE;
3305#ifdef FEAT_MOUSE
3306 setmouse();
3307 mouse_dragging = 0;
3308#endif
3309
3310 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003311 curbuf->b_visual.vi_mode = VIsual_mode;
3312 curbuf->b_visual.vi_start = VIsual;
3313 curbuf->b_visual.vi_end = curwin->w_cursor;
3314 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315#ifdef FEAT_EVAL
3316 curbuf->b_visual_mode_eval = VIsual_mode;
3317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318#ifdef FEAT_VIRTUALEDIT
3319 if (!virtual_active())
3320 curwin->w_cursor.coladd = 0;
3321#endif
3322
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003323 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 clear_cmdline = TRUE; /* unshow visual mode later */
3325#ifdef FEAT_CMDL_INFO
3326 else
3327 clear_showcmd();
3328#endif
3329
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003330 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331}
3332
3333/*
3334 * Reset VIsual_active and VIsual_reselect.
3335 */
3336 void
3337reset_VIsual_and_resel()
3338{
3339 if (VIsual_active)
3340 {
3341 end_visual_mode();
3342 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3343 }
3344 VIsual_reselect = FALSE;
3345}
3346
3347/*
3348 * Reset VIsual_active and VIsual_reselect if it's set.
3349 */
3350 void
3351reset_VIsual()
3352{
3353 if (VIsual_active)
3354 {
3355 end_visual_mode();
3356 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3357 VIsual_reselect = FALSE;
3358 }
3359}
3360#endif /* FEAT_VISUAL */
3361
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003362#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3364
3365/*
3366 * Check for a balloon-eval special item to include when searching for an
3367 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3368 * Returns TRUE if the character at "*ptr" should be included.
3369 * "dir" is FORWARD or BACKWARD, the direction of searching.
3370 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3371 * "bnp" points to a counter for square brackets.
3372 */
3373 static int
3374find_is_eval_item(ptr, colp, bnp, dir)
3375 char_u *ptr;
3376 int *colp;
3377 int *bnp;
3378 int dir;
3379{
3380 /* Accept everything inside []. */
3381 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3382 ++*bnp;
3383 if (*bnp > 0)
3384 {
3385 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3386 --*bnp;
3387 return TRUE;
3388 }
3389
3390 /* skip over "s.var" */
3391 if (*ptr == '.')
3392 return TRUE;
3393
3394 /* two-character item: s->var */
3395 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3396 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3397 {
3398 *colp += dir;
3399 return TRUE;
3400 }
3401 return FALSE;
3402}
3403#endif
3404
3405/*
3406 * Find the identifier under or to the right of the cursor.
3407 * "find_type" can have one of three values:
3408 * FIND_IDENT: find an identifier (keyword)
3409 * FIND_STRING: find any non-white string
3410 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003411 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 *
3413 * There are three steps:
3414 * 1. Search forward for the start of an identifier/string. Doesn't move if
3415 * already on one.
3416 * 2. Search backward for the start of this identifier/string.
3417 * This doesn't match the real Vi but I like it a little better and it
3418 * shouldn't bother anyone.
3419 * 3. Search forward to the end of this identifier/string.
3420 * When FIND_IDENT isn't defined, we backup until a blank.
3421 *
3422 * Returns the length of the string, or zero if no string is found.
3423 * If a string is found, a pointer to the string is put in "*string". This
3424 * string is not always NUL terminated.
3425 */
3426 int
3427find_ident_under_cursor(string, find_type)
3428 char_u **string;
3429 int find_type;
3430{
3431 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3432 curwin->w_cursor.col, string, find_type);
3433}
3434
3435/*
3436 * Like find_ident_under_cursor(), but for any window and any position.
3437 * However: Uses 'iskeyword' from the current window!.
3438 */
3439 int
3440find_ident_at_pos(wp, lnum, startcol, string, find_type)
3441 win_T *wp;
3442 linenr_T lnum;
3443 colnr_T startcol;
3444 char_u **string;
3445 int find_type;
3446{
3447 char_u *ptr;
3448 int col = 0; /* init to shut up GCC */
3449 int i;
3450#ifdef FEAT_MBYTE
3451 int this_class = 0;
3452 int prev_class;
3453 int prevcol;
3454#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003455#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 int bn = 0; /* bracket nesting */
3457#endif
3458
3459 /*
3460 * if i == 0: try to find an identifier
3461 * if i == 1: try to find any non-white string
3462 */
3463 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3464 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3465 {
3466 /*
3467 * 1. skip to start of identifier/string
3468 */
3469 col = startcol;
3470#ifdef FEAT_MBYTE
3471 if (has_mbyte)
3472 {
3473 while (ptr[col] != NUL)
3474 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003475# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 /* Stop at a ']' to evaluate "a[x]". */
3477 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3478 break;
3479# endif
3480 this_class = mb_get_class(ptr + col);
3481 if (this_class != 0 && (i == 1 || this_class != 1))
3482 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003483 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 }
3485 }
3486 else
3487#endif
3488 while (ptr[col] != NUL
3489 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003490# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3492# endif
3493 )
3494 ++col;
3495
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003496#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 /* When starting on a ']' count it, so that we include the '['. */
3498 bn = ptr[col] == ']';
3499#endif
3500
3501 /*
3502 * 2. Back up to start of identifier/string.
3503 */
3504#ifdef FEAT_MBYTE
3505 if (has_mbyte)
3506 {
3507 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003508# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3510 this_class = mb_get_class((char_u *)"a");
3511 else
3512# endif
3513 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003514 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 {
3516 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3517 prev_class = mb_get_class(ptr + prevcol);
3518 if (this_class != prev_class
3519 && (i == 0
3520 || prev_class == 0
3521 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003522# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 && (!(find_type & FIND_EVAL)
3524 || prevcol == 0
3525 || !find_is_eval_item(ptr + prevcol, &prevcol,
3526 &bn, BACKWARD))
3527# endif
3528 )
3529 break;
3530 col = prevcol;
3531 }
3532
3533 /* If we don't want just any old string, or we've found an
3534 * identifier, stop searching. */
3535 if (this_class > 2)
3536 this_class = 2;
3537 if (!(find_type & FIND_STRING) || this_class == 2)
3538 break;
3539 }
3540 else
3541#endif
3542 {
3543 while (col > 0
3544 && ((i == 0
3545 ? vim_iswordc(ptr[col - 1])
3546 : (!vim_iswhite(ptr[col - 1])
3547 && (!(find_type & FIND_IDENT)
3548 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003549#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 || ((find_type & FIND_EVAL)
3551 && col > 1
3552 && find_is_eval_item(ptr + col - 1, &col,
3553 &bn, BACKWARD))
3554#endif
3555 ))
3556 --col;
3557
3558 /* If we don't want just any old string, or we've found an
3559 * identifier, stop searching. */
3560 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3561 break;
3562 }
3563 }
3564
3565 if (ptr[col] == NUL || (i == 0 && (
3566#ifdef FEAT_MBYTE
3567 has_mbyte ? this_class != 2 :
3568#endif
3569 !vim_iswordc(ptr[col]))))
3570 {
3571 /*
3572 * didn't find an identifier or string
3573 */
3574 if (find_type & FIND_STRING)
3575 EMSG(_("E348: No string under cursor"));
3576 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003577 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 return 0;
3579 }
3580 ptr += col;
3581 *string = ptr;
3582
3583 /*
3584 * 3. Find the end if the identifier/string.
3585 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003586#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 bn = 0;
3588 startcol -= col;
3589#endif
3590 col = 0;
3591#ifdef FEAT_MBYTE
3592 if (has_mbyte)
3593 {
3594 /* Search for point of changing multibyte character class. */
3595 this_class = mb_get_class(ptr);
3596 while (ptr[col] != NUL
3597 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3598 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003599# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 || ((find_type & FIND_EVAL)
3601 && col <= (int)startcol
3602 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3603# endif
3604 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003605 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 }
3607 else
3608#endif
3609 while ((i == 0 ? vim_iswordc(ptr[col])
3610 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003611# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 || ((find_type & FIND_EVAL)
3613 && col <= (int)startcol
3614 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3615# endif
3616 )
3617 {
3618 ++col;
3619 }
3620
3621 return col;
3622}
3623
3624/*
3625 * Prepare for redo of a normal command.
3626 */
3627 static void
3628prep_redo_cmd(cap)
3629 cmdarg_T *cap;
3630{
3631 prep_redo(cap->oap->regname, cap->count0,
3632 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3633}
3634
3635/*
3636 * Prepare for redo of any command.
3637 * Note that only the last argument can be a multi-byte char.
3638 */
3639 static void
3640prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3641 int regname;
3642 long num;
3643 int cmd1;
3644 int cmd2;
3645 int cmd3;
3646 int cmd4;
3647 int cmd5;
3648{
3649 ResetRedobuff();
3650 if (regname != 0) /* yank from specified buffer */
3651 {
3652 AppendCharToRedobuff('"');
3653 AppendCharToRedobuff(regname);
3654 }
3655 if (num)
3656 AppendNumberToRedobuff(num);
3657
3658 if (cmd1 != NUL)
3659 AppendCharToRedobuff(cmd1);
3660 if (cmd2 != NUL)
3661 AppendCharToRedobuff(cmd2);
3662 if (cmd3 != NUL)
3663 AppendCharToRedobuff(cmd3);
3664 if (cmd4 != NUL)
3665 AppendCharToRedobuff(cmd4);
3666 if (cmd5 != NUL)
3667 AppendCharToRedobuff(cmd5);
3668}
3669
3670/*
3671 * check for operator active and clear it
3672 *
3673 * return TRUE if operator was active
3674 */
3675 static int
3676checkclearop(oap)
3677 oparg_T *oap;
3678{
3679 if (oap->op_type == OP_NOP)
3680 return FALSE;
3681 clearopbeep(oap);
3682 return TRUE;
3683}
3684
3685/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003686 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003688 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 */
3690 static int
3691checkclearopq(oap)
3692 oparg_T *oap;
3693{
3694 if (oap->op_type == OP_NOP
3695#ifdef FEAT_VISUAL
3696 && !VIsual_active
3697#endif
3698 )
3699 return FALSE;
3700 clearopbeep(oap);
3701 return TRUE;
3702}
3703
3704 static void
3705clearop(oap)
3706 oparg_T *oap;
3707{
3708 oap->op_type = OP_NOP;
3709 oap->regname = 0;
3710 oap->motion_force = NUL;
3711 oap->use_reg_one = FALSE;
3712}
3713
3714 static void
3715clearopbeep(oap)
3716 oparg_T *oap;
3717{
3718 clearop(oap);
3719 beep_flush();
3720}
3721
3722#ifdef FEAT_VISUAL
3723/*
3724 * Remove the shift modifier from a special key.
3725 */
3726 static void
3727unshift_special(cap)
3728 cmdarg_T *cap;
3729{
3730 switch (cap->cmdchar)
3731 {
3732 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3733 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3734 case K_S_UP: cap->cmdchar = K_UP; break;
3735 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3736 case K_S_HOME: cap->cmdchar = K_HOME; break;
3737 case K_S_END: cap->cmdchar = K_END; break;
3738 }
3739 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3740}
3741#endif
3742
3743#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3744/*
3745 * Routines for displaying a partly typed command
3746 */
3747
3748#ifdef FEAT_VISUAL /* need room for size of Visual area */
3749# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3750#else
3751# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3752#endif
3753static char_u showcmd_buf[SHOWCMD_BUFLEN];
3754static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3755static int showcmd_is_clear = TRUE;
3756static int showcmd_visual = FALSE;
3757
3758static void display_showcmd __ARGS((void));
3759
3760 void
3761clear_showcmd()
3762{
3763 if (!p_sc)
3764 return;
3765
3766#ifdef FEAT_VISUAL
3767 if (VIsual_active && !char_avail())
3768 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003769 int cursor_bot = lt(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 long lines;
3771 colnr_T leftcol, rightcol;
3772 linenr_T top, bot;
3773
3774 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003775 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 {
3777 top = VIsual.lnum;
3778 bot = curwin->w_cursor.lnum;
3779 }
3780 else
3781 {
3782 top = curwin->w_cursor.lnum;
3783 bot = VIsual.lnum;
3784 }
3785# ifdef FEAT_FOLDING
3786 /* Include closed folds as a whole. */
3787 hasFolding(top, &top, NULL);
3788 hasFolding(bot, NULL, &bot);
3789# endif
3790 lines = bot - top + 1;
3791
3792 if (VIsual_mode == Ctrl_V)
3793 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003794# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003795 char_u *saved_sbr = p_sbr;
3796
3797 /* Make 'sbr' empty for a moment to get the correct size. */
3798 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003799# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003801# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003802 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003803# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3805 (long)(rightcol - leftcol + 1));
3806 }
3807 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3808 sprintf((char *)showcmd_buf, "%ld", lines);
3809 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003810 {
3811 char_u *s, *e;
3812 int l;
3813 int bytes = 0;
3814 int chars = 0;
3815
3816 if (cursor_bot)
3817 {
3818 s = ml_get_pos(&VIsual);
3819 e = ml_get_cursor();
3820 }
3821 else
3822 {
3823 s = ml_get_cursor();
3824 e = ml_get_pos(&VIsual);
3825 }
3826 while ((*p_sel != 'e') ? s <= e : s < e)
3827 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003828# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003829 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003830# else
3831 l = (*s == NUL) ? 0 : 1;
3832# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003833 if (l == 0)
3834 {
3835 ++bytes;
3836 ++chars;
3837 break; /* end of line */
3838 }
3839 bytes += l;
3840 ++chars;
3841 s += l;
3842 }
3843 if (bytes == chars)
3844 sprintf((char *)showcmd_buf, "%d", chars);
3845 else
3846 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3849 showcmd_visual = TRUE;
3850 }
3851 else
3852#endif
3853 {
3854 showcmd_buf[0] = NUL;
3855 showcmd_visual = FALSE;
3856
3857 /* Don't actually display something if there is nothing to clear. */
3858 if (showcmd_is_clear)
3859 return;
3860 }
3861
3862 display_showcmd();
3863}
3864
3865/*
3866 * Add 'c' to string of shown command chars.
3867 * Return TRUE if output has been written (and setcursor() has been called).
3868 */
3869 int
3870add_to_showcmd(c)
3871 int c;
3872{
3873 char_u *p;
3874 int old_len;
3875 int extra_len;
3876 int overflow;
3877#if defined(FEAT_MOUSE)
3878 int i;
3879 static int ignore[] =
3880 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003881# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3883 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003884# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 K_IGNORE,
3886 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3887 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3888 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003889 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003891 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 0
3893 };
3894#endif
3895
Bram Moolenaar09df3122006-01-23 22:23:09 +00003896 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 return FALSE;
3898
3899 if (showcmd_visual)
3900 {
3901 showcmd_buf[0] = NUL;
3902 showcmd_visual = FALSE;
3903 }
3904
3905#if defined(FEAT_MOUSE)
3906 /* Ignore keys that are scrollbar updates and mouse clicks */
3907 if (IS_SPECIAL(c))
3908 for (i = 0; ignore[i] != 0; ++i)
3909 if (ignore[i] == c)
3910 return FALSE;
3911#endif
3912
3913 p = transchar(c);
3914 old_len = (int)STRLEN(showcmd_buf);
3915 extra_len = (int)STRLEN(p);
3916 overflow = old_len + extra_len - SHOWCMD_COLS;
3917 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003918 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3919 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 STRCAT(showcmd_buf, p);
3921
3922 if (char_avail())
3923 return FALSE;
3924
3925 display_showcmd();
3926
3927 return TRUE;
3928}
3929
3930 void
3931add_to_showcmd_c(c)
3932 int c;
3933{
3934 if (!add_to_showcmd(c))
3935 setcursor();
3936}
3937
3938/*
3939 * Delete 'len' characters from the end of the shown command.
3940 */
3941 static void
3942del_from_showcmd(len)
3943 int len;
3944{
3945 int old_len;
3946
3947 if (!p_sc)
3948 return;
3949
3950 old_len = (int)STRLEN(showcmd_buf);
3951 if (len > old_len)
3952 len = old_len;
3953 showcmd_buf[old_len - len] = NUL;
3954
3955 if (!char_avail())
3956 display_showcmd();
3957}
3958
3959/*
3960 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3961 * something and there is a partial mapping.
3962 */
3963 void
3964push_showcmd()
3965{
3966 if (p_sc)
3967 STRCPY(old_showcmd_buf, showcmd_buf);
3968}
3969
3970 void
3971pop_showcmd()
3972{
3973 if (!p_sc)
3974 return;
3975
3976 STRCPY(showcmd_buf, old_showcmd_buf);
3977
3978 display_showcmd();
3979}
3980
3981 static void
3982display_showcmd()
3983{
3984 int len;
3985
3986 cursor_off();
3987
3988 len = (int)STRLEN(showcmd_buf);
3989 if (len == 0)
3990 showcmd_is_clear = TRUE;
3991 else
3992 {
3993 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3994 showcmd_is_clear = FALSE;
3995 }
3996
3997 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003998 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3999 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 */
4001 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
4002
4003 setcursor(); /* put cursor back where it belongs */
4004}
4005#endif
4006
4007#ifdef FEAT_SCROLLBIND
4008/*
4009 * When "check" is FALSE, prepare for commands that scroll the window.
4010 * When "check" is TRUE, take care of scroll-binding after the window has
4011 * scrolled. Called from normal_cmd() and edit().
4012 */
4013 void
4014do_check_scrollbind(check)
4015 int check;
4016{
4017 static win_T *old_curwin = NULL;
4018 static linenr_T old_topline = 0;
4019#ifdef FEAT_DIFF
4020 static int old_topfill = 0;
4021#endif
4022 static buf_T *old_buf = NULL;
4023 static colnr_T old_leftcol = 0;
4024
4025 if (check && curwin->w_p_scb)
4026 {
4027 /* If a ":syncbind" command was just used, don't scroll, only reset
4028 * the values. */
4029 if (did_syncbind)
4030 did_syncbind = FALSE;
4031 else if (curwin == old_curwin)
4032 {
4033 /*
4034 * Synchronize other windows, as necessary according to
4035 * 'scrollbind'. Don't do this after an ":edit" command, except
4036 * when 'diff' is set.
4037 */
4038 if ((curwin->w_buffer == old_buf
4039#ifdef FEAT_DIFF
4040 || curwin->w_p_diff
4041#endif
4042 )
4043 && (curwin->w_topline != old_topline
4044#ifdef FEAT_DIFF
4045 || curwin->w_topfill != old_topfill
4046#endif
4047 || curwin->w_leftcol != old_leftcol))
4048 {
4049 check_scrollbind(curwin->w_topline - old_topline,
4050 (long)(curwin->w_leftcol - old_leftcol));
4051 }
4052 }
4053 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4054 {
4055 /*
4056 * When switching between windows, make sure that the relative
4057 * vertical offset is valid for the new window. The relative
4058 * offset is invalid whenever another 'scrollbind' window has
4059 * scrolled to a point that would force the current window to
4060 * scroll past the beginning or end of its buffer. When the
4061 * resync is performed, some of the other 'scrollbind' windows may
4062 * need to jump so that the current window's relative position is
4063 * visible on-screen.
4064 */
4065 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4066 }
4067 curwin->w_scbind_pos = curwin->w_topline;
4068 }
4069
4070 old_curwin = curwin;
4071 old_topline = curwin->w_topline;
4072#ifdef FEAT_DIFF
4073 old_topfill = curwin->w_topfill;
4074#endif
4075 old_buf = curwin->w_buffer;
4076 old_leftcol = curwin->w_leftcol;
4077}
4078
4079/*
4080 * Synchronize any windows that have "scrollbind" set, based on the
4081 * number of rows by which the current window has changed
4082 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4083 */
4084 void
4085check_scrollbind(topline_diff, leftcol_diff)
4086 linenr_T topline_diff;
4087 long leftcol_diff;
4088{
4089 int want_ver;
4090 int want_hor;
4091 win_T *old_curwin = curwin;
4092 buf_T *old_curbuf = curbuf;
4093#ifdef FEAT_VISUAL
4094 int old_VIsual_select = VIsual_select;
4095 int old_VIsual_active = VIsual_active;
4096#endif
4097 colnr_T tgt_leftcol = curwin->w_leftcol;
4098 long topline;
4099 long y;
4100
4101 /*
4102 * check 'scrollopt' string for vertical and horizontal scroll options
4103 */
4104 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4105#ifdef FEAT_DIFF
4106 want_ver |= old_curwin->w_p_diff;
4107#endif
4108 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4109
4110 /*
4111 * loop through the scrollbound windows and scroll accordingly
4112 */
4113#ifdef FEAT_VISUAL
4114 VIsual_select = VIsual_active = 0;
4115#endif
4116 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4117 {
4118 curbuf = curwin->w_buffer;
4119 /* skip original window and windows with 'noscrollbind' */
4120 if (curwin != old_curwin && curwin->w_p_scb)
4121 {
4122 /*
4123 * do the vertical scroll
4124 */
4125 if (want_ver)
4126 {
4127#ifdef FEAT_DIFF
4128 if (old_curwin->w_p_diff && curwin->w_p_diff)
4129 {
4130 diff_set_topline(old_curwin, curwin);
4131 }
4132 else
4133#endif
4134 {
4135 curwin->w_scbind_pos += topline_diff;
4136 topline = curwin->w_scbind_pos;
4137 if (topline > curbuf->b_ml.ml_line_count)
4138 topline = curbuf->b_ml.ml_line_count;
4139 if (topline < 1)
4140 topline = 1;
4141
4142 y = topline - curwin->w_topline;
4143 if (y > 0)
4144 scrollup(y, FALSE);
4145 else
4146 scrolldown(-y, FALSE);
4147 }
4148
4149 redraw_later(VALID);
4150 cursor_correct();
4151#ifdef FEAT_WINDOWS
4152 curwin->w_redr_status = TRUE;
4153#endif
4154 }
4155
4156 /*
4157 * do the horizontal scroll
4158 */
4159 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4160 {
4161 curwin->w_leftcol = tgt_leftcol;
4162 leftcol_changed();
4163 }
4164 }
4165 }
4166
4167 /*
4168 * reset current-window
4169 */
4170#ifdef FEAT_VISUAL
4171 VIsual_select = old_VIsual_select;
4172 VIsual_active = old_VIsual_active;
4173#endif
4174 curwin = old_curwin;
4175 curbuf = old_curbuf;
4176}
4177#endif /* #ifdef FEAT_SCROLLBIND */
4178
4179/*
4180 * Command character that's ignored.
4181 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004182 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 static void
4185nv_ignore(cap)
4186 cmdarg_T *cap;
4187{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004188 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189}
4190
4191/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004192 * Command character that doesn't do anything, but unlike nv_ignore() does
4193 * start edit(). Used for "startinsert" executed while starting up.
4194 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004195 static void
4196nv_nop(cap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004197 cmdarg_T *cap UNUSED;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004198{
4199}
4200
4201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 * Command character doesn't exist.
4203 */
4204 static void
4205nv_error(cap)
4206 cmdarg_T *cap;
4207{
4208 clearopbeep(cap->oap);
4209}
4210
4211/*
4212 * <Help> and <F1> commands.
4213 */
4214 static void
4215nv_help(cap)
4216 cmdarg_T *cap;
4217{
4218 if (!checkclearopq(cap->oap))
4219 ex_help(NULL);
4220}
4221
4222/*
4223 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4224 */
4225 static void
4226nv_addsub(cap)
4227 cmdarg_T *cap;
4228{
4229 if (!checkclearopq(cap->oap)
4230 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4231 prep_redo_cmd(cap);
4232}
4233
4234/*
4235 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4236 */
4237 static void
4238nv_page(cap)
4239 cmdarg_T *cap;
4240{
4241 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004242 {
4243#ifdef FEAT_WINDOWS
4244 if (mod_mask & MOD_MASK_CTRL)
4245 {
4246 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4247 if (cap->arg == BACKWARD)
4248 goto_tabpage(-(int)cap->count1);
4249 else
4250 goto_tabpage((int)cap->count0);
4251 }
4252 else
4253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256}
4257
4258/*
4259 * Implementation of "gd" and "gD" command.
4260 */
4261 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004262nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004263 oparg_T *oap;
4264 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004265 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266{
4267 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 char_u *ptr;
4269
Bram Moolenaard9d30582005-05-18 22:10:28 +00004270 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004271 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004273#ifdef FEAT_FOLDING
4274 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4275 foldOpenCursor();
4276#endif
4277}
4278
4279/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004280 * Search for variable declaration of "ptr[len]".
4281 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4282 * current file ("gD").
4283 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004284 * Return FAIL when not found.
4285 */
4286 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004287find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004288 char_u *ptr;
4289 int len;
4290 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004291 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004292 int searchflags; /* flags passed to searchit() */
4293{
4294 char_u *pat;
4295 pos_T old_pos;
4296 pos_T par_pos;
4297 pos_T found_pos;
4298 int t;
4299 int save_p_ws;
4300 int save_p_scs;
4301 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004302 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004303
4304 if ((pat = alloc(len + 7)) == NULL)
4305 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004306
4307 /* Put "\V" before the pattern to avoid that the special meaning of "."
4308 * and "~" causes trouble. */
4309 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4310 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 old_pos = curwin->w_cursor;
4312 save_p_ws = p_ws;
4313 save_p_scs = p_scs;
4314 p_ws = FALSE; /* don't wrap around end of file now */
4315 p_scs = FALSE; /* don't switch ignorecase off now */
4316
4317 /*
4318 * With "gD" go to line 1.
4319 * With "gd" Search back for the start of the current function, then go
4320 * back until a blank line. If this fails go to line 1.
4321 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004322 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 {
4324 setpcmark(); /* Set in findpar() otherwise */
4325 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004326 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
4328 else
4329 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004330 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4332 --curwin->w_cursor.lnum;
4333 }
4334 curwin->w_cursor.col = 0;
4335
4336 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004337 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004338 for (;;)
4339 {
4340 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004341 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004342 if (curwin->w_cursor.lnum >= old_pos.lnum)
4343 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004344
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004345 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004346 {
4347 pos_T *pos;
4348
4349 /* Check that the block the match is in doesn't end before the
4350 * position where we started the search from. */
4351 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4352 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4353 && pos->lnum < old_pos.lnum)
4354 continue;
4355 }
4356
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004357 if (t == FAIL)
4358 {
4359 /* If we previously found a valid position, use it. */
4360 if (found_pos.lnum != 0)
4361 {
4362 curwin->w_cursor = found_pos;
4363 t = OK;
4364 }
4365 break;
4366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367#ifdef FEAT_COMMENTS
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004368 if (get_leader_len(ml_get_curline(), NULL, FALSE) > 0)
4369 {
4370 /* Ignore this line, continue at start of next line. */
4371 ++curwin->w_cursor.lnum;
4372 curwin->w_cursor.col = 0;
4373 continue;
4374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004376 if (!locally) /* global search: use first match found */
4377 break;
4378 if (curwin->w_cursor.lnum >= par_pos.lnum)
4379 {
4380 /* If we previously found a valid position, use it. */
4381 if (found_pos.lnum != 0)
4382 curwin->w_cursor = found_pos;
4383 break;
4384 }
4385
4386 /* For finding a local variable and the match is before the "{" search
4387 * to find a later match. For K&R style function declarations this
4388 * skips the function header without types. */
4389 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004391
4392 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004394 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 curwin->w_cursor = old_pos;
4396 }
4397 else
4398 {
4399 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 /* "n" searches forward now */
4401 reset_search_dir();
4402 }
4403
4404 vim_free(pat);
4405 p_ws = save_p_ws;
4406 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004407
4408 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409}
4410
4411/*
4412 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4413 * lines rather than lines in the file.
4414 * 'dist' must be positive.
4415 *
4416 * Return OK if able to move cursor, FAIL otherwise.
4417 */
4418 static int
4419nv_screengo(oap, dir, dist)
4420 oparg_T *oap;
4421 int dir;
4422 long dist;
4423{
4424 int linelen = linetabsize(ml_get_curline());
4425 int retval = OK;
4426 int atend = FALSE;
4427 int n;
4428 int col_off1; /* margin offset for first screen line */
4429 int col_off2; /* margin offset for wrapped screen line */
4430 int width1; /* text width for first screen line */
4431 int width2; /* test width for wrapped screen line */
4432
4433 oap->motion_type = MCHAR;
4434 oap->inclusive = FALSE;
4435
4436 col_off1 = curwin_col_off();
4437 col_off2 = col_off1 - curwin_col_off2();
4438 width1 = W_WIDTH(curwin) - col_off1;
4439 width2 = W_WIDTH(curwin) - col_off2;
4440
4441#ifdef FEAT_VERTSPLIT
4442 if (curwin->w_width != 0)
4443 {
4444#endif
4445 /*
4446 * Instead of sticking at the last character of the buffer line we
4447 * try to stick in the last column of the screen.
4448 */
4449 if (curwin->w_curswant == MAXCOL)
4450 {
4451 atend = TRUE;
4452 validate_virtcol();
4453 if (width1 <= 0)
4454 curwin->w_curswant = 0;
4455 else
4456 {
4457 curwin->w_curswant = width1 - 1;
4458 if (curwin->w_virtcol > curwin->w_curswant)
4459 curwin->w_curswant += ((curwin->w_virtcol
4460 - curwin->w_curswant - 1) / width2 + 1) * width2;
4461 }
4462 }
4463 else
4464 {
4465 if (linelen > width1)
4466 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4467 else
4468 n = width1;
4469 if (curwin->w_curswant > (colnr_T)n + 1)
4470 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4471 * width2;
4472 }
4473
4474 while (dist--)
4475 {
4476 if (dir == BACKWARD)
4477 {
4478 if ((long)curwin->w_curswant >= width2)
4479 /* move back within line */
4480 curwin->w_curswant -= width2;
4481 else
4482 {
4483 /* to previous line */
4484 if (curwin->w_cursor.lnum == 1)
4485 {
4486 retval = FAIL;
4487 break;
4488 }
4489 --curwin->w_cursor.lnum;
4490#ifdef FEAT_FOLDING
4491 /* Move to the start of a closed fold. Don't do that when
4492 * 'foldopen' contains "all": it will open in a moment. */
4493 if (!(fdo_flags & FDO_ALL))
4494 (void)hasFolding(curwin->w_cursor.lnum,
4495 &curwin->w_cursor.lnum, NULL);
4496#endif
4497 linelen = linetabsize(ml_get_curline());
4498 if (linelen > width1)
4499 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4500 + 1) * width2;
4501 }
4502 }
4503 else /* dir == FORWARD */
4504 {
4505 if (linelen > width1)
4506 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4507 else
4508 n = width1;
4509 if (curwin->w_curswant + width2 < (colnr_T)n)
4510 /* move forward within line */
4511 curwin->w_curswant += width2;
4512 else
4513 {
4514 /* to next line */
4515#ifdef FEAT_FOLDING
4516 /* Move to the end of a closed fold. */
4517 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4518 &curwin->w_cursor.lnum);
4519#endif
4520 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4521 {
4522 retval = FAIL;
4523 break;
4524 }
4525 curwin->w_cursor.lnum++;
4526 curwin->w_curswant %= width2;
4527 }
4528 }
4529 }
4530#ifdef FEAT_VERTSPLIT
4531 }
4532#endif
4533
4534 coladvance(curwin->w_curswant);
4535
4536#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4537 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4538 {
4539 /*
4540 * Check for landing on a character that got split at the end of the
4541 * last line. We want to advance a screenline, not end up in the same
4542 * screenline or move two screenlines.
4543 */
4544 validate_virtcol();
4545 if (curwin->w_virtcol > curwin->w_curswant
4546 && (curwin->w_curswant < (colnr_T)width1
4547 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4548 : ((curwin->w_curswant - width1) % width2
4549 > (colnr_T)width2 / 2)))
4550 --curwin->w_cursor.col;
4551 }
4552#endif
4553
4554 if (atend)
4555 curwin->w_curswant = MAXCOL; /* stick in the last column */
4556
4557 return retval;
4558}
4559
4560#ifdef FEAT_MOUSE
4561/*
4562 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4563 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004564 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4565 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 */
4567 static void
4568nv_mousescroll(cap)
4569 cmdarg_T *cap;
4570{
4571# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004572 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573
4574 /* Currently we only get the mouse coordinates in the GUI. */
4575 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4576 {
4577 int row, col;
4578
4579 row = mouse_row;
4580 col = mouse_col;
4581
4582 /* find the window at the pointer coordinates */
4583 curwin = mouse_find_win(&row, &col);
4584 curbuf = curwin->w_buffer;
4585 }
4586# endif
4587
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004588 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004590 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4591 {
4592 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4593 }
4594 else
4595 {
4596 cap->count1 = 3;
4597 cap->count0 = 3;
4598 nv_scroll_line(cap);
4599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004601# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 else
4603 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004604 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4605 if (!curwin->w_p_wrap)
4606 {
4607 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004608
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004609 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4610 step = W_WIDTH(curwin);
4611 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4612 if (val < 0)
4613 val = 0;
4614
4615 gui_do_horiz_scroll(val, TRUE);
4616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004618# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619
4620# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4621 curwin->w_redr_status = TRUE;
4622
4623 curwin = old_curwin;
4624 curbuf = curwin->w_buffer;
4625# endif
4626}
4627
4628/*
4629 * Mouse clicks and drags.
4630 */
4631 static void
4632nv_mouse(cap)
4633 cmdarg_T *cap;
4634{
4635 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4636}
4637#endif
4638
4639/*
4640 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4641 * cap->arg must be TRUE for CTRL-E.
4642 */
4643 static void
4644nv_scroll_line(cap)
4645 cmdarg_T *cap;
4646{
4647 if (!checkclearop(cap->oap))
4648 scroll_redraw(cap->arg, cap->count1);
4649}
4650
4651/*
4652 * Scroll "count" lines up or down, and redraw.
4653 */
4654 void
4655scroll_redraw(up, count)
4656 int up;
4657 long count;
4658{
4659 linenr_T prev_topline = curwin->w_topline;
4660#ifdef FEAT_DIFF
4661 int prev_topfill = curwin->w_topfill;
4662#endif
4663 linenr_T prev_lnum = curwin->w_cursor.lnum;
4664
4665 if (up)
4666 scrollup(count, TRUE);
4667 else
4668 scrolldown(count, TRUE);
4669 if (p_so)
4670 {
4671 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4672 * valid, otherwise the screen jumps back at the end of the file. */
4673 cursor_correct();
4674 check_cursor_moved(curwin);
4675 curwin->w_valid |= VALID_TOPLINE;
4676
4677 /* If moved back to where we were, at least move the cursor, otherwise
4678 * we get stuck at one position. Don't move the cursor up if the
4679 * first line of the buffer is already on the screen */
4680 while (curwin->w_topline == prev_topline
4681#ifdef FEAT_DIFF
4682 && curwin->w_topfill == prev_topfill
4683#endif
4684 )
4685 {
4686 if (up)
4687 {
4688 if (curwin->w_cursor.lnum > prev_lnum
4689 || cursor_down(1L, FALSE) == FAIL)
4690 break;
4691 }
4692 else
4693 {
4694 if (curwin->w_cursor.lnum < prev_lnum
4695 || prev_topline == 1L
4696 || cursor_up(1L, FALSE) == FAIL)
4697 break;
4698 }
4699 /* Mark w_topline as valid, otherwise the screen jumps back at the
4700 * end of the file. */
4701 check_cursor_moved(curwin);
4702 curwin->w_valid |= VALID_TOPLINE;
4703 }
4704 }
4705 if (curwin->w_cursor.lnum != prev_lnum)
4706 coladvance(curwin->w_curswant);
4707 redraw_later(VALID);
4708}
4709
4710/*
4711 * Commands that start with "z".
4712 */
4713 static void
4714nv_zet(cap)
4715 cmdarg_T *cap;
4716{
4717 long n;
4718 colnr_T col;
4719 int nchar = cap->nchar;
4720#ifdef FEAT_FOLDING
4721 long old_fdl = curwin->w_p_fdl;
4722 int old_fen = curwin->w_p_fen;
4723#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004724#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004725 int undo = FALSE;
4726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727
4728 if (VIM_ISDIGIT(nchar))
4729 {
4730 /*
4731 * "z123{nchar}": edit the count before obtaining {nchar}
4732 */
4733 if (checkclearop(cap->oap))
4734 return;
4735 n = nchar - '0';
4736 for (;;)
4737 {
4738#ifdef USE_ON_FLY_SCROLL
4739 dont_scroll = TRUE; /* disallow scrolling here */
4740#endif
4741 ++no_mapping;
4742 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004743 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 --no_mapping;
4746 --allow_keys;
4747#ifdef FEAT_CMDL_INFO
4748 (void)add_to_showcmd(nchar);
4749#endif
4750 if (nchar == K_DEL || nchar == K_KDEL)
4751 n /= 10;
4752 else if (VIM_ISDIGIT(nchar))
4753 n = n * 10 + (nchar - '0');
4754 else if (nchar == CAR)
4755 {
4756#ifdef FEAT_GUI
4757 need_mouse_correct = TRUE;
4758#endif
4759 win_setheight((int)n);
4760 break;
4761 }
4762 else if (nchar == 'l'
4763 || nchar == 'h'
4764 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004765 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 {
4767 cap->count1 = n ? n * cap->count1 : cap->count1;
4768 goto dozet;
4769 }
4770 else
4771 {
4772 clearopbeep(cap->oap);
4773 break;
4774 }
4775 }
4776 cap->oap->op_type = OP_NOP;
4777 return;
4778 }
4779
4780dozet:
4781 if (
4782#ifdef FEAT_FOLDING
4783 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4784 * and "zC" only in Visual mode. "zj" and "zk" are motion
4785 * commands. */
4786 cap->nchar != 'f' && cap->nchar != 'F'
4787 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4788 && cap->nchar != 'j' && cap->nchar != 'k'
4789 &&
4790#endif
4791 checkclearop(cap->oap))
4792 return;
4793
4794 /*
4795 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4796 * If line number given, set cursor.
4797 */
4798 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4799 && cap->count0
4800 && cap->count0 != curwin->w_cursor.lnum)
4801 {
4802 setpcmark();
4803 if (cap->count0 > curbuf->b_ml.ml_line_count)
4804 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4805 else
4806 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004807 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 }
4809
4810 switch (nchar)
4811 {
4812 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4813 case '+':
4814 if (cap->count0 == 0)
4815 {
4816 /* No count given: put cursor at the line below screen */
4817 validate_botline(); /* make sure w_botline is valid */
4818 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4819 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4820 else
4821 curwin->w_cursor.lnum = curwin->w_botline;
4822 }
4823 /* FALLTHROUGH */
4824 case NL:
4825 case CAR:
4826 case K_KENTER:
4827 beginline(BL_WHITE | BL_FIX);
4828 /* FALLTHROUGH */
4829
4830 case 't': scroll_cursor_top(0, TRUE);
4831 redraw_later(VALID);
4832 break;
4833
4834 /* "z." and "zz": put cursor in middle of screen */
4835 case '.': beginline(BL_WHITE | BL_FIX);
4836 /* FALLTHROUGH */
4837
4838 case 'z': scroll_cursor_halfway(TRUE);
4839 redraw_later(VALID);
4840 break;
4841
4842 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4843 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4844 * when <count> is at bottom of window, and puts that one at
4845 * bottom of window. */
4846 if (cap->count0 != 0)
4847 {
4848 scroll_cursor_bot(0, TRUE);
4849 curwin->w_cursor.lnum = curwin->w_topline;
4850 }
4851 else if (curwin->w_topline == 1)
4852 curwin->w_cursor.lnum = 1;
4853 else
4854 curwin->w_cursor.lnum = curwin->w_topline - 1;
4855 /* FALLTHROUGH */
4856 case '-':
4857 beginline(BL_WHITE | BL_FIX);
4858 /* FALLTHROUGH */
4859
4860 case 'b': scroll_cursor_bot(0, TRUE);
4861 redraw_later(VALID);
4862 break;
4863
4864 /* "zH" - scroll screen right half-page */
4865 case 'H':
4866 cap->count1 *= W_WIDTH(curwin) / 2;
4867 /* FALLTHROUGH */
4868
4869 /* "zh" - scroll screen to the right */
4870 case 'h':
4871 case K_LEFT:
4872 if (!curwin->w_p_wrap)
4873 {
4874 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4875 curwin->w_leftcol = 0;
4876 else
4877 curwin->w_leftcol -= (colnr_T)cap->count1;
4878 leftcol_changed();
4879 }
4880 break;
4881
4882 /* "zL" - scroll screen left half-page */
4883 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4884 /* FALLTHROUGH */
4885
4886 /* "zl" - scroll screen to the left */
4887 case 'l':
4888 case K_RIGHT:
4889 if (!curwin->w_p_wrap)
4890 {
4891 /* scroll the window left */
4892 curwin->w_leftcol += (colnr_T)cap->count1;
4893 leftcol_changed();
4894 }
4895 break;
4896
4897 /* "zs" - scroll screen, cursor at the start */
4898 case 's': if (!curwin->w_p_wrap)
4899 {
4900#ifdef FEAT_FOLDING
4901 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4902 col = 0; /* like the cursor is in col 0 */
4903 else
4904#endif
4905 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4906 if ((long)col > p_siso)
4907 col -= p_siso;
4908 else
4909 col = 0;
4910 if (curwin->w_leftcol != col)
4911 {
4912 curwin->w_leftcol = col;
4913 redraw_later(NOT_VALID);
4914 }
4915 }
4916 break;
4917
4918 /* "ze" - scroll screen, cursor at the end */
4919 case 'e': if (!curwin->w_p_wrap)
4920 {
4921#ifdef FEAT_FOLDING
4922 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4923 col = 0; /* like the cursor is in col 0 */
4924 else
4925#endif
4926 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4927 n = W_WIDTH(curwin) - curwin_col_off();
4928 if ((long)col + p_siso < n)
4929 col = 0;
4930 else
4931 col = col + p_siso - n + 1;
4932 if (curwin->w_leftcol != col)
4933 {
4934 curwin->w_leftcol = col;
4935 redraw_later(NOT_VALID);
4936 }
4937 }
4938 break;
4939
4940#ifdef FEAT_FOLDING
4941 /* "zF": create fold command */
4942 /* "zf": create fold operator */
4943 case 'F':
4944 case 'f': if (foldManualAllowed(TRUE))
4945 {
4946 cap->nchar = 'f';
4947 nv_operator(cap);
4948 curwin->w_p_fen = TRUE;
4949
4950 /* "zF" is like "zfzf" */
4951 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4952 {
4953 nv_operator(cap);
4954 finish_op = TRUE;
4955 }
4956 }
4957 else
4958 clearopbeep(cap->oap);
4959 break;
4960
4961 /* "zd": delete fold at cursor */
4962 /* "zD": delete fold at cursor recursively */
4963 case 'd':
4964 case 'D': if (foldManualAllowed(FALSE))
4965 {
4966 if (VIsual_active)
4967 nv_operator(cap);
4968 else
4969 deleteFold(curwin->w_cursor.lnum,
4970 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4971 }
4972 break;
4973
4974 /* "zE": erease all folds */
4975 case 'E': if (foldmethodIsManual(curwin))
4976 {
4977 clearFolding(curwin);
4978 changed_window_setting();
4979 }
4980 else if (foldmethodIsMarker(curwin))
4981 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4982 TRUE, FALSE);
4983 else
4984 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4985 break;
4986
4987 /* "zn": fold none: reset 'foldenable' */
4988 case 'n': curwin->w_p_fen = FALSE;
4989 break;
4990
4991 /* "zN": fold Normal: set 'foldenable' */
4992 case 'N': curwin->w_p_fen = TRUE;
4993 break;
4994
4995 /* "zi": invert folding: toggle 'foldenable' */
4996 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4997 break;
4998
4999 /* "za": open closed fold or close open fold at cursor */
5000 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5001 openFold(curwin->w_cursor.lnum, cap->count1);
5002 else
5003 {
5004 closeFold(curwin->w_cursor.lnum, cap->count1);
5005 curwin->w_p_fen = TRUE;
5006 }
5007 break;
5008
5009 /* "zA": open fold at cursor recursively */
5010 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5011 openFoldRecurse(curwin->w_cursor.lnum);
5012 else
5013 {
5014 closeFoldRecurse(curwin->w_cursor.lnum);
5015 curwin->w_p_fen = TRUE;
5016 }
5017 break;
5018
5019 /* "zo": open fold at cursor or Visual area */
5020 case 'o': if (VIsual_active)
5021 nv_operator(cap);
5022 else
5023 openFold(curwin->w_cursor.lnum, cap->count1);
5024 break;
5025
5026 /* "zO": open fold recursively */
5027 case 'O': if (VIsual_active)
5028 nv_operator(cap);
5029 else
5030 openFoldRecurse(curwin->w_cursor.lnum);
5031 break;
5032
5033 /* "zc": close fold at cursor or Visual area */
5034 case 'c': if (VIsual_active)
5035 nv_operator(cap);
5036 else
5037 closeFold(curwin->w_cursor.lnum, cap->count1);
5038 curwin->w_p_fen = TRUE;
5039 break;
5040
5041 /* "zC": close fold recursively */
5042 case 'C': if (VIsual_active)
5043 nv_operator(cap);
5044 else
5045 closeFoldRecurse(curwin->w_cursor.lnum);
5046 curwin->w_p_fen = TRUE;
5047 break;
5048
5049 /* "zv": open folds at the cursor */
5050 case 'v': foldOpenCursor();
5051 break;
5052
5053 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5054 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005055 curwin->w_foldinvalid = TRUE; /* recompute folds */
5056 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 foldOpenCursor();
5058 break;
5059
5060 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5061 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005062 curwin->w_foldinvalid = TRUE; /* recompute folds */
5063 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 break;
5065
5066 /* "zm": fold more */
5067 case 'm': if (curwin->w_p_fdl > 0)
5068 --curwin->w_p_fdl;
5069 old_fdl = -1; /* force an update */
5070 curwin->w_p_fen = TRUE;
5071 break;
5072
5073 /* "zM": close all folds */
5074 case 'M': curwin->w_p_fdl = 0;
5075 old_fdl = -1; /* force an update */
5076 curwin->w_p_fen = TRUE;
5077 break;
5078
5079 /* "zr": reduce folding */
5080 case 'r': ++curwin->w_p_fdl;
5081 break;
5082
5083 /* "zR": open all folds */
5084 case 'R': curwin->w_p_fdl = getDeepestNesting();
5085 old_fdl = -1; /* force an update */
5086 break;
5087
5088 case 'j': /* "zj" move to next fold downwards */
5089 case 'k': /* "zk" move to next fold upwards */
5090 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5091 cap->count1) == FAIL)
5092 clearopbeep(cap->oap);
5093 break;
5094
5095#endif /* FEAT_FOLDING */
5096
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005097#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005098 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5099 ++no_mapping;
5100 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005101 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005102 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005103 --no_mapping;
5104 --allow_keys;
5105#ifdef FEAT_CMDL_INFO
5106 (void)add_to_showcmd(nchar);
5107#endif
5108 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5109 {
5110 clearopbeep(cap->oap);
5111 break;
5112 }
5113 undo = TRUE;
5114 /*FALLTHROUGH*/
5115
Bram Moolenaarb765d632005-06-07 21:00:02 +00005116 case 'g': /* "zg": add good word to word list */
5117 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005118 case 'G': /* "zG": add good word to temp word list */
5119 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005120 {
5121 char_u *ptr = NULL;
5122 int len;
5123
5124 if (checkclearop(cap->oap))
5125 break;
5126# ifdef FEAT_VISUAL
5127 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5128 == FAIL)
5129 return;
5130# endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005131 if (ptr == NULL)
5132 {
5133 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005134
5135 /* Find bad word under the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005136 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005137 if (len != 0 && curwin->w_cursor.col <= pos.col)
5138 ptr = ml_get_pos(&curwin->w_cursor);
5139 curwin->w_cursor = pos;
5140 }
5141
Bram Moolenaarb765d632005-06-07 21:00:02 +00005142 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5143 FIND_IDENT)) == 0)
5144 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005145 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005146 (nchar == 'G' || nchar == 'W')
5147 ? 0 : (int)cap->count1,
5148 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005149 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005150 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005151
Bram Moolenaar43abc522005-12-10 20:15:02 +00005152 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005153 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005154 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005155 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005156#endif
5157
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 default: clearopbeep(cap->oap);
5159 }
5160
5161#ifdef FEAT_FOLDING
5162 /* Redraw when 'foldenable' changed */
5163 if (old_fen != curwin->w_p_fen)
5164 {
5165# ifdef FEAT_DIFF
5166 win_T *wp;
5167
5168 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5169 {
5170 /* Adjust 'foldenable' in diff-synced windows. */
5171 FOR_ALL_WINDOWS(wp)
5172 {
5173 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5174 {
5175 wp->w_p_fen = curwin->w_p_fen;
5176 changed_window_setting_win(wp);
5177 }
5178 }
5179 }
5180# endif
5181 changed_window_setting();
5182 }
5183
5184 /* Redraw when 'foldlevel' changed. */
5185 if (old_fdl != curwin->w_p_fdl)
5186 newFoldLevel();
5187#endif
5188}
5189
5190#ifdef FEAT_GUI
5191/*
5192 * Vertical scrollbar movement.
5193 */
5194 static void
5195nv_ver_scrollbar(cap)
5196 cmdarg_T *cap;
5197{
5198 if (cap->oap->op_type != OP_NOP)
5199 clearopbeep(cap->oap);
5200
5201 /* Even if an operator was pending, we still want to scroll */
5202 gui_do_scroll();
5203}
5204
5205/*
5206 * Horizontal scrollbar movement.
5207 */
5208 static void
5209nv_hor_scrollbar(cap)
5210 cmdarg_T *cap;
5211{
5212 if (cap->oap->op_type != OP_NOP)
5213 clearopbeep(cap->oap);
5214
5215 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005216 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217}
5218#endif
5219
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005220#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005221/*
5222 * Click in GUI tab.
5223 */
5224 static void
5225nv_tabline(cap)
5226 cmdarg_T *cap;
5227{
5228 if (cap->oap->op_type != OP_NOP)
5229 clearopbeep(cap->oap);
5230
5231 /* Even if an operator was pending, we still want to jump tabs. */
5232 goto_tabpage(current_tab);
5233}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005234
5235/*
5236 * Selected item in tab line menu.
5237 */
5238 static void
5239nv_tabmenu(cap)
5240 cmdarg_T *cap;
5241{
5242 if (cap->oap->op_type != OP_NOP)
5243 clearopbeep(cap->oap);
5244
5245 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005246 handle_tabmenu();
5247}
5248
5249/*
5250 * Handle selecting an item of the GUI tab line menu.
5251 * Used in Normal and Insert mode.
5252 */
5253 void
5254handle_tabmenu()
5255{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005256 switch (current_tabmenu)
5257 {
5258 case TABLINE_MENU_CLOSE:
5259 if (current_tab == 0)
5260 do_cmdline_cmd((char_u *)"tabclose");
5261 else
5262 {
5263 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5264 current_tab);
5265 do_cmdline_cmd(IObuff);
5266 }
5267 break;
5268
5269 case TABLINE_MENU_NEW:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005270 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5271 current_tab > 0 ? current_tab - 1 : 999);
5272 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005273 break;
5274
5275 case TABLINE_MENU_OPEN:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005276 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5277 current_tab > 0 ? current_tab - 1 : 999);
5278 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005279 break;
5280 }
5281}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005282#endif
5283
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284/*
5285 * "Q" command.
5286 */
5287 static void
5288nv_exmode(cap)
5289 cmdarg_T *cap;
5290{
5291 /*
5292 * Ignore 'Q' in Visual mode, just give a beep.
5293 */
5294#ifdef FEAT_VISUAL
5295 if (VIsual_active)
5296 vim_beep();
5297 else
5298#endif
5299 if (!checkclearop(cap->oap))
5300 do_exmode(FALSE);
5301}
5302
5303/*
5304 * Handle a ":" command.
5305 */
5306 static void
5307nv_colon(cap)
5308 cmdarg_T *cap;
5309{
5310 int old_p_im;
5311
5312#ifdef FEAT_VISUAL
5313 if (VIsual_active)
5314 nv_operator(cap);
5315 else
5316#endif
5317 {
5318 if (cap->oap->op_type != OP_NOP)
5319 {
5320 /* Using ":" as a movement is characterwise exclusive. */
5321 cap->oap->motion_type = MCHAR;
5322 cap->oap->inclusive = FALSE;
5323 }
5324 else if (cap->count0)
5325 {
5326 /* translate "count:" into ":.,.+(count - 1)" */
5327 stuffcharReadbuff('.');
5328 if (cap->count0 > 1)
5329 {
5330 stuffReadbuff((char_u *)",.+");
5331 stuffnumReadbuff((long)cap->count0 - 1L);
5332 }
5333 }
5334
5335 /* When typing, don't type below an old message */
5336 if (KeyTyped)
5337 compute_cmdrow();
5338
5339 old_p_im = p_im;
5340
5341 /* get a command line and execute it */
5342 do_cmdline(NULL, getexline, NULL,
5343 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5344
5345 /* If 'insertmode' changed, enter or exit Insert mode */
5346 if (p_im != old_p_im)
5347 {
5348 if (p_im)
5349 restart_edit = 'i';
5350 else
5351 restart_edit = 0;
5352 }
5353
5354 /* The start of the operator may have become invalid by the Ex
5355 * command. */
5356 if (cap->oap->op_type != OP_NOP
5357 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5358 || cap->oap->start.col >
Bram Moolenaar78a15312009-05-15 19:33:18 +00005359 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 clearopbeep(cap->oap);
5361 }
5362}
5363
5364/*
5365 * Handle CTRL-G command.
5366 */
5367 static void
5368nv_ctrlg(cap)
5369 cmdarg_T *cap;
5370{
5371#ifdef FEAT_VISUAL
5372 if (VIsual_active) /* toggle Selection/Visual mode */
5373 {
5374 VIsual_select = !VIsual_select;
5375 showmode();
5376 }
5377 else
5378#endif
5379 if (!checkclearop(cap->oap))
5380 /* print full name if count given or :cd used */
5381 fileinfo((int)cap->count0, FALSE, TRUE);
5382}
5383
5384/*
5385 * Handle CTRL-H <Backspace> command.
5386 */
5387 static void
5388nv_ctrlh(cap)
5389 cmdarg_T *cap;
5390{
5391#ifdef FEAT_VISUAL
5392 if (VIsual_active && VIsual_select)
5393 {
5394 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5395 v_visop(cap);
5396 }
5397 else
5398#endif
5399 nv_left(cap);
5400}
5401
5402/*
5403 * CTRL-L: clear screen and redraw.
5404 */
5405 static void
5406nv_clear(cap)
5407 cmdarg_T *cap;
5408{
5409 if (!checkclearop(cap->oap))
5410 {
5411#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5412 /*
5413 * Right now, the BeBox doesn't seem to have an easy way to detect
5414 * window resizing, so we cheat and make the user detect it
5415 * manually with CTRL-L instead
5416 */
5417 ui_get_shellsize();
5418#endif
5419#ifdef FEAT_SYN_HL
5420 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005421 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422#endif
5423 redraw_later(CLEAR);
5424 }
5425}
5426
5427/*
5428 * CTRL-O: In Select mode: switch to Visual mode for one command.
5429 * Otherwise: Go to older pcmark.
5430 */
5431 static void
5432nv_ctrlo(cap)
5433 cmdarg_T *cap;
5434{
5435#ifdef FEAT_VISUAL
5436 if (VIsual_active && VIsual_select)
5437 {
5438 VIsual_select = FALSE;
5439 showmode();
5440 restart_VIsual_select = 2; /* restart Select mode later */
5441 }
5442 else
5443#endif
5444 {
5445 cap->count1 = -cap->count1;
5446 nv_pcmark(cap);
5447 }
5448}
5449
5450/*
5451 * CTRL-^ command, short for ":e #"
5452 */
5453 static void
5454nv_hat(cap)
5455 cmdarg_T *cap;
5456{
5457 if (!checkclearopq(cap->oap))
5458 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5459 GETF_SETMARK|GETF_ALT, FALSE);
5460}
5461
5462/*
5463 * "Z" commands.
5464 */
5465 static void
5466nv_Zet(cap)
5467 cmdarg_T *cap;
5468{
5469 if (!checkclearopq(cap->oap))
5470 {
5471 switch (cap->nchar)
5472 {
5473 /* "ZZ": equivalent to ":x". */
5474 case 'Z': do_cmdline_cmd((char_u *)"x");
5475 break;
5476
5477 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5478 case 'Q': do_cmdline_cmd((char_u *)"q!");
5479 break;
5480
5481 default: clearopbeep(cap->oap);
5482 }
5483 }
5484}
5485
5486#if defined(FEAT_WINDOWS) || defined(PROTO)
5487/*
5488 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5489 */
5490 void
5491do_nv_ident(c1, c2)
5492 int c1;
5493 int c2;
5494{
5495 oparg_T oa;
5496 cmdarg_T ca;
5497
5498 clear_oparg(&oa);
5499 vim_memset(&ca, 0, sizeof(ca));
5500 ca.oap = &oa;
5501 ca.cmdchar = c1;
5502 ca.nchar = c2;
5503 nv_ident(&ca);
5504}
5505#endif
5506
5507/*
5508 * Handle the commands that use the word under the cursor.
5509 * [g] CTRL-] :ta to current identifier
5510 * [g] 'K' run program for current identifier
5511 * [g] '*' / to current identifier or string
5512 * [g] '#' ? to current identifier or string
5513 * g ']' :tselect for current identifier
5514 */
5515 static void
5516nv_ident(cap)
5517 cmdarg_T *cap;
5518{
5519 char_u *ptr = NULL;
5520 char_u *buf;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005521 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 char_u *p;
5523 char_u *kp; /* value of 'keywordprg' */
5524 int kp_help; /* 'keywordprg' is ":help" */
5525 int n = 0; /* init for GCC */
5526 int cmdchar;
5527 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005528 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 char_u *aux_ptr;
5530 int isman;
5531 int isman_s;
5532
5533 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5534 {
5535 cmdchar = cap->nchar;
5536 g_cmd = TRUE;
5537 }
5538 else
5539 {
5540 cmdchar = cap->cmdchar;
5541 g_cmd = FALSE;
5542 }
5543
5544 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5545 cmdchar = '#';
5546
5547 /*
5548 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5549 */
5550 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5551 {
5552#ifdef FEAT_VISUAL
5553 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5554 return;
5555#endif
5556 if (checkclearopq(cap->oap))
5557 return;
5558 }
5559
5560 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5561 (cmdchar == '*' || cmdchar == '#')
5562 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5563 {
5564 clearop(cap->oap);
5565 return;
5566 }
5567
5568 /* Allocate buffer to put the command in. Inserting backslashes can
5569 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5570 * and some numbers. */
5571 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5572 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5573 || STRCMP(kp, ":help") == 0);
5574 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5575 if (buf == NULL)
5576 return;
5577 buf[0] = NUL;
5578
5579 switch (cmdchar)
5580 {
5581 case '*':
5582 case '#':
5583 /*
5584 * Put cursor at start of word, makes search skip the word
5585 * under the cursor.
5586 * Call setpcmark() first, so "*``" puts the cursor back where
5587 * it was.
5588 */
5589 setpcmark();
5590 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5591
5592 if (!g_cmd && vim_iswordp(ptr))
5593 STRCPY(buf, "\\<");
5594 no_smartcase = TRUE; /* don't use 'smartcase' now */
5595 break;
5596
5597 case 'K':
5598 if (kp_help)
5599 STRCPY(buf, "he! ");
5600 else
5601 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005602 /* An external command will probably use an argument starting
5603 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005604 while (*ptr == '-' && n > 0)
5605 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005606 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005607 --n;
5608 }
5609 if (n == 0)
5610 {
5611 EMSG(_(e_noident)); /* found dashes only */
5612 vim_free(buf);
5613 return;
5614 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005615
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 /* When a count is given, turn it into a range. Is this
5617 * really what we want? */
5618 isman = (STRCMP(kp, "man") == 0);
5619 isman_s = (STRCMP(kp, "man -s") == 0);
5620 if (cap->count0 != 0 && !(isman || isman_s))
5621 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5622
5623 STRCAT(buf, "! ");
5624 if (cap->count0 == 0 && isman_s)
5625 STRCAT(buf, "man");
5626 else
5627 STRCAT(buf, kp);
5628 STRCAT(buf, " ");
5629 if (cap->count0 != 0 && (isman || isman_s))
5630 {
5631 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5632 STRCAT(buf, " ");
5633 }
5634 }
5635 break;
5636
5637 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005638 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639#ifdef FEAT_CSCOPE
5640 if (p_cst)
5641 STRCPY(buf, "cstag ");
5642 else
5643#endif
5644 STRCPY(buf, "ts ");
5645 break;
5646
5647 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005648 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 if (curbuf->b_help)
5650 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005652 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005653 if (g_cmd)
5654 STRCPY(buf, "tj ");
5655 else
5656 sprintf((char *)buf, "%ldta ", cap->count0);
5657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 }
5659
5660 /*
5661 * Now grab the chars in the identifier
5662 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005663 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005665 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005666 ptr = vim_strnsave(ptr, n);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005667 p = vim_strsave_shellescape(ptr, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005668 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005669 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005671 vim_free(buf);
5672 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005674 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5675 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005676 {
5677 vim_free(buf);
5678 vim_free(p);
5679 return;
5680 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005681 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005682 STRCAT(buf, p);
5683 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005685 else
5686 {
5687 if (cmdchar == '*')
5688 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5689 else if (cmdchar == '#')
5690 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005691 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005692 {
5693 if (curbuf->b_help)
5694 /* ":help" handles unescaped argument */
5695 aux_ptr = (char_u *)"";
5696 else
5697 aux_ptr = (char_u *)"\\|\"\n[";
5698 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005699 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005700 aux_ptr = (char_u *)"\\|\"\n*?[";
5701
5702 p = buf + STRLEN(buf);
5703 while (n-- > 0)
5704 {
5705 /* put a backslash before \ and some others */
5706 if (vim_strchr(aux_ptr, *ptr) != NULL)
5707 *p++ = '\\';
5708#ifdef FEAT_MBYTE
5709 /* When current byte is a part of multibyte character, copy all
5710 * bytes of that character. */
5711 if (has_mbyte)
5712 {
5713 int i;
5714 int len = (*mb_ptr2len)(ptr) - 1;
5715
5716 for (i = 0; i < len && n >= 1; ++i, --n)
5717 *p++ = *ptr++;
5718 }
5719#endif
5720 *p++ = *ptr++;
5721 }
5722 *p = NUL;
5723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
5725 /*
5726 * Execute the command.
5727 */
5728 if (cmdchar == '*' || cmdchar == '#')
5729 {
5730 if (!g_cmd && (
5731#ifdef FEAT_MBYTE
5732 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5733#endif
5734 vim_iswordc(ptr[-1])))
5735 STRCAT(buf, "\\>");
5736#ifdef FEAT_CMDHIST
5737 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005738 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5740#endif
5741 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5742 }
5743 else
5744 do_cmdline_cmd(buf);
5745
5746 vim_free(buf);
5747}
5748
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005749#if defined(FEAT_VISUAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750/*
5751 * Get visually selected text, within one line only.
5752 * Returns FAIL if more than one line selected.
5753 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005754 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755get_visual_text(cap, pp, lenp)
5756 cmdarg_T *cap;
5757 char_u **pp; /* return: start of selected text */
5758 int *lenp; /* return: length of selected text */
5759{
5760 if (VIsual_mode != 'V')
5761 unadjust_for_sel();
5762 if (VIsual.lnum != curwin->w_cursor.lnum)
5763 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005764 if (cap != NULL)
5765 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 return FAIL;
5767 }
5768 if (VIsual_mode == 'V')
5769 {
5770 *pp = ml_get_curline();
5771 *lenp = (int)STRLEN(*pp);
5772 }
5773 else
5774 {
5775 if (lt(curwin->w_cursor, VIsual))
5776 {
5777 *pp = ml_get_pos(&curwin->w_cursor);
5778 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5779 }
5780 else
5781 {
5782 *pp = ml_get_pos(&VIsual);
5783 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5784 }
5785#ifdef FEAT_MBYTE
5786 if (has_mbyte)
5787 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005788 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789#endif
5790 }
5791 reset_VIsual_and_resel();
5792 return OK;
5793}
5794#endif
5795
5796/*
5797 * CTRL-T: backwards in tag stack
5798 */
5799 static void
5800nv_tagpop(cap)
5801 cmdarg_T *cap;
5802{
5803 if (!checkclearopq(cap->oap))
5804 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5805}
5806
5807/*
5808 * Handle scrolling command 'H', 'L' and 'M'.
5809 */
5810 static void
5811nv_scroll(cap)
5812 cmdarg_T *cap;
5813{
5814 int used = 0;
5815 long n;
5816#ifdef FEAT_FOLDING
5817 linenr_T lnum;
5818#endif
5819 int half;
5820
5821 cap->oap->motion_type = MLINE;
5822 setpcmark();
5823
5824 if (cap->cmdchar == 'L')
5825 {
5826 validate_botline(); /* make sure curwin->w_botline is valid */
5827 curwin->w_cursor.lnum = curwin->w_botline - 1;
5828 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5829 curwin->w_cursor.lnum = 1;
5830 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005831 {
5832#ifdef FEAT_FOLDING
5833 if (hasAnyFolding(curwin))
5834 {
5835 /* Count a fold for one screen line. */
5836 for (n = cap->count1 - 1; n > 0
5837 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5838 {
5839 (void)hasFolding(curwin->w_cursor.lnum,
5840 &curwin->w_cursor.lnum, NULL);
5841 --curwin->w_cursor.lnum;
5842 }
5843 }
5844 else
5845#endif
5846 curwin->w_cursor.lnum -= cap->count1 - 1;
5847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 }
5849 else
5850 {
5851 if (cap->cmdchar == 'M')
5852 {
5853#ifdef FEAT_DIFF
5854 /* Don't count filler lines above the window. */
5855 used -= diff_check_fill(curwin, curwin->w_topline)
5856 - curwin->w_topfill;
5857#endif
5858 validate_botline(); /* make sure w_empty_rows is valid */
5859 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5860 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5861 {
5862#ifdef FEAT_DIFF
5863 /* Count half he number of filler lines to be "below this
5864 * line" and half to be "above the next line". */
5865 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5866 + n) / 2 >= half)
5867 {
5868 --n;
5869 break;
5870 }
5871#endif
5872 used += plines(curwin->w_topline + n);
5873 if (used >= half)
5874 break;
5875#ifdef FEAT_FOLDING
5876 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5877 n = lnum - curwin->w_topline;
5878#endif
5879 }
5880 if (n > 0 && used > curwin->w_height)
5881 --n;
5882 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005883 else /* (cap->cmdchar == 'H') */
5884 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005886#ifdef FEAT_FOLDING
5887 if (hasAnyFolding(curwin))
5888 {
5889 /* Count a fold for one screen line. */
5890 lnum = curwin->w_topline;
5891 while (n-- > 0 && lnum < curwin->w_botline - 1)
5892 {
5893 hasFolding(lnum, NULL, &lnum);
5894 ++lnum;
5895 }
5896 n = lnum - curwin->w_topline;
5897 }
5898#endif
5899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 curwin->w_cursor.lnum = curwin->w_topline + n;
5901 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5902 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5903 }
5904
5905 cursor_correct(); /* correct for 'so' */
5906 beginline(BL_SOL | BL_FIX);
5907}
5908
5909/*
5910 * Cursor right commands.
5911 */
5912 static void
5913nv_right(cap)
5914 cmdarg_T *cap;
5915{
5916 long n;
5917#ifdef FEAT_VISUAL
5918 int PAST_LINE;
5919#else
5920# define PAST_LINE 0
5921#endif
5922
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005923 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5924 {
5925 /* <C-Right> and <S-Right> move a word or WORD right */
5926 if (mod_mask & MOD_MASK_CTRL)
5927 cap->arg = TRUE;
5928 nv_wordcmd(cap);
5929 return;
5930 }
5931
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 cap->oap->motion_type = MCHAR;
5933 cap->oap->inclusive = FALSE;
5934#ifdef FEAT_VISUAL
5935 PAST_LINE = (VIsual_active && *p_sel != 'o');
5936
5937# ifdef FEAT_VIRTUALEDIT
5938 /*
5939 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
Bram Moolenaarf711faf2007-05-10 16:48:19 +00005940 * (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 */
5942 if (virtual_active())
5943 PAST_LINE = 0;
5944# endif
5945#endif
5946
5947 for (n = cap->count1; n > 0; --n)
5948 {
5949 if ((!PAST_LINE && oneright() == FAIL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005950#ifdef FEAT_VISUAL
5951 || (PAST_LINE && *ml_get_cursor() == NUL)
5952#endif
5953 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 {
5955 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005956 * <Space> wraps to next line if 'whichwrap' has 's'.
5957 * 'l' wraps to next line if 'whichwrap' has 'l'.
5958 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 */
5960 if ( ((cap->cmdchar == ' '
5961 && vim_strchr(p_ww, 's') != NULL)
5962 || (cap->cmdchar == 'l'
5963 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005964 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 && vim_strchr(p_ww, '>') != NULL))
5966 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5967 {
5968 /* When deleting we also count the NL as a character.
5969 * Set cap->oap->inclusive when last char in the line is
5970 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005971 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 && !cap->oap->inclusive
5973 && !lineempty(curwin->w_cursor.lnum))
5974 cap->oap->inclusive = TRUE;
5975 else
5976 {
5977 ++curwin->w_cursor.lnum;
5978 curwin->w_cursor.col = 0;
5979#ifdef FEAT_VIRTUALEDIT
5980 curwin->w_cursor.coladd = 0;
5981#endif
5982 curwin->w_set_curswant = TRUE;
5983 cap->oap->inclusive = FALSE;
5984 }
5985 continue;
5986 }
5987 if (cap->oap->op_type == OP_NOP)
5988 {
5989 /* Only beep and flush if not moved at all */
5990 if (n == cap->count1)
5991 beep_flush();
5992 }
5993 else
5994 {
5995 if (!lineempty(curwin->w_cursor.lnum))
5996 cap->oap->inclusive = TRUE;
5997 }
5998 break;
5999 }
6000#ifdef FEAT_VISUAL
6001 else if (PAST_LINE)
6002 {
6003 curwin->w_set_curswant = TRUE;
6004# ifdef FEAT_VIRTUALEDIT
6005 if (virtual_active())
6006 oneright();
6007 else
6008# endif
6009 {
6010# ifdef FEAT_MBYTE
6011 if (has_mbyte)
6012 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006013 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 else
6015# endif
6016 ++curwin->w_cursor.col;
6017 }
6018 }
6019#endif
6020 }
6021#ifdef FEAT_FOLDING
6022 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6023 && cap->oap->op_type == OP_NOP)
6024 foldOpenCursor();
6025#endif
6026}
6027
6028/*
6029 * Cursor left commands.
6030 *
6031 * Returns TRUE when operator end should not be adjusted.
6032 */
6033 static void
6034nv_left(cap)
6035 cmdarg_T *cap;
6036{
6037 long n;
6038
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006039 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6040 {
6041 /* <C-Left> and <S-Left> move a word or WORD left */
6042 if (mod_mask & MOD_MASK_CTRL)
6043 cap->arg = 1;
6044 nv_bck_word(cap);
6045 return;
6046 }
6047
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 cap->oap->motion_type = MCHAR;
6049 cap->oap->inclusive = FALSE;
6050 for (n = cap->count1; n > 0; --n)
6051 {
6052 if (oneleft() == FAIL)
6053 {
6054 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6055 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6056 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6057 */
6058 if ( (((cap->cmdchar == K_BS
6059 || cap->cmdchar == Ctrl_H)
6060 && vim_strchr(p_ww, 'b') != NULL)
6061 || (cap->cmdchar == 'h'
6062 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006063 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 && vim_strchr(p_ww, '<') != NULL))
6065 && curwin->w_cursor.lnum > 1)
6066 {
6067 --(curwin->w_cursor.lnum);
6068 coladvance((colnr_T)MAXCOL);
6069 curwin->w_set_curswant = TRUE;
6070
6071 /* When the NL before the first char has to be deleted we
6072 * put the cursor on the NUL after the previous line.
6073 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006074 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 if ( (cap->oap->op_type == OP_DELETE
6076 || cap->oap->op_type == OP_CHANGE)
6077 && !lineempty(curwin->w_cursor.lnum))
6078 {
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006079 if (*ml_get_cursor() != NUL)
6080 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 cap->retval |= CA_NO_ADJ_OP_END;
6082 }
6083 continue;
6084 }
6085 /* Only beep and flush if not moved at all */
6086 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6087 beep_flush();
6088 break;
6089 }
6090 }
6091#ifdef FEAT_FOLDING
6092 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6093 && cap->oap->op_type == OP_NOP)
6094 foldOpenCursor();
6095#endif
6096}
6097
6098/*
6099 * Cursor up commands.
6100 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6101 */
6102 static void
6103nv_up(cap)
6104 cmdarg_T *cap;
6105{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006106 if (mod_mask & MOD_MASK_SHIFT)
6107 {
6108 /* <S-Up> is page up */
6109 cap->arg = BACKWARD;
6110 nv_page(cap);
6111 }
6112 else
6113 {
6114 cap->oap->motion_type = MLINE;
6115 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6116 clearopbeep(cap->oap);
6117 else if (cap->arg)
6118 beginline(BL_WHITE | BL_FIX);
6119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120}
6121
6122/*
6123 * Cursor down commands.
6124 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6125 */
6126 static void
6127nv_down(cap)
6128 cmdarg_T *cap;
6129{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006130 if (mod_mask & MOD_MASK_SHIFT)
6131 {
6132 /* <S-Down> is page down */
6133 cap->arg = FORWARD;
6134 nv_page(cap);
6135 }
6136 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6138 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006139 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006140 if (curwin->w_llist_ref == NULL)
6141 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6142 else
6143 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 else
6145#endif
6146 {
6147#ifdef FEAT_CMDWIN
6148 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006149 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 cmdwin_result = CAR;
6151 else
6152#endif
6153 {
6154 cap->oap->motion_type = MLINE;
6155 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6156 clearopbeep(cap->oap);
6157 else if (cap->arg)
6158 beginline(BL_WHITE | BL_FIX);
6159 }
6160 }
6161}
6162
6163#ifdef FEAT_SEARCHPATH
6164/*
6165 * Grab the file name under the cursor and edit it.
6166 */
6167 static void
6168nv_gotofile(cap)
6169 cmdarg_T *cap;
6170{
6171 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006172 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006174 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 {
6176 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006177 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 return;
6179 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006180#ifdef FEAT_AUTOCMD
6181 if (curbuf_locked())
6182 {
6183 clearop(cap->oap);
6184 return;
6185 }
6186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006188 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189
6190 if (ptr != NULL)
6191 {
6192 /* do autowrite if necessary */
6193 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6194 autowrite(curbuf, FALSE);
6195 setpcmark();
6196 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006197 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006198 if (cap->nchar == 'F' && lnum >= 0)
6199 {
6200 curwin->w_cursor.lnum = lnum;
6201 check_cursor_lnum();
6202 beginline(BL_SOL | BL_FIX);
6203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 vim_free(ptr);
6205 }
6206 else
6207 clearop(cap->oap);
6208}
6209#endif
6210
6211/*
6212 * <End> command: to end of current line or last line.
6213 */
6214 static void
6215nv_end(cap)
6216 cmdarg_T *cap;
6217{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006218 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006220 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 nv_goto(cap);
6222 cap->count1 = 1; /* to end of current line */
6223 }
6224 nv_dollar(cap);
6225}
6226
6227/*
6228 * Handle the "$" command.
6229 */
6230 static void
6231nv_dollar(cap)
6232 cmdarg_T *cap;
6233{
6234 cap->oap->motion_type = MCHAR;
6235 cap->oap->inclusive = TRUE;
6236#ifdef FEAT_VIRTUALEDIT
6237 /* In virtual mode when off the edge of a line and an operator
6238 * is pending (whew!) keep the cursor where it is.
6239 * Otherwise, send it to the end of the line. */
6240 if (!virtual_active() || gchar_cursor() != NUL
6241 || cap->oap->op_type == OP_NOP)
6242#endif
6243 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6244 if (cursor_down((long)(cap->count1 - 1),
6245 cap->oap->op_type == OP_NOP) == FAIL)
6246 clearopbeep(cap->oap);
6247#ifdef FEAT_FOLDING
6248 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6249 foldOpenCursor();
6250#endif
6251}
6252
6253/*
6254 * Implementation of '?' and '/' commands.
6255 * If cap->arg is TRUE don't set PC mark.
6256 */
6257 static void
6258nv_search(cap)
6259 cmdarg_T *cap;
6260{
6261 oparg_T *oap = cap->oap;
6262
6263 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6264 {
6265 /* Translate "g??" to "g?g?" */
6266 cap->cmdchar = 'g';
6267 cap->nchar = '?';
6268 nv_operator(cap);
6269 return;
6270 }
6271
6272 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6273
6274 if (cap->searchbuf == NULL)
6275 {
6276 clearop(oap);
6277 return;
6278 }
6279
6280 normal_search(cap, cap->cmdchar, cap->searchbuf,
6281 (cap->arg ? 0 : SEARCH_MARK));
6282}
6283
6284/*
6285 * Handle "N" and "n" commands.
6286 * cap->arg is SEARCH_REV for "N", 0 for "n".
6287 */
6288 static void
6289nv_next(cap)
6290 cmdarg_T *cap;
6291{
6292 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6293}
6294
6295/*
6296 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6297 * Uses only cap->count1 and cap->oap from "cap".
6298 */
6299 static void
6300normal_search(cap, dir, pat, opt)
6301 cmdarg_T *cap;
6302 int dir;
6303 char_u *pat;
6304 int opt; /* extra flags for do_search() */
6305{
6306 int i;
6307
6308 cap->oap->motion_type = MCHAR;
6309 cap->oap->inclusive = FALSE;
6310 cap->oap->use_reg_one = TRUE;
6311 curwin->w_set_curswant = TRUE;
6312
6313 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006314 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 if (i == 0)
6316 clearop(cap->oap);
6317 else
6318 {
6319 if (i == 2)
6320 cap->oap->motion_type = MLINE;
6321#ifdef FEAT_VIRTUALEDIT
6322 curwin->w_cursor.coladd = 0;
6323#endif
6324#ifdef FEAT_FOLDING
6325 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6326 foldOpenCursor();
6327#endif
6328 }
6329
6330 /* "/$" will put the cursor after the end of the line, may need to
6331 * correct that here */
6332 check_cursor();
6333}
6334
6335/*
6336 * Character search commands.
6337 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6338 * ',' and FALSE for ';'.
6339 * cap->nchar is NUL for ',' and ';' (repeat the search)
6340 */
6341 static void
6342nv_csearch(cap)
6343 cmdarg_T *cap;
6344{
6345 int t_cmd;
6346
6347 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6348 t_cmd = TRUE;
6349 else
6350 t_cmd = FALSE;
6351
6352 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6354 clearopbeep(cap->oap);
6355 else
6356 {
6357 curwin->w_set_curswant = TRUE;
6358#ifdef FEAT_VIRTUALEDIT
6359 /* Include a Tab for "tx" and for "dfx". */
6360 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6361 && (t_cmd || cap->oap->op_type != OP_NOP))
6362 {
6363 colnr_T scol, ecol;
6364
6365 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6366 curwin->w_cursor.coladd = ecol - scol;
6367 }
6368 else
6369 curwin->w_cursor.coladd = 0;
6370#endif
6371#ifdef FEAT_VISUAL
6372 adjust_for_sel(cap);
6373#endif
6374#ifdef FEAT_FOLDING
6375 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6376 foldOpenCursor();
6377#endif
6378 }
6379}
6380
6381/*
6382 * "[" and "]" commands.
6383 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6384 */
6385 static void
6386nv_brackets(cap)
6387 cmdarg_T *cap;
6388{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006389 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 pos_T prev_pos;
6391 pos_T *pos = NULL; /* init for GCC */
6392 pos_T old_pos; /* cursor position before command */
6393 int flag;
6394 long n;
6395 int findc;
6396 int c;
6397
6398 cap->oap->motion_type = MCHAR;
6399 cap->oap->inclusive = FALSE;
6400 old_pos = curwin->w_cursor;
6401#ifdef FEAT_VIRTUALEDIT
6402 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6403#endif
6404
6405#ifdef FEAT_SEARCHPATH
6406 /*
6407 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6408 */
6409 if (cap->nchar == 'f')
6410 nv_gotofile(cap);
6411 else
6412#endif
6413
6414#ifdef FEAT_FIND_ID
6415 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006416 * Find the occurrence(s) of the identifier or define under cursor
6417 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 *
6419 * search list jump
6420 * fwd bwd fwd bwd fwd bwd
6421 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6422 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6423 */
6424 if (vim_strchr((char_u *)
6425#ifdef EBCDIC
6426 "iI\005dD\067",
6427#else
6428 "iI\011dD\004",
6429#endif
6430 cap->nchar) != NULL)
6431 {
6432 char_u *ptr;
6433 int len;
6434
6435 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6436 clearop(cap->oap);
6437 else
6438 {
6439 find_pattern_in_path(ptr, 0, len, TRUE,
6440 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6441 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6442 cap->count1,
6443 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6444 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6445 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6446 (linenr_T)MAXLNUM);
6447 curwin->w_set_curswant = TRUE;
6448 }
6449 }
6450 else
6451#endif
6452
6453 /*
6454 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6455 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6456 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6457 * "[m" or "]m" search for prev/next start of (Java) method.
6458 * "[M" or "]M" search for prev/next end of (Java) method.
6459 */
6460 if ( (cap->cmdchar == '['
6461 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6462 || (cap->cmdchar == ']'
6463 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6464 {
6465 if (cap->nchar == '*')
6466 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 prev_pos.lnum = 0;
6468 if (cap->nchar == 'm' || cap->nchar == 'M')
6469 {
6470 if (cap->cmdchar == '[')
6471 findc = '{';
6472 else
6473 findc = '}';
6474 n = 9999;
6475 }
6476 else
6477 {
6478 findc = cap->nchar;
6479 n = cap->count1;
6480 }
6481 for ( ; n > 0; --n)
6482 {
6483 if ((pos = findmatchlimit(cap->oap, findc,
6484 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6485 {
6486 if (new_pos.lnum == 0) /* nothing found */
6487 {
6488 if (cap->nchar != 'm' && cap->nchar != 'M')
6489 clearopbeep(cap->oap);
6490 }
6491 else
6492 pos = &new_pos; /* use last one found */
6493 break;
6494 }
6495 prev_pos = new_pos;
6496 curwin->w_cursor = *pos;
6497 new_pos = *pos;
6498 }
6499 curwin->w_cursor = old_pos;
6500
6501 /*
6502 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6503 * brought us to the match for "[m" and "]M" when inside a method.
6504 * Try finding the '{' or '}' we want to be at.
6505 * Also repeat for the given count.
6506 */
6507 if (cap->nchar == 'm' || cap->nchar == 'M')
6508 {
6509 /* norm is TRUE for "]M" and "[m" */
6510 int norm = ((findc == '{') == (cap->nchar == 'm'));
6511
6512 n = cap->count1;
6513 /* found a match: we were inside a method */
6514 if (prev_pos.lnum != 0)
6515 {
6516 pos = &prev_pos;
6517 curwin->w_cursor = prev_pos;
6518 if (norm)
6519 --n;
6520 }
6521 else
6522 pos = NULL;
6523 while (n > 0)
6524 {
6525 for (;;)
6526 {
6527 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6528 {
6529 /* if not found anything, that's an error */
6530 if (pos == NULL)
6531 clearopbeep(cap->oap);
6532 n = 0;
6533 break;
6534 }
6535 c = gchar_cursor();
6536 if (c == '{' || c == '}')
6537 {
6538 /* Must have found end/start of class: use it.
6539 * Or found the place to be at. */
6540 if ((c == findc && norm) || (n == 1 && !norm))
6541 {
6542 new_pos = curwin->w_cursor;
6543 pos = &new_pos;
6544 n = 0;
6545 }
6546 /* if no match found at all, we started outside of the
6547 * class and we're inside now. Just go on. */
6548 else if (new_pos.lnum == 0)
6549 {
6550 new_pos = curwin->w_cursor;
6551 pos = &new_pos;
6552 }
6553 /* found start/end of other method: go to match */
6554 else if ((pos = findmatchlimit(cap->oap, findc,
6555 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6556 0)) == NULL)
6557 n = 0;
6558 else
6559 curwin->w_cursor = *pos;
6560 break;
6561 }
6562 }
6563 --n;
6564 }
6565 curwin->w_cursor = old_pos;
6566 if (pos == NULL && new_pos.lnum != 0)
6567 clearopbeep(cap->oap);
6568 }
6569 if (pos != NULL)
6570 {
6571 setpcmark();
6572 curwin->w_cursor = *pos;
6573 curwin->w_set_curswant = TRUE;
6574#ifdef FEAT_FOLDING
6575 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6576 && cap->oap->op_type == OP_NOP)
6577 foldOpenCursor();
6578#endif
6579 }
6580 }
6581
6582 /*
6583 * "[[", "[]", "]]" and "][": move to start or end of function
6584 */
6585 else if (cap->nchar == '[' || cap->nchar == ']')
6586 {
6587 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6588 flag = '{';
6589 else
6590 flag = '}'; /* "][" or "[]" */
6591
6592 curwin->w_set_curswant = TRUE;
6593 /*
6594 * Imitate strange Vi behaviour: When using "]]" with an operator
6595 * we also stop at '}'.
6596 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006597 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 (cap->oap->op_type != OP_NOP
6599 && cap->arg == FORWARD && flag == '{')))
6600 clearopbeep(cap->oap);
6601 else
6602 {
6603 if (cap->oap->op_type == OP_NOP)
6604 beginline(BL_WHITE | BL_FIX);
6605#ifdef FEAT_FOLDING
6606 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6607 foldOpenCursor();
6608#endif
6609 }
6610 }
6611
6612 /*
6613 * "[p", "[P", "]P" and "]p": put with indent adjustment
6614 */
6615 else if (cap->nchar == 'p' || cap->nchar == 'P')
6616 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006617 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 {
6619 prep_redo_cmd(cap);
6620 do_put(cap->oap->regname,
6621 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6622 cap->count1, PUT_FIXINDENT);
6623 }
6624 }
6625
6626 /*
6627 * "['", "[`", "]'" and "]`": jump to next mark
6628 */
6629 else if (cap->nchar == '\'' || cap->nchar == '`')
6630 {
6631 pos = &curwin->w_cursor;
6632 for (n = cap->count1; n > 0; --n)
6633 {
6634 prev_pos = *pos;
6635 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6636 cap->nchar == '\'');
6637 if (pos == NULL)
6638 break;
6639 }
6640 if (pos == NULL)
6641 pos = &prev_pos;
6642 nv_cursormark(cap, cap->nchar == '\'', pos);
6643 }
6644
6645#ifdef FEAT_MOUSE
6646 /*
6647 * [ or ] followed by a middle mouse click: put selected text with
6648 * indent adjustment. Any other button just does as usual.
6649 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006650 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 {
6652 (void)do_mouse(cap->oap, cap->nchar,
6653 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6654 cap->count1, PUT_FIXINDENT);
6655 }
6656#endif /* FEAT_MOUSE */
6657
6658#ifdef FEAT_FOLDING
6659 /*
6660 * "[z" and "]z": move to start or end of open fold.
6661 */
6662 else if (cap->nchar == 'z')
6663 {
6664 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6665 cap->count1) == FAIL)
6666 clearopbeep(cap->oap);
6667 }
6668#endif
6669
6670#ifdef FEAT_DIFF
6671 /*
6672 * "[c" and "]c": move to next or previous diff-change.
6673 */
6674 else if (cap->nchar == 'c')
6675 {
6676 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6677 cap->count1) == FAIL)
6678 clearopbeep(cap->oap);
6679 }
6680#endif
6681
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006682#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006683 /*
6684 * "[s", "[S", "]s" and "]S": move to next spell error.
6685 */
6686 else if (cap->nchar == 's' || cap->nchar == 'S')
6687 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006688 setpcmark();
6689 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006690 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6691 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006692 {
6693 clearopbeep(cap->oap);
6694 break;
6695 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006696# ifdef FEAT_FOLDING
6697 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6698 foldOpenCursor();
6699# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006700 }
6701#endif
6702
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 /* Not a valid cap->nchar. */
6704 else
6705 clearopbeep(cap->oap);
6706}
6707
6708/*
6709 * Handle Normal mode "%" command.
6710 */
6711 static void
6712nv_percent(cap)
6713 cmdarg_T *cap;
6714{
6715 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006716#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 linenr_T lnum = curwin->w_cursor.lnum;
6718#endif
6719
6720 cap->oap->inclusive = TRUE;
6721 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6722 {
6723 if (cap->count0 > 100)
6724 clearopbeep(cap->oap);
6725 else
6726 {
6727 cap->oap->motion_type = MLINE;
6728 setpcmark();
6729 /* Round up, so CTRL-G will give same value. Watch out for a
6730 * large line count, the line number must not go negative! */
6731 if (curbuf->b_ml.ml_line_count > 1000000)
6732 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6733 / 100L * cap->count0;
6734 else
6735 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6736 cap->count0 + 99L) / 100L;
6737 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6738 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6739 beginline(BL_SOL | BL_FIX);
6740 }
6741 }
6742 else /* "%" : go to matching paren */
6743 {
6744 cap->oap->motion_type = MCHAR;
6745 cap->oap->use_reg_one = TRUE;
6746 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6747 clearopbeep(cap->oap);
6748 else
6749 {
6750 setpcmark();
6751 curwin->w_cursor = *pos;
6752 curwin->w_set_curswant = TRUE;
6753#ifdef FEAT_VIRTUALEDIT
6754 curwin->w_cursor.coladd = 0;
6755#endif
6756#ifdef FEAT_VISUAL
6757 adjust_for_sel(cap);
6758#endif
6759 }
6760 }
6761#ifdef FEAT_FOLDING
6762 if (cap->oap->op_type == OP_NOP
6763 && lnum != curwin->w_cursor.lnum
6764 && (fdo_flags & FDO_PERCENT)
6765 && KeyTyped)
6766 foldOpenCursor();
6767#endif
6768}
6769
6770/*
6771 * Handle "(" and ")" commands.
6772 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6773 */
6774 static void
6775nv_brace(cap)
6776 cmdarg_T *cap;
6777{
6778 cap->oap->motion_type = MCHAR;
6779 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006780 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6781 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 curwin->w_set_curswant = TRUE;
6783
6784 if (findsent(cap->arg, cap->count1) == FAIL)
6785 clearopbeep(cap->oap);
6786 else
6787 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006788 /* Don't leave the cursor on the NUL past end of line. */
6789 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790#ifdef FEAT_VIRTUALEDIT
6791 curwin->w_cursor.coladd = 0;
6792#endif
6793#ifdef FEAT_FOLDING
6794 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6795 foldOpenCursor();
6796#endif
6797 }
6798}
6799
6800/*
6801 * "m" command: Mark a position.
6802 */
6803 static void
6804nv_mark(cap)
6805 cmdarg_T *cap;
6806{
6807 if (!checkclearop(cap->oap))
6808 {
6809 if (setmark(cap->nchar) == FAIL)
6810 clearopbeep(cap->oap);
6811 }
6812}
6813
6814/*
6815 * "{" and "}" commands.
6816 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6817 */
6818 static void
6819nv_findpar(cap)
6820 cmdarg_T *cap;
6821{
6822 cap->oap->motion_type = MCHAR;
6823 cap->oap->inclusive = FALSE;
6824 cap->oap->use_reg_one = TRUE;
6825 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006826 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 clearopbeep(cap->oap);
6828 else
6829 {
6830#ifdef FEAT_VIRTUALEDIT
6831 curwin->w_cursor.coladd = 0;
6832#endif
6833#ifdef FEAT_FOLDING
6834 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6835 foldOpenCursor();
6836#endif
6837 }
6838}
6839
6840/*
6841 * "u" command: Undo or make lower case.
6842 */
6843 static void
6844nv_undo(cap)
6845 cmdarg_T *cap;
6846{
6847 if (cap->oap->op_type == OP_LOWER
6848#ifdef FEAT_VISUAL
6849 || VIsual_active
6850#endif
6851 )
6852 {
6853 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6854 cap->cmdchar = 'g';
6855 cap->nchar = 'u';
6856 nv_operator(cap);
6857 }
6858 else
6859 nv_kundo(cap);
6860}
6861
6862/*
6863 * <Undo> command.
6864 */
6865 static void
6866nv_kundo(cap)
6867 cmdarg_T *cap;
6868{
6869 if (!checkclearopq(cap->oap))
6870 {
6871 u_undo((int)cap->count1);
6872 curwin->w_set_curswant = TRUE;
6873 }
6874}
6875
6876/*
6877 * Handle the "r" command.
6878 */
6879 static void
6880nv_replace(cap)
6881 cmdarg_T *cap;
6882{
6883 char_u *ptr;
6884 int had_ctrl_v;
6885 long n;
6886
6887 if (checkclearop(cap->oap))
6888 return;
6889
6890 /* get another character */
6891 if (cap->nchar == Ctrl_V)
6892 {
6893 had_ctrl_v = Ctrl_V;
6894 cap->nchar = get_literal();
6895 /* Don't redo a multibyte character with CTRL-V. */
6896 if (cap->nchar > DEL)
6897 had_ctrl_v = NUL;
6898 }
6899 else
6900 had_ctrl_v = NUL;
6901
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006902 /* Abort if the character is a special key. */
6903 if (IS_SPECIAL(cap->nchar))
6904 {
6905 clearopbeep(cap->oap);
6906 return;
6907 }
6908
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909#ifdef FEAT_VISUAL
6910 /* Visual mode "r" */
6911 if (VIsual_active)
6912 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006913 if (got_int)
6914 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 nv_operator(cap);
6916 return;
6917 }
6918#endif
6919
6920#ifdef FEAT_VIRTUALEDIT
6921 /* Break tabs, etc. */
6922 if (virtual_active())
6923 {
6924 if (u_save_cursor() == FAIL)
6925 return;
6926 if (gchar_cursor() == NUL)
6927 {
6928 /* Add extra space and put the cursor on the first one. */
6929 coladvance_force((colnr_T)(getviscol() + cap->count1));
6930 curwin->w_cursor.col -= cap->count1;
6931 }
6932 else if (gchar_cursor() == TAB)
6933 coladvance_force(getviscol());
6934 }
6935#endif
6936
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006937 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006939 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940#ifdef FEAT_MBYTE
6941 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6942#endif
6943 )
6944 {
6945 clearopbeep(cap->oap);
6946 return;
6947 }
6948
6949 /*
6950 * Replacing with a TAB is done by edit() when it is complicated because
6951 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6952 * Other characters are done below to avoid problems with things like
6953 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6954 */
6955 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6956 {
6957 stuffnumReadbuff(cap->count1);
6958 stuffcharReadbuff('R');
6959 stuffcharReadbuff('\t');
6960 stuffcharReadbuff(ESC);
6961 return;
6962 }
6963
6964 /* save line for undo */
6965 if (u_save_cursor() == FAIL)
6966 return;
6967
6968 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6969 {
6970 /*
6971 * Replace character(s) by a single newline.
6972 * Strange vi behaviour: Only one newline is inserted.
6973 * Delete the characters here.
6974 * Insert the newline with an insert command, takes care of
6975 * autoindent. The insert command depends on being on the last
6976 * character of a line or not.
6977 */
6978#ifdef FEAT_MBYTE
6979 (void)del_chars(cap->count1, FALSE); /* delete the characters */
6980#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00006981 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982#endif
6983 stuffcharReadbuff('\r');
6984 stuffcharReadbuff(ESC);
6985
6986 /* Give 'r' to edit(), to get the redo command right. */
6987 invoke_edit(cap, TRUE, 'r', FALSE);
6988 }
6989 else
6990 {
6991 prep_redo(cap->oap->regname, cap->count1,
6992 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
6993
6994 curbuf->b_op_start = curwin->w_cursor;
6995#ifdef FEAT_MBYTE
6996 if (has_mbyte)
6997 {
6998 int old_State = State;
6999
7000 if (cap->ncharC1 != 0)
7001 AppendCharToRedobuff(cap->ncharC1);
7002 if (cap->ncharC2 != 0)
7003 AppendCharToRedobuff(cap->ncharC2);
7004
7005 /* This is slow, but it handles replacing a single-byte with a
7006 * multi-byte and the other way around. Also handles adding
7007 * composing characters for utf-8. */
7008 for (n = cap->count1; n > 0; --n)
7009 {
7010 State = REPLACE;
7011 ins_char(cap->nchar);
7012 State = old_State;
7013 if (cap->ncharC1 != 0)
7014 ins_char(cap->ncharC1);
7015 if (cap->ncharC2 != 0)
7016 ins_char(cap->ncharC2);
7017 }
7018 }
7019 else
7020#endif
7021 {
7022 /*
7023 * Replace the characters within one line.
7024 */
7025 for (n = cap->count1; n > 0; --n)
7026 {
7027 /*
7028 * Get ptr again, because u_save and/or showmatch() will have
7029 * released the line. At the same time we let know that the
7030 * line will be changed.
7031 */
7032 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
7033 ptr[curwin->w_cursor.col] = cap->nchar;
7034 if (p_sm && msg_silent == 0)
7035 showmatch(cap->nchar);
7036 ++curwin->w_cursor.col;
7037 }
7038#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007039 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007041 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042
Bram Moolenaar009b2592004-10-24 19:18:58 +00007043 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007044 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007046 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 }
7048#endif
7049
7050 /* mark the buffer as changed and prepare for displaying */
7051 changed_bytes(curwin->w_cursor.lnum,
7052 (colnr_T)(curwin->w_cursor.col - cap->count1));
7053 }
7054 --curwin->w_cursor.col; /* cursor on the last replaced char */
7055#ifdef FEAT_MBYTE
7056 /* if the character on the left of the current cursor is a multi-byte
7057 * character, move two characters left */
7058 if (has_mbyte)
7059 mb_adjust_cursor();
7060#endif
7061 curbuf->b_op_end = curwin->w_cursor;
7062 curwin->w_set_curswant = TRUE;
7063 set_last_insert(cap->nchar);
7064 }
7065}
7066
7067#ifdef FEAT_VISUAL
7068/*
7069 * 'o': Exchange start and end of Visual area.
7070 * 'O': same, but in block mode exchange left and right corners.
7071 */
7072 static void
7073v_swap_corners(cmdchar)
7074 int cmdchar;
7075{
7076 pos_T old_cursor;
7077 colnr_T left, right;
7078
7079 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7080 {
7081 old_cursor = curwin->w_cursor;
7082 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7083 curwin->w_cursor.lnum = VIsual.lnum;
7084 coladvance(left);
7085 VIsual = curwin->w_cursor;
7086
7087 curwin->w_cursor.lnum = old_cursor.lnum;
7088 curwin->w_curswant = right;
7089 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7090 * right one column */
7091 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7092 ++curwin->w_curswant;
7093 coladvance(curwin->w_curswant);
7094 if (curwin->w_cursor.col == old_cursor.col
7095#ifdef FEAT_VIRTUALEDIT
7096 && (!virtual_active()
7097 || curwin->w_cursor.coladd == old_cursor.coladd)
7098#endif
7099 )
7100 {
7101 curwin->w_cursor.lnum = VIsual.lnum;
7102 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7103 ++right;
7104 coladvance(right);
7105 VIsual = curwin->w_cursor;
7106
7107 curwin->w_cursor.lnum = old_cursor.lnum;
7108 coladvance(left);
7109 curwin->w_curswant = left;
7110 }
7111 }
7112 else
7113 {
7114 old_cursor = curwin->w_cursor;
7115 curwin->w_cursor = VIsual;
7116 VIsual = old_cursor;
7117 curwin->w_set_curswant = TRUE;
7118 }
7119}
7120#endif /* FEAT_VISUAL */
7121
7122/*
7123 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7124 */
7125 static void
7126nv_Replace(cap)
7127 cmdarg_T *cap;
7128{
7129#ifdef FEAT_VISUAL
7130 if (VIsual_active) /* "R" is replace lines */
7131 {
7132 cap->cmdchar = 'c';
7133 cap->nchar = NUL;
7134 VIsual_mode = 'V';
7135 nv_operator(cap);
7136 }
7137 else
7138#endif
7139 if (!checkclearopq(cap->oap))
7140 {
7141 if (!curbuf->b_p_ma)
7142 EMSG(_(e_modifiable));
7143 else
7144 {
7145#ifdef FEAT_VIRTUALEDIT
7146 if (virtual_active())
7147 coladvance(getviscol());
7148#endif
7149 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7150 }
7151 }
7152}
7153
7154#ifdef FEAT_VREPLACE
7155/*
7156 * "gr".
7157 */
7158 static void
7159nv_vreplace(cap)
7160 cmdarg_T *cap;
7161{
7162# ifdef FEAT_VISUAL
7163 if (VIsual_active)
7164 {
7165 cap->cmdchar = 'r';
7166 cap->nchar = cap->extra_char;
7167 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7168 }
7169 else
7170# endif
7171 if (!checkclearopq(cap->oap))
7172 {
7173 if (!curbuf->b_p_ma)
7174 EMSG(_(e_modifiable));
7175 else
7176 {
7177 if (cap->extra_char == Ctrl_V) /* get another character */
7178 cap->extra_char = get_literal();
7179 stuffcharReadbuff(cap->extra_char);
7180 stuffcharReadbuff(ESC);
7181# ifdef FEAT_VIRTUALEDIT
7182 if (virtual_active())
7183 coladvance(getviscol());
7184# endif
7185 invoke_edit(cap, TRUE, 'v', FALSE);
7186 }
7187 }
7188}
7189#endif
7190
7191/*
7192 * Swap case for "~" command, when it does not work like an operator.
7193 */
7194 static void
7195n_swapchar(cap)
7196 cmdarg_T *cap;
7197{
7198 long n;
7199 pos_T startpos;
7200 int did_change = 0;
7201#ifdef FEAT_NETBEANS_INTG
7202 pos_T pos;
7203 char_u *ptr;
7204 int count;
7205#endif
7206
7207 if (checkclearopq(cap->oap))
7208 return;
7209
7210 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7211 {
7212 clearopbeep(cap->oap);
7213 return;
7214 }
7215
7216 prep_redo_cmd(cap);
7217
7218 if (u_save_cursor() == FAIL)
7219 return;
7220
7221 startpos = curwin->w_cursor;
7222#ifdef FEAT_NETBEANS_INTG
7223 pos = startpos;
7224#endif
7225 for (n = cap->count1; n > 0; --n)
7226 {
7227 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7228 inc_cursor();
7229 if (gchar_cursor() == NUL)
7230 {
7231 if (vim_strchr(p_ww, '~') != NULL
7232 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7233 {
7234#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007235 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 {
7237 if (did_change)
7238 {
7239 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007240 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007241 netbeans_removed(curbuf, pos.lnum, pos.col,
7242 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007244 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 }
7246 pos.col = 0;
7247 pos.lnum++;
7248 }
7249#endif
7250 ++curwin->w_cursor.lnum;
7251 curwin->w_cursor.col = 0;
7252 if (n > 1)
7253 {
7254 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7255 break;
7256 u_clearline();
7257 }
7258 }
7259 else
7260 break;
7261 }
7262 }
7263#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007264 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 {
7266 ptr = ml_get(pos.lnum);
7267 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007268 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7269 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 }
7271#endif
7272
7273
7274 check_cursor();
7275 curwin->w_set_curswant = TRUE;
7276 if (did_change)
7277 {
7278 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7279 0L);
7280 curbuf->b_op_start = startpos;
7281 curbuf->b_op_end = curwin->w_cursor;
7282 if (curbuf->b_op_end.col > 0)
7283 --curbuf->b_op_end.col;
7284 }
7285}
7286
7287/*
7288 * Move cursor to mark.
7289 */
7290 static void
7291nv_cursormark(cap, flag, pos)
7292 cmdarg_T *cap;
7293 int flag;
7294 pos_T *pos;
7295{
7296 if (check_mark(pos) == FAIL)
7297 clearop(cap->oap);
7298 else
7299 {
7300 if (cap->cmdchar == '\''
7301 || cap->cmdchar == '`'
7302 || cap->cmdchar == '['
7303 || cap->cmdchar == ']')
7304 setpcmark();
7305 curwin->w_cursor = *pos;
7306 if (flag)
7307 beginline(BL_WHITE | BL_FIX);
7308 else
7309 check_cursor();
7310 }
7311 cap->oap->motion_type = flag ? MLINE : MCHAR;
7312 if (cap->cmdchar == '`')
7313 cap->oap->use_reg_one = TRUE;
7314 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7315 curwin->w_set_curswant = TRUE;
7316}
7317
7318#ifdef FEAT_VISUAL
7319/*
7320 * Handle commands that are operators in Visual mode.
7321 */
7322 static void
7323v_visop(cap)
7324 cmdarg_T *cap;
7325{
7326 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7327
7328 /* Uppercase means linewise, except in block mode, then "D" deletes till
7329 * the end of the line, and "C" replaces til EOL */
7330 if (isupper(cap->cmdchar))
7331 {
7332 if (VIsual_mode != Ctrl_V)
7333 VIsual_mode = 'V';
7334 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7335 curwin->w_curswant = MAXCOL;
7336 }
7337 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7338 nv_operator(cap);
7339}
7340#endif
7341
7342/*
7343 * "s" and "S" commands.
7344 */
7345 static void
7346nv_subst(cap)
7347 cmdarg_T *cap;
7348{
7349#ifdef FEAT_VISUAL
7350 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7351 {
7352 if (cap->cmdchar == 'S')
7353 VIsual_mode = 'V';
7354 cap->cmdchar = 'c';
7355 nv_operator(cap);
7356 }
7357 else
7358#endif
7359 nv_optrans(cap);
7360}
7361
7362/*
7363 * Abbreviated commands.
7364 */
7365 static void
7366nv_abbrev(cap)
7367 cmdarg_T *cap;
7368{
7369 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7370 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7371
7372#ifdef FEAT_VISUAL
7373 /* in Visual mode these commands are operators */
7374 if (VIsual_active)
7375 v_visop(cap);
7376 else
7377#endif
7378 nv_optrans(cap);
7379}
7380
7381/*
7382 * Translate a command into another command.
7383 */
7384 static void
7385nv_optrans(cap)
7386 cmdarg_T *cap;
7387{
7388 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7389 (char_u *)"d$", (char_u *)"c$",
7390 (char_u *)"cl", (char_u *)"cc",
7391 (char_u *)"yy", (char_u *)":s\r"};
7392 static char_u *str = (char_u *)"xXDCsSY&";
7393
7394 if (!checkclearopq(cap->oap))
7395 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007396 /* In Vi "2D" doesn't delete the next line. Can't translate it
7397 * either, because "2." should also not use the count. */
7398 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7399 {
7400 cap->oap->start = curwin->w_cursor;
7401 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007402#ifdef FEAT_EVAL
7403 set_op_var(OP_DELETE);
7404#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007405 cap->count1 = 1;
7406 nv_dollar(cap);
7407 finish_op = TRUE;
7408 ResetRedobuff();
7409 AppendCharToRedobuff('D');
7410 }
7411 else
7412 {
7413 if (cap->count0)
7414 stuffnumReadbuff(cap->count0);
7415 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 }
7418 cap->opcount = 0;
7419}
7420
7421/*
7422 * "'" and "`" commands. Also for "g'" and "g`".
7423 * cap->arg is TRUE for "'" and "g'".
7424 */
7425 static void
7426nv_gomark(cap)
7427 cmdarg_T *cap;
7428{
7429 pos_T *pos;
7430 int c;
7431#ifdef FEAT_FOLDING
7432 linenr_T lnum = curwin->w_cursor.lnum;
7433 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7434#endif
7435
7436 if (cap->cmdchar == 'g')
7437 c = cap->extra_char;
7438 else
7439 c = cap->nchar;
7440 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7441 if (pos == (pos_T *)-1) /* jumped to other file */
7442 {
7443 if (cap->arg)
7444 {
7445 check_cursor_lnum();
7446 beginline(BL_WHITE | BL_FIX);
7447 }
7448 else
7449 check_cursor();
7450 }
7451 else
7452 nv_cursormark(cap, cap->arg, pos);
7453
7454#ifdef FEAT_VIRTUALEDIT
7455 /* May need to clear the coladd that a mark includes. */
7456 if (!virtual_active())
7457 curwin->w_cursor.coladd = 0;
7458#endif
7459#ifdef FEAT_FOLDING
7460 if (cap->oap->op_type == OP_NOP
7461 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7462 && (fdo_flags & FDO_MARK)
7463 && old_KeyTyped)
7464 foldOpenCursor();
7465#endif
7466}
7467
7468/*
7469 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7470 */
7471 static void
7472nv_pcmark(cap)
7473 cmdarg_T *cap;
7474{
7475#ifdef FEAT_JUMPLIST
7476 pos_T *pos;
7477# ifdef FEAT_FOLDING
7478 linenr_T lnum = curwin->w_cursor.lnum;
7479 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7480# endif
7481
7482 if (!checkclearopq(cap->oap))
7483 {
7484 if (cap->cmdchar == 'g')
7485 pos = movechangelist((int)cap->count1);
7486 else
7487 pos = movemark((int)cap->count1);
7488 if (pos == (pos_T *)-1) /* jump to other file */
7489 {
7490 curwin->w_set_curswant = TRUE;
7491 check_cursor();
7492 }
7493 else if (pos != NULL) /* can jump */
7494 nv_cursormark(cap, FALSE, pos);
7495 else if (cap->cmdchar == 'g')
7496 {
7497 if (curbuf->b_changelistlen == 0)
7498 EMSG(_("E664: changelist is empty"));
7499 else if (cap->count1 < 0)
7500 EMSG(_("E662: At start of changelist"));
7501 else
7502 EMSG(_("E663: At end of changelist"));
7503 }
7504 else
7505 clearopbeep(cap->oap);
7506# ifdef FEAT_FOLDING
7507 if (cap->oap->op_type == OP_NOP
7508 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7509 && (fdo_flags & FDO_MARK)
7510 && old_KeyTyped)
7511 foldOpenCursor();
7512# endif
7513 }
7514#else
7515 clearopbeep(cap->oap);
7516#endif
7517}
7518
7519/*
7520 * Handle '"' command.
7521 */
7522 static void
7523nv_regname(cap)
7524 cmdarg_T *cap;
7525{
7526 if (checkclearop(cap->oap))
7527 return;
7528#ifdef FEAT_EVAL
7529 if (cap->nchar == '=')
7530 cap->nchar = get_expr_register();
7531#endif
7532 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7533 {
7534 cap->oap->regname = cap->nchar;
7535 cap->opcount = cap->count0; /* remember count before '"' */
7536#ifdef FEAT_EVAL
7537 set_reg_var(cap->oap->regname);
7538#endif
7539 }
7540 else
7541 clearopbeep(cap->oap);
7542}
7543
7544#ifdef FEAT_VISUAL
7545/*
7546 * Handle "v", "V" and "CTRL-V" commands.
7547 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7548 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007549 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 */
7551 static void
7552nv_visual(cap)
7553 cmdarg_T *cap;
7554{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007555 if (cap->cmdchar == Ctrl_Q)
7556 cap->cmdchar = Ctrl_V;
7557
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7559 * characterwise, linewise, or blockwise. */
7560 if (cap->oap->op_type != OP_NOP)
7561 {
7562 cap->oap->motion_force = cap->cmdchar;
7563 finish_op = FALSE; /* operator doesn't finish now but later */
7564 return;
7565 }
7566
7567 VIsual_select = cap->arg;
7568 if (VIsual_active) /* change Visual mode */
7569 {
7570 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7571 end_visual_mode();
7572 else /* toggle char/block mode */
7573 { /* or char/line mode */
7574 VIsual_mode = cap->cmdchar;
7575 showmode();
7576 }
7577 redraw_curbuf_later(INVERTED); /* update the inversion */
7578 }
7579 else /* start Visual mode */
7580 {
7581 check_visual_highlight();
7582 if (cap->count0) /* use previously selected part */
7583 {
7584 if (resel_VIsual_mode == NUL) /* there is none */
7585 {
7586 beep_flush();
7587 return;
7588 }
7589 VIsual = curwin->w_cursor;
7590
7591 VIsual_active = TRUE;
7592 VIsual_reselect = TRUE;
7593 if (!cap->arg)
7594 /* start Select mode when 'selectmode' contains "cmd" */
7595 may_start_select('c');
7596#ifdef FEAT_MOUSE
7597 setmouse();
7598#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007599 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 redraw_cmdline = TRUE; /* show visual mode later */
7601 /*
7602 * For V and ^V, we multiply the number of lines even if there
7603 * was only one -- webb
7604 */
7605 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7606 {
7607 curwin->w_cursor.lnum +=
7608 resel_VIsual_line_count * cap->count0 - 1;
7609 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7610 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7611 }
7612 VIsual_mode = resel_VIsual_mode;
7613 if (VIsual_mode == 'v')
7614 {
7615 if (resel_VIsual_line_count <= 1)
7616 curwin->w_cursor.col += resel_VIsual_col * cap->count0 - 1;
7617 else
7618 curwin->w_cursor.col = resel_VIsual_col;
7619 check_cursor_col();
7620 }
7621 if (resel_VIsual_col == MAXCOL)
7622 {
7623 curwin->w_curswant = MAXCOL;
7624 coladvance((colnr_T)MAXCOL);
7625 }
7626 else if (VIsual_mode == Ctrl_V)
7627 {
7628 validate_virtcol();
7629 curwin->w_curswant = curwin->w_virtcol
7630 + resel_VIsual_col * cap->count0 - 1;
7631 coladvance(curwin->w_curswant);
7632 }
7633 else
7634 curwin->w_set_curswant = TRUE;
7635 redraw_curbuf_later(INVERTED); /* show the inversion */
7636 }
7637 else
7638 {
7639 if (!cap->arg)
7640 /* start Select mode when 'selectmode' contains "cmd" */
7641 may_start_select('c');
7642 n_start_visual_mode(cap->cmdchar);
7643 }
7644 }
7645}
7646
7647/*
7648 * Start selection for Shift-movement keys.
7649 */
7650 void
7651start_selection()
7652{
7653 /* if 'selectmode' contains "key", start Select mode */
7654 may_start_select('k');
7655 n_start_visual_mode('v');
7656}
7657
7658/*
7659 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7660 */
7661 void
7662may_start_select(c)
7663 int c;
7664{
7665 VIsual_select = (stuff_empty() && typebuf_typed()
7666 && (vim_strchr(p_slm, c) != NULL));
7667}
7668
7669/*
7670 * Start Visual mode "c".
7671 * Should set VIsual_select before calling this.
7672 */
7673 static void
7674n_start_visual_mode(c)
7675 int c;
7676{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007677#ifdef FEAT_CONCEAL
7678 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007679 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007680#endif
7681
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 VIsual_mode = c;
7683 VIsual_active = TRUE;
7684 VIsual_reselect = TRUE;
7685#ifdef FEAT_VIRTUALEDIT
7686 /* Corner case: the 0 position in a tab may change when going into
7687 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7688 */
7689 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7690 coladvance(curwin->w_virtcol);
7691#endif
7692 VIsual = curwin->w_cursor;
7693
7694#ifdef FEAT_FOLDING
7695 foldAdjustVisual();
7696#endif
7697
7698#ifdef FEAT_MOUSE
7699 setmouse();
7700#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007701#ifdef FEAT_CONCEAL
7702 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007703 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007704#endif
7705
Bram Moolenaar09df3122006-01-23 22:23:09 +00007706 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 redraw_cmdline = TRUE; /* show visual mode later */
7708#ifdef FEAT_CLIPBOARD
7709 /* Make sure the clipboard gets updated. Needed because start and
7710 * end may still be the same, and the selection needs to be owned */
7711 clip_star.vmode = NUL;
7712#endif
7713
7714 /* Only need to redraw this line, unless still need to redraw an old
7715 * Visual area (when 'lazyredraw' is set). */
7716 if (curwin->w_redr_type < INVERTED)
7717 {
7718 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7719 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7720 }
7721}
7722
7723#endif /* FEAT_VISUAL */
7724
7725/*
7726 * CTRL-W: Window commands
7727 */
7728 static void
7729nv_window(cap)
7730 cmdarg_T *cap;
7731{
7732#ifdef FEAT_WINDOWS
7733 if (!checkclearop(cap->oap))
7734 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7735#else
7736 (void)checkclearop(cap->oap);
7737#endif
7738}
7739
7740/*
7741 * CTRL-Z: Suspend
7742 */
7743 static void
7744nv_suspend(cap)
7745 cmdarg_T *cap;
7746{
7747 clearop(cap->oap);
7748#ifdef FEAT_VISUAL
7749 if (VIsual_active)
7750 end_visual_mode(); /* stop Visual mode */
7751#endif
7752 do_cmdline_cmd((char_u *)"st");
7753}
7754
7755/*
7756 * Commands starting with "g".
7757 */
7758 static void
7759nv_g_cmd(cap)
7760 cmdarg_T *cap;
7761{
7762 oparg_T *oap = cap->oap;
7763#ifdef FEAT_VISUAL
7764 pos_T tpos;
7765#endif
7766 int i;
7767 int flag = FALSE;
7768
7769 switch (cap->nchar)
7770 {
7771#ifdef MEM_PROFILE
7772 /*
7773 * "g^A": dump log of used memory.
7774 */
7775 case Ctrl_A:
7776 vim_mem_profile_dump();
7777 break;
7778#endif
7779
7780#ifdef FEAT_VREPLACE
7781 /*
7782 * "gR": Enter virtual replace mode.
7783 */
7784 case 'R':
7785 cap->arg = TRUE;
7786 nv_Replace(cap);
7787 break;
7788
7789 case 'r':
7790 nv_vreplace(cap);
7791 break;
7792#endif
7793
7794 case '&':
7795 do_cmdline_cmd((char_u *)"%s//~/&");
7796 break;
7797
7798#ifdef FEAT_VISUAL
7799 /*
7800 * "gv": Reselect the previous Visual area. If Visual already active,
7801 * exchange previous and current Visual area.
7802 */
7803 case 'v':
7804 if (checkclearop(oap))
7805 break;
7806
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007807 if ( curbuf->b_visual.vi_start.lnum == 0
7808 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7809 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 beep_flush();
7811 else
7812 {
7813 /* set w_cursor to the start of the Visual area, tpos to the end */
7814 if (VIsual_active)
7815 {
7816 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007817 VIsual_mode = curbuf->b_visual.vi_mode;
7818 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819# ifdef FEAT_EVAL
7820 curbuf->b_visual_mode_eval = i;
7821# endif
7822 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007823 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7824 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007826 tpos = curbuf->b_visual.vi_end;
7827 curbuf->b_visual.vi_end = curwin->w_cursor;
7828 curwin->w_cursor = curbuf->b_visual.vi_start;
7829 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 }
7831 else
7832 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007833 VIsual_mode = curbuf->b_visual.vi_mode;
7834 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7835 tpos = curbuf->b_visual.vi_end;
7836 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 }
7838
7839 VIsual_active = TRUE;
7840 VIsual_reselect = TRUE;
7841
7842 /* Set Visual to the start and w_cursor to the end of the Visual
7843 * area. Make sure they are on an existing character. */
7844 check_cursor();
7845 VIsual = curwin->w_cursor;
7846 curwin->w_cursor = tpos;
7847 check_cursor();
7848 update_topline();
7849 /*
7850 * When called from normal "g" command: start Select mode when
7851 * 'selectmode' contains "cmd". When called for K_SELECT, always
7852 * start Select mode.
7853 */
7854 if (cap->arg)
7855 VIsual_select = TRUE;
7856 else
7857 may_start_select('c');
7858#ifdef FEAT_MOUSE
7859 setmouse();
7860#endif
7861#ifdef FEAT_CLIPBOARD
7862 /* Make sure the clipboard gets updated. Needed because start and
7863 * end are still the same, and the selection needs to be owned */
7864 clip_star.vmode = NUL;
7865#endif
7866 redraw_curbuf_later(INVERTED);
7867 showmode();
7868 }
7869 break;
7870 /*
7871 * "gV": Don't reselect the previous Visual area after a Select mode
7872 * mapping of menu.
7873 */
7874 case 'V':
7875 VIsual_reselect = FALSE;
7876 break;
7877
7878 /*
7879 * "gh": start Select mode.
7880 * "gH": start Select line mode.
7881 * "g^H": start Select block mode.
7882 */
7883 case K_BS:
7884 cap->nchar = Ctrl_H;
7885 /* FALLTHROUGH */
7886 case 'h':
7887 case 'H':
7888 case Ctrl_H:
7889# ifdef EBCDIC
7890 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7891 if (cap->nchar == Ctrl_H)
7892 cap->cmdchar = Ctrl_V;
7893 else
7894# endif
7895 cap->cmdchar = cap->nchar + ('v' - 'h');
7896 cap->arg = TRUE;
7897 nv_visual(cap);
7898 break;
7899#endif /* FEAT_VISUAL */
7900
7901 /*
7902 * "gj" and "gk" two new funny movement keys -- up and down
7903 * movement based on *screen* line rather than *file* line.
7904 */
7905 case 'j':
7906 case K_DOWN:
7907 /* with 'nowrap' it works just like the normal "j" command; also when
7908 * in a closed fold */
7909 if (!curwin->w_p_wrap
7910#ifdef FEAT_FOLDING
7911 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7912#endif
7913 )
7914 {
7915 oap->motion_type = MLINE;
7916 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7917 }
7918 else
7919 i = nv_screengo(oap, FORWARD, cap->count1);
7920 if (i == FAIL)
7921 clearopbeep(oap);
7922 break;
7923
7924 case 'k':
7925 case K_UP:
7926 /* with 'nowrap' it works just like the normal "k" command; also when
7927 * in a closed fold */
7928 if (!curwin->w_p_wrap
7929#ifdef FEAT_FOLDING
7930 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7931#endif
7932 )
7933 {
7934 oap->motion_type = MLINE;
7935 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7936 }
7937 else
7938 i = nv_screengo(oap, BACKWARD, cap->count1);
7939 if (i == FAIL)
7940 clearopbeep(oap);
7941 break;
7942
7943 /*
7944 * "gJ": join two lines without inserting a space.
7945 */
7946 case 'J':
7947 nv_join(cap);
7948 break;
7949
7950 /*
7951 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7952 * "gm": middle of "g0" and "g$".
7953 */
7954 case '^':
7955 flag = TRUE;
7956 /* FALLTHROUGH */
7957
7958 case '0':
7959 case 'm':
7960 case K_HOME:
7961 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 oap->motion_type = MCHAR;
7963 oap->inclusive = FALSE;
7964 if (curwin->w_p_wrap
7965#ifdef FEAT_VERTSPLIT
7966 && curwin->w_width != 0
7967#endif
7968 )
7969 {
7970 int width1 = W_WIDTH(curwin) - curwin_col_off();
7971 int width2 = width1 + curwin_col_off2();
7972
7973 validate_virtcol();
7974 i = 0;
7975 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
7976 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
7977 }
7978 else
7979 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02007980 /* Go to the middle of the screen line. When 'number' or
7981 * 'relativenumber' is on and lines are wrapping the middle can be more
7982 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 if (cap->nchar == 'm')
7984 i += (W_WIDTH(curwin) - curwin_col_off()
7985 + ((curwin->w_p_wrap && i > 0)
7986 ? curwin_col_off2() : 0)) / 2;
7987 coladvance((colnr_T)i);
7988 if (flag)
7989 {
7990 do
7991 i = gchar_cursor();
7992 while (vim_iswhite(i) && oneright() == OK);
7993 }
7994 curwin->w_set_curswant = TRUE;
7995 break;
7996
7997 case '_':
7998 /* "g_": to the last non-blank character in the line or <count> lines
7999 * downward. */
8000 cap->oap->motion_type = MCHAR;
8001 cap->oap->inclusive = TRUE;
8002 curwin->w_curswant = MAXCOL;
8003 if (cursor_down((long)(cap->count1 - 1),
8004 cap->oap->op_type == OP_NOP) == FAIL)
8005 clearopbeep(cap->oap);
8006 else
8007 {
8008 char_u *ptr = ml_get_curline();
8009
8010 /* In Visual mode we may end up after the line. */
8011 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8012 --curwin->w_cursor.col;
8013
8014 /* Decrease the cursor column until it's on a non-blank. */
8015 while (curwin->w_cursor.col > 0
8016 && vim_iswhite(ptr[curwin->w_cursor.col]))
8017 --curwin->w_cursor.col;
8018 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008019#ifdef FEAT_VISUAL
8020 adjust_for_sel(cap);
8021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 }
8023 break;
8024
8025 case '$':
8026 case K_END:
8027 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 {
8029 int col_off = curwin_col_off();
8030
8031 oap->motion_type = MCHAR;
8032 oap->inclusive = TRUE;
8033 if (curwin->w_p_wrap
8034#ifdef FEAT_VERTSPLIT
8035 && curwin->w_width != 0
8036#endif
8037 )
8038 {
8039 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8040 if (cap->count1 == 1)
8041 {
8042 int width1 = W_WIDTH(curwin) - col_off;
8043 int width2 = width1 + curwin_col_off2();
8044
8045 validate_virtcol();
8046 i = width1 - 1;
8047 if (curwin->w_virtcol >= (colnr_T)width1)
8048 i += ((curwin->w_virtcol - width1) / width2 + 1)
8049 * width2;
8050 coladvance((colnr_T)i);
8051#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8052 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8053 {
8054 /*
8055 * Check for landing on a character that got split at
8056 * the end of the line. We do not want to advance to
8057 * the next screen line.
8058 */
8059 validate_virtcol();
8060 if (curwin->w_virtcol > (colnr_T)i)
8061 --curwin->w_cursor.col;
8062 }
8063#endif
8064 }
8065 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8066 clearopbeep(oap);
8067 }
8068 else
8069 {
8070 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8071 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008072
8073 /* Make sure we stick in this column. */
8074 validate_virtcol();
8075 curwin->w_curswant = curwin->w_virtcol;
8076 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 }
8078 }
8079 break;
8080
8081 /*
8082 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8083 */
8084 case '*':
8085 case '#':
8086#if POUND != '#'
8087 case POUND: /* pound sign (sometimes equal to '#') */
8088#endif
8089 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8090 case ']': /* :tselect for current identifier */
8091 nv_ident(cap);
8092 break;
8093
8094 /*
8095 * ge and gE: go back to end of word
8096 */
8097 case 'e':
8098 case 'E':
8099 oap->motion_type = MCHAR;
8100 curwin->w_set_curswant = TRUE;
8101 oap->inclusive = TRUE;
8102 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8103 clearopbeep(oap);
8104 break;
8105
8106 /*
8107 * "g CTRL-G": display info about cursor position
8108 */
8109 case Ctrl_G:
8110 cursor_pos_info();
8111 break;
8112
8113 /*
8114 * "gi": start Insert at the last position.
8115 */
8116 case 'i':
8117 if (curbuf->b_last_insert.lnum != 0)
8118 {
8119 curwin->w_cursor = curbuf->b_last_insert;
8120 check_cursor_lnum();
8121 i = (int)STRLEN(ml_get_curline());
8122 if (curwin->w_cursor.col > (colnr_T)i)
8123 {
8124#ifdef FEAT_VIRTUALEDIT
8125 if (virtual_active())
8126 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8127#endif
8128 curwin->w_cursor.col = i;
8129 }
8130 }
8131 cap->cmdchar = 'i';
8132 nv_edit(cap);
8133 break;
8134
8135 /*
8136 * "gI": Start insert in column 1.
8137 */
8138 case 'I':
8139 beginline(0);
8140 if (!checkclearopq(oap))
8141 invoke_edit(cap, FALSE, 'g', FALSE);
8142 break;
8143
8144#ifdef FEAT_SEARCHPATH
8145 /*
8146 * "gf": goto file, edit file under cursor
8147 * "]f" and "[f": can also be used.
8148 */
8149 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008150 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 nv_gotofile(cap);
8152 break;
8153#endif
8154
8155 /* "g'm" and "g`m": jump to mark without setting pcmark */
8156 case '\'':
8157 cap->arg = TRUE;
8158 /*FALLTHROUGH*/
8159 case '`':
8160 nv_gomark(cap);
8161 break;
8162
8163 /*
8164 * "gs": Goto sleep.
8165 */
8166 case 's':
8167 do_sleep(cap->count1 * 1000L);
8168 break;
8169
8170 /*
8171 * "ga": Display the ascii value of the character under the
8172 * cursor. It is displayed in decimal, hex, and octal. -- webb
8173 */
8174 case 'a':
8175 do_ascii(NULL);
8176 break;
8177
8178#ifdef FEAT_MBYTE
8179 /*
8180 * "g8": Display the bytes used for the UTF-8 character under the
8181 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008182 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 */
8184 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008185 if (cap->count0 == 8)
8186 utf_find_illegal();
8187 else
8188 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 break;
8190#endif
8191
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008192 case '<':
8193 show_sb_text();
8194 break;
8195
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 /*
8197 * "gg": Goto the first line in file. With a count it goes to
8198 * that line number like for "G". -- webb
8199 */
8200 case 'g':
8201 cap->arg = FALSE;
8202 nv_goto(cap);
8203 break;
8204
8205 /*
8206 * Two-character operators:
8207 * "gq" Format text
8208 * "gw" Format text and keep cursor position
8209 * "g~" Toggle the case of the text.
8210 * "gu" Change text to lower case.
8211 * "gU" Change text to upper case.
8212 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008213 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 */
8215 case 'q':
8216 case 'w':
8217 oap->cursor_start = curwin->w_cursor;
8218 /*FALLTHROUGH*/
8219 case '~':
8220 case 'u':
8221 case 'U':
8222 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008223 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 nv_operator(cap);
8225 break;
8226
8227 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008228 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 * current function
8230 * "gD": idem, but in the current file.
8231 */
8232 case 'd':
8233 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008234 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 break;
8236
8237#ifdef FEAT_MOUSE
8238 /*
8239 * g<*Mouse> : <C-*mouse>
8240 */
8241 case K_MIDDLEMOUSE:
8242 case K_MIDDLEDRAG:
8243 case K_MIDDLERELEASE:
8244 case K_LEFTMOUSE:
8245 case K_LEFTDRAG:
8246 case K_LEFTRELEASE:
8247 case K_RIGHTMOUSE:
8248 case K_RIGHTDRAG:
8249 case K_RIGHTRELEASE:
8250 case K_X1MOUSE:
8251 case K_X1DRAG:
8252 case K_X1RELEASE:
8253 case K_X2MOUSE:
8254 case K_X2DRAG:
8255 case K_X2RELEASE:
8256 mod_mask = MOD_MASK_CTRL;
8257 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8258 break;
8259#endif
8260
8261 case K_IGNORE:
8262 break;
8263
8264 /*
8265 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8266 */
8267 case 'p':
8268 case 'P':
8269 nv_put(cap);
8270 break;
8271
8272#ifdef FEAT_BYTEOFF
8273 /* "go": goto byte count from start of buffer */
8274 case 'o':
8275 goto_byte(cap->count0);
8276 break;
8277#endif
8278
8279 /* "gQ": improved Ex mode */
8280 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008281 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 {
8283 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008284 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 break;
8286 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008287
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 if (!checkclearopq(oap))
8289 do_exmode(TRUE);
8290 break;
8291
8292#ifdef FEAT_JUMPLIST
8293 case ',':
8294 nv_pcmark(cap);
8295 break;
8296
8297 case ';':
8298 cap->count1 = -cap->count1;
8299 nv_pcmark(cap);
8300 break;
8301#endif
8302
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008303#ifdef FEAT_WINDOWS
8304 case 't':
8305 goto_tabpage((int)cap->count0);
8306 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008307 case 'T':
8308 goto_tabpage(-(int)cap->count1);
8309 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008310#endif
8311
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008312 case '+':
8313 case '-': /* "g+" and "g-": undo or redo along the timeline */
8314 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008315 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008316 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008317 break;
8318
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 default:
8320 clearopbeep(oap);
8321 break;
8322 }
8323}
8324
8325/*
8326 * Handle "o" and "O" commands.
8327 */
8328 static void
8329n_opencmd(cap)
8330 cmdarg_T *cap;
8331{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008332#ifdef FEAT_CONCEAL
8333 linenr_T oldline = curwin->w_cursor.lnum;
8334#endif
8335
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 if (!checkclearopq(cap->oap))
8337 {
8338#ifdef FEAT_FOLDING
8339 if (cap->cmdchar == 'O')
8340 /* Open above the first line of a folded sequence of lines */
8341 (void)hasFolding(curwin->w_cursor.lnum,
8342 &curwin->w_cursor.lnum, NULL);
8343 else
8344 /* Open below the last line of a folded sequence of lines */
8345 (void)hasFolding(curwin->w_cursor.lnum,
8346 NULL, &curwin->w_cursor.lnum);
8347#endif
8348 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8349 (cap->cmdchar == 'O' ? 1 : 0)),
8350 (linenr_T)(curwin->w_cursor.lnum +
8351 (cap->cmdchar == 'o' ? 1 : 0))
8352 ) == OK
8353 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8354#ifdef FEAT_COMMENTS
8355 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8356#endif
8357 0, 0))
8358 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008359#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008360 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008361 update_single_line(curwin, oldline);
8362#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008363 /* When '#' is in 'cpoptions' ignore the count. */
8364 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8365 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8367 }
8368 }
8369}
8370
8371/*
8372 * "." command: redo last change.
8373 */
8374 static void
8375nv_dot(cap)
8376 cmdarg_T *cap;
8377{
8378 if (!checkclearopq(cap->oap))
8379 {
8380 /*
8381 * If "restart_edit" is TRUE, the last but one command is repeated
8382 * instead of the last command (inserting text). This is used for
8383 * CTRL-O <.> in insert mode.
8384 */
8385 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8386 clearopbeep(cap->oap);
8387 }
8388}
8389
8390/*
8391 * CTRL-R: undo undo
8392 */
8393 static void
8394nv_redo(cap)
8395 cmdarg_T *cap;
8396{
8397 if (!checkclearopq(cap->oap))
8398 {
8399 u_redo((int)cap->count1);
8400 curwin->w_set_curswant = TRUE;
8401 }
8402}
8403
8404/*
8405 * Handle "U" command.
8406 */
8407 static void
8408nv_Undo(cap)
8409 cmdarg_T *cap;
8410{
8411 /* In Visual mode and typing "gUU" triggers an operator */
8412 if (cap->oap->op_type == OP_UPPER
8413#ifdef FEAT_VISUAL
8414 || VIsual_active
8415#endif
8416 )
8417 {
8418 /* translate "gUU" to "gUgU" */
8419 cap->cmdchar = 'g';
8420 cap->nchar = 'U';
8421 nv_operator(cap);
8422 }
8423 else if (!checkclearopq(cap->oap))
8424 {
8425 u_undoline();
8426 curwin->w_set_curswant = TRUE;
8427 }
8428}
8429
8430/*
8431 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8432 * single character.
8433 */
8434 static void
8435nv_tilde(cap)
8436 cmdarg_T *cap;
8437{
8438 if (!p_to
8439#ifdef FEAT_VISUAL
8440 && !VIsual_active
8441#endif
8442 && cap->oap->op_type != OP_TILDE)
8443 n_swapchar(cap);
8444 else
8445 nv_operator(cap);
8446}
8447
8448/*
8449 * Handle an operator command.
8450 * The actual work is done by do_pending_operator().
8451 */
8452 static void
8453nv_operator(cap)
8454 cmdarg_T *cap;
8455{
8456 int op_type;
8457
8458 op_type = get_op_type(cap->cmdchar, cap->nchar);
8459
8460 if (op_type == cap->oap->op_type) /* double operator works on lines */
8461 nv_lineop(cap);
8462 else if (!checkclearop(cap->oap))
8463 {
8464 cap->oap->start = curwin->w_cursor;
8465 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008466#ifdef FEAT_EVAL
8467 set_op_var(op_type);
8468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 }
8470}
8471
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008472#ifdef FEAT_EVAL
8473/*
8474 * Set v:operator to the characters for "optype".
8475 */
8476 static void
8477set_op_var(optype)
8478 int optype;
8479{
8480 char_u opchars[3];
8481
8482 if (optype == OP_NOP)
8483 set_vim_var_string(VV_OP, NULL, 0);
8484 else
8485 {
8486 opchars[0] = get_op_char(optype);
8487 opchars[1] = get_extra_op_char(optype);
8488 opchars[2] = NUL;
8489 set_vim_var_string(VV_OP, opchars, -1);
8490 }
8491}
8492#endif
8493
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494/*
8495 * Handle linewise operator "dd", "yy", etc.
8496 *
8497 * "_" is is a strange motion command that helps make operators more logical.
8498 * It is actually implemented, but not documented in the real Vi. This motion
8499 * command actually refers to "the current line". Commands like "dd" and "yy"
8500 * are really an alternate form of "d_" and "y_". It does accept a count, so
8501 * "d3_" works to delete 3 lines.
8502 */
8503 static void
8504nv_lineop(cap)
8505 cmdarg_T *cap;
8506{
8507 cap->oap->motion_type = MLINE;
8508 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8509 clearopbeep(cap->oap);
8510 else if ( cap->oap->op_type == OP_DELETE
8511 || cap->oap->op_type == OP_LSHIFT
8512 || cap->oap->op_type == OP_RSHIFT)
8513 beginline(BL_SOL | BL_FIX);
8514 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8515 beginline(BL_WHITE | BL_FIX);
8516}
8517
8518/*
8519 * <Home> command.
8520 */
8521 static void
8522nv_home(cap)
8523 cmdarg_T *cap;
8524{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008525 /* CTRL-HOME is like "gg" */
8526 if (mod_mask & MOD_MASK_CTRL)
8527 nv_goto(cap);
8528 else
8529 {
8530 cap->count0 = 1;
8531 nv_pipe(cap);
8532 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008533 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8534 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535}
8536
8537/*
8538 * "|" command.
8539 */
8540 static void
8541nv_pipe(cap)
8542 cmdarg_T *cap;
8543{
8544 cap->oap->motion_type = MCHAR;
8545 cap->oap->inclusive = FALSE;
8546 beginline(0);
8547 if (cap->count0 > 0)
8548 {
8549 coladvance((colnr_T)(cap->count0 - 1));
8550 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8551 }
8552 else
8553 curwin->w_curswant = 0;
8554 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008555 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 curwin->w_set_curswant = FALSE;
8557}
8558
8559/*
8560 * Handle back-word command "b" and "B".
8561 * cap->arg is 1 for "B"
8562 */
8563 static void
8564nv_bck_word(cap)
8565 cmdarg_T *cap;
8566{
8567 cap->oap->motion_type = MCHAR;
8568 cap->oap->inclusive = FALSE;
8569 curwin->w_set_curswant = TRUE;
8570 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8571 clearopbeep(cap->oap);
8572#ifdef FEAT_FOLDING
8573 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8574 foldOpenCursor();
8575#endif
8576}
8577
8578/*
8579 * Handle word motion commands "e", "E", "w" and "W".
8580 * cap->arg is TRUE for "E" and "W".
8581 */
8582 static void
8583nv_wordcmd(cap)
8584 cmdarg_T *cap;
8585{
8586 int n;
8587 int word_end;
8588 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008589 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590
8591 /*
8592 * Set inclusive for the "E" and "e" command.
8593 */
8594 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8595 word_end = TRUE;
8596 else
8597 word_end = FALSE;
8598 cap->oap->inclusive = word_end;
8599
8600 /*
8601 * "cw" and "cW" are a special case.
8602 */
8603 if (!word_end && cap->oap->op_type == OP_CHANGE)
8604 {
8605 n = gchar_cursor();
8606 if (n != NUL) /* not an empty line */
8607 {
8608 if (vim_iswhite(n))
8609 {
8610 /*
8611 * Reproduce a funny Vi behaviour: "cw" on a blank only
8612 * changes one character, not all blanks until the start of
8613 * the next word. Only do this when the 'w' flag is included
8614 * in 'cpoptions'.
8615 */
8616 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8617 {
8618 cap->oap->inclusive = TRUE;
8619 cap->oap->motion_type = MCHAR;
8620 return;
8621 }
8622 }
8623 else
8624 {
8625 /*
8626 * This is a little strange. To match what the real Vi does,
8627 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8628 * that we are not on a space or a TAB. This seems impolite
8629 * at first, but it's really more what we mean when we say
8630 * 'cw'.
8631 * Another strangeness: When standing on the end of a word
8632 * "ce" will change until the end of the next wordt, but "cw"
8633 * will change only one character! This is done by setting
8634 * flag.
8635 */
8636 cap->oap->inclusive = TRUE;
8637 word_end = TRUE;
8638 flag = TRUE;
8639 }
8640 }
8641 }
8642
8643 cap->oap->motion_type = MCHAR;
8644 curwin->w_set_curswant = TRUE;
8645 if (word_end)
8646 n = end_word(cap->count1, cap->arg, flag, FALSE);
8647 else
8648 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8649
Bram Moolenaardfefb982008-04-01 10:06:39 +00008650 /* Don't leave the cursor on the NUL past the end of line. Unless we
8651 * didn't move it forward. */
8652 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008653 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654
8655 if (n == FAIL && cap->oap->op_type == OP_NOP)
8656 clearopbeep(cap->oap);
8657 else
8658 {
8659#ifdef FEAT_VISUAL
8660 adjust_for_sel(cap);
8661#endif
8662#ifdef FEAT_FOLDING
8663 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8664 foldOpenCursor();
8665#endif
8666 }
8667}
8668
8669/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008670 * Used after a movement command: If the cursor ends up on the NUL after the
8671 * end of the line, may move it back to the last character and make the motion
8672 * inclusive.
8673 */
8674 static void
8675adjust_cursor(oap)
8676 oparg_T *oap;
8677{
8678 /* The cursor cannot remain on the NUL when:
8679 * - the column is > 0
8680 * - not in Visual mode or 'selection' is "o"
8681 * - 'virtualedit' is not "all" and not "onemore".
8682 */
8683 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
8684#ifdef FEAT_VISUAL
8685 && (!VIsual_active || *p_sel == 'o')
8686#endif
8687#ifdef FEAT_VIRTUALEDIT
8688 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8689#endif
8690 )
8691 {
8692 --curwin->w_cursor.col;
8693#ifdef FEAT_MBYTE
8694 /* prevent cursor from moving on the trail byte */
8695 if (has_mbyte)
8696 mb_adjust_cursor();
8697#endif
8698 oap->inclusive = TRUE;
8699 }
8700}
8701
8702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 * "0" and "^" commands.
8704 * cap->arg is the argument for beginline().
8705 */
8706 static void
8707nv_beginline(cap)
8708 cmdarg_T *cap;
8709{
8710 cap->oap->motion_type = MCHAR;
8711 cap->oap->inclusive = FALSE;
8712 beginline(cap->arg);
8713#ifdef FEAT_FOLDING
8714 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8715 foldOpenCursor();
8716#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008717 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8718 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719}
8720
8721#ifdef FEAT_VISUAL
8722/*
8723 * In exclusive Visual mode, may include the last character.
8724 */
8725 static void
8726adjust_for_sel(cap)
8727 cmdarg_T *cap;
8728{
8729 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8730 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8731 {
8732# ifdef FEAT_MBYTE
8733 if (has_mbyte)
8734 inc_cursor();
8735 else
8736# endif
8737 ++curwin->w_cursor.col;
8738 cap->oap->inclusive = FALSE;
8739 }
8740}
8741
8742/*
8743 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8744 * Should check VIsual_mode before calling this.
8745 * Returns TRUE when backed up to the previous line.
8746 */
8747 static int
8748unadjust_for_sel()
8749{
8750 pos_T *pp;
8751
8752 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8753 {
8754 if (lt(VIsual, curwin->w_cursor))
8755 pp = &curwin->w_cursor;
8756 else
8757 pp = &VIsual;
8758#ifdef FEAT_VIRTUALEDIT
8759 if (pp->coladd > 0)
8760 --pp->coladd;
8761 else
8762#endif
8763 if (pp->col > 0)
8764 {
8765 --pp->col;
8766#ifdef FEAT_MBYTE
8767 mb_adjustpos(pp);
8768#endif
8769 }
8770 else if (pp->lnum > 1)
8771 {
8772 --pp->lnum;
8773 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8774 return TRUE;
8775 }
8776 }
8777 return FALSE;
8778}
8779
8780/*
8781 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8782 */
8783 static void
8784nv_select(cap)
8785 cmdarg_T *cap;
8786{
8787 if (VIsual_active)
8788 VIsual_select = TRUE;
8789 else if (VIsual_reselect)
8790 {
8791 cap->nchar = 'v'; /* fake "gv" command */
8792 cap->arg = TRUE;
8793 nv_g_cmd(cap);
8794 }
8795}
8796
8797#endif
8798
8799/*
8800 * "G", "gg", CTRL-END, CTRL-HOME.
8801 * cap->arg is TRUE for "G".
8802 */
8803 static void
8804nv_goto(cap)
8805 cmdarg_T *cap;
8806{
8807 linenr_T lnum;
8808
8809 if (cap->arg)
8810 lnum = curbuf->b_ml.ml_line_count;
8811 else
8812 lnum = 1L;
8813 cap->oap->motion_type = MLINE;
8814 setpcmark();
8815
8816 /* When a count is given, use it instead of the default lnum */
8817 if (cap->count0 != 0)
8818 lnum = cap->count0;
8819 if (lnum < 1L)
8820 lnum = 1L;
8821 else if (lnum > curbuf->b_ml.ml_line_count)
8822 lnum = curbuf->b_ml.ml_line_count;
8823 curwin->w_cursor.lnum = lnum;
8824 beginline(BL_SOL | BL_FIX);
8825#ifdef FEAT_FOLDING
8826 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8827 foldOpenCursor();
8828#endif
8829}
8830
8831/*
8832 * CTRL-\ in Normal mode.
8833 */
8834 static void
8835nv_normal(cap)
8836 cmdarg_T *cap;
8837{
8838 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8839 {
8840 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008841 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 clear_cmdline = TRUE; /* unshow mode later */
8843 restart_edit = 0;
8844#ifdef FEAT_CMDWIN
8845 if (cmdwin_type != 0)
8846 cmdwin_result = Ctrl_C;
8847#endif
8848#ifdef FEAT_VISUAL
8849 if (VIsual_active)
8850 {
8851 end_visual_mode(); /* stop Visual */
8852 redraw_curbuf_later(INVERTED);
8853 }
8854#endif
8855 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8856 if (cap->nchar == Ctrl_G && p_im)
8857 restart_edit = 'a';
8858 }
8859 else
8860 clearopbeep(cap->oap);
8861}
8862
8863/*
8864 * ESC in Normal mode: beep, but don't flush buffers.
8865 * Don't even beep if we are canceling a command.
8866 */
8867 static void
8868nv_esc(cap)
8869 cmdarg_T *cap;
8870{
8871 int no_reason;
8872
8873 no_reason = (cap->oap->op_type == OP_NOP
8874 && cap->opcount == 0
8875 && cap->count0 == 0
8876 && cap->oap->regname == 0
8877 && !p_im);
8878
8879 if (cap->arg) /* TRUE for CTRL-C */
8880 {
8881 if (restart_edit == 0
8882#ifdef FEAT_CMDWIN
8883 && cmdwin_type == 0
8884#endif
8885#ifdef FEAT_VISUAL
8886 && !VIsual_active
8887#endif
8888 && no_reason)
8889 MSG(_("Type :quit<Enter> to exit Vim"));
8890
8891 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8892 * set again below when halfway a mapping. */
8893 if (!p_im)
8894 restart_edit = 0;
8895#ifdef FEAT_CMDWIN
8896 if (cmdwin_type != 0)
8897 {
8898 cmdwin_result = K_IGNORE;
8899 got_int = FALSE; /* don't stop executing autocommands et al. */
8900 return;
8901 }
8902#endif
8903 }
8904
8905#ifdef FEAT_VISUAL
8906 if (VIsual_active)
8907 {
8908 end_visual_mode(); /* stop Visual */
8909 check_cursor_col(); /* make sure cursor is not beyond EOL */
8910 curwin->w_set_curswant = TRUE;
8911 redraw_curbuf_later(INVERTED);
8912 }
8913 else
8914#endif
8915 if (no_reason)
8916 vim_beep();
8917 clearop(cap->oap);
8918
8919 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8920 * set return to Insert mode afterwards. */
8921 if (restart_edit == 0 && goto_im()
8922#ifdef FEAT_EX_EXTRA
8923 && ex_normal_busy == 0
8924#endif
8925 )
8926 restart_edit = 'a';
8927}
8928
8929/*
8930 * Handle "A", "a", "I", "i" and <Insert> commands.
8931 */
8932 static void
8933nv_edit(cap)
8934 cmdarg_T *cap;
8935{
8936 /* <Insert> is equal to "i" */
8937 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8938 cap->cmdchar = 'i';
8939
8940#ifdef FEAT_VISUAL
8941 /* in Visual mode "A" and "I" are an operator */
8942 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8943 v_visop(cap);
8944
8945 /* in Visual mode and after an operator "a" and "i" are for text objects */
8946 else
8947#endif
8948 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8949 && (cap->oap->op_type != OP_NOP
8950#ifdef FEAT_VISUAL
8951 || VIsual_active
8952#endif
8953 ))
8954 {
8955#ifdef FEAT_TEXTOBJ
8956 nv_object(cap);
8957#else
8958 clearopbeep(cap->oap);
8959#endif
8960 }
8961 else if (!curbuf->b_p_ma && !p_im)
8962 {
8963 /* Only give this error when 'insertmode' is off. */
8964 EMSG(_(e_modifiable));
8965 clearop(cap->oap);
8966 }
8967 else if (!checkclearopq(cap->oap))
8968 {
8969 switch (cap->cmdchar)
8970 {
8971 case 'A': /* "A"ppend after the line */
8972 curwin->w_set_curswant = TRUE;
8973#ifdef FEAT_VIRTUALEDIT
8974 if (ve_flags == VE_ALL)
8975 {
8976 int save_State = State;
8977
8978 /* Pretent Insert mode here to allow the cursor on the
8979 * character past the end of the line */
8980 State = INSERT;
8981 coladvance((colnr_T)MAXCOL);
8982 State = save_State;
8983 }
8984 else
8985#endif
8986 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8987 break;
8988
8989 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008990 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
8991 beginline(BL_WHITE);
8992 else
8993 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 break;
8995
8996 case 'a': /* "a"ppend is like "i"nsert on the next character. */
8997#ifdef FEAT_VIRTUALEDIT
8998 /* increment coladd when in virtual space, increment the
8999 * column otherwise, also to append after an unprintable char */
9000 if (virtual_active()
9001 && (curwin->w_cursor.coladd > 0
9002 || *ml_get_cursor() == NUL
9003 || *ml_get_cursor() == TAB))
9004 curwin->w_cursor.coladd++;
9005 else
9006#endif
9007 if (*ml_get_cursor() != NUL)
9008 inc_cursor();
9009 break;
9010 }
9011
9012#ifdef FEAT_VIRTUALEDIT
9013 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9014 {
9015 int save_State = State;
9016
9017 /* Pretent Insert mode here to allow the cursor on the
9018 * character past the end of the line */
9019 State = INSERT;
9020 coladvance(getviscol());
9021 State = save_State;
9022 }
9023#endif
9024
9025 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9026 }
9027}
9028
9029/*
9030 * Invoke edit() and take care of "restart_edit" and the return value.
9031 */
9032 static void
9033invoke_edit(cap, repl, cmd, startln)
9034 cmdarg_T *cap;
9035 int repl; /* "r" or "gr" command */
9036 int cmd;
9037 int startln;
9038{
9039 int restart_edit_save = 0;
9040
9041 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9042 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9043 * it. */
9044 if (repl || !stuff_empty())
9045 restart_edit_save = restart_edit;
9046 else
9047 restart_edit_save = 0;
9048
9049 /* Always reset "restart_edit", this is not a restarted edit. */
9050 restart_edit = 0;
9051
9052 if (edit(cmd, startln, cap->count1))
9053 cap->retval |= CA_COMMAND_BUSY;
9054
9055 if (restart_edit == 0)
9056 restart_edit = restart_edit_save;
9057}
9058
9059#ifdef FEAT_TEXTOBJ
9060/*
9061 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9062 */
9063 static void
9064nv_object(cap)
9065 cmdarg_T *cap;
9066{
9067 int flag;
9068 int include;
9069 char_u *mps_save;
9070
9071 if (cap->cmdchar == 'i')
9072 include = FALSE; /* "ix" = inner object: exclude white space */
9073 else
9074 include = TRUE; /* "ax" = an object: include white space */
9075
9076 /* Make sure (), [], {} and <> are in 'matchpairs' */
9077 mps_save = curbuf->b_p_mps;
9078 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9079
9080 switch (cap->nchar)
9081 {
9082 case 'w': /* "aw" = a word */
9083 flag = current_word(cap->oap, cap->count1, include, FALSE);
9084 break;
9085 case 'W': /* "aW" = a WORD */
9086 flag = current_word(cap->oap, cap->count1, include, TRUE);
9087 break;
9088 case 'b': /* "ab" = a braces block */
9089 case '(':
9090 case ')':
9091 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9092 break;
9093 case 'B': /* "aB" = a Brackets block */
9094 case '{':
9095 case '}':
9096 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9097 break;
9098 case '[': /* "a[" = a [] block */
9099 case ']':
9100 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9101 break;
9102 case '<': /* "a<" = a <> block */
9103 case '>':
9104 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9105 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009106 case 't': /* "at" = a tag block (xml and html) */
9107 flag = current_tagblock(cap->oap, cap->count1, include);
9108 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 case 'p': /* "ap" = a paragraph */
9110 flag = current_par(cap->oap, cap->count1, include, 'p');
9111 break;
9112 case 's': /* "as" = a sentence */
9113 flag = current_sent(cap->oap, cap->count1, include);
9114 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009115 case '"': /* "a"" = a double quoted string */
9116 case '\'': /* "a'" = a single quoted string */
9117 case '`': /* "a`" = a backtick quoted string */
9118 flag = current_quote(cap->oap, cap->count1, include,
9119 cap->nchar);
9120 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121#if 0 /* TODO */
9122 case 'S': /* "aS" = a section */
9123 case 'f': /* "af" = a filename */
9124 case 'u': /* "au" = a URL */
9125#endif
9126 default:
9127 flag = FAIL;
9128 break;
9129 }
9130
9131 curbuf->b_p_mps = mps_save;
9132 if (flag == FAIL)
9133 clearopbeep(cap->oap);
9134 adjust_cursor_col();
9135 curwin->w_set_curswant = TRUE;
9136}
9137#endif
9138
9139/*
9140 * "q" command: Start/stop recording.
9141 * "q:", "q/", "q?": edit command-line in command-line window.
9142 */
9143 static void
9144nv_record(cap)
9145 cmdarg_T *cap;
9146{
9147 if (cap->oap->op_type == OP_FORMAT)
9148 {
9149 /* "gqq" is the same as "gqgq": format line */
9150 cap->cmdchar = 'g';
9151 cap->nchar = 'q';
9152 nv_operator(cap);
9153 }
9154 else if (!checkclearop(cap->oap))
9155 {
9156#ifdef FEAT_CMDWIN
9157 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9158 {
9159 stuffcharReadbuff(cap->nchar);
9160 stuffcharReadbuff(K_CMDWIN);
9161 }
9162 else
9163#endif
9164 /* (stop) recording into a named register, unless executing a
9165 * register */
9166 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9167 clearopbeep(cap->oap);
9168 }
9169}
9170
9171/*
9172 * Handle the "@r" command.
9173 */
9174 static void
9175nv_at(cap)
9176 cmdarg_T *cap;
9177{
9178 if (checkclearop(cap->oap))
9179 return;
9180#ifdef FEAT_EVAL
9181 if (cap->nchar == '=')
9182 {
9183 if (get_expr_register() == NUL)
9184 return;
9185 }
9186#endif
9187 while (cap->count1-- && !got_int)
9188 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009189 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 {
9191 clearopbeep(cap->oap);
9192 break;
9193 }
9194 line_breakcheck();
9195 }
9196}
9197
9198/*
9199 * Handle the CTRL-U and CTRL-D commands.
9200 */
9201 static void
9202nv_halfpage(cap)
9203 cmdarg_T *cap;
9204{
9205 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9206 || (cap->cmdchar == Ctrl_D
9207 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9208 clearopbeep(cap->oap);
9209 else if (!checkclearop(cap->oap))
9210 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9211}
9212
9213/*
9214 * Handle "J" or "gJ" command.
9215 */
9216 static void
9217nv_join(cap)
9218 cmdarg_T *cap;
9219{
9220#ifdef FEAT_VISUAL
9221 if (VIsual_active) /* join the visual lines */
9222 nv_operator(cap);
9223 else
9224#endif
9225 if (!checkclearop(cap->oap))
9226 {
9227 if (cap->count0 <= 1)
9228 cap->count0 = 2; /* default for join is two lines! */
9229 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9230 curbuf->b_ml.ml_line_count)
9231 clearopbeep(cap->oap); /* beyond last line */
9232 else
9233 {
9234 prep_redo(cap->oap->regname, cap->count0,
9235 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02009236 (void)do_join(cap->count0, cap->nchar == NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 }
9238 }
9239}
9240
9241/*
9242 * "P", "gP", "p" and "gp" commands.
9243 */
9244 static void
9245nv_put(cap)
9246 cmdarg_T *cap;
9247{
9248#ifdef FEAT_VISUAL
9249 int regname = 0;
9250 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009251 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009252 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253#endif
9254 int dir;
9255 int flags = 0;
9256
9257 if (cap->oap->op_type != OP_NOP)
9258 {
9259#ifdef FEAT_DIFF
9260 /* "dp" is ":diffput" */
9261 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9262 {
9263 clearop(cap->oap);
9264 nv_diffgetput(TRUE);
9265 }
9266 else
9267#endif
9268 clearopbeep(cap->oap);
9269 }
9270 else
9271 {
9272 dir = (cap->cmdchar == 'P'
9273 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9274 ? BACKWARD : FORWARD;
9275 prep_redo_cmd(cap);
9276 if (cap->cmdchar == 'g')
9277 flags |= PUT_CURSEND;
9278
9279#ifdef FEAT_VISUAL
9280 if (VIsual_active)
9281 {
9282 /* Putting in Visual mode: The put text replaces the selected
9283 * text. First delete the selected text, then put the new text.
9284 * Need to save and restore the registers that the delete
9285 * overwrites if the old contents is being put.
9286 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009287 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 regname = cap->oap->regname;
9289# ifdef FEAT_CLIPBOARD
9290 adjust_clip_reg(&regname);
9291# endif
9292 if (regname == 0 || VIM_ISDIGIT(regname)
9293# ifdef FEAT_CLIPBOARD
9294 || (clip_unnamed && (regname == '*' || regname == '+'))
9295# endif
9296
9297 )
9298 {
9299 /* the delete is going to overwrite the register we want to
9300 * put, save it first. */
9301 reg1 = get_register(regname, TRUE);
9302 }
9303
9304 /* Now delete the selected text. */
9305 cap->cmdchar = 'd';
9306 cap->nchar = NUL;
9307 cap->oap->regname = NUL;
9308 nv_operator(cap);
9309 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009310 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311
9312 /* delete PUT_LINE_BACKWARD; */
9313 cap->oap->regname = regname;
9314
9315 if (reg1 != NULL)
9316 {
9317 /* Delete probably changed the register we want to put, save
9318 * it first. Then put back what was there before the delete. */
9319 reg2 = get_register(regname, FALSE);
9320 put_register(regname, reg1);
9321 }
9322
9323 /* When deleted a linewise Visual area, put the register as
9324 * lines to avoid it joined with the next line. When deletion was
9325 * characterwise, split a line when putting lines. */
9326 if (VIsual_mode == 'V')
9327 flags |= PUT_LINE;
9328 else if (VIsual_mode == 'v')
9329 flags |= PUT_LINE_SPLIT;
9330 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9331 flags |= PUT_LINE_FORWARD;
9332 dir = BACKWARD;
9333 if ((VIsual_mode != 'V'
9334 && curwin->w_cursor.col < curbuf->b_op_start.col)
9335 || (VIsual_mode == 'V'
9336 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9337 /* cursor is at the end of the line or end of file, put
9338 * forward. */
9339 dir = FORWARD;
9340 }
9341#endif
9342 do_put(cap->oap->regname, dir, cap->count1, flags);
9343
9344#ifdef FEAT_VISUAL
9345 /* If a register was saved, put it back now. */
9346 if (reg2 != NULL)
9347 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009348
9349 /* What to reselect with "gv"? Selecting the just put text seems to
9350 * be the most useful, since the original text was removed. */
9351 if (was_visual)
9352 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009353 curbuf->b_visual.vi_start = curbuf->b_op_start;
9354 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009355 }
9356
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009357 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009358 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009359 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009360 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009361 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009362
9363 /* If the cursor was in that line, move it to the end of the last
9364 * line. */
9365 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9366 {
9367 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9368 coladvance((colnr_T)MAXCOL);
9369 }
9370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371#endif
9372 auto_format(FALSE, TRUE);
9373 }
9374}
9375
9376/*
9377 * "o" and "O" commands.
9378 */
9379 static void
9380nv_open(cap)
9381 cmdarg_T *cap;
9382{
9383#ifdef FEAT_DIFF
9384 /* "do" is ":diffget" */
9385 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9386 {
9387 clearop(cap->oap);
9388 nv_diffgetput(FALSE);
9389 }
9390 else
9391#endif
9392#ifdef FEAT_VISUAL
9393 if (VIsual_active) /* switch start and end of visual */
9394 v_swap_corners(cap->cmdchar);
9395 else
9396#endif
9397 n_opencmd(cap);
9398}
9399
9400#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 static void
9402nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009403 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404{
9405 ProcessSniffRequests();
9406}
9407#endif
9408
9409#ifdef FEAT_NETBEANS_INTG
9410 static void
9411nv_nbcmd(cap)
9412 cmdarg_T *cap;
9413{
9414 netbeans_keycommand(cap->nchar);
9415}
9416#endif
9417
9418#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 static void
9420nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009421 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422{
9423 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9424}
9425#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009426
9427#ifdef FEAT_AUTOCMD
9428/*
9429 * Trigger CursorHold event.
9430 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9431 * input buffer. "did_cursorhold" is set to avoid retriggering.
9432 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009433 static void
9434nv_cursorhold(cap)
9435 cmdarg_T *cap;
9436{
9437 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9438 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009439 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009440}
9441#endif