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