blob: 6759e8e728ed8c36a0a4f56266566591e8c6714c [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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
Bram Moolenaar071d4272004-06-13 20:20:40 +000017/*
18 * The Visual area is remembered for reselection.
19 */
20static int resel_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
21static linenr_T resel_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +020022static colnr_T resel_VIsual_vcol; /* nr of cols or end col */
Bram Moolenaar7d311c52014-02-22 23:49:35 +010023static int VIsual_mode_orig = NUL; /* saved Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25static int restart_VIsual_select = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010027#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010028static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount);
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000030static int
Bram Moolenaar0c50a6b2012-05-25 11:04:38 +020031#ifdef __BORLANDC__
32 _RTLENTRYF
33#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034 nv_compare(const void *s1, const void *s2);
35static int find_command(int cmdchar);
36static void op_colon(oparg_T *oap);
37static void op_function(oparg_T *oap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010038#if defined(FEAT_MOUSE)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010039static void find_start_of_word(pos_T *);
40static void find_end_of_word(pos_T *);
41static int get_mouse_class(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000042#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010043static void prep_redo_cmd(cmdarg_T *cap);
44static void prep_redo(int regname, long, int, int, int, int, int);
45static int checkclearop(oparg_T *oap);
46static int checkclearopq(oparg_T *oap);
47static void clearop(oparg_T *oap);
48static void clearopbeep(oparg_T *oap);
49static void unshift_special(cmdarg_T *cap);
50static void may_clear_cmdline(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010052static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000053#endif
54
55/*
56 * nv_*(): functions called to handle Normal and Visual mode commands.
57 * n_*(): functions called to handle Normal mode commands.
58 * v_*(): functions called to handle Visual mode commands.
59 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010060static void nv_ignore(cmdarg_T *cap);
61static void nv_nop(cmdarg_T *cap);
62static void nv_error(cmdarg_T *cap);
63static void nv_help(cmdarg_T *cap);
64static void nv_addsub(cmdarg_T *cap);
65static void nv_page(cmdarg_T *cap);
66static void nv_gd(oparg_T *oap, int nchar, int thisblock);
67static int nv_screengo(oparg_T *oap, int dir, long dist);
Bram Moolenaar071d4272004-06-13 20:20:40 +000068#ifdef FEAT_MOUSE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010069static void nv_mousescroll(cmdarg_T *cap);
70static void nv_mouse(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010072static void nv_scroll_line(cmdarg_T *cap);
73static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000074#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010075static void nv_ver_scrollbar(cmdarg_T *cap);
76static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000077#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000078#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010079static void nv_tabline(cmdarg_T *cap);
80static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000081#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010082static void nv_exmode(cmdarg_T *cap);
83static void nv_colon(cmdarg_T *cap);
84static void nv_ctrlg(cmdarg_T *cap);
85static void nv_ctrlh(cmdarg_T *cap);
86static void nv_clear(cmdarg_T *cap);
87static void nv_ctrlo(cmdarg_T *cap);
88static void nv_hat(cmdarg_T *cap);
89static void nv_Zet(cmdarg_T *cap);
90static void nv_ident(cmdarg_T *cap);
91static void nv_tagpop(cmdarg_T *cap);
92static void nv_scroll(cmdarg_T *cap);
93static void nv_right(cmdarg_T *cap);
94static void nv_left(cmdarg_T *cap);
95static void nv_up(cmdarg_T *cap);
96static void nv_down(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#ifdef FEAT_SEARCHPATH
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010098static void nv_gotofile(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000099#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100100static void nv_end(cmdarg_T *cap);
101static void nv_dollar(cmdarg_T *cap);
102static void nv_search(cmdarg_T *cap);
103static void nv_next(cmdarg_T *cap);
104static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt);
105static void nv_csearch(cmdarg_T *cap);
106static void nv_brackets(cmdarg_T *cap);
107static void nv_percent(cmdarg_T *cap);
108static void nv_brace(cmdarg_T *cap);
109static void nv_mark(cmdarg_T *cap);
110static void nv_findpar(cmdarg_T *cap);
111static void nv_undo(cmdarg_T *cap);
112static void nv_kundo(cmdarg_T *cap);
113static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#ifdef FEAT_VREPLACE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100115static void nv_vreplace(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100117static void v_swap_corners(int cmdchar);
118static void nv_replace(cmdarg_T *cap);
119static void n_swapchar(cmdarg_T *cap);
120static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
121static void v_visop(cmdarg_T *cap);
122static void nv_subst(cmdarg_T *cap);
123static void nv_abbrev(cmdarg_T *cap);
124static void nv_optrans(cmdarg_T *cap);
125static void nv_gomark(cmdarg_T *cap);
126static void nv_pcmark(cmdarg_T *cap);
127static void nv_regname(cmdarg_T *cap);
128static void nv_visual(cmdarg_T *cap);
129static void n_start_visual_mode(int c);
130static void nv_window(cmdarg_T *cap);
131static void nv_suspend(cmdarg_T *cap);
132static void nv_g_cmd(cmdarg_T *cap);
133static void n_opencmd(cmdarg_T *cap);
134static void nv_dot(cmdarg_T *cap);
135static void nv_redo(cmdarg_T *cap);
136static void nv_Undo(cmdarg_T *cap);
137static void nv_tilde(cmdarg_T *cap);
138static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000139#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100140static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000141#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100142static void nv_lineop(cmdarg_T *cap);
143static void nv_home(cmdarg_T *cap);
144static void nv_pipe(cmdarg_T *cap);
145static void nv_bck_word(cmdarg_T *cap);
146static void nv_wordcmd(cmdarg_T *cap);
147static void nv_beginline(cmdarg_T *cap);
148static void adjust_cursor(oparg_T *oap);
149static void adjust_for_sel(cmdarg_T *cap);
150static int unadjust_for_sel(void);
151static void nv_select(cmdarg_T *cap);
152static void nv_goto(cmdarg_T *cap);
153static void nv_normal(cmdarg_T *cap);
154static void nv_esc(cmdarg_T *oap);
155static void nv_edit(cmdarg_T *cap);
156static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100158static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100160static void nv_record(cmdarg_T *cap);
161static void nv_at(cmdarg_T *cap);
162static void nv_halfpage(cmdarg_T *cap);
163static void nv_join(cmdarg_T *cap);
164static void nv_put(cmdarg_T *cap);
165static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100167static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#endif
169#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100170static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000172#ifdef FEAT_AUTOCMD
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100173static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar3918c952005-03-15 22:34:55 +0000174#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100175static void get_op_vcol(oparg_T *oap, colnr_T col, int initial);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000177static char *e_noident = N_("E349: No identifier under cursor");
178
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179/*
180 * Function to be called for a Normal or Visual mode command.
181 * The argument is a cmdarg_T.
182 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100183typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184
185/* Values for cmd_flags. */
186#define NV_NCH 0x01 /* may need to get a second char */
187#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
188#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
189#define NV_LANG 0x08 /* second char needs language adjustment */
190
191#define NV_SS 0x10 /* may start selection */
192#define NV_SSS 0x20 /* may start selection with shift modifier */
193#define NV_STS 0x40 /* may stop selection without shift modif. */
194#define NV_RL 0x80 /* 'rightleft' modifies command */
195#define NV_KEEPREG 0x100 /* don't clear regname */
196#define NV_NCW 0x200 /* not allowed in command-line window */
197
198/*
199 * Generally speaking, every Normal mode command should either clear any
200 * pending operator (with *clearop*()), or set the motion type variable
201 * oap->motion_type.
202 *
203 * When a cursor motion command is made, it is marked as being a character or
204 * line oriented motion. Then, if an operator is in effect, the operation
205 * becomes character or line oriented accordingly.
206 */
207
208/*
209 * This table contains one entry for every Normal or Visual mode command.
210 * The order doesn't matter, init_normal_cmds() will create a sorted index.
211 * It is faster when all keys from zero to '~' are present.
212 */
213static const struct nv_cmd
214{
215 int cmd_char; /* (first) command character */
216 nv_func_T cmd_func; /* function for this command */
217 short_u cmd_flags; /* NV_ flags */
218 short cmd_arg; /* value for ca.arg */
219} nv_cmds[] =
220{
221 {NUL, nv_error, 0, 0},
222 {Ctrl_A, nv_addsub, 0, 0},
223 {Ctrl_B, nv_page, NV_STS, BACKWARD},
224 {Ctrl_C, nv_esc, 0, TRUE},
225 {Ctrl_D, nv_halfpage, 0, 0},
226 {Ctrl_E, nv_scroll_line, 0, TRUE},
227 {Ctrl_F, nv_page, NV_STS, FORWARD},
228 {Ctrl_G, nv_ctrlg, 0, 0},
229 {Ctrl_H, nv_ctrlh, 0, 0},
230 {Ctrl_I, nv_pcmark, 0, 0},
231 {NL, nv_down, 0, FALSE},
232 {Ctrl_K, nv_error, 0, 0},
233 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000234 {Ctrl_M, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 {Ctrl_N, nv_down, NV_STS, FALSE},
236 {Ctrl_O, nv_ctrlo, 0, 0},
237 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000238 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 {Ctrl_R, nv_redo, 0, 0},
240 {Ctrl_S, nv_ignore, 0, 0},
241 {Ctrl_T, nv_tagpop, NV_NCW, 0},
242 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 {Ctrl_V, nv_visual, 0, FALSE},
244 {'V', nv_visual, 0, FALSE},
245 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 {Ctrl_W, nv_window, 0, 0},
247 {Ctrl_X, nv_addsub, 0, 0},
248 {Ctrl_Y, nv_scroll_line, 0, FALSE},
249 {Ctrl_Z, nv_suspend, 0, 0},
250 {ESC, nv_esc, 0, FALSE},
251 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
252 {Ctrl_RSB, nv_ident, NV_NCW, 0},
253 {Ctrl_HAT, nv_hat, NV_NCW, 0},
254 {Ctrl__, nv_error, 0, 0},
255 {' ', nv_right, 0, 0},
256 {'!', nv_operator, 0, 0},
257 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
258 {'#', nv_ident, 0, 0},
259 {'$', nv_dollar, 0, 0},
260 {'%', nv_percent, 0, 0},
261 {'&', nv_optrans, 0, 0},
262 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
263 {'(', nv_brace, 0, BACKWARD},
264 {')', nv_brace, 0, FORWARD},
265 {'*', nv_ident, 0, 0},
266 {'+', nv_down, 0, TRUE},
267 {',', nv_csearch, 0, TRUE},
268 {'-', nv_up, 0, TRUE},
269 {'.', nv_dot, NV_KEEPREG, 0},
270 {'/', nv_search, 0, FALSE},
271 {'0', nv_beginline, 0, 0},
272 {'1', nv_ignore, 0, 0},
273 {'2', nv_ignore, 0, 0},
274 {'3', nv_ignore, 0, 0},
275 {'4', nv_ignore, 0, 0},
276 {'5', nv_ignore, 0, 0},
277 {'6', nv_ignore, 0, 0},
278 {'7', nv_ignore, 0, 0},
279 {'8', nv_ignore, 0, 0},
280 {'9', nv_ignore, 0, 0},
281 {':', nv_colon, 0, 0},
282 {';', nv_csearch, 0, FALSE},
283 {'<', nv_operator, NV_RL, 0},
284 {'=', nv_operator, 0, 0},
285 {'>', nv_operator, NV_RL, 0},
286 {'?', nv_search, 0, FALSE},
287 {'@', nv_at, NV_NCH_NOP, FALSE},
288 {'A', nv_edit, 0, 0},
289 {'B', nv_bck_word, 0, 1},
290 {'C', nv_abbrev, NV_KEEPREG, 0},
291 {'D', nv_abbrev, NV_KEEPREG, 0},
292 {'E', nv_wordcmd, 0, TRUE},
293 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
294 {'G', nv_goto, 0, TRUE},
295 {'H', nv_scroll, 0, 0},
296 {'I', nv_edit, 0, 0},
297 {'J', nv_join, 0, 0},
298 {'K', nv_ident, 0, 0},
299 {'L', nv_scroll, 0, 0},
300 {'M', nv_scroll, 0, 0},
301 {'N', nv_next, 0, SEARCH_REV},
302 {'O', nv_open, 0, 0},
303 {'P', nv_put, 0, 0},
304 {'Q', nv_exmode, NV_NCW, 0},
305 {'R', nv_Replace, 0, FALSE},
306 {'S', nv_subst, NV_KEEPREG, 0},
307 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
308 {'U', nv_Undo, 0, 0},
309 {'W', nv_wordcmd, 0, TRUE},
310 {'X', nv_abbrev, NV_KEEPREG, 0},
311 {'Y', nv_abbrev, NV_KEEPREG, 0},
312 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
313 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
314 {'\\', nv_error, 0, 0},
315 {']', nv_brackets, NV_NCH_ALW, FORWARD},
316 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
317 {'_', nv_lineop, 0, 0},
318 {'`', nv_gomark, NV_NCH_ALW, FALSE},
319 {'a', nv_edit, NV_NCH, 0},
320 {'b', nv_bck_word, 0, 0},
321 {'c', nv_operator, 0, 0},
322 {'d', nv_operator, 0, 0},
323 {'e', nv_wordcmd, 0, FALSE},
324 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
325 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
326 {'h', nv_left, NV_RL, 0},
327 {'i', nv_edit, NV_NCH, 0},
328 {'j', nv_down, 0, FALSE},
329 {'k', nv_up, 0, FALSE},
330 {'l', nv_right, NV_RL, 0},
331 {'m', nv_mark, NV_NCH_NOP, 0},
332 {'n', nv_next, 0, 0},
333 {'o', nv_open, 0, 0},
334 {'p', nv_put, 0, 0},
335 {'q', nv_record, NV_NCH, 0},
336 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
337 {'s', nv_subst, NV_KEEPREG, 0},
338 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
339 {'u', nv_undo, 0, 0},
340 {'w', nv_wordcmd, 0, FALSE},
341 {'x', nv_abbrev, NV_KEEPREG, 0},
342 {'y', nv_operator, 0, 0},
343 {'z', nv_zet, NV_NCH_ALW, 0},
344 {'{', nv_findpar, 0, BACKWARD},
345 {'|', nv_pipe, 0, 0},
346 {'}', nv_findpar, 0, FORWARD},
347 {'~', nv_tilde, 0, 0},
348
349 /* pound sign */
350 {POUND, nv_ident, 0, 0},
351#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200352 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
353 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
354 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
355 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 {K_LEFTMOUSE, nv_mouse, 0, 0},
357 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
358 {K_LEFTDRAG, nv_mouse, 0, 0},
359 {K_LEFTRELEASE, nv_mouse, 0, 0},
360 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
361 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
362 {K_MIDDLEDRAG, nv_mouse, 0, 0},
363 {K_MIDDLERELEASE, nv_mouse, 0, 0},
364 {K_RIGHTMOUSE, nv_mouse, 0, 0},
365 {K_RIGHTDRAG, nv_mouse, 0, 0},
366 {K_RIGHTRELEASE, nv_mouse, 0, 0},
367 {K_X1MOUSE, nv_mouse, 0, 0},
368 {K_X1DRAG, nv_mouse, 0, 0},
369 {K_X1RELEASE, nv_mouse, 0, 0},
370 {K_X2MOUSE, nv_mouse, 0, 0},
371 {K_X2DRAG, nv_mouse, 0, 0},
372 {K_X2RELEASE, nv_mouse, 0, 0},
373#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000374 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000375 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 {K_INS, nv_edit, 0, 0},
377 {K_KINS, nv_edit, 0, 0},
378 {K_BS, nv_ctrlh, 0, 0},
379 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
380 {K_S_UP, nv_page, NV_SS, BACKWARD},
381 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
382 {K_S_DOWN, nv_page, NV_SS, FORWARD},
383 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
384 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
385 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
386 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
387 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
388 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
389 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
390 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
391 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
392 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
393 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
394 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 {K_S_END, nv_end, NV_SS, FALSE},
396 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
397 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
398 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 {K_S_HOME, nv_home, NV_SS, 0},
400 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
401 {K_DEL, nv_abbrev, 0, 0},
402 {K_KDEL, nv_abbrev, 0, 0},
403 {K_UNDO, nv_kundo, 0, 0},
404 {K_HELP, nv_help, NV_NCW, 0},
405 {K_F1, nv_help, NV_NCW, 0},
406 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408#ifdef FEAT_GUI
409 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
410 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
411#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000412#ifdef FEAT_GUI_TABLINE
413 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000414 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416#ifdef FEAT_FKMAP
Bram Moolenaaree2615a2016-07-02 18:25:34 +0200417 {K_F8, farsi_f8, 0, 0},
418 {K_F9, farsi_f9, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420#ifdef FEAT_NETBEANS_INTG
421 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
422#endif
423#ifdef FEAT_DND
424 {K_DROP, nv_drop, NV_STS, 0},
425#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000426#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000427 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaar3918c952005-03-15 22:34:55 +0000428#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100429 {K_PS, nv_edit, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430};
431
432/* Number of commands in nv_cmds[]. */
433#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
434
435/* Sorted index of commands in nv_cmds[]. */
436static short nv_cmd_idx[NV_CMDS_SIZE];
437
438/* The highest index for which
439 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
440static int nv_max_linear;
441
442/*
443 * Compare functions for qsort() below, that checks the command character
444 * through the index in nv_cmd_idx[].
445 */
446 static int
447#ifdef __BORLANDC__
448_RTLENTRYF
449#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +0100450nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451{
452 int c1, c2;
453
454 /* The commands are sorted on absolute value. */
455 c1 = nv_cmds[*(const short *)s1].cmd_char;
456 c2 = nv_cmds[*(const short *)s2].cmd_char;
457 if (c1 < 0)
458 c1 = -c1;
459 if (c2 < 0)
460 c2 = -c2;
461 return c1 - c2;
462}
463
464/*
465 * Initialize the nv_cmd_idx[] table.
466 */
467 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100468init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469{
470 int i;
471
472 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000473 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 nv_cmd_idx[i] = i;
475
476 /* Sort the commands by the command character. */
477 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
478
479 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000480 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
482 break;
483 nv_max_linear = i - 1;
484}
485
486/*
487 * Search for a command in the commands table.
488 * Returns -1 for invalid command.
489 */
490 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100491find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492{
493 int i;
494 int idx;
495 int top, bot;
496 int c;
497
498#ifdef FEAT_MBYTE
499 /* A multi-byte character is never a command. */
500 if (cmdchar >= 0x100)
501 return -1;
502#endif
503
504 /* We use the absolute value of the character. Special keys have a
505 * negative value, but are sorted on their absolute value. */
506 if (cmdchar < 0)
507 cmdchar = -cmdchar;
508
509 /* If the character is in the first part: The character is the index into
510 * nv_cmd_idx[]. */
511 if (cmdchar <= nv_max_linear)
512 return nv_cmd_idx[cmdchar];
513
514 /* Perform a binary search. */
515 bot = nv_max_linear + 1;
516 top = NV_CMDS_SIZE - 1;
517 idx = -1;
518 while (bot <= top)
519 {
520 i = (top + bot) / 2;
521 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
522 if (c < 0)
523 c = -c;
524 if (cmdchar == c)
525 {
526 idx = nv_cmd_idx[i];
527 break;
528 }
529 if (cmdchar > c)
530 bot = i + 1;
531 else
532 top = i - 1;
533 }
534 return idx;
535}
536
537/*
538 * Execute a command in Normal mode.
539 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100541normal_cmd(
542 oparg_T *oap,
543 int toplevel UNUSED) /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 cmdarg_T ca; /* command arguments */
546 int c;
547 int ctrl_w = FALSE; /* got CTRL-W command */
548 int old_col = curwin->w_curswant;
549#ifdef FEAT_CMDL_INFO
550 int need_flushbuf; /* need to call out_flush() */
551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 pos_T old_pos; /* cursor position before command */
553 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 static int old_mapped_len = 0;
555 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000556#ifdef FEAT_EVAL
557 int set_prevcount = FALSE;
558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559
560 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
561 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000562
563 /* Use a count remembered from before entering an operator. After typing
564 * "3d" we return from normal_cmd() and come back here, the "3" is
565 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 ca.opcount = opcount;
567
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 /*
569 * If there is an operator pending, then the command we take this time
570 * will terminate it. Finish_op tells us to finish the operation before
571 * returning this time (unless the operation was cancelled).
572 */
573#ifdef CURSOR_SHAPE
574 c = finish_op;
575#endif
576 finish_op = (oap->op_type != OP_NOP);
577#ifdef CURSOR_SHAPE
578 if (finish_op != c)
579 {
580 ui_cursor_shape(); /* may show different cursor shape */
581# ifdef FEAT_MOUSESHAPE
582 update_mouseshape(-1);
583# endif
584 }
585#endif
586
Bram Moolenaara983fe92008-07-31 20:04:27 +0000587 /* When not finishing an operator and no register name typed, reset the
588 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000592#ifdef FEAT_EVAL
593 set_prevcount = TRUE;
594#endif
595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596
Bram Moolenaara983fe92008-07-31 20:04:27 +0000597#ifdef FEAT_AUTOCMD
598 /* Restore counts from before receiving K_CURSORHOLD. This means after
599 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
600 * "3 * 2". */
601 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
602 {
603 ca.opcount = oap->prev_opcount;
604 ca.count0 = oap->prev_count0;
605 oap->prev_opcount = 0;
606 oap->prev_count0 = 0;
607 }
608#endif
609
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611
612 State = NORMAL_BUSY;
613#ifdef USE_ON_FLY_SCROLL
614 dont_scroll = FALSE; /* allow scrolling here */
615#endif
616
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100617#ifdef FEAT_EVAL
618 /* Set v:count here, when called from main() and not a stuffed
619 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100620 * when there is no count. Do set it for redo. */
621 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100622 set_vcount_ca(&ca, &set_prevcount);
623#endif
624
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 /*
626 * Get the command character from the user.
627 */
628 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100629 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630
631 /*
632 * If a mapping was started in Visual or Select mode, remember the length
633 * of the mapping. This is used below to not return to Insert mode for as
634 * long as the mapping is being executed.
635 */
636 if (restart_edit == 0)
637 old_mapped_len = 0;
638 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000639 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 old_mapped_len = typebuf_maplen();
641
642 if (c == NUL)
643 c = K_ZERO;
644
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 /*
646 * In Select mode, typed text replaces the selection.
647 */
648 if (VIsual_active
649 && VIsual_select
650 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
651 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000652 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
653 * 'insertmode' is set) fake a "d"elete command, Insert mode will
654 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000655 * Insert the typed character in the typeahead buffer, so that it can
656 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000657 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000658 if (restart_edit != 0)
659 c = 'd';
660 else
661 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000662 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar9bad29d2013-05-21 12:46:02 +0200663 old_mapped_len = 0; /* do go to Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665
666#ifdef FEAT_CMDL_INFO
667 need_flushbuf = add_to_showcmd(c);
668#endif
669
670getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 {
673 /*
674 * Handle a count before a command and compute ca.count0.
675 * Note that '0' is a command and not the start of a count, but it's
676 * part of a count after other digits.
677 */
678 while ( (c >= '1' && c <= '9')
679 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
680 {
681 if (c == K_DEL || c == K_KDEL)
682 {
683 ca.count0 /= 10;
684#ifdef FEAT_CMDL_INFO
685 del_from_showcmd(4); /* delete the digit and ~@% */
686#endif
687 }
688 else
689 ca.count0 = ca.count0 * 10 + (c - '0');
690 if (ca.count0 < 0) /* got too large! */
691 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000692#ifdef FEAT_EVAL
693 /* Set v:count here, when called from main() and not a stuffed
694 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100695 * right after the count. Do set it for redo. */
696 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100697 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 if (ctrl_w)
700 {
701 ++no_mapping;
702 ++allow_keys; /* no mapping for nchar, but keys */
703 }
704 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000705 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 --no_zero_mapping;
708 if (ctrl_w)
709 {
710 --no_mapping;
711 --allow_keys;
712 }
713#ifdef FEAT_CMDL_INFO
714 need_flushbuf |= add_to_showcmd(c);
715#endif
716 }
717
718 /*
719 * If we got CTRL-W there may be a/another count
720 */
721 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
722 {
723 ctrl_w = TRUE;
724 ca.opcount = ca.count0; /* remember first count */
725 ca.count0 = 0;
726 ++no_mapping;
727 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000728 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 --no_mapping;
731 --allow_keys;
732#ifdef FEAT_CMDL_INFO
733 need_flushbuf |= add_to_showcmd(c);
734#endif
735 goto getcount; /* jump back */
736 }
737 }
738
Bram Moolenaara983fe92008-07-31 20:04:27 +0000739#ifdef FEAT_AUTOCMD
740 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000742 /* Save the count values so that ca.opcount and ca.count0 are exactly
743 * the same when coming back here after handling K_CURSORHOLD. */
744 oap->prev_opcount = ca.opcount;
745 oap->prev_count0 = ca.count0;
746 }
747 else
748#endif
749 if (ca.opcount != 0)
750 {
751 /*
752 * If we're in the middle of an operator (including after entering a
753 * yank buffer with '"') AND we had a count before the operator, then
754 * that count overrides the current value of ca.count0.
755 * What this means effectively, is that commands like "3dw" get turned
756 * into "d3w" which makes things fall into place pretty neatly.
757 * If you give a count before AND after the operator, they are
758 * multiplied.
759 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 if (ca.count0)
761 ca.count0 *= ca.opcount;
762 else
763 ca.count0 = ca.opcount;
764 }
765
766 /*
767 * Always remember the count. It will be set to zero (on the next call,
768 * above) when there is no pending operator.
769 * When called from main(), save the count for use by the "count" built-in
770 * variable.
771 */
772 ca.opcount = ca.count0;
773 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
774
775#ifdef FEAT_EVAL
776 /*
777 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100778 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100780 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000781 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782#endif
783
784 /*
785 * Find the command character in the table of commands.
786 * For CTRL-W we already got nchar when looking for a count.
787 */
788 if (ctrl_w)
789 {
790 ca.nchar = c;
791 ca.cmdchar = Ctrl_W;
792 }
793 else
794 ca.cmdchar = c;
795 idx = find_command(ca.cmdchar);
796 if (idx < 0)
797 {
798 /* Not a known command: beep. */
799 clearopbeep(oap);
800 goto normal_end;
801 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000802
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000803 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 {
Bram Moolenaard69bd9a2014-04-29 12:15:40 +0200805 /* This command is not allowed while editing a cmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000807 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 goto normal_end;
809 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000810#ifdef FEAT_AUTOCMD
811 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
812 goto normal_end;
813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 /*
816 * In Visual/Select mode, a few keys are handled in a special way.
817 */
818 if (VIsual_active)
819 {
820 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
821 if (km_stopsel
822 && (nv_cmds[idx].cmd_flags & NV_STS)
823 && !(mod_mask & MOD_MASK_SHIFT))
824 {
825 end_visual_mode();
826 redraw_curbuf_later(INVERTED);
827 }
828
829 /* Keys that work different when 'keymodel' contains "startsel" */
830 if (km_startsel)
831 {
832 if (nv_cmds[idx].cmd_flags & NV_SS)
833 {
834 unshift_special(&ca);
835 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000836 if (idx < 0)
837 {
838 /* Just in case */
839 clearopbeep(oap);
840 goto normal_end;
841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 }
843 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
844 && (mod_mask & MOD_MASK_SHIFT))
845 {
846 mod_mask &= ~MOD_MASK_SHIFT;
847 }
848 }
849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850
851#ifdef FEAT_RIGHTLEFT
852 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
853 && (nv_cmds[idx].cmd_flags & NV_RL))
854 {
855 /* Invert horizontal movements and operations. Only when typed by the
856 * user directly, not when the result of a mapping or "x" translated
857 * to "dl". */
858 switch (ca.cmdchar)
859 {
860 case 'l': ca.cmdchar = 'h'; break;
861 case K_RIGHT: ca.cmdchar = K_LEFT; break;
862 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
863 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
864 case 'h': ca.cmdchar = 'l'; break;
865 case K_LEFT: ca.cmdchar = K_RIGHT; break;
866 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
867 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
868 case '>': ca.cmdchar = '<'; break;
869 case '<': ca.cmdchar = '>'; break;
870 }
871 idx = find_command(ca.cmdchar);
872 }
873#endif
874
875 /*
876 * Get an additional character if we need one.
877 */
878 if ((nv_cmds[idx].cmd_flags & NV_NCH)
879 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
880 && oap->op_type == OP_NOP)
881 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
882 || (ca.cmdchar == 'q'
883 && oap->op_type == OP_NOP
884 && !Recording
885 && !Exec_reg)
886 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100887 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 {
889 int *cp;
890 int repl = FALSE; /* get character for replace mode */
891 int lit = FALSE; /* get extra character literally */
892 int langmap_active = FALSE; /* using :lmap mappings */
893 int lang; /* getting a text character */
894#ifdef USE_IM_CONTROL
895 int save_smd; /* saved value of p_smd */
896#endif
897
898 ++no_mapping;
899 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000900#ifdef FEAT_AUTOCMD
901 /* Don't generate a CursorHold event here, most commands can't handle
902 * it, e.g., nv_replace(), nv_csearch(). */
903 did_cursorhold = TRUE;
904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 if (ca.cmdchar == 'g')
906 {
907 /*
908 * For 'g' get the next character now, so that we can check for
909 * "gr", "g'" and "g`".
910 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000911 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913#ifdef FEAT_CMDL_INFO
914 need_flushbuf |= add_to_showcmd(ca.nchar);
915#endif
916 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100917 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 {
919 cp = &ca.extra_char; /* need to get a third character */
920 if (ca.nchar != 'r')
921 lit = TRUE; /* get it literally */
922 else
923 repl = TRUE; /* get it in replace mode */
924 }
925 else
926 cp = NULL; /* no third character needed */
927 }
928 else
929 {
930 if (ca.cmdchar == 'r') /* get it in replace mode */
931 repl = TRUE;
932 cp = &ca.nchar;
933 }
934 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
935
936 /*
937 * Get a second or third character.
938 */
939 if (cp != NULL)
940 {
941#ifdef CURSOR_SHAPE
942 if (repl)
943 {
944 State = REPLACE; /* pretend Replace mode */
945 ui_cursor_shape(); /* show different cursor shape */
946 }
947#endif
948 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
949 {
950 /* Allow mappings defined with ":lmap". */
951 --no_mapping;
952 --allow_keys;
953 if (repl)
954 State = LREPLACE;
955 else
956 State = LANGMAP;
957 langmap_active = TRUE;
958 }
959#ifdef USE_IM_CONTROL
960 save_smd = p_smd;
961 p_smd = FALSE; /* Don't let the IM code show the mode here */
962 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
963 im_set_active(TRUE);
964#endif
965
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000966 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967
968 if (langmap_active)
969 {
970 /* Undo the decrement done above */
971 ++no_mapping;
972 ++allow_keys;
973 State = NORMAL_BUSY;
974 }
975#ifdef USE_IM_CONTROL
976 if (lang)
977 {
978 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
979 im_save_status(&curbuf->b_p_iminsert);
980 im_set_active(FALSE);
981 }
982 p_smd = save_smd;
983#endif
984#ifdef CURSOR_SHAPE
985 State = NORMAL_BUSY;
986#endif
987#ifdef FEAT_CMDL_INFO
988 need_flushbuf |= add_to_showcmd(*cp);
989#endif
990
991 if (!lit)
992 {
993#ifdef FEAT_DIGRAPHS
994 /* Typing CTRL-K gets a digraph. */
995 if (*cp == Ctrl_K
996 && ((nv_cmds[idx].cmd_flags & NV_LANG)
997 || cp == &ca.extra_char)
998 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
999 {
1000 c = get_digraph(FALSE);
1001 if (c > 0)
1002 {
1003 *cp = c;
1004# ifdef FEAT_CMDL_INFO
1005 /* Guessing how to update showcmd here... */
1006 del_from_showcmd(3);
1007 need_flushbuf |= add_to_showcmd(*cp);
1008# endif
1009 }
1010 }
1011#endif
1012
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 /* adjust chars > 127, except after "tTfFr" commands */
1014 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015#ifdef FEAT_RIGHTLEFT
1016 /* adjust Hebrew mapped char */
1017 if (p_hkmap && lang && KeyTyped)
1018 *cp = hkmap(*cp);
1019# ifdef FEAT_FKMAP
1020 /* adjust Farsi mapped char */
1021 if (p_fkmap && lang && KeyTyped)
1022 *cp = fkmap(*cp);
1023# endif
1024#endif
1025 }
1026
1027 /*
1028 * When the next character is CTRL-\ a following CTRL-N means the
1029 * command is aborted and we go to Normal mode.
1030 */
1031 if (cp == &ca.extra_char
1032 && ca.nchar == Ctrl_BSL
1033 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1034 {
1035 ca.cmdchar = Ctrl_BSL;
1036 ca.nchar = ca.extra_char;
1037 idx = find_command(ca.cmdchar);
1038 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001039 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001040 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 else if (*cp == Ctrl_BSL)
1042 {
1043 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1044
1045 /* There is a busy wait here when typing "f<C-\>" and then
1046 * something different from CTRL-N. Can't be avoided. */
1047 while ((c = vpeekc()) <= 0 && towait > 0L)
1048 {
1049 do_sleep(towait > 50L ? 50L : towait);
1050 towait -= 50L;
1051 }
1052 if (c > 0)
1053 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001054 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 if (c != Ctrl_N && c != Ctrl_G)
1056 vungetc(c);
1057 else
1058 {
1059 ca.cmdchar = Ctrl_BSL;
1060 ca.nchar = c;
1061 idx = find_command(ca.cmdchar);
1062 }
1063 }
1064 }
1065
1066#ifdef FEAT_MBYTE
1067 /* When getting a text character and the next character is a
1068 * multi-byte character, it could be a composing character.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001069 * However, don't wait for it to arrive. Also, do enable mapping,
1070 * because if it's put back with vungetc() it's too late to apply
1071 * mapping. */
1072 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 while (enc_utf8 && lang && (c = vpeekc()) > 0
1074 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1075 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001076 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 if (!utf_iscomposing(c))
1078 {
1079 vungetc(c); /* it wasn't, put it back */
1080 break;
1081 }
1082 else if (ca.ncharC1 == 0)
1083 ca.ncharC1 = c;
1084 else
1085 ca.ncharC2 = c;
1086 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001087 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088#endif
1089 }
1090 --no_mapping;
1091 --allow_keys;
1092 }
1093
1094#ifdef FEAT_CMDL_INFO
1095 /*
1096 * Flush the showcmd characters onto the screen so we can see them while
1097 * the command is being executed. Only do this when the shown command was
1098 * actually displayed, otherwise this will slow down a lot when executing
1099 * mappings.
1100 */
1101 if (need_flushbuf)
1102 out_flush();
1103#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00001104#ifdef FEAT_AUTOCMD
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001105 if (ca.cmdchar != K_IGNORE)
1106 did_cursorhold = FALSE;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108
1109 State = NORMAL;
1110
1111 if (ca.nchar == ESC)
1112 {
1113 clearop(oap);
1114 if (restart_edit == 0 && goto_im())
1115 restart_edit = 'a';
1116 goto normal_end;
1117 }
1118
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001119 if (ca.cmdchar != K_IGNORE)
1120 {
1121 msg_didout = FALSE; /* don't scroll screen up for normal command */
1122 msg_col = 0;
1123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 old_pos = curwin->w_cursor; /* remember where cursor was */
1126
1127 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1128 * mode. */
1129 if (!VIsual_active && km_startsel)
1130 {
1131 if (nv_cmds[idx].cmd_flags & NV_SS)
1132 {
1133 start_selection();
1134 unshift_special(&ca);
1135 idx = find_command(ca.cmdchar);
1136 }
1137 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1138 && (mod_mask & MOD_MASK_SHIFT))
1139 {
1140 start_selection();
1141 mod_mask &= ~MOD_MASK_SHIFT;
1142 }
1143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144
1145 /*
1146 * Execute the command!
1147 * Call the command function found in the commands table.
1148 */
1149 ca.arg = nv_cmds[idx].cmd_arg;
1150 (nv_cmds[idx].cmd_func)(&ca);
1151
1152 /*
1153 * If we didn't start or finish an operator, reset oap->regname, unless we
1154 * need it later.
1155 */
1156 if (!finish_op
1157 && !oap->op_type
1158 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1159 {
1160 clearop(oap);
1161#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001162 {
1163 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001164
Bram Moolenaar536681b2011-05-10 16:12:45 +02001165 /* Adjust the register according to 'clipboard', so that when
1166 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001167# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001168 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001169# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001170 set_reg_var(regname);
1171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172#endif
1173 }
1174
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001175 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001176 * character or "z333<cr>". */
1177 if (old_mapped_len > 0)
1178 old_mapped_len = typebuf_maplen();
1179
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 /*
1181 * If an operation is pending, handle it...
1182 */
1183 do_pending_operator(&ca, old_col, FALSE);
1184
1185 /*
1186 * Wait for a moment when a message is displayed that will be overwritten
1187 * by the mode message.
1188 * In Visual mode and with "^O" in Insert mode, a short message will be
1189 * overwritten by the mode message. Wait a bit, until a key is hit.
1190 * In Visual mode, it's more important to keep the Visual area updated
1191 * than keeping a message (e.g. from a /pat search).
1192 * Only do this if the command was typed, not from a mapping.
1193 * Don't wait when emsg_silent is non-zero.
1194 * Also wait a bit after an error message, e.g. for "^O:".
1195 * Don't redraw the screen, it would remove the message.
1196 */
1197 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001198 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 || (VIsual_active
1201 && old_pos.lnum == curwin->w_cursor.lnum
1202 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 )
1204 && (clear_cmdline
1205 || redraw_cmdline)
1206 && (msg_didout || (msg_didany && msg_scroll))
1207 && !msg_nowait
1208 && KeyTyped)
1209 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 && (msg_scroll
1212 || emsg_on_display)))
1213 && oap->regname == 0
1214 && !(ca.retval & CA_COMMAND_BUSY)
1215 && stuff_empty()
1216 && typebuf_typed()
1217 && emsg_silent == 0
1218 && !did_wait_return
1219 && oap->op_type == OP_NOP)
1220 {
1221 int save_State = State;
1222
1223 /* Draw the cursor with the right shape here */
1224 if (restart_edit != 0)
1225 State = INSERT;
1226
1227 /* If need to redraw, and there is a "keep_msg", redraw before the
1228 * delay */
1229 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1230 {
1231 char_u *kmsg;
1232
1233 kmsg = keep_msg;
1234 keep_msg = NULL;
1235 /* showmode() will clear keep_msg, but we want to use it anyway */
1236 update_screen(0);
1237 /* now reset it, otherwise it's put in the history again */
1238 keep_msg = kmsg;
1239 msg_attr(kmsg, keep_msg_attr);
1240 vim_free(kmsg);
1241 }
1242 setcursor();
1243 cursor_on();
1244 out_flush();
1245 if (msg_scroll || emsg_on_display)
1246 ui_delay(1000L, TRUE); /* wait at least one second */
1247 ui_delay(3000L, FALSE); /* wait up to three seconds */
1248 State = save_State;
1249
1250 msg_scroll = FALSE;
1251 emsg_on_display = FALSE;
1252 }
1253
1254 /*
1255 * Finish up after executing a Normal mode command.
1256 */
1257normal_end:
1258
1259 msg_nowait = FALSE;
1260
1261 /* Reset finish_op, in case it was set */
1262#ifdef CURSOR_SHAPE
1263 c = finish_op;
1264#endif
1265 finish_op = FALSE;
1266#ifdef CURSOR_SHAPE
1267 /* Redraw the cursor with another shape, if we were in Operator-pending
1268 * mode or did a replace command. */
1269 if (c || ca.cmdchar == 'r')
1270 {
1271 ui_cursor_shape(); /* may show different cursor shape */
1272# ifdef FEAT_MOUSESHAPE
1273 update_mouseshape(-1);
1274# endif
1275 }
1276#endif
1277
1278#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001279 if (oap->op_type == OP_NOP && oap->regname == 0
1280# ifdef FEAT_AUTOCMD
1281 && ca.cmdchar != K_CURSORHOLD
1282# endif
1283 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 clear_showcmd();
1285#endif
1286
1287 checkpcmark(); /* check if we moved since setting pcmark */
1288 vim_free(ca.searchbuf);
1289
1290#ifdef FEAT_MBYTE
1291 if (has_mbyte)
1292 mb_adjust_cursor();
1293#endif
1294
1295#ifdef FEAT_SCROLLBIND
1296 if (curwin->w_p_scb && toplevel)
1297 {
1298 validate_cursor(); /* may need to update w_leftcol */
1299 do_check_scrollbind(TRUE);
1300 }
1301#endif
1302
Bram Moolenaar860cae12010-06-05 23:22:07 +02001303#ifdef FEAT_CURSORBIND
1304 if (curwin->w_p_crb && toplevel)
1305 {
1306 validate_cursor(); /* may need to update w_leftcol */
1307 do_check_cursorbind();
1308 }
1309#endif
1310
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 /*
1312 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1313 * if still inside a mapping that started in Visual mode).
1314 * May switch from Visual to Select mode after CTRL-O command.
1315 */
1316 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1318 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 && !(ca.retval & CA_COMMAND_BUSY)
1320 && stuff_empty()
1321 && oap->regname == 0)
1322 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 if (restart_VIsual_select == 1)
1324 {
1325 VIsual_select = TRUE;
1326 showmode();
1327 restart_VIsual_select = 0;
1328 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001329 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 (void)edit(restart_edit, FALSE, 1L);
1331 }
1332
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (restart_VIsual_select == 2)
1334 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
1336 /* Save count before an operator for next time. */
1337 opcount = ca.opcount;
1338}
1339
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001340#ifdef FEAT_EVAL
1341/*
1342 * Set v:count and v:count1 according to "cap".
1343 * Set v:prevcount only when "set_prevcount" is TRUE.
1344 */
1345 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001346set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001347{
1348 long count = cap->count0;
1349
1350 /* multiply with cap->opcount the same way as above */
1351 if (cap->opcount != 0)
1352 count = cap->opcount * (count == 0 ? 1 : count);
1353 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1354 *set_prevcount = FALSE; /* only set v:prevcount once */
1355}
1356#endif
1357
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358/*
1359 * Handle an operator after visual mode or when the movement is finished
1360 */
1361 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001362do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363{
1364 oparg_T *oap = cap->oap;
1365 pos_T old_cursor;
1366 int empty_region_error;
1367 int restart_edit_save;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001368#ifdef FEAT_LINEBREAK
1369 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 /* The visual area is remembered for redo */
1373 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1374 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001375 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaard79e5502016-01-10 22:13:02 +01001377 static int redo_VIsual_arg; /* extra argument */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001378#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 int include_line_break = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380#endif
1381
1382#if defined(FEAT_CLIPBOARD)
1383 /*
1384 * Yank the visual area into the GUI selection register before we operate
1385 * on it and lose it forever.
1386 * Don't do it if a specific register was specified, so that ""x"*P works.
1387 * This could call do_pending_operator() recursively, but that's OK
1388 * because gui_yank will be TRUE for the nested call.
1389 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001390 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 && oap->op_type != OP_NOP
1392 && !gui_yank
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 && VIsual_active
1394 && !redo_VIsual_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 && oap->regname == 0)
1396 clip_auto_select();
1397#endif
1398 old_cursor = curwin->w_cursor;
1399
1400 /*
1401 * If an operation is pending, handle it...
1402 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001403 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 {
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001405#ifdef FEAT_LINEBREAK
1406 /* Avoid a problem with unwanted linebreaks in block mode. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001407 if (curwin->w_p_lbr)
1408 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001409 curwin->w_p_lbr = FALSE;
1410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 oap->is_VIsual = VIsual_active;
1412 if (oap->motion_force == 'V')
1413 oap->motion_type = MLINE;
1414 else if (oap->motion_force == 'v')
1415 {
1416 /* If the motion was linewise, "inclusive" will not have been set.
1417 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1418 if (oap->motion_type == MLINE)
1419 oap->inclusive = FALSE;
1420 /* If the motion already was characterwise, toggle "inclusive" */
1421 else if (oap->motion_type == MCHAR)
1422 oap->inclusive = !oap->inclusive;
1423 oap->motion_type = MCHAR;
1424 }
1425 else if (oap->motion_force == Ctrl_V)
1426 {
1427 /* Change line- or characterwise motion into Visual block mode. */
1428 VIsual_active = TRUE;
1429 VIsual = oap->start;
1430 VIsual_mode = Ctrl_V;
1431 VIsual_select = FALSE;
1432 VIsual_reselect = FALSE;
1433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434
Bram Moolenaar7afea822013-04-24 18:34:45 +02001435 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1436 /* Never redo "zf" (define fold). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
Bram Moolenaar7afea822013-04-24 18:34:45 +02001438 && ((!VIsual_active || oap->motion_force)
1439 /* Also redo Operator-pending Visual mode mappings */
1440 || (VIsual_active && cap->cmdchar == ':'
1441 && oap->op_type != OP_COLON))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001442 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443#ifdef FEAT_FOLDING
1444 && oap->op_type != OP_FOLD
1445 && oap->op_type != OP_FOLDOPEN
1446 && oap->op_type != OP_FOLDOPENREC
1447 && oap->op_type != OP_FOLDCLOSE
1448 && oap->op_type != OP_FOLDCLOSEREC
1449 && oap->op_type != OP_FOLDDEL
1450 && oap->op_type != OP_FOLDDELREC
1451#endif
1452 )
1453 {
1454 prep_redo(oap->regname, cap->count0,
1455 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1456 oap->motion_force, cap->cmdchar, cap->nchar);
1457 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1458 {
1459 /*
1460 * If 'cpoptions' does not contain 'r', insert the search
1461 * pattern to really repeat the same command.
1462 */
1463 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001464 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 AppendToRedobuff(NL_STR);
1466 }
1467 else if (cap->cmdchar == ':')
1468 {
1469 /* do_cmdline() has stored the first typed line in
1470 * "repeat_cmdline". When several lines are typed repeating
1471 * won't be possible. */
1472 if (repeat_cmdline == NULL)
1473 ResetRedobuff();
1474 else
1475 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001476 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 AppendToRedobuff(NL_STR);
1478 vim_free(repeat_cmdline);
1479 repeat_cmdline = NULL;
1480 }
1481 }
1482 }
1483
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 if (redo_VIsual_busy)
1485 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001486 /* Redo of an operation on a Visual area. Use the same size from
1487 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 oap->start = curwin->w_cursor;
1489 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1490 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1491 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1492 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001493 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001495 if (VIsual_mode == 'v')
1496 {
1497 if (redo_VIsual_line_count <= 1)
1498 {
1499 validate_virtcol();
1500 curwin->w_curswant =
1501 curwin->w_virtcol + redo_VIsual_vcol - 1;
1502 }
1503 else
1504 curwin->w_curswant = redo_VIsual_vcol;
1505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001507 {
1508 curwin->w_curswant = MAXCOL;
1509 }
1510 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 }
1512 cap->count0 = redo_VIsual_count;
1513 if (redo_VIsual_count != 0)
1514 cap->count1 = redo_VIsual_count;
1515 else
1516 cap->count1 = 1;
1517 }
1518 else if (VIsual_active)
1519 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001520 if (!gui_yank)
1521 {
1522 /* Save the current VIsual area for '< and '> marks, and "gv" */
1523 curbuf->b_visual.vi_start = VIsual;
1524 curbuf->b_visual.vi_end = curwin->w_cursor;
1525 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001526 if (VIsual_mode_orig != NUL)
1527 {
1528 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1529 VIsual_mode_orig = NUL;
1530 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001531 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001533 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536
1537 /* In Select mode, a linewise selection is operated upon like a
Bram Moolenaard009e862015-06-09 20:20:03 +02001538 * characterwise selection.
1539 * Special case: gH<Del> deletes the last line. */
1540 if (VIsual_select && VIsual_mode == 'V'
1541 && cap->oap->op_type != OP_DELETE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001543 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 {
1545 VIsual.col = 0;
1546 curwin->w_cursor.col =
1547 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1548 }
1549 else
1550 {
1551 curwin->w_cursor.col = 0;
1552 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1553 }
1554 VIsual_mode = 'v';
1555 }
1556 /* If 'selection' is "exclusive", backup one character for
1557 * charwise selections. */
1558 else if (VIsual_mode == 'v')
1559 {
1560# ifdef FEAT_VIRTUALEDIT
1561 include_line_break =
1562# endif
1563 unadjust_for_sel();
1564 }
1565
1566 oap->start = VIsual;
1567 if (VIsual_mode == 'V')
1568 oap->start.col = 0;
1569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570
1571 /*
1572 * Set oap->start to the first position of the operated text, oap->end
1573 * to the end of the operated text. w_cursor is equal to oap->start.
1574 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001575 if (LT_POS(oap->start, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 {
1577#ifdef FEAT_FOLDING
1578 /* Include folded lines completely. */
1579 if (!VIsual_active)
1580 {
1581 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1582 oap->start.col = 0;
1583 if (hasFolding(curwin->w_cursor.lnum, NULL,
1584 &curwin->w_cursor.lnum))
1585 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1586 }
1587#endif
1588 oap->end = curwin->w_cursor;
1589 curwin->w_cursor = oap->start;
1590
1591 /* w_virtcol may have been updated; if the cursor goes back to its
1592 * previous position w_virtcol becomes invalid and isn't updated
1593 * automatically. */
1594 curwin->w_valid &= ~VALID_VIRTCOL;
1595 }
1596 else
1597 {
1598#ifdef FEAT_FOLDING
1599 /* Include folded lines completely. */
1600 if (!VIsual_active && oap->motion_type == MLINE)
1601 {
1602 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1603 NULL))
1604 curwin->w_cursor.col = 0;
1605 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1606 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1607 }
1608#endif
1609 oap->end = oap->start;
1610 oap->start = curwin->w_cursor;
1611 }
1612
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001613 /* Just in case lines were deleted that make the position invalid. */
1614 check_pos(curwin->w_buffer, &oap->end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1616
1617#ifdef FEAT_VIRTUALEDIT
1618 /* Set "virtual_op" before resetting VIsual_active. */
1619 virtual_op = virtual_active();
1620#endif
1621
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 if (VIsual_active || redo_VIsual_busy)
1623 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001624 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625
1626 if (!redo_VIsual_busy && !gui_yank)
1627 {
1628 /*
1629 * Prepare to reselect and redo Visual: this is based on the
1630 * size of the Visual text
1631 */
1632 resel_VIsual_mode = VIsual_mode;
1633 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001634 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001636 {
1637 if (VIsual_mode != Ctrl_V)
1638 getvvcol(curwin, &(oap->end),
1639 NULL, NULL, &oap->end_vcol);
1640 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1641 {
1642 if (VIsual_mode != Ctrl_V)
1643 getvvcol(curwin, &(oap->start),
1644 &oap->start_vcol, NULL, NULL);
1645 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1646 }
1647 else
1648 resel_VIsual_vcol = oap->end_vcol;
1649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 resel_VIsual_line_count = oap->line_count;
1651 }
1652
1653 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1654 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1655 && oap->op_type != OP_COLON
1656#ifdef FEAT_FOLDING
1657 && oap->op_type != OP_FOLD
1658 && oap->op_type != OP_FOLDOPEN
1659 && oap->op_type != OP_FOLDOPENREC
1660 && oap->op_type != OP_FOLDCLOSE
1661 && oap->op_type != OP_FOLDCLOSEREC
1662 && oap->op_type != OP_FOLDDEL
1663 && oap->op_type != OP_FOLDDELREC
1664#endif
1665 && oap->motion_force == NUL
1666 )
1667 {
1668 /* Prepare for redoing. Only use the nchar field for "r",
1669 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001670 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1671 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001672 prep_redo(oap->regname, cap->count0,
1673 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1674 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001675 else if (cap->cmdchar != ':')
Bram Moolenaar641e2862012-07-25 15:06:34 +02001676 prep_redo(oap->regname, 0L, NUL, 'v',
1677 get_op_char(oap->op_type),
1678 get_extra_op_char(oap->op_type),
1679 oap->op_type == OP_REPLACE
1680 ? cap->nchar : NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 if (!redo_VIsual_busy)
1682 {
1683 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001684 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 redo_VIsual_line_count = resel_VIsual_line_count;
1686 redo_VIsual_count = cap->count0;
Bram Moolenaard79e5502016-01-10 22:13:02 +01001687 redo_VIsual_arg = cap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 }
1689 }
1690
1691 /*
1692 * oap->inclusive defaults to TRUE.
1693 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1694 * FALSE. This makes "d}P" and "v}dP" work the same.
1695 */
1696 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1697 oap->inclusive = TRUE;
1698 if (VIsual_mode == 'V')
1699 oap->motion_type = MLINE;
1700 else
1701 {
1702 oap->motion_type = MCHAR;
1703 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001704#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 && (include_line_break || !virtual_op)
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 )
1708 {
1709 oap->inclusive = FALSE;
1710 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001711 * that works on lines only. */
Bram Moolenaard009e862015-06-09 20:20:03 +02001712 if (*p_sel != 'o'
1713 && !op_on_lines(oap->op_type)
1714 && oap->end.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 {
Bram Moolenaard009e862015-06-09 20:20:03 +02001716 ++oap->end.lnum;
1717 oap->end.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001718#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard009e862015-06-09 20:20:03 +02001719 oap->end.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001720#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02001721 ++oap->line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 }
1723 }
1724 }
1725
1726 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001727
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 /*
1729 * Switch Visual off now, so screen updating does
1730 * not show inverted text when the screen is redrawn.
1731 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1732 * no screen redraw, so it is done here to remove the inverted
1733 * part.
1734 */
1735 if (!gui_yank)
1736 {
1737 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001738#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 setmouse();
1740 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001741#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001742 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 if ((oap->op_type == OP_YANK
1744 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001745 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 || oap->op_type == OP_FILTER)
1747 && oap->motion_force == NUL)
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001748 {
1749#ifdef FEAT_LINEBREAK
1750 /* make sure redrawing is correct */
1751 curwin->w_p_lbr = lbr_saved;
1752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 }
1756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757
1758#ifdef FEAT_MBYTE
1759 /* Include the trailing byte of a multi-byte char. */
1760 if (has_mbyte && oap->inclusive)
1761 {
1762 int l;
1763
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001764 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 if (l > 1)
1766 oap->end.col += l - 1;
1767 }
1768#endif
1769 curwin->w_set_curswant = TRUE;
1770
1771 /*
1772 * oap->empty is set when start and end are the same. The inclusive
1773 * flag affects this too, unless yanking and the end is on a NUL.
1774 */
1775 oap->empty = (oap->motion_type == MCHAR
1776 && (!oap->inclusive
1777 || (oap->op_type == OP_YANK
1778 && gchar_pos(&oap->end) == NUL))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001779 && EQUAL_POS(oap->start, oap->end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780#ifdef FEAT_VIRTUALEDIT
1781 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1782#endif
1783 );
1784 /*
1785 * For delete, change and yank, it's an error to operate on an
1786 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1787 */
1788 empty_region_error = (oap->empty
1789 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1790
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 /* Force a redraw when operating on an empty Visual region, when
1792 * 'modifiable is off or creating a fold. */
1793 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001794#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 ))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001798 {
1799#ifdef FEAT_LINEBREAK
1800 curwin->w_p_lbr = lbr_saved;
1801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804
1805 /*
1806 * If the end of an operator is in column one while oap->motion_type
1807 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1808 * character in the previous line. If op_start is on or before the
1809 * first non-blank in the line, the operator becomes linewise
1810 * (strange, but that's the way vi does it).
1811 */
1812 if ( oap->motion_type == MCHAR
1813 && oap->inclusive == FALSE
1814 && !(cap->retval & CA_NO_ADJ_OP_END)
1815 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001817 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 && oap->line_count > 1)
1819 {
1820 oap->end_adjusted = TRUE; /* remember that we did this */
1821 --oap->line_count;
1822 --oap->end.lnum;
1823 if (inindent(0))
1824 oap->motion_type = MLINE;
1825 else
1826 {
1827 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1828 if (oap->end.col)
1829 {
1830 --oap->end.col;
1831 oap->inclusive = TRUE;
1832 }
1833 }
1834 }
1835 else
1836 oap->end_adjusted = FALSE;
1837
1838 switch (oap->op_type)
1839 {
1840 case OP_LSHIFT:
1841 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001842 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 auto_format(FALSE, TRUE);
1844 break;
1845
1846 case OP_JOIN_NS:
1847 case OP_JOIN:
1848 if (oap->line_count < 2)
1849 oap->line_count = 2;
1850 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1851 curbuf->b_ml.ml_line_count)
1852 beep_flush();
1853 else
1854 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001855 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001856 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 auto_format(FALSE, TRUE);
1858 }
1859 break;
1860
1861 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001864 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001865 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001866 CancelRedo();
1867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 else
1869 {
1870 (void)op_delete(oap);
1871 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1872 u_save_cursor(); /* cursor line wasn't saved yet */
1873 auto_format(FALSE, TRUE);
1874 }
1875 break;
1876
1877 case OP_YANK:
1878 if (empty_region_error)
1879 {
1880 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001881 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001882 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001883 CancelRedo();
1884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 }
1886 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001887 {
1888#ifdef FEAT_LINEBREAK
1889 curwin->w_p_lbr = lbr_saved;
1890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 (void)op_yank(oap, FALSE, !gui_yank);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 check_cursor_col();
1894 break;
1895
1896 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001899 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001900 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001901 CancelRedo();
1902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 else
1904 {
1905 /* This is a new edit command, not a restart. Need to
1906 * remember it to make 'insertmode' work with mappings for
1907 * Visual mode. But do this only once and not when typed and
1908 * 'insertmode' isn't set. */
1909 if (p_im || !KeyTyped)
1910 restart_edit_save = restart_edit;
1911 else
1912 restart_edit_save = 0;
1913 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001914#ifdef FEAT_LINEBREAK
1915 /* Restore linebreak, so that when the user edits it looks as
1916 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001917 if (curwin->w_p_lbr != lbr_saved)
1918 {
1919 curwin->w_p_lbr = lbr_saved;
1920 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1921 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 /* Reset finish_op now, don't want it set inside edit(). */
1924 finish_op = FALSE;
1925 if (op_change(oap)) /* will call edit() */
1926 cap->retval |= CA_COMMAND_BUSY;
1927 if (restart_edit == 0)
1928 restart_edit = restart_edit_save;
1929 }
1930 break;
1931
1932 case OP_FILTER:
1933 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1934 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1935 else
1936 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
1937
1938 case OP_INDENT:
1939 case OP_COLON:
1940
1941#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1942 /*
1943 * If 'equalprg' is empty, do the indenting internally.
1944 */
1945 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1946 {
1947# ifdef FEAT_LISP
1948 if (curbuf->b_p_lisp)
1949 {
1950 op_reindent(oap, get_lisp_indent);
1951 break;
1952 }
1953# endif
1954# ifdef FEAT_CINDENT
1955 op_reindent(oap,
1956# ifdef FEAT_EVAL
1957 *curbuf->b_p_inde != NUL ? get_expr_indent :
1958# endif
1959 get_c_indent);
1960 break;
1961# endif
1962 }
1963#endif
1964
1965 op_colon(oap);
1966 break;
1967
1968 case OP_TILDE:
1969 case OP_UPPER:
1970 case OP_LOWER:
1971 case OP_ROT13:
1972 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001973 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001974 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001975 CancelRedo();
1976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 else
1978 op_tilde(oap);
1979 check_cursor_col();
1980 break;
1981
1982 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001983#if defined(FEAT_EVAL)
1984 if (*curbuf->b_p_fex != NUL)
1985 op_formatexpr(oap); /* use expression */
1986 else
1987#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01001988 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 op_colon(oap); /* use external command */
1990 else
1991 op_format(oap, FALSE); /* use internal function */
1992 break;
1993
1994 case OP_FORMAT2:
1995 op_format(oap, TRUE); /* use internal function */
1996 break;
1997
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001998 case OP_FUNCTION:
Bram Moolenaar4a08b0d2016-11-05 21:55:13 +01001999#ifdef FEAT_LINEBREAK
2000 /* Restore linebreak, so that when the user edits it looks as
2001 * before. */
2002 curwin->w_p_lbr = lbr_saved;
2003#endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002004 op_function(oap); /* call 'operatorfunc' */
2005 break;
2006
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 case OP_INSERT:
2008 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010#ifdef FEAT_VISUALEXTRA
2011 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002012 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002013 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002014 CancelRedo();
2015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 else
2017 {
2018 /* This is a new edit command, not a restart. Need to
2019 * remember it to make 'insertmode' work with mappings for
2020 * Visual mode. But do this only once. */
2021 restart_edit_save = restart_edit;
2022 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002023#ifdef FEAT_LINEBREAK
2024 /* Restore linebreak, so that when the user edits it looks as
2025 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002026 if (curwin->w_p_lbr != lbr_saved)
2027 {
2028 curwin->w_p_lbr = lbr_saved;
2029 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2030 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 op_insert(oap, cap->count1);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002033#ifdef FEAT_LINEBREAK
2034 /* Reset linebreak, so that formatting works correctly. */
2035 curwin->w_p_lbr = FALSE;
2036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037
2038 /* TODO: when inserting in several lines, should format all
2039 * the lines. */
2040 auto_format(FALSE, TRUE);
2041
2042 if (restart_edit == 0)
2043 restart_edit = restart_edit_save;
Bram Moolenaar0b5c93a2017-02-01 15:03:30 +01002044 else
2045 cap->retval |= CA_COMMAND_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 }
2047#else
Bram Moolenaar165bc692015-07-21 17:53:25 +02002048 vim_beep(BO_OPER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049#endif
2050 break;
2051
2052 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054#ifdef FEAT_VISUALEXTRA
2055 if (empty_region_error)
2056#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002057 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002058 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002059 CancelRedo();
2060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061#ifdef FEAT_VISUALEXTRA
2062 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002063 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002064# ifdef FEAT_LINEBREAK
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002065 /* Restore linebreak, so that when the user edits it looks as
2066 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002067 if (curwin->w_p_lbr != lbr_saved)
2068 {
2069 curwin->w_p_lbr = lbr_saved;
2070 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2071 }
2072# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 op_replace(oap, cap->nchar);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075#endif
2076 break;
2077
2078#ifdef FEAT_FOLDING
2079 case OP_FOLD:
2080 VIsual_reselect = FALSE; /* don't reselect now */
2081 foldCreate(oap->start.lnum, oap->end.lnum);
2082 break;
2083
2084 case OP_FOLDOPEN:
2085 case OP_FOLDOPENREC:
2086 case OP_FOLDCLOSE:
2087 case OP_FOLDCLOSEREC:
2088 VIsual_reselect = FALSE; /* don't reselect now */
2089 opFoldRange(oap->start.lnum, oap->end.lnum,
2090 oap->op_type == OP_FOLDOPEN
2091 || oap->op_type == OP_FOLDOPENREC,
2092 oap->op_type == OP_FOLDOPENREC
2093 || oap->op_type == OP_FOLDCLOSEREC,
2094 oap->is_VIsual);
2095 break;
2096
2097 case OP_FOLDDEL:
2098 case OP_FOLDDELREC:
2099 VIsual_reselect = FALSE; /* don't reselect now */
2100 deleteFold(oap->start.lnum, oap->end.lnum,
2101 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2102 break;
2103#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002104 case OP_NR_ADD:
2105 case OP_NR_SUB:
2106 if (empty_region_error)
2107 {
2108 vim_beep(BO_OPER);
2109 CancelRedo();
2110 }
2111 else
2112 {
2113 VIsual_active = TRUE;
2114#ifdef FEAT_LINEBREAK
2115 curwin->w_p_lbr = lbr_saved;
2116#endif
2117 op_addsub(oap, cap->count1, redo_VIsual_arg);
2118 VIsual_active = FALSE;
2119 }
2120 check_cursor_col();
2121 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 default:
2123 clearopbeep(oap);
2124 }
2125#ifdef FEAT_VIRTUALEDIT
2126 virtual_op = MAYBE;
2127#endif
2128 if (!gui_yank)
2129 {
2130 /*
2131 * if 'sol' not set, go back to old column for some commands
2132 */
2133 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2134 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2135 || oap->op_type == OP_DELETE))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002136 {
2137#ifdef FEAT_LINEBREAK
2138 curwin->w_p_lbr = FALSE;
2139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 coladvance(curwin->w_curswant = old_col);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 }
2143 else
2144 {
2145 curwin->w_cursor = old_cursor;
2146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 clearop(oap);
2149 }
Bram Moolenaar404406a2014-10-09 13:24:43 +02002150#ifdef FEAT_LINEBREAK
2151 curwin->w_p_lbr = lbr_saved;
2152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153}
2154
2155/*
2156 * Handle indent and format operators and visual mode ":".
2157 */
2158 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002159op_colon(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160{
2161 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 if (oap->is_VIsual)
2163 stuffReadbuff((char_u *)"'<,'>");
2164 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 {
2166 /*
2167 * Make the range look nice, so it can be repeated.
2168 */
2169 if (oap->start.lnum == curwin->w_cursor.lnum)
2170 stuffcharReadbuff('.');
2171 else
2172 stuffnumReadbuff((long)oap->start.lnum);
2173 if (oap->end.lnum != oap->start.lnum)
2174 {
2175 stuffcharReadbuff(',');
2176 if (oap->end.lnum == curwin->w_cursor.lnum)
2177 stuffcharReadbuff('.');
2178 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2179 stuffcharReadbuff('$');
2180 else if (oap->start.lnum == curwin->w_cursor.lnum)
2181 {
2182 stuffReadbuff((char_u *)".+");
2183 stuffnumReadbuff((long)oap->line_count - 1);
2184 }
2185 else
2186 stuffnumReadbuff((long)oap->end.lnum);
2187 }
2188 }
2189 if (oap->op_type != OP_COLON)
2190 stuffReadbuff((char_u *)"!");
2191 if (oap->op_type == OP_INDENT)
2192 {
2193#ifndef FEAT_CINDENT
2194 if (*get_equalprg() == NUL)
2195 stuffReadbuff((char_u *)"indent");
2196 else
2197#endif
2198 stuffReadbuff(get_equalprg());
2199 stuffReadbuff((char_u *)"\n");
2200 }
2201 else if (oap->op_type == OP_FORMAT)
2202 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002203 if (*curbuf->b_p_fp != NUL)
2204 stuffReadbuff(curbuf->b_p_fp);
2205 else if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 stuffReadbuff(p_fp);
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002207 else
2208 stuffReadbuff((char_u *)"fmt");
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002209 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 }
2211
2212 /*
2213 * do_cmdline() does the rest
2214 */
2215}
2216
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002217/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002218 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002219 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002220 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002221op_function(oparg_T *oap UNUSED)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002222{
2223#ifdef FEAT_EVAL
2224 char_u *(argv[1]);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002225# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002226 int save_virtual_op = virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002227# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002228
2229 if (*p_opfunc == NUL)
2230 EMSG(_("E774: 'operatorfunc' is empty"));
2231 else
2232 {
2233 /* Set '[ and '] marks to text to be operated on. */
2234 curbuf->b_op_start = oap->start;
2235 curbuf->b_op_end = oap->end;
2236 if (oap->motion_type != MLINE && !oap->inclusive)
2237 /* Exclude the end position. */
2238 decl(&curbuf->b_op_end);
2239
2240 if (oap->block_mode)
2241 argv[0] = (char_u *)"block";
2242 else if (oap->motion_type == MLINE)
2243 argv[0] = (char_u *)"line";
2244 else
2245 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002246
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002247# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002248 /* Reset virtual_op so that 'virtualedit' can be changed in the
2249 * function. */
2250 virtual_op = MAYBE;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002251# endif
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002252
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002253 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002254
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002255# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002256 virtual_op = save_virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002257# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002258 }
2259#else
2260 EMSG(_("E775: Eval feature not available"));
2261#endif
2262}
2263
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264#if defined(FEAT_MOUSE) || defined(PROTO)
2265/*
2266 * Do the appropriate action for the current mouse click in the current mode.
2267 * Not used for Command-line mode.
2268 *
2269 * Normal Mode:
2270 * event modi- position visual change action
2271 * fier cursor window
2272 * left press - yes end yes
2273 * left press C yes end yes "^]" (2)
2274 * left press S yes end yes "*" (2)
2275 * left drag - yes start if moved no
2276 * left relse - yes start if moved no
2277 * middle press - yes if not active no put register
2278 * middle press - yes if active no yank and put
2279 * right press - yes start or extend yes
2280 * right press S yes no change yes "#" (2)
2281 * right drag - yes extend no
2282 * right relse - yes extend no
2283 *
2284 * Insert or Replace Mode:
2285 * event modi- position visual change action
2286 * fier cursor window
2287 * left press - yes (cannot be active) yes
2288 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2289 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2290 * left drag - yes start or extend (1) no CTRL-O (1)
2291 * left relse - yes start or extend (1) no CTRL-O (1)
2292 * middle press - no (cannot be active) no put register
2293 * right press - yes start or extend yes CTRL-O
2294 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2295 *
2296 * (1) only if mouse pointer moved since press
2297 * (2) only if click is in same buffer
2298 *
2299 * Return TRUE if start_arrow() should be called for edit mode.
2300 */
2301 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002302do_mouse(
2303 oparg_T *oap, /* operator argument, can be NULL */
2304 int c, /* K_LEFTMOUSE, etc */
2305 int dir, /* Direction to 'put' if necessary */
2306 long count,
2307 int fixindent) /* PUT_FIXINDENT if fixing indent necessary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308{
2309 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2310 static int got_click = FALSE; /* got a click some time back */
2311
2312 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2313 int is_click; /* If FALSE it's a drag or release event */
2314 int is_drag; /* If TRUE it's a drag event */
2315 int jump_flags = 0; /* flags for jump_to_mouse() */
2316 pos_T start_visual;
2317 int moved; /* Has cursor moved? */
2318 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002319#ifdef FEAT_WINDOWS
2320 static int in_tab_line = FALSE; /* mouse clicked in tab line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 int in_sep_line; /* mouse in vertical separator line */
2322#endif
2323 int c1, c2;
2324#if defined(FEAT_FOLDING)
2325 pos_T save_cursor;
2326#endif
2327 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 static pos_T orig_cursor;
2329 colnr_T leftcol, rightcol;
2330 pos_T end_visual;
2331 int diff;
2332 int old_active = VIsual_active;
2333 int old_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 int regname;
2335
2336#if defined(FEAT_FOLDING)
2337 save_cursor = curwin->w_cursor;
2338#endif
2339
2340 /*
2341 * When GUI is active, always recognize mouse events, otherwise:
2342 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2343 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2344 * - For command line and insert mode 'mouse' is checked before calling
2345 * do_mouse().
2346 */
2347 if (do_always)
2348 do_always = FALSE;
2349 else
2350#ifdef FEAT_GUI
2351 if (!gui.in_use)
2352#endif
2353 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 if (VIsual_active)
2355 {
2356 if (!mouse_has(MOUSE_VISUAL))
2357 return FALSE;
2358 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002359 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 return FALSE;
2361 }
2362
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002363 for (;;)
2364 {
2365 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2366 if (is_drag)
2367 {
2368 /* If the next character is the same mouse event then use that
2369 * one. Speeds up dragging the status line. */
2370 if (vpeekc() != NUL)
2371 {
2372 int nc;
2373 int save_mouse_row = mouse_row;
2374 int save_mouse_col = mouse_col;
2375
2376 /* Need to get the character, peeking doesn't get the actual
2377 * one. */
2378 nc = safe_vgetc();
2379 if (c == nc)
2380 continue;
2381 vungetc(nc);
2382 mouse_row = save_mouse_row;
2383 mouse_col = save_mouse_col;
2384 }
2385 }
2386 break;
2387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388
2389#ifdef FEAT_MOUSESHAPE
2390 /* May have stopped dragging the status or separator line. The pointer is
2391 * most likely still on the status or separator line. */
2392 if (!is_drag && drag_status_line)
2393 {
2394 drag_status_line = FALSE;
2395 update_mouseshape(SHAPE_IDX_STATUS);
2396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 if (!is_drag && drag_sep_line)
2398 {
2399 drag_sep_line = FALSE;
2400 update_mouseshape(SHAPE_IDX_VSEP);
2401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402#endif
2403
2404 /*
2405 * Ignore drag and release events if we didn't get a click.
2406 */
2407 if (is_click)
2408 got_click = TRUE;
2409 else
2410 {
2411 if (!got_click) /* didn't get click, ignore */
2412 return FALSE;
2413 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002414 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002416#ifdef FEAT_WINDOWS
2417 if (in_tab_line)
2418 {
2419 in_tab_line = FALSE;
2420 return FALSE;
2421 }
2422#endif
2423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 }
2425
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 /*
2427 * CTRL right mouse button does CTRL-T
2428 */
2429 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2430 {
2431 if (State & INSERT)
2432 stuffcharReadbuff(Ctrl_O);
2433 if (count > 1)
2434 stuffnumReadbuff(count);
2435 stuffcharReadbuff(Ctrl_T);
2436 got_click = FALSE; /* ignore drag&release now */
2437 return FALSE;
2438 }
2439
2440 /*
2441 * CTRL only works with left mouse button
2442 */
2443 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2444 return FALSE;
2445
2446 /*
2447 * When a modifier is down, ignore drag and release events, as well as
2448 * multiple clicks and the middle mouse button.
2449 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2450 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002451 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2452 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 && (!is_click
2454 || (mod_mask & MOD_MASK_MULTI_CLICK)
2455 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002456 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 && mouse_model_popup()
2458 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002459 && !((mod_mask & MOD_MASK_ALT)
2460 && !mouse_model_popup()
2461 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 )
2463 return FALSE;
2464
2465 /*
2466 * If the button press was used as the movement command for an operator
2467 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2468 * drag/release events.
2469 */
2470 if (!is_click && which_button == MOUSE_MIDDLE)
2471 return FALSE;
2472
2473 if (oap != NULL)
2474 regname = oap->regname;
2475 else
2476 regname = 0;
2477
2478 /*
2479 * Middle mouse button does a 'put' of the selected text
2480 */
2481 if (which_button == MOUSE_MIDDLE)
2482 {
2483 if (State == NORMAL)
2484 {
2485 /*
2486 * If an operator was pending, we don't know what the user wanted
2487 * to do. Go back to normal mode: Clear the operator and beep().
2488 */
2489 if (oap != NULL && oap->op_type != OP_NOP)
2490 {
2491 clearopbeep(oap);
2492 return FALSE;
2493 }
2494
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 /*
2496 * If visual was active, yank the highlighted text and put it
2497 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002498 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 */
2500 if (VIsual_active)
2501 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002502 if (VIsual_select)
2503 {
2504 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002505 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002506 }
2507 else
2508 {
2509 stuffcharReadbuff('y');
2510 stuffcharReadbuff(K_MIDDLEMOUSE);
2511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 do_always = TRUE; /* ignore 'mouse' setting next time */
2513 return FALSE;
2514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 /*
2516 * The rest is below jump_to_mouse()
2517 */
2518 }
2519
2520 else if ((State & INSERT) == 0)
2521 return FALSE;
2522
2523 /*
2524 * Middle click in insert mode doesn't move the mouse, just insert the
2525 * contents of a register. '.' register is special, can't insert that
2526 * with do_put().
2527 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2528 * happens for the GUI).
2529 */
2530 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2531 {
2532 if (regname == '.')
2533 insert_reg(regname, TRUE);
2534 else
2535 {
2536#ifdef FEAT_CLIPBOARD
2537 if (clip_star.available && regname == 0)
2538 regname = '*';
2539#endif
2540 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2541 insert_reg(regname, TRUE);
2542 else
2543 {
2544 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2545
2546 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2547 AppendCharToRedobuff(Ctrl_R);
2548 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2549 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2550 }
2551 }
2552 return FALSE;
2553 }
2554 }
2555
2556 /* When dragging or button-up stay in the same window. */
2557 if (!is_click)
2558 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2559
2560 start_visual.lnum = 0;
2561
Bram Moolenaarf740b292006-02-16 22:11:02 +00002562#ifdef FEAT_WINDOWS
2563 /* Check for clicking in the tab page line. */
2564 if (mouse_row == 0 && firstwin->w_winrow > 0)
2565 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002566 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002567 {
2568 if (in_tab_line)
2569 {
2570 c1 = TabPageIdxs[mouse_col];
Bram Moolenaar4a4b8212015-09-08 17:50:41 +02002571 tabpage_move(c1 <= 0 ? 9999 : c1 < tabpage_index(curtab)
2572 ? c1 - 1 : c1);
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002573 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002574 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002575 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002576
Bram Moolenaarf740b292006-02-16 22:11:02 +00002577 /* click in a tab selects that tab page */
2578 if (is_click
2579# ifdef FEAT_CMDWIN
2580 && cmdwin_type == 0
2581# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002582 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002583 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002584 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002585 c1 = TabPageIdxs[mouse_col];
2586 if (c1 >= 0)
2587 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002588 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2589 {
2590 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002591 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002592 tabpage_new();
2593 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2594 }
2595 else
2596 {
2597 /* Go to specified tab page, or next one if not clicking
2598 * on a label. */
2599 goto_tabpage(c1);
2600
2601 /* It's like clicking on the status line of a window. */
2602 if (curwin != old_curwin)
2603 end_visual_mode();
2604 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002605 }
2606 else if (c1 < 0)
2607 {
2608 tabpage_T *tp;
2609
2610 /* Close the current or specified tab page. */
2611 if (c1 == -999)
2612 tp = curtab;
2613 else
2614 tp = find_tabpage(-c1);
2615 if (tp == curtab)
2616 {
2617 if (first_tabpage->tp_next != NULL)
2618 tabpage_close(FALSE);
2619 }
2620 else if (tp != NULL)
2621 tabpage_close_other(tp, FALSE);
2622 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002623 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002624 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002625 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002626 else if (is_drag && in_tab_line)
2627 {
2628 c1 = TabPageIdxs[mouse_col];
2629 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2630 return FALSE;
2631 }
2632
Bram Moolenaarf740b292006-02-16 22:11:02 +00002633#endif
2634
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 /*
2636 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2637 * right button up -> pop-up menu
2638 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002639 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 */
2641 if (mouse_model_popup())
2642 {
2643 if (which_button == MOUSE_RIGHT
2644 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2645 {
2646 /*
2647 * NOTE: Ignore right button down and drag mouse events.
2648 * Windows only shows the popup menu on the button up event.
2649 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002650#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2651 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 if (!is_click)
2653 return FALSE;
2654#endif
2655#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2656 if (is_click || is_drag)
2657 return FALSE;
2658#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002659#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2661 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2662 if (gui.in_use)
2663 {
2664 jump_flags = 0;
2665 if (STRCMP(p_mousem, "popup_setpos") == 0)
2666 {
2667 /* First set the cursor position before showing the popup
2668 * menu. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 if (VIsual_active)
2670 {
2671 pos_T m_pos;
2672
2673 /*
2674 * set MOUSE_MAY_STOP_VIS if we are outside the
2675 * selection or the current window (might have false
2676 * negative here)
2677 */
2678 if (mouse_row < W_WINROW(curwin)
2679 || mouse_row
2680 > (W_WINROW(curwin) + curwin->w_height))
2681 jump_flags = MOUSE_MAY_STOP_VIS;
2682 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2683 jump_flags = MOUSE_MAY_STOP_VIS;
2684 else
2685 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002686 if ((LT_POS(curwin->w_cursor, VIsual)
2687 && (LT_POS(m_pos, curwin->w_cursor)
2688 || LT_POS(VIsual, m_pos)))
2689 || (LT_POS(VIsual, curwin->w_cursor)
2690 && (LT_POS(m_pos, VIsual)
2691 || LT_POS(curwin->w_cursor, m_pos))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 {
2693 jump_flags = MOUSE_MAY_STOP_VIS;
2694 }
2695 else if (VIsual_mode == Ctrl_V)
2696 {
2697 getvcols(curwin, &curwin->w_cursor, &VIsual,
2698 &leftcol, &rightcol);
2699 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2700 if (m_pos.col < leftcol || m_pos.col > rightcol)
2701 jump_flags = MOUSE_MAY_STOP_VIS;
2702 }
2703 }
2704 }
2705 else
2706 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 }
2708 if (jump_flags)
2709 {
2710 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002711 update_curbuf(VIsual_active ? INVERTED : VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 setcursor();
2713 out_flush(); /* Update before showing popup menu */
2714 }
2715# ifdef FEAT_MENU
2716 gui_show_popupmenu();
2717# endif
2718 return (jump_flags & CURSOR_MOVED) != 0;
2719 }
2720 else
2721 return FALSE;
2722#else
2723 return FALSE;
2724#endif
2725 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002726 if (which_button == MOUSE_LEFT
2727 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 {
2729 which_button = MOUSE_RIGHT;
2730 mod_mask &= ~MOD_MASK_SHIFT;
2731 }
2732 }
2733
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 if ((State & (NORMAL | INSERT))
2735 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2736 {
2737 if (which_button == MOUSE_LEFT)
2738 {
2739 if (is_click)
2740 {
2741 /* stop Visual mode for a left click in a window, but not when
2742 * on a status line */
2743 if (VIsual_active)
2744 jump_flags |= MOUSE_MAY_STOP_VIS;
2745 }
2746 else if (mouse_has(MOUSE_VISUAL))
2747 jump_flags |= MOUSE_MAY_VIS;
2748 }
2749 else if (which_button == MOUSE_RIGHT)
2750 {
2751 if (is_click && VIsual_active)
2752 {
2753 /*
2754 * Remember the start and end of visual before moving the
2755 * cursor.
2756 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002757 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 {
2759 start_visual = curwin->w_cursor;
2760 end_visual = VIsual;
2761 }
2762 else
2763 {
2764 start_visual = VIsual;
2765 end_visual = curwin->w_cursor;
2766 }
2767 }
2768 jump_flags |= MOUSE_FOCUS;
2769 if (mouse_has(MOUSE_VISUAL))
2770 jump_flags |= MOUSE_MAY_VIS;
2771 }
2772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773
2774 /*
2775 * If an operator is pending, ignore all drags and releases until the
2776 * next mouse click.
2777 */
2778 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2779 {
2780 got_click = FALSE;
2781 oap->motion_type = MCHAR;
2782 }
2783
2784 /* When releasing the button let jump_to_mouse() know. */
2785 if (!is_click && !is_drag)
2786 jump_flags |= MOUSE_RELEASED;
2787
2788 /*
2789 * JUMP!
2790 */
2791 jump_flags = jump_to_mouse(jump_flags,
2792 oap == NULL ? NULL : &(oap->inclusive), which_button);
2793 moved = (jump_flags & CURSOR_MOVED);
2794 in_status_line = (jump_flags & IN_STATUS_LINE);
Bram Moolenaar829c8e32016-03-19 23:07:23 +01002795#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 in_sep_line = (jump_flags & IN_SEP_LINE);
Bram Moolenaar829c8e32016-03-19 23:07:23 +01002797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798
2799#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002800 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2802 {
2803 int key = KEY2TERMCAP1(c);
2804
2805 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2806 || key == (int)KE_RIGHTRELEASE)
2807 netbeans_button_release(which_button);
2808 }
2809#endif
2810
2811 /* When jumping to another window, clear a pending operator. That's a bit
2812 * friendlier than beeping and not jumping to that window. */
2813 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2814 clearop(oap);
2815
2816#ifdef FEAT_FOLDING
2817 if (mod_mask == 0
2818 && !is_drag
2819 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2820 && which_button == MOUSE_LEFT)
2821 {
2822 /* open or close a fold at this line */
2823 if (jump_flags & MOUSE_FOLD_OPEN)
2824 openFold(curwin->w_cursor.lnum, 1L);
2825 else
2826 closeFold(curwin->w_cursor.lnum, 1L);
2827 /* don't move the cursor if still in the same window */
2828 if (curwin == old_curwin)
2829 curwin->w_cursor = save_cursor;
2830 }
2831#endif
2832
2833#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2834 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2835 {
2836 clip_modeless(which_button, is_click, is_drag);
2837 return FALSE;
2838 }
2839#endif
2840
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002842 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 if (VIsual_active && is_drag && p_so)
2844 {
2845 /* In the very first line, allow scrolling one line */
2846 if (mouse_row == 0)
2847 mouse_dragging = 2;
2848 else
2849 mouse_dragging = 1;
2850 }
2851
2852 /* When dragging the mouse above the window, scroll down. */
2853 if (is_drag && mouse_row < 0 && !in_status_line)
2854 {
2855 scroll_redraw(FALSE, 1L);
2856 mouse_row = 0;
2857 }
2858
2859 if (start_visual.lnum) /* right click in visual mode */
2860 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002861 /* When ALT is pressed make Visual mode blockwise. */
2862 if (mod_mask & MOD_MASK_ALT)
2863 VIsual_mode = Ctrl_V;
2864
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 /*
2866 * In Visual-block mode, divide the area in four, pick up the corner
2867 * that is in the quarter that the cursor is in.
2868 */
2869 if (VIsual_mode == Ctrl_V)
2870 {
2871 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2872 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2873 end_visual.col = leftcol;
2874 else
2875 end_visual.col = rightcol;
Bram Moolenaarcde88542015-08-11 19:14:00 +02002876 if (curwin->w_cursor.lnum >=
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 (start_visual.lnum + end_visual.lnum) / 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 end_visual.lnum = start_visual.lnum;
2879
2880 /* move VIsual to the right column */
2881 start_visual = curwin->w_cursor; /* save the cursor pos */
2882 curwin->w_cursor = end_visual;
2883 coladvance(end_visual.col);
2884 VIsual = curwin->w_cursor;
2885 curwin->w_cursor = start_visual; /* restore the cursor */
2886 }
2887 else
2888 {
2889 /*
2890 * If the click is before the start of visual, change the start.
2891 * If the click is after the end of visual, change the end. If
2892 * the click is inside the visual, change the closest side.
2893 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002894 if (LT_POS(curwin->w_cursor, start_visual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 VIsual = end_visual;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002896 else if (LT_POS(end_visual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 VIsual = start_visual;
2898 else
2899 {
2900 /* In the same line, compare column number */
2901 if (end_visual.lnum == start_visual.lnum)
2902 {
2903 if (curwin->w_cursor.col - start_visual.col >
2904 end_visual.col - curwin->w_cursor.col)
2905 VIsual = start_visual;
2906 else
2907 VIsual = end_visual;
2908 }
2909
2910 /* In different lines, compare line number */
2911 else
2912 {
2913 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2914 (end_visual.lnum - curwin->w_cursor.lnum);
2915
2916 if (diff > 0) /* closest to end */
2917 VIsual = start_visual;
2918 else if (diff < 0) /* closest to start */
2919 VIsual = end_visual;
2920 else /* in the middle line */
2921 {
2922 if (curwin->w_cursor.col <
2923 (start_visual.col + end_visual.col) / 2)
2924 VIsual = end_visual;
2925 else
2926 VIsual = start_visual;
2927 }
2928 }
2929 }
2930 }
2931 }
2932 /*
2933 * If Visual mode started in insert mode, execute "CTRL-O"
2934 */
2935 else if ((State & INSERT) && VIsual_active)
2936 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937
2938 /*
2939 * Middle mouse click: Put text before cursor.
2940 */
2941 if (which_button == MOUSE_MIDDLE)
2942 {
2943#ifdef FEAT_CLIPBOARD
2944 if (clip_star.available && regname == 0)
2945 regname = '*';
2946#endif
2947 if (yank_register_mline(regname))
2948 {
2949 if (mouse_past_bottom)
2950 dir = FORWARD;
2951 }
2952 else if (mouse_past_eol)
2953 dir = FORWARD;
2954
2955 if (fixindent)
2956 {
2957 c1 = (dir == BACKWARD) ? '[' : ']';
2958 c2 = 'p';
2959 }
2960 else
2961 {
2962 c1 = (dir == FORWARD) ? 'p' : 'P';
2963 c2 = NUL;
2964 }
2965 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2966
2967 /*
2968 * Remember where the paste started, so in edit() Insstart can be set
2969 * to this position
2970 */
2971 if (restart_edit != 0)
2972 where_paste_started = curwin->w_cursor;
2973 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2974 }
2975
2976#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2977 /*
2978 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2979 * error under the mouse pointer.
2980 */
2981 else if (((mod_mask & MOD_MASK_CTRL)
2982 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2983 && bt_quickfix(curbuf))
2984 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002985 if (curwin->w_llist_ref == NULL) /* quickfix window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01002986 do_cmdline_cmd((char_u *)".cc");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002987 else /* location list window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01002988 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 got_click = FALSE; /* ignore drag&release now */
2990 }
2991#endif
2992
2993 /*
2994 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2995 * under the mouse pointer.
2996 */
2997 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2998 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2999 {
3000 if (State & INSERT)
3001 stuffcharReadbuff(Ctrl_O);
3002 stuffcharReadbuff(Ctrl_RSB);
3003 got_click = FALSE; /* ignore drag&release now */
3004 }
3005
3006 /*
3007 * Shift-Mouse click searches for the next occurrence of the word under
3008 * the mouse pointer
3009 */
3010 else if ((mod_mask & MOD_MASK_SHIFT))
3011 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003012 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 stuffcharReadbuff(Ctrl_O);
3014 if (which_button == MOUSE_LEFT)
3015 stuffcharReadbuff('*');
3016 else /* MOUSE_RIGHT */
3017 stuffcharReadbuff('#');
3018 }
3019
3020 /* Handle double clicks, unless on status line */
3021 else if (in_status_line)
3022 {
3023#ifdef FEAT_MOUSESHAPE
3024 if ((is_drag || is_click) && !drag_status_line)
3025 {
3026 drag_status_line = TRUE;
3027 update_mouseshape(-1);
3028 }
3029#endif
3030 }
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003031#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 else if (in_sep_line)
3033 {
3034# ifdef FEAT_MOUSESHAPE
3035 if ((is_drag || is_click) && !drag_sep_line)
3036 {
3037 drag_sep_line = TRUE;
3038 update_mouseshape(-1);
3039 }
3040# endif
3041 }
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3044 && mouse_has(MOUSE_VISUAL))
3045 {
3046 if (is_click || !VIsual_active)
3047 {
3048 if (VIsual_active)
3049 orig_cursor = VIsual;
3050 else
3051 {
3052 check_visual_highlight();
3053 VIsual = curwin->w_cursor;
3054 orig_cursor = VIsual;
3055 VIsual_active = TRUE;
3056 VIsual_reselect = TRUE;
3057 /* start Select mode if 'selectmode' contains "mouse" */
3058 may_start_select('o');
3059 setmouse();
3060 }
3061 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003062 {
3063 /* Double click with ALT pressed makes it blockwise. */
3064 if (mod_mask & MOD_MASK_ALT)
3065 VIsual_mode = Ctrl_V;
3066 else
3067 VIsual_mode = 'v';
3068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3070 VIsual_mode = 'V';
3071 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3072 VIsual_mode = Ctrl_V;
3073#ifdef FEAT_CLIPBOARD
3074 /* Make sure the clipboard gets updated. Needed because start and
3075 * end may still be the same, and the selection needs to be owned */
3076 clip_star.vmode = NUL;
3077#endif
3078 }
3079 /*
3080 * A double click selects a word or a block.
3081 */
3082 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3083 {
3084 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003085 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086
3087 if (is_click)
3088 {
3089 /* If the character under the cursor (skipping white space) is
3090 * not a word character, try finding a match and select a (),
3091 * {}, [], #if/#endif, etc. block. */
3092 end_visual = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01003093 while (gc = gchar_pos(&end_visual), VIM_ISWHITE(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 inc(&end_visual);
3095 if (oap != NULL)
3096 oap->motion_type = MCHAR;
3097 if (oap != NULL
3098 && VIsual_mode == 'v'
3099 && !vim_iswordc(gchar_pos(&end_visual))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003100 && EQUAL_POS(curwin->w_cursor, VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 && (pos = findmatch(oap, NUL)) != NULL)
3102 {
3103 curwin->w_cursor = *pos;
3104 if (oap->motion_type == MLINE)
3105 VIsual_mode = 'V';
3106 else if (*p_sel == 'e')
3107 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003108 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 ++VIsual.col;
3110 else
3111 ++curwin->w_cursor.col;
3112 }
3113 }
3114 }
3115
3116 if (pos == NULL && (is_click || is_drag))
3117 {
3118 /* When not found a match or when dragging: extend to include
3119 * a word. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003120 if (LT_POS(curwin->w_cursor, orig_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 {
3122 find_start_of_word(&curwin->w_cursor);
3123 find_end_of_word(&VIsual);
3124 }
3125 else
3126 {
3127 find_start_of_word(&VIsual);
3128 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3129#ifdef FEAT_MBYTE
3130 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003131 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132#else
3133 ++curwin->w_cursor.col;
3134#endif
3135 find_end_of_word(&curwin->w_cursor);
3136 }
3137 }
3138 curwin->w_set_curswant = TRUE;
3139 }
3140 if (is_click)
3141 redraw_curbuf_later(INVERTED); /* update the inversion */
3142 }
3143 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003144 {
3145 if (mod_mask & MOD_MASK_ALT)
3146 VIsual_mode = Ctrl_V;
3147 else
3148 VIsual_mode = 'v';
3149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150
3151 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003152 if ((!VIsual_active && old_active && mode_displayed)
3153 || (VIsual_active && p_smd && msg_silent == 0
3154 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
3157 return moved;
3158}
3159
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160/*
3161 * Move "pos" back to the start of the word it's in.
3162 */
3163 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003164find_start_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165{
3166 char_u *line;
3167 int cclass;
3168 int col;
3169
3170 line = ml_get(pos->lnum);
3171 cclass = get_mouse_class(line + pos->col);
3172
3173 while (pos->col > 0)
3174 {
3175 col = pos->col - 1;
3176#ifdef FEAT_MBYTE
3177 col -= (*mb_head_off)(line, line + col);
3178#endif
3179 if (get_mouse_class(line + col) != cclass)
3180 break;
3181 pos->col = col;
3182 }
3183}
3184
3185/*
3186 * Move "pos" forward to the end of the word it's in.
3187 * When 'selection' is "exclusive", the position is just after the word.
3188 */
3189 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003190find_end_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191{
3192 char_u *line;
3193 int cclass;
3194 int col;
3195
3196 line = ml_get(pos->lnum);
3197 if (*p_sel == 'e' && pos->col > 0)
3198 {
3199 --pos->col;
3200#ifdef FEAT_MBYTE
3201 pos->col -= (*mb_head_off)(line, line + pos->col);
3202#endif
3203 }
3204 cclass = get_mouse_class(line + pos->col);
3205 while (line[pos->col] != NUL)
3206 {
3207#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003208 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209#else
3210 col = pos->col + 1;
3211#endif
3212 if (get_mouse_class(line + col) != cclass)
3213 {
3214 if (*p_sel == 'e')
3215 pos->col = col;
3216 break;
3217 }
3218 pos->col = col;
3219 }
3220}
3221
3222/*
3223 * Get class of a character for selection: same class means same word.
3224 * 0: blank
3225 * 1: punctuation groups
3226 * 2: normal word character
3227 * >2: multi-byte word character.
3228 */
3229 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003230get_mouse_class(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231{
3232 int c;
3233
3234#ifdef FEAT_MBYTE
3235 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3236 return mb_get_class(p);
3237#endif
3238
3239 c = *p;
3240 if (c == ' ' || c == '\t')
3241 return 0;
3242
3243 if (vim_iswordc(c))
3244 return 2;
3245
3246 /*
3247 * There are a few special cases where we want certain combinations of
3248 * characters to be considered as a single word. These are things like
3249 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003250 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 */
3252 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3253 return 1;
3254 return c;
3255}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256#endif /* FEAT_MOUSE */
3257
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258/*
3259 * Check if highlighting for visual mode is possible, give a warning message
3260 * if not.
3261 */
3262 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003263check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264{
3265 static int did_check = FALSE;
3266
3267 if (full_screen)
3268 {
3269 if (!did_check && hl_attr(HLF_V) == 0)
3270 MSG(_("Warning: terminal cannot highlight"));
3271 did_check = TRUE;
3272 }
3273}
3274
3275/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003276 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 * This function should ALWAYS be called to end Visual mode, except from
3278 * do_pending_operator().
3279 */
3280 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003281end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282{
3283#ifdef FEAT_CLIPBOARD
3284 /*
3285 * If we are using the clipboard, then remember what was selected in case
3286 * we need to paste it somewhere while we still own the selection.
3287 * Only do this when the clipboard is already owned. Don't want to grab
3288 * the selection when hitting ESC.
3289 */
3290 if (clip_star.available && clip_star.owned)
3291 clip_auto_select();
3292#endif
3293
3294 VIsual_active = FALSE;
3295#ifdef FEAT_MOUSE
3296 setmouse();
3297 mouse_dragging = 0;
3298#endif
3299
3300 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003301 curbuf->b_visual.vi_mode = VIsual_mode;
3302 curbuf->b_visual.vi_start = VIsual;
3303 curbuf->b_visual.vi_end = curwin->w_cursor;
3304 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305#ifdef FEAT_EVAL
3306 curbuf->b_visual_mode_eval = VIsual_mode;
3307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308#ifdef FEAT_VIRTUALEDIT
3309 if (!virtual_active())
3310 curwin->w_cursor.coladd = 0;
3311#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003312 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003314 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315}
3316
3317/*
3318 * Reset VIsual_active and VIsual_reselect.
3319 */
3320 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003321reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322{
3323 if (VIsual_active)
3324 {
3325 end_visual_mode();
3326 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3327 }
3328 VIsual_reselect = FALSE;
3329}
3330
3331/*
3332 * Reset VIsual_active and VIsual_reselect if it's set.
3333 */
3334 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003335reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336{
3337 if (VIsual_active)
3338 {
3339 end_visual_mode();
3340 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3341 VIsual_reselect = FALSE;
3342 }
3343}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003345#if defined(FEAT_BEVAL)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003346static int find_is_eval_item(char_u *ptr, int *colp, int *nbp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347
3348/*
3349 * Check for a balloon-eval special item to include when searching for an
3350 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3351 * Returns TRUE if the character at "*ptr" should be included.
3352 * "dir" is FORWARD or BACKWARD, the direction of searching.
3353 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3354 * "bnp" points to a counter for square brackets.
3355 */
3356 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003357find_is_eval_item(
3358 char_u *ptr,
3359 int *colp,
3360 int *bnp,
3361 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362{
3363 /* Accept everything inside []. */
3364 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3365 ++*bnp;
3366 if (*bnp > 0)
3367 {
3368 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3369 --*bnp;
3370 return TRUE;
3371 }
3372
3373 /* skip over "s.var" */
3374 if (*ptr == '.')
3375 return TRUE;
3376
3377 /* two-character item: s->var */
3378 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3379 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3380 {
3381 *colp += dir;
3382 return TRUE;
3383 }
3384 return FALSE;
3385}
3386#endif
3387
3388/*
3389 * Find the identifier under or to the right of the cursor.
3390 * "find_type" can have one of three values:
3391 * FIND_IDENT: find an identifier (keyword)
3392 * FIND_STRING: find any non-white string
3393 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003394 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 *
3396 * There are three steps:
3397 * 1. Search forward for the start of an identifier/string. Doesn't move if
3398 * already on one.
3399 * 2. Search backward for the start of this identifier/string.
3400 * This doesn't match the real Vi but I like it a little better and it
3401 * shouldn't bother anyone.
3402 * 3. Search forward to the end of this identifier/string.
3403 * When FIND_IDENT isn't defined, we backup until a blank.
3404 *
3405 * Returns the length of the string, or zero if no string is found.
3406 * If a string is found, a pointer to the string is put in "*string". This
3407 * string is not always NUL terminated.
3408 */
3409 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003410find_ident_under_cursor(char_u **string, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411{
3412 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3413 curwin->w_cursor.col, string, find_type);
3414}
3415
3416/*
3417 * Like find_ident_under_cursor(), but for any window and any position.
3418 * However: Uses 'iskeyword' from the current window!.
3419 */
3420 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003421find_ident_at_pos(
3422 win_T *wp,
3423 linenr_T lnum,
3424 colnr_T startcol,
3425 char_u **string,
3426 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427{
3428 char_u *ptr;
3429 int col = 0; /* init to shut up GCC */
3430 int i;
3431#ifdef FEAT_MBYTE
3432 int this_class = 0;
3433 int prev_class;
3434 int prevcol;
3435#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003436#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 int bn = 0; /* bracket nesting */
3438#endif
3439
3440 /*
3441 * if i == 0: try to find an identifier
3442 * if i == 1: try to find any non-white string
3443 */
3444 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3445 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3446 {
3447 /*
3448 * 1. skip to start of identifier/string
3449 */
3450 col = startcol;
3451#ifdef FEAT_MBYTE
3452 if (has_mbyte)
3453 {
3454 while (ptr[col] != NUL)
3455 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003456# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 /* Stop at a ']' to evaluate "a[x]". */
3458 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3459 break;
3460# endif
3461 this_class = mb_get_class(ptr + col);
3462 if (this_class != 0 && (i == 1 || this_class != 1))
3463 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003464 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 }
3466 }
3467 else
3468#endif
3469 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01003470 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003471# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3473# endif
3474 )
3475 ++col;
3476
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003477#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 /* When starting on a ']' count it, so that we include the '['. */
3479 bn = ptr[col] == ']';
3480#endif
3481
3482 /*
3483 * 2. Back up to start of identifier/string.
3484 */
3485#ifdef FEAT_MBYTE
3486 if (has_mbyte)
3487 {
3488 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003489# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3491 this_class = mb_get_class((char_u *)"a");
3492 else
3493# endif
3494 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003495 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 {
3497 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3498 prev_class = mb_get_class(ptr + prevcol);
3499 if (this_class != prev_class
3500 && (i == 0
3501 || prev_class == 0
3502 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003503# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 && (!(find_type & FIND_EVAL)
3505 || prevcol == 0
3506 || !find_is_eval_item(ptr + prevcol, &prevcol,
3507 &bn, BACKWARD))
3508# endif
3509 )
3510 break;
3511 col = prevcol;
3512 }
3513
3514 /* If we don't want just any old string, or we've found an
3515 * identifier, stop searching. */
3516 if (this_class > 2)
3517 this_class = 2;
3518 if (!(find_type & FIND_STRING) || this_class == 2)
3519 break;
3520 }
3521 else
3522#endif
3523 {
3524 while (col > 0
3525 && ((i == 0
3526 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003527 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 && (!(find_type & FIND_IDENT)
3529 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003530#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 || ((find_type & FIND_EVAL)
3532 && col > 1
3533 && find_is_eval_item(ptr + col - 1, &col,
3534 &bn, BACKWARD))
3535#endif
3536 ))
3537 --col;
3538
3539 /* If we don't want just any old string, or we've found an
3540 * identifier, stop searching. */
3541 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3542 break;
3543 }
3544 }
3545
3546 if (ptr[col] == NUL || (i == 0 && (
3547#ifdef FEAT_MBYTE
3548 has_mbyte ? this_class != 2 :
3549#endif
3550 !vim_iswordc(ptr[col]))))
3551 {
3552 /*
3553 * didn't find an identifier or string
3554 */
3555 if (find_type & FIND_STRING)
3556 EMSG(_("E348: No string under cursor"));
3557 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003558 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 return 0;
3560 }
3561 ptr += col;
3562 *string = ptr;
3563
3564 /*
3565 * 3. Find the end if the identifier/string.
3566 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003567#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 bn = 0;
3569 startcol -= col;
3570#endif
3571 col = 0;
3572#ifdef FEAT_MBYTE
3573 if (has_mbyte)
3574 {
3575 /* Search for point of changing multibyte character class. */
3576 this_class = mb_get_class(ptr);
3577 while (ptr[col] != NUL
3578 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3579 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003580# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 || ((find_type & FIND_EVAL)
3582 && col <= (int)startcol
3583 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3584# endif
3585 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003586 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 }
3588 else
3589#endif
3590 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003591 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003592# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 || ((find_type & FIND_EVAL)
3594 && col <= (int)startcol
3595 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3596# endif
3597 )
3598 {
3599 ++col;
3600 }
3601
3602 return col;
3603}
3604
3605/*
3606 * Prepare for redo of a normal command.
3607 */
3608 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003609prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610{
3611 prep_redo(cap->oap->regname, cap->count0,
3612 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3613}
3614
3615/*
3616 * Prepare for redo of any command.
3617 * Note that only the last argument can be a multi-byte char.
3618 */
3619 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003620prep_redo(
3621 int regname,
3622 long num,
3623 int cmd1,
3624 int cmd2,
3625 int cmd3,
3626 int cmd4,
3627 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628{
3629 ResetRedobuff();
3630 if (regname != 0) /* yank from specified buffer */
3631 {
3632 AppendCharToRedobuff('"');
3633 AppendCharToRedobuff(regname);
3634 }
3635 if (num)
3636 AppendNumberToRedobuff(num);
3637
3638 if (cmd1 != NUL)
3639 AppendCharToRedobuff(cmd1);
3640 if (cmd2 != NUL)
3641 AppendCharToRedobuff(cmd2);
3642 if (cmd3 != NUL)
3643 AppendCharToRedobuff(cmd3);
3644 if (cmd4 != NUL)
3645 AppendCharToRedobuff(cmd4);
3646 if (cmd5 != NUL)
3647 AppendCharToRedobuff(cmd5);
3648}
3649
3650/*
3651 * check for operator active and clear it
3652 *
3653 * return TRUE if operator was active
3654 */
3655 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003656checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657{
3658 if (oap->op_type == OP_NOP)
3659 return FALSE;
3660 clearopbeep(oap);
3661 return TRUE;
3662}
3663
3664/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003665 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003667 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 */
3669 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003670checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003672 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 return FALSE;
3674 clearopbeep(oap);
3675 return TRUE;
3676}
3677
3678 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003679clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680{
3681 oap->op_type = OP_NOP;
3682 oap->regname = 0;
3683 oap->motion_force = NUL;
3684 oap->use_reg_one = FALSE;
3685}
3686
3687 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003688clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689{
3690 clearop(oap);
3691 beep_flush();
3692}
3693
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694/*
3695 * Remove the shift modifier from a special key.
3696 */
3697 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003698unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699{
3700 switch (cap->cmdchar)
3701 {
3702 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3703 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3704 case K_S_UP: cap->cmdchar = K_UP; break;
3705 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3706 case K_S_HOME: cap->cmdchar = K_HOME; break;
3707 case K_S_END: cap->cmdchar = K_END; break;
3708 }
3709 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3710}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003712/*
3713 * If the mode is currently displayed clear the command line or update the
3714 * command displayed.
3715 */
3716 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003717may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003718{
3719 if (mode_displayed)
3720 clear_cmdline = TRUE; /* unshow visual mode later */
3721#ifdef FEAT_CMDL_INFO
3722 else
3723 clear_showcmd();
3724#endif
3725}
3726
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3728/*
3729 * Routines for displaying a partly typed command
3730 */
3731
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003732#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733static char_u showcmd_buf[SHOWCMD_BUFLEN];
3734static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3735static int showcmd_is_clear = TRUE;
3736static int showcmd_visual = FALSE;
3737
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003738static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739
3740 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003741clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742{
3743 if (!p_sc)
3744 return;
3745
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 if (VIsual_active && !char_avail())
3747 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003748 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 long lines;
3750 colnr_T leftcol, rightcol;
3751 linenr_T top, bot;
3752
3753 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003754 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 {
3756 top = VIsual.lnum;
3757 bot = curwin->w_cursor.lnum;
3758 }
3759 else
3760 {
3761 top = curwin->w_cursor.lnum;
3762 bot = VIsual.lnum;
3763 }
3764# ifdef FEAT_FOLDING
3765 /* Include closed folds as a whole. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02003766 (void)hasFolding(top, &top, NULL);
3767 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768# endif
3769 lines = bot - top + 1;
3770
3771 if (VIsual_mode == Ctrl_V)
3772 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003773# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003774 char_u *saved_sbr = p_sbr;
3775
3776 /* Make 'sbr' empty for a moment to get the correct size. */
3777 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003778# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003780# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003781 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003782# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3784 (long)(rightcol - leftcol + 1));
3785 }
3786 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3787 sprintf((char *)showcmd_buf, "%ld", lines);
3788 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003789 {
3790 char_u *s, *e;
3791 int l;
3792 int bytes = 0;
3793 int chars = 0;
3794
3795 if (cursor_bot)
3796 {
3797 s = ml_get_pos(&VIsual);
3798 e = ml_get_cursor();
3799 }
3800 else
3801 {
3802 s = ml_get_cursor();
3803 e = ml_get_pos(&VIsual);
3804 }
3805 while ((*p_sel != 'e') ? s <= e : s < e)
3806 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003807# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003808 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003809# else
3810 l = (*s == NUL) ? 0 : 1;
3811# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003812 if (l == 0)
3813 {
3814 ++bytes;
3815 ++chars;
3816 break; /* end of line */
3817 }
3818 bytes += l;
3819 ++chars;
3820 s += l;
3821 }
3822 if (bytes == chars)
3823 sprintf((char *)showcmd_buf, "%d", chars);
3824 else
3825 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3828 showcmd_visual = TRUE;
3829 }
3830 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 {
3832 showcmd_buf[0] = NUL;
3833 showcmd_visual = FALSE;
3834
3835 /* Don't actually display something if there is nothing to clear. */
3836 if (showcmd_is_clear)
3837 return;
3838 }
3839
3840 display_showcmd();
3841}
3842
3843/*
3844 * Add 'c' to string of shown command chars.
3845 * Return TRUE if output has been written (and setcursor() has been called).
3846 */
3847 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003848add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849{
3850 char_u *p;
3851 int old_len;
3852 int extra_len;
3853 int overflow;
3854#if defined(FEAT_MOUSE)
3855 int i;
3856 static int ignore[] =
3857 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003858# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3860 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003861# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01003862 K_IGNORE, K_PS,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3864 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3865 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003866 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003868 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 0
3870 };
3871#endif
3872
Bram Moolenaar09df3122006-01-23 22:23:09 +00003873 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 return FALSE;
3875
3876 if (showcmd_visual)
3877 {
3878 showcmd_buf[0] = NUL;
3879 showcmd_visual = FALSE;
3880 }
3881
3882#if defined(FEAT_MOUSE)
3883 /* Ignore keys that are scrollbar updates and mouse clicks */
3884 if (IS_SPECIAL(c))
3885 for (i = 0; ignore[i] != 0; ++i)
3886 if (ignore[i] == c)
3887 return FALSE;
3888#endif
3889
3890 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003891 if (*p == ' ')
3892 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 old_len = (int)STRLEN(showcmd_buf);
3894 extra_len = (int)STRLEN(p);
3895 overflow = old_len + extra_len - SHOWCMD_COLS;
3896 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003897 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3898 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 STRCAT(showcmd_buf, p);
3900
3901 if (char_avail())
3902 return FALSE;
3903
3904 display_showcmd();
3905
3906 return TRUE;
3907}
3908
3909 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003910add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911{
3912 if (!add_to_showcmd(c))
3913 setcursor();
3914}
3915
3916/*
3917 * Delete 'len' characters from the end of the shown command.
3918 */
3919 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003920del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921{
3922 int old_len;
3923
3924 if (!p_sc)
3925 return;
3926
3927 old_len = (int)STRLEN(showcmd_buf);
3928 if (len > old_len)
3929 len = old_len;
3930 showcmd_buf[old_len - len] = NUL;
3931
3932 if (!char_avail())
3933 display_showcmd();
3934}
3935
3936/*
3937 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3938 * something and there is a partial mapping.
3939 */
3940 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003941push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942{
3943 if (p_sc)
3944 STRCPY(old_showcmd_buf, showcmd_buf);
3945}
3946
3947 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003948pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949{
3950 if (!p_sc)
3951 return;
3952
3953 STRCPY(showcmd_buf, old_showcmd_buf);
3954
3955 display_showcmd();
3956}
3957
3958 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003959display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960{
3961 int len;
3962
3963 cursor_off();
3964
3965 len = (int)STRLEN(showcmd_buf);
3966 if (len == 0)
3967 showcmd_is_clear = TRUE;
3968 else
3969 {
3970 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3971 showcmd_is_clear = FALSE;
3972 }
3973
3974 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003975 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3976 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 */
3978 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3979
3980 setcursor(); /* put cursor back where it belongs */
3981}
3982#endif
3983
3984#ifdef FEAT_SCROLLBIND
3985/*
3986 * When "check" is FALSE, prepare for commands that scroll the window.
3987 * When "check" is TRUE, take care of scroll-binding after the window has
3988 * scrolled. Called from normal_cmd() and edit().
3989 */
3990 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003991do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992{
3993 static win_T *old_curwin = NULL;
3994 static linenr_T old_topline = 0;
3995#ifdef FEAT_DIFF
3996 static int old_topfill = 0;
3997#endif
3998 static buf_T *old_buf = NULL;
3999 static colnr_T old_leftcol = 0;
4000
4001 if (check && curwin->w_p_scb)
4002 {
4003 /* If a ":syncbind" command was just used, don't scroll, only reset
4004 * the values. */
4005 if (did_syncbind)
4006 did_syncbind = FALSE;
4007 else if (curwin == old_curwin)
4008 {
4009 /*
4010 * Synchronize other windows, as necessary according to
4011 * 'scrollbind'. Don't do this after an ":edit" command, except
4012 * when 'diff' is set.
4013 */
4014 if ((curwin->w_buffer == old_buf
4015#ifdef FEAT_DIFF
4016 || curwin->w_p_diff
4017#endif
4018 )
4019 && (curwin->w_topline != old_topline
4020#ifdef FEAT_DIFF
4021 || curwin->w_topfill != old_topfill
4022#endif
4023 || curwin->w_leftcol != old_leftcol))
4024 {
4025 check_scrollbind(curwin->w_topline - old_topline,
4026 (long)(curwin->w_leftcol - old_leftcol));
4027 }
4028 }
4029 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4030 {
4031 /*
4032 * When switching between windows, make sure that the relative
4033 * vertical offset is valid for the new window. The relative
4034 * offset is invalid whenever another 'scrollbind' window has
4035 * scrolled to a point that would force the current window to
4036 * scroll past the beginning or end of its buffer. When the
4037 * resync is performed, some of the other 'scrollbind' windows may
4038 * need to jump so that the current window's relative position is
4039 * visible on-screen.
4040 */
4041 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4042 }
4043 curwin->w_scbind_pos = curwin->w_topline;
4044 }
4045
4046 old_curwin = curwin;
4047 old_topline = curwin->w_topline;
4048#ifdef FEAT_DIFF
4049 old_topfill = curwin->w_topfill;
4050#endif
4051 old_buf = curwin->w_buffer;
4052 old_leftcol = curwin->w_leftcol;
4053}
4054
4055/*
4056 * Synchronize any windows that have "scrollbind" set, based on the
4057 * number of rows by which the current window has changed
4058 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4059 */
4060 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004061check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062{
4063 int want_ver;
4064 int want_hor;
4065 win_T *old_curwin = curwin;
4066 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 int old_VIsual_select = VIsual_select;
4068 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 colnr_T tgt_leftcol = curwin->w_leftcol;
4070 long topline;
4071 long y;
4072
4073 /*
4074 * check 'scrollopt' string for vertical and horizontal scroll options
4075 */
4076 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4077#ifdef FEAT_DIFF
4078 want_ver |= old_curwin->w_p_diff;
4079#endif
4080 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4081
4082 /*
4083 * loop through the scrollbound windows and scroll accordingly
4084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02004086 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 {
4088 curbuf = curwin->w_buffer;
4089 /* skip original window and windows with 'noscrollbind' */
4090 if (curwin != old_curwin && curwin->w_p_scb)
4091 {
4092 /*
4093 * do the vertical scroll
4094 */
4095 if (want_ver)
4096 {
4097#ifdef FEAT_DIFF
4098 if (old_curwin->w_p_diff && curwin->w_p_diff)
4099 {
4100 diff_set_topline(old_curwin, curwin);
4101 }
4102 else
4103#endif
4104 {
4105 curwin->w_scbind_pos += topline_diff;
4106 topline = curwin->w_scbind_pos;
4107 if (topline > curbuf->b_ml.ml_line_count)
4108 topline = curbuf->b_ml.ml_line_count;
4109 if (topline < 1)
4110 topline = 1;
4111
4112 y = topline - curwin->w_topline;
4113 if (y > 0)
4114 scrollup(y, FALSE);
4115 else
4116 scrolldown(-y, FALSE);
4117 }
4118
4119 redraw_later(VALID);
4120 cursor_correct();
4121#ifdef FEAT_WINDOWS
4122 curwin->w_redr_status = TRUE;
4123#endif
4124 }
4125
4126 /*
4127 * do the horizontal scroll
4128 */
4129 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4130 {
4131 curwin->w_leftcol = tgt_leftcol;
4132 leftcol_changed();
4133 }
4134 }
4135 }
4136
4137 /*
4138 * reset current-window
4139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 VIsual_select = old_VIsual_select;
4141 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 curwin = old_curwin;
4143 curbuf = old_curbuf;
4144}
4145#endif /* #ifdef FEAT_SCROLLBIND */
4146
4147/*
4148 * Command character that's ignored.
4149 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004150 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004153nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004155 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156}
4157
4158/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004159 * Command character that doesn't do anything, but unlike nv_ignore() does
4160 * start edit(). Used for "startinsert" executed while starting up.
4161 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004162 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004163nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00004164{
4165}
4166
4167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 * Command character doesn't exist.
4169 */
4170 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004171nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172{
4173 clearopbeep(cap->oap);
4174}
4175
4176/*
4177 * <Help> and <F1> commands.
4178 */
4179 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004180nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181{
4182 if (!checkclearopq(cap->oap))
4183 ex_help(NULL);
4184}
4185
4186/*
4187 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4188 */
4189 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004190nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191{
Bram Moolenaard79e5502016-01-10 22:13:02 +01004192 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004193 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01004194 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01004195 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
4196 op_addsub(cap->oap, cap->count1, cap->arg);
4197 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004198 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01004199 else if (VIsual_active)
4200 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004201 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01004202 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203}
4204
4205/*
4206 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4207 */
4208 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004209nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210{
4211 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004212 {
4213#ifdef FEAT_WINDOWS
4214 if (mod_mask & MOD_MASK_CTRL)
4215 {
4216 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4217 if (cap->arg == BACKWARD)
4218 goto_tabpage(-(int)cap->count1);
4219 else
4220 goto_tabpage((int)cap->count0);
4221 }
4222 else
4223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226}
4227
4228/*
4229 * Implementation of "gd" and "gD" command.
4230 */
4231 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004232nv_gd(
4233 oparg_T *oap,
4234 int nchar,
4235 int thisblock) /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236{
4237 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 char_u *ptr;
4239
Bram Moolenaard9d30582005-05-18 22:10:28 +00004240 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02004241 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
4242 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004244#ifdef FEAT_FOLDING
4245 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4246 foldOpenCursor();
4247#endif
4248}
4249
4250/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02004251 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
4252 * otherwise.
4253 */
4254 static int
4255is_ident(char_u *line, int offset)
4256{
4257 int i;
4258 int incomment = FALSE;
4259 int instring = 0;
4260 int prev = 0;
4261
4262 for (i = 0; i < offset && line[i] != NUL; i++)
4263 {
4264 if (instring != 0)
4265 {
4266 if (prev != '\\' && line[i] == instring)
4267 instring = 0;
4268 }
4269 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
4270 {
4271 instring = line[i];
4272 }
4273 else
4274 {
4275 if (incomment)
4276 {
4277 if (prev == '*' && line[i] == '/')
4278 incomment = FALSE;
4279 }
4280 else if (prev == '/' && line[i] == '*')
4281 {
4282 incomment = TRUE;
4283 }
4284 else if (prev == '/' && line[i] == '/')
4285 {
4286 return FALSE;
4287 }
4288 }
4289
4290 prev = line[i];
4291 }
4292
4293 return incomment == FALSE && instring == 0;
4294}
4295
4296/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004297 * Search for variable declaration of "ptr[len]".
4298 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4299 * current file ("gD").
4300 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004301 * Return FAIL when not found.
4302 */
4303 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004304find_decl(
4305 char_u *ptr,
4306 int len,
4307 int locally,
4308 int thisblock,
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004309 int flags_arg) /* flags passed to searchit() */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004310{
4311 char_u *pat;
4312 pos_T old_pos;
4313 pos_T par_pos;
4314 pos_T found_pos;
4315 int t;
4316 int save_p_ws;
4317 int save_p_scs;
4318 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004319 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004320 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004321 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004322
4323 if ((pat = alloc(len + 7)) == NULL)
4324 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004325
4326 /* Put "\V" before the pattern to avoid that the special meaning of "."
4327 * and "~" causes trouble. */
4328 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4329 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 old_pos = curwin->w_cursor;
4331 save_p_ws = p_ws;
4332 save_p_scs = p_scs;
4333 p_ws = FALSE; /* don't wrap around end of file now */
4334 p_scs = FALSE; /* don't switch ignorecase off now */
4335
4336 /*
4337 * With "gD" go to line 1.
4338 * With "gd" Search back for the start of the current function, then go
4339 * back until a blank line. If this fails go to line 1.
4340 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004341 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 {
4343 setpcmark(); /* Set in findpar() otherwise */
4344 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004345 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 }
4347 else
4348 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004349 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4351 --curwin->w_cursor.lnum;
4352 }
4353 curwin->w_cursor.col = 0;
4354
4355 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004356 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004357 for (;;)
4358 {
Bram Moolenaar226630a2016-10-08 19:21:31 +02004359 valid = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004360 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004361 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004362 if (curwin->w_cursor.lnum >= old_pos.lnum)
4363 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004364
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004365 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004366 {
4367 pos_T *pos;
4368
4369 /* Check that the block the match is in doesn't end before the
4370 * position where we started the search from. */
4371 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4372 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4373 && pos->lnum < old_pos.lnum)
4374 continue;
4375 }
4376
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004377 if (t == FAIL)
4378 {
4379 /* If we previously found a valid position, use it. */
4380 if (found_pos.lnum != 0)
4381 {
4382 curwin->w_cursor = found_pos;
4383 t = OK;
4384 }
4385 break;
4386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004388 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004389 {
4390 /* Ignore this line, continue at start of next line. */
4391 ++curwin->w_cursor.lnum;
4392 curwin->w_cursor.col = 0;
4393 continue;
4394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395#endif
Bram Moolenaar226630a2016-10-08 19:21:31 +02004396 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
4397
4398 /* If the current position is not a valid identifier and a previous
4399 * match is present, favor that one instead. */
4400 if (!valid && found_pos.lnum != 0)
4401 {
4402 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004403 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004404 }
4405
4406 /* Global search: use first valid match found */
4407 if (valid && !locally)
4408 break;
4409 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004410 {
4411 /* If we previously found a valid position, use it. */
4412 if (found_pos.lnum != 0)
4413 curwin->w_cursor = found_pos;
4414 break;
4415 }
4416
Bram Moolenaar226630a2016-10-08 19:21:31 +02004417 /* For finding a local variable and the match is before the "{" or
4418 * inside a comment, continue searching. For K&R style function
4419 * declarations this skips the function header without types. */
4420 if (!valid)
4421 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004422 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02004423 }
4424 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02004425 found_pos = curwin->w_cursor;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004426 /* Remove SEARCH_START from flags to avoid getting stuck at one
4427 * position. */
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004428 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004430
4431 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004433 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 curwin->w_cursor = old_pos;
4435 }
4436 else
4437 {
4438 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 /* "n" searches forward now */
4440 reset_search_dir();
4441 }
4442
4443 vim_free(pat);
4444 p_ws = save_p_ws;
4445 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004446
4447 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448}
4449
4450/*
4451 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4452 * lines rather than lines in the file.
4453 * 'dist' must be positive.
4454 *
4455 * Return OK if able to move cursor, FAIL otherwise.
4456 */
4457 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004458nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459{
4460 int linelen = linetabsize(ml_get_curline());
4461 int retval = OK;
4462 int atend = FALSE;
4463 int n;
4464 int col_off1; /* margin offset for first screen line */
4465 int col_off2; /* margin offset for wrapped screen line */
4466 int width1; /* text width for first screen line */
4467 int width2; /* test width for wrapped screen line */
4468
4469 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004470 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471
4472 col_off1 = curwin_col_off();
4473 col_off2 = col_off1 - curwin_col_off2();
4474 width1 = W_WIDTH(curwin) - col_off1;
4475 width2 = W_WIDTH(curwin) - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01004476 if (width2 == 0)
4477 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004479#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 if (curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004482 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 /*
4484 * Instead of sticking at the last character of the buffer line we
4485 * try to stick in the last column of the screen.
4486 */
4487 if (curwin->w_curswant == MAXCOL)
4488 {
4489 atend = TRUE;
4490 validate_virtcol();
4491 if (width1 <= 0)
4492 curwin->w_curswant = 0;
4493 else
4494 {
4495 curwin->w_curswant = width1 - 1;
4496 if (curwin->w_virtcol > curwin->w_curswant)
4497 curwin->w_curswant += ((curwin->w_virtcol
4498 - curwin->w_curswant - 1) / width2 + 1) * width2;
4499 }
4500 }
4501 else
4502 {
4503 if (linelen > width1)
4504 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4505 else
4506 n = width1;
4507 if (curwin->w_curswant > (colnr_T)n + 1)
4508 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4509 * width2;
4510 }
4511
4512 while (dist--)
4513 {
4514 if (dir == BACKWARD)
4515 {
4516 if ((long)curwin->w_curswant >= width2)
4517 /* move back within line */
4518 curwin->w_curswant -= width2;
4519 else
4520 {
4521 /* to previous line */
4522 if (curwin->w_cursor.lnum == 1)
4523 {
4524 retval = FAIL;
4525 break;
4526 }
4527 --curwin->w_cursor.lnum;
4528#ifdef FEAT_FOLDING
4529 /* Move to the start of a closed fold. Don't do that when
4530 * 'foldopen' contains "all": it will open in a moment. */
4531 if (!(fdo_flags & FDO_ALL))
4532 (void)hasFolding(curwin->w_cursor.lnum,
4533 &curwin->w_cursor.lnum, NULL);
4534#endif
4535 linelen = linetabsize(ml_get_curline());
4536 if (linelen > width1)
4537 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4538 + 1) * width2;
4539 }
4540 }
4541 else /* dir == FORWARD */
4542 {
4543 if (linelen > width1)
4544 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4545 else
4546 n = width1;
4547 if (curwin->w_curswant + width2 < (colnr_T)n)
4548 /* move forward within line */
4549 curwin->w_curswant += width2;
4550 else
4551 {
4552 /* to next line */
4553#ifdef FEAT_FOLDING
4554 /* Move to the end of a closed fold. */
4555 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4556 &curwin->w_cursor.lnum);
4557#endif
4558 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4559 {
4560 retval = FAIL;
4561 break;
4562 }
4563 curwin->w_cursor.lnum++;
4564 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004565 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 }
4567 }
4568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004571 if (virtual_active() && atend)
4572 coladvance(MAXCOL);
4573 else
4574 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575
4576#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4577 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4578 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004579 colnr_T virtcol;
4580
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 /*
4582 * Check for landing on a character that got split at the end of the
4583 * last line. We want to advance a screenline, not end up in the same
4584 * screenline or move two screenlines.
4585 */
4586 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004587 virtcol = curwin->w_virtcol;
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004588# if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004589 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4590 virtcol -= vim_strsize(p_sbr);
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004591# endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004592
4593 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 && (curwin->w_curswant < (colnr_T)width1
4595 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4596 : ((curwin->w_curswant - width1) % width2
4597 > (colnr_T)width2 / 2)))
4598 --curwin->w_cursor.col;
4599 }
4600#endif
4601
4602 if (atend)
4603 curwin->w_curswant = MAXCOL; /* stick in the last column */
4604
4605 return retval;
4606}
4607
4608#ifdef FEAT_MOUSE
4609/*
4610 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4611 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004612 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4613 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 */
4615 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004616nv_mousescroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617{
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004618# ifdef FEAT_WINDOWS
Bram Moolenaara5792f52005-11-23 21:25:05 +00004619 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004621 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 {
4623 int row, col;
4624
4625 row = mouse_row;
4626 col = mouse_col;
4627
4628 /* find the window at the pointer coordinates */
4629 curwin = mouse_find_win(&row, &col);
4630 curbuf = curwin->w_buffer;
4631 }
4632# endif
4633
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004634 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004636 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4637 {
4638 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4639 }
4640 else
4641 {
4642 cap->count1 = 3;
4643 cap->count0 = 3;
4644 nv_scroll_line(cap);
4645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004647# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 else
4649 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004650 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4651 if (!curwin->w_p_wrap)
4652 {
4653 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004654
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004655 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4656 step = W_WIDTH(curwin);
4657 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4658 if (val < 0)
4659 val = 0;
4660
4661 gui_do_horiz_scroll(val, TRUE);
4662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004664# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004666# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 curwin->w_redr_status = TRUE;
4668
4669 curwin = old_curwin;
4670 curbuf = curwin->w_buffer;
4671# endif
4672}
4673
4674/*
4675 * Mouse clicks and drags.
4676 */
4677 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004678nv_mouse(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679{
4680 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4681}
4682#endif
4683
4684/*
4685 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4686 * cap->arg must be TRUE for CTRL-E.
4687 */
4688 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004689nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690{
4691 if (!checkclearop(cap->oap))
4692 scroll_redraw(cap->arg, cap->count1);
4693}
4694
4695/*
4696 * Scroll "count" lines up or down, and redraw.
4697 */
4698 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004699scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700{
4701 linenr_T prev_topline = curwin->w_topline;
4702#ifdef FEAT_DIFF
4703 int prev_topfill = curwin->w_topfill;
4704#endif
4705 linenr_T prev_lnum = curwin->w_cursor.lnum;
4706
4707 if (up)
4708 scrollup(count, TRUE);
4709 else
4710 scrolldown(count, TRUE);
4711 if (p_so)
4712 {
4713 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4714 * valid, otherwise the screen jumps back at the end of the file. */
4715 cursor_correct();
4716 check_cursor_moved(curwin);
4717 curwin->w_valid |= VALID_TOPLINE;
4718
4719 /* If moved back to where we were, at least move the cursor, otherwise
4720 * we get stuck at one position. Don't move the cursor up if the
4721 * first line of the buffer is already on the screen */
4722 while (curwin->w_topline == prev_topline
4723#ifdef FEAT_DIFF
4724 && curwin->w_topfill == prev_topfill
4725#endif
4726 )
4727 {
4728 if (up)
4729 {
4730 if (curwin->w_cursor.lnum > prev_lnum
4731 || cursor_down(1L, FALSE) == FAIL)
4732 break;
4733 }
4734 else
4735 {
4736 if (curwin->w_cursor.lnum < prev_lnum
4737 || prev_topline == 1L
4738 || cursor_up(1L, FALSE) == FAIL)
4739 break;
4740 }
4741 /* Mark w_topline as valid, otherwise the screen jumps back at the
4742 * end of the file. */
4743 check_cursor_moved(curwin);
4744 curwin->w_valid |= VALID_TOPLINE;
4745 }
4746 }
4747 if (curwin->w_cursor.lnum != prev_lnum)
4748 coladvance(curwin->w_curswant);
4749 redraw_later(VALID);
4750}
4751
4752/*
4753 * Commands that start with "z".
4754 */
4755 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004756nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757{
4758 long n;
4759 colnr_T col;
4760 int nchar = cap->nchar;
4761#ifdef FEAT_FOLDING
4762 long old_fdl = curwin->w_p_fdl;
4763 int old_fen = curwin->w_p_fen;
4764#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004765#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004766 int undo = FALSE;
4767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768
4769 if (VIM_ISDIGIT(nchar))
4770 {
4771 /*
4772 * "z123{nchar}": edit the count before obtaining {nchar}
4773 */
4774 if (checkclearop(cap->oap))
4775 return;
4776 n = nchar - '0';
4777 for (;;)
4778 {
4779#ifdef USE_ON_FLY_SCROLL
4780 dont_scroll = TRUE; /* disallow scrolling here */
4781#endif
4782 ++no_mapping;
4783 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004784 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 --no_mapping;
4787 --allow_keys;
4788#ifdef FEAT_CMDL_INFO
4789 (void)add_to_showcmd(nchar);
4790#endif
4791 if (nchar == K_DEL || nchar == K_KDEL)
4792 n /= 10;
4793 else if (VIM_ISDIGIT(nchar))
4794 n = n * 10 + (nchar - '0');
4795 else if (nchar == CAR)
4796 {
4797#ifdef FEAT_GUI
4798 need_mouse_correct = TRUE;
4799#endif
4800 win_setheight((int)n);
4801 break;
4802 }
4803 else if (nchar == 'l'
4804 || nchar == 'h'
4805 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004806 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 {
4808 cap->count1 = n ? n * cap->count1 : cap->count1;
4809 goto dozet;
4810 }
4811 else
4812 {
4813 clearopbeep(cap->oap);
4814 break;
4815 }
4816 }
4817 cap->oap->op_type = OP_NOP;
4818 return;
4819 }
4820
4821dozet:
4822 if (
4823#ifdef FEAT_FOLDING
4824 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4825 * and "zC" only in Visual mode. "zj" and "zk" are motion
4826 * commands. */
4827 cap->nchar != 'f' && cap->nchar != 'F'
4828 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4829 && cap->nchar != 'j' && cap->nchar != 'k'
4830 &&
4831#endif
4832 checkclearop(cap->oap))
4833 return;
4834
4835 /*
4836 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4837 * If line number given, set cursor.
4838 */
4839 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4840 && cap->count0
4841 && cap->count0 != curwin->w_cursor.lnum)
4842 {
4843 setpcmark();
4844 if (cap->count0 > curbuf->b_ml.ml_line_count)
4845 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4846 else
4847 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004848 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 }
4850
4851 switch (nchar)
4852 {
4853 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4854 case '+':
4855 if (cap->count0 == 0)
4856 {
4857 /* No count given: put cursor at the line below screen */
4858 validate_botline(); /* make sure w_botline is valid */
4859 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4860 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4861 else
4862 curwin->w_cursor.lnum = curwin->w_botline;
4863 }
4864 /* FALLTHROUGH */
4865 case NL:
4866 case CAR:
4867 case K_KENTER:
4868 beginline(BL_WHITE | BL_FIX);
4869 /* FALLTHROUGH */
4870
4871 case 't': scroll_cursor_top(0, TRUE);
4872 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004873 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 break;
4875
4876 /* "z." and "zz": put cursor in middle of screen */
4877 case '.': beginline(BL_WHITE | BL_FIX);
4878 /* FALLTHROUGH */
4879
4880 case 'z': scroll_cursor_halfway(TRUE);
4881 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004882 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 break;
4884
4885 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4886 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4887 * when <count> is at bottom of window, and puts that one at
4888 * bottom of window. */
4889 if (cap->count0 != 0)
4890 {
4891 scroll_cursor_bot(0, TRUE);
4892 curwin->w_cursor.lnum = curwin->w_topline;
4893 }
4894 else if (curwin->w_topline == 1)
4895 curwin->w_cursor.lnum = 1;
4896 else
4897 curwin->w_cursor.lnum = curwin->w_topline - 1;
4898 /* FALLTHROUGH */
4899 case '-':
4900 beginline(BL_WHITE | BL_FIX);
4901 /* FALLTHROUGH */
4902
4903 case 'b': scroll_cursor_bot(0, TRUE);
4904 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004905 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 break;
4907
4908 /* "zH" - scroll screen right half-page */
4909 case 'H':
4910 cap->count1 *= W_WIDTH(curwin) / 2;
4911 /* FALLTHROUGH */
4912
4913 /* "zh" - scroll screen to the right */
4914 case 'h':
4915 case K_LEFT:
4916 if (!curwin->w_p_wrap)
4917 {
4918 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4919 curwin->w_leftcol = 0;
4920 else
4921 curwin->w_leftcol -= (colnr_T)cap->count1;
4922 leftcol_changed();
4923 }
4924 break;
4925
4926 /* "zL" - scroll screen left half-page */
4927 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4928 /* FALLTHROUGH */
4929
4930 /* "zl" - scroll screen to the left */
4931 case 'l':
4932 case K_RIGHT:
4933 if (!curwin->w_p_wrap)
4934 {
4935 /* scroll the window left */
4936 curwin->w_leftcol += (colnr_T)cap->count1;
4937 leftcol_changed();
4938 }
4939 break;
4940
4941 /* "zs" - scroll screen, cursor at the start */
4942 case 's': if (!curwin->w_p_wrap)
4943 {
4944#ifdef FEAT_FOLDING
4945 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4946 col = 0; /* like the cursor is in col 0 */
4947 else
4948#endif
4949 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4950 if ((long)col > p_siso)
4951 col -= p_siso;
4952 else
4953 col = 0;
4954 if (curwin->w_leftcol != col)
4955 {
4956 curwin->w_leftcol = col;
4957 redraw_later(NOT_VALID);
4958 }
4959 }
4960 break;
4961
4962 /* "ze" - scroll screen, cursor at the end */
4963 case 'e': if (!curwin->w_p_wrap)
4964 {
4965#ifdef FEAT_FOLDING
4966 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4967 col = 0; /* like the cursor is in col 0 */
4968 else
4969#endif
4970 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4971 n = W_WIDTH(curwin) - curwin_col_off();
4972 if ((long)col + p_siso < n)
4973 col = 0;
4974 else
4975 col = col + p_siso - n + 1;
4976 if (curwin->w_leftcol != col)
4977 {
4978 curwin->w_leftcol = col;
4979 redraw_later(NOT_VALID);
4980 }
4981 }
4982 break;
4983
4984#ifdef FEAT_FOLDING
4985 /* "zF": create fold command */
4986 /* "zf": create fold operator */
4987 case 'F':
4988 case 'f': if (foldManualAllowed(TRUE))
4989 {
4990 cap->nchar = 'f';
4991 nv_operator(cap);
4992 curwin->w_p_fen = TRUE;
4993
4994 /* "zF" is like "zfzf" */
4995 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4996 {
4997 nv_operator(cap);
4998 finish_op = TRUE;
4999 }
5000 }
5001 else
5002 clearopbeep(cap->oap);
5003 break;
5004
5005 /* "zd": delete fold at cursor */
5006 /* "zD": delete fold at cursor recursively */
5007 case 'd':
5008 case 'D': if (foldManualAllowed(FALSE))
5009 {
5010 if (VIsual_active)
5011 nv_operator(cap);
5012 else
5013 deleteFold(curwin->w_cursor.lnum,
5014 curwin->w_cursor.lnum, nchar == 'D', FALSE);
5015 }
5016 break;
5017
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005018 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 case 'E': if (foldmethodIsManual(curwin))
5020 {
5021 clearFolding(curwin);
5022 changed_window_setting();
5023 }
5024 else if (foldmethodIsMarker(curwin))
5025 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5026 TRUE, FALSE);
5027 else
5028 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5029 break;
5030
5031 /* "zn": fold none: reset 'foldenable' */
5032 case 'n': curwin->w_p_fen = FALSE;
5033 break;
5034
5035 /* "zN": fold Normal: set 'foldenable' */
5036 case 'N': curwin->w_p_fen = TRUE;
5037 break;
5038
5039 /* "zi": invert folding: toggle 'foldenable' */
5040 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5041 break;
5042
5043 /* "za": open closed fold or close open fold at cursor */
5044 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5045 openFold(curwin->w_cursor.lnum, cap->count1);
5046 else
5047 {
5048 closeFold(curwin->w_cursor.lnum, cap->count1);
5049 curwin->w_p_fen = TRUE;
5050 }
5051 break;
5052
5053 /* "zA": open fold at cursor recursively */
5054 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5055 openFoldRecurse(curwin->w_cursor.lnum);
5056 else
5057 {
5058 closeFoldRecurse(curwin->w_cursor.lnum);
5059 curwin->w_p_fen = TRUE;
5060 }
5061 break;
5062
5063 /* "zo": open fold at cursor or Visual area */
5064 case 'o': if (VIsual_active)
5065 nv_operator(cap);
5066 else
5067 openFold(curwin->w_cursor.lnum, cap->count1);
5068 break;
5069
5070 /* "zO": open fold recursively */
5071 case 'O': if (VIsual_active)
5072 nv_operator(cap);
5073 else
5074 openFoldRecurse(curwin->w_cursor.lnum);
5075 break;
5076
5077 /* "zc": close fold at cursor or Visual area */
5078 case 'c': if (VIsual_active)
5079 nv_operator(cap);
5080 else
5081 closeFold(curwin->w_cursor.lnum, cap->count1);
5082 curwin->w_p_fen = TRUE;
5083 break;
5084
5085 /* "zC": close fold recursively */
5086 case 'C': if (VIsual_active)
5087 nv_operator(cap);
5088 else
5089 closeFoldRecurse(curwin->w_cursor.lnum);
5090 curwin->w_p_fen = TRUE;
5091 break;
5092
5093 /* "zv": open folds at the cursor */
5094 case 'v': foldOpenCursor();
5095 break;
5096
5097 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5098 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005099 curwin->w_foldinvalid = TRUE; /* recompute folds */
5100 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 foldOpenCursor();
5102 break;
5103
5104 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5105 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005106 curwin->w_foldinvalid = TRUE; /* recompute folds */
5107 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 break;
5109
5110 /* "zm": fold more */
5111 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005112 {
5113 curwin->w_p_fdl -= cap->count1;
5114 if (curwin->w_p_fdl < 0)
5115 curwin->w_p_fdl = 0;
5116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 old_fdl = -1; /* force an update */
5118 curwin->w_p_fen = TRUE;
5119 break;
5120
5121 /* "zM": close all folds */
5122 case 'M': curwin->w_p_fdl = 0;
5123 old_fdl = -1; /* force an update */
5124 curwin->w_p_fen = TRUE;
5125 break;
5126
5127 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005128 case 'r': curwin->w_p_fdl += cap->count1;
5129 {
5130 int d = getDeepestNesting();
5131
5132 if (curwin->w_p_fdl >= d)
5133 curwin->w_p_fdl = d;
5134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 break;
5136
5137 /* "zR": open all folds */
5138 case 'R': curwin->w_p_fdl = getDeepestNesting();
5139 old_fdl = -1; /* force an update */
5140 break;
5141
5142 case 'j': /* "zj" move to next fold downwards */
5143 case 'k': /* "zk" move to next fold upwards */
5144 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5145 cap->count1) == FAIL)
5146 clearopbeep(cap->oap);
5147 break;
5148
5149#endif /* FEAT_FOLDING */
5150
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005151#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005152 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5153 ++no_mapping;
5154 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005155 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005156 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005157 --no_mapping;
5158 --allow_keys;
5159#ifdef FEAT_CMDL_INFO
5160 (void)add_to_showcmd(nchar);
5161#endif
5162 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5163 {
5164 clearopbeep(cap->oap);
5165 break;
5166 }
5167 undo = TRUE;
5168 /*FALLTHROUGH*/
5169
Bram Moolenaarb765d632005-06-07 21:00:02 +00005170 case 'g': /* "zg": add good word to word list */
5171 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005172 case 'G': /* "zG": add good word to temp word list */
5173 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005174 {
5175 char_u *ptr = NULL;
5176 int len;
5177
5178 if (checkclearop(cap->oap))
5179 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005180 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5181 == FAIL)
5182 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005183 if (ptr == NULL)
5184 {
5185 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005186
Bram Moolenaar134bf072013-09-25 18:54:24 +02005187 /* Find bad word under the cursor. When 'spell' is
5188 * off this fails and find_ident_under_cursor() is
5189 * used below. */
5190 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005191 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005192 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005193 if (len != 0 && curwin->w_cursor.col <= pos.col)
5194 ptr = ml_get_pos(&curwin->w_cursor);
5195 curwin->w_cursor = pos;
5196 }
5197
Bram Moolenaarb765d632005-06-07 21:00:02 +00005198 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5199 FIND_IDENT)) == 0)
5200 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005201 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005202 (nchar == 'G' || nchar == 'W')
5203 ? 0 : (int)cap->count1,
5204 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005205 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005206 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005207
Bram Moolenaar43abc522005-12-10 20:15:02 +00005208 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005209 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005210 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005211 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005212#endif
5213
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 default: clearopbeep(cap->oap);
5215 }
5216
5217#ifdef FEAT_FOLDING
5218 /* Redraw when 'foldenable' changed */
5219 if (old_fen != curwin->w_p_fen)
5220 {
5221# ifdef FEAT_DIFF
5222 win_T *wp;
5223
5224 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5225 {
5226 /* Adjust 'foldenable' in diff-synced windows. */
5227 FOR_ALL_WINDOWS(wp)
5228 {
5229 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5230 {
5231 wp->w_p_fen = curwin->w_p_fen;
5232 changed_window_setting_win(wp);
5233 }
5234 }
5235 }
5236# endif
5237 changed_window_setting();
5238 }
5239
5240 /* Redraw when 'foldlevel' changed. */
5241 if (old_fdl != curwin->w_p_fdl)
5242 newFoldLevel();
5243#endif
5244}
5245
5246#ifdef FEAT_GUI
5247/*
5248 * Vertical scrollbar movement.
5249 */
5250 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005251nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252{
5253 if (cap->oap->op_type != OP_NOP)
5254 clearopbeep(cap->oap);
5255
5256 /* Even if an operator was pending, we still want to scroll */
5257 gui_do_scroll();
5258}
5259
5260/*
5261 * Horizontal scrollbar movement.
5262 */
5263 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005264nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265{
5266 if (cap->oap->op_type != OP_NOP)
5267 clearopbeep(cap->oap);
5268
5269 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005270 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271}
5272#endif
5273
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005274#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005275/*
5276 * Click in GUI tab.
5277 */
5278 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005279nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005280{
5281 if (cap->oap->op_type != OP_NOP)
5282 clearopbeep(cap->oap);
5283
5284 /* Even if an operator was pending, we still want to jump tabs. */
5285 goto_tabpage(current_tab);
5286}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005287
5288/*
5289 * Selected item in tab line menu.
5290 */
5291 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005292nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005293{
5294 if (cap->oap->op_type != OP_NOP)
5295 clearopbeep(cap->oap);
5296
5297 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005298 handle_tabmenu();
5299}
5300
5301/*
5302 * Handle selecting an item of the GUI tab line menu.
5303 * Used in Normal and Insert mode.
5304 */
5305 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005306handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005307{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005308 switch (current_tabmenu)
5309 {
5310 case TABLINE_MENU_CLOSE:
5311 if (current_tab == 0)
5312 do_cmdline_cmd((char_u *)"tabclose");
5313 else
5314 {
5315 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5316 current_tab);
5317 do_cmdline_cmd(IObuff);
5318 }
5319 break;
5320
5321 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005322 if (current_tab == 0)
5323 do_cmdline_cmd((char_u *)"$tabnew");
5324 else
5325 {
5326 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5327 current_tab - 1);
5328 do_cmdline_cmd(IObuff);
5329 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005330 break;
5331
5332 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005333 if (current_tab == 0)
5334 do_cmdline_cmd((char_u *)"browse $tabnew");
5335 else
5336 {
5337 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5338 current_tab - 1);
5339 do_cmdline_cmd(IObuff);
5340 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005341 break;
5342 }
5343}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005344#endif
5345
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346/*
5347 * "Q" command.
5348 */
5349 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005350nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351{
5352 /*
5353 * Ignore 'Q' in Visual mode, just give a beep.
5354 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005356 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005357 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 do_exmode(FALSE);
5359}
5360
5361/*
5362 * Handle a ":" command.
5363 */
5364 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005365nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366{
5367 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005368 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 if (VIsual_active)
5371 nv_operator(cap);
5372 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 {
5374 if (cap->oap->op_type != OP_NOP)
5375 {
5376 /* Using ":" as a movement is characterwise exclusive. */
5377 cap->oap->motion_type = MCHAR;
5378 cap->oap->inclusive = FALSE;
5379 }
5380 else if (cap->count0)
5381 {
5382 /* translate "count:" into ":.,.+(count - 1)" */
5383 stuffcharReadbuff('.');
5384 if (cap->count0 > 1)
5385 {
5386 stuffReadbuff((char_u *)",.+");
5387 stuffnumReadbuff((long)cap->count0 - 1L);
5388 }
5389 }
5390
5391 /* When typing, don't type below an old message */
5392 if (KeyTyped)
5393 compute_cmdrow();
5394
5395 old_p_im = p_im;
5396
5397 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005398 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5400
5401 /* If 'insertmode' changed, enter or exit Insert mode */
5402 if (p_im != old_p_im)
5403 {
5404 if (p_im)
5405 restart_edit = 'i';
5406 else
5407 restart_edit = 0;
5408 }
5409
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005410 if (cmd_result == FAIL)
5411 /* The Ex command failed, do not execute the operator. */
5412 clearop(cap->oap);
5413 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5415 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005416 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5417 || did_emsg
5418 ))
5419 /* The start of the operator has become invalid by the Ex command.
5420 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 clearopbeep(cap->oap);
5422 }
5423}
5424
5425/*
5426 * Handle CTRL-G command.
5427 */
5428 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005429nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 if (VIsual_active) /* toggle Selection/Visual mode */
5432 {
5433 VIsual_select = !VIsual_select;
5434 showmode();
5435 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005436 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 /* print full name if count given or :cd used */
5438 fileinfo((int)cap->count0, FALSE, TRUE);
5439}
5440
5441/*
5442 * Handle CTRL-H <Backspace> command.
5443 */
5444 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005445nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 if (VIsual_active && VIsual_select)
5448 {
5449 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5450 v_visop(cap);
5451 }
5452 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 nv_left(cap);
5454}
5455
5456/*
5457 * CTRL-L: clear screen and redraw.
5458 */
5459 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005460nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461{
5462 if (!checkclearop(cap->oap))
5463 {
5464#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5465 /*
5466 * Right now, the BeBox doesn't seem to have an easy way to detect
5467 * window resizing, so we cheat and make the user detect it
5468 * manually with CTRL-L instead
5469 */
5470 ui_get_shellsize();
5471#endif
5472#ifdef FEAT_SYN_HL
5473 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005474 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475#endif
5476 redraw_later(CLEAR);
5477 }
5478}
5479
5480/*
5481 * CTRL-O: In Select mode: switch to Visual mode for one command.
5482 * Otherwise: Go to older pcmark.
5483 */
5484 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005485nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 if (VIsual_active && VIsual_select)
5488 {
5489 VIsual_select = FALSE;
5490 showmode();
5491 restart_VIsual_select = 2; /* restart Select mode later */
5492 }
5493 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 {
5495 cap->count1 = -cap->count1;
5496 nv_pcmark(cap);
5497 }
5498}
5499
5500/*
5501 * CTRL-^ command, short for ":e #"
5502 */
5503 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005504nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505{
5506 if (!checkclearopq(cap->oap))
5507 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5508 GETF_SETMARK|GETF_ALT, FALSE);
5509}
5510
5511/*
5512 * "Z" commands.
5513 */
5514 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005515nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516{
5517 if (!checkclearopq(cap->oap))
5518 {
5519 switch (cap->nchar)
5520 {
5521 /* "ZZ": equivalent to ":x". */
5522 case 'Z': do_cmdline_cmd((char_u *)"x");
5523 break;
5524
5525 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5526 case 'Q': do_cmdline_cmd((char_u *)"q!");
5527 break;
5528
5529 default: clearopbeep(cap->oap);
5530 }
5531 }
5532}
5533
5534#if defined(FEAT_WINDOWS) || defined(PROTO)
5535/*
5536 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5537 */
5538 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005539do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540{
5541 oparg_T oa;
5542 cmdarg_T ca;
5543
5544 clear_oparg(&oa);
5545 vim_memset(&ca, 0, sizeof(ca));
5546 ca.oap = &oa;
5547 ca.cmdchar = c1;
5548 ca.nchar = c2;
5549 nv_ident(&ca);
5550}
5551#endif
5552
5553/*
5554 * Handle the commands that use the word under the cursor.
5555 * [g] CTRL-] :ta to current identifier
5556 * [g] 'K' run program for current identifier
5557 * [g] '*' / to current identifier or string
5558 * [g] '#' ? to current identifier or string
5559 * g ']' :tselect for current identifier
5560 */
5561 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005562nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563{
5564 char_u *ptr = NULL;
5565 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005566 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005567 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 char_u *p;
5569 char_u *kp; /* value of 'keywordprg' */
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005570 int kp_help; /* 'keywordprg' is ":he" */
5571 int kp_ex; /* 'keywordprg' starts with ":" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 int n = 0; /* init for GCC */
5573 int cmdchar;
5574 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005575 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 char_u *aux_ptr;
5577 int isman;
5578 int isman_s;
5579
5580 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5581 {
5582 cmdchar = cap->nchar;
5583 g_cmd = TRUE;
5584 }
5585 else
5586 {
5587 cmdchar = cap->cmdchar;
5588 g_cmd = FALSE;
5589 }
5590
5591 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5592 cmdchar = '#';
5593
5594 /*
5595 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5596 */
5597 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5598 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5600 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 if (checkclearopq(cap->oap))
5602 return;
5603 }
5604
5605 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5606 (cmdchar == '*' || cmdchar == '#')
5607 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5608 {
5609 clearop(cap->oap);
5610 return;
5611 }
5612
5613 /* Allocate buffer to put the command in. Inserting backslashes can
5614 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5615 * and some numbers. */
5616 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5617 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5618 || STRCMP(kp, ":help") == 0);
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005619 kp_ex = (*kp == ':');
5620 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
5621 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 if (buf == NULL)
5623 return;
5624 buf[0] = NUL;
5625
5626 switch (cmdchar)
5627 {
5628 case '*':
5629 case '#':
5630 /*
5631 * Put cursor at start of word, makes search skip the word
5632 * under the cursor.
5633 * Call setpcmark() first, so "*``" puts the cursor back where
5634 * it was.
5635 */
5636 setpcmark();
5637 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5638
5639 if (!g_cmd && vim_iswordp(ptr))
5640 STRCPY(buf, "\\<");
5641 no_smartcase = TRUE; /* don't use 'smartcase' now */
5642 break;
5643
5644 case 'K':
5645 if (kp_help)
5646 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005647 else if (kp_ex)
5648 {
5649 if (cap->count0 != 0)
5650 vim_snprintf((char *)buf, buflen, "%s %ld",
5651 kp, cap->count0);
5652 else
5653 STRCPY(buf, kp);
5654 STRCAT(buf, " ");
5655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 else
5657 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005658 /* An external command will probably use an argument starting
5659 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005660 while (*ptr == '-' && n > 0)
5661 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005662 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005663 --n;
5664 }
5665 if (n == 0)
5666 {
5667 EMSG(_(e_noident)); /* found dashes only */
5668 vim_free(buf);
5669 return;
5670 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005671
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 /* When a count is given, turn it into a range. Is this
5673 * really what we want? */
5674 isman = (STRCMP(kp, "man") == 0);
5675 isman_s = (STRCMP(kp, "man -s") == 0);
5676 if (cap->count0 != 0 && !(isman || isman_s))
5677 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5678
5679 STRCAT(buf, "! ");
5680 if (cap->count0 == 0 && isman_s)
5681 STRCAT(buf, "man");
5682 else
5683 STRCAT(buf, kp);
5684 STRCAT(buf, " ");
5685 if (cap->count0 != 0 && (isman || isman_s))
5686 {
5687 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5688 STRCAT(buf, " ");
5689 }
5690 }
5691 break;
5692
5693 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005694 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695#ifdef FEAT_CSCOPE
5696 if (p_cst)
5697 STRCPY(buf, "cstag ");
5698 else
5699#endif
5700 STRCPY(buf, "ts ");
5701 break;
5702
5703 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005704 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 if (curbuf->b_help)
5706 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005708 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005709 if (g_cmd)
5710 STRCPY(buf, "tj ");
5711 else
5712 sprintf((char *)buf, "%ldta ", cap->count0);
5713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 }
5715
5716 /*
5717 * Now grab the chars in the identifier
5718 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005719 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005721 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01005722 if (kp_ex)
5723 /* Escape the argument properly for an Ex command */
5724 p = vim_strsave_fnameescape(ptr, FALSE);
5725 else
5726 /* Escape the argument properly for a shell command */
5727 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005728 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005729 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005731 vim_free(buf);
5732 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005734 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5735 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005736 {
5737 vim_free(buf);
5738 vim_free(p);
5739 return;
5740 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005741 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005742 STRCAT(buf, p);
5743 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005745 else
5746 {
5747 if (cmdchar == '*')
5748 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5749 else if (cmdchar == '#')
5750 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005751 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005752 {
5753 if (curbuf->b_help)
5754 /* ":help" handles unescaped argument */
5755 aux_ptr = (char_u *)"";
5756 else
5757 aux_ptr = (char_u *)"\\|\"\n[";
5758 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005759 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005760 aux_ptr = (char_u *)"\\|\"\n*?[";
5761
5762 p = buf + STRLEN(buf);
5763 while (n-- > 0)
5764 {
5765 /* put a backslash before \ and some others */
5766 if (vim_strchr(aux_ptr, *ptr) != NULL)
5767 *p++ = '\\';
5768#ifdef FEAT_MBYTE
5769 /* When current byte is a part of multibyte character, copy all
5770 * bytes of that character. */
5771 if (has_mbyte)
5772 {
5773 int i;
5774 int len = (*mb_ptr2len)(ptr) - 1;
5775
5776 for (i = 0; i < len && n >= 1; ++i, --n)
5777 *p++ = *ptr++;
5778 }
5779#endif
5780 *p++ = *ptr++;
5781 }
5782 *p = NUL;
5783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784
5785 /*
5786 * Execute the command.
5787 */
5788 if (cmdchar == '*' || cmdchar == '#')
5789 {
5790 if (!g_cmd && (
5791#ifdef FEAT_MBYTE
5792 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5793#endif
5794 vim_iswordc(ptr[-1])))
5795 STRCAT(buf, "\\>");
5796#ifdef FEAT_CMDHIST
5797 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005798 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5800#endif
Bram Moolenaar46539112015-02-17 15:43:57 +01005801 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 }
5803 else
5804 do_cmdline_cmd(buf);
5805
5806 vim_free(buf);
5807}
5808
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809/*
5810 * Get visually selected text, within one line only.
5811 * Returns FAIL if more than one line selected.
5812 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005813 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005814get_visual_text(
5815 cmdarg_T *cap,
5816 char_u **pp, /* return: start of selected text */
5817 int *lenp) /* return: length of selected text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818{
5819 if (VIsual_mode != 'V')
5820 unadjust_for_sel();
5821 if (VIsual.lnum != curwin->w_cursor.lnum)
5822 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005823 if (cap != NULL)
5824 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 return FAIL;
5826 }
5827 if (VIsual_mode == 'V')
5828 {
5829 *pp = ml_get_curline();
5830 *lenp = (int)STRLEN(*pp);
5831 }
5832 else
5833 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005834 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 {
5836 *pp = ml_get_pos(&curwin->w_cursor);
5837 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5838 }
5839 else
5840 {
5841 *pp = ml_get_pos(&VIsual);
5842 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5843 }
5844#ifdef FEAT_MBYTE
5845 if (has_mbyte)
5846 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005847 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848#endif
5849 }
5850 reset_VIsual_and_resel();
5851 return OK;
5852}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853
5854/*
5855 * CTRL-T: backwards in tag stack
5856 */
5857 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005858nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859{
5860 if (!checkclearopq(cap->oap))
5861 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5862}
5863
5864/*
5865 * Handle scrolling command 'H', 'L' and 'M'.
5866 */
5867 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005868nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869{
5870 int used = 0;
5871 long n;
5872#ifdef FEAT_FOLDING
5873 linenr_T lnum;
5874#endif
5875 int half;
5876
5877 cap->oap->motion_type = MLINE;
5878 setpcmark();
5879
5880 if (cap->cmdchar == 'L')
5881 {
5882 validate_botline(); /* make sure curwin->w_botline is valid */
5883 curwin->w_cursor.lnum = curwin->w_botline - 1;
5884 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5885 curwin->w_cursor.lnum = 1;
5886 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005887 {
5888#ifdef FEAT_FOLDING
5889 if (hasAnyFolding(curwin))
5890 {
5891 /* Count a fold for one screen line. */
5892 for (n = cap->count1 - 1; n > 0
5893 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5894 {
5895 (void)hasFolding(curwin->w_cursor.lnum,
5896 &curwin->w_cursor.lnum, NULL);
5897 --curwin->w_cursor.lnum;
5898 }
5899 }
5900 else
5901#endif
5902 curwin->w_cursor.lnum -= cap->count1 - 1;
5903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 }
5905 else
5906 {
5907 if (cap->cmdchar == 'M')
5908 {
5909#ifdef FEAT_DIFF
5910 /* Don't count filler lines above the window. */
5911 used -= diff_check_fill(curwin, curwin->w_topline)
5912 - curwin->w_topfill;
5913#endif
5914 validate_botline(); /* make sure w_empty_rows is valid */
5915 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5916 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5917 {
5918#ifdef FEAT_DIFF
5919 /* Count half he number of filler lines to be "below this
5920 * line" and half to be "above the next line". */
5921 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5922 + n) / 2 >= half)
5923 {
5924 --n;
5925 break;
5926 }
5927#endif
5928 used += plines(curwin->w_topline + n);
5929 if (used >= half)
5930 break;
5931#ifdef FEAT_FOLDING
5932 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5933 n = lnum - curwin->w_topline;
5934#endif
5935 }
5936 if (n > 0 && used > curwin->w_height)
5937 --n;
5938 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005939 else /* (cap->cmdchar == 'H') */
5940 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005942#ifdef FEAT_FOLDING
5943 if (hasAnyFolding(curwin))
5944 {
5945 /* Count a fold for one screen line. */
5946 lnum = curwin->w_topline;
5947 while (n-- > 0 && lnum < curwin->w_botline - 1)
5948 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02005949 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005950 ++lnum;
5951 }
5952 n = lnum - curwin->w_topline;
5953 }
5954#endif
5955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 curwin->w_cursor.lnum = curwin->w_topline + n;
5957 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5958 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5959 }
5960
5961 cursor_correct(); /* correct for 'so' */
5962 beginline(BL_SOL | BL_FIX);
5963}
5964
5965/*
5966 * Cursor right commands.
5967 */
5968 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005969nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970{
5971 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005972 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005974 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5975 {
5976 /* <C-Right> and <S-Right> move a word or WORD right */
5977 if (mod_mask & MOD_MASK_CTRL)
5978 cap->arg = TRUE;
5979 nv_wordcmd(cap);
5980 return;
5981 }
5982
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 cap->oap->motion_type = MCHAR;
5984 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005985 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005987#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005989 * In virtual edit mode, there's no such thing as "past_line", as lines
5990 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 */
5992 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005993 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994#endif
5995
5996 for (n = cap->count1; n > 0; --n)
5997 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005998 if ((!past_line && oneright() == FAIL)
5999 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006000 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 {
6002 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006003 * <Space> wraps to next line if 'whichwrap' has 's'.
6004 * 'l' wraps to next line if 'whichwrap' has 'l'.
6005 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 */
6007 if ( ((cap->cmdchar == ' '
6008 && vim_strchr(p_ww, 's') != NULL)
6009 || (cap->cmdchar == 'l'
6010 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006011 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 && vim_strchr(p_ww, '>') != NULL))
6013 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6014 {
6015 /* When deleting we also count the NL as a character.
6016 * Set cap->oap->inclusive when last char in the line is
6017 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006018 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006020 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 cap->oap->inclusive = TRUE;
6022 else
6023 {
6024 ++curwin->w_cursor.lnum;
6025 curwin->w_cursor.col = 0;
6026#ifdef FEAT_VIRTUALEDIT
6027 curwin->w_cursor.coladd = 0;
6028#endif
6029 curwin->w_set_curswant = TRUE;
6030 cap->oap->inclusive = FALSE;
6031 }
6032 continue;
6033 }
6034 if (cap->oap->op_type == OP_NOP)
6035 {
6036 /* Only beep and flush if not moved at all */
6037 if (n == cap->count1)
6038 beep_flush();
6039 }
6040 else
6041 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006042 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 cap->oap->inclusive = TRUE;
6044 }
6045 break;
6046 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006047 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 {
6049 curwin->w_set_curswant = TRUE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006050#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 if (virtual_active())
6052 oneright();
6053 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006056#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 if (has_mbyte)
6058 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006059 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 ++curwin->w_cursor.col;
6063 }
6064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 }
6066#ifdef FEAT_FOLDING
6067 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6068 && cap->oap->op_type == OP_NOP)
6069 foldOpenCursor();
6070#endif
6071}
6072
6073/*
6074 * Cursor left commands.
6075 *
6076 * Returns TRUE when operator end should not be adjusted.
6077 */
6078 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006079nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080{
6081 long n;
6082
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006083 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6084 {
6085 /* <C-Left> and <S-Left> move a word or WORD left */
6086 if (mod_mask & MOD_MASK_CTRL)
6087 cap->arg = 1;
6088 nv_bck_word(cap);
6089 return;
6090 }
6091
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 cap->oap->motion_type = MCHAR;
6093 cap->oap->inclusive = FALSE;
6094 for (n = cap->count1; n > 0; --n)
6095 {
6096 if (oneleft() == FAIL)
6097 {
6098 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6099 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6100 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6101 */
6102 if ( (((cap->cmdchar == K_BS
6103 || cap->cmdchar == Ctrl_H)
6104 && vim_strchr(p_ww, 'b') != NULL)
6105 || (cap->cmdchar == 'h'
6106 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006107 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 && vim_strchr(p_ww, '<') != NULL))
6109 && curwin->w_cursor.lnum > 1)
6110 {
6111 --(curwin->w_cursor.lnum);
6112 coladvance((colnr_T)MAXCOL);
6113 curwin->w_set_curswant = TRUE;
6114
6115 /* When the NL before the first char has to be deleted we
6116 * put the cursor on the NUL after the previous line.
6117 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006118 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 if ( (cap->oap->op_type == OP_DELETE
6120 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006121 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006123 char_u *cp = ml_get_cursor();
6124
6125 if (*cp != NUL)
6126 {
6127#ifdef FEAT_MBYTE
6128 if (has_mbyte)
6129 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6130 else
6131#endif
6132 ++curwin->w_cursor.col;
6133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 cap->retval |= CA_NO_ADJ_OP_END;
6135 }
6136 continue;
6137 }
6138 /* Only beep and flush if not moved at all */
6139 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6140 beep_flush();
6141 break;
6142 }
6143 }
6144#ifdef FEAT_FOLDING
6145 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6146 && cap->oap->op_type == OP_NOP)
6147 foldOpenCursor();
6148#endif
6149}
6150
6151/*
6152 * Cursor up commands.
6153 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6154 */
6155 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006156nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006158 if (mod_mask & MOD_MASK_SHIFT)
6159 {
6160 /* <S-Up> is page up */
6161 cap->arg = BACKWARD;
6162 nv_page(cap);
6163 }
6164 else
6165 {
6166 cap->oap->motion_type = MLINE;
6167 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6168 clearopbeep(cap->oap);
6169 else if (cap->arg)
6170 beginline(BL_WHITE | BL_FIX);
6171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172}
6173
6174/*
6175 * Cursor down commands.
6176 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6177 */
6178 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02006179nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006181 if (mod_mask & MOD_MASK_SHIFT)
6182 {
6183 /* <S-Down> is page down */
6184 cap->arg = FORWARD;
6185 nv_page(cap);
6186 }
6187 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6189 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006190 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar74a47162017-02-26 19:09:05 +01006191 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006192 if (curwin->w_llist_ref == NULL)
6193 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6194 else
6195 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar74a47162017-02-26 19:09:05 +01006196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 else
6198#endif
6199 {
6200#ifdef FEAT_CMDWIN
6201 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006202 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 cmdwin_result = CAR;
6204 else
6205#endif
6206 {
6207 cap->oap->motion_type = MLINE;
6208 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6209 clearopbeep(cap->oap);
6210 else if (cap->arg)
6211 beginline(BL_WHITE | BL_FIX);
6212 }
6213 }
6214}
6215
6216#ifdef FEAT_SEARCHPATH
6217/*
6218 * Grab the file name under the cursor and edit it.
6219 */
6220 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006221nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222{
6223 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006224 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006226 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 {
6228 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006229 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 return;
6231 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006232#ifdef FEAT_AUTOCMD
6233 if (curbuf_locked())
6234 {
6235 clearop(cap->oap);
6236 return;
6237 }
6238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006240 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241
6242 if (ptr != NULL)
6243 {
6244 /* do autowrite if necessary */
6245 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02006246 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 setpcmark();
6248 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006249 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006250 if (cap->nchar == 'F' && lnum >= 0)
6251 {
6252 curwin->w_cursor.lnum = lnum;
6253 check_cursor_lnum();
6254 beginline(BL_SOL | BL_FIX);
6255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 vim_free(ptr);
6257 }
6258 else
6259 clearop(cap->oap);
6260}
6261#endif
6262
6263/*
6264 * <End> command: to end of current line or last line.
6265 */
6266 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006267nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006269 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006271 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 nv_goto(cap);
6273 cap->count1 = 1; /* to end of current line */
6274 }
6275 nv_dollar(cap);
6276}
6277
6278/*
6279 * Handle the "$" command.
6280 */
6281 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006282nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283{
6284 cap->oap->motion_type = MCHAR;
6285 cap->oap->inclusive = TRUE;
6286#ifdef FEAT_VIRTUALEDIT
6287 /* In virtual mode when off the edge of a line and an operator
6288 * is pending (whew!) keep the cursor where it is.
6289 * Otherwise, send it to the end of the line. */
6290 if (!virtual_active() || gchar_cursor() != NUL
6291 || cap->oap->op_type == OP_NOP)
6292#endif
6293 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6294 if (cursor_down((long)(cap->count1 - 1),
6295 cap->oap->op_type == OP_NOP) == FAIL)
6296 clearopbeep(cap->oap);
6297#ifdef FEAT_FOLDING
6298 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6299 foldOpenCursor();
6300#endif
6301}
6302
6303/*
6304 * Implementation of '?' and '/' commands.
6305 * If cap->arg is TRUE don't set PC mark.
6306 */
6307 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006308nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309{
6310 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02006311 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312
6313 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6314 {
6315 /* Translate "g??" to "g?g?" */
6316 cap->cmdchar = 'g';
6317 cap->nchar = '?';
6318 nv_operator(cap);
6319 return;
6320 }
6321
Bram Moolenaardda933d2016-09-03 21:04:58 +02006322 /* When using 'incsearch' the cursor may be moved to set a different search
6323 * start position. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6325
6326 if (cap->searchbuf == NULL)
6327 {
6328 clearop(oap);
6329 return;
6330 }
6331
Bram Moolenaar46539112015-02-17 15:43:57 +01006332 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006333 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaardda933d2016-09-03 21:04:58 +02006334 ? 0 : SEARCH_MARK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335}
6336
6337/*
6338 * Handle "N" and "n" commands.
6339 * cap->arg is SEARCH_REV for "N", 0 for "n".
6340 */
6341 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006342nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343{
Bram Moolenaar46539112015-02-17 15:43:57 +01006344 pos_T old = curwin->w_cursor;
6345 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6346
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006347 if (i == 1 && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01006348 {
6349 /* Avoid getting stuck on the current cursor position, which can
6350 * happen when an offset is given and the cursor is on the last char
6351 * in the buffer: Repeat with count + 1. */
6352 cap->count1 += 1;
6353 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6354 cap->count1 -= 1;
6355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356}
6357
6358/*
6359 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6360 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01006361 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 */
Bram Moolenaar46539112015-02-17 15:43:57 +01006363 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006364normal_search(
6365 cmdarg_T *cap,
6366 int dir,
6367 char_u *pat,
6368 int opt) /* extra flags for do_search() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369{
6370 int i;
6371
6372 cap->oap->motion_type = MCHAR;
6373 cap->oap->inclusive = FALSE;
6374 cap->oap->use_reg_one = TRUE;
6375 curwin->w_set_curswant = TRUE;
6376
6377 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006378 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 if (i == 0)
6380 clearop(cap->oap);
6381 else
6382 {
6383 if (i == 2)
6384 cap->oap->motion_type = MLINE;
6385#ifdef FEAT_VIRTUALEDIT
6386 curwin->w_cursor.coladd = 0;
6387#endif
6388#ifdef FEAT_FOLDING
6389 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6390 foldOpenCursor();
6391#endif
6392 }
6393
6394 /* "/$" will put the cursor after the end of the line, may need to
6395 * correct that here */
6396 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01006397 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398}
6399
6400/*
6401 * Character search commands.
6402 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6403 * ',' and FALSE for ';'.
6404 * cap->nchar is NUL for ',' and ';' (repeat the search)
6405 */
6406 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006407nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408{
6409 int t_cmd;
6410
6411 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6412 t_cmd = TRUE;
6413 else
6414 t_cmd = FALSE;
6415
6416 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6418 clearopbeep(cap->oap);
6419 else
6420 {
6421 curwin->w_set_curswant = TRUE;
6422#ifdef FEAT_VIRTUALEDIT
6423 /* Include a Tab for "tx" and for "dfx". */
6424 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6425 && (t_cmd || cap->oap->op_type != OP_NOP))
6426 {
6427 colnr_T scol, ecol;
6428
6429 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6430 curwin->w_cursor.coladd = ecol - scol;
6431 }
6432 else
6433 curwin->w_cursor.coladd = 0;
6434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436#ifdef FEAT_FOLDING
6437 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6438 foldOpenCursor();
6439#endif
6440 }
6441}
6442
6443/*
6444 * "[" and "]" commands.
6445 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6446 */
6447 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006448nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006450 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 pos_T prev_pos;
6452 pos_T *pos = NULL; /* init for GCC */
6453 pos_T old_pos; /* cursor position before command */
6454 int flag;
6455 long n;
6456 int findc;
6457 int c;
6458
6459 cap->oap->motion_type = MCHAR;
6460 cap->oap->inclusive = FALSE;
6461 old_pos = curwin->w_cursor;
6462#ifdef FEAT_VIRTUALEDIT
6463 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6464#endif
6465
6466#ifdef FEAT_SEARCHPATH
6467 /*
6468 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6469 */
6470 if (cap->nchar == 'f')
6471 nv_gotofile(cap);
6472 else
6473#endif
6474
6475#ifdef FEAT_FIND_ID
6476 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006477 * Find the occurrence(s) of the identifier or define under cursor
6478 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 *
6480 * search list jump
6481 * fwd bwd fwd bwd fwd bwd
6482 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6483 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6484 */
6485 if (vim_strchr((char_u *)
6486#ifdef EBCDIC
6487 "iI\005dD\067",
6488#else
6489 "iI\011dD\004",
6490#endif
6491 cap->nchar) != NULL)
6492 {
6493 char_u *ptr;
6494 int len;
6495
6496 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6497 clearop(cap->oap);
6498 else
6499 {
6500 find_pattern_in_path(ptr, 0, len, TRUE,
6501 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6502 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6503 cap->count1,
6504 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6505 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6506 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6507 (linenr_T)MAXLNUM);
6508 curwin->w_set_curswant = TRUE;
6509 }
6510 }
6511 else
6512#endif
6513
6514 /*
6515 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6516 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6517 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6518 * "[m" or "]m" search for prev/next start of (Java) method.
6519 * "[M" or "]M" search for prev/next end of (Java) method.
6520 */
6521 if ( (cap->cmdchar == '['
6522 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6523 || (cap->cmdchar == ']'
6524 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6525 {
6526 if (cap->nchar == '*')
6527 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 prev_pos.lnum = 0;
6529 if (cap->nchar == 'm' || cap->nchar == 'M')
6530 {
6531 if (cap->cmdchar == '[')
6532 findc = '{';
6533 else
6534 findc = '}';
6535 n = 9999;
6536 }
6537 else
6538 {
6539 findc = cap->nchar;
6540 n = cap->count1;
6541 }
6542 for ( ; n > 0; --n)
6543 {
6544 if ((pos = findmatchlimit(cap->oap, findc,
6545 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6546 {
6547 if (new_pos.lnum == 0) /* nothing found */
6548 {
6549 if (cap->nchar != 'm' && cap->nchar != 'M')
6550 clearopbeep(cap->oap);
6551 }
6552 else
6553 pos = &new_pos; /* use last one found */
6554 break;
6555 }
6556 prev_pos = new_pos;
6557 curwin->w_cursor = *pos;
6558 new_pos = *pos;
6559 }
6560 curwin->w_cursor = old_pos;
6561
6562 /*
6563 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6564 * brought us to the match for "[m" and "]M" when inside a method.
6565 * Try finding the '{' or '}' we want to be at.
6566 * Also repeat for the given count.
6567 */
6568 if (cap->nchar == 'm' || cap->nchar == 'M')
6569 {
6570 /* norm is TRUE for "]M" and "[m" */
6571 int norm = ((findc == '{') == (cap->nchar == 'm'));
6572
6573 n = cap->count1;
6574 /* found a match: we were inside a method */
6575 if (prev_pos.lnum != 0)
6576 {
6577 pos = &prev_pos;
6578 curwin->w_cursor = prev_pos;
6579 if (norm)
6580 --n;
6581 }
6582 else
6583 pos = NULL;
6584 while (n > 0)
6585 {
6586 for (;;)
6587 {
6588 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6589 {
6590 /* if not found anything, that's an error */
6591 if (pos == NULL)
6592 clearopbeep(cap->oap);
6593 n = 0;
6594 break;
6595 }
6596 c = gchar_cursor();
6597 if (c == '{' || c == '}')
6598 {
6599 /* Must have found end/start of class: use it.
6600 * Or found the place to be at. */
6601 if ((c == findc && norm) || (n == 1 && !norm))
6602 {
6603 new_pos = curwin->w_cursor;
6604 pos = &new_pos;
6605 n = 0;
6606 }
6607 /* if no match found at all, we started outside of the
6608 * class and we're inside now. Just go on. */
6609 else if (new_pos.lnum == 0)
6610 {
6611 new_pos = curwin->w_cursor;
6612 pos = &new_pos;
6613 }
6614 /* found start/end of other method: go to match */
6615 else if ((pos = findmatchlimit(cap->oap, findc,
6616 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6617 0)) == NULL)
6618 n = 0;
6619 else
6620 curwin->w_cursor = *pos;
6621 break;
6622 }
6623 }
6624 --n;
6625 }
6626 curwin->w_cursor = old_pos;
6627 if (pos == NULL && new_pos.lnum != 0)
6628 clearopbeep(cap->oap);
6629 }
6630 if (pos != NULL)
6631 {
6632 setpcmark();
6633 curwin->w_cursor = *pos;
6634 curwin->w_set_curswant = TRUE;
6635#ifdef FEAT_FOLDING
6636 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6637 && cap->oap->op_type == OP_NOP)
6638 foldOpenCursor();
6639#endif
6640 }
6641 }
6642
6643 /*
6644 * "[[", "[]", "]]" and "][": move to start or end of function
6645 */
6646 else if (cap->nchar == '[' || cap->nchar == ']')
6647 {
6648 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6649 flag = '{';
6650 else
6651 flag = '}'; /* "][" or "[]" */
6652
6653 curwin->w_set_curswant = TRUE;
6654 /*
6655 * Imitate strange Vi behaviour: When using "]]" with an operator
6656 * we also stop at '}'.
6657 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006658 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 (cap->oap->op_type != OP_NOP
6660 && cap->arg == FORWARD && flag == '{')))
6661 clearopbeep(cap->oap);
6662 else
6663 {
6664 if (cap->oap->op_type == OP_NOP)
6665 beginline(BL_WHITE | BL_FIX);
6666#ifdef FEAT_FOLDING
6667 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6668 foldOpenCursor();
6669#endif
6670 }
6671 }
6672
6673 /*
6674 * "[p", "[P", "]P" and "]p": put with indent adjustment
6675 */
6676 else if (cap->nchar == 'p' || cap->nchar == 'P')
6677 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006678 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006680 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6681 ? FORWARD : BACKWARD;
6682 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006683 int was_visual = VIsual_active;
6684 int line_count = curbuf->b_ml.ml_line_count;
6685 pos_T start, end;
6686
6687 if (VIsual_active)
6688 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006689 start = LTOREQ_POS(VIsual, curwin->w_cursor)
Bram Moolenaar27bed202014-03-12 17:42:04 +01006690 ? VIsual : curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006691 end = EQUAL_POS(start,VIsual) ? curwin->w_cursor : VIsual;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006692 curwin->w_cursor = (dir == BACKWARD ? start : end);
6693 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006694# ifdef FEAT_CLIPBOARD
6695 adjust_clip_reg(&regname);
6696# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006698
6699 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006700 if (was_visual)
6701 {
6702 VIsual = start;
6703 curwin->w_cursor = end;
6704 if (dir == BACKWARD)
6705 {
6706 /* adjust lines */
6707 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6708 curwin->w_cursor.lnum +=
6709 curbuf->b_ml.ml_line_count - line_count;
6710 }
6711
6712 VIsual_active = TRUE;
6713 if (VIsual_mode == 'V')
6714 {
6715 /* delete visually selected lines */
6716 cap->cmdchar = 'd';
6717 cap->nchar = NUL;
6718 cap->oap->regname = regname;
6719 nv_operator(cap);
6720 do_pending_operator(cap, 0, FALSE);
6721 }
6722 if (VIsual_active)
6723 {
6724 end_visual_mode();
6725 redraw_later(SOME_VALID);
6726 }
6727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 }
6729 }
6730
6731 /*
6732 * "['", "[`", "]'" and "]`": jump to next mark
6733 */
6734 else if (cap->nchar == '\'' || cap->nchar == '`')
6735 {
6736 pos = &curwin->w_cursor;
6737 for (n = cap->count1; n > 0; --n)
6738 {
6739 prev_pos = *pos;
6740 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6741 cap->nchar == '\'');
6742 if (pos == NULL)
6743 break;
6744 }
6745 if (pos == NULL)
6746 pos = &prev_pos;
6747 nv_cursormark(cap, cap->nchar == '\'', pos);
6748 }
6749
6750#ifdef FEAT_MOUSE
6751 /*
6752 * [ or ] followed by a middle mouse click: put selected text with
6753 * indent adjustment. Any other button just does as usual.
6754 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006755 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 {
6757 (void)do_mouse(cap->oap, cap->nchar,
6758 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6759 cap->count1, PUT_FIXINDENT);
6760 }
6761#endif /* FEAT_MOUSE */
6762
6763#ifdef FEAT_FOLDING
6764 /*
6765 * "[z" and "]z": move to start or end of open fold.
6766 */
6767 else if (cap->nchar == 'z')
6768 {
6769 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6770 cap->count1) == FAIL)
6771 clearopbeep(cap->oap);
6772 }
6773#endif
6774
6775#ifdef FEAT_DIFF
6776 /*
6777 * "[c" and "]c": move to next or previous diff-change.
6778 */
6779 else if (cap->nchar == 'c')
6780 {
6781 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6782 cap->count1) == FAIL)
6783 clearopbeep(cap->oap);
6784 }
6785#endif
6786
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006787#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006788 /*
6789 * "[s", "[S", "]s" and "]S": move to next spell error.
6790 */
6791 else if (cap->nchar == 's' || cap->nchar == 'S')
6792 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006793 setpcmark();
6794 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006795 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6796 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006797 {
6798 clearopbeep(cap->oap);
6799 break;
6800 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006801# ifdef FEAT_FOLDING
6802 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6803 foldOpenCursor();
6804# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006805 }
6806#endif
6807
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 /* Not a valid cap->nchar. */
6809 else
6810 clearopbeep(cap->oap);
6811}
6812
6813/*
6814 * Handle Normal mode "%" command.
6815 */
6816 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006817nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818{
6819 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006820#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 linenr_T lnum = curwin->w_cursor.lnum;
6822#endif
6823
6824 cap->oap->inclusive = TRUE;
6825 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6826 {
6827 if (cap->count0 > 100)
6828 clearopbeep(cap->oap);
6829 else
6830 {
6831 cap->oap->motion_type = MLINE;
6832 setpcmark();
6833 /* Round up, so CTRL-G will give same value. Watch out for a
6834 * large line count, the line number must not go negative! */
6835 if (curbuf->b_ml.ml_line_count > 1000000)
6836 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6837 / 100L * cap->count0;
6838 else
6839 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6840 cap->count0 + 99L) / 100L;
6841 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6842 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6843 beginline(BL_SOL | BL_FIX);
6844 }
6845 }
6846 else /* "%" : go to matching paren */
6847 {
6848 cap->oap->motion_type = MCHAR;
6849 cap->oap->use_reg_one = TRUE;
6850 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6851 clearopbeep(cap->oap);
6852 else
6853 {
6854 setpcmark();
6855 curwin->w_cursor = *pos;
6856 curwin->w_set_curswant = TRUE;
6857#ifdef FEAT_VIRTUALEDIT
6858 curwin->w_cursor.coladd = 0;
6859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 }
6862 }
6863#ifdef FEAT_FOLDING
6864 if (cap->oap->op_type == OP_NOP
6865 && lnum != curwin->w_cursor.lnum
6866 && (fdo_flags & FDO_PERCENT)
6867 && KeyTyped)
6868 foldOpenCursor();
6869#endif
6870}
6871
6872/*
6873 * Handle "(" and ")" commands.
6874 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6875 */
6876 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006877nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878{
6879 cap->oap->motion_type = MCHAR;
6880 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006881 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6882 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 curwin->w_set_curswant = TRUE;
6884
6885 if (findsent(cap->arg, cap->count1) == FAIL)
6886 clearopbeep(cap->oap);
6887 else
6888 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006889 /* Don't leave the cursor on the NUL past end of line. */
6890 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891#ifdef FEAT_VIRTUALEDIT
6892 curwin->w_cursor.coladd = 0;
6893#endif
6894#ifdef FEAT_FOLDING
6895 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6896 foldOpenCursor();
6897#endif
6898 }
6899}
6900
6901/*
6902 * "m" command: Mark a position.
6903 */
6904 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006905nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906{
6907 if (!checkclearop(cap->oap))
6908 {
6909 if (setmark(cap->nchar) == FAIL)
6910 clearopbeep(cap->oap);
6911 }
6912}
6913
6914/*
6915 * "{" and "}" commands.
6916 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6917 */
6918 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006919nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920{
6921 cap->oap->motion_type = MCHAR;
6922 cap->oap->inclusive = FALSE;
6923 cap->oap->use_reg_one = TRUE;
6924 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006925 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 clearopbeep(cap->oap);
6927 else
6928 {
6929#ifdef FEAT_VIRTUALEDIT
6930 curwin->w_cursor.coladd = 0;
6931#endif
6932#ifdef FEAT_FOLDING
6933 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6934 foldOpenCursor();
6935#endif
6936 }
6937}
6938
6939/*
6940 * "u" command: Undo or make lower case.
6941 */
6942 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006943nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006945 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 {
6947 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6948 cap->cmdchar = 'g';
6949 cap->nchar = 'u';
6950 nv_operator(cap);
6951 }
6952 else
6953 nv_kundo(cap);
6954}
6955
6956/*
6957 * <Undo> command.
6958 */
6959 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006960nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961{
6962 if (!checkclearopq(cap->oap))
6963 {
6964 u_undo((int)cap->count1);
6965 curwin->w_set_curswant = TRUE;
6966 }
6967}
6968
6969/*
6970 * Handle the "r" command.
6971 */
6972 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006973nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974{
6975 char_u *ptr;
6976 int had_ctrl_v;
6977 long n;
6978
6979 if (checkclearop(cap->oap))
6980 return;
6981
6982 /* get another character */
6983 if (cap->nchar == Ctrl_V)
6984 {
6985 had_ctrl_v = Ctrl_V;
6986 cap->nchar = get_literal();
6987 /* Don't redo a multibyte character with CTRL-V. */
6988 if (cap->nchar > DEL)
6989 had_ctrl_v = NUL;
6990 }
6991 else
6992 had_ctrl_v = NUL;
6993
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006994 /* Abort if the character is a special key. */
6995 if (IS_SPECIAL(cap->nchar))
6996 {
6997 clearopbeep(cap->oap);
6998 return;
6999 }
7000
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 /* Visual mode "r" */
7002 if (VIsual_active)
7003 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00007004 if (got_int)
7005 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01007006 if (had_ctrl_v)
7007 {
7008 if (cap->nchar == '\r')
7009 cap->nchar = -1;
7010 else if (cap->nchar == '\n')
7011 cap->nchar = -2;
7012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 nv_operator(cap);
7014 return;
7015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016
7017#ifdef FEAT_VIRTUALEDIT
7018 /* Break tabs, etc. */
7019 if (virtual_active())
7020 {
7021 if (u_save_cursor() == FAIL)
7022 return;
7023 if (gchar_cursor() == NUL)
7024 {
7025 /* Add extra space and put the cursor on the first one. */
7026 coladvance_force((colnr_T)(getviscol() + cap->count1));
7027 curwin->w_cursor.col -= cap->count1;
7028 }
7029 else if (gchar_cursor() == TAB)
7030 coladvance_force(getviscol());
7031 }
7032#endif
7033
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007034 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007036 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037#ifdef FEAT_MBYTE
7038 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7039#endif
7040 )
7041 {
7042 clearopbeep(cap->oap);
7043 return;
7044 }
7045
7046 /*
7047 * Replacing with a TAB is done by edit() when it is complicated because
7048 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7049 * Other characters are done below to avoid problems with things like
7050 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7051 */
7052 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7053 {
7054 stuffnumReadbuff(cap->count1);
7055 stuffcharReadbuff('R');
7056 stuffcharReadbuff('\t');
7057 stuffcharReadbuff(ESC);
7058 return;
7059 }
7060
7061 /* save line for undo */
7062 if (u_save_cursor() == FAIL)
7063 return;
7064
7065 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7066 {
7067 /*
7068 * Replace character(s) by a single newline.
7069 * Strange vi behaviour: Only one newline is inserted.
7070 * Delete the characters here.
7071 * Insert the newline with an insert command, takes care of
7072 * autoindent. The insert command depends on being on the last
7073 * character of a line or not.
7074 */
7075#ifdef FEAT_MBYTE
7076 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7077#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007078 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079#endif
7080 stuffcharReadbuff('\r');
7081 stuffcharReadbuff(ESC);
7082
7083 /* Give 'r' to edit(), to get the redo command right. */
7084 invoke_edit(cap, TRUE, 'r', FALSE);
7085 }
7086 else
7087 {
7088 prep_redo(cap->oap->regname, cap->count1,
7089 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7090
7091 curbuf->b_op_start = curwin->w_cursor;
7092#ifdef FEAT_MBYTE
7093 if (has_mbyte)
7094 {
7095 int old_State = State;
7096
7097 if (cap->ncharC1 != 0)
7098 AppendCharToRedobuff(cap->ncharC1);
7099 if (cap->ncharC2 != 0)
7100 AppendCharToRedobuff(cap->ncharC2);
7101
7102 /* This is slow, but it handles replacing a single-byte with a
7103 * multi-byte and the other way around. Also handles adding
7104 * composing characters for utf-8. */
7105 for (n = cap->count1; n > 0; --n)
7106 {
7107 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007108 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7109 {
7110 int c = ins_copychar(curwin->w_cursor.lnum
7111 + (cap->nchar == Ctrl_Y ? -1 : 1));
7112 if (c != NUL)
7113 ins_char(c);
7114 else
7115 /* will be decremented further down */
7116 ++curwin->w_cursor.col;
7117 }
7118 else
7119 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 State = old_State;
7121 if (cap->ncharC1 != 0)
7122 ins_char(cap->ncharC1);
7123 if (cap->ncharC2 != 0)
7124 ins_char(cap->ncharC2);
7125 }
7126 }
7127 else
7128#endif
7129 {
7130 /*
7131 * Replace the characters within one line.
7132 */
7133 for (n = cap->count1; n > 0; --n)
7134 {
7135 /*
7136 * Get ptr again, because u_save and/or showmatch() will have
7137 * released the line. At the same time we let know that the
7138 * line will be changed.
7139 */
7140 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007141 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7142 {
7143 int c = ins_copychar(curwin->w_cursor.lnum
7144 + (cap->nchar == Ctrl_Y ? -1 : 1));
7145 if (c != NUL)
7146 ptr[curwin->w_cursor.col] = c;
7147 }
7148 else
7149 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 if (p_sm && msg_silent == 0)
7151 showmatch(cap->nchar);
7152 ++curwin->w_cursor.col;
7153 }
7154#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007155 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007157 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158
Bram Moolenaar009b2592004-10-24 19:18:58 +00007159 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007160 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007162 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 }
7164#endif
7165
7166 /* mark the buffer as changed and prepare for displaying */
7167 changed_bytes(curwin->w_cursor.lnum,
7168 (colnr_T)(curwin->w_cursor.col - cap->count1));
7169 }
7170 --curwin->w_cursor.col; /* cursor on the last replaced char */
7171#ifdef FEAT_MBYTE
7172 /* if the character on the left of the current cursor is a multi-byte
7173 * character, move two characters left */
7174 if (has_mbyte)
7175 mb_adjust_cursor();
7176#endif
7177 curbuf->b_op_end = curwin->w_cursor;
7178 curwin->w_set_curswant = TRUE;
7179 set_last_insert(cap->nchar);
7180 }
7181}
7182
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183/*
7184 * 'o': Exchange start and end of Visual area.
7185 * 'O': same, but in block mode exchange left and right corners.
7186 */
7187 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007188v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189{
7190 pos_T old_cursor;
7191 colnr_T left, right;
7192
7193 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7194 {
7195 old_cursor = curwin->w_cursor;
7196 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7197 curwin->w_cursor.lnum = VIsual.lnum;
7198 coladvance(left);
7199 VIsual = curwin->w_cursor;
7200
7201 curwin->w_cursor.lnum = old_cursor.lnum;
7202 curwin->w_curswant = right;
7203 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7204 * right one column */
7205 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7206 ++curwin->w_curswant;
7207 coladvance(curwin->w_curswant);
7208 if (curwin->w_cursor.col == old_cursor.col
7209#ifdef FEAT_VIRTUALEDIT
7210 && (!virtual_active()
7211 || curwin->w_cursor.coladd == old_cursor.coladd)
7212#endif
7213 )
7214 {
7215 curwin->w_cursor.lnum = VIsual.lnum;
7216 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7217 ++right;
7218 coladvance(right);
7219 VIsual = curwin->w_cursor;
7220
7221 curwin->w_cursor.lnum = old_cursor.lnum;
7222 coladvance(left);
7223 curwin->w_curswant = left;
7224 }
7225 }
7226 else
7227 {
7228 old_cursor = curwin->w_cursor;
7229 curwin->w_cursor = VIsual;
7230 VIsual = old_cursor;
7231 curwin->w_set_curswant = TRUE;
7232 }
7233}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234
7235/*
7236 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7237 */
7238 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007239nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 if (VIsual_active) /* "R" is replace lines */
7242 {
7243 cap->cmdchar = 'c';
7244 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007245 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 VIsual_mode = 'V';
7247 nv_operator(cap);
7248 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007249 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 {
7251 if (!curbuf->b_p_ma)
7252 EMSG(_(e_modifiable));
7253 else
7254 {
7255#ifdef FEAT_VIRTUALEDIT
7256 if (virtual_active())
7257 coladvance(getviscol());
7258#endif
7259 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7260 }
7261 }
7262}
7263
7264#ifdef FEAT_VREPLACE
7265/*
7266 * "gr".
7267 */
7268 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007269nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 if (VIsual_active)
7272 {
7273 cap->cmdchar = 'r';
7274 cap->nchar = cap->extra_char;
7275 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7276 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007277 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 {
7279 if (!curbuf->b_p_ma)
7280 EMSG(_(e_modifiable));
7281 else
7282 {
7283 if (cap->extra_char == Ctrl_V) /* get another character */
7284 cap->extra_char = get_literal();
7285 stuffcharReadbuff(cap->extra_char);
7286 stuffcharReadbuff(ESC);
7287# ifdef FEAT_VIRTUALEDIT
7288 if (virtual_active())
7289 coladvance(getviscol());
7290# endif
7291 invoke_edit(cap, TRUE, 'v', FALSE);
7292 }
7293 }
7294}
7295#endif
7296
7297/*
7298 * Swap case for "~" command, when it does not work like an operator.
7299 */
7300 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007301n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302{
7303 long n;
7304 pos_T startpos;
7305 int did_change = 0;
7306#ifdef FEAT_NETBEANS_INTG
7307 pos_T pos;
7308 char_u *ptr;
7309 int count;
7310#endif
7311
7312 if (checkclearopq(cap->oap))
7313 return;
7314
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007315 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 {
7317 clearopbeep(cap->oap);
7318 return;
7319 }
7320
7321 prep_redo_cmd(cap);
7322
7323 if (u_save_cursor() == FAIL)
7324 return;
7325
7326 startpos = curwin->w_cursor;
7327#ifdef FEAT_NETBEANS_INTG
7328 pos = startpos;
7329#endif
7330 for (n = cap->count1; n > 0; --n)
7331 {
7332 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7333 inc_cursor();
7334 if (gchar_cursor() == NUL)
7335 {
7336 if (vim_strchr(p_ww, '~') != NULL
7337 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7338 {
7339#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007340 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 {
7342 if (did_change)
7343 {
7344 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007345 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007346 netbeans_removed(curbuf, pos.lnum, pos.col,
7347 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007349 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 }
7351 pos.col = 0;
7352 pos.lnum++;
7353 }
7354#endif
7355 ++curwin->w_cursor.lnum;
7356 curwin->w_cursor.col = 0;
7357 if (n > 1)
7358 {
7359 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7360 break;
7361 u_clearline();
7362 }
7363 }
7364 else
7365 break;
7366 }
7367 }
7368#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007369 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 {
7371 ptr = ml_get(pos.lnum);
7372 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007373 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7374 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 }
7376#endif
7377
7378
7379 check_cursor();
7380 curwin->w_set_curswant = TRUE;
7381 if (did_change)
7382 {
7383 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7384 0L);
7385 curbuf->b_op_start = startpos;
7386 curbuf->b_op_end = curwin->w_cursor;
7387 if (curbuf->b_op_end.col > 0)
7388 --curbuf->b_op_end.col;
7389 }
7390}
7391
7392/*
7393 * Move cursor to mark.
7394 */
7395 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007396nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397{
7398 if (check_mark(pos) == FAIL)
7399 clearop(cap->oap);
7400 else
7401 {
7402 if (cap->cmdchar == '\''
7403 || cap->cmdchar == '`'
7404 || cap->cmdchar == '['
7405 || cap->cmdchar == ']')
7406 setpcmark();
7407 curwin->w_cursor = *pos;
7408 if (flag)
7409 beginline(BL_WHITE | BL_FIX);
7410 else
7411 check_cursor();
7412 }
7413 cap->oap->motion_type = flag ? MLINE : MCHAR;
7414 if (cap->cmdchar == '`')
7415 cap->oap->use_reg_one = TRUE;
7416 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7417 curwin->w_set_curswant = TRUE;
7418}
7419
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420/*
7421 * Handle commands that are operators in Visual mode.
7422 */
7423 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007424v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425{
7426 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7427
7428 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007429 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 if (isupper(cap->cmdchar))
7431 {
7432 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007433 {
7434 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7438 curwin->w_curswant = MAXCOL;
7439 }
7440 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7441 nv_operator(cap);
7442}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443
7444/*
7445 * "s" and "S" commands.
7446 */
7447 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007448nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7451 {
7452 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007453 {
7454 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 cap->cmdchar = 'c';
7458 nv_operator(cap);
7459 }
7460 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 nv_optrans(cap);
7462}
7463
7464/*
7465 * Abbreviated commands.
7466 */
7467 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007468nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469{
7470 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7471 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7472
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 /* in Visual mode these commands are operators */
7474 if (VIsual_active)
7475 v_visop(cap);
7476 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 nv_optrans(cap);
7478}
7479
7480/*
7481 * Translate a command into another command.
7482 */
7483 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007484nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485{
7486 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7487 (char_u *)"d$", (char_u *)"c$",
7488 (char_u *)"cl", (char_u *)"cc",
7489 (char_u *)"yy", (char_u *)":s\r"};
7490 static char_u *str = (char_u *)"xXDCsSY&";
7491
7492 if (!checkclearopq(cap->oap))
7493 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007494 /* In Vi "2D" doesn't delete the next line. Can't translate it
7495 * either, because "2." should also not use the count. */
7496 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7497 {
7498 cap->oap->start = curwin->w_cursor;
7499 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007500#ifdef FEAT_EVAL
7501 set_op_var(OP_DELETE);
7502#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007503 cap->count1 = 1;
7504 nv_dollar(cap);
7505 finish_op = TRUE;
7506 ResetRedobuff();
7507 AppendCharToRedobuff('D');
7508 }
7509 else
7510 {
7511 if (cap->count0)
7512 stuffnumReadbuff(cap->count0);
7513 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 }
7516 cap->opcount = 0;
7517}
7518
7519/*
7520 * "'" and "`" commands. Also for "g'" and "g`".
7521 * cap->arg is TRUE for "'" and "g'".
7522 */
7523 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007524nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525{
7526 pos_T *pos;
7527 int c;
7528#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007529 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7531#endif
7532
7533 if (cap->cmdchar == 'g')
7534 c = cap->extra_char;
7535 else
7536 c = cap->nchar;
7537 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7538 if (pos == (pos_T *)-1) /* jumped to other file */
7539 {
7540 if (cap->arg)
7541 {
7542 check_cursor_lnum();
7543 beginline(BL_WHITE | BL_FIX);
7544 }
7545 else
7546 check_cursor();
7547 }
7548 else
7549 nv_cursormark(cap, cap->arg, pos);
7550
7551#ifdef FEAT_VIRTUALEDIT
7552 /* May need to clear the coladd that a mark includes. */
7553 if (!virtual_active())
7554 curwin->w_cursor.coladd = 0;
7555#endif
7556#ifdef FEAT_FOLDING
7557 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007558 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007559 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 && (fdo_flags & FDO_MARK)
7561 && old_KeyTyped)
7562 foldOpenCursor();
7563#endif
7564}
7565
7566/*
7567 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7568 */
7569 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007570nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571{
7572#ifdef FEAT_JUMPLIST
7573 pos_T *pos;
7574# ifdef FEAT_FOLDING
7575 linenr_T lnum = curwin->w_cursor.lnum;
7576 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7577# endif
7578
7579 if (!checkclearopq(cap->oap))
7580 {
7581 if (cap->cmdchar == 'g')
7582 pos = movechangelist((int)cap->count1);
7583 else
7584 pos = movemark((int)cap->count1);
7585 if (pos == (pos_T *)-1) /* jump to other file */
7586 {
7587 curwin->w_set_curswant = TRUE;
7588 check_cursor();
7589 }
7590 else if (pos != NULL) /* can jump */
7591 nv_cursormark(cap, FALSE, pos);
7592 else if (cap->cmdchar == 'g')
7593 {
7594 if (curbuf->b_changelistlen == 0)
7595 EMSG(_("E664: changelist is empty"));
7596 else if (cap->count1 < 0)
7597 EMSG(_("E662: At start of changelist"));
7598 else
7599 EMSG(_("E663: At end of changelist"));
7600 }
7601 else
7602 clearopbeep(cap->oap);
7603# ifdef FEAT_FOLDING
7604 if (cap->oap->op_type == OP_NOP
7605 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7606 && (fdo_flags & FDO_MARK)
7607 && old_KeyTyped)
7608 foldOpenCursor();
7609# endif
7610 }
7611#else
7612 clearopbeep(cap->oap);
7613#endif
7614}
7615
7616/*
7617 * Handle '"' command.
7618 */
7619 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007620nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621{
7622 if (checkclearop(cap->oap))
7623 return;
7624#ifdef FEAT_EVAL
7625 if (cap->nchar == '=')
7626 cap->nchar = get_expr_register();
7627#endif
7628 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7629 {
7630 cap->oap->regname = cap->nchar;
7631 cap->opcount = cap->count0; /* remember count before '"' */
7632#ifdef FEAT_EVAL
7633 set_reg_var(cap->oap->regname);
7634#endif
7635 }
7636 else
7637 clearopbeep(cap->oap);
7638}
7639
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640/*
7641 * Handle "v", "V" and "CTRL-V" commands.
7642 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7643 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007644 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 */
7646 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007647nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007649 if (cap->cmdchar == Ctrl_Q)
7650 cap->cmdchar = Ctrl_V;
7651
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7653 * characterwise, linewise, or blockwise. */
7654 if (cap->oap->op_type != OP_NOP)
7655 {
7656 cap->oap->motion_force = cap->cmdchar;
7657 finish_op = FALSE; /* operator doesn't finish now but later */
7658 return;
7659 }
7660
7661 VIsual_select = cap->arg;
7662 if (VIsual_active) /* change Visual mode */
7663 {
7664 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7665 end_visual_mode();
7666 else /* toggle char/block mode */
7667 { /* or char/line mode */
7668 VIsual_mode = cap->cmdchar;
7669 showmode();
7670 }
7671 redraw_curbuf_later(INVERTED); /* update the inversion */
7672 }
7673 else /* start Visual mode */
7674 {
7675 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007676 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007678 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 VIsual = curwin->w_cursor;
7680
7681 VIsual_active = TRUE;
7682 VIsual_reselect = TRUE;
7683 if (!cap->arg)
7684 /* start Select mode when 'selectmode' contains "cmd" */
7685 may_start_select('c');
7686#ifdef FEAT_MOUSE
7687 setmouse();
7688#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007689 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 redraw_cmdline = TRUE; /* show visual mode later */
7691 /*
7692 * For V and ^V, we multiply the number of lines even if there
7693 * was only one -- webb
7694 */
7695 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7696 {
7697 curwin->w_cursor.lnum +=
7698 resel_VIsual_line_count * cap->count0 - 1;
7699 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7700 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7701 }
7702 VIsual_mode = resel_VIsual_mode;
7703 if (VIsual_mode == 'v')
7704 {
7705 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007706 {
7707 validate_virtcol();
7708 curwin->w_curswant = curwin->w_virtcol
7709 + resel_VIsual_vcol * cap->count0 - 1;
7710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007712 curwin->w_curswant = resel_VIsual_vcol;
7713 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007715 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 {
7717 curwin->w_curswant = MAXCOL;
7718 coladvance((colnr_T)MAXCOL);
7719 }
7720 else if (VIsual_mode == Ctrl_V)
7721 {
7722 validate_virtcol();
7723 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007724 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 coladvance(curwin->w_curswant);
7726 }
7727 else
7728 curwin->w_set_curswant = TRUE;
7729 redraw_curbuf_later(INVERTED); /* show the inversion */
7730 }
7731 else
7732 {
7733 if (!cap->arg)
7734 /* start Select mode when 'selectmode' contains "cmd" */
7735 may_start_select('c');
7736 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007737 if (VIsual_mode != 'V' && *p_sel == 'e')
7738 ++cap->count1; /* include one more char */
7739 if (cap->count0 > 0 && --cap->count1 > 0)
7740 {
7741 /* With a count select that many characters or lines. */
7742 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7743 nv_right(cap);
7744 else if (VIsual_mode == 'V')
7745 nv_down(cap);
7746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 }
7748 }
7749}
7750
7751/*
7752 * Start selection for Shift-movement keys.
7753 */
7754 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007755start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756{
7757 /* if 'selectmode' contains "key", start Select mode */
7758 may_start_select('k');
7759 n_start_visual_mode('v');
7760}
7761
7762/*
7763 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7764 */
7765 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007766may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767{
7768 VIsual_select = (stuff_empty() && typebuf_typed()
7769 && (vim_strchr(p_slm, c) != NULL));
7770}
7771
7772/*
7773 * Start Visual mode "c".
7774 * Should set VIsual_select before calling this.
7775 */
7776 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007777n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007779#ifdef FEAT_CONCEAL
7780 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007781 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007782#endif
7783
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 VIsual_mode = c;
7785 VIsual_active = TRUE;
7786 VIsual_reselect = TRUE;
7787#ifdef FEAT_VIRTUALEDIT
7788 /* Corner case: the 0 position in a tab may change when going into
7789 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7790 */
7791 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007792 {
7793 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796#endif
7797 VIsual = curwin->w_cursor;
7798
7799#ifdef FEAT_FOLDING
7800 foldAdjustVisual();
7801#endif
7802
7803#ifdef FEAT_MOUSE
7804 setmouse();
7805#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007806#ifdef FEAT_CONCEAL
7807 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007808 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007809#endif
7810
Bram Moolenaar09df3122006-01-23 22:23:09 +00007811 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 redraw_cmdline = TRUE; /* show visual mode later */
7813#ifdef FEAT_CLIPBOARD
7814 /* Make sure the clipboard gets updated. Needed because start and
7815 * end may still be the same, and the selection needs to be owned */
7816 clip_star.vmode = NUL;
7817#endif
7818
7819 /* Only need to redraw this line, unless still need to redraw an old
7820 * Visual area (when 'lazyredraw' is set). */
7821 if (curwin->w_redr_type < INVERTED)
7822 {
7823 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7824 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7825 }
7826}
7827
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828
7829/*
7830 * CTRL-W: Window commands
7831 */
7832 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007833nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834{
7835#ifdef FEAT_WINDOWS
7836 if (!checkclearop(cap->oap))
7837 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7838#else
7839 (void)checkclearop(cap->oap);
7840#endif
7841}
7842
7843/*
7844 * CTRL-Z: Suspend
7845 */
7846 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007847nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848{
7849 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 if (VIsual_active)
7851 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 do_cmdline_cmd((char_u *)"st");
7853}
7854
7855/*
7856 * Commands starting with "g".
7857 */
7858 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007859nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860{
7861 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 int i;
7864 int flag = FALSE;
7865
7866 switch (cap->nchar)
7867 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007868 case Ctrl_A:
7869 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870#ifdef MEM_PROFILE
7871 /*
7872 * "g^A": dump log of used memory.
7873 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007874 if (!VIsual_active && cap->nchar == Ctrl_A)
7875 vim_mem_profile_dump();
7876 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007878 /*
7879 * "g^A/g^X": sequentially increment visually selected region
7880 */
7881 if (VIsual_active)
7882 {
7883 cap->arg = TRUE;
7884 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01007885 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007886 nv_addsub(cap);
7887 }
7888 else
7889 clearopbeep(oap);
7890 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891
7892#ifdef FEAT_VREPLACE
7893 /*
7894 * "gR": Enter virtual replace mode.
7895 */
7896 case 'R':
7897 cap->arg = TRUE;
7898 nv_Replace(cap);
7899 break;
7900
7901 case 'r':
7902 nv_vreplace(cap);
7903 break;
7904#endif
7905
7906 case '&':
7907 do_cmdline_cmd((char_u *)"%s//~/&");
7908 break;
7909
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 /*
7911 * "gv": Reselect the previous Visual area. If Visual already active,
7912 * exchange previous and current Visual area.
7913 */
7914 case 'v':
7915 if (checkclearop(oap))
7916 break;
7917
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007918 if ( curbuf->b_visual.vi_start.lnum == 0
7919 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7920 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 beep_flush();
7922 else
7923 {
7924 /* set w_cursor to the start of the Visual area, tpos to the end */
7925 if (VIsual_active)
7926 {
7927 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007928 VIsual_mode = curbuf->b_visual.vi_mode;
7929 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930# ifdef FEAT_EVAL
7931 curbuf->b_visual_mode_eval = i;
7932# endif
7933 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007934 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7935 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007937 tpos = curbuf->b_visual.vi_end;
7938 curbuf->b_visual.vi_end = curwin->w_cursor;
7939 curwin->w_cursor = curbuf->b_visual.vi_start;
7940 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 }
7942 else
7943 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007944 VIsual_mode = curbuf->b_visual.vi_mode;
7945 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7946 tpos = curbuf->b_visual.vi_end;
7947 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 }
7949
7950 VIsual_active = TRUE;
7951 VIsual_reselect = TRUE;
7952
7953 /* Set Visual to the start and w_cursor to the end of the Visual
7954 * area. Make sure they are on an existing character. */
7955 check_cursor();
7956 VIsual = curwin->w_cursor;
7957 curwin->w_cursor = tpos;
7958 check_cursor();
7959 update_topline();
7960 /*
7961 * When called from normal "g" command: start Select mode when
7962 * 'selectmode' contains "cmd". When called for K_SELECT, always
7963 * start Select mode.
7964 */
7965 if (cap->arg)
7966 VIsual_select = TRUE;
7967 else
7968 may_start_select('c');
7969#ifdef FEAT_MOUSE
7970 setmouse();
7971#endif
7972#ifdef FEAT_CLIPBOARD
7973 /* Make sure the clipboard gets updated. Needed because start and
7974 * end are still the same, and the selection needs to be owned */
7975 clip_star.vmode = NUL;
7976#endif
7977 redraw_curbuf_later(INVERTED);
7978 showmode();
7979 }
7980 break;
7981 /*
7982 * "gV": Don't reselect the previous Visual area after a Select mode
7983 * mapping of menu.
7984 */
7985 case 'V':
7986 VIsual_reselect = FALSE;
7987 break;
7988
7989 /*
7990 * "gh": start Select mode.
7991 * "gH": start Select line mode.
7992 * "g^H": start Select block mode.
7993 */
7994 case K_BS:
7995 cap->nchar = Ctrl_H;
7996 /* FALLTHROUGH */
7997 case 'h':
7998 case 'H':
7999 case Ctrl_H:
8000# ifdef EBCDIC
8001 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
8002 if (cap->nchar == Ctrl_H)
8003 cap->cmdchar = Ctrl_V;
8004 else
8005# endif
8006 cap->cmdchar = cap->nchar + ('v' - 'h');
8007 cap->arg = TRUE;
8008 nv_visual(cap);
8009 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02008010
8011 /* "gn", "gN" visually select next/previous search match
8012 * "gn" selects next match
8013 * "gN" selects previous match
8014 */
8015 case 'N':
8016 case 'n':
8017 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02008018 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02008019 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020
8021 /*
8022 * "gj" and "gk" two new funny movement keys -- up and down
8023 * movement based on *screen* line rather than *file* line.
8024 */
8025 case 'j':
8026 case K_DOWN:
8027 /* with 'nowrap' it works just like the normal "j" command; also when
8028 * in a closed fold */
8029 if (!curwin->w_p_wrap
8030#ifdef FEAT_FOLDING
8031 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8032#endif
8033 )
8034 {
8035 oap->motion_type = MLINE;
8036 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8037 }
8038 else
8039 i = nv_screengo(oap, FORWARD, cap->count1);
8040 if (i == FAIL)
8041 clearopbeep(oap);
8042 break;
8043
8044 case 'k':
8045 case K_UP:
8046 /* with 'nowrap' it works just like the normal "k" command; also when
8047 * in a closed fold */
8048 if (!curwin->w_p_wrap
8049#ifdef FEAT_FOLDING
8050 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8051#endif
8052 )
8053 {
8054 oap->motion_type = MLINE;
8055 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8056 }
8057 else
8058 i = nv_screengo(oap, BACKWARD, cap->count1);
8059 if (i == FAIL)
8060 clearopbeep(oap);
8061 break;
8062
8063 /*
8064 * "gJ": join two lines without inserting a space.
8065 */
8066 case 'J':
8067 nv_join(cap);
8068 break;
8069
8070 /*
8071 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8072 * "gm": middle of "g0" and "g$".
8073 */
8074 case '^':
8075 flag = TRUE;
8076 /* FALLTHROUGH */
8077
8078 case '0':
8079 case 'm':
8080 case K_HOME:
8081 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 oap->motion_type = MCHAR;
8083 oap->inclusive = FALSE;
8084 if (curwin->w_p_wrap
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008085#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 && curwin->w_width != 0
8087#endif
8088 )
8089 {
8090 int width1 = W_WIDTH(curwin) - curwin_col_off();
8091 int width2 = width1 + curwin_col_off2();
8092
8093 validate_virtcol();
8094 i = 0;
8095 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8096 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8097 }
8098 else
8099 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008100 /* Go to the middle of the screen line. When 'number' or
8101 * 'relativenumber' is on and lines are wrapping the middle can be more
8102 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 if (cap->nchar == 'm')
8104 i += (W_WIDTH(curwin) - curwin_col_off()
8105 + ((curwin->w_p_wrap && i > 0)
8106 ? curwin_col_off2() : 0)) / 2;
8107 coladvance((colnr_T)i);
8108 if (flag)
8109 {
8110 do
8111 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01008112 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 }
8114 curwin->w_set_curswant = TRUE;
8115 break;
8116
8117 case '_':
8118 /* "g_": to the last non-blank character in the line or <count> lines
8119 * downward. */
8120 cap->oap->motion_type = MCHAR;
8121 cap->oap->inclusive = TRUE;
8122 curwin->w_curswant = MAXCOL;
8123 if (cursor_down((long)(cap->count1 - 1),
8124 cap->oap->op_type == OP_NOP) == FAIL)
8125 clearopbeep(cap->oap);
8126 else
8127 {
8128 char_u *ptr = ml_get_curline();
8129
8130 /* In Visual mode we may end up after the line. */
8131 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8132 --curwin->w_cursor.col;
8133
8134 /* Decrease the cursor column until it's on a non-blank. */
8135 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01008136 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 --curwin->w_cursor.col;
8138 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008139 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 }
8141 break;
8142
8143 case '$':
8144 case K_END:
8145 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 {
8147 int col_off = curwin_col_off();
8148
8149 oap->motion_type = MCHAR;
8150 oap->inclusive = TRUE;
8151 if (curwin->w_p_wrap
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008152#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 && curwin->w_width != 0
8154#endif
8155 )
8156 {
8157 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8158 if (cap->count1 == 1)
8159 {
8160 int width1 = W_WIDTH(curwin) - col_off;
8161 int width2 = width1 + curwin_col_off2();
8162
8163 validate_virtcol();
8164 i = width1 - 1;
8165 if (curwin->w_virtcol >= (colnr_T)width1)
8166 i += ((curwin->w_virtcol - width1) / width2 + 1)
8167 * width2;
8168 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008169
8170 /* Make sure we stick in this column. */
8171 validate_virtcol();
8172 curwin->w_curswant = curwin->w_virtcol;
8173 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8175 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8176 {
8177 /*
8178 * Check for landing on a character that got split at
8179 * the end of the line. We do not want to advance to
8180 * the next screen line.
8181 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 if (curwin->w_virtcol > (colnr_T)i)
8183 --curwin->w_cursor.col;
8184 }
8185#endif
8186 }
8187 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8188 clearopbeep(oap);
8189 }
8190 else
8191 {
8192 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8193 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008194
8195 /* Make sure we stick in this column. */
8196 validate_virtcol();
8197 curwin->w_curswant = curwin->w_virtcol;
8198 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 }
8200 }
8201 break;
8202
8203 /*
8204 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8205 */
8206 case '*':
8207 case '#':
8208#if POUND != '#'
8209 case POUND: /* pound sign (sometimes equal to '#') */
8210#endif
8211 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8212 case ']': /* :tselect for current identifier */
8213 nv_ident(cap);
8214 break;
8215
8216 /*
8217 * ge and gE: go back to end of word
8218 */
8219 case 'e':
8220 case 'E':
8221 oap->motion_type = MCHAR;
8222 curwin->w_set_curswant = TRUE;
8223 oap->inclusive = TRUE;
8224 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8225 clearopbeep(oap);
8226 break;
8227
8228 /*
8229 * "g CTRL-G": display info about cursor position
8230 */
8231 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01008232 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 break;
8234
8235 /*
8236 * "gi": start Insert at the last position.
8237 */
8238 case 'i':
8239 if (curbuf->b_last_insert.lnum != 0)
8240 {
8241 curwin->w_cursor = curbuf->b_last_insert;
8242 check_cursor_lnum();
8243 i = (int)STRLEN(ml_get_curline());
8244 if (curwin->w_cursor.col > (colnr_T)i)
8245 {
8246#ifdef FEAT_VIRTUALEDIT
8247 if (virtual_active())
8248 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8249#endif
8250 curwin->w_cursor.col = i;
8251 }
8252 }
8253 cap->cmdchar = 'i';
8254 nv_edit(cap);
8255 break;
8256
8257 /*
8258 * "gI": Start insert in column 1.
8259 */
8260 case 'I':
8261 beginline(0);
8262 if (!checkclearopq(oap))
8263 invoke_edit(cap, FALSE, 'g', FALSE);
8264 break;
8265
8266#ifdef FEAT_SEARCHPATH
8267 /*
8268 * "gf": goto file, edit file under cursor
8269 * "]f" and "[f": can also be used.
8270 */
8271 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008272 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 nv_gotofile(cap);
8274 break;
8275#endif
8276
8277 /* "g'm" and "g`m": jump to mark without setting pcmark */
8278 case '\'':
8279 cap->arg = TRUE;
8280 /*FALLTHROUGH*/
8281 case '`':
8282 nv_gomark(cap);
8283 break;
8284
8285 /*
8286 * "gs": Goto sleep.
8287 */
8288 case 's':
8289 do_sleep(cap->count1 * 1000L);
8290 break;
8291
8292 /*
8293 * "ga": Display the ascii value of the character under the
8294 * cursor. It is displayed in decimal, hex, and octal. -- webb
8295 */
8296 case 'a':
8297 do_ascii(NULL);
8298 break;
8299
8300#ifdef FEAT_MBYTE
8301 /*
8302 * "g8": Display the bytes used for the UTF-8 character under the
8303 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008304 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 */
8306 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008307 if (cap->count0 == 8)
8308 utf_find_illegal();
8309 else
8310 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 break;
8312#endif
8313
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008314 case '<':
8315 show_sb_text();
8316 break;
8317
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 /*
8319 * "gg": Goto the first line in file. With a count it goes to
8320 * that line number like for "G". -- webb
8321 */
8322 case 'g':
8323 cap->arg = FALSE;
8324 nv_goto(cap);
8325 break;
8326
8327 /*
8328 * Two-character operators:
8329 * "gq" Format text
8330 * "gw" Format text and keep cursor position
8331 * "g~" Toggle the case of the text.
8332 * "gu" Change text to lower case.
8333 * "gU" Change text to upper case.
8334 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008335 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 */
8337 case 'q':
8338 case 'w':
8339 oap->cursor_start = curwin->w_cursor;
8340 /*FALLTHROUGH*/
8341 case '~':
8342 case 'u':
8343 case 'U':
8344 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008345 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 nv_operator(cap);
8347 break;
8348
8349 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008350 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 * current function
8352 * "gD": idem, but in the current file.
8353 */
8354 case 'd':
8355 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008356 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 break;
8358
8359#ifdef FEAT_MOUSE
8360 /*
8361 * g<*Mouse> : <C-*mouse>
8362 */
8363 case K_MIDDLEMOUSE:
8364 case K_MIDDLEDRAG:
8365 case K_MIDDLERELEASE:
8366 case K_LEFTMOUSE:
8367 case K_LEFTDRAG:
8368 case K_LEFTRELEASE:
8369 case K_RIGHTMOUSE:
8370 case K_RIGHTDRAG:
8371 case K_RIGHTRELEASE:
8372 case K_X1MOUSE:
8373 case K_X1DRAG:
8374 case K_X1RELEASE:
8375 case K_X2MOUSE:
8376 case K_X2DRAG:
8377 case K_X2RELEASE:
8378 mod_mask = MOD_MASK_CTRL;
8379 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8380 break;
8381#endif
8382
8383 case K_IGNORE:
8384 break;
8385
8386 /*
8387 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8388 */
8389 case 'p':
8390 case 'P':
8391 nv_put(cap);
8392 break;
8393
8394#ifdef FEAT_BYTEOFF
8395 /* "go": goto byte count from start of buffer */
8396 case 'o':
8397 goto_byte(cap->count0);
8398 break;
8399#endif
8400
8401 /* "gQ": improved Ex mode */
8402 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008403 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 {
8405 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008406 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 break;
8408 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008409
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 if (!checkclearopq(oap))
8411 do_exmode(TRUE);
8412 break;
8413
8414#ifdef FEAT_JUMPLIST
8415 case ',':
8416 nv_pcmark(cap);
8417 break;
8418
8419 case ';':
8420 cap->count1 = -cap->count1;
8421 nv_pcmark(cap);
8422 break;
8423#endif
8424
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008425#ifdef FEAT_WINDOWS
8426 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008427 if (!checkclearop(oap))
8428 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008429 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008430 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008431 if (!checkclearop(oap))
8432 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008433 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008434#endif
8435
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008436 case '+':
8437 case '-': /* "g+" and "g-": undo or redo along the timeline */
8438 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008439 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008440 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008441 break;
8442
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 default:
8444 clearopbeep(oap);
8445 break;
8446 }
8447}
8448
8449/*
8450 * Handle "o" and "O" commands.
8451 */
8452 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008453n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008455#ifdef FEAT_CONCEAL
8456 linenr_T oldline = curwin->w_cursor.lnum;
8457#endif
8458
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 if (!checkclearopq(cap->oap))
8460 {
8461#ifdef FEAT_FOLDING
8462 if (cap->cmdchar == 'O')
8463 /* Open above the first line of a folded sequence of lines */
8464 (void)hasFolding(curwin->w_cursor.lnum,
8465 &curwin->w_cursor.lnum, NULL);
8466 else
8467 /* Open below the last line of a folded sequence of lines */
8468 (void)hasFolding(curwin->w_cursor.lnum,
8469 NULL, &curwin->w_cursor.lnum);
8470#endif
8471 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8472 (cap->cmdchar == 'O' ? 1 : 0)),
8473 (linenr_T)(curwin->w_cursor.lnum +
8474 (cap->cmdchar == 'o' ? 1 : 0))
8475 ) == OK
8476 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8477#ifdef FEAT_COMMENTS
8478 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8479#endif
8480 0, 0))
8481 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008482#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008483 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008484 update_single_line(curwin, oldline);
8485#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008486 /* When '#' is in 'cpoptions' ignore the count. */
8487 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8488 cap->count1 = 1;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008489#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02008490 if (curwin->w_p_cul)
8491 /* force redraw of cursorline */
8492 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008493#endif
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
Bram Moolenaar9b578142016-01-30 19:39:49 +01008503nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504{
8505 if (!checkclearopq(cap->oap))
8506 {
8507 /*
8508 * If "restart_edit" is TRUE, the last but one command is repeated
8509 * instead of the last command (inserting text). This is used for
8510 * CTRL-O <.> in insert mode.
8511 */
8512 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8513 clearopbeep(cap->oap);
8514 }
8515}
8516
8517/*
8518 * CTRL-R: undo undo
8519 */
8520 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008521nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522{
8523 if (!checkclearopq(cap->oap))
8524 {
8525 u_redo((int)cap->count1);
8526 curwin->w_set_curswant = TRUE;
8527 }
8528}
8529
8530/*
8531 * Handle "U" command.
8532 */
8533 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008534nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535{
8536 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008537 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 {
8539 /* translate "gUU" to "gUgU" */
8540 cap->cmdchar = 'g';
8541 cap->nchar = 'U';
8542 nv_operator(cap);
8543 }
8544 else if (!checkclearopq(cap->oap))
8545 {
8546 u_undoline();
8547 curwin->w_set_curswant = TRUE;
8548 }
8549}
8550
8551/*
8552 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8553 * single character.
8554 */
8555 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008556nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008558 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 n_swapchar(cap);
8560 else
8561 nv_operator(cap);
8562}
8563
8564/*
8565 * Handle an operator command.
8566 * The actual work is done by do_pending_operator().
8567 */
8568 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008569nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570{
8571 int op_type;
8572
8573 op_type = get_op_type(cap->cmdchar, cap->nchar);
8574
8575 if (op_type == cap->oap->op_type) /* double operator works on lines */
8576 nv_lineop(cap);
8577 else if (!checkclearop(cap->oap))
8578 {
8579 cap->oap->start = curwin->w_cursor;
8580 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008581#ifdef FEAT_EVAL
8582 set_op_var(op_type);
8583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 }
8585}
8586
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008587#ifdef FEAT_EVAL
8588/*
8589 * Set v:operator to the characters for "optype".
8590 */
8591 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008592set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008593{
8594 char_u opchars[3];
8595
8596 if (optype == OP_NOP)
8597 set_vim_var_string(VV_OP, NULL, 0);
8598 else
8599 {
8600 opchars[0] = get_op_char(optype);
8601 opchars[1] = get_extra_op_char(optype);
8602 opchars[2] = NUL;
8603 set_vim_var_string(VV_OP, opchars, -1);
8604 }
8605}
8606#endif
8607
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608/*
8609 * Handle linewise operator "dd", "yy", etc.
8610 *
8611 * "_" is is a strange motion command that helps make operators more logical.
8612 * It is actually implemented, but not documented in the real Vi. This motion
8613 * command actually refers to "the current line". Commands like "dd" and "yy"
8614 * are really an alternate form of "d_" and "y_". It does accept a count, so
8615 * "d3_" works to delete 3 lines.
8616 */
8617 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008618nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619{
8620 cap->oap->motion_type = MLINE;
8621 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8622 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008623 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8624 && cap->oap->motion_force != 'v'
8625 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 || cap->oap->op_type == OP_LSHIFT
8627 || cap->oap->op_type == OP_RSHIFT)
8628 beginline(BL_SOL | BL_FIX);
8629 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8630 beginline(BL_WHITE | BL_FIX);
8631}
8632
8633/*
8634 * <Home> command.
8635 */
8636 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008637nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008639 /* CTRL-HOME is like "gg" */
8640 if (mod_mask & MOD_MASK_CTRL)
8641 nv_goto(cap);
8642 else
8643 {
8644 cap->count0 = 1;
8645 nv_pipe(cap);
8646 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008647 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8648 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649}
8650
8651/*
8652 * "|" command.
8653 */
8654 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008655nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656{
8657 cap->oap->motion_type = MCHAR;
8658 cap->oap->inclusive = FALSE;
8659 beginline(0);
8660 if (cap->count0 > 0)
8661 {
8662 coladvance((colnr_T)(cap->count0 - 1));
8663 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8664 }
8665 else
8666 curwin->w_curswant = 0;
8667 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008668 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 curwin->w_set_curswant = FALSE;
8670}
8671
8672/*
8673 * Handle back-word command "b" and "B".
8674 * cap->arg is 1 for "B"
8675 */
8676 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008677nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678{
8679 cap->oap->motion_type = MCHAR;
8680 cap->oap->inclusive = FALSE;
8681 curwin->w_set_curswant = TRUE;
8682 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8683 clearopbeep(cap->oap);
8684#ifdef FEAT_FOLDING
8685 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8686 foldOpenCursor();
8687#endif
8688}
8689
8690/*
8691 * Handle word motion commands "e", "E", "w" and "W".
8692 * cap->arg is TRUE for "E" and "W".
8693 */
8694 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008695nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696{
8697 int n;
8698 int word_end;
8699 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008700 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701
8702 /*
8703 * Set inclusive for the "E" and "e" command.
8704 */
8705 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8706 word_end = TRUE;
8707 else
8708 word_end = FALSE;
8709 cap->oap->inclusive = word_end;
8710
8711 /*
8712 * "cw" and "cW" are a special case.
8713 */
8714 if (!word_end && cap->oap->op_type == OP_CHANGE)
8715 {
8716 n = gchar_cursor();
8717 if (n != NUL) /* not an empty line */
8718 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008719 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 {
8721 /*
8722 * Reproduce a funny Vi behaviour: "cw" on a blank only
8723 * changes one character, not all blanks until the start of
8724 * the next word. Only do this when the 'w' flag is included
8725 * in 'cpoptions'.
8726 */
8727 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8728 {
8729 cap->oap->inclusive = TRUE;
8730 cap->oap->motion_type = MCHAR;
8731 return;
8732 }
8733 }
8734 else
8735 {
8736 /*
8737 * This is a little strange. To match what the real Vi does,
8738 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8739 * that we are not on a space or a TAB. This seems impolite
8740 * at first, but it's really more what we mean when we say
8741 * 'cw'.
8742 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008743 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 * will change only one character! This is done by setting
8745 * flag.
8746 */
8747 cap->oap->inclusive = TRUE;
8748 word_end = TRUE;
8749 flag = TRUE;
8750 }
8751 }
8752 }
8753
8754 cap->oap->motion_type = MCHAR;
8755 curwin->w_set_curswant = TRUE;
8756 if (word_end)
8757 n = end_word(cap->count1, cap->arg, flag, FALSE);
8758 else
8759 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8760
Bram Moolenaardfefb982008-04-01 10:06:39 +00008761 /* Don't leave the cursor on the NUL past the end of line. Unless we
8762 * didn't move it forward. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008763 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008764 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765
8766 if (n == FAIL && cap->oap->op_type == OP_NOP)
8767 clearopbeep(cap->oap);
8768 else
8769 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771#ifdef FEAT_FOLDING
8772 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8773 foldOpenCursor();
8774#endif
8775 }
8776}
8777
8778/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008779 * Used after a movement command: If the cursor ends up on the NUL after the
8780 * end of the line, may move it back to the last character and make the motion
8781 * inclusive.
8782 */
8783 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008784adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008785{
8786 /* The cursor cannot remain on the NUL when:
8787 * - the column is > 0
8788 * - not in Visual mode or 'selection' is "o"
8789 * - 'virtualedit' is not "all" and not "onemore".
8790 */
8791 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008792 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008793#ifdef FEAT_VIRTUALEDIT
8794 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8795#endif
8796 )
8797 {
8798 --curwin->w_cursor.col;
8799#ifdef FEAT_MBYTE
8800 /* prevent cursor from moving on the trail byte */
8801 if (has_mbyte)
8802 mb_adjust_cursor();
8803#endif
8804 oap->inclusive = TRUE;
8805 }
8806}
8807
8808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 * "0" and "^" commands.
8810 * cap->arg is the argument for beginline().
8811 */
8812 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008813nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814{
8815 cap->oap->motion_type = MCHAR;
8816 cap->oap->inclusive = FALSE;
8817 beginline(cap->arg);
8818#ifdef FEAT_FOLDING
8819 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8820 foldOpenCursor();
8821#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008822 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8823 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824}
8825
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826/*
8827 * In exclusive Visual mode, may include the last character.
8828 */
8829 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008830adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831{
8832 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008833 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008835#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 if (has_mbyte)
8837 inc_cursor();
8838 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 ++curwin->w_cursor.col;
8841 cap->oap->inclusive = FALSE;
8842 }
8843}
8844
8845/*
8846 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8847 * Should check VIsual_mode before calling this.
8848 * Returns TRUE when backed up to the previous line.
8849 */
8850 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008851unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852{
8853 pos_T *pp;
8854
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008855 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008857 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 pp = &curwin->w_cursor;
8859 else
8860 pp = &VIsual;
8861#ifdef FEAT_VIRTUALEDIT
8862 if (pp->coladd > 0)
8863 --pp->coladd;
8864 else
8865#endif
8866 if (pp->col > 0)
8867 {
8868 --pp->col;
8869#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008870 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871#endif
8872 }
8873 else if (pp->lnum > 1)
8874 {
8875 --pp->lnum;
8876 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8877 return TRUE;
8878 }
8879 }
8880 return FALSE;
8881}
8882
8883/*
8884 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8885 */
8886 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008887nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888{
8889 if (VIsual_active)
8890 VIsual_select = TRUE;
8891 else if (VIsual_reselect)
8892 {
8893 cap->nchar = 'v'; /* fake "gv" command */
8894 cap->arg = TRUE;
8895 nv_g_cmd(cap);
8896 }
8897}
8898
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899
8900/*
8901 * "G", "gg", CTRL-END, CTRL-HOME.
8902 * cap->arg is TRUE for "G".
8903 */
8904 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008905nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906{
8907 linenr_T lnum;
8908
8909 if (cap->arg)
8910 lnum = curbuf->b_ml.ml_line_count;
8911 else
8912 lnum = 1L;
8913 cap->oap->motion_type = MLINE;
8914 setpcmark();
8915
8916 /* When a count is given, use it instead of the default lnum */
8917 if (cap->count0 != 0)
8918 lnum = cap->count0;
8919 if (lnum < 1L)
8920 lnum = 1L;
8921 else if (lnum > curbuf->b_ml.ml_line_count)
8922 lnum = curbuf->b_ml.ml_line_count;
8923 curwin->w_cursor.lnum = lnum;
8924 beginline(BL_SOL | BL_FIX);
8925#ifdef FEAT_FOLDING
8926 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8927 foldOpenCursor();
8928#endif
8929}
8930
8931/*
8932 * CTRL-\ in Normal mode.
8933 */
8934 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008935nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936{
8937 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8938 {
8939 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008940 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 clear_cmdline = TRUE; /* unshow mode later */
8942 restart_edit = 0;
8943#ifdef FEAT_CMDWIN
8944 if (cmdwin_type != 0)
8945 cmdwin_result = Ctrl_C;
8946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 if (VIsual_active)
8948 {
8949 end_visual_mode(); /* stop Visual */
8950 redraw_curbuf_later(INVERTED);
8951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8953 if (cap->nchar == Ctrl_G && p_im)
8954 restart_edit = 'a';
8955 }
8956 else
8957 clearopbeep(cap->oap);
8958}
8959
8960/*
8961 * ESC in Normal mode: beep, but don't flush buffers.
8962 * Don't even beep if we are canceling a command.
8963 */
8964 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008965nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966{
8967 int no_reason;
8968
8969 no_reason = (cap->oap->op_type == OP_NOP
8970 && cap->opcount == 0
8971 && cap->count0 == 0
8972 && cap->oap->regname == 0
8973 && !p_im);
8974
8975 if (cap->arg) /* TRUE for CTRL-C */
8976 {
8977 if (restart_edit == 0
8978#ifdef FEAT_CMDWIN
8979 && cmdwin_type == 0
8980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 && no_reason)
8983 MSG(_("Type :quit<Enter> to exit Vim"));
8984
8985 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8986 * set again below when halfway a mapping. */
8987 if (!p_im)
8988 restart_edit = 0;
8989#ifdef FEAT_CMDWIN
8990 if (cmdwin_type != 0)
8991 {
8992 cmdwin_result = K_IGNORE;
8993 got_int = FALSE; /* don't stop executing autocommands et al. */
8994 return;
8995 }
8996#endif
8997 }
8998
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 if (VIsual_active)
9000 {
9001 end_visual_mode(); /* stop Visual */
9002 check_cursor_col(); /* make sure cursor is not beyond EOL */
9003 curwin->w_set_curswant = TRUE;
9004 redraw_curbuf_later(INVERTED);
9005 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009006 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009007 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 clearop(cap->oap);
9009
9010 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9011 * set return to Insert mode afterwards. */
Bram Moolenaare2c38102016-01-31 14:55:40 +01009012 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 restart_edit = 'a';
9014}
9015
9016/*
9017 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009018 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 */
9020 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009021nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022{
9023 /* <Insert> is equal to "i" */
9024 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9025 cap->cmdchar = 'i';
9026
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 /* in Visual mode "A" and "I" are an operator */
9028 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
9029 v_visop(cap);
9030
9031 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009032 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9033 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 {
9035#ifdef FEAT_TEXTOBJ
9036 nv_object(cap);
9037#else
9038 clearopbeep(cap->oap);
9039#endif
9040 }
9041 else if (!curbuf->b_p_ma && !p_im)
9042 {
9043 /* Only give this error when 'insertmode' is off. */
9044 EMSG(_(e_modifiable));
9045 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009046 if (cap->cmdchar == K_PS)
9047 /* drop the pasted text */
9048 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 }
Bram Moolenaara1891842017-02-04 21:34:31 +01009050 else if (cap->cmdchar == K_PS && VIsual_active)
9051 {
9052 pos_T old_pos = curwin->w_cursor;
9053 pos_T old_visual = VIsual;
9054
9055 /* In Visual mode the selected text is deleted. */
9056 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
9057 {
9058 shift_delete_registers();
9059 cap->oap->regname = '1';
9060 }
9061 else
9062 cap->oap->regname = '-';
9063 cap->cmdchar = 'd';
9064 cap->nchar = NUL;
9065 nv_operator(cap);
9066 do_pending_operator(cap, 0, FALSE);
9067 cap->cmdchar = K_PS;
9068
9069 /* When the last char in the line was deleted then append. Detect this
9070 * by checking if the cursor moved to before the Visual area. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009071 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
9072 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01009073 inc_cursor();
9074
9075 /* Insert to replace the deleted text with the pasted text. */
9076 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 else if (!checkclearopq(cap->oap))
9079 {
9080 switch (cap->cmdchar)
9081 {
9082 case 'A': /* "A"ppend after the line */
9083 curwin->w_set_curswant = TRUE;
9084#ifdef FEAT_VIRTUALEDIT
9085 if (ve_flags == VE_ALL)
9086 {
9087 int save_State = State;
9088
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009089 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 * character past the end of the line */
9091 State = INSERT;
9092 coladvance((colnr_T)MAXCOL);
9093 State = save_State;
9094 }
9095 else
9096#endif
9097 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9098 break;
9099
9100 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009101 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9102 beginline(BL_WHITE);
9103 else
9104 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 break;
9106
Bram Moolenaara1891842017-02-04 21:34:31 +01009107 case K_PS:
9108 /* Bracketed paste works like "a"ppend, unless the cursor is in
9109 * the first column, then it inserts. */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009110 if (curwin->w_cursor.col == 0)
9111 break;
9112 /*FALLTHROUGH*/
9113
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9115#ifdef FEAT_VIRTUALEDIT
9116 /* increment coladd when in virtual space, increment the
9117 * column otherwise, also to append after an unprintable char */
9118 if (virtual_active()
9119 && (curwin->w_cursor.coladd > 0
9120 || *ml_get_cursor() == NUL
9121 || *ml_get_cursor() == TAB))
9122 curwin->w_cursor.coladd++;
9123 else
9124#endif
9125 if (*ml_get_cursor() != NUL)
9126 inc_cursor();
9127 break;
9128 }
9129
9130#ifdef FEAT_VIRTUALEDIT
9131 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9132 {
9133 int save_State = State;
9134
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009135 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 * character past the end of the line */
9137 State = INSERT;
9138 coladvance(getviscol());
9139 State = save_State;
9140 }
9141#endif
9142
9143 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9144 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009145 else if (cap->cmdchar == K_PS)
9146 /* drop the pasted text */
9147 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148}
9149
9150/*
9151 * Invoke edit() and take care of "restart_edit" and the return value.
9152 */
9153 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009154invoke_edit(
9155 cmdarg_T *cap,
9156 int repl, /* "r" or "gr" command */
9157 int cmd,
9158 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159{
9160 int restart_edit_save = 0;
9161
9162 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9163 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9164 * it. */
9165 if (repl || !stuff_empty())
9166 restart_edit_save = restart_edit;
9167 else
9168 restart_edit_save = 0;
9169
9170 /* Always reset "restart_edit", this is not a restarted edit. */
9171 restart_edit = 0;
9172
9173 if (edit(cmd, startln, cap->count1))
9174 cap->retval |= CA_COMMAND_BUSY;
9175
9176 if (restart_edit == 0)
9177 restart_edit = restart_edit_save;
9178}
9179
9180#ifdef FEAT_TEXTOBJ
9181/*
9182 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9183 */
9184 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009185nv_object(
9186 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187{
9188 int flag;
9189 int include;
9190 char_u *mps_save;
9191
9192 if (cap->cmdchar == 'i')
9193 include = FALSE; /* "ix" = inner object: exclude white space */
9194 else
9195 include = TRUE; /* "ax" = an object: include white space */
9196
9197 /* Make sure (), [], {} and <> are in 'matchpairs' */
9198 mps_save = curbuf->b_p_mps;
9199 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9200
9201 switch (cap->nchar)
9202 {
9203 case 'w': /* "aw" = a word */
9204 flag = current_word(cap->oap, cap->count1, include, FALSE);
9205 break;
9206 case 'W': /* "aW" = a WORD */
9207 flag = current_word(cap->oap, cap->count1, include, TRUE);
9208 break;
9209 case 'b': /* "ab" = a braces block */
9210 case '(':
9211 case ')':
9212 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9213 break;
9214 case 'B': /* "aB" = a Brackets block */
9215 case '{':
9216 case '}':
9217 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9218 break;
9219 case '[': /* "a[" = a [] block */
9220 case ']':
9221 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9222 break;
9223 case '<': /* "a<" = a <> block */
9224 case '>':
9225 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9226 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009227 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01009228 /* Do not adjust oap->end in do_pending_operator()
9229 * otherwise there are different results for 'dit'
9230 * (note leading whitespace in last line):
9231 * 1) <b> 2) <b>
9232 * foobar foobar
9233 * </b> </b>
9234 */
9235 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009236 flag = current_tagblock(cap->oap, cap->count1, include);
9237 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 case 'p': /* "ap" = a paragraph */
9239 flag = current_par(cap->oap, cap->count1, include, 'p');
9240 break;
9241 case 's': /* "as" = a sentence */
9242 flag = current_sent(cap->oap, cap->count1, include);
9243 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009244 case '"': /* "a"" = a double quoted string */
9245 case '\'': /* "a'" = a single quoted string */
9246 case '`': /* "a`" = a backtick quoted string */
9247 flag = current_quote(cap->oap, cap->count1, include,
9248 cap->nchar);
9249 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250#if 0 /* TODO */
9251 case 'S': /* "aS" = a section */
9252 case 'f': /* "af" = a filename */
9253 case 'u': /* "au" = a URL */
9254#endif
9255 default:
9256 flag = FAIL;
9257 break;
9258 }
9259
9260 curbuf->b_p_mps = mps_save;
9261 if (flag == FAIL)
9262 clearopbeep(cap->oap);
9263 adjust_cursor_col();
9264 curwin->w_set_curswant = TRUE;
9265}
9266#endif
9267
9268/*
9269 * "q" command: Start/stop recording.
9270 * "q:", "q/", "q?": edit command-line in command-line window.
9271 */
9272 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009273nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274{
9275 if (cap->oap->op_type == OP_FORMAT)
9276 {
9277 /* "gqq" is the same as "gqgq": format line */
9278 cap->cmdchar = 'g';
9279 cap->nchar = 'q';
9280 nv_operator(cap);
9281 }
9282 else if (!checkclearop(cap->oap))
9283 {
9284#ifdef FEAT_CMDWIN
9285 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9286 {
9287 stuffcharReadbuff(cap->nchar);
9288 stuffcharReadbuff(K_CMDWIN);
9289 }
9290 else
9291#endif
9292 /* (stop) recording into a named register, unless executing a
9293 * register */
9294 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9295 clearopbeep(cap->oap);
9296 }
9297}
9298
9299/*
9300 * Handle the "@r" command.
9301 */
9302 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009303nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304{
9305 if (checkclearop(cap->oap))
9306 return;
9307#ifdef FEAT_EVAL
9308 if (cap->nchar == '=')
9309 {
9310 if (get_expr_register() == NUL)
9311 return;
9312 }
9313#endif
9314 while (cap->count1-- && !got_int)
9315 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009316 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 {
9318 clearopbeep(cap->oap);
9319 break;
9320 }
9321 line_breakcheck();
9322 }
9323}
9324
9325/*
9326 * Handle the CTRL-U and CTRL-D commands.
9327 */
9328 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009329nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330{
9331 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9332 || (cap->cmdchar == Ctrl_D
9333 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9334 clearopbeep(cap->oap);
9335 else if (!checkclearop(cap->oap))
9336 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9337}
9338
9339/*
9340 * Handle "J" or "gJ" command.
9341 */
9342 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009343nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 if (VIsual_active) /* join the visual lines */
9346 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009347 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 {
9349 if (cap->count0 <= 1)
9350 cap->count0 = 2; /* default for join is two lines! */
9351 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9352 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 {
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009354 /* can't join when on the last line */
9355 if (cap->count0 <= 2)
9356 {
9357 clearopbeep(cap->oap);
9358 return;
9359 }
9360 cap->count0 = curbuf->b_ml.ml_line_count
9361 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009363
9364 prep_redo(cap->oap->regname, cap->count0,
9365 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9366 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 }
9368}
9369
9370/*
9371 * "P", "gP", "p" and "gp" commands.
9372 */
9373 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009374nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 int regname = 0;
9377 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009378 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009379 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 int dir;
9381 int flags = 0;
9382
9383 if (cap->oap->op_type != OP_NOP)
9384 {
9385#ifdef FEAT_DIFF
9386 /* "dp" is ":diffput" */
9387 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9388 {
9389 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009390 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 }
9392 else
9393#endif
9394 clearopbeep(cap->oap);
9395 }
9396 else
9397 {
9398 dir = (cap->cmdchar == 'P'
9399 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9400 ? BACKWARD : FORWARD;
9401 prep_redo_cmd(cap);
9402 if (cap->cmdchar == 'g')
9403 flags |= PUT_CURSEND;
9404
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 if (VIsual_active)
9406 {
9407 /* Putting in Visual mode: The put text replaces the selected
9408 * text. First delete the selected text, then put the new text.
9409 * Need to save and restore the registers that the delete
9410 * overwrites if the old contents is being put.
9411 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009412 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009414#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009416#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009417 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009418 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009419#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422
9423 )
9424 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009425 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 * put, save it first. */
9427 reg1 = get_register(regname, TRUE);
9428 }
9429
9430 /* Now delete the selected text. */
9431 cap->cmdchar = 'd';
9432 cap->nchar = NUL;
9433 cap->oap->regname = NUL;
9434 nv_operator(cap);
9435 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009436 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437
9438 /* delete PUT_LINE_BACKWARD; */
9439 cap->oap->regname = regname;
9440
9441 if (reg1 != NULL)
9442 {
9443 /* Delete probably changed the register we want to put, save
9444 * it first. Then put back what was there before the delete. */
9445 reg2 = get_register(regname, FALSE);
9446 put_register(regname, reg1);
9447 }
9448
9449 /* When deleted a linewise Visual area, put the register as
9450 * lines to avoid it joined with the next line. When deletion was
9451 * characterwise, split a line when putting lines. */
9452 if (VIsual_mode == 'V')
9453 flags |= PUT_LINE;
9454 else if (VIsual_mode == 'v')
9455 flags |= PUT_LINE_SPLIT;
9456 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9457 flags |= PUT_LINE_FORWARD;
9458 dir = BACKWARD;
9459 if ((VIsual_mode != 'V'
9460 && curwin->w_cursor.col < curbuf->b_op_start.col)
9461 || (VIsual_mode == 'V'
9462 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9463 /* cursor is at the end of the line or end of file, put
9464 * forward. */
9465 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009466 /* May have been reset in do_put(). */
9467 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 do_put(cap->oap->regname, dir, cap->count1, flags);
9470
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 /* If a register was saved, put it back now. */
9472 if (reg2 != NULL)
9473 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009474
9475 /* What to reselect with "gv"? Selecting the just put text seems to
9476 * be the most useful, since the original text was removed. */
9477 if (was_visual)
9478 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009479 curbuf->b_visual.vi_start = curbuf->b_op_start;
9480 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01009481 /* need to adjust cursor position */
9482 if (*p_sel == 'e')
9483 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009484 }
9485
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009486 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009487 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009488 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009489 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009490 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009491
9492 /* If the cursor was in that line, move it to the end of the last
9493 * line. */
9494 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9495 {
9496 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9497 coladvance((colnr_T)MAXCOL);
9498 }
9499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 auto_format(FALSE, TRUE);
9501 }
9502}
9503
9504/*
9505 * "o" and "O" commands.
9506 */
9507 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009508nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509{
9510#ifdef FEAT_DIFF
9511 /* "do" is ":diffget" */
9512 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9513 {
9514 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009515 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 }
9517 else
9518#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 if (VIsual_active) /* switch start and end of visual */
9520 v_swap_corners(cap->cmdchar);
9521 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 n_opencmd(cap);
9523}
9524
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525#ifdef FEAT_NETBEANS_INTG
9526 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009527nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528{
9529 netbeans_keycommand(cap->nchar);
9530}
9531#endif
9532
9533#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009535nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536{
9537 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9538}
9539#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009540
9541#ifdef FEAT_AUTOCMD
9542/*
9543 * Trigger CursorHold event.
9544 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9545 * input buffer. "did_cursorhold" is set to avoid retriggering.
9546 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009547 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009548nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00009549{
9550 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9551 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009552 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009553}
9554#endif
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009555
9556/*
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009557 * Calculate start/end virtual columns for operating in block mode.
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009558 */
9559 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009560get_op_vcol(
9561 oparg_T *oap,
9562 colnr_T redo_VIsual_vcol,
9563 int initial) /* when TRUE adjust position for 'selectmode' */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009564{
9565 colnr_T start, end;
9566
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009567 if (VIsual_mode != Ctrl_V
9568 || (!initial && oap->end.col < W_WIDTH(curwin)))
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009569 return;
9570
Bram Moolenaar10ad1d92015-09-25 19:35:02 +02009571 oap->block_mode = TRUE;
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009572
9573#ifdef FEAT_MBYTE
9574 /* prevent from moving onto a trail byte */
9575 if (has_mbyte)
9576 mb_adjustpos(curwin->w_buffer, &oap->end);
9577#endif
9578
9579 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009580
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009581 if (!redo_VIsual_busy)
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009582 {
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009583 getvvcol(curwin, &(oap->end), &start, NULL, &end);
9584
9585 if (start < oap->start_vcol)
9586 oap->start_vcol = start;
9587 if (end > oap->end_vcol)
9588 {
9589 if (initial && *p_sel == 'e' && start >= 1
9590 && start - 1 >= oap->end_vcol)
9591 oap->end_vcol = start - 1;
9592 else
9593 oap->end_vcol = end;
9594 }
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009595 }
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009596
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009597 /* if '$' was used, get oap->end_vcol from longest line */
9598 if (curwin->w_curswant == MAXCOL)
9599 {
9600 curwin->w_cursor.col = MAXCOL;
9601 oap->end_vcol = 0;
9602 for (curwin->w_cursor.lnum = oap->start.lnum;
9603 curwin->w_cursor.lnum <= oap->end.lnum;
9604 ++curwin->w_cursor.lnum)
9605 {
9606 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
9607 if (end > oap->end_vcol)
9608 oap->end_vcol = end;
9609 }
9610 }
9611 else if (redo_VIsual_busy)
9612 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
9613 /*
9614 * Correct oap->end.col and oap->start.col to be the
9615 * upper-left and lower-right corner of the block area.
9616 *
9617 * (Actually, this does convert column positions into character
9618 * positions)
9619 */
9620 curwin->w_cursor.lnum = oap->end.lnum;
9621 coladvance(oap->end_vcol);
9622 oap->end = curwin->w_cursor;
9623
9624 curwin->w_cursor = oap->start;
9625 coladvance(oap->start_vcol);
9626 oap->start = curwin->w_cursor;
9627}