blob: c79b67b1c1ecc370c5e2b105256b64926103c350 [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 */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +020023static colnr_T resel_VIsual_vcol; /* nr of cols or end col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
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
Bram Moolenaar0c50a6b2012-05-25 11:04:38 +020032#ifdef __BORLANDC__
33 _RTLENTRYF
34#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000035 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
Bram Moolenaarf00dc262012-10-21 03:54:33 +0200963 /* For "gn" from redo, need to get one more char to determine the
964 * operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarf00dc262012-10-21 03:54:33 +0200966 || ca.nchar == Ctrl_BSL
967 || ((ca.nchar == 'n' || ca.nchar == 'N') && !stuff_empty()))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 {
969 cp = &ca.extra_char; /* need to get a third character */
970 if (ca.nchar != 'r')
971 lit = TRUE; /* get it literally */
972 else
973 repl = TRUE; /* get it in replace mode */
974 }
975 else
976 cp = NULL; /* no third character needed */
977 }
978 else
979 {
980 if (ca.cmdchar == 'r') /* get it in replace mode */
981 repl = TRUE;
982 cp = &ca.nchar;
983 }
984 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
985
986 /*
987 * Get a second or third character.
988 */
989 if (cp != NULL)
990 {
991#ifdef CURSOR_SHAPE
992 if (repl)
993 {
994 State = REPLACE; /* pretend Replace mode */
995 ui_cursor_shape(); /* show different cursor shape */
996 }
997#endif
998 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
999 {
1000 /* Allow mappings defined with ":lmap". */
1001 --no_mapping;
1002 --allow_keys;
1003 if (repl)
1004 State = LREPLACE;
1005 else
1006 State = LANGMAP;
1007 langmap_active = TRUE;
1008 }
1009#ifdef USE_IM_CONTROL
1010 save_smd = p_smd;
1011 p_smd = FALSE; /* Don't let the IM code show the mode here */
1012 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
1013 im_set_active(TRUE);
1014#endif
1015
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001016 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017
1018 if (langmap_active)
1019 {
1020 /* Undo the decrement done above */
1021 ++no_mapping;
1022 ++allow_keys;
1023 State = NORMAL_BUSY;
1024 }
1025#ifdef USE_IM_CONTROL
1026 if (lang)
1027 {
1028 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
1029 im_save_status(&curbuf->b_p_iminsert);
1030 im_set_active(FALSE);
1031 }
1032 p_smd = save_smd;
1033#endif
1034#ifdef CURSOR_SHAPE
1035 State = NORMAL_BUSY;
1036#endif
1037#ifdef FEAT_CMDL_INFO
1038 need_flushbuf |= add_to_showcmd(*cp);
1039#endif
1040
1041 if (!lit)
1042 {
1043#ifdef FEAT_DIGRAPHS
1044 /* Typing CTRL-K gets a digraph. */
1045 if (*cp == Ctrl_K
1046 && ((nv_cmds[idx].cmd_flags & NV_LANG)
1047 || cp == &ca.extra_char)
1048 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1049 {
1050 c = get_digraph(FALSE);
1051 if (c > 0)
1052 {
1053 *cp = c;
1054# ifdef FEAT_CMDL_INFO
1055 /* Guessing how to update showcmd here... */
1056 del_from_showcmd(3);
1057 need_flushbuf |= add_to_showcmd(*cp);
1058# endif
1059 }
1060 }
1061#endif
1062
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 /* adjust chars > 127, except after "tTfFr" commands */
1064 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065#ifdef FEAT_RIGHTLEFT
1066 /* adjust Hebrew mapped char */
1067 if (p_hkmap && lang && KeyTyped)
1068 *cp = hkmap(*cp);
1069# ifdef FEAT_FKMAP
1070 /* adjust Farsi mapped char */
1071 if (p_fkmap && lang && KeyTyped)
1072 *cp = fkmap(*cp);
1073# endif
1074#endif
1075 }
1076
1077 /*
1078 * When the next character is CTRL-\ a following CTRL-N means the
1079 * command is aborted and we go to Normal mode.
1080 */
1081 if (cp == &ca.extra_char
1082 && ca.nchar == Ctrl_BSL
1083 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1084 {
1085 ca.cmdchar = Ctrl_BSL;
1086 ca.nchar = ca.extra_char;
1087 idx = find_command(ca.cmdchar);
1088 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001089 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001090 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 else if (*cp == Ctrl_BSL)
1092 {
1093 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1094
1095 /* There is a busy wait here when typing "f<C-\>" and then
1096 * something different from CTRL-N. Can't be avoided. */
1097 while ((c = vpeekc()) <= 0 && towait > 0L)
1098 {
1099 do_sleep(towait > 50L ? 50L : towait);
1100 towait -= 50L;
1101 }
1102 if (c > 0)
1103 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001104 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 if (c != Ctrl_N && c != Ctrl_G)
1106 vungetc(c);
1107 else
1108 {
1109 ca.cmdchar = Ctrl_BSL;
1110 ca.nchar = c;
1111 idx = find_command(ca.cmdchar);
1112 }
1113 }
1114 }
1115
1116#ifdef FEAT_MBYTE
1117 /* When getting a text character and the next character is a
1118 * multi-byte character, it could be a composing character.
1119 * However, don't wait for it to arrive. */
1120 while (enc_utf8 && lang && (c = vpeekc()) > 0
1121 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1122 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001123 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 if (!utf_iscomposing(c))
1125 {
1126 vungetc(c); /* it wasn't, put it back */
1127 break;
1128 }
1129 else if (ca.ncharC1 == 0)
1130 ca.ncharC1 = c;
1131 else
1132 ca.ncharC2 = c;
1133 }
1134#endif
1135 }
1136 --no_mapping;
1137 --allow_keys;
1138 }
1139
1140#ifdef FEAT_CMDL_INFO
1141 /*
1142 * Flush the showcmd characters onto the screen so we can see them while
1143 * the command is being executed. Only do this when the shown command was
1144 * actually displayed, otherwise this will slow down a lot when executing
1145 * mappings.
1146 */
1147 if (need_flushbuf)
1148 out_flush();
1149#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00001150#ifdef FEAT_AUTOCMD
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001151 if (ca.cmdchar != K_IGNORE)
1152 did_cursorhold = FALSE;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154
1155 State = NORMAL;
1156
1157 if (ca.nchar == ESC)
1158 {
1159 clearop(oap);
1160 if (restart_edit == 0 && goto_im())
1161 restart_edit = 'a';
1162 goto normal_end;
1163 }
1164
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001165 if (ca.cmdchar != K_IGNORE)
1166 {
1167 msg_didout = FALSE; /* don't scroll screen up for normal command */
1168 msg_col = 0;
1169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170
1171#ifdef FEAT_VISUAL
1172 old_pos = curwin->w_cursor; /* remember where cursor was */
1173
1174 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1175 * mode. */
1176 if (!VIsual_active && km_startsel)
1177 {
1178 if (nv_cmds[idx].cmd_flags & NV_SS)
1179 {
1180 start_selection();
1181 unshift_special(&ca);
1182 idx = find_command(ca.cmdchar);
1183 }
1184 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1185 && (mod_mask & MOD_MASK_SHIFT))
1186 {
1187 start_selection();
1188 mod_mask &= ~MOD_MASK_SHIFT;
1189 }
1190 }
1191#endif
1192
1193 /*
1194 * Execute the command!
1195 * Call the command function found in the commands table.
1196 */
1197 ca.arg = nv_cmds[idx].cmd_arg;
1198 (nv_cmds[idx].cmd_func)(&ca);
1199
1200 /*
1201 * If we didn't start or finish an operator, reset oap->regname, unless we
1202 * need it later.
1203 */
1204 if (!finish_op
1205 && !oap->op_type
1206 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1207 {
1208 clearop(oap);
1209#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001210 {
1211 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001212
Bram Moolenaar536681b2011-05-10 16:12:45 +02001213 /* Adjust the register according to 'clipboard', so that when
1214 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001215# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001216 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001217# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001218 set_reg_var(regname);
1219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220#endif
1221 }
1222
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001223#ifdef FEAT_VISUAL
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001224 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001225 * character or "z333<cr>". */
1226 if (old_mapped_len > 0)
1227 old_mapped_len = typebuf_maplen();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001228#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001229
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 /*
1231 * If an operation is pending, handle it...
1232 */
1233 do_pending_operator(&ca, old_col, FALSE);
1234
1235 /*
1236 * Wait for a moment when a message is displayed that will be overwritten
1237 * by the mode message.
1238 * In Visual mode and with "^O" in Insert mode, a short message will be
1239 * overwritten by the mode message. Wait a bit, until a key is hit.
1240 * In Visual mode, it's more important to keep the Visual area updated
1241 * than keeping a message (e.g. from a /pat search).
1242 * Only do this if the command was typed, not from a mapping.
1243 * Don't wait when emsg_silent is non-zero.
1244 * Also wait a bit after an error message, e.g. for "^O:".
1245 * Don't redraw the screen, it would remove the message.
1246 */
1247 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001248 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 && (restart_edit != 0
1250#ifdef FEAT_VISUAL
1251 || (VIsual_active
1252 && old_pos.lnum == curwin->w_cursor.lnum
1253 && old_pos.col == curwin->w_cursor.col)
1254#endif
1255 )
1256 && (clear_cmdline
1257 || redraw_cmdline)
1258 && (msg_didout || (msg_didany && msg_scroll))
1259 && !msg_nowait
1260 && KeyTyped)
1261 || (restart_edit != 0
1262#ifdef FEAT_VISUAL
1263 && !VIsual_active
1264#endif
1265 && (msg_scroll
1266 || emsg_on_display)))
1267 && oap->regname == 0
1268 && !(ca.retval & CA_COMMAND_BUSY)
1269 && stuff_empty()
1270 && typebuf_typed()
1271 && emsg_silent == 0
1272 && !did_wait_return
1273 && oap->op_type == OP_NOP)
1274 {
1275 int save_State = State;
1276
1277 /* Draw the cursor with the right shape here */
1278 if (restart_edit != 0)
1279 State = INSERT;
1280
1281 /* If need to redraw, and there is a "keep_msg", redraw before the
1282 * delay */
1283 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1284 {
1285 char_u *kmsg;
1286
1287 kmsg = keep_msg;
1288 keep_msg = NULL;
1289 /* showmode() will clear keep_msg, but we want to use it anyway */
1290 update_screen(0);
1291 /* now reset it, otherwise it's put in the history again */
1292 keep_msg = kmsg;
1293 msg_attr(kmsg, keep_msg_attr);
1294 vim_free(kmsg);
1295 }
1296 setcursor();
1297 cursor_on();
1298 out_flush();
1299 if (msg_scroll || emsg_on_display)
1300 ui_delay(1000L, TRUE); /* wait at least one second */
1301 ui_delay(3000L, FALSE); /* wait up to three seconds */
1302 State = save_State;
1303
1304 msg_scroll = FALSE;
1305 emsg_on_display = FALSE;
1306 }
1307
1308 /*
1309 * Finish up after executing a Normal mode command.
1310 */
1311normal_end:
1312
1313 msg_nowait = FALSE;
1314
1315 /* Reset finish_op, in case it was set */
1316#ifdef CURSOR_SHAPE
1317 c = finish_op;
1318#endif
1319 finish_op = FALSE;
1320#ifdef CURSOR_SHAPE
1321 /* Redraw the cursor with another shape, if we were in Operator-pending
1322 * mode or did a replace command. */
1323 if (c || ca.cmdchar == 'r')
1324 {
1325 ui_cursor_shape(); /* may show different cursor shape */
1326# ifdef FEAT_MOUSESHAPE
1327 update_mouseshape(-1);
1328# endif
1329 }
1330#endif
1331
1332#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001333 if (oap->op_type == OP_NOP && oap->regname == 0
1334# ifdef FEAT_AUTOCMD
1335 && ca.cmdchar != K_CURSORHOLD
1336# endif
1337 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 clear_showcmd();
1339#endif
1340
1341 checkpcmark(); /* check if we moved since setting pcmark */
1342 vim_free(ca.searchbuf);
1343
1344#ifdef FEAT_MBYTE
1345 if (has_mbyte)
1346 mb_adjust_cursor();
1347#endif
1348
1349#ifdef FEAT_SCROLLBIND
1350 if (curwin->w_p_scb && toplevel)
1351 {
1352 validate_cursor(); /* may need to update w_leftcol */
1353 do_check_scrollbind(TRUE);
1354 }
1355#endif
1356
Bram Moolenaar860cae12010-06-05 23:22:07 +02001357#ifdef FEAT_CURSORBIND
1358 if (curwin->w_p_crb && toplevel)
1359 {
1360 validate_cursor(); /* may need to update w_leftcol */
1361 do_check_cursorbind();
1362 }
1363#endif
1364
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 /*
1366 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1367 * if still inside a mapping that started in Visual mode).
1368 * May switch from Visual to Select mode after CTRL-O command.
1369 */
1370 if ( oap->op_type == OP_NOP
1371#ifdef FEAT_VISUAL
1372 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1373 || restart_VIsual_select == 1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001374#else
1375 && restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376#endif
1377 && !(ca.retval & CA_COMMAND_BUSY)
1378 && stuff_empty()
1379 && oap->regname == 0)
1380 {
1381#ifdef FEAT_VISUAL
1382 if (restart_VIsual_select == 1)
1383 {
1384 VIsual_select = TRUE;
1385 showmode();
1386 restart_VIsual_select = 0;
1387 }
1388#endif
1389 if (restart_edit != 0
1390#ifdef FEAT_VISUAL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001391 && !VIsual_active && old_mapped_len == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001393 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 (void)edit(restart_edit, FALSE, 1L);
1395 }
1396
1397#ifdef FEAT_VISUAL
1398 if (restart_VIsual_select == 2)
1399 restart_VIsual_select = 1;
1400#endif
1401
1402 /* Save count before an operator for next time. */
1403 opcount = ca.opcount;
1404}
1405
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001406#ifdef FEAT_EVAL
1407/*
1408 * Set v:count and v:count1 according to "cap".
1409 * Set v:prevcount only when "set_prevcount" is TRUE.
1410 */
1411 static void
1412set_vcount_ca(cap, set_prevcount)
1413 cmdarg_T *cap;
1414 int *set_prevcount;
1415{
1416 long count = cap->count0;
1417
1418 /* multiply with cap->opcount the same way as above */
1419 if (cap->opcount != 0)
1420 count = cap->opcount * (count == 0 ? 1 : count);
1421 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1422 *set_prevcount = FALSE; /* only set v:prevcount once */
1423}
1424#endif
1425
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426/*
1427 * Handle an operator after visual mode or when the movement is finished
1428 */
1429 void
1430do_pending_operator(cap, old_col, gui_yank)
1431 cmdarg_T *cap;
1432 int old_col;
1433 int gui_yank;
1434{
1435 oparg_T *oap = cap->oap;
1436 pos_T old_cursor;
1437 int empty_region_error;
1438 int restart_edit_save;
1439
1440#ifdef FEAT_VISUAL
1441 /* The visual area is remembered for redo */
1442 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1443 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001444 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 static long redo_VIsual_count; /* count for Visual operator */
1446# ifdef FEAT_VIRTUALEDIT
1447 int include_line_break = FALSE;
1448# endif
1449#endif
1450
1451#if defined(FEAT_CLIPBOARD)
1452 /*
1453 * Yank the visual area into the GUI selection register before we operate
1454 * on it and lose it forever.
1455 * Don't do it if a specific register was specified, so that ""x"*P works.
1456 * This could call do_pending_operator() recursively, but that's OK
1457 * because gui_yank will be TRUE for the nested call.
1458 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001459 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 && oap->op_type != OP_NOP
1461 && !gui_yank
1462# ifdef FEAT_VISUAL
1463 && VIsual_active
1464 && !redo_VIsual_busy
1465# endif
1466 && oap->regname == 0)
1467 clip_auto_select();
1468#endif
1469 old_cursor = curwin->w_cursor;
1470
1471 /*
1472 * If an operation is pending, handle it...
1473 */
1474 if ((finish_op
1475#ifdef FEAT_VISUAL
1476 || VIsual_active
1477#endif
1478 ) && oap->op_type != OP_NOP)
1479 {
1480#ifdef FEAT_VISUAL
1481 oap->is_VIsual = VIsual_active;
1482 if (oap->motion_force == 'V')
1483 oap->motion_type = MLINE;
1484 else if (oap->motion_force == 'v')
1485 {
1486 /* If the motion was linewise, "inclusive" will not have been set.
1487 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1488 if (oap->motion_type == MLINE)
1489 oap->inclusive = FALSE;
1490 /* If the motion already was characterwise, toggle "inclusive" */
1491 else if (oap->motion_type == MCHAR)
1492 oap->inclusive = !oap->inclusive;
1493 oap->motion_type = MCHAR;
1494 }
1495 else if (oap->motion_force == Ctrl_V)
1496 {
1497 /* Change line- or characterwise motion into Visual block mode. */
1498 VIsual_active = TRUE;
1499 VIsual = oap->start;
1500 VIsual_mode = Ctrl_V;
1501 VIsual_select = FALSE;
1502 VIsual_reselect = FALSE;
1503 }
1504#endif
1505
1506 /* only redo yank when 'y' flag is in 'cpoptions' */
1507 /* never redo "zf" (define fold) */
1508 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1509#ifdef FEAT_VISUAL
1510 && (!VIsual_active || oap->motion_force)
1511#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001512 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513#ifdef FEAT_FOLDING
1514 && oap->op_type != OP_FOLD
1515 && oap->op_type != OP_FOLDOPEN
1516 && oap->op_type != OP_FOLDOPENREC
1517 && oap->op_type != OP_FOLDCLOSE
1518 && oap->op_type != OP_FOLDCLOSEREC
1519 && oap->op_type != OP_FOLDDEL
1520 && oap->op_type != OP_FOLDDELREC
1521#endif
1522 )
1523 {
1524 prep_redo(oap->regname, cap->count0,
1525 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1526 oap->motion_force, cap->cmdchar, cap->nchar);
1527 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1528 {
1529 /*
1530 * If 'cpoptions' does not contain 'r', insert the search
1531 * pattern to really repeat the same command.
1532 */
1533 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001534 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 AppendToRedobuff(NL_STR);
1536 }
1537 else if (cap->cmdchar == ':')
1538 {
1539 /* do_cmdline() has stored the first typed line in
1540 * "repeat_cmdline". When several lines are typed repeating
1541 * won't be possible. */
1542 if (repeat_cmdline == NULL)
1543 ResetRedobuff();
1544 else
1545 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001546 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 AppendToRedobuff(NL_STR);
1548 vim_free(repeat_cmdline);
1549 repeat_cmdline = NULL;
1550 }
1551 }
1552 }
1553
1554#ifdef FEAT_VISUAL
1555 if (redo_VIsual_busy)
1556 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001557 /* Redo of an operation on a Visual area. Use the same size from
1558 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 oap->start = curwin->w_cursor;
1560 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1561 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1562 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1563 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001564 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001566 if (VIsual_mode == 'v')
1567 {
1568 if (redo_VIsual_line_count <= 1)
1569 {
1570 validate_virtcol();
1571 curwin->w_curswant =
1572 curwin->w_virtcol + redo_VIsual_vcol - 1;
1573 }
1574 else
1575 curwin->w_curswant = redo_VIsual_vcol;
1576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001578 {
1579 curwin->w_curswant = MAXCOL;
1580 }
1581 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 }
1583 cap->count0 = redo_VIsual_count;
1584 if (redo_VIsual_count != 0)
1585 cap->count1 = redo_VIsual_count;
1586 else
1587 cap->count1 = 1;
1588 }
1589 else if (VIsual_active)
1590 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001591 if (!gui_yank)
1592 {
1593 /* Save the current VIsual area for '< and '> marks, and "gv" */
1594 curbuf->b_visual.vi_start = VIsual;
1595 curbuf->b_visual.vi_end = curwin->w_cursor;
1596 curbuf->b_visual.vi_mode = VIsual_mode;
1597 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001599 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602
1603 /* In Select mode, a linewise selection is operated upon like a
1604 * characterwise selection. */
1605 if (VIsual_select && VIsual_mode == 'V')
1606 {
1607 if (lt(VIsual, curwin->w_cursor))
1608 {
1609 VIsual.col = 0;
1610 curwin->w_cursor.col =
1611 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1612 }
1613 else
1614 {
1615 curwin->w_cursor.col = 0;
1616 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1617 }
1618 VIsual_mode = 'v';
1619 }
1620 /* If 'selection' is "exclusive", backup one character for
1621 * charwise selections. */
1622 else if (VIsual_mode == 'v')
1623 {
1624# ifdef FEAT_VIRTUALEDIT
1625 include_line_break =
1626# endif
1627 unadjust_for_sel();
1628 }
1629
1630 oap->start = VIsual;
1631 if (VIsual_mode == 'V')
1632 oap->start.col = 0;
1633 }
1634#endif /* FEAT_VISUAL */
1635
1636 /*
1637 * Set oap->start to the first position of the operated text, oap->end
1638 * to the end of the operated text. w_cursor is equal to oap->start.
1639 */
1640 if (lt(oap->start, curwin->w_cursor))
1641 {
1642#ifdef FEAT_FOLDING
1643 /* Include folded lines completely. */
1644 if (!VIsual_active)
1645 {
1646 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1647 oap->start.col = 0;
1648 if (hasFolding(curwin->w_cursor.lnum, NULL,
1649 &curwin->w_cursor.lnum))
1650 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1651 }
1652#endif
1653 oap->end = curwin->w_cursor;
1654 curwin->w_cursor = oap->start;
1655
1656 /* w_virtcol may have been updated; if the cursor goes back to its
1657 * previous position w_virtcol becomes invalid and isn't updated
1658 * automatically. */
1659 curwin->w_valid &= ~VALID_VIRTCOL;
1660 }
1661 else
1662 {
1663#ifdef FEAT_FOLDING
1664 /* Include folded lines completely. */
1665 if (!VIsual_active && oap->motion_type == MLINE)
1666 {
1667 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1668 NULL))
1669 curwin->w_cursor.col = 0;
1670 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1671 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1672 }
1673#endif
1674 oap->end = oap->start;
1675 oap->start = curwin->w_cursor;
1676 }
1677
1678 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1679
1680#ifdef FEAT_VIRTUALEDIT
1681 /* Set "virtual_op" before resetting VIsual_active. */
1682 virtual_op = virtual_active();
1683#endif
1684
1685#ifdef FEAT_VISUAL
1686 if (VIsual_active || redo_VIsual_busy)
1687 {
1688 if (VIsual_mode == Ctrl_V) /* block mode */
1689 {
1690 colnr_T start, end;
1691
1692 oap->block_mode = TRUE;
1693
1694 getvvcol(curwin, &(oap->start),
1695 &oap->start_vcol, NULL, &oap->end_vcol);
1696 if (!redo_VIsual_busy)
1697 {
1698 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1699
1700 if (start < oap->start_vcol)
1701 oap->start_vcol = start;
1702 if (end > oap->end_vcol)
1703 {
1704 if (*p_sel == 'e' && start >= 1
1705 && start - 1 >= oap->end_vcol)
1706 oap->end_vcol = start - 1;
1707 else
1708 oap->end_vcol = end;
1709 }
1710 }
1711
1712 /* if '$' was used, get oap->end_vcol from longest line */
1713 if (curwin->w_curswant == MAXCOL)
1714 {
1715 curwin->w_cursor.col = MAXCOL;
1716 oap->end_vcol = 0;
1717 for (curwin->w_cursor.lnum = oap->start.lnum;
1718 curwin->w_cursor.lnum <= oap->end.lnum;
1719 ++curwin->w_cursor.lnum)
1720 {
1721 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1722 if (end > oap->end_vcol)
1723 oap->end_vcol = end;
1724 }
1725 }
1726 else if (redo_VIsual_busy)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001727 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 /*
1729 * Correct oap->end.col and oap->start.col to be the
1730 * upper-left and lower-right corner of the block area.
1731 *
1732 * (Actually, this does convert column positions into character
1733 * positions)
1734 */
1735 curwin->w_cursor.lnum = oap->end.lnum;
1736 coladvance(oap->end_vcol);
1737 oap->end = curwin->w_cursor;
1738
1739 curwin->w_cursor = oap->start;
1740 coladvance(oap->start_vcol);
1741 oap->start = curwin->w_cursor;
1742 }
1743
1744 if (!redo_VIsual_busy && !gui_yank)
1745 {
1746 /*
1747 * Prepare to reselect and redo Visual: this is based on the
1748 * size of the Visual text
1749 */
1750 resel_VIsual_mode = VIsual_mode;
1751 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001752 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001754 {
1755 if (VIsual_mode != Ctrl_V)
1756 getvvcol(curwin, &(oap->end),
1757 NULL, NULL, &oap->end_vcol);
1758 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1759 {
1760 if (VIsual_mode != Ctrl_V)
1761 getvvcol(curwin, &(oap->start),
1762 &oap->start_vcol, NULL, NULL);
1763 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1764 }
1765 else
1766 resel_VIsual_vcol = oap->end_vcol;
1767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 resel_VIsual_line_count = oap->line_count;
1769 }
1770
1771 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1772 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1773 && oap->op_type != OP_COLON
1774#ifdef FEAT_FOLDING
1775 && oap->op_type != OP_FOLD
1776 && oap->op_type != OP_FOLDOPEN
1777 && oap->op_type != OP_FOLDOPENREC
1778 && oap->op_type != OP_FOLDCLOSE
1779 && oap->op_type != OP_FOLDCLOSEREC
1780 && oap->op_type != OP_FOLDDEL
1781 && oap->op_type != OP_FOLDDELREC
1782#endif
1783 && oap->motion_force == NUL
1784 )
1785 {
1786 /* Prepare for redoing. Only use the nchar field for "r",
1787 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001788 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1789 || cap->nchar == 'N'))
1790 /* "gn" and "gN" are a bit different */
1791 prep_redo(oap->regname, 0L, NUL, cap->cmdchar, cap->nchar,
1792 get_op_char(oap->op_type),
1793 get_extra_op_char(oap->op_type));
1794 else
1795 prep_redo(oap->regname, 0L, NUL, 'v',
1796 get_op_char(oap->op_type),
1797 get_extra_op_char(oap->op_type),
1798 oap->op_type == OP_REPLACE
1799 ? cap->nchar : NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 if (!redo_VIsual_busy)
1801 {
1802 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001803 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 redo_VIsual_line_count = resel_VIsual_line_count;
1805 redo_VIsual_count = cap->count0;
1806 }
1807 }
1808
1809 /*
1810 * oap->inclusive defaults to TRUE.
1811 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1812 * FALSE. This makes "d}P" and "v}dP" work the same.
1813 */
1814 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1815 oap->inclusive = TRUE;
1816 if (VIsual_mode == 'V')
1817 oap->motion_type = MLINE;
1818 else
1819 {
1820 oap->motion_type = MCHAR;
1821 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1822# ifdef FEAT_VIRTUALEDIT
1823 && (include_line_break || !virtual_op)
1824# endif
1825 )
1826 {
1827 oap->inclusive = FALSE;
1828 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001829 * that works on lines only. */
1830 if (*p_sel != 'o' && !op_on_lines(oap->op_type))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001832 if (oap->end.lnum < curbuf->b_ml.ml_line_count)
1833 {
1834 ++oap->end.lnum;
1835 oap->end.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001837 oap->end.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838# endif
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001839 ++oap->line_count;
1840 }
1841 else
1842 {
1843 /* Cannot move below the last line, make the op
1844 * inclusive to tell the operation to include the
1845 * line break. */
1846 oap->inclusive = TRUE;
1847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 }
1849 }
1850 }
1851
1852 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001853
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 /*
1855 * Switch Visual off now, so screen updating does
1856 * not show inverted text when the screen is redrawn.
1857 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1858 * no screen redraw, so it is done here to remove the inverted
1859 * part.
1860 */
1861 if (!gui_yank)
1862 {
1863 VIsual_active = FALSE;
1864# ifdef FEAT_MOUSE
1865 setmouse();
1866 mouse_dragging = 0;
1867# endif
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001868 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 clear_cmdline = TRUE; /* unshow visual mode later */
1870#ifdef FEAT_CMDL_INFO
1871 else
1872 clear_showcmd();
1873#endif
1874 if ((oap->op_type == OP_YANK
1875 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001876 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 || oap->op_type == OP_FILTER)
1878 && oap->motion_force == NUL)
1879 redraw_curbuf_later(INVERTED);
1880 }
1881 }
1882#endif
1883
1884#ifdef FEAT_MBYTE
1885 /* Include the trailing byte of a multi-byte char. */
1886 if (has_mbyte && oap->inclusive)
1887 {
1888 int l;
1889
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001890 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (l > 1)
1892 oap->end.col += l - 1;
1893 }
1894#endif
1895 curwin->w_set_curswant = TRUE;
1896
1897 /*
1898 * oap->empty is set when start and end are the same. The inclusive
1899 * flag affects this too, unless yanking and the end is on a NUL.
1900 */
1901 oap->empty = (oap->motion_type == MCHAR
1902 && (!oap->inclusive
1903 || (oap->op_type == OP_YANK
1904 && gchar_pos(&oap->end) == NUL))
1905 && equalpos(oap->start, oap->end)
1906#ifdef FEAT_VIRTUALEDIT
1907 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1908#endif
1909 );
1910 /*
1911 * For delete, change and yank, it's an error to operate on an
1912 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1913 */
1914 empty_region_error = (oap->empty
1915 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1916
1917#ifdef FEAT_VISUAL
1918 /* Force a redraw when operating on an empty Visual region, when
1919 * 'modifiable is off or creating a fold. */
1920 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1921# ifdef FEAT_FOLDING
1922 || oap->op_type == OP_FOLD
1923# endif
1924 ))
1925 redraw_curbuf_later(INVERTED);
1926#endif
1927
1928 /*
1929 * If the end of an operator is in column one while oap->motion_type
1930 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1931 * character in the previous line. If op_start is on or before the
1932 * first non-blank in the line, the operator becomes linewise
1933 * (strange, but that's the way vi does it).
1934 */
1935 if ( oap->motion_type == MCHAR
1936 && oap->inclusive == FALSE
1937 && !(cap->retval & CA_NO_ADJ_OP_END)
1938 && oap->end.col == 0
1939#ifdef FEAT_VISUAL
1940 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001941 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942#endif
1943 && oap->line_count > 1)
1944 {
1945 oap->end_adjusted = TRUE; /* remember that we did this */
1946 --oap->line_count;
1947 --oap->end.lnum;
1948 if (inindent(0))
1949 oap->motion_type = MLINE;
1950 else
1951 {
1952 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1953 if (oap->end.col)
1954 {
1955 --oap->end.col;
1956 oap->inclusive = TRUE;
1957 }
1958 }
1959 }
1960 else
1961 oap->end_adjusted = FALSE;
1962
1963 switch (oap->op_type)
1964 {
1965 case OP_LSHIFT:
1966 case OP_RSHIFT:
1967 op_shift(oap, TRUE,
1968#ifdef FEAT_VISUAL
1969 oap->is_VIsual ? (int)cap->count1 :
1970#endif
1971 1);
1972 auto_format(FALSE, TRUE);
1973 break;
1974
1975 case OP_JOIN_NS:
1976 case OP_JOIN:
1977 if (oap->line_count < 2)
1978 oap->line_count = 2;
1979 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1980 curbuf->b_ml.ml_line_count)
1981 beep_flush();
1982 else
1983 {
Bram Moolenaar81340392012-06-06 16:12:59 +02001984 (void)do_join(oap->line_count, oap->op_type == OP_JOIN, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 auto_format(FALSE, TRUE);
1986 }
1987 break;
1988
1989 case OP_DELETE:
1990#ifdef FEAT_VISUAL
1991 VIsual_reselect = FALSE; /* don't reselect now */
1992#endif
1993 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001994 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001996 CancelRedo();
1997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 else
1999 {
2000 (void)op_delete(oap);
2001 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
2002 u_save_cursor(); /* cursor line wasn't saved yet */
2003 auto_format(FALSE, TRUE);
2004 }
2005 break;
2006
2007 case OP_YANK:
2008 if (empty_region_error)
2009 {
2010 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002011 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002013 CancelRedo();
2014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 }
2016 else
2017 (void)op_yank(oap, FALSE, !gui_yank);
2018 check_cursor_col();
2019 break;
2020
2021 case OP_CHANGE:
2022#ifdef FEAT_VISUAL
2023 VIsual_reselect = FALSE; /* don't reselect now */
2024#endif
2025 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002026 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002028 CancelRedo();
2029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 else
2031 {
2032 /* This is a new edit command, not a restart. Need to
2033 * remember it to make 'insertmode' work with mappings for
2034 * Visual mode. But do this only once and not when typed and
2035 * 'insertmode' isn't set. */
2036 if (p_im || !KeyTyped)
2037 restart_edit_save = restart_edit;
2038 else
2039 restart_edit_save = 0;
2040 restart_edit = 0;
2041 /* Reset finish_op now, don't want it set inside edit(). */
2042 finish_op = FALSE;
2043 if (op_change(oap)) /* will call edit() */
2044 cap->retval |= CA_COMMAND_BUSY;
2045 if (restart_edit == 0)
2046 restart_edit = restart_edit_save;
2047 }
2048 break;
2049
2050 case OP_FILTER:
2051 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
2052 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
2053 else
2054 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
2055
2056 case OP_INDENT:
2057 case OP_COLON:
2058
2059#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2060 /*
2061 * If 'equalprg' is empty, do the indenting internally.
2062 */
2063 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
2064 {
2065# ifdef FEAT_LISP
2066 if (curbuf->b_p_lisp)
2067 {
2068 op_reindent(oap, get_lisp_indent);
2069 break;
2070 }
2071# endif
2072# ifdef FEAT_CINDENT
2073 op_reindent(oap,
2074# ifdef FEAT_EVAL
2075 *curbuf->b_p_inde != NUL ? get_expr_indent :
2076# endif
2077 get_c_indent);
2078 break;
2079# endif
2080 }
2081#endif
2082
2083 op_colon(oap);
2084 break;
2085
2086 case OP_TILDE:
2087 case OP_UPPER:
2088 case OP_LOWER:
2089 case OP_ROT13:
2090 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002091 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002093 CancelRedo();
2094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 else
2096 op_tilde(oap);
2097 check_cursor_col();
2098 break;
2099
2100 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002101#if defined(FEAT_EVAL)
2102 if (*curbuf->b_p_fex != NUL)
2103 op_formatexpr(oap); /* use expression */
2104 else
2105#endif
2106 if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 op_colon(oap); /* use external command */
2108 else
2109 op_format(oap, FALSE); /* use internal function */
2110 break;
2111
2112 case OP_FORMAT2:
2113 op_format(oap, TRUE); /* use internal function */
2114 break;
2115
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002116 case OP_FUNCTION:
2117 op_function(oap); /* call 'operatorfunc' */
2118 break;
2119
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 case OP_INSERT:
2121 case OP_APPEND:
2122#ifdef FEAT_VISUAL
2123 VIsual_reselect = FALSE; /* don't reselect now */
2124#endif
2125#ifdef FEAT_VISUALEXTRA
2126 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002127 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002129 CancelRedo();
2130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 else
2132 {
2133 /* This is a new edit command, not a restart. Need to
2134 * remember it to make 'insertmode' work with mappings for
2135 * Visual mode. But do this only once. */
2136 restart_edit_save = restart_edit;
2137 restart_edit = 0;
2138
2139 op_insert(oap, cap->count1);
2140
2141 /* TODO: when inserting in several lines, should format all
2142 * the lines. */
2143 auto_format(FALSE, TRUE);
2144
2145 if (restart_edit == 0)
2146 restart_edit = restart_edit_save;
2147 }
2148#else
2149 vim_beep();
2150#endif
2151 break;
2152
2153 case OP_REPLACE:
2154#ifdef FEAT_VISUAL
2155 VIsual_reselect = FALSE; /* don't reselect now */
2156#endif
2157#ifdef FEAT_VISUALEXTRA
2158 if (empty_region_error)
2159#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002160 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002162 CancelRedo();
2163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164#ifdef FEAT_VISUALEXTRA
2165 else
2166 op_replace(oap, cap->nchar);
2167#endif
2168 break;
2169
2170#ifdef FEAT_FOLDING
2171 case OP_FOLD:
2172 VIsual_reselect = FALSE; /* don't reselect now */
2173 foldCreate(oap->start.lnum, oap->end.lnum);
2174 break;
2175
2176 case OP_FOLDOPEN:
2177 case OP_FOLDOPENREC:
2178 case OP_FOLDCLOSE:
2179 case OP_FOLDCLOSEREC:
2180 VIsual_reselect = FALSE; /* don't reselect now */
2181 opFoldRange(oap->start.lnum, oap->end.lnum,
2182 oap->op_type == OP_FOLDOPEN
2183 || oap->op_type == OP_FOLDOPENREC,
2184 oap->op_type == OP_FOLDOPENREC
2185 || oap->op_type == OP_FOLDCLOSEREC,
2186 oap->is_VIsual);
2187 break;
2188
2189 case OP_FOLDDEL:
2190 case OP_FOLDDELREC:
2191 VIsual_reselect = FALSE; /* don't reselect now */
2192 deleteFold(oap->start.lnum, oap->end.lnum,
2193 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2194 break;
2195#endif
2196 default:
2197 clearopbeep(oap);
2198 }
2199#ifdef FEAT_VIRTUALEDIT
2200 virtual_op = MAYBE;
2201#endif
2202 if (!gui_yank)
2203 {
2204 /*
2205 * if 'sol' not set, go back to old column for some commands
2206 */
2207 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2208 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2209 || oap->op_type == OP_DELETE))
2210 coladvance(curwin->w_curswant = old_col);
2211 }
2212 else
2213 {
2214 curwin->w_cursor = old_cursor;
2215 }
2216#ifdef FEAT_VISUAL
2217 oap->block_mode = FALSE;
2218#endif
2219 clearop(oap);
2220 }
2221}
2222
2223/*
2224 * Handle indent and format operators and visual mode ":".
2225 */
2226 static void
2227op_colon(oap)
2228 oparg_T *oap;
2229{
2230 stuffcharReadbuff(':');
2231#ifdef FEAT_VISUAL
2232 if (oap->is_VIsual)
2233 stuffReadbuff((char_u *)"'<,'>");
2234 else
2235#endif
2236 {
2237 /*
2238 * Make the range look nice, so it can be repeated.
2239 */
2240 if (oap->start.lnum == curwin->w_cursor.lnum)
2241 stuffcharReadbuff('.');
2242 else
2243 stuffnumReadbuff((long)oap->start.lnum);
2244 if (oap->end.lnum != oap->start.lnum)
2245 {
2246 stuffcharReadbuff(',');
2247 if (oap->end.lnum == curwin->w_cursor.lnum)
2248 stuffcharReadbuff('.');
2249 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2250 stuffcharReadbuff('$');
2251 else if (oap->start.lnum == curwin->w_cursor.lnum)
2252 {
2253 stuffReadbuff((char_u *)".+");
2254 stuffnumReadbuff((long)oap->line_count - 1);
2255 }
2256 else
2257 stuffnumReadbuff((long)oap->end.lnum);
2258 }
2259 }
2260 if (oap->op_type != OP_COLON)
2261 stuffReadbuff((char_u *)"!");
2262 if (oap->op_type == OP_INDENT)
2263 {
2264#ifndef FEAT_CINDENT
2265 if (*get_equalprg() == NUL)
2266 stuffReadbuff((char_u *)"indent");
2267 else
2268#endif
2269 stuffReadbuff(get_equalprg());
2270 stuffReadbuff((char_u *)"\n");
2271 }
2272 else if (oap->op_type == OP_FORMAT)
2273 {
2274 if (*p_fp == NUL)
2275 stuffReadbuff((char_u *)"fmt");
2276 else
2277 stuffReadbuff(p_fp);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002278 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 }
2280
2281 /*
2282 * do_cmdline() does the rest
2283 */
2284}
2285
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002286/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002287 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002288 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002289 static void
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002290op_function(oap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002291 oparg_T *oap UNUSED;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002292{
2293#ifdef FEAT_EVAL
2294 char_u *(argv[1]);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002295# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002296 int save_virtual_op = virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002297# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002298
2299 if (*p_opfunc == NUL)
2300 EMSG(_("E774: 'operatorfunc' is empty"));
2301 else
2302 {
2303 /* Set '[ and '] marks to text to be operated on. */
2304 curbuf->b_op_start = oap->start;
2305 curbuf->b_op_end = oap->end;
2306 if (oap->motion_type != MLINE && !oap->inclusive)
2307 /* Exclude the end position. */
2308 decl(&curbuf->b_op_end);
2309
2310 if (oap->block_mode)
2311 argv[0] = (char_u *)"block";
2312 else if (oap->motion_type == MLINE)
2313 argv[0] = (char_u *)"line";
2314 else
2315 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002316
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002317# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002318 /* Reset virtual_op so that 'virtualedit' can be changed in the
2319 * function. */
2320 virtual_op = MAYBE;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002321# endif
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002322
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002323 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002324
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002325# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002326 virtual_op = save_virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002327# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002328 }
2329#else
2330 EMSG(_("E775: Eval feature not available"));
2331#endif
2332}
2333
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334#if defined(FEAT_MOUSE) || defined(PROTO)
2335/*
2336 * Do the appropriate action for the current mouse click in the current mode.
2337 * Not used for Command-line mode.
2338 *
2339 * Normal Mode:
2340 * event modi- position visual change action
2341 * fier cursor window
2342 * left press - yes end yes
2343 * left press C yes end yes "^]" (2)
2344 * left press S yes end yes "*" (2)
2345 * left drag - yes start if moved no
2346 * left relse - yes start if moved no
2347 * middle press - yes if not active no put register
2348 * middle press - yes if active no yank and put
2349 * right press - yes start or extend yes
2350 * right press S yes no change yes "#" (2)
2351 * right drag - yes extend no
2352 * right relse - yes extend no
2353 *
2354 * Insert or Replace Mode:
2355 * event modi- position visual change action
2356 * fier cursor window
2357 * left press - yes (cannot be active) yes
2358 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2359 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2360 * left drag - yes start or extend (1) no CTRL-O (1)
2361 * left relse - yes start or extend (1) no CTRL-O (1)
2362 * middle press - no (cannot be active) no put register
2363 * right press - yes start or extend yes CTRL-O
2364 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2365 *
2366 * (1) only if mouse pointer moved since press
2367 * (2) only if click is in same buffer
2368 *
2369 * Return TRUE if start_arrow() should be called for edit mode.
2370 */
2371 int
2372do_mouse(oap, c, dir, count, fixindent)
2373 oparg_T *oap; /* operator argument, can be NULL */
2374 int c; /* K_LEFTMOUSE, etc */
2375 int dir; /* Direction to 'put' if necessary */
2376 long count;
2377 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2378{
2379 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2380 static int got_click = FALSE; /* got a click some time back */
2381
2382 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2383 int is_click; /* If FALSE it's a drag or release event */
2384 int is_drag; /* If TRUE it's a drag event */
2385 int jump_flags = 0; /* flags for jump_to_mouse() */
2386 pos_T start_visual;
2387 int moved; /* Has cursor moved? */
2388 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002389#ifdef FEAT_WINDOWS
2390 static int in_tab_line = FALSE; /* mouse clicked in tab line */
2391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392#ifdef FEAT_VERTSPLIT
2393 int in_sep_line; /* mouse in vertical separator line */
2394#endif
2395 int c1, c2;
2396#if defined(FEAT_FOLDING)
2397 pos_T save_cursor;
2398#endif
2399 win_T *old_curwin = curwin;
2400#ifdef FEAT_VISUAL
2401 static pos_T orig_cursor;
2402 colnr_T leftcol, rightcol;
2403 pos_T end_visual;
2404 int diff;
2405 int old_active = VIsual_active;
2406 int old_mode = VIsual_mode;
2407#endif
2408 int regname;
2409
2410#if defined(FEAT_FOLDING)
2411 save_cursor = curwin->w_cursor;
2412#endif
2413
2414 /*
2415 * When GUI is active, always recognize mouse events, otherwise:
2416 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2417 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2418 * - For command line and insert mode 'mouse' is checked before calling
2419 * do_mouse().
2420 */
2421 if (do_always)
2422 do_always = FALSE;
2423 else
2424#ifdef FEAT_GUI
2425 if (!gui.in_use)
2426#endif
2427 {
2428#ifdef FEAT_VISUAL
2429 if (VIsual_active)
2430 {
2431 if (!mouse_has(MOUSE_VISUAL))
2432 return FALSE;
2433 }
2434 else
2435#endif
2436 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2437 return FALSE;
2438 }
2439
2440 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2441
2442#ifdef FEAT_MOUSESHAPE
2443 /* May have stopped dragging the status or separator line. The pointer is
2444 * most likely still on the status or separator line. */
2445 if (!is_drag && drag_status_line)
2446 {
2447 drag_status_line = FALSE;
2448 update_mouseshape(SHAPE_IDX_STATUS);
2449 }
2450# ifdef FEAT_VERTSPLIT
2451 if (!is_drag && drag_sep_line)
2452 {
2453 drag_sep_line = FALSE;
2454 update_mouseshape(SHAPE_IDX_VSEP);
2455 }
2456# endif
2457#endif
2458
2459 /*
2460 * Ignore drag and release events if we didn't get a click.
2461 */
2462 if (is_click)
2463 got_click = TRUE;
2464 else
2465 {
2466 if (!got_click) /* didn't get click, ignore */
2467 return FALSE;
2468 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002469 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002471#ifdef FEAT_WINDOWS
2472 if (in_tab_line)
2473 {
2474 in_tab_line = FALSE;
2475 return FALSE;
2476 }
2477#endif
2478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 }
2480
Bram Moolenaar64969662005-12-14 21:59:55 +00002481#ifndef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 /*
Bram Moolenaar64969662005-12-14 21:59:55 +00002483 * ALT is only used for starging/extending Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 */
2485 if ((mod_mask & MOD_MASK_ALT))
2486 return FALSE;
Bram Moolenaar64969662005-12-14 21:59:55 +00002487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488
2489 /*
2490 * CTRL right mouse button does CTRL-T
2491 */
2492 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2493 {
2494 if (State & INSERT)
2495 stuffcharReadbuff(Ctrl_O);
2496 if (count > 1)
2497 stuffnumReadbuff(count);
2498 stuffcharReadbuff(Ctrl_T);
2499 got_click = FALSE; /* ignore drag&release now */
2500 return FALSE;
2501 }
2502
2503 /*
2504 * CTRL only works with left mouse button
2505 */
2506 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2507 return FALSE;
2508
2509 /*
2510 * When a modifier is down, ignore drag and release events, as well as
2511 * multiple clicks and the middle mouse button.
2512 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2513 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002514 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2515 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 && (!is_click
2517 || (mod_mask & MOD_MASK_MULTI_CLICK)
2518 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002519 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 && mouse_model_popup()
2521 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002522 && !((mod_mask & MOD_MASK_ALT)
2523 && !mouse_model_popup()
2524 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 )
2526 return FALSE;
2527
2528 /*
2529 * If the button press was used as the movement command for an operator
2530 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2531 * drag/release events.
2532 */
2533 if (!is_click && which_button == MOUSE_MIDDLE)
2534 return FALSE;
2535
2536 if (oap != NULL)
2537 regname = oap->regname;
2538 else
2539 regname = 0;
2540
2541 /*
2542 * Middle mouse button does a 'put' of the selected text
2543 */
2544 if (which_button == MOUSE_MIDDLE)
2545 {
2546 if (State == NORMAL)
2547 {
2548 /*
2549 * If an operator was pending, we don't know what the user wanted
2550 * to do. Go back to normal mode: Clear the operator and beep().
2551 */
2552 if (oap != NULL && oap->op_type != OP_NOP)
2553 {
2554 clearopbeep(oap);
2555 return FALSE;
2556 }
2557
2558#ifdef FEAT_VISUAL
2559 /*
2560 * If visual was active, yank the highlighted text and put it
2561 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002562 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 */
2564 if (VIsual_active)
2565 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002566 if (VIsual_select)
2567 {
2568 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002569 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002570 }
2571 else
2572 {
2573 stuffcharReadbuff('y');
2574 stuffcharReadbuff(K_MIDDLEMOUSE);
2575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 do_always = TRUE; /* ignore 'mouse' setting next time */
2577 return FALSE;
2578 }
2579#endif
2580 /*
2581 * The rest is below jump_to_mouse()
2582 */
2583 }
2584
2585 else if ((State & INSERT) == 0)
2586 return FALSE;
2587
2588 /*
2589 * Middle click in insert mode doesn't move the mouse, just insert the
2590 * contents of a register. '.' register is special, can't insert that
2591 * with do_put().
2592 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2593 * happens for the GUI).
2594 */
2595 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2596 {
2597 if (regname == '.')
2598 insert_reg(regname, TRUE);
2599 else
2600 {
2601#ifdef FEAT_CLIPBOARD
2602 if (clip_star.available && regname == 0)
2603 regname = '*';
2604#endif
2605 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2606 insert_reg(regname, TRUE);
2607 else
2608 {
2609 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2610
2611 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2612 AppendCharToRedobuff(Ctrl_R);
2613 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2614 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2615 }
2616 }
2617 return FALSE;
2618 }
2619 }
2620
2621 /* When dragging or button-up stay in the same window. */
2622 if (!is_click)
2623 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2624
2625 start_visual.lnum = 0;
2626
Bram Moolenaarf740b292006-02-16 22:11:02 +00002627#ifdef FEAT_WINDOWS
2628 /* Check for clicking in the tab page line. */
2629 if (mouse_row == 0 && firstwin->w_winrow > 0)
2630 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002631 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002632 {
2633 if (in_tab_line)
2634 {
2635 c1 = TabPageIdxs[mouse_col];
2636 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2637 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002638 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002639 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002640
Bram Moolenaarf740b292006-02-16 22:11:02 +00002641 /* click in a tab selects that tab page */
2642 if (is_click
2643# ifdef FEAT_CMDWIN
2644 && cmdwin_type == 0
2645# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002646 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002647 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002648 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002649 c1 = TabPageIdxs[mouse_col];
2650 if (c1 >= 0)
2651 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002652 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2653 {
2654 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002655 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002656 tabpage_new();
2657 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2658 }
2659 else
2660 {
2661 /* Go to specified tab page, or next one if not clicking
2662 * on a label. */
2663 goto_tabpage(c1);
2664
2665 /* It's like clicking on the status line of a window. */
2666 if (curwin != old_curwin)
2667 end_visual_mode();
2668 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002669 }
2670 else if (c1 < 0)
2671 {
2672 tabpage_T *tp;
2673
2674 /* Close the current or specified tab page. */
2675 if (c1 == -999)
2676 tp = curtab;
2677 else
2678 tp = find_tabpage(-c1);
2679 if (tp == curtab)
2680 {
2681 if (first_tabpage->tp_next != NULL)
2682 tabpage_close(FALSE);
2683 }
2684 else if (tp != NULL)
2685 tabpage_close_other(tp, FALSE);
2686 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002687 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002688 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002689 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002690 else if (is_drag && in_tab_line)
2691 {
2692 c1 = TabPageIdxs[mouse_col];
2693 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2694 return FALSE;
2695 }
2696
Bram Moolenaarf740b292006-02-16 22:11:02 +00002697#endif
2698
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 /*
2700 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2701 * right button up -> pop-up menu
2702 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002703 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 */
2705 if (mouse_model_popup())
2706 {
2707 if (which_button == MOUSE_RIGHT
2708 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2709 {
2710 /*
2711 * NOTE: Ignore right button down and drag mouse events.
2712 * Windows only shows the popup menu on the button up event.
2713 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002714#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2715 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 if (!is_click)
2717 return FALSE;
2718#endif
2719#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2720 if (is_click || is_drag)
2721 return FALSE;
2722#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002723#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2725 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2726 if (gui.in_use)
2727 {
2728 jump_flags = 0;
2729 if (STRCMP(p_mousem, "popup_setpos") == 0)
2730 {
2731 /* First set the cursor position before showing the popup
2732 * menu. */
2733#ifdef FEAT_VISUAL
2734 if (VIsual_active)
2735 {
2736 pos_T m_pos;
2737
2738 /*
2739 * set MOUSE_MAY_STOP_VIS if we are outside the
2740 * selection or the current window (might have false
2741 * negative here)
2742 */
2743 if (mouse_row < W_WINROW(curwin)
2744 || mouse_row
2745 > (W_WINROW(curwin) + curwin->w_height))
2746 jump_flags = MOUSE_MAY_STOP_VIS;
2747 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2748 jump_flags = MOUSE_MAY_STOP_VIS;
2749 else
2750 {
2751 if ((lt(curwin->w_cursor, VIsual)
2752 && (lt(m_pos, curwin->w_cursor)
2753 || lt(VIsual, m_pos)))
2754 || (lt(VIsual, curwin->w_cursor)
2755 && (lt(m_pos, VIsual)
2756 || lt(curwin->w_cursor, m_pos))))
2757 {
2758 jump_flags = MOUSE_MAY_STOP_VIS;
2759 }
2760 else if (VIsual_mode == Ctrl_V)
2761 {
2762 getvcols(curwin, &curwin->w_cursor, &VIsual,
2763 &leftcol, &rightcol);
2764 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2765 if (m_pos.col < leftcol || m_pos.col > rightcol)
2766 jump_flags = MOUSE_MAY_STOP_VIS;
2767 }
2768 }
2769 }
2770 else
2771 jump_flags = MOUSE_MAY_STOP_VIS;
2772#endif
2773 }
2774 if (jump_flags)
2775 {
2776 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2777 update_curbuf(
2778#ifdef FEAT_VISUAL
2779 VIsual_active ? INVERTED :
2780#endif
2781 VALID);
2782 setcursor();
2783 out_flush(); /* Update before showing popup menu */
2784 }
2785# ifdef FEAT_MENU
2786 gui_show_popupmenu();
2787# endif
2788 return (jump_flags & CURSOR_MOVED) != 0;
2789 }
2790 else
2791 return FALSE;
2792#else
2793 return FALSE;
2794#endif
2795 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002796 if (which_button == MOUSE_LEFT
2797 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 {
2799 which_button = MOUSE_RIGHT;
2800 mod_mask &= ~MOD_MASK_SHIFT;
2801 }
2802 }
2803
2804#ifdef FEAT_VISUAL
2805 if ((State & (NORMAL | INSERT))
2806 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2807 {
2808 if (which_button == MOUSE_LEFT)
2809 {
2810 if (is_click)
2811 {
2812 /* stop Visual mode for a left click in a window, but not when
2813 * on a status line */
2814 if (VIsual_active)
2815 jump_flags |= MOUSE_MAY_STOP_VIS;
2816 }
2817 else if (mouse_has(MOUSE_VISUAL))
2818 jump_flags |= MOUSE_MAY_VIS;
2819 }
2820 else if (which_button == MOUSE_RIGHT)
2821 {
2822 if (is_click && VIsual_active)
2823 {
2824 /*
2825 * Remember the start and end of visual before moving the
2826 * cursor.
2827 */
2828 if (lt(curwin->w_cursor, VIsual))
2829 {
2830 start_visual = curwin->w_cursor;
2831 end_visual = VIsual;
2832 }
2833 else
2834 {
2835 start_visual = VIsual;
2836 end_visual = curwin->w_cursor;
2837 }
2838 }
2839 jump_flags |= MOUSE_FOCUS;
2840 if (mouse_has(MOUSE_VISUAL))
2841 jump_flags |= MOUSE_MAY_VIS;
2842 }
2843 }
2844#endif
2845
2846 /*
2847 * If an operator is pending, ignore all drags and releases until the
2848 * next mouse click.
2849 */
2850 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2851 {
2852 got_click = FALSE;
2853 oap->motion_type = MCHAR;
2854 }
2855
2856 /* When releasing the button let jump_to_mouse() know. */
2857 if (!is_click && !is_drag)
2858 jump_flags |= MOUSE_RELEASED;
2859
2860 /*
2861 * JUMP!
2862 */
2863 jump_flags = jump_to_mouse(jump_flags,
2864 oap == NULL ? NULL : &(oap->inclusive), which_button);
2865 moved = (jump_flags & CURSOR_MOVED);
2866 in_status_line = (jump_flags & IN_STATUS_LINE);
2867#ifdef FEAT_VERTSPLIT
2868 in_sep_line = (jump_flags & IN_SEP_LINE);
2869#endif
2870
2871#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002872 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2874 {
2875 int key = KEY2TERMCAP1(c);
2876
2877 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2878 || key == (int)KE_RIGHTRELEASE)
2879 netbeans_button_release(which_button);
2880 }
2881#endif
2882
2883 /* When jumping to another window, clear a pending operator. That's a bit
2884 * friendlier than beeping and not jumping to that window. */
2885 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2886 clearop(oap);
2887
2888#ifdef FEAT_FOLDING
2889 if (mod_mask == 0
2890 && !is_drag
2891 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2892 && which_button == MOUSE_LEFT)
2893 {
2894 /* open or close a fold at this line */
2895 if (jump_flags & MOUSE_FOLD_OPEN)
2896 openFold(curwin->w_cursor.lnum, 1L);
2897 else
2898 closeFold(curwin->w_cursor.lnum, 1L);
2899 /* don't move the cursor if still in the same window */
2900 if (curwin == old_curwin)
2901 curwin->w_cursor = save_cursor;
2902 }
2903#endif
2904
2905#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2906 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2907 {
2908 clip_modeless(which_button, is_click, is_drag);
2909 return FALSE;
2910 }
2911#endif
2912
2913#ifdef FEAT_VISUAL
2914 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002915 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 if (VIsual_active && is_drag && p_so)
2917 {
2918 /* In the very first line, allow scrolling one line */
2919 if (mouse_row == 0)
2920 mouse_dragging = 2;
2921 else
2922 mouse_dragging = 1;
2923 }
2924
2925 /* When dragging the mouse above the window, scroll down. */
2926 if (is_drag && mouse_row < 0 && !in_status_line)
2927 {
2928 scroll_redraw(FALSE, 1L);
2929 mouse_row = 0;
2930 }
2931
2932 if (start_visual.lnum) /* right click in visual mode */
2933 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002934 /* When ALT is pressed make Visual mode blockwise. */
2935 if (mod_mask & MOD_MASK_ALT)
2936 VIsual_mode = Ctrl_V;
2937
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 /*
2939 * In Visual-block mode, divide the area in four, pick up the corner
2940 * that is in the quarter that the cursor is in.
2941 */
2942 if (VIsual_mode == Ctrl_V)
2943 {
2944 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2945 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2946 end_visual.col = leftcol;
2947 else
2948 end_visual.col = rightcol;
2949 if (curwin->w_cursor.lnum <
2950 (start_visual.lnum + end_visual.lnum) / 2)
2951 end_visual.lnum = end_visual.lnum;
2952 else
2953 end_visual.lnum = start_visual.lnum;
2954
2955 /* move VIsual to the right column */
2956 start_visual = curwin->w_cursor; /* save the cursor pos */
2957 curwin->w_cursor = end_visual;
2958 coladvance(end_visual.col);
2959 VIsual = curwin->w_cursor;
2960 curwin->w_cursor = start_visual; /* restore the cursor */
2961 }
2962 else
2963 {
2964 /*
2965 * If the click is before the start of visual, change the start.
2966 * If the click is after the end of visual, change the end. If
2967 * the click is inside the visual, change the closest side.
2968 */
2969 if (lt(curwin->w_cursor, start_visual))
2970 VIsual = end_visual;
2971 else if (lt(end_visual, curwin->w_cursor))
2972 VIsual = start_visual;
2973 else
2974 {
2975 /* In the same line, compare column number */
2976 if (end_visual.lnum == start_visual.lnum)
2977 {
2978 if (curwin->w_cursor.col - start_visual.col >
2979 end_visual.col - curwin->w_cursor.col)
2980 VIsual = start_visual;
2981 else
2982 VIsual = end_visual;
2983 }
2984
2985 /* In different lines, compare line number */
2986 else
2987 {
2988 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2989 (end_visual.lnum - curwin->w_cursor.lnum);
2990
2991 if (diff > 0) /* closest to end */
2992 VIsual = start_visual;
2993 else if (diff < 0) /* closest to start */
2994 VIsual = end_visual;
2995 else /* in the middle line */
2996 {
2997 if (curwin->w_cursor.col <
2998 (start_visual.col + end_visual.col) / 2)
2999 VIsual = end_visual;
3000 else
3001 VIsual = start_visual;
3002 }
3003 }
3004 }
3005 }
3006 }
3007 /*
3008 * If Visual mode started in insert mode, execute "CTRL-O"
3009 */
3010 else if ((State & INSERT) && VIsual_active)
3011 stuffcharReadbuff(Ctrl_O);
3012#endif
3013
3014 /*
3015 * Middle mouse click: Put text before cursor.
3016 */
3017 if (which_button == MOUSE_MIDDLE)
3018 {
3019#ifdef FEAT_CLIPBOARD
3020 if (clip_star.available && regname == 0)
3021 regname = '*';
3022#endif
3023 if (yank_register_mline(regname))
3024 {
3025 if (mouse_past_bottom)
3026 dir = FORWARD;
3027 }
3028 else if (mouse_past_eol)
3029 dir = FORWARD;
3030
3031 if (fixindent)
3032 {
3033 c1 = (dir == BACKWARD) ? '[' : ']';
3034 c2 = 'p';
3035 }
3036 else
3037 {
3038 c1 = (dir == FORWARD) ? 'p' : 'P';
3039 c2 = NUL;
3040 }
3041 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
3042
3043 /*
3044 * Remember where the paste started, so in edit() Insstart can be set
3045 * to this position
3046 */
3047 if (restart_edit != 0)
3048 where_paste_started = curwin->w_cursor;
3049 do_put(regname, dir, count, fixindent | PUT_CURSEND);
3050 }
3051
3052#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3053 /*
3054 * Ctrl-Mouse click or double click in a quickfix window jumps to the
3055 * error under the mouse pointer.
3056 */
3057 else if (((mod_mask & MOD_MASK_CTRL)
3058 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3059 && bt_quickfix(curbuf))
3060 {
3061 if (State & INSERT)
3062 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003063 if (curwin->w_llist_ref == NULL) /* quickfix window */
3064 stuffReadbuff((char_u *)":.cc\n");
3065 else /* location list window */
3066 stuffReadbuff((char_u *)":.ll\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 got_click = FALSE; /* ignore drag&release now */
3068 }
3069#endif
3070
3071 /*
3072 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
3073 * under the mouse pointer.
3074 */
3075 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3076 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3077 {
3078 if (State & INSERT)
3079 stuffcharReadbuff(Ctrl_O);
3080 stuffcharReadbuff(Ctrl_RSB);
3081 got_click = FALSE; /* ignore drag&release now */
3082 }
3083
3084 /*
3085 * Shift-Mouse click searches for the next occurrence of the word under
3086 * the mouse pointer
3087 */
3088 else if ((mod_mask & MOD_MASK_SHIFT))
3089 {
3090 if (State & INSERT
3091#ifdef FEAT_VISUAL
3092 || (VIsual_active && VIsual_select)
3093#endif
3094 )
3095 stuffcharReadbuff(Ctrl_O);
3096 if (which_button == MOUSE_LEFT)
3097 stuffcharReadbuff('*');
3098 else /* MOUSE_RIGHT */
3099 stuffcharReadbuff('#');
3100 }
3101
3102 /* Handle double clicks, unless on status line */
3103 else if (in_status_line)
3104 {
3105#ifdef FEAT_MOUSESHAPE
3106 if ((is_drag || is_click) && !drag_status_line)
3107 {
3108 drag_status_line = TRUE;
3109 update_mouseshape(-1);
3110 }
3111#endif
3112 }
3113#ifdef FEAT_VERTSPLIT
3114 else if (in_sep_line)
3115 {
3116# ifdef FEAT_MOUSESHAPE
3117 if ((is_drag || is_click) && !drag_sep_line)
3118 {
3119 drag_sep_line = TRUE;
3120 update_mouseshape(-1);
3121 }
3122# endif
3123 }
3124#endif
3125#ifdef FEAT_VISUAL
3126 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3127 && mouse_has(MOUSE_VISUAL))
3128 {
3129 if (is_click || !VIsual_active)
3130 {
3131 if (VIsual_active)
3132 orig_cursor = VIsual;
3133 else
3134 {
3135 check_visual_highlight();
3136 VIsual = curwin->w_cursor;
3137 orig_cursor = VIsual;
3138 VIsual_active = TRUE;
3139 VIsual_reselect = TRUE;
3140 /* start Select mode if 'selectmode' contains "mouse" */
3141 may_start_select('o');
3142 setmouse();
3143 }
3144 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003145 {
3146 /* Double click with ALT pressed makes it blockwise. */
3147 if (mod_mask & MOD_MASK_ALT)
3148 VIsual_mode = Ctrl_V;
3149 else
3150 VIsual_mode = 'v';
3151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3153 VIsual_mode = 'V';
3154 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3155 VIsual_mode = Ctrl_V;
3156#ifdef FEAT_CLIPBOARD
3157 /* Make sure the clipboard gets updated. Needed because start and
3158 * end may still be the same, and the selection needs to be owned */
3159 clip_star.vmode = NUL;
3160#endif
3161 }
3162 /*
3163 * A double click selects a word or a block.
3164 */
3165 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3166 {
3167 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003168 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169
3170 if (is_click)
3171 {
3172 /* If the character under the cursor (skipping white space) is
3173 * not a word character, try finding a match and select a (),
3174 * {}, [], #if/#endif, etc. block. */
3175 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003176 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 inc(&end_visual);
3178 if (oap != NULL)
3179 oap->motion_type = MCHAR;
3180 if (oap != NULL
3181 && VIsual_mode == 'v'
3182 && !vim_iswordc(gchar_pos(&end_visual))
3183 && equalpos(curwin->w_cursor, VIsual)
3184 && (pos = findmatch(oap, NUL)) != NULL)
3185 {
3186 curwin->w_cursor = *pos;
3187 if (oap->motion_type == MLINE)
3188 VIsual_mode = 'V';
3189 else if (*p_sel == 'e')
3190 {
3191 if (lt(curwin->w_cursor, VIsual))
3192 ++VIsual.col;
3193 else
3194 ++curwin->w_cursor.col;
3195 }
3196 }
3197 }
3198
3199 if (pos == NULL && (is_click || is_drag))
3200 {
3201 /* When not found a match or when dragging: extend to include
3202 * a word. */
3203 if (lt(curwin->w_cursor, orig_cursor))
3204 {
3205 find_start_of_word(&curwin->w_cursor);
3206 find_end_of_word(&VIsual);
3207 }
3208 else
3209 {
3210 find_start_of_word(&VIsual);
3211 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3212#ifdef FEAT_MBYTE
3213 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003214 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215#else
3216 ++curwin->w_cursor.col;
3217#endif
3218 find_end_of_word(&curwin->w_cursor);
3219 }
3220 }
3221 curwin->w_set_curswant = TRUE;
3222 }
3223 if (is_click)
3224 redraw_curbuf_later(INVERTED); /* update the inversion */
3225 }
3226 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003227 {
3228 if (mod_mask & MOD_MASK_ALT)
3229 VIsual_mode = Ctrl_V;
3230 else
3231 VIsual_mode = 'v';
3232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233
3234 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003235 if ((!VIsual_active && old_active && mode_displayed)
3236 || (VIsual_active && p_smd && msg_silent == 0
3237 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 redraw_cmdline = TRUE;
3239#endif
3240
3241 return moved;
3242}
3243
3244#ifdef FEAT_VISUAL
3245/*
3246 * Move "pos" back to the start of the word it's in.
3247 */
3248 static void
3249find_start_of_word(pos)
3250 pos_T *pos;
3251{
3252 char_u *line;
3253 int cclass;
3254 int col;
3255
3256 line = ml_get(pos->lnum);
3257 cclass = get_mouse_class(line + pos->col);
3258
3259 while (pos->col > 0)
3260 {
3261 col = pos->col - 1;
3262#ifdef FEAT_MBYTE
3263 col -= (*mb_head_off)(line, line + col);
3264#endif
3265 if (get_mouse_class(line + col) != cclass)
3266 break;
3267 pos->col = col;
3268 }
3269}
3270
3271/*
3272 * Move "pos" forward to the end of the word it's in.
3273 * When 'selection' is "exclusive", the position is just after the word.
3274 */
3275 static void
3276find_end_of_word(pos)
3277 pos_T *pos;
3278{
3279 char_u *line;
3280 int cclass;
3281 int col;
3282
3283 line = ml_get(pos->lnum);
3284 if (*p_sel == 'e' && pos->col > 0)
3285 {
3286 --pos->col;
3287#ifdef FEAT_MBYTE
3288 pos->col -= (*mb_head_off)(line, line + pos->col);
3289#endif
3290 }
3291 cclass = get_mouse_class(line + pos->col);
3292 while (line[pos->col] != NUL)
3293 {
3294#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003295 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296#else
3297 col = pos->col + 1;
3298#endif
3299 if (get_mouse_class(line + col) != cclass)
3300 {
3301 if (*p_sel == 'e')
3302 pos->col = col;
3303 break;
3304 }
3305 pos->col = col;
3306 }
3307}
3308
3309/*
3310 * Get class of a character for selection: same class means same word.
3311 * 0: blank
3312 * 1: punctuation groups
3313 * 2: normal word character
3314 * >2: multi-byte word character.
3315 */
3316 static int
3317get_mouse_class(p)
3318 char_u *p;
3319{
3320 int c;
3321
3322#ifdef FEAT_MBYTE
3323 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3324 return mb_get_class(p);
3325#endif
3326
3327 c = *p;
3328 if (c == ' ' || c == '\t')
3329 return 0;
3330
3331 if (vim_iswordc(c))
3332 return 2;
3333
3334 /*
3335 * There are a few special cases where we want certain combinations of
3336 * characters to be considered as a single word. These are things like
3337 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003338 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 */
3340 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3341 return 1;
3342 return c;
3343}
3344#endif /* FEAT_VISUAL */
3345#endif /* FEAT_MOUSE */
3346
3347#if defined(FEAT_VISUAL) || defined(PROTO)
3348/*
3349 * Check if highlighting for visual mode is possible, give a warning message
3350 * if not.
3351 */
3352 void
3353check_visual_highlight()
3354{
3355 static int did_check = FALSE;
3356
3357 if (full_screen)
3358 {
3359 if (!did_check && hl_attr(HLF_V) == 0)
3360 MSG(_("Warning: terminal cannot highlight"));
3361 did_check = TRUE;
3362 }
3363}
3364
3365/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003366 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 * This function should ALWAYS be called to end Visual mode, except from
3368 * do_pending_operator().
3369 */
3370 void
3371end_visual_mode()
3372{
3373#ifdef FEAT_CLIPBOARD
3374 /*
3375 * If we are using the clipboard, then remember what was selected in case
3376 * we need to paste it somewhere while we still own the selection.
3377 * Only do this when the clipboard is already owned. Don't want to grab
3378 * the selection when hitting ESC.
3379 */
3380 if (clip_star.available && clip_star.owned)
3381 clip_auto_select();
3382#endif
3383
3384 VIsual_active = FALSE;
3385#ifdef FEAT_MOUSE
3386 setmouse();
3387 mouse_dragging = 0;
3388#endif
3389
3390 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003391 curbuf->b_visual.vi_mode = VIsual_mode;
3392 curbuf->b_visual.vi_start = VIsual;
3393 curbuf->b_visual.vi_end = curwin->w_cursor;
3394 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395#ifdef FEAT_EVAL
3396 curbuf->b_visual_mode_eval = VIsual_mode;
3397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398#ifdef FEAT_VIRTUALEDIT
3399 if (!virtual_active())
3400 curwin->w_cursor.coladd = 0;
3401#endif
3402
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003403 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 clear_cmdline = TRUE; /* unshow visual mode later */
3405#ifdef FEAT_CMDL_INFO
3406 else
3407 clear_showcmd();
3408#endif
3409
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003410 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411}
3412
3413/*
3414 * Reset VIsual_active and VIsual_reselect.
3415 */
3416 void
3417reset_VIsual_and_resel()
3418{
3419 if (VIsual_active)
3420 {
3421 end_visual_mode();
3422 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3423 }
3424 VIsual_reselect = FALSE;
3425}
3426
3427/*
3428 * Reset VIsual_active and VIsual_reselect if it's set.
3429 */
3430 void
3431reset_VIsual()
3432{
3433 if (VIsual_active)
3434 {
3435 end_visual_mode();
3436 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3437 VIsual_reselect = FALSE;
3438 }
3439}
3440#endif /* FEAT_VISUAL */
3441
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003442#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3444
3445/*
3446 * Check for a balloon-eval special item to include when searching for an
3447 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3448 * Returns TRUE if the character at "*ptr" should be included.
3449 * "dir" is FORWARD or BACKWARD, the direction of searching.
3450 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3451 * "bnp" points to a counter for square brackets.
3452 */
3453 static int
3454find_is_eval_item(ptr, colp, bnp, dir)
3455 char_u *ptr;
3456 int *colp;
3457 int *bnp;
3458 int dir;
3459{
3460 /* Accept everything inside []. */
3461 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3462 ++*bnp;
3463 if (*bnp > 0)
3464 {
3465 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3466 --*bnp;
3467 return TRUE;
3468 }
3469
3470 /* skip over "s.var" */
3471 if (*ptr == '.')
3472 return TRUE;
3473
3474 /* two-character item: s->var */
3475 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3476 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3477 {
3478 *colp += dir;
3479 return TRUE;
3480 }
3481 return FALSE;
3482}
3483#endif
3484
3485/*
3486 * Find the identifier under or to the right of the cursor.
3487 * "find_type" can have one of three values:
3488 * FIND_IDENT: find an identifier (keyword)
3489 * FIND_STRING: find any non-white string
3490 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003491 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 *
3493 * There are three steps:
3494 * 1. Search forward for the start of an identifier/string. Doesn't move if
3495 * already on one.
3496 * 2. Search backward for the start of this identifier/string.
3497 * This doesn't match the real Vi but I like it a little better and it
3498 * shouldn't bother anyone.
3499 * 3. Search forward to the end of this identifier/string.
3500 * When FIND_IDENT isn't defined, we backup until a blank.
3501 *
3502 * Returns the length of the string, or zero if no string is found.
3503 * If a string is found, a pointer to the string is put in "*string". This
3504 * string is not always NUL terminated.
3505 */
3506 int
3507find_ident_under_cursor(string, find_type)
3508 char_u **string;
3509 int find_type;
3510{
3511 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3512 curwin->w_cursor.col, string, find_type);
3513}
3514
3515/*
3516 * Like find_ident_under_cursor(), but for any window and any position.
3517 * However: Uses 'iskeyword' from the current window!.
3518 */
3519 int
3520find_ident_at_pos(wp, lnum, startcol, string, find_type)
3521 win_T *wp;
3522 linenr_T lnum;
3523 colnr_T startcol;
3524 char_u **string;
3525 int find_type;
3526{
3527 char_u *ptr;
3528 int col = 0; /* init to shut up GCC */
3529 int i;
3530#ifdef FEAT_MBYTE
3531 int this_class = 0;
3532 int prev_class;
3533 int prevcol;
3534#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003535#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 int bn = 0; /* bracket nesting */
3537#endif
3538
3539 /*
3540 * if i == 0: try to find an identifier
3541 * if i == 1: try to find any non-white string
3542 */
3543 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3544 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3545 {
3546 /*
3547 * 1. skip to start of identifier/string
3548 */
3549 col = startcol;
3550#ifdef FEAT_MBYTE
3551 if (has_mbyte)
3552 {
3553 while (ptr[col] != NUL)
3554 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003555# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 /* Stop at a ']' to evaluate "a[x]". */
3557 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3558 break;
3559# endif
3560 this_class = mb_get_class(ptr + col);
3561 if (this_class != 0 && (i == 1 || this_class != 1))
3562 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003563 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 }
3565 }
3566 else
3567#endif
3568 while (ptr[col] != NUL
3569 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003570# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3572# endif
3573 )
3574 ++col;
3575
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003576#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 /* When starting on a ']' count it, so that we include the '['. */
3578 bn = ptr[col] == ']';
3579#endif
3580
3581 /*
3582 * 2. Back up to start of identifier/string.
3583 */
3584#ifdef FEAT_MBYTE
3585 if (has_mbyte)
3586 {
3587 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003588# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3590 this_class = mb_get_class((char_u *)"a");
3591 else
3592# endif
3593 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003594 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 {
3596 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3597 prev_class = mb_get_class(ptr + prevcol);
3598 if (this_class != prev_class
3599 && (i == 0
3600 || prev_class == 0
3601 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003602# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 && (!(find_type & FIND_EVAL)
3604 || prevcol == 0
3605 || !find_is_eval_item(ptr + prevcol, &prevcol,
3606 &bn, BACKWARD))
3607# endif
3608 )
3609 break;
3610 col = prevcol;
3611 }
3612
3613 /* If we don't want just any old string, or we've found an
3614 * identifier, stop searching. */
3615 if (this_class > 2)
3616 this_class = 2;
3617 if (!(find_type & FIND_STRING) || this_class == 2)
3618 break;
3619 }
3620 else
3621#endif
3622 {
3623 while (col > 0
3624 && ((i == 0
3625 ? vim_iswordc(ptr[col - 1])
3626 : (!vim_iswhite(ptr[col - 1])
3627 && (!(find_type & FIND_IDENT)
3628 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003629#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 || ((find_type & FIND_EVAL)
3631 && col > 1
3632 && find_is_eval_item(ptr + col - 1, &col,
3633 &bn, BACKWARD))
3634#endif
3635 ))
3636 --col;
3637
3638 /* If we don't want just any old string, or we've found an
3639 * identifier, stop searching. */
3640 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3641 break;
3642 }
3643 }
3644
3645 if (ptr[col] == NUL || (i == 0 && (
3646#ifdef FEAT_MBYTE
3647 has_mbyte ? this_class != 2 :
3648#endif
3649 !vim_iswordc(ptr[col]))))
3650 {
3651 /*
3652 * didn't find an identifier or string
3653 */
3654 if (find_type & FIND_STRING)
3655 EMSG(_("E348: No string under cursor"));
3656 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003657 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 return 0;
3659 }
3660 ptr += col;
3661 *string = ptr;
3662
3663 /*
3664 * 3. Find the end if the identifier/string.
3665 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003666#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 bn = 0;
3668 startcol -= col;
3669#endif
3670 col = 0;
3671#ifdef FEAT_MBYTE
3672 if (has_mbyte)
3673 {
3674 /* Search for point of changing multibyte character class. */
3675 this_class = mb_get_class(ptr);
3676 while (ptr[col] != NUL
3677 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3678 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003679# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 || ((find_type & FIND_EVAL)
3681 && col <= (int)startcol
3682 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3683# endif
3684 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003685 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 }
3687 else
3688#endif
3689 while ((i == 0 ? vim_iswordc(ptr[col])
3690 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003691# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 || ((find_type & FIND_EVAL)
3693 && col <= (int)startcol
3694 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3695# endif
3696 )
3697 {
3698 ++col;
3699 }
3700
3701 return col;
3702}
3703
3704/*
3705 * Prepare for redo of a normal command.
3706 */
3707 static void
3708prep_redo_cmd(cap)
3709 cmdarg_T *cap;
3710{
3711 prep_redo(cap->oap->regname, cap->count0,
3712 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3713}
3714
3715/*
3716 * Prepare for redo of any command.
3717 * Note that only the last argument can be a multi-byte char.
3718 */
3719 static void
3720prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3721 int regname;
3722 long num;
3723 int cmd1;
3724 int cmd2;
3725 int cmd3;
3726 int cmd4;
3727 int cmd5;
3728{
3729 ResetRedobuff();
3730 if (regname != 0) /* yank from specified buffer */
3731 {
3732 AppendCharToRedobuff('"');
3733 AppendCharToRedobuff(regname);
3734 }
3735 if (num)
3736 AppendNumberToRedobuff(num);
3737
3738 if (cmd1 != NUL)
3739 AppendCharToRedobuff(cmd1);
3740 if (cmd2 != NUL)
3741 AppendCharToRedobuff(cmd2);
3742 if (cmd3 != NUL)
3743 AppendCharToRedobuff(cmd3);
3744 if (cmd4 != NUL)
3745 AppendCharToRedobuff(cmd4);
3746 if (cmd5 != NUL)
3747 AppendCharToRedobuff(cmd5);
3748}
3749
3750/*
3751 * check for operator active and clear it
3752 *
3753 * return TRUE if operator was active
3754 */
3755 static int
3756checkclearop(oap)
3757 oparg_T *oap;
3758{
3759 if (oap->op_type == OP_NOP)
3760 return FALSE;
3761 clearopbeep(oap);
3762 return TRUE;
3763}
3764
3765/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003766 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003768 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 */
3770 static int
3771checkclearopq(oap)
3772 oparg_T *oap;
3773{
3774 if (oap->op_type == OP_NOP
3775#ifdef FEAT_VISUAL
3776 && !VIsual_active
3777#endif
3778 )
3779 return FALSE;
3780 clearopbeep(oap);
3781 return TRUE;
3782}
3783
3784 static void
3785clearop(oap)
3786 oparg_T *oap;
3787{
3788 oap->op_type = OP_NOP;
3789 oap->regname = 0;
3790 oap->motion_force = NUL;
3791 oap->use_reg_one = FALSE;
3792}
3793
3794 static void
3795clearopbeep(oap)
3796 oparg_T *oap;
3797{
3798 clearop(oap);
3799 beep_flush();
3800}
3801
3802#ifdef FEAT_VISUAL
3803/*
3804 * Remove the shift modifier from a special key.
3805 */
3806 static void
3807unshift_special(cap)
3808 cmdarg_T *cap;
3809{
3810 switch (cap->cmdchar)
3811 {
3812 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3813 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3814 case K_S_UP: cap->cmdchar = K_UP; break;
3815 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3816 case K_S_HOME: cap->cmdchar = K_HOME; break;
3817 case K_S_END: cap->cmdchar = K_END; break;
3818 }
3819 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3820}
3821#endif
3822
3823#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3824/*
3825 * Routines for displaying a partly typed command
3826 */
3827
3828#ifdef FEAT_VISUAL /* need room for size of Visual area */
3829# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3830#else
3831# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3832#endif
3833static char_u showcmd_buf[SHOWCMD_BUFLEN];
3834static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3835static int showcmd_is_clear = TRUE;
3836static int showcmd_visual = FALSE;
3837
3838static void display_showcmd __ARGS((void));
3839
3840 void
3841clear_showcmd()
3842{
3843 if (!p_sc)
3844 return;
3845
3846#ifdef FEAT_VISUAL
3847 if (VIsual_active && !char_avail())
3848 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003849 int cursor_bot = lt(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 long lines;
3851 colnr_T leftcol, rightcol;
3852 linenr_T top, bot;
3853
3854 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003855 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 {
3857 top = VIsual.lnum;
3858 bot = curwin->w_cursor.lnum;
3859 }
3860 else
3861 {
3862 top = curwin->w_cursor.lnum;
3863 bot = VIsual.lnum;
3864 }
3865# ifdef FEAT_FOLDING
3866 /* Include closed folds as a whole. */
3867 hasFolding(top, &top, NULL);
3868 hasFolding(bot, NULL, &bot);
3869# endif
3870 lines = bot - top + 1;
3871
3872 if (VIsual_mode == Ctrl_V)
3873 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003874# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003875 char_u *saved_sbr = p_sbr;
3876
3877 /* Make 'sbr' empty for a moment to get the correct size. */
3878 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003879# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003881# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003882 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003883# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3885 (long)(rightcol - leftcol + 1));
3886 }
3887 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3888 sprintf((char *)showcmd_buf, "%ld", lines);
3889 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003890 {
3891 char_u *s, *e;
3892 int l;
3893 int bytes = 0;
3894 int chars = 0;
3895
3896 if (cursor_bot)
3897 {
3898 s = ml_get_pos(&VIsual);
3899 e = ml_get_cursor();
3900 }
3901 else
3902 {
3903 s = ml_get_cursor();
3904 e = ml_get_pos(&VIsual);
3905 }
3906 while ((*p_sel != 'e') ? s <= e : s < e)
3907 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003908# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003909 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003910# else
3911 l = (*s == NUL) ? 0 : 1;
3912# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003913 if (l == 0)
3914 {
3915 ++bytes;
3916 ++chars;
3917 break; /* end of line */
3918 }
3919 bytes += l;
3920 ++chars;
3921 s += l;
3922 }
3923 if (bytes == chars)
3924 sprintf((char *)showcmd_buf, "%d", chars);
3925 else
3926 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3929 showcmd_visual = TRUE;
3930 }
3931 else
3932#endif
3933 {
3934 showcmd_buf[0] = NUL;
3935 showcmd_visual = FALSE;
3936
3937 /* Don't actually display something if there is nothing to clear. */
3938 if (showcmd_is_clear)
3939 return;
3940 }
3941
3942 display_showcmd();
3943}
3944
3945/*
3946 * Add 'c' to string of shown command chars.
3947 * Return TRUE if output has been written (and setcursor() has been called).
3948 */
3949 int
3950add_to_showcmd(c)
3951 int c;
3952{
3953 char_u *p;
3954 int old_len;
3955 int extra_len;
3956 int overflow;
3957#if defined(FEAT_MOUSE)
3958 int i;
3959 static int ignore[] =
3960 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003961# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3963 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003964# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 K_IGNORE,
3966 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3967 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3968 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003969 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003971 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 0
3973 };
3974#endif
3975
Bram Moolenaar09df3122006-01-23 22:23:09 +00003976 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 return FALSE;
3978
3979 if (showcmd_visual)
3980 {
3981 showcmd_buf[0] = NUL;
3982 showcmd_visual = FALSE;
3983 }
3984
3985#if defined(FEAT_MOUSE)
3986 /* Ignore keys that are scrollbar updates and mouse clicks */
3987 if (IS_SPECIAL(c))
3988 for (i = 0; ignore[i] != 0; ++i)
3989 if (ignore[i] == c)
3990 return FALSE;
3991#endif
3992
3993 p = transchar(c);
3994 old_len = (int)STRLEN(showcmd_buf);
3995 extra_len = (int)STRLEN(p);
3996 overflow = old_len + extra_len - SHOWCMD_COLS;
3997 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003998 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3999 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 STRCAT(showcmd_buf, p);
4001
4002 if (char_avail())
4003 return FALSE;
4004
4005 display_showcmd();
4006
4007 return TRUE;
4008}
4009
4010 void
4011add_to_showcmd_c(c)
4012 int c;
4013{
4014 if (!add_to_showcmd(c))
4015 setcursor();
4016}
4017
4018/*
4019 * Delete 'len' characters from the end of the shown command.
4020 */
4021 static void
4022del_from_showcmd(len)
4023 int len;
4024{
4025 int old_len;
4026
4027 if (!p_sc)
4028 return;
4029
4030 old_len = (int)STRLEN(showcmd_buf);
4031 if (len > old_len)
4032 len = old_len;
4033 showcmd_buf[old_len - len] = NUL;
4034
4035 if (!char_avail())
4036 display_showcmd();
4037}
4038
4039/*
4040 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
4041 * something and there is a partial mapping.
4042 */
4043 void
4044push_showcmd()
4045{
4046 if (p_sc)
4047 STRCPY(old_showcmd_buf, showcmd_buf);
4048}
4049
4050 void
4051pop_showcmd()
4052{
4053 if (!p_sc)
4054 return;
4055
4056 STRCPY(showcmd_buf, old_showcmd_buf);
4057
4058 display_showcmd();
4059}
4060
4061 static void
4062display_showcmd()
4063{
4064 int len;
4065
4066 cursor_off();
4067
4068 len = (int)STRLEN(showcmd_buf);
4069 if (len == 0)
4070 showcmd_is_clear = TRUE;
4071 else
4072 {
4073 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
4074 showcmd_is_clear = FALSE;
4075 }
4076
4077 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004078 * clear the rest of an old message by outputting up to SHOWCMD_COLS
4079 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 */
4081 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
4082
4083 setcursor(); /* put cursor back where it belongs */
4084}
4085#endif
4086
4087#ifdef FEAT_SCROLLBIND
4088/*
4089 * When "check" is FALSE, prepare for commands that scroll the window.
4090 * When "check" is TRUE, take care of scroll-binding after the window has
4091 * scrolled. Called from normal_cmd() and edit().
4092 */
4093 void
4094do_check_scrollbind(check)
4095 int check;
4096{
4097 static win_T *old_curwin = NULL;
4098 static linenr_T old_topline = 0;
4099#ifdef FEAT_DIFF
4100 static int old_topfill = 0;
4101#endif
4102 static buf_T *old_buf = NULL;
4103 static colnr_T old_leftcol = 0;
4104
4105 if (check && curwin->w_p_scb)
4106 {
4107 /* If a ":syncbind" command was just used, don't scroll, only reset
4108 * the values. */
4109 if (did_syncbind)
4110 did_syncbind = FALSE;
4111 else if (curwin == old_curwin)
4112 {
4113 /*
4114 * Synchronize other windows, as necessary according to
4115 * 'scrollbind'. Don't do this after an ":edit" command, except
4116 * when 'diff' is set.
4117 */
4118 if ((curwin->w_buffer == old_buf
4119#ifdef FEAT_DIFF
4120 || curwin->w_p_diff
4121#endif
4122 )
4123 && (curwin->w_topline != old_topline
4124#ifdef FEAT_DIFF
4125 || curwin->w_topfill != old_topfill
4126#endif
4127 || curwin->w_leftcol != old_leftcol))
4128 {
4129 check_scrollbind(curwin->w_topline - old_topline,
4130 (long)(curwin->w_leftcol - old_leftcol));
4131 }
4132 }
4133 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4134 {
4135 /*
4136 * When switching between windows, make sure that the relative
4137 * vertical offset is valid for the new window. The relative
4138 * offset is invalid whenever another 'scrollbind' window has
4139 * scrolled to a point that would force the current window to
4140 * scroll past the beginning or end of its buffer. When the
4141 * resync is performed, some of the other 'scrollbind' windows may
4142 * need to jump so that the current window's relative position is
4143 * visible on-screen.
4144 */
4145 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4146 }
4147 curwin->w_scbind_pos = curwin->w_topline;
4148 }
4149
4150 old_curwin = curwin;
4151 old_topline = curwin->w_topline;
4152#ifdef FEAT_DIFF
4153 old_topfill = curwin->w_topfill;
4154#endif
4155 old_buf = curwin->w_buffer;
4156 old_leftcol = curwin->w_leftcol;
4157}
4158
4159/*
4160 * Synchronize any windows that have "scrollbind" set, based on the
4161 * number of rows by which the current window has changed
4162 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4163 */
4164 void
4165check_scrollbind(topline_diff, leftcol_diff)
4166 linenr_T topline_diff;
4167 long leftcol_diff;
4168{
4169 int want_ver;
4170 int want_hor;
4171 win_T *old_curwin = curwin;
4172 buf_T *old_curbuf = curbuf;
4173#ifdef FEAT_VISUAL
4174 int old_VIsual_select = VIsual_select;
4175 int old_VIsual_active = VIsual_active;
4176#endif
4177 colnr_T tgt_leftcol = curwin->w_leftcol;
4178 long topline;
4179 long y;
4180
4181 /*
4182 * check 'scrollopt' string for vertical and horizontal scroll options
4183 */
4184 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4185#ifdef FEAT_DIFF
4186 want_ver |= old_curwin->w_p_diff;
4187#endif
4188 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4189
4190 /*
4191 * loop through the scrollbound windows and scroll accordingly
4192 */
4193#ifdef FEAT_VISUAL
4194 VIsual_select = VIsual_active = 0;
4195#endif
4196 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4197 {
4198 curbuf = curwin->w_buffer;
4199 /* skip original window and windows with 'noscrollbind' */
4200 if (curwin != old_curwin && curwin->w_p_scb)
4201 {
4202 /*
4203 * do the vertical scroll
4204 */
4205 if (want_ver)
4206 {
4207#ifdef FEAT_DIFF
4208 if (old_curwin->w_p_diff && curwin->w_p_diff)
4209 {
4210 diff_set_topline(old_curwin, curwin);
4211 }
4212 else
4213#endif
4214 {
4215 curwin->w_scbind_pos += topline_diff;
4216 topline = curwin->w_scbind_pos;
4217 if (topline > curbuf->b_ml.ml_line_count)
4218 topline = curbuf->b_ml.ml_line_count;
4219 if (topline < 1)
4220 topline = 1;
4221
4222 y = topline - curwin->w_topline;
4223 if (y > 0)
4224 scrollup(y, FALSE);
4225 else
4226 scrolldown(-y, FALSE);
4227 }
4228
4229 redraw_later(VALID);
4230 cursor_correct();
4231#ifdef FEAT_WINDOWS
4232 curwin->w_redr_status = TRUE;
4233#endif
4234 }
4235
4236 /*
4237 * do the horizontal scroll
4238 */
4239 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4240 {
4241 curwin->w_leftcol = tgt_leftcol;
4242 leftcol_changed();
4243 }
4244 }
4245 }
4246
4247 /*
4248 * reset current-window
4249 */
4250#ifdef FEAT_VISUAL
4251 VIsual_select = old_VIsual_select;
4252 VIsual_active = old_VIsual_active;
4253#endif
4254 curwin = old_curwin;
4255 curbuf = old_curbuf;
4256}
4257#endif /* #ifdef FEAT_SCROLLBIND */
4258
4259/*
4260 * Command character that's ignored.
4261 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004262 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 static void
4265nv_ignore(cap)
4266 cmdarg_T *cap;
4267{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004268 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269}
4270
4271/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004272 * Command character that doesn't do anything, but unlike nv_ignore() does
4273 * start edit(). Used for "startinsert" executed while starting up.
4274 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004275 static void
4276nv_nop(cap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004277 cmdarg_T *cap UNUSED;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004278{
4279}
4280
4281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 * Command character doesn't exist.
4283 */
4284 static void
4285nv_error(cap)
4286 cmdarg_T *cap;
4287{
4288 clearopbeep(cap->oap);
4289}
4290
4291/*
4292 * <Help> and <F1> commands.
4293 */
4294 static void
4295nv_help(cap)
4296 cmdarg_T *cap;
4297{
4298 if (!checkclearopq(cap->oap))
4299 ex_help(NULL);
4300}
4301
4302/*
4303 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4304 */
4305 static void
4306nv_addsub(cap)
4307 cmdarg_T *cap;
4308{
4309 if (!checkclearopq(cap->oap)
4310 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4311 prep_redo_cmd(cap);
4312}
4313
4314/*
4315 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4316 */
4317 static void
4318nv_page(cap)
4319 cmdarg_T *cap;
4320{
4321 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004322 {
4323#ifdef FEAT_WINDOWS
4324 if (mod_mask & MOD_MASK_CTRL)
4325 {
4326 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4327 if (cap->arg == BACKWARD)
4328 goto_tabpage(-(int)cap->count1);
4329 else
4330 goto_tabpage((int)cap->count0);
4331 }
4332 else
4333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336}
4337
4338/*
4339 * Implementation of "gd" and "gD" command.
4340 */
4341 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004342nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004343 oparg_T *oap;
4344 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004345 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346{
4347 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 char_u *ptr;
4349
Bram Moolenaard9d30582005-05-18 22:10:28 +00004350 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004351 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004353#ifdef FEAT_FOLDING
4354 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4355 foldOpenCursor();
4356#endif
4357}
4358
4359/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004360 * Search for variable declaration of "ptr[len]".
4361 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4362 * current file ("gD").
4363 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004364 * Return FAIL when not found.
4365 */
4366 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004367find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004368 char_u *ptr;
4369 int len;
4370 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004371 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004372 int searchflags; /* flags passed to searchit() */
4373{
4374 char_u *pat;
4375 pos_T old_pos;
4376 pos_T par_pos;
4377 pos_T found_pos;
4378 int t;
4379 int save_p_ws;
4380 int save_p_scs;
4381 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004382 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004383
4384 if ((pat = alloc(len + 7)) == NULL)
4385 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004386
4387 /* Put "\V" before the pattern to avoid that the special meaning of "."
4388 * and "~" causes trouble. */
4389 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4390 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 old_pos = curwin->w_cursor;
4392 save_p_ws = p_ws;
4393 save_p_scs = p_scs;
4394 p_ws = FALSE; /* don't wrap around end of file now */
4395 p_scs = FALSE; /* don't switch ignorecase off now */
4396
4397 /*
4398 * With "gD" go to line 1.
4399 * With "gd" Search back for the start of the current function, then go
4400 * back until a blank line. If this fails go to line 1.
4401 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004402 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 {
4404 setpcmark(); /* Set in findpar() otherwise */
4405 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004406 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 }
4408 else
4409 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004410 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4412 --curwin->w_cursor.lnum;
4413 }
4414 curwin->w_cursor.col = 0;
4415
4416 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004417 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004418 for (;;)
4419 {
4420 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004421 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004422 if (curwin->w_cursor.lnum >= old_pos.lnum)
4423 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004424
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004425 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004426 {
4427 pos_T *pos;
4428
4429 /* Check that the block the match is in doesn't end before the
4430 * position where we started the search from. */
4431 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4432 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4433 && pos->lnum < old_pos.lnum)
4434 continue;
4435 }
4436
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004437 if (t == FAIL)
4438 {
4439 /* If we previously found a valid position, use it. */
4440 if (found_pos.lnum != 0)
4441 {
4442 curwin->w_cursor = found_pos;
4443 t = OK;
4444 }
4445 break;
4446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004448 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004449 {
4450 /* Ignore this line, continue at start of next line. */
4451 ++curwin->w_cursor.lnum;
4452 curwin->w_cursor.col = 0;
4453 continue;
4454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004456 if (!locally) /* global search: use first match found */
4457 break;
4458 if (curwin->w_cursor.lnum >= par_pos.lnum)
4459 {
4460 /* If we previously found a valid position, use it. */
4461 if (found_pos.lnum != 0)
4462 curwin->w_cursor = found_pos;
4463 break;
4464 }
4465
4466 /* For finding a local variable and the match is before the "{" search
4467 * to find a later match. For K&R style function declarations this
4468 * skips the function header without types. */
4469 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004471
4472 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004474 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 curwin->w_cursor = old_pos;
4476 }
4477 else
4478 {
4479 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 /* "n" searches forward now */
4481 reset_search_dir();
4482 }
4483
4484 vim_free(pat);
4485 p_ws = save_p_ws;
4486 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004487
4488 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489}
4490
4491/*
4492 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4493 * lines rather than lines in the file.
4494 * 'dist' must be positive.
4495 *
4496 * Return OK if able to move cursor, FAIL otherwise.
4497 */
4498 static int
4499nv_screengo(oap, dir, dist)
4500 oparg_T *oap;
4501 int dir;
4502 long dist;
4503{
4504 int linelen = linetabsize(ml_get_curline());
4505 int retval = OK;
4506 int atend = FALSE;
4507 int n;
4508 int col_off1; /* margin offset for first screen line */
4509 int col_off2; /* margin offset for wrapped screen line */
4510 int width1; /* text width for first screen line */
4511 int width2; /* test width for wrapped screen line */
4512
4513 oap->motion_type = MCHAR;
4514 oap->inclusive = FALSE;
4515
4516 col_off1 = curwin_col_off();
4517 col_off2 = col_off1 - curwin_col_off2();
4518 width1 = W_WIDTH(curwin) - col_off1;
4519 width2 = W_WIDTH(curwin) - col_off2;
4520
4521#ifdef FEAT_VERTSPLIT
4522 if (curwin->w_width != 0)
4523 {
4524#endif
4525 /*
4526 * Instead of sticking at the last character of the buffer line we
4527 * try to stick in the last column of the screen.
4528 */
4529 if (curwin->w_curswant == MAXCOL)
4530 {
4531 atend = TRUE;
4532 validate_virtcol();
4533 if (width1 <= 0)
4534 curwin->w_curswant = 0;
4535 else
4536 {
4537 curwin->w_curswant = width1 - 1;
4538 if (curwin->w_virtcol > curwin->w_curswant)
4539 curwin->w_curswant += ((curwin->w_virtcol
4540 - curwin->w_curswant - 1) / width2 + 1) * width2;
4541 }
4542 }
4543 else
4544 {
4545 if (linelen > width1)
4546 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4547 else
4548 n = width1;
4549 if (curwin->w_curswant > (colnr_T)n + 1)
4550 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4551 * width2;
4552 }
4553
4554 while (dist--)
4555 {
4556 if (dir == BACKWARD)
4557 {
4558 if ((long)curwin->w_curswant >= width2)
4559 /* move back within line */
4560 curwin->w_curswant -= width2;
4561 else
4562 {
4563 /* to previous line */
4564 if (curwin->w_cursor.lnum == 1)
4565 {
4566 retval = FAIL;
4567 break;
4568 }
4569 --curwin->w_cursor.lnum;
4570#ifdef FEAT_FOLDING
4571 /* Move to the start of a closed fold. Don't do that when
4572 * 'foldopen' contains "all": it will open in a moment. */
4573 if (!(fdo_flags & FDO_ALL))
4574 (void)hasFolding(curwin->w_cursor.lnum,
4575 &curwin->w_cursor.lnum, NULL);
4576#endif
4577 linelen = linetabsize(ml_get_curline());
4578 if (linelen > width1)
4579 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4580 + 1) * width2;
4581 }
4582 }
4583 else /* dir == FORWARD */
4584 {
4585 if (linelen > width1)
4586 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4587 else
4588 n = width1;
4589 if (curwin->w_curswant + width2 < (colnr_T)n)
4590 /* move forward within line */
4591 curwin->w_curswant += width2;
4592 else
4593 {
4594 /* to next line */
4595#ifdef FEAT_FOLDING
4596 /* Move to the end of a closed fold. */
4597 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4598 &curwin->w_cursor.lnum);
4599#endif
4600 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4601 {
4602 retval = FAIL;
4603 break;
4604 }
4605 curwin->w_cursor.lnum++;
4606 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004607 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 }
4609 }
4610 }
4611#ifdef FEAT_VERTSPLIT
4612 }
4613#endif
4614
4615 coladvance(curwin->w_curswant);
4616
4617#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4618 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4619 {
4620 /*
4621 * Check for landing on a character that got split at the end of the
4622 * last line. We want to advance a screenline, not end up in the same
4623 * screenline or move two screenlines.
4624 */
4625 validate_virtcol();
4626 if (curwin->w_virtcol > curwin->w_curswant
4627 && (curwin->w_curswant < (colnr_T)width1
4628 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4629 : ((curwin->w_curswant - width1) % width2
4630 > (colnr_T)width2 / 2)))
4631 --curwin->w_cursor.col;
4632 }
4633#endif
4634
4635 if (atend)
4636 curwin->w_curswant = MAXCOL; /* stick in the last column */
4637
4638 return retval;
4639}
4640
4641#ifdef FEAT_MOUSE
4642/*
4643 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4644 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004645 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4646 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 */
4648 static void
4649nv_mousescroll(cap)
4650 cmdarg_T *cap;
4651{
4652# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004653 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654
4655 /* Currently we only get the mouse coordinates in the GUI. */
4656 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4657 {
4658 int row, col;
4659
4660 row = mouse_row;
4661 col = mouse_col;
4662
4663 /* find the window at the pointer coordinates */
4664 curwin = mouse_find_win(&row, &col);
4665 curbuf = curwin->w_buffer;
4666 }
4667# endif
4668
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004669 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004671 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4672 {
4673 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4674 }
4675 else
4676 {
4677 cap->count1 = 3;
4678 cap->count0 = 3;
4679 nv_scroll_line(cap);
4680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004682# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 else
4684 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004685 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4686 if (!curwin->w_p_wrap)
4687 {
4688 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004689
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004690 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4691 step = W_WIDTH(curwin);
4692 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4693 if (val < 0)
4694 val = 0;
4695
4696 gui_do_horiz_scroll(val, TRUE);
4697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004699# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700
4701# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4702 curwin->w_redr_status = TRUE;
4703
4704 curwin = old_curwin;
4705 curbuf = curwin->w_buffer;
4706# endif
4707}
4708
4709/*
4710 * Mouse clicks and drags.
4711 */
4712 static void
4713nv_mouse(cap)
4714 cmdarg_T *cap;
4715{
4716 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4717}
4718#endif
4719
4720/*
4721 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4722 * cap->arg must be TRUE for CTRL-E.
4723 */
4724 static void
4725nv_scroll_line(cap)
4726 cmdarg_T *cap;
4727{
4728 if (!checkclearop(cap->oap))
4729 scroll_redraw(cap->arg, cap->count1);
4730}
4731
4732/*
4733 * Scroll "count" lines up or down, and redraw.
4734 */
4735 void
4736scroll_redraw(up, count)
4737 int up;
4738 long count;
4739{
4740 linenr_T prev_topline = curwin->w_topline;
4741#ifdef FEAT_DIFF
4742 int prev_topfill = curwin->w_topfill;
4743#endif
4744 linenr_T prev_lnum = curwin->w_cursor.lnum;
4745
4746 if (up)
4747 scrollup(count, TRUE);
4748 else
4749 scrolldown(count, TRUE);
4750 if (p_so)
4751 {
4752 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4753 * valid, otherwise the screen jumps back at the end of the file. */
4754 cursor_correct();
4755 check_cursor_moved(curwin);
4756 curwin->w_valid |= VALID_TOPLINE;
4757
4758 /* If moved back to where we were, at least move the cursor, otherwise
4759 * we get stuck at one position. Don't move the cursor up if the
4760 * first line of the buffer is already on the screen */
4761 while (curwin->w_topline == prev_topline
4762#ifdef FEAT_DIFF
4763 && curwin->w_topfill == prev_topfill
4764#endif
4765 )
4766 {
4767 if (up)
4768 {
4769 if (curwin->w_cursor.lnum > prev_lnum
4770 || cursor_down(1L, FALSE) == FAIL)
4771 break;
4772 }
4773 else
4774 {
4775 if (curwin->w_cursor.lnum < prev_lnum
4776 || prev_topline == 1L
4777 || cursor_up(1L, FALSE) == FAIL)
4778 break;
4779 }
4780 /* Mark w_topline as valid, otherwise the screen jumps back at the
4781 * end of the file. */
4782 check_cursor_moved(curwin);
4783 curwin->w_valid |= VALID_TOPLINE;
4784 }
4785 }
4786 if (curwin->w_cursor.lnum != prev_lnum)
4787 coladvance(curwin->w_curswant);
4788 redraw_later(VALID);
4789}
4790
4791/*
4792 * Commands that start with "z".
4793 */
4794 static void
4795nv_zet(cap)
4796 cmdarg_T *cap;
4797{
4798 long n;
4799 colnr_T col;
4800 int nchar = cap->nchar;
4801#ifdef FEAT_FOLDING
4802 long old_fdl = curwin->w_p_fdl;
4803 int old_fen = curwin->w_p_fen;
4804#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004805#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004806 int undo = FALSE;
4807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808
4809 if (VIM_ISDIGIT(nchar))
4810 {
4811 /*
4812 * "z123{nchar}": edit the count before obtaining {nchar}
4813 */
4814 if (checkclearop(cap->oap))
4815 return;
4816 n = nchar - '0';
4817 for (;;)
4818 {
4819#ifdef USE_ON_FLY_SCROLL
4820 dont_scroll = TRUE; /* disallow scrolling here */
4821#endif
4822 ++no_mapping;
4823 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004824 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 --no_mapping;
4827 --allow_keys;
4828#ifdef FEAT_CMDL_INFO
4829 (void)add_to_showcmd(nchar);
4830#endif
4831 if (nchar == K_DEL || nchar == K_KDEL)
4832 n /= 10;
4833 else if (VIM_ISDIGIT(nchar))
4834 n = n * 10 + (nchar - '0');
4835 else if (nchar == CAR)
4836 {
4837#ifdef FEAT_GUI
4838 need_mouse_correct = TRUE;
4839#endif
4840 win_setheight((int)n);
4841 break;
4842 }
4843 else if (nchar == 'l'
4844 || nchar == 'h'
4845 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004846 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 {
4848 cap->count1 = n ? n * cap->count1 : cap->count1;
4849 goto dozet;
4850 }
4851 else
4852 {
4853 clearopbeep(cap->oap);
4854 break;
4855 }
4856 }
4857 cap->oap->op_type = OP_NOP;
4858 return;
4859 }
4860
4861dozet:
4862 if (
4863#ifdef FEAT_FOLDING
4864 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4865 * and "zC" only in Visual mode. "zj" and "zk" are motion
4866 * commands. */
4867 cap->nchar != 'f' && cap->nchar != 'F'
4868 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4869 && cap->nchar != 'j' && cap->nchar != 'k'
4870 &&
4871#endif
4872 checkclearop(cap->oap))
4873 return;
4874
4875 /*
4876 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4877 * If line number given, set cursor.
4878 */
4879 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4880 && cap->count0
4881 && cap->count0 != curwin->w_cursor.lnum)
4882 {
4883 setpcmark();
4884 if (cap->count0 > curbuf->b_ml.ml_line_count)
4885 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4886 else
4887 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004888 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 }
4890
4891 switch (nchar)
4892 {
4893 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4894 case '+':
4895 if (cap->count0 == 0)
4896 {
4897 /* No count given: put cursor at the line below screen */
4898 validate_botline(); /* make sure w_botline is valid */
4899 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4900 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4901 else
4902 curwin->w_cursor.lnum = curwin->w_botline;
4903 }
4904 /* FALLTHROUGH */
4905 case NL:
4906 case CAR:
4907 case K_KENTER:
4908 beginline(BL_WHITE | BL_FIX);
4909 /* FALLTHROUGH */
4910
4911 case 't': scroll_cursor_top(0, TRUE);
4912 redraw_later(VALID);
4913 break;
4914
4915 /* "z." and "zz": put cursor in middle of screen */
4916 case '.': beginline(BL_WHITE | BL_FIX);
4917 /* FALLTHROUGH */
4918
4919 case 'z': scroll_cursor_halfway(TRUE);
4920 redraw_later(VALID);
4921 break;
4922
4923 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4924 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4925 * when <count> is at bottom of window, and puts that one at
4926 * bottom of window. */
4927 if (cap->count0 != 0)
4928 {
4929 scroll_cursor_bot(0, TRUE);
4930 curwin->w_cursor.lnum = curwin->w_topline;
4931 }
4932 else if (curwin->w_topline == 1)
4933 curwin->w_cursor.lnum = 1;
4934 else
4935 curwin->w_cursor.lnum = curwin->w_topline - 1;
4936 /* FALLTHROUGH */
4937 case '-':
4938 beginline(BL_WHITE | BL_FIX);
4939 /* FALLTHROUGH */
4940
4941 case 'b': scroll_cursor_bot(0, TRUE);
4942 redraw_later(VALID);
4943 break;
4944
4945 /* "zH" - scroll screen right half-page */
4946 case 'H':
4947 cap->count1 *= W_WIDTH(curwin) / 2;
4948 /* FALLTHROUGH */
4949
4950 /* "zh" - scroll screen to the right */
4951 case 'h':
4952 case K_LEFT:
4953 if (!curwin->w_p_wrap)
4954 {
4955 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4956 curwin->w_leftcol = 0;
4957 else
4958 curwin->w_leftcol -= (colnr_T)cap->count1;
4959 leftcol_changed();
4960 }
4961 break;
4962
4963 /* "zL" - scroll screen left half-page */
4964 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4965 /* FALLTHROUGH */
4966
4967 /* "zl" - scroll screen to the left */
4968 case 'l':
4969 case K_RIGHT:
4970 if (!curwin->w_p_wrap)
4971 {
4972 /* scroll the window left */
4973 curwin->w_leftcol += (colnr_T)cap->count1;
4974 leftcol_changed();
4975 }
4976 break;
4977
4978 /* "zs" - scroll screen, cursor at the start */
4979 case 's': if (!curwin->w_p_wrap)
4980 {
4981#ifdef FEAT_FOLDING
4982 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4983 col = 0; /* like the cursor is in col 0 */
4984 else
4985#endif
4986 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4987 if ((long)col > p_siso)
4988 col -= p_siso;
4989 else
4990 col = 0;
4991 if (curwin->w_leftcol != col)
4992 {
4993 curwin->w_leftcol = col;
4994 redraw_later(NOT_VALID);
4995 }
4996 }
4997 break;
4998
4999 /* "ze" - scroll screen, cursor at the end */
5000 case 'e': if (!curwin->w_p_wrap)
5001 {
5002#ifdef FEAT_FOLDING
5003 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5004 col = 0; /* like the cursor is in col 0 */
5005 else
5006#endif
5007 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
5008 n = W_WIDTH(curwin) - curwin_col_off();
5009 if ((long)col + p_siso < n)
5010 col = 0;
5011 else
5012 col = col + p_siso - n + 1;
5013 if (curwin->w_leftcol != col)
5014 {
5015 curwin->w_leftcol = col;
5016 redraw_later(NOT_VALID);
5017 }
5018 }
5019 break;
5020
5021#ifdef FEAT_FOLDING
5022 /* "zF": create fold command */
5023 /* "zf": create fold operator */
5024 case 'F':
5025 case 'f': if (foldManualAllowed(TRUE))
5026 {
5027 cap->nchar = 'f';
5028 nv_operator(cap);
5029 curwin->w_p_fen = TRUE;
5030
5031 /* "zF" is like "zfzf" */
5032 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
5033 {
5034 nv_operator(cap);
5035 finish_op = TRUE;
5036 }
5037 }
5038 else
5039 clearopbeep(cap->oap);
5040 break;
5041
5042 /* "zd": delete fold at cursor */
5043 /* "zD": delete fold at cursor recursively */
5044 case 'd':
5045 case 'D': if (foldManualAllowed(FALSE))
5046 {
5047 if (VIsual_active)
5048 nv_operator(cap);
5049 else
5050 deleteFold(curwin->w_cursor.lnum,
5051 curwin->w_cursor.lnum, nchar == 'D', FALSE);
5052 }
5053 break;
5054
5055 /* "zE": erease all folds */
5056 case 'E': if (foldmethodIsManual(curwin))
5057 {
5058 clearFolding(curwin);
5059 changed_window_setting();
5060 }
5061 else if (foldmethodIsMarker(curwin))
5062 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5063 TRUE, FALSE);
5064 else
5065 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5066 break;
5067
5068 /* "zn": fold none: reset 'foldenable' */
5069 case 'n': curwin->w_p_fen = FALSE;
5070 break;
5071
5072 /* "zN": fold Normal: set 'foldenable' */
5073 case 'N': curwin->w_p_fen = TRUE;
5074 break;
5075
5076 /* "zi": invert folding: toggle 'foldenable' */
5077 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5078 break;
5079
5080 /* "za": open closed fold or close open fold at cursor */
5081 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5082 openFold(curwin->w_cursor.lnum, cap->count1);
5083 else
5084 {
5085 closeFold(curwin->w_cursor.lnum, cap->count1);
5086 curwin->w_p_fen = TRUE;
5087 }
5088 break;
5089
5090 /* "zA": open fold at cursor recursively */
5091 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5092 openFoldRecurse(curwin->w_cursor.lnum);
5093 else
5094 {
5095 closeFoldRecurse(curwin->w_cursor.lnum);
5096 curwin->w_p_fen = TRUE;
5097 }
5098 break;
5099
5100 /* "zo": open fold at cursor or Visual area */
5101 case 'o': if (VIsual_active)
5102 nv_operator(cap);
5103 else
5104 openFold(curwin->w_cursor.lnum, cap->count1);
5105 break;
5106
5107 /* "zO": open fold recursively */
5108 case 'O': if (VIsual_active)
5109 nv_operator(cap);
5110 else
5111 openFoldRecurse(curwin->w_cursor.lnum);
5112 break;
5113
5114 /* "zc": close fold at cursor or Visual area */
5115 case 'c': if (VIsual_active)
5116 nv_operator(cap);
5117 else
5118 closeFold(curwin->w_cursor.lnum, cap->count1);
5119 curwin->w_p_fen = TRUE;
5120 break;
5121
5122 /* "zC": close fold recursively */
5123 case 'C': if (VIsual_active)
5124 nv_operator(cap);
5125 else
5126 closeFoldRecurse(curwin->w_cursor.lnum);
5127 curwin->w_p_fen = TRUE;
5128 break;
5129
5130 /* "zv": open folds at the cursor */
5131 case 'v': foldOpenCursor();
5132 break;
5133
5134 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5135 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005136 curwin->w_foldinvalid = TRUE; /* recompute folds */
5137 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 foldOpenCursor();
5139 break;
5140
5141 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5142 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005143 curwin->w_foldinvalid = TRUE; /* recompute folds */
5144 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 break;
5146
5147 /* "zm": fold more */
5148 case 'm': if (curwin->w_p_fdl > 0)
5149 --curwin->w_p_fdl;
5150 old_fdl = -1; /* force an update */
5151 curwin->w_p_fen = TRUE;
5152 break;
5153
5154 /* "zM": close all folds */
5155 case 'M': curwin->w_p_fdl = 0;
5156 old_fdl = -1; /* force an update */
5157 curwin->w_p_fen = TRUE;
5158 break;
5159
5160 /* "zr": reduce folding */
5161 case 'r': ++curwin->w_p_fdl;
5162 break;
5163
5164 /* "zR": open all folds */
5165 case 'R': curwin->w_p_fdl = getDeepestNesting();
5166 old_fdl = -1; /* force an update */
5167 break;
5168
5169 case 'j': /* "zj" move to next fold downwards */
5170 case 'k': /* "zk" move to next fold upwards */
5171 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5172 cap->count1) == FAIL)
5173 clearopbeep(cap->oap);
5174 break;
5175
5176#endif /* FEAT_FOLDING */
5177
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005178#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005179 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5180 ++no_mapping;
5181 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005182 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005183 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005184 --no_mapping;
5185 --allow_keys;
5186#ifdef FEAT_CMDL_INFO
5187 (void)add_to_showcmd(nchar);
5188#endif
5189 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5190 {
5191 clearopbeep(cap->oap);
5192 break;
5193 }
5194 undo = TRUE;
5195 /*FALLTHROUGH*/
5196
Bram Moolenaarb765d632005-06-07 21:00:02 +00005197 case 'g': /* "zg": add good word to word list */
5198 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005199 case 'G': /* "zG": add good word to temp word list */
5200 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005201 {
5202 char_u *ptr = NULL;
5203 int len;
5204
5205 if (checkclearop(cap->oap))
5206 break;
5207# ifdef FEAT_VISUAL
5208 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5209 == FAIL)
5210 return;
5211# endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005212 if (ptr == NULL)
5213 {
5214 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005215
5216 /* Find bad word under the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005217 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005218 if (len != 0 && curwin->w_cursor.col <= pos.col)
5219 ptr = ml_get_pos(&curwin->w_cursor);
5220 curwin->w_cursor = pos;
5221 }
5222
Bram Moolenaarb765d632005-06-07 21:00:02 +00005223 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5224 FIND_IDENT)) == 0)
5225 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005226 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005227 (nchar == 'G' || nchar == 'W')
5228 ? 0 : (int)cap->count1,
5229 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005230 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005231 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005232
Bram Moolenaar43abc522005-12-10 20:15:02 +00005233 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005234 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005235 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005236 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005237#endif
5238
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 default: clearopbeep(cap->oap);
5240 }
5241
5242#ifdef FEAT_FOLDING
5243 /* Redraw when 'foldenable' changed */
5244 if (old_fen != curwin->w_p_fen)
5245 {
5246# ifdef FEAT_DIFF
5247 win_T *wp;
5248
5249 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5250 {
5251 /* Adjust 'foldenable' in diff-synced windows. */
5252 FOR_ALL_WINDOWS(wp)
5253 {
5254 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5255 {
5256 wp->w_p_fen = curwin->w_p_fen;
5257 changed_window_setting_win(wp);
5258 }
5259 }
5260 }
5261# endif
5262 changed_window_setting();
5263 }
5264
5265 /* Redraw when 'foldlevel' changed. */
5266 if (old_fdl != curwin->w_p_fdl)
5267 newFoldLevel();
5268#endif
5269}
5270
5271#ifdef FEAT_GUI
5272/*
5273 * Vertical scrollbar movement.
5274 */
5275 static void
5276nv_ver_scrollbar(cap)
5277 cmdarg_T *cap;
5278{
5279 if (cap->oap->op_type != OP_NOP)
5280 clearopbeep(cap->oap);
5281
5282 /* Even if an operator was pending, we still want to scroll */
5283 gui_do_scroll();
5284}
5285
5286/*
5287 * Horizontal scrollbar movement.
5288 */
5289 static void
5290nv_hor_scrollbar(cap)
5291 cmdarg_T *cap;
5292{
5293 if (cap->oap->op_type != OP_NOP)
5294 clearopbeep(cap->oap);
5295
5296 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005297 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298}
5299#endif
5300
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005301#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005302/*
5303 * Click in GUI tab.
5304 */
5305 static void
5306nv_tabline(cap)
5307 cmdarg_T *cap;
5308{
5309 if (cap->oap->op_type != OP_NOP)
5310 clearopbeep(cap->oap);
5311
5312 /* Even if an operator was pending, we still want to jump tabs. */
5313 goto_tabpage(current_tab);
5314}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005315
5316/*
5317 * Selected item in tab line menu.
5318 */
5319 static void
5320nv_tabmenu(cap)
5321 cmdarg_T *cap;
5322{
5323 if (cap->oap->op_type != OP_NOP)
5324 clearopbeep(cap->oap);
5325
5326 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005327 handle_tabmenu();
5328}
5329
5330/*
5331 * Handle selecting an item of the GUI tab line menu.
5332 * Used in Normal and Insert mode.
5333 */
5334 void
5335handle_tabmenu()
5336{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005337 switch (current_tabmenu)
5338 {
5339 case TABLINE_MENU_CLOSE:
5340 if (current_tab == 0)
5341 do_cmdline_cmd((char_u *)"tabclose");
5342 else
5343 {
5344 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5345 current_tab);
5346 do_cmdline_cmd(IObuff);
5347 }
5348 break;
5349
5350 case TABLINE_MENU_NEW:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005351 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5352 current_tab > 0 ? current_tab - 1 : 999);
5353 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005354 break;
5355
5356 case TABLINE_MENU_OPEN:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005357 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5358 current_tab > 0 ? current_tab - 1 : 999);
5359 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005360 break;
5361 }
5362}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005363#endif
5364
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365/*
5366 * "Q" command.
5367 */
5368 static void
5369nv_exmode(cap)
5370 cmdarg_T *cap;
5371{
5372 /*
5373 * Ignore 'Q' in Visual mode, just give a beep.
5374 */
5375#ifdef FEAT_VISUAL
5376 if (VIsual_active)
5377 vim_beep();
5378 else
5379#endif
5380 if (!checkclearop(cap->oap))
5381 do_exmode(FALSE);
5382}
5383
5384/*
5385 * Handle a ":" command.
5386 */
5387 static void
5388nv_colon(cap)
5389 cmdarg_T *cap;
5390{
5391 int old_p_im;
5392
5393#ifdef FEAT_VISUAL
5394 if (VIsual_active)
5395 nv_operator(cap);
5396 else
5397#endif
5398 {
5399 if (cap->oap->op_type != OP_NOP)
5400 {
5401 /* Using ":" as a movement is characterwise exclusive. */
5402 cap->oap->motion_type = MCHAR;
5403 cap->oap->inclusive = FALSE;
5404 }
5405 else if (cap->count0)
5406 {
5407 /* translate "count:" into ":.,.+(count - 1)" */
5408 stuffcharReadbuff('.');
5409 if (cap->count0 > 1)
5410 {
5411 stuffReadbuff((char_u *)",.+");
5412 stuffnumReadbuff((long)cap->count0 - 1L);
5413 }
5414 }
5415
5416 /* When typing, don't type below an old message */
5417 if (KeyTyped)
5418 compute_cmdrow();
5419
5420 old_p_im = p_im;
5421
5422 /* get a command line and execute it */
5423 do_cmdline(NULL, getexline, NULL,
5424 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5425
5426 /* If 'insertmode' changed, enter or exit Insert mode */
5427 if (p_im != old_p_im)
5428 {
5429 if (p_im)
5430 restart_edit = 'i';
5431 else
5432 restart_edit = 0;
5433 }
5434
5435 /* The start of the operator may have become invalid by the Ex
5436 * command. */
5437 if (cap->oap->op_type != OP_NOP
5438 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5439 || cap->oap->start.col >
Bram Moolenaar78a15312009-05-15 19:33:18 +00005440 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 clearopbeep(cap->oap);
5442 }
5443}
5444
5445/*
5446 * Handle CTRL-G command.
5447 */
5448 static void
5449nv_ctrlg(cap)
5450 cmdarg_T *cap;
5451{
5452#ifdef FEAT_VISUAL
5453 if (VIsual_active) /* toggle Selection/Visual mode */
5454 {
5455 VIsual_select = !VIsual_select;
5456 showmode();
5457 }
5458 else
5459#endif
5460 if (!checkclearop(cap->oap))
5461 /* print full name if count given or :cd used */
5462 fileinfo((int)cap->count0, FALSE, TRUE);
5463}
5464
5465/*
5466 * Handle CTRL-H <Backspace> command.
5467 */
5468 static void
5469nv_ctrlh(cap)
5470 cmdarg_T *cap;
5471{
5472#ifdef FEAT_VISUAL
5473 if (VIsual_active && VIsual_select)
5474 {
5475 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5476 v_visop(cap);
5477 }
5478 else
5479#endif
5480 nv_left(cap);
5481}
5482
5483/*
5484 * CTRL-L: clear screen and redraw.
5485 */
5486 static void
5487nv_clear(cap)
5488 cmdarg_T *cap;
5489{
5490 if (!checkclearop(cap->oap))
5491 {
5492#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5493 /*
5494 * Right now, the BeBox doesn't seem to have an easy way to detect
5495 * window resizing, so we cheat and make the user detect it
5496 * manually with CTRL-L instead
5497 */
5498 ui_get_shellsize();
5499#endif
5500#ifdef FEAT_SYN_HL
5501 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005502 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503#endif
5504 redraw_later(CLEAR);
5505 }
5506}
5507
5508/*
5509 * CTRL-O: In Select mode: switch to Visual mode for one command.
5510 * Otherwise: Go to older pcmark.
5511 */
5512 static void
5513nv_ctrlo(cap)
5514 cmdarg_T *cap;
5515{
5516#ifdef FEAT_VISUAL
5517 if (VIsual_active && VIsual_select)
5518 {
5519 VIsual_select = FALSE;
5520 showmode();
5521 restart_VIsual_select = 2; /* restart Select mode later */
5522 }
5523 else
5524#endif
5525 {
5526 cap->count1 = -cap->count1;
5527 nv_pcmark(cap);
5528 }
5529}
5530
5531/*
5532 * CTRL-^ command, short for ":e #"
5533 */
5534 static void
5535nv_hat(cap)
5536 cmdarg_T *cap;
5537{
5538 if (!checkclearopq(cap->oap))
5539 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5540 GETF_SETMARK|GETF_ALT, FALSE);
5541}
5542
5543/*
5544 * "Z" commands.
5545 */
5546 static void
5547nv_Zet(cap)
5548 cmdarg_T *cap;
5549{
5550 if (!checkclearopq(cap->oap))
5551 {
5552 switch (cap->nchar)
5553 {
5554 /* "ZZ": equivalent to ":x". */
5555 case 'Z': do_cmdline_cmd((char_u *)"x");
5556 break;
5557
5558 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5559 case 'Q': do_cmdline_cmd((char_u *)"q!");
5560 break;
5561
5562 default: clearopbeep(cap->oap);
5563 }
5564 }
5565}
5566
5567#if defined(FEAT_WINDOWS) || defined(PROTO)
5568/*
5569 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5570 */
5571 void
5572do_nv_ident(c1, c2)
5573 int c1;
5574 int c2;
5575{
5576 oparg_T oa;
5577 cmdarg_T ca;
5578
5579 clear_oparg(&oa);
5580 vim_memset(&ca, 0, sizeof(ca));
5581 ca.oap = &oa;
5582 ca.cmdchar = c1;
5583 ca.nchar = c2;
5584 nv_ident(&ca);
5585}
5586#endif
5587
5588/*
5589 * Handle the commands that use the word under the cursor.
5590 * [g] CTRL-] :ta to current identifier
5591 * [g] 'K' run program for current identifier
5592 * [g] '*' / to current identifier or string
5593 * [g] '#' ? to current identifier or string
5594 * g ']' :tselect for current identifier
5595 */
5596 static void
5597nv_ident(cap)
5598 cmdarg_T *cap;
5599{
5600 char_u *ptr = NULL;
5601 char_u *buf;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005602 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 char_u *p;
5604 char_u *kp; /* value of 'keywordprg' */
5605 int kp_help; /* 'keywordprg' is ":help" */
5606 int n = 0; /* init for GCC */
5607 int cmdchar;
5608 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005609 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 char_u *aux_ptr;
5611 int isman;
5612 int isman_s;
5613
5614 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5615 {
5616 cmdchar = cap->nchar;
5617 g_cmd = TRUE;
5618 }
5619 else
5620 {
5621 cmdchar = cap->cmdchar;
5622 g_cmd = FALSE;
5623 }
5624
5625 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5626 cmdchar = '#';
5627
5628 /*
5629 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5630 */
5631 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5632 {
5633#ifdef FEAT_VISUAL
5634 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5635 return;
5636#endif
5637 if (checkclearopq(cap->oap))
5638 return;
5639 }
5640
5641 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5642 (cmdchar == '*' || cmdchar == '#')
5643 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5644 {
5645 clearop(cap->oap);
5646 return;
5647 }
5648
5649 /* Allocate buffer to put the command in. Inserting backslashes can
5650 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5651 * and some numbers. */
5652 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5653 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5654 || STRCMP(kp, ":help") == 0);
5655 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5656 if (buf == NULL)
5657 return;
5658 buf[0] = NUL;
5659
5660 switch (cmdchar)
5661 {
5662 case '*':
5663 case '#':
5664 /*
5665 * Put cursor at start of word, makes search skip the word
5666 * under the cursor.
5667 * Call setpcmark() first, so "*``" puts the cursor back where
5668 * it was.
5669 */
5670 setpcmark();
5671 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5672
5673 if (!g_cmd && vim_iswordp(ptr))
5674 STRCPY(buf, "\\<");
5675 no_smartcase = TRUE; /* don't use 'smartcase' now */
5676 break;
5677
5678 case 'K':
5679 if (kp_help)
5680 STRCPY(buf, "he! ");
5681 else
5682 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005683 /* An external command will probably use an argument starting
5684 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005685 while (*ptr == '-' && n > 0)
5686 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005687 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005688 --n;
5689 }
5690 if (n == 0)
5691 {
5692 EMSG(_(e_noident)); /* found dashes only */
5693 vim_free(buf);
5694 return;
5695 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005696
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 /* When a count is given, turn it into a range. Is this
5698 * really what we want? */
5699 isman = (STRCMP(kp, "man") == 0);
5700 isman_s = (STRCMP(kp, "man -s") == 0);
5701 if (cap->count0 != 0 && !(isman || isman_s))
5702 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5703
5704 STRCAT(buf, "! ");
5705 if (cap->count0 == 0 && isman_s)
5706 STRCAT(buf, "man");
5707 else
5708 STRCAT(buf, kp);
5709 STRCAT(buf, " ");
5710 if (cap->count0 != 0 && (isman || isman_s))
5711 {
5712 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5713 STRCAT(buf, " ");
5714 }
5715 }
5716 break;
5717
5718 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005719 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720#ifdef FEAT_CSCOPE
5721 if (p_cst)
5722 STRCPY(buf, "cstag ");
5723 else
5724#endif
5725 STRCPY(buf, "ts ");
5726 break;
5727
5728 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005729 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 if (curbuf->b_help)
5731 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005733 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005734 if (g_cmd)
5735 STRCPY(buf, "tj ");
5736 else
5737 sprintf((char *)buf, "%ldta ", cap->count0);
5738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 }
5740
5741 /*
5742 * Now grab the chars in the identifier
5743 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005744 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005746 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005747 ptr = vim_strnsave(ptr, n);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005748 p = vim_strsave_shellescape(ptr, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005749 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005750 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005752 vim_free(buf);
5753 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005755 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5756 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005757 {
5758 vim_free(buf);
5759 vim_free(p);
5760 return;
5761 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005762 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005763 STRCAT(buf, p);
5764 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005766 else
5767 {
5768 if (cmdchar == '*')
5769 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5770 else if (cmdchar == '#')
5771 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005772 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005773 {
5774 if (curbuf->b_help)
5775 /* ":help" handles unescaped argument */
5776 aux_ptr = (char_u *)"";
5777 else
5778 aux_ptr = (char_u *)"\\|\"\n[";
5779 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005780 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005781 aux_ptr = (char_u *)"\\|\"\n*?[";
5782
5783 p = buf + STRLEN(buf);
5784 while (n-- > 0)
5785 {
5786 /* put a backslash before \ and some others */
5787 if (vim_strchr(aux_ptr, *ptr) != NULL)
5788 *p++ = '\\';
5789#ifdef FEAT_MBYTE
5790 /* When current byte is a part of multibyte character, copy all
5791 * bytes of that character. */
5792 if (has_mbyte)
5793 {
5794 int i;
5795 int len = (*mb_ptr2len)(ptr) - 1;
5796
5797 for (i = 0; i < len && n >= 1; ++i, --n)
5798 *p++ = *ptr++;
5799 }
5800#endif
5801 *p++ = *ptr++;
5802 }
5803 *p = NUL;
5804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805
5806 /*
5807 * Execute the command.
5808 */
5809 if (cmdchar == '*' || cmdchar == '#')
5810 {
5811 if (!g_cmd && (
5812#ifdef FEAT_MBYTE
5813 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5814#endif
5815 vim_iswordc(ptr[-1])))
5816 STRCAT(buf, "\\>");
5817#ifdef FEAT_CMDHIST
5818 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005819 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5821#endif
5822 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5823 }
5824 else
5825 do_cmdline_cmd(buf);
5826
5827 vim_free(buf);
5828}
5829
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005830#if defined(FEAT_VISUAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831/*
5832 * Get visually selected text, within one line only.
5833 * Returns FAIL if more than one line selected.
5834 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005835 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836get_visual_text(cap, pp, lenp)
5837 cmdarg_T *cap;
5838 char_u **pp; /* return: start of selected text */
5839 int *lenp; /* return: length of selected text */
5840{
5841 if (VIsual_mode != 'V')
5842 unadjust_for_sel();
5843 if (VIsual.lnum != curwin->w_cursor.lnum)
5844 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005845 if (cap != NULL)
5846 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 return FAIL;
5848 }
5849 if (VIsual_mode == 'V')
5850 {
5851 *pp = ml_get_curline();
5852 *lenp = (int)STRLEN(*pp);
5853 }
5854 else
5855 {
5856 if (lt(curwin->w_cursor, VIsual))
5857 {
5858 *pp = ml_get_pos(&curwin->w_cursor);
5859 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5860 }
5861 else
5862 {
5863 *pp = ml_get_pos(&VIsual);
5864 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5865 }
5866#ifdef FEAT_MBYTE
5867 if (has_mbyte)
5868 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005869 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870#endif
5871 }
5872 reset_VIsual_and_resel();
5873 return OK;
5874}
5875#endif
5876
5877/*
5878 * CTRL-T: backwards in tag stack
5879 */
5880 static void
5881nv_tagpop(cap)
5882 cmdarg_T *cap;
5883{
5884 if (!checkclearopq(cap->oap))
5885 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5886}
5887
5888/*
5889 * Handle scrolling command 'H', 'L' and 'M'.
5890 */
5891 static void
5892nv_scroll(cap)
5893 cmdarg_T *cap;
5894{
5895 int used = 0;
5896 long n;
5897#ifdef FEAT_FOLDING
5898 linenr_T lnum;
5899#endif
5900 int half;
5901
5902 cap->oap->motion_type = MLINE;
5903 setpcmark();
5904
5905 if (cap->cmdchar == 'L')
5906 {
5907 validate_botline(); /* make sure curwin->w_botline is valid */
5908 curwin->w_cursor.lnum = curwin->w_botline - 1;
5909 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5910 curwin->w_cursor.lnum = 1;
5911 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005912 {
5913#ifdef FEAT_FOLDING
5914 if (hasAnyFolding(curwin))
5915 {
5916 /* Count a fold for one screen line. */
5917 for (n = cap->count1 - 1; n > 0
5918 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5919 {
5920 (void)hasFolding(curwin->w_cursor.lnum,
5921 &curwin->w_cursor.lnum, NULL);
5922 --curwin->w_cursor.lnum;
5923 }
5924 }
5925 else
5926#endif
5927 curwin->w_cursor.lnum -= cap->count1 - 1;
5928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 }
5930 else
5931 {
5932 if (cap->cmdchar == 'M')
5933 {
5934#ifdef FEAT_DIFF
5935 /* Don't count filler lines above the window. */
5936 used -= diff_check_fill(curwin, curwin->w_topline)
5937 - curwin->w_topfill;
5938#endif
5939 validate_botline(); /* make sure w_empty_rows is valid */
5940 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5941 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5942 {
5943#ifdef FEAT_DIFF
5944 /* Count half he number of filler lines to be "below this
5945 * line" and half to be "above the next line". */
5946 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5947 + n) / 2 >= half)
5948 {
5949 --n;
5950 break;
5951 }
5952#endif
5953 used += plines(curwin->w_topline + n);
5954 if (used >= half)
5955 break;
5956#ifdef FEAT_FOLDING
5957 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5958 n = lnum - curwin->w_topline;
5959#endif
5960 }
5961 if (n > 0 && used > curwin->w_height)
5962 --n;
5963 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005964 else /* (cap->cmdchar == 'H') */
5965 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005967#ifdef FEAT_FOLDING
5968 if (hasAnyFolding(curwin))
5969 {
5970 /* Count a fold for one screen line. */
5971 lnum = curwin->w_topline;
5972 while (n-- > 0 && lnum < curwin->w_botline - 1)
5973 {
5974 hasFolding(lnum, NULL, &lnum);
5975 ++lnum;
5976 }
5977 n = lnum - curwin->w_topline;
5978 }
5979#endif
5980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 curwin->w_cursor.lnum = curwin->w_topline + n;
5982 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5983 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5984 }
5985
5986 cursor_correct(); /* correct for 'so' */
5987 beginline(BL_SOL | BL_FIX);
5988}
5989
5990/*
5991 * Cursor right commands.
5992 */
5993 static void
5994nv_right(cap)
5995 cmdarg_T *cap;
5996{
5997 long n;
5998#ifdef FEAT_VISUAL
5999 int PAST_LINE;
6000#else
6001# define PAST_LINE 0
6002#endif
6003
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006004 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6005 {
6006 /* <C-Right> and <S-Right> move a word or WORD right */
6007 if (mod_mask & MOD_MASK_CTRL)
6008 cap->arg = TRUE;
6009 nv_wordcmd(cap);
6010 return;
6011 }
6012
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 cap->oap->motion_type = MCHAR;
6014 cap->oap->inclusive = FALSE;
6015#ifdef FEAT_VISUAL
6016 PAST_LINE = (VIsual_active && *p_sel != 'o');
6017
6018# ifdef FEAT_VIRTUALEDIT
6019 /*
6020 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006021 * (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 */
6023 if (virtual_active())
6024 PAST_LINE = 0;
6025# endif
6026#endif
6027
6028 for (n = cap->count1; n > 0; --n)
6029 {
6030 if ((!PAST_LINE && oneright() == FAIL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006031#ifdef FEAT_VISUAL
6032 || (PAST_LINE && *ml_get_cursor() == NUL)
6033#endif
6034 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 {
6036 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006037 * <Space> wraps to next line if 'whichwrap' has 's'.
6038 * 'l' wraps to next line if 'whichwrap' has 'l'.
6039 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 */
6041 if ( ((cap->cmdchar == ' '
6042 && vim_strchr(p_ww, 's') != NULL)
6043 || (cap->cmdchar == 'l'
6044 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006045 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 && vim_strchr(p_ww, '>') != NULL))
6047 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6048 {
6049 /* When deleting we also count the NL as a character.
6050 * Set cap->oap->inclusive when last char in the line is
6051 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006052 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 && !cap->oap->inclusive
6054 && !lineempty(curwin->w_cursor.lnum))
6055 cap->oap->inclusive = TRUE;
6056 else
6057 {
6058 ++curwin->w_cursor.lnum;
6059 curwin->w_cursor.col = 0;
6060#ifdef FEAT_VIRTUALEDIT
6061 curwin->w_cursor.coladd = 0;
6062#endif
6063 curwin->w_set_curswant = TRUE;
6064 cap->oap->inclusive = FALSE;
6065 }
6066 continue;
6067 }
6068 if (cap->oap->op_type == OP_NOP)
6069 {
6070 /* Only beep and flush if not moved at all */
6071 if (n == cap->count1)
6072 beep_flush();
6073 }
6074 else
6075 {
6076 if (!lineempty(curwin->w_cursor.lnum))
6077 cap->oap->inclusive = TRUE;
6078 }
6079 break;
6080 }
6081#ifdef FEAT_VISUAL
6082 else if (PAST_LINE)
6083 {
6084 curwin->w_set_curswant = TRUE;
6085# ifdef FEAT_VIRTUALEDIT
6086 if (virtual_active())
6087 oneright();
6088 else
6089# endif
6090 {
6091# ifdef FEAT_MBYTE
6092 if (has_mbyte)
6093 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006094 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 else
6096# endif
6097 ++curwin->w_cursor.col;
6098 }
6099 }
6100#endif
6101 }
6102#ifdef FEAT_FOLDING
6103 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6104 && cap->oap->op_type == OP_NOP)
6105 foldOpenCursor();
6106#endif
6107}
6108
6109/*
6110 * Cursor left commands.
6111 *
6112 * Returns TRUE when operator end should not be adjusted.
6113 */
6114 static void
6115nv_left(cap)
6116 cmdarg_T *cap;
6117{
6118 long n;
6119
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006120 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6121 {
6122 /* <C-Left> and <S-Left> move a word or WORD left */
6123 if (mod_mask & MOD_MASK_CTRL)
6124 cap->arg = 1;
6125 nv_bck_word(cap);
6126 return;
6127 }
6128
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 cap->oap->motion_type = MCHAR;
6130 cap->oap->inclusive = FALSE;
6131 for (n = cap->count1; n > 0; --n)
6132 {
6133 if (oneleft() == FAIL)
6134 {
6135 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6136 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6137 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6138 */
6139 if ( (((cap->cmdchar == K_BS
6140 || cap->cmdchar == Ctrl_H)
6141 && vim_strchr(p_ww, 'b') != NULL)
6142 || (cap->cmdchar == 'h'
6143 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006144 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 && vim_strchr(p_ww, '<') != NULL))
6146 && curwin->w_cursor.lnum > 1)
6147 {
6148 --(curwin->w_cursor.lnum);
6149 coladvance((colnr_T)MAXCOL);
6150 curwin->w_set_curswant = TRUE;
6151
6152 /* When the NL before the first char has to be deleted we
6153 * put the cursor on the NUL after the previous line.
6154 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006155 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 if ( (cap->oap->op_type == OP_DELETE
6157 || cap->oap->op_type == OP_CHANGE)
6158 && !lineempty(curwin->w_cursor.lnum))
6159 {
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006160 if (*ml_get_cursor() != NUL)
6161 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 cap->retval |= CA_NO_ADJ_OP_END;
6163 }
6164 continue;
6165 }
6166 /* Only beep and flush if not moved at all */
6167 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6168 beep_flush();
6169 break;
6170 }
6171 }
6172#ifdef FEAT_FOLDING
6173 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6174 && cap->oap->op_type == OP_NOP)
6175 foldOpenCursor();
6176#endif
6177}
6178
6179/*
6180 * Cursor up commands.
6181 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6182 */
6183 static void
6184nv_up(cap)
6185 cmdarg_T *cap;
6186{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006187 if (mod_mask & MOD_MASK_SHIFT)
6188 {
6189 /* <S-Up> is page up */
6190 cap->arg = BACKWARD;
6191 nv_page(cap);
6192 }
6193 else
6194 {
6195 cap->oap->motion_type = MLINE;
6196 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6197 clearopbeep(cap->oap);
6198 else if (cap->arg)
6199 beginline(BL_WHITE | BL_FIX);
6200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201}
6202
6203/*
6204 * Cursor down commands.
6205 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6206 */
6207 static void
6208nv_down(cap)
6209 cmdarg_T *cap;
6210{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006211 if (mod_mask & MOD_MASK_SHIFT)
6212 {
6213 /* <S-Down> is page down */
6214 cap->arg = FORWARD;
6215 nv_page(cap);
6216 }
6217 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6219 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006220 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006221 if (curwin->w_llist_ref == NULL)
6222 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6223 else
6224 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 else
6226#endif
6227 {
6228#ifdef FEAT_CMDWIN
6229 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006230 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 cmdwin_result = CAR;
6232 else
6233#endif
6234 {
6235 cap->oap->motion_type = MLINE;
6236 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6237 clearopbeep(cap->oap);
6238 else if (cap->arg)
6239 beginline(BL_WHITE | BL_FIX);
6240 }
6241 }
6242}
6243
6244#ifdef FEAT_SEARCHPATH
6245/*
6246 * Grab the file name under the cursor and edit it.
6247 */
6248 static void
6249nv_gotofile(cap)
6250 cmdarg_T *cap;
6251{
6252 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006253 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006255 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 {
6257 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006258 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 return;
6260 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006261#ifdef FEAT_AUTOCMD
6262 if (curbuf_locked())
6263 {
6264 clearop(cap->oap);
6265 return;
6266 }
6267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006269 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270
6271 if (ptr != NULL)
6272 {
6273 /* do autowrite if necessary */
6274 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6275 autowrite(curbuf, FALSE);
6276 setpcmark();
6277 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006278 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006279 if (cap->nchar == 'F' && lnum >= 0)
6280 {
6281 curwin->w_cursor.lnum = lnum;
6282 check_cursor_lnum();
6283 beginline(BL_SOL | BL_FIX);
6284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 vim_free(ptr);
6286 }
6287 else
6288 clearop(cap->oap);
6289}
6290#endif
6291
6292/*
6293 * <End> command: to end of current line or last line.
6294 */
6295 static void
6296nv_end(cap)
6297 cmdarg_T *cap;
6298{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006299 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006301 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 nv_goto(cap);
6303 cap->count1 = 1; /* to end of current line */
6304 }
6305 nv_dollar(cap);
6306}
6307
6308/*
6309 * Handle the "$" command.
6310 */
6311 static void
6312nv_dollar(cap)
6313 cmdarg_T *cap;
6314{
6315 cap->oap->motion_type = MCHAR;
6316 cap->oap->inclusive = TRUE;
6317#ifdef FEAT_VIRTUALEDIT
6318 /* In virtual mode when off the edge of a line and an operator
6319 * is pending (whew!) keep the cursor where it is.
6320 * Otherwise, send it to the end of the line. */
6321 if (!virtual_active() || gchar_cursor() != NUL
6322 || cap->oap->op_type == OP_NOP)
6323#endif
6324 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6325 if (cursor_down((long)(cap->count1 - 1),
6326 cap->oap->op_type == OP_NOP) == FAIL)
6327 clearopbeep(cap->oap);
6328#ifdef FEAT_FOLDING
6329 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6330 foldOpenCursor();
6331#endif
6332}
6333
6334/*
6335 * Implementation of '?' and '/' commands.
6336 * If cap->arg is TRUE don't set PC mark.
6337 */
6338 static void
6339nv_search(cap)
6340 cmdarg_T *cap;
6341{
6342 oparg_T *oap = cap->oap;
6343
6344 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6345 {
6346 /* Translate "g??" to "g?g?" */
6347 cap->cmdchar = 'g';
6348 cap->nchar = '?';
6349 nv_operator(cap);
6350 return;
6351 }
6352
6353 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6354
6355 if (cap->searchbuf == NULL)
6356 {
6357 clearop(oap);
6358 return;
6359 }
6360
6361 normal_search(cap, cap->cmdchar, cap->searchbuf,
6362 (cap->arg ? 0 : SEARCH_MARK));
6363}
6364
6365/*
6366 * Handle "N" and "n" commands.
6367 * cap->arg is SEARCH_REV for "N", 0 for "n".
6368 */
6369 static void
6370nv_next(cap)
6371 cmdarg_T *cap;
6372{
6373 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6374}
6375
6376/*
6377 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6378 * Uses only cap->count1 and cap->oap from "cap".
6379 */
6380 static void
6381normal_search(cap, dir, pat, opt)
6382 cmdarg_T *cap;
6383 int dir;
6384 char_u *pat;
6385 int opt; /* extra flags for do_search() */
6386{
6387 int i;
6388
6389 cap->oap->motion_type = MCHAR;
6390 cap->oap->inclusive = FALSE;
6391 cap->oap->use_reg_one = TRUE;
6392 curwin->w_set_curswant = TRUE;
6393
6394 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006395 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 if (i == 0)
6397 clearop(cap->oap);
6398 else
6399 {
6400 if (i == 2)
6401 cap->oap->motion_type = MLINE;
6402#ifdef FEAT_VIRTUALEDIT
6403 curwin->w_cursor.coladd = 0;
6404#endif
6405#ifdef FEAT_FOLDING
6406 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6407 foldOpenCursor();
6408#endif
6409 }
6410
6411 /* "/$" will put the cursor after the end of the line, may need to
6412 * correct that here */
6413 check_cursor();
6414}
6415
6416/*
6417 * Character search commands.
6418 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6419 * ',' and FALSE for ';'.
6420 * cap->nchar is NUL for ',' and ';' (repeat the search)
6421 */
6422 static void
6423nv_csearch(cap)
6424 cmdarg_T *cap;
6425{
6426 int t_cmd;
6427
6428 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6429 t_cmd = TRUE;
6430 else
6431 t_cmd = FALSE;
6432
6433 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6435 clearopbeep(cap->oap);
6436 else
6437 {
6438 curwin->w_set_curswant = TRUE;
6439#ifdef FEAT_VIRTUALEDIT
6440 /* Include a Tab for "tx" and for "dfx". */
6441 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6442 && (t_cmd || cap->oap->op_type != OP_NOP))
6443 {
6444 colnr_T scol, ecol;
6445
6446 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6447 curwin->w_cursor.coladd = ecol - scol;
6448 }
6449 else
6450 curwin->w_cursor.coladd = 0;
6451#endif
6452#ifdef FEAT_VISUAL
6453 adjust_for_sel(cap);
6454#endif
6455#ifdef FEAT_FOLDING
6456 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6457 foldOpenCursor();
6458#endif
6459 }
6460}
6461
6462/*
6463 * "[" and "]" commands.
6464 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6465 */
6466 static void
6467nv_brackets(cap)
6468 cmdarg_T *cap;
6469{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006470 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 pos_T prev_pos;
6472 pos_T *pos = NULL; /* init for GCC */
6473 pos_T old_pos; /* cursor position before command */
6474 int flag;
6475 long n;
6476 int findc;
6477 int c;
6478
6479 cap->oap->motion_type = MCHAR;
6480 cap->oap->inclusive = FALSE;
6481 old_pos = curwin->w_cursor;
6482#ifdef FEAT_VIRTUALEDIT
6483 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6484#endif
6485
6486#ifdef FEAT_SEARCHPATH
6487 /*
6488 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6489 */
6490 if (cap->nchar == 'f')
6491 nv_gotofile(cap);
6492 else
6493#endif
6494
6495#ifdef FEAT_FIND_ID
6496 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006497 * Find the occurrence(s) of the identifier or define under cursor
6498 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 *
6500 * search list jump
6501 * fwd bwd fwd bwd fwd bwd
6502 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6503 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6504 */
6505 if (vim_strchr((char_u *)
6506#ifdef EBCDIC
6507 "iI\005dD\067",
6508#else
6509 "iI\011dD\004",
6510#endif
6511 cap->nchar) != NULL)
6512 {
6513 char_u *ptr;
6514 int len;
6515
6516 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6517 clearop(cap->oap);
6518 else
6519 {
6520 find_pattern_in_path(ptr, 0, len, TRUE,
6521 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6522 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6523 cap->count1,
6524 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6525 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6526 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6527 (linenr_T)MAXLNUM);
6528 curwin->w_set_curswant = TRUE;
6529 }
6530 }
6531 else
6532#endif
6533
6534 /*
6535 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6536 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6537 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6538 * "[m" or "]m" search for prev/next start of (Java) method.
6539 * "[M" or "]M" search for prev/next end of (Java) method.
6540 */
6541 if ( (cap->cmdchar == '['
6542 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6543 || (cap->cmdchar == ']'
6544 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6545 {
6546 if (cap->nchar == '*')
6547 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 prev_pos.lnum = 0;
6549 if (cap->nchar == 'm' || cap->nchar == 'M')
6550 {
6551 if (cap->cmdchar == '[')
6552 findc = '{';
6553 else
6554 findc = '}';
6555 n = 9999;
6556 }
6557 else
6558 {
6559 findc = cap->nchar;
6560 n = cap->count1;
6561 }
6562 for ( ; n > 0; --n)
6563 {
6564 if ((pos = findmatchlimit(cap->oap, findc,
6565 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6566 {
6567 if (new_pos.lnum == 0) /* nothing found */
6568 {
6569 if (cap->nchar != 'm' && cap->nchar != 'M')
6570 clearopbeep(cap->oap);
6571 }
6572 else
6573 pos = &new_pos; /* use last one found */
6574 break;
6575 }
6576 prev_pos = new_pos;
6577 curwin->w_cursor = *pos;
6578 new_pos = *pos;
6579 }
6580 curwin->w_cursor = old_pos;
6581
6582 /*
6583 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6584 * brought us to the match for "[m" and "]M" when inside a method.
6585 * Try finding the '{' or '}' we want to be at.
6586 * Also repeat for the given count.
6587 */
6588 if (cap->nchar == 'm' || cap->nchar == 'M')
6589 {
6590 /* norm is TRUE for "]M" and "[m" */
6591 int norm = ((findc == '{') == (cap->nchar == 'm'));
6592
6593 n = cap->count1;
6594 /* found a match: we were inside a method */
6595 if (prev_pos.lnum != 0)
6596 {
6597 pos = &prev_pos;
6598 curwin->w_cursor = prev_pos;
6599 if (norm)
6600 --n;
6601 }
6602 else
6603 pos = NULL;
6604 while (n > 0)
6605 {
6606 for (;;)
6607 {
6608 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6609 {
6610 /* if not found anything, that's an error */
6611 if (pos == NULL)
6612 clearopbeep(cap->oap);
6613 n = 0;
6614 break;
6615 }
6616 c = gchar_cursor();
6617 if (c == '{' || c == '}')
6618 {
6619 /* Must have found end/start of class: use it.
6620 * Or found the place to be at. */
6621 if ((c == findc && norm) || (n == 1 && !norm))
6622 {
6623 new_pos = curwin->w_cursor;
6624 pos = &new_pos;
6625 n = 0;
6626 }
6627 /* if no match found at all, we started outside of the
6628 * class and we're inside now. Just go on. */
6629 else if (new_pos.lnum == 0)
6630 {
6631 new_pos = curwin->w_cursor;
6632 pos = &new_pos;
6633 }
6634 /* found start/end of other method: go to match */
6635 else if ((pos = findmatchlimit(cap->oap, findc,
6636 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6637 0)) == NULL)
6638 n = 0;
6639 else
6640 curwin->w_cursor = *pos;
6641 break;
6642 }
6643 }
6644 --n;
6645 }
6646 curwin->w_cursor = old_pos;
6647 if (pos == NULL && new_pos.lnum != 0)
6648 clearopbeep(cap->oap);
6649 }
6650 if (pos != NULL)
6651 {
6652 setpcmark();
6653 curwin->w_cursor = *pos;
6654 curwin->w_set_curswant = TRUE;
6655#ifdef FEAT_FOLDING
6656 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6657 && cap->oap->op_type == OP_NOP)
6658 foldOpenCursor();
6659#endif
6660 }
6661 }
6662
6663 /*
6664 * "[[", "[]", "]]" and "][": move to start or end of function
6665 */
6666 else if (cap->nchar == '[' || cap->nchar == ']')
6667 {
6668 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6669 flag = '{';
6670 else
6671 flag = '}'; /* "][" or "[]" */
6672
6673 curwin->w_set_curswant = TRUE;
6674 /*
6675 * Imitate strange Vi behaviour: When using "]]" with an operator
6676 * we also stop at '}'.
6677 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006678 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 (cap->oap->op_type != OP_NOP
6680 && cap->arg == FORWARD && flag == '{')))
6681 clearopbeep(cap->oap);
6682 else
6683 {
6684 if (cap->oap->op_type == OP_NOP)
6685 beginline(BL_WHITE | BL_FIX);
6686#ifdef FEAT_FOLDING
6687 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6688 foldOpenCursor();
6689#endif
6690 }
6691 }
6692
6693 /*
6694 * "[p", "[P", "]P" and "]p": put with indent adjustment
6695 */
6696 else if (cap->nchar == 'p' || cap->nchar == 'P')
6697 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006698 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 {
6700 prep_redo_cmd(cap);
6701 do_put(cap->oap->regname,
6702 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6703 cap->count1, PUT_FIXINDENT);
6704 }
6705 }
6706
6707 /*
6708 * "['", "[`", "]'" and "]`": jump to next mark
6709 */
6710 else if (cap->nchar == '\'' || cap->nchar == '`')
6711 {
6712 pos = &curwin->w_cursor;
6713 for (n = cap->count1; n > 0; --n)
6714 {
6715 prev_pos = *pos;
6716 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6717 cap->nchar == '\'');
6718 if (pos == NULL)
6719 break;
6720 }
6721 if (pos == NULL)
6722 pos = &prev_pos;
6723 nv_cursormark(cap, cap->nchar == '\'', pos);
6724 }
6725
6726#ifdef FEAT_MOUSE
6727 /*
6728 * [ or ] followed by a middle mouse click: put selected text with
6729 * indent adjustment. Any other button just does as usual.
6730 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006731 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 {
6733 (void)do_mouse(cap->oap, cap->nchar,
6734 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6735 cap->count1, PUT_FIXINDENT);
6736 }
6737#endif /* FEAT_MOUSE */
6738
6739#ifdef FEAT_FOLDING
6740 /*
6741 * "[z" and "]z": move to start or end of open fold.
6742 */
6743 else if (cap->nchar == 'z')
6744 {
6745 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6746 cap->count1) == FAIL)
6747 clearopbeep(cap->oap);
6748 }
6749#endif
6750
6751#ifdef FEAT_DIFF
6752 /*
6753 * "[c" and "]c": move to next or previous diff-change.
6754 */
6755 else if (cap->nchar == 'c')
6756 {
6757 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6758 cap->count1) == FAIL)
6759 clearopbeep(cap->oap);
6760 }
6761#endif
6762
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006763#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006764 /*
6765 * "[s", "[S", "]s" and "]S": move to next spell error.
6766 */
6767 else if (cap->nchar == 's' || cap->nchar == 'S')
6768 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006769 setpcmark();
6770 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006771 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6772 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006773 {
6774 clearopbeep(cap->oap);
6775 break;
6776 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006777# ifdef FEAT_FOLDING
6778 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6779 foldOpenCursor();
6780# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006781 }
6782#endif
6783
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 /* Not a valid cap->nchar. */
6785 else
6786 clearopbeep(cap->oap);
6787}
6788
6789/*
6790 * Handle Normal mode "%" command.
6791 */
6792 static void
6793nv_percent(cap)
6794 cmdarg_T *cap;
6795{
6796 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006797#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 linenr_T lnum = curwin->w_cursor.lnum;
6799#endif
6800
6801 cap->oap->inclusive = TRUE;
6802 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6803 {
6804 if (cap->count0 > 100)
6805 clearopbeep(cap->oap);
6806 else
6807 {
6808 cap->oap->motion_type = MLINE;
6809 setpcmark();
6810 /* Round up, so CTRL-G will give same value. Watch out for a
6811 * large line count, the line number must not go negative! */
6812 if (curbuf->b_ml.ml_line_count > 1000000)
6813 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6814 / 100L * cap->count0;
6815 else
6816 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6817 cap->count0 + 99L) / 100L;
6818 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6819 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6820 beginline(BL_SOL | BL_FIX);
6821 }
6822 }
6823 else /* "%" : go to matching paren */
6824 {
6825 cap->oap->motion_type = MCHAR;
6826 cap->oap->use_reg_one = TRUE;
6827 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6828 clearopbeep(cap->oap);
6829 else
6830 {
6831 setpcmark();
6832 curwin->w_cursor = *pos;
6833 curwin->w_set_curswant = TRUE;
6834#ifdef FEAT_VIRTUALEDIT
6835 curwin->w_cursor.coladd = 0;
6836#endif
6837#ifdef FEAT_VISUAL
6838 adjust_for_sel(cap);
6839#endif
6840 }
6841 }
6842#ifdef FEAT_FOLDING
6843 if (cap->oap->op_type == OP_NOP
6844 && lnum != curwin->w_cursor.lnum
6845 && (fdo_flags & FDO_PERCENT)
6846 && KeyTyped)
6847 foldOpenCursor();
6848#endif
6849}
6850
6851/*
6852 * Handle "(" and ")" commands.
6853 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6854 */
6855 static void
6856nv_brace(cap)
6857 cmdarg_T *cap;
6858{
6859 cap->oap->motion_type = MCHAR;
6860 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006861 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6862 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 curwin->w_set_curswant = TRUE;
6864
6865 if (findsent(cap->arg, cap->count1) == FAIL)
6866 clearopbeep(cap->oap);
6867 else
6868 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006869 /* Don't leave the cursor on the NUL past end of line. */
6870 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871#ifdef FEAT_VIRTUALEDIT
6872 curwin->w_cursor.coladd = 0;
6873#endif
6874#ifdef FEAT_FOLDING
6875 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6876 foldOpenCursor();
6877#endif
6878 }
6879}
6880
6881/*
6882 * "m" command: Mark a position.
6883 */
6884 static void
6885nv_mark(cap)
6886 cmdarg_T *cap;
6887{
6888 if (!checkclearop(cap->oap))
6889 {
6890 if (setmark(cap->nchar) == FAIL)
6891 clearopbeep(cap->oap);
6892 }
6893}
6894
6895/*
6896 * "{" and "}" commands.
6897 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6898 */
6899 static void
6900nv_findpar(cap)
6901 cmdarg_T *cap;
6902{
6903 cap->oap->motion_type = MCHAR;
6904 cap->oap->inclusive = FALSE;
6905 cap->oap->use_reg_one = TRUE;
6906 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006907 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 clearopbeep(cap->oap);
6909 else
6910 {
6911#ifdef FEAT_VIRTUALEDIT
6912 curwin->w_cursor.coladd = 0;
6913#endif
6914#ifdef FEAT_FOLDING
6915 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6916 foldOpenCursor();
6917#endif
6918 }
6919}
6920
6921/*
6922 * "u" command: Undo or make lower case.
6923 */
6924 static void
6925nv_undo(cap)
6926 cmdarg_T *cap;
6927{
6928 if (cap->oap->op_type == OP_LOWER
6929#ifdef FEAT_VISUAL
6930 || VIsual_active
6931#endif
6932 )
6933 {
6934 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6935 cap->cmdchar = 'g';
6936 cap->nchar = 'u';
6937 nv_operator(cap);
6938 }
6939 else
6940 nv_kundo(cap);
6941}
6942
6943/*
6944 * <Undo> command.
6945 */
6946 static void
6947nv_kundo(cap)
6948 cmdarg_T *cap;
6949{
6950 if (!checkclearopq(cap->oap))
6951 {
6952 u_undo((int)cap->count1);
6953 curwin->w_set_curswant = TRUE;
6954 }
6955}
6956
6957/*
6958 * Handle the "r" command.
6959 */
6960 static void
6961nv_replace(cap)
6962 cmdarg_T *cap;
6963{
6964 char_u *ptr;
6965 int had_ctrl_v;
6966 long n;
6967
6968 if (checkclearop(cap->oap))
6969 return;
6970
6971 /* get another character */
6972 if (cap->nchar == Ctrl_V)
6973 {
6974 had_ctrl_v = Ctrl_V;
6975 cap->nchar = get_literal();
6976 /* Don't redo a multibyte character with CTRL-V. */
6977 if (cap->nchar > DEL)
6978 had_ctrl_v = NUL;
6979 }
6980 else
6981 had_ctrl_v = NUL;
6982
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006983 /* Abort if the character is a special key. */
6984 if (IS_SPECIAL(cap->nchar))
6985 {
6986 clearopbeep(cap->oap);
6987 return;
6988 }
6989
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990#ifdef FEAT_VISUAL
6991 /* Visual mode "r" */
6992 if (VIsual_active)
6993 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006994 if (got_int)
6995 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 nv_operator(cap);
6997 return;
6998 }
6999#endif
7000
7001#ifdef FEAT_VIRTUALEDIT
7002 /* Break tabs, etc. */
7003 if (virtual_active())
7004 {
7005 if (u_save_cursor() == FAIL)
7006 return;
7007 if (gchar_cursor() == NUL)
7008 {
7009 /* Add extra space and put the cursor on the first one. */
7010 coladvance_force((colnr_T)(getviscol() + cap->count1));
7011 curwin->w_cursor.col -= cap->count1;
7012 }
7013 else if (gchar_cursor() == TAB)
7014 coladvance_force(getviscol());
7015 }
7016#endif
7017
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007018 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007020 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021#ifdef FEAT_MBYTE
7022 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7023#endif
7024 )
7025 {
7026 clearopbeep(cap->oap);
7027 return;
7028 }
7029
7030 /*
7031 * Replacing with a TAB is done by edit() when it is complicated because
7032 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7033 * Other characters are done below to avoid problems with things like
7034 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7035 */
7036 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7037 {
7038 stuffnumReadbuff(cap->count1);
7039 stuffcharReadbuff('R');
7040 stuffcharReadbuff('\t');
7041 stuffcharReadbuff(ESC);
7042 return;
7043 }
7044
7045 /* save line for undo */
7046 if (u_save_cursor() == FAIL)
7047 return;
7048
7049 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7050 {
7051 /*
7052 * Replace character(s) by a single newline.
7053 * Strange vi behaviour: Only one newline is inserted.
7054 * Delete the characters here.
7055 * Insert the newline with an insert command, takes care of
7056 * autoindent. The insert command depends on being on the last
7057 * character of a line or not.
7058 */
7059#ifdef FEAT_MBYTE
7060 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7061#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007062 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063#endif
7064 stuffcharReadbuff('\r');
7065 stuffcharReadbuff(ESC);
7066
7067 /* Give 'r' to edit(), to get the redo command right. */
7068 invoke_edit(cap, TRUE, 'r', FALSE);
7069 }
7070 else
7071 {
7072 prep_redo(cap->oap->regname, cap->count1,
7073 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7074
7075 curbuf->b_op_start = curwin->w_cursor;
7076#ifdef FEAT_MBYTE
7077 if (has_mbyte)
7078 {
7079 int old_State = State;
7080
7081 if (cap->ncharC1 != 0)
7082 AppendCharToRedobuff(cap->ncharC1);
7083 if (cap->ncharC2 != 0)
7084 AppendCharToRedobuff(cap->ncharC2);
7085
7086 /* This is slow, but it handles replacing a single-byte with a
7087 * multi-byte and the other way around. Also handles adding
7088 * composing characters for utf-8. */
7089 for (n = cap->count1; n > 0; --n)
7090 {
7091 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007092 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7093 {
7094 int c = ins_copychar(curwin->w_cursor.lnum
7095 + (cap->nchar == Ctrl_Y ? -1 : 1));
7096 if (c != NUL)
7097 ins_char(c);
7098 else
7099 /* will be decremented further down */
7100 ++curwin->w_cursor.col;
7101 }
7102 else
7103 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 State = old_State;
7105 if (cap->ncharC1 != 0)
7106 ins_char(cap->ncharC1);
7107 if (cap->ncharC2 != 0)
7108 ins_char(cap->ncharC2);
7109 }
7110 }
7111 else
7112#endif
7113 {
7114 /*
7115 * Replace the characters within one line.
7116 */
7117 for (n = cap->count1; n > 0; --n)
7118 {
7119 /*
7120 * Get ptr again, because u_save and/or showmatch() will have
7121 * released the line. At the same time we let know that the
7122 * line will be changed.
7123 */
7124 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007125 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7126 {
7127 int c = ins_copychar(curwin->w_cursor.lnum
7128 + (cap->nchar == Ctrl_Y ? -1 : 1));
7129 if (c != NUL)
7130 ptr[curwin->w_cursor.col] = c;
7131 }
7132 else
7133 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 if (p_sm && msg_silent == 0)
7135 showmatch(cap->nchar);
7136 ++curwin->w_cursor.col;
7137 }
7138#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007139 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007141 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142
Bram Moolenaar009b2592004-10-24 19:18:58 +00007143 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007144 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007146 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 }
7148#endif
7149
7150 /* mark the buffer as changed and prepare for displaying */
7151 changed_bytes(curwin->w_cursor.lnum,
7152 (colnr_T)(curwin->w_cursor.col - cap->count1));
7153 }
7154 --curwin->w_cursor.col; /* cursor on the last replaced char */
7155#ifdef FEAT_MBYTE
7156 /* if the character on the left of the current cursor is a multi-byte
7157 * character, move two characters left */
7158 if (has_mbyte)
7159 mb_adjust_cursor();
7160#endif
7161 curbuf->b_op_end = curwin->w_cursor;
7162 curwin->w_set_curswant = TRUE;
7163 set_last_insert(cap->nchar);
7164 }
7165}
7166
7167#ifdef FEAT_VISUAL
7168/*
7169 * 'o': Exchange start and end of Visual area.
7170 * 'O': same, but in block mode exchange left and right corners.
7171 */
7172 static void
7173v_swap_corners(cmdchar)
7174 int cmdchar;
7175{
7176 pos_T old_cursor;
7177 colnr_T left, right;
7178
7179 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7180 {
7181 old_cursor = curwin->w_cursor;
7182 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7183 curwin->w_cursor.lnum = VIsual.lnum;
7184 coladvance(left);
7185 VIsual = curwin->w_cursor;
7186
7187 curwin->w_cursor.lnum = old_cursor.lnum;
7188 curwin->w_curswant = right;
7189 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7190 * right one column */
7191 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7192 ++curwin->w_curswant;
7193 coladvance(curwin->w_curswant);
7194 if (curwin->w_cursor.col == old_cursor.col
7195#ifdef FEAT_VIRTUALEDIT
7196 && (!virtual_active()
7197 || curwin->w_cursor.coladd == old_cursor.coladd)
7198#endif
7199 )
7200 {
7201 curwin->w_cursor.lnum = VIsual.lnum;
7202 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7203 ++right;
7204 coladvance(right);
7205 VIsual = curwin->w_cursor;
7206
7207 curwin->w_cursor.lnum = old_cursor.lnum;
7208 coladvance(left);
7209 curwin->w_curswant = left;
7210 }
7211 }
7212 else
7213 {
7214 old_cursor = curwin->w_cursor;
7215 curwin->w_cursor = VIsual;
7216 VIsual = old_cursor;
7217 curwin->w_set_curswant = TRUE;
7218 }
7219}
7220#endif /* FEAT_VISUAL */
7221
7222/*
7223 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7224 */
7225 static void
7226nv_Replace(cap)
7227 cmdarg_T *cap;
7228{
7229#ifdef FEAT_VISUAL
7230 if (VIsual_active) /* "R" is replace lines */
7231 {
7232 cap->cmdchar = 'c';
7233 cap->nchar = NUL;
7234 VIsual_mode = 'V';
7235 nv_operator(cap);
7236 }
7237 else
7238#endif
7239 if (!checkclearopq(cap->oap))
7240 {
7241 if (!curbuf->b_p_ma)
7242 EMSG(_(e_modifiable));
7243 else
7244 {
7245#ifdef FEAT_VIRTUALEDIT
7246 if (virtual_active())
7247 coladvance(getviscol());
7248#endif
7249 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7250 }
7251 }
7252}
7253
7254#ifdef FEAT_VREPLACE
7255/*
7256 * "gr".
7257 */
7258 static void
7259nv_vreplace(cap)
7260 cmdarg_T *cap;
7261{
7262# ifdef FEAT_VISUAL
7263 if (VIsual_active)
7264 {
7265 cap->cmdchar = 'r';
7266 cap->nchar = cap->extra_char;
7267 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7268 }
7269 else
7270# endif
7271 if (!checkclearopq(cap->oap))
7272 {
7273 if (!curbuf->b_p_ma)
7274 EMSG(_(e_modifiable));
7275 else
7276 {
7277 if (cap->extra_char == Ctrl_V) /* get another character */
7278 cap->extra_char = get_literal();
7279 stuffcharReadbuff(cap->extra_char);
7280 stuffcharReadbuff(ESC);
7281# ifdef FEAT_VIRTUALEDIT
7282 if (virtual_active())
7283 coladvance(getviscol());
7284# endif
7285 invoke_edit(cap, TRUE, 'v', FALSE);
7286 }
7287 }
7288}
7289#endif
7290
7291/*
7292 * Swap case for "~" command, when it does not work like an operator.
7293 */
7294 static void
7295n_swapchar(cap)
7296 cmdarg_T *cap;
7297{
7298 long n;
7299 pos_T startpos;
7300 int did_change = 0;
7301#ifdef FEAT_NETBEANS_INTG
7302 pos_T pos;
7303 char_u *ptr;
7304 int count;
7305#endif
7306
7307 if (checkclearopq(cap->oap))
7308 return;
7309
7310 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7311 {
7312 clearopbeep(cap->oap);
7313 return;
7314 }
7315
7316 prep_redo_cmd(cap);
7317
7318 if (u_save_cursor() == FAIL)
7319 return;
7320
7321 startpos = curwin->w_cursor;
7322#ifdef FEAT_NETBEANS_INTG
7323 pos = startpos;
7324#endif
7325 for (n = cap->count1; n > 0; --n)
7326 {
7327 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7328 inc_cursor();
7329 if (gchar_cursor() == NUL)
7330 {
7331 if (vim_strchr(p_ww, '~') != NULL
7332 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7333 {
7334#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007335 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 {
7337 if (did_change)
7338 {
7339 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007340 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007341 netbeans_removed(curbuf, pos.lnum, pos.col,
7342 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007344 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 }
7346 pos.col = 0;
7347 pos.lnum++;
7348 }
7349#endif
7350 ++curwin->w_cursor.lnum;
7351 curwin->w_cursor.col = 0;
7352 if (n > 1)
7353 {
7354 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7355 break;
7356 u_clearline();
7357 }
7358 }
7359 else
7360 break;
7361 }
7362 }
7363#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007364 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 {
7366 ptr = ml_get(pos.lnum);
7367 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007368 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7369 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 }
7371#endif
7372
7373
7374 check_cursor();
7375 curwin->w_set_curswant = TRUE;
7376 if (did_change)
7377 {
7378 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7379 0L);
7380 curbuf->b_op_start = startpos;
7381 curbuf->b_op_end = curwin->w_cursor;
7382 if (curbuf->b_op_end.col > 0)
7383 --curbuf->b_op_end.col;
7384 }
7385}
7386
7387/*
7388 * Move cursor to mark.
7389 */
7390 static void
7391nv_cursormark(cap, flag, pos)
7392 cmdarg_T *cap;
7393 int flag;
7394 pos_T *pos;
7395{
7396 if (check_mark(pos) == FAIL)
7397 clearop(cap->oap);
7398 else
7399 {
7400 if (cap->cmdchar == '\''
7401 || cap->cmdchar == '`'
7402 || cap->cmdchar == '['
7403 || cap->cmdchar == ']')
7404 setpcmark();
7405 curwin->w_cursor = *pos;
7406 if (flag)
7407 beginline(BL_WHITE | BL_FIX);
7408 else
7409 check_cursor();
7410 }
7411 cap->oap->motion_type = flag ? MLINE : MCHAR;
7412 if (cap->cmdchar == '`')
7413 cap->oap->use_reg_one = TRUE;
7414 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7415 curwin->w_set_curswant = TRUE;
7416}
7417
7418#ifdef FEAT_VISUAL
7419/*
7420 * Handle commands that are operators in Visual mode.
7421 */
7422 static void
7423v_visop(cap)
7424 cmdarg_T *cap;
7425{
7426 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7427
7428 /* Uppercase means linewise, except in block mode, then "D" deletes till
7429 * the end of the line, and "C" replaces til EOL */
7430 if (isupper(cap->cmdchar))
7431 {
7432 if (VIsual_mode != Ctrl_V)
7433 VIsual_mode = 'V';
7434 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7435 curwin->w_curswant = MAXCOL;
7436 }
7437 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7438 nv_operator(cap);
7439}
7440#endif
7441
7442/*
7443 * "s" and "S" commands.
7444 */
7445 static void
7446nv_subst(cap)
7447 cmdarg_T *cap;
7448{
7449#ifdef FEAT_VISUAL
7450 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7451 {
7452 if (cap->cmdchar == 'S')
7453 VIsual_mode = 'V';
7454 cap->cmdchar = 'c';
7455 nv_operator(cap);
7456 }
7457 else
7458#endif
7459 nv_optrans(cap);
7460}
7461
7462/*
7463 * Abbreviated commands.
7464 */
7465 static void
7466nv_abbrev(cap)
7467 cmdarg_T *cap;
7468{
7469 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7470 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7471
7472#ifdef FEAT_VISUAL
7473 /* in Visual mode these commands are operators */
7474 if (VIsual_active)
7475 v_visop(cap);
7476 else
7477#endif
7478 nv_optrans(cap);
7479}
7480
7481/*
7482 * Translate a command into another command.
7483 */
7484 static void
7485nv_optrans(cap)
7486 cmdarg_T *cap;
7487{
7488 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7489 (char_u *)"d$", (char_u *)"c$",
7490 (char_u *)"cl", (char_u *)"cc",
7491 (char_u *)"yy", (char_u *)":s\r"};
7492 static char_u *str = (char_u *)"xXDCsSY&";
7493
7494 if (!checkclearopq(cap->oap))
7495 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007496 /* In Vi "2D" doesn't delete the next line. Can't translate it
7497 * either, because "2." should also not use the count. */
7498 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7499 {
7500 cap->oap->start = curwin->w_cursor;
7501 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007502#ifdef FEAT_EVAL
7503 set_op_var(OP_DELETE);
7504#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007505 cap->count1 = 1;
7506 nv_dollar(cap);
7507 finish_op = TRUE;
7508 ResetRedobuff();
7509 AppendCharToRedobuff('D');
7510 }
7511 else
7512 {
7513 if (cap->count0)
7514 stuffnumReadbuff(cap->count0);
7515 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 }
7518 cap->opcount = 0;
7519}
7520
7521/*
7522 * "'" and "`" commands. Also for "g'" and "g`".
7523 * cap->arg is TRUE for "'" and "g'".
7524 */
7525 static void
7526nv_gomark(cap)
7527 cmdarg_T *cap;
7528{
7529 pos_T *pos;
7530 int c;
7531#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007532 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7534#endif
7535
7536 if (cap->cmdchar == 'g')
7537 c = cap->extra_char;
7538 else
7539 c = cap->nchar;
7540 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7541 if (pos == (pos_T *)-1) /* jumped to other file */
7542 {
7543 if (cap->arg)
7544 {
7545 check_cursor_lnum();
7546 beginline(BL_WHITE | BL_FIX);
7547 }
7548 else
7549 check_cursor();
7550 }
7551 else
7552 nv_cursormark(cap, cap->arg, pos);
7553
7554#ifdef FEAT_VIRTUALEDIT
7555 /* May need to clear the coladd that a mark includes. */
7556 if (!virtual_active())
7557 curwin->w_cursor.coladd = 0;
7558#endif
7559#ifdef FEAT_FOLDING
7560 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007561 && pos != NULL
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007562 && (pos == (pos_T *)-1 || !equalpos(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 && (fdo_flags & FDO_MARK)
7564 && old_KeyTyped)
7565 foldOpenCursor();
7566#endif
7567}
7568
7569/*
7570 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7571 */
7572 static void
7573nv_pcmark(cap)
7574 cmdarg_T *cap;
7575{
7576#ifdef FEAT_JUMPLIST
7577 pos_T *pos;
7578# ifdef FEAT_FOLDING
7579 linenr_T lnum = curwin->w_cursor.lnum;
7580 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7581# endif
7582
7583 if (!checkclearopq(cap->oap))
7584 {
7585 if (cap->cmdchar == 'g')
7586 pos = movechangelist((int)cap->count1);
7587 else
7588 pos = movemark((int)cap->count1);
7589 if (pos == (pos_T *)-1) /* jump to other file */
7590 {
7591 curwin->w_set_curswant = TRUE;
7592 check_cursor();
7593 }
7594 else if (pos != NULL) /* can jump */
7595 nv_cursormark(cap, FALSE, pos);
7596 else if (cap->cmdchar == 'g')
7597 {
7598 if (curbuf->b_changelistlen == 0)
7599 EMSG(_("E664: changelist is empty"));
7600 else if (cap->count1 < 0)
7601 EMSG(_("E662: At start of changelist"));
7602 else
7603 EMSG(_("E663: At end of changelist"));
7604 }
7605 else
7606 clearopbeep(cap->oap);
7607# ifdef FEAT_FOLDING
7608 if (cap->oap->op_type == OP_NOP
7609 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7610 && (fdo_flags & FDO_MARK)
7611 && old_KeyTyped)
7612 foldOpenCursor();
7613# endif
7614 }
7615#else
7616 clearopbeep(cap->oap);
7617#endif
7618}
7619
7620/*
7621 * Handle '"' command.
7622 */
7623 static void
7624nv_regname(cap)
7625 cmdarg_T *cap;
7626{
7627 if (checkclearop(cap->oap))
7628 return;
7629#ifdef FEAT_EVAL
7630 if (cap->nchar == '=')
7631 cap->nchar = get_expr_register();
7632#endif
7633 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7634 {
7635 cap->oap->regname = cap->nchar;
7636 cap->opcount = cap->count0; /* remember count before '"' */
7637#ifdef FEAT_EVAL
7638 set_reg_var(cap->oap->regname);
7639#endif
7640 }
7641 else
7642 clearopbeep(cap->oap);
7643}
7644
7645#ifdef FEAT_VISUAL
7646/*
7647 * Handle "v", "V" and "CTRL-V" commands.
7648 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7649 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007650 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 */
7652 static void
7653nv_visual(cap)
7654 cmdarg_T *cap;
7655{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007656 if (cap->cmdchar == Ctrl_Q)
7657 cap->cmdchar = Ctrl_V;
7658
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7660 * characterwise, linewise, or blockwise. */
7661 if (cap->oap->op_type != OP_NOP)
7662 {
7663 cap->oap->motion_force = cap->cmdchar;
7664 finish_op = FALSE; /* operator doesn't finish now but later */
7665 return;
7666 }
7667
7668 VIsual_select = cap->arg;
7669 if (VIsual_active) /* change Visual mode */
7670 {
7671 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7672 end_visual_mode();
7673 else /* toggle char/block mode */
7674 { /* or char/line mode */
7675 VIsual_mode = cap->cmdchar;
7676 showmode();
7677 }
7678 redraw_curbuf_later(INVERTED); /* update the inversion */
7679 }
7680 else /* start Visual mode */
7681 {
7682 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007683 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007685 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 VIsual = curwin->w_cursor;
7687
7688 VIsual_active = TRUE;
7689 VIsual_reselect = TRUE;
7690 if (!cap->arg)
7691 /* start Select mode when 'selectmode' contains "cmd" */
7692 may_start_select('c');
7693#ifdef FEAT_MOUSE
7694 setmouse();
7695#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007696 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 redraw_cmdline = TRUE; /* show visual mode later */
7698 /*
7699 * For V and ^V, we multiply the number of lines even if there
7700 * was only one -- webb
7701 */
7702 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7703 {
7704 curwin->w_cursor.lnum +=
7705 resel_VIsual_line_count * cap->count0 - 1;
7706 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7707 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7708 }
7709 VIsual_mode = resel_VIsual_mode;
7710 if (VIsual_mode == 'v')
7711 {
7712 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007713 {
7714 validate_virtcol();
7715 curwin->w_curswant = curwin->w_virtcol
7716 + resel_VIsual_vcol * cap->count0 - 1;
7717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007719 curwin->w_curswant = resel_VIsual_vcol;
7720 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007722 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 {
7724 curwin->w_curswant = MAXCOL;
7725 coladvance((colnr_T)MAXCOL);
7726 }
7727 else if (VIsual_mode == Ctrl_V)
7728 {
7729 validate_virtcol();
7730 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007731 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 coladvance(curwin->w_curswant);
7733 }
7734 else
7735 curwin->w_set_curswant = TRUE;
7736 redraw_curbuf_later(INVERTED); /* show the inversion */
7737 }
7738 else
7739 {
7740 if (!cap->arg)
7741 /* start Select mode when 'selectmode' contains "cmd" */
7742 may_start_select('c');
7743 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007744 if (VIsual_mode != 'V' && *p_sel == 'e')
7745 ++cap->count1; /* include one more char */
7746 if (cap->count0 > 0 && --cap->count1 > 0)
7747 {
7748 /* With a count select that many characters or lines. */
7749 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7750 nv_right(cap);
7751 else if (VIsual_mode == 'V')
7752 nv_down(cap);
7753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 }
7755 }
7756}
7757
7758/*
7759 * Start selection for Shift-movement keys.
7760 */
7761 void
7762start_selection()
7763{
7764 /* if 'selectmode' contains "key", start Select mode */
7765 may_start_select('k');
7766 n_start_visual_mode('v');
7767}
7768
7769/*
7770 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7771 */
7772 void
7773may_start_select(c)
7774 int c;
7775{
7776 VIsual_select = (stuff_empty() && typebuf_typed()
7777 && (vim_strchr(p_slm, c) != NULL));
7778}
7779
7780/*
7781 * Start Visual mode "c".
7782 * Should set VIsual_select before calling this.
7783 */
7784 static void
7785n_start_visual_mode(c)
7786 int c;
7787{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007788#ifdef FEAT_CONCEAL
7789 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007790 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007791#endif
7792
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 VIsual_mode = c;
7794 VIsual_active = TRUE;
7795 VIsual_reselect = TRUE;
7796#ifdef FEAT_VIRTUALEDIT
7797 /* Corner case: the 0 position in a tab may change when going into
7798 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7799 */
7800 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007801 {
7802 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805#endif
7806 VIsual = curwin->w_cursor;
7807
7808#ifdef FEAT_FOLDING
7809 foldAdjustVisual();
7810#endif
7811
7812#ifdef FEAT_MOUSE
7813 setmouse();
7814#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007815#ifdef FEAT_CONCEAL
7816 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007817 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007818#endif
7819
Bram Moolenaar09df3122006-01-23 22:23:09 +00007820 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 redraw_cmdline = TRUE; /* show visual mode later */
7822#ifdef FEAT_CLIPBOARD
7823 /* Make sure the clipboard gets updated. Needed because start and
7824 * end may still be the same, and the selection needs to be owned */
7825 clip_star.vmode = NUL;
7826#endif
7827
7828 /* Only need to redraw this line, unless still need to redraw an old
7829 * Visual area (when 'lazyredraw' is set). */
7830 if (curwin->w_redr_type < INVERTED)
7831 {
7832 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7833 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7834 }
7835}
7836
7837#endif /* FEAT_VISUAL */
7838
7839/*
7840 * CTRL-W: Window commands
7841 */
7842 static void
7843nv_window(cap)
7844 cmdarg_T *cap;
7845{
7846#ifdef FEAT_WINDOWS
7847 if (!checkclearop(cap->oap))
7848 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7849#else
7850 (void)checkclearop(cap->oap);
7851#endif
7852}
7853
7854/*
7855 * CTRL-Z: Suspend
7856 */
7857 static void
7858nv_suspend(cap)
7859 cmdarg_T *cap;
7860{
7861 clearop(cap->oap);
7862#ifdef FEAT_VISUAL
7863 if (VIsual_active)
7864 end_visual_mode(); /* stop Visual mode */
7865#endif
7866 do_cmdline_cmd((char_u *)"st");
7867}
7868
7869/*
7870 * Commands starting with "g".
7871 */
7872 static void
7873nv_g_cmd(cap)
7874 cmdarg_T *cap;
7875{
7876 oparg_T *oap = cap->oap;
7877#ifdef FEAT_VISUAL
7878 pos_T tpos;
7879#endif
7880 int i;
7881 int flag = FALSE;
7882
7883 switch (cap->nchar)
7884 {
7885#ifdef MEM_PROFILE
7886 /*
7887 * "g^A": dump log of used memory.
7888 */
7889 case Ctrl_A:
7890 vim_mem_profile_dump();
7891 break;
7892#endif
7893
7894#ifdef FEAT_VREPLACE
7895 /*
7896 * "gR": Enter virtual replace mode.
7897 */
7898 case 'R':
7899 cap->arg = TRUE;
7900 nv_Replace(cap);
7901 break;
7902
7903 case 'r':
7904 nv_vreplace(cap);
7905 break;
7906#endif
7907
7908 case '&':
7909 do_cmdline_cmd((char_u *)"%s//~/&");
7910 break;
7911
7912#ifdef FEAT_VISUAL
7913 /*
7914 * "gv": Reselect the previous Visual area. If Visual already active,
7915 * exchange previous and current Visual area.
7916 */
7917 case 'v':
7918 if (checkclearop(oap))
7919 break;
7920
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007921 if ( curbuf->b_visual.vi_start.lnum == 0
7922 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7923 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 beep_flush();
7925 else
7926 {
7927 /* set w_cursor to the start of the Visual area, tpos to the end */
7928 if (VIsual_active)
7929 {
7930 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007931 VIsual_mode = curbuf->b_visual.vi_mode;
7932 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933# ifdef FEAT_EVAL
7934 curbuf->b_visual_mode_eval = i;
7935# endif
7936 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007937 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7938 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007940 tpos = curbuf->b_visual.vi_end;
7941 curbuf->b_visual.vi_end = curwin->w_cursor;
7942 curwin->w_cursor = curbuf->b_visual.vi_start;
7943 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 }
7945 else
7946 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007947 VIsual_mode = curbuf->b_visual.vi_mode;
7948 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7949 tpos = curbuf->b_visual.vi_end;
7950 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 }
7952
7953 VIsual_active = TRUE;
7954 VIsual_reselect = TRUE;
7955
7956 /* Set Visual to the start and w_cursor to the end of the Visual
7957 * area. Make sure they are on an existing character. */
7958 check_cursor();
7959 VIsual = curwin->w_cursor;
7960 curwin->w_cursor = tpos;
7961 check_cursor();
7962 update_topline();
7963 /*
7964 * When called from normal "g" command: start Select mode when
7965 * 'selectmode' contains "cmd". When called for K_SELECT, always
7966 * start Select mode.
7967 */
7968 if (cap->arg)
7969 VIsual_select = TRUE;
7970 else
7971 may_start_select('c');
7972#ifdef FEAT_MOUSE
7973 setmouse();
7974#endif
7975#ifdef FEAT_CLIPBOARD
7976 /* Make sure the clipboard gets updated. Needed because start and
7977 * end are still the same, and the selection needs to be owned */
7978 clip_star.vmode = NUL;
7979#endif
7980 redraw_curbuf_later(INVERTED);
7981 showmode();
7982 }
7983 break;
7984 /*
7985 * "gV": Don't reselect the previous Visual area after a Select mode
7986 * mapping of menu.
7987 */
7988 case 'V':
7989 VIsual_reselect = FALSE;
7990 break;
7991
7992 /*
7993 * "gh": start Select mode.
7994 * "gH": start Select line mode.
7995 * "g^H": start Select block mode.
7996 */
7997 case K_BS:
7998 cap->nchar = Ctrl_H;
7999 /* FALLTHROUGH */
8000 case 'h':
8001 case 'H':
8002 case Ctrl_H:
8003# ifdef EBCDIC
8004 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
8005 if (cap->nchar == Ctrl_H)
8006 cap->cmdchar = Ctrl_V;
8007 else
8008# endif
8009 cap->cmdchar = cap->nchar + ('v' - 'h');
8010 cap->arg = TRUE;
8011 nv_visual(cap);
8012 break;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02008013#endif /* FEAT_VISUAL */
Bram Moolenaar641e2862012-07-25 15:06:34 +02008014
8015 /* "gn", "gN" visually select next/previous search match
8016 * "gn" selects next match
8017 * "gN" selects previous match
8018 */
8019 case 'N':
8020 case 'n':
Bram Moolenaardad937f2012-07-27 21:05:54 +02008021#ifdef FEAT_VISUAL
Bram Moolenaar641e2862012-07-25 15:06:34 +02008022 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaardad937f2012-07-27 21:05:54 +02008023#endif
Bram Moolenaarf00dc262012-10-21 03:54:33 +02008024 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02008025 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026
8027 /*
8028 * "gj" and "gk" two new funny movement keys -- up and down
8029 * movement based on *screen* line rather than *file* line.
8030 */
8031 case 'j':
8032 case K_DOWN:
8033 /* with 'nowrap' it works just like the normal "j" command; also when
8034 * in a closed fold */
8035 if (!curwin->w_p_wrap
8036#ifdef FEAT_FOLDING
8037 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8038#endif
8039 )
8040 {
8041 oap->motion_type = MLINE;
8042 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8043 }
8044 else
8045 i = nv_screengo(oap, FORWARD, cap->count1);
8046 if (i == FAIL)
8047 clearopbeep(oap);
8048 break;
8049
8050 case 'k':
8051 case K_UP:
8052 /* with 'nowrap' it works just like the normal "k" command; also when
8053 * in a closed fold */
8054 if (!curwin->w_p_wrap
8055#ifdef FEAT_FOLDING
8056 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8057#endif
8058 )
8059 {
8060 oap->motion_type = MLINE;
8061 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8062 }
8063 else
8064 i = nv_screengo(oap, BACKWARD, cap->count1);
8065 if (i == FAIL)
8066 clearopbeep(oap);
8067 break;
8068
8069 /*
8070 * "gJ": join two lines without inserting a space.
8071 */
8072 case 'J':
8073 nv_join(cap);
8074 break;
8075
8076 /*
8077 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8078 * "gm": middle of "g0" and "g$".
8079 */
8080 case '^':
8081 flag = TRUE;
8082 /* FALLTHROUGH */
8083
8084 case '0':
8085 case 'm':
8086 case K_HOME:
8087 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 oap->motion_type = MCHAR;
8089 oap->inclusive = FALSE;
8090 if (curwin->w_p_wrap
8091#ifdef FEAT_VERTSPLIT
8092 && curwin->w_width != 0
8093#endif
8094 )
8095 {
8096 int width1 = W_WIDTH(curwin) - curwin_col_off();
8097 int width2 = width1 + curwin_col_off2();
8098
8099 validate_virtcol();
8100 i = 0;
8101 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8102 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8103 }
8104 else
8105 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008106 /* Go to the middle of the screen line. When 'number' or
8107 * 'relativenumber' is on and lines are wrapping the middle can be more
8108 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 if (cap->nchar == 'm')
8110 i += (W_WIDTH(curwin) - curwin_col_off()
8111 + ((curwin->w_p_wrap && i > 0)
8112 ? curwin_col_off2() : 0)) / 2;
8113 coladvance((colnr_T)i);
8114 if (flag)
8115 {
8116 do
8117 i = gchar_cursor();
8118 while (vim_iswhite(i) && oneright() == OK);
8119 }
8120 curwin->w_set_curswant = TRUE;
8121 break;
8122
8123 case '_':
8124 /* "g_": to the last non-blank character in the line or <count> lines
8125 * downward. */
8126 cap->oap->motion_type = MCHAR;
8127 cap->oap->inclusive = TRUE;
8128 curwin->w_curswant = MAXCOL;
8129 if (cursor_down((long)(cap->count1 - 1),
8130 cap->oap->op_type == OP_NOP) == FAIL)
8131 clearopbeep(cap->oap);
8132 else
8133 {
8134 char_u *ptr = ml_get_curline();
8135
8136 /* In Visual mode we may end up after the line. */
8137 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8138 --curwin->w_cursor.col;
8139
8140 /* Decrease the cursor column until it's on a non-blank. */
8141 while (curwin->w_cursor.col > 0
8142 && vim_iswhite(ptr[curwin->w_cursor.col]))
8143 --curwin->w_cursor.col;
8144 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008145#ifdef FEAT_VISUAL
8146 adjust_for_sel(cap);
8147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 }
8149 break;
8150
8151 case '$':
8152 case K_END:
8153 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {
8155 int col_off = curwin_col_off();
8156
8157 oap->motion_type = MCHAR;
8158 oap->inclusive = TRUE;
8159 if (curwin->w_p_wrap
8160#ifdef FEAT_VERTSPLIT
8161 && curwin->w_width != 0
8162#endif
8163 )
8164 {
8165 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8166 if (cap->count1 == 1)
8167 {
8168 int width1 = W_WIDTH(curwin) - col_off;
8169 int width2 = width1 + curwin_col_off2();
8170
8171 validate_virtcol();
8172 i = width1 - 1;
8173 if (curwin->w_virtcol >= (colnr_T)width1)
8174 i += ((curwin->w_virtcol - width1) / width2 + 1)
8175 * width2;
8176 coladvance((colnr_T)i);
8177#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8178 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8179 {
8180 /*
8181 * Check for landing on a character that got split at
8182 * the end of the line. We do not want to advance to
8183 * the next screen line.
8184 */
8185 validate_virtcol();
8186 if (curwin->w_virtcol > (colnr_T)i)
8187 --curwin->w_cursor.col;
8188 }
8189#endif
8190 }
8191 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8192 clearopbeep(oap);
8193 }
8194 else
8195 {
8196 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8197 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008198
8199 /* Make sure we stick in this column. */
8200 validate_virtcol();
8201 curwin->w_curswant = curwin->w_virtcol;
8202 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 }
8204 }
8205 break;
8206
8207 /*
8208 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8209 */
8210 case '*':
8211 case '#':
8212#if POUND != '#'
8213 case POUND: /* pound sign (sometimes equal to '#') */
8214#endif
8215 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8216 case ']': /* :tselect for current identifier */
8217 nv_ident(cap);
8218 break;
8219
8220 /*
8221 * ge and gE: go back to end of word
8222 */
8223 case 'e':
8224 case 'E':
8225 oap->motion_type = MCHAR;
8226 curwin->w_set_curswant = TRUE;
8227 oap->inclusive = TRUE;
8228 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8229 clearopbeep(oap);
8230 break;
8231
8232 /*
8233 * "g CTRL-G": display info about cursor position
8234 */
8235 case Ctrl_G:
8236 cursor_pos_info();
8237 break;
8238
8239 /*
8240 * "gi": start Insert at the last position.
8241 */
8242 case 'i':
8243 if (curbuf->b_last_insert.lnum != 0)
8244 {
8245 curwin->w_cursor = curbuf->b_last_insert;
8246 check_cursor_lnum();
8247 i = (int)STRLEN(ml_get_curline());
8248 if (curwin->w_cursor.col > (colnr_T)i)
8249 {
8250#ifdef FEAT_VIRTUALEDIT
8251 if (virtual_active())
8252 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8253#endif
8254 curwin->w_cursor.col = i;
8255 }
8256 }
8257 cap->cmdchar = 'i';
8258 nv_edit(cap);
8259 break;
8260
8261 /*
8262 * "gI": Start insert in column 1.
8263 */
8264 case 'I':
8265 beginline(0);
8266 if (!checkclearopq(oap))
8267 invoke_edit(cap, FALSE, 'g', FALSE);
8268 break;
8269
8270#ifdef FEAT_SEARCHPATH
8271 /*
8272 * "gf": goto file, edit file under cursor
8273 * "]f" and "[f": can also be used.
8274 */
8275 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008276 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 nv_gotofile(cap);
8278 break;
8279#endif
8280
8281 /* "g'm" and "g`m": jump to mark without setting pcmark */
8282 case '\'':
8283 cap->arg = TRUE;
8284 /*FALLTHROUGH*/
8285 case '`':
8286 nv_gomark(cap);
8287 break;
8288
8289 /*
8290 * "gs": Goto sleep.
8291 */
8292 case 's':
8293 do_sleep(cap->count1 * 1000L);
8294 break;
8295
8296 /*
8297 * "ga": Display the ascii value of the character under the
8298 * cursor. It is displayed in decimal, hex, and octal. -- webb
8299 */
8300 case 'a':
8301 do_ascii(NULL);
8302 break;
8303
8304#ifdef FEAT_MBYTE
8305 /*
8306 * "g8": Display the bytes used for the UTF-8 character under the
8307 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008308 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 */
8310 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008311 if (cap->count0 == 8)
8312 utf_find_illegal();
8313 else
8314 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 break;
8316#endif
8317
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008318 case '<':
8319 show_sb_text();
8320 break;
8321
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 /*
8323 * "gg": Goto the first line in file. With a count it goes to
8324 * that line number like for "G". -- webb
8325 */
8326 case 'g':
8327 cap->arg = FALSE;
8328 nv_goto(cap);
8329 break;
8330
8331 /*
8332 * Two-character operators:
8333 * "gq" Format text
8334 * "gw" Format text and keep cursor position
8335 * "g~" Toggle the case of the text.
8336 * "gu" Change text to lower case.
8337 * "gU" Change text to upper case.
8338 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008339 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 */
8341 case 'q':
8342 case 'w':
8343 oap->cursor_start = curwin->w_cursor;
8344 /*FALLTHROUGH*/
8345 case '~':
8346 case 'u':
8347 case 'U':
8348 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008349 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 nv_operator(cap);
8351 break;
8352
8353 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008354 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 * current function
8356 * "gD": idem, but in the current file.
8357 */
8358 case 'd':
8359 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008360 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 break;
8362
8363#ifdef FEAT_MOUSE
8364 /*
8365 * g<*Mouse> : <C-*mouse>
8366 */
8367 case K_MIDDLEMOUSE:
8368 case K_MIDDLEDRAG:
8369 case K_MIDDLERELEASE:
8370 case K_LEFTMOUSE:
8371 case K_LEFTDRAG:
8372 case K_LEFTRELEASE:
8373 case K_RIGHTMOUSE:
8374 case K_RIGHTDRAG:
8375 case K_RIGHTRELEASE:
8376 case K_X1MOUSE:
8377 case K_X1DRAG:
8378 case K_X1RELEASE:
8379 case K_X2MOUSE:
8380 case K_X2DRAG:
8381 case K_X2RELEASE:
8382 mod_mask = MOD_MASK_CTRL;
8383 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8384 break;
8385#endif
8386
8387 case K_IGNORE:
8388 break;
8389
8390 /*
8391 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8392 */
8393 case 'p':
8394 case 'P':
8395 nv_put(cap);
8396 break;
8397
8398#ifdef FEAT_BYTEOFF
8399 /* "go": goto byte count from start of buffer */
8400 case 'o':
8401 goto_byte(cap->count0);
8402 break;
8403#endif
8404
8405 /* "gQ": improved Ex mode */
8406 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008407 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 {
8409 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008410 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 break;
8412 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008413
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 if (!checkclearopq(oap))
8415 do_exmode(TRUE);
8416 break;
8417
8418#ifdef FEAT_JUMPLIST
8419 case ',':
8420 nv_pcmark(cap);
8421 break;
8422
8423 case ';':
8424 cap->count1 = -cap->count1;
8425 nv_pcmark(cap);
8426 break;
8427#endif
8428
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008429#ifdef FEAT_WINDOWS
8430 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008431 if (!checkclearop(oap))
8432 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008433 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008434 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008435 if (!checkclearop(oap))
8436 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008437 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008438#endif
8439
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008440 case '+':
8441 case '-': /* "g+" and "g-": undo or redo along the timeline */
8442 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008443 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008444 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008445 break;
8446
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 default:
8448 clearopbeep(oap);
8449 break;
8450 }
8451}
8452
8453/*
8454 * Handle "o" and "O" commands.
8455 */
8456 static void
8457n_opencmd(cap)
8458 cmdarg_T *cap;
8459{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008460#ifdef FEAT_CONCEAL
8461 linenr_T oldline = curwin->w_cursor.lnum;
8462#endif
8463
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 if (!checkclearopq(cap->oap))
8465 {
8466#ifdef FEAT_FOLDING
8467 if (cap->cmdchar == 'O')
8468 /* Open above the first line of a folded sequence of lines */
8469 (void)hasFolding(curwin->w_cursor.lnum,
8470 &curwin->w_cursor.lnum, NULL);
8471 else
8472 /* Open below the last line of a folded sequence of lines */
8473 (void)hasFolding(curwin->w_cursor.lnum,
8474 NULL, &curwin->w_cursor.lnum);
8475#endif
8476 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8477 (cap->cmdchar == 'O' ? 1 : 0)),
8478 (linenr_T)(curwin->w_cursor.lnum +
8479 (cap->cmdchar == 'o' ? 1 : 0))
8480 ) == OK
8481 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8482#ifdef FEAT_COMMENTS
8483 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8484#endif
8485 0, 0))
8486 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008487#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008488 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008489 update_single_line(curwin, oldline);
8490#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008491 /* When '#' is in 'cpoptions' ignore the count. */
8492 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8493 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8495 }
8496 }
8497}
8498
8499/*
8500 * "." command: redo last change.
8501 */
8502 static void
8503nv_dot(cap)
8504 cmdarg_T *cap;
8505{
8506 if (!checkclearopq(cap->oap))
8507 {
8508 /*
8509 * If "restart_edit" is TRUE, the last but one command is repeated
8510 * instead of the last command (inserting text). This is used for
8511 * CTRL-O <.> in insert mode.
8512 */
8513 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8514 clearopbeep(cap->oap);
8515 }
8516}
8517
8518/*
8519 * CTRL-R: undo undo
8520 */
8521 static void
8522nv_redo(cap)
8523 cmdarg_T *cap;
8524{
8525 if (!checkclearopq(cap->oap))
8526 {
8527 u_redo((int)cap->count1);
8528 curwin->w_set_curswant = TRUE;
8529 }
8530}
8531
8532/*
8533 * Handle "U" command.
8534 */
8535 static void
8536nv_Undo(cap)
8537 cmdarg_T *cap;
8538{
8539 /* In Visual mode and typing "gUU" triggers an operator */
8540 if (cap->oap->op_type == OP_UPPER
8541#ifdef FEAT_VISUAL
8542 || VIsual_active
8543#endif
8544 )
8545 {
8546 /* translate "gUU" to "gUgU" */
8547 cap->cmdchar = 'g';
8548 cap->nchar = 'U';
8549 nv_operator(cap);
8550 }
8551 else if (!checkclearopq(cap->oap))
8552 {
8553 u_undoline();
8554 curwin->w_set_curswant = TRUE;
8555 }
8556}
8557
8558/*
8559 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8560 * single character.
8561 */
8562 static void
8563nv_tilde(cap)
8564 cmdarg_T *cap;
8565{
8566 if (!p_to
8567#ifdef FEAT_VISUAL
8568 && !VIsual_active
8569#endif
8570 && cap->oap->op_type != OP_TILDE)
8571 n_swapchar(cap);
8572 else
8573 nv_operator(cap);
8574}
8575
8576/*
8577 * Handle an operator command.
8578 * The actual work is done by do_pending_operator().
8579 */
8580 static void
8581nv_operator(cap)
8582 cmdarg_T *cap;
8583{
8584 int op_type;
8585
8586 op_type = get_op_type(cap->cmdchar, cap->nchar);
8587
8588 if (op_type == cap->oap->op_type) /* double operator works on lines */
8589 nv_lineop(cap);
8590 else if (!checkclearop(cap->oap))
8591 {
8592 cap->oap->start = curwin->w_cursor;
8593 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008594#ifdef FEAT_EVAL
8595 set_op_var(op_type);
8596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 }
8598}
8599
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008600#ifdef FEAT_EVAL
8601/*
8602 * Set v:operator to the characters for "optype".
8603 */
8604 static void
8605set_op_var(optype)
8606 int optype;
8607{
8608 char_u opchars[3];
8609
8610 if (optype == OP_NOP)
8611 set_vim_var_string(VV_OP, NULL, 0);
8612 else
8613 {
8614 opchars[0] = get_op_char(optype);
8615 opchars[1] = get_extra_op_char(optype);
8616 opchars[2] = NUL;
8617 set_vim_var_string(VV_OP, opchars, -1);
8618 }
8619}
8620#endif
8621
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622/*
8623 * Handle linewise operator "dd", "yy", etc.
8624 *
8625 * "_" is is a strange motion command that helps make operators more logical.
8626 * It is actually implemented, but not documented in the real Vi. This motion
8627 * command actually refers to "the current line". Commands like "dd" and "yy"
8628 * are really an alternate form of "d_" and "y_". It does accept a count, so
8629 * "d3_" works to delete 3 lines.
8630 */
8631 static void
8632nv_lineop(cap)
8633 cmdarg_T *cap;
8634{
8635 cap->oap->motion_type = MLINE;
8636 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8637 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008638 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8639 && cap->oap->motion_force != 'v'
8640 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 || cap->oap->op_type == OP_LSHIFT
8642 || cap->oap->op_type == OP_RSHIFT)
8643 beginline(BL_SOL | BL_FIX);
8644 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8645 beginline(BL_WHITE | BL_FIX);
8646}
8647
8648/*
8649 * <Home> command.
8650 */
8651 static void
8652nv_home(cap)
8653 cmdarg_T *cap;
8654{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008655 /* CTRL-HOME is like "gg" */
8656 if (mod_mask & MOD_MASK_CTRL)
8657 nv_goto(cap);
8658 else
8659 {
8660 cap->count0 = 1;
8661 nv_pipe(cap);
8662 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008663 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8664 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665}
8666
8667/*
8668 * "|" command.
8669 */
8670 static void
8671nv_pipe(cap)
8672 cmdarg_T *cap;
8673{
8674 cap->oap->motion_type = MCHAR;
8675 cap->oap->inclusive = FALSE;
8676 beginline(0);
8677 if (cap->count0 > 0)
8678 {
8679 coladvance((colnr_T)(cap->count0 - 1));
8680 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8681 }
8682 else
8683 curwin->w_curswant = 0;
8684 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008685 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 curwin->w_set_curswant = FALSE;
8687}
8688
8689/*
8690 * Handle back-word command "b" and "B".
8691 * cap->arg is 1 for "B"
8692 */
8693 static void
8694nv_bck_word(cap)
8695 cmdarg_T *cap;
8696{
8697 cap->oap->motion_type = MCHAR;
8698 cap->oap->inclusive = FALSE;
8699 curwin->w_set_curswant = TRUE;
8700 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8701 clearopbeep(cap->oap);
8702#ifdef FEAT_FOLDING
8703 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8704 foldOpenCursor();
8705#endif
8706}
8707
8708/*
8709 * Handle word motion commands "e", "E", "w" and "W".
8710 * cap->arg is TRUE for "E" and "W".
8711 */
8712 static void
8713nv_wordcmd(cap)
8714 cmdarg_T *cap;
8715{
8716 int n;
8717 int word_end;
8718 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008719 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720
8721 /*
8722 * Set inclusive for the "E" and "e" command.
8723 */
8724 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8725 word_end = TRUE;
8726 else
8727 word_end = FALSE;
8728 cap->oap->inclusive = word_end;
8729
8730 /*
8731 * "cw" and "cW" are a special case.
8732 */
8733 if (!word_end && cap->oap->op_type == OP_CHANGE)
8734 {
8735 n = gchar_cursor();
8736 if (n != NUL) /* not an empty line */
8737 {
8738 if (vim_iswhite(n))
8739 {
8740 /*
8741 * Reproduce a funny Vi behaviour: "cw" on a blank only
8742 * changes one character, not all blanks until the start of
8743 * the next word. Only do this when the 'w' flag is included
8744 * in 'cpoptions'.
8745 */
8746 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8747 {
8748 cap->oap->inclusive = TRUE;
8749 cap->oap->motion_type = MCHAR;
8750 return;
8751 }
8752 }
8753 else
8754 {
8755 /*
8756 * This is a little strange. To match what the real Vi does,
8757 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8758 * that we are not on a space or a TAB. This seems impolite
8759 * at first, but it's really more what we mean when we say
8760 * 'cw'.
8761 * Another strangeness: When standing on the end of a word
8762 * "ce" will change until the end of the next wordt, but "cw"
8763 * will change only one character! This is done by setting
8764 * flag.
8765 */
8766 cap->oap->inclusive = TRUE;
8767 word_end = TRUE;
8768 flag = TRUE;
8769 }
8770 }
8771 }
8772
8773 cap->oap->motion_type = MCHAR;
8774 curwin->w_set_curswant = TRUE;
8775 if (word_end)
8776 n = end_word(cap->count1, cap->arg, flag, FALSE);
8777 else
8778 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8779
Bram Moolenaardfefb982008-04-01 10:06:39 +00008780 /* Don't leave the cursor on the NUL past the end of line. Unless we
8781 * didn't move it forward. */
8782 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008783 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784
8785 if (n == FAIL && cap->oap->op_type == OP_NOP)
8786 clearopbeep(cap->oap);
8787 else
8788 {
8789#ifdef FEAT_VISUAL
8790 adjust_for_sel(cap);
8791#endif
8792#ifdef FEAT_FOLDING
8793 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8794 foldOpenCursor();
8795#endif
8796 }
8797}
8798
8799/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008800 * Used after a movement command: If the cursor ends up on the NUL after the
8801 * end of the line, may move it back to the last character and make the motion
8802 * inclusive.
8803 */
8804 static void
8805adjust_cursor(oap)
8806 oparg_T *oap;
8807{
8808 /* The cursor cannot remain on the NUL when:
8809 * - the column is > 0
8810 * - not in Visual mode or 'selection' is "o"
8811 * - 'virtualedit' is not "all" and not "onemore".
8812 */
8813 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
8814#ifdef FEAT_VISUAL
8815 && (!VIsual_active || *p_sel == 'o')
8816#endif
8817#ifdef FEAT_VIRTUALEDIT
8818 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8819#endif
8820 )
8821 {
8822 --curwin->w_cursor.col;
8823#ifdef FEAT_MBYTE
8824 /* prevent cursor from moving on the trail byte */
8825 if (has_mbyte)
8826 mb_adjust_cursor();
8827#endif
8828 oap->inclusive = TRUE;
8829 }
8830}
8831
8832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 * "0" and "^" commands.
8834 * cap->arg is the argument for beginline().
8835 */
8836 static void
8837nv_beginline(cap)
8838 cmdarg_T *cap;
8839{
8840 cap->oap->motion_type = MCHAR;
8841 cap->oap->inclusive = FALSE;
8842 beginline(cap->arg);
8843#ifdef FEAT_FOLDING
8844 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8845 foldOpenCursor();
8846#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008847 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8848 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849}
8850
8851#ifdef FEAT_VISUAL
8852/*
8853 * In exclusive Visual mode, may include the last character.
8854 */
8855 static void
8856adjust_for_sel(cap)
8857 cmdarg_T *cap;
8858{
8859 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8860 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8861 {
8862# ifdef FEAT_MBYTE
8863 if (has_mbyte)
8864 inc_cursor();
8865 else
8866# endif
8867 ++curwin->w_cursor.col;
8868 cap->oap->inclusive = FALSE;
8869 }
8870}
8871
8872/*
8873 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8874 * Should check VIsual_mode before calling this.
8875 * Returns TRUE when backed up to the previous line.
8876 */
8877 static int
8878unadjust_for_sel()
8879{
8880 pos_T *pp;
8881
8882 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8883 {
8884 if (lt(VIsual, curwin->w_cursor))
8885 pp = &curwin->w_cursor;
8886 else
8887 pp = &VIsual;
8888#ifdef FEAT_VIRTUALEDIT
8889 if (pp->coladd > 0)
8890 --pp->coladd;
8891 else
8892#endif
8893 if (pp->col > 0)
8894 {
8895 --pp->col;
8896#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008897 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898#endif
8899 }
8900 else if (pp->lnum > 1)
8901 {
8902 --pp->lnum;
8903 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8904 return TRUE;
8905 }
8906 }
8907 return FALSE;
8908}
8909
8910/*
8911 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8912 */
8913 static void
8914nv_select(cap)
8915 cmdarg_T *cap;
8916{
8917 if (VIsual_active)
8918 VIsual_select = TRUE;
8919 else if (VIsual_reselect)
8920 {
8921 cap->nchar = 'v'; /* fake "gv" command */
8922 cap->arg = TRUE;
8923 nv_g_cmd(cap);
8924 }
8925}
8926
8927#endif
8928
8929/*
8930 * "G", "gg", CTRL-END, CTRL-HOME.
8931 * cap->arg is TRUE for "G".
8932 */
8933 static void
8934nv_goto(cap)
8935 cmdarg_T *cap;
8936{
8937 linenr_T lnum;
8938
8939 if (cap->arg)
8940 lnum = curbuf->b_ml.ml_line_count;
8941 else
8942 lnum = 1L;
8943 cap->oap->motion_type = MLINE;
8944 setpcmark();
8945
8946 /* When a count is given, use it instead of the default lnum */
8947 if (cap->count0 != 0)
8948 lnum = cap->count0;
8949 if (lnum < 1L)
8950 lnum = 1L;
8951 else if (lnum > curbuf->b_ml.ml_line_count)
8952 lnum = curbuf->b_ml.ml_line_count;
8953 curwin->w_cursor.lnum = lnum;
8954 beginline(BL_SOL | BL_FIX);
8955#ifdef FEAT_FOLDING
8956 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8957 foldOpenCursor();
8958#endif
8959}
8960
8961/*
8962 * CTRL-\ in Normal mode.
8963 */
8964 static void
8965nv_normal(cap)
8966 cmdarg_T *cap;
8967{
8968 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8969 {
8970 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008971 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 clear_cmdline = TRUE; /* unshow mode later */
8973 restart_edit = 0;
8974#ifdef FEAT_CMDWIN
8975 if (cmdwin_type != 0)
8976 cmdwin_result = Ctrl_C;
8977#endif
8978#ifdef FEAT_VISUAL
8979 if (VIsual_active)
8980 {
8981 end_visual_mode(); /* stop Visual */
8982 redraw_curbuf_later(INVERTED);
8983 }
8984#endif
8985 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8986 if (cap->nchar == Ctrl_G && p_im)
8987 restart_edit = 'a';
8988 }
8989 else
8990 clearopbeep(cap->oap);
8991}
8992
8993/*
8994 * ESC in Normal mode: beep, but don't flush buffers.
8995 * Don't even beep if we are canceling a command.
8996 */
8997 static void
8998nv_esc(cap)
8999 cmdarg_T *cap;
9000{
9001 int no_reason;
9002
9003 no_reason = (cap->oap->op_type == OP_NOP
9004 && cap->opcount == 0
9005 && cap->count0 == 0
9006 && cap->oap->regname == 0
9007 && !p_im);
9008
9009 if (cap->arg) /* TRUE for CTRL-C */
9010 {
9011 if (restart_edit == 0
9012#ifdef FEAT_CMDWIN
9013 && cmdwin_type == 0
9014#endif
9015#ifdef FEAT_VISUAL
9016 && !VIsual_active
9017#endif
9018 && no_reason)
9019 MSG(_("Type :quit<Enter> to exit Vim"));
9020
9021 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
9022 * set again below when halfway a mapping. */
9023 if (!p_im)
9024 restart_edit = 0;
9025#ifdef FEAT_CMDWIN
9026 if (cmdwin_type != 0)
9027 {
9028 cmdwin_result = K_IGNORE;
9029 got_int = FALSE; /* don't stop executing autocommands et al. */
9030 return;
9031 }
9032#endif
9033 }
9034
9035#ifdef FEAT_VISUAL
9036 if (VIsual_active)
9037 {
9038 end_visual_mode(); /* stop Visual */
9039 check_cursor_col(); /* make sure cursor is not beyond EOL */
9040 curwin->w_set_curswant = TRUE;
9041 redraw_curbuf_later(INVERTED);
9042 }
9043 else
9044#endif
9045 if (no_reason)
9046 vim_beep();
9047 clearop(cap->oap);
9048
9049 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9050 * set return to Insert mode afterwards. */
9051 if (restart_edit == 0 && goto_im()
9052#ifdef FEAT_EX_EXTRA
9053 && ex_normal_busy == 0
9054#endif
9055 )
9056 restart_edit = 'a';
9057}
9058
9059/*
9060 * Handle "A", "a", "I", "i" and <Insert> commands.
9061 */
9062 static void
9063nv_edit(cap)
9064 cmdarg_T *cap;
9065{
9066 /* <Insert> is equal to "i" */
9067 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9068 cap->cmdchar = 'i';
9069
9070#ifdef FEAT_VISUAL
9071 /* in Visual mode "A" and "I" are an operator */
9072 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
9073 v_visop(cap);
9074
9075 /* in Visual mode and after an operator "a" and "i" are for text objects */
9076 else
9077#endif
9078 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9079 && (cap->oap->op_type != OP_NOP
9080#ifdef FEAT_VISUAL
9081 || VIsual_active
9082#endif
9083 ))
9084 {
9085#ifdef FEAT_TEXTOBJ
9086 nv_object(cap);
9087#else
9088 clearopbeep(cap->oap);
9089#endif
9090 }
9091 else if (!curbuf->b_p_ma && !p_im)
9092 {
9093 /* Only give this error when 'insertmode' is off. */
9094 EMSG(_(e_modifiable));
9095 clearop(cap->oap);
9096 }
9097 else if (!checkclearopq(cap->oap))
9098 {
9099 switch (cap->cmdchar)
9100 {
9101 case 'A': /* "A"ppend after the line */
9102 curwin->w_set_curswant = TRUE;
9103#ifdef FEAT_VIRTUALEDIT
9104 if (ve_flags == VE_ALL)
9105 {
9106 int save_State = State;
9107
9108 /* Pretent Insert mode here to allow the cursor on the
9109 * character past the end of the line */
9110 State = INSERT;
9111 coladvance((colnr_T)MAXCOL);
9112 State = save_State;
9113 }
9114 else
9115#endif
9116 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9117 break;
9118
9119 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009120 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9121 beginline(BL_WHITE);
9122 else
9123 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 break;
9125
9126 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9127#ifdef FEAT_VIRTUALEDIT
9128 /* increment coladd when in virtual space, increment the
9129 * column otherwise, also to append after an unprintable char */
9130 if (virtual_active()
9131 && (curwin->w_cursor.coladd > 0
9132 || *ml_get_cursor() == NUL
9133 || *ml_get_cursor() == TAB))
9134 curwin->w_cursor.coladd++;
9135 else
9136#endif
9137 if (*ml_get_cursor() != NUL)
9138 inc_cursor();
9139 break;
9140 }
9141
9142#ifdef FEAT_VIRTUALEDIT
9143 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9144 {
9145 int save_State = State;
9146
9147 /* Pretent Insert mode here to allow the cursor on the
9148 * character past the end of the line */
9149 State = INSERT;
9150 coladvance(getviscol());
9151 State = save_State;
9152 }
9153#endif
9154
9155 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9156 }
9157}
9158
9159/*
9160 * Invoke edit() and take care of "restart_edit" and the return value.
9161 */
9162 static void
9163invoke_edit(cap, repl, cmd, startln)
9164 cmdarg_T *cap;
9165 int repl; /* "r" or "gr" command */
9166 int cmd;
9167 int startln;
9168{
9169 int restart_edit_save = 0;
9170
9171 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9172 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9173 * it. */
9174 if (repl || !stuff_empty())
9175 restart_edit_save = restart_edit;
9176 else
9177 restart_edit_save = 0;
9178
9179 /* Always reset "restart_edit", this is not a restarted edit. */
9180 restart_edit = 0;
9181
9182 if (edit(cmd, startln, cap->count1))
9183 cap->retval |= CA_COMMAND_BUSY;
9184
9185 if (restart_edit == 0)
9186 restart_edit = restart_edit_save;
9187}
9188
9189#ifdef FEAT_TEXTOBJ
9190/*
9191 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9192 */
9193 static void
9194nv_object(cap)
9195 cmdarg_T *cap;
9196{
9197 int flag;
9198 int include;
9199 char_u *mps_save;
9200
9201 if (cap->cmdchar == 'i')
9202 include = FALSE; /* "ix" = inner object: exclude white space */
9203 else
9204 include = TRUE; /* "ax" = an object: include white space */
9205
9206 /* Make sure (), [], {} and <> are in 'matchpairs' */
9207 mps_save = curbuf->b_p_mps;
9208 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9209
9210 switch (cap->nchar)
9211 {
9212 case 'w': /* "aw" = a word */
9213 flag = current_word(cap->oap, cap->count1, include, FALSE);
9214 break;
9215 case 'W': /* "aW" = a WORD */
9216 flag = current_word(cap->oap, cap->count1, include, TRUE);
9217 break;
9218 case 'b': /* "ab" = a braces block */
9219 case '(':
9220 case ')':
9221 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9222 break;
9223 case 'B': /* "aB" = a Brackets block */
9224 case '{':
9225 case '}':
9226 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9227 break;
9228 case '[': /* "a[" = a [] block */
9229 case ']':
9230 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9231 break;
9232 case '<': /* "a<" = a <> block */
9233 case '>':
9234 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9235 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009236 case 't': /* "at" = a tag block (xml and html) */
9237 flag = current_tagblock(cap->oap, cap->count1, include);
9238 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 case 'p': /* "ap" = a paragraph */
9240 flag = current_par(cap->oap, cap->count1, include, 'p');
9241 break;
9242 case 's': /* "as" = a sentence */
9243 flag = current_sent(cap->oap, cap->count1, include);
9244 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009245 case '"': /* "a"" = a double quoted string */
9246 case '\'': /* "a'" = a single quoted string */
9247 case '`': /* "a`" = a backtick quoted string */
9248 flag = current_quote(cap->oap, cap->count1, include,
9249 cap->nchar);
9250 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251#if 0 /* TODO */
9252 case 'S': /* "aS" = a section */
9253 case 'f': /* "af" = a filename */
9254 case 'u': /* "au" = a URL */
9255#endif
9256 default:
9257 flag = FAIL;
9258 break;
9259 }
9260
9261 curbuf->b_p_mps = mps_save;
9262 if (flag == FAIL)
9263 clearopbeep(cap->oap);
9264 adjust_cursor_col();
9265 curwin->w_set_curswant = TRUE;
9266}
9267#endif
9268
9269/*
9270 * "q" command: Start/stop recording.
9271 * "q:", "q/", "q?": edit command-line in command-line window.
9272 */
9273 static void
9274nv_record(cap)
9275 cmdarg_T *cap;
9276{
9277 if (cap->oap->op_type == OP_FORMAT)
9278 {
9279 /* "gqq" is the same as "gqgq": format line */
9280 cap->cmdchar = 'g';
9281 cap->nchar = 'q';
9282 nv_operator(cap);
9283 }
9284 else if (!checkclearop(cap->oap))
9285 {
9286#ifdef FEAT_CMDWIN
9287 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9288 {
9289 stuffcharReadbuff(cap->nchar);
9290 stuffcharReadbuff(K_CMDWIN);
9291 }
9292 else
9293#endif
9294 /* (stop) recording into a named register, unless executing a
9295 * register */
9296 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9297 clearopbeep(cap->oap);
9298 }
9299}
9300
9301/*
9302 * Handle the "@r" command.
9303 */
9304 static void
9305nv_at(cap)
9306 cmdarg_T *cap;
9307{
9308 if (checkclearop(cap->oap))
9309 return;
9310#ifdef FEAT_EVAL
9311 if (cap->nchar == '=')
9312 {
9313 if (get_expr_register() == NUL)
9314 return;
9315 }
9316#endif
9317 while (cap->count1-- && !got_int)
9318 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009319 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 {
9321 clearopbeep(cap->oap);
9322 break;
9323 }
9324 line_breakcheck();
9325 }
9326}
9327
9328/*
9329 * Handle the CTRL-U and CTRL-D commands.
9330 */
9331 static void
9332nv_halfpage(cap)
9333 cmdarg_T *cap;
9334{
9335 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9336 || (cap->cmdchar == Ctrl_D
9337 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9338 clearopbeep(cap->oap);
9339 else if (!checkclearop(cap->oap))
9340 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9341}
9342
9343/*
9344 * Handle "J" or "gJ" command.
9345 */
9346 static void
9347nv_join(cap)
9348 cmdarg_T *cap;
9349{
9350#ifdef FEAT_VISUAL
9351 if (VIsual_active) /* join the visual lines */
9352 nv_operator(cap);
9353 else
9354#endif
9355 if (!checkclearop(cap->oap))
9356 {
9357 if (cap->count0 <= 1)
9358 cap->count0 = 2; /* default for join is two lines! */
9359 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9360 curbuf->b_ml.ml_line_count)
9361 clearopbeep(cap->oap); /* beyond last line */
9362 else
9363 {
9364 prep_redo(cap->oap->regname, cap->count0,
9365 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
Bram Moolenaar81340392012-06-06 16:12:59 +02009366 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 }
9368 }
9369}
9370
9371/*
9372 * "P", "gP", "p" and "gp" commands.
9373 */
9374 static void
9375nv_put(cap)
9376 cmdarg_T *cap;
9377{
9378#ifdef FEAT_VISUAL
9379 int regname = 0;
9380 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009381 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009382 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383#endif
9384 int dir;
9385 int flags = 0;
9386
9387 if (cap->oap->op_type != OP_NOP)
9388 {
9389#ifdef FEAT_DIFF
9390 /* "dp" is ":diffput" */
9391 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9392 {
9393 clearop(cap->oap);
9394 nv_diffgetput(TRUE);
9395 }
9396 else
9397#endif
9398 clearopbeep(cap->oap);
9399 }
9400 else
9401 {
9402 dir = (cap->cmdchar == 'P'
9403 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9404 ? BACKWARD : FORWARD;
9405 prep_redo_cmd(cap);
9406 if (cap->cmdchar == 'g')
9407 flags |= PUT_CURSEND;
9408
9409#ifdef FEAT_VISUAL
9410 if (VIsual_active)
9411 {
9412 /* Putting in Visual mode: The put text replaces the selected
9413 * text. First delete the selected text, then put the new text.
9414 * Need to save and restore the registers that the delete
9415 * overwrites if the old contents is being put.
9416 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009417 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 regname = cap->oap->regname;
9419# ifdef FEAT_CLIPBOARD
9420 adjust_clip_reg(&regname);
9421# endif
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009422 if (regname == 0 || regname == '"'
9423 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424# ifdef FEAT_CLIPBOARD
9425 || (clip_unnamed && (regname == '*' || regname == '+'))
9426# endif
9427
9428 )
9429 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009430 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 * put, save it first. */
9432 reg1 = get_register(regname, TRUE);
9433 }
9434
9435 /* Now delete the selected text. */
9436 cap->cmdchar = 'd';
9437 cap->nchar = NUL;
9438 cap->oap->regname = NUL;
9439 nv_operator(cap);
9440 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009441 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442
9443 /* delete PUT_LINE_BACKWARD; */
9444 cap->oap->regname = regname;
9445
9446 if (reg1 != NULL)
9447 {
9448 /* Delete probably changed the register we want to put, save
9449 * it first. Then put back what was there before the delete. */
9450 reg2 = get_register(regname, FALSE);
9451 put_register(regname, reg1);
9452 }
9453
9454 /* When deleted a linewise Visual area, put the register as
9455 * lines to avoid it joined with the next line. When deletion was
9456 * characterwise, split a line when putting lines. */
9457 if (VIsual_mode == 'V')
9458 flags |= PUT_LINE;
9459 else if (VIsual_mode == 'v')
9460 flags |= PUT_LINE_SPLIT;
9461 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9462 flags |= PUT_LINE_FORWARD;
9463 dir = BACKWARD;
9464 if ((VIsual_mode != 'V'
9465 && curwin->w_cursor.col < curbuf->b_op_start.col)
9466 || (VIsual_mode == 'V'
9467 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9468 /* cursor is at the end of the line or end of file, put
9469 * forward. */
9470 dir = FORWARD;
9471 }
9472#endif
9473 do_put(cap->oap->regname, dir, cap->count1, flags);
9474
9475#ifdef FEAT_VISUAL
9476 /* If a register was saved, put it back now. */
9477 if (reg2 != NULL)
9478 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009479
9480 /* What to reselect with "gv"? Selecting the just put text seems to
9481 * be the most useful, since the original text was removed. */
9482 if (was_visual)
9483 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009484 curbuf->b_visual.vi_start = curbuf->b_op_start;
9485 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009486 }
9487
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009488 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009489 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009490 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009491 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009492 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009493
9494 /* If the cursor was in that line, move it to the end of the last
9495 * line. */
9496 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9497 {
9498 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9499 coladvance((colnr_T)MAXCOL);
9500 }
9501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502#endif
9503 auto_format(FALSE, TRUE);
9504 }
9505}
9506
9507/*
9508 * "o" and "O" commands.
9509 */
9510 static void
9511nv_open(cap)
9512 cmdarg_T *cap;
9513{
9514#ifdef FEAT_DIFF
9515 /* "do" is ":diffget" */
9516 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9517 {
9518 clearop(cap->oap);
9519 nv_diffgetput(FALSE);
9520 }
9521 else
9522#endif
9523#ifdef FEAT_VISUAL
9524 if (VIsual_active) /* switch start and end of visual */
9525 v_swap_corners(cap->cmdchar);
9526 else
9527#endif
9528 n_opencmd(cap);
9529}
9530
9531#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 static void
9533nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009534 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535{
9536 ProcessSniffRequests();
9537}
9538#endif
9539
9540#ifdef FEAT_NETBEANS_INTG
9541 static void
9542nv_nbcmd(cap)
9543 cmdarg_T *cap;
9544{
9545 netbeans_keycommand(cap->nchar);
9546}
9547#endif
9548
9549#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 static void
9551nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009552 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553{
9554 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9555}
9556#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009557
9558#ifdef FEAT_AUTOCMD
9559/*
9560 * Trigger CursorHold event.
9561 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9562 * input buffer. "did_cursorhold" is set to avoid retriggering.
9563 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009564 static void
9565nv_cursorhold(cap)
9566 cmdarg_T *cap;
9567{
9568 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9569 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009570 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009571}
9572#endif