blob: bd6f1f239cb76c99b2359874835143a96c9192e9 [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
Bram Moolenaar536681b2011-05-10 16:12:45 +02001205 {
1206 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001207
Bram Moolenaar536681b2011-05-10 16:12:45 +02001208 /* Adjust the register according to 'clipboard', so that when
1209 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001210# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001211 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001212# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001213 set_reg_var(regname);
1214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215#endif
1216 }
1217
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001218#ifdef FEAT_VISUAL
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001219 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001220 * character or "z333<cr>". */
1221 if (old_mapped_len > 0)
1222 old_mapped_len = typebuf_maplen();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001223#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001224
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 /*
1226 * If an operation is pending, handle it...
1227 */
1228 do_pending_operator(&ca, old_col, FALSE);
1229
1230 /*
1231 * Wait for a moment when a message is displayed that will be overwritten
1232 * by the mode message.
1233 * In Visual mode and with "^O" in Insert mode, a short message will be
1234 * overwritten by the mode message. Wait a bit, until a key is hit.
1235 * In Visual mode, it's more important to keep the Visual area updated
1236 * than keeping a message (e.g. from a /pat search).
1237 * Only do this if the command was typed, not from a mapping.
1238 * Don't wait when emsg_silent is non-zero.
1239 * Also wait a bit after an error message, e.g. for "^O:".
1240 * Don't redraw the screen, it would remove the message.
1241 */
1242 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001243 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 && (restart_edit != 0
1245#ifdef FEAT_VISUAL
1246 || (VIsual_active
1247 && old_pos.lnum == curwin->w_cursor.lnum
1248 && old_pos.col == curwin->w_cursor.col)
1249#endif
1250 )
1251 && (clear_cmdline
1252 || redraw_cmdline)
1253 && (msg_didout || (msg_didany && msg_scroll))
1254 && !msg_nowait
1255 && KeyTyped)
1256 || (restart_edit != 0
1257#ifdef FEAT_VISUAL
1258 && !VIsual_active
1259#endif
1260 && (msg_scroll
1261 || emsg_on_display)))
1262 && oap->regname == 0
1263 && !(ca.retval & CA_COMMAND_BUSY)
1264 && stuff_empty()
1265 && typebuf_typed()
1266 && emsg_silent == 0
1267 && !did_wait_return
1268 && oap->op_type == OP_NOP)
1269 {
1270 int save_State = State;
1271
1272 /* Draw the cursor with the right shape here */
1273 if (restart_edit != 0)
1274 State = INSERT;
1275
1276 /* If need to redraw, and there is a "keep_msg", redraw before the
1277 * delay */
1278 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1279 {
1280 char_u *kmsg;
1281
1282 kmsg = keep_msg;
1283 keep_msg = NULL;
1284 /* showmode() will clear keep_msg, but we want to use it anyway */
1285 update_screen(0);
1286 /* now reset it, otherwise it's put in the history again */
1287 keep_msg = kmsg;
1288 msg_attr(kmsg, keep_msg_attr);
1289 vim_free(kmsg);
1290 }
1291 setcursor();
1292 cursor_on();
1293 out_flush();
1294 if (msg_scroll || emsg_on_display)
1295 ui_delay(1000L, TRUE); /* wait at least one second */
1296 ui_delay(3000L, FALSE); /* wait up to three seconds */
1297 State = save_State;
1298
1299 msg_scroll = FALSE;
1300 emsg_on_display = FALSE;
1301 }
1302
1303 /*
1304 * Finish up after executing a Normal mode command.
1305 */
1306normal_end:
1307
1308 msg_nowait = FALSE;
1309
1310 /* Reset finish_op, in case it was set */
1311#ifdef CURSOR_SHAPE
1312 c = finish_op;
1313#endif
1314 finish_op = FALSE;
1315#ifdef CURSOR_SHAPE
1316 /* Redraw the cursor with another shape, if we were in Operator-pending
1317 * mode or did a replace command. */
1318 if (c || ca.cmdchar == 'r')
1319 {
1320 ui_cursor_shape(); /* may show different cursor shape */
1321# ifdef FEAT_MOUSESHAPE
1322 update_mouseshape(-1);
1323# endif
1324 }
1325#endif
1326
1327#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001328 if (oap->op_type == OP_NOP && oap->regname == 0
1329# ifdef FEAT_AUTOCMD
1330 && ca.cmdchar != K_CURSORHOLD
1331# endif
1332 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 clear_showcmd();
1334#endif
1335
1336 checkpcmark(); /* check if we moved since setting pcmark */
1337 vim_free(ca.searchbuf);
1338
1339#ifdef FEAT_MBYTE
1340 if (has_mbyte)
1341 mb_adjust_cursor();
1342#endif
1343
1344#ifdef FEAT_SCROLLBIND
1345 if (curwin->w_p_scb && toplevel)
1346 {
1347 validate_cursor(); /* may need to update w_leftcol */
1348 do_check_scrollbind(TRUE);
1349 }
1350#endif
1351
Bram Moolenaar860cae12010-06-05 23:22:07 +02001352#ifdef FEAT_CURSORBIND
1353 if (curwin->w_p_crb && toplevel)
1354 {
1355 validate_cursor(); /* may need to update w_leftcol */
1356 do_check_cursorbind();
1357 }
1358#endif
1359
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 /*
1361 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1362 * if still inside a mapping that started in Visual mode).
1363 * May switch from Visual to Select mode after CTRL-O command.
1364 */
1365 if ( oap->op_type == OP_NOP
1366#ifdef FEAT_VISUAL
1367 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1368 || restart_VIsual_select == 1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001369#else
1370 && restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371#endif
1372 && !(ca.retval & CA_COMMAND_BUSY)
1373 && stuff_empty()
1374 && oap->regname == 0)
1375 {
1376#ifdef FEAT_VISUAL
1377 if (restart_VIsual_select == 1)
1378 {
1379 VIsual_select = TRUE;
1380 showmode();
1381 restart_VIsual_select = 0;
1382 }
1383#endif
1384 if (restart_edit != 0
1385#ifdef FEAT_VISUAL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001386 && !VIsual_active && old_mapped_len == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001388 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 (void)edit(restart_edit, FALSE, 1L);
1390 }
1391
1392#ifdef FEAT_VISUAL
1393 if (restart_VIsual_select == 2)
1394 restart_VIsual_select = 1;
1395#endif
1396
1397 /* Save count before an operator for next time. */
1398 opcount = ca.opcount;
1399}
1400
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001401#ifdef FEAT_EVAL
1402/*
1403 * Set v:count and v:count1 according to "cap".
1404 * Set v:prevcount only when "set_prevcount" is TRUE.
1405 */
1406 static void
1407set_vcount_ca(cap, set_prevcount)
1408 cmdarg_T *cap;
1409 int *set_prevcount;
1410{
1411 long count = cap->count0;
1412
1413 /* multiply with cap->opcount the same way as above */
1414 if (cap->opcount != 0)
1415 count = cap->opcount * (count == 0 ? 1 : count);
1416 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1417 *set_prevcount = FALSE; /* only set v:prevcount once */
1418}
1419#endif
1420
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421/*
1422 * Handle an operator after visual mode or when the movement is finished
1423 */
1424 void
1425do_pending_operator(cap, old_col, gui_yank)
1426 cmdarg_T *cap;
1427 int old_col;
1428 int gui_yank;
1429{
1430 oparg_T *oap = cap->oap;
1431 pos_T old_cursor;
1432 int empty_region_error;
1433 int restart_edit_save;
1434
1435#ifdef FEAT_VISUAL
1436 /* The visual area is remembered for redo */
1437 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1438 static linenr_T redo_VIsual_line_count; /* number of lines */
1439 static colnr_T redo_VIsual_col; /* number of cols or end column */
1440 static long redo_VIsual_count; /* count for Visual operator */
1441# ifdef FEAT_VIRTUALEDIT
1442 int include_line_break = FALSE;
1443# endif
1444#endif
1445
1446#if defined(FEAT_CLIPBOARD)
1447 /*
1448 * Yank the visual area into the GUI selection register before we operate
1449 * on it and lose it forever.
1450 * Don't do it if a specific register was specified, so that ""x"*P works.
1451 * This could call do_pending_operator() recursively, but that's OK
1452 * because gui_yank will be TRUE for the nested call.
1453 */
1454 if (clip_star.available
1455 && oap->op_type != OP_NOP
1456 && !gui_yank
1457# ifdef FEAT_VISUAL
1458 && VIsual_active
1459 && !redo_VIsual_busy
1460# endif
1461 && oap->regname == 0)
1462 clip_auto_select();
1463#endif
1464 old_cursor = curwin->w_cursor;
1465
1466 /*
1467 * If an operation is pending, handle it...
1468 */
1469 if ((finish_op
1470#ifdef FEAT_VISUAL
1471 || VIsual_active
1472#endif
1473 ) && oap->op_type != OP_NOP)
1474 {
1475#ifdef FEAT_VISUAL
1476 oap->is_VIsual = VIsual_active;
1477 if (oap->motion_force == 'V')
1478 oap->motion_type = MLINE;
1479 else if (oap->motion_force == 'v')
1480 {
1481 /* If the motion was linewise, "inclusive" will not have been set.
1482 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1483 if (oap->motion_type == MLINE)
1484 oap->inclusive = FALSE;
1485 /* If the motion already was characterwise, toggle "inclusive" */
1486 else if (oap->motion_type == MCHAR)
1487 oap->inclusive = !oap->inclusive;
1488 oap->motion_type = MCHAR;
1489 }
1490 else if (oap->motion_force == Ctrl_V)
1491 {
1492 /* Change line- or characterwise motion into Visual block mode. */
1493 VIsual_active = TRUE;
1494 VIsual = oap->start;
1495 VIsual_mode = Ctrl_V;
1496 VIsual_select = FALSE;
1497 VIsual_reselect = FALSE;
1498 }
1499#endif
1500
1501 /* only redo yank when 'y' flag is in 'cpoptions' */
1502 /* never redo "zf" (define fold) */
1503 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1504#ifdef FEAT_VISUAL
1505 && (!VIsual_active || oap->motion_force)
1506#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001507 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508#ifdef FEAT_FOLDING
1509 && oap->op_type != OP_FOLD
1510 && oap->op_type != OP_FOLDOPEN
1511 && oap->op_type != OP_FOLDOPENREC
1512 && oap->op_type != OP_FOLDCLOSE
1513 && oap->op_type != OP_FOLDCLOSEREC
1514 && oap->op_type != OP_FOLDDEL
1515 && oap->op_type != OP_FOLDDELREC
1516#endif
1517 )
1518 {
1519 prep_redo(oap->regname, cap->count0,
1520 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1521 oap->motion_force, cap->cmdchar, cap->nchar);
1522 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1523 {
1524 /*
1525 * If 'cpoptions' does not contain 'r', insert the search
1526 * pattern to really repeat the same command.
1527 */
1528 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001529 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 AppendToRedobuff(NL_STR);
1531 }
1532 else if (cap->cmdchar == ':')
1533 {
1534 /* do_cmdline() has stored the first typed line in
1535 * "repeat_cmdline". When several lines are typed repeating
1536 * won't be possible. */
1537 if (repeat_cmdline == NULL)
1538 ResetRedobuff();
1539 else
1540 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001541 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 AppendToRedobuff(NL_STR);
1543 vim_free(repeat_cmdline);
1544 repeat_cmdline = NULL;
1545 }
1546 }
1547 }
1548
1549#ifdef FEAT_VISUAL
1550 if (redo_VIsual_busy)
1551 {
1552 oap->start = curwin->w_cursor;
1553 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1554 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1555 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1556 VIsual_mode = redo_VIsual_mode;
1557 if (VIsual_mode == 'v')
1558 {
1559 if (redo_VIsual_line_count <= 1)
1560 curwin->w_cursor.col += redo_VIsual_col - 1;
1561 else
1562 curwin->w_cursor.col = redo_VIsual_col;
1563 }
1564 if (redo_VIsual_col == MAXCOL)
1565 {
1566 curwin->w_curswant = MAXCOL;
1567 coladvance((colnr_T)MAXCOL);
1568 }
1569 cap->count0 = redo_VIsual_count;
1570 if (redo_VIsual_count != 0)
1571 cap->count1 = redo_VIsual_count;
1572 else
1573 cap->count1 = 1;
1574 }
1575 else if (VIsual_active)
1576 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001577 if (!gui_yank)
1578 {
1579 /* Save the current VIsual area for '< and '> marks, and "gv" */
1580 curbuf->b_visual.vi_start = VIsual;
1581 curbuf->b_visual.vi_end = curwin->w_cursor;
1582 curbuf->b_visual.vi_mode = VIsual_mode;
1583 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001585 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588
1589 /* In Select mode, a linewise selection is operated upon like a
1590 * characterwise selection. */
1591 if (VIsual_select && VIsual_mode == 'V')
1592 {
1593 if (lt(VIsual, curwin->w_cursor))
1594 {
1595 VIsual.col = 0;
1596 curwin->w_cursor.col =
1597 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1598 }
1599 else
1600 {
1601 curwin->w_cursor.col = 0;
1602 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1603 }
1604 VIsual_mode = 'v';
1605 }
1606 /* If 'selection' is "exclusive", backup one character for
1607 * charwise selections. */
1608 else if (VIsual_mode == 'v')
1609 {
1610# ifdef FEAT_VIRTUALEDIT
1611 include_line_break =
1612# endif
1613 unadjust_for_sel();
1614 }
1615
1616 oap->start = VIsual;
1617 if (VIsual_mode == 'V')
1618 oap->start.col = 0;
1619 }
1620#endif /* FEAT_VISUAL */
1621
1622 /*
1623 * Set oap->start to the first position of the operated text, oap->end
1624 * to the end of the operated text. w_cursor is equal to oap->start.
1625 */
1626 if (lt(oap->start, curwin->w_cursor))
1627 {
1628#ifdef FEAT_FOLDING
1629 /* Include folded lines completely. */
1630 if (!VIsual_active)
1631 {
1632 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1633 oap->start.col = 0;
1634 if (hasFolding(curwin->w_cursor.lnum, NULL,
1635 &curwin->w_cursor.lnum))
1636 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1637 }
1638#endif
1639 oap->end = curwin->w_cursor;
1640 curwin->w_cursor = oap->start;
1641
1642 /* w_virtcol may have been updated; if the cursor goes back to its
1643 * previous position w_virtcol becomes invalid and isn't updated
1644 * automatically. */
1645 curwin->w_valid &= ~VALID_VIRTCOL;
1646 }
1647 else
1648 {
1649#ifdef FEAT_FOLDING
1650 /* Include folded lines completely. */
1651 if (!VIsual_active && oap->motion_type == MLINE)
1652 {
1653 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1654 NULL))
1655 curwin->w_cursor.col = 0;
1656 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1657 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1658 }
1659#endif
1660 oap->end = oap->start;
1661 oap->start = curwin->w_cursor;
1662 }
1663
1664 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1665
1666#ifdef FEAT_VIRTUALEDIT
1667 /* Set "virtual_op" before resetting VIsual_active. */
1668 virtual_op = virtual_active();
1669#endif
1670
1671#ifdef FEAT_VISUAL
1672 if (VIsual_active || redo_VIsual_busy)
1673 {
1674 if (VIsual_mode == Ctrl_V) /* block mode */
1675 {
1676 colnr_T start, end;
1677
1678 oap->block_mode = TRUE;
1679
1680 getvvcol(curwin, &(oap->start),
1681 &oap->start_vcol, NULL, &oap->end_vcol);
1682 if (!redo_VIsual_busy)
1683 {
1684 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1685
1686 if (start < oap->start_vcol)
1687 oap->start_vcol = start;
1688 if (end > oap->end_vcol)
1689 {
1690 if (*p_sel == 'e' && start >= 1
1691 && start - 1 >= oap->end_vcol)
1692 oap->end_vcol = start - 1;
1693 else
1694 oap->end_vcol = end;
1695 }
1696 }
1697
1698 /* if '$' was used, get oap->end_vcol from longest line */
1699 if (curwin->w_curswant == MAXCOL)
1700 {
1701 curwin->w_cursor.col = MAXCOL;
1702 oap->end_vcol = 0;
1703 for (curwin->w_cursor.lnum = oap->start.lnum;
1704 curwin->w_cursor.lnum <= oap->end.lnum;
1705 ++curwin->w_cursor.lnum)
1706 {
1707 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1708 if (end > oap->end_vcol)
1709 oap->end_vcol = end;
1710 }
1711 }
1712 else if (redo_VIsual_busy)
1713 oap->end_vcol = oap->start_vcol + redo_VIsual_col - 1;
1714 /*
1715 * Correct oap->end.col and oap->start.col to be the
1716 * upper-left and lower-right corner of the block area.
1717 *
1718 * (Actually, this does convert column positions into character
1719 * positions)
1720 */
1721 curwin->w_cursor.lnum = oap->end.lnum;
1722 coladvance(oap->end_vcol);
1723 oap->end = curwin->w_cursor;
1724
1725 curwin->w_cursor = oap->start;
1726 coladvance(oap->start_vcol);
1727 oap->start = curwin->w_cursor;
1728 }
1729
1730 if (!redo_VIsual_busy && !gui_yank)
1731 {
1732 /*
1733 * Prepare to reselect and redo Visual: this is based on the
1734 * size of the Visual text
1735 */
1736 resel_VIsual_mode = VIsual_mode;
1737 if (curwin->w_curswant == MAXCOL)
1738 resel_VIsual_col = MAXCOL;
1739 else if (VIsual_mode == Ctrl_V)
1740 resel_VIsual_col = oap->end_vcol - oap->start_vcol + 1;
1741 else if (oap->line_count > 1)
1742 resel_VIsual_col = oap->end.col;
1743 else
1744 resel_VIsual_col = oap->end.col - oap->start.col + 1;
1745 resel_VIsual_line_count = oap->line_count;
1746 }
1747
1748 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1749 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1750 && oap->op_type != OP_COLON
1751#ifdef FEAT_FOLDING
1752 && oap->op_type != OP_FOLD
1753 && oap->op_type != OP_FOLDOPEN
1754 && oap->op_type != OP_FOLDOPENREC
1755 && oap->op_type != OP_FOLDCLOSE
1756 && oap->op_type != OP_FOLDCLOSEREC
1757 && oap->op_type != OP_FOLDDEL
1758 && oap->op_type != OP_FOLDDELREC
1759#endif
1760 && oap->motion_force == NUL
1761 )
1762 {
1763 /* Prepare for redoing. Only use the nchar field for "r",
1764 * otherwise it might be the second char of the operator. */
1765 prep_redo(oap->regname, 0L, NUL, 'v',
1766 get_op_char(oap->op_type),
1767 get_extra_op_char(oap->op_type),
1768 oap->op_type == OP_REPLACE ? cap->nchar : NUL);
1769 if (!redo_VIsual_busy)
1770 {
1771 redo_VIsual_mode = resel_VIsual_mode;
1772 redo_VIsual_col = resel_VIsual_col;
1773 redo_VIsual_line_count = resel_VIsual_line_count;
1774 redo_VIsual_count = cap->count0;
1775 }
1776 }
1777
1778 /*
1779 * oap->inclusive defaults to TRUE.
1780 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1781 * FALSE. This makes "d}P" and "v}dP" work the same.
1782 */
1783 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1784 oap->inclusive = TRUE;
1785 if (VIsual_mode == 'V')
1786 oap->motion_type = MLINE;
1787 else
1788 {
1789 oap->motion_type = MCHAR;
1790 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1791# ifdef FEAT_VIRTUALEDIT
1792 && (include_line_break || !virtual_op)
1793# endif
1794 )
1795 {
1796 oap->inclusive = FALSE;
1797 /* Try to include the newline, unless it's an operator
1798 * that works on lines only */
1799 if (*p_sel != 'o'
1800 && !op_on_lines(oap->op_type)
1801 && oap->end.lnum < curbuf->b_ml.ml_line_count)
1802 {
1803 ++oap->end.lnum;
1804 oap->end.col = 0;
1805# ifdef FEAT_VIRTUALEDIT
1806 oap->end.coladd = 0;
1807# endif
1808 ++oap->line_count;
1809 }
1810 }
1811 }
1812
1813 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001814
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 /*
1816 * Switch Visual off now, so screen updating does
1817 * not show inverted text when the screen is redrawn.
1818 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1819 * no screen redraw, so it is done here to remove the inverted
1820 * part.
1821 */
1822 if (!gui_yank)
1823 {
1824 VIsual_active = FALSE;
1825# ifdef FEAT_MOUSE
1826 setmouse();
1827 mouse_dragging = 0;
1828# endif
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001829 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 clear_cmdline = TRUE; /* unshow visual mode later */
1831#ifdef FEAT_CMDL_INFO
1832 else
1833 clear_showcmd();
1834#endif
1835 if ((oap->op_type == OP_YANK
1836 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001837 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 || oap->op_type == OP_FILTER)
1839 && oap->motion_force == NUL)
1840 redraw_curbuf_later(INVERTED);
1841 }
1842 }
1843#endif
1844
1845#ifdef FEAT_MBYTE
1846 /* Include the trailing byte of a multi-byte char. */
1847 if (has_mbyte && oap->inclusive)
1848 {
1849 int l;
1850
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001851 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 if (l > 1)
1853 oap->end.col += l - 1;
1854 }
1855#endif
1856 curwin->w_set_curswant = TRUE;
1857
1858 /*
1859 * oap->empty is set when start and end are the same. The inclusive
1860 * flag affects this too, unless yanking and the end is on a NUL.
1861 */
1862 oap->empty = (oap->motion_type == MCHAR
1863 && (!oap->inclusive
1864 || (oap->op_type == OP_YANK
1865 && gchar_pos(&oap->end) == NUL))
1866 && equalpos(oap->start, oap->end)
1867#ifdef FEAT_VIRTUALEDIT
1868 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1869#endif
1870 );
1871 /*
1872 * For delete, change and yank, it's an error to operate on an
1873 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1874 */
1875 empty_region_error = (oap->empty
1876 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1877
1878#ifdef FEAT_VISUAL
1879 /* Force a redraw when operating on an empty Visual region, when
1880 * 'modifiable is off or creating a fold. */
1881 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1882# ifdef FEAT_FOLDING
1883 || oap->op_type == OP_FOLD
1884# endif
1885 ))
1886 redraw_curbuf_later(INVERTED);
1887#endif
1888
1889 /*
1890 * If the end of an operator is in column one while oap->motion_type
1891 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1892 * character in the previous line. If op_start is on or before the
1893 * first non-blank in the line, the operator becomes linewise
1894 * (strange, but that's the way vi does it).
1895 */
1896 if ( oap->motion_type == MCHAR
1897 && oap->inclusive == FALSE
1898 && !(cap->retval & CA_NO_ADJ_OP_END)
1899 && oap->end.col == 0
1900#ifdef FEAT_VISUAL
1901 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001902 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903#endif
1904 && oap->line_count > 1)
1905 {
1906 oap->end_adjusted = TRUE; /* remember that we did this */
1907 --oap->line_count;
1908 --oap->end.lnum;
1909 if (inindent(0))
1910 oap->motion_type = MLINE;
1911 else
1912 {
1913 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1914 if (oap->end.col)
1915 {
1916 --oap->end.col;
1917 oap->inclusive = TRUE;
1918 }
1919 }
1920 }
1921 else
1922 oap->end_adjusted = FALSE;
1923
1924 switch (oap->op_type)
1925 {
1926 case OP_LSHIFT:
1927 case OP_RSHIFT:
1928 op_shift(oap, TRUE,
1929#ifdef FEAT_VISUAL
1930 oap->is_VIsual ? (int)cap->count1 :
1931#endif
1932 1);
1933 auto_format(FALSE, TRUE);
1934 break;
1935
1936 case OP_JOIN_NS:
1937 case OP_JOIN:
1938 if (oap->line_count < 2)
1939 oap->line_count = 2;
1940 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1941 curbuf->b_ml.ml_line_count)
1942 beep_flush();
1943 else
1944 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001945 (void)do_join(oap->line_count, oap->op_type == OP_JOIN, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 auto_format(FALSE, TRUE);
1947 }
1948 break;
1949
1950 case OP_DELETE:
1951#ifdef FEAT_VISUAL
1952 VIsual_reselect = FALSE; /* don't reselect now */
1953#endif
1954 if (empty_region_error)
1955 vim_beep();
1956 else
1957 {
1958 (void)op_delete(oap);
1959 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1960 u_save_cursor(); /* cursor line wasn't saved yet */
1961 auto_format(FALSE, TRUE);
1962 }
1963 break;
1964
1965 case OP_YANK:
1966 if (empty_region_error)
1967 {
1968 if (!gui_yank)
1969 vim_beep();
1970 }
1971 else
1972 (void)op_yank(oap, FALSE, !gui_yank);
1973 check_cursor_col();
1974 break;
1975
1976 case OP_CHANGE:
1977#ifdef FEAT_VISUAL
1978 VIsual_reselect = FALSE; /* don't reselect now */
1979#endif
1980 if (empty_region_error)
1981 vim_beep();
1982 else
1983 {
1984 /* This is a new edit command, not a restart. Need to
1985 * remember it to make 'insertmode' work with mappings for
1986 * Visual mode. But do this only once and not when typed and
1987 * 'insertmode' isn't set. */
1988 if (p_im || !KeyTyped)
1989 restart_edit_save = restart_edit;
1990 else
1991 restart_edit_save = 0;
1992 restart_edit = 0;
1993 /* Reset finish_op now, don't want it set inside edit(). */
1994 finish_op = FALSE;
1995 if (op_change(oap)) /* will call edit() */
1996 cap->retval |= CA_COMMAND_BUSY;
1997 if (restart_edit == 0)
1998 restart_edit = restart_edit_save;
1999 }
2000 break;
2001
2002 case OP_FILTER:
2003 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
2004 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
2005 else
2006 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
2007
2008 case OP_INDENT:
2009 case OP_COLON:
2010
2011#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2012 /*
2013 * If 'equalprg' is empty, do the indenting internally.
2014 */
2015 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
2016 {
2017# ifdef FEAT_LISP
2018 if (curbuf->b_p_lisp)
2019 {
2020 op_reindent(oap, get_lisp_indent);
2021 break;
2022 }
2023# endif
2024# ifdef FEAT_CINDENT
2025 op_reindent(oap,
2026# ifdef FEAT_EVAL
2027 *curbuf->b_p_inde != NUL ? get_expr_indent :
2028# endif
2029 get_c_indent);
2030 break;
2031# endif
2032 }
2033#endif
2034
2035 op_colon(oap);
2036 break;
2037
2038 case OP_TILDE:
2039 case OP_UPPER:
2040 case OP_LOWER:
2041 case OP_ROT13:
2042 if (empty_region_error)
2043 vim_beep();
2044 else
2045 op_tilde(oap);
2046 check_cursor_col();
2047 break;
2048
2049 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002050#if defined(FEAT_EVAL)
2051 if (*curbuf->b_p_fex != NUL)
2052 op_formatexpr(oap); /* use expression */
2053 else
2054#endif
2055 if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 op_colon(oap); /* use external command */
2057 else
2058 op_format(oap, FALSE); /* use internal function */
2059 break;
2060
2061 case OP_FORMAT2:
2062 op_format(oap, TRUE); /* use internal function */
2063 break;
2064
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002065 case OP_FUNCTION:
2066 op_function(oap); /* call 'operatorfunc' */
2067 break;
2068
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 case OP_INSERT:
2070 case OP_APPEND:
2071#ifdef FEAT_VISUAL
2072 VIsual_reselect = FALSE; /* don't reselect now */
2073#endif
2074#ifdef FEAT_VISUALEXTRA
2075 if (empty_region_error)
2076 vim_beep();
2077 else
2078 {
2079 /* This is a new edit command, not a restart. Need to
2080 * remember it to make 'insertmode' work with mappings for
2081 * Visual mode. But do this only once. */
2082 restart_edit_save = restart_edit;
2083 restart_edit = 0;
2084
2085 op_insert(oap, cap->count1);
2086
2087 /* TODO: when inserting in several lines, should format all
2088 * the lines. */
2089 auto_format(FALSE, TRUE);
2090
2091 if (restart_edit == 0)
2092 restart_edit = restart_edit_save;
2093 }
2094#else
2095 vim_beep();
2096#endif
2097 break;
2098
2099 case OP_REPLACE:
2100#ifdef FEAT_VISUAL
2101 VIsual_reselect = FALSE; /* don't reselect now */
2102#endif
2103#ifdef FEAT_VISUALEXTRA
2104 if (empty_region_error)
2105#endif
2106 vim_beep();
2107#ifdef FEAT_VISUALEXTRA
2108 else
2109 op_replace(oap, cap->nchar);
2110#endif
2111 break;
2112
2113#ifdef FEAT_FOLDING
2114 case OP_FOLD:
2115 VIsual_reselect = FALSE; /* don't reselect now */
2116 foldCreate(oap->start.lnum, oap->end.lnum);
2117 break;
2118
2119 case OP_FOLDOPEN:
2120 case OP_FOLDOPENREC:
2121 case OP_FOLDCLOSE:
2122 case OP_FOLDCLOSEREC:
2123 VIsual_reselect = FALSE; /* don't reselect now */
2124 opFoldRange(oap->start.lnum, oap->end.lnum,
2125 oap->op_type == OP_FOLDOPEN
2126 || oap->op_type == OP_FOLDOPENREC,
2127 oap->op_type == OP_FOLDOPENREC
2128 || oap->op_type == OP_FOLDCLOSEREC,
2129 oap->is_VIsual);
2130 break;
2131
2132 case OP_FOLDDEL:
2133 case OP_FOLDDELREC:
2134 VIsual_reselect = FALSE; /* don't reselect now */
2135 deleteFold(oap->start.lnum, oap->end.lnum,
2136 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2137 break;
2138#endif
2139 default:
2140 clearopbeep(oap);
2141 }
2142#ifdef FEAT_VIRTUALEDIT
2143 virtual_op = MAYBE;
2144#endif
2145 if (!gui_yank)
2146 {
2147 /*
2148 * if 'sol' not set, go back to old column for some commands
2149 */
2150 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2151 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2152 || oap->op_type == OP_DELETE))
2153 coladvance(curwin->w_curswant = old_col);
2154 }
2155 else
2156 {
2157 curwin->w_cursor = old_cursor;
2158 }
2159#ifdef FEAT_VISUAL
2160 oap->block_mode = FALSE;
2161#endif
2162 clearop(oap);
2163 }
2164}
2165
2166/*
2167 * Handle indent and format operators and visual mode ":".
2168 */
2169 static void
2170op_colon(oap)
2171 oparg_T *oap;
2172{
2173 stuffcharReadbuff(':');
2174#ifdef FEAT_VISUAL
2175 if (oap->is_VIsual)
2176 stuffReadbuff((char_u *)"'<,'>");
2177 else
2178#endif
2179 {
2180 /*
2181 * Make the range look nice, so it can be repeated.
2182 */
2183 if (oap->start.lnum == curwin->w_cursor.lnum)
2184 stuffcharReadbuff('.');
2185 else
2186 stuffnumReadbuff((long)oap->start.lnum);
2187 if (oap->end.lnum != oap->start.lnum)
2188 {
2189 stuffcharReadbuff(',');
2190 if (oap->end.lnum == curwin->w_cursor.lnum)
2191 stuffcharReadbuff('.');
2192 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2193 stuffcharReadbuff('$');
2194 else if (oap->start.lnum == curwin->w_cursor.lnum)
2195 {
2196 stuffReadbuff((char_u *)".+");
2197 stuffnumReadbuff((long)oap->line_count - 1);
2198 }
2199 else
2200 stuffnumReadbuff((long)oap->end.lnum);
2201 }
2202 }
2203 if (oap->op_type != OP_COLON)
2204 stuffReadbuff((char_u *)"!");
2205 if (oap->op_type == OP_INDENT)
2206 {
2207#ifndef FEAT_CINDENT
2208 if (*get_equalprg() == NUL)
2209 stuffReadbuff((char_u *)"indent");
2210 else
2211#endif
2212 stuffReadbuff(get_equalprg());
2213 stuffReadbuff((char_u *)"\n");
2214 }
2215 else if (oap->op_type == OP_FORMAT)
2216 {
2217 if (*p_fp == NUL)
2218 stuffReadbuff((char_u *)"fmt");
2219 else
2220 stuffReadbuff(p_fp);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002221 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 }
2223
2224 /*
2225 * do_cmdline() does the rest
2226 */
2227}
2228
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002229/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002230 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002231 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002232 static void
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002233op_function(oap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002234 oparg_T *oap UNUSED;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002235{
2236#ifdef FEAT_EVAL
2237 char_u *(argv[1]);
2238
2239 if (*p_opfunc == NUL)
2240 EMSG(_("E774: 'operatorfunc' is empty"));
2241 else
2242 {
2243 /* Set '[ and '] marks to text to be operated on. */
2244 curbuf->b_op_start = oap->start;
2245 curbuf->b_op_end = oap->end;
2246 if (oap->motion_type != MLINE && !oap->inclusive)
2247 /* Exclude the end position. */
2248 decl(&curbuf->b_op_end);
2249
2250 if (oap->block_mode)
2251 argv[0] = (char_u *)"block";
2252 else if (oap->motion_type == MLINE)
2253 argv[0] = (char_u *)"line";
2254 else
2255 argv[0] = (char_u *)"char";
2256 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
2257 }
2258#else
2259 EMSG(_("E775: Eval feature not available"));
2260#endif
2261}
2262
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263#if defined(FEAT_MOUSE) || defined(PROTO)
2264/*
2265 * Do the appropriate action for the current mouse click in the current mode.
2266 * Not used for Command-line mode.
2267 *
2268 * Normal Mode:
2269 * event modi- position visual change action
2270 * fier cursor window
2271 * left press - yes end yes
2272 * left press C yes end yes "^]" (2)
2273 * left press S yes end yes "*" (2)
2274 * left drag - yes start if moved no
2275 * left relse - yes start if moved no
2276 * middle press - yes if not active no put register
2277 * middle press - yes if active no yank and put
2278 * right press - yes start or extend yes
2279 * right press S yes no change yes "#" (2)
2280 * right drag - yes extend no
2281 * right relse - yes extend no
2282 *
2283 * Insert or Replace Mode:
2284 * event modi- position visual change action
2285 * fier cursor window
2286 * left press - yes (cannot be active) yes
2287 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2288 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2289 * left drag - yes start or extend (1) no CTRL-O (1)
2290 * left relse - yes start or extend (1) no CTRL-O (1)
2291 * middle press - no (cannot be active) no put register
2292 * right press - yes start or extend yes CTRL-O
2293 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2294 *
2295 * (1) only if mouse pointer moved since press
2296 * (2) only if click is in same buffer
2297 *
2298 * Return TRUE if start_arrow() should be called for edit mode.
2299 */
2300 int
2301do_mouse(oap, c, dir, count, fixindent)
2302 oparg_T *oap; /* operator argument, can be NULL */
2303 int c; /* K_LEFTMOUSE, etc */
2304 int dir; /* Direction to 'put' if necessary */
2305 long count;
2306 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2307{
2308 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2309 static int got_click = FALSE; /* got a click some time back */
2310
2311 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2312 int is_click; /* If FALSE it's a drag or release event */
2313 int is_drag; /* If TRUE it's a drag event */
2314 int jump_flags = 0; /* flags for jump_to_mouse() */
2315 pos_T start_visual;
2316 int moved; /* Has cursor moved? */
2317 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002318#ifdef FEAT_WINDOWS
2319 static int in_tab_line = FALSE; /* mouse clicked in tab line */
2320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321#ifdef FEAT_VERTSPLIT
2322 int in_sep_line; /* mouse in vertical separator line */
2323#endif
2324 int c1, c2;
2325#if defined(FEAT_FOLDING)
2326 pos_T save_cursor;
2327#endif
2328 win_T *old_curwin = curwin;
2329#ifdef FEAT_VISUAL
2330 static pos_T orig_cursor;
2331 colnr_T leftcol, rightcol;
2332 pos_T end_visual;
2333 int diff;
2334 int old_active = VIsual_active;
2335 int old_mode = VIsual_mode;
2336#endif
2337 int regname;
2338
2339#if defined(FEAT_FOLDING)
2340 save_cursor = curwin->w_cursor;
2341#endif
2342
2343 /*
2344 * When GUI is active, always recognize mouse events, otherwise:
2345 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2346 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2347 * - For command line and insert mode 'mouse' is checked before calling
2348 * do_mouse().
2349 */
2350 if (do_always)
2351 do_always = FALSE;
2352 else
2353#ifdef FEAT_GUI
2354 if (!gui.in_use)
2355#endif
2356 {
2357#ifdef FEAT_VISUAL
2358 if (VIsual_active)
2359 {
2360 if (!mouse_has(MOUSE_VISUAL))
2361 return FALSE;
2362 }
2363 else
2364#endif
2365 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2366 return FALSE;
2367 }
2368
2369 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2370
2371#ifdef FEAT_MOUSESHAPE
2372 /* May have stopped dragging the status or separator line. The pointer is
2373 * most likely still on the status or separator line. */
2374 if (!is_drag && drag_status_line)
2375 {
2376 drag_status_line = FALSE;
2377 update_mouseshape(SHAPE_IDX_STATUS);
2378 }
2379# ifdef FEAT_VERTSPLIT
2380 if (!is_drag && drag_sep_line)
2381 {
2382 drag_sep_line = FALSE;
2383 update_mouseshape(SHAPE_IDX_VSEP);
2384 }
2385# endif
2386#endif
2387
2388 /*
2389 * Ignore drag and release events if we didn't get a click.
2390 */
2391 if (is_click)
2392 got_click = TRUE;
2393 else
2394 {
2395 if (!got_click) /* didn't get click, ignore */
2396 return FALSE;
2397 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002398 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002400#ifdef FEAT_WINDOWS
2401 if (in_tab_line)
2402 {
2403 in_tab_line = FALSE;
2404 return FALSE;
2405 }
2406#endif
2407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 }
2409
Bram Moolenaar64969662005-12-14 21:59:55 +00002410#ifndef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 /*
Bram Moolenaar64969662005-12-14 21:59:55 +00002412 * ALT is only used for starging/extending Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 */
2414 if ((mod_mask & MOD_MASK_ALT))
2415 return FALSE;
Bram Moolenaar64969662005-12-14 21:59:55 +00002416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417
2418 /*
2419 * CTRL right mouse button does CTRL-T
2420 */
2421 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2422 {
2423 if (State & INSERT)
2424 stuffcharReadbuff(Ctrl_O);
2425 if (count > 1)
2426 stuffnumReadbuff(count);
2427 stuffcharReadbuff(Ctrl_T);
2428 got_click = FALSE; /* ignore drag&release now */
2429 return FALSE;
2430 }
2431
2432 /*
2433 * CTRL only works with left mouse button
2434 */
2435 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2436 return FALSE;
2437
2438 /*
2439 * When a modifier is down, ignore drag and release events, as well as
2440 * multiple clicks and the middle mouse button.
2441 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2442 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002443 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2444 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 && (!is_click
2446 || (mod_mask & MOD_MASK_MULTI_CLICK)
2447 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002448 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 && mouse_model_popup()
2450 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002451 && !((mod_mask & MOD_MASK_ALT)
2452 && !mouse_model_popup()
2453 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 )
2455 return FALSE;
2456
2457 /*
2458 * If the button press was used as the movement command for an operator
2459 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2460 * drag/release events.
2461 */
2462 if (!is_click && which_button == MOUSE_MIDDLE)
2463 return FALSE;
2464
2465 if (oap != NULL)
2466 regname = oap->regname;
2467 else
2468 regname = 0;
2469
2470 /*
2471 * Middle mouse button does a 'put' of the selected text
2472 */
2473 if (which_button == MOUSE_MIDDLE)
2474 {
2475 if (State == NORMAL)
2476 {
2477 /*
2478 * If an operator was pending, we don't know what the user wanted
2479 * to do. Go back to normal mode: Clear the operator and beep().
2480 */
2481 if (oap != NULL && oap->op_type != OP_NOP)
2482 {
2483 clearopbeep(oap);
2484 return FALSE;
2485 }
2486
2487#ifdef FEAT_VISUAL
2488 /*
2489 * If visual was active, yank the highlighted text and put it
2490 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002491 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 */
2493 if (VIsual_active)
2494 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002495 if (VIsual_select)
2496 {
2497 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002498 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002499 }
2500 else
2501 {
2502 stuffcharReadbuff('y');
2503 stuffcharReadbuff(K_MIDDLEMOUSE);
2504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 do_always = TRUE; /* ignore 'mouse' setting next time */
2506 return FALSE;
2507 }
2508#endif
2509 /*
2510 * The rest is below jump_to_mouse()
2511 */
2512 }
2513
2514 else if ((State & INSERT) == 0)
2515 return FALSE;
2516
2517 /*
2518 * Middle click in insert mode doesn't move the mouse, just insert the
2519 * contents of a register. '.' register is special, can't insert that
2520 * with do_put().
2521 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2522 * happens for the GUI).
2523 */
2524 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2525 {
2526 if (regname == '.')
2527 insert_reg(regname, TRUE);
2528 else
2529 {
2530#ifdef FEAT_CLIPBOARD
2531 if (clip_star.available && regname == 0)
2532 regname = '*';
2533#endif
2534 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2535 insert_reg(regname, TRUE);
2536 else
2537 {
2538 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2539
2540 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2541 AppendCharToRedobuff(Ctrl_R);
2542 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2543 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2544 }
2545 }
2546 return FALSE;
2547 }
2548 }
2549
2550 /* When dragging or button-up stay in the same window. */
2551 if (!is_click)
2552 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2553
2554 start_visual.lnum = 0;
2555
Bram Moolenaarf740b292006-02-16 22:11:02 +00002556#ifdef FEAT_WINDOWS
2557 /* Check for clicking in the tab page line. */
2558 if (mouse_row == 0 && firstwin->w_winrow > 0)
2559 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002560 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002561 {
2562 if (in_tab_line)
2563 {
2564 c1 = TabPageIdxs[mouse_col];
2565 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2566 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002567 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002568 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002569
Bram Moolenaarf740b292006-02-16 22:11:02 +00002570 /* click in a tab selects that tab page */
2571 if (is_click
2572# ifdef FEAT_CMDWIN
2573 && cmdwin_type == 0
2574# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002575 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002576 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002577 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002578 c1 = TabPageIdxs[mouse_col];
2579 if (c1 >= 0)
2580 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002581 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2582 {
2583 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002584 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002585 tabpage_new();
2586 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2587 }
2588 else
2589 {
2590 /* Go to specified tab page, or next one if not clicking
2591 * on a label. */
2592 goto_tabpage(c1);
2593
2594 /* It's like clicking on the status line of a window. */
2595 if (curwin != old_curwin)
2596 end_visual_mode();
2597 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002598 }
2599 else if (c1 < 0)
2600 {
2601 tabpage_T *tp;
2602
2603 /* Close the current or specified tab page. */
2604 if (c1 == -999)
2605 tp = curtab;
2606 else
2607 tp = find_tabpage(-c1);
2608 if (tp == curtab)
2609 {
2610 if (first_tabpage->tp_next != NULL)
2611 tabpage_close(FALSE);
2612 }
2613 else if (tp != NULL)
2614 tabpage_close_other(tp, FALSE);
2615 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002616 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002617 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002618 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002619 else if (is_drag && in_tab_line)
2620 {
2621 c1 = TabPageIdxs[mouse_col];
2622 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2623 return FALSE;
2624 }
2625
Bram Moolenaarf740b292006-02-16 22:11:02 +00002626#endif
2627
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 /*
2629 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2630 * right button up -> pop-up menu
2631 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002632 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 */
2634 if (mouse_model_popup())
2635 {
2636 if (which_button == MOUSE_RIGHT
2637 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2638 {
2639 /*
2640 * NOTE: Ignore right button down and drag mouse events.
2641 * Windows only shows the popup menu on the button up event.
2642 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002643#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2644 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 if (!is_click)
2646 return FALSE;
2647#endif
2648#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2649 if (is_click || is_drag)
2650 return FALSE;
2651#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002652#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2654 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2655 if (gui.in_use)
2656 {
2657 jump_flags = 0;
2658 if (STRCMP(p_mousem, "popup_setpos") == 0)
2659 {
2660 /* First set the cursor position before showing the popup
2661 * menu. */
2662#ifdef FEAT_VISUAL
2663 if (VIsual_active)
2664 {
2665 pos_T m_pos;
2666
2667 /*
2668 * set MOUSE_MAY_STOP_VIS if we are outside the
2669 * selection or the current window (might have false
2670 * negative here)
2671 */
2672 if (mouse_row < W_WINROW(curwin)
2673 || mouse_row
2674 > (W_WINROW(curwin) + curwin->w_height))
2675 jump_flags = MOUSE_MAY_STOP_VIS;
2676 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2677 jump_flags = MOUSE_MAY_STOP_VIS;
2678 else
2679 {
2680 if ((lt(curwin->w_cursor, VIsual)
2681 && (lt(m_pos, curwin->w_cursor)
2682 || lt(VIsual, m_pos)))
2683 || (lt(VIsual, curwin->w_cursor)
2684 && (lt(m_pos, VIsual)
2685 || lt(curwin->w_cursor, m_pos))))
2686 {
2687 jump_flags = MOUSE_MAY_STOP_VIS;
2688 }
2689 else if (VIsual_mode == Ctrl_V)
2690 {
2691 getvcols(curwin, &curwin->w_cursor, &VIsual,
2692 &leftcol, &rightcol);
2693 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2694 if (m_pos.col < leftcol || m_pos.col > rightcol)
2695 jump_flags = MOUSE_MAY_STOP_VIS;
2696 }
2697 }
2698 }
2699 else
2700 jump_flags = MOUSE_MAY_STOP_VIS;
2701#endif
2702 }
2703 if (jump_flags)
2704 {
2705 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2706 update_curbuf(
2707#ifdef FEAT_VISUAL
2708 VIsual_active ? INVERTED :
2709#endif
2710 VALID);
2711 setcursor();
2712 out_flush(); /* Update before showing popup menu */
2713 }
2714# ifdef FEAT_MENU
2715 gui_show_popupmenu();
2716# endif
2717 return (jump_flags & CURSOR_MOVED) != 0;
2718 }
2719 else
2720 return FALSE;
2721#else
2722 return FALSE;
2723#endif
2724 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002725 if (which_button == MOUSE_LEFT
2726 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 {
2728 which_button = MOUSE_RIGHT;
2729 mod_mask &= ~MOD_MASK_SHIFT;
2730 }
2731 }
2732
2733#ifdef FEAT_VISUAL
2734 if ((State & (NORMAL | INSERT))
2735 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2736 {
2737 if (which_button == MOUSE_LEFT)
2738 {
2739 if (is_click)
2740 {
2741 /* stop Visual mode for a left click in a window, but not when
2742 * on a status line */
2743 if (VIsual_active)
2744 jump_flags |= MOUSE_MAY_STOP_VIS;
2745 }
2746 else if (mouse_has(MOUSE_VISUAL))
2747 jump_flags |= MOUSE_MAY_VIS;
2748 }
2749 else if (which_button == MOUSE_RIGHT)
2750 {
2751 if (is_click && VIsual_active)
2752 {
2753 /*
2754 * Remember the start and end of visual before moving the
2755 * cursor.
2756 */
2757 if (lt(curwin->w_cursor, VIsual))
2758 {
2759 start_visual = curwin->w_cursor;
2760 end_visual = VIsual;
2761 }
2762 else
2763 {
2764 start_visual = VIsual;
2765 end_visual = curwin->w_cursor;
2766 }
2767 }
2768 jump_flags |= MOUSE_FOCUS;
2769 if (mouse_has(MOUSE_VISUAL))
2770 jump_flags |= MOUSE_MAY_VIS;
2771 }
2772 }
2773#endif
2774
2775 /*
2776 * If an operator is pending, ignore all drags and releases until the
2777 * next mouse click.
2778 */
2779 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2780 {
2781 got_click = FALSE;
2782 oap->motion_type = MCHAR;
2783 }
2784
2785 /* When releasing the button let jump_to_mouse() know. */
2786 if (!is_click && !is_drag)
2787 jump_flags |= MOUSE_RELEASED;
2788
2789 /*
2790 * JUMP!
2791 */
2792 jump_flags = jump_to_mouse(jump_flags,
2793 oap == NULL ? NULL : &(oap->inclusive), which_button);
2794 moved = (jump_flags & CURSOR_MOVED);
2795 in_status_line = (jump_flags & IN_STATUS_LINE);
2796#ifdef FEAT_VERTSPLIT
2797 in_sep_line = (jump_flags & IN_SEP_LINE);
2798#endif
2799
2800#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002801 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2803 {
2804 int key = KEY2TERMCAP1(c);
2805
2806 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2807 || key == (int)KE_RIGHTRELEASE)
2808 netbeans_button_release(which_button);
2809 }
2810#endif
2811
2812 /* When jumping to another window, clear a pending operator. That's a bit
2813 * friendlier than beeping and not jumping to that window. */
2814 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2815 clearop(oap);
2816
2817#ifdef FEAT_FOLDING
2818 if (mod_mask == 0
2819 && !is_drag
2820 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2821 && which_button == MOUSE_LEFT)
2822 {
2823 /* open or close a fold at this line */
2824 if (jump_flags & MOUSE_FOLD_OPEN)
2825 openFold(curwin->w_cursor.lnum, 1L);
2826 else
2827 closeFold(curwin->w_cursor.lnum, 1L);
2828 /* don't move the cursor if still in the same window */
2829 if (curwin == old_curwin)
2830 curwin->w_cursor = save_cursor;
2831 }
2832#endif
2833
2834#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2835 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2836 {
2837 clip_modeless(which_button, is_click, is_drag);
2838 return FALSE;
2839 }
2840#endif
2841
2842#ifdef FEAT_VISUAL
2843 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002844 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 if (VIsual_active && is_drag && p_so)
2846 {
2847 /* In the very first line, allow scrolling one line */
2848 if (mouse_row == 0)
2849 mouse_dragging = 2;
2850 else
2851 mouse_dragging = 1;
2852 }
2853
2854 /* When dragging the mouse above the window, scroll down. */
2855 if (is_drag && mouse_row < 0 && !in_status_line)
2856 {
2857 scroll_redraw(FALSE, 1L);
2858 mouse_row = 0;
2859 }
2860
2861 if (start_visual.lnum) /* right click in visual mode */
2862 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002863 /* When ALT is pressed make Visual mode blockwise. */
2864 if (mod_mask & MOD_MASK_ALT)
2865 VIsual_mode = Ctrl_V;
2866
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 /*
2868 * In Visual-block mode, divide the area in four, pick up the corner
2869 * that is in the quarter that the cursor is in.
2870 */
2871 if (VIsual_mode == Ctrl_V)
2872 {
2873 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2874 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2875 end_visual.col = leftcol;
2876 else
2877 end_visual.col = rightcol;
2878 if (curwin->w_cursor.lnum <
2879 (start_visual.lnum + end_visual.lnum) / 2)
2880 end_visual.lnum = end_visual.lnum;
2881 else
2882 end_visual.lnum = start_visual.lnum;
2883
2884 /* move VIsual to the right column */
2885 start_visual = curwin->w_cursor; /* save the cursor pos */
2886 curwin->w_cursor = end_visual;
2887 coladvance(end_visual.col);
2888 VIsual = curwin->w_cursor;
2889 curwin->w_cursor = start_visual; /* restore the cursor */
2890 }
2891 else
2892 {
2893 /*
2894 * If the click is before the start of visual, change the start.
2895 * If the click is after the end of visual, change the end. If
2896 * the click is inside the visual, change the closest side.
2897 */
2898 if (lt(curwin->w_cursor, start_visual))
2899 VIsual = end_visual;
2900 else if (lt(end_visual, curwin->w_cursor))
2901 VIsual = start_visual;
2902 else
2903 {
2904 /* In the same line, compare column number */
2905 if (end_visual.lnum == start_visual.lnum)
2906 {
2907 if (curwin->w_cursor.col - start_visual.col >
2908 end_visual.col - curwin->w_cursor.col)
2909 VIsual = start_visual;
2910 else
2911 VIsual = end_visual;
2912 }
2913
2914 /* In different lines, compare line number */
2915 else
2916 {
2917 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2918 (end_visual.lnum - curwin->w_cursor.lnum);
2919
2920 if (diff > 0) /* closest to end */
2921 VIsual = start_visual;
2922 else if (diff < 0) /* closest to start */
2923 VIsual = end_visual;
2924 else /* in the middle line */
2925 {
2926 if (curwin->w_cursor.col <
2927 (start_visual.col + end_visual.col) / 2)
2928 VIsual = end_visual;
2929 else
2930 VIsual = start_visual;
2931 }
2932 }
2933 }
2934 }
2935 }
2936 /*
2937 * If Visual mode started in insert mode, execute "CTRL-O"
2938 */
2939 else if ((State & INSERT) && VIsual_active)
2940 stuffcharReadbuff(Ctrl_O);
2941#endif
2942
2943 /*
2944 * Middle mouse click: Put text before cursor.
2945 */
2946 if (which_button == MOUSE_MIDDLE)
2947 {
2948#ifdef FEAT_CLIPBOARD
2949 if (clip_star.available && regname == 0)
2950 regname = '*';
2951#endif
2952 if (yank_register_mline(regname))
2953 {
2954 if (mouse_past_bottom)
2955 dir = FORWARD;
2956 }
2957 else if (mouse_past_eol)
2958 dir = FORWARD;
2959
2960 if (fixindent)
2961 {
2962 c1 = (dir == BACKWARD) ? '[' : ']';
2963 c2 = 'p';
2964 }
2965 else
2966 {
2967 c1 = (dir == FORWARD) ? 'p' : 'P';
2968 c2 = NUL;
2969 }
2970 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2971
2972 /*
2973 * Remember where the paste started, so in edit() Insstart can be set
2974 * to this position
2975 */
2976 if (restart_edit != 0)
2977 where_paste_started = curwin->w_cursor;
2978 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2979 }
2980
2981#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2982 /*
2983 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2984 * error under the mouse pointer.
2985 */
2986 else if (((mod_mask & MOD_MASK_CTRL)
2987 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2988 && bt_quickfix(curbuf))
2989 {
2990 if (State & INSERT)
2991 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002992 if (curwin->w_llist_ref == NULL) /* quickfix window */
2993 stuffReadbuff((char_u *)":.cc\n");
2994 else /* location list window */
2995 stuffReadbuff((char_u *)":.ll\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 got_click = FALSE; /* ignore drag&release now */
2997 }
2998#endif
2999
3000 /*
3001 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
3002 * under the mouse pointer.
3003 */
3004 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3005 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3006 {
3007 if (State & INSERT)
3008 stuffcharReadbuff(Ctrl_O);
3009 stuffcharReadbuff(Ctrl_RSB);
3010 got_click = FALSE; /* ignore drag&release now */
3011 }
3012
3013 /*
3014 * Shift-Mouse click searches for the next occurrence of the word under
3015 * the mouse pointer
3016 */
3017 else if ((mod_mask & MOD_MASK_SHIFT))
3018 {
3019 if (State & INSERT
3020#ifdef FEAT_VISUAL
3021 || (VIsual_active && VIsual_select)
3022#endif
3023 )
3024 stuffcharReadbuff(Ctrl_O);
3025 if (which_button == MOUSE_LEFT)
3026 stuffcharReadbuff('*');
3027 else /* MOUSE_RIGHT */
3028 stuffcharReadbuff('#');
3029 }
3030
3031 /* Handle double clicks, unless on status line */
3032 else if (in_status_line)
3033 {
3034#ifdef FEAT_MOUSESHAPE
3035 if ((is_drag || is_click) && !drag_status_line)
3036 {
3037 drag_status_line = TRUE;
3038 update_mouseshape(-1);
3039 }
3040#endif
3041 }
3042#ifdef FEAT_VERTSPLIT
3043 else if (in_sep_line)
3044 {
3045# ifdef FEAT_MOUSESHAPE
3046 if ((is_drag || is_click) && !drag_sep_line)
3047 {
3048 drag_sep_line = TRUE;
3049 update_mouseshape(-1);
3050 }
3051# endif
3052 }
3053#endif
3054#ifdef FEAT_VISUAL
3055 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3056 && mouse_has(MOUSE_VISUAL))
3057 {
3058 if (is_click || !VIsual_active)
3059 {
3060 if (VIsual_active)
3061 orig_cursor = VIsual;
3062 else
3063 {
3064 check_visual_highlight();
3065 VIsual = curwin->w_cursor;
3066 orig_cursor = VIsual;
3067 VIsual_active = TRUE;
3068 VIsual_reselect = TRUE;
3069 /* start Select mode if 'selectmode' contains "mouse" */
3070 may_start_select('o');
3071 setmouse();
3072 }
3073 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003074 {
3075 /* Double click with ALT pressed makes it blockwise. */
3076 if (mod_mask & MOD_MASK_ALT)
3077 VIsual_mode = Ctrl_V;
3078 else
3079 VIsual_mode = 'v';
3080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3082 VIsual_mode = 'V';
3083 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3084 VIsual_mode = Ctrl_V;
3085#ifdef FEAT_CLIPBOARD
3086 /* Make sure the clipboard gets updated. Needed because start and
3087 * end may still be the same, and the selection needs to be owned */
3088 clip_star.vmode = NUL;
3089#endif
3090 }
3091 /*
3092 * A double click selects a word or a block.
3093 */
3094 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3095 {
3096 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003097 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098
3099 if (is_click)
3100 {
3101 /* If the character under the cursor (skipping white space) is
3102 * not a word character, try finding a match and select a (),
3103 * {}, [], #if/#endif, etc. block. */
3104 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003105 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 inc(&end_visual);
3107 if (oap != NULL)
3108 oap->motion_type = MCHAR;
3109 if (oap != NULL
3110 && VIsual_mode == 'v'
3111 && !vim_iswordc(gchar_pos(&end_visual))
3112 && equalpos(curwin->w_cursor, VIsual)
3113 && (pos = findmatch(oap, NUL)) != NULL)
3114 {
3115 curwin->w_cursor = *pos;
3116 if (oap->motion_type == MLINE)
3117 VIsual_mode = 'V';
3118 else if (*p_sel == 'e')
3119 {
3120 if (lt(curwin->w_cursor, VIsual))
3121 ++VIsual.col;
3122 else
3123 ++curwin->w_cursor.col;
3124 }
3125 }
3126 }
3127
3128 if (pos == NULL && (is_click || is_drag))
3129 {
3130 /* When not found a match or when dragging: extend to include
3131 * a word. */
3132 if (lt(curwin->w_cursor, orig_cursor))
3133 {
3134 find_start_of_word(&curwin->w_cursor);
3135 find_end_of_word(&VIsual);
3136 }
3137 else
3138 {
3139 find_start_of_word(&VIsual);
3140 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3141#ifdef FEAT_MBYTE
3142 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003143 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144#else
3145 ++curwin->w_cursor.col;
3146#endif
3147 find_end_of_word(&curwin->w_cursor);
3148 }
3149 }
3150 curwin->w_set_curswant = TRUE;
3151 }
3152 if (is_click)
3153 redraw_curbuf_later(INVERTED); /* update the inversion */
3154 }
3155 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003156 {
3157 if (mod_mask & MOD_MASK_ALT)
3158 VIsual_mode = Ctrl_V;
3159 else
3160 VIsual_mode = 'v';
3161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162
3163 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003164 if ((!VIsual_active && old_active && mode_displayed)
3165 || (VIsual_active && p_smd && msg_silent == 0
3166 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 redraw_cmdline = TRUE;
3168#endif
3169
3170 return moved;
3171}
3172
3173#ifdef FEAT_VISUAL
3174/*
3175 * Move "pos" back to the start of the word it's in.
3176 */
3177 static void
3178find_start_of_word(pos)
3179 pos_T *pos;
3180{
3181 char_u *line;
3182 int cclass;
3183 int col;
3184
3185 line = ml_get(pos->lnum);
3186 cclass = get_mouse_class(line + pos->col);
3187
3188 while (pos->col > 0)
3189 {
3190 col = pos->col - 1;
3191#ifdef FEAT_MBYTE
3192 col -= (*mb_head_off)(line, line + col);
3193#endif
3194 if (get_mouse_class(line + col) != cclass)
3195 break;
3196 pos->col = col;
3197 }
3198}
3199
3200/*
3201 * Move "pos" forward to the end of the word it's in.
3202 * When 'selection' is "exclusive", the position is just after the word.
3203 */
3204 static void
3205find_end_of_word(pos)
3206 pos_T *pos;
3207{
3208 char_u *line;
3209 int cclass;
3210 int col;
3211
3212 line = ml_get(pos->lnum);
3213 if (*p_sel == 'e' && pos->col > 0)
3214 {
3215 --pos->col;
3216#ifdef FEAT_MBYTE
3217 pos->col -= (*mb_head_off)(line, line + pos->col);
3218#endif
3219 }
3220 cclass = get_mouse_class(line + pos->col);
3221 while (line[pos->col] != NUL)
3222 {
3223#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003224 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225#else
3226 col = pos->col + 1;
3227#endif
3228 if (get_mouse_class(line + col) != cclass)
3229 {
3230 if (*p_sel == 'e')
3231 pos->col = col;
3232 break;
3233 }
3234 pos->col = col;
3235 }
3236}
3237
3238/*
3239 * Get class of a character for selection: same class means same word.
3240 * 0: blank
3241 * 1: punctuation groups
3242 * 2: normal word character
3243 * >2: multi-byte word character.
3244 */
3245 static int
3246get_mouse_class(p)
3247 char_u *p;
3248{
3249 int c;
3250
3251#ifdef FEAT_MBYTE
3252 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3253 return mb_get_class(p);
3254#endif
3255
3256 c = *p;
3257 if (c == ' ' || c == '\t')
3258 return 0;
3259
3260 if (vim_iswordc(c))
3261 return 2;
3262
3263 /*
3264 * There are a few special cases where we want certain combinations of
3265 * characters to be considered as a single word. These are things like
3266 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003267 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 */
3269 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3270 return 1;
3271 return c;
3272}
3273#endif /* FEAT_VISUAL */
3274#endif /* FEAT_MOUSE */
3275
3276#if defined(FEAT_VISUAL) || defined(PROTO)
3277/*
3278 * Check if highlighting for visual mode is possible, give a warning message
3279 * if not.
3280 */
3281 void
3282check_visual_highlight()
3283{
3284 static int did_check = FALSE;
3285
3286 if (full_screen)
3287 {
3288 if (!did_check && hl_attr(HLF_V) == 0)
3289 MSG(_("Warning: terminal cannot highlight"));
3290 did_check = TRUE;
3291 }
3292}
3293
3294/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003295 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 * This function should ALWAYS be called to end Visual mode, except from
3297 * do_pending_operator().
3298 */
3299 void
3300end_visual_mode()
3301{
3302#ifdef FEAT_CLIPBOARD
3303 /*
3304 * If we are using the clipboard, then remember what was selected in case
3305 * we need to paste it somewhere while we still own the selection.
3306 * Only do this when the clipboard is already owned. Don't want to grab
3307 * the selection when hitting ESC.
3308 */
3309 if (clip_star.available && clip_star.owned)
3310 clip_auto_select();
3311#endif
3312
3313 VIsual_active = FALSE;
3314#ifdef FEAT_MOUSE
3315 setmouse();
3316 mouse_dragging = 0;
3317#endif
3318
3319 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003320 curbuf->b_visual.vi_mode = VIsual_mode;
3321 curbuf->b_visual.vi_start = VIsual;
3322 curbuf->b_visual.vi_end = curwin->w_cursor;
3323 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324#ifdef FEAT_EVAL
3325 curbuf->b_visual_mode_eval = VIsual_mode;
3326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327#ifdef FEAT_VIRTUALEDIT
3328 if (!virtual_active())
3329 curwin->w_cursor.coladd = 0;
3330#endif
3331
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003332 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 clear_cmdline = TRUE; /* unshow visual mode later */
3334#ifdef FEAT_CMDL_INFO
3335 else
3336 clear_showcmd();
3337#endif
3338
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003339 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340}
3341
3342/*
3343 * Reset VIsual_active and VIsual_reselect.
3344 */
3345 void
3346reset_VIsual_and_resel()
3347{
3348 if (VIsual_active)
3349 {
3350 end_visual_mode();
3351 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3352 }
3353 VIsual_reselect = FALSE;
3354}
3355
3356/*
3357 * Reset VIsual_active and VIsual_reselect if it's set.
3358 */
3359 void
3360reset_VIsual()
3361{
3362 if (VIsual_active)
3363 {
3364 end_visual_mode();
3365 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3366 VIsual_reselect = FALSE;
3367 }
3368}
3369#endif /* FEAT_VISUAL */
3370
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003371#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3373
3374/*
3375 * Check for a balloon-eval special item to include when searching for an
3376 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3377 * Returns TRUE if the character at "*ptr" should be included.
3378 * "dir" is FORWARD or BACKWARD, the direction of searching.
3379 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3380 * "bnp" points to a counter for square brackets.
3381 */
3382 static int
3383find_is_eval_item(ptr, colp, bnp, dir)
3384 char_u *ptr;
3385 int *colp;
3386 int *bnp;
3387 int dir;
3388{
3389 /* Accept everything inside []. */
3390 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3391 ++*bnp;
3392 if (*bnp > 0)
3393 {
3394 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3395 --*bnp;
3396 return TRUE;
3397 }
3398
3399 /* skip over "s.var" */
3400 if (*ptr == '.')
3401 return TRUE;
3402
3403 /* two-character item: s->var */
3404 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3405 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3406 {
3407 *colp += dir;
3408 return TRUE;
3409 }
3410 return FALSE;
3411}
3412#endif
3413
3414/*
3415 * Find the identifier under or to the right of the cursor.
3416 * "find_type" can have one of three values:
3417 * FIND_IDENT: find an identifier (keyword)
3418 * FIND_STRING: find any non-white string
3419 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003420 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 *
3422 * There are three steps:
3423 * 1. Search forward for the start of an identifier/string. Doesn't move if
3424 * already on one.
3425 * 2. Search backward for the start of this identifier/string.
3426 * This doesn't match the real Vi but I like it a little better and it
3427 * shouldn't bother anyone.
3428 * 3. Search forward to the end of this identifier/string.
3429 * When FIND_IDENT isn't defined, we backup until a blank.
3430 *
3431 * Returns the length of the string, or zero if no string is found.
3432 * If a string is found, a pointer to the string is put in "*string". This
3433 * string is not always NUL terminated.
3434 */
3435 int
3436find_ident_under_cursor(string, find_type)
3437 char_u **string;
3438 int find_type;
3439{
3440 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3441 curwin->w_cursor.col, string, find_type);
3442}
3443
3444/*
3445 * Like find_ident_under_cursor(), but for any window and any position.
3446 * However: Uses 'iskeyword' from the current window!.
3447 */
3448 int
3449find_ident_at_pos(wp, lnum, startcol, string, find_type)
3450 win_T *wp;
3451 linenr_T lnum;
3452 colnr_T startcol;
3453 char_u **string;
3454 int find_type;
3455{
3456 char_u *ptr;
3457 int col = 0; /* init to shut up GCC */
3458 int i;
3459#ifdef FEAT_MBYTE
3460 int this_class = 0;
3461 int prev_class;
3462 int prevcol;
3463#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003464#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 int bn = 0; /* bracket nesting */
3466#endif
3467
3468 /*
3469 * if i == 0: try to find an identifier
3470 * if i == 1: try to find any non-white string
3471 */
3472 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3473 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3474 {
3475 /*
3476 * 1. skip to start of identifier/string
3477 */
3478 col = startcol;
3479#ifdef FEAT_MBYTE
3480 if (has_mbyte)
3481 {
3482 while (ptr[col] != NUL)
3483 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003484# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 /* Stop at a ']' to evaluate "a[x]". */
3486 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3487 break;
3488# endif
3489 this_class = mb_get_class(ptr + col);
3490 if (this_class != 0 && (i == 1 || this_class != 1))
3491 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003492 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 }
3494 }
3495 else
3496#endif
3497 while (ptr[col] != NUL
3498 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003499# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3501# endif
3502 )
3503 ++col;
3504
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003505#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 /* When starting on a ']' count it, so that we include the '['. */
3507 bn = ptr[col] == ']';
3508#endif
3509
3510 /*
3511 * 2. Back up to start of identifier/string.
3512 */
3513#ifdef FEAT_MBYTE
3514 if (has_mbyte)
3515 {
3516 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003517# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3519 this_class = mb_get_class((char_u *)"a");
3520 else
3521# endif
3522 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003523 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 {
3525 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3526 prev_class = mb_get_class(ptr + prevcol);
3527 if (this_class != prev_class
3528 && (i == 0
3529 || prev_class == 0
3530 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003531# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 && (!(find_type & FIND_EVAL)
3533 || prevcol == 0
3534 || !find_is_eval_item(ptr + prevcol, &prevcol,
3535 &bn, BACKWARD))
3536# endif
3537 )
3538 break;
3539 col = prevcol;
3540 }
3541
3542 /* If we don't want just any old string, or we've found an
3543 * identifier, stop searching. */
3544 if (this_class > 2)
3545 this_class = 2;
3546 if (!(find_type & FIND_STRING) || this_class == 2)
3547 break;
3548 }
3549 else
3550#endif
3551 {
3552 while (col > 0
3553 && ((i == 0
3554 ? vim_iswordc(ptr[col - 1])
3555 : (!vim_iswhite(ptr[col - 1])
3556 && (!(find_type & FIND_IDENT)
3557 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003558#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 || ((find_type & FIND_EVAL)
3560 && col > 1
3561 && find_is_eval_item(ptr + col - 1, &col,
3562 &bn, BACKWARD))
3563#endif
3564 ))
3565 --col;
3566
3567 /* If we don't want just any old string, or we've found an
3568 * identifier, stop searching. */
3569 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3570 break;
3571 }
3572 }
3573
3574 if (ptr[col] == NUL || (i == 0 && (
3575#ifdef FEAT_MBYTE
3576 has_mbyte ? this_class != 2 :
3577#endif
3578 !vim_iswordc(ptr[col]))))
3579 {
3580 /*
3581 * didn't find an identifier or string
3582 */
3583 if (find_type & FIND_STRING)
3584 EMSG(_("E348: No string under cursor"));
3585 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003586 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 return 0;
3588 }
3589 ptr += col;
3590 *string = ptr;
3591
3592 /*
3593 * 3. Find the end if the identifier/string.
3594 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003595#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 bn = 0;
3597 startcol -= col;
3598#endif
3599 col = 0;
3600#ifdef FEAT_MBYTE
3601 if (has_mbyte)
3602 {
3603 /* Search for point of changing multibyte character class. */
3604 this_class = mb_get_class(ptr);
3605 while (ptr[col] != NUL
3606 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3607 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003608# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 || ((find_type & FIND_EVAL)
3610 && col <= (int)startcol
3611 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3612# endif
3613 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003614 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 }
3616 else
3617#endif
3618 while ((i == 0 ? vim_iswordc(ptr[col])
3619 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003620# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 || ((find_type & FIND_EVAL)
3622 && col <= (int)startcol
3623 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3624# endif
3625 )
3626 {
3627 ++col;
3628 }
3629
3630 return col;
3631}
3632
3633/*
3634 * Prepare for redo of a normal command.
3635 */
3636 static void
3637prep_redo_cmd(cap)
3638 cmdarg_T *cap;
3639{
3640 prep_redo(cap->oap->regname, cap->count0,
3641 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3642}
3643
3644/*
3645 * Prepare for redo of any command.
3646 * Note that only the last argument can be a multi-byte char.
3647 */
3648 static void
3649prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3650 int regname;
3651 long num;
3652 int cmd1;
3653 int cmd2;
3654 int cmd3;
3655 int cmd4;
3656 int cmd5;
3657{
3658 ResetRedobuff();
3659 if (regname != 0) /* yank from specified buffer */
3660 {
3661 AppendCharToRedobuff('"');
3662 AppendCharToRedobuff(regname);
3663 }
3664 if (num)
3665 AppendNumberToRedobuff(num);
3666
3667 if (cmd1 != NUL)
3668 AppendCharToRedobuff(cmd1);
3669 if (cmd2 != NUL)
3670 AppendCharToRedobuff(cmd2);
3671 if (cmd3 != NUL)
3672 AppendCharToRedobuff(cmd3);
3673 if (cmd4 != NUL)
3674 AppendCharToRedobuff(cmd4);
3675 if (cmd5 != NUL)
3676 AppendCharToRedobuff(cmd5);
3677}
3678
3679/*
3680 * check for operator active and clear it
3681 *
3682 * return TRUE if operator was active
3683 */
3684 static int
3685checkclearop(oap)
3686 oparg_T *oap;
3687{
3688 if (oap->op_type == OP_NOP)
3689 return FALSE;
3690 clearopbeep(oap);
3691 return TRUE;
3692}
3693
3694/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003695 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003697 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 */
3699 static int
3700checkclearopq(oap)
3701 oparg_T *oap;
3702{
3703 if (oap->op_type == OP_NOP
3704#ifdef FEAT_VISUAL
3705 && !VIsual_active
3706#endif
3707 )
3708 return FALSE;
3709 clearopbeep(oap);
3710 return TRUE;
3711}
3712
3713 static void
3714clearop(oap)
3715 oparg_T *oap;
3716{
3717 oap->op_type = OP_NOP;
3718 oap->regname = 0;
3719 oap->motion_force = NUL;
3720 oap->use_reg_one = FALSE;
3721}
3722
3723 static void
3724clearopbeep(oap)
3725 oparg_T *oap;
3726{
3727 clearop(oap);
3728 beep_flush();
3729}
3730
3731#ifdef FEAT_VISUAL
3732/*
3733 * Remove the shift modifier from a special key.
3734 */
3735 static void
3736unshift_special(cap)
3737 cmdarg_T *cap;
3738{
3739 switch (cap->cmdchar)
3740 {
3741 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3742 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3743 case K_S_UP: cap->cmdchar = K_UP; break;
3744 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3745 case K_S_HOME: cap->cmdchar = K_HOME; break;
3746 case K_S_END: cap->cmdchar = K_END; break;
3747 }
3748 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3749}
3750#endif
3751
3752#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3753/*
3754 * Routines for displaying a partly typed command
3755 */
3756
3757#ifdef FEAT_VISUAL /* need room for size of Visual area */
3758# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3759#else
3760# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3761#endif
3762static char_u showcmd_buf[SHOWCMD_BUFLEN];
3763static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3764static int showcmd_is_clear = TRUE;
3765static int showcmd_visual = FALSE;
3766
3767static void display_showcmd __ARGS((void));
3768
3769 void
3770clear_showcmd()
3771{
3772 if (!p_sc)
3773 return;
3774
3775#ifdef FEAT_VISUAL
3776 if (VIsual_active && !char_avail())
3777 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003778 int cursor_bot = lt(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 long lines;
3780 colnr_T leftcol, rightcol;
3781 linenr_T top, bot;
3782
3783 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003784 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 {
3786 top = VIsual.lnum;
3787 bot = curwin->w_cursor.lnum;
3788 }
3789 else
3790 {
3791 top = curwin->w_cursor.lnum;
3792 bot = VIsual.lnum;
3793 }
3794# ifdef FEAT_FOLDING
3795 /* Include closed folds as a whole. */
3796 hasFolding(top, &top, NULL);
3797 hasFolding(bot, NULL, &bot);
3798# endif
3799 lines = bot - top + 1;
3800
3801 if (VIsual_mode == Ctrl_V)
3802 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003803# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003804 char_u *saved_sbr = p_sbr;
3805
3806 /* Make 'sbr' empty for a moment to get the correct size. */
3807 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003808# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003810# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003811 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003812# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3814 (long)(rightcol - leftcol + 1));
3815 }
3816 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3817 sprintf((char *)showcmd_buf, "%ld", lines);
3818 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003819 {
3820 char_u *s, *e;
3821 int l;
3822 int bytes = 0;
3823 int chars = 0;
3824
3825 if (cursor_bot)
3826 {
3827 s = ml_get_pos(&VIsual);
3828 e = ml_get_cursor();
3829 }
3830 else
3831 {
3832 s = ml_get_cursor();
3833 e = ml_get_pos(&VIsual);
3834 }
3835 while ((*p_sel != 'e') ? s <= e : s < e)
3836 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003837# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003838 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003839# else
3840 l = (*s == NUL) ? 0 : 1;
3841# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003842 if (l == 0)
3843 {
3844 ++bytes;
3845 ++chars;
3846 break; /* end of line */
3847 }
3848 bytes += l;
3849 ++chars;
3850 s += l;
3851 }
3852 if (bytes == chars)
3853 sprintf((char *)showcmd_buf, "%d", chars);
3854 else
3855 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3858 showcmd_visual = TRUE;
3859 }
3860 else
3861#endif
3862 {
3863 showcmd_buf[0] = NUL;
3864 showcmd_visual = FALSE;
3865
3866 /* Don't actually display something if there is nothing to clear. */
3867 if (showcmd_is_clear)
3868 return;
3869 }
3870
3871 display_showcmd();
3872}
3873
3874/*
3875 * Add 'c' to string of shown command chars.
3876 * Return TRUE if output has been written (and setcursor() has been called).
3877 */
3878 int
3879add_to_showcmd(c)
3880 int c;
3881{
3882 char_u *p;
3883 int old_len;
3884 int extra_len;
3885 int overflow;
3886#if defined(FEAT_MOUSE)
3887 int i;
3888 static int ignore[] =
3889 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003890# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3892 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003893# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 K_IGNORE,
3895 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3896 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3897 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003898 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003900 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 0
3902 };
3903#endif
3904
Bram Moolenaar09df3122006-01-23 22:23:09 +00003905 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 return FALSE;
3907
3908 if (showcmd_visual)
3909 {
3910 showcmd_buf[0] = NUL;
3911 showcmd_visual = FALSE;
3912 }
3913
3914#if defined(FEAT_MOUSE)
3915 /* Ignore keys that are scrollbar updates and mouse clicks */
3916 if (IS_SPECIAL(c))
3917 for (i = 0; ignore[i] != 0; ++i)
3918 if (ignore[i] == c)
3919 return FALSE;
3920#endif
3921
3922 p = transchar(c);
3923 old_len = (int)STRLEN(showcmd_buf);
3924 extra_len = (int)STRLEN(p);
3925 overflow = old_len + extra_len - SHOWCMD_COLS;
3926 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003927 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3928 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 STRCAT(showcmd_buf, p);
3930
3931 if (char_avail())
3932 return FALSE;
3933
3934 display_showcmd();
3935
3936 return TRUE;
3937}
3938
3939 void
3940add_to_showcmd_c(c)
3941 int c;
3942{
3943 if (!add_to_showcmd(c))
3944 setcursor();
3945}
3946
3947/*
3948 * Delete 'len' characters from the end of the shown command.
3949 */
3950 static void
3951del_from_showcmd(len)
3952 int len;
3953{
3954 int old_len;
3955
3956 if (!p_sc)
3957 return;
3958
3959 old_len = (int)STRLEN(showcmd_buf);
3960 if (len > old_len)
3961 len = old_len;
3962 showcmd_buf[old_len - len] = NUL;
3963
3964 if (!char_avail())
3965 display_showcmd();
3966}
3967
3968/*
3969 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3970 * something and there is a partial mapping.
3971 */
3972 void
3973push_showcmd()
3974{
3975 if (p_sc)
3976 STRCPY(old_showcmd_buf, showcmd_buf);
3977}
3978
3979 void
3980pop_showcmd()
3981{
3982 if (!p_sc)
3983 return;
3984
3985 STRCPY(showcmd_buf, old_showcmd_buf);
3986
3987 display_showcmd();
3988}
3989
3990 static void
3991display_showcmd()
3992{
3993 int len;
3994
3995 cursor_off();
3996
3997 len = (int)STRLEN(showcmd_buf);
3998 if (len == 0)
3999 showcmd_is_clear = TRUE;
4000 else
4001 {
4002 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
4003 showcmd_is_clear = FALSE;
4004 }
4005
4006 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004007 * clear the rest of an old message by outputting up to SHOWCMD_COLS
4008 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 */
4010 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
4011
4012 setcursor(); /* put cursor back where it belongs */
4013}
4014#endif
4015
4016#ifdef FEAT_SCROLLBIND
4017/*
4018 * When "check" is FALSE, prepare for commands that scroll the window.
4019 * When "check" is TRUE, take care of scroll-binding after the window has
4020 * scrolled. Called from normal_cmd() and edit().
4021 */
4022 void
4023do_check_scrollbind(check)
4024 int check;
4025{
4026 static win_T *old_curwin = NULL;
4027 static linenr_T old_topline = 0;
4028#ifdef FEAT_DIFF
4029 static int old_topfill = 0;
4030#endif
4031 static buf_T *old_buf = NULL;
4032 static colnr_T old_leftcol = 0;
4033
4034 if (check && curwin->w_p_scb)
4035 {
4036 /* If a ":syncbind" command was just used, don't scroll, only reset
4037 * the values. */
4038 if (did_syncbind)
4039 did_syncbind = FALSE;
4040 else if (curwin == old_curwin)
4041 {
4042 /*
4043 * Synchronize other windows, as necessary according to
4044 * 'scrollbind'. Don't do this after an ":edit" command, except
4045 * when 'diff' is set.
4046 */
4047 if ((curwin->w_buffer == old_buf
4048#ifdef FEAT_DIFF
4049 || curwin->w_p_diff
4050#endif
4051 )
4052 && (curwin->w_topline != old_topline
4053#ifdef FEAT_DIFF
4054 || curwin->w_topfill != old_topfill
4055#endif
4056 || curwin->w_leftcol != old_leftcol))
4057 {
4058 check_scrollbind(curwin->w_topline - old_topline,
4059 (long)(curwin->w_leftcol - old_leftcol));
4060 }
4061 }
4062 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4063 {
4064 /*
4065 * When switching between windows, make sure that the relative
4066 * vertical offset is valid for the new window. The relative
4067 * offset is invalid whenever another 'scrollbind' window has
4068 * scrolled to a point that would force the current window to
4069 * scroll past the beginning or end of its buffer. When the
4070 * resync is performed, some of the other 'scrollbind' windows may
4071 * need to jump so that the current window's relative position is
4072 * visible on-screen.
4073 */
4074 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4075 }
4076 curwin->w_scbind_pos = curwin->w_topline;
4077 }
4078
4079 old_curwin = curwin;
4080 old_topline = curwin->w_topline;
4081#ifdef FEAT_DIFF
4082 old_topfill = curwin->w_topfill;
4083#endif
4084 old_buf = curwin->w_buffer;
4085 old_leftcol = curwin->w_leftcol;
4086}
4087
4088/*
4089 * Synchronize any windows that have "scrollbind" set, based on the
4090 * number of rows by which the current window has changed
4091 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4092 */
4093 void
4094check_scrollbind(topline_diff, leftcol_diff)
4095 linenr_T topline_diff;
4096 long leftcol_diff;
4097{
4098 int want_ver;
4099 int want_hor;
4100 win_T *old_curwin = curwin;
4101 buf_T *old_curbuf = curbuf;
4102#ifdef FEAT_VISUAL
4103 int old_VIsual_select = VIsual_select;
4104 int old_VIsual_active = VIsual_active;
4105#endif
4106 colnr_T tgt_leftcol = curwin->w_leftcol;
4107 long topline;
4108 long y;
4109
4110 /*
4111 * check 'scrollopt' string for vertical and horizontal scroll options
4112 */
4113 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4114#ifdef FEAT_DIFF
4115 want_ver |= old_curwin->w_p_diff;
4116#endif
4117 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4118
4119 /*
4120 * loop through the scrollbound windows and scroll accordingly
4121 */
4122#ifdef FEAT_VISUAL
4123 VIsual_select = VIsual_active = 0;
4124#endif
4125 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4126 {
4127 curbuf = curwin->w_buffer;
4128 /* skip original window and windows with 'noscrollbind' */
4129 if (curwin != old_curwin && curwin->w_p_scb)
4130 {
4131 /*
4132 * do the vertical scroll
4133 */
4134 if (want_ver)
4135 {
4136#ifdef FEAT_DIFF
4137 if (old_curwin->w_p_diff && curwin->w_p_diff)
4138 {
4139 diff_set_topline(old_curwin, curwin);
4140 }
4141 else
4142#endif
4143 {
4144 curwin->w_scbind_pos += topline_diff;
4145 topline = curwin->w_scbind_pos;
4146 if (topline > curbuf->b_ml.ml_line_count)
4147 topline = curbuf->b_ml.ml_line_count;
4148 if (topline < 1)
4149 topline = 1;
4150
4151 y = topline - curwin->w_topline;
4152 if (y > 0)
4153 scrollup(y, FALSE);
4154 else
4155 scrolldown(-y, FALSE);
4156 }
4157
4158 redraw_later(VALID);
4159 cursor_correct();
4160#ifdef FEAT_WINDOWS
4161 curwin->w_redr_status = TRUE;
4162#endif
4163 }
4164
4165 /*
4166 * do the horizontal scroll
4167 */
4168 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4169 {
4170 curwin->w_leftcol = tgt_leftcol;
4171 leftcol_changed();
4172 }
4173 }
4174 }
4175
4176 /*
4177 * reset current-window
4178 */
4179#ifdef FEAT_VISUAL
4180 VIsual_select = old_VIsual_select;
4181 VIsual_active = old_VIsual_active;
4182#endif
4183 curwin = old_curwin;
4184 curbuf = old_curbuf;
4185}
4186#endif /* #ifdef FEAT_SCROLLBIND */
4187
4188/*
4189 * Command character that's ignored.
4190 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004191 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 static void
4194nv_ignore(cap)
4195 cmdarg_T *cap;
4196{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004197 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198}
4199
4200/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004201 * Command character that doesn't do anything, but unlike nv_ignore() does
4202 * start edit(). Used for "startinsert" executed while starting up.
4203 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004204 static void
4205nv_nop(cap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004206 cmdarg_T *cap UNUSED;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004207{
4208}
4209
4210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 * Command character doesn't exist.
4212 */
4213 static void
4214nv_error(cap)
4215 cmdarg_T *cap;
4216{
4217 clearopbeep(cap->oap);
4218}
4219
4220/*
4221 * <Help> and <F1> commands.
4222 */
4223 static void
4224nv_help(cap)
4225 cmdarg_T *cap;
4226{
4227 if (!checkclearopq(cap->oap))
4228 ex_help(NULL);
4229}
4230
4231/*
4232 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4233 */
4234 static void
4235nv_addsub(cap)
4236 cmdarg_T *cap;
4237{
4238 if (!checkclearopq(cap->oap)
4239 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4240 prep_redo_cmd(cap);
4241}
4242
4243/*
4244 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4245 */
4246 static void
4247nv_page(cap)
4248 cmdarg_T *cap;
4249{
4250 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004251 {
4252#ifdef FEAT_WINDOWS
4253 if (mod_mask & MOD_MASK_CTRL)
4254 {
4255 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4256 if (cap->arg == BACKWARD)
4257 goto_tabpage(-(int)cap->count1);
4258 else
4259 goto_tabpage((int)cap->count0);
4260 }
4261 else
4262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265}
4266
4267/*
4268 * Implementation of "gd" and "gD" command.
4269 */
4270 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004271nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004272 oparg_T *oap;
4273 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004274 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275{
4276 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 char_u *ptr;
4278
Bram Moolenaard9d30582005-05-18 22:10:28 +00004279 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004280 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004282#ifdef FEAT_FOLDING
4283 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4284 foldOpenCursor();
4285#endif
4286}
4287
4288/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004289 * Search for variable declaration of "ptr[len]".
4290 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4291 * current file ("gD").
4292 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004293 * Return FAIL when not found.
4294 */
4295 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004296find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004297 char_u *ptr;
4298 int len;
4299 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004300 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004301 int searchflags; /* flags passed to searchit() */
4302{
4303 char_u *pat;
4304 pos_T old_pos;
4305 pos_T par_pos;
4306 pos_T found_pos;
4307 int t;
4308 int save_p_ws;
4309 int save_p_scs;
4310 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004311 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004312
4313 if ((pat = alloc(len + 7)) == NULL)
4314 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004315
4316 /* Put "\V" before the pattern to avoid that the special meaning of "."
4317 * and "~" causes trouble. */
4318 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4319 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 old_pos = curwin->w_cursor;
4321 save_p_ws = p_ws;
4322 save_p_scs = p_scs;
4323 p_ws = FALSE; /* don't wrap around end of file now */
4324 p_scs = FALSE; /* don't switch ignorecase off now */
4325
4326 /*
4327 * With "gD" go to line 1.
4328 * With "gd" Search back for the start of the current function, then go
4329 * back until a blank line. If this fails go to line 1.
4330 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004331 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 {
4333 setpcmark(); /* Set in findpar() otherwise */
4334 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004335 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 }
4337 else
4338 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004339 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4341 --curwin->w_cursor.lnum;
4342 }
4343 curwin->w_cursor.col = 0;
4344
4345 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004346 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004347 for (;;)
4348 {
4349 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004350 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004351 if (curwin->w_cursor.lnum >= old_pos.lnum)
4352 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004353
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004354 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004355 {
4356 pos_T *pos;
4357
4358 /* Check that the block the match is in doesn't end before the
4359 * position where we started the search from. */
4360 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4361 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4362 && pos->lnum < old_pos.lnum)
4363 continue;
4364 }
4365
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004366 if (t == FAIL)
4367 {
4368 /* If we previously found a valid position, use it. */
4369 if (found_pos.lnum != 0)
4370 {
4371 curwin->w_cursor = found_pos;
4372 t = OK;
4373 }
4374 break;
4375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376#ifdef FEAT_COMMENTS
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004377 if (get_leader_len(ml_get_curline(), NULL, FALSE) > 0)
4378 {
4379 /* Ignore this line, continue at start of next line. */
4380 ++curwin->w_cursor.lnum;
4381 curwin->w_cursor.col = 0;
4382 continue;
4383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004385 if (!locally) /* global search: use first match found */
4386 break;
4387 if (curwin->w_cursor.lnum >= par_pos.lnum)
4388 {
4389 /* If we previously found a valid position, use it. */
4390 if (found_pos.lnum != 0)
4391 curwin->w_cursor = found_pos;
4392 break;
4393 }
4394
4395 /* For finding a local variable and the match is before the "{" search
4396 * to find a later match. For K&R style function declarations this
4397 * skips the function header without types. */
4398 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004400
4401 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004403 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 curwin->w_cursor = old_pos;
4405 }
4406 else
4407 {
4408 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 /* "n" searches forward now */
4410 reset_search_dir();
4411 }
4412
4413 vim_free(pat);
4414 p_ws = save_p_ws;
4415 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004416
4417 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418}
4419
4420/*
4421 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4422 * lines rather than lines in the file.
4423 * 'dist' must be positive.
4424 *
4425 * Return OK if able to move cursor, FAIL otherwise.
4426 */
4427 static int
4428nv_screengo(oap, dir, dist)
4429 oparg_T *oap;
4430 int dir;
4431 long dist;
4432{
4433 int linelen = linetabsize(ml_get_curline());
4434 int retval = OK;
4435 int atend = FALSE;
4436 int n;
4437 int col_off1; /* margin offset for first screen line */
4438 int col_off2; /* margin offset for wrapped screen line */
4439 int width1; /* text width for first screen line */
4440 int width2; /* test width for wrapped screen line */
4441
4442 oap->motion_type = MCHAR;
4443 oap->inclusive = FALSE;
4444
4445 col_off1 = curwin_col_off();
4446 col_off2 = col_off1 - curwin_col_off2();
4447 width1 = W_WIDTH(curwin) - col_off1;
4448 width2 = W_WIDTH(curwin) - col_off2;
4449
4450#ifdef FEAT_VERTSPLIT
4451 if (curwin->w_width != 0)
4452 {
4453#endif
4454 /*
4455 * Instead of sticking at the last character of the buffer line we
4456 * try to stick in the last column of the screen.
4457 */
4458 if (curwin->w_curswant == MAXCOL)
4459 {
4460 atend = TRUE;
4461 validate_virtcol();
4462 if (width1 <= 0)
4463 curwin->w_curswant = 0;
4464 else
4465 {
4466 curwin->w_curswant = width1 - 1;
4467 if (curwin->w_virtcol > curwin->w_curswant)
4468 curwin->w_curswant += ((curwin->w_virtcol
4469 - curwin->w_curswant - 1) / width2 + 1) * width2;
4470 }
4471 }
4472 else
4473 {
4474 if (linelen > width1)
4475 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4476 else
4477 n = width1;
4478 if (curwin->w_curswant > (colnr_T)n + 1)
4479 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4480 * width2;
4481 }
4482
4483 while (dist--)
4484 {
4485 if (dir == BACKWARD)
4486 {
4487 if ((long)curwin->w_curswant >= width2)
4488 /* move back within line */
4489 curwin->w_curswant -= width2;
4490 else
4491 {
4492 /* to previous line */
4493 if (curwin->w_cursor.lnum == 1)
4494 {
4495 retval = FAIL;
4496 break;
4497 }
4498 --curwin->w_cursor.lnum;
4499#ifdef FEAT_FOLDING
4500 /* Move to the start of a closed fold. Don't do that when
4501 * 'foldopen' contains "all": it will open in a moment. */
4502 if (!(fdo_flags & FDO_ALL))
4503 (void)hasFolding(curwin->w_cursor.lnum,
4504 &curwin->w_cursor.lnum, NULL);
4505#endif
4506 linelen = linetabsize(ml_get_curline());
4507 if (linelen > width1)
4508 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4509 + 1) * width2;
4510 }
4511 }
4512 else /* dir == FORWARD */
4513 {
4514 if (linelen > width1)
4515 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4516 else
4517 n = width1;
4518 if (curwin->w_curswant + width2 < (colnr_T)n)
4519 /* move forward within line */
4520 curwin->w_curswant += width2;
4521 else
4522 {
4523 /* to next line */
4524#ifdef FEAT_FOLDING
4525 /* Move to the end of a closed fold. */
4526 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4527 &curwin->w_cursor.lnum);
4528#endif
4529 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4530 {
4531 retval = FAIL;
4532 break;
4533 }
4534 curwin->w_cursor.lnum++;
4535 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004536 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 }
4538 }
4539 }
4540#ifdef FEAT_VERTSPLIT
4541 }
4542#endif
4543
4544 coladvance(curwin->w_curswant);
4545
4546#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4547 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4548 {
4549 /*
4550 * Check for landing on a character that got split at the end of the
4551 * last line. We want to advance a screenline, not end up in the same
4552 * screenline or move two screenlines.
4553 */
4554 validate_virtcol();
4555 if (curwin->w_virtcol > curwin->w_curswant
4556 && (curwin->w_curswant < (colnr_T)width1
4557 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4558 : ((curwin->w_curswant - width1) % width2
4559 > (colnr_T)width2 / 2)))
4560 --curwin->w_cursor.col;
4561 }
4562#endif
4563
4564 if (atend)
4565 curwin->w_curswant = MAXCOL; /* stick in the last column */
4566
4567 return retval;
4568}
4569
4570#ifdef FEAT_MOUSE
4571/*
4572 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4573 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004574 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4575 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 */
4577 static void
4578nv_mousescroll(cap)
4579 cmdarg_T *cap;
4580{
4581# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004582 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583
4584 /* Currently we only get the mouse coordinates in the GUI. */
4585 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4586 {
4587 int row, col;
4588
4589 row = mouse_row;
4590 col = mouse_col;
4591
4592 /* find the window at the pointer coordinates */
4593 curwin = mouse_find_win(&row, &col);
4594 curbuf = curwin->w_buffer;
4595 }
4596# endif
4597
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004598 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004600 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4601 {
4602 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4603 }
4604 else
4605 {
4606 cap->count1 = 3;
4607 cap->count0 = 3;
4608 nv_scroll_line(cap);
4609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004611# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 else
4613 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004614 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4615 if (!curwin->w_p_wrap)
4616 {
4617 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004618
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004619 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4620 step = W_WIDTH(curwin);
4621 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4622 if (val < 0)
4623 val = 0;
4624
4625 gui_do_horiz_scroll(val, TRUE);
4626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004628# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629
4630# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4631 curwin->w_redr_status = TRUE;
4632
4633 curwin = old_curwin;
4634 curbuf = curwin->w_buffer;
4635# endif
4636}
4637
4638/*
4639 * Mouse clicks and drags.
4640 */
4641 static void
4642nv_mouse(cap)
4643 cmdarg_T *cap;
4644{
4645 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4646}
4647#endif
4648
4649/*
4650 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4651 * cap->arg must be TRUE for CTRL-E.
4652 */
4653 static void
4654nv_scroll_line(cap)
4655 cmdarg_T *cap;
4656{
4657 if (!checkclearop(cap->oap))
4658 scroll_redraw(cap->arg, cap->count1);
4659}
4660
4661/*
4662 * Scroll "count" lines up or down, and redraw.
4663 */
4664 void
4665scroll_redraw(up, count)
4666 int up;
4667 long count;
4668{
4669 linenr_T prev_topline = curwin->w_topline;
4670#ifdef FEAT_DIFF
4671 int prev_topfill = curwin->w_topfill;
4672#endif
4673 linenr_T prev_lnum = curwin->w_cursor.lnum;
4674
4675 if (up)
4676 scrollup(count, TRUE);
4677 else
4678 scrolldown(count, TRUE);
4679 if (p_so)
4680 {
4681 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4682 * valid, otherwise the screen jumps back at the end of the file. */
4683 cursor_correct();
4684 check_cursor_moved(curwin);
4685 curwin->w_valid |= VALID_TOPLINE;
4686
4687 /* If moved back to where we were, at least move the cursor, otherwise
4688 * we get stuck at one position. Don't move the cursor up if the
4689 * first line of the buffer is already on the screen */
4690 while (curwin->w_topline == prev_topline
4691#ifdef FEAT_DIFF
4692 && curwin->w_topfill == prev_topfill
4693#endif
4694 )
4695 {
4696 if (up)
4697 {
4698 if (curwin->w_cursor.lnum > prev_lnum
4699 || cursor_down(1L, FALSE) == FAIL)
4700 break;
4701 }
4702 else
4703 {
4704 if (curwin->w_cursor.lnum < prev_lnum
4705 || prev_topline == 1L
4706 || cursor_up(1L, FALSE) == FAIL)
4707 break;
4708 }
4709 /* Mark w_topline as valid, otherwise the screen jumps back at the
4710 * end of the file. */
4711 check_cursor_moved(curwin);
4712 curwin->w_valid |= VALID_TOPLINE;
4713 }
4714 }
4715 if (curwin->w_cursor.lnum != prev_lnum)
4716 coladvance(curwin->w_curswant);
4717 redraw_later(VALID);
4718}
4719
4720/*
4721 * Commands that start with "z".
4722 */
4723 static void
4724nv_zet(cap)
4725 cmdarg_T *cap;
4726{
4727 long n;
4728 colnr_T col;
4729 int nchar = cap->nchar;
4730#ifdef FEAT_FOLDING
4731 long old_fdl = curwin->w_p_fdl;
4732 int old_fen = curwin->w_p_fen;
4733#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004734#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004735 int undo = FALSE;
4736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737
4738 if (VIM_ISDIGIT(nchar))
4739 {
4740 /*
4741 * "z123{nchar}": edit the count before obtaining {nchar}
4742 */
4743 if (checkclearop(cap->oap))
4744 return;
4745 n = nchar - '0';
4746 for (;;)
4747 {
4748#ifdef USE_ON_FLY_SCROLL
4749 dont_scroll = TRUE; /* disallow scrolling here */
4750#endif
4751 ++no_mapping;
4752 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004753 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 --no_mapping;
4756 --allow_keys;
4757#ifdef FEAT_CMDL_INFO
4758 (void)add_to_showcmd(nchar);
4759#endif
4760 if (nchar == K_DEL || nchar == K_KDEL)
4761 n /= 10;
4762 else if (VIM_ISDIGIT(nchar))
4763 n = n * 10 + (nchar - '0');
4764 else if (nchar == CAR)
4765 {
4766#ifdef FEAT_GUI
4767 need_mouse_correct = TRUE;
4768#endif
4769 win_setheight((int)n);
4770 break;
4771 }
4772 else if (nchar == 'l'
4773 || nchar == 'h'
4774 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004775 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 {
4777 cap->count1 = n ? n * cap->count1 : cap->count1;
4778 goto dozet;
4779 }
4780 else
4781 {
4782 clearopbeep(cap->oap);
4783 break;
4784 }
4785 }
4786 cap->oap->op_type = OP_NOP;
4787 return;
4788 }
4789
4790dozet:
4791 if (
4792#ifdef FEAT_FOLDING
4793 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4794 * and "zC" only in Visual mode. "zj" and "zk" are motion
4795 * commands. */
4796 cap->nchar != 'f' && cap->nchar != 'F'
4797 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4798 && cap->nchar != 'j' && cap->nchar != 'k'
4799 &&
4800#endif
4801 checkclearop(cap->oap))
4802 return;
4803
4804 /*
4805 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4806 * If line number given, set cursor.
4807 */
4808 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4809 && cap->count0
4810 && cap->count0 != curwin->w_cursor.lnum)
4811 {
4812 setpcmark();
4813 if (cap->count0 > curbuf->b_ml.ml_line_count)
4814 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4815 else
4816 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004817 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 }
4819
4820 switch (nchar)
4821 {
4822 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4823 case '+':
4824 if (cap->count0 == 0)
4825 {
4826 /* No count given: put cursor at the line below screen */
4827 validate_botline(); /* make sure w_botline is valid */
4828 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4829 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4830 else
4831 curwin->w_cursor.lnum = curwin->w_botline;
4832 }
4833 /* FALLTHROUGH */
4834 case NL:
4835 case CAR:
4836 case K_KENTER:
4837 beginline(BL_WHITE | BL_FIX);
4838 /* FALLTHROUGH */
4839
4840 case 't': scroll_cursor_top(0, TRUE);
4841 redraw_later(VALID);
4842 break;
4843
4844 /* "z." and "zz": put cursor in middle of screen */
4845 case '.': beginline(BL_WHITE | BL_FIX);
4846 /* FALLTHROUGH */
4847
4848 case 'z': scroll_cursor_halfway(TRUE);
4849 redraw_later(VALID);
4850 break;
4851
4852 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4853 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4854 * when <count> is at bottom of window, and puts that one at
4855 * bottom of window. */
4856 if (cap->count0 != 0)
4857 {
4858 scroll_cursor_bot(0, TRUE);
4859 curwin->w_cursor.lnum = curwin->w_topline;
4860 }
4861 else if (curwin->w_topline == 1)
4862 curwin->w_cursor.lnum = 1;
4863 else
4864 curwin->w_cursor.lnum = curwin->w_topline - 1;
4865 /* FALLTHROUGH */
4866 case '-':
4867 beginline(BL_WHITE | BL_FIX);
4868 /* FALLTHROUGH */
4869
4870 case 'b': scroll_cursor_bot(0, TRUE);
4871 redraw_later(VALID);
4872 break;
4873
4874 /* "zH" - scroll screen right half-page */
4875 case 'H':
4876 cap->count1 *= W_WIDTH(curwin) / 2;
4877 /* FALLTHROUGH */
4878
4879 /* "zh" - scroll screen to the right */
4880 case 'h':
4881 case K_LEFT:
4882 if (!curwin->w_p_wrap)
4883 {
4884 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4885 curwin->w_leftcol = 0;
4886 else
4887 curwin->w_leftcol -= (colnr_T)cap->count1;
4888 leftcol_changed();
4889 }
4890 break;
4891
4892 /* "zL" - scroll screen left half-page */
4893 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4894 /* FALLTHROUGH */
4895
4896 /* "zl" - scroll screen to the left */
4897 case 'l':
4898 case K_RIGHT:
4899 if (!curwin->w_p_wrap)
4900 {
4901 /* scroll the window left */
4902 curwin->w_leftcol += (colnr_T)cap->count1;
4903 leftcol_changed();
4904 }
4905 break;
4906
4907 /* "zs" - scroll screen, cursor at the start */
4908 case 's': if (!curwin->w_p_wrap)
4909 {
4910#ifdef FEAT_FOLDING
4911 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4912 col = 0; /* like the cursor is in col 0 */
4913 else
4914#endif
4915 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4916 if ((long)col > p_siso)
4917 col -= p_siso;
4918 else
4919 col = 0;
4920 if (curwin->w_leftcol != col)
4921 {
4922 curwin->w_leftcol = col;
4923 redraw_later(NOT_VALID);
4924 }
4925 }
4926 break;
4927
4928 /* "ze" - scroll screen, cursor at the end */
4929 case 'e': if (!curwin->w_p_wrap)
4930 {
4931#ifdef FEAT_FOLDING
4932 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4933 col = 0; /* like the cursor is in col 0 */
4934 else
4935#endif
4936 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4937 n = W_WIDTH(curwin) - curwin_col_off();
4938 if ((long)col + p_siso < n)
4939 col = 0;
4940 else
4941 col = col + p_siso - n + 1;
4942 if (curwin->w_leftcol != col)
4943 {
4944 curwin->w_leftcol = col;
4945 redraw_later(NOT_VALID);
4946 }
4947 }
4948 break;
4949
4950#ifdef FEAT_FOLDING
4951 /* "zF": create fold command */
4952 /* "zf": create fold operator */
4953 case 'F':
4954 case 'f': if (foldManualAllowed(TRUE))
4955 {
4956 cap->nchar = 'f';
4957 nv_operator(cap);
4958 curwin->w_p_fen = TRUE;
4959
4960 /* "zF" is like "zfzf" */
4961 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4962 {
4963 nv_operator(cap);
4964 finish_op = TRUE;
4965 }
4966 }
4967 else
4968 clearopbeep(cap->oap);
4969 break;
4970
4971 /* "zd": delete fold at cursor */
4972 /* "zD": delete fold at cursor recursively */
4973 case 'd':
4974 case 'D': if (foldManualAllowed(FALSE))
4975 {
4976 if (VIsual_active)
4977 nv_operator(cap);
4978 else
4979 deleteFold(curwin->w_cursor.lnum,
4980 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4981 }
4982 break;
4983
4984 /* "zE": erease all folds */
4985 case 'E': if (foldmethodIsManual(curwin))
4986 {
4987 clearFolding(curwin);
4988 changed_window_setting();
4989 }
4990 else if (foldmethodIsMarker(curwin))
4991 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4992 TRUE, FALSE);
4993 else
4994 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4995 break;
4996
4997 /* "zn": fold none: reset 'foldenable' */
4998 case 'n': curwin->w_p_fen = FALSE;
4999 break;
5000
5001 /* "zN": fold Normal: set 'foldenable' */
5002 case 'N': curwin->w_p_fen = TRUE;
5003 break;
5004
5005 /* "zi": invert folding: toggle 'foldenable' */
5006 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5007 break;
5008
5009 /* "za": open closed fold or close open fold at cursor */
5010 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5011 openFold(curwin->w_cursor.lnum, cap->count1);
5012 else
5013 {
5014 closeFold(curwin->w_cursor.lnum, cap->count1);
5015 curwin->w_p_fen = TRUE;
5016 }
5017 break;
5018
5019 /* "zA": open fold at cursor recursively */
5020 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5021 openFoldRecurse(curwin->w_cursor.lnum);
5022 else
5023 {
5024 closeFoldRecurse(curwin->w_cursor.lnum);
5025 curwin->w_p_fen = TRUE;
5026 }
5027 break;
5028
5029 /* "zo": open fold at cursor or Visual area */
5030 case 'o': if (VIsual_active)
5031 nv_operator(cap);
5032 else
5033 openFold(curwin->w_cursor.lnum, cap->count1);
5034 break;
5035
5036 /* "zO": open fold recursively */
5037 case 'O': if (VIsual_active)
5038 nv_operator(cap);
5039 else
5040 openFoldRecurse(curwin->w_cursor.lnum);
5041 break;
5042
5043 /* "zc": close fold at cursor or Visual area */
5044 case 'c': if (VIsual_active)
5045 nv_operator(cap);
5046 else
5047 closeFold(curwin->w_cursor.lnum, cap->count1);
5048 curwin->w_p_fen = TRUE;
5049 break;
5050
5051 /* "zC": close fold recursively */
5052 case 'C': if (VIsual_active)
5053 nv_operator(cap);
5054 else
5055 closeFoldRecurse(curwin->w_cursor.lnum);
5056 curwin->w_p_fen = TRUE;
5057 break;
5058
5059 /* "zv": open folds at the cursor */
5060 case 'v': foldOpenCursor();
5061 break;
5062
5063 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5064 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005065 curwin->w_foldinvalid = TRUE; /* recompute folds */
5066 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 foldOpenCursor();
5068 break;
5069
5070 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5071 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005072 curwin->w_foldinvalid = TRUE; /* recompute folds */
5073 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 break;
5075
5076 /* "zm": fold more */
5077 case 'm': if (curwin->w_p_fdl > 0)
5078 --curwin->w_p_fdl;
5079 old_fdl = -1; /* force an update */
5080 curwin->w_p_fen = TRUE;
5081 break;
5082
5083 /* "zM": close all folds */
5084 case 'M': curwin->w_p_fdl = 0;
5085 old_fdl = -1; /* force an update */
5086 curwin->w_p_fen = TRUE;
5087 break;
5088
5089 /* "zr": reduce folding */
5090 case 'r': ++curwin->w_p_fdl;
5091 break;
5092
5093 /* "zR": open all folds */
5094 case 'R': curwin->w_p_fdl = getDeepestNesting();
5095 old_fdl = -1; /* force an update */
5096 break;
5097
5098 case 'j': /* "zj" move to next fold downwards */
5099 case 'k': /* "zk" move to next fold upwards */
5100 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5101 cap->count1) == FAIL)
5102 clearopbeep(cap->oap);
5103 break;
5104
5105#endif /* FEAT_FOLDING */
5106
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005107#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005108 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5109 ++no_mapping;
5110 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005111 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005112 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005113 --no_mapping;
5114 --allow_keys;
5115#ifdef FEAT_CMDL_INFO
5116 (void)add_to_showcmd(nchar);
5117#endif
5118 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5119 {
5120 clearopbeep(cap->oap);
5121 break;
5122 }
5123 undo = TRUE;
5124 /*FALLTHROUGH*/
5125
Bram Moolenaarb765d632005-06-07 21:00:02 +00005126 case 'g': /* "zg": add good word to word list */
5127 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005128 case 'G': /* "zG": add good word to temp word list */
5129 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005130 {
5131 char_u *ptr = NULL;
5132 int len;
5133
5134 if (checkclearop(cap->oap))
5135 break;
5136# ifdef FEAT_VISUAL
5137 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5138 == FAIL)
5139 return;
5140# endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005141 if (ptr == NULL)
5142 {
5143 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005144
5145 /* Find bad word under the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005146 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005147 if (len != 0 && curwin->w_cursor.col <= pos.col)
5148 ptr = ml_get_pos(&curwin->w_cursor);
5149 curwin->w_cursor = pos;
5150 }
5151
Bram Moolenaarb765d632005-06-07 21:00:02 +00005152 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5153 FIND_IDENT)) == 0)
5154 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005155 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005156 (nchar == 'G' || nchar == 'W')
5157 ? 0 : (int)cap->count1,
5158 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005159 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005160 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005161
Bram Moolenaar43abc522005-12-10 20:15:02 +00005162 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005163 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005164 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005165 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005166#endif
5167
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 default: clearopbeep(cap->oap);
5169 }
5170
5171#ifdef FEAT_FOLDING
5172 /* Redraw when 'foldenable' changed */
5173 if (old_fen != curwin->w_p_fen)
5174 {
5175# ifdef FEAT_DIFF
5176 win_T *wp;
5177
5178 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5179 {
5180 /* Adjust 'foldenable' in diff-synced windows. */
5181 FOR_ALL_WINDOWS(wp)
5182 {
5183 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5184 {
5185 wp->w_p_fen = curwin->w_p_fen;
5186 changed_window_setting_win(wp);
5187 }
5188 }
5189 }
5190# endif
5191 changed_window_setting();
5192 }
5193
5194 /* Redraw when 'foldlevel' changed. */
5195 if (old_fdl != curwin->w_p_fdl)
5196 newFoldLevel();
5197#endif
5198}
5199
5200#ifdef FEAT_GUI
5201/*
5202 * Vertical scrollbar movement.
5203 */
5204 static void
5205nv_ver_scrollbar(cap)
5206 cmdarg_T *cap;
5207{
5208 if (cap->oap->op_type != OP_NOP)
5209 clearopbeep(cap->oap);
5210
5211 /* Even if an operator was pending, we still want to scroll */
5212 gui_do_scroll();
5213}
5214
5215/*
5216 * Horizontal scrollbar movement.
5217 */
5218 static void
5219nv_hor_scrollbar(cap)
5220 cmdarg_T *cap;
5221{
5222 if (cap->oap->op_type != OP_NOP)
5223 clearopbeep(cap->oap);
5224
5225 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005226 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227}
5228#endif
5229
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005230#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005231/*
5232 * Click in GUI tab.
5233 */
5234 static void
5235nv_tabline(cap)
5236 cmdarg_T *cap;
5237{
5238 if (cap->oap->op_type != OP_NOP)
5239 clearopbeep(cap->oap);
5240
5241 /* Even if an operator was pending, we still want to jump tabs. */
5242 goto_tabpage(current_tab);
5243}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005244
5245/*
5246 * Selected item in tab line menu.
5247 */
5248 static void
5249nv_tabmenu(cap)
5250 cmdarg_T *cap;
5251{
5252 if (cap->oap->op_type != OP_NOP)
5253 clearopbeep(cap->oap);
5254
5255 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005256 handle_tabmenu();
5257}
5258
5259/*
5260 * Handle selecting an item of the GUI tab line menu.
5261 * Used in Normal and Insert mode.
5262 */
5263 void
5264handle_tabmenu()
5265{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005266 switch (current_tabmenu)
5267 {
5268 case TABLINE_MENU_CLOSE:
5269 if (current_tab == 0)
5270 do_cmdline_cmd((char_u *)"tabclose");
5271 else
5272 {
5273 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5274 current_tab);
5275 do_cmdline_cmd(IObuff);
5276 }
5277 break;
5278
5279 case TABLINE_MENU_NEW:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005280 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5281 current_tab > 0 ? current_tab - 1 : 999);
5282 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005283 break;
5284
5285 case TABLINE_MENU_OPEN:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005286 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5287 current_tab > 0 ? current_tab - 1 : 999);
5288 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005289 break;
5290 }
5291}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005292#endif
5293
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294/*
5295 * "Q" command.
5296 */
5297 static void
5298nv_exmode(cap)
5299 cmdarg_T *cap;
5300{
5301 /*
5302 * Ignore 'Q' in Visual mode, just give a beep.
5303 */
5304#ifdef FEAT_VISUAL
5305 if (VIsual_active)
5306 vim_beep();
5307 else
5308#endif
5309 if (!checkclearop(cap->oap))
5310 do_exmode(FALSE);
5311}
5312
5313/*
5314 * Handle a ":" command.
5315 */
5316 static void
5317nv_colon(cap)
5318 cmdarg_T *cap;
5319{
5320 int old_p_im;
5321
5322#ifdef FEAT_VISUAL
5323 if (VIsual_active)
5324 nv_operator(cap);
5325 else
5326#endif
5327 {
5328 if (cap->oap->op_type != OP_NOP)
5329 {
5330 /* Using ":" as a movement is characterwise exclusive. */
5331 cap->oap->motion_type = MCHAR;
5332 cap->oap->inclusive = FALSE;
5333 }
5334 else if (cap->count0)
5335 {
5336 /* translate "count:" into ":.,.+(count - 1)" */
5337 stuffcharReadbuff('.');
5338 if (cap->count0 > 1)
5339 {
5340 stuffReadbuff((char_u *)",.+");
5341 stuffnumReadbuff((long)cap->count0 - 1L);
5342 }
5343 }
5344
5345 /* When typing, don't type below an old message */
5346 if (KeyTyped)
5347 compute_cmdrow();
5348
5349 old_p_im = p_im;
5350
5351 /* get a command line and execute it */
5352 do_cmdline(NULL, getexline, NULL,
5353 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5354
5355 /* If 'insertmode' changed, enter or exit Insert mode */
5356 if (p_im != old_p_im)
5357 {
5358 if (p_im)
5359 restart_edit = 'i';
5360 else
5361 restart_edit = 0;
5362 }
5363
5364 /* The start of the operator may have become invalid by the Ex
5365 * command. */
5366 if (cap->oap->op_type != OP_NOP
5367 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5368 || cap->oap->start.col >
Bram Moolenaar78a15312009-05-15 19:33:18 +00005369 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 clearopbeep(cap->oap);
5371 }
5372}
5373
5374/*
5375 * Handle CTRL-G command.
5376 */
5377 static void
5378nv_ctrlg(cap)
5379 cmdarg_T *cap;
5380{
5381#ifdef FEAT_VISUAL
5382 if (VIsual_active) /* toggle Selection/Visual mode */
5383 {
5384 VIsual_select = !VIsual_select;
5385 showmode();
5386 }
5387 else
5388#endif
5389 if (!checkclearop(cap->oap))
5390 /* print full name if count given or :cd used */
5391 fileinfo((int)cap->count0, FALSE, TRUE);
5392}
5393
5394/*
5395 * Handle CTRL-H <Backspace> command.
5396 */
5397 static void
5398nv_ctrlh(cap)
5399 cmdarg_T *cap;
5400{
5401#ifdef FEAT_VISUAL
5402 if (VIsual_active && VIsual_select)
5403 {
5404 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5405 v_visop(cap);
5406 }
5407 else
5408#endif
5409 nv_left(cap);
5410}
5411
5412/*
5413 * CTRL-L: clear screen and redraw.
5414 */
5415 static void
5416nv_clear(cap)
5417 cmdarg_T *cap;
5418{
5419 if (!checkclearop(cap->oap))
5420 {
5421#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5422 /*
5423 * Right now, the BeBox doesn't seem to have an easy way to detect
5424 * window resizing, so we cheat and make the user detect it
5425 * manually with CTRL-L instead
5426 */
5427 ui_get_shellsize();
5428#endif
5429#ifdef FEAT_SYN_HL
5430 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005431 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432#endif
5433 redraw_later(CLEAR);
5434 }
5435}
5436
5437/*
5438 * CTRL-O: In Select mode: switch to Visual mode for one command.
5439 * Otherwise: Go to older pcmark.
5440 */
5441 static void
5442nv_ctrlo(cap)
5443 cmdarg_T *cap;
5444{
5445#ifdef FEAT_VISUAL
5446 if (VIsual_active && VIsual_select)
5447 {
5448 VIsual_select = FALSE;
5449 showmode();
5450 restart_VIsual_select = 2; /* restart Select mode later */
5451 }
5452 else
5453#endif
5454 {
5455 cap->count1 = -cap->count1;
5456 nv_pcmark(cap);
5457 }
5458}
5459
5460/*
5461 * CTRL-^ command, short for ":e #"
5462 */
5463 static void
5464nv_hat(cap)
5465 cmdarg_T *cap;
5466{
5467 if (!checkclearopq(cap->oap))
5468 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5469 GETF_SETMARK|GETF_ALT, FALSE);
5470}
5471
5472/*
5473 * "Z" commands.
5474 */
5475 static void
5476nv_Zet(cap)
5477 cmdarg_T *cap;
5478{
5479 if (!checkclearopq(cap->oap))
5480 {
5481 switch (cap->nchar)
5482 {
5483 /* "ZZ": equivalent to ":x". */
5484 case 'Z': do_cmdline_cmd((char_u *)"x");
5485 break;
5486
5487 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5488 case 'Q': do_cmdline_cmd((char_u *)"q!");
5489 break;
5490
5491 default: clearopbeep(cap->oap);
5492 }
5493 }
5494}
5495
5496#if defined(FEAT_WINDOWS) || defined(PROTO)
5497/*
5498 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5499 */
5500 void
5501do_nv_ident(c1, c2)
5502 int c1;
5503 int c2;
5504{
5505 oparg_T oa;
5506 cmdarg_T ca;
5507
5508 clear_oparg(&oa);
5509 vim_memset(&ca, 0, sizeof(ca));
5510 ca.oap = &oa;
5511 ca.cmdchar = c1;
5512 ca.nchar = c2;
5513 nv_ident(&ca);
5514}
5515#endif
5516
5517/*
5518 * Handle the commands that use the word under the cursor.
5519 * [g] CTRL-] :ta to current identifier
5520 * [g] 'K' run program for current identifier
5521 * [g] '*' / to current identifier or string
5522 * [g] '#' ? to current identifier or string
5523 * g ']' :tselect for current identifier
5524 */
5525 static void
5526nv_ident(cap)
5527 cmdarg_T *cap;
5528{
5529 char_u *ptr = NULL;
5530 char_u *buf;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005531 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 char_u *p;
5533 char_u *kp; /* value of 'keywordprg' */
5534 int kp_help; /* 'keywordprg' is ":help" */
5535 int n = 0; /* init for GCC */
5536 int cmdchar;
5537 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005538 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 char_u *aux_ptr;
5540 int isman;
5541 int isman_s;
5542
5543 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5544 {
5545 cmdchar = cap->nchar;
5546 g_cmd = TRUE;
5547 }
5548 else
5549 {
5550 cmdchar = cap->cmdchar;
5551 g_cmd = FALSE;
5552 }
5553
5554 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5555 cmdchar = '#';
5556
5557 /*
5558 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5559 */
5560 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5561 {
5562#ifdef FEAT_VISUAL
5563 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5564 return;
5565#endif
5566 if (checkclearopq(cap->oap))
5567 return;
5568 }
5569
5570 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5571 (cmdchar == '*' || cmdchar == '#')
5572 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5573 {
5574 clearop(cap->oap);
5575 return;
5576 }
5577
5578 /* Allocate buffer to put the command in. Inserting backslashes can
5579 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5580 * and some numbers. */
5581 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5582 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5583 || STRCMP(kp, ":help") == 0);
5584 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5585 if (buf == NULL)
5586 return;
5587 buf[0] = NUL;
5588
5589 switch (cmdchar)
5590 {
5591 case '*':
5592 case '#':
5593 /*
5594 * Put cursor at start of word, makes search skip the word
5595 * under the cursor.
5596 * Call setpcmark() first, so "*``" puts the cursor back where
5597 * it was.
5598 */
5599 setpcmark();
5600 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5601
5602 if (!g_cmd && vim_iswordp(ptr))
5603 STRCPY(buf, "\\<");
5604 no_smartcase = TRUE; /* don't use 'smartcase' now */
5605 break;
5606
5607 case 'K':
5608 if (kp_help)
5609 STRCPY(buf, "he! ");
5610 else
5611 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005612 /* An external command will probably use an argument starting
5613 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005614 while (*ptr == '-' && n > 0)
5615 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005616 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005617 --n;
5618 }
5619 if (n == 0)
5620 {
5621 EMSG(_(e_noident)); /* found dashes only */
5622 vim_free(buf);
5623 return;
5624 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005625
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 /* When a count is given, turn it into a range. Is this
5627 * really what we want? */
5628 isman = (STRCMP(kp, "man") == 0);
5629 isman_s = (STRCMP(kp, "man -s") == 0);
5630 if (cap->count0 != 0 && !(isman || isman_s))
5631 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5632
5633 STRCAT(buf, "! ");
5634 if (cap->count0 == 0 && isman_s)
5635 STRCAT(buf, "man");
5636 else
5637 STRCAT(buf, kp);
5638 STRCAT(buf, " ");
5639 if (cap->count0 != 0 && (isman || isman_s))
5640 {
5641 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5642 STRCAT(buf, " ");
5643 }
5644 }
5645 break;
5646
5647 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005648 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649#ifdef FEAT_CSCOPE
5650 if (p_cst)
5651 STRCPY(buf, "cstag ");
5652 else
5653#endif
5654 STRCPY(buf, "ts ");
5655 break;
5656
5657 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005658 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 if (curbuf->b_help)
5660 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005662 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005663 if (g_cmd)
5664 STRCPY(buf, "tj ");
5665 else
5666 sprintf((char *)buf, "%ldta ", cap->count0);
5667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 }
5669
5670 /*
5671 * Now grab the chars in the identifier
5672 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005673 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005675 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005676 ptr = vim_strnsave(ptr, n);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005677 p = vim_strsave_shellescape(ptr, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005678 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005679 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005681 vim_free(buf);
5682 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005684 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5685 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005686 {
5687 vim_free(buf);
5688 vim_free(p);
5689 return;
5690 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005691 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005692 STRCAT(buf, p);
5693 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005695 else
5696 {
5697 if (cmdchar == '*')
5698 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5699 else if (cmdchar == '#')
5700 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005701 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005702 {
5703 if (curbuf->b_help)
5704 /* ":help" handles unescaped argument */
5705 aux_ptr = (char_u *)"";
5706 else
5707 aux_ptr = (char_u *)"\\|\"\n[";
5708 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005709 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005710 aux_ptr = (char_u *)"\\|\"\n*?[";
5711
5712 p = buf + STRLEN(buf);
5713 while (n-- > 0)
5714 {
5715 /* put a backslash before \ and some others */
5716 if (vim_strchr(aux_ptr, *ptr) != NULL)
5717 *p++ = '\\';
5718#ifdef FEAT_MBYTE
5719 /* When current byte is a part of multibyte character, copy all
5720 * bytes of that character. */
5721 if (has_mbyte)
5722 {
5723 int i;
5724 int len = (*mb_ptr2len)(ptr) - 1;
5725
5726 for (i = 0; i < len && n >= 1; ++i, --n)
5727 *p++ = *ptr++;
5728 }
5729#endif
5730 *p++ = *ptr++;
5731 }
5732 *p = NUL;
5733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734
5735 /*
5736 * Execute the command.
5737 */
5738 if (cmdchar == '*' || cmdchar == '#')
5739 {
5740 if (!g_cmd && (
5741#ifdef FEAT_MBYTE
5742 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5743#endif
5744 vim_iswordc(ptr[-1])))
5745 STRCAT(buf, "\\>");
5746#ifdef FEAT_CMDHIST
5747 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005748 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5750#endif
5751 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5752 }
5753 else
5754 do_cmdline_cmd(buf);
5755
5756 vim_free(buf);
5757}
5758
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005759#if defined(FEAT_VISUAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760/*
5761 * Get visually selected text, within one line only.
5762 * Returns FAIL if more than one line selected.
5763 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005764 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765get_visual_text(cap, pp, lenp)
5766 cmdarg_T *cap;
5767 char_u **pp; /* return: start of selected text */
5768 int *lenp; /* return: length of selected text */
5769{
5770 if (VIsual_mode != 'V')
5771 unadjust_for_sel();
5772 if (VIsual.lnum != curwin->w_cursor.lnum)
5773 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005774 if (cap != NULL)
5775 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 return FAIL;
5777 }
5778 if (VIsual_mode == 'V')
5779 {
5780 *pp = ml_get_curline();
5781 *lenp = (int)STRLEN(*pp);
5782 }
5783 else
5784 {
5785 if (lt(curwin->w_cursor, VIsual))
5786 {
5787 *pp = ml_get_pos(&curwin->w_cursor);
5788 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5789 }
5790 else
5791 {
5792 *pp = ml_get_pos(&VIsual);
5793 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5794 }
5795#ifdef FEAT_MBYTE
5796 if (has_mbyte)
5797 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005798 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799#endif
5800 }
5801 reset_VIsual_and_resel();
5802 return OK;
5803}
5804#endif
5805
5806/*
5807 * CTRL-T: backwards in tag stack
5808 */
5809 static void
5810nv_tagpop(cap)
5811 cmdarg_T *cap;
5812{
5813 if (!checkclearopq(cap->oap))
5814 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5815}
5816
5817/*
5818 * Handle scrolling command 'H', 'L' and 'M'.
5819 */
5820 static void
5821nv_scroll(cap)
5822 cmdarg_T *cap;
5823{
5824 int used = 0;
5825 long n;
5826#ifdef FEAT_FOLDING
5827 linenr_T lnum;
5828#endif
5829 int half;
5830
5831 cap->oap->motion_type = MLINE;
5832 setpcmark();
5833
5834 if (cap->cmdchar == 'L')
5835 {
5836 validate_botline(); /* make sure curwin->w_botline is valid */
5837 curwin->w_cursor.lnum = curwin->w_botline - 1;
5838 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5839 curwin->w_cursor.lnum = 1;
5840 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005841 {
5842#ifdef FEAT_FOLDING
5843 if (hasAnyFolding(curwin))
5844 {
5845 /* Count a fold for one screen line. */
5846 for (n = cap->count1 - 1; n > 0
5847 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5848 {
5849 (void)hasFolding(curwin->w_cursor.lnum,
5850 &curwin->w_cursor.lnum, NULL);
5851 --curwin->w_cursor.lnum;
5852 }
5853 }
5854 else
5855#endif
5856 curwin->w_cursor.lnum -= cap->count1 - 1;
5857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 }
5859 else
5860 {
5861 if (cap->cmdchar == 'M')
5862 {
5863#ifdef FEAT_DIFF
5864 /* Don't count filler lines above the window. */
5865 used -= diff_check_fill(curwin, curwin->w_topline)
5866 - curwin->w_topfill;
5867#endif
5868 validate_botline(); /* make sure w_empty_rows is valid */
5869 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5870 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5871 {
5872#ifdef FEAT_DIFF
5873 /* Count half he number of filler lines to be "below this
5874 * line" and half to be "above the next line". */
5875 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5876 + n) / 2 >= half)
5877 {
5878 --n;
5879 break;
5880 }
5881#endif
5882 used += plines(curwin->w_topline + n);
5883 if (used >= half)
5884 break;
5885#ifdef FEAT_FOLDING
5886 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5887 n = lnum - curwin->w_topline;
5888#endif
5889 }
5890 if (n > 0 && used > curwin->w_height)
5891 --n;
5892 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005893 else /* (cap->cmdchar == 'H') */
5894 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005896#ifdef FEAT_FOLDING
5897 if (hasAnyFolding(curwin))
5898 {
5899 /* Count a fold for one screen line. */
5900 lnum = curwin->w_topline;
5901 while (n-- > 0 && lnum < curwin->w_botline - 1)
5902 {
5903 hasFolding(lnum, NULL, &lnum);
5904 ++lnum;
5905 }
5906 n = lnum - curwin->w_topline;
5907 }
5908#endif
5909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 curwin->w_cursor.lnum = curwin->w_topline + n;
5911 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5912 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5913 }
5914
5915 cursor_correct(); /* correct for 'so' */
5916 beginline(BL_SOL | BL_FIX);
5917}
5918
5919/*
5920 * Cursor right commands.
5921 */
5922 static void
5923nv_right(cap)
5924 cmdarg_T *cap;
5925{
5926 long n;
5927#ifdef FEAT_VISUAL
5928 int PAST_LINE;
5929#else
5930# define PAST_LINE 0
5931#endif
5932
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005933 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5934 {
5935 /* <C-Right> and <S-Right> move a word or WORD right */
5936 if (mod_mask & MOD_MASK_CTRL)
5937 cap->arg = TRUE;
5938 nv_wordcmd(cap);
5939 return;
5940 }
5941
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 cap->oap->motion_type = MCHAR;
5943 cap->oap->inclusive = FALSE;
5944#ifdef FEAT_VISUAL
5945 PAST_LINE = (VIsual_active && *p_sel != 'o');
5946
5947# ifdef FEAT_VIRTUALEDIT
5948 /*
5949 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
Bram Moolenaarf711faf2007-05-10 16:48:19 +00005950 * (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 */
5952 if (virtual_active())
5953 PAST_LINE = 0;
5954# endif
5955#endif
5956
5957 for (n = cap->count1; n > 0; --n)
5958 {
5959 if ((!PAST_LINE && oneright() == FAIL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005960#ifdef FEAT_VISUAL
5961 || (PAST_LINE && *ml_get_cursor() == NUL)
5962#endif
5963 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 {
5965 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005966 * <Space> wraps to next line if 'whichwrap' has 's'.
5967 * 'l' wraps to next line if 'whichwrap' has 'l'.
5968 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 */
5970 if ( ((cap->cmdchar == ' '
5971 && vim_strchr(p_ww, 's') != NULL)
5972 || (cap->cmdchar == 'l'
5973 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005974 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 && vim_strchr(p_ww, '>') != NULL))
5976 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5977 {
5978 /* When deleting we also count the NL as a character.
5979 * Set cap->oap->inclusive when last char in the line is
5980 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005981 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 && !cap->oap->inclusive
5983 && !lineempty(curwin->w_cursor.lnum))
5984 cap->oap->inclusive = TRUE;
5985 else
5986 {
5987 ++curwin->w_cursor.lnum;
5988 curwin->w_cursor.col = 0;
5989#ifdef FEAT_VIRTUALEDIT
5990 curwin->w_cursor.coladd = 0;
5991#endif
5992 curwin->w_set_curswant = TRUE;
5993 cap->oap->inclusive = FALSE;
5994 }
5995 continue;
5996 }
5997 if (cap->oap->op_type == OP_NOP)
5998 {
5999 /* Only beep and flush if not moved at all */
6000 if (n == cap->count1)
6001 beep_flush();
6002 }
6003 else
6004 {
6005 if (!lineempty(curwin->w_cursor.lnum))
6006 cap->oap->inclusive = TRUE;
6007 }
6008 break;
6009 }
6010#ifdef FEAT_VISUAL
6011 else if (PAST_LINE)
6012 {
6013 curwin->w_set_curswant = TRUE;
6014# ifdef FEAT_VIRTUALEDIT
6015 if (virtual_active())
6016 oneright();
6017 else
6018# endif
6019 {
6020# ifdef FEAT_MBYTE
6021 if (has_mbyte)
6022 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006023 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 else
6025# endif
6026 ++curwin->w_cursor.col;
6027 }
6028 }
6029#endif
6030 }
6031#ifdef FEAT_FOLDING
6032 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6033 && cap->oap->op_type == OP_NOP)
6034 foldOpenCursor();
6035#endif
6036}
6037
6038/*
6039 * Cursor left commands.
6040 *
6041 * Returns TRUE when operator end should not be adjusted.
6042 */
6043 static void
6044nv_left(cap)
6045 cmdarg_T *cap;
6046{
6047 long n;
6048
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006049 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6050 {
6051 /* <C-Left> and <S-Left> move a word or WORD left */
6052 if (mod_mask & MOD_MASK_CTRL)
6053 cap->arg = 1;
6054 nv_bck_word(cap);
6055 return;
6056 }
6057
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 cap->oap->motion_type = MCHAR;
6059 cap->oap->inclusive = FALSE;
6060 for (n = cap->count1; n > 0; --n)
6061 {
6062 if (oneleft() == FAIL)
6063 {
6064 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6065 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6066 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6067 */
6068 if ( (((cap->cmdchar == K_BS
6069 || cap->cmdchar == Ctrl_H)
6070 && vim_strchr(p_ww, 'b') != NULL)
6071 || (cap->cmdchar == 'h'
6072 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006073 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 && vim_strchr(p_ww, '<') != NULL))
6075 && curwin->w_cursor.lnum > 1)
6076 {
6077 --(curwin->w_cursor.lnum);
6078 coladvance((colnr_T)MAXCOL);
6079 curwin->w_set_curswant = TRUE;
6080
6081 /* When the NL before the first char has to be deleted we
6082 * put the cursor on the NUL after the previous line.
6083 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006084 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 if ( (cap->oap->op_type == OP_DELETE
6086 || cap->oap->op_type == OP_CHANGE)
6087 && !lineempty(curwin->w_cursor.lnum))
6088 {
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006089 if (*ml_get_cursor() != NUL)
6090 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 cap->retval |= CA_NO_ADJ_OP_END;
6092 }
6093 continue;
6094 }
6095 /* Only beep and flush if not moved at all */
6096 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6097 beep_flush();
6098 break;
6099 }
6100 }
6101#ifdef FEAT_FOLDING
6102 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6103 && cap->oap->op_type == OP_NOP)
6104 foldOpenCursor();
6105#endif
6106}
6107
6108/*
6109 * Cursor up commands.
6110 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6111 */
6112 static void
6113nv_up(cap)
6114 cmdarg_T *cap;
6115{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006116 if (mod_mask & MOD_MASK_SHIFT)
6117 {
6118 /* <S-Up> is page up */
6119 cap->arg = BACKWARD;
6120 nv_page(cap);
6121 }
6122 else
6123 {
6124 cap->oap->motion_type = MLINE;
6125 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6126 clearopbeep(cap->oap);
6127 else if (cap->arg)
6128 beginline(BL_WHITE | BL_FIX);
6129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130}
6131
6132/*
6133 * Cursor down commands.
6134 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6135 */
6136 static void
6137nv_down(cap)
6138 cmdarg_T *cap;
6139{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006140 if (mod_mask & MOD_MASK_SHIFT)
6141 {
6142 /* <S-Down> is page down */
6143 cap->arg = FORWARD;
6144 nv_page(cap);
6145 }
6146 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6148 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006149 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006150 if (curwin->w_llist_ref == NULL)
6151 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6152 else
6153 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 else
6155#endif
6156 {
6157#ifdef FEAT_CMDWIN
6158 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006159 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 cmdwin_result = CAR;
6161 else
6162#endif
6163 {
6164 cap->oap->motion_type = MLINE;
6165 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6166 clearopbeep(cap->oap);
6167 else if (cap->arg)
6168 beginline(BL_WHITE | BL_FIX);
6169 }
6170 }
6171}
6172
6173#ifdef FEAT_SEARCHPATH
6174/*
6175 * Grab the file name under the cursor and edit it.
6176 */
6177 static void
6178nv_gotofile(cap)
6179 cmdarg_T *cap;
6180{
6181 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006182 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006184 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 {
6186 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006187 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 return;
6189 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006190#ifdef FEAT_AUTOCMD
6191 if (curbuf_locked())
6192 {
6193 clearop(cap->oap);
6194 return;
6195 }
6196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006198 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199
6200 if (ptr != NULL)
6201 {
6202 /* do autowrite if necessary */
6203 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6204 autowrite(curbuf, FALSE);
6205 setpcmark();
6206 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006207 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006208 if (cap->nchar == 'F' && lnum >= 0)
6209 {
6210 curwin->w_cursor.lnum = lnum;
6211 check_cursor_lnum();
6212 beginline(BL_SOL | BL_FIX);
6213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 vim_free(ptr);
6215 }
6216 else
6217 clearop(cap->oap);
6218}
6219#endif
6220
6221/*
6222 * <End> command: to end of current line or last line.
6223 */
6224 static void
6225nv_end(cap)
6226 cmdarg_T *cap;
6227{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006228 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006230 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 nv_goto(cap);
6232 cap->count1 = 1; /* to end of current line */
6233 }
6234 nv_dollar(cap);
6235}
6236
6237/*
6238 * Handle the "$" command.
6239 */
6240 static void
6241nv_dollar(cap)
6242 cmdarg_T *cap;
6243{
6244 cap->oap->motion_type = MCHAR;
6245 cap->oap->inclusive = TRUE;
6246#ifdef FEAT_VIRTUALEDIT
6247 /* In virtual mode when off the edge of a line and an operator
6248 * is pending (whew!) keep the cursor where it is.
6249 * Otherwise, send it to the end of the line. */
6250 if (!virtual_active() || gchar_cursor() != NUL
6251 || cap->oap->op_type == OP_NOP)
6252#endif
6253 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6254 if (cursor_down((long)(cap->count1 - 1),
6255 cap->oap->op_type == OP_NOP) == FAIL)
6256 clearopbeep(cap->oap);
6257#ifdef FEAT_FOLDING
6258 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6259 foldOpenCursor();
6260#endif
6261}
6262
6263/*
6264 * Implementation of '?' and '/' commands.
6265 * If cap->arg is TRUE don't set PC mark.
6266 */
6267 static void
6268nv_search(cap)
6269 cmdarg_T *cap;
6270{
6271 oparg_T *oap = cap->oap;
6272
6273 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6274 {
6275 /* Translate "g??" to "g?g?" */
6276 cap->cmdchar = 'g';
6277 cap->nchar = '?';
6278 nv_operator(cap);
6279 return;
6280 }
6281
6282 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6283
6284 if (cap->searchbuf == NULL)
6285 {
6286 clearop(oap);
6287 return;
6288 }
6289
6290 normal_search(cap, cap->cmdchar, cap->searchbuf,
6291 (cap->arg ? 0 : SEARCH_MARK));
6292}
6293
6294/*
6295 * Handle "N" and "n" commands.
6296 * cap->arg is SEARCH_REV for "N", 0 for "n".
6297 */
6298 static void
6299nv_next(cap)
6300 cmdarg_T *cap;
6301{
6302 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6303}
6304
6305/*
6306 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6307 * Uses only cap->count1 and cap->oap from "cap".
6308 */
6309 static void
6310normal_search(cap, dir, pat, opt)
6311 cmdarg_T *cap;
6312 int dir;
6313 char_u *pat;
6314 int opt; /* extra flags for do_search() */
6315{
6316 int i;
6317
6318 cap->oap->motion_type = MCHAR;
6319 cap->oap->inclusive = FALSE;
6320 cap->oap->use_reg_one = TRUE;
6321 curwin->w_set_curswant = TRUE;
6322
6323 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006324 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 if (i == 0)
6326 clearop(cap->oap);
6327 else
6328 {
6329 if (i == 2)
6330 cap->oap->motion_type = MLINE;
6331#ifdef FEAT_VIRTUALEDIT
6332 curwin->w_cursor.coladd = 0;
6333#endif
6334#ifdef FEAT_FOLDING
6335 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6336 foldOpenCursor();
6337#endif
6338 }
6339
6340 /* "/$" will put the cursor after the end of the line, may need to
6341 * correct that here */
6342 check_cursor();
6343}
6344
6345/*
6346 * Character search commands.
6347 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6348 * ',' and FALSE for ';'.
6349 * cap->nchar is NUL for ',' and ';' (repeat the search)
6350 */
6351 static void
6352nv_csearch(cap)
6353 cmdarg_T *cap;
6354{
6355 int t_cmd;
6356
6357 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6358 t_cmd = TRUE;
6359 else
6360 t_cmd = FALSE;
6361
6362 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6364 clearopbeep(cap->oap);
6365 else
6366 {
6367 curwin->w_set_curswant = TRUE;
6368#ifdef FEAT_VIRTUALEDIT
6369 /* Include a Tab for "tx" and for "dfx". */
6370 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6371 && (t_cmd || cap->oap->op_type != OP_NOP))
6372 {
6373 colnr_T scol, ecol;
6374
6375 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6376 curwin->w_cursor.coladd = ecol - scol;
6377 }
6378 else
6379 curwin->w_cursor.coladd = 0;
6380#endif
6381#ifdef FEAT_VISUAL
6382 adjust_for_sel(cap);
6383#endif
6384#ifdef FEAT_FOLDING
6385 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6386 foldOpenCursor();
6387#endif
6388 }
6389}
6390
6391/*
6392 * "[" and "]" commands.
6393 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6394 */
6395 static void
6396nv_brackets(cap)
6397 cmdarg_T *cap;
6398{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006399 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 pos_T prev_pos;
6401 pos_T *pos = NULL; /* init for GCC */
6402 pos_T old_pos; /* cursor position before command */
6403 int flag;
6404 long n;
6405 int findc;
6406 int c;
6407
6408 cap->oap->motion_type = MCHAR;
6409 cap->oap->inclusive = FALSE;
6410 old_pos = curwin->w_cursor;
6411#ifdef FEAT_VIRTUALEDIT
6412 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6413#endif
6414
6415#ifdef FEAT_SEARCHPATH
6416 /*
6417 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6418 */
6419 if (cap->nchar == 'f')
6420 nv_gotofile(cap);
6421 else
6422#endif
6423
6424#ifdef FEAT_FIND_ID
6425 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006426 * Find the occurrence(s) of the identifier or define under cursor
6427 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 *
6429 * search list jump
6430 * fwd bwd fwd bwd fwd bwd
6431 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6432 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6433 */
6434 if (vim_strchr((char_u *)
6435#ifdef EBCDIC
6436 "iI\005dD\067",
6437#else
6438 "iI\011dD\004",
6439#endif
6440 cap->nchar) != NULL)
6441 {
6442 char_u *ptr;
6443 int len;
6444
6445 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6446 clearop(cap->oap);
6447 else
6448 {
6449 find_pattern_in_path(ptr, 0, len, TRUE,
6450 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6451 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6452 cap->count1,
6453 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6454 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6455 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6456 (linenr_T)MAXLNUM);
6457 curwin->w_set_curswant = TRUE;
6458 }
6459 }
6460 else
6461#endif
6462
6463 /*
6464 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6465 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6466 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6467 * "[m" or "]m" search for prev/next start of (Java) method.
6468 * "[M" or "]M" search for prev/next end of (Java) method.
6469 */
6470 if ( (cap->cmdchar == '['
6471 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6472 || (cap->cmdchar == ']'
6473 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6474 {
6475 if (cap->nchar == '*')
6476 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 prev_pos.lnum = 0;
6478 if (cap->nchar == 'm' || cap->nchar == 'M')
6479 {
6480 if (cap->cmdchar == '[')
6481 findc = '{';
6482 else
6483 findc = '}';
6484 n = 9999;
6485 }
6486 else
6487 {
6488 findc = cap->nchar;
6489 n = cap->count1;
6490 }
6491 for ( ; n > 0; --n)
6492 {
6493 if ((pos = findmatchlimit(cap->oap, findc,
6494 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6495 {
6496 if (new_pos.lnum == 0) /* nothing found */
6497 {
6498 if (cap->nchar != 'm' && cap->nchar != 'M')
6499 clearopbeep(cap->oap);
6500 }
6501 else
6502 pos = &new_pos; /* use last one found */
6503 break;
6504 }
6505 prev_pos = new_pos;
6506 curwin->w_cursor = *pos;
6507 new_pos = *pos;
6508 }
6509 curwin->w_cursor = old_pos;
6510
6511 /*
6512 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6513 * brought us to the match for "[m" and "]M" when inside a method.
6514 * Try finding the '{' or '}' we want to be at.
6515 * Also repeat for the given count.
6516 */
6517 if (cap->nchar == 'm' || cap->nchar == 'M')
6518 {
6519 /* norm is TRUE for "]M" and "[m" */
6520 int norm = ((findc == '{') == (cap->nchar == 'm'));
6521
6522 n = cap->count1;
6523 /* found a match: we were inside a method */
6524 if (prev_pos.lnum != 0)
6525 {
6526 pos = &prev_pos;
6527 curwin->w_cursor = prev_pos;
6528 if (norm)
6529 --n;
6530 }
6531 else
6532 pos = NULL;
6533 while (n > 0)
6534 {
6535 for (;;)
6536 {
6537 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6538 {
6539 /* if not found anything, that's an error */
6540 if (pos == NULL)
6541 clearopbeep(cap->oap);
6542 n = 0;
6543 break;
6544 }
6545 c = gchar_cursor();
6546 if (c == '{' || c == '}')
6547 {
6548 /* Must have found end/start of class: use it.
6549 * Or found the place to be at. */
6550 if ((c == findc && norm) || (n == 1 && !norm))
6551 {
6552 new_pos = curwin->w_cursor;
6553 pos = &new_pos;
6554 n = 0;
6555 }
6556 /* if no match found at all, we started outside of the
6557 * class and we're inside now. Just go on. */
6558 else if (new_pos.lnum == 0)
6559 {
6560 new_pos = curwin->w_cursor;
6561 pos = &new_pos;
6562 }
6563 /* found start/end of other method: go to match */
6564 else if ((pos = findmatchlimit(cap->oap, findc,
6565 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6566 0)) == NULL)
6567 n = 0;
6568 else
6569 curwin->w_cursor = *pos;
6570 break;
6571 }
6572 }
6573 --n;
6574 }
6575 curwin->w_cursor = old_pos;
6576 if (pos == NULL && new_pos.lnum != 0)
6577 clearopbeep(cap->oap);
6578 }
6579 if (pos != NULL)
6580 {
6581 setpcmark();
6582 curwin->w_cursor = *pos;
6583 curwin->w_set_curswant = TRUE;
6584#ifdef FEAT_FOLDING
6585 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6586 && cap->oap->op_type == OP_NOP)
6587 foldOpenCursor();
6588#endif
6589 }
6590 }
6591
6592 /*
6593 * "[[", "[]", "]]" and "][": move to start or end of function
6594 */
6595 else if (cap->nchar == '[' || cap->nchar == ']')
6596 {
6597 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6598 flag = '{';
6599 else
6600 flag = '}'; /* "][" or "[]" */
6601
6602 curwin->w_set_curswant = TRUE;
6603 /*
6604 * Imitate strange Vi behaviour: When using "]]" with an operator
6605 * we also stop at '}'.
6606 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006607 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 (cap->oap->op_type != OP_NOP
6609 && cap->arg == FORWARD && flag == '{')))
6610 clearopbeep(cap->oap);
6611 else
6612 {
6613 if (cap->oap->op_type == OP_NOP)
6614 beginline(BL_WHITE | BL_FIX);
6615#ifdef FEAT_FOLDING
6616 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6617 foldOpenCursor();
6618#endif
6619 }
6620 }
6621
6622 /*
6623 * "[p", "[P", "]P" and "]p": put with indent adjustment
6624 */
6625 else if (cap->nchar == 'p' || cap->nchar == 'P')
6626 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006627 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 {
6629 prep_redo_cmd(cap);
6630 do_put(cap->oap->regname,
6631 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6632 cap->count1, PUT_FIXINDENT);
6633 }
6634 }
6635
6636 /*
6637 * "['", "[`", "]'" and "]`": jump to next mark
6638 */
6639 else if (cap->nchar == '\'' || cap->nchar == '`')
6640 {
6641 pos = &curwin->w_cursor;
6642 for (n = cap->count1; n > 0; --n)
6643 {
6644 prev_pos = *pos;
6645 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6646 cap->nchar == '\'');
6647 if (pos == NULL)
6648 break;
6649 }
6650 if (pos == NULL)
6651 pos = &prev_pos;
6652 nv_cursormark(cap, cap->nchar == '\'', pos);
6653 }
6654
6655#ifdef FEAT_MOUSE
6656 /*
6657 * [ or ] followed by a middle mouse click: put selected text with
6658 * indent adjustment. Any other button just does as usual.
6659 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006660 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 {
6662 (void)do_mouse(cap->oap, cap->nchar,
6663 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6664 cap->count1, PUT_FIXINDENT);
6665 }
6666#endif /* FEAT_MOUSE */
6667
6668#ifdef FEAT_FOLDING
6669 /*
6670 * "[z" and "]z": move to start or end of open fold.
6671 */
6672 else if (cap->nchar == 'z')
6673 {
6674 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6675 cap->count1) == FAIL)
6676 clearopbeep(cap->oap);
6677 }
6678#endif
6679
6680#ifdef FEAT_DIFF
6681 /*
6682 * "[c" and "]c": move to next or previous diff-change.
6683 */
6684 else if (cap->nchar == 'c')
6685 {
6686 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6687 cap->count1) == FAIL)
6688 clearopbeep(cap->oap);
6689 }
6690#endif
6691
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006692#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006693 /*
6694 * "[s", "[S", "]s" and "]S": move to next spell error.
6695 */
6696 else if (cap->nchar == 's' || cap->nchar == 'S')
6697 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006698 setpcmark();
6699 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006700 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6701 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006702 {
6703 clearopbeep(cap->oap);
6704 break;
6705 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006706# ifdef FEAT_FOLDING
6707 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6708 foldOpenCursor();
6709# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006710 }
6711#endif
6712
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 /* Not a valid cap->nchar. */
6714 else
6715 clearopbeep(cap->oap);
6716}
6717
6718/*
6719 * Handle Normal mode "%" command.
6720 */
6721 static void
6722nv_percent(cap)
6723 cmdarg_T *cap;
6724{
6725 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006726#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 linenr_T lnum = curwin->w_cursor.lnum;
6728#endif
6729
6730 cap->oap->inclusive = TRUE;
6731 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6732 {
6733 if (cap->count0 > 100)
6734 clearopbeep(cap->oap);
6735 else
6736 {
6737 cap->oap->motion_type = MLINE;
6738 setpcmark();
6739 /* Round up, so CTRL-G will give same value. Watch out for a
6740 * large line count, the line number must not go negative! */
6741 if (curbuf->b_ml.ml_line_count > 1000000)
6742 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6743 / 100L * cap->count0;
6744 else
6745 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6746 cap->count0 + 99L) / 100L;
6747 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6748 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6749 beginline(BL_SOL | BL_FIX);
6750 }
6751 }
6752 else /* "%" : go to matching paren */
6753 {
6754 cap->oap->motion_type = MCHAR;
6755 cap->oap->use_reg_one = TRUE;
6756 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6757 clearopbeep(cap->oap);
6758 else
6759 {
6760 setpcmark();
6761 curwin->w_cursor = *pos;
6762 curwin->w_set_curswant = TRUE;
6763#ifdef FEAT_VIRTUALEDIT
6764 curwin->w_cursor.coladd = 0;
6765#endif
6766#ifdef FEAT_VISUAL
6767 adjust_for_sel(cap);
6768#endif
6769 }
6770 }
6771#ifdef FEAT_FOLDING
6772 if (cap->oap->op_type == OP_NOP
6773 && lnum != curwin->w_cursor.lnum
6774 && (fdo_flags & FDO_PERCENT)
6775 && KeyTyped)
6776 foldOpenCursor();
6777#endif
6778}
6779
6780/*
6781 * Handle "(" and ")" commands.
6782 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6783 */
6784 static void
6785nv_brace(cap)
6786 cmdarg_T *cap;
6787{
6788 cap->oap->motion_type = MCHAR;
6789 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006790 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6791 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 curwin->w_set_curswant = TRUE;
6793
6794 if (findsent(cap->arg, cap->count1) == FAIL)
6795 clearopbeep(cap->oap);
6796 else
6797 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006798 /* Don't leave the cursor on the NUL past end of line. */
6799 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800#ifdef FEAT_VIRTUALEDIT
6801 curwin->w_cursor.coladd = 0;
6802#endif
6803#ifdef FEAT_FOLDING
6804 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6805 foldOpenCursor();
6806#endif
6807 }
6808}
6809
6810/*
6811 * "m" command: Mark a position.
6812 */
6813 static void
6814nv_mark(cap)
6815 cmdarg_T *cap;
6816{
6817 if (!checkclearop(cap->oap))
6818 {
6819 if (setmark(cap->nchar) == FAIL)
6820 clearopbeep(cap->oap);
6821 }
6822}
6823
6824/*
6825 * "{" and "}" commands.
6826 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6827 */
6828 static void
6829nv_findpar(cap)
6830 cmdarg_T *cap;
6831{
6832 cap->oap->motion_type = MCHAR;
6833 cap->oap->inclusive = FALSE;
6834 cap->oap->use_reg_one = TRUE;
6835 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006836 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 clearopbeep(cap->oap);
6838 else
6839 {
6840#ifdef FEAT_VIRTUALEDIT
6841 curwin->w_cursor.coladd = 0;
6842#endif
6843#ifdef FEAT_FOLDING
6844 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6845 foldOpenCursor();
6846#endif
6847 }
6848}
6849
6850/*
6851 * "u" command: Undo or make lower case.
6852 */
6853 static void
6854nv_undo(cap)
6855 cmdarg_T *cap;
6856{
6857 if (cap->oap->op_type == OP_LOWER
6858#ifdef FEAT_VISUAL
6859 || VIsual_active
6860#endif
6861 )
6862 {
6863 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6864 cap->cmdchar = 'g';
6865 cap->nchar = 'u';
6866 nv_operator(cap);
6867 }
6868 else
6869 nv_kundo(cap);
6870}
6871
6872/*
6873 * <Undo> command.
6874 */
6875 static void
6876nv_kundo(cap)
6877 cmdarg_T *cap;
6878{
6879 if (!checkclearopq(cap->oap))
6880 {
6881 u_undo((int)cap->count1);
6882 curwin->w_set_curswant = TRUE;
6883 }
6884}
6885
6886/*
6887 * Handle the "r" command.
6888 */
6889 static void
6890nv_replace(cap)
6891 cmdarg_T *cap;
6892{
6893 char_u *ptr;
6894 int had_ctrl_v;
6895 long n;
6896
6897 if (checkclearop(cap->oap))
6898 return;
6899
6900 /* get another character */
6901 if (cap->nchar == Ctrl_V)
6902 {
6903 had_ctrl_v = Ctrl_V;
6904 cap->nchar = get_literal();
6905 /* Don't redo a multibyte character with CTRL-V. */
6906 if (cap->nchar > DEL)
6907 had_ctrl_v = NUL;
6908 }
6909 else
6910 had_ctrl_v = NUL;
6911
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006912 /* Abort if the character is a special key. */
6913 if (IS_SPECIAL(cap->nchar))
6914 {
6915 clearopbeep(cap->oap);
6916 return;
6917 }
6918
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919#ifdef FEAT_VISUAL
6920 /* Visual mode "r" */
6921 if (VIsual_active)
6922 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006923 if (got_int)
6924 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 nv_operator(cap);
6926 return;
6927 }
6928#endif
6929
6930#ifdef FEAT_VIRTUALEDIT
6931 /* Break tabs, etc. */
6932 if (virtual_active())
6933 {
6934 if (u_save_cursor() == FAIL)
6935 return;
6936 if (gchar_cursor() == NUL)
6937 {
6938 /* Add extra space and put the cursor on the first one. */
6939 coladvance_force((colnr_T)(getviscol() + cap->count1));
6940 curwin->w_cursor.col -= cap->count1;
6941 }
6942 else if (gchar_cursor() == TAB)
6943 coladvance_force(getviscol());
6944 }
6945#endif
6946
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006947 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006949 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950#ifdef FEAT_MBYTE
6951 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6952#endif
6953 )
6954 {
6955 clearopbeep(cap->oap);
6956 return;
6957 }
6958
6959 /*
6960 * Replacing with a TAB is done by edit() when it is complicated because
6961 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6962 * Other characters are done below to avoid problems with things like
6963 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6964 */
6965 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6966 {
6967 stuffnumReadbuff(cap->count1);
6968 stuffcharReadbuff('R');
6969 stuffcharReadbuff('\t');
6970 stuffcharReadbuff(ESC);
6971 return;
6972 }
6973
6974 /* save line for undo */
6975 if (u_save_cursor() == FAIL)
6976 return;
6977
6978 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6979 {
6980 /*
6981 * Replace character(s) by a single newline.
6982 * Strange vi behaviour: Only one newline is inserted.
6983 * Delete the characters here.
6984 * Insert the newline with an insert command, takes care of
6985 * autoindent. The insert command depends on being on the last
6986 * character of a line or not.
6987 */
6988#ifdef FEAT_MBYTE
6989 (void)del_chars(cap->count1, FALSE); /* delete the characters */
6990#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00006991 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992#endif
6993 stuffcharReadbuff('\r');
6994 stuffcharReadbuff(ESC);
6995
6996 /* Give 'r' to edit(), to get the redo command right. */
6997 invoke_edit(cap, TRUE, 'r', FALSE);
6998 }
6999 else
7000 {
7001 prep_redo(cap->oap->regname, cap->count1,
7002 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7003
7004 curbuf->b_op_start = curwin->w_cursor;
7005#ifdef FEAT_MBYTE
7006 if (has_mbyte)
7007 {
7008 int old_State = State;
7009
7010 if (cap->ncharC1 != 0)
7011 AppendCharToRedobuff(cap->ncharC1);
7012 if (cap->ncharC2 != 0)
7013 AppendCharToRedobuff(cap->ncharC2);
7014
7015 /* This is slow, but it handles replacing a single-byte with a
7016 * multi-byte and the other way around. Also handles adding
7017 * composing characters for utf-8. */
7018 for (n = cap->count1; n > 0; --n)
7019 {
7020 State = REPLACE;
7021 ins_char(cap->nchar);
7022 State = old_State;
7023 if (cap->ncharC1 != 0)
7024 ins_char(cap->ncharC1);
7025 if (cap->ncharC2 != 0)
7026 ins_char(cap->ncharC2);
7027 }
7028 }
7029 else
7030#endif
7031 {
7032 /*
7033 * Replace the characters within one line.
7034 */
7035 for (n = cap->count1; n > 0; --n)
7036 {
7037 /*
7038 * Get ptr again, because u_save and/or showmatch() will have
7039 * released the line. At the same time we let know that the
7040 * line will be changed.
7041 */
7042 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
7043 ptr[curwin->w_cursor.col] = cap->nchar;
7044 if (p_sm && msg_silent == 0)
7045 showmatch(cap->nchar);
7046 ++curwin->w_cursor.col;
7047 }
7048#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007049 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007051 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052
Bram Moolenaar009b2592004-10-24 19:18:58 +00007053 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007054 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007056 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 }
7058#endif
7059
7060 /* mark the buffer as changed and prepare for displaying */
7061 changed_bytes(curwin->w_cursor.lnum,
7062 (colnr_T)(curwin->w_cursor.col - cap->count1));
7063 }
7064 --curwin->w_cursor.col; /* cursor on the last replaced char */
7065#ifdef FEAT_MBYTE
7066 /* if the character on the left of the current cursor is a multi-byte
7067 * character, move two characters left */
7068 if (has_mbyte)
7069 mb_adjust_cursor();
7070#endif
7071 curbuf->b_op_end = curwin->w_cursor;
7072 curwin->w_set_curswant = TRUE;
7073 set_last_insert(cap->nchar);
7074 }
7075}
7076
7077#ifdef FEAT_VISUAL
7078/*
7079 * 'o': Exchange start and end of Visual area.
7080 * 'O': same, but in block mode exchange left and right corners.
7081 */
7082 static void
7083v_swap_corners(cmdchar)
7084 int cmdchar;
7085{
7086 pos_T old_cursor;
7087 colnr_T left, right;
7088
7089 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7090 {
7091 old_cursor = curwin->w_cursor;
7092 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7093 curwin->w_cursor.lnum = VIsual.lnum;
7094 coladvance(left);
7095 VIsual = curwin->w_cursor;
7096
7097 curwin->w_cursor.lnum = old_cursor.lnum;
7098 curwin->w_curswant = right;
7099 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7100 * right one column */
7101 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7102 ++curwin->w_curswant;
7103 coladvance(curwin->w_curswant);
7104 if (curwin->w_cursor.col == old_cursor.col
7105#ifdef FEAT_VIRTUALEDIT
7106 && (!virtual_active()
7107 || curwin->w_cursor.coladd == old_cursor.coladd)
7108#endif
7109 )
7110 {
7111 curwin->w_cursor.lnum = VIsual.lnum;
7112 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7113 ++right;
7114 coladvance(right);
7115 VIsual = curwin->w_cursor;
7116
7117 curwin->w_cursor.lnum = old_cursor.lnum;
7118 coladvance(left);
7119 curwin->w_curswant = left;
7120 }
7121 }
7122 else
7123 {
7124 old_cursor = curwin->w_cursor;
7125 curwin->w_cursor = VIsual;
7126 VIsual = old_cursor;
7127 curwin->w_set_curswant = TRUE;
7128 }
7129}
7130#endif /* FEAT_VISUAL */
7131
7132/*
7133 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7134 */
7135 static void
7136nv_Replace(cap)
7137 cmdarg_T *cap;
7138{
7139#ifdef FEAT_VISUAL
7140 if (VIsual_active) /* "R" is replace lines */
7141 {
7142 cap->cmdchar = 'c';
7143 cap->nchar = NUL;
7144 VIsual_mode = 'V';
7145 nv_operator(cap);
7146 }
7147 else
7148#endif
7149 if (!checkclearopq(cap->oap))
7150 {
7151 if (!curbuf->b_p_ma)
7152 EMSG(_(e_modifiable));
7153 else
7154 {
7155#ifdef FEAT_VIRTUALEDIT
7156 if (virtual_active())
7157 coladvance(getviscol());
7158#endif
7159 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7160 }
7161 }
7162}
7163
7164#ifdef FEAT_VREPLACE
7165/*
7166 * "gr".
7167 */
7168 static void
7169nv_vreplace(cap)
7170 cmdarg_T *cap;
7171{
7172# ifdef FEAT_VISUAL
7173 if (VIsual_active)
7174 {
7175 cap->cmdchar = 'r';
7176 cap->nchar = cap->extra_char;
7177 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7178 }
7179 else
7180# endif
7181 if (!checkclearopq(cap->oap))
7182 {
7183 if (!curbuf->b_p_ma)
7184 EMSG(_(e_modifiable));
7185 else
7186 {
7187 if (cap->extra_char == Ctrl_V) /* get another character */
7188 cap->extra_char = get_literal();
7189 stuffcharReadbuff(cap->extra_char);
7190 stuffcharReadbuff(ESC);
7191# ifdef FEAT_VIRTUALEDIT
7192 if (virtual_active())
7193 coladvance(getviscol());
7194# endif
7195 invoke_edit(cap, TRUE, 'v', FALSE);
7196 }
7197 }
7198}
7199#endif
7200
7201/*
7202 * Swap case for "~" command, when it does not work like an operator.
7203 */
7204 static void
7205n_swapchar(cap)
7206 cmdarg_T *cap;
7207{
7208 long n;
7209 pos_T startpos;
7210 int did_change = 0;
7211#ifdef FEAT_NETBEANS_INTG
7212 pos_T pos;
7213 char_u *ptr;
7214 int count;
7215#endif
7216
7217 if (checkclearopq(cap->oap))
7218 return;
7219
7220 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7221 {
7222 clearopbeep(cap->oap);
7223 return;
7224 }
7225
7226 prep_redo_cmd(cap);
7227
7228 if (u_save_cursor() == FAIL)
7229 return;
7230
7231 startpos = curwin->w_cursor;
7232#ifdef FEAT_NETBEANS_INTG
7233 pos = startpos;
7234#endif
7235 for (n = cap->count1; n > 0; --n)
7236 {
7237 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7238 inc_cursor();
7239 if (gchar_cursor() == NUL)
7240 {
7241 if (vim_strchr(p_ww, '~') != NULL
7242 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7243 {
7244#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007245 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 {
7247 if (did_change)
7248 {
7249 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007250 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007251 netbeans_removed(curbuf, pos.lnum, pos.col,
7252 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007254 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 }
7256 pos.col = 0;
7257 pos.lnum++;
7258 }
7259#endif
7260 ++curwin->w_cursor.lnum;
7261 curwin->w_cursor.col = 0;
7262 if (n > 1)
7263 {
7264 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7265 break;
7266 u_clearline();
7267 }
7268 }
7269 else
7270 break;
7271 }
7272 }
7273#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007274 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 {
7276 ptr = ml_get(pos.lnum);
7277 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007278 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7279 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 }
7281#endif
7282
7283
7284 check_cursor();
7285 curwin->w_set_curswant = TRUE;
7286 if (did_change)
7287 {
7288 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7289 0L);
7290 curbuf->b_op_start = startpos;
7291 curbuf->b_op_end = curwin->w_cursor;
7292 if (curbuf->b_op_end.col > 0)
7293 --curbuf->b_op_end.col;
7294 }
7295}
7296
7297/*
7298 * Move cursor to mark.
7299 */
7300 static void
7301nv_cursormark(cap, flag, pos)
7302 cmdarg_T *cap;
7303 int flag;
7304 pos_T *pos;
7305{
7306 if (check_mark(pos) == FAIL)
7307 clearop(cap->oap);
7308 else
7309 {
7310 if (cap->cmdchar == '\''
7311 || cap->cmdchar == '`'
7312 || cap->cmdchar == '['
7313 || cap->cmdchar == ']')
7314 setpcmark();
7315 curwin->w_cursor = *pos;
7316 if (flag)
7317 beginline(BL_WHITE | BL_FIX);
7318 else
7319 check_cursor();
7320 }
7321 cap->oap->motion_type = flag ? MLINE : MCHAR;
7322 if (cap->cmdchar == '`')
7323 cap->oap->use_reg_one = TRUE;
7324 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7325 curwin->w_set_curswant = TRUE;
7326}
7327
7328#ifdef FEAT_VISUAL
7329/*
7330 * Handle commands that are operators in Visual mode.
7331 */
7332 static void
7333v_visop(cap)
7334 cmdarg_T *cap;
7335{
7336 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7337
7338 /* Uppercase means linewise, except in block mode, then "D" deletes till
7339 * the end of the line, and "C" replaces til EOL */
7340 if (isupper(cap->cmdchar))
7341 {
7342 if (VIsual_mode != Ctrl_V)
7343 VIsual_mode = 'V';
7344 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7345 curwin->w_curswant = MAXCOL;
7346 }
7347 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7348 nv_operator(cap);
7349}
7350#endif
7351
7352/*
7353 * "s" and "S" commands.
7354 */
7355 static void
7356nv_subst(cap)
7357 cmdarg_T *cap;
7358{
7359#ifdef FEAT_VISUAL
7360 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7361 {
7362 if (cap->cmdchar == 'S')
7363 VIsual_mode = 'V';
7364 cap->cmdchar = 'c';
7365 nv_operator(cap);
7366 }
7367 else
7368#endif
7369 nv_optrans(cap);
7370}
7371
7372/*
7373 * Abbreviated commands.
7374 */
7375 static void
7376nv_abbrev(cap)
7377 cmdarg_T *cap;
7378{
7379 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7380 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7381
7382#ifdef FEAT_VISUAL
7383 /* in Visual mode these commands are operators */
7384 if (VIsual_active)
7385 v_visop(cap);
7386 else
7387#endif
7388 nv_optrans(cap);
7389}
7390
7391/*
7392 * Translate a command into another command.
7393 */
7394 static void
7395nv_optrans(cap)
7396 cmdarg_T *cap;
7397{
7398 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7399 (char_u *)"d$", (char_u *)"c$",
7400 (char_u *)"cl", (char_u *)"cc",
7401 (char_u *)"yy", (char_u *)":s\r"};
7402 static char_u *str = (char_u *)"xXDCsSY&";
7403
7404 if (!checkclearopq(cap->oap))
7405 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007406 /* In Vi "2D" doesn't delete the next line. Can't translate it
7407 * either, because "2." should also not use the count. */
7408 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7409 {
7410 cap->oap->start = curwin->w_cursor;
7411 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007412#ifdef FEAT_EVAL
7413 set_op_var(OP_DELETE);
7414#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007415 cap->count1 = 1;
7416 nv_dollar(cap);
7417 finish_op = TRUE;
7418 ResetRedobuff();
7419 AppendCharToRedobuff('D');
7420 }
7421 else
7422 {
7423 if (cap->count0)
7424 stuffnumReadbuff(cap->count0);
7425 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 }
7428 cap->opcount = 0;
7429}
7430
7431/*
7432 * "'" and "`" commands. Also for "g'" and "g`".
7433 * cap->arg is TRUE for "'" and "g'".
7434 */
7435 static void
7436nv_gomark(cap)
7437 cmdarg_T *cap;
7438{
7439 pos_T *pos;
7440 int c;
7441#ifdef FEAT_FOLDING
7442 linenr_T lnum = curwin->w_cursor.lnum;
7443 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7444#endif
7445
7446 if (cap->cmdchar == 'g')
7447 c = cap->extra_char;
7448 else
7449 c = cap->nchar;
7450 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7451 if (pos == (pos_T *)-1) /* jumped to other file */
7452 {
7453 if (cap->arg)
7454 {
7455 check_cursor_lnum();
7456 beginline(BL_WHITE | BL_FIX);
7457 }
7458 else
7459 check_cursor();
7460 }
7461 else
7462 nv_cursormark(cap, cap->arg, pos);
7463
7464#ifdef FEAT_VIRTUALEDIT
7465 /* May need to clear the coladd that a mark includes. */
7466 if (!virtual_active())
7467 curwin->w_cursor.coladd = 0;
7468#endif
7469#ifdef FEAT_FOLDING
7470 if (cap->oap->op_type == OP_NOP
7471 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7472 && (fdo_flags & FDO_MARK)
7473 && old_KeyTyped)
7474 foldOpenCursor();
7475#endif
7476}
7477
7478/*
7479 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7480 */
7481 static void
7482nv_pcmark(cap)
7483 cmdarg_T *cap;
7484{
7485#ifdef FEAT_JUMPLIST
7486 pos_T *pos;
7487# ifdef FEAT_FOLDING
7488 linenr_T lnum = curwin->w_cursor.lnum;
7489 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7490# endif
7491
7492 if (!checkclearopq(cap->oap))
7493 {
7494 if (cap->cmdchar == 'g')
7495 pos = movechangelist((int)cap->count1);
7496 else
7497 pos = movemark((int)cap->count1);
7498 if (pos == (pos_T *)-1) /* jump to other file */
7499 {
7500 curwin->w_set_curswant = TRUE;
7501 check_cursor();
7502 }
7503 else if (pos != NULL) /* can jump */
7504 nv_cursormark(cap, FALSE, pos);
7505 else if (cap->cmdchar == 'g')
7506 {
7507 if (curbuf->b_changelistlen == 0)
7508 EMSG(_("E664: changelist is empty"));
7509 else if (cap->count1 < 0)
7510 EMSG(_("E662: At start of changelist"));
7511 else
7512 EMSG(_("E663: At end of changelist"));
7513 }
7514 else
7515 clearopbeep(cap->oap);
7516# ifdef FEAT_FOLDING
7517 if (cap->oap->op_type == OP_NOP
7518 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7519 && (fdo_flags & FDO_MARK)
7520 && old_KeyTyped)
7521 foldOpenCursor();
7522# endif
7523 }
7524#else
7525 clearopbeep(cap->oap);
7526#endif
7527}
7528
7529/*
7530 * Handle '"' command.
7531 */
7532 static void
7533nv_regname(cap)
7534 cmdarg_T *cap;
7535{
7536 if (checkclearop(cap->oap))
7537 return;
7538#ifdef FEAT_EVAL
7539 if (cap->nchar == '=')
7540 cap->nchar = get_expr_register();
7541#endif
7542 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7543 {
7544 cap->oap->regname = cap->nchar;
7545 cap->opcount = cap->count0; /* remember count before '"' */
7546#ifdef FEAT_EVAL
7547 set_reg_var(cap->oap->regname);
7548#endif
7549 }
7550 else
7551 clearopbeep(cap->oap);
7552}
7553
7554#ifdef FEAT_VISUAL
7555/*
7556 * Handle "v", "V" and "CTRL-V" commands.
7557 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7558 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007559 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 */
7561 static void
7562nv_visual(cap)
7563 cmdarg_T *cap;
7564{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007565 if (cap->cmdchar == Ctrl_Q)
7566 cap->cmdchar = Ctrl_V;
7567
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7569 * characterwise, linewise, or blockwise. */
7570 if (cap->oap->op_type != OP_NOP)
7571 {
7572 cap->oap->motion_force = cap->cmdchar;
7573 finish_op = FALSE; /* operator doesn't finish now but later */
7574 return;
7575 }
7576
7577 VIsual_select = cap->arg;
7578 if (VIsual_active) /* change Visual mode */
7579 {
7580 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7581 end_visual_mode();
7582 else /* toggle char/block mode */
7583 { /* or char/line mode */
7584 VIsual_mode = cap->cmdchar;
7585 showmode();
7586 }
7587 redraw_curbuf_later(INVERTED); /* update the inversion */
7588 }
7589 else /* start Visual mode */
7590 {
7591 check_visual_highlight();
7592 if (cap->count0) /* use previously selected part */
7593 {
7594 if (resel_VIsual_mode == NUL) /* there is none */
7595 {
7596 beep_flush();
7597 return;
7598 }
7599 VIsual = curwin->w_cursor;
7600
7601 VIsual_active = TRUE;
7602 VIsual_reselect = TRUE;
7603 if (!cap->arg)
7604 /* start Select mode when 'selectmode' contains "cmd" */
7605 may_start_select('c');
7606#ifdef FEAT_MOUSE
7607 setmouse();
7608#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007609 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 redraw_cmdline = TRUE; /* show visual mode later */
7611 /*
7612 * For V and ^V, we multiply the number of lines even if there
7613 * was only one -- webb
7614 */
7615 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7616 {
7617 curwin->w_cursor.lnum +=
7618 resel_VIsual_line_count * cap->count0 - 1;
7619 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7620 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7621 }
7622 VIsual_mode = resel_VIsual_mode;
7623 if (VIsual_mode == 'v')
7624 {
7625 if (resel_VIsual_line_count <= 1)
7626 curwin->w_cursor.col += resel_VIsual_col * cap->count0 - 1;
7627 else
7628 curwin->w_cursor.col = resel_VIsual_col;
7629 check_cursor_col();
7630 }
7631 if (resel_VIsual_col == MAXCOL)
7632 {
7633 curwin->w_curswant = MAXCOL;
7634 coladvance((colnr_T)MAXCOL);
7635 }
7636 else if (VIsual_mode == Ctrl_V)
7637 {
7638 validate_virtcol();
7639 curwin->w_curswant = curwin->w_virtcol
7640 + resel_VIsual_col * cap->count0 - 1;
7641 coladvance(curwin->w_curswant);
7642 }
7643 else
7644 curwin->w_set_curswant = TRUE;
7645 redraw_curbuf_later(INVERTED); /* show the inversion */
7646 }
7647 else
7648 {
7649 if (!cap->arg)
7650 /* start Select mode when 'selectmode' contains "cmd" */
7651 may_start_select('c');
7652 n_start_visual_mode(cap->cmdchar);
7653 }
7654 }
7655}
7656
7657/*
7658 * Start selection for Shift-movement keys.
7659 */
7660 void
7661start_selection()
7662{
7663 /* if 'selectmode' contains "key", start Select mode */
7664 may_start_select('k');
7665 n_start_visual_mode('v');
7666}
7667
7668/*
7669 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7670 */
7671 void
7672may_start_select(c)
7673 int c;
7674{
7675 VIsual_select = (stuff_empty() && typebuf_typed()
7676 && (vim_strchr(p_slm, c) != NULL));
7677}
7678
7679/*
7680 * Start Visual mode "c".
7681 * Should set VIsual_select before calling this.
7682 */
7683 static void
7684n_start_visual_mode(c)
7685 int c;
7686{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007687#ifdef FEAT_CONCEAL
7688 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007689 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007690#endif
7691
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 VIsual_mode = c;
7693 VIsual_active = TRUE;
7694 VIsual_reselect = TRUE;
7695#ifdef FEAT_VIRTUALEDIT
7696 /* Corner case: the 0 position in a tab may change when going into
7697 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7698 */
7699 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7700 coladvance(curwin->w_virtcol);
7701#endif
7702 VIsual = curwin->w_cursor;
7703
7704#ifdef FEAT_FOLDING
7705 foldAdjustVisual();
7706#endif
7707
7708#ifdef FEAT_MOUSE
7709 setmouse();
7710#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007711#ifdef FEAT_CONCEAL
7712 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007713 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007714#endif
7715
Bram Moolenaar09df3122006-01-23 22:23:09 +00007716 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 redraw_cmdline = TRUE; /* show visual mode later */
7718#ifdef FEAT_CLIPBOARD
7719 /* Make sure the clipboard gets updated. Needed because start and
7720 * end may still be the same, and the selection needs to be owned */
7721 clip_star.vmode = NUL;
7722#endif
7723
7724 /* Only need to redraw this line, unless still need to redraw an old
7725 * Visual area (when 'lazyredraw' is set). */
7726 if (curwin->w_redr_type < INVERTED)
7727 {
7728 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7729 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7730 }
7731}
7732
7733#endif /* FEAT_VISUAL */
7734
7735/*
7736 * CTRL-W: Window commands
7737 */
7738 static void
7739nv_window(cap)
7740 cmdarg_T *cap;
7741{
7742#ifdef FEAT_WINDOWS
7743 if (!checkclearop(cap->oap))
7744 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7745#else
7746 (void)checkclearop(cap->oap);
7747#endif
7748}
7749
7750/*
7751 * CTRL-Z: Suspend
7752 */
7753 static void
7754nv_suspend(cap)
7755 cmdarg_T *cap;
7756{
7757 clearop(cap->oap);
7758#ifdef FEAT_VISUAL
7759 if (VIsual_active)
7760 end_visual_mode(); /* stop Visual mode */
7761#endif
7762 do_cmdline_cmd((char_u *)"st");
7763}
7764
7765/*
7766 * Commands starting with "g".
7767 */
7768 static void
7769nv_g_cmd(cap)
7770 cmdarg_T *cap;
7771{
7772 oparg_T *oap = cap->oap;
7773#ifdef FEAT_VISUAL
7774 pos_T tpos;
7775#endif
7776 int i;
7777 int flag = FALSE;
7778
7779 switch (cap->nchar)
7780 {
7781#ifdef MEM_PROFILE
7782 /*
7783 * "g^A": dump log of used memory.
7784 */
7785 case Ctrl_A:
7786 vim_mem_profile_dump();
7787 break;
7788#endif
7789
7790#ifdef FEAT_VREPLACE
7791 /*
7792 * "gR": Enter virtual replace mode.
7793 */
7794 case 'R':
7795 cap->arg = TRUE;
7796 nv_Replace(cap);
7797 break;
7798
7799 case 'r':
7800 nv_vreplace(cap);
7801 break;
7802#endif
7803
7804 case '&':
7805 do_cmdline_cmd((char_u *)"%s//~/&");
7806 break;
7807
7808#ifdef FEAT_VISUAL
7809 /*
7810 * "gv": Reselect the previous Visual area. If Visual already active,
7811 * exchange previous and current Visual area.
7812 */
7813 case 'v':
7814 if (checkclearop(oap))
7815 break;
7816
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007817 if ( curbuf->b_visual.vi_start.lnum == 0
7818 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7819 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 beep_flush();
7821 else
7822 {
7823 /* set w_cursor to the start of the Visual area, tpos to the end */
7824 if (VIsual_active)
7825 {
7826 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007827 VIsual_mode = curbuf->b_visual.vi_mode;
7828 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829# ifdef FEAT_EVAL
7830 curbuf->b_visual_mode_eval = i;
7831# endif
7832 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007833 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7834 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007836 tpos = curbuf->b_visual.vi_end;
7837 curbuf->b_visual.vi_end = curwin->w_cursor;
7838 curwin->w_cursor = curbuf->b_visual.vi_start;
7839 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 }
7841 else
7842 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007843 VIsual_mode = curbuf->b_visual.vi_mode;
7844 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7845 tpos = curbuf->b_visual.vi_end;
7846 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 }
7848
7849 VIsual_active = TRUE;
7850 VIsual_reselect = TRUE;
7851
7852 /* Set Visual to the start and w_cursor to the end of the Visual
7853 * area. Make sure they are on an existing character. */
7854 check_cursor();
7855 VIsual = curwin->w_cursor;
7856 curwin->w_cursor = tpos;
7857 check_cursor();
7858 update_topline();
7859 /*
7860 * When called from normal "g" command: start Select mode when
7861 * 'selectmode' contains "cmd". When called for K_SELECT, always
7862 * start Select mode.
7863 */
7864 if (cap->arg)
7865 VIsual_select = TRUE;
7866 else
7867 may_start_select('c');
7868#ifdef FEAT_MOUSE
7869 setmouse();
7870#endif
7871#ifdef FEAT_CLIPBOARD
7872 /* Make sure the clipboard gets updated. Needed because start and
7873 * end are still the same, and the selection needs to be owned */
7874 clip_star.vmode = NUL;
7875#endif
7876 redraw_curbuf_later(INVERTED);
7877 showmode();
7878 }
7879 break;
7880 /*
7881 * "gV": Don't reselect the previous Visual area after a Select mode
7882 * mapping of menu.
7883 */
7884 case 'V':
7885 VIsual_reselect = FALSE;
7886 break;
7887
7888 /*
7889 * "gh": start Select mode.
7890 * "gH": start Select line mode.
7891 * "g^H": start Select block mode.
7892 */
7893 case K_BS:
7894 cap->nchar = Ctrl_H;
7895 /* FALLTHROUGH */
7896 case 'h':
7897 case 'H':
7898 case Ctrl_H:
7899# ifdef EBCDIC
7900 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7901 if (cap->nchar == Ctrl_H)
7902 cap->cmdchar = Ctrl_V;
7903 else
7904# endif
7905 cap->cmdchar = cap->nchar + ('v' - 'h');
7906 cap->arg = TRUE;
7907 nv_visual(cap);
7908 break;
7909#endif /* FEAT_VISUAL */
7910
7911 /*
7912 * "gj" and "gk" two new funny movement keys -- up and down
7913 * movement based on *screen* line rather than *file* line.
7914 */
7915 case 'j':
7916 case K_DOWN:
7917 /* with 'nowrap' it works just like the normal "j" command; also when
7918 * in a closed fold */
7919 if (!curwin->w_p_wrap
7920#ifdef FEAT_FOLDING
7921 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7922#endif
7923 )
7924 {
7925 oap->motion_type = MLINE;
7926 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7927 }
7928 else
7929 i = nv_screengo(oap, FORWARD, cap->count1);
7930 if (i == FAIL)
7931 clearopbeep(oap);
7932 break;
7933
7934 case 'k':
7935 case K_UP:
7936 /* with 'nowrap' it works just like the normal "k" command; also when
7937 * in a closed fold */
7938 if (!curwin->w_p_wrap
7939#ifdef FEAT_FOLDING
7940 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7941#endif
7942 )
7943 {
7944 oap->motion_type = MLINE;
7945 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7946 }
7947 else
7948 i = nv_screengo(oap, BACKWARD, cap->count1);
7949 if (i == FAIL)
7950 clearopbeep(oap);
7951 break;
7952
7953 /*
7954 * "gJ": join two lines without inserting a space.
7955 */
7956 case 'J':
7957 nv_join(cap);
7958 break;
7959
7960 /*
7961 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7962 * "gm": middle of "g0" and "g$".
7963 */
7964 case '^':
7965 flag = TRUE;
7966 /* FALLTHROUGH */
7967
7968 case '0':
7969 case 'm':
7970 case K_HOME:
7971 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 oap->motion_type = MCHAR;
7973 oap->inclusive = FALSE;
7974 if (curwin->w_p_wrap
7975#ifdef FEAT_VERTSPLIT
7976 && curwin->w_width != 0
7977#endif
7978 )
7979 {
7980 int width1 = W_WIDTH(curwin) - curwin_col_off();
7981 int width2 = width1 + curwin_col_off2();
7982
7983 validate_virtcol();
7984 i = 0;
7985 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
7986 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
7987 }
7988 else
7989 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02007990 /* Go to the middle of the screen line. When 'number' or
7991 * 'relativenumber' is on and lines are wrapping the middle can be more
7992 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 if (cap->nchar == 'm')
7994 i += (W_WIDTH(curwin) - curwin_col_off()
7995 + ((curwin->w_p_wrap && i > 0)
7996 ? curwin_col_off2() : 0)) / 2;
7997 coladvance((colnr_T)i);
7998 if (flag)
7999 {
8000 do
8001 i = gchar_cursor();
8002 while (vim_iswhite(i) && oneright() == OK);
8003 }
8004 curwin->w_set_curswant = TRUE;
8005 break;
8006
8007 case '_':
8008 /* "g_": to the last non-blank character in the line or <count> lines
8009 * downward. */
8010 cap->oap->motion_type = MCHAR;
8011 cap->oap->inclusive = TRUE;
8012 curwin->w_curswant = MAXCOL;
8013 if (cursor_down((long)(cap->count1 - 1),
8014 cap->oap->op_type == OP_NOP) == FAIL)
8015 clearopbeep(cap->oap);
8016 else
8017 {
8018 char_u *ptr = ml_get_curline();
8019
8020 /* In Visual mode we may end up after the line. */
8021 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8022 --curwin->w_cursor.col;
8023
8024 /* Decrease the cursor column until it's on a non-blank. */
8025 while (curwin->w_cursor.col > 0
8026 && vim_iswhite(ptr[curwin->w_cursor.col]))
8027 --curwin->w_cursor.col;
8028 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008029#ifdef FEAT_VISUAL
8030 adjust_for_sel(cap);
8031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 }
8033 break;
8034
8035 case '$':
8036 case K_END:
8037 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 {
8039 int col_off = curwin_col_off();
8040
8041 oap->motion_type = MCHAR;
8042 oap->inclusive = TRUE;
8043 if (curwin->w_p_wrap
8044#ifdef FEAT_VERTSPLIT
8045 && curwin->w_width != 0
8046#endif
8047 )
8048 {
8049 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8050 if (cap->count1 == 1)
8051 {
8052 int width1 = W_WIDTH(curwin) - col_off;
8053 int width2 = width1 + curwin_col_off2();
8054
8055 validate_virtcol();
8056 i = width1 - 1;
8057 if (curwin->w_virtcol >= (colnr_T)width1)
8058 i += ((curwin->w_virtcol - width1) / width2 + 1)
8059 * width2;
8060 coladvance((colnr_T)i);
8061#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8062 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8063 {
8064 /*
8065 * Check for landing on a character that got split at
8066 * the end of the line. We do not want to advance to
8067 * the next screen line.
8068 */
8069 validate_virtcol();
8070 if (curwin->w_virtcol > (colnr_T)i)
8071 --curwin->w_cursor.col;
8072 }
8073#endif
8074 }
8075 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8076 clearopbeep(oap);
8077 }
8078 else
8079 {
8080 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8081 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008082
8083 /* Make sure we stick in this column. */
8084 validate_virtcol();
8085 curwin->w_curswant = curwin->w_virtcol;
8086 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 }
8088 }
8089 break;
8090
8091 /*
8092 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8093 */
8094 case '*':
8095 case '#':
8096#if POUND != '#'
8097 case POUND: /* pound sign (sometimes equal to '#') */
8098#endif
8099 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8100 case ']': /* :tselect for current identifier */
8101 nv_ident(cap);
8102 break;
8103
8104 /*
8105 * ge and gE: go back to end of word
8106 */
8107 case 'e':
8108 case 'E':
8109 oap->motion_type = MCHAR;
8110 curwin->w_set_curswant = TRUE;
8111 oap->inclusive = TRUE;
8112 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8113 clearopbeep(oap);
8114 break;
8115
8116 /*
8117 * "g CTRL-G": display info about cursor position
8118 */
8119 case Ctrl_G:
8120 cursor_pos_info();
8121 break;
8122
8123 /*
8124 * "gi": start Insert at the last position.
8125 */
8126 case 'i':
8127 if (curbuf->b_last_insert.lnum != 0)
8128 {
8129 curwin->w_cursor = curbuf->b_last_insert;
8130 check_cursor_lnum();
8131 i = (int)STRLEN(ml_get_curline());
8132 if (curwin->w_cursor.col > (colnr_T)i)
8133 {
8134#ifdef FEAT_VIRTUALEDIT
8135 if (virtual_active())
8136 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8137#endif
8138 curwin->w_cursor.col = i;
8139 }
8140 }
8141 cap->cmdchar = 'i';
8142 nv_edit(cap);
8143 break;
8144
8145 /*
8146 * "gI": Start insert in column 1.
8147 */
8148 case 'I':
8149 beginline(0);
8150 if (!checkclearopq(oap))
8151 invoke_edit(cap, FALSE, 'g', FALSE);
8152 break;
8153
8154#ifdef FEAT_SEARCHPATH
8155 /*
8156 * "gf": goto file, edit file under cursor
8157 * "]f" and "[f": can also be used.
8158 */
8159 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008160 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 nv_gotofile(cap);
8162 break;
8163#endif
8164
8165 /* "g'm" and "g`m": jump to mark without setting pcmark */
8166 case '\'':
8167 cap->arg = TRUE;
8168 /*FALLTHROUGH*/
8169 case '`':
8170 nv_gomark(cap);
8171 break;
8172
8173 /*
8174 * "gs": Goto sleep.
8175 */
8176 case 's':
8177 do_sleep(cap->count1 * 1000L);
8178 break;
8179
8180 /*
8181 * "ga": Display the ascii value of the character under the
8182 * cursor. It is displayed in decimal, hex, and octal. -- webb
8183 */
8184 case 'a':
8185 do_ascii(NULL);
8186 break;
8187
8188#ifdef FEAT_MBYTE
8189 /*
8190 * "g8": Display the bytes used for the UTF-8 character under the
8191 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008192 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 */
8194 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008195 if (cap->count0 == 8)
8196 utf_find_illegal();
8197 else
8198 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 break;
8200#endif
8201
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008202 case '<':
8203 show_sb_text();
8204 break;
8205
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 /*
8207 * "gg": Goto the first line in file. With a count it goes to
8208 * that line number like for "G". -- webb
8209 */
8210 case 'g':
8211 cap->arg = FALSE;
8212 nv_goto(cap);
8213 break;
8214
8215 /*
8216 * Two-character operators:
8217 * "gq" Format text
8218 * "gw" Format text and keep cursor position
8219 * "g~" Toggle the case of the text.
8220 * "gu" Change text to lower case.
8221 * "gU" Change text to upper case.
8222 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008223 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 */
8225 case 'q':
8226 case 'w':
8227 oap->cursor_start = curwin->w_cursor;
8228 /*FALLTHROUGH*/
8229 case '~':
8230 case 'u':
8231 case 'U':
8232 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008233 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 nv_operator(cap);
8235 break;
8236
8237 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008238 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 * current function
8240 * "gD": idem, but in the current file.
8241 */
8242 case 'd':
8243 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008244 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 break;
8246
8247#ifdef FEAT_MOUSE
8248 /*
8249 * g<*Mouse> : <C-*mouse>
8250 */
8251 case K_MIDDLEMOUSE:
8252 case K_MIDDLEDRAG:
8253 case K_MIDDLERELEASE:
8254 case K_LEFTMOUSE:
8255 case K_LEFTDRAG:
8256 case K_LEFTRELEASE:
8257 case K_RIGHTMOUSE:
8258 case K_RIGHTDRAG:
8259 case K_RIGHTRELEASE:
8260 case K_X1MOUSE:
8261 case K_X1DRAG:
8262 case K_X1RELEASE:
8263 case K_X2MOUSE:
8264 case K_X2DRAG:
8265 case K_X2RELEASE:
8266 mod_mask = MOD_MASK_CTRL;
8267 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8268 break;
8269#endif
8270
8271 case K_IGNORE:
8272 break;
8273
8274 /*
8275 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8276 */
8277 case 'p':
8278 case 'P':
8279 nv_put(cap);
8280 break;
8281
8282#ifdef FEAT_BYTEOFF
8283 /* "go": goto byte count from start of buffer */
8284 case 'o':
8285 goto_byte(cap->count0);
8286 break;
8287#endif
8288
8289 /* "gQ": improved Ex mode */
8290 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008291 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 {
8293 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008294 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 break;
8296 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008297
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 if (!checkclearopq(oap))
8299 do_exmode(TRUE);
8300 break;
8301
8302#ifdef FEAT_JUMPLIST
8303 case ',':
8304 nv_pcmark(cap);
8305 break;
8306
8307 case ';':
8308 cap->count1 = -cap->count1;
8309 nv_pcmark(cap);
8310 break;
8311#endif
8312
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008313#ifdef FEAT_WINDOWS
8314 case 't':
8315 goto_tabpage((int)cap->count0);
8316 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008317 case 'T':
8318 goto_tabpage(-(int)cap->count1);
8319 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008320#endif
8321
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008322 case '+':
8323 case '-': /* "g+" and "g-": undo or redo along the timeline */
8324 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008325 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008326 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008327 break;
8328
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 default:
8330 clearopbeep(oap);
8331 break;
8332 }
8333}
8334
8335/*
8336 * Handle "o" and "O" commands.
8337 */
8338 static void
8339n_opencmd(cap)
8340 cmdarg_T *cap;
8341{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008342#ifdef FEAT_CONCEAL
8343 linenr_T oldline = curwin->w_cursor.lnum;
8344#endif
8345
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 if (!checkclearopq(cap->oap))
8347 {
8348#ifdef FEAT_FOLDING
8349 if (cap->cmdchar == 'O')
8350 /* Open above the first line of a folded sequence of lines */
8351 (void)hasFolding(curwin->w_cursor.lnum,
8352 &curwin->w_cursor.lnum, NULL);
8353 else
8354 /* Open below the last line of a folded sequence of lines */
8355 (void)hasFolding(curwin->w_cursor.lnum,
8356 NULL, &curwin->w_cursor.lnum);
8357#endif
8358 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8359 (cap->cmdchar == 'O' ? 1 : 0)),
8360 (linenr_T)(curwin->w_cursor.lnum +
8361 (cap->cmdchar == 'o' ? 1 : 0))
8362 ) == OK
8363 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8364#ifdef FEAT_COMMENTS
8365 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8366#endif
8367 0, 0))
8368 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008369#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008370 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008371 update_single_line(curwin, oldline);
8372#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008373 /* When '#' is in 'cpoptions' ignore the count. */
8374 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8375 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8377 }
8378 }
8379}
8380
8381/*
8382 * "." command: redo last change.
8383 */
8384 static void
8385nv_dot(cap)
8386 cmdarg_T *cap;
8387{
8388 if (!checkclearopq(cap->oap))
8389 {
8390 /*
8391 * If "restart_edit" is TRUE, the last but one command is repeated
8392 * instead of the last command (inserting text). This is used for
8393 * CTRL-O <.> in insert mode.
8394 */
8395 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8396 clearopbeep(cap->oap);
8397 }
8398}
8399
8400/*
8401 * CTRL-R: undo undo
8402 */
8403 static void
8404nv_redo(cap)
8405 cmdarg_T *cap;
8406{
8407 if (!checkclearopq(cap->oap))
8408 {
8409 u_redo((int)cap->count1);
8410 curwin->w_set_curswant = TRUE;
8411 }
8412}
8413
8414/*
8415 * Handle "U" command.
8416 */
8417 static void
8418nv_Undo(cap)
8419 cmdarg_T *cap;
8420{
8421 /* In Visual mode and typing "gUU" triggers an operator */
8422 if (cap->oap->op_type == OP_UPPER
8423#ifdef FEAT_VISUAL
8424 || VIsual_active
8425#endif
8426 )
8427 {
8428 /* translate "gUU" to "gUgU" */
8429 cap->cmdchar = 'g';
8430 cap->nchar = 'U';
8431 nv_operator(cap);
8432 }
8433 else if (!checkclearopq(cap->oap))
8434 {
8435 u_undoline();
8436 curwin->w_set_curswant = TRUE;
8437 }
8438}
8439
8440/*
8441 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8442 * single character.
8443 */
8444 static void
8445nv_tilde(cap)
8446 cmdarg_T *cap;
8447{
8448 if (!p_to
8449#ifdef FEAT_VISUAL
8450 && !VIsual_active
8451#endif
8452 && cap->oap->op_type != OP_TILDE)
8453 n_swapchar(cap);
8454 else
8455 nv_operator(cap);
8456}
8457
8458/*
8459 * Handle an operator command.
8460 * The actual work is done by do_pending_operator().
8461 */
8462 static void
8463nv_operator(cap)
8464 cmdarg_T *cap;
8465{
8466 int op_type;
8467
8468 op_type = get_op_type(cap->cmdchar, cap->nchar);
8469
8470 if (op_type == cap->oap->op_type) /* double operator works on lines */
8471 nv_lineop(cap);
8472 else if (!checkclearop(cap->oap))
8473 {
8474 cap->oap->start = curwin->w_cursor;
8475 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008476#ifdef FEAT_EVAL
8477 set_op_var(op_type);
8478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 }
8480}
8481
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008482#ifdef FEAT_EVAL
8483/*
8484 * Set v:operator to the characters for "optype".
8485 */
8486 static void
8487set_op_var(optype)
8488 int optype;
8489{
8490 char_u opchars[3];
8491
8492 if (optype == OP_NOP)
8493 set_vim_var_string(VV_OP, NULL, 0);
8494 else
8495 {
8496 opchars[0] = get_op_char(optype);
8497 opchars[1] = get_extra_op_char(optype);
8498 opchars[2] = NUL;
8499 set_vim_var_string(VV_OP, opchars, -1);
8500 }
8501}
8502#endif
8503
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504/*
8505 * Handle linewise operator "dd", "yy", etc.
8506 *
8507 * "_" is is a strange motion command that helps make operators more logical.
8508 * It is actually implemented, but not documented in the real Vi. This motion
8509 * command actually refers to "the current line". Commands like "dd" and "yy"
8510 * are really an alternate form of "d_" and "y_". It does accept a count, so
8511 * "d3_" works to delete 3 lines.
8512 */
8513 static void
8514nv_lineop(cap)
8515 cmdarg_T *cap;
8516{
8517 cap->oap->motion_type = MLINE;
8518 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8519 clearopbeep(cap->oap);
8520 else if ( cap->oap->op_type == OP_DELETE
8521 || cap->oap->op_type == OP_LSHIFT
8522 || cap->oap->op_type == OP_RSHIFT)
8523 beginline(BL_SOL | BL_FIX);
8524 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8525 beginline(BL_WHITE | BL_FIX);
8526}
8527
8528/*
8529 * <Home> command.
8530 */
8531 static void
8532nv_home(cap)
8533 cmdarg_T *cap;
8534{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008535 /* CTRL-HOME is like "gg" */
8536 if (mod_mask & MOD_MASK_CTRL)
8537 nv_goto(cap);
8538 else
8539 {
8540 cap->count0 = 1;
8541 nv_pipe(cap);
8542 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008543 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8544 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545}
8546
8547/*
8548 * "|" command.
8549 */
8550 static void
8551nv_pipe(cap)
8552 cmdarg_T *cap;
8553{
8554 cap->oap->motion_type = MCHAR;
8555 cap->oap->inclusive = FALSE;
8556 beginline(0);
8557 if (cap->count0 > 0)
8558 {
8559 coladvance((colnr_T)(cap->count0 - 1));
8560 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8561 }
8562 else
8563 curwin->w_curswant = 0;
8564 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008565 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 curwin->w_set_curswant = FALSE;
8567}
8568
8569/*
8570 * Handle back-word command "b" and "B".
8571 * cap->arg is 1 for "B"
8572 */
8573 static void
8574nv_bck_word(cap)
8575 cmdarg_T *cap;
8576{
8577 cap->oap->motion_type = MCHAR;
8578 cap->oap->inclusive = FALSE;
8579 curwin->w_set_curswant = TRUE;
8580 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8581 clearopbeep(cap->oap);
8582#ifdef FEAT_FOLDING
8583 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8584 foldOpenCursor();
8585#endif
8586}
8587
8588/*
8589 * Handle word motion commands "e", "E", "w" and "W".
8590 * cap->arg is TRUE for "E" and "W".
8591 */
8592 static void
8593nv_wordcmd(cap)
8594 cmdarg_T *cap;
8595{
8596 int n;
8597 int word_end;
8598 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008599 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600
8601 /*
8602 * Set inclusive for the "E" and "e" command.
8603 */
8604 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8605 word_end = TRUE;
8606 else
8607 word_end = FALSE;
8608 cap->oap->inclusive = word_end;
8609
8610 /*
8611 * "cw" and "cW" are a special case.
8612 */
8613 if (!word_end && cap->oap->op_type == OP_CHANGE)
8614 {
8615 n = gchar_cursor();
8616 if (n != NUL) /* not an empty line */
8617 {
8618 if (vim_iswhite(n))
8619 {
8620 /*
8621 * Reproduce a funny Vi behaviour: "cw" on a blank only
8622 * changes one character, not all blanks until the start of
8623 * the next word. Only do this when the 'w' flag is included
8624 * in 'cpoptions'.
8625 */
8626 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8627 {
8628 cap->oap->inclusive = TRUE;
8629 cap->oap->motion_type = MCHAR;
8630 return;
8631 }
8632 }
8633 else
8634 {
8635 /*
8636 * This is a little strange. To match what the real Vi does,
8637 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8638 * that we are not on a space or a TAB. This seems impolite
8639 * at first, but it's really more what we mean when we say
8640 * 'cw'.
8641 * Another strangeness: When standing on the end of a word
8642 * "ce" will change until the end of the next wordt, but "cw"
8643 * will change only one character! This is done by setting
8644 * flag.
8645 */
8646 cap->oap->inclusive = TRUE;
8647 word_end = TRUE;
8648 flag = TRUE;
8649 }
8650 }
8651 }
8652
8653 cap->oap->motion_type = MCHAR;
8654 curwin->w_set_curswant = TRUE;
8655 if (word_end)
8656 n = end_word(cap->count1, cap->arg, flag, FALSE);
8657 else
8658 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8659
Bram Moolenaardfefb982008-04-01 10:06:39 +00008660 /* Don't leave the cursor on the NUL past the end of line. Unless we
8661 * didn't move it forward. */
8662 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008663 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664
8665 if (n == FAIL && cap->oap->op_type == OP_NOP)
8666 clearopbeep(cap->oap);
8667 else
8668 {
8669#ifdef FEAT_VISUAL
8670 adjust_for_sel(cap);
8671#endif
8672#ifdef FEAT_FOLDING
8673 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8674 foldOpenCursor();
8675#endif
8676 }
8677}
8678
8679/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008680 * Used after a movement command: If the cursor ends up on the NUL after the
8681 * end of the line, may move it back to the last character and make the motion
8682 * inclusive.
8683 */
8684 static void
8685adjust_cursor(oap)
8686 oparg_T *oap;
8687{
8688 /* The cursor cannot remain on the NUL when:
8689 * - the column is > 0
8690 * - not in Visual mode or 'selection' is "o"
8691 * - 'virtualedit' is not "all" and not "onemore".
8692 */
8693 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
8694#ifdef FEAT_VISUAL
8695 && (!VIsual_active || *p_sel == 'o')
8696#endif
8697#ifdef FEAT_VIRTUALEDIT
8698 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8699#endif
8700 )
8701 {
8702 --curwin->w_cursor.col;
8703#ifdef FEAT_MBYTE
8704 /* prevent cursor from moving on the trail byte */
8705 if (has_mbyte)
8706 mb_adjust_cursor();
8707#endif
8708 oap->inclusive = TRUE;
8709 }
8710}
8711
8712/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 * "0" and "^" commands.
8714 * cap->arg is the argument for beginline().
8715 */
8716 static void
8717nv_beginline(cap)
8718 cmdarg_T *cap;
8719{
8720 cap->oap->motion_type = MCHAR;
8721 cap->oap->inclusive = FALSE;
8722 beginline(cap->arg);
8723#ifdef FEAT_FOLDING
8724 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8725 foldOpenCursor();
8726#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008727 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8728 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729}
8730
8731#ifdef FEAT_VISUAL
8732/*
8733 * In exclusive Visual mode, may include the last character.
8734 */
8735 static void
8736adjust_for_sel(cap)
8737 cmdarg_T *cap;
8738{
8739 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8740 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8741 {
8742# ifdef FEAT_MBYTE
8743 if (has_mbyte)
8744 inc_cursor();
8745 else
8746# endif
8747 ++curwin->w_cursor.col;
8748 cap->oap->inclusive = FALSE;
8749 }
8750}
8751
8752/*
8753 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8754 * Should check VIsual_mode before calling this.
8755 * Returns TRUE when backed up to the previous line.
8756 */
8757 static int
8758unadjust_for_sel()
8759{
8760 pos_T *pp;
8761
8762 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8763 {
8764 if (lt(VIsual, curwin->w_cursor))
8765 pp = &curwin->w_cursor;
8766 else
8767 pp = &VIsual;
8768#ifdef FEAT_VIRTUALEDIT
8769 if (pp->coladd > 0)
8770 --pp->coladd;
8771 else
8772#endif
8773 if (pp->col > 0)
8774 {
8775 --pp->col;
8776#ifdef FEAT_MBYTE
8777 mb_adjustpos(pp);
8778#endif
8779 }
8780 else if (pp->lnum > 1)
8781 {
8782 --pp->lnum;
8783 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8784 return TRUE;
8785 }
8786 }
8787 return FALSE;
8788}
8789
8790/*
8791 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8792 */
8793 static void
8794nv_select(cap)
8795 cmdarg_T *cap;
8796{
8797 if (VIsual_active)
8798 VIsual_select = TRUE;
8799 else if (VIsual_reselect)
8800 {
8801 cap->nchar = 'v'; /* fake "gv" command */
8802 cap->arg = TRUE;
8803 nv_g_cmd(cap);
8804 }
8805}
8806
8807#endif
8808
8809/*
8810 * "G", "gg", CTRL-END, CTRL-HOME.
8811 * cap->arg is TRUE for "G".
8812 */
8813 static void
8814nv_goto(cap)
8815 cmdarg_T *cap;
8816{
8817 linenr_T lnum;
8818
8819 if (cap->arg)
8820 lnum = curbuf->b_ml.ml_line_count;
8821 else
8822 lnum = 1L;
8823 cap->oap->motion_type = MLINE;
8824 setpcmark();
8825
8826 /* When a count is given, use it instead of the default lnum */
8827 if (cap->count0 != 0)
8828 lnum = cap->count0;
8829 if (lnum < 1L)
8830 lnum = 1L;
8831 else if (lnum > curbuf->b_ml.ml_line_count)
8832 lnum = curbuf->b_ml.ml_line_count;
8833 curwin->w_cursor.lnum = lnum;
8834 beginline(BL_SOL | BL_FIX);
8835#ifdef FEAT_FOLDING
8836 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8837 foldOpenCursor();
8838#endif
8839}
8840
8841/*
8842 * CTRL-\ in Normal mode.
8843 */
8844 static void
8845nv_normal(cap)
8846 cmdarg_T *cap;
8847{
8848 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8849 {
8850 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008851 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 clear_cmdline = TRUE; /* unshow mode later */
8853 restart_edit = 0;
8854#ifdef FEAT_CMDWIN
8855 if (cmdwin_type != 0)
8856 cmdwin_result = Ctrl_C;
8857#endif
8858#ifdef FEAT_VISUAL
8859 if (VIsual_active)
8860 {
8861 end_visual_mode(); /* stop Visual */
8862 redraw_curbuf_later(INVERTED);
8863 }
8864#endif
8865 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8866 if (cap->nchar == Ctrl_G && p_im)
8867 restart_edit = 'a';
8868 }
8869 else
8870 clearopbeep(cap->oap);
8871}
8872
8873/*
8874 * ESC in Normal mode: beep, but don't flush buffers.
8875 * Don't even beep if we are canceling a command.
8876 */
8877 static void
8878nv_esc(cap)
8879 cmdarg_T *cap;
8880{
8881 int no_reason;
8882
8883 no_reason = (cap->oap->op_type == OP_NOP
8884 && cap->opcount == 0
8885 && cap->count0 == 0
8886 && cap->oap->regname == 0
8887 && !p_im);
8888
8889 if (cap->arg) /* TRUE for CTRL-C */
8890 {
8891 if (restart_edit == 0
8892#ifdef FEAT_CMDWIN
8893 && cmdwin_type == 0
8894#endif
8895#ifdef FEAT_VISUAL
8896 && !VIsual_active
8897#endif
8898 && no_reason)
8899 MSG(_("Type :quit<Enter> to exit Vim"));
8900
8901 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8902 * set again below when halfway a mapping. */
8903 if (!p_im)
8904 restart_edit = 0;
8905#ifdef FEAT_CMDWIN
8906 if (cmdwin_type != 0)
8907 {
8908 cmdwin_result = K_IGNORE;
8909 got_int = FALSE; /* don't stop executing autocommands et al. */
8910 return;
8911 }
8912#endif
8913 }
8914
8915#ifdef FEAT_VISUAL
8916 if (VIsual_active)
8917 {
8918 end_visual_mode(); /* stop Visual */
8919 check_cursor_col(); /* make sure cursor is not beyond EOL */
8920 curwin->w_set_curswant = TRUE;
8921 redraw_curbuf_later(INVERTED);
8922 }
8923 else
8924#endif
8925 if (no_reason)
8926 vim_beep();
8927 clearop(cap->oap);
8928
8929 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8930 * set return to Insert mode afterwards. */
8931 if (restart_edit == 0 && goto_im()
8932#ifdef FEAT_EX_EXTRA
8933 && ex_normal_busy == 0
8934#endif
8935 )
8936 restart_edit = 'a';
8937}
8938
8939/*
8940 * Handle "A", "a", "I", "i" and <Insert> commands.
8941 */
8942 static void
8943nv_edit(cap)
8944 cmdarg_T *cap;
8945{
8946 /* <Insert> is equal to "i" */
8947 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8948 cap->cmdchar = 'i';
8949
8950#ifdef FEAT_VISUAL
8951 /* in Visual mode "A" and "I" are an operator */
8952 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8953 v_visop(cap);
8954
8955 /* in Visual mode and after an operator "a" and "i" are for text objects */
8956 else
8957#endif
8958 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8959 && (cap->oap->op_type != OP_NOP
8960#ifdef FEAT_VISUAL
8961 || VIsual_active
8962#endif
8963 ))
8964 {
8965#ifdef FEAT_TEXTOBJ
8966 nv_object(cap);
8967#else
8968 clearopbeep(cap->oap);
8969#endif
8970 }
8971 else if (!curbuf->b_p_ma && !p_im)
8972 {
8973 /* Only give this error when 'insertmode' is off. */
8974 EMSG(_(e_modifiable));
8975 clearop(cap->oap);
8976 }
8977 else if (!checkclearopq(cap->oap))
8978 {
8979 switch (cap->cmdchar)
8980 {
8981 case 'A': /* "A"ppend after the line */
8982 curwin->w_set_curswant = TRUE;
8983#ifdef FEAT_VIRTUALEDIT
8984 if (ve_flags == VE_ALL)
8985 {
8986 int save_State = State;
8987
8988 /* Pretent Insert mode here to allow the cursor on the
8989 * character past the end of the line */
8990 State = INSERT;
8991 coladvance((colnr_T)MAXCOL);
8992 State = save_State;
8993 }
8994 else
8995#endif
8996 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8997 break;
8998
8999 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009000 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9001 beginline(BL_WHITE);
9002 else
9003 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 break;
9005
9006 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9007#ifdef FEAT_VIRTUALEDIT
9008 /* increment coladd when in virtual space, increment the
9009 * column otherwise, also to append after an unprintable char */
9010 if (virtual_active()
9011 && (curwin->w_cursor.coladd > 0
9012 || *ml_get_cursor() == NUL
9013 || *ml_get_cursor() == TAB))
9014 curwin->w_cursor.coladd++;
9015 else
9016#endif
9017 if (*ml_get_cursor() != NUL)
9018 inc_cursor();
9019 break;
9020 }
9021
9022#ifdef FEAT_VIRTUALEDIT
9023 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9024 {
9025 int save_State = State;
9026
9027 /* Pretent Insert mode here to allow the cursor on the
9028 * character past the end of the line */
9029 State = INSERT;
9030 coladvance(getviscol());
9031 State = save_State;
9032 }
9033#endif
9034
9035 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9036 }
9037}
9038
9039/*
9040 * Invoke edit() and take care of "restart_edit" and the return value.
9041 */
9042 static void
9043invoke_edit(cap, repl, cmd, startln)
9044 cmdarg_T *cap;
9045 int repl; /* "r" or "gr" command */
9046 int cmd;
9047 int startln;
9048{
9049 int restart_edit_save = 0;
9050
9051 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9052 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9053 * it. */
9054 if (repl || !stuff_empty())
9055 restart_edit_save = restart_edit;
9056 else
9057 restart_edit_save = 0;
9058
9059 /* Always reset "restart_edit", this is not a restarted edit. */
9060 restart_edit = 0;
9061
9062 if (edit(cmd, startln, cap->count1))
9063 cap->retval |= CA_COMMAND_BUSY;
9064
9065 if (restart_edit == 0)
9066 restart_edit = restart_edit_save;
9067}
9068
9069#ifdef FEAT_TEXTOBJ
9070/*
9071 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9072 */
9073 static void
9074nv_object(cap)
9075 cmdarg_T *cap;
9076{
9077 int flag;
9078 int include;
9079 char_u *mps_save;
9080
9081 if (cap->cmdchar == 'i')
9082 include = FALSE; /* "ix" = inner object: exclude white space */
9083 else
9084 include = TRUE; /* "ax" = an object: include white space */
9085
9086 /* Make sure (), [], {} and <> are in 'matchpairs' */
9087 mps_save = curbuf->b_p_mps;
9088 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9089
9090 switch (cap->nchar)
9091 {
9092 case 'w': /* "aw" = a word */
9093 flag = current_word(cap->oap, cap->count1, include, FALSE);
9094 break;
9095 case 'W': /* "aW" = a WORD */
9096 flag = current_word(cap->oap, cap->count1, include, TRUE);
9097 break;
9098 case 'b': /* "ab" = a braces block */
9099 case '(':
9100 case ')':
9101 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9102 break;
9103 case 'B': /* "aB" = a Brackets block */
9104 case '{':
9105 case '}':
9106 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9107 break;
9108 case '[': /* "a[" = a [] block */
9109 case ']':
9110 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9111 break;
9112 case '<': /* "a<" = a <> block */
9113 case '>':
9114 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9115 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009116 case 't': /* "at" = a tag block (xml and html) */
9117 flag = current_tagblock(cap->oap, cap->count1, include);
9118 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 case 'p': /* "ap" = a paragraph */
9120 flag = current_par(cap->oap, cap->count1, include, 'p');
9121 break;
9122 case 's': /* "as" = a sentence */
9123 flag = current_sent(cap->oap, cap->count1, include);
9124 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009125 case '"': /* "a"" = a double quoted string */
9126 case '\'': /* "a'" = a single quoted string */
9127 case '`': /* "a`" = a backtick quoted string */
9128 flag = current_quote(cap->oap, cap->count1, include,
9129 cap->nchar);
9130 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131#if 0 /* TODO */
9132 case 'S': /* "aS" = a section */
9133 case 'f': /* "af" = a filename */
9134 case 'u': /* "au" = a URL */
9135#endif
9136 default:
9137 flag = FAIL;
9138 break;
9139 }
9140
9141 curbuf->b_p_mps = mps_save;
9142 if (flag == FAIL)
9143 clearopbeep(cap->oap);
9144 adjust_cursor_col();
9145 curwin->w_set_curswant = TRUE;
9146}
9147#endif
9148
9149/*
9150 * "q" command: Start/stop recording.
9151 * "q:", "q/", "q?": edit command-line in command-line window.
9152 */
9153 static void
9154nv_record(cap)
9155 cmdarg_T *cap;
9156{
9157 if (cap->oap->op_type == OP_FORMAT)
9158 {
9159 /* "gqq" is the same as "gqgq": format line */
9160 cap->cmdchar = 'g';
9161 cap->nchar = 'q';
9162 nv_operator(cap);
9163 }
9164 else if (!checkclearop(cap->oap))
9165 {
9166#ifdef FEAT_CMDWIN
9167 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9168 {
9169 stuffcharReadbuff(cap->nchar);
9170 stuffcharReadbuff(K_CMDWIN);
9171 }
9172 else
9173#endif
9174 /* (stop) recording into a named register, unless executing a
9175 * register */
9176 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9177 clearopbeep(cap->oap);
9178 }
9179}
9180
9181/*
9182 * Handle the "@r" command.
9183 */
9184 static void
9185nv_at(cap)
9186 cmdarg_T *cap;
9187{
9188 if (checkclearop(cap->oap))
9189 return;
9190#ifdef FEAT_EVAL
9191 if (cap->nchar == '=')
9192 {
9193 if (get_expr_register() == NUL)
9194 return;
9195 }
9196#endif
9197 while (cap->count1-- && !got_int)
9198 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009199 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 {
9201 clearopbeep(cap->oap);
9202 break;
9203 }
9204 line_breakcheck();
9205 }
9206}
9207
9208/*
9209 * Handle the CTRL-U and CTRL-D commands.
9210 */
9211 static void
9212nv_halfpage(cap)
9213 cmdarg_T *cap;
9214{
9215 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9216 || (cap->cmdchar == Ctrl_D
9217 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9218 clearopbeep(cap->oap);
9219 else if (!checkclearop(cap->oap))
9220 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9221}
9222
9223/*
9224 * Handle "J" or "gJ" command.
9225 */
9226 static void
9227nv_join(cap)
9228 cmdarg_T *cap;
9229{
9230#ifdef FEAT_VISUAL
9231 if (VIsual_active) /* join the visual lines */
9232 nv_operator(cap);
9233 else
9234#endif
9235 if (!checkclearop(cap->oap))
9236 {
9237 if (cap->count0 <= 1)
9238 cap->count0 = 2; /* default for join is two lines! */
9239 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9240 curbuf->b_ml.ml_line_count)
9241 clearopbeep(cap->oap); /* beyond last line */
9242 else
9243 {
9244 prep_redo(cap->oap->regname, cap->count0,
9245 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02009246 (void)do_join(cap->count0, cap->nchar == NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 }
9248 }
9249}
9250
9251/*
9252 * "P", "gP", "p" and "gp" commands.
9253 */
9254 static void
9255nv_put(cap)
9256 cmdarg_T *cap;
9257{
9258#ifdef FEAT_VISUAL
9259 int regname = 0;
9260 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009261 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009262 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263#endif
9264 int dir;
9265 int flags = 0;
9266
9267 if (cap->oap->op_type != OP_NOP)
9268 {
9269#ifdef FEAT_DIFF
9270 /* "dp" is ":diffput" */
9271 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9272 {
9273 clearop(cap->oap);
9274 nv_diffgetput(TRUE);
9275 }
9276 else
9277#endif
9278 clearopbeep(cap->oap);
9279 }
9280 else
9281 {
9282 dir = (cap->cmdchar == 'P'
9283 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9284 ? BACKWARD : FORWARD;
9285 prep_redo_cmd(cap);
9286 if (cap->cmdchar == 'g')
9287 flags |= PUT_CURSEND;
9288
9289#ifdef FEAT_VISUAL
9290 if (VIsual_active)
9291 {
9292 /* Putting in Visual mode: The put text replaces the selected
9293 * text. First delete the selected text, then put the new text.
9294 * Need to save and restore the registers that the delete
9295 * overwrites if the old contents is being put.
9296 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009297 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 regname = cap->oap->regname;
9299# ifdef FEAT_CLIPBOARD
9300 adjust_clip_reg(&regname);
9301# endif
9302 if (regname == 0 || VIM_ISDIGIT(regname)
9303# ifdef FEAT_CLIPBOARD
9304 || (clip_unnamed && (regname == '*' || regname == '+'))
9305# endif
9306
9307 )
9308 {
9309 /* the delete is going to overwrite the register we want to
9310 * put, save it first. */
9311 reg1 = get_register(regname, TRUE);
9312 }
9313
9314 /* Now delete the selected text. */
9315 cap->cmdchar = 'd';
9316 cap->nchar = NUL;
9317 cap->oap->regname = NUL;
9318 nv_operator(cap);
9319 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009320 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321
9322 /* delete PUT_LINE_BACKWARD; */
9323 cap->oap->regname = regname;
9324
9325 if (reg1 != NULL)
9326 {
9327 /* Delete probably changed the register we want to put, save
9328 * it first. Then put back what was there before the delete. */
9329 reg2 = get_register(regname, FALSE);
9330 put_register(regname, reg1);
9331 }
9332
9333 /* When deleted a linewise Visual area, put the register as
9334 * lines to avoid it joined with the next line. When deletion was
9335 * characterwise, split a line when putting lines. */
9336 if (VIsual_mode == 'V')
9337 flags |= PUT_LINE;
9338 else if (VIsual_mode == 'v')
9339 flags |= PUT_LINE_SPLIT;
9340 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9341 flags |= PUT_LINE_FORWARD;
9342 dir = BACKWARD;
9343 if ((VIsual_mode != 'V'
9344 && curwin->w_cursor.col < curbuf->b_op_start.col)
9345 || (VIsual_mode == 'V'
9346 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9347 /* cursor is at the end of the line or end of file, put
9348 * forward. */
9349 dir = FORWARD;
9350 }
9351#endif
9352 do_put(cap->oap->regname, dir, cap->count1, flags);
9353
9354#ifdef FEAT_VISUAL
9355 /* If a register was saved, put it back now. */
9356 if (reg2 != NULL)
9357 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009358
9359 /* What to reselect with "gv"? Selecting the just put text seems to
9360 * be the most useful, since the original text was removed. */
9361 if (was_visual)
9362 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009363 curbuf->b_visual.vi_start = curbuf->b_op_start;
9364 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009365 }
9366
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009367 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009368 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009369 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009370 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009371 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009372
9373 /* If the cursor was in that line, move it to the end of the last
9374 * line. */
9375 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9376 {
9377 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9378 coladvance((colnr_T)MAXCOL);
9379 }
9380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381#endif
9382 auto_format(FALSE, TRUE);
9383 }
9384}
9385
9386/*
9387 * "o" and "O" commands.
9388 */
9389 static void
9390nv_open(cap)
9391 cmdarg_T *cap;
9392{
9393#ifdef FEAT_DIFF
9394 /* "do" is ":diffget" */
9395 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9396 {
9397 clearop(cap->oap);
9398 nv_diffgetput(FALSE);
9399 }
9400 else
9401#endif
9402#ifdef FEAT_VISUAL
9403 if (VIsual_active) /* switch start and end of visual */
9404 v_swap_corners(cap->cmdchar);
9405 else
9406#endif
9407 n_opencmd(cap);
9408}
9409
9410#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 static void
9412nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009413 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414{
9415 ProcessSniffRequests();
9416}
9417#endif
9418
9419#ifdef FEAT_NETBEANS_INTG
9420 static void
9421nv_nbcmd(cap)
9422 cmdarg_T *cap;
9423{
9424 netbeans_keycommand(cap->nchar);
9425}
9426#endif
9427
9428#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 static void
9430nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009431 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432{
9433 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9434}
9435#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009436
9437#ifdef FEAT_AUTOCMD
9438/*
9439 * Trigger CursorHold event.
9440 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9441 * input buffer. "did_cursorhold" is set to avoid retriggering.
9442 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009443 static void
9444nv_cursorhold(cap)
9445 cmdarg_T *cap;
9446{
9447 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9448 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009449 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009450}
9451#endif