blob: ea963667d48e7ab3a63941d1ea01abf306a9cb5d [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 Moolenaar6fe15bb2017-08-16 21:09:18 +02001311#ifdef FEAT_TERMINAL
1312 /* don't go to Insert mode from Terminal-Job mode */
1313 if (term_use_loop())
1314 restart_edit = 0;
1315#endif
1316
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 /*
1318 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1319 * if still inside a mapping that started in Visual mode).
1320 * May switch from Visual to Select mode after CTRL-O command.
1321 */
1322 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1324 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 && !(ca.retval & CA_COMMAND_BUSY)
1326 && stuff_empty()
1327 && oap->regname == 0)
1328 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 if (restart_VIsual_select == 1)
1330 {
1331 VIsual_select = TRUE;
1332 showmode();
1333 restart_VIsual_select = 0;
1334 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001335 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 (void)edit(restart_edit, FALSE, 1L);
1337 }
1338
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 if (restart_VIsual_select == 2)
1340 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341
1342 /* Save count before an operator for next time. */
1343 opcount = ca.opcount;
1344}
1345
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001346#ifdef FEAT_EVAL
1347/*
1348 * Set v:count and v:count1 according to "cap".
1349 * Set v:prevcount only when "set_prevcount" is TRUE.
1350 */
1351 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001352set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001353{
1354 long count = cap->count0;
1355
1356 /* multiply with cap->opcount the same way as above */
1357 if (cap->opcount != 0)
1358 count = cap->opcount * (count == 0 ? 1 : count);
1359 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1360 *set_prevcount = FALSE; /* only set v:prevcount once */
1361}
1362#endif
1363
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364/*
1365 * Handle an operator after visual mode or when the movement is finished
1366 */
1367 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001368do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369{
1370 oparg_T *oap = cap->oap;
1371 pos_T old_cursor;
1372 int empty_region_error;
1373 int restart_edit_save;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001374#ifdef FEAT_LINEBREAK
1375 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 /* The visual area is remembered for redo */
1379 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1380 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001381 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaard79e5502016-01-10 22:13:02 +01001383 static int redo_VIsual_arg; /* extra argument */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001384#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 int include_line_break = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386#endif
1387
1388#if defined(FEAT_CLIPBOARD)
1389 /*
1390 * Yank the visual area into the GUI selection register before we operate
1391 * on it and lose it forever.
1392 * Don't do it if a specific register was specified, so that ""x"*P works.
1393 * This could call do_pending_operator() recursively, but that's OK
1394 * because gui_yank will be TRUE for the nested call.
1395 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001396 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 && oap->op_type != OP_NOP
1398 && !gui_yank
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 && VIsual_active
1400 && !redo_VIsual_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 && oap->regname == 0)
1402 clip_auto_select();
1403#endif
1404 old_cursor = curwin->w_cursor;
1405
1406 /*
1407 * If an operation is pending, handle it...
1408 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001409 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 {
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001411#ifdef FEAT_LINEBREAK
1412 /* Avoid a problem with unwanted linebreaks in block mode. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001413 if (curwin->w_p_lbr)
1414 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001415 curwin->w_p_lbr = FALSE;
1416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 oap->is_VIsual = VIsual_active;
1418 if (oap->motion_force == 'V')
1419 oap->motion_type = MLINE;
1420 else if (oap->motion_force == 'v')
1421 {
1422 /* If the motion was linewise, "inclusive" will not have been set.
1423 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1424 if (oap->motion_type == MLINE)
1425 oap->inclusive = FALSE;
1426 /* If the motion already was characterwise, toggle "inclusive" */
1427 else if (oap->motion_type == MCHAR)
1428 oap->inclusive = !oap->inclusive;
1429 oap->motion_type = MCHAR;
1430 }
1431 else if (oap->motion_force == Ctrl_V)
1432 {
1433 /* Change line- or characterwise motion into Visual block mode. */
1434 VIsual_active = TRUE;
1435 VIsual = oap->start;
1436 VIsual_mode = Ctrl_V;
1437 VIsual_select = FALSE;
1438 VIsual_reselect = FALSE;
1439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
Bram Moolenaar7afea822013-04-24 18:34:45 +02001441 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1442 /* Never redo "zf" (define fold). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
Bram Moolenaar7afea822013-04-24 18:34:45 +02001444 && ((!VIsual_active || oap->motion_force)
1445 /* Also redo Operator-pending Visual mode mappings */
1446 || (VIsual_active && cap->cmdchar == ':'
1447 && oap->op_type != OP_COLON))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001448 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449#ifdef FEAT_FOLDING
1450 && oap->op_type != OP_FOLD
1451 && oap->op_type != OP_FOLDOPEN
1452 && oap->op_type != OP_FOLDOPENREC
1453 && oap->op_type != OP_FOLDCLOSE
1454 && oap->op_type != OP_FOLDCLOSEREC
1455 && oap->op_type != OP_FOLDDEL
1456 && oap->op_type != OP_FOLDDELREC
1457#endif
1458 )
1459 {
1460 prep_redo(oap->regname, cap->count0,
1461 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1462 oap->motion_force, cap->cmdchar, cap->nchar);
1463 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1464 {
1465 /*
1466 * If 'cpoptions' does not contain 'r', insert the search
1467 * pattern to really repeat the same command.
1468 */
1469 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001470 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 AppendToRedobuff(NL_STR);
1472 }
1473 else if (cap->cmdchar == ':')
1474 {
1475 /* do_cmdline() has stored the first typed line in
1476 * "repeat_cmdline". When several lines are typed repeating
1477 * won't be possible. */
1478 if (repeat_cmdline == NULL)
1479 ResetRedobuff();
1480 else
1481 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001482 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 AppendToRedobuff(NL_STR);
1484 vim_free(repeat_cmdline);
1485 repeat_cmdline = NULL;
1486 }
1487 }
1488 }
1489
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 if (redo_VIsual_busy)
1491 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001492 /* Redo of an operation on a Visual area. Use the same size from
1493 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 oap->start = curwin->w_cursor;
1495 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1496 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1497 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1498 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001499 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001501 if (VIsual_mode == 'v')
1502 {
1503 if (redo_VIsual_line_count <= 1)
1504 {
1505 validate_virtcol();
1506 curwin->w_curswant =
1507 curwin->w_virtcol + redo_VIsual_vcol - 1;
1508 }
1509 else
1510 curwin->w_curswant = redo_VIsual_vcol;
1511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001513 {
1514 curwin->w_curswant = MAXCOL;
1515 }
1516 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 }
1518 cap->count0 = redo_VIsual_count;
1519 if (redo_VIsual_count != 0)
1520 cap->count1 = redo_VIsual_count;
1521 else
1522 cap->count1 = 1;
1523 }
1524 else if (VIsual_active)
1525 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001526 if (!gui_yank)
1527 {
1528 /* Save the current VIsual area for '< and '> marks, and "gv" */
1529 curbuf->b_visual.vi_start = VIsual;
1530 curbuf->b_visual.vi_end = curwin->w_cursor;
1531 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001532 if (VIsual_mode_orig != NUL)
1533 {
1534 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1535 VIsual_mode_orig = NUL;
1536 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001537 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001539 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542
1543 /* In Select mode, a linewise selection is operated upon like a
Bram Moolenaard009e862015-06-09 20:20:03 +02001544 * characterwise selection.
1545 * Special case: gH<Del> deletes the last line. */
1546 if (VIsual_select && VIsual_mode == 'V'
1547 && cap->oap->op_type != OP_DELETE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001549 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 {
1551 VIsual.col = 0;
1552 curwin->w_cursor.col =
1553 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1554 }
1555 else
1556 {
1557 curwin->w_cursor.col = 0;
1558 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1559 }
1560 VIsual_mode = 'v';
1561 }
1562 /* If 'selection' is "exclusive", backup one character for
1563 * charwise selections. */
1564 else if (VIsual_mode == 'v')
1565 {
1566# ifdef FEAT_VIRTUALEDIT
1567 include_line_break =
1568# endif
1569 unadjust_for_sel();
1570 }
1571
1572 oap->start = VIsual;
1573 if (VIsual_mode == 'V')
1574 oap->start.col = 0;
1575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
1577 /*
1578 * Set oap->start to the first position of the operated text, oap->end
1579 * to the end of the operated text. w_cursor is equal to oap->start.
1580 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001581 if (LT_POS(oap->start, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 {
1583#ifdef FEAT_FOLDING
1584 /* Include folded lines completely. */
1585 if (!VIsual_active)
1586 {
1587 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1588 oap->start.col = 0;
1589 if (hasFolding(curwin->w_cursor.lnum, NULL,
1590 &curwin->w_cursor.lnum))
1591 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1592 }
1593#endif
1594 oap->end = curwin->w_cursor;
1595 curwin->w_cursor = oap->start;
1596
1597 /* w_virtcol may have been updated; if the cursor goes back to its
1598 * previous position w_virtcol becomes invalid and isn't updated
1599 * automatically. */
1600 curwin->w_valid &= ~VALID_VIRTCOL;
1601 }
1602 else
1603 {
1604#ifdef FEAT_FOLDING
1605 /* Include folded lines completely. */
1606 if (!VIsual_active && oap->motion_type == MLINE)
1607 {
1608 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1609 NULL))
1610 curwin->w_cursor.col = 0;
1611 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1612 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1613 }
1614#endif
1615 oap->end = oap->start;
1616 oap->start = curwin->w_cursor;
1617 }
1618
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001619 /* Just in case lines were deleted that make the position invalid. */
1620 check_pos(curwin->w_buffer, &oap->end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1622
1623#ifdef FEAT_VIRTUALEDIT
1624 /* Set "virtual_op" before resetting VIsual_active. */
1625 virtual_op = virtual_active();
1626#endif
1627
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 if (VIsual_active || redo_VIsual_busy)
1629 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001630 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
1632 if (!redo_VIsual_busy && !gui_yank)
1633 {
1634 /*
1635 * Prepare to reselect and redo Visual: this is based on the
1636 * size of the Visual text
1637 */
1638 resel_VIsual_mode = VIsual_mode;
1639 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001640 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001642 {
1643 if (VIsual_mode != Ctrl_V)
1644 getvvcol(curwin, &(oap->end),
1645 NULL, NULL, &oap->end_vcol);
1646 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1647 {
1648 if (VIsual_mode != Ctrl_V)
1649 getvvcol(curwin, &(oap->start),
1650 &oap->start_vcol, NULL, NULL);
1651 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1652 }
1653 else
1654 resel_VIsual_vcol = oap->end_vcol;
1655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 resel_VIsual_line_count = oap->line_count;
1657 }
1658
1659 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1660 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1661 && oap->op_type != OP_COLON
1662#ifdef FEAT_FOLDING
1663 && oap->op_type != OP_FOLD
1664 && oap->op_type != OP_FOLDOPEN
1665 && oap->op_type != OP_FOLDOPENREC
1666 && oap->op_type != OP_FOLDCLOSE
1667 && oap->op_type != OP_FOLDCLOSEREC
1668 && oap->op_type != OP_FOLDDEL
1669 && oap->op_type != OP_FOLDDELREC
1670#endif
1671 && oap->motion_force == NUL
1672 )
1673 {
1674 /* Prepare for redoing. Only use the nchar field for "r",
1675 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001676 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1677 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001678 prep_redo(oap->regname, cap->count0,
1679 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1680 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001681 else if (cap->cmdchar != ':')
Bram Moolenaar641e2862012-07-25 15:06:34 +02001682 prep_redo(oap->regname, 0L, NUL, 'v',
1683 get_op_char(oap->op_type),
1684 get_extra_op_char(oap->op_type),
1685 oap->op_type == OP_REPLACE
1686 ? cap->nchar : NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 if (!redo_VIsual_busy)
1688 {
1689 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001690 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 redo_VIsual_line_count = resel_VIsual_line_count;
1692 redo_VIsual_count = cap->count0;
Bram Moolenaard79e5502016-01-10 22:13:02 +01001693 redo_VIsual_arg = cap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 }
1695 }
1696
1697 /*
1698 * oap->inclusive defaults to TRUE.
1699 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1700 * FALSE. This makes "d}P" and "v}dP" work the same.
1701 */
1702 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1703 oap->inclusive = TRUE;
1704 if (VIsual_mode == 'V')
1705 oap->motion_type = MLINE;
1706 else
1707 {
1708 oap->motion_type = MCHAR;
1709 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001710#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 && (include_line_break || !virtual_op)
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 )
1714 {
1715 oap->inclusive = FALSE;
1716 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001717 * that works on lines only. */
Bram Moolenaard009e862015-06-09 20:20:03 +02001718 if (*p_sel != 'o'
1719 && !op_on_lines(oap->op_type)
1720 && oap->end.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 {
Bram Moolenaard009e862015-06-09 20:20:03 +02001722 ++oap->end.lnum;
1723 oap->end.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001724#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard009e862015-06-09 20:20:03 +02001725 oap->end.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001726#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02001727 ++oap->line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 }
1729 }
1730 }
1731
1732 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001733
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 /*
1735 * Switch Visual off now, so screen updating does
1736 * not show inverted text when the screen is redrawn.
1737 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1738 * no screen redraw, so it is done here to remove the inverted
1739 * part.
1740 */
1741 if (!gui_yank)
1742 {
1743 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001744#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 setmouse();
1746 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001747#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001748 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 if ((oap->op_type == OP_YANK
1750 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001751 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 || oap->op_type == OP_FILTER)
1753 && oap->motion_force == NUL)
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001754 {
1755#ifdef FEAT_LINEBREAK
1756 /* make sure redrawing is correct */
1757 curwin->w_p_lbr = lbr_saved;
1758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 }
1762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763
1764#ifdef FEAT_MBYTE
1765 /* Include the trailing byte of a multi-byte char. */
1766 if (has_mbyte && oap->inclusive)
1767 {
1768 int l;
1769
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001770 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 if (l > 1)
1772 oap->end.col += l - 1;
1773 }
1774#endif
1775 curwin->w_set_curswant = TRUE;
1776
1777 /*
1778 * oap->empty is set when start and end are the same. The inclusive
1779 * flag affects this too, unless yanking and the end is on a NUL.
1780 */
1781 oap->empty = (oap->motion_type == MCHAR
1782 && (!oap->inclusive
1783 || (oap->op_type == OP_YANK
1784 && gchar_pos(&oap->end) == NUL))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001785 && EQUAL_POS(oap->start, oap->end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786#ifdef FEAT_VIRTUALEDIT
1787 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1788#endif
1789 );
1790 /*
1791 * For delete, change and yank, it's an error to operate on an
1792 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1793 */
1794 empty_region_error = (oap->empty
1795 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1796
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 /* Force a redraw when operating on an empty Visual region, when
1798 * 'modifiable is off or creating a fold. */
1799 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001800#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 ))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001804 {
1805#ifdef FEAT_LINEBREAK
1806 curwin->w_p_lbr = lbr_saved;
1807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
1811 /*
1812 * If the end of an operator is in column one while oap->motion_type
1813 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1814 * character in the previous line. If op_start is on or before the
1815 * first non-blank in the line, the operator becomes linewise
1816 * (strange, but that's the way vi does it).
1817 */
1818 if ( oap->motion_type == MCHAR
1819 && oap->inclusive == FALSE
1820 && !(cap->retval & CA_NO_ADJ_OP_END)
1821 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001823 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 && oap->line_count > 1)
1825 {
1826 oap->end_adjusted = TRUE; /* remember that we did this */
1827 --oap->line_count;
1828 --oap->end.lnum;
1829 if (inindent(0))
1830 oap->motion_type = MLINE;
1831 else
1832 {
1833 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1834 if (oap->end.col)
1835 {
1836 --oap->end.col;
1837 oap->inclusive = TRUE;
1838 }
1839 }
1840 }
1841 else
1842 oap->end_adjusted = FALSE;
1843
1844 switch (oap->op_type)
1845 {
1846 case OP_LSHIFT:
1847 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001848 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 auto_format(FALSE, TRUE);
1850 break;
1851
1852 case OP_JOIN_NS:
1853 case OP_JOIN:
1854 if (oap->line_count < 2)
1855 oap->line_count = 2;
1856 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1857 curbuf->b_ml.ml_line_count)
1858 beep_flush();
1859 else
1860 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001861 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001862 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 auto_format(FALSE, TRUE);
1864 }
1865 break;
1866
1867 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001870 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001871 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001872 CancelRedo();
1873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 else
1875 {
1876 (void)op_delete(oap);
1877 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1878 u_save_cursor(); /* cursor line wasn't saved yet */
1879 auto_format(FALSE, TRUE);
1880 }
1881 break;
1882
1883 case OP_YANK:
1884 if (empty_region_error)
1885 {
1886 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001887 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001888 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001889 CancelRedo();
1890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 }
1892 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001893 {
1894#ifdef FEAT_LINEBREAK
1895 curwin->w_p_lbr = lbr_saved;
1896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 (void)op_yank(oap, FALSE, !gui_yank);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 check_cursor_col();
1900 break;
1901
1902 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001905 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001906 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001907 CancelRedo();
1908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 else
1910 {
1911 /* This is a new edit command, not a restart. Need to
1912 * remember it to make 'insertmode' work with mappings for
1913 * Visual mode. But do this only once and not when typed and
1914 * 'insertmode' isn't set. */
1915 if (p_im || !KeyTyped)
1916 restart_edit_save = restart_edit;
1917 else
1918 restart_edit_save = 0;
1919 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001920#ifdef FEAT_LINEBREAK
1921 /* Restore linebreak, so that when the user edits it looks as
1922 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001923 if (curwin->w_p_lbr != lbr_saved)
1924 {
1925 curwin->w_p_lbr = lbr_saved;
1926 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1927 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 /* Reset finish_op now, don't want it set inside edit(). */
1930 finish_op = FALSE;
1931 if (op_change(oap)) /* will call edit() */
1932 cap->retval |= CA_COMMAND_BUSY;
1933 if (restart_edit == 0)
1934 restart_edit = restart_edit_save;
1935 }
1936 break;
1937
1938 case OP_FILTER:
1939 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1940 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1941 else
1942 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
1943
1944 case OP_INDENT:
1945 case OP_COLON:
1946
1947#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1948 /*
1949 * If 'equalprg' is empty, do the indenting internally.
1950 */
1951 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1952 {
1953# ifdef FEAT_LISP
1954 if (curbuf->b_p_lisp)
1955 {
1956 op_reindent(oap, get_lisp_indent);
1957 break;
1958 }
1959# endif
1960# ifdef FEAT_CINDENT
1961 op_reindent(oap,
1962# ifdef FEAT_EVAL
1963 *curbuf->b_p_inde != NUL ? get_expr_indent :
1964# endif
1965 get_c_indent);
1966 break;
1967# endif
1968 }
1969#endif
1970
1971 op_colon(oap);
1972 break;
1973
1974 case OP_TILDE:
1975 case OP_UPPER:
1976 case OP_LOWER:
1977 case OP_ROT13:
1978 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001979 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001980 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001981 CancelRedo();
1982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 else
1984 op_tilde(oap);
1985 check_cursor_col();
1986 break;
1987
1988 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001989#if defined(FEAT_EVAL)
1990 if (*curbuf->b_p_fex != NUL)
1991 op_formatexpr(oap); /* use expression */
1992 else
1993#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01001994 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 op_colon(oap); /* use external command */
1996 else
1997 op_format(oap, FALSE); /* use internal function */
1998 break;
1999
2000 case OP_FORMAT2:
2001 op_format(oap, TRUE); /* use internal function */
2002 break;
2003
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002004 case OP_FUNCTION:
Bram Moolenaar4a08b0d2016-11-05 21:55:13 +01002005#ifdef FEAT_LINEBREAK
2006 /* Restore linebreak, so that when the user edits it looks as
2007 * before. */
2008 curwin->w_p_lbr = lbr_saved;
2009#endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002010 op_function(oap); /* call 'operatorfunc' */
2011 break;
2012
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 case OP_INSERT:
2014 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016#ifdef FEAT_VISUALEXTRA
2017 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002018 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002019 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002020 CancelRedo();
2021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 else
2023 {
2024 /* This is a new edit command, not a restart. Need to
2025 * remember it to make 'insertmode' work with mappings for
2026 * Visual mode. But do this only once. */
2027 restart_edit_save = restart_edit;
2028 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002029#ifdef FEAT_LINEBREAK
2030 /* Restore linebreak, so that when the user edits it looks as
2031 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002032 if (curwin->w_p_lbr != lbr_saved)
2033 {
2034 curwin->w_p_lbr = lbr_saved;
2035 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2036 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 op_insert(oap, cap->count1);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002039#ifdef FEAT_LINEBREAK
2040 /* Reset linebreak, so that formatting works correctly. */
2041 curwin->w_p_lbr = FALSE;
2042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043
2044 /* TODO: when inserting in several lines, should format all
2045 * the lines. */
2046 auto_format(FALSE, TRUE);
2047
2048 if (restart_edit == 0)
2049 restart_edit = restart_edit_save;
Bram Moolenaar0b5c93a2017-02-01 15:03:30 +01002050 else
2051 cap->retval |= CA_COMMAND_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 }
2053#else
Bram Moolenaar165bc692015-07-21 17:53:25 +02002054 vim_beep(BO_OPER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055#endif
2056 break;
2057
2058 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060#ifdef FEAT_VISUALEXTRA
2061 if (empty_region_error)
2062#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002063 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002064 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002065 CancelRedo();
2066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067#ifdef FEAT_VISUALEXTRA
2068 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002069 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002070# ifdef FEAT_LINEBREAK
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002071 /* Restore linebreak, so that when the user edits it looks as
2072 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002073 if (curwin->w_p_lbr != lbr_saved)
2074 {
2075 curwin->w_p_lbr = lbr_saved;
2076 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2077 }
2078# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 op_replace(oap, cap->nchar);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081#endif
2082 break;
2083
2084#ifdef FEAT_FOLDING
2085 case OP_FOLD:
2086 VIsual_reselect = FALSE; /* don't reselect now */
2087 foldCreate(oap->start.lnum, oap->end.lnum);
2088 break;
2089
2090 case OP_FOLDOPEN:
2091 case OP_FOLDOPENREC:
2092 case OP_FOLDCLOSE:
2093 case OP_FOLDCLOSEREC:
2094 VIsual_reselect = FALSE; /* don't reselect now */
2095 opFoldRange(oap->start.lnum, oap->end.lnum,
2096 oap->op_type == OP_FOLDOPEN
2097 || oap->op_type == OP_FOLDOPENREC,
2098 oap->op_type == OP_FOLDOPENREC
2099 || oap->op_type == OP_FOLDCLOSEREC,
2100 oap->is_VIsual);
2101 break;
2102
2103 case OP_FOLDDEL:
2104 case OP_FOLDDELREC:
2105 VIsual_reselect = FALSE; /* don't reselect now */
2106 deleteFold(oap->start.lnum, oap->end.lnum,
2107 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2108 break;
2109#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002110 case OP_NR_ADD:
2111 case OP_NR_SUB:
2112 if (empty_region_error)
2113 {
2114 vim_beep(BO_OPER);
2115 CancelRedo();
2116 }
2117 else
2118 {
2119 VIsual_active = TRUE;
2120#ifdef FEAT_LINEBREAK
2121 curwin->w_p_lbr = lbr_saved;
2122#endif
2123 op_addsub(oap, cap->count1, redo_VIsual_arg);
2124 VIsual_active = FALSE;
2125 }
2126 check_cursor_col();
2127 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 default:
2129 clearopbeep(oap);
2130 }
2131#ifdef FEAT_VIRTUALEDIT
2132 virtual_op = MAYBE;
2133#endif
2134 if (!gui_yank)
2135 {
2136 /*
2137 * if 'sol' not set, go back to old column for some commands
2138 */
2139 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2140 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2141 || oap->op_type == OP_DELETE))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002142 {
2143#ifdef FEAT_LINEBREAK
2144 curwin->w_p_lbr = FALSE;
2145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 coladvance(curwin->w_curswant = old_col);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 }
2149 else
2150 {
2151 curwin->w_cursor = old_cursor;
2152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 clearop(oap);
2155 }
Bram Moolenaar404406a2014-10-09 13:24:43 +02002156#ifdef FEAT_LINEBREAK
2157 curwin->w_p_lbr = lbr_saved;
2158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159}
2160
2161/*
2162 * Handle indent and format operators and visual mode ":".
2163 */
2164 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002165op_colon(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166{
2167 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 if (oap->is_VIsual)
2169 stuffReadbuff((char_u *)"'<,'>");
2170 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 {
2172 /*
2173 * Make the range look nice, so it can be repeated.
2174 */
2175 if (oap->start.lnum == curwin->w_cursor.lnum)
2176 stuffcharReadbuff('.');
2177 else
2178 stuffnumReadbuff((long)oap->start.lnum);
2179 if (oap->end.lnum != oap->start.lnum)
2180 {
2181 stuffcharReadbuff(',');
2182 if (oap->end.lnum == curwin->w_cursor.lnum)
2183 stuffcharReadbuff('.');
2184 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2185 stuffcharReadbuff('$');
2186 else if (oap->start.lnum == curwin->w_cursor.lnum)
2187 {
2188 stuffReadbuff((char_u *)".+");
2189 stuffnumReadbuff((long)oap->line_count - 1);
2190 }
2191 else
2192 stuffnumReadbuff((long)oap->end.lnum);
2193 }
2194 }
2195 if (oap->op_type != OP_COLON)
2196 stuffReadbuff((char_u *)"!");
2197 if (oap->op_type == OP_INDENT)
2198 {
2199#ifndef FEAT_CINDENT
2200 if (*get_equalprg() == NUL)
2201 stuffReadbuff((char_u *)"indent");
2202 else
2203#endif
2204 stuffReadbuff(get_equalprg());
2205 stuffReadbuff((char_u *)"\n");
2206 }
2207 else if (oap->op_type == OP_FORMAT)
2208 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002209 if (*curbuf->b_p_fp != NUL)
2210 stuffReadbuff(curbuf->b_p_fp);
2211 else if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 stuffReadbuff(p_fp);
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002213 else
2214 stuffReadbuff((char_u *)"fmt");
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002215 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 }
2217
2218 /*
2219 * do_cmdline() does the rest
2220 */
2221}
2222
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002223/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002224 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002225 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002226 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002227op_function(oparg_T *oap UNUSED)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002228{
2229#ifdef FEAT_EVAL
2230 char_u *(argv[1]);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002231# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002232 int save_virtual_op = virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002233# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002234
2235 if (*p_opfunc == NUL)
2236 EMSG(_("E774: 'operatorfunc' is empty"));
2237 else
2238 {
2239 /* Set '[ and '] marks to text to be operated on. */
2240 curbuf->b_op_start = oap->start;
2241 curbuf->b_op_end = oap->end;
2242 if (oap->motion_type != MLINE && !oap->inclusive)
2243 /* Exclude the end position. */
2244 decl(&curbuf->b_op_end);
2245
2246 if (oap->block_mode)
2247 argv[0] = (char_u *)"block";
2248 else if (oap->motion_type == MLINE)
2249 argv[0] = (char_u *)"line";
2250 else
2251 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002252
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002253# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002254 /* Reset virtual_op so that 'virtualedit' can be changed in the
2255 * function. */
2256 virtual_op = MAYBE;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002257# endif
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002258
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002259 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002260
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002261# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002262 virtual_op = save_virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002263# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002264 }
2265#else
2266 EMSG(_("E775: Eval feature not available"));
2267#endif
2268}
2269
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270#if defined(FEAT_MOUSE) || defined(PROTO)
2271/*
2272 * Do the appropriate action for the current mouse click in the current mode.
2273 * Not used for Command-line mode.
2274 *
2275 * Normal Mode:
2276 * event modi- position visual change action
2277 * fier cursor window
2278 * left press - yes end yes
2279 * left press C yes end yes "^]" (2)
2280 * left press S yes end yes "*" (2)
2281 * left drag - yes start if moved no
2282 * left relse - yes start if moved no
2283 * middle press - yes if not active no put register
2284 * middle press - yes if active no yank and put
2285 * right press - yes start or extend yes
2286 * right press S yes no change yes "#" (2)
2287 * right drag - yes extend no
2288 * right relse - yes extend no
2289 *
2290 * Insert or Replace Mode:
2291 * event modi- position visual change action
2292 * fier cursor window
2293 * left press - yes (cannot be active) yes
2294 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2295 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2296 * left drag - yes start or extend (1) no CTRL-O (1)
2297 * left relse - yes start or extend (1) no CTRL-O (1)
2298 * middle press - no (cannot be active) no put register
2299 * right press - yes start or extend yes CTRL-O
2300 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2301 *
2302 * (1) only if mouse pointer moved since press
2303 * (2) only if click is in same buffer
2304 *
2305 * Return TRUE if start_arrow() should be called for edit mode.
2306 */
2307 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002308do_mouse(
2309 oparg_T *oap, /* operator argument, can be NULL */
2310 int c, /* K_LEFTMOUSE, etc */
2311 int dir, /* Direction to 'put' if necessary */
2312 long count,
2313 int fixindent) /* PUT_FIXINDENT if fixing indent necessary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314{
2315 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2316 static int got_click = FALSE; /* got a click some time back */
2317
2318 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2319 int is_click; /* If FALSE it's a drag or release event */
2320 int is_drag; /* If TRUE it's a drag event */
2321 int jump_flags = 0; /* flags for jump_to_mouse() */
2322 pos_T start_visual;
2323 int moved; /* Has cursor moved? */
2324 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002325#ifdef FEAT_WINDOWS
2326 static int in_tab_line = FALSE; /* mouse clicked in tab line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 int in_sep_line; /* mouse in vertical separator line */
2328#endif
2329 int c1, c2;
2330#if defined(FEAT_FOLDING)
2331 pos_T save_cursor;
2332#endif
2333 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 static pos_T orig_cursor;
2335 colnr_T leftcol, rightcol;
2336 pos_T end_visual;
2337 int diff;
2338 int old_active = VIsual_active;
2339 int old_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 int regname;
2341
2342#if defined(FEAT_FOLDING)
2343 save_cursor = curwin->w_cursor;
2344#endif
2345
2346 /*
2347 * When GUI is active, always recognize mouse events, otherwise:
2348 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2349 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2350 * - For command line and insert mode 'mouse' is checked before calling
2351 * do_mouse().
2352 */
2353 if (do_always)
2354 do_always = FALSE;
2355 else
2356#ifdef FEAT_GUI
2357 if (!gui.in_use)
2358#endif
2359 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 if (VIsual_active)
2361 {
2362 if (!mouse_has(MOUSE_VISUAL))
2363 return FALSE;
2364 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002365 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 return FALSE;
2367 }
2368
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002369 for (;;)
2370 {
2371 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2372 if (is_drag)
2373 {
2374 /* If the next character is the same mouse event then use that
2375 * one. Speeds up dragging the status line. */
2376 if (vpeekc() != NUL)
2377 {
2378 int nc;
2379 int save_mouse_row = mouse_row;
2380 int save_mouse_col = mouse_col;
2381
2382 /* Need to get the character, peeking doesn't get the actual
2383 * one. */
2384 nc = safe_vgetc();
2385 if (c == nc)
2386 continue;
2387 vungetc(nc);
2388 mouse_row = save_mouse_row;
2389 mouse_col = save_mouse_col;
2390 }
2391 }
2392 break;
2393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394
2395#ifdef FEAT_MOUSESHAPE
2396 /* May have stopped dragging the status or separator line. The pointer is
2397 * most likely still on the status or separator line. */
2398 if (!is_drag && drag_status_line)
2399 {
2400 drag_status_line = FALSE;
2401 update_mouseshape(SHAPE_IDX_STATUS);
2402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 if (!is_drag && drag_sep_line)
2404 {
2405 drag_sep_line = FALSE;
2406 update_mouseshape(SHAPE_IDX_VSEP);
2407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408#endif
2409
2410 /*
2411 * Ignore drag and release events if we didn't get a click.
2412 */
2413 if (is_click)
2414 got_click = TRUE;
2415 else
2416 {
2417 if (!got_click) /* didn't get click, ignore */
2418 return FALSE;
2419 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002420 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002422#ifdef FEAT_WINDOWS
2423 if (in_tab_line)
2424 {
2425 in_tab_line = FALSE;
2426 return FALSE;
2427 }
2428#endif
2429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 }
2431
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 /*
2433 * CTRL right mouse button does CTRL-T
2434 */
2435 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2436 {
2437 if (State & INSERT)
2438 stuffcharReadbuff(Ctrl_O);
2439 if (count > 1)
2440 stuffnumReadbuff(count);
2441 stuffcharReadbuff(Ctrl_T);
2442 got_click = FALSE; /* ignore drag&release now */
2443 return FALSE;
2444 }
2445
2446 /*
2447 * CTRL only works with left mouse button
2448 */
2449 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2450 return FALSE;
2451
2452 /*
2453 * When a modifier is down, ignore drag and release events, as well as
2454 * multiple clicks and the middle mouse button.
2455 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2456 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002457 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2458 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 && (!is_click
2460 || (mod_mask & MOD_MASK_MULTI_CLICK)
2461 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002462 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 && mouse_model_popup()
2464 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002465 && !((mod_mask & MOD_MASK_ALT)
2466 && !mouse_model_popup()
2467 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 )
2469 return FALSE;
2470
2471 /*
2472 * If the button press was used as the movement command for an operator
2473 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2474 * drag/release events.
2475 */
2476 if (!is_click && which_button == MOUSE_MIDDLE)
2477 return FALSE;
2478
2479 if (oap != NULL)
2480 regname = oap->regname;
2481 else
2482 regname = 0;
2483
2484 /*
2485 * Middle mouse button does a 'put' of the selected text
2486 */
2487 if (which_button == MOUSE_MIDDLE)
2488 {
2489 if (State == NORMAL)
2490 {
2491 /*
2492 * If an operator was pending, we don't know what the user wanted
2493 * to do. Go back to normal mode: Clear the operator and beep().
2494 */
2495 if (oap != NULL && oap->op_type != OP_NOP)
2496 {
2497 clearopbeep(oap);
2498 return FALSE;
2499 }
2500
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 /*
2502 * If visual was active, yank the highlighted text and put it
2503 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002504 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 */
2506 if (VIsual_active)
2507 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002508 if (VIsual_select)
2509 {
2510 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002511 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002512 }
2513 else
2514 {
2515 stuffcharReadbuff('y');
2516 stuffcharReadbuff(K_MIDDLEMOUSE);
2517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 do_always = TRUE; /* ignore 'mouse' setting next time */
2519 return FALSE;
2520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 /*
2522 * The rest is below jump_to_mouse()
2523 */
2524 }
2525
2526 else if ((State & INSERT) == 0)
2527 return FALSE;
2528
2529 /*
2530 * Middle click in insert mode doesn't move the mouse, just insert the
2531 * contents of a register. '.' register is special, can't insert that
2532 * with do_put().
2533 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2534 * happens for the GUI).
2535 */
2536 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2537 {
2538 if (regname == '.')
2539 insert_reg(regname, TRUE);
2540 else
2541 {
2542#ifdef FEAT_CLIPBOARD
2543 if (clip_star.available && regname == 0)
2544 regname = '*';
2545#endif
2546 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2547 insert_reg(regname, TRUE);
2548 else
2549 {
2550 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2551
2552 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2553 AppendCharToRedobuff(Ctrl_R);
2554 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2555 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2556 }
2557 }
2558 return FALSE;
2559 }
2560 }
2561
2562 /* When dragging or button-up stay in the same window. */
2563 if (!is_click)
2564 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2565
2566 start_visual.lnum = 0;
2567
Bram Moolenaarf740b292006-02-16 22:11:02 +00002568#ifdef FEAT_WINDOWS
2569 /* Check for clicking in the tab page line. */
2570 if (mouse_row == 0 && firstwin->w_winrow > 0)
2571 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002572 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002573 {
2574 if (in_tab_line)
2575 {
2576 c1 = TabPageIdxs[mouse_col];
Bram Moolenaar4a4b8212015-09-08 17:50:41 +02002577 tabpage_move(c1 <= 0 ? 9999 : c1 < tabpage_index(curtab)
2578 ? c1 - 1 : c1);
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002579 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002580 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002581 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002582
Bram Moolenaarf740b292006-02-16 22:11:02 +00002583 /* click in a tab selects that tab page */
2584 if (is_click
2585# ifdef FEAT_CMDWIN
2586 && cmdwin_type == 0
2587# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002588 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002589 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002590 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002591 c1 = TabPageIdxs[mouse_col];
2592 if (c1 >= 0)
2593 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002594 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2595 {
2596 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002597 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002598 tabpage_new();
2599 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2600 }
2601 else
2602 {
2603 /* Go to specified tab page, or next one if not clicking
2604 * on a label. */
2605 goto_tabpage(c1);
2606
2607 /* It's like clicking on the status line of a window. */
2608 if (curwin != old_curwin)
2609 end_visual_mode();
2610 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002611 }
2612 else if (c1 < 0)
2613 {
2614 tabpage_T *tp;
2615
2616 /* Close the current or specified tab page. */
2617 if (c1 == -999)
2618 tp = curtab;
2619 else
2620 tp = find_tabpage(-c1);
2621 if (tp == curtab)
2622 {
2623 if (first_tabpage->tp_next != NULL)
2624 tabpage_close(FALSE);
2625 }
2626 else if (tp != NULL)
2627 tabpage_close_other(tp, FALSE);
2628 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002629 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002630 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002631 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002632 else if (is_drag && in_tab_line)
2633 {
2634 c1 = TabPageIdxs[mouse_col];
2635 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2636 return FALSE;
2637 }
2638
Bram Moolenaarf740b292006-02-16 22:11:02 +00002639#endif
2640
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 /*
2642 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2643 * right button up -> pop-up menu
2644 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002645 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 */
2647 if (mouse_model_popup())
2648 {
2649 if (which_button == MOUSE_RIGHT
2650 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2651 {
2652 /*
2653 * NOTE: Ignore right button down and drag mouse events.
2654 * Windows only shows the popup menu on the button up event.
2655 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002656#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2657 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 if (!is_click)
2659 return FALSE;
2660#endif
2661#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2662 if (is_click || is_drag)
2663 return FALSE;
2664#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002665#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2667 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2668 if (gui.in_use)
2669 {
2670 jump_flags = 0;
2671 if (STRCMP(p_mousem, "popup_setpos") == 0)
2672 {
2673 /* First set the cursor position before showing the popup
2674 * menu. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 if (VIsual_active)
2676 {
2677 pos_T m_pos;
2678
2679 /*
2680 * set MOUSE_MAY_STOP_VIS if we are outside the
2681 * selection or the current window (might have false
2682 * negative here)
2683 */
2684 if (mouse_row < W_WINROW(curwin)
2685 || mouse_row
2686 > (W_WINROW(curwin) + curwin->w_height))
2687 jump_flags = MOUSE_MAY_STOP_VIS;
2688 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2689 jump_flags = MOUSE_MAY_STOP_VIS;
2690 else
2691 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002692 if ((LT_POS(curwin->w_cursor, VIsual)
2693 && (LT_POS(m_pos, curwin->w_cursor)
2694 || LT_POS(VIsual, m_pos)))
2695 || (LT_POS(VIsual, curwin->w_cursor)
2696 && (LT_POS(m_pos, VIsual)
2697 || LT_POS(curwin->w_cursor, m_pos))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 {
2699 jump_flags = MOUSE_MAY_STOP_VIS;
2700 }
2701 else if (VIsual_mode == Ctrl_V)
2702 {
2703 getvcols(curwin, &curwin->w_cursor, &VIsual,
2704 &leftcol, &rightcol);
2705 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2706 if (m_pos.col < leftcol || m_pos.col > rightcol)
2707 jump_flags = MOUSE_MAY_STOP_VIS;
2708 }
2709 }
2710 }
2711 else
2712 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
2714 if (jump_flags)
2715 {
2716 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002717 update_curbuf(VIsual_active ? INVERTED : VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 setcursor();
2719 out_flush(); /* Update before showing popup menu */
2720 }
2721# ifdef FEAT_MENU
2722 gui_show_popupmenu();
2723# endif
2724 return (jump_flags & CURSOR_MOVED) != 0;
2725 }
2726 else
2727 return FALSE;
2728#else
2729 return FALSE;
2730#endif
2731 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002732 if (which_button == MOUSE_LEFT
2733 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 {
2735 which_button = MOUSE_RIGHT;
2736 mod_mask &= ~MOD_MASK_SHIFT;
2737 }
2738 }
2739
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 if ((State & (NORMAL | INSERT))
2741 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2742 {
2743 if (which_button == MOUSE_LEFT)
2744 {
2745 if (is_click)
2746 {
2747 /* stop Visual mode for a left click in a window, but not when
2748 * on a status line */
2749 if (VIsual_active)
2750 jump_flags |= MOUSE_MAY_STOP_VIS;
2751 }
2752 else if (mouse_has(MOUSE_VISUAL))
2753 jump_flags |= MOUSE_MAY_VIS;
2754 }
2755 else if (which_button == MOUSE_RIGHT)
2756 {
2757 if (is_click && VIsual_active)
2758 {
2759 /*
2760 * Remember the start and end of visual before moving the
2761 * cursor.
2762 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002763 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 {
2765 start_visual = curwin->w_cursor;
2766 end_visual = VIsual;
2767 }
2768 else
2769 {
2770 start_visual = VIsual;
2771 end_visual = curwin->w_cursor;
2772 }
2773 }
2774 jump_flags |= MOUSE_FOCUS;
2775 if (mouse_has(MOUSE_VISUAL))
2776 jump_flags |= MOUSE_MAY_VIS;
2777 }
2778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779
2780 /*
2781 * If an operator is pending, ignore all drags and releases until the
2782 * next mouse click.
2783 */
2784 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2785 {
2786 got_click = FALSE;
2787 oap->motion_type = MCHAR;
2788 }
2789
2790 /* When releasing the button let jump_to_mouse() know. */
2791 if (!is_click && !is_drag)
2792 jump_flags |= MOUSE_RELEASED;
2793
2794 /*
2795 * JUMP!
2796 */
2797 jump_flags = jump_to_mouse(jump_flags,
2798 oap == NULL ? NULL : &(oap->inclusive), which_button);
2799 moved = (jump_flags & CURSOR_MOVED);
2800 in_status_line = (jump_flags & IN_STATUS_LINE);
Bram Moolenaar829c8e32016-03-19 23:07:23 +01002801#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 in_sep_line = (jump_flags & IN_SEP_LINE);
Bram Moolenaar829c8e32016-03-19 23:07:23 +01002803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804
2805#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002806 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2808 {
2809 int key = KEY2TERMCAP1(c);
2810
2811 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2812 || key == (int)KE_RIGHTRELEASE)
2813 netbeans_button_release(which_button);
2814 }
2815#endif
2816
2817 /* When jumping to another window, clear a pending operator. That's a bit
2818 * friendlier than beeping and not jumping to that window. */
2819 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2820 clearop(oap);
2821
2822#ifdef FEAT_FOLDING
2823 if (mod_mask == 0
2824 && !is_drag
2825 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2826 && which_button == MOUSE_LEFT)
2827 {
2828 /* open or close a fold at this line */
2829 if (jump_flags & MOUSE_FOLD_OPEN)
2830 openFold(curwin->w_cursor.lnum, 1L);
2831 else
2832 closeFold(curwin->w_cursor.lnum, 1L);
2833 /* don't move the cursor if still in the same window */
2834 if (curwin == old_curwin)
2835 curwin->w_cursor = save_cursor;
2836 }
2837#endif
2838
2839#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2840 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2841 {
2842 clip_modeless(which_button, is_click, is_drag);
2843 return FALSE;
2844 }
2845#endif
2846
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002848 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 if (VIsual_active && is_drag && p_so)
2850 {
2851 /* In the very first line, allow scrolling one line */
2852 if (mouse_row == 0)
2853 mouse_dragging = 2;
2854 else
2855 mouse_dragging = 1;
2856 }
2857
2858 /* When dragging the mouse above the window, scroll down. */
2859 if (is_drag && mouse_row < 0 && !in_status_line)
2860 {
2861 scroll_redraw(FALSE, 1L);
2862 mouse_row = 0;
2863 }
2864
2865 if (start_visual.lnum) /* right click in visual mode */
2866 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002867 /* When ALT is pressed make Visual mode blockwise. */
2868 if (mod_mask & MOD_MASK_ALT)
2869 VIsual_mode = Ctrl_V;
2870
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 /*
2872 * In Visual-block mode, divide the area in four, pick up the corner
2873 * that is in the quarter that the cursor is in.
2874 */
2875 if (VIsual_mode == Ctrl_V)
2876 {
2877 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2878 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2879 end_visual.col = leftcol;
2880 else
2881 end_visual.col = rightcol;
Bram Moolenaarcde88542015-08-11 19:14:00 +02002882 if (curwin->w_cursor.lnum >=
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 (start_visual.lnum + end_visual.lnum) / 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 end_visual.lnum = start_visual.lnum;
2885
2886 /* move VIsual to the right column */
2887 start_visual = curwin->w_cursor; /* save the cursor pos */
2888 curwin->w_cursor = end_visual;
2889 coladvance(end_visual.col);
2890 VIsual = curwin->w_cursor;
2891 curwin->w_cursor = start_visual; /* restore the cursor */
2892 }
2893 else
2894 {
2895 /*
2896 * If the click is before the start of visual, change the start.
2897 * If the click is after the end of visual, change the end. If
2898 * the click is inside the visual, change the closest side.
2899 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002900 if (LT_POS(curwin->w_cursor, start_visual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 VIsual = end_visual;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002902 else if (LT_POS(end_visual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 VIsual = start_visual;
2904 else
2905 {
2906 /* In the same line, compare column number */
2907 if (end_visual.lnum == start_visual.lnum)
2908 {
2909 if (curwin->w_cursor.col - start_visual.col >
2910 end_visual.col - curwin->w_cursor.col)
2911 VIsual = start_visual;
2912 else
2913 VIsual = end_visual;
2914 }
2915
2916 /* In different lines, compare line number */
2917 else
2918 {
2919 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2920 (end_visual.lnum - curwin->w_cursor.lnum);
2921
2922 if (diff > 0) /* closest to end */
2923 VIsual = start_visual;
2924 else if (diff < 0) /* closest to start */
2925 VIsual = end_visual;
2926 else /* in the middle line */
2927 {
2928 if (curwin->w_cursor.col <
2929 (start_visual.col + end_visual.col) / 2)
2930 VIsual = end_visual;
2931 else
2932 VIsual = start_visual;
2933 }
2934 }
2935 }
2936 }
2937 }
2938 /*
2939 * If Visual mode started in insert mode, execute "CTRL-O"
2940 */
2941 else if ((State & INSERT) && VIsual_active)
2942 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943
2944 /*
2945 * Middle mouse click: Put text before cursor.
2946 */
2947 if (which_button == MOUSE_MIDDLE)
2948 {
2949#ifdef FEAT_CLIPBOARD
2950 if (clip_star.available && regname == 0)
2951 regname = '*';
2952#endif
2953 if (yank_register_mline(regname))
2954 {
2955 if (mouse_past_bottom)
2956 dir = FORWARD;
2957 }
2958 else if (mouse_past_eol)
2959 dir = FORWARD;
2960
2961 if (fixindent)
2962 {
2963 c1 = (dir == BACKWARD) ? '[' : ']';
2964 c2 = 'p';
2965 }
2966 else
2967 {
2968 c1 = (dir == FORWARD) ? 'p' : 'P';
2969 c2 = NUL;
2970 }
2971 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2972
2973 /*
2974 * Remember where the paste started, so in edit() Insstart can be set
2975 * to this position
2976 */
2977 if (restart_edit != 0)
2978 where_paste_started = curwin->w_cursor;
2979 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2980 }
2981
2982#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2983 /*
2984 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2985 * error under the mouse pointer.
2986 */
2987 else if (((mod_mask & MOD_MASK_CTRL)
2988 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2989 && bt_quickfix(curbuf))
2990 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002991 if (curwin->w_llist_ref == NULL) /* quickfix window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01002992 do_cmdline_cmd((char_u *)".cc");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002993 else /* location list window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01002994 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 got_click = FALSE; /* ignore drag&release now */
2996 }
2997#endif
2998
2999 /*
3000 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
3001 * under the mouse pointer.
3002 */
3003 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3004 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3005 {
3006 if (State & INSERT)
3007 stuffcharReadbuff(Ctrl_O);
3008 stuffcharReadbuff(Ctrl_RSB);
3009 got_click = FALSE; /* ignore drag&release now */
3010 }
3011
3012 /*
3013 * Shift-Mouse click searches for the next occurrence of the word under
3014 * the mouse pointer
3015 */
3016 else if ((mod_mask & MOD_MASK_SHIFT))
3017 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003018 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 stuffcharReadbuff(Ctrl_O);
3020 if (which_button == MOUSE_LEFT)
3021 stuffcharReadbuff('*');
3022 else /* MOUSE_RIGHT */
3023 stuffcharReadbuff('#');
3024 }
3025
3026 /* Handle double clicks, unless on status line */
3027 else if (in_status_line)
3028 {
3029#ifdef FEAT_MOUSESHAPE
3030 if ((is_drag || is_click) && !drag_status_line)
3031 {
3032 drag_status_line = TRUE;
3033 update_mouseshape(-1);
3034 }
3035#endif
3036 }
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003037#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 else if (in_sep_line)
3039 {
3040# ifdef FEAT_MOUSESHAPE
3041 if ((is_drag || is_click) && !drag_sep_line)
3042 {
3043 drag_sep_line = TRUE;
3044 update_mouseshape(-1);
3045 }
3046# endif
3047 }
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3050 && mouse_has(MOUSE_VISUAL))
3051 {
3052 if (is_click || !VIsual_active)
3053 {
3054 if (VIsual_active)
3055 orig_cursor = VIsual;
3056 else
3057 {
3058 check_visual_highlight();
3059 VIsual = curwin->w_cursor;
3060 orig_cursor = VIsual;
3061 VIsual_active = TRUE;
3062 VIsual_reselect = TRUE;
3063 /* start Select mode if 'selectmode' contains "mouse" */
3064 may_start_select('o');
3065 setmouse();
3066 }
3067 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003068 {
3069 /* Double click with ALT pressed makes it blockwise. */
3070 if (mod_mask & MOD_MASK_ALT)
3071 VIsual_mode = Ctrl_V;
3072 else
3073 VIsual_mode = 'v';
3074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3076 VIsual_mode = 'V';
3077 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3078 VIsual_mode = Ctrl_V;
3079#ifdef FEAT_CLIPBOARD
3080 /* Make sure the clipboard gets updated. Needed because start and
3081 * end may still be the same, and the selection needs to be owned */
3082 clip_star.vmode = NUL;
3083#endif
3084 }
3085 /*
3086 * A double click selects a word or a block.
3087 */
3088 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3089 {
3090 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003091 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092
3093 if (is_click)
3094 {
3095 /* If the character under the cursor (skipping white space) is
3096 * not a word character, try finding a match and select a (),
3097 * {}, [], #if/#endif, etc. block. */
3098 end_visual = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01003099 while (gc = gchar_pos(&end_visual), VIM_ISWHITE(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 inc(&end_visual);
3101 if (oap != NULL)
3102 oap->motion_type = MCHAR;
3103 if (oap != NULL
3104 && VIsual_mode == 'v'
3105 && !vim_iswordc(gchar_pos(&end_visual))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003106 && EQUAL_POS(curwin->w_cursor, VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 && (pos = findmatch(oap, NUL)) != NULL)
3108 {
3109 curwin->w_cursor = *pos;
3110 if (oap->motion_type == MLINE)
3111 VIsual_mode = 'V';
3112 else if (*p_sel == 'e')
3113 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003114 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 ++VIsual.col;
3116 else
3117 ++curwin->w_cursor.col;
3118 }
3119 }
3120 }
3121
3122 if (pos == NULL && (is_click || is_drag))
3123 {
3124 /* When not found a match or when dragging: extend to include
3125 * a word. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003126 if (LT_POS(curwin->w_cursor, orig_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 {
3128 find_start_of_word(&curwin->w_cursor);
3129 find_end_of_word(&VIsual);
3130 }
3131 else
3132 {
3133 find_start_of_word(&VIsual);
3134 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3135#ifdef FEAT_MBYTE
3136 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003137 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138#else
3139 ++curwin->w_cursor.col;
3140#endif
3141 find_end_of_word(&curwin->w_cursor);
3142 }
3143 }
3144 curwin->w_set_curswant = TRUE;
3145 }
3146 if (is_click)
3147 redraw_curbuf_later(INVERTED); /* update the inversion */
3148 }
3149 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003150 {
3151 if (mod_mask & MOD_MASK_ALT)
3152 VIsual_mode = Ctrl_V;
3153 else
3154 VIsual_mode = 'v';
3155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
3157 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003158 if ((!VIsual_active && old_active && mode_displayed)
3159 || (VIsual_active && p_smd && msg_silent == 0
3160 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162
3163 return moved;
3164}
3165
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166/*
3167 * Move "pos" back to the start of the word it's in.
3168 */
3169 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003170find_start_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171{
3172 char_u *line;
3173 int cclass;
3174 int col;
3175
3176 line = ml_get(pos->lnum);
3177 cclass = get_mouse_class(line + pos->col);
3178
3179 while (pos->col > 0)
3180 {
3181 col = pos->col - 1;
3182#ifdef FEAT_MBYTE
3183 col -= (*mb_head_off)(line, line + col);
3184#endif
3185 if (get_mouse_class(line + col) != cclass)
3186 break;
3187 pos->col = col;
3188 }
3189}
3190
3191/*
3192 * Move "pos" forward to the end of the word it's in.
3193 * When 'selection' is "exclusive", the position is just after the word.
3194 */
3195 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003196find_end_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197{
3198 char_u *line;
3199 int cclass;
3200 int col;
3201
3202 line = ml_get(pos->lnum);
3203 if (*p_sel == 'e' && pos->col > 0)
3204 {
3205 --pos->col;
3206#ifdef FEAT_MBYTE
3207 pos->col -= (*mb_head_off)(line, line + pos->col);
3208#endif
3209 }
3210 cclass = get_mouse_class(line + pos->col);
3211 while (line[pos->col] != NUL)
3212 {
3213#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003214 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215#else
3216 col = pos->col + 1;
3217#endif
3218 if (get_mouse_class(line + col) != cclass)
3219 {
3220 if (*p_sel == 'e')
3221 pos->col = col;
3222 break;
3223 }
3224 pos->col = col;
3225 }
3226}
3227
3228/*
3229 * Get class of a character for selection: same class means same word.
3230 * 0: blank
3231 * 1: punctuation groups
3232 * 2: normal word character
3233 * >2: multi-byte word character.
3234 */
3235 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003236get_mouse_class(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237{
3238 int c;
3239
3240#ifdef FEAT_MBYTE
3241 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3242 return mb_get_class(p);
3243#endif
3244
3245 c = *p;
3246 if (c == ' ' || c == '\t')
3247 return 0;
3248
3249 if (vim_iswordc(c))
3250 return 2;
3251
3252 /*
3253 * There are a few special cases where we want certain combinations of
3254 * characters to be considered as a single word. These are things like
3255 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003256 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 */
3258 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3259 return 1;
3260 return c;
3261}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262#endif /* FEAT_MOUSE */
3263
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264/*
3265 * Check if highlighting for visual mode is possible, give a warning message
3266 * if not.
3267 */
3268 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003269check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270{
3271 static int did_check = FALSE;
3272
3273 if (full_screen)
3274 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003275 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 MSG(_("Warning: terminal cannot highlight"));
3277 did_check = TRUE;
3278 }
3279}
3280
3281/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003282 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 * This function should ALWAYS be called to end Visual mode, except from
3284 * do_pending_operator().
3285 */
3286 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003287end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288{
3289#ifdef FEAT_CLIPBOARD
3290 /*
3291 * If we are using the clipboard, then remember what was selected in case
3292 * we need to paste it somewhere while we still own the selection.
3293 * Only do this when the clipboard is already owned. Don't want to grab
3294 * the selection when hitting ESC.
3295 */
3296 if (clip_star.available && clip_star.owned)
3297 clip_auto_select();
3298#endif
3299
3300 VIsual_active = FALSE;
3301#ifdef FEAT_MOUSE
3302 setmouse();
3303 mouse_dragging = 0;
3304#endif
3305
3306 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003307 curbuf->b_visual.vi_mode = VIsual_mode;
3308 curbuf->b_visual.vi_start = VIsual;
3309 curbuf->b_visual.vi_end = curwin->w_cursor;
3310 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311#ifdef FEAT_EVAL
3312 curbuf->b_visual_mode_eval = VIsual_mode;
3313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314#ifdef FEAT_VIRTUALEDIT
3315 if (!virtual_active())
3316 curwin->w_cursor.coladd = 0;
3317#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003318 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003320 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321}
3322
3323/*
3324 * Reset VIsual_active and VIsual_reselect.
3325 */
3326 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003327reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
3329 if (VIsual_active)
3330 {
3331 end_visual_mode();
3332 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3333 }
3334 VIsual_reselect = FALSE;
3335}
3336
3337/*
3338 * Reset VIsual_active and VIsual_reselect if it's set.
3339 */
3340 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003341reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342{
3343 if (VIsual_active)
3344 {
3345 end_visual_mode();
3346 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3347 VIsual_reselect = FALSE;
3348 }
3349}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003351#if defined(FEAT_BEVAL)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003352static int find_is_eval_item(char_u *ptr, int *colp, int *nbp, int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353
3354/*
3355 * Check for a balloon-eval special item to include when searching for an
3356 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3357 * Returns TRUE if the character at "*ptr" should be included.
3358 * "dir" is FORWARD or BACKWARD, the direction of searching.
3359 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3360 * "bnp" points to a counter for square brackets.
3361 */
3362 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003363find_is_eval_item(
3364 char_u *ptr,
3365 int *colp,
3366 int *bnp,
3367 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368{
3369 /* Accept everything inside []. */
3370 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3371 ++*bnp;
3372 if (*bnp > 0)
3373 {
3374 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3375 --*bnp;
3376 return TRUE;
3377 }
3378
3379 /* skip over "s.var" */
3380 if (*ptr == '.')
3381 return TRUE;
3382
3383 /* two-character item: s->var */
3384 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3385 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3386 {
3387 *colp += dir;
3388 return TRUE;
3389 }
3390 return FALSE;
3391}
3392#endif
3393
3394/*
3395 * Find the identifier under or to the right of the cursor.
3396 * "find_type" can have one of three values:
3397 * FIND_IDENT: find an identifier (keyword)
3398 * FIND_STRING: find any non-white string
3399 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003400 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 *
3402 * There are three steps:
3403 * 1. Search forward for the start of an identifier/string. Doesn't move if
3404 * already on one.
3405 * 2. Search backward for the start of this identifier/string.
3406 * This doesn't match the real Vi but I like it a little better and it
3407 * shouldn't bother anyone.
3408 * 3. Search forward to the end of this identifier/string.
3409 * When FIND_IDENT isn't defined, we backup until a blank.
3410 *
3411 * Returns the length of the string, or zero if no string is found.
3412 * If a string is found, a pointer to the string is put in "*string". This
3413 * string is not always NUL terminated.
3414 */
3415 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003416find_ident_under_cursor(char_u **string, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417{
3418 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3419 curwin->w_cursor.col, string, find_type);
3420}
3421
3422/*
3423 * Like find_ident_under_cursor(), but for any window and any position.
3424 * However: Uses 'iskeyword' from the current window!.
3425 */
3426 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003427find_ident_at_pos(
3428 win_T *wp,
3429 linenr_T lnum,
3430 colnr_T startcol,
3431 char_u **string,
3432 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
3434 char_u *ptr;
3435 int col = 0; /* init to shut up GCC */
3436 int i;
3437#ifdef FEAT_MBYTE
3438 int this_class = 0;
3439 int prev_class;
3440 int prevcol;
3441#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003442#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 int bn = 0; /* bracket nesting */
3444#endif
3445
3446 /*
3447 * if i == 0: try to find an identifier
3448 * if i == 1: try to find any non-white string
3449 */
3450 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3451 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3452 {
3453 /*
3454 * 1. skip to start of identifier/string
3455 */
3456 col = startcol;
3457#ifdef FEAT_MBYTE
3458 if (has_mbyte)
3459 {
3460 while (ptr[col] != NUL)
3461 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003462# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 /* Stop at a ']' to evaluate "a[x]". */
3464 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3465 break;
3466# endif
3467 this_class = mb_get_class(ptr + col);
3468 if (this_class != 0 && (i == 1 || this_class != 1))
3469 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003470 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 }
3472 }
3473 else
3474#endif
3475 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01003476 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003477# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3479# endif
3480 )
3481 ++col;
3482
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003483#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 /* When starting on a ']' count it, so that we include the '['. */
3485 bn = ptr[col] == ']';
3486#endif
3487
3488 /*
3489 * 2. Back up to start of identifier/string.
3490 */
3491#ifdef FEAT_MBYTE
3492 if (has_mbyte)
3493 {
3494 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003495# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3497 this_class = mb_get_class((char_u *)"a");
3498 else
3499# endif
3500 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003501 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 {
3503 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3504 prev_class = mb_get_class(ptr + prevcol);
3505 if (this_class != prev_class
3506 && (i == 0
3507 || prev_class == 0
3508 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003509# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 && (!(find_type & FIND_EVAL)
3511 || prevcol == 0
3512 || !find_is_eval_item(ptr + prevcol, &prevcol,
3513 &bn, BACKWARD))
3514# endif
3515 )
3516 break;
3517 col = prevcol;
3518 }
3519
3520 /* If we don't want just any old string, or we've found an
3521 * identifier, stop searching. */
3522 if (this_class > 2)
3523 this_class = 2;
3524 if (!(find_type & FIND_STRING) || this_class == 2)
3525 break;
3526 }
3527 else
3528#endif
3529 {
3530 while (col > 0
3531 && ((i == 0
3532 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003533 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 && (!(find_type & FIND_IDENT)
3535 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003536#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 || ((find_type & FIND_EVAL)
3538 && col > 1
3539 && find_is_eval_item(ptr + col - 1, &col,
3540 &bn, BACKWARD))
3541#endif
3542 ))
3543 --col;
3544
3545 /* If we don't want just any old string, or we've found an
3546 * identifier, stop searching. */
3547 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3548 break;
3549 }
3550 }
3551
3552 if (ptr[col] == NUL || (i == 0 && (
3553#ifdef FEAT_MBYTE
3554 has_mbyte ? this_class != 2 :
3555#endif
3556 !vim_iswordc(ptr[col]))))
3557 {
3558 /*
3559 * didn't find an identifier or string
3560 */
3561 if (find_type & FIND_STRING)
3562 EMSG(_("E348: No string under cursor"));
3563 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003564 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 return 0;
3566 }
3567 ptr += col;
3568 *string = ptr;
3569
3570 /*
3571 * 3. Find the end if the identifier/string.
3572 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003573#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 bn = 0;
3575 startcol -= col;
3576#endif
3577 col = 0;
3578#ifdef FEAT_MBYTE
3579 if (has_mbyte)
3580 {
3581 /* Search for point of changing multibyte character class. */
3582 this_class = mb_get_class(ptr);
3583 while (ptr[col] != NUL
3584 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3585 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003586# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 || ((find_type & FIND_EVAL)
3588 && col <= (int)startcol
3589 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3590# endif
3591 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003592 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 }
3594 else
3595#endif
3596 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003597 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003598# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 || ((find_type & FIND_EVAL)
3600 && col <= (int)startcol
3601 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3602# endif
3603 )
3604 {
3605 ++col;
3606 }
3607
3608 return col;
3609}
3610
3611/*
3612 * Prepare for redo of a normal command.
3613 */
3614 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003615prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616{
3617 prep_redo(cap->oap->regname, cap->count0,
3618 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3619}
3620
3621/*
3622 * Prepare for redo of any command.
3623 * Note that only the last argument can be a multi-byte char.
3624 */
3625 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003626prep_redo(
3627 int regname,
3628 long num,
3629 int cmd1,
3630 int cmd2,
3631 int cmd3,
3632 int cmd4,
3633 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634{
3635 ResetRedobuff();
3636 if (regname != 0) /* yank from specified buffer */
3637 {
3638 AppendCharToRedobuff('"');
3639 AppendCharToRedobuff(regname);
3640 }
3641 if (num)
3642 AppendNumberToRedobuff(num);
3643
3644 if (cmd1 != NUL)
3645 AppendCharToRedobuff(cmd1);
3646 if (cmd2 != NUL)
3647 AppendCharToRedobuff(cmd2);
3648 if (cmd3 != NUL)
3649 AppendCharToRedobuff(cmd3);
3650 if (cmd4 != NUL)
3651 AppendCharToRedobuff(cmd4);
3652 if (cmd5 != NUL)
3653 AppendCharToRedobuff(cmd5);
3654}
3655
3656/*
3657 * check for operator active and clear it
3658 *
3659 * return TRUE if operator was active
3660 */
3661 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003662checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663{
3664 if (oap->op_type == OP_NOP)
3665 return FALSE;
3666 clearopbeep(oap);
3667 return TRUE;
3668}
3669
3670/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003671 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003673 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 */
3675 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003676checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003678 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 return FALSE;
3680 clearopbeep(oap);
3681 return TRUE;
3682}
3683
3684 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003685clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686{
3687 oap->op_type = OP_NOP;
3688 oap->regname = 0;
3689 oap->motion_force = NUL;
3690 oap->use_reg_one = FALSE;
3691}
3692
3693 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003694clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695{
3696 clearop(oap);
3697 beep_flush();
3698}
3699
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700/*
3701 * Remove the shift modifier from a special key.
3702 */
3703 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003704unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705{
3706 switch (cap->cmdchar)
3707 {
3708 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3709 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3710 case K_S_UP: cap->cmdchar = K_UP; break;
3711 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3712 case K_S_HOME: cap->cmdchar = K_HOME; break;
3713 case K_S_END: cap->cmdchar = K_END; break;
3714 }
3715 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3716}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003718/*
3719 * If the mode is currently displayed clear the command line or update the
3720 * command displayed.
3721 */
3722 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003723may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003724{
3725 if (mode_displayed)
3726 clear_cmdline = TRUE; /* unshow visual mode later */
3727#ifdef FEAT_CMDL_INFO
3728 else
3729 clear_showcmd();
3730#endif
3731}
3732
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3734/*
3735 * Routines for displaying a partly typed command
3736 */
3737
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003738#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739static char_u showcmd_buf[SHOWCMD_BUFLEN];
3740static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3741static int showcmd_is_clear = TRUE;
3742static int showcmd_visual = FALSE;
3743
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003744static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745
3746 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003747clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748{
3749 if (!p_sc)
3750 return;
3751
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 if (VIsual_active && !char_avail())
3753 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003754 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 long lines;
3756 colnr_T leftcol, rightcol;
3757 linenr_T top, bot;
3758
3759 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003760 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 {
3762 top = VIsual.lnum;
3763 bot = curwin->w_cursor.lnum;
3764 }
3765 else
3766 {
3767 top = curwin->w_cursor.lnum;
3768 bot = VIsual.lnum;
3769 }
3770# ifdef FEAT_FOLDING
3771 /* Include closed folds as a whole. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02003772 (void)hasFolding(top, &top, NULL);
3773 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774# endif
3775 lines = bot - top + 1;
3776
3777 if (VIsual_mode == Ctrl_V)
3778 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003779# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003780 char_u *saved_sbr = p_sbr;
3781
3782 /* Make 'sbr' empty for a moment to get the correct size. */
3783 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003784# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003786# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003787 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003788# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3790 (long)(rightcol - leftcol + 1));
3791 }
3792 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3793 sprintf((char *)showcmd_buf, "%ld", lines);
3794 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003795 {
3796 char_u *s, *e;
3797 int l;
3798 int bytes = 0;
3799 int chars = 0;
3800
3801 if (cursor_bot)
3802 {
3803 s = ml_get_pos(&VIsual);
3804 e = ml_get_cursor();
3805 }
3806 else
3807 {
3808 s = ml_get_cursor();
3809 e = ml_get_pos(&VIsual);
3810 }
3811 while ((*p_sel != 'e') ? s <= e : s < e)
3812 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003813# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003814 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003815# else
3816 l = (*s == NUL) ? 0 : 1;
3817# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003818 if (l == 0)
3819 {
3820 ++bytes;
3821 ++chars;
3822 break; /* end of line */
3823 }
3824 bytes += l;
3825 ++chars;
3826 s += l;
3827 }
3828 if (bytes == chars)
3829 sprintf((char *)showcmd_buf, "%d", chars);
3830 else
3831 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3834 showcmd_visual = TRUE;
3835 }
3836 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 {
3838 showcmd_buf[0] = NUL;
3839 showcmd_visual = FALSE;
3840
3841 /* Don't actually display something if there is nothing to clear. */
3842 if (showcmd_is_clear)
3843 return;
3844 }
3845
3846 display_showcmd();
3847}
3848
3849/*
3850 * Add 'c' to string of shown command chars.
3851 * Return TRUE if output has been written (and setcursor() has been called).
3852 */
3853 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003854add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855{
3856 char_u *p;
3857 int old_len;
3858 int extra_len;
3859 int overflow;
3860#if defined(FEAT_MOUSE)
3861 int i;
3862 static int ignore[] =
3863 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003864# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3866 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003867# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01003868 K_IGNORE, K_PS,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3870 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3871 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003872 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003874 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 0
3876 };
3877#endif
3878
Bram Moolenaar09df3122006-01-23 22:23:09 +00003879 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 return FALSE;
3881
3882 if (showcmd_visual)
3883 {
3884 showcmd_buf[0] = NUL;
3885 showcmd_visual = FALSE;
3886 }
3887
3888#if defined(FEAT_MOUSE)
3889 /* Ignore keys that are scrollbar updates and mouse clicks */
3890 if (IS_SPECIAL(c))
3891 for (i = 0; ignore[i] != 0; ++i)
3892 if (ignore[i] == c)
3893 return FALSE;
3894#endif
3895
3896 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003897 if (*p == ' ')
3898 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 old_len = (int)STRLEN(showcmd_buf);
3900 extra_len = (int)STRLEN(p);
3901 overflow = old_len + extra_len - SHOWCMD_COLS;
3902 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003903 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3904 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 STRCAT(showcmd_buf, p);
3906
3907 if (char_avail())
3908 return FALSE;
3909
3910 display_showcmd();
3911
3912 return TRUE;
3913}
3914
3915 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003916add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917{
3918 if (!add_to_showcmd(c))
3919 setcursor();
3920}
3921
3922/*
3923 * Delete 'len' characters from the end of the shown command.
3924 */
3925 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003926del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927{
3928 int old_len;
3929
3930 if (!p_sc)
3931 return;
3932
3933 old_len = (int)STRLEN(showcmd_buf);
3934 if (len > old_len)
3935 len = old_len;
3936 showcmd_buf[old_len - len] = NUL;
3937
3938 if (!char_avail())
3939 display_showcmd();
3940}
3941
3942/*
3943 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3944 * something and there is a partial mapping.
3945 */
3946 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003947push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948{
3949 if (p_sc)
3950 STRCPY(old_showcmd_buf, showcmd_buf);
3951}
3952
3953 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003954pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955{
3956 if (!p_sc)
3957 return;
3958
3959 STRCPY(showcmd_buf, old_showcmd_buf);
3960
3961 display_showcmd();
3962}
3963
3964 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003965display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966{
3967 int len;
3968
3969 cursor_off();
3970
3971 len = (int)STRLEN(showcmd_buf);
3972 if (len == 0)
3973 showcmd_is_clear = TRUE;
3974 else
3975 {
3976 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3977 showcmd_is_clear = FALSE;
3978 }
3979
3980 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003981 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3982 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 */
3984 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3985
3986 setcursor(); /* put cursor back where it belongs */
3987}
3988#endif
3989
3990#ifdef FEAT_SCROLLBIND
3991/*
3992 * When "check" is FALSE, prepare for commands that scroll the window.
3993 * When "check" is TRUE, take care of scroll-binding after the window has
3994 * scrolled. Called from normal_cmd() and edit().
3995 */
3996 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003997do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998{
3999 static win_T *old_curwin = NULL;
4000 static linenr_T old_topline = 0;
4001#ifdef FEAT_DIFF
4002 static int old_topfill = 0;
4003#endif
4004 static buf_T *old_buf = NULL;
4005 static colnr_T old_leftcol = 0;
4006
4007 if (check && curwin->w_p_scb)
4008 {
4009 /* If a ":syncbind" command was just used, don't scroll, only reset
4010 * the values. */
4011 if (did_syncbind)
4012 did_syncbind = FALSE;
4013 else if (curwin == old_curwin)
4014 {
4015 /*
4016 * Synchronize other windows, as necessary according to
4017 * 'scrollbind'. Don't do this after an ":edit" command, except
4018 * when 'diff' is set.
4019 */
4020 if ((curwin->w_buffer == old_buf
4021#ifdef FEAT_DIFF
4022 || curwin->w_p_diff
4023#endif
4024 )
4025 && (curwin->w_topline != old_topline
4026#ifdef FEAT_DIFF
4027 || curwin->w_topfill != old_topfill
4028#endif
4029 || curwin->w_leftcol != old_leftcol))
4030 {
4031 check_scrollbind(curwin->w_topline - old_topline,
4032 (long)(curwin->w_leftcol - old_leftcol));
4033 }
4034 }
4035 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4036 {
4037 /*
4038 * When switching between windows, make sure that the relative
4039 * vertical offset is valid for the new window. The relative
4040 * offset is invalid whenever another 'scrollbind' window has
4041 * scrolled to a point that would force the current window to
4042 * scroll past the beginning or end of its buffer. When the
4043 * resync is performed, some of the other 'scrollbind' windows may
4044 * need to jump so that the current window's relative position is
4045 * visible on-screen.
4046 */
4047 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4048 }
4049 curwin->w_scbind_pos = curwin->w_topline;
4050 }
4051
4052 old_curwin = curwin;
4053 old_topline = curwin->w_topline;
4054#ifdef FEAT_DIFF
4055 old_topfill = curwin->w_topfill;
4056#endif
4057 old_buf = curwin->w_buffer;
4058 old_leftcol = curwin->w_leftcol;
4059}
4060
4061/*
4062 * Synchronize any windows that have "scrollbind" set, based on the
4063 * number of rows by which the current window has changed
4064 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4065 */
4066 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004067check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068{
4069 int want_ver;
4070 int want_hor;
4071 win_T *old_curwin = curwin;
4072 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 int old_VIsual_select = VIsual_select;
4074 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 colnr_T tgt_leftcol = curwin->w_leftcol;
4076 long topline;
4077 long y;
4078
4079 /*
4080 * check 'scrollopt' string for vertical and horizontal scroll options
4081 */
4082 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4083#ifdef FEAT_DIFF
4084 want_ver |= old_curwin->w_p_diff;
4085#endif
4086 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4087
4088 /*
4089 * loop through the scrollbound windows and scroll accordingly
4090 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02004092 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 {
4094 curbuf = curwin->w_buffer;
4095 /* skip original window and windows with 'noscrollbind' */
4096 if (curwin != old_curwin && curwin->w_p_scb)
4097 {
4098 /*
4099 * do the vertical scroll
4100 */
4101 if (want_ver)
4102 {
4103#ifdef FEAT_DIFF
4104 if (old_curwin->w_p_diff && curwin->w_p_diff)
4105 {
4106 diff_set_topline(old_curwin, curwin);
4107 }
4108 else
4109#endif
4110 {
4111 curwin->w_scbind_pos += topline_diff;
4112 topline = curwin->w_scbind_pos;
4113 if (topline > curbuf->b_ml.ml_line_count)
4114 topline = curbuf->b_ml.ml_line_count;
4115 if (topline < 1)
4116 topline = 1;
4117
4118 y = topline - curwin->w_topline;
4119 if (y > 0)
4120 scrollup(y, FALSE);
4121 else
4122 scrolldown(-y, FALSE);
4123 }
4124
4125 redraw_later(VALID);
4126 cursor_correct();
4127#ifdef FEAT_WINDOWS
4128 curwin->w_redr_status = TRUE;
4129#endif
4130 }
4131
4132 /*
4133 * do the horizontal scroll
4134 */
4135 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4136 {
4137 curwin->w_leftcol = tgt_leftcol;
4138 leftcol_changed();
4139 }
4140 }
4141 }
4142
4143 /*
4144 * reset current-window
4145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 VIsual_select = old_VIsual_select;
4147 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 curwin = old_curwin;
4149 curbuf = old_curbuf;
4150}
4151#endif /* #ifdef FEAT_SCROLLBIND */
4152
4153/*
4154 * Command character that's ignored.
4155 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004156 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004159nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004161 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162}
4163
4164/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004165 * Command character that doesn't do anything, but unlike nv_ignore() does
4166 * start edit(). Used for "startinsert" executed while starting up.
4167 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004168 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004169nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00004170{
4171}
4172
4173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 * Command character doesn't exist.
4175 */
4176 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004177nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178{
4179 clearopbeep(cap->oap);
4180}
4181
4182/*
4183 * <Help> and <F1> commands.
4184 */
4185 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004186nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187{
4188 if (!checkclearopq(cap->oap))
4189 ex_help(NULL);
4190}
4191
4192/*
4193 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4194 */
4195 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004196nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197{
Bram Moolenaard79e5502016-01-10 22:13:02 +01004198 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004199 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01004200 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01004201 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
4202 op_addsub(cap->oap, cap->count1, cap->arg);
4203 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004204 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01004205 else if (VIsual_active)
4206 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004207 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01004208 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209}
4210
4211/*
4212 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4213 */
4214 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004215nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216{
4217 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004218 {
4219#ifdef FEAT_WINDOWS
4220 if (mod_mask & MOD_MASK_CTRL)
4221 {
4222 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4223 if (cap->arg == BACKWARD)
4224 goto_tabpage(-(int)cap->count1);
4225 else
4226 goto_tabpage((int)cap->count0);
4227 }
4228 else
4229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232}
4233
4234/*
4235 * Implementation of "gd" and "gD" command.
4236 */
4237 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004238nv_gd(
4239 oparg_T *oap,
4240 int nchar,
4241 int thisblock) /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242{
4243 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 char_u *ptr;
4245
Bram Moolenaard9d30582005-05-18 22:10:28 +00004246 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02004247 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
4248 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004250#ifdef FEAT_FOLDING
4251 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4252 foldOpenCursor();
4253#endif
4254}
4255
4256/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02004257 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
4258 * otherwise.
4259 */
4260 static int
4261is_ident(char_u *line, int offset)
4262{
4263 int i;
4264 int incomment = FALSE;
4265 int instring = 0;
4266 int prev = 0;
4267
4268 for (i = 0; i < offset && line[i] != NUL; i++)
4269 {
4270 if (instring != 0)
4271 {
4272 if (prev != '\\' && line[i] == instring)
4273 instring = 0;
4274 }
4275 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
4276 {
4277 instring = line[i];
4278 }
4279 else
4280 {
4281 if (incomment)
4282 {
4283 if (prev == '*' && line[i] == '/')
4284 incomment = FALSE;
4285 }
4286 else if (prev == '/' && line[i] == '*')
4287 {
4288 incomment = TRUE;
4289 }
4290 else if (prev == '/' && line[i] == '/')
4291 {
4292 return FALSE;
4293 }
4294 }
4295
4296 prev = line[i];
4297 }
4298
4299 return incomment == FALSE && instring == 0;
4300}
4301
4302/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004303 * Search for variable declaration of "ptr[len]".
4304 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4305 * current file ("gD").
4306 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004307 * Return FAIL when not found.
4308 */
4309 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004310find_decl(
4311 char_u *ptr,
4312 int len,
4313 int locally,
4314 int thisblock,
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004315 int flags_arg) /* flags passed to searchit() */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004316{
4317 char_u *pat;
4318 pos_T old_pos;
4319 pos_T par_pos;
4320 pos_T found_pos;
4321 int t;
4322 int save_p_ws;
4323 int save_p_scs;
4324 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004325 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004326 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004327 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004328
4329 if ((pat = alloc(len + 7)) == NULL)
4330 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004331
4332 /* Put "\V" before the pattern to avoid that the special meaning of "."
4333 * and "~" causes trouble. */
4334 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4335 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 old_pos = curwin->w_cursor;
4337 save_p_ws = p_ws;
4338 save_p_scs = p_scs;
4339 p_ws = FALSE; /* don't wrap around end of file now */
4340 p_scs = FALSE; /* don't switch ignorecase off now */
4341
4342 /*
4343 * With "gD" go to line 1.
4344 * With "gd" Search back for the start of the current function, then go
4345 * back until a blank line. If this fails go to line 1.
4346 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004347 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 {
4349 setpcmark(); /* Set in findpar() otherwise */
4350 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004351 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 }
4353 else
4354 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004355 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4357 --curwin->w_cursor.lnum;
4358 }
4359 curwin->w_cursor.col = 0;
4360
4361 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004362 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004363 for (;;)
4364 {
Bram Moolenaar226630a2016-10-08 19:21:31 +02004365 valid = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004366 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004367 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004368 if (curwin->w_cursor.lnum >= old_pos.lnum)
4369 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004370
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004371 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004372 {
4373 pos_T *pos;
4374
4375 /* Check that the block the match is in doesn't end before the
4376 * position where we started the search from. */
4377 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4378 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4379 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02004380 {
4381 /* There can't be a useful match before the end of this block.
4382 * Skip to the end. */
4383 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004384 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02004385 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004386 }
4387
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004388 if (t == FAIL)
4389 {
4390 /* If we previously found a valid position, use it. */
4391 if (found_pos.lnum != 0)
4392 {
4393 curwin->w_cursor = found_pos;
4394 t = OK;
4395 }
4396 break;
4397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004399 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004400 {
4401 /* Ignore this line, continue at start of next line. */
4402 ++curwin->w_cursor.lnum;
4403 curwin->w_cursor.col = 0;
4404 continue;
4405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406#endif
Bram Moolenaar226630a2016-10-08 19:21:31 +02004407 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
4408
4409 /* If the current position is not a valid identifier and a previous
4410 * match is present, favor that one instead. */
4411 if (!valid && found_pos.lnum != 0)
4412 {
4413 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004414 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004415 }
4416
4417 /* Global search: use first valid match found */
4418 if (valid && !locally)
4419 break;
4420 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004421 {
4422 /* If we previously found a valid position, use it. */
4423 if (found_pos.lnum != 0)
4424 curwin->w_cursor = found_pos;
4425 break;
4426 }
4427
Bram Moolenaar226630a2016-10-08 19:21:31 +02004428 /* For finding a local variable and the match is before the "{" or
4429 * inside a comment, continue searching. For K&R style function
4430 * declarations this skips the function header without types. */
4431 if (!valid)
4432 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004433 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02004434 }
4435 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02004436 found_pos = curwin->w_cursor;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004437 /* Remove SEARCH_START from flags to avoid getting stuck at one
4438 * position. */
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004439 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004441
4442 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004444 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 curwin->w_cursor = old_pos;
4446 }
4447 else
4448 {
4449 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 /* "n" searches forward now */
4451 reset_search_dir();
4452 }
4453
4454 vim_free(pat);
4455 p_ws = save_p_ws;
4456 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004457
4458 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459}
4460
4461/*
4462 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4463 * lines rather than lines in the file.
4464 * 'dist' must be positive.
4465 *
4466 * Return OK if able to move cursor, FAIL otherwise.
4467 */
4468 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004469nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470{
4471 int linelen = linetabsize(ml_get_curline());
4472 int retval = OK;
4473 int atend = FALSE;
4474 int n;
4475 int col_off1; /* margin offset for first screen line */
4476 int col_off2; /* margin offset for wrapped screen line */
4477 int width1; /* text width for first screen line */
4478 int width2; /* test width for wrapped screen line */
4479
4480 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004481 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482
4483 col_off1 = curwin_col_off();
4484 col_off2 = col_off1 - curwin_col_off2();
4485 width1 = W_WIDTH(curwin) - col_off1;
4486 width2 = W_WIDTH(curwin) - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01004487 if (width2 == 0)
4488 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004490#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 if (curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004493 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 /*
4495 * Instead of sticking at the last character of the buffer line we
4496 * try to stick in the last column of the screen.
4497 */
4498 if (curwin->w_curswant == MAXCOL)
4499 {
4500 atend = TRUE;
4501 validate_virtcol();
4502 if (width1 <= 0)
4503 curwin->w_curswant = 0;
4504 else
4505 {
4506 curwin->w_curswant = width1 - 1;
4507 if (curwin->w_virtcol > curwin->w_curswant)
4508 curwin->w_curswant += ((curwin->w_virtcol
4509 - curwin->w_curswant - 1) / width2 + 1) * width2;
4510 }
4511 }
4512 else
4513 {
4514 if (linelen > width1)
4515 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4516 else
4517 n = width1;
4518 if (curwin->w_curswant > (colnr_T)n + 1)
4519 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4520 * width2;
4521 }
4522
4523 while (dist--)
4524 {
4525 if (dir == BACKWARD)
4526 {
4527 if ((long)curwin->w_curswant >= width2)
4528 /* move back within line */
4529 curwin->w_curswant -= width2;
4530 else
4531 {
4532 /* to previous line */
4533 if (curwin->w_cursor.lnum == 1)
4534 {
4535 retval = FAIL;
4536 break;
4537 }
4538 --curwin->w_cursor.lnum;
4539#ifdef FEAT_FOLDING
4540 /* Move to the start of a closed fold. Don't do that when
4541 * 'foldopen' contains "all": it will open in a moment. */
4542 if (!(fdo_flags & FDO_ALL))
4543 (void)hasFolding(curwin->w_cursor.lnum,
4544 &curwin->w_cursor.lnum, NULL);
4545#endif
4546 linelen = linetabsize(ml_get_curline());
4547 if (linelen > width1)
4548 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4549 + 1) * width2;
4550 }
4551 }
4552 else /* dir == FORWARD */
4553 {
4554 if (linelen > width1)
4555 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4556 else
4557 n = width1;
4558 if (curwin->w_curswant + width2 < (colnr_T)n)
4559 /* move forward within line */
4560 curwin->w_curswant += width2;
4561 else
4562 {
4563 /* to next line */
4564#ifdef FEAT_FOLDING
4565 /* Move to the end of a closed fold. */
4566 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4567 &curwin->w_cursor.lnum);
4568#endif
4569 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4570 {
4571 retval = FAIL;
4572 break;
4573 }
4574 curwin->w_cursor.lnum++;
4575 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004576 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 }
4578 }
4579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004582 if (virtual_active() && atend)
4583 coladvance(MAXCOL);
4584 else
4585 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586
4587#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4588 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4589 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004590 colnr_T virtcol;
4591
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 /*
4593 * Check for landing on a character that got split at the end of the
4594 * last line. We want to advance a screenline, not end up in the same
4595 * screenline or move two screenlines.
4596 */
4597 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004598 virtcol = curwin->w_virtcol;
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004599# if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004600 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4601 virtcol -= vim_strsize(p_sbr);
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004602# endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004603
4604 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 && (curwin->w_curswant < (colnr_T)width1
4606 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4607 : ((curwin->w_curswant - width1) % width2
4608 > (colnr_T)width2 / 2)))
4609 --curwin->w_cursor.col;
4610 }
4611#endif
4612
4613 if (atend)
4614 curwin->w_curswant = MAXCOL; /* stick in the last column */
4615
4616 return retval;
4617}
4618
4619#ifdef FEAT_MOUSE
4620/*
4621 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4622 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004623 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4624 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 */
4626 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004627nv_mousescroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628{
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004629# ifdef FEAT_WINDOWS
Bram Moolenaara5792f52005-11-23 21:25:05 +00004630 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004632 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 {
4634 int row, col;
4635
4636 row = mouse_row;
4637 col = mouse_col;
4638
4639 /* find the window at the pointer coordinates */
4640 curwin = mouse_find_win(&row, &col);
4641 curbuf = curwin->w_buffer;
4642 }
4643# endif
4644
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004645 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004647# ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02004648 if (term_use_loop())
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004649 send_keys_to_term(curbuf->b_term, cap->cmdchar, TRUE);
4650 else
4651# endif
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004652 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4653 {
4654 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4655 }
4656 else
4657 {
4658 cap->count1 = 3;
4659 cap->count0 = 3;
4660 nv_scroll_line(cap);
4661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004663# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 else
4665 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004666 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4667 if (!curwin->w_p_wrap)
4668 {
4669 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004670
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004671 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4672 step = W_WIDTH(curwin);
4673 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4674 if (val < 0)
4675 val = 0;
4676
4677 gui_do_horiz_scroll(val, TRUE);
4678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004680# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004682# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 curwin->w_redr_status = TRUE;
4684
4685 curwin = old_curwin;
4686 curbuf = curwin->w_buffer;
4687# endif
4688}
4689
4690/*
4691 * Mouse clicks and drags.
4692 */
4693 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004694nv_mouse(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695{
4696 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4697}
4698#endif
4699
4700/*
4701 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4702 * cap->arg must be TRUE for CTRL-E.
4703 */
4704 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004705nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706{
4707 if (!checkclearop(cap->oap))
4708 scroll_redraw(cap->arg, cap->count1);
4709}
4710
4711/*
4712 * Scroll "count" lines up or down, and redraw.
4713 */
4714 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004715scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716{
4717 linenr_T prev_topline = curwin->w_topline;
4718#ifdef FEAT_DIFF
4719 int prev_topfill = curwin->w_topfill;
4720#endif
4721 linenr_T prev_lnum = curwin->w_cursor.lnum;
4722
4723 if (up)
4724 scrollup(count, TRUE);
4725 else
4726 scrolldown(count, TRUE);
4727 if (p_so)
4728 {
4729 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4730 * valid, otherwise the screen jumps back at the end of the file. */
4731 cursor_correct();
4732 check_cursor_moved(curwin);
4733 curwin->w_valid |= VALID_TOPLINE;
4734
4735 /* If moved back to where we were, at least move the cursor, otherwise
4736 * we get stuck at one position. Don't move the cursor up if the
4737 * first line of the buffer is already on the screen */
4738 while (curwin->w_topline == prev_topline
4739#ifdef FEAT_DIFF
4740 && curwin->w_topfill == prev_topfill
4741#endif
4742 )
4743 {
4744 if (up)
4745 {
4746 if (curwin->w_cursor.lnum > prev_lnum
4747 || cursor_down(1L, FALSE) == FAIL)
4748 break;
4749 }
4750 else
4751 {
4752 if (curwin->w_cursor.lnum < prev_lnum
4753 || prev_topline == 1L
4754 || cursor_up(1L, FALSE) == FAIL)
4755 break;
4756 }
4757 /* Mark w_topline as valid, otherwise the screen jumps back at the
4758 * end of the file. */
4759 check_cursor_moved(curwin);
4760 curwin->w_valid |= VALID_TOPLINE;
4761 }
4762 }
4763 if (curwin->w_cursor.lnum != prev_lnum)
4764 coladvance(curwin->w_curswant);
4765 redraw_later(VALID);
4766}
4767
4768/*
4769 * Commands that start with "z".
4770 */
4771 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004772nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773{
4774 long n;
4775 colnr_T col;
4776 int nchar = cap->nchar;
4777#ifdef FEAT_FOLDING
4778 long old_fdl = curwin->w_p_fdl;
4779 int old_fen = curwin->w_p_fen;
4780#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004781#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004782 int undo = FALSE;
4783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784
4785 if (VIM_ISDIGIT(nchar))
4786 {
4787 /*
4788 * "z123{nchar}": edit the count before obtaining {nchar}
4789 */
4790 if (checkclearop(cap->oap))
4791 return;
4792 n = nchar - '0';
4793 for (;;)
4794 {
4795#ifdef USE_ON_FLY_SCROLL
4796 dont_scroll = TRUE; /* disallow scrolling here */
4797#endif
4798 ++no_mapping;
4799 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004800 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 --no_mapping;
4803 --allow_keys;
4804#ifdef FEAT_CMDL_INFO
4805 (void)add_to_showcmd(nchar);
4806#endif
4807 if (nchar == K_DEL || nchar == K_KDEL)
4808 n /= 10;
4809 else if (VIM_ISDIGIT(nchar))
4810 n = n * 10 + (nchar - '0');
4811 else if (nchar == CAR)
4812 {
4813#ifdef FEAT_GUI
4814 need_mouse_correct = TRUE;
4815#endif
4816 win_setheight((int)n);
4817 break;
4818 }
4819 else if (nchar == 'l'
4820 || nchar == 'h'
4821 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004822 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 {
4824 cap->count1 = n ? n * cap->count1 : cap->count1;
4825 goto dozet;
4826 }
4827 else
4828 {
4829 clearopbeep(cap->oap);
4830 break;
4831 }
4832 }
4833 cap->oap->op_type = OP_NOP;
4834 return;
4835 }
4836
4837dozet:
4838 if (
4839#ifdef FEAT_FOLDING
4840 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4841 * and "zC" only in Visual mode. "zj" and "zk" are motion
4842 * commands. */
4843 cap->nchar != 'f' && cap->nchar != 'F'
4844 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4845 && cap->nchar != 'j' && cap->nchar != 'k'
4846 &&
4847#endif
4848 checkclearop(cap->oap))
4849 return;
4850
4851 /*
4852 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4853 * If line number given, set cursor.
4854 */
4855 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4856 && cap->count0
4857 && cap->count0 != curwin->w_cursor.lnum)
4858 {
4859 setpcmark();
4860 if (cap->count0 > curbuf->b_ml.ml_line_count)
4861 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4862 else
4863 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004864 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 }
4866
4867 switch (nchar)
4868 {
4869 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4870 case '+':
4871 if (cap->count0 == 0)
4872 {
4873 /* No count given: put cursor at the line below screen */
4874 validate_botline(); /* make sure w_botline is valid */
4875 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4876 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4877 else
4878 curwin->w_cursor.lnum = curwin->w_botline;
4879 }
4880 /* FALLTHROUGH */
4881 case NL:
4882 case CAR:
4883 case K_KENTER:
4884 beginline(BL_WHITE | BL_FIX);
4885 /* FALLTHROUGH */
4886
4887 case 't': scroll_cursor_top(0, TRUE);
4888 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004889 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 break;
4891
4892 /* "z." and "zz": put cursor in middle of screen */
4893 case '.': beginline(BL_WHITE | BL_FIX);
4894 /* FALLTHROUGH */
4895
4896 case 'z': scroll_cursor_halfway(TRUE);
4897 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004898 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 break;
4900
4901 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4902 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4903 * when <count> is at bottom of window, and puts that one at
4904 * bottom of window. */
4905 if (cap->count0 != 0)
4906 {
4907 scroll_cursor_bot(0, TRUE);
4908 curwin->w_cursor.lnum = curwin->w_topline;
4909 }
4910 else if (curwin->w_topline == 1)
4911 curwin->w_cursor.lnum = 1;
4912 else
4913 curwin->w_cursor.lnum = curwin->w_topline - 1;
4914 /* FALLTHROUGH */
4915 case '-':
4916 beginline(BL_WHITE | BL_FIX);
4917 /* FALLTHROUGH */
4918
4919 case 'b': scroll_cursor_bot(0, TRUE);
4920 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004921 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 break;
4923
4924 /* "zH" - scroll screen right half-page */
4925 case 'H':
4926 cap->count1 *= W_WIDTH(curwin) / 2;
4927 /* FALLTHROUGH */
4928
4929 /* "zh" - scroll screen to the right */
4930 case 'h':
4931 case K_LEFT:
4932 if (!curwin->w_p_wrap)
4933 {
4934 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4935 curwin->w_leftcol = 0;
4936 else
4937 curwin->w_leftcol -= (colnr_T)cap->count1;
4938 leftcol_changed();
4939 }
4940 break;
4941
4942 /* "zL" - scroll screen left half-page */
4943 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4944 /* FALLTHROUGH */
4945
4946 /* "zl" - scroll screen to the left */
4947 case 'l':
4948 case K_RIGHT:
4949 if (!curwin->w_p_wrap)
4950 {
4951 /* scroll the window left */
4952 curwin->w_leftcol += (colnr_T)cap->count1;
4953 leftcol_changed();
4954 }
4955 break;
4956
4957 /* "zs" - scroll screen, cursor at the start */
4958 case 's': if (!curwin->w_p_wrap)
4959 {
4960#ifdef FEAT_FOLDING
4961 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4962 col = 0; /* like the cursor is in col 0 */
4963 else
4964#endif
4965 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4966 if ((long)col > p_siso)
4967 col -= p_siso;
4968 else
4969 col = 0;
4970 if (curwin->w_leftcol != col)
4971 {
4972 curwin->w_leftcol = col;
4973 redraw_later(NOT_VALID);
4974 }
4975 }
4976 break;
4977
4978 /* "ze" - scroll screen, cursor at the end */
4979 case 'e': if (!curwin->w_p_wrap)
4980 {
4981#ifdef FEAT_FOLDING
4982 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4983 col = 0; /* like the cursor is in col 0 */
4984 else
4985#endif
4986 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4987 n = W_WIDTH(curwin) - curwin_col_off();
4988 if ((long)col + p_siso < n)
4989 col = 0;
4990 else
4991 col = col + p_siso - n + 1;
4992 if (curwin->w_leftcol != col)
4993 {
4994 curwin->w_leftcol = col;
4995 redraw_later(NOT_VALID);
4996 }
4997 }
4998 break;
4999
5000#ifdef FEAT_FOLDING
5001 /* "zF": create fold command */
5002 /* "zf": create fold operator */
5003 case 'F':
5004 case 'f': if (foldManualAllowed(TRUE))
5005 {
5006 cap->nchar = 'f';
5007 nv_operator(cap);
5008 curwin->w_p_fen = TRUE;
5009
5010 /* "zF" is like "zfzf" */
5011 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
5012 {
5013 nv_operator(cap);
5014 finish_op = TRUE;
5015 }
5016 }
5017 else
5018 clearopbeep(cap->oap);
5019 break;
5020
5021 /* "zd": delete fold at cursor */
5022 /* "zD": delete fold at cursor recursively */
5023 case 'd':
5024 case 'D': if (foldManualAllowed(FALSE))
5025 {
5026 if (VIsual_active)
5027 nv_operator(cap);
5028 else
5029 deleteFold(curwin->w_cursor.lnum,
5030 curwin->w_cursor.lnum, nchar == 'D', FALSE);
5031 }
5032 break;
5033
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005034 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 case 'E': if (foldmethodIsManual(curwin))
5036 {
5037 clearFolding(curwin);
5038 changed_window_setting();
5039 }
5040 else if (foldmethodIsMarker(curwin))
5041 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5042 TRUE, FALSE);
5043 else
5044 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5045 break;
5046
5047 /* "zn": fold none: reset 'foldenable' */
5048 case 'n': curwin->w_p_fen = FALSE;
5049 break;
5050
5051 /* "zN": fold Normal: set 'foldenable' */
5052 case 'N': curwin->w_p_fen = TRUE;
5053 break;
5054
5055 /* "zi": invert folding: toggle 'foldenable' */
5056 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5057 break;
5058
5059 /* "za": open closed fold or close open fold at cursor */
5060 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5061 openFold(curwin->w_cursor.lnum, cap->count1);
5062 else
5063 {
5064 closeFold(curwin->w_cursor.lnum, cap->count1);
5065 curwin->w_p_fen = TRUE;
5066 }
5067 break;
5068
5069 /* "zA": open fold at cursor recursively */
5070 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5071 openFoldRecurse(curwin->w_cursor.lnum);
5072 else
5073 {
5074 closeFoldRecurse(curwin->w_cursor.lnum);
5075 curwin->w_p_fen = TRUE;
5076 }
5077 break;
5078
5079 /* "zo": open fold at cursor or Visual area */
5080 case 'o': if (VIsual_active)
5081 nv_operator(cap);
5082 else
5083 openFold(curwin->w_cursor.lnum, cap->count1);
5084 break;
5085
5086 /* "zO": open fold recursively */
5087 case 'O': if (VIsual_active)
5088 nv_operator(cap);
5089 else
5090 openFoldRecurse(curwin->w_cursor.lnum);
5091 break;
5092
5093 /* "zc": close fold at cursor or Visual area */
5094 case 'c': if (VIsual_active)
5095 nv_operator(cap);
5096 else
5097 closeFold(curwin->w_cursor.lnum, cap->count1);
5098 curwin->w_p_fen = TRUE;
5099 break;
5100
5101 /* "zC": close fold recursively */
5102 case 'C': if (VIsual_active)
5103 nv_operator(cap);
5104 else
5105 closeFoldRecurse(curwin->w_cursor.lnum);
5106 curwin->w_p_fen = TRUE;
5107 break;
5108
5109 /* "zv": open folds at the cursor */
5110 case 'v': foldOpenCursor();
5111 break;
5112
5113 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5114 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005115 curwin->w_foldinvalid = TRUE; /* recompute folds */
5116 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 foldOpenCursor();
5118 break;
5119
5120 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5121 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005122 curwin->w_foldinvalid = TRUE; /* recompute folds */
5123 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 break;
5125
5126 /* "zm": fold more */
5127 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005128 {
5129 curwin->w_p_fdl -= cap->count1;
5130 if (curwin->w_p_fdl < 0)
5131 curwin->w_p_fdl = 0;
5132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 old_fdl = -1; /* force an update */
5134 curwin->w_p_fen = TRUE;
5135 break;
5136
5137 /* "zM": close all folds */
5138 case 'M': curwin->w_p_fdl = 0;
5139 old_fdl = -1; /* force an update */
5140 curwin->w_p_fen = TRUE;
5141 break;
5142
5143 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005144 case 'r': curwin->w_p_fdl += cap->count1;
5145 {
5146 int d = getDeepestNesting();
5147
5148 if (curwin->w_p_fdl >= d)
5149 curwin->w_p_fdl = d;
5150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 break;
5152
5153 /* "zR": open all folds */
5154 case 'R': curwin->w_p_fdl = getDeepestNesting();
5155 old_fdl = -1; /* force an update */
5156 break;
5157
5158 case 'j': /* "zj" move to next fold downwards */
5159 case 'k': /* "zk" move to next fold upwards */
5160 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5161 cap->count1) == FAIL)
5162 clearopbeep(cap->oap);
5163 break;
5164
5165#endif /* FEAT_FOLDING */
5166
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005167#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005168 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5169 ++no_mapping;
5170 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005171 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005172 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005173 --no_mapping;
5174 --allow_keys;
5175#ifdef FEAT_CMDL_INFO
5176 (void)add_to_showcmd(nchar);
5177#endif
5178 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5179 {
5180 clearopbeep(cap->oap);
5181 break;
5182 }
5183 undo = TRUE;
5184 /*FALLTHROUGH*/
5185
Bram Moolenaarb765d632005-06-07 21:00:02 +00005186 case 'g': /* "zg": add good word to word list */
5187 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005188 case 'G': /* "zG": add good word to temp word list */
5189 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005190 {
5191 char_u *ptr = NULL;
5192 int len;
5193
5194 if (checkclearop(cap->oap))
5195 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005196 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5197 == FAIL)
5198 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005199 if (ptr == NULL)
5200 {
5201 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005202
Bram Moolenaar134bf072013-09-25 18:54:24 +02005203 /* Find bad word under the cursor. When 'spell' is
5204 * off this fails and find_ident_under_cursor() is
5205 * used below. */
5206 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005207 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005208 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005209 if (len != 0 && curwin->w_cursor.col <= pos.col)
5210 ptr = ml_get_pos(&curwin->w_cursor);
5211 curwin->w_cursor = pos;
5212 }
5213
Bram Moolenaarb765d632005-06-07 21:00:02 +00005214 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5215 FIND_IDENT)) == 0)
5216 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005217 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005218 (nchar == 'G' || nchar == 'W')
5219 ? 0 : (int)cap->count1,
5220 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005221 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005222 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005223
Bram Moolenaar43abc522005-12-10 20:15:02 +00005224 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005225 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005226 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005227 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005228#endif
5229
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 default: clearopbeep(cap->oap);
5231 }
5232
5233#ifdef FEAT_FOLDING
5234 /* Redraw when 'foldenable' changed */
5235 if (old_fen != curwin->w_p_fen)
5236 {
5237# ifdef FEAT_DIFF
5238 win_T *wp;
5239
5240 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5241 {
5242 /* Adjust 'foldenable' in diff-synced windows. */
5243 FOR_ALL_WINDOWS(wp)
5244 {
5245 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5246 {
5247 wp->w_p_fen = curwin->w_p_fen;
5248 changed_window_setting_win(wp);
5249 }
5250 }
5251 }
5252# endif
5253 changed_window_setting();
5254 }
5255
5256 /* Redraw when 'foldlevel' changed. */
5257 if (old_fdl != curwin->w_p_fdl)
5258 newFoldLevel();
5259#endif
5260}
5261
5262#ifdef FEAT_GUI
5263/*
5264 * Vertical scrollbar movement.
5265 */
5266 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005267nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268{
5269 if (cap->oap->op_type != OP_NOP)
5270 clearopbeep(cap->oap);
5271
5272 /* Even if an operator was pending, we still want to scroll */
5273 gui_do_scroll();
5274}
5275
5276/*
5277 * Horizontal scrollbar movement.
5278 */
5279 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005280nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281{
5282 if (cap->oap->op_type != OP_NOP)
5283 clearopbeep(cap->oap);
5284
5285 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005286 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287}
5288#endif
5289
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005290#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005291/*
5292 * Click in GUI tab.
5293 */
5294 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005295nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005296{
5297 if (cap->oap->op_type != OP_NOP)
5298 clearopbeep(cap->oap);
5299
5300 /* Even if an operator was pending, we still want to jump tabs. */
5301 goto_tabpage(current_tab);
5302}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005303
5304/*
5305 * Selected item in tab line menu.
5306 */
5307 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005308nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005309{
5310 if (cap->oap->op_type != OP_NOP)
5311 clearopbeep(cap->oap);
5312
5313 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005314 handle_tabmenu();
5315}
5316
5317/*
5318 * Handle selecting an item of the GUI tab line menu.
5319 * Used in Normal and Insert mode.
5320 */
5321 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005322handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005323{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005324 switch (current_tabmenu)
5325 {
5326 case TABLINE_MENU_CLOSE:
5327 if (current_tab == 0)
5328 do_cmdline_cmd((char_u *)"tabclose");
5329 else
5330 {
5331 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5332 current_tab);
5333 do_cmdline_cmd(IObuff);
5334 }
5335 break;
5336
5337 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005338 if (current_tab == 0)
5339 do_cmdline_cmd((char_u *)"$tabnew");
5340 else
5341 {
5342 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5343 current_tab - 1);
5344 do_cmdline_cmd(IObuff);
5345 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005346 break;
5347
5348 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005349 if (current_tab == 0)
5350 do_cmdline_cmd((char_u *)"browse $tabnew");
5351 else
5352 {
5353 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5354 current_tab - 1);
5355 do_cmdline_cmd(IObuff);
5356 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005357 break;
5358 }
5359}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005360#endif
5361
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362/*
5363 * "Q" command.
5364 */
5365 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005366nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367{
5368 /*
5369 * Ignore 'Q' in Visual mode, just give a beep.
5370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005372 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005373 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 do_exmode(FALSE);
5375}
5376
5377/*
5378 * Handle a ":" command.
5379 */
5380 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005381nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382{
5383 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005384 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 if (VIsual_active)
5387 nv_operator(cap);
5388 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 {
5390 if (cap->oap->op_type != OP_NOP)
5391 {
5392 /* Using ":" as a movement is characterwise exclusive. */
5393 cap->oap->motion_type = MCHAR;
5394 cap->oap->inclusive = FALSE;
5395 }
5396 else if (cap->count0)
5397 {
5398 /* translate "count:" into ":.,.+(count - 1)" */
5399 stuffcharReadbuff('.');
5400 if (cap->count0 > 1)
5401 {
5402 stuffReadbuff((char_u *)",.+");
5403 stuffnumReadbuff((long)cap->count0 - 1L);
5404 }
5405 }
5406
5407 /* When typing, don't type below an old message */
5408 if (KeyTyped)
5409 compute_cmdrow();
5410
5411 old_p_im = p_im;
5412
5413 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005414 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5416
5417 /* If 'insertmode' changed, enter or exit Insert mode */
5418 if (p_im != old_p_im)
5419 {
5420 if (p_im)
5421 restart_edit = 'i';
5422 else
5423 restart_edit = 0;
5424 }
5425
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005426 if (cmd_result == FAIL)
5427 /* The Ex command failed, do not execute the operator. */
5428 clearop(cap->oap);
5429 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5431 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005432 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5433 || did_emsg
5434 ))
5435 /* The start of the operator has become invalid by the Ex command.
5436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 clearopbeep(cap->oap);
5438 }
5439}
5440
5441/*
5442 * Handle CTRL-G command.
5443 */
5444 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005445nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 if (VIsual_active) /* toggle Selection/Visual mode */
5448 {
5449 VIsual_select = !VIsual_select;
5450 showmode();
5451 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005452 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 /* print full name if count given or :cd used */
5454 fileinfo((int)cap->count0, FALSE, TRUE);
5455}
5456
5457/*
5458 * Handle CTRL-H <Backspace> command.
5459 */
5460 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005461nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 if (VIsual_active && VIsual_select)
5464 {
5465 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5466 v_visop(cap);
5467 }
5468 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 nv_left(cap);
5470}
5471
5472/*
5473 * CTRL-L: clear screen and redraw.
5474 */
5475 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005476nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477{
5478 if (!checkclearop(cap->oap))
5479 {
5480#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5481 /*
5482 * Right now, the BeBox doesn't seem to have an easy way to detect
5483 * window resizing, so we cheat and make the user detect it
5484 * manually with CTRL-L instead
5485 */
5486 ui_get_shellsize();
5487#endif
5488#ifdef FEAT_SYN_HL
5489 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005490 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02005491# ifdef FEAT_RELTIME
5492 {
5493 win_T *wp;
5494
5495 FOR_ALL_WINDOWS(wp)
5496 wp->w_s->b_syn_slow = FALSE;
5497 }
5498# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499#endif
5500 redraw_later(CLEAR);
5501 }
5502}
5503
5504/*
5505 * CTRL-O: In Select mode: switch to Visual mode for one command.
5506 * Otherwise: Go to older pcmark.
5507 */
5508 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005509nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 if (VIsual_active && VIsual_select)
5512 {
5513 VIsual_select = FALSE;
5514 showmode();
5515 restart_VIsual_select = 2; /* restart Select mode later */
5516 }
5517 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 {
5519 cap->count1 = -cap->count1;
5520 nv_pcmark(cap);
5521 }
5522}
5523
5524/*
5525 * CTRL-^ command, short for ":e #"
5526 */
5527 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005528nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529{
5530 if (!checkclearopq(cap->oap))
5531 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5532 GETF_SETMARK|GETF_ALT, FALSE);
5533}
5534
5535/*
5536 * "Z" commands.
5537 */
5538 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005539nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540{
5541 if (!checkclearopq(cap->oap))
5542 {
5543 switch (cap->nchar)
5544 {
5545 /* "ZZ": equivalent to ":x". */
5546 case 'Z': do_cmdline_cmd((char_u *)"x");
5547 break;
5548
5549 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5550 case 'Q': do_cmdline_cmd((char_u *)"q!");
5551 break;
5552
5553 default: clearopbeep(cap->oap);
5554 }
5555 }
5556}
5557
5558#if defined(FEAT_WINDOWS) || defined(PROTO)
5559/*
5560 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5561 */
5562 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005563do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564{
5565 oparg_T oa;
5566 cmdarg_T ca;
5567
5568 clear_oparg(&oa);
5569 vim_memset(&ca, 0, sizeof(ca));
5570 ca.oap = &oa;
5571 ca.cmdchar = c1;
5572 ca.nchar = c2;
5573 nv_ident(&ca);
5574}
5575#endif
5576
5577/*
5578 * Handle the commands that use the word under the cursor.
5579 * [g] CTRL-] :ta to current identifier
5580 * [g] 'K' run program for current identifier
5581 * [g] '*' / to current identifier or string
5582 * [g] '#' ? to current identifier or string
5583 * g ']' :tselect for current identifier
5584 */
5585 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005586nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587{
5588 char_u *ptr = NULL;
5589 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005590 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005591 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 char_u *p;
5593 char_u *kp; /* value of 'keywordprg' */
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005594 int kp_help; /* 'keywordprg' is ":he" */
5595 int kp_ex; /* 'keywordprg' starts with ":" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 int n = 0; /* init for GCC */
5597 int cmdchar;
5598 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005599 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 char_u *aux_ptr;
5601 int isman;
5602 int isman_s;
5603
5604 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5605 {
5606 cmdchar = cap->nchar;
5607 g_cmd = TRUE;
5608 }
5609 else
5610 {
5611 cmdchar = cap->cmdchar;
5612 g_cmd = FALSE;
5613 }
5614
5615 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5616 cmdchar = '#';
5617
5618 /*
5619 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5620 */
5621 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5622 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5624 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 if (checkclearopq(cap->oap))
5626 return;
5627 }
5628
5629 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5630 (cmdchar == '*' || cmdchar == '#')
5631 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5632 {
5633 clearop(cap->oap);
5634 return;
5635 }
5636
5637 /* Allocate buffer to put the command in. Inserting backslashes can
5638 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5639 * and some numbers. */
5640 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5641 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5642 || STRCMP(kp, ":help") == 0);
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005643 kp_ex = (*kp == ':');
5644 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
5645 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 if (buf == NULL)
5647 return;
5648 buf[0] = NUL;
5649
5650 switch (cmdchar)
5651 {
5652 case '*':
5653 case '#':
5654 /*
5655 * Put cursor at start of word, makes search skip the word
5656 * under the cursor.
5657 * Call setpcmark() first, so "*``" puts the cursor back where
5658 * it was.
5659 */
5660 setpcmark();
5661 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5662
5663 if (!g_cmd && vim_iswordp(ptr))
5664 STRCPY(buf, "\\<");
5665 no_smartcase = TRUE; /* don't use 'smartcase' now */
5666 break;
5667
5668 case 'K':
5669 if (kp_help)
5670 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005671 else if (kp_ex)
5672 {
5673 if (cap->count0 != 0)
5674 vim_snprintf((char *)buf, buflen, "%s %ld",
5675 kp, cap->count0);
5676 else
5677 STRCPY(buf, kp);
5678 STRCAT(buf, " ");
5679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 else
5681 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005682 /* An external command will probably use an argument starting
5683 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005684 while (*ptr == '-' && n > 0)
5685 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005686 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005687 --n;
5688 }
5689 if (n == 0)
5690 {
5691 EMSG(_(e_noident)); /* found dashes only */
5692 vim_free(buf);
5693 return;
5694 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005695
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 /* When a count is given, turn it into a range. Is this
5697 * really what we want? */
5698 isman = (STRCMP(kp, "man") == 0);
5699 isman_s = (STRCMP(kp, "man -s") == 0);
5700 if (cap->count0 != 0 && !(isman || isman_s))
5701 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5702
5703 STRCAT(buf, "! ");
5704 if (cap->count0 == 0 && isman_s)
5705 STRCAT(buf, "man");
5706 else
5707 STRCAT(buf, kp);
5708 STRCAT(buf, " ");
5709 if (cap->count0 != 0 && (isman || isman_s))
5710 {
5711 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5712 STRCAT(buf, " ");
5713 }
5714 }
5715 break;
5716
5717 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005718 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719#ifdef FEAT_CSCOPE
5720 if (p_cst)
5721 STRCPY(buf, "cstag ");
5722 else
5723#endif
5724 STRCPY(buf, "ts ");
5725 break;
5726
5727 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005728 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 if (curbuf->b_help)
5730 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005732 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005733 if (g_cmd)
5734 STRCPY(buf, "tj ");
5735 else
5736 sprintf((char *)buf, "%ldta ", cap->count0);
5737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 }
5739
5740 /*
5741 * Now grab the chars in the identifier
5742 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005743 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005745 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01005746 if (kp_ex)
5747 /* Escape the argument properly for an Ex command */
5748 p = vim_strsave_fnameescape(ptr, FALSE);
5749 else
5750 /* Escape the argument properly for a shell command */
5751 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005752 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005753 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005755 vim_free(buf);
5756 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005758 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5759 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005760 {
5761 vim_free(buf);
5762 vim_free(p);
5763 return;
5764 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005765 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005766 STRCAT(buf, p);
5767 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005769 else
5770 {
5771 if (cmdchar == '*')
5772 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5773 else if (cmdchar == '#')
5774 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005775 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005776 {
5777 if (curbuf->b_help)
5778 /* ":help" handles unescaped argument */
5779 aux_ptr = (char_u *)"";
5780 else
5781 aux_ptr = (char_u *)"\\|\"\n[";
5782 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005783 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005784 aux_ptr = (char_u *)"\\|\"\n*?[";
5785
5786 p = buf + STRLEN(buf);
5787 while (n-- > 0)
5788 {
5789 /* put a backslash before \ and some others */
5790 if (vim_strchr(aux_ptr, *ptr) != NULL)
5791 *p++ = '\\';
5792#ifdef FEAT_MBYTE
5793 /* When current byte is a part of multibyte character, copy all
5794 * bytes of that character. */
5795 if (has_mbyte)
5796 {
5797 int i;
5798 int len = (*mb_ptr2len)(ptr) - 1;
5799
5800 for (i = 0; i < len && n >= 1; ++i, --n)
5801 *p++ = *ptr++;
5802 }
5803#endif
5804 *p++ = *ptr++;
5805 }
5806 *p = NUL;
5807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808
5809 /*
5810 * Execute the command.
5811 */
5812 if (cmdchar == '*' || cmdchar == '#')
5813 {
5814 if (!g_cmd && (
5815#ifdef FEAT_MBYTE
5816 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5817#endif
5818 vim_iswordc(ptr[-1])))
5819 STRCAT(buf, "\\>");
5820#ifdef FEAT_CMDHIST
5821 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005822 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5824#endif
Bram Moolenaar46539112015-02-17 15:43:57 +01005825 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 }
5827 else
5828 do_cmdline_cmd(buf);
5829
5830 vim_free(buf);
5831}
5832
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833/*
5834 * Get visually selected text, within one line only.
5835 * Returns FAIL if more than one line selected.
5836 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005837 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005838get_visual_text(
5839 cmdarg_T *cap,
5840 char_u **pp, /* return: start of selected text */
5841 int *lenp) /* return: length of selected text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842{
5843 if (VIsual_mode != 'V')
5844 unadjust_for_sel();
5845 if (VIsual.lnum != curwin->w_cursor.lnum)
5846 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005847 if (cap != NULL)
5848 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 return FAIL;
5850 }
5851 if (VIsual_mode == 'V')
5852 {
5853 *pp = ml_get_curline();
5854 *lenp = (int)STRLEN(*pp);
5855 }
5856 else
5857 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005858 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 {
5860 *pp = ml_get_pos(&curwin->w_cursor);
5861 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5862 }
5863 else
5864 {
5865 *pp = ml_get_pos(&VIsual);
5866 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5867 }
5868#ifdef FEAT_MBYTE
5869 if (has_mbyte)
5870 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005871 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872#endif
5873 }
5874 reset_VIsual_and_resel();
5875 return OK;
5876}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877
5878/*
5879 * CTRL-T: backwards in tag stack
5880 */
5881 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005882nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883{
5884 if (!checkclearopq(cap->oap))
5885 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5886}
5887
5888/*
5889 * Handle scrolling command 'H', 'L' and 'M'.
5890 */
5891 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005892nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893{
5894 int used = 0;
5895 long n;
5896#ifdef FEAT_FOLDING
5897 linenr_T lnum;
5898#endif
5899 int half;
5900
5901 cap->oap->motion_type = MLINE;
5902 setpcmark();
5903
5904 if (cap->cmdchar == 'L')
5905 {
5906 validate_botline(); /* make sure curwin->w_botline is valid */
5907 curwin->w_cursor.lnum = curwin->w_botline - 1;
5908 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5909 curwin->w_cursor.lnum = 1;
5910 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005911 {
5912#ifdef FEAT_FOLDING
5913 if (hasAnyFolding(curwin))
5914 {
5915 /* Count a fold for one screen line. */
5916 for (n = cap->count1 - 1; n > 0
5917 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5918 {
5919 (void)hasFolding(curwin->w_cursor.lnum,
5920 &curwin->w_cursor.lnum, NULL);
5921 --curwin->w_cursor.lnum;
5922 }
5923 }
5924 else
5925#endif
5926 curwin->w_cursor.lnum -= cap->count1 - 1;
5927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 }
5929 else
5930 {
5931 if (cap->cmdchar == 'M')
5932 {
5933#ifdef FEAT_DIFF
5934 /* Don't count filler lines above the window. */
5935 used -= diff_check_fill(curwin, curwin->w_topline)
5936 - curwin->w_topfill;
5937#endif
5938 validate_botline(); /* make sure w_empty_rows is valid */
5939 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5940 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5941 {
5942#ifdef FEAT_DIFF
5943 /* Count half he number of filler lines to be "below this
5944 * line" and half to be "above the next line". */
5945 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5946 + n) / 2 >= half)
5947 {
5948 --n;
5949 break;
5950 }
5951#endif
5952 used += plines(curwin->w_topline + n);
5953 if (used >= half)
5954 break;
5955#ifdef FEAT_FOLDING
5956 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5957 n = lnum - curwin->w_topline;
5958#endif
5959 }
5960 if (n > 0 && used > curwin->w_height)
5961 --n;
5962 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005963 else /* (cap->cmdchar == 'H') */
5964 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005966#ifdef FEAT_FOLDING
5967 if (hasAnyFolding(curwin))
5968 {
5969 /* Count a fold for one screen line. */
5970 lnum = curwin->w_topline;
5971 while (n-- > 0 && lnum < curwin->w_botline - 1)
5972 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02005973 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005974 ++lnum;
5975 }
5976 n = lnum - curwin->w_topline;
5977 }
5978#endif
5979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 curwin->w_cursor.lnum = curwin->w_topline + n;
5981 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5982 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5983 }
5984
5985 cursor_correct(); /* correct for 'so' */
5986 beginline(BL_SOL | BL_FIX);
5987}
5988
5989/*
5990 * Cursor right commands.
5991 */
5992 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005993nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994{
5995 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005996 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005998 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5999 {
6000 /* <C-Right> and <S-Right> move a word or WORD right */
6001 if (mod_mask & MOD_MASK_CTRL)
6002 cap->arg = TRUE;
6003 nv_wordcmd(cap);
6004 return;
6005 }
6006
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 cap->oap->motion_type = MCHAR;
6008 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006009 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006011#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006013 * In virtual edit mode, there's no such thing as "past_line", as lines
6014 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 */
6016 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006017 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018#endif
6019
6020 for (n = cap->count1; n > 0; --n)
6021 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006022 if ((!past_line && oneright() == FAIL)
6023 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006024 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 {
6026 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006027 * <Space> wraps to next line if 'whichwrap' has 's'.
6028 * 'l' wraps to next line if 'whichwrap' has 'l'.
6029 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 */
6031 if ( ((cap->cmdchar == ' '
6032 && vim_strchr(p_ww, 's') != NULL)
6033 || (cap->cmdchar == 'l'
6034 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006035 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 && vim_strchr(p_ww, '>') != NULL))
6037 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6038 {
6039 /* When deleting we also count the NL as a character.
6040 * Set cap->oap->inclusive when last char in the line is
6041 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006042 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006044 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 cap->oap->inclusive = TRUE;
6046 else
6047 {
6048 ++curwin->w_cursor.lnum;
6049 curwin->w_cursor.col = 0;
6050#ifdef FEAT_VIRTUALEDIT
6051 curwin->w_cursor.coladd = 0;
6052#endif
6053 curwin->w_set_curswant = TRUE;
6054 cap->oap->inclusive = FALSE;
6055 }
6056 continue;
6057 }
6058 if (cap->oap->op_type == OP_NOP)
6059 {
6060 /* Only beep and flush if not moved at all */
6061 if (n == cap->count1)
6062 beep_flush();
6063 }
6064 else
6065 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006066 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 cap->oap->inclusive = TRUE;
6068 }
6069 break;
6070 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006071 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 {
6073 curwin->w_set_curswant = TRUE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006074#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 if (virtual_active())
6076 oneright();
6077 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006080#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 if (has_mbyte)
6082 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006083 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 ++curwin->w_cursor.col;
6087 }
6088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 }
6090#ifdef FEAT_FOLDING
6091 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6092 && cap->oap->op_type == OP_NOP)
6093 foldOpenCursor();
6094#endif
6095}
6096
6097/*
6098 * Cursor left commands.
6099 *
6100 * Returns TRUE when operator end should not be adjusted.
6101 */
6102 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006103nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104{
6105 long n;
6106
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006107 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6108 {
6109 /* <C-Left> and <S-Left> move a word or WORD left */
6110 if (mod_mask & MOD_MASK_CTRL)
6111 cap->arg = 1;
6112 nv_bck_word(cap);
6113 return;
6114 }
6115
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 cap->oap->motion_type = MCHAR;
6117 cap->oap->inclusive = FALSE;
6118 for (n = cap->count1; n > 0; --n)
6119 {
6120 if (oneleft() == FAIL)
6121 {
6122 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6123 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6124 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6125 */
6126 if ( (((cap->cmdchar == K_BS
6127 || cap->cmdchar == Ctrl_H)
6128 && vim_strchr(p_ww, 'b') != NULL)
6129 || (cap->cmdchar == 'h'
6130 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006131 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 && vim_strchr(p_ww, '<') != NULL))
6133 && curwin->w_cursor.lnum > 1)
6134 {
6135 --(curwin->w_cursor.lnum);
6136 coladvance((colnr_T)MAXCOL);
6137 curwin->w_set_curswant = TRUE;
6138
6139 /* When the NL before the first char has to be deleted we
6140 * put the cursor on the NUL after the previous line.
6141 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006142 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 if ( (cap->oap->op_type == OP_DELETE
6144 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006145 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006147 char_u *cp = ml_get_cursor();
6148
6149 if (*cp != NUL)
6150 {
6151#ifdef FEAT_MBYTE
6152 if (has_mbyte)
6153 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6154 else
6155#endif
6156 ++curwin->w_cursor.col;
6157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 cap->retval |= CA_NO_ADJ_OP_END;
6159 }
6160 continue;
6161 }
6162 /* Only beep and flush if not moved at all */
6163 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6164 beep_flush();
6165 break;
6166 }
6167 }
6168#ifdef FEAT_FOLDING
6169 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6170 && cap->oap->op_type == OP_NOP)
6171 foldOpenCursor();
6172#endif
6173}
6174
6175/*
6176 * Cursor up commands.
6177 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6178 */
6179 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006180nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006182 if (mod_mask & MOD_MASK_SHIFT)
6183 {
6184 /* <S-Up> is page up */
6185 cap->arg = BACKWARD;
6186 nv_page(cap);
6187 }
6188 else
6189 {
6190 cap->oap->motion_type = MLINE;
6191 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6192 clearopbeep(cap->oap);
6193 else if (cap->arg)
6194 beginline(BL_WHITE | BL_FIX);
6195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196}
6197
6198/*
6199 * Cursor down commands.
6200 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6201 */
6202 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02006203nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006205 if (mod_mask & MOD_MASK_SHIFT)
6206 {
6207 /* <S-Down> is page down */
6208 cap->arg = FORWARD;
6209 nv_page(cap);
6210 }
6211 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6213 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006214 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar74a47162017-02-26 19:09:05 +01006215 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006216 if (curwin->w_llist_ref == NULL)
6217 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6218 else
6219 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar74a47162017-02-26 19:09:05 +01006220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 else
6222#endif
6223 {
6224#ifdef FEAT_CMDWIN
6225 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006226 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 cmdwin_result = CAR;
6228 else
6229#endif
6230 {
6231 cap->oap->motion_type = MLINE;
6232 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6233 clearopbeep(cap->oap);
6234 else if (cap->arg)
6235 beginline(BL_WHITE | BL_FIX);
6236 }
6237 }
6238}
6239
6240#ifdef FEAT_SEARCHPATH
6241/*
6242 * Grab the file name under the cursor and edit it.
6243 */
6244 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006245nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246{
6247 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006248 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006250 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 {
6252 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006253 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 return;
6255 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006256#ifdef FEAT_AUTOCMD
6257 if (curbuf_locked())
6258 {
6259 clearop(cap->oap);
6260 return;
6261 }
6262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006264 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265
6266 if (ptr != NULL)
6267 {
6268 /* do autowrite if necessary */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006269 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02006270 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006272 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006273 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006274 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006275 {
6276 curwin->w_cursor.lnum = lnum;
6277 check_cursor_lnum();
6278 beginline(BL_SOL | BL_FIX);
6279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 vim_free(ptr);
6281 }
6282 else
6283 clearop(cap->oap);
6284}
6285#endif
6286
6287/*
6288 * <End> command: to end of current line or last line.
6289 */
6290 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006291nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006293 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006295 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 nv_goto(cap);
6297 cap->count1 = 1; /* to end of current line */
6298 }
6299 nv_dollar(cap);
6300}
6301
6302/*
6303 * Handle the "$" command.
6304 */
6305 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006306nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307{
6308 cap->oap->motion_type = MCHAR;
6309 cap->oap->inclusive = TRUE;
6310#ifdef FEAT_VIRTUALEDIT
6311 /* In virtual mode when off the edge of a line and an operator
6312 * is pending (whew!) keep the cursor where it is.
6313 * Otherwise, send it to the end of the line. */
6314 if (!virtual_active() || gchar_cursor() != NUL
6315 || cap->oap->op_type == OP_NOP)
6316#endif
6317 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6318 if (cursor_down((long)(cap->count1 - 1),
6319 cap->oap->op_type == OP_NOP) == FAIL)
6320 clearopbeep(cap->oap);
6321#ifdef FEAT_FOLDING
6322 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6323 foldOpenCursor();
6324#endif
6325}
6326
6327/*
6328 * Implementation of '?' and '/' commands.
6329 * If cap->arg is TRUE don't set PC mark.
6330 */
6331 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006332nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333{
6334 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02006335 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336
6337 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6338 {
6339 /* Translate "g??" to "g?g?" */
6340 cap->cmdchar = 'g';
6341 cap->nchar = '?';
6342 nv_operator(cap);
6343 return;
6344 }
6345
Bram Moolenaardda933d2016-09-03 21:04:58 +02006346 /* When using 'incsearch' the cursor may be moved to set a different search
6347 * start position. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6349
6350 if (cap->searchbuf == NULL)
6351 {
6352 clearop(oap);
6353 return;
6354 }
6355
Bram Moolenaar46539112015-02-17 15:43:57 +01006356 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006357 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaardda933d2016-09-03 21:04:58 +02006358 ? 0 : SEARCH_MARK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359}
6360
6361/*
6362 * Handle "N" and "n" commands.
6363 * cap->arg is SEARCH_REV for "N", 0 for "n".
6364 */
6365 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006366nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367{
Bram Moolenaar46539112015-02-17 15:43:57 +01006368 pos_T old = curwin->w_cursor;
6369 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6370
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006371 if (i == 1 && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01006372 {
6373 /* Avoid getting stuck on the current cursor position, which can
6374 * happen when an offset is given and the cursor is on the last char
6375 * in the buffer: Repeat with count + 1. */
6376 cap->count1 += 1;
6377 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6378 cap->count1 -= 1;
6379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380}
6381
6382/*
6383 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6384 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01006385 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 */
Bram Moolenaar46539112015-02-17 15:43:57 +01006387 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006388normal_search(
6389 cmdarg_T *cap,
6390 int dir,
6391 char_u *pat,
6392 int opt) /* extra flags for do_search() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393{
6394 int i;
6395
6396 cap->oap->motion_type = MCHAR;
6397 cap->oap->inclusive = FALSE;
6398 cap->oap->use_reg_one = TRUE;
6399 curwin->w_set_curswant = TRUE;
6400
6401 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006402 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 if (i == 0)
6404 clearop(cap->oap);
6405 else
6406 {
6407 if (i == 2)
6408 cap->oap->motion_type = MLINE;
6409#ifdef FEAT_VIRTUALEDIT
6410 curwin->w_cursor.coladd = 0;
6411#endif
6412#ifdef FEAT_FOLDING
6413 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6414 foldOpenCursor();
6415#endif
6416 }
6417
6418 /* "/$" will put the cursor after the end of the line, may need to
6419 * correct that here */
6420 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01006421 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422}
6423
6424/*
6425 * Character search commands.
6426 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6427 * ',' and FALSE for ';'.
6428 * cap->nchar is NUL for ',' and ';' (repeat the search)
6429 */
6430 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006431nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432{
6433 int t_cmd;
6434
6435 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6436 t_cmd = TRUE;
6437 else
6438 t_cmd = FALSE;
6439
6440 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6442 clearopbeep(cap->oap);
6443 else
6444 {
6445 curwin->w_set_curswant = TRUE;
6446#ifdef FEAT_VIRTUALEDIT
6447 /* Include a Tab for "tx" and for "dfx". */
6448 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6449 && (t_cmd || cap->oap->op_type != OP_NOP))
6450 {
6451 colnr_T scol, ecol;
6452
6453 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6454 curwin->w_cursor.coladd = ecol - scol;
6455 }
6456 else
6457 curwin->w_cursor.coladd = 0;
6458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460#ifdef FEAT_FOLDING
6461 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6462 foldOpenCursor();
6463#endif
6464 }
6465}
6466
6467/*
6468 * "[" and "]" commands.
6469 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6470 */
6471 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006472nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006474 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 pos_T prev_pos;
6476 pos_T *pos = NULL; /* init for GCC */
6477 pos_T old_pos; /* cursor position before command */
6478 int flag;
6479 long n;
6480 int findc;
6481 int c;
6482
6483 cap->oap->motion_type = MCHAR;
6484 cap->oap->inclusive = FALSE;
6485 old_pos = curwin->w_cursor;
6486#ifdef FEAT_VIRTUALEDIT
6487 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6488#endif
6489
6490#ifdef FEAT_SEARCHPATH
6491 /*
6492 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6493 */
6494 if (cap->nchar == 'f')
6495 nv_gotofile(cap);
6496 else
6497#endif
6498
6499#ifdef FEAT_FIND_ID
6500 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006501 * Find the occurrence(s) of the identifier or define under cursor
6502 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 *
6504 * search list jump
6505 * fwd bwd fwd bwd fwd bwd
6506 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6507 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6508 */
6509 if (vim_strchr((char_u *)
6510#ifdef EBCDIC
6511 "iI\005dD\067",
6512#else
6513 "iI\011dD\004",
6514#endif
6515 cap->nchar) != NULL)
6516 {
6517 char_u *ptr;
6518 int len;
6519
6520 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6521 clearop(cap->oap);
6522 else
6523 {
6524 find_pattern_in_path(ptr, 0, len, TRUE,
6525 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6526 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6527 cap->count1,
6528 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6529 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6530 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6531 (linenr_T)MAXLNUM);
6532 curwin->w_set_curswant = TRUE;
6533 }
6534 }
6535 else
6536#endif
6537
6538 /*
6539 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6540 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6541 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6542 * "[m" or "]m" search for prev/next start of (Java) method.
6543 * "[M" or "]M" search for prev/next end of (Java) method.
6544 */
6545 if ( (cap->cmdchar == '['
6546 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6547 || (cap->cmdchar == ']'
6548 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6549 {
6550 if (cap->nchar == '*')
6551 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 prev_pos.lnum = 0;
6553 if (cap->nchar == 'm' || cap->nchar == 'M')
6554 {
6555 if (cap->cmdchar == '[')
6556 findc = '{';
6557 else
6558 findc = '}';
6559 n = 9999;
6560 }
6561 else
6562 {
6563 findc = cap->nchar;
6564 n = cap->count1;
6565 }
6566 for ( ; n > 0; --n)
6567 {
6568 if ((pos = findmatchlimit(cap->oap, findc,
6569 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6570 {
6571 if (new_pos.lnum == 0) /* nothing found */
6572 {
6573 if (cap->nchar != 'm' && cap->nchar != 'M')
6574 clearopbeep(cap->oap);
6575 }
6576 else
6577 pos = &new_pos; /* use last one found */
6578 break;
6579 }
6580 prev_pos = new_pos;
6581 curwin->w_cursor = *pos;
6582 new_pos = *pos;
6583 }
6584 curwin->w_cursor = old_pos;
6585
6586 /*
6587 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6588 * brought us to the match for "[m" and "]M" when inside a method.
6589 * Try finding the '{' or '}' we want to be at.
6590 * Also repeat for the given count.
6591 */
6592 if (cap->nchar == 'm' || cap->nchar == 'M')
6593 {
6594 /* norm is TRUE for "]M" and "[m" */
6595 int norm = ((findc == '{') == (cap->nchar == 'm'));
6596
6597 n = cap->count1;
6598 /* found a match: we were inside a method */
6599 if (prev_pos.lnum != 0)
6600 {
6601 pos = &prev_pos;
6602 curwin->w_cursor = prev_pos;
6603 if (norm)
6604 --n;
6605 }
6606 else
6607 pos = NULL;
6608 while (n > 0)
6609 {
6610 for (;;)
6611 {
6612 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6613 {
6614 /* if not found anything, that's an error */
6615 if (pos == NULL)
6616 clearopbeep(cap->oap);
6617 n = 0;
6618 break;
6619 }
6620 c = gchar_cursor();
6621 if (c == '{' || c == '}')
6622 {
6623 /* Must have found end/start of class: use it.
6624 * Or found the place to be at. */
6625 if ((c == findc && norm) || (n == 1 && !norm))
6626 {
6627 new_pos = curwin->w_cursor;
6628 pos = &new_pos;
6629 n = 0;
6630 }
6631 /* if no match found at all, we started outside of the
6632 * class and we're inside now. Just go on. */
6633 else if (new_pos.lnum == 0)
6634 {
6635 new_pos = curwin->w_cursor;
6636 pos = &new_pos;
6637 }
6638 /* found start/end of other method: go to match */
6639 else if ((pos = findmatchlimit(cap->oap, findc,
6640 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6641 0)) == NULL)
6642 n = 0;
6643 else
6644 curwin->w_cursor = *pos;
6645 break;
6646 }
6647 }
6648 --n;
6649 }
6650 curwin->w_cursor = old_pos;
6651 if (pos == NULL && new_pos.lnum != 0)
6652 clearopbeep(cap->oap);
6653 }
6654 if (pos != NULL)
6655 {
6656 setpcmark();
6657 curwin->w_cursor = *pos;
6658 curwin->w_set_curswant = TRUE;
6659#ifdef FEAT_FOLDING
6660 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6661 && cap->oap->op_type == OP_NOP)
6662 foldOpenCursor();
6663#endif
6664 }
6665 }
6666
6667 /*
6668 * "[[", "[]", "]]" and "][": move to start or end of function
6669 */
6670 else if (cap->nchar == '[' || cap->nchar == ']')
6671 {
6672 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6673 flag = '{';
6674 else
6675 flag = '}'; /* "][" or "[]" */
6676
6677 curwin->w_set_curswant = TRUE;
6678 /*
6679 * Imitate strange Vi behaviour: When using "]]" with an operator
6680 * we also stop at '}'.
6681 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006682 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 (cap->oap->op_type != OP_NOP
6684 && cap->arg == FORWARD && flag == '{')))
6685 clearopbeep(cap->oap);
6686 else
6687 {
6688 if (cap->oap->op_type == OP_NOP)
6689 beginline(BL_WHITE | BL_FIX);
6690#ifdef FEAT_FOLDING
6691 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6692 foldOpenCursor();
6693#endif
6694 }
6695 }
6696
6697 /*
6698 * "[p", "[P", "]P" and "]p": put with indent adjustment
6699 */
6700 else if (cap->nchar == 'p' || cap->nchar == 'P')
6701 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006702 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006704 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6705 ? FORWARD : BACKWARD;
6706 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006707 int was_visual = VIsual_active;
6708 int line_count = curbuf->b_ml.ml_line_count;
6709 pos_T start, end;
6710
6711 if (VIsual_active)
6712 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006713 start = LTOREQ_POS(VIsual, curwin->w_cursor)
Bram Moolenaar27bed202014-03-12 17:42:04 +01006714 ? VIsual : curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006715 end = EQUAL_POS(start,VIsual) ? curwin->w_cursor : VIsual;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006716 curwin->w_cursor = (dir == BACKWARD ? start : end);
6717 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006718# ifdef FEAT_CLIPBOARD
6719 adjust_clip_reg(&regname);
6720# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006722
6723 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006724 if (was_visual)
6725 {
6726 VIsual = start;
6727 curwin->w_cursor = end;
6728 if (dir == BACKWARD)
6729 {
6730 /* adjust lines */
6731 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6732 curwin->w_cursor.lnum +=
6733 curbuf->b_ml.ml_line_count - line_count;
6734 }
6735
6736 VIsual_active = TRUE;
6737 if (VIsual_mode == 'V')
6738 {
6739 /* delete visually selected lines */
6740 cap->cmdchar = 'd';
6741 cap->nchar = NUL;
6742 cap->oap->regname = regname;
6743 nv_operator(cap);
6744 do_pending_operator(cap, 0, FALSE);
6745 }
6746 if (VIsual_active)
6747 {
6748 end_visual_mode();
6749 redraw_later(SOME_VALID);
6750 }
6751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 }
6753 }
6754
6755 /*
6756 * "['", "[`", "]'" and "]`": jump to next mark
6757 */
6758 else if (cap->nchar == '\'' || cap->nchar == '`')
6759 {
6760 pos = &curwin->w_cursor;
6761 for (n = cap->count1; n > 0; --n)
6762 {
6763 prev_pos = *pos;
6764 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6765 cap->nchar == '\'');
6766 if (pos == NULL)
6767 break;
6768 }
6769 if (pos == NULL)
6770 pos = &prev_pos;
6771 nv_cursormark(cap, cap->nchar == '\'', pos);
6772 }
6773
6774#ifdef FEAT_MOUSE
6775 /*
6776 * [ or ] followed by a middle mouse click: put selected text with
6777 * indent adjustment. Any other button just does as usual.
6778 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006779 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 {
6781 (void)do_mouse(cap->oap, cap->nchar,
6782 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6783 cap->count1, PUT_FIXINDENT);
6784 }
6785#endif /* FEAT_MOUSE */
6786
6787#ifdef FEAT_FOLDING
6788 /*
6789 * "[z" and "]z": move to start or end of open fold.
6790 */
6791 else if (cap->nchar == 'z')
6792 {
6793 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6794 cap->count1) == FAIL)
6795 clearopbeep(cap->oap);
6796 }
6797#endif
6798
6799#ifdef FEAT_DIFF
6800 /*
6801 * "[c" and "]c": move to next or previous diff-change.
6802 */
6803 else if (cap->nchar == 'c')
6804 {
6805 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6806 cap->count1) == FAIL)
6807 clearopbeep(cap->oap);
6808 }
6809#endif
6810
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006811#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006812 /*
6813 * "[s", "[S", "]s" and "]S": move to next spell error.
6814 */
6815 else if (cap->nchar == 's' || cap->nchar == 'S')
6816 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006817 setpcmark();
6818 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006819 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6820 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006821 {
6822 clearopbeep(cap->oap);
6823 break;
6824 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006825# ifdef FEAT_FOLDING
6826 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6827 foldOpenCursor();
6828# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006829 }
6830#endif
6831
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 /* Not a valid cap->nchar. */
6833 else
6834 clearopbeep(cap->oap);
6835}
6836
6837/*
6838 * Handle Normal mode "%" command.
6839 */
6840 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006841nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842{
6843 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006844#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 linenr_T lnum = curwin->w_cursor.lnum;
6846#endif
6847
6848 cap->oap->inclusive = TRUE;
6849 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6850 {
6851 if (cap->count0 > 100)
6852 clearopbeep(cap->oap);
6853 else
6854 {
6855 cap->oap->motion_type = MLINE;
6856 setpcmark();
6857 /* Round up, so CTRL-G will give same value. Watch out for a
6858 * large line count, the line number must not go negative! */
6859 if (curbuf->b_ml.ml_line_count > 1000000)
6860 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6861 / 100L * cap->count0;
6862 else
6863 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6864 cap->count0 + 99L) / 100L;
6865 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6866 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6867 beginline(BL_SOL | BL_FIX);
6868 }
6869 }
6870 else /* "%" : go to matching paren */
6871 {
6872 cap->oap->motion_type = MCHAR;
6873 cap->oap->use_reg_one = TRUE;
6874 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6875 clearopbeep(cap->oap);
6876 else
6877 {
6878 setpcmark();
6879 curwin->w_cursor = *pos;
6880 curwin->w_set_curswant = TRUE;
6881#ifdef FEAT_VIRTUALEDIT
6882 curwin->w_cursor.coladd = 0;
6883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 }
6886 }
6887#ifdef FEAT_FOLDING
6888 if (cap->oap->op_type == OP_NOP
6889 && lnum != curwin->w_cursor.lnum
6890 && (fdo_flags & FDO_PERCENT)
6891 && KeyTyped)
6892 foldOpenCursor();
6893#endif
6894}
6895
6896/*
6897 * Handle "(" and ")" commands.
6898 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6899 */
6900 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006901nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902{
6903 cap->oap->motion_type = MCHAR;
6904 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006905 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6906 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 curwin->w_set_curswant = TRUE;
6908
6909 if (findsent(cap->arg, cap->count1) == FAIL)
6910 clearopbeep(cap->oap);
6911 else
6912 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006913 /* Don't leave the cursor on the NUL past end of line. */
6914 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915#ifdef FEAT_VIRTUALEDIT
6916 curwin->w_cursor.coladd = 0;
6917#endif
6918#ifdef FEAT_FOLDING
6919 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6920 foldOpenCursor();
6921#endif
6922 }
6923}
6924
6925/*
6926 * "m" command: Mark a position.
6927 */
6928 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006929nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930{
6931 if (!checkclearop(cap->oap))
6932 {
6933 if (setmark(cap->nchar) == FAIL)
6934 clearopbeep(cap->oap);
6935 }
6936}
6937
6938/*
6939 * "{" and "}" commands.
6940 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6941 */
6942 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006943nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944{
6945 cap->oap->motion_type = MCHAR;
6946 cap->oap->inclusive = FALSE;
6947 cap->oap->use_reg_one = TRUE;
6948 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006949 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 clearopbeep(cap->oap);
6951 else
6952 {
6953#ifdef FEAT_VIRTUALEDIT
6954 curwin->w_cursor.coladd = 0;
6955#endif
6956#ifdef FEAT_FOLDING
6957 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6958 foldOpenCursor();
6959#endif
6960 }
6961}
6962
6963/*
6964 * "u" command: Undo or make lower case.
6965 */
6966 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006967nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006969 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 {
6971 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6972 cap->cmdchar = 'g';
6973 cap->nchar = 'u';
6974 nv_operator(cap);
6975 }
6976 else
6977 nv_kundo(cap);
6978}
6979
6980/*
6981 * <Undo> command.
6982 */
6983 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006984nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985{
6986 if (!checkclearopq(cap->oap))
6987 {
6988 u_undo((int)cap->count1);
6989 curwin->w_set_curswant = TRUE;
6990 }
6991}
6992
6993/*
6994 * Handle the "r" command.
6995 */
6996 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006997nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998{
6999 char_u *ptr;
7000 int had_ctrl_v;
7001 long n;
7002
7003 if (checkclearop(cap->oap))
7004 return;
7005
7006 /* get another character */
7007 if (cap->nchar == Ctrl_V)
7008 {
7009 had_ctrl_v = Ctrl_V;
7010 cap->nchar = get_literal();
7011 /* Don't redo a multibyte character with CTRL-V. */
7012 if (cap->nchar > DEL)
7013 had_ctrl_v = NUL;
7014 }
7015 else
7016 had_ctrl_v = NUL;
7017
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007018 /* Abort if the character is a special key. */
7019 if (IS_SPECIAL(cap->nchar))
7020 {
7021 clearopbeep(cap->oap);
7022 return;
7023 }
7024
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 /* Visual mode "r" */
7026 if (VIsual_active)
7027 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00007028 if (got_int)
7029 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01007030 if (had_ctrl_v)
7031 {
7032 if (cap->nchar == '\r')
7033 cap->nchar = -1;
7034 else if (cap->nchar == '\n')
7035 cap->nchar = -2;
7036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 nv_operator(cap);
7038 return;
7039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040
7041#ifdef FEAT_VIRTUALEDIT
7042 /* Break tabs, etc. */
7043 if (virtual_active())
7044 {
7045 if (u_save_cursor() == FAIL)
7046 return;
7047 if (gchar_cursor() == NUL)
7048 {
7049 /* Add extra space and put the cursor on the first one. */
7050 coladvance_force((colnr_T)(getviscol() + cap->count1));
7051 curwin->w_cursor.col -= cap->count1;
7052 }
7053 else if (gchar_cursor() == TAB)
7054 coladvance_force(getviscol());
7055 }
7056#endif
7057
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007058 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007060 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061#ifdef FEAT_MBYTE
7062 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7063#endif
7064 )
7065 {
7066 clearopbeep(cap->oap);
7067 return;
7068 }
7069
7070 /*
7071 * Replacing with a TAB is done by edit() when it is complicated because
7072 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7073 * Other characters are done below to avoid problems with things like
7074 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7075 */
7076 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7077 {
7078 stuffnumReadbuff(cap->count1);
7079 stuffcharReadbuff('R');
7080 stuffcharReadbuff('\t');
7081 stuffcharReadbuff(ESC);
7082 return;
7083 }
7084
7085 /* save line for undo */
7086 if (u_save_cursor() == FAIL)
7087 return;
7088
7089 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7090 {
7091 /*
7092 * Replace character(s) by a single newline.
7093 * Strange vi behaviour: Only one newline is inserted.
7094 * Delete the characters here.
7095 * Insert the newline with an insert command, takes care of
7096 * autoindent. The insert command depends on being on the last
7097 * character of a line or not.
7098 */
7099#ifdef FEAT_MBYTE
7100 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7101#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007102 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103#endif
7104 stuffcharReadbuff('\r');
7105 stuffcharReadbuff(ESC);
7106
7107 /* Give 'r' to edit(), to get the redo command right. */
7108 invoke_edit(cap, TRUE, 'r', FALSE);
7109 }
7110 else
7111 {
7112 prep_redo(cap->oap->regname, cap->count1,
7113 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7114
7115 curbuf->b_op_start = curwin->w_cursor;
7116#ifdef FEAT_MBYTE
7117 if (has_mbyte)
7118 {
7119 int old_State = State;
7120
7121 if (cap->ncharC1 != 0)
7122 AppendCharToRedobuff(cap->ncharC1);
7123 if (cap->ncharC2 != 0)
7124 AppendCharToRedobuff(cap->ncharC2);
7125
7126 /* This is slow, but it handles replacing a single-byte with a
7127 * multi-byte and the other way around. Also handles adding
7128 * composing characters for utf-8. */
7129 for (n = cap->count1; n > 0; --n)
7130 {
7131 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007132 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7133 {
7134 int c = ins_copychar(curwin->w_cursor.lnum
7135 + (cap->nchar == Ctrl_Y ? -1 : 1));
7136 if (c != NUL)
7137 ins_char(c);
7138 else
7139 /* will be decremented further down */
7140 ++curwin->w_cursor.col;
7141 }
7142 else
7143 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 State = old_State;
7145 if (cap->ncharC1 != 0)
7146 ins_char(cap->ncharC1);
7147 if (cap->ncharC2 != 0)
7148 ins_char(cap->ncharC2);
7149 }
7150 }
7151 else
7152#endif
7153 {
7154 /*
7155 * Replace the characters within one line.
7156 */
7157 for (n = cap->count1; n > 0; --n)
7158 {
7159 /*
7160 * Get ptr again, because u_save and/or showmatch() will have
7161 * released the line. At the same time we let know that the
7162 * line will be changed.
7163 */
7164 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007165 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7166 {
7167 int c = ins_copychar(curwin->w_cursor.lnum
7168 + (cap->nchar == Ctrl_Y ? -1 : 1));
7169 if (c != NUL)
7170 ptr[curwin->w_cursor.col] = c;
7171 }
7172 else
7173 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 if (p_sm && msg_silent == 0)
7175 showmatch(cap->nchar);
7176 ++curwin->w_cursor.col;
7177 }
7178#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007179 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007181 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182
Bram Moolenaar009b2592004-10-24 19:18:58 +00007183 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007184 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007186 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 }
7188#endif
7189
7190 /* mark the buffer as changed and prepare for displaying */
7191 changed_bytes(curwin->w_cursor.lnum,
7192 (colnr_T)(curwin->w_cursor.col - cap->count1));
7193 }
7194 --curwin->w_cursor.col; /* cursor on the last replaced char */
7195#ifdef FEAT_MBYTE
7196 /* if the character on the left of the current cursor is a multi-byte
7197 * character, move two characters left */
7198 if (has_mbyte)
7199 mb_adjust_cursor();
7200#endif
7201 curbuf->b_op_end = curwin->w_cursor;
7202 curwin->w_set_curswant = TRUE;
7203 set_last_insert(cap->nchar);
7204 }
7205}
7206
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207/*
7208 * 'o': Exchange start and end of Visual area.
7209 * 'O': same, but in block mode exchange left and right corners.
7210 */
7211 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007212v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213{
7214 pos_T old_cursor;
7215 colnr_T left, right;
7216
7217 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7218 {
7219 old_cursor = curwin->w_cursor;
7220 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7221 curwin->w_cursor.lnum = VIsual.lnum;
7222 coladvance(left);
7223 VIsual = curwin->w_cursor;
7224
7225 curwin->w_cursor.lnum = old_cursor.lnum;
7226 curwin->w_curswant = right;
7227 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7228 * right one column */
7229 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7230 ++curwin->w_curswant;
7231 coladvance(curwin->w_curswant);
7232 if (curwin->w_cursor.col == old_cursor.col
7233#ifdef FEAT_VIRTUALEDIT
7234 && (!virtual_active()
7235 || curwin->w_cursor.coladd == old_cursor.coladd)
7236#endif
7237 )
7238 {
7239 curwin->w_cursor.lnum = VIsual.lnum;
7240 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7241 ++right;
7242 coladvance(right);
7243 VIsual = curwin->w_cursor;
7244
7245 curwin->w_cursor.lnum = old_cursor.lnum;
7246 coladvance(left);
7247 curwin->w_curswant = left;
7248 }
7249 }
7250 else
7251 {
7252 old_cursor = curwin->w_cursor;
7253 curwin->w_cursor = VIsual;
7254 VIsual = old_cursor;
7255 curwin->w_set_curswant = TRUE;
7256 }
7257}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258
7259/*
7260 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7261 */
7262 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007263nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 if (VIsual_active) /* "R" is replace lines */
7266 {
7267 cap->cmdchar = 'c';
7268 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007269 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 VIsual_mode = 'V';
7271 nv_operator(cap);
7272 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007273 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 {
7275 if (!curbuf->b_p_ma)
7276 EMSG(_(e_modifiable));
7277 else
7278 {
7279#ifdef FEAT_VIRTUALEDIT
7280 if (virtual_active())
7281 coladvance(getviscol());
7282#endif
7283 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7284 }
7285 }
7286}
7287
7288#ifdef FEAT_VREPLACE
7289/*
7290 * "gr".
7291 */
7292 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007293nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 if (VIsual_active)
7296 {
7297 cap->cmdchar = 'r';
7298 cap->nchar = cap->extra_char;
7299 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7300 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007301 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 {
7303 if (!curbuf->b_p_ma)
7304 EMSG(_(e_modifiable));
7305 else
7306 {
7307 if (cap->extra_char == Ctrl_V) /* get another character */
7308 cap->extra_char = get_literal();
7309 stuffcharReadbuff(cap->extra_char);
7310 stuffcharReadbuff(ESC);
7311# ifdef FEAT_VIRTUALEDIT
7312 if (virtual_active())
7313 coladvance(getviscol());
7314# endif
7315 invoke_edit(cap, TRUE, 'v', FALSE);
7316 }
7317 }
7318}
7319#endif
7320
7321/*
7322 * Swap case for "~" command, when it does not work like an operator.
7323 */
7324 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007325n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326{
7327 long n;
7328 pos_T startpos;
7329 int did_change = 0;
7330#ifdef FEAT_NETBEANS_INTG
7331 pos_T pos;
7332 char_u *ptr;
7333 int count;
7334#endif
7335
7336 if (checkclearopq(cap->oap))
7337 return;
7338
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007339 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 {
7341 clearopbeep(cap->oap);
7342 return;
7343 }
7344
7345 prep_redo_cmd(cap);
7346
7347 if (u_save_cursor() == FAIL)
7348 return;
7349
7350 startpos = curwin->w_cursor;
7351#ifdef FEAT_NETBEANS_INTG
7352 pos = startpos;
7353#endif
7354 for (n = cap->count1; n > 0; --n)
7355 {
7356 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7357 inc_cursor();
7358 if (gchar_cursor() == NUL)
7359 {
7360 if (vim_strchr(p_ww, '~') != NULL
7361 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7362 {
7363#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007364 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 {
7366 if (did_change)
7367 {
7368 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007369 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007370 netbeans_removed(curbuf, pos.lnum, pos.col,
7371 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007373 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 }
7375 pos.col = 0;
7376 pos.lnum++;
7377 }
7378#endif
7379 ++curwin->w_cursor.lnum;
7380 curwin->w_cursor.col = 0;
7381 if (n > 1)
7382 {
7383 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7384 break;
7385 u_clearline();
7386 }
7387 }
7388 else
7389 break;
7390 }
7391 }
7392#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007393 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 {
7395 ptr = ml_get(pos.lnum);
7396 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007397 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7398 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 }
7400#endif
7401
7402
7403 check_cursor();
7404 curwin->w_set_curswant = TRUE;
7405 if (did_change)
7406 {
7407 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7408 0L);
7409 curbuf->b_op_start = startpos;
7410 curbuf->b_op_end = curwin->w_cursor;
7411 if (curbuf->b_op_end.col > 0)
7412 --curbuf->b_op_end.col;
7413 }
7414}
7415
7416/*
7417 * Move cursor to mark.
7418 */
7419 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007420nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421{
7422 if (check_mark(pos) == FAIL)
7423 clearop(cap->oap);
7424 else
7425 {
7426 if (cap->cmdchar == '\''
7427 || cap->cmdchar == '`'
7428 || cap->cmdchar == '['
7429 || cap->cmdchar == ']')
7430 setpcmark();
7431 curwin->w_cursor = *pos;
7432 if (flag)
7433 beginline(BL_WHITE | BL_FIX);
7434 else
7435 check_cursor();
7436 }
7437 cap->oap->motion_type = flag ? MLINE : MCHAR;
7438 if (cap->cmdchar == '`')
7439 cap->oap->use_reg_one = TRUE;
7440 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7441 curwin->w_set_curswant = TRUE;
7442}
7443
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444/*
7445 * Handle commands that are operators in Visual mode.
7446 */
7447 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007448v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449{
7450 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7451
7452 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007453 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 if (isupper(cap->cmdchar))
7455 {
7456 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007457 {
7458 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7462 curwin->w_curswant = MAXCOL;
7463 }
7464 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7465 nv_operator(cap);
7466}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467
7468/*
7469 * "s" and "S" commands.
7470 */
7471 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007472nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7475 {
7476 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007477 {
7478 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 cap->cmdchar = 'c';
7482 nv_operator(cap);
7483 }
7484 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 nv_optrans(cap);
7486}
7487
7488/*
7489 * Abbreviated commands.
7490 */
7491 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007492nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493{
7494 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7495 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7496
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 /* in Visual mode these commands are operators */
7498 if (VIsual_active)
7499 v_visop(cap);
7500 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 nv_optrans(cap);
7502}
7503
7504/*
7505 * Translate a command into another command.
7506 */
7507 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007508nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509{
7510 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7511 (char_u *)"d$", (char_u *)"c$",
7512 (char_u *)"cl", (char_u *)"cc",
7513 (char_u *)"yy", (char_u *)":s\r"};
7514 static char_u *str = (char_u *)"xXDCsSY&";
7515
7516 if (!checkclearopq(cap->oap))
7517 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007518 /* In Vi "2D" doesn't delete the next line. Can't translate it
7519 * either, because "2." should also not use the count. */
7520 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7521 {
7522 cap->oap->start = curwin->w_cursor;
7523 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007524#ifdef FEAT_EVAL
7525 set_op_var(OP_DELETE);
7526#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007527 cap->count1 = 1;
7528 nv_dollar(cap);
7529 finish_op = TRUE;
7530 ResetRedobuff();
7531 AppendCharToRedobuff('D');
7532 }
7533 else
7534 {
7535 if (cap->count0)
7536 stuffnumReadbuff(cap->count0);
7537 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 }
7540 cap->opcount = 0;
7541}
7542
7543/*
7544 * "'" and "`" commands. Also for "g'" and "g`".
7545 * cap->arg is TRUE for "'" and "g'".
7546 */
7547 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007548nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549{
7550 pos_T *pos;
7551 int c;
7552#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007553 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7555#endif
7556
7557 if (cap->cmdchar == 'g')
7558 c = cap->extra_char;
7559 else
7560 c = cap->nchar;
7561 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7562 if (pos == (pos_T *)-1) /* jumped to other file */
7563 {
7564 if (cap->arg)
7565 {
7566 check_cursor_lnum();
7567 beginline(BL_WHITE | BL_FIX);
7568 }
7569 else
7570 check_cursor();
7571 }
7572 else
7573 nv_cursormark(cap, cap->arg, pos);
7574
7575#ifdef FEAT_VIRTUALEDIT
7576 /* May need to clear the coladd that a mark includes. */
7577 if (!virtual_active())
7578 curwin->w_cursor.coladd = 0;
7579#endif
7580#ifdef FEAT_FOLDING
7581 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007582 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007583 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 && (fdo_flags & FDO_MARK)
7585 && old_KeyTyped)
7586 foldOpenCursor();
7587#endif
7588}
7589
7590/*
7591 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7592 */
7593 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007594nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595{
7596#ifdef FEAT_JUMPLIST
7597 pos_T *pos;
7598# ifdef FEAT_FOLDING
7599 linenr_T lnum = curwin->w_cursor.lnum;
7600 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7601# endif
7602
7603 if (!checkclearopq(cap->oap))
7604 {
7605 if (cap->cmdchar == 'g')
7606 pos = movechangelist((int)cap->count1);
7607 else
7608 pos = movemark((int)cap->count1);
7609 if (pos == (pos_T *)-1) /* jump to other file */
7610 {
7611 curwin->w_set_curswant = TRUE;
7612 check_cursor();
7613 }
7614 else if (pos != NULL) /* can jump */
7615 nv_cursormark(cap, FALSE, pos);
7616 else if (cap->cmdchar == 'g')
7617 {
7618 if (curbuf->b_changelistlen == 0)
7619 EMSG(_("E664: changelist is empty"));
7620 else if (cap->count1 < 0)
7621 EMSG(_("E662: At start of changelist"));
7622 else
7623 EMSG(_("E663: At end of changelist"));
7624 }
7625 else
7626 clearopbeep(cap->oap);
7627# ifdef FEAT_FOLDING
7628 if (cap->oap->op_type == OP_NOP
7629 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7630 && (fdo_flags & FDO_MARK)
7631 && old_KeyTyped)
7632 foldOpenCursor();
7633# endif
7634 }
7635#else
7636 clearopbeep(cap->oap);
7637#endif
7638}
7639
7640/*
7641 * Handle '"' command.
7642 */
7643 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007644nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645{
7646 if (checkclearop(cap->oap))
7647 return;
7648#ifdef FEAT_EVAL
7649 if (cap->nchar == '=')
7650 cap->nchar = get_expr_register();
7651#endif
7652 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7653 {
7654 cap->oap->regname = cap->nchar;
7655 cap->opcount = cap->count0; /* remember count before '"' */
7656#ifdef FEAT_EVAL
7657 set_reg_var(cap->oap->regname);
7658#endif
7659 }
7660 else
7661 clearopbeep(cap->oap);
7662}
7663
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664/*
7665 * Handle "v", "V" and "CTRL-V" commands.
7666 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7667 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007668 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 */
7670 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007671nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007673 if (cap->cmdchar == Ctrl_Q)
7674 cap->cmdchar = Ctrl_V;
7675
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7677 * characterwise, linewise, or blockwise. */
7678 if (cap->oap->op_type != OP_NOP)
7679 {
7680 cap->oap->motion_force = cap->cmdchar;
7681 finish_op = FALSE; /* operator doesn't finish now but later */
7682 return;
7683 }
7684
7685 VIsual_select = cap->arg;
7686 if (VIsual_active) /* change Visual mode */
7687 {
7688 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7689 end_visual_mode();
7690 else /* toggle char/block mode */
7691 { /* or char/line mode */
7692 VIsual_mode = cap->cmdchar;
7693 showmode();
7694 }
7695 redraw_curbuf_later(INVERTED); /* update the inversion */
7696 }
7697 else /* start Visual mode */
7698 {
7699 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007700 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007702 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 VIsual = curwin->w_cursor;
7704
7705 VIsual_active = TRUE;
7706 VIsual_reselect = TRUE;
7707 if (!cap->arg)
7708 /* start Select mode when 'selectmode' contains "cmd" */
7709 may_start_select('c');
7710#ifdef FEAT_MOUSE
7711 setmouse();
7712#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007713 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 redraw_cmdline = TRUE; /* show visual mode later */
7715 /*
7716 * For V and ^V, we multiply the number of lines even if there
7717 * was only one -- webb
7718 */
7719 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7720 {
7721 curwin->w_cursor.lnum +=
7722 resel_VIsual_line_count * cap->count0 - 1;
7723 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7724 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7725 }
7726 VIsual_mode = resel_VIsual_mode;
7727 if (VIsual_mode == 'v')
7728 {
7729 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007730 {
7731 validate_virtcol();
7732 curwin->w_curswant = curwin->w_virtcol
7733 + resel_VIsual_vcol * cap->count0 - 1;
7734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007736 curwin->w_curswant = resel_VIsual_vcol;
7737 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007739 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 {
7741 curwin->w_curswant = MAXCOL;
7742 coladvance((colnr_T)MAXCOL);
7743 }
7744 else if (VIsual_mode == Ctrl_V)
7745 {
7746 validate_virtcol();
7747 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007748 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 coladvance(curwin->w_curswant);
7750 }
7751 else
7752 curwin->w_set_curswant = TRUE;
7753 redraw_curbuf_later(INVERTED); /* show the inversion */
7754 }
7755 else
7756 {
7757 if (!cap->arg)
7758 /* start Select mode when 'selectmode' contains "cmd" */
7759 may_start_select('c');
7760 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007761 if (VIsual_mode != 'V' && *p_sel == 'e')
7762 ++cap->count1; /* include one more char */
7763 if (cap->count0 > 0 && --cap->count1 > 0)
7764 {
7765 /* With a count select that many characters or lines. */
7766 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7767 nv_right(cap);
7768 else if (VIsual_mode == 'V')
7769 nv_down(cap);
7770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 }
7772 }
7773}
7774
7775/*
7776 * Start selection for Shift-movement keys.
7777 */
7778 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007779start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780{
7781 /* if 'selectmode' contains "key", start Select mode */
7782 may_start_select('k');
7783 n_start_visual_mode('v');
7784}
7785
7786/*
7787 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7788 */
7789 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007790may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791{
7792 VIsual_select = (stuff_empty() && typebuf_typed()
7793 && (vim_strchr(p_slm, c) != NULL));
7794}
7795
7796/*
7797 * Start Visual mode "c".
7798 * Should set VIsual_select before calling this.
7799 */
7800 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007801n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007803#ifdef FEAT_CONCEAL
7804 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007805 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007806#endif
7807
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 VIsual_mode = c;
7809 VIsual_active = TRUE;
7810 VIsual_reselect = TRUE;
7811#ifdef FEAT_VIRTUALEDIT
7812 /* Corner case: the 0 position in a tab may change when going into
7813 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7814 */
7815 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007816 {
7817 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820#endif
7821 VIsual = curwin->w_cursor;
7822
7823#ifdef FEAT_FOLDING
7824 foldAdjustVisual();
7825#endif
7826
7827#ifdef FEAT_MOUSE
7828 setmouse();
7829#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007830#ifdef FEAT_CONCEAL
7831 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007832 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007833#endif
7834
Bram Moolenaar09df3122006-01-23 22:23:09 +00007835 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 redraw_cmdline = TRUE; /* show visual mode later */
7837#ifdef FEAT_CLIPBOARD
7838 /* Make sure the clipboard gets updated. Needed because start and
7839 * end may still be the same, and the selection needs to be owned */
7840 clip_star.vmode = NUL;
7841#endif
7842
7843 /* Only need to redraw this line, unless still need to redraw an old
7844 * Visual area (when 'lazyredraw' is set). */
7845 if (curwin->w_redr_type < INVERTED)
7846 {
7847 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7848 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7849 }
7850}
7851
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852
7853/*
7854 * CTRL-W: Window commands
7855 */
7856 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007857nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858{
7859#ifdef FEAT_WINDOWS
Bram Moolenaar938783d2017-07-16 20:13:26 +02007860 if (cap->nchar == ':')
7861 /* "CTRL-W :" is the same as typing ":"; useful in a terminal window */
7862 nv_colon(cap);
7863 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7865#else
7866 (void)checkclearop(cap->oap);
7867#endif
7868}
7869
7870/*
7871 * CTRL-Z: Suspend
7872 */
7873 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007874nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875{
7876 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 if (VIsual_active)
7878 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 do_cmdline_cmd((char_u *)"st");
7880}
7881
7882/*
7883 * Commands starting with "g".
7884 */
7885 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007886nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887{
7888 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 int i;
7891 int flag = FALSE;
7892
7893 switch (cap->nchar)
7894 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007895 case Ctrl_A:
7896 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897#ifdef MEM_PROFILE
7898 /*
7899 * "g^A": dump log of used memory.
7900 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007901 if (!VIsual_active && cap->nchar == Ctrl_A)
7902 vim_mem_profile_dump();
7903 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007905 /*
7906 * "g^A/g^X": sequentially increment visually selected region
7907 */
7908 if (VIsual_active)
7909 {
7910 cap->arg = TRUE;
7911 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01007912 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007913 nv_addsub(cap);
7914 }
7915 else
7916 clearopbeep(oap);
7917 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918
7919#ifdef FEAT_VREPLACE
7920 /*
7921 * "gR": Enter virtual replace mode.
7922 */
7923 case 'R':
7924 cap->arg = TRUE;
7925 nv_Replace(cap);
7926 break;
7927
7928 case 'r':
7929 nv_vreplace(cap);
7930 break;
7931#endif
7932
7933 case '&':
7934 do_cmdline_cmd((char_u *)"%s//~/&");
7935 break;
7936
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 /*
7938 * "gv": Reselect the previous Visual area. If Visual already active,
7939 * exchange previous and current Visual area.
7940 */
7941 case 'v':
7942 if (checkclearop(oap))
7943 break;
7944
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007945 if ( curbuf->b_visual.vi_start.lnum == 0
7946 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7947 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 beep_flush();
7949 else
7950 {
7951 /* set w_cursor to the start of the Visual area, tpos to the end */
7952 if (VIsual_active)
7953 {
7954 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007955 VIsual_mode = curbuf->b_visual.vi_mode;
7956 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957# ifdef FEAT_EVAL
7958 curbuf->b_visual_mode_eval = i;
7959# endif
7960 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007961 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7962 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007964 tpos = curbuf->b_visual.vi_end;
7965 curbuf->b_visual.vi_end = curwin->w_cursor;
7966 curwin->w_cursor = curbuf->b_visual.vi_start;
7967 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 }
7969 else
7970 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007971 VIsual_mode = curbuf->b_visual.vi_mode;
7972 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7973 tpos = curbuf->b_visual.vi_end;
7974 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 }
7976
7977 VIsual_active = TRUE;
7978 VIsual_reselect = TRUE;
7979
7980 /* Set Visual to the start and w_cursor to the end of the Visual
7981 * area. Make sure they are on an existing character. */
7982 check_cursor();
7983 VIsual = curwin->w_cursor;
7984 curwin->w_cursor = tpos;
7985 check_cursor();
7986 update_topline();
7987 /*
7988 * When called from normal "g" command: start Select mode when
7989 * 'selectmode' contains "cmd". When called for K_SELECT, always
7990 * start Select mode.
7991 */
7992 if (cap->arg)
7993 VIsual_select = TRUE;
7994 else
7995 may_start_select('c');
7996#ifdef FEAT_MOUSE
7997 setmouse();
7998#endif
7999#ifdef FEAT_CLIPBOARD
8000 /* Make sure the clipboard gets updated. Needed because start and
8001 * end are still the same, and the selection needs to be owned */
8002 clip_star.vmode = NUL;
8003#endif
8004 redraw_curbuf_later(INVERTED);
8005 showmode();
8006 }
8007 break;
8008 /*
8009 * "gV": Don't reselect the previous Visual area after a Select mode
8010 * mapping of menu.
8011 */
8012 case 'V':
8013 VIsual_reselect = FALSE;
8014 break;
8015
8016 /*
8017 * "gh": start Select mode.
8018 * "gH": start Select line mode.
8019 * "g^H": start Select block mode.
8020 */
8021 case K_BS:
8022 cap->nchar = Ctrl_H;
8023 /* FALLTHROUGH */
8024 case 'h':
8025 case 'H':
8026 case Ctrl_H:
8027# ifdef EBCDIC
8028 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
8029 if (cap->nchar == Ctrl_H)
8030 cap->cmdchar = Ctrl_V;
8031 else
8032# endif
8033 cap->cmdchar = cap->nchar + ('v' - 'h');
8034 cap->arg = TRUE;
8035 nv_visual(cap);
8036 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02008037
8038 /* "gn", "gN" visually select next/previous search match
8039 * "gn" selects next match
8040 * "gN" selects previous match
8041 */
8042 case 'N':
8043 case 'n':
8044 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02008045 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02008046 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047
8048 /*
8049 * "gj" and "gk" two new funny movement keys -- up and down
8050 * movement based on *screen* line rather than *file* line.
8051 */
8052 case 'j':
8053 case K_DOWN:
8054 /* with 'nowrap' it works just like the normal "j" command; also when
8055 * in a closed fold */
8056 if (!curwin->w_p_wrap
8057#ifdef FEAT_FOLDING
8058 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8059#endif
8060 )
8061 {
8062 oap->motion_type = MLINE;
8063 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8064 }
8065 else
8066 i = nv_screengo(oap, FORWARD, cap->count1);
8067 if (i == FAIL)
8068 clearopbeep(oap);
8069 break;
8070
8071 case 'k':
8072 case K_UP:
8073 /* with 'nowrap' it works just like the normal "k" command; also when
8074 * in a closed fold */
8075 if (!curwin->w_p_wrap
8076#ifdef FEAT_FOLDING
8077 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8078#endif
8079 )
8080 {
8081 oap->motion_type = MLINE;
8082 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8083 }
8084 else
8085 i = nv_screengo(oap, BACKWARD, cap->count1);
8086 if (i == FAIL)
8087 clearopbeep(oap);
8088 break;
8089
8090 /*
8091 * "gJ": join two lines without inserting a space.
8092 */
8093 case 'J':
8094 nv_join(cap);
8095 break;
8096
8097 /*
8098 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8099 * "gm": middle of "g0" and "g$".
8100 */
8101 case '^':
8102 flag = TRUE;
8103 /* FALLTHROUGH */
8104
8105 case '0':
8106 case 'm':
8107 case K_HOME:
8108 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 oap->motion_type = MCHAR;
8110 oap->inclusive = FALSE;
8111 if (curwin->w_p_wrap
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008112#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 && curwin->w_width != 0
8114#endif
8115 )
8116 {
8117 int width1 = W_WIDTH(curwin) - curwin_col_off();
8118 int width2 = width1 + curwin_col_off2();
8119
8120 validate_virtcol();
8121 i = 0;
8122 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8123 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8124 }
8125 else
8126 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008127 /* Go to the middle of the screen line. When 'number' or
8128 * 'relativenumber' is on and lines are wrapping the middle can be more
8129 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 if (cap->nchar == 'm')
8131 i += (W_WIDTH(curwin) - curwin_col_off()
8132 + ((curwin->w_p_wrap && i > 0)
8133 ? curwin_col_off2() : 0)) / 2;
8134 coladvance((colnr_T)i);
8135 if (flag)
8136 {
8137 do
8138 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01008139 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 }
8141 curwin->w_set_curswant = TRUE;
8142 break;
8143
8144 case '_':
8145 /* "g_": to the last non-blank character in the line or <count> lines
8146 * downward. */
8147 cap->oap->motion_type = MCHAR;
8148 cap->oap->inclusive = TRUE;
8149 curwin->w_curswant = MAXCOL;
8150 if (cursor_down((long)(cap->count1 - 1),
8151 cap->oap->op_type == OP_NOP) == FAIL)
8152 clearopbeep(cap->oap);
8153 else
8154 {
8155 char_u *ptr = ml_get_curline();
8156
8157 /* In Visual mode we may end up after the line. */
8158 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8159 --curwin->w_cursor.col;
8160
8161 /* Decrease the cursor column until it's on a non-blank. */
8162 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01008163 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 --curwin->w_cursor.col;
8165 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008166 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 }
8168 break;
8169
8170 case '$':
8171 case K_END:
8172 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {
8174 int col_off = curwin_col_off();
8175
8176 oap->motion_type = MCHAR;
8177 oap->inclusive = TRUE;
8178 if (curwin->w_p_wrap
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008179#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 && curwin->w_width != 0
8181#endif
8182 )
8183 {
8184 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8185 if (cap->count1 == 1)
8186 {
8187 int width1 = W_WIDTH(curwin) - col_off;
8188 int width2 = width1 + curwin_col_off2();
8189
8190 validate_virtcol();
8191 i = width1 - 1;
8192 if (curwin->w_virtcol >= (colnr_T)width1)
8193 i += ((curwin->w_virtcol - width1) / width2 + 1)
8194 * width2;
8195 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008196
8197 /* Make sure we stick in this column. */
8198 validate_virtcol();
8199 curwin->w_curswant = curwin->w_virtcol;
8200 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8202 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8203 {
8204 /*
8205 * Check for landing on a character that got split at
8206 * the end of the line. We do not want to advance to
8207 * the next screen line.
8208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 if (curwin->w_virtcol > (colnr_T)i)
8210 --curwin->w_cursor.col;
8211 }
8212#endif
8213 }
8214 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8215 clearopbeep(oap);
8216 }
8217 else
8218 {
8219 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8220 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008221
8222 /* Make sure we stick in this column. */
8223 validate_virtcol();
8224 curwin->w_curswant = curwin->w_virtcol;
8225 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 }
8227 }
8228 break;
8229
8230 /*
8231 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8232 */
8233 case '*':
8234 case '#':
8235#if POUND != '#'
8236 case POUND: /* pound sign (sometimes equal to '#') */
8237#endif
8238 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8239 case ']': /* :tselect for current identifier */
8240 nv_ident(cap);
8241 break;
8242
8243 /*
8244 * ge and gE: go back to end of word
8245 */
8246 case 'e':
8247 case 'E':
8248 oap->motion_type = MCHAR;
8249 curwin->w_set_curswant = TRUE;
8250 oap->inclusive = TRUE;
8251 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8252 clearopbeep(oap);
8253 break;
8254
8255 /*
8256 * "g CTRL-G": display info about cursor position
8257 */
8258 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01008259 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 break;
8261
8262 /*
8263 * "gi": start Insert at the last position.
8264 */
8265 case 'i':
8266 if (curbuf->b_last_insert.lnum != 0)
8267 {
8268 curwin->w_cursor = curbuf->b_last_insert;
8269 check_cursor_lnum();
8270 i = (int)STRLEN(ml_get_curline());
8271 if (curwin->w_cursor.col > (colnr_T)i)
8272 {
8273#ifdef FEAT_VIRTUALEDIT
8274 if (virtual_active())
8275 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8276#endif
8277 curwin->w_cursor.col = i;
8278 }
8279 }
8280 cap->cmdchar = 'i';
8281 nv_edit(cap);
8282 break;
8283
8284 /*
8285 * "gI": Start insert in column 1.
8286 */
8287 case 'I':
8288 beginline(0);
8289 if (!checkclearopq(oap))
8290 invoke_edit(cap, FALSE, 'g', FALSE);
8291 break;
8292
8293#ifdef FEAT_SEARCHPATH
8294 /*
8295 * "gf": goto file, edit file under cursor
8296 * "]f" and "[f": can also be used.
8297 */
8298 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008299 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 nv_gotofile(cap);
8301 break;
8302#endif
8303
8304 /* "g'm" and "g`m": jump to mark without setting pcmark */
8305 case '\'':
8306 cap->arg = TRUE;
8307 /*FALLTHROUGH*/
8308 case '`':
8309 nv_gomark(cap);
8310 break;
8311
8312 /*
8313 * "gs": Goto sleep.
8314 */
8315 case 's':
8316 do_sleep(cap->count1 * 1000L);
8317 break;
8318
8319 /*
8320 * "ga": Display the ascii value of the character under the
8321 * cursor. It is displayed in decimal, hex, and octal. -- webb
8322 */
8323 case 'a':
8324 do_ascii(NULL);
8325 break;
8326
8327#ifdef FEAT_MBYTE
8328 /*
8329 * "g8": Display the bytes used for the UTF-8 character under the
8330 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008331 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 */
8333 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008334 if (cap->count0 == 8)
8335 utf_find_illegal();
8336 else
8337 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 break;
8339#endif
8340
Bram Moolenaar60402d62017-04-20 18:54:50 +02008341 /* "g<": show scrollback text */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008342 case '<':
8343 show_sb_text();
8344 break;
8345
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 /*
8347 * "gg": Goto the first line in file. With a count it goes to
8348 * that line number like for "G". -- webb
8349 */
8350 case 'g':
8351 cap->arg = FALSE;
8352 nv_goto(cap);
8353 break;
8354
8355 /*
8356 * Two-character operators:
8357 * "gq" Format text
8358 * "gw" Format text and keep cursor position
8359 * "g~" Toggle the case of the text.
8360 * "gu" Change text to lower case.
8361 * "gU" Change text to upper case.
8362 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008363 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 */
8365 case 'q':
8366 case 'w':
8367 oap->cursor_start = curwin->w_cursor;
8368 /*FALLTHROUGH*/
8369 case '~':
8370 case 'u':
8371 case 'U':
8372 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008373 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 nv_operator(cap);
8375 break;
8376
8377 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008378 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 * current function
8380 * "gD": idem, but in the current file.
8381 */
8382 case 'd':
8383 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008384 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 break;
8386
8387#ifdef FEAT_MOUSE
8388 /*
8389 * g<*Mouse> : <C-*mouse>
8390 */
8391 case K_MIDDLEMOUSE:
8392 case K_MIDDLEDRAG:
8393 case K_MIDDLERELEASE:
8394 case K_LEFTMOUSE:
8395 case K_LEFTDRAG:
8396 case K_LEFTRELEASE:
8397 case K_RIGHTMOUSE:
8398 case K_RIGHTDRAG:
8399 case K_RIGHTRELEASE:
8400 case K_X1MOUSE:
8401 case K_X1DRAG:
8402 case K_X1RELEASE:
8403 case K_X2MOUSE:
8404 case K_X2DRAG:
8405 case K_X2RELEASE:
8406 mod_mask = MOD_MASK_CTRL;
8407 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8408 break;
8409#endif
8410
8411 case K_IGNORE:
8412 break;
8413
8414 /*
8415 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8416 */
8417 case 'p':
8418 case 'P':
8419 nv_put(cap);
8420 break;
8421
8422#ifdef FEAT_BYTEOFF
8423 /* "go": goto byte count from start of buffer */
8424 case 'o':
8425 goto_byte(cap->count0);
8426 break;
8427#endif
8428
8429 /* "gQ": improved Ex mode */
8430 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008431 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 {
8433 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008434 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 break;
8436 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008437
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 if (!checkclearopq(oap))
8439 do_exmode(TRUE);
8440 break;
8441
8442#ifdef FEAT_JUMPLIST
8443 case ',':
8444 nv_pcmark(cap);
8445 break;
8446
8447 case ';':
8448 cap->count1 = -cap->count1;
8449 nv_pcmark(cap);
8450 break;
8451#endif
8452
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008453#ifdef FEAT_WINDOWS
8454 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008455 if (!checkclearop(oap))
8456 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008457 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008458 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008459 if (!checkclearop(oap))
8460 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008461 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008462#endif
8463
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008464 case '+':
8465 case '-': /* "g+" and "g-": undo or redo along the timeline */
8466 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008467 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008468 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008469 break;
8470
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 default:
8472 clearopbeep(oap);
8473 break;
8474 }
8475}
8476
8477/*
8478 * Handle "o" and "O" commands.
8479 */
8480 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008481n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008483#ifdef FEAT_CONCEAL
8484 linenr_T oldline = curwin->w_cursor.lnum;
8485#endif
8486
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 if (!checkclearopq(cap->oap))
8488 {
8489#ifdef FEAT_FOLDING
8490 if (cap->cmdchar == 'O')
8491 /* Open above the first line of a folded sequence of lines */
8492 (void)hasFolding(curwin->w_cursor.lnum,
8493 &curwin->w_cursor.lnum, NULL);
8494 else
8495 /* Open below the last line of a folded sequence of lines */
8496 (void)hasFolding(curwin->w_cursor.lnum,
8497 NULL, &curwin->w_cursor.lnum);
8498#endif
8499 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8500 (cap->cmdchar == 'O' ? 1 : 0)),
8501 (linenr_T)(curwin->w_cursor.lnum +
8502 (cap->cmdchar == 'o' ? 1 : 0))
8503 ) == OK
8504 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8505#ifdef FEAT_COMMENTS
8506 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8507#endif
8508 0, 0))
8509 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008510#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008511 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008512 update_single_line(curwin, oldline);
8513#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008514 /* When '#' is in 'cpoptions' ignore the count. */
8515 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8516 cap->count1 = 1;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008517#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02008518 if (curwin->w_p_cul)
8519 /* force redraw of cursorline */
8520 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8523 }
8524 }
8525}
8526
8527/*
8528 * "." command: redo last change.
8529 */
8530 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008531nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532{
8533 if (!checkclearopq(cap->oap))
8534 {
8535 /*
8536 * If "restart_edit" is TRUE, the last but one command is repeated
8537 * instead of the last command (inserting text). This is used for
8538 * CTRL-O <.> in insert mode.
8539 */
8540 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8541 clearopbeep(cap->oap);
8542 }
8543}
8544
8545/*
8546 * CTRL-R: undo undo
8547 */
8548 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008549nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550{
8551 if (!checkclearopq(cap->oap))
8552 {
8553 u_redo((int)cap->count1);
8554 curwin->w_set_curswant = TRUE;
8555 }
8556}
8557
8558/*
8559 * Handle "U" command.
8560 */
8561 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008562nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563{
8564 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008565 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 {
8567 /* translate "gUU" to "gUgU" */
8568 cap->cmdchar = 'g';
8569 cap->nchar = 'U';
8570 nv_operator(cap);
8571 }
8572 else if (!checkclearopq(cap->oap))
8573 {
8574 u_undoline();
8575 curwin->w_set_curswant = TRUE;
8576 }
8577}
8578
8579/*
8580 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8581 * single character.
8582 */
8583 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008584nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008586 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 n_swapchar(cap);
8588 else
8589 nv_operator(cap);
8590}
8591
8592/*
8593 * Handle an operator command.
8594 * The actual work is done by do_pending_operator().
8595 */
8596 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008597nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598{
8599 int op_type;
8600
8601 op_type = get_op_type(cap->cmdchar, cap->nchar);
8602
8603 if (op_type == cap->oap->op_type) /* double operator works on lines */
8604 nv_lineop(cap);
8605 else if (!checkclearop(cap->oap))
8606 {
8607 cap->oap->start = curwin->w_cursor;
8608 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008609#ifdef FEAT_EVAL
8610 set_op_var(op_type);
8611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 }
8613}
8614
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008615#ifdef FEAT_EVAL
8616/*
8617 * Set v:operator to the characters for "optype".
8618 */
8619 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008620set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008621{
8622 char_u opchars[3];
8623
8624 if (optype == OP_NOP)
8625 set_vim_var_string(VV_OP, NULL, 0);
8626 else
8627 {
8628 opchars[0] = get_op_char(optype);
8629 opchars[1] = get_extra_op_char(optype);
8630 opchars[2] = NUL;
8631 set_vim_var_string(VV_OP, opchars, -1);
8632 }
8633}
8634#endif
8635
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636/*
8637 * Handle linewise operator "dd", "yy", etc.
8638 *
8639 * "_" is is a strange motion command that helps make operators more logical.
8640 * It is actually implemented, but not documented in the real Vi. This motion
8641 * command actually refers to "the current line". Commands like "dd" and "yy"
8642 * are really an alternate form of "d_" and "y_". It does accept a count, so
8643 * "d3_" works to delete 3 lines.
8644 */
8645 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008646nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647{
8648 cap->oap->motion_type = MLINE;
8649 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8650 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008651 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8652 && cap->oap->motion_force != 'v'
8653 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 || cap->oap->op_type == OP_LSHIFT
8655 || cap->oap->op_type == OP_RSHIFT)
8656 beginline(BL_SOL | BL_FIX);
8657 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8658 beginline(BL_WHITE | BL_FIX);
8659}
8660
8661/*
8662 * <Home> command.
8663 */
8664 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008665nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008667 /* CTRL-HOME is like "gg" */
8668 if (mod_mask & MOD_MASK_CTRL)
8669 nv_goto(cap);
8670 else
8671 {
8672 cap->count0 = 1;
8673 nv_pipe(cap);
8674 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008675 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8676 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677}
8678
8679/*
8680 * "|" command.
8681 */
8682 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008683nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684{
8685 cap->oap->motion_type = MCHAR;
8686 cap->oap->inclusive = FALSE;
8687 beginline(0);
8688 if (cap->count0 > 0)
8689 {
8690 coladvance((colnr_T)(cap->count0 - 1));
8691 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8692 }
8693 else
8694 curwin->w_curswant = 0;
8695 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008696 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 curwin->w_set_curswant = FALSE;
8698}
8699
8700/*
8701 * Handle back-word command "b" and "B".
8702 * cap->arg is 1 for "B"
8703 */
8704 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008705nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706{
8707 cap->oap->motion_type = MCHAR;
8708 cap->oap->inclusive = FALSE;
8709 curwin->w_set_curswant = TRUE;
8710 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8711 clearopbeep(cap->oap);
8712#ifdef FEAT_FOLDING
8713 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8714 foldOpenCursor();
8715#endif
8716}
8717
8718/*
8719 * Handle word motion commands "e", "E", "w" and "W".
8720 * cap->arg is TRUE for "E" and "W".
8721 */
8722 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008723nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724{
8725 int n;
8726 int word_end;
8727 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008728 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729
8730 /*
8731 * Set inclusive for the "E" and "e" command.
8732 */
8733 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8734 word_end = TRUE;
8735 else
8736 word_end = FALSE;
8737 cap->oap->inclusive = word_end;
8738
8739 /*
8740 * "cw" and "cW" are a special case.
8741 */
8742 if (!word_end && cap->oap->op_type == OP_CHANGE)
8743 {
8744 n = gchar_cursor();
8745 if (n != NUL) /* not an empty line */
8746 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008747 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 {
8749 /*
8750 * Reproduce a funny Vi behaviour: "cw" on a blank only
8751 * changes one character, not all blanks until the start of
8752 * the next word. Only do this when the 'w' flag is included
8753 * in 'cpoptions'.
8754 */
8755 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8756 {
8757 cap->oap->inclusive = TRUE;
8758 cap->oap->motion_type = MCHAR;
8759 return;
8760 }
8761 }
8762 else
8763 {
8764 /*
8765 * This is a little strange. To match what the real Vi does,
8766 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8767 * that we are not on a space or a TAB. This seems impolite
8768 * at first, but it's really more what we mean when we say
8769 * 'cw'.
8770 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008771 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 * will change only one character! This is done by setting
8773 * flag.
8774 */
8775 cap->oap->inclusive = TRUE;
8776 word_end = TRUE;
8777 flag = TRUE;
8778 }
8779 }
8780 }
8781
8782 cap->oap->motion_type = MCHAR;
8783 curwin->w_set_curswant = TRUE;
8784 if (word_end)
8785 n = end_word(cap->count1, cap->arg, flag, FALSE);
8786 else
8787 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8788
Bram Moolenaardfefb982008-04-01 10:06:39 +00008789 /* Don't leave the cursor on the NUL past the end of line. Unless we
8790 * didn't move it forward. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008791 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008792 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793
8794 if (n == FAIL && cap->oap->op_type == OP_NOP)
8795 clearopbeep(cap->oap);
8796 else
8797 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799#ifdef FEAT_FOLDING
8800 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8801 foldOpenCursor();
8802#endif
8803 }
8804}
8805
8806/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008807 * Used after a movement command: If the cursor ends up on the NUL after the
8808 * end of the line, may move it back to the last character and make the motion
8809 * inclusive.
8810 */
8811 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008812adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008813{
8814 /* The cursor cannot remain on the NUL when:
8815 * - the column is > 0
8816 * - not in Visual mode or 'selection' is "o"
8817 * - 'virtualedit' is not "all" and not "onemore".
8818 */
8819 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008820 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008821#ifdef FEAT_VIRTUALEDIT
8822 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8823#endif
8824 )
8825 {
8826 --curwin->w_cursor.col;
8827#ifdef FEAT_MBYTE
8828 /* prevent cursor from moving on the trail byte */
8829 if (has_mbyte)
8830 mb_adjust_cursor();
8831#endif
8832 oap->inclusive = TRUE;
8833 }
8834}
8835
8836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 * "0" and "^" commands.
8838 * cap->arg is the argument for beginline().
8839 */
8840 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008841nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842{
8843 cap->oap->motion_type = MCHAR;
8844 cap->oap->inclusive = FALSE;
8845 beginline(cap->arg);
8846#ifdef FEAT_FOLDING
8847 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8848 foldOpenCursor();
8849#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008850 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8851 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852}
8853
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854/*
8855 * In exclusive Visual mode, may include the last character.
8856 */
8857 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008858adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859{
8860 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008861 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008863#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 if (has_mbyte)
8865 inc_cursor();
8866 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 ++curwin->w_cursor.col;
8869 cap->oap->inclusive = FALSE;
8870 }
8871}
8872
8873/*
8874 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8875 * Should check VIsual_mode before calling this.
8876 * Returns TRUE when backed up to the previous line.
8877 */
8878 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008879unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880{
8881 pos_T *pp;
8882
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008883 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008885 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 pp = &curwin->w_cursor;
8887 else
8888 pp = &VIsual;
8889#ifdef FEAT_VIRTUALEDIT
8890 if (pp->coladd > 0)
8891 --pp->coladd;
8892 else
8893#endif
8894 if (pp->col > 0)
8895 {
8896 --pp->col;
8897#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008898 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899#endif
8900 }
8901 else if (pp->lnum > 1)
8902 {
8903 --pp->lnum;
8904 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8905 return TRUE;
8906 }
8907 }
8908 return FALSE;
8909}
8910
8911/*
8912 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8913 */
8914 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008915nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916{
8917 if (VIsual_active)
8918 VIsual_select = TRUE;
8919 else if (VIsual_reselect)
8920 {
8921 cap->nchar = 'v'; /* fake "gv" command */
8922 cap->arg = TRUE;
8923 nv_g_cmd(cap);
8924 }
8925}
8926
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927
8928/*
8929 * "G", "gg", CTRL-END, CTRL-HOME.
8930 * cap->arg is TRUE for "G".
8931 */
8932 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008933nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934{
8935 linenr_T lnum;
8936
8937 if (cap->arg)
8938 lnum = curbuf->b_ml.ml_line_count;
8939 else
8940 lnum = 1L;
8941 cap->oap->motion_type = MLINE;
8942 setpcmark();
8943
8944 /* When a count is given, use it instead of the default lnum */
8945 if (cap->count0 != 0)
8946 lnum = cap->count0;
8947 if (lnum < 1L)
8948 lnum = 1L;
8949 else if (lnum > curbuf->b_ml.ml_line_count)
8950 lnum = curbuf->b_ml.ml_line_count;
8951 curwin->w_cursor.lnum = lnum;
8952 beginline(BL_SOL | BL_FIX);
8953#ifdef FEAT_FOLDING
8954 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8955 foldOpenCursor();
8956#endif
8957}
8958
8959/*
8960 * CTRL-\ in Normal mode.
8961 */
8962 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008963nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964{
8965 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8966 {
8967 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008968 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 clear_cmdline = TRUE; /* unshow mode later */
8970 restart_edit = 0;
8971#ifdef FEAT_CMDWIN
8972 if (cmdwin_type != 0)
8973 cmdwin_result = Ctrl_C;
8974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 if (VIsual_active)
8976 {
8977 end_visual_mode(); /* stop Visual */
8978 redraw_curbuf_later(INVERTED);
8979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8981 if (cap->nchar == Ctrl_G && p_im)
8982 restart_edit = 'a';
8983 }
8984 else
8985 clearopbeep(cap->oap);
8986}
8987
8988/*
8989 * ESC in Normal mode: beep, but don't flush buffers.
8990 * Don't even beep if we are canceling a command.
8991 */
8992 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008993nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994{
8995 int no_reason;
8996
8997 no_reason = (cap->oap->op_type == OP_NOP
8998 && cap->opcount == 0
8999 && cap->count0 == 0
9000 && cap->oap->regname == 0
9001 && !p_im);
9002
9003 if (cap->arg) /* TRUE for CTRL-C */
9004 {
9005 if (restart_edit == 0
9006#ifdef FEAT_CMDWIN
9007 && cmdwin_type == 0
9008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 && no_reason)
Bram Moolenaar28a81932017-06-04 15:33:48 +02009011 MSG(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012
9013 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
9014 * set again below when halfway a mapping. */
9015 if (!p_im)
9016 restart_edit = 0;
9017#ifdef FEAT_CMDWIN
9018 if (cmdwin_type != 0)
9019 {
9020 cmdwin_result = K_IGNORE;
9021 got_int = FALSE; /* don't stop executing autocommands et al. */
9022 return;
9023 }
9024#endif
9025 }
9026
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 if (VIsual_active)
9028 {
9029 end_visual_mode(); /* stop Visual */
9030 check_cursor_col(); /* make sure cursor is not beyond EOL */
9031 curwin->w_set_curswant = TRUE;
9032 redraw_curbuf_later(INVERTED);
9033 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009034 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009035 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 clearop(cap->oap);
9037
9038 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9039 * set return to Insert mode afterwards. */
Bram Moolenaare2c38102016-01-31 14:55:40 +01009040 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 restart_edit = 'a';
9042}
9043
9044/*
9045 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009046 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 */
9048 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009049nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050{
9051 /* <Insert> is equal to "i" */
9052 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9053 cap->cmdchar = 'i';
9054
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 /* in Visual mode "A" and "I" are an operator */
9056 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
9057 v_visop(cap);
9058
9059 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009060 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9061 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 {
9063#ifdef FEAT_TEXTOBJ
9064 nv_object(cap);
9065#else
9066 clearopbeep(cap->oap);
9067#endif
9068 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02009069#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02009070 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02009071 {
9072 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02009073 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02009074 return;
9075 }
9076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 else if (!curbuf->b_p_ma && !p_im)
9078 {
9079 /* Only give this error when 'insertmode' is off. */
9080 EMSG(_(e_modifiable));
9081 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009082 if (cap->cmdchar == K_PS)
9083 /* drop the pasted text */
9084 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 }
Bram Moolenaara1891842017-02-04 21:34:31 +01009086 else if (cap->cmdchar == K_PS && VIsual_active)
9087 {
9088 pos_T old_pos = curwin->w_cursor;
9089 pos_T old_visual = VIsual;
9090
9091 /* In Visual mode the selected text is deleted. */
9092 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
9093 {
9094 shift_delete_registers();
9095 cap->oap->regname = '1';
9096 }
9097 else
9098 cap->oap->regname = '-';
9099 cap->cmdchar = 'd';
9100 cap->nchar = NUL;
9101 nv_operator(cap);
9102 do_pending_operator(cap, 0, FALSE);
9103 cap->cmdchar = K_PS;
9104
9105 /* When the last char in the line was deleted then append. Detect this
9106 * by checking if the cursor moved to before the Visual area. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009107 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
9108 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01009109 inc_cursor();
9110
9111 /* Insert to replace the deleted text with the pasted text. */
9112 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 else if (!checkclearopq(cap->oap))
9115 {
9116 switch (cap->cmdchar)
9117 {
9118 case 'A': /* "A"ppend after the line */
9119 curwin->w_set_curswant = TRUE;
9120#ifdef FEAT_VIRTUALEDIT
9121 if (ve_flags == VE_ALL)
9122 {
9123 int save_State = State;
9124
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009125 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 * character past the end of the line */
9127 State = INSERT;
9128 coladvance((colnr_T)MAXCOL);
9129 State = save_State;
9130 }
9131 else
9132#endif
9133 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9134 break;
9135
9136 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009137 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9138 beginline(BL_WHITE);
9139 else
9140 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 break;
9142
Bram Moolenaara1891842017-02-04 21:34:31 +01009143 case K_PS:
9144 /* Bracketed paste works like "a"ppend, unless the cursor is in
9145 * the first column, then it inserts. */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009146 if (curwin->w_cursor.col == 0)
9147 break;
9148 /*FALLTHROUGH*/
9149
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9151#ifdef FEAT_VIRTUALEDIT
9152 /* increment coladd when in virtual space, increment the
9153 * column otherwise, also to append after an unprintable char */
9154 if (virtual_active()
9155 && (curwin->w_cursor.coladd > 0
9156 || *ml_get_cursor() == NUL
9157 || *ml_get_cursor() == TAB))
9158 curwin->w_cursor.coladd++;
9159 else
9160#endif
9161 if (*ml_get_cursor() != NUL)
9162 inc_cursor();
9163 break;
9164 }
9165
9166#ifdef FEAT_VIRTUALEDIT
9167 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9168 {
9169 int save_State = State;
9170
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009171 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 * character past the end of the line */
9173 State = INSERT;
9174 coladvance(getviscol());
9175 State = save_State;
9176 }
9177#endif
9178
9179 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9180 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009181 else if (cap->cmdchar == K_PS)
9182 /* drop the pasted text */
9183 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184}
9185
9186/*
9187 * Invoke edit() and take care of "restart_edit" and the return value.
9188 */
9189 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009190invoke_edit(
9191 cmdarg_T *cap,
9192 int repl, /* "r" or "gr" command */
9193 int cmd,
9194 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195{
9196 int restart_edit_save = 0;
9197
9198 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9199 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9200 * it. */
9201 if (repl || !stuff_empty())
9202 restart_edit_save = restart_edit;
9203 else
9204 restart_edit_save = 0;
9205
9206 /* Always reset "restart_edit", this is not a restarted edit. */
9207 restart_edit = 0;
9208
9209 if (edit(cmd, startln, cap->count1))
9210 cap->retval |= CA_COMMAND_BUSY;
9211
9212 if (restart_edit == 0)
9213 restart_edit = restart_edit_save;
9214}
9215
9216#ifdef FEAT_TEXTOBJ
9217/*
9218 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9219 */
9220 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009221nv_object(
9222 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223{
9224 int flag;
9225 int include;
9226 char_u *mps_save;
9227
9228 if (cap->cmdchar == 'i')
9229 include = FALSE; /* "ix" = inner object: exclude white space */
9230 else
9231 include = TRUE; /* "ax" = an object: include white space */
9232
9233 /* Make sure (), [], {} and <> are in 'matchpairs' */
9234 mps_save = curbuf->b_p_mps;
9235 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9236
9237 switch (cap->nchar)
9238 {
9239 case 'w': /* "aw" = a word */
9240 flag = current_word(cap->oap, cap->count1, include, FALSE);
9241 break;
9242 case 'W': /* "aW" = a WORD */
9243 flag = current_word(cap->oap, cap->count1, include, TRUE);
9244 break;
9245 case 'b': /* "ab" = a braces block */
9246 case '(':
9247 case ')':
9248 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9249 break;
9250 case 'B': /* "aB" = a Brackets block */
9251 case '{':
9252 case '}':
9253 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9254 break;
9255 case '[': /* "a[" = a [] block */
9256 case ']':
9257 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9258 break;
9259 case '<': /* "a<" = a <> block */
9260 case '>':
9261 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9262 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009263 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01009264 /* Do not adjust oap->end in do_pending_operator()
9265 * otherwise there are different results for 'dit'
9266 * (note leading whitespace in last line):
9267 * 1) <b> 2) <b>
9268 * foobar foobar
9269 * </b> </b>
9270 */
9271 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009272 flag = current_tagblock(cap->oap, cap->count1, include);
9273 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 case 'p': /* "ap" = a paragraph */
9275 flag = current_par(cap->oap, cap->count1, include, 'p');
9276 break;
9277 case 's': /* "as" = a sentence */
9278 flag = current_sent(cap->oap, cap->count1, include);
9279 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009280 case '"': /* "a"" = a double quoted string */
9281 case '\'': /* "a'" = a single quoted string */
9282 case '`': /* "a`" = a backtick quoted string */
9283 flag = current_quote(cap->oap, cap->count1, include,
9284 cap->nchar);
9285 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286#if 0 /* TODO */
9287 case 'S': /* "aS" = a section */
9288 case 'f': /* "af" = a filename */
9289 case 'u': /* "au" = a URL */
9290#endif
9291 default:
9292 flag = FAIL;
9293 break;
9294 }
9295
9296 curbuf->b_p_mps = mps_save;
9297 if (flag == FAIL)
9298 clearopbeep(cap->oap);
9299 adjust_cursor_col();
9300 curwin->w_set_curswant = TRUE;
9301}
9302#endif
9303
9304/*
9305 * "q" command: Start/stop recording.
9306 * "q:", "q/", "q?": edit command-line in command-line window.
9307 */
9308 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009309nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310{
9311 if (cap->oap->op_type == OP_FORMAT)
9312 {
9313 /* "gqq" is the same as "gqgq": format line */
9314 cap->cmdchar = 'g';
9315 cap->nchar = 'q';
9316 nv_operator(cap);
9317 }
9318 else if (!checkclearop(cap->oap))
9319 {
9320#ifdef FEAT_CMDWIN
9321 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9322 {
9323 stuffcharReadbuff(cap->nchar);
9324 stuffcharReadbuff(K_CMDWIN);
9325 }
9326 else
9327#endif
9328 /* (stop) recording into a named register, unless executing a
9329 * register */
9330 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9331 clearopbeep(cap->oap);
9332 }
9333}
9334
9335/*
9336 * Handle the "@r" command.
9337 */
9338 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009339nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340{
9341 if (checkclearop(cap->oap))
9342 return;
9343#ifdef FEAT_EVAL
9344 if (cap->nchar == '=')
9345 {
9346 if (get_expr_register() == NUL)
9347 return;
9348 }
9349#endif
9350 while (cap->count1-- && !got_int)
9351 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009352 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 {
9354 clearopbeep(cap->oap);
9355 break;
9356 }
9357 line_breakcheck();
9358 }
9359}
9360
9361/*
9362 * Handle the CTRL-U and CTRL-D commands.
9363 */
9364 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009365nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366{
9367 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9368 || (cap->cmdchar == Ctrl_D
9369 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9370 clearopbeep(cap->oap);
9371 else if (!checkclearop(cap->oap))
9372 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9373}
9374
9375/*
9376 * Handle "J" or "gJ" command.
9377 */
9378 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009379nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 if (VIsual_active) /* join the visual lines */
9382 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009383 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 {
9385 if (cap->count0 <= 1)
9386 cap->count0 = 2; /* default for join is two lines! */
9387 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9388 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 {
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009390 /* can't join when on the last line */
9391 if (cap->count0 <= 2)
9392 {
9393 clearopbeep(cap->oap);
9394 return;
9395 }
9396 cap->count0 = curbuf->b_ml.ml_line_count
9397 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009399
9400 prep_redo(cap->oap->regname, cap->count0,
9401 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9402 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 }
9404}
9405
9406/*
9407 * "P", "gP", "p" and "gp" commands.
9408 */
9409 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009410nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 int regname = 0;
9413 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009414 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009415 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 int dir;
9417 int flags = 0;
9418
9419 if (cap->oap->op_type != OP_NOP)
9420 {
9421#ifdef FEAT_DIFF
9422 /* "dp" is ":diffput" */
9423 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9424 {
9425 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009426 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 }
9428 else
9429#endif
9430 clearopbeep(cap->oap);
9431 }
9432 else
9433 {
9434 dir = (cap->cmdchar == 'P'
9435 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9436 ? BACKWARD : FORWARD;
9437 prep_redo_cmd(cap);
9438 if (cap->cmdchar == 'g')
9439 flags |= PUT_CURSEND;
9440
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 if (VIsual_active)
9442 {
9443 /* Putting in Visual mode: The put text replaces the selected
9444 * text. First delete the selected text, then put the new text.
9445 * Need to save and restore the registers that the delete
9446 * overwrites if the old contents is being put.
9447 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009448 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009450#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009452#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009453 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009454 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009455#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458
9459 )
9460 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009461 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 * put, save it first. */
9463 reg1 = get_register(regname, TRUE);
9464 }
9465
9466 /* Now delete the selected text. */
9467 cap->cmdchar = 'd';
9468 cap->nchar = NUL;
9469 cap->oap->regname = NUL;
9470 nv_operator(cap);
9471 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009472 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473
9474 /* delete PUT_LINE_BACKWARD; */
9475 cap->oap->regname = regname;
9476
9477 if (reg1 != NULL)
9478 {
9479 /* Delete probably changed the register we want to put, save
9480 * it first. Then put back what was there before the delete. */
9481 reg2 = get_register(regname, FALSE);
9482 put_register(regname, reg1);
9483 }
9484
9485 /* When deleted a linewise Visual area, put the register as
9486 * lines to avoid it joined with the next line. When deletion was
9487 * characterwise, split a line when putting lines. */
9488 if (VIsual_mode == 'V')
9489 flags |= PUT_LINE;
9490 else if (VIsual_mode == 'v')
9491 flags |= PUT_LINE_SPLIT;
9492 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9493 flags |= PUT_LINE_FORWARD;
9494 dir = BACKWARD;
9495 if ((VIsual_mode != 'V'
9496 && curwin->w_cursor.col < curbuf->b_op_start.col)
9497 || (VIsual_mode == 'V'
9498 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9499 /* cursor is at the end of the line or end of file, put
9500 * forward. */
9501 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009502 /* May have been reset in do_put(). */
9503 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 do_put(cap->oap->regname, dir, cap->count1, flags);
9506
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 /* If a register was saved, put it back now. */
9508 if (reg2 != NULL)
9509 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009510
9511 /* What to reselect with "gv"? Selecting the just put text seems to
9512 * be the most useful, since the original text was removed. */
9513 if (was_visual)
9514 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009515 curbuf->b_visual.vi_start = curbuf->b_op_start;
9516 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01009517 /* need to adjust cursor position */
9518 if (*p_sel == 'e')
9519 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009520 }
9521
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009522 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009523 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009524 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009525 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009526 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009527
9528 /* If the cursor was in that line, move it to the end of the last
9529 * line. */
9530 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9531 {
9532 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9533 coladvance((colnr_T)MAXCOL);
9534 }
9535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 auto_format(FALSE, TRUE);
9537 }
9538}
9539
9540/*
9541 * "o" and "O" commands.
9542 */
9543 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009544nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545{
9546#ifdef FEAT_DIFF
9547 /* "do" is ":diffget" */
9548 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9549 {
9550 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009551 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 }
9553 else
9554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 if (VIsual_active) /* switch start and end of visual */
9556 v_swap_corners(cap->cmdchar);
9557 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 n_opencmd(cap);
9559}
9560
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561#ifdef FEAT_NETBEANS_INTG
9562 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009563nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564{
9565 netbeans_keycommand(cap->nchar);
9566}
9567#endif
9568
9569#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009571nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572{
9573 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9574}
9575#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009576
9577#ifdef FEAT_AUTOCMD
9578/*
9579 * Trigger CursorHold event.
9580 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9581 * input buffer. "did_cursorhold" is set to avoid retriggering.
9582 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009583 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009584nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00009585{
9586 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9587 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009588 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009589}
9590#endif
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009591
9592/*
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009593 * Calculate start/end virtual columns for operating in block mode.
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009594 */
9595 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009596get_op_vcol(
9597 oparg_T *oap,
9598 colnr_T redo_VIsual_vcol,
9599 int initial) /* when TRUE adjust position for 'selectmode' */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009600{
9601 colnr_T start, end;
9602
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009603 if (VIsual_mode != Ctrl_V
9604 || (!initial && oap->end.col < W_WIDTH(curwin)))
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009605 return;
9606
Bram Moolenaar10ad1d92015-09-25 19:35:02 +02009607 oap->block_mode = TRUE;
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009608
9609#ifdef FEAT_MBYTE
9610 /* prevent from moving onto a trail byte */
9611 if (has_mbyte)
9612 mb_adjustpos(curwin->w_buffer, &oap->end);
9613#endif
9614
9615 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009616
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009617 if (!redo_VIsual_busy)
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009618 {
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009619 getvvcol(curwin, &(oap->end), &start, NULL, &end);
9620
9621 if (start < oap->start_vcol)
9622 oap->start_vcol = start;
9623 if (end > oap->end_vcol)
9624 {
9625 if (initial && *p_sel == 'e' && start >= 1
9626 && start - 1 >= oap->end_vcol)
9627 oap->end_vcol = start - 1;
9628 else
9629 oap->end_vcol = end;
9630 }
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009631 }
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009632
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009633 /* if '$' was used, get oap->end_vcol from longest line */
9634 if (curwin->w_curswant == MAXCOL)
9635 {
9636 curwin->w_cursor.col = MAXCOL;
9637 oap->end_vcol = 0;
9638 for (curwin->w_cursor.lnum = oap->start.lnum;
9639 curwin->w_cursor.lnum <= oap->end.lnum;
9640 ++curwin->w_cursor.lnum)
9641 {
9642 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
9643 if (end > oap->end_vcol)
9644 oap->end_vcol = end;
9645 }
9646 }
9647 else if (redo_VIsual_busy)
9648 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
9649 /*
9650 * Correct oap->end.col and oap->start.col to be the
9651 * upper-left and lower-right corner of the block area.
9652 *
9653 * (Actually, this does convert column positions into character
9654 * positions)
9655 */
9656 curwin->w_cursor.lnum = oap->end.lnum;
9657 coladvance(oap->end_vcol);
9658 oap->end = curwin->w_cursor;
9659
9660 curwin->w_cursor = oap->start;
9661 coladvance(oap->start_vcol);
9662 oap->start = curwin->w_cursor;
9663}