blob: e781cd70e23fc40e0cc2c33300b5d87217b0703f [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
Bram Moolenaareef9add2017-09-16 15:38:04 +02001312 /* don't go to Insert mode if a terminal has a running job */
1313 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001314 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')
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001574 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 oap->start.col = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001576# ifdef FEAT_VIRTUALEDIT
1577 oap->start.coladd = 0;
1578# endif
1579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
1582 /*
1583 * Set oap->start to the first position of the operated text, oap->end
1584 * to the end of the operated text. w_cursor is equal to oap->start.
1585 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001586 if (LT_POS(oap->start, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 {
1588#ifdef FEAT_FOLDING
1589 /* Include folded lines completely. */
1590 if (!VIsual_active)
1591 {
1592 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1593 oap->start.col = 0;
1594 if (hasFolding(curwin->w_cursor.lnum, NULL,
1595 &curwin->w_cursor.lnum))
1596 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1597 }
1598#endif
1599 oap->end = curwin->w_cursor;
1600 curwin->w_cursor = oap->start;
1601
1602 /* w_virtcol may have been updated; if the cursor goes back to its
1603 * previous position w_virtcol becomes invalid and isn't updated
1604 * automatically. */
1605 curwin->w_valid &= ~VALID_VIRTCOL;
1606 }
1607 else
1608 {
1609#ifdef FEAT_FOLDING
1610 /* Include folded lines completely. */
1611 if (!VIsual_active && oap->motion_type == MLINE)
1612 {
1613 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1614 NULL))
1615 curwin->w_cursor.col = 0;
1616 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1617 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1618 }
1619#endif
1620 oap->end = oap->start;
1621 oap->start = curwin->w_cursor;
1622 }
1623
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001624 /* Just in case lines were deleted that make the position invalid. */
1625 check_pos(curwin->w_buffer, &oap->end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1627
1628#ifdef FEAT_VIRTUALEDIT
1629 /* Set "virtual_op" before resetting VIsual_active. */
1630 virtual_op = virtual_active();
1631#endif
1632
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 if (VIsual_active || redo_VIsual_busy)
1634 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001635 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
1637 if (!redo_VIsual_busy && !gui_yank)
1638 {
1639 /*
1640 * Prepare to reselect and redo Visual: this is based on the
1641 * size of the Visual text
1642 */
1643 resel_VIsual_mode = VIsual_mode;
1644 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001645 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001647 {
1648 if (VIsual_mode != Ctrl_V)
1649 getvvcol(curwin, &(oap->end),
1650 NULL, NULL, &oap->end_vcol);
1651 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1652 {
1653 if (VIsual_mode != Ctrl_V)
1654 getvvcol(curwin, &(oap->start),
1655 &oap->start_vcol, NULL, NULL);
1656 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1657 }
1658 else
1659 resel_VIsual_vcol = oap->end_vcol;
1660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 resel_VIsual_line_count = oap->line_count;
1662 }
1663
1664 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1665 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1666 && oap->op_type != OP_COLON
1667#ifdef FEAT_FOLDING
1668 && oap->op_type != OP_FOLD
1669 && oap->op_type != OP_FOLDOPEN
1670 && oap->op_type != OP_FOLDOPENREC
1671 && oap->op_type != OP_FOLDCLOSE
1672 && oap->op_type != OP_FOLDCLOSEREC
1673 && oap->op_type != OP_FOLDDEL
1674 && oap->op_type != OP_FOLDDELREC
1675#endif
1676 && oap->motion_force == NUL
1677 )
1678 {
1679 /* Prepare for redoing. Only use the nchar field for "r",
1680 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001681 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1682 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001683 prep_redo(oap->regname, cap->count0,
1684 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1685 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001686 else if (cap->cmdchar != ':')
Bram Moolenaar641e2862012-07-25 15:06:34 +02001687 prep_redo(oap->regname, 0L, NUL, 'v',
1688 get_op_char(oap->op_type),
1689 get_extra_op_char(oap->op_type),
1690 oap->op_type == OP_REPLACE
1691 ? cap->nchar : NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 if (!redo_VIsual_busy)
1693 {
1694 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001695 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 redo_VIsual_line_count = resel_VIsual_line_count;
1697 redo_VIsual_count = cap->count0;
Bram Moolenaard79e5502016-01-10 22:13:02 +01001698 redo_VIsual_arg = cap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 }
1700 }
1701
1702 /*
1703 * oap->inclusive defaults to TRUE.
1704 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1705 * FALSE. This makes "d}P" and "v}dP" work the same.
1706 */
1707 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1708 oap->inclusive = TRUE;
1709 if (VIsual_mode == 'V')
1710 oap->motion_type = MLINE;
1711 else
1712 {
1713 oap->motion_type = MCHAR;
1714 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001715#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 && (include_line_break || !virtual_op)
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 )
1719 {
1720 oap->inclusive = FALSE;
1721 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001722 * that works on lines only. */
Bram Moolenaard009e862015-06-09 20:20:03 +02001723 if (*p_sel != 'o'
1724 && !op_on_lines(oap->op_type)
1725 && oap->end.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 {
Bram Moolenaard009e862015-06-09 20:20:03 +02001727 ++oap->end.lnum;
1728 oap->end.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001729#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard009e862015-06-09 20:20:03 +02001730 oap->end.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001731#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02001732 ++oap->line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 }
1734 }
1735 }
1736
1737 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001738
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 /*
1740 * Switch Visual off now, so screen updating does
1741 * not show inverted text when the screen is redrawn.
1742 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1743 * no screen redraw, so it is done here to remove the inverted
1744 * part.
1745 */
1746 if (!gui_yank)
1747 {
1748 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001749#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 setmouse();
1751 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001752#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001753 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 if ((oap->op_type == OP_YANK
1755 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001756 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 || oap->op_type == OP_FILTER)
1758 && oap->motion_force == NUL)
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001759 {
1760#ifdef FEAT_LINEBREAK
1761 /* make sure redrawing is correct */
1762 curwin->w_p_lbr = lbr_saved;
1763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 }
1767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
1769#ifdef FEAT_MBYTE
1770 /* Include the trailing byte of a multi-byte char. */
1771 if (has_mbyte && oap->inclusive)
1772 {
1773 int l;
1774
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001775 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 if (l > 1)
1777 oap->end.col += l - 1;
1778 }
1779#endif
1780 curwin->w_set_curswant = TRUE;
1781
1782 /*
1783 * oap->empty is set when start and end are the same. The inclusive
1784 * flag affects this too, unless yanking and the end is on a NUL.
1785 */
1786 oap->empty = (oap->motion_type == MCHAR
1787 && (!oap->inclusive
1788 || (oap->op_type == OP_YANK
1789 && gchar_pos(&oap->end) == NUL))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001790 && EQUAL_POS(oap->start, oap->end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791#ifdef FEAT_VIRTUALEDIT
1792 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1793#endif
1794 );
1795 /*
1796 * For delete, change and yank, it's an error to operate on an
1797 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1798 */
1799 empty_region_error = (oap->empty
1800 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1801
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 /* Force a redraw when operating on an empty Visual region, when
1803 * 'modifiable is off or creating a fold. */
1804 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001805#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 ))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001809 {
1810#ifdef FEAT_LINEBREAK
1811 curwin->w_p_lbr = lbr_saved;
1812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815
1816 /*
1817 * If the end of an operator is in column one while oap->motion_type
1818 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1819 * character in the previous line. If op_start is on or before the
1820 * first non-blank in the line, the operator becomes linewise
1821 * (strange, but that's the way vi does it).
1822 */
1823 if ( oap->motion_type == MCHAR
1824 && oap->inclusive == FALSE
1825 && !(cap->retval & CA_NO_ADJ_OP_END)
1826 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001828 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 && oap->line_count > 1)
1830 {
1831 oap->end_adjusted = TRUE; /* remember that we did this */
1832 --oap->line_count;
1833 --oap->end.lnum;
1834 if (inindent(0))
1835 oap->motion_type = MLINE;
1836 else
1837 {
1838 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1839 if (oap->end.col)
1840 {
1841 --oap->end.col;
1842 oap->inclusive = TRUE;
1843 }
1844 }
1845 }
1846 else
1847 oap->end_adjusted = FALSE;
1848
1849 switch (oap->op_type)
1850 {
1851 case OP_LSHIFT:
1852 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001853 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 auto_format(FALSE, TRUE);
1855 break;
1856
1857 case OP_JOIN_NS:
1858 case OP_JOIN:
1859 if (oap->line_count < 2)
1860 oap->line_count = 2;
1861 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1862 curbuf->b_ml.ml_line_count)
1863 beep_flush();
1864 else
1865 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001866 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001867 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 auto_format(FALSE, TRUE);
1869 }
1870 break;
1871
1872 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001875 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001876 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001877 CancelRedo();
1878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 else
1880 {
1881 (void)op_delete(oap);
1882 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1883 u_save_cursor(); /* cursor line wasn't saved yet */
1884 auto_format(FALSE, TRUE);
1885 }
1886 break;
1887
1888 case OP_YANK:
1889 if (empty_region_error)
1890 {
1891 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001892 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001893 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001894 CancelRedo();
1895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 }
1897 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001898 {
1899#ifdef FEAT_LINEBREAK
1900 curwin->w_p_lbr = lbr_saved;
1901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 (void)op_yank(oap, FALSE, !gui_yank);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 check_cursor_col();
1905 break;
1906
1907 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001910 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001911 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001912 CancelRedo();
1913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 else
1915 {
1916 /* This is a new edit command, not a restart. Need to
1917 * remember it to make 'insertmode' work with mappings for
1918 * Visual mode. But do this only once and not when typed and
1919 * 'insertmode' isn't set. */
1920 if (p_im || !KeyTyped)
1921 restart_edit_save = restart_edit;
1922 else
1923 restart_edit_save = 0;
1924 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001925#ifdef FEAT_LINEBREAK
1926 /* Restore linebreak, so that when the user edits it looks as
1927 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001928 if (curwin->w_p_lbr != lbr_saved)
1929 {
1930 curwin->w_p_lbr = lbr_saved;
1931 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1932 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 /* Reset finish_op now, don't want it set inside edit(). */
1935 finish_op = FALSE;
1936 if (op_change(oap)) /* will call edit() */
1937 cap->retval |= CA_COMMAND_BUSY;
1938 if (restart_edit == 0)
1939 restart_edit = restart_edit_save;
1940 }
1941 break;
1942
1943 case OP_FILTER:
1944 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1945 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1946 else
1947 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001948 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949
1950 case OP_INDENT:
1951 case OP_COLON:
1952
1953#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1954 /*
1955 * If 'equalprg' is empty, do the indenting internally.
1956 */
1957 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1958 {
1959# ifdef FEAT_LISP
1960 if (curbuf->b_p_lisp)
1961 {
1962 op_reindent(oap, get_lisp_indent);
1963 break;
1964 }
1965# endif
1966# ifdef FEAT_CINDENT
1967 op_reindent(oap,
1968# ifdef FEAT_EVAL
1969 *curbuf->b_p_inde != NUL ? get_expr_indent :
1970# endif
1971 get_c_indent);
1972 break;
1973# endif
1974 }
1975#endif
1976
1977 op_colon(oap);
1978 break;
1979
1980 case OP_TILDE:
1981 case OP_UPPER:
1982 case OP_LOWER:
1983 case OP_ROT13:
1984 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001985 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001986 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001987 CancelRedo();
1988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 else
1990 op_tilde(oap);
1991 check_cursor_col();
1992 break;
1993
1994 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001995#if defined(FEAT_EVAL)
1996 if (*curbuf->b_p_fex != NUL)
1997 op_formatexpr(oap); /* use expression */
1998 else
1999#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002000 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 op_colon(oap); /* use external command */
2002 else
2003 op_format(oap, FALSE); /* use internal function */
2004 break;
2005
2006 case OP_FORMAT2:
2007 op_format(oap, TRUE); /* use internal function */
2008 break;
2009
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002010 case OP_FUNCTION:
Bram Moolenaar4a08b0d2016-11-05 21:55:13 +01002011#ifdef FEAT_LINEBREAK
2012 /* Restore linebreak, so that when the user edits it looks as
2013 * before. */
2014 curwin->w_p_lbr = lbr_saved;
2015#endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002016 op_function(oap); /* call 'operatorfunc' */
2017 break;
2018
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 case OP_INSERT:
2020 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022#ifdef FEAT_VISUALEXTRA
2023 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002024 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002025 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002026 CancelRedo();
2027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 else
2029 {
2030 /* This is a new edit command, not a restart. Need to
2031 * remember it to make 'insertmode' work with mappings for
2032 * Visual mode. But do this only once. */
2033 restart_edit_save = restart_edit;
2034 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002035#ifdef FEAT_LINEBREAK
2036 /* Restore linebreak, so that when the user edits it looks as
2037 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002038 if (curwin->w_p_lbr != lbr_saved)
2039 {
2040 curwin->w_p_lbr = lbr_saved;
2041 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2042 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 op_insert(oap, cap->count1);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002045#ifdef FEAT_LINEBREAK
2046 /* Reset linebreak, so that formatting works correctly. */
2047 curwin->w_p_lbr = FALSE;
2048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049
2050 /* TODO: when inserting in several lines, should format all
2051 * the lines. */
2052 auto_format(FALSE, TRUE);
2053
2054 if (restart_edit == 0)
2055 restart_edit = restart_edit_save;
Bram Moolenaar0b5c93a2017-02-01 15:03:30 +01002056 else
2057 cap->retval |= CA_COMMAND_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 }
2059#else
Bram Moolenaar165bc692015-07-21 17:53:25 +02002060 vim_beep(BO_OPER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061#endif
2062 break;
2063
2064 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066#ifdef FEAT_VISUALEXTRA
2067 if (empty_region_error)
2068#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002069 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002070 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002071 CancelRedo();
2072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073#ifdef FEAT_VISUALEXTRA
2074 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002075 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002076# ifdef FEAT_LINEBREAK
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002077 /* Restore linebreak, so that when the user edits it looks as
2078 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002079 if (curwin->w_p_lbr != lbr_saved)
2080 {
2081 curwin->w_p_lbr = lbr_saved;
2082 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2083 }
2084# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 op_replace(oap, cap->nchar);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087#endif
2088 break;
2089
2090#ifdef FEAT_FOLDING
2091 case OP_FOLD:
2092 VIsual_reselect = FALSE; /* don't reselect now */
2093 foldCreate(oap->start.lnum, oap->end.lnum);
2094 break;
2095
2096 case OP_FOLDOPEN:
2097 case OP_FOLDOPENREC:
2098 case OP_FOLDCLOSE:
2099 case OP_FOLDCLOSEREC:
2100 VIsual_reselect = FALSE; /* don't reselect now */
2101 opFoldRange(oap->start.lnum, oap->end.lnum,
2102 oap->op_type == OP_FOLDOPEN
2103 || oap->op_type == OP_FOLDOPENREC,
2104 oap->op_type == OP_FOLDOPENREC
2105 || oap->op_type == OP_FOLDCLOSEREC,
2106 oap->is_VIsual);
2107 break;
2108
2109 case OP_FOLDDEL:
2110 case OP_FOLDDELREC:
2111 VIsual_reselect = FALSE; /* don't reselect now */
2112 deleteFold(oap->start.lnum, oap->end.lnum,
2113 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2114 break;
2115#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002116 case OP_NR_ADD:
2117 case OP_NR_SUB:
2118 if (empty_region_error)
2119 {
2120 vim_beep(BO_OPER);
2121 CancelRedo();
2122 }
2123 else
2124 {
2125 VIsual_active = TRUE;
2126#ifdef FEAT_LINEBREAK
2127 curwin->w_p_lbr = lbr_saved;
2128#endif
2129 op_addsub(oap, cap->count1, redo_VIsual_arg);
2130 VIsual_active = FALSE;
2131 }
2132 check_cursor_col();
2133 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 default:
2135 clearopbeep(oap);
2136 }
2137#ifdef FEAT_VIRTUALEDIT
2138 virtual_op = MAYBE;
2139#endif
2140 if (!gui_yank)
2141 {
2142 /*
2143 * if 'sol' not set, go back to old column for some commands
2144 */
2145 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2146 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2147 || oap->op_type == OP_DELETE))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002148 {
2149#ifdef FEAT_LINEBREAK
2150 curwin->w_p_lbr = FALSE;
2151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 coladvance(curwin->w_curswant = old_col);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 }
2155 else
2156 {
2157 curwin->w_cursor = old_cursor;
2158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 clearop(oap);
2161 }
Bram Moolenaar404406a2014-10-09 13:24:43 +02002162#ifdef FEAT_LINEBREAK
2163 curwin->w_p_lbr = lbr_saved;
2164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165}
2166
2167/*
2168 * Handle indent and format operators and visual mode ":".
2169 */
2170 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002171op_colon(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172{
2173 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 if (oap->is_VIsual)
2175 stuffReadbuff((char_u *)"'<,'>");
2176 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 {
2178 /*
2179 * Make the range look nice, so it can be repeated.
2180 */
2181 if (oap->start.lnum == curwin->w_cursor.lnum)
2182 stuffcharReadbuff('.');
2183 else
2184 stuffnumReadbuff((long)oap->start.lnum);
2185 if (oap->end.lnum != oap->start.lnum)
2186 {
2187 stuffcharReadbuff(',');
2188 if (oap->end.lnum == curwin->w_cursor.lnum)
2189 stuffcharReadbuff('.');
2190 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2191 stuffcharReadbuff('$');
2192 else if (oap->start.lnum == curwin->w_cursor.lnum)
2193 {
2194 stuffReadbuff((char_u *)".+");
2195 stuffnumReadbuff((long)oap->line_count - 1);
2196 }
2197 else
2198 stuffnumReadbuff((long)oap->end.lnum);
2199 }
2200 }
2201 if (oap->op_type != OP_COLON)
2202 stuffReadbuff((char_u *)"!");
2203 if (oap->op_type == OP_INDENT)
2204 {
2205#ifndef FEAT_CINDENT
2206 if (*get_equalprg() == NUL)
2207 stuffReadbuff((char_u *)"indent");
2208 else
2209#endif
2210 stuffReadbuff(get_equalprg());
2211 stuffReadbuff((char_u *)"\n");
2212 }
2213 else if (oap->op_type == OP_FORMAT)
2214 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002215 if (*curbuf->b_p_fp != NUL)
2216 stuffReadbuff(curbuf->b_p_fp);
2217 else if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 stuffReadbuff(p_fp);
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002219 else
2220 stuffReadbuff((char_u *)"fmt");
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002221 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 }
2223
2224 /*
2225 * do_cmdline() does the rest
2226 */
2227}
2228
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002229/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002230 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002231 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002232 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002233op_function(oparg_T *oap UNUSED)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002234{
2235#ifdef FEAT_EVAL
2236 char_u *(argv[1]);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002237# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002238 int save_virtual_op = virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002239# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002240
2241 if (*p_opfunc == NUL)
2242 EMSG(_("E774: 'operatorfunc' is empty"));
2243 else
2244 {
2245 /* Set '[ and '] marks to text to be operated on. */
2246 curbuf->b_op_start = oap->start;
2247 curbuf->b_op_end = oap->end;
2248 if (oap->motion_type != MLINE && !oap->inclusive)
2249 /* Exclude the end position. */
2250 decl(&curbuf->b_op_end);
2251
2252 if (oap->block_mode)
2253 argv[0] = (char_u *)"block";
2254 else if (oap->motion_type == MLINE)
2255 argv[0] = (char_u *)"line";
2256 else
2257 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002258
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002259# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002260 /* Reset virtual_op so that 'virtualedit' can be changed in the
2261 * function. */
2262 virtual_op = MAYBE;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002263# endif
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002264
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002265 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002266
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002267# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002268 virtual_op = save_virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002269# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002270 }
2271#else
2272 EMSG(_("E775: Eval feature not available"));
2273#endif
2274}
2275
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276#if defined(FEAT_MOUSE) || defined(PROTO)
2277/*
2278 * Do the appropriate action for the current mouse click in the current mode.
2279 * Not used for Command-line mode.
2280 *
2281 * Normal Mode:
2282 * event modi- position visual change action
2283 * fier cursor window
2284 * left press - yes end yes
2285 * left press C yes end yes "^]" (2)
2286 * left press S yes end yes "*" (2)
2287 * left drag - yes start if moved no
2288 * left relse - yes start if moved no
2289 * middle press - yes if not active no put register
2290 * middle press - yes if active no yank and put
2291 * right press - yes start or extend yes
2292 * right press S yes no change yes "#" (2)
2293 * right drag - yes extend no
2294 * right relse - yes extend no
2295 *
2296 * Insert or Replace Mode:
2297 * event modi- position visual change action
2298 * fier cursor window
2299 * left press - yes (cannot be active) yes
2300 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2301 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2302 * left drag - yes start or extend (1) no CTRL-O (1)
2303 * left relse - yes start or extend (1) no CTRL-O (1)
2304 * middle press - no (cannot be active) no put register
2305 * right press - yes start or extend yes CTRL-O
2306 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2307 *
2308 * (1) only if mouse pointer moved since press
2309 * (2) only if click is in same buffer
2310 *
2311 * Return TRUE if start_arrow() should be called for edit mode.
2312 */
2313 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002314do_mouse(
2315 oparg_T *oap, /* operator argument, can be NULL */
2316 int c, /* K_LEFTMOUSE, etc */
2317 int dir, /* Direction to 'put' if necessary */
2318 long count,
2319 int fixindent) /* PUT_FIXINDENT if fixing indent necessary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320{
2321 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2322 static int got_click = FALSE; /* got a click some time back */
2323
2324 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2325 int is_click; /* If FALSE it's a drag or release event */
2326 int is_drag; /* If TRUE it's a drag event */
2327 int jump_flags = 0; /* flags for jump_to_mouse() */
2328 pos_T start_visual;
2329 int moved; /* Has cursor moved? */
2330 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002331 static int in_tab_line = FALSE; /* mouse clicked in tab line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 int in_sep_line; /* mouse in vertical separator line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 int c1, c2;
2334#if defined(FEAT_FOLDING)
2335 pos_T save_cursor;
2336#endif
2337 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 static pos_T orig_cursor;
2339 colnr_T leftcol, rightcol;
2340 pos_T end_visual;
2341 int diff;
2342 int old_active = VIsual_active;
2343 int old_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 int regname;
2345
2346#if defined(FEAT_FOLDING)
2347 save_cursor = curwin->w_cursor;
2348#endif
2349
2350 /*
2351 * When GUI is active, always recognize mouse events, otherwise:
2352 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2353 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2354 * - For command line and insert mode 'mouse' is checked before calling
2355 * do_mouse().
2356 */
2357 if (do_always)
2358 do_always = FALSE;
2359 else
2360#ifdef FEAT_GUI
2361 if (!gui.in_use)
2362#endif
2363 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 if (VIsual_active)
2365 {
2366 if (!mouse_has(MOUSE_VISUAL))
2367 return FALSE;
2368 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002369 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 return FALSE;
2371 }
2372
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002373 for (;;)
2374 {
2375 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2376 if (is_drag)
2377 {
2378 /* If the next character is the same mouse event then use that
2379 * one. Speeds up dragging the status line. */
2380 if (vpeekc() != NUL)
2381 {
2382 int nc;
2383 int save_mouse_row = mouse_row;
2384 int save_mouse_col = mouse_col;
2385
2386 /* Need to get the character, peeking doesn't get the actual
2387 * one. */
2388 nc = safe_vgetc();
2389 if (c == nc)
2390 continue;
2391 vungetc(nc);
2392 mouse_row = save_mouse_row;
2393 mouse_col = save_mouse_col;
2394 }
2395 }
2396 break;
2397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
2399#ifdef FEAT_MOUSESHAPE
2400 /* May have stopped dragging the status or separator line. The pointer is
2401 * most likely still on the status or separator line. */
2402 if (!is_drag && drag_status_line)
2403 {
2404 drag_status_line = FALSE;
2405 update_mouseshape(SHAPE_IDX_STATUS);
2406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 if (!is_drag && drag_sep_line)
2408 {
2409 drag_sep_line = FALSE;
2410 update_mouseshape(SHAPE_IDX_VSEP);
2411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412#endif
2413
2414 /*
2415 * Ignore drag and release events if we didn't get a click.
2416 */
2417 if (is_click)
2418 got_click = TRUE;
2419 else
2420 {
2421 if (!got_click) /* didn't get click, ignore */
2422 return FALSE;
2423 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002424 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002426 if (in_tab_line)
2427 {
2428 in_tab_line = FALSE;
2429 return FALSE;
2430 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 }
2433
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 /*
2435 * CTRL right mouse button does CTRL-T
2436 */
2437 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2438 {
2439 if (State & INSERT)
2440 stuffcharReadbuff(Ctrl_O);
2441 if (count > 1)
2442 stuffnumReadbuff(count);
2443 stuffcharReadbuff(Ctrl_T);
2444 got_click = FALSE; /* ignore drag&release now */
2445 return FALSE;
2446 }
2447
2448 /*
2449 * CTRL only works with left mouse button
2450 */
2451 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2452 return FALSE;
2453
2454 /*
2455 * When a modifier is down, ignore drag and release events, as well as
2456 * multiple clicks and the middle mouse button.
2457 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2458 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002459 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2460 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 && (!is_click
2462 || (mod_mask & MOD_MASK_MULTI_CLICK)
2463 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002464 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 && mouse_model_popup()
2466 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002467 && !((mod_mask & MOD_MASK_ALT)
2468 && !mouse_model_popup()
2469 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 )
2471 return FALSE;
2472
2473 /*
2474 * If the button press was used as the movement command for an operator
2475 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2476 * drag/release events.
2477 */
2478 if (!is_click && which_button == MOUSE_MIDDLE)
2479 return FALSE;
2480
2481 if (oap != NULL)
2482 regname = oap->regname;
2483 else
2484 regname = 0;
2485
2486 /*
2487 * Middle mouse button does a 'put' of the selected text
2488 */
2489 if (which_button == MOUSE_MIDDLE)
2490 {
2491 if (State == NORMAL)
2492 {
2493 /*
2494 * If an operator was pending, we don't know what the user wanted
2495 * to do. Go back to normal mode: Clear the operator and beep().
2496 */
2497 if (oap != NULL && oap->op_type != OP_NOP)
2498 {
2499 clearopbeep(oap);
2500 return FALSE;
2501 }
2502
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 /*
2504 * If visual was active, yank the highlighted text and put it
2505 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002506 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 */
2508 if (VIsual_active)
2509 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002510 if (VIsual_select)
2511 {
2512 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002513 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002514 }
2515 else
2516 {
2517 stuffcharReadbuff('y');
2518 stuffcharReadbuff(K_MIDDLEMOUSE);
2519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 do_always = TRUE; /* ignore 'mouse' setting next time */
2521 return FALSE;
2522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 /*
2524 * The rest is below jump_to_mouse()
2525 */
2526 }
2527
2528 else if ((State & INSERT) == 0)
2529 return FALSE;
2530
2531 /*
2532 * Middle click in insert mode doesn't move the mouse, just insert the
2533 * contents of a register. '.' register is special, can't insert that
2534 * with do_put().
2535 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2536 * happens for the GUI).
2537 */
2538 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2539 {
2540 if (regname == '.')
2541 insert_reg(regname, TRUE);
2542 else
2543 {
2544#ifdef FEAT_CLIPBOARD
2545 if (clip_star.available && regname == 0)
2546 regname = '*';
2547#endif
2548 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2549 insert_reg(regname, TRUE);
2550 else
2551 {
2552 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2553
2554 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2555 AppendCharToRedobuff(Ctrl_R);
2556 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2557 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2558 }
2559 }
2560 return FALSE;
2561 }
2562 }
2563
2564 /* When dragging or button-up stay in the same window. */
2565 if (!is_click)
2566 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2567
2568 start_visual.lnum = 0;
2569
Bram Moolenaarf740b292006-02-16 22:11:02 +00002570 /* Check for clicking in the tab page line. */
2571 if (mouse_row == 0 && firstwin->w_winrow > 0)
2572 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002573 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002574 {
2575 if (in_tab_line)
2576 {
2577 c1 = TabPageIdxs[mouse_col];
Bram Moolenaar4a4b8212015-09-08 17:50:41 +02002578 tabpage_move(c1 <= 0 ? 9999 : c1 < tabpage_index(curtab)
2579 ? c1 - 1 : c1);
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002580 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002581 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002582 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002583
Bram Moolenaarf740b292006-02-16 22:11:02 +00002584 /* click in a tab selects that tab page */
2585 if (is_click
2586# ifdef FEAT_CMDWIN
2587 && cmdwin_type == 0
2588# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002589 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002590 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002591 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002592 c1 = TabPageIdxs[mouse_col];
2593 if (c1 >= 0)
2594 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002595 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2596 {
2597 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002598 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002599 tabpage_new();
2600 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2601 }
2602 else
2603 {
2604 /* Go to specified tab page, or next one if not clicking
2605 * on a label. */
2606 goto_tabpage(c1);
2607
2608 /* It's like clicking on the status line of a window. */
2609 if (curwin != old_curwin)
2610 end_visual_mode();
2611 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002612 }
2613 else if (c1 < 0)
2614 {
2615 tabpage_T *tp;
2616
2617 /* Close the current or specified tab page. */
2618 if (c1 == -999)
2619 tp = curtab;
2620 else
2621 tp = find_tabpage(-c1);
2622 if (tp == curtab)
2623 {
2624 if (first_tabpage->tp_next != NULL)
2625 tabpage_close(FALSE);
2626 }
2627 else if (tp != NULL)
2628 tabpage_close_other(tp, FALSE);
2629 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002630 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002631 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002632 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002633 else if (is_drag && in_tab_line)
2634 {
2635 c1 = TabPageIdxs[mouse_col];
2636 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2637 return FALSE;
2638 }
2639
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 /*
2641 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2642 * right button up -> pop-up menu
2643 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002644 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 */
2646 if (mouse_model_popup())
2647 {
2648 if (which_button == MOUSE_RIGHT
2649 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2650 {
2651 /*
2652 * NOTE: Ignore right button down and drag mouse events.
2653 * Windows only shows the popup menu on the button up event.
2654 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002655#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2656 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 if (!is_click)
2658 return FALSE;
2659#endif
2660#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2661 if (is_click || is_drag)
2662 return FALSE;
2663#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002664#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2666 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2667 if (gui.in_use)
2668 {
2669 jump_flags = 0;
2670 if (STRCMP(p_mousem, "popup_setpos") == 0)
2671 {
2672 /* First set the cursor position before showing the popup
2673 * menu. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 if (VIsual_active)
2675 {
2676 pos_T m_pos;
2677
2678 /*
2679 * set MOUSE_MAY_STOP_VIS if we are outside the
2680 * selection or the current window (might have false
2681 * negative here)
2682 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002683 if (mouse_row < curwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 || mouse_row
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002685 > (curwin->w_winrow + curwin->w_height))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 jump_flags = MOUSE_MAY_STOP_VIS;
2687 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2688 jump_flags = MOUSE_MAY_STOP_VIS;
2689 else
2690 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002691 if ((LT_POS(curwin->w_cursor, VIsual)
2692 && (LT_POS(m_pos, curwin->w_cursor)
2693 || LT_POS(VIsual, m_pos)))
2694 || (LT_POS(VIsual, curwin->w_cursor)
2695 && (LT_POS(m_pos, VIsual)
2696 || LT_POS(curwin->w_cursor, m_pos))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 {
2698 jump_flags = MOUSE_MAY_STOP_VIS;
2699 }
2700 else if (VIsual_mode == Ctrl_V)
2701 {
2702 getvcols(curwin, &curwin->w_cursor, &VIsual,
2703 &leftcol, &rightcol);
2704 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2705 if (m_pos.col < leftcol || m_pos.col > rightcol)
2706 jump_flags = MOUSE_MAY_STOP_VIS;
2707 }
2708 }
2709 }
2710 else
2711 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 }
2713 if (jump_flags)
2714 {
2715 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002716 update_curbuf(VIsual_active ? INVERTED : VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 setcursor();
2718 out_flush(); /* Update before showing popup menu */
2719 }
2720# ifdef FEAT_MENU
2721 gui_show_popupmenu();
2722# endif
2723 return (jump_flags & CURSOR_MOVED) != 0;
2724 }
2725 else
2726 return FALSE;
2727#else
2728 return FALSE;
2729#endif
2730 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002731 if (which_button == MOUSE_LEFT
2732 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 {
2734 which_button = MOUSE_RIGHT;
2735 mod_mask &= ~MOD_MASK_SHIFT;
2736 }
2737 }
2738
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 if ((State & (NORMAL | INSERT))
2740 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2741 {
2742 if (which_button == MOUSE_LEFT)
2743 {
2744 if (is_click)
2745 {
2746 /* stop Visual mode for a left click in a window, but not when
2747 * on a status line */
2748 if (VIsual_active)
2749 jump_flags |= MOUSE_MAY_STOP_VIS;
2750 }
2751 else if (mouse_has(MOUSE_VISUAL))
2752 jump_flags |= MOUSE_MAY_VIS;
2753 }
2754 else if (which_button == MOUSE_RIGHT)
2755 {
2756 if (is_click && VIsual_active)
2757 {
2758 /*
2759 * Remember the start and end of visual before moving the
2760 * cursor.
2761 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002762 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 {
2764 start_visual = curwin->w_cursor;
2765 end_visual = VIsual;
2766 }
2767 else
2768 {
2769 start_visual = VIsual;
2770 end_visual = curwin->w_cursor;
2771 }
2772 }
2773 jump_flags |= MOUSE_FOCUS;
2774 if (mouse_has(MOUSE_VISUAL))
2775 jump_flags |= MOUSE_MAY_VIS;
2776 }
2777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778
2779 /*
2780 * If an operator is pending, ignore all drags and releases until the
2781 * next mouse click.
2782 */
2783 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2784 {
2785 got_click = FALSE;
2786 oap->motion_type = MCHAR;
2787 }
2788
2789 /* When releasing the button let jump_to_mouse() know. */
2790 if (!is_click && !is_drag)
2791 jump_flags |= MOUSE_RELEASED;
2792
2793 /*
2794 * JUMP!
2795 */
2796 jump_flags = jump_to_mouse(jump_flags,
2797 oap == NULL ? NULL : &(oap->inclusive), which_button);
Bram Moolenaareb163d72017-09-23 15:08:17 +02002798
2799#ifdef FEAT_MENU
2800 /* A click in the window toolbar has no side effects. */
2801 if (jump_flags & MOUSE_WINBAR)
2802 return FALSE;
2803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 moved = (jump_flags & CURSOR_MOVED);
2805 in_status_line = (jump_flags & IN_STATUS_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 in_sep_line = (jump_flags & IN_SEP_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807
2808#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002809 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2811 {
2812 int key = KEY2TERMCAP1(c);
2813
2814 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2815 || key == (int)KE_RIGHTRELEASE)
2816 netbeans_button_release(which_button);
2817 }
2818#endif
2819
2820 /* When jumping to another window, clear a pending operator. That's a bit
2821 * friendlier than beeping and not jumping to that window. */
2822 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2823 clearop(oap);
2824
2825#ifdef FEAT_FOLDING
2826 if (mod_mask == 0
2827 && !is_drag
2828 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2829 && which_button == MOUSE_LEFT)
2830 {
2831 /* open or close a fold at this line */
2832 if (jump_flags & MOUSE_FOLD_OPEN)
2833 openFold(curwin->w_cursor.lnum, 1L);
2834 else
2835 closeFold(curwin->w_cursor.lnum, 1L);
2836 /* don't move the cursor if still in the same window */
2837 if (curwin == old_curwin)
2838 curwin->w_cursor = save_cursor;
2839 }
2840#endif
2841
2842#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2843 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2844 {
2845 clip_modeless(which_button, is_click, is_drag);
2846 return FALSE;
2847 }
2848#endif
2849
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002851 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 if (VIsual_active && is_drag && p_so)
2853 {
2854 /* In the very first line, allow scrolling one line */
2855 if (mouse_row == 0)
2856 mouse_dragging = 2;
2857 else
2858 mouse_dragging = 1;
2859 }
2860
2861 /* When dragging the mouse above the window, scroll down. */
2862 if (is_drag && mouse_row < 0 && !in_status_line)
2863 {
2864 scroll_redraw(FALSE, 1L);
2865 mouse_row = 0;
2866 }
2867
2868 if (start_visual.lnum) /* right click in visual mode */
2869 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002870 /* When ALT is pressed make Visual mode blockwise. */
2871 if (mod_mask & MOD_MASK_ALT)
2872 VIsual_mode = Ctrl_V;
2873
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 /*
2875 * In Visual-block mode, divide the area in four, pick up the corner
2876 * that is in the quarter that the cursor is in.
2877 */
2878 if (VIsual_mode == Ctrl_V)
2879 {
2880 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2881 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2882 end_visual.col = leftcol;
2883 else
2884 end_visual.col = rightcol;
Bram Moolenaarcde88542015-08-11 19:14:00 +02002885 if (curwin->w_cursor.lnum >=
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 (start_visual.lnum + end_visual.lnum) / 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 end_visual.lnum = start_visual.lnum;
2888
2889 /* move VIsual to the right column */
2890 start_visual = curwin->w_cursor; /* save the cursor pos */
2891 curwin->w_cursor = end_visual;
2892 coladvance(end_visual.col);
2893 VIsual = curwin->w_cursor;
2894 curwin->w_cursor = start_visual; /* restore the cursor */
2895 }
2896 else
2897 {
2898 /*
2899 * If the click is before the start of visual, change the start.
2900 * If the click is after the end of visual, change the end. If
2901 * the click is inside the visual, change the closest side.
2902 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002903 if (LT_POS(curwin->w_cursor, start_visual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 VIsual = end_visual;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002905 else if (LT_POS(end_visual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 VIsual = start_visual;
2907 else
2908 {
2909 /* In the same line, compare column number */
2910 if (end_visual.lnum == start_visual.lnum)
2911 {
2912 if (curwin->w_cursor.col - start_visual.col >
2913 end_visual.col - curwin->w_cursor.col)
2914 VIsual = start_visual;
2915 else
2916 VIsual = end_visual;
2917 }
2918
2919 /* In different lines, compare line number */
2920 else
2921 {
2922 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2923 (end_visual.lnum - curwin->w_cursor.lnum);
2924
2925 if (diff > 0) /* closest to end */
2926 VIsual = start_visual;
2927 else if (diff < 0) /* closest to start */
2928 VIsual = end_visual;
2929 else /* in the middle line */
2930 {
2931 if (curwin->w_cursor.col <
2932 (start_visual.col + end_visual.col) / 2)
2933 VIsual = end_visual;
2934 else
2935 VIsual = start_visual;
2936 }
2937 }
2938 }
2939 }
2940 }
2941 /*
2942 * If Visual mode started in insert mode, execute "CTRL-O"
2943 */
2944 else if ((State & INSERT) && VIsual_active)
2945 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946
2947 /*
2948 * Middle mouse click: Put text before cursor.
2949 */
2950 if (which_button == MOUSE_MIDDLE)
2951 {
2952#ifdef FEAT_CLIPBOARD
2953 if (clip_star.available && regname == 0)
2954 regname = '*';
2955#endif
2956 if (yank_register_mline(regname))
2957 {
2958 if (mouse_past_bottom)
2959 dir = FORWARD;
2960 }
2961 else if (mouse_past_eol)
2962 dir = FORWARD;
2963
2964 if (fixindent)
2965 {
2966 c1 = (dir == BACKWARD) ? '[' : ']';
2967 c2 = 'p';
2968 }
2969 else
2970 {
2971 c1 = (dir == FORWARD) ? 'p' : 'P';
2972 c2 = NUL;
2973 }
2974 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2975
2976 /*
2977 * Remember where the paste started, so in edit() Insstart can be set
2978 * to this position
2979 */
2980 if (restart_edit != 0)
2981 where_paste_started = curwin->w_cursor;
2982 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2983 }
2984
Bram Moolenaar4033c552017-09-16 20:54:51 +02002985#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 /*
2987 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2988 * error under the mouse pointer.
2989 */
2990 else if (((mod_mask & MOD_MASK_CTRL)
2991 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2992 && bt_quickfix(curbuf))
2993 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002994 if (curwin->w_llist_ref == NULL) /* quickfix window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01002995 do_cmdline_cmd((char_u *)".cc");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002996 else /* location list window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01002997 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 got_click = FALSE; /* ignore drag&release now */
2999 }
3000#endif
3001
3002 /*
3003 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
3004 * under the mouse pointer.
3005 */
3006 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3007 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3008 {
3009 if (State & INSERT)
3010 stuffcharReadbuff(Ctrl_O);
3011 stuffcharReadbuff(Ctrl_RSB);
3012 got_click = FALSE; /* ignore drag&release now */
3013 }
3014
3015 /*
3016 * Shift-Mouse click searches for the next occurrence of the word under
3017 * the mouse pointer
3018 */
3019 else if ((mod_mask & MOD_MASK_SHIFT))
3020 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003021 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 stuffcharReadbuff(Ctrl_O);
3023 if (which_button == MOUSE_LEFT)
3024 stuffcharReadbuff('*');
3025 else /* MOUSE_RIGHT */
3026 stuffcharReadbuff('#');
3027 }
3028
3029 /* Handle double clicks, unless on status line */
3030 else if (in_status_line)
3031 {
3032#ifdef FEAT_MOUSESHAPE
3033 if ((is_drag || is_click) && !drag_status_line)
3034 {
3035 drag_status_line = TRUE;
3036 update_mouseshape(-1);
3037 }
3038#endif
3039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 else if (in_sep_line)
3041 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003042#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 if ((is_drag || is_click) && !drag_sep_line)
3044 {
3045 drag_sep_line = TRUE;
3046 update_mouseshape(-1);
3047 }
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003048#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3051 && mouse_has(MOUSE_VISUAL))
3052 {
3053 if (is_click || !VIsual_active)
3054 {
3055 if (VIsual_active)
3056 orig_cursor = VIsual;
3057 else
3058 {
3059 check_visual_highlight();
3060 VIsual = curwin->w_cursor;
3061 orig_cursor = VIsual;
3062 VIsual_active = TRUE;
3063 VIsual_reselect = TRUE;
3064 /* start Select mode if 'selectmode' contains "mouse" */
3065 may_start_select('o');
3066 setmouse();
3067 }
3068 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003069 {
3070 /* Double click with ALT pressed makes it blockwise. */
3071 if (mod_mask & MOD_MASK_ALT)
3072 VIsual_mode = Ctrl_V;
3073 else
3074 VIsual_mode = 'v';
3075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3077 VIsual_mode = 'V';
3078 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3079 VIsual_mode = Ctrl_V;
3080#ifdef FEAT_CLIPBOARD
3081 /* Make sure the clipboard gets updated. Needed because start and
3082 * end may still be the same, and the selection needs to be owned */
3083 clip_star.vmode = NUL;
3084#endif
3085 }
3086 /*
3087 * A double click selects a word or a block.
3088 */
3089 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3090 {
3091 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003092 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093
3094 if (is_click)
3095 {
3096 /* If the character under the cursor (skipping white space) is
3097 * not a word character, try finding a match and select a (),
3098 * {}, [], #if/#endif, etc. block. */
3099 end_visual = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01003100 while (gc = gchar_pos(&end_visual), VIM_ISWHITE(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 inc(&end_visual);
3102 if (oap != NULL)
3103 oap->motion_type = MCHAR;
3104 if (oap != NULL
3105 && VIsual_mode == 'v'
3106 && !vim_iswordc(gchar_pos(&end_visual))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003107 && EQUAL_POS(curwin->w_cursor, VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 && (pos = findmatch(oap, NUL)) != NULL)
3109 {
3110 curwin->w_cursor = *pos;
3111 if (oap->motion_type == MLINE)
3112 VIsual_mode = 'V';
3113 else if (*p_sel == 'e')
3114 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003115 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 ++VIsual.col;
3117 else
3118 ++curwin->w_cursor.col;
3119 }
3120 }
3121 }
3122
3123 if (pos == NULL && (is_click || is_drag))
3124 {
3125 /* When not found a match or when dragging: extend to include
3126 * a word. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003127 if (LT_POS(curwin->w_cursor, orig_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 {
3129 find_start_of_word(&curwin->w_cursor);
3130 find_end_of_word(&VIsual);
3131 }
3132 else
3133 {
3134 find_start_of_word(&VIsual);
3135 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3136#ifdef FEAT_MBYTE
3137 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003138 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139#else
3140 ++curwin->w_cursor.col;
3141#endif
3142 find_end_of_word(&curwin->w_cursor);
3143 }
3144 }
3145 curwin->w_set_curswant = TRUE;
3146 }
3147 if (is_click)
3148 redraw_curbuf_later(INVERTED); /* update the inversion */
3149 }
3150 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003151 {
3152 if (mod_mask & MOD_MASK_ALT)
3153 VIsual_mode = Ctrl_V;
3154 else
3155 VIsual_mode = 'v';
3156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157
3158 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003159 if ((!VIsual_active && old_active && mode_displayed)
3160 || (VIsual_active && p_smd && msg_silent == 0
3161 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
3164 return moved;
3165}
3166
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167/*
3168 * Move "pos" back to the start of the word it's in.
3169 */
3170 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003171find_start_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172{
3173 char_u *line;
3174 int cclass;
3175 int col;
3176
3177 line = ml_get(pos->lnum);
3178 cclass = get_mouse_class(line + pos->col);
3179
3180 while (pos->col > 0)
3181 {
3182 col = pos->col - 1;
3183#ifdef FEAT_MBYTE
3184 col -= (*mb_head_off)(line, line + col);
3185#endif
3186 if (get_mouse_class(line + col) != cclass)
3187 break;
3188 pos->col = col;
3189 }
3190}
3191
3192/*
3193 * Move "pos" forward to the end of the word it's in.
3194 * When 'selection' is "exclusive", the position is just after the word.
3195 */
3196 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003197find_end_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198{
3199 char_u *line;
3200 int cclass;
3201 int col;
3202
3203 line = ml_get(pos->lnum);
3204 if (*p_sel == 'e' && pos->col > 0)
3205 {
3206 --pos->col;
3207#ifdef FEAT_MBYTE
3208 pos->col -= (*mb_head_off)(line, line + pos->col);
3209#endif
3210 }
3211 cclass = get_mouse_class(line + pos->col);
3212 while (line[pos->col] != NUL)
3213 {
3214#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003215 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216#else
3217 col = pos->col + 1;
3218#endif
3219 if (get_mouse_class(line + col) != cclass)
3220 {
3221 if (*p_sel == 'e')
3222 pos->col = col;
3223 break;
3224 }
3225 pos->col = col;
3226 }
3227}
3228
3229/*
3230 * Get class of a character for selection: same class means same word.
3231 * 0: blank
3232 * 1: punctuation groups
3233 * 2: normal word character
3234 * >2: multi-byte word character.
3235 */
3236 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003237get_mouse_class(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238{
3239 int c;
3240
3241#ifdef FEAT_MBYTE
3242 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3243 return mb_get_class(p);
3244#endif
3245
3246 c = *p;
3247 if (c == ' ' || c == '\t')
3248 return 0;
3249
3250 if (vim_iswordc(c))
3251 return 2;
3252
3253 /*
3254 * There are a few special cases where we want certain combinations of
3255 * characters to be considered as a single word. These are things like
3256 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003257 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 */
3259 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3260 return 1;
3261 return c;
3262}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263#endif /* FEAT_MOUSE */
3264
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265/*
3266 * Check if highlighting for visual mode is possible, give a warning message
3267 * if not.
3268 */
3269 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003270check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271{
3272 static int did_check = FALSE;
3273
3274 if (full_screen)
3275 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003276 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 MSG(_("Warning: terminal cannot highlight"));
3278 did_check = TRUE;
3279 }
3280}
3281
3282/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003283 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 * This function should ALWAYS be called to end Visual mode, except from
3285 * do_pending_operator().
3286 */
3287 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003288end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289{
3290#ifdef FEAT_CLIPBOARD
3291 /*
3292 * If we are using the clipboard, then remember what was selected in case
3293 * we need to paste it somewhere while we still own the selection.
3294 * Only do this when the clipboard is already owned. Don't want to grab
3295 * the selection when hitting ESC.
3296 */
3297 if (clip_star.available && clip_star.owned)
3298 clip_auto_select();
3299#endif
3300
3301 VIsual_active = FALSE;
3302#ifdef FEAT_MOUSE
3303 setmouse();
3304 mouse_dragging = 0;
3305#endif
3306
3307 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003308 curbuf->b_visual.vi_mode = VIsual_mode;
3309 curbuf->b_visual.vi_start = VIsual;
3310 curbuf->b_visual.vi_end = curwin->w_cursor;
3311 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312#ifdef FEAT_EVAL
3313 curbuf->b_visual_mode_eval = VIsual_mode;
3314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315#ifdef FEAT_VIRTUALEDIT
3316 if (!virtual_active())
3317 curwin->w_cursor.coladd = 0;
3318#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003319 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003321 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322}
3323
3324/*
3325 * Reset VIsual_active and VIsual_reselect.
3326 */
3327 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003328reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329{
3330 if (VIsual_active)
3331 {
3332 end_visual_mode();
3333 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3334 }
3335 VIsual_reselect = FALSE;
3336}
3337
3338/*
3339 * Reset VIsual_active and VIsual_reselect if it's set.
3340 */
3341 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003342reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343{
3344 if (VIsual_active)
3345 {
3346 end_visual_mode();
3347 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3348 VIsual_reselect = FALSE;
3349 }
3350}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352/*
3353 * Check for a balloon-eval special item to include when searching for an
3354 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3355 * Returns TRUE if the character at "*ptr" should be included.
3356 * "dir" is FORWARD or BACKWARD, the direction of searching.
3357 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3358 * "bnp" points to a counter for square brackets.
3359 */
3360 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003361find_is_eval_item(
3362 char_u *ptr,
3363 int *colp,
3364 int *bnp,
3365 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366{
3367 /* Accept everything inside []. */
3368 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3369 ++*bnp;
3370 if (*bnp > 0)
3371 {
3372 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3373 --*bnp;
3374 return TRUE;
3375 }
3376
3377 /* skip over "s.var" */
3378 if (*ptr == '.')
3379 return TRUE;
3380
3381 /* two-character item: s->var */
3382 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3383 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3384 {
3385 *colp += dir;
3386 return TRUE;
3387 }
3388 return FALSE;
3389}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390
3391/*
3392 * Find the identifier under or to the right of the cursor.
3393 * "find_type" can have one of three values:
3394 * FIND_IDENT: find an identifier (keyword)
3395 * FIND_STRING: find any non-white string
3396 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003397 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 *
3399 * There are three steps:
3400 * 1. Search forward for the start of an identifier/string. Doesn't move if
3401 * already on one.
3402 * 2. Search backward for the start of this identifier/string.
3403 * This doesn't match the real Vi but I like it a little better and it
3404 * shouldn't bother anyone.
3405 * 3. Search forward to the end of this identifier/string.
3406 * When FIND_IDENT isn't defined, we backup until a blank.
3407 *
3408 * Returns the length of the string, or zero if no string is found.
3409 * If a string is found, a pointer to the string is put in "*string". This
3410 * string is not always NUL terminated.
3411 */
3412 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003413find_ident_under_cursor(char_u **string, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414{
3415 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3416 curwin->w_cursor.col, string, find_type);
3417}
3418
3419/*
3420 * Like find_ident_under_cursor(), but for any window and any position.
3421 * However: Uses 'iskeyword' from the current window!.
3422 */
3423 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003424find_ident_at_pos(
3425 win_T *wp,
3426 linenr_T lnum,
3427 colnr_T startcol,
3428 char_u **string,
3429 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430{
3431 char_u *ptr;
3432 int col = 0; /* init to shut up GCC */
3433 int i;
3434#ifdef FEAT_MBYTE
3435 int this_class = 0;
3436 int prev_class;
3437 int prevcol;
3438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 int bn = 0; /* bracket nesting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440
3441 /*
3442 * if i == 0: try to find an identifier
3443 * if i == 1: try to find any non-white string
3444 */
3445 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3446 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3447 {
3448 /*
3449 * 1. skip to start of identifier/string
3450 */
3451 col = startcol;
3452#ifdef FEAT_MBYTE
3453 if (has_mbyte)
3454 {
3455 while (ptr[col] != NUL)
3456 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 /* Stop at a ']' to evaluate "a[x]". */
3458 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3459 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 this_class = mb_get_class(ptr + col);
3461 if (this_class != 0 && (i == 1 || this_class != 1))
3462 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003463 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 }
3465 }
3466 else
3467#endif
3468 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01003469 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 )
3472 ++col;
3473
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 /* When starting on a ']' count it, so that we include the '['. */
3475 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476
3477 /*
3478 * 2. Back up to start of identifier/string.
3479 */
3480#ifdef FEAT_MBYTE
3481 if (has_mbyte)
3482 {
3483 /* Remember class of character under cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3485 this_class = mb_get_class((char_u *)"a");
3486 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003488 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 {
3490 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3491 prev_class = mb_get_class(ptr + prevcol);
3492 if (this_class != prev_class
3493 && (i == 0
3494 || prev_class == 0
3495 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 && (!(find_type & FIND_EVAL)
3497 || prevcol == 0
3498 || !find_is_eval_item(ptr + prevcol, &prevcol,
3499 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 )
3501 break;
3502 col = prevcol;
3503 }
3504
3505 /* If we don't want just any old string, or we've found an
3506 * identifier, stop searching. */
3507 if (this_class > 2)
3508 this_class = 2;
3509 if (!(find_type & FIND_STRING) || this_class == 2)
3510 break;
3511 }
3512 else
3513#endif
3514 {
3515 while (col > 0
3516 && ((i == 0
3517 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003518 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 && (!(find_type & FIND_IDENT)
3520 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 || ((find_type & FIND_EVAL)
3522 && col > 1
3523 && find_is_eval_item(ptr + col - 1, &col,
3524 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 ))
3526 --col;
3527
3528 /* If we don't want just any old string, or we've found an
3529 * identifier, stop searching. */
3530 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3531 break;
3532 }
3533 }
3534
3535 if (ptr[col] == NUL || (i == 0 && (
3536#ifdef FEAT_MBYTE
3537 has_mbyte ? this_class != 2 :
3538#endif
3539 !vim_iswordc(ptr[col]))))
3540 {
3541 /*
3542 * didn't find an identifier or string
3543 */
3544 if (find_type & FIND_STRING)
3545 EMSG(_("E348: No string under cursor"));
3546 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003547 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 return 0;
3549 }
3550 ptr += col;
3551 *string = ptr;
3552
3553 /*
3554 * 3. Find the end if the identifier/string.
3555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 bn = 0;
3557 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 col = 0;
3559#ifdef FEAT_MBYTE
3560 if (has_mbyte)
3561 {
3562 /* Search for point of changing multibyte character class. */
3563 this_class = mb_get_class(ptr);
3564 while (ptr[col] != NUL
3565 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3566 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 || ((find_type & FIND_EVAL)
3568 && col <= (int)startcol
3569 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003571 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 }
3573 else
3574#endif
3575 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003576 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 || ((find_type & FIND_EVAL)
3578 && col <= (int)startcol
3579 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 )
3581 {
3582 ++col;
3583 }
3584
3585 return col;
3586}
3587
3588/*
3589 * Prepare for redo of a normal command.
3590 */
3591 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003592prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593{
3594 prep_redo(cap->oap->regname, cap->count0,
3595 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3596}
3597
3598/*
3599 * Prepare for redo of any command.
3600 * Note that only the last argument can be a multi-byte char.
3601 */
3602 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003603prep_redo(
3604 int regname,
3605 long num,
3606 int cmd1,
3607 int cmd2,
3608 int cmd3,
3609 int cmd4,
3610 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611{
3612 ResetRedobuff();
3613 if (regname != 0) /* yank from specified buffer */
3614 {
3615 AppendCharToRedobuff('"');
3616 AppendCharToRedobuff(regname);
3617 }
3618 if (num)
3619 AppendNumberToRedobuff(num);
3620
3621 if (cmd1 != NUL)
3622 AppendCharToRedobuff(cmd1);
3623 if (cmd2 != NUL)
3624 AppendCharToRedobuff(cmd2);
3625 if (cmd3 != NUL)
3626 AppendCharToRedobuff(cmd3);
3627 if (cmd4 != NUL)
3628 AppendCharToRedobuff(cmd4);
3629 if (cmd5 != NUL)
3630 AppendCharToRedobuff(cmd5);
3631}
3632
3633/*
3634 * check for operator active and clear it
3635 *
3636 * return TRUE if operator was active
3637 */
3638 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003639checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640{
3641 if (oap->op_type == OP_NOP)
3642 return FALSE;
3643 clearopbeep(oap);
3644 return TRUE;
3645}
3646
3647/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003648 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003650 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 */
3652 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003653checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003655 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 return FALSE;
3657 clearopbeep(oap);
3658 return TRUE;
3659}
3660
3661 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003662clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663{
3664 oap->op_type = OP_NOP;
3665 oap->regname = 0;
3666 oap->motion_force = NUL;
3667 oap->use_reg_one = FALSE;
3668}
3669
3670 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003671clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672{
3673 clearop(oap);
3674 beep_flush();
3675}
3676
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677/*
3678 * Remove the shift modifier from a special key.
3679 */
3680 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003681unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682{
3683 switch (cap->cmdchar)
3684 {
3685 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3686 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3687 case K_S_UP: cap->cmdchar = K_UP; break;
3688 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3689 case K_S_HOME: cap->cmdchar = K_HOME; break;
3690 case K_S_END: cap->cmdchar = K_END; break;
3691 }
3692 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3693}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003695/*
3696 * If the mode is currently displayed clear the command line or update the
3697 * command displayed.
3698 */
3699 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003700may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003701{
3702 if (mode_displayed)
3703 clear_cmdline = TRUE; /* unshow visual mode later */
3704#ifdef FEAT_CMDL_INFO
3705 else
3706 clear_showcmd();
3707#endif
3708}
3709
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3711/*
3712 * Routines for displaying a partly typed command
3713 */
3714
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003715#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716static char_u showcmd_buf[SHOWCMD_BUFLEN];
3717static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3718static int showcmd_is_clear = TRUE;
3719static int showcmd_visual = FALSE;
3720
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003721static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722
3723 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003724clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725{
3726 if (!p_sc)
3727 return;
3728
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 if (VIsual_active && !char_avail())
3730 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003731 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 long lines;
3733 colnr_T leftcol, rightcol;
3734 linenr_T top, bot;
3735
3736 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003737 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 {
3739 top = VIsual.lnum;
3740 bot = curwin->w_cursor.lnum;
3741 }
3742 else
3743 {
3744 top = curwin->w_cursor.lnum;
3745 bot = VIsual.lnum;
3746 }
3747# ifdef FEAT_FOLDING
3748 /* Include closed folds as a whole. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02003749 (void)hasFolding(top, &top, NULL);
3750 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751# endif
3752 lines = bot - top + 1;
3753
3754 if (VIsual_mode == Ctrl_V)
3755 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003756# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003757 char_u *saved_sbr = p_sbr;
3758
3759 /* Make 'sbr' empty for a moment to get the correct size. */
3760 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003761# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003763# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003764 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003765# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3767 (long)(rightcol - leftcol + 1));
3768 }
3769 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3770 sprintf((char *)showcmd_buf, "%ld", lines);
3771 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003772 {
3773 char_u *s, *e;
3774 int l;
3775 int bytes = 0;
3776 int chars = 0;
3777
3778 if (cursor_bot)
3779 {
3780 s = ml_get_pos(&VIsual);
3781 e = ml_get_cursor();
3782 }
3783 else
3784 {
3785 s = ml_get_cursor();
3786 e = ml_get_pos(&VIsual);
3787 }
3788 while ((*p_sel != 'e') ? s <= e : s < e)
3789 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003790# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003791 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003792# else
3793 l = (*s == NUL) ? 0 : 1;
3794# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003795 if (l == 0)
3796 {
3797 ++bytes;
3798 ++chars;
3799 break; /* end of line */
3800 }
3801 bytes += l;
3802 ++chars;
3803 s += l;
3804 }
3805 if (bytes == chars)
3806 sprintf((char *)showcmd_buf, "%d", chars);
3807 else
3808 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3811 showcmd_visual = TRUE;
3812 }
3813 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 {
3815 showcmd_buf[0] = NUL;
3816 showcmd_visual = FALSE;
3817
3818 /* Don't actually display something if there is nothing to clear. */
3819 if (showcmd_is_clear)
3820 return;
3821 }
3822
3823 display_showcmd();
3824}
3825
3826/*
3827 * Add 'c' to string of shown command chars.
3828 * Return TRUE if output has been written (and setcursor() has been called).
3829 */
3830 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003831add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832{
3833 char_u *p;
3834 int old_len;
3835 int extra_len;
3836 int overflow;
3837#if defined(FEAT_MOUSE)
3838 int i;
3839 static int ignore[] =
3840 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003841# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3843 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003844# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01003845 K_IGNORE, K_PS,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3847 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3848 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003849 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003851 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 0
3853 };
3854#endif
3855
Bram Moolenaar09df3122006-01-23 22:23:09 +00003856 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 return FALSE;
3858
3859 if (showcmd_visual)
3860 {
3861 showcmd_buf[0] = NUL;
3862 showcmd_visual = FALSE;
3863 }
3864
3865#if defined(FEAT_MOUSE)
3866 /* Ignore keys that are scrollbar updates and mouse clicks */
3867 if (IS_SPECIAL(c))
3868 for (i = 0; ignore[i] != 0; ++i)
3869 if (ignore[i] == c)
3870 return FALSE;
3871#endif
3872
3873 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003874 if (*p == ' ')
3875 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 old_len = (int)STRLEN(showcmd_buf);
3877 extra_len = (int)STRLEN(p);
3878 overflow = old_len + extra_len - SHOWCMD_COLS;
3879 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003880 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3881 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 STRCAT(showcmd_buf, p);
3883
3884 if (char_avail())
3885 return FALSE;
3886
3887 display_showcmd();
3888
3889 return TRUE;
3890}
3891
3892 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003893add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894{
3895 if (!add_to_showcmd(c))
3896 setcursor();
3897}
3898
3899/*
3900 * Delete 'len' characters from the end of the shown command.
3901 */
3902 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003903del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904{
3905 int old_len;
3906
3907 if (!p_sc)
3908 return;
3909
3910 old_len = (int)STRLEN(showcmd_buf);
3911 if (len > old_len)
3912 len = old_len;
3913 showcmd_buf[old_len - len] = NUL;
3914
3915 if (!char_avail())
3916 display_showcmd();
3917}
3918
3919/*
3920 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3921 * something and there is a partial mapping.
3922 */
3923 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003924push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925{
3926 if (p_sc)
3927 STRCPY(old_showcmd_buf, showcmd_buf);
3928}
3929
3930 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003931pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932{
3933 if (!p_sc)
3934 return;
3935
3936 STRCPY(showcmd_buf, old_showcmd_buf);
3937
3938 display_showcmd();
3939}
3940
3941 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003942display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943{
3944 int len;
3945
3946 cursor_off();
3947
3948 len = (int)STRLEN(showcmd_buf);
3949 if (len == 0)
3950 showcmd_is_clear = TRUE;
3951 else
3952 {
3953 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3954 showcmd_is_clear = FALSE;
3955 }
3956
3957 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003958 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3959 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 */
3961 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3962
3963 setcursor(); /* put cursor back where it belongs */
3964}
3965#endif
3966
3967#ifdef FEAT_SCROLLBIND
3968/*
3969 * When "check" is FALSE, prepare for commands that scroll the window.
3970 * When "check" is TRUE, take care of scroll-binding after the window has
3971 * scrolled. Called from normal_cmd() and edit().
3972 */
3973 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003974do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975{
3976 static win_T *old_curwin = NULL;
3977 static linenr_T old_topline = 0;
3978#ifdef FEAT_DIFF
3979 static int old_topfill = 0;
3980#endif
3981 static buf_T *old_buf = NULL;
3982 static colnr_T old_leftcol = 0;
3983
3984 if (check && curwin->w_p_scb)
3985 {
3986 /* If a ":syncbind" command was just used, don't scroll, only reset
3987 * the values. */
3988 if (did_syncbind)
3989 did_syncbind = FALSE;
3990 else if (curwin == old_curwin)
3991 {
3992 /*
3993 * Synchronize other windows, as necessary according to
3994 * 'scrollbind'. Don't do this after an ":edit" command, except
3995 * when 'diff' is set.
3996 */
3997 if ((curwin->w_buffer == old_buf
3998#ifdef FEAT_DIFF
3999 || curwin->w_p_diff
4000#endif
4001 )
4002 && (curwin->w_topline != old_topline
4003#ifdef FEAT_DIFF
4004 || curwin->w_topfill != old_topfill
4005#endif
4006 || curwin->w_leftcol != old_leftcol))
4007 {
4008 check_scrollbind(curwin->w_topline - old_topline,
4009 (long)(curwin->w_leftcol - old_leftcol));
4010 }
4011 }
4012 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4013 {
4014 /*
4015 * When switching between windows, make sure that the relative
4016 * vertical offset is valid for the new window. The relative
4017 * offset is invalid whenever another 'scrollbind' window has
4018 * scrolled to a point that would force the current window to
4019 * scroll past the beginning or end of its buffer. When the
4020 * resync is performed, some of the other 'scrollbind' windows may
4021 * need to jump so that the current window's relative position is
4022 * visible on-screen.
4023 */
4024 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4025 }
4026 curwin->w_scbind_pos = curwin->w_topline;
4027 }
4028
4029 old_curwin = curwin;
4030 old_topline = curwin->w_topline;
4031#ifdef FEAT_DIFF
4032 old_topfill = curwin->w_topfill;
4033#endif
4034 old_buf = curwin->w_buffer;
4035 old_leftcol = curwin->w_leftcol;
4036}
4037
4038/*
4039 * Synchronize any windows that have "scrollbind" set, based on the
4040 * number of rows by which the current window has changed
4041 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4042 */
4043 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004044check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045{
4046 int want_ver;
4047 int want_hor;
4048 win_T *old_curwin = curwin;
4049 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 int old_VIsual_select = VIsual_select;
4051 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 colnr_T tgt_leftcol = curwin->w_leftcol;
4053 long topline;
4054 long y;
4055
4056 /*
4057 * check 'scrollopt' string for vertical and horizontal scroll options
4058 */
4059 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4060#ifdef FEAT_DIFF
4061 want_ver |= old_curwin->w_p_diff;
4062#endif
4063 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4064
4065 /*
4066 * loop through the scrollbound windows and scroll accordingly
4067 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02004069 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 {
4071 curbuf = curwin->w_buffer;
4072 /* skip original window and windows with 'noscrollbind' */
4073 if (curwin != old_curwin && curwin->w_p_scb)
4074 {
4075 /*
4076 * do the vertical scroll
4077 */
4078 if (want_ver)
4079 {
4080#ifdef FEAT_DIFF
4081 if (old_curwin->w_p_diff && curwin->w_p_diff)
4082 {
4083 diff_set_topline(old_curwin, curwin);
4084 }
4085 else
4086#endif
4087 {
4088 curwin->w_scbind_pos += topline_diff;
4089 topline = curwin->w_scbind_pos;
4090 if (topline > curbuf->b_ml.ml_line_count)
4091 topline = curbuf->b_ml.ml_line_count;
4092 if (topline < 1)
4093 topline = 1;
4094
4095 y = topline - curwin->w_topline;
4096 if (y > 0)
4097 scrollup(y, FALSE);
4098 else
4099 scrolldown(-y, FALSE);
4100 }
4101
4102 redraw_later(VALID);
4103 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 }
4106
4107 /*
4108 * do the horizontal scroll
4109 */
4110 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4111 {
4112 curwin->w_leftcol = tgt_leftcol;
4113 leftcol_changed();
4114 }
4115 }
4116 }
4117
4118 /*
4119 * reset current-window
4120 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 VIsual_select = old_VIsual_select;
4122 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 curwin = old_curwin;
4124 curbuf = old_curbuf;
4125}
4126#endif /* #ifdef FEAT_SCROLLBIND */
4127
4128/*
4129 * Command character that's ignored.
4130 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004131 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004134nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004136 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137}
4138
4139/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004140 * Command character that doesn't do anything, but unlike nv_ignore() does
4141 * start edit(). Used for "startinsert" executed while starting up.
4142 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004143 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004144nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00004145{
4146}
4147
4148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 * Command character doesn't exist.
4150 */
4151 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004152nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153{
4154 clearopbeep(cap->oap);
4155}
4156
4157/*
4158 * <Help> and <F1> commands.
4159 */
4160 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004161nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162{
4163 if (!checkclearopq(cap->oap))
4164 ex_help(NULL);
4165}
4166
4167/*
4168 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4169 */
4170 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004171nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172{
Bram Moolenaard79e5502016-01-10 22:13:02 +01004173 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004174 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01004175 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01004176 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
4177 op_addsub(cap->oap, cap->count1, cap->arg);
4178 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004179 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01004180 else if (VIsual_active)
4181 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004182 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01004183 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184}
4185
4186/*
4187 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4188 */
4189 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004190nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191{
4192 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004193 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004194 if (mod_mask & MOD_MASK_CTRL)
4195 {
4196 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4197 if (cap->arg == BACKWARD)
4198 goto_tabpage(-(int)cap->count1);
4199 else
4200 goto_tabpage((int)cap->count0);
4201 }
4202 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02004203 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205}
4206
4207/*
4208 * Implementation of "gd" and "gD" command.
4209 */
4210 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004211nv_gd(
4212 oparg_T *oap,
4213 int nchar,
4214 int thisblock) /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215{
4216 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 char_u *ptr;
4218
Bram Moolenaard9d30582005-05-18 22:10:28 +00004219 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02004220 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
4221 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004223#ifdef FEAT_FOLDING
4224 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4225 foldOpenCursor();
4226#endif
4227}
4228
4229/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02004230 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
4231 * otherwise.
4232 */
4233 static int
4234is_ident(char_u *line, int offset)
4235{
4236 int i;
4237 int incomment = FALSE;
4238 int instring = 0;
4239 int prev = 0;
4240
4241 for (i = 0; i < offset && line[i] != NUL; i++)
4242 {
4243 if (instring != 0)
4244 {
4245 if (prev != '\\' && line[i] == instring)
4246 instring = 0;
4247 }
4248 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
4249 {
4250 instring = line[i];
4251 }
4252 else
4253 {
4254 if (incomment)
4255 {
4256 if (prev == '*' && line[i] == '/')
4257 incomment = FALSE;
4258 }
4259 else if (prev == '/' && line[i] == '*')
4260 {
4261 incomment = TRUE;
4262 }
4263 else if (prev == '/' && line[i] == '/')
4264 {
4265 return FALSE;
4266 }
4267 }
4268
4269 prev = line[i];
4270 }
4271
4272 return incomment == FALSE && instring == 0;
4273}
4274
4275/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004276 * Search for variable declaration of "ptr[len]".
4277 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4278 * current file ("gD").
4279 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004280 * Return FAIL when not found.
4281 */
4282 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004283find_decl(
4284 char_u *ptr,
4285 int len,
4286 int locally,
4287 int thisblock,
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004288 int flags_arg) /* flags passed to searchit() */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004289{
4290 char_u *pat;
4291 pos_T old_pos;
4292 pos_T par_pos;
4293 pos_T found_pos;
4294 int t;
4295 int save_p_ws;
4296 int save_p_scs;
4297 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004298 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004299 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004300 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004301
4302 if ((pat = alloc(len + 7)) == NULL)
4303 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004304
4305 /* Put "\V" before the pattern to avoid that the special meaning of "."
4306 * and "~" causes trouble. */
4307 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4308 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 old_pos = curwin->w_cursor;
4310 save_p_ws = p_ws;
4311 save_p_scs = p_scs;
4312 p_ws = FALSE; /* don't wrap around end of file now */
4313 p_scs = FALSE; /* don't switch ignorecase off now */
4314
4315 /*
4316 * With "gD" go to line 1.
4317 * With "gd" Search back for the start of the current function, then go
4318 * back until a blank line. If this fails go to line 1.
4319 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004320 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 {
4322 setpcmark(); /* Set in findpar() otherwise */
4323 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004324 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 }
4326 else
4327 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004328 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4330 --curwin->w_cursor.lnum;
4331 }
4332 curwin->w_cursor.col = 0;
4333
4334 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004335 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004336 for (;;)
4337 {
Bram Moolenaar226630a2016-10-08 19:21:31 +02004338 valid = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004339 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004340 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004341 if (curwin->w_cursor.lnum >= old_pos.lnum)
4342 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004343
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004344 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004345 {
4346 pos_T *pos;
4347
4348 /* Check that the block the match is in doesn't end before the
4349 * position where we started the search from. */
4350 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4351 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4352 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02004353 {
4354 /* There can't be a useful match before the end of this block.
4355 * Skip to the end. */
4356 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004357 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02004358 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004359 }
4360
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004361 if (t == FAIL)
4362 {
4363 /* If we previously found a valid position, use it. */
4364 if (found_pos.lnum != 0)
4365 {
4366 curwin->w_cursor = found_pos;
4367 t = OK;
4368 }
4369 break;
4370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004372 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004373 {
4374 /* Ignore this line, continue at start of next line. */
4375 ++curwin->w_cursor.lnum;
4376 curwin->w_cursor.col = 0;
4377 continue;
4378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379#endif
Bram Moolenaar226630a2016-10-08 19:21:31 +02004380 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
4381
4382 /* If the current position is not a valid identifier and a previous
4383 * match is present, favor that one instead. */
4384 if (!valid && found_pos.lnum != 0)
4385 {
4386 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004387 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004388 }
4389
4390 /* Global search: use first valid match found */
4391 if (valid && !locally)
4392 break;
4393 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004394 {
4395 /* If we previously found a valid position, use it. */
4396 if (found_pos.lnum != 0)
4397 curwin->w_cursor = found_pos;
4398 break;
4399 }
4400
Bram Moolenaar226630a2016-10-08 19:21:31 +02004401 /* For finding a local variable and the match is before the "{" or
4402 * inside a comment, continue searching. For K&R style function
4403 * declarations this skips the function header without types. */
4404 if (!valid)
4405 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004406 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02004407 }
4408 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02004409 found_pos = curwin->w_cursor;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004410 /* Remove SEARCH_START from flags to avoid getting stuck at one
4411 * position. */
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004412 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004414
4415 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004417 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 curwin->w_cursor = old_pos;
4419 }
4420 else
4421 {
4422 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 /* "n" searches forward now */
4424 reset_search_dir();
4425 }
4426
4427 vim_free(pat);
4428 p_ws = save_p_ws;
4429 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004430
4431 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432}
4433
4434/*
4435 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4436 * lines rather than lines in the file.
4437 * 'dist' must be positive.
4438 *
4439 * Return OK if able to move cursor, FAIL otherwise.
4440 */
4441 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004442nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443{
4444 int linelen = linetabsize(ml_get_curline());
4445 int retval = OK;
4446 int atend = FALSE;
4447 int n;
4448 int col_off1; /* margin offset for first screen line */
4449 int col_off2; /* margin offset for wrapped screen line */
4450 int width1; /* text width for first screen line */
4451 int width2; /* test width for wrapped screen line */
4452
4453 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004454 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455
4456 col_off1 = curwin_col_off();
4457 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02004458 width1 = curwin->w_width - col_off1;
4459 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01004460 if (width2 == 0)
4461 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004464 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 /*
4466 * Instead of sticking at the last character of the buffer line we
4467 * try to stick in the last column of the screen.
4468 */
4469 if (curwin->w_curswant == MAXCOL)
4470 {
4471 atend = TRUE;
4472 validate_virtcol();
4473 if (width1 <= 0)
4474 curwin->w_curswant = 0;
4475 else
4476 {
4477 curwin->w_curswant = width1 - 1;
4478 if (curwin->w_virtcol > curwin->w_curswant)
4479 curwin->w_curswant += ((curwin->w_virtcol
4480 - curwin->w_curswant - 1) / width2 + 1) * width2;
4481 }
4482 }
4483 else
4484 {
4485 if (linelen > width1)
4486 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4487 else
4488 n = width1;
4489 if (curwin->w_curswant > (colnr_T)n + 1)
4490 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4491 * width2;
4492 }
4493
4494 while (dist--)
4495 {
4496 if (dir == BACKWARD)
4497 {
4498 if ((long)curwin->w_curswant >= width2)
4499 /* move back within line */
4500 curwin->w_curswant -= width2;
4501 else
4502 {
4503 /* to previous line */
4504 if (curwin->w_cursor.lnum == 1)
4505 {
4506 retval = FAIL;
4507 break;
4508 }
4509 --curwin->w_cursor.lnum;
4510#ifdef FEAT_FOLDING
4511 /* Move to the start of a closed fold. Don't do that when
4512 * 'foldopen' contains "all": it will open in a moment. */
4513 if (!(fdo_flags & FDO_ALL))
4514 (void)hasFolding(curwin->w_cursor.lnum,
4515 &curwin->w_cursor.lnum, NULL);
4516#endif
4517 linelen = linetabsize(ml_get_curline());
4518 if (linelen > width1)
4519 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4520 + 1) * width2;
4521 }
4522 }
4523 else /* dir == FORWARD */
4524 {
4525 if (linelen > width1)
4526 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4527 else
4528 n = width1;
4529 if (curwin->w_curswant + width2 < (colnr_T)n)
4530 /* move forward within line */
4531 curwin->w_curswant += width2;
4532 else
4533 {
4534 /* to next line */
4535#ifdef FEAT_FOLDING
4536 /* Move to the end of a closed fold. */
4537 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4538 &curwin->w_cursor.lnum);
4539#endif
4540 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4541 {
4542 retval = FAIL;
4543 break;
4544 }
4545 curwin->w_cursor.lnum++;
4546 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004547 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 }
4549 }
4550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004553 if (virtual_active() && atend)
4554 coladvance(MAXCOL);
4555 else
4556 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557
4558#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4559 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4560 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004561 colnr_T virtcol;
4562
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 /*
4564 * Check for landing on a character that got split at the end of the
4565 * last line. We want to advance a screenline, not end up in the same
4566 * screenline or move two screenlines.
4567 */
4568 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004569 virtcol = curwin->w_virtcol;
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004570# if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004571 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4572 virtcol -= vim_strsize(p_sbr);
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004573# endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004574
4575 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 && (curwin->w_curswant < (colnr_T)width1
4577 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4578 : ((curwin->w_curswant - width1) % width2
4579 > (colnr_T)width2 / 2)))
4580 --curwin->w_cursor.col;
4581 }
4582#endif
4583
4584 if (atend)
4585 curwin->w_curswant = MAXCOL; /* stick in the last column */
4586
4587 return retval;
4588}
4589
4590#ifdef FEAT_MOUSE
4591/*
4592 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4593 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004594 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4595 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 */
4597 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004598nv_mousescroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599{
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004600 win_T *old_curwin = curwin, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004602 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 {
4604 int row, col;
4605
4606 row = mouse_row;
4607 col = mouse_col;
4608
4609 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004610 wp = mouse_find_win(&row, &col);
4611 if (wp == NULL)
4612 return;
4613 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 curbuf = curwin->w_buffer;
4615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004617 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004619# ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02004620 if (term_use_loop())
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004621 send_keys_to_term(curbuf->b_term, cap->cmdchar, TRUE);
4622 else
4623# endif
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004624 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4625 {
4626 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4627 }
4628 else
4629 {
4630 cap->count1 = 3;
4631 cap->count0 = 3;
4632 nv_scroll_line(cap);
4633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004635# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 else
4637 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004638 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4639 if (!curwin->w_p_wrap)
4640 {
4641 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004642
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004643 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02004644 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004645 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4646 if (val < 0)
4647 val = 0;
4648
4649 gui_do_horiz_scroll(val, TRUE);
4650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004652# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 curwin->w_redr_status = TRUE;
4655
4656 curwin = old_curwin;
4657 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658}
4659
4660/*
4661 * Mouse clicks and drags.
4662 */
4663 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004664nv_mouse(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665{
4666 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4667}
4668#endif
4669
4670/*
4671 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4672 * cap->arg must be TRUE for CTRL-E.
4673 */
4674 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004675nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676{
4677 if (!checkclearop(cap->oap))
4678 scroll_redraw(cap->arg, cap->count1);
4679}
4680
4681/*
4682 * Scroll "count" lines up or down, and redraw.
4683 */
4684 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004685scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686{
4687 linenr_T prev_topline = curwin->w_topline;
4688#ifdef FEAT_DIFF
4689 int prev_topfill = curwin->w_topfill;
4690#endif
4691 linenr_T prev_lnum = curwin->w_cursor.lnum;
4692
4693 if (up)
4694 scrollup(count, TRUE);
4695 else
4696 scrolldown(count, TRUE);
4697 if (p_so)
4698 {
4699 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4700 * valid, otherwise the screen jumps back at the end of the file. */
4701 cursor_correct();
4702 check_cursor_moved(curwin);
4703 curwin->w_valid |= VALID_TOPLINE;
4704
4705 /* If moved back to where we were, at least move the cursor, otherwise
4706 * we get stuck at one position. Don't move the cursor up if the
4707 * first line of the buffer is already on the screen */
4708 while (curwin->w_topline == prev_topline
4709#ifdef FEAT_DIFF
4710 && curwin->w_topfill == prev_topfill
4711#endif
4712 )
4713 {
4714 if (up)
4715 {
4716 if (curwin->w_cursor.lnum > prev_lnum
4717 || cursor_down(1L, FALSE) == FAIL)
4718 break;
4719 }
4720 else
4721 {
4722 if (curwin->w_cursor.lnum < prev_lnum
4723 || prev_topline == 1L
4724 || cursor_up(1L, FALSE) == FAIL)
4725 break;
4726 }
4727 /* Mark w_topline as valid, otherwise the screen jumps back at the
4728 * end of the file. */
4729 check_cursor_moved(curwin);
4730 curwin->w_valid |= VALID_TOPLINE;
4731 }
4732 }
4733 if (curwin->w_cursor.lnum != prev_lnum)
4734 coladvance(curwin->w_curswant);
4735 redraw_later(VALID);
4736}
4737
4738/*
4739 * Commands that start with "z".
4740 */
4741 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004742nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743{
4744 long n;
4745 colnr_T col;
4746 int nchar = cap->nchar;
4747#ifdef FEAT_FOLDING
4748 long old_fdl = curwin->w_p_fdl;
4749 int old_fen = curwin->w_p_fen;
4750#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004751#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004752 int undo = FALSE;
4753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754
4755 if (VIM_ISDIGIT(nchar))
4756 {
4757 /*
4758 * "z123{nchar}": edit the count before obtaining {nchar}
4759 */
4760 if (checkclearop(cap->oap))
4761 return;
4762 n = nchar - '0';
4763 for (;;)
4764 {
4765#ifdef USE_ON_FLY_SCROLL
4766 dont_scroll = TRUE; /* disallow scrolling here */
4767#endif
4768 ++no_mapping;
4769 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004770 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 --no_mapping;
4773 --allow_keys;
4774#ifdef FEAT_CMDL_INFO
4775 (void)add_to_showcmd(nchar);
4776#endif
4777 if (nchar == K_DEL || nchar == K_KDEL)
4778 n /= 10;
4779 else if (VIM_ISDIGIT(nchar))
4780 n = n * 10 + (nchar - '0');
4781 else if (nchar == CAR)
4782 {
4783#ifdef FEAT_GUI
4784 need_mouse_correct = TRUE;
4785#endif
4786 win_setheight((int)n);
4787 break;
4788 }
4789 else if (nchar == 'l'
4790 || nchar == 'h'
4791 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004792 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 {
4794 cap->count1 = n ? n * cap->count1 : cap->count1;
4795 goto dozet;
4796 }
4797 else
4798 {
4799 clearopbeep(cap->oap);
4800 break;
4801 }
4802 }
4803 cap->oap->op_type = OP_NOP;
4804 return;
4805 }
4806
4807dozet:
4808 if (
4809#ifdef FEAT_FOLDING
4810 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4811 * and "zC" only in Visual mode. "zj" and "zk" are motion
4812 * commands. */
4813 cap->nchar != 'f' && cap->nchar != 'F'
4814 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4815 && cap->nchar != 'j' && cap->nchar != 'k'
4816 &&
4817#endif
4818 checkclearop(cap->oap))
4819 return;
4820
4821 /*
4822 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4823 * If line number given, set cursor.
4824 */
4825 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4826 && cap->count0
4827 && cap->count0 != curwin->w_cursor.lnum)
4828 {
4829 setpcmark();
4830 if (cap->count0 > curbuf->b_ml.ml_line_count)
4831 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4832 else
4833 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004834 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 }
4836
4837 switch (nchar)
4838 {
4839 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4840 case '+':
4841 if (cap->count0 == 0)
4842 {
4843 /* No count given: put cursor at the line below screen */
4844 validate_botline(); /* make sure w_botline is valid */
4845 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4846 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4847 else
4848 curwin->w_cursor.lnum = curwin->w_botline;
4849 }
4850 /* FALLTHROUGH */
4851 case NL:
4852 case CAR:
4853 case K_KENTER:
4854 beginline(BL_WHITE | BL_FIX);
4855 /* FALLTHROUGH */
4856
4857 case 't': scroll_cursor_top(0, TRUE);
4858 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004859 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 break;
4861
4862 /* "z." and "zz": put cursor in middle of screen */
4863 case '.': beginline(BL_WHITE | BL_FIX);
4864 /* FALLTHROUGH */
4865
4866 case 'z': scroll_cursor_halfway(TRUE);
4867 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004868 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 break;
4870
4871 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4872 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4873 * when <count> is at bottom of window, and puts that one at
4874 * bottom of window. */
4875 if (cap->count0 != 0)
4876 {
4877 scroll_cursor_bot(0, TRUE);
4878 curwin->w_cursor.lnum = curwin->w_topline;
4879 }
4880 else if (curwin->w_topline == 1)
4881 curwin->w_cursor.lnum = 1;
4882 else
4883 curwin->w_cursor.lnum = curwin->w_topline - 1;
4884 /* FALLTHROUGH */
4885 case '-':
4886 beginline(BL_WHITE | BL_FIX);
4887 /* FALLTHROUGH */
4888
4889 case 'b': scroll_cursor_bot(0, TRUE);
4890 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004891 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 break;
4893
4894 /* "zH" - scroll screen right half-page */
4895 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02004896 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 /* FALLTHROUGH */
4898
4899 /* "zh" - scroll screen to the right */
4900 case 'h':
4901 case K_LEFT:
4902 if (!curwin->w_p_wrap)
4903 {
4904 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4905 curwin->w_leftcol = 0;
4906 else
4907 curwin->w_leftcol -= (colnr_T)cap->count1;
4908 leftcol_changed();
4909 }
4910 break;
4911
4912 /* "zL" - scroll screen left half-page */
Bram Moolenaar02631462017-09-22 15:20:32 +02004913 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 /* FALLTHROUGH */
4915
4916 /* "zl" - scroll screen to the left */
4917 case 'l':
4918 case K_RIGHT:
4919 if (!curwin->w_p_wrap)
4920 {
4921 /* scroll the window left */
4922 curwin->w_leftcol += (colnr_T)cap->count1;
4923 leftcol_changed();
4924 }
4925 break;
4926
4927 /* "zs" - scroll screen, cursor at the start */
4928 case 's': if (!curwin->w_p_wrap)
4929 {
4930#ifdef FEAT_FOLDING
4931 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4932 col = 0; /* like the cursor is in col 0 */
4933 else
4934#endif
4935 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4936 if ((long)col > p_siso)
4937 col -= p_siso;
4938 else
4939 col = 0;
4940 if (curwin->w_leftcol != col)
4941 {
4942 curwin->w_leftcol = col;
4943 redraw_later(NOT_VALID);
4944 }
4945 }
4946 break;
4947
4948 /* "ze" - scroll screen, cursor at the end */
4949 case 'e': if (!curwin->w_p_wrap)
4950 {
4951#ifdef FEAT_FOLDING
4952 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4953 col = 0; /* like the cursor is in col 0 */
4954 else
4955#endif
4956 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02004957 n = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 if ((long)col + p_siso < n)
4959 col = 0;
4960 else
4961 col = col + p_siso - n + 1;
4962 if (curwin->w_leftcol != col)
4963 {
4964 curwin->w_leftcol = col;
4965 redraw_later(NOT_VALID);
4966 }
4967 }
4968 break;
4969
4970#ifdef FEAT_FOLDING
4971 /* "zF": create fold command */
4972 /* "zf": create fold operator */
4973 case 'F':
4974 case 'f': if (foldManualAllowed(TRUE))
4975 {
4976 cap->nchar = 'f';
4977 nv_operator(cap);
4978 curwin->w_p_fen = TRUE;
4979
4980 /* "zF" is like "zfzf" */
4981 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4982 {
4983 nv_operator(cap);
4984 finish_op = TRUE;
4985 }
4986 }
4987 else
4988 clearopbeep(cap->oap);
4989 break;
4990
4991 /* "zd": delete fold at cursor */
4992 /* "zD": delete fold at cursor recursively */
4993 case 'd':
4994 case 'D': if (foldManualAllowed(FALSE))
4995 {
4996 if (VIsual_active)
4997 nv_operator(cap);
4998 else
4999 deleteFold(curwin->w_cursor.lnum,
5000 curwin->w_cursor.lnum, nchar == 'D', FALSE);
5001 }
5002 break;
5003
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005004 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 case 'E': if (foldmethodIsManual(curwin))
5006 {
5007 clearFolding(curwin);
5008 changed_window_setting();
5009 }
5010 else if (foldmethodIsMarker(curwin))
5011 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5012 TRUE, FALSE);
5013 else
5014 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5015 break;
5016
5017 /* "zn": fold none: reset 'foldenable' */
5018 case 'n': curwin->w_p_fen = FALSE;
5019 break;
5020
5021 /* "zN": fold Normal: set 'foldenable' */
5022 case 'N': curwin->w_p_fen = TRUE;
5023 break;
5024
5025 /* "zi": invert folding: toggle 'foldenable' */
5026 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5027 break;
5028
5029 /* "za": open closed fold or close open fold at cursor */
5030 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5031 openFold(curwin->w_cursor.lnum, cap->count1);
5032 else
5033 {
5034 closeFold(curwin->w_cursor.lnum, cap->count1);
5035 curwin->w_p_fen = TRUE;
5036 }
5037 break;
5038
5039 /* "zA": open fold at cursor recursively */
5040 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5041 openFoldRecurse(curwin->w_cursor.lnum);
5042 else
5043 {
5044 closeFoldRecurse(curwin->w_cursor.lnum);
5045 curwin->w_p_fen = TRUE;
5046 }
5047 break;
5048
5049 /* "zo": open fold at cursor or Visual area */
5050 case 'o': if (VIsual_active)
5051 nv_operator(cap);
5052 else
5053 openFold(curwin->w_cursor.lnum, cap->count1);
5054 break;
5055
5056 /* "zO": open fold recursively */
5057 case 'O': if (VIsual_active)
5058 nv_operator(cap);
5059 else
5060 openFoldRecurse(curwin->w_cursor.lnum);
5061 break;
5062
5063 /* "zc": close fold at cursor or Visual area */
5064 case 'c': if (VIsual_active)
5065 nv_operator(cap);
5066 else
5067 closeFold(curwin->w_cursor.lnum, cap->count1);
5068 curwin->w_p_fen = TRUE;
5069 break;
5070
5071 /* "zC": close fold recursively */
5072 case 'C': if (VIsual_active)
5073 nv_operator(cap);
5074 else
5075 closeFoldRecurse(curwin->w_cursor.lnum);
5076 curwin->w_p_fen = TRUE;
5077 break;
5078
5079 /* "zv": open folds at the cursor */
5080 case 'v': foldOpenCursor();
5081 break;
5082
5083 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5084 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005085 curwin->w_foldinvalid = TRUE; /* recompute folds */
5086 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 foldOpenCursor();
5088 break;
5089
5090 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5091 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005092 curwin->w_foldinvalid = TRUE; /* recompute folds */
5093 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 break;
5095
5096 /* "zm": fold more */
5097 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005098 {
5099 curwin->w_p_fdl -= cap->count1;
5100 if (curwin->w_p_fdl < 0)
5101 curwin->w_p_fdl = 0;
5102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 old_fdl = -1; /* force an update */
5104 curwin->w_p_fen = TRUE;
5105 break;
5106
5107 /* "zM": close all folds */
5108 case 'M': curwin->w_p_fdl = 0;
5109 old_fdl = -1; /* force an update */
5110 curwin->w_p_fen = TRUE;
5111 break;
5112
5113 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005114 case 'r': curwin->w_p_fdl += cap->count1;
5115 {
5116 int d = getDeepestNesting();
5117
5118 if (curwin->w_p_fdl >= d)
5119 curwin->w_p_fdl = d;
5120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 break;
5122
5123 /* "zR": open all folds */
5124 case 'R': curwin->w_p_fdl = getDeepestNesting();
5125 old_fdl = -1; /* force an update */
5126 break;
5127
5128 case 'j': /* "zj" move to next fold downwards */
5129 case 'k': /* "zk" move to next fold upwards */
5130 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5131 cap->count1) == FAIL)
5132 clearopbeep(cap->oap);
5133 break;
5134
5135#endif /* FEAT_FOLDING */
5136
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005137#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005138 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5139 ++no_mapping;
5140 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005141 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005142 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005143 --no_mapping;
5144 --allow_keys;
5145#ifdef FEAT_CMDL_INFO
5146 (void)add_to_showcmd(nchar);
5147#endif
5148 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5149 {
5150 clearopbeep(cap->oap);
5151 break;
5152 }
5153 undo = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005154 /* FALLTHROUGH */
Bram Moolenaard0131a82006-03-04 21:46:13 +00005155
Bram Moolenaarb765d632005-06-07 21:00:02 +00005156 case 'g': /* "zg": add good word to word list */
5157 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005158 case 'G': /* "zG": add good word to temp word list */
5159 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005160 {
5161 char_u *ptr = NULL;
5162 int len;
5163
5164 if (checkclearop(cap->oap))
5165 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005166 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5167 == FAIL)
5168 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005169 if (ptr == NULL)
5170 {
5171 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005172
Bram Moolenaar134bf072013-09-25 18:54:24 +02005173 /* Find bad word under the cursor. When 'spell' is
5174 * off this fails and find_ident_under_cursor() is
5175 * used below. */
5176 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005177 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005178 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005179 if (len != 0 && curwin->w_cursor.col <= pos.col)
5180 ptr = ml_get_pos(&curwin->w_cursor);
5181 curwin->w_cursor = pos;
5182 }
5183
Bram Moolenaarb765d632005-06-07 21:00:02 +00005184 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5185 FIND_IDENT)) == 0)
5186 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005187 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005188 (nchar == 'G' || nchar == 'W')
5189 ? 0 : (int)cap->count1,
5190 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005191 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005192 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005193
Bram Moolenaar43abc522005-12-10 20:15:02 +00005194 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005195 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005196 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005197 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005198#endif
5199
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 default: clearopbeep(cap->oap);
5201 }
5202
5203#ifdef FEAT_FOLDING
5204 /* Redraw when 'foldenable' changed */
5205 if (old_fen != curwin->w_p_fen)
5206 {
5207# ifdef FEAT_DIFF
5208 win_T *wp;
5209
5210 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5211 {
5212 /* Adjust 'foldenable' in diff-synced windows. */
5213 FOR_ALL_WINDOWS(wp)
5214 {
5215 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5216 {
5217 wp->w_p_fen = curwin->w_p_fen;
5218 changed_window_setting_win(wp);
5219 }
5220 }
5221 }
5222# endif
5223 changed_window_setting();
5224 }
5225
5226 /* Redraw when 'foldlevel' changed. */
5227 if (old_fdl != curwin->w_p_fdl)
5228 newFoldLevel();
5229#endif
5230}
5231
5232#ifdef FEAT_GUI
5233/*
5234 * Vertical scrollbar movement.
5235 */
5236 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005237nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238{
5239 if (cap->oap->op_type != OP_NOP)
5240 clearopbeep(cap->oap);
5241
5242 /* Even if an operator was pending, we still want to scroll */
5243 gui_do_scroll();
5244}
5245
5246/*
5247 * Horizontal scrollbar movement.
5248 */
5249 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005250nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251{
5252 if (cap->oap->op_type != OP_NOP)
5253 clearopbeep(cap->oap);
5254
5255 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005256 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257}
5258#endif
5259
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005260#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005261/*
5262 * Click in GUI tab.
5263 */
5264 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005265nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005266{
5267 if (cap->oap->op_type != OP_NOP)
5268 clearopbeep(cap->oap);
5269
5270 /* Even if an operator was pending, we still want to jump tabs. */
5271 goto_tabpage(current_tab);
5272}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005273
5274/*
5275 * Selected item in tab line menu.
5276 */
5277 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005278nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005279{
5280 if (cap->oap->op_type != OP_NOP)
5281 clearopbeep(cap->oap);
5282
5283 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005284 handle_tabmenu();
5285}
5286
5287/*
5288 * Handle selecting an item of the GUI tab line menu.
5289 * Used in Normal and Insert mode.
5290 */
5291 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005292handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005293{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005294 switch (current_tabmenu)
5295 {
5296 case TABLINE_MENU_CLOSE:
5297 if (current_tab == 0)
5298 do_cmdline_cmd((char_u *)"tabclose");
5299 else
5300 {
5301 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5302 current_tab);
5303 do_cmdline_cmd(IObuff);
5304 }
5305 break;
5306
5307 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005308 if (current_tab == 0)
5309 do_cmdline_cmd((char_u *)"$tabnew");
5310 else
5311 {
5312 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5313 current_tab - 1);
5314 do_cmdline_cmd(IObuff);
5315 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005316 break;
5317
5318 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005319 if (current_tab == 0)
5320 do_cmdline_cmd((char_u *)"browse $tabnew");
5321 else
5322 {
5323 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5324 current_tab - 1);
5325 do_cmdline_cmd(IObuff);
5326 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005327 break;
5328 }
5329}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005330#endif
5331
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332/*
5333 * "Q" command.
5334 */
5335 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005336nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337{
5338 /*
5339 * Ignore 'Q' in Visual mode, just give a beep.
5340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005342 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005343 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 do_exmode(FALSE);
5345}
5346
5347/*
5348 * Handle a ":" command.
5349 */
5350 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005351nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352{
5353 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005354 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 if (VIsual_active)
5357 nv_operator(cap);
5358 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 {
5360 if (cap->oap->op_type != OP_NOP)
5361 {
5362 /* Using ":" as a movement is characterwise exclusive. */
5363 cap->oap->motion_type = MCHAR;
5364 cap->oap->inclusive = FALSE;
5365 }
5366 else if (cap->count0)
5367 {
5368 /* translate "count:" into ":.,.+(count - 1)" */
5369 stuffcharReadbuff('.');
5370 if (cap->count0 > 1)
5371 {
5372 stuffReadbuff((char_u *)",.+");
5373 stuffnumReadbuff((long)cap->count0 - 1L);
5374 }
5375 }
5376
5377 /* When typing, don't type below an old message */
5378 if (KeyTyped)
5379 compute_cmdrow();
5380
5381 old_p_im = p_im;
5382
5383 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005384 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5386
5387 /* If 'insertmode' changed, enter or exit Insert mode */
5388 if (p_im != old_p_im)
5389 {
5390 if (p_im)
5391 restart_edit = 'i';
5392 else
5393 restart_edit = 0;
5394 }
5395
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005396 if (cmd_result == FAIL)
5397 /* The Ex command failed, do not execute the operator. */
5398 clearop(cap->oap);
5399 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5401 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005402 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5403 || did_emsg
5404 ))
5405 /* The start of the operator has become invalid by the Ex command.
5406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 clearopbeep(cap->oap);
5408 }
5409}
5410
5411/*
5412 * Handle CTRL-G command.
5413 */
5414 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005415nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 if (VIsual_active) /* toggle Selection/Visual mode */
5418 {
5419 VIsual_select = !VIsual_select;
5420 showmode();
5421 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005422 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 /* print full name if count given or :cd used */
5424 fileinfo((int)cap->count0, FALSE, TRUE);
5425}
5426
5427/*
5428 * Handle CTRL-H <Backspace> command.
5429 */
5430 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005431nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 if (VIsual_active && VIsual_select)
5434 {
5435 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5436 v_visop(cap);
5437 }
5438 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 nv_left(cap);
5440}
5441
5442/*
5443 * CTRL-L: clear screen and redraw.
5444 */
5445 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005446nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447{
5448 if (!checkclearop(cap->oap))
5449 {
5450#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5451 /*
5452 * Right now, the BeBox doesn't seem to have an easy way to detect
5453 * window resizing, so we cheat and make the user detect it
5454 * manually with CTRL-L instead
5455 */
5456 ui_get_shellsize();
5457#endif
5458#ifdef FEAT_SYN_HL
5459 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005460 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02005461# ifdef FEAT_RELTIME
5462 {
5463 win_T *wp;
5464
5465 FOR_ALL_WINDOWS(wp)
5466 wp->w_s->b_syn_slow = FALSE;
5467 }
5468# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469#endif
5470 redraw_later(CLEAR);
5471 }
5472}
5473
5474/*
5475 * CTRL-O: In Select mode: switch to Visual mode for one command.
5476 * Otherwise: Go to older pcmark.
5477 */
5478 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005479nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 if (VIsual_active && VIsual_select)
5482 {
5483 VIsual_select = FALSE;
5484 showmode();
5485 restart_VIsual_select = 2; /* restart Select mode later */
5486 }
5487 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 {
5489 cap->count1 = -cap->count1;
5490 nv_pcmark(cap);
5491 }
5492}
5493
5494/*
5495 * CTRL-^ command, short for ":e #"
5496 */
5497 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005498nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499{
5500 if (!checkclearopq(cap->oap))
5501 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5502 GETF_SETMARK|GETF_ALT, FALSE);
5503}
5504
5505/*
5506 * "Z" commands.
5507 */
5508 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005509nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510{
5511 if (!checkclearopq(cap->oap))
5512 {
5513 switch (cap->nchar)
5514 {
5515 /* "ZZ": equivalent to ":x". */
5516 case 'Z': do_cmdline_cmd((char_u *)"x");
5517 break;
5518
5519 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5520 case 'Q': do_cmdline_cmd((char_u *)"q!");
5521 break;
5522
5523 default: clearopbeep(cap->oap);
5524 }
5525 }
5526}
5527
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528/*
5529 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5530 */
5531 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005532do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533{
5534 oparg_T oa;
5535 cmdarg_T ca;
5536
5537 clear_oparg(&oa);
5538 vim_memset(&ca, 0, sizeof(ca));
5539 ca.oap = &oa;
5540 ca.cmdchar = c1;
5541 ca.nchar = c2;
5542 nv_ident(&ca);
5543}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544
5545/*
5546 * Handle the commands that use the word under the cursor.
5547 * [g] CTRL-] :ta to current identifier
5548 * [g] 'K' run program for current identifier
5549 * [g] '*' / to current identifier or string
5550 * [g] '#' ? to current identifier or string
5551 * g ']' :tselect for current identifier
5552 */
5553 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005554nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555{
5556 char_u *ptr = NULL;
5557 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005558 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005559 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 char_u *p;
5561 char_u *kp; /* value of 'keywordprg' */
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005562 int kp_help; /* 'keywordprg' is ":he" */
5563 int kp_ex; /* 'keywordprg' starts with ":" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 int n = 0; /* init for GCC */
5565 int cmdchar;
5566 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005567 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 char_u *aux_ptr;
5569 int isman;
5570 int isman_s;
5571
5572 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5573 {
5574 cmdchar = cap->nchar;
5575 g_cmd = TRUE;
5576 }
5577 else
5578 {
5579 cmdchar = cap->cmdchar;
5580 g_cmd = FALSE;
5581 }
5582
5583 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5584 cmdchar = '#';
5585
5586 /*
5587 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5588 */
5589 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5592 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 if (checkclearopq(cap->oap))
5594 return;
5595 }
5596
5597 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5598 (cmdchar == '*' || cmdchar == '#')
5599 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5600 {
5601 clearop(cap->oap);
5602 return;
5603 }
5604
5605 /* Allocate buffer to put the command in. Inserting backslashes can
5606 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5607 * and some numbers. */
5608 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5609 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5610 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02005611 if (kp_help && *skipwhite(ptr) == NUL)
5612 {
5613 EMSG(_(e_noident)); /* found white space only */
5614 return;
5615 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005616 kp_ex = (*kp == ':');
5617 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
5618 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 if (buf == NULL)
5620 return;
5621 buf[0] = NUL;
5622
5623 switch (cmdchar)
5624 {
5625 case '*':
5626 case '#':
5627 /*
5628 * Put cursor at start of word, makes search skip the word
5629 * under the cursor.
5630 * Call setpcmark() first, so "*``" puts the cursor back where
5631 * it was.
5632 */
5633 setpcmark();
5634 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5635
5636 if (!g_cmd && vim_iswordp(ptr))
5637 STRCPY(buf, "\\<");
5638 no_smartcase = TRUE; /* don't use 'smartcase' now */
5639 break;
5640
5641 case 'K':
5642 if (kp_help)
5643 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005644 else if (kp_ex)
5645 {
5646 if (cap->count0 != 0)
5647 vim_snprintf((char *)buf, buflen, "%s %ld",
5648 kp, cap->count0);
5649 else
5650 STRCPY(buf, kp);
5651 STRCAT(buf, " ");
5652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 else
5654 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005655 /* An external command will probably use an argument starting
5656 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005657 while (*ptr == '-' && n > 0)
5658 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005659 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005660 --n;
5661 }
5662 if (n == 0)
5663 {
5664 EMSG(_(e_noident)); /* found dashes only */
5665 vim_free(buf);
5666 return;
5667 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005668
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 /* When a count is given, turn it into a range. Is this
5670 * really what we want? */
5671 isman = (STRCMP(kp, "man") == 0);
5672 isman_s = (STRCMP(kp, "man -s") == 0);
5673 if (cap->count0 != 0 && !(isman || isman_s))
5674 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5675
5676 STRCAT(buf, "! ");
5677 if (cap->count0 == 0 && isman_s)
5678 STRCAT(buf, "man");
5679 else
5680 STRCAT(buf, kp);
5681 STRCAT(buf, " ");
5682 if (cap->count0 != 0 && (isman || isman_s))
5683 {
5684 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5685 STRCAT(buf, " ");
5686 }
5687 }
5688 break;
5689
5690 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005691 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692#ifdef FEAT_CSCOPE
5693 if (p_cst)
5694 STRCPY(buf, "cstag ");
5695 else
5696#endif
5697 STRCPY(buf, "ts ");
5698 break;
5699
5700 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005701 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 if (curbuf->b_help)
5703 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005705 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005706 if (g_cmd)
5707 STRCPY(buf, "tj ");
5708 else
5709 sprintf((char *)buf, "%ldta ", cap->count0);
5710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 }
5712
5713 /*
5714 * Now grab the chars in the identifier
5715 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005716 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005718 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01005719 if (kp_ex)
5720 /* Escape the argument properly for an Ex command */
5721 p = vim_strsave_fnameescape(ptr, FALSE);
5722 else
5723 /* Escape the argument properly for a shell command */
5724 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005725 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005726 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005728 vim_free(buf);
5729 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005731 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5732 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005733 {
5734 vim_free(buf);
5735 vim_free(p);
5736 return;
5737 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005738 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005739 STRCAT(buf, p);
5740 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005742 else
5743 {
5744 if (cmdchar == '*')
5745 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5746 else if (cmdchar == '#')
5747 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005748 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005749 {
5750 if (curbuf->b_help)
5751 /* ":help" handles unescaped argument */
5752 aux_ptr = (char_u *)"";
5753 else
5754 aux_ptr = (char_u *)"\\|\"\n[";
5755 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005756 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005757 aux_ptr = (char_u *)"\\|\"\n*?[";
5758
5759 p = buf + STRLEN(buf);
5760 while (n-- > 0)
5761 {
5762 /* put a backslash before \ and some others */
5763 if (vim_strchr(aux_ptr, *ptr) != NULL)
5764 *p++ = '\\';
5765#ifdef FEAT_MBYTE
5766 /* When current byte is a part of multibyte character, copy all
5767 * bytes of that character. */
5768 if (has_mbyte)
5769 {
5770 int i;
5771 int len = (*mb_ptr2len)(ptr) - 1;
5772
5773 for (i = 0; i < len && n >= 1; ++i, --n)
5774 *p++ = *ptr++;
5775 }
5776#endif
5777 *p++ = *ptr++;
5778 }
5779 *p = NUL;
5780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781
5782 /*
5783 * Execute the command.
5784 */
5785 if (cmdchar == '*' || cmdchar == '#')
5786 {
5787 if (!g_cmd && (
5788#ifdef FEAT_MBYTE
5789 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5790#endif
5791 vim_iswordc(ptr[-1])))
5792 STRCAT(buf, "\\>");
5793#ifdef FEAT_CMDHIST
5794 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005795 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5797#endif
Bram Moolenaar46539112015-02-17 15:43:57 +01005798 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 }
5800 else
5801 do_cmdline_cmd(buf);
5802
5803 vim_free(buf);
5804}
5805
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806/*
5807 * Get visually selected text, within one line only.
5808 * Returns FAIL if more than one line selected.
5809 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005810 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005811get_visual_text(
5812 cmdarg_T *cap,
5813 char_u **pp, /* return: start of selected text */
5814 int *lenp) /* return: length of selected text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815{
5816 if (VIsual_mode != 'V')
5817 unadjust_for_sel();
5818 if (VIsual.lnum != curwin->w_cursor.lnum)
5819 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005820 if (cap != NULL)
5821 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 return FAIL;
5823 }
5824 if (VIsual_mode == 'V')
5825 {
5826 *pp = ml_get_curline();
5827 *lenp = (int)STRLEN(*pp);
5828 }
5829 else
5830 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005831 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 {
5833 *pp = ml_get_pos(&curwin->w_cursor);
5834 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5835 }
5836 else
5837 {
5838 *pp = ml_get_pos(&VIsual);
5839 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5840 }
5841#ifdef FEAT_MBYTE
5842 if (has_mbyte)
5843 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005844 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845#endif
5846 }
5847 reset_VIsual_and_resel();
5848 return OK;
5849}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850
5851/*
5852 * CTRL-T: backwards in tag stack
5853 */
5854 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005855nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856{
5857 if (!checkclearopq(cap->oap))
5858 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5859}
5860
5861/*
5862 * Handle scrolling command 'H', 'L' and 'M'.
5863 */
5864 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005865nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866{
5867 int used = 0;
5868 long n;
5869#ifdef FEAT_FOLDING
5870 linenr_T lnum;
5871#endif
5872 int half;
5873
5874 cap->oap->motion_type = MLINE;
5875 setpcmark();
5876
5877 if (cap->cmdchar == 'L')
5878 {
5879 validate_botline(); /* make sure curwin->w_botline is valid */
5880 curwin->w_cursor.lnum = curwin->w_botline - 1;
5881 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5882 curwin->w_cursor.lnum = 1;
5883 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005884 {
5885#ifdef FEAT_FOLDING
5886 if (hasAnyFolding(curwin))
5887 {
5888 /* Count a fold for one screen line. */
5889 for (n = cap->count1 - 1; n > 0
5890 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5891 {
5892 (void)hasFolding(curwin->w_cursor.lnum,
5893 &curwin->w_cursor.lnum, NULL);
5894 --curwin->w_cursor.lnum;
5895 }
5896 }
5897 else
5898#endif
5899 curwin->w_cursor.lnum -= cap->count1 - 1;
5900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 }
5902 else
5903 {
5904 if (cap->cmdchar == 'M')
5905 {
5906#ifdef FEAT_DIFF
5907 /* Don't count filler lines above the window. */
5908 used -= diff_check_fill(curwin, curwin->w_topline)
5909 - curwin->w_topfill;
5910#endif
5911 validate_botline(); /* make sure w_empty_rows is valid */
5912 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5913 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5914 {
5915#ifdef FEAT_DIFF
5916 /* Count half he number of filler lines to be "below this
5917 * line" and half to be "above the next line". */
5918 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5919 + n) / 2 >= half)
5920 {
5921 --n;
5922 break;
5923 }
5924#endif
5925 used += plines(curwin->w_topline + n);
5926 if (used >= half)
5927 break;
5928#ifdef FEAT_FOLDING
5929 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5930 n = lnum - curwin->w_topline;
5931#endif
5932 }
5933 if (n > 0 && used > curwin->w_height)
5934 --n;
5935 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005936 else /* (cap->cmdchar == 'H') */
5937 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005939#ifdef FEAT_FOLDING
5940 if (hasAnyFolding(curwin))
5941 {
5942 /* Count a fold for one screen line. */
5943 lnum = curwin->w_topline;
5944 while (n-- > 0 && lnum < curwin->w_botline - 1)
5945 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02005946 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005947 ++lnum;
5948 }
5949 n = lnum - curwin->w_topline;
5950 }
5951#endif
5952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 curwin->w_cursor.lnum = curwin->w_topline + n;
5954 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5955 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5956 }
5957
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02005958 /* Correct for 'so', except when an operator is pending. */
5959 if (cap->oap->op_type == OP_NOP)
5960 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 beginline(BL_SOL | BL_FIX);
5962}
5963
5964/*
5965 * Cursor right commands.
5966 */
5967 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005968nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969{
5970 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005971 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005973 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5974 {
5975 /* <C-Right> and <S-Right> move a word or WORD right */
5976 if (mod_mask & MOD_MASK_CTRL)
5977 cap->arg = TRUE;
5978 nv_wordcmd(cap);
5979 return;
5980 }
5981
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 cap->oap->motion_type = MCHAR;
5983 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005984 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005986#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005988 * In virtual edit mode, there's no such thing as "past_line", as lines
5989 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 */
5991 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005992 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993#endif
5994
5995 for (n = cap->count1; n > 0; --n)
5996 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005997 if ((!past_line && oneright() == FAIL)
5998 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005999 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 {
6001 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006002 * <Space> wraps to next line if 'whichwrap' has 's'.
6003 * 'l' wraps to next line if 'whichwrap' has 'l'.
6004 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 */
6006 if ( ((cap->cmdchar == ' '
6007 && vim_strchr(p_ww, 's') != NULL)
6008 || (cap->cmdchar == 'l'
6009 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006010 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 && vim_strchr(p_ww, '>') != NULL))
6012 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6013 {
6014 /* When deleting we also count the NL as a character.
6015 * Set cap->oap->inclusive when last char in the line is
6016 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006017 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006019 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 cap->oap->inclusive = TRUE;
6021 else
6022 {
6023 ++curwin->w_cursor.lnum;
6024 curwin->w_cursor.col = 0;
6025#ifdef FEAT_VIRTUALEDIT
6026 curwin->w_cursor.coladd = 0;
6027#endif
6028 curwin->w_set_curswant = TRUE;
6029 cap->oap->inclusive = FALSE;
6030 }
6031 continue;
6032 }
6033 if (cap->oap->op_type == OP_NOP)
6034 {
6035 /* Only beep and flush if not moved at all */
6036 if (n == cap->count1)
6037 beep_flush();
6038 }
6039 else
6040 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006041 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 cap->oap->inclusive = TRUE;
6043 }
6044 break;
6045 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006046 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 {
6048 curwin->w_set_curswant = TRUE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006049#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 if (virtual_active())
6051 oneright();
6052 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006055#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 if (has_mbyte)
6057 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006058 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 ++curwin->w_cursor.col;
6062 }
6063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 }
6065#ifdef FEAT_FOLDING
6066 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6067 && cap->oap->op_type == OP_NOP)
6068 foldOpenCursor();
6069#endif
6070}
6071
6072/*
6073 * Cursor left commands.
6074 *
6075 * Returns TRUE when operator end should not be adjusted.
6076 */
6077 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006078nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079{
6080 long n;
6081
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006082 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6083 {
6084 /* <C-Left> and <S-Left> move a word or WORD left */
6085 if (mod_mask & MOD_MASK_CTRL)
6086 cap->arg = 1;
6087 nv_bck_word(cap);
6088 return;
6089 }
6090
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 cap->oap->motion_type = MCHAR;
6092 cap->oap->inclusive = FALSE;
6093 for (n = cap->count1; n > 0; --n)
6094 {
6095 if (oneleft() == FAIL)
6096 {
6097 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6098 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6099 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6100 */
6101 if ( (((cap->cmdchar == K_BS
6102 || cap->cmdchar == Ctrl_H)
6103 && vim_strchr(p_ww, 'b') != NULL)
6104 || (cap->cmdchar == 'h'
6105 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006106 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 && vim_strchr(p_ww, '<') != NULL))
6108 && curwin->w_cursor.lnum > 1)
6109 {
6110 --(curwin->w_cursor.lnum);
6111 coladvance((colnr_T)MAXCOL);
6112 curwin->w_set_curswant = TRUE;
6113
6114 /* When the NL before the first char has to be deleted we
6115 * put the cursor on the NUL after the previous line.
6116 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006117 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 if ( (cap->oap->op_type == OP_DELETE
6119 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006120 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006122 char_u *cp = ml_get_cursor();
6123
6124 if (*cp != NUL)
6125 {
6126#ifdef FEAT_MBYTE
6127 if (has_mbyte)
6128 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6129 else
6130#endif
6131 ++curwin->w_cursor.col;
6132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 cap->retval |= CA_NO_ADJ_OP_END;
6134 }
6135 continue;
6136 }
6137 /* Only beep and flush if not moved at all */
6138 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6139 beep_flush();
6140 break;
6141 }
6142 }
6143#ifdef FEAT_FOLDING
6144 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6145 && cap->oap->op_type == OP_NOP)
6146 foldOpenCursor();
6147#endif
6148}
6149
6150/*
6151 * Cursor up commands.
6152 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6153 */
6154 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006155nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006157 if (mod_mask & MOD_MASK_SHIFT)
6158 {
6159 /* <S-Up> is page up */
6160 cap->arg = BACKWARD;
6161 nv_page(cap);
6162 }
6163 else
6164 {
6165 cap->oap->motion_type = MLINE;
6166 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6167 clearopbeep(cap->oap);
6168 else if (cap->arg)
6169 beginline(BL_WHITE | BL_FIX);
6170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171}
6172
6173/*
6174 * Cursor down commands.
6175 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6176 */
6177 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02006178nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006180 if (mod_mask & MOD_MASK_SHIFT)
6181 {
6182 /* <S-Down> is page down */
6183 cap->arg = FORWARD;
6184 nv_page(cap);
6185 }
6186 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006187#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006189 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar74a47162017-02-26 19:09:05 +01006190 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006191 if (curwin->w_llist_ref == NULL)
6192 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6193 else
6194 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar74a47162017-02-26 19:09:05 +01006195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 else
6197#endif
6198 {
6199#ifdef FEAT_CMDWIN
6200 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006201 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 cmdwin_result = CAR;
6203 else
6204#endif
6205 {
6206 cap->oap->motion_type = MLINE;
6207 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6208 clearopbeep(cap->oap);
6209 else if (cap->arg)
6210 beginline(BL_WHITE | BL_FIX);
6211 }
6212 }
6213}
6214
6215#ifdef FEAT_SEARCHPATH
6216/*
6217 * Grab the file name under the cursor and edit it.
6218 */
6219 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006220nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221{
6222 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006223 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006225 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 {
6227 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006228 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 return;
6230 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006231#ifdef FEAT_AUTOCMD
6232 if (curbuf_locked())
6233 {
6234 clearop(cap->oap);
6235 return;
6236 }
6237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006239 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240
6241 if (ptr != NULL)
6242 {
6243 /* do autowrite if necessary */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006244 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02006245 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006247 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006248 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006249 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006250 {
6251 curwin->w_cursor.lnum = lnum;
6252 check_cursor_lnum();
6253 beginline(BL_SOL | BL_FIX);
6254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 vim_free(ptr);
6256 }
6257 else
6258 clearop(cap->oap);
6259}
6260#endif
6261
6262/*
6263 * <End> command: to end of current line or last line.
6264 */
6265 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006266nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006268 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006270 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 nv_goto(cap);
6272 cap->count1 = 1; /* to end of current line */
6273 }
6274 nv_dollar(cap);
6275}
6276
6277/*
6278 * Handle the "$" command.
6279 */
6280 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006281nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282{
6283 cap->oap->motion_type = MCHAR;
6284 cap->oap->inclusive = TRUE;
6285#ifdef FEAT_VIRTUALEDIT
6286 /* In virtual mode when off the edge of a line and an operator
6287 * is pending (whew!) keep the cursor where it is.
6288 * Otherwise, send it to the end of the line. */
6289 if (!virtual_active() || gchar_cursor() != NUL
6290 || cap->oap->op_type == OP_NOP)
6291#endif
6292 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6293 if (cursor_down((long)(cap->count1 - 1),
6294 cap->oap->op_type == OP_NOP) == FAIL)
6295 clearopbeep(cap->oap);
6296#ifdef FEAT_FOLDING
6297 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6298 foldOpenCursor();
6299#endif
6300}
6301
6302/*
6303 * Implementation of '?' and '/' commands.
6304 * If cap->arg is TRUE don't set PC mark.
6305 */
6306 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006307nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308{
6309 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02006310 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311
6312 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6313 {
6314 /* Translate "g??" to "g?g?" */
6315 cap->cmdchar = 'g';
6316 cap->nchar = '?';
6317 nv_operator(cap);
6318 return;
6319 }
6320
Bram Moolenaardda933d2016-09-03 21:04:58 +02006321 /* When using 'incsearch' the cursor may be moved to set a different search
6322 * start position. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6324
6325 if (cap->searchbuf == NULL)
6326 {
6327 clearop(oap);
6328 return;
6329 }
6330
Bram Moolenaar46539112015-02-17 15:43:57 +01006331 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006332 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaardda933d2016-09-03 21:04:58 +02006333 ? 0 : SEARCH_MARK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334}
6335
6336/*
6337 * Handle "N" and "n" commands.
6338 * cap->arg is SEARCH_REV for "N", 0 for "n".
6339 */
6340 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006341nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342{
Bram Moolenaar46539112015-02-17 15:43:57 +01006343 pos_T old = curwin->w_cursor;
6344 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6345
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006346 if (i == 1 && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01006347 {
6348 /* Avoid getting stuck on the current cursor position, which can
6349 * happen when an offset is given and the cursor is on the last char
6350 * in the buffer: Repeat with count + 1. */
6351 cap->count1 += 1;
6352 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6353 cap->count1 -= 1;
6354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355}
6356
6357/*
6358 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6359 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01006360 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 */
Bram Moolenaar46539112015-02-17 15:43:57 +01006362 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006363normal_search(
6364 cmdarg_T *cap,
6365 int dir,
6366 char_u *pat,
6367 int opt) /* extra flags for do_search() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368{
6369 int i;
6370
6371 cap->oap->motion_type = MCHAR;
6372 cap->oap->inclusive = FALSE;
6373 cap->oap->use_reg_one = TRUE;
6374 curwin->w_set_curswant = TRUE;
6375
6376 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006377 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 if (i == 0)
6379 clearop(cap->oap);
6380 else
6381 {
6382 if (i == 2)
6383 cap->oap->motion_type = MLINE;
6384#ifdef FEAT_VIRTUALEDIT
6385 curwin->w_cursor.coladd = 0;
6386#endif
6387#ifdef FEAT_FOLDING
6388 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6389 foldOpenCursor();
6390#endif
6391 }
6392
6393 /* "/$" will put the cursor after the end of the line, may need to
6394 * correct that here */
6395 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01006396 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397}
6398
6399/*
6400 * Character search commands.
6401 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6402 * ',' and FALSE for ';'.
6403 * cap->nchar is NUL for ',' and ';' (repeat the search)
6404 */
6405 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006406nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407{
6408 int t_cmd;
6409
6410 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6411 t_cmd = TRUE;
6412 else
6413 t_cmd = FALSE;
6414
6415 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6417 clearopbeep(cap->oap);
6418 else
6419 {
6420 curwin->w_set_curswant = TRUE;
6421#ifdef FEAT_VIRTUALEDIT
6422 /* Include a Tab for "tx" and for "dfx". */
6423 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6424 && (t_cmd || cap->oap->op_type != OP_NOP))
6425 {
6426 colnr_T scol, ecol;
6427
6428 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6429 curwin->w_cursor.coladd = ecol - scol;
6430 }
6431 else
6432 curwin->w_cursor.coladd = 0;
6433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435#ifdef FEAT_FOLDING
6436 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6437 foldOpenCursor();
6438#endif
6439 }
6440}
6441
6442/*
6443 * "[" and "]" commands.
6444 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6445 */
6446 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006447nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006449 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 pos_T prev_pos;
6451 pos_T *pos = NULL; /* init for GCC */
6452 pos_T old_pos; /* cursor position before command */
6453 int flag;
6454 long n;
6455 int findc;
6456 int c;
6457
6458 cap->oap->motion_type = MCHAR;
6459 cap->oap->inclusive = FALSE;
6460 old_pos = curwin->w_cursor;
6461#ifdef FEAT_VIRTUALEDIT
6462 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6463#endif
6464
6465#ifdef FEAT_SEARCHPATH
6466 /*
6467 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6468 */
6469 if (cap->nchar == 'f')
6470 nv_gotofile(cap);
6471 else
6472#endif
6473
6474#ifdef FEAT_FIND_ID
6475 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006476 * Find the occurrence(s) of the identifier or define under cursor
6477 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 *
6479 * search list jump
6480 * fwd bwd fwd bwd fwd bwd
6481 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6482 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6483 */
6484 if (vim_strchr((char_u *)
6485#ifdef EBCDIC
6486 "iI\005dD\067",
6487#else
6488 "iI\011dD\004",
6489#endif
6490 cap->nchar) != NULL)
6491 {
6492 char_u *ptr;
6493 int len;
6494
6495 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6496 clearop(cap->oap);
6497 else
6498 {
6499 find_pattern_in_path(ptr, 0, len, TRUE,
6500 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6501 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6502 cap->count1,
6503 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6504 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6505 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6506 (linenr_T)MAXLNUM);
6507 curwin->w_set_curswant = TRUE;
6508 }
6509 }
6510 else
6511#endif
6512
6513 /*
6514 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6515 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6516 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6517 * "[m" or "]m" search for prev/next start of (Java) method.
6518 * "[M" or "]M" search for prev/next end of (Java) method.
6519 */
6520 if ( (cap->cmdchar == '['
6521 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6522 || (cap->cmdchar == ']'
6523 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6524 {
6525 if (cap->nchar == '*')
6526 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 prev_pos.lnum = 0;
6528 if (cap->nchar == 'm' || cap->nchar == 'M')
6529 {
6530 if (cap->cmdchar == '[')
6531 findc = '{';
6532 else
6533 findc = '}';
6534 n = 9999;
6535 }
6536 else
6537 {
6538 findc = cap->nchar;
6539 n = cap->count1;
6540 }
6541 for ( ; n > 0; --n)
6542 {
6543 if ((pos = findmatchlimit(cap->oap, findc,
6544 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6545 {
6546 if (new_pos.lnum == 0) /* nothing found */
6547 {
6548 if (cap->nchar != 'm' && cap->nchar != 'M')
6549 clearopbeep(cap->oap);
6550 }
6551 else
6552 pos = &new_pos; /* use last one found */
6553 break;
6554 }
6555 prev_pos = new_pos;
6556 curwin->w_cursor = *pos;
6557 new_pos = *pos;
6558 }
6559 curwin->w_cursor = old_pos;
6560
6561 /*
6562 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6563 * brought us to the match for "[m" and "]M" when inside a method.
6564 * Try finding the '{' or '}' we want to be at.
6565 * Also repeat for the given count.
6566 */
6567 if (cap->nchar == 'm' || cap->nchar == 'M')
6568 {
6569 /* norm is TRUE for "]M" and "[m" */
6570 int norm = ((findc == '{') == (cap->nchar == 'm'));
6571
6572 n = cap->count1;
6573 /* found a match: we were inside a method */
6574 if (prev_pos.lnum != 0)
6575 {
6576 pos = &prev_pos;
6577 curwin->w_cursor = prev_pos;
6578 if (norm)
6579 --n;
6580 }
6581 else
6582 pos = NULL;
6583 while (n > 0)
6584 {
6585 for (;;)
6586 {
6587 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6588 {
6589 /* if not found anything, that's an error */
6590 if (pos == NULL)
6591 clearopbeep(cap->oap);
6592 n = 0;
6593 break;
6594 }
6595 c = gchar_cursor();
6596 if (c == '{' || c == '}')
6597 {
6598 /* Must have found end/start of class: use it.
6599 * Or found the place to be at. */
6600 if ((c == findc && norm) || (n == 1 && !norm))
6601 {
6602 new_pos = curwin->w_cursor;
6603 pos = &new_pos;
6604 n = 0;
6605 }
6606 /* if no match found at all, we started outside of the
6607 * class and we're inside now. Just go on. */
6608 else if (new_pos.lnum == 0)
6609 {
6610 new_pos = curwin->w_cursor;
6611 pos = &new_pos;
6612 }
6613 /* found start/end of other method: go to match */
6614 else if ((pos = findmatchlimit(cap->oap, findc,
6615 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6616 0)) == NULL)
6617 n = 0;
6618 else
6619 curwin->w_cursor = *pos;
6620 break;
6621 }
6622 }
6623 --n;
6624 }
6625 curwin->w_cursor = old_pos;
6626 if (pos == NULL && new_pos.lnum != 0)
6627 clearopbeep(cap->oap);
6628 }
6629 if (pos != NULL)
6630 {
6631 setpcmark();
6632 curwin->w_cursor = *pos;
6633 curwin->w_set_curswant = TRUE;
6634#ifdef FEAT_FOLDING
6635 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6636 && cap->oap->op_type == OP_NOP)
6637 foldOpenCursor();
6638#endif
6639 }
6640 }
6641
6642 /*
6643 * "[[", "[]", "]]" and "][": move to start or end of function
6644 */
6645 else if (cap->nchar == '[' || cap->nchar == ']')
6646 {
6647 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6648 flag = '{';
6649 else
6650 flag = '}'; /* "][" or "[]" */
6651
6652 curwin->w_set_curswant = TRUE;
6653 /*
6654 * Imitate strange Vi behaviour: When using "]]" with an operator
6655 * we also stop at '}'.
6656 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006657 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 (cap->oap->op_type != OP_NOP
6659 && cap->arg == FORWARD && flag == '{')))
6660 clearopbeep(cap->oap);
6661 else
6662 {
6663 if (cap->oap->op_type == OP_NOP)
6664 beginline(BL_WHITE | BL_FIX);
6665#ifdef FEAT_FOLDING
6666 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6667 foldOpenCursor();
6668#endif
6669 }
6670 }
6671
6672 /*
6673 * "[p", "[P", "]P" and "]p": put with indent adjustment
6674 */
6675 else if (cap->nchar == 'p' || cap->nchar == 'P')
6676 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006677 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006679 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6680 ? FORWARD : BACKWARD;
6681 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006682 int was_visual = VIsual_active;
6683 int line_count = curbuf->b_ml.ml_line_count;
6684 pos_T start, end;
6685
6686 if (VIsual_active)
6687 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006688 start = LTOREQ_POS(VIsual, curwin->w_cursor)
Bram Moolenaar27bed202014-03-12 17:42:04 +01006689 ? VIsual : curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006690 end = EQUAL_POS(start,VIsual) ? curwin->w_cursor : VIsual;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006691 curwin->w_cursor = (dir == BACKWARD ? start : end);
6692 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006693# ifdef FEAT_CLIPBOARD
6694 adjust_clip_reg(&regname);
6695# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006697
6698 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006699 if (was_visual)
6700 {
6701 VIsual = start;
6702 curwin->w_cursor = end;
6703 if (dir == BACKWARD)
6704 {
6705 /* adjust lines */
6706 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6707 curwin->w_cursor.lnum +=
6708 curbuf->b_ml.ml_line_count - line_count;
6709 }
6710
6711 VIsual_active = TRUE;
6712 if (VIsual_mode == 'V')
6713 {
6714 /* delete visually selected lines */
6715 cap->cmdchar = 'd';
6716 cap->nchar = NUL;
6717 cap->oap->regname = regname;
6718 nv_operator(cap);
6719 do_pending_operator(cap, 0, FALSE);
6720 }
6721 if (VIsual_active)
6722 {
6723 end_visual_mode();
6724 redraw_later(SOME_VALID);
6725 }
6726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 }
6728 }
6729
6730 /*
6731 * "['", "[`", "]'" and "]`": jump to next mark
6732 */
6733 else if (cap->nchar == '\'' || cap->nchar == '`')
6734 {
6735 pos = &curwin->w_cursor;
6736 for (n = cap->count1; n > 0; --n)
6737 {
6738 prev_pos = *pos;
6739 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6740 cap->nchar == '\'');
6741 if (pos == NULL)
6742 break;
6743 }
6744 if (pos == NULL)
6745 pos = &prev_pos;
6746 nv_cursormark(cap, cap->nchar == '\'', pos);
6747 }
6748
6749#ifdef FEAT_MOUSE
6750 /*
6751 * [ or ] followed by a middle mouse click: put selected text with
6752 * indent adjustment. Any other button just does as usual.
6753 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006754 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 {
6756 (void)do_mouse(cap->oap, cap->nchar,
6757 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6758 cap->count1, PUT_FIXINDENT);
6759 }
6760#endif /* FEAT_MOUSE */
6761
6762#ifdef FEAT_FOLDING
6763 /*
6764 * "[z" and "]z": move to start or end of open fold.
6765 */
6766 else if (cap->nchar == 'z')
6767 {
6768 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6769 cap->count1) == FAIL)
6770 clearopbeep(cap->oap);
6771 }
6772#endif
6773
6774#ifdef FEAT_DIFF
6775 /*
6776 * "[c" and "]c": move to next or previous diff-change.
6777 */
6778 else if (cap->nchar == 'c')
6779 {
6780 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6781 cap->count1) == FAIL)
6782 clearopbeep(cap->oap);
6783 }
6784#endif
6785
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006786#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006787 /*
6788 * "[s", "[S", "]s" and "]S": move to next spell error.
6789 */
6790 else if (cap->nchar == 's' || cap->nchar == 'S')
6791 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006792 setpcmark();
6793 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006794 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6795 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006796 {
6797 clearopbeep(cap->oap);
6798 break;
6799 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006800# ifdef FEAT_FOLDING
6801 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6802 foldOpenCursor();
6803# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006804 }
6805#endif
6806
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 /* Not a valid cap->nchar. */
6808 else
6809 clearopbeep(cap->oap);
6810}
6811
6812/*
6813 * Handle Normal mode "%" command.
6814 */
6815 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006816nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817{
6818 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006819#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 linenr_T lnum = curwin->w_cursor.lnum;
6821#endif
6822
6823 cap->oap->inclusive = TRUE;
6824 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6825 {
6826 if (cap->count0 > 100)
6827 clearopbeep(cap->oap);
6828 else
6829 {
6830 cap->oap->motion_type = MLINE;
6831 setpcmark();
6832 /* Round up, so CTRL-G will give same value. Watch out for a
6833 * large line count, the line number must not go negative! */
6834 if (curbuf->b_ml.ml_line_count > 1000000)
6835 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6836 / 100L * cap->count0;
6837 else
6838 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6839 cap->count0 + 99L) / 100L;
6840 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6841 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6842 beginline(BL_SOL | BL_FIX);
6843 }
6844 }
6845 else /* "%" : go to matching paren */
6846 {
6847 cap->oap->motion_type = MCHAR;
6848 cap->oap->use_reg_one = TRUE;
6849 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6850 clearopbeep(cap->oap);
6851 else
6852 {
6853 setpcmark();
6854 curwin->w_cursor = *pos;
6855 curwin->w_set_curswant = TRUE;
6856#ifdef FEAT_VIRTUALEDIT
6857 curwin->w_cursor.coladd = 0;
6858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 }
6861 }
6862#ifdef FEAT_FOLDING
6863 if (cap->oap->op_type == OP_NOP
6864 && lnum != curwin->w_cursor.lnum
6865 && (fdo_flags & FDO_PERCENT)
6866 && KeyTyped)
6867 foldOpenCursor();
6868#endif
6869}
6870
6871/*
6872 * Handle "(" and ")" commands.
6873 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6874 */
6875 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006876nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877{
6878 cap->oap->motion_type = MCHAR;
6879 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006880 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6881 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 curwin->w_set_curswant = TRUE;
6883
6884 if (findsent(cap->arg, cap->count1) == FAIL)
6885 clearopbeep(cap->oap);
6886 else
6887 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006888 /* Don't leave the cursor on the NUL past end of line. */
6889 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890#ifdef FEAT_VIRTUALEDIT
6891 curwin->w_cursor.coladd = 0;
6892#endif
6893#ifdef FEAT_FOLDING
6894 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6895 foldOpenCursor();
6896#endif
6897 }
6898}
6899
6900/*
6901 * "m" command: Mark a position.
6902 */
6903 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006904nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905{
6906 if (!checkclearop(cap->oap))
6907 {
6908 if (setmark(cap->nchar) == FAIL)
6909 clearopbeep(cap->oap);
6910 }
6911}
6912
6913/*
6914 * "{" and "}" commands.
6915 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6916 */
6917 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006918nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919{
6920 cap->oap->motion_type = MCHAR;
6921 cap->oap->inclusive = FALSE;
6922 cap->oap->use_reg_one = TRUE;
6923 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006924 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 clearopbeep(cap->oap);
6926 else
6927 {
6928#ifdef FEAT_VIRTUALEDIT
6929 curwin->w_cursor.coladd = 0;
6930#endif
6931#ifdef FEAT_FOLDING
6932 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6933 foldOpenCursor();
6934#endif
6935 }
6936}
6937
6938/*
6939 * "u" command: Undo or make lower case.
6940 */
6941 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006942nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006944 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 {
6946 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6947 cap->cmdchar = 'g';
6948 cap->nchar = 'u';
6949 nv_operator(cap);
6950 }
6951 else
6952 nv_kundo(cap);
6953}
6954
6955/*
6956 * <Undo> command.
6957 */
6958 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006959nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960{
6961 if (!checkclearopq(cap->oap))
6962 {
6963 u_undo((int)cap->count1);
6964 curwin->w_set_curswant = TRUE;
6965 }
6966}
6967
6968/*
6969 * Handle the "r" command.
6970 */
6971 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006972nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973{
6974 char_u *ptr;
6975 int had_ctrl_v;
6976 long n;
6977
6978 if (checkclearop(cap->oap))
6979 return;
6980
6981 /* get another character */
6982 if (cap->nchar == Ctrl_V)
6983 {
6984 had_ctrl_v = Ctrl_V;
6985 cap->nchar = get_literal();
6986 /* Don't redo a multibyte character with CTRL-V. */
6987 if (cap->nchar > DEL)
6988 had_ctrl_v = NUL;
6989 }
6990 else
6991 had_ctrl_v = NUL;
6992
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006993 /* Abort if the character is a special key. */
6994 if (IS_SPECIAL(cap->nchar))
6995 {
6996 clearopbeep(cap->oap);
6997 return;
6998 }
6999
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 /* Visual mode "r" */
7001 if (VIsual_active)
7002 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00007003 if (got_int)
7004 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01007005 if (had_ctrl_v)
7006 {
7007 if (cap->nchar == '\r')
7008 cap->nchar = -1;
7009 else if (cap->nchar == '\n')
7010 cap->nchar = -2;
7011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 nv_operator(cap);
7013 return;
7014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015
7016#ifdef FEAT_VIRTUALEDIT
7017 /* Break tabs, etc. */
7018 if (virtual_active())
7019 {
7020 if (u_save_cursor() == FAIL)
7021 return;
7022 if (gchar_cursor() == NUL)
7023 {
7024 /* Add extra space and put the cursor on the first one. */
7025 coladvance_force((colnr_T)(getviscol() + cap->count1));
7026 curwin->w_cursor.col -= cap->count1;
7027 }
7028 else if (gchar_cursor() == TAB)
7029 coladvance_force(getviscol());
7030 }
7031#endif
7032
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007033 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007035 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036#ifdef FEAT_MBYTE
7037 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7038#endif
7039 )
7040 {
7041 clearopbeep(cap->oap);
7042 return;
7043 }
7044
7045 /*
7046 * Replacing with a TAB is done by edit() when it is complicated because
7047 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7048 * Other characters are done below to avoid problems with things like
7049 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7050 */
7051 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7052 {
7053 stuffnumReadbuff(cap->count1);
7054 stuffcharReadbuff('R');
7055 stuffcharReadbuff('\t');
7056 stuffcharReadbuff(ESC);
7057 return;
7058 }
7059
7060 /* save line for undo */
7061 if (u_save_cursor() == FAIL)
7062 return;
7063
7064 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7065 {
7066 /*
7067 * Replace character(s) by a single newline.
7068 * Strange vi behaviour: Only one newline is inserted.
7069 * Delete the characters here.
7070 * Insert the newline with an insert command, takes care of
7071 * autoindent. The insert command depends on being on the last
7072 * character of a line or not.
7073 */
7074#ifdef FEAT_MBYTE
7075 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7076#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007077 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078#endif
7079 stuffcharReadbuff('\r');
7080 stuffcharReadbuff(ESC);
7081
7082 /* Give 'r' to edit(), to get the redo command right. */
7083 invoke_edit(cap, TRUE, 'r', FALSE);
7084 }
7085 else
7086 {
7087 prep_redo(cap->oap->regname, cap->count1,
7088 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7089
7090 curbuf->b_op_start = curwin->w_cursor;
7091#ifdef FEAT_MBYTE
7092 if (has_mbyte)
7093 {
7094 int old_State = State;
7095
7096 if (cap->ncharC1 != 0)
7097 AppendCharToRedobuff(cap->ncharC1);
7098 if (cap->ncharC2 != 0)
7099 AppendCharToRedobuff(cap->ncharC2);
7100
7101 /* This is slow, but it handles replacing a single-byte with a
7102 * multi-byte and the other way around. Also handles adding
7103 * composing characters for utf-8. */
7104 for (n = cap->count1; n > 0; --n)
7105 {
7106 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007107 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7108 {
7109 int c = ins_copychar(curwin->w_cursor.lnum
7110 + (cap->nchar == Ctrl_Y ? -1 : 1));
7111 if (c != NUL)
7112 ins_char(c);
7113 else
7114 /* will be decremented further down */
7115 ++curwin->w_cursor.col;
7116 }
7117 else
7118 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 State = old_State;
7120 if (cap->ncharC1 != 0)
7121 ins_char(cap->ncharC1);
7122 if (cap->ncharC2 != 0)
7123 ins_char(cap->ncharC2);
7124 }
7125 }
7126 else
7127#endif
7128 {
7129 /*
7130 * Replace the characters within one line.
7131 */
7132 for (n = cap->count1; n > 0; --n)
7133 {
7134 /*
7135 * Get ptr again, because u_save and/or showmatch() will have
7136 * released the line. At the same time we let know that the
7137 * line will be changed.
7138 */
7139 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007140 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7141 {
7142 int c = ins_copychar(curwin->w_cursor.lnum
7143 + (cap->nchar == Ctrl_Y ? -1 : 1));
7144 if (c != NUL)
7145 ptr[curwin->w_cursor.col] = c;
7146 }
7147 else
7148 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 if (p_sm && msg_silent == 0)
7150 showmatch(cap->nchar);
7151 ++curwin->w_cursor.col;
7152 }
7153#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007154 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007156 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157
Bram Moolenaar009b2592004-10-24 19:18:58 +00007158 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007159 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007161 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 }
7163#endif
7164
7165 /* mark the buffer as changed and prepare for displaying */
7166 changed_bytes(curwin->w_cursor.lnum,
7167 (colnr_T)(curwin->w_cursor.col - cap->count1));
7168 }
7169 --curwin->w_cursor.col; /* cursor on the last replaced char */
7170#ifdef FEAT_MBYTE
7171 /* if the character on the left of the current cursor is a multi-byte
7172 * character, move two characters left */
7173 if (has_mbyte)
7174 mb_adjust_cursor();
7175#endif
7176 curbuf->b_op_end = curwin->w_cursor;
7177 curwin->w_set_curswant = TRUE;
7178 set_last_insert(cap->nchar);
7179 }
7180}
7181
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182/*
7183 * 'o': Exchange start and end of Visual area.
7184 * 'O': same, but in block mode exchange left and right corners.
7185 */
7186 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007187v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188{
7189 pos_T old_cursor;
7190 colnr_T left, right;
7191
7192 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7193 {
7194 old_cursor = curwin->w_cursor;
7195 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7196 curwin->w_cursor.lnum = VIsual.lnum;
7197 coladvance(left);
7198 VIsual = curwin->w_cursor;
7199
7200 curwin->w_cursor.lnum = old_cursor.lnum;
7201 curwin->w_curswant = right;
7202 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7203 * right one column */
7204 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7205 ++curwin->w_curswant;
7206 coladvance(curwin->w_curswant);
7207 if (curwin->w_cursor.col == old_cursor.col
7208#ifdef FEAT_VIRTUALEDIT
7209 && (!virtual_active()
7210 || curwin->w_cursor.coladd == old_cursor.coladd)
7211#endif
7212 )
7213 {
7214 curwin->w_cursor.lnum = VIsual.lnum;
7215 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7216 ++right;
7217 coladvance(right);
7218 VIsual = curwin->w_cursor;
7219
7220 curwin->w_cursor.lnum = old_cursor.lnum;
7221 coladvance(left);
7222 curwin->w_curswant = left;
7223 }
7224 }
7225 else
7226 {
7227 old_cursor = curwin->w_cursor;
7228 curwin->w_cursor = VIsual;
7229 VIsual = old_cursor;
7230 curwin->w_set_curswant = TRUE;
7231 }
7232}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233
7234/*
7235 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7236 */
7237 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007238nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 if (VIsual_active) /* "R" is replace lines */
7241 {
7242 cap->cmdchar = 'c';
7243 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007244 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 VIsual_mode = 'V';
7246 nv_operator(cap);
7247 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007248 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 {
7250 if (!curbuf->b_p_ma)
7251 EMSG(_(e_modifiable));
7252 else
7253 {
7254#ifdef FEAT_VIRTUALEDIT
7255 if (virtual_active())
7256 coladvance(getviscol());
7257#endif
7258 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7259 }
7260 }
7261}
7262
7263#ifdef FEAT_VREPLACE
7264/*
7265 * "gr".
7266 */
7267 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007268nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 if (VIsual_active)
7271 {
7272 cap->cmdchar = 'r';
7273 cap->nchar = cap->extra_char;
7274 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7275 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007276 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 {
7278 if (!curbuf->b_p_ma)
7279 EMSG(_(e_modifiable));
7280 else
7281 {
7282 if (cap->extra_char == Ctrl_V) /* get another character */
7283 cap->extra_char = get_literal();
7284 stuffcharReadbuff(cap->extra_char);
7285 stuffcharReadbuff(ESC);
7286# ifdef FEAT_VIRTUALEDIT
7287 if (virtual_active())
7288 coladvance(getviscol());
7289# endif
7290 invoke_edit(cap, TRUE, 'v', FALSE);
7291 }
7292 }
7293}
7294#endif
7295
7296/*
7297 * Swap case for "~" command, when it does not work like an operator.
7298 */
7299 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007300n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301{
7302 long n;
7303 pos_T startpos;
7304 int did_change = 0;
7305#ifdef FEAT_NETBEANS_INTG
7306 pos_T pos;
7307 char_u *ptr;
7308 int count;
7309#endif
7310
7311 if (checkclearopq(cap->oap))
7312 return;
7313
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007314 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 {
7316 clearopbeep(cap->oap);
7317 return;
7318 }
7319
7320 prep_redo_cmd(cap);
7321
7322 if (u_save_cursor() == FAIL)
7323 return;
7324
7325 startpos = curwin->w_cursor;
7326#ifdef FEAT_NETBEANS_INTG
7327 pos = startpos;
7328#endif
7329 for (n = cap->count1; n > 0; --n)
7330 {
7331 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7332 inc_cursor();
7333 if (gchar_cursor() == NUL)
7334 {
7335 if (vim_strchr(p_ww, '~') != NULL
7336 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7337 {
7338#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007339 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 {
7341 if (did_change)
7342 {
7343 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007344 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007345 netbeans_removed(curbuf, pos.lnum, pos.col,
7346 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007348 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 }
7350 pos.col = 0;
7351 pos.lnum++;
7352 }
7353#endif
7354 ++curwin->w_cursor.lnum;
7355 curwin->w_cursor.col = 0;
7356 if (n > 1)
7357 {
7358 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7359 break;
7360 u_clearline();
7361 }
7362 }
7363 else
7364 break;
7365 }
7366 }
7367#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007368 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 {
7370 ptr = ml_get(pos.lnum);
7371 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007372 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7373 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 }
7375#endif
7376
7377
7378 check_cursor();
7379 curwin->w_set_curswant = TRUE;
7380 if (did_change)
7381 {
7382 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7383 0L);
7384 curbuf->b_op_start = startpos;
7385 curbuf->b_op_end = curwin->w_cursor;
7386 if (curbuf->b_op_end.col > 0)
7387 --curbuf->b_op_end.col;
7388 }
7389}
7390
7391/*
7392 * Move cursor to mark.
7393 */
7394 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007395nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396{
7397 if (check_mark(pos) == FAIL)
7398 clearop(cap->oap);
7399 else
7400 {
7401 if (cap->cmdchar == '\''
7402 || cap->cmdchar == '`'
7403 || cap->cmdchar == '['
7404 || cap->cmdchar == ']')
7405 setpcmark();
7406 curwin->w_cursor = *pos;
7407 if (flag)
7408 beginline(BL_WHITE | BL_FIX);
7409 else
7410 check_cursor();
7411 }
7412 cap->oap->motion_type = flag ? MLINE : MCHAR;
7413 if (cap->cmdchar == '`')
7414 cap->oap->use_reg_one = TRUE;
7415 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7416 curwin->w_set_curswant = TRUE;
7417}
7418
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419/*
7420 * Handle commands that are operators in Visual mode.
7421 */
7422 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007423v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424{
7425 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7426
7427 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007428 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 if (isupper(cap->cmdchar))
7430 {
7431 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007432 {
7433 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7437 curwin->w_curswant = MAXCOL;
7438 }
7439 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7440 nv_operator(cap);
7441}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442
7443/*
7444 * "s" and "S" commands.
7445 */
7446 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007447nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7450 {
7451 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007452 {
7453 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 cap->cmdchar = 'c';
7457 nv_operator(cap);
7458 }
7459 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 nv_optrans(cap);
7461}
7462
7463/*
7464 * Abbreviated commands.
7465 */
7466 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007467nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468{
7469 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7470 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7471
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 /* in Visual mode these commands are operators */
7473 if (VIsual_active)
7474 v_visop(cap);
7475 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 nv_optrans(cap);
7477}
7478
7479/*
7480 * Translate a command into another command.
7481 */
7482 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007483nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484{
7485 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7486 (char_u *)"d$", (char_u *)"c$",
7487 (char_u *)"cl", (char_u *)"cc",
7488 (char_u *)"yy", (char_u *)":s\r"};
7489 static char_u *str = (char_u *)"xXDCsSY&";
7490
7491 if (!checkclearopq(cap->oap))
7492 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007493 /* In Vi "2D" doesn't delete the next line. Can't translate it
7494 * either, because "2." should also not use the count. */
7495 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7496 {
7497 cap->oap->start = curwin->w_cursor;
7498 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007499#ifdef FEAT_EVAL
7500 set_op_var(OP_DELETE);
7501#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007502 cap->count1 = 1;
7503 nv_dollar(cap);
7504 finish_op = TRUE;
7505 ResetRedobuff();
7506 AppendCharToRedobuff('D');
7507 }
7508 else
7509 {
7510 if (cap->count0)
7511 stuffnumReadbuff(cap->count0);
7512 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 }
7515 cap->opcount = 0;
7516}
7517
7518/*
7519 * "'" and "`" commands. Also for "g'" and "g`".
7520 * cap->arg is TRUE for "'" and "g'".
7521 */
7522 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007523nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524{
7525 pos_T *pos;
7526 int c;
7527#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007528 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7530#endif
7531
7532 if (cap->cmdchar == 'g')
7533 c = cap->extra_char;
7534 else
7535 c = cap->nchar;
7536 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7537 if (pos == (pos_T *)-1) /* jumped to other file */
7538 {
7539 if (cap->arg)
7540 {
7541 check_cursor_lnum();
7542 beginline(BL_WHITE | BL_FIX);
7543 }
7544 else
7545 check_cursor();
7546 }
7547 else
7548 nv_cursormark(cap, cap->arg, pos);
7549
7550#ifdef FEAT_VIRTUALEDIT
7551 /* May need to clear the coladd that a mark includes. */
7552 if (!virtual_active())
7553 curwin->w_cursor.coladd = 0;
7554#endif
Bram Moolenaar9aa15692017-08-19 15:05:32 +02007555 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556#ifdef FEAT_FOLDING
7557 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007558 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007559 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 && (fdo_flags & FDO_MARK)
7561 && old_KeyTyped)
7562 foldOpenCursor();
7563#endif
7564}
7565
7566/*
7567 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7568 */
7569 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007570nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571{
7572#ifdef FEAT_JUMPLIST
7573 pos_T *pos;
7574# ifdef FEAT_FOLDING
7575 linenr_T lnum = curwin->w_cursor.lnum;
7576 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7577# endif
7578
7579 if (!checkclearopq(cap->oap))
7580 {
7581 if (cap->cmdchar == 'g')
7582 pos = movechangelist((int)cap->count1);
7583 else
7584 pos = movemark((int)cap->count1);
7585 if (pos == (pos_T *)-1) /* jump to other file */
7586 {
7587 curwin->w_set_curswant = TRUE;
7588 check_cursor();
7589 }
7590 else if (pos != NULL) /* can jump */
7591 nv_cursormark(cap, FALSE, pos);
7592 else if (cap->cmdchar == 'g')
7593 {
7594 if (curbuf->b_changelistlen == 0)
7595 EMSG(_("E664: changelist is empty"));
7596 else if (cap->count1 < 0)
7597 EMSG(_("E662: At start of changelist"));
7598 else
7599 EMSG(_("E663: At end of changelist"));
7600 }
7601 else
7602 clearopbeep(cap->oap);
7603# ifdef FEAT_FOLDING
7604 if (cap->oap->op_type == OP_NOP
7605 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7606 && (fdo_flags & FDO_MARK)
7607 && old_KeyTyped)
7608 foldOpenCursor();
7609# endif
7610 }
7611#else
7612 clearopbeep(cap->oap);
7613#endif
7614}
7615
7616/*
7617 * Handle '"' command.
7618 */
7619 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007620nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621{
7622 if (checkclearop(cap->oap))
7623 return;
7624#ifdef FEAT_EVAL
7625 if (cap->nchar == '=')
7626 cap->nchar = get_expr_register();
7627#endif
7628 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7629 {
7630 cap->oap->regname = cap->nchar;
7631 cap->opcount = cap->count0; /* remember count before '"' */
7632#ifdef FEAT_EVAL
7633 set_reg_var(cap->oap->regname);
7634#endif
7635 }
7636 else
7637 clearopbeep(cap->oap);
7638}
7639
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640/*
7641 * Handle "v", "V" and "CTRL-V" commands.
7642 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7643 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007644 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 */
7646 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007647nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007649 if (cap->cmdchar == Ctrl_Q)
7650 cap->cmdchar = Ctrl_V;
7651
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7653 * characterwise, linewise, or blockwise. */
7654 if (cap->oap->op_type != OP_NOP)
7655 {
7656 cap->oap->motion_force = cap->cmdchar;
7657 finish_op = FALSE; /* operator doesn't finish now but later */
7658 return;
7659 }
7660
7661 VIsual_select = cap->arg;
7662 if (VIsual_active) /* change Visual mode */
7663 {
7664 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7665 end_visual_mode();
7666 else /* toggle char/block mode */
7667 { /* or char/line mode */
7668 VIsual_mode = cap->cmdchar;
7669 showmode();
7670 }
7671 redraw_curbuf_later(INVERTED); /* update the inversion */
7672 }
7673 else /* start Visual mode */
7674 {
7675 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007676 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007678 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 VIsual = curwin->w_cursor;
7680
7681 VIsual_active = TRUE;
7682 VIsual_reselect = TRUE;
7683 if (!cap->arg)
7684 /* start Select mode when 'selectmode' contains "cmd" */
7685 may_start_select('c');
7686#ifdef FEAT_MOUSE
7687 setmouse();
7688#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007689 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 redraw_cmdline = TRUE; /* show visual mode later */
7691 /*
7692 * For V and ^V, we multiply the number of lines even if there
7693 * was only one -- webb
7694 */
7695 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7696 {
7697 curwin->w_cursor.lnum +=
7698 resel_VIsual_line_count * cap->count0 - 1;
7699 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7700 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7701 }
7702 VIsual_mode = resel_VIsual_mode;
7703 if (VIsual_mode == 'v')
7704 {
7705 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007706 {
7707 validate_virtcol();
7708 curwin->w_curswant = curwin->w_virtcol
7709 + resel_VIsual_vcol * cap->count0 - 1;
7710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007712 curwin->w_curswant = resel_VIsual_vcol;
7713 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007715 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 {
7717 curwin->w_curswant = MAXCOL;
7718 coladvance((colnr_T)MAXCOL);
7719 }
7720 else if (VIsual_mode == Ctrl_V)
7721 {
7722 validate_virtcol();
7723 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007724 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 coladvance(curwin->w_curswant);
7726 }
7727 else
7728 curwin->w_set_curswant = TRUE;
7729 redraw_curbuf_later(INVERTED); /* show the inversion */
7730 }
7731 else
7732 {
7733 if (!cap->arg)
7734 /* start Select mode when 'selectmode' contains "cmd" */
7735 may_start_select('c');
7736 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007737 if (VIsual_mode != 'V' && *p_sel == 'e')
7738 ++cap->count1; /* include one more char */
7739 if (cap->count0 > 0 && --cap->count1 > 0)
7740 {
7741 /* With a count select that many characters or lines. */
7742 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7743 nv_right(cap);
7744 else if (VIsual_mode == 'V')
7745 nv_down(cap);
7746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 }
7748 }
7749}
7750
7751/*
7752 * Start selection for Shift-movement keys.
7753 */
7754 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007755start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756{
7757 /* if 'selectmode' contains "key", start Select mode */
7758 may_start_select('k');
7759 n_start_visual_mode('v');
7760}
7761
7762/*
7763 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7764 */
7765 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007766may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767{
7768 VIsual_select = (stuff_empty() && typebuf_typed()
7769 && (vim_strchr(p_slm, c) != NULL));
7770}
7771
7772/*
7773 * Start Visual mode "c".
7774 * Should set VIsual_select before calling this.
7775 */
7776 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007777n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007779#ifdef FEAT_CONCEAL
7780 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007781 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007782#endif
7783
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 VIsual_mode = c;
7785 VIsual_active = TRUE;
7786 VIsual_reselect = TRUE;
7787#ifdef FEAT_VIRTUALEDIT
7788 /* Corner case: the 0 position in a tab may change when going into
7789 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7790 */
7791 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007792 {
7793 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796#endif
7797 VIsual = curwin->w_cursor;
7798
7799#ifdef FEAT_FOLDING
7800 foldAdjustVisual();
7801#endif
7802
7803#ifdef FEAT_MOUSE
7804 setmouse();
7805#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007806#ifdef FEAT_CONCEAL
7807 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007808 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007809#endif
7810
Bram Moolenaar09df3122006-01-23 22:23:09 +00007811 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 redraw_cmdline = TRUE; /* show visual mode later */
7813#ifdef FEAT_CLIPBOARD
7814 /* Make sure the clipboard gets updated. Needed because start and
7815 * end may still be the same, and the selection needs to be owned */
7816 clip_star.vmode = NUL;
7817#endif
7818
7819 /* Only need to redraw this line, unless still need to redraw an old
7820 * Visual area (when 'lazyredraw' is set). */
7821 if (curwin->w_redr_type < INVERTED)
7822 {
7823 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7824 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7825 }
7826}
7827
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828
7829/*
7830 * CTRL-W: Window commands
7831 */
7832 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007833nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834{
Bram Moolenaar938783d2017-07-16 20:13:26 +02007835 if (cap->nchar == ':')
7836 /* "CTRL-W :" is the same as typing ":"; useful in a terminal window */
7837 nv_colon(cap);
7838 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840}
7841
7842/*
7843 * CTRL-Z: Suspend
7844 */
7845 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007846nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847{
7848 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 if (VIsual_active)
7850 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 do_cmdline_cmd((char_u *)"st");
7852}
7853
7854/*
7855 * Commands starting with "g".
7856 */
7857 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007858nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859{
7860 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 int i;
7863 int flag = FALSE;
7864
7865 switch (cap->nchar)
7866 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007867 case Ctrl_A:
7868 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869#ifdef MEM_PROFILE
7870 /*
7871 * "g^A": dump log of used memory.
7872 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007873 if (!VIsual_active && cap->nchar == Ctrl_A)
7874 vim_mem_profile_dump();
7875 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007877 /*
7878 * "g^A/g^X": sequentially increment visually selected region
7879 */
7880 if (VIsual_active)
7881 {
7882 cap->arg = TRUE;
7883 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01007884 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007885 nv_addsub(cap);
7886 }
7887 else
7888 clearopbeep(oap);
7889 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890
7891#ifdef FEAT_VREPLACE
7892 /*
7893 * "gR": Enter virtual replace mode.
7894 */
7895 case 'R':
7896 cap->arg = TRUE;
7897 nv_Replace(cap);
7898 break;
7899
7900 case 'r':
7901 nv_vreplace(cap);
7902 break;
7903#endif
7904
7905 case '&':
7906 do_cmdline_cmd((char_u *)"%s//~/&");
7907 break;
7908
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 /*
7910 * "gv": Reselect the previous Visual area. If Visual already active,
7911 * exchange previous and current Visual area.
7912 */
7913 case 'v':
7914 if (checkclearop(oap))
7915 break;
7916
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007917 if ( curbuf->b_visual.vi_start.lnum == 0
7918 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7919 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 beep_flush();
7921 else
7922 {
7923 /* set w_cursor to the start of the Visual area, tpos to the end */
7924 if (VIsual_active)
7925 {
7926 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007927 VIsual_mode = curbuf->b_visual.vi_mode;
7928 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929# ifdef FEAT_EVAL
7930 curbuf->b_visual_mode_eval = i;
7931# endif
7932 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007933 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7934 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007936 tpos = curbuf->b_visual.vi_end;
7937 curbuf->b_visual.vi_end = curwin->w_cursor;
7938 curwin->w_cursor = curbuf->b_visual.vi_start;
7939 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 }
7941 else
7942 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007943 VIsual_mode = curbuf->b_visual.vi_mode;
7944 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7945 tpos = curbuf->b_visual.vi_end;
7946 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 }
7948
7949 VIsual_active = TRUE;
7950 VIsual_reselect = TRUE;
7951
7952 /* Set Visual to the start and w_cursor to the end of the Visual
7953 * area. Make sure they are on an existing character. */
7954 check_cursor();
7955 VIsual = curwin->w_cursor;
7956 curwin->w_cursor = tpos;
7957 check_cursor();
7958 update_topline();
7959 /*
7960 * When called from normal "g" command: start Select mode when
7961 * 'selectmode' contains "cmd". When called for K_SELECT, always
7962 * start Select mode.
7963 */
7964 if (cap->arg)
7965 VIsual_select = TRUE;
7966 else
7967 may_start_select('c');
7968#ifdef FEAT_MOUSE
7969 setmouse();
7970#endif
7971#ifdef FEAT_CLIPBOARD
7972 /* Make sure the clipboard gets updated. Needed because start and
7973 * end are still the same, and the selection needs to be owned */
7974 clip_star.vmode = NUL;
7975#endif
7976 redraw_curbuf_later(INVERTED);
7977 showmode();
7978 }
7979 break;
7980 /*
7981 * "gV": Don't reselect the previous Visual area after a Select mode
7982 * mapping of menu.
7983 */
7984 case 'V':
7985 VIsual_reselect = FALSE;
7986 break;
7987
7988 /*
7989 * "gh": start Select mode.
7990 * "gH": start Select line mode.
7991 * "g^H": start Select block mode.
7992 */
7993 case K_BS:
7994 cap->nchar = Ctrl_H;
7995 /* FALLTHROUGH */
7996 case 'h':
7997 case 'H':
7998 case Ctrl_H:
7999# ifdef EBCDIC
8000 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
8001 if (cap->nchar == Ctrl_H)
8002 cap->cmdchar = Ctrl_V;
8003 else
8004# endif
8005 cap->cmdchar = cap->nchar + ('v' - 'h');
8006 cap->arg = TRUE;
8007 nv_visual(cap);
8008 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02008009
8010 /* "gn", "gN" visually select next/previous search match
8011 * "gn" selects next match
8012 * "gN" selects previous match
8013 */
8014 case 'N':
8015 case 'n':
8016 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02008017 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02008018 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019
8020 /*
8021 * "gj" and "gk" two new funny movement keys -- up and down
8022 * movement based on *screen* line rather than *file* line.
8023 */
8024 case 'j':
8025 case K_DOWN:
8026 /* with 'nowrap' it works just like the normal "j" command; also when
8027 * in a closed fold */
8028 if (!curwin->w_p_wrap
8029#ifdef FEAT_FOLDING
8030 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8031#endif
8032 )
8033 {
8034 oap->motion_type = MLINE;
8035 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8036 }
8037 else
8038 i = nv_screengo(oap, FORWARD, cap->count1);
8039 if (i == FAIL)
8040 clearopbeep(oap);
8041 break;
8042
8043 case 'k':
8044 case K_UP:
8045 /* with 'nowrap' it works just like the normal "k" command; also when
8046 * in a closed fold */
8047 if (!curwin->w_p_wrap
8048#ifdef FEAT_FOLDING
8049 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8050#endif
8051 )
8052 {
8053 oap->motion_type = MLINE;
8054 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8055 }
8056 else
8057 i = nv_screengo(oap, BACKWARD, cap->count1);
8058 if (i == FAIL)
8059 clearopbeep(oap);
8060 break;
8061
8062 /*
8063 * "gJ": join two lines without inserting a space.
8064 */
8065 case 'J':
8066 nv_join(cap);
8067 break;
8068
8069 /*
8070 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8071 * "gm": middle of "g0" and "g$".
8072 */
8073 case '^':
8074 flag = TRUE;
8075 /* FALLTHROUGH */
8076
8077 case '0':
8078 case 'm':
8079 case K_HOME:
8080 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 oap->motion_type = MCHAR;
8082 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008083 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008085 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 int width2 = width1 + curwin_col_off2();
8087
8088 validate_virtcol();
8089 i = 0;
8090 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8091 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8092 }
8093 else
8094 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008095 /* Go to the middle of the screen line. When 'number' or
8096 * 'relativenumber' is on and lines are wrapping the middle can be more
8097 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02008099 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 + ((curwin->w_p_wrap && i > 0)
8101 ? curwin_col_off2() : 0)) / 2;
8102 coladvance((colnr_T)i);
8103 if (flag)
8104 {
8105 do
8106 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01008107 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 }
8109 curwin->w_set_curswant = TRUE;
8110 break;
8111
8112 case '_':
8113 /* "g_": to the last non-blank character in the line or <count> lines
8114 * downward. */
8115 cap->oap->motion_type = MCHAR;
8116 cap->oap->inclusive = TRUE;
8117 curwin->w_curswant = MAXCOL;
8118 if (cursor_down((long)(cap->count1 - 1),
8119 cap->oap->op_type == OP_NOP) == FAIL)
8120 clearopbeep(cap->oap);
8121 else
8122 {
8123 char_u *ptr = ml_get_curline();
8124
8125 /* In Visual mode we may end up after the line. */
8126 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8127 --curwin->w_cursor.col;
8128
8129 /* Decrease the cursor column until it's on a non-blank. */
8130 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01008131 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 --curwin->w_cursor.col;
8133 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008134 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 }
8136 break;
8137
8138 case '$':
8139 case K_END:
8140 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {
8142 int col_off = curwin_col_off();
8143
8144 oap->motion_type = MCHAR;
8145 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008146 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 {
8148 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8149 if (cap->count1 == 1)
8150 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008151 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 int width2 = width1 + curwin_col_off2();
8153
8154 validate_virtcol();
8155 i = width1 - 1;
8156 if (curwin->w_virtcol >= (colnr_T)width1)
8157 i += ((curwin->w_virtcol - width1) / width2 + 1)
8158 * width2;
8159 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008160
8161 /* Make sure we stick in this column. */
8162 validate_virtcol();
8163 curwin->w_curswant = curwin->w_virtcol;
8164 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8166 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8167 {
8168 /*
8169 * Check for landing on a character that got split at
8170 * the end of the line. We do not want to advance to
8171 * the next screen line.
8172 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 if (curwin->w_virtcol > (colnr_T)i)
8174 --curwin->w_cursor.col;
8175 }
8176#endif
8177 }
8178 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8179 clearopbeep(oap);
8180 }
8181 else
8182 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008183 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008185
8186 /* Make sure we stick in this column. */
8187 validate_virtcol();
8188 curwin->w_curswant = curwin->w_virtcol;
8189 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 }
8191 }
8192 break;
8193
8194 /*
8195 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8196 */
8197 case '*':
8198 case '#':
8199#if POUND != '#'
8200 case POUND: /* pound sign (sometimes equal to '#') */
8201#endif
8202 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8203 case ']': /* :tselect for current identifier */
8204 nv_ident(cap);
8205 break;
8206
8207 /*
8208 * ge and gE: go back to end of word
8209 */
8210 case 'e':
8211 case 'E':
8212 oap->motion_type = MCHAR;
8213 curwin->w_set_curswant = TRUE;
8214 oap->inclusive = TRUE;
8215 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8216 clearopbeep(oap);
8217 break;
8218
8219 /*
8220 * "g CTRL-G": display info about cursor position
8221 */
8222 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01008223 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 break;
8225
8226 /*
8227 * "gi": start Insert at the last position.
8228 */
8229 case 'i':
8230 if (curbuf->b_last_insert.lnum != 0)
8231 {
8232 curwin->w_cursor = curbuf->b_last_insert;
8233 check_cursor_lnum();
8234 i = (int)STRLEN(ml_get_curline());
8235 if (curwin->w_cursor.col > (colnr_T)i)
8236 {
8237#ifdef FEAT_VIRTUALEDIT
8238 if (virtual_active())
8239 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8240#endif
8241 curwin->w_cursor.col = i;
8242 }
8243 }
8244 cap->cmdchar = 'i';
8245 nv_edit(cap);
8246 break;
8247
8248 /*
8249 * "gI": Start insert in column 1.
8250 */
8251 case 'I':
8252 beginline(0);
8253 if (!checkclearopq(oap))
8254 invoke_edit(cap, FALSE, 'g', FALSE);
8255 break;
8256
8257#ifdef FEAT_SEARCHPATH
8258 /*
8259 * "gf": goto file, edit file under cursor
8260 * "]f" and "[f": can also be used.
8261 */
8262 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008263 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 nv_gotofile(cap);
8265 break;
8266#endif
8267
8268 /* "g'm" and "g`m": jump to mark without setting pcmark */
8269 case '\'':
8270 cap->arg = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008271 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 case '`':
8273 nv_gomark(cap);
8274 break;
8275
8276 /*
8277 * "gs": Goto sleep.
8278 */
8279 case 's':
8280 do_sleep(cap->count1 * 1000L);
8281 break;
8282
8283 /*
8284 * "ga": Display the ascii value of the character under the
8285 * cursor. It is displayed in decimal, hex, and octal. -- webb
8286 */
8287 case 'a':
8288 do_ascii(NULL);
8289 break;
8290
8291#ifdef FEAT_MBYTE
8292 /*
8293 * "g8": Display the bytes used for the UTF-8 character under the
8294 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008295 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 */
8297 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008298 if (cap->count0 == 8)
8299 utf_find_illegal();
8300 else
8301 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 break;
8303#endif
8304
Bram Moolenaar60402d62017-04-20 18:54:50 +02008305 /* "g<": show scrollback text */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008306 case '<':
8307 show_sb_text();
8308 break;
8309
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 /*
8311 * "gg": Goto the first line in file. With a count it goes to
8312 * that line number like for "G". -- webb
8313 */
8314 case 'g':
8315 cap->arg = FALSE;
8316 nv_goto(cap);
8317 break;
8318
8319 /*
8320 * Two-character operators:
8321 * "gq" Format text
8322 * "gw" Format text and keep cursor position
8323 * "g~" Toggle the case of the text.
8324 * "gu" Change text to lower case.
8325 * "gU" Change text to upper case.
8326 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008327 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 */
8329 case 'q':
8330 case 'w':
8331 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008332 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 case '~':
8334 case 'u':
8335 case 'U':
8336 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008337 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 nv_operator(cap);
8339 break;
8340
8341 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008342 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 * current function
8344 * "gD": idem, but in the current file.
8345 */
8346 case 'd':
8347 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008348 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 break;
8350
8351#ifdef FEAT_MOUSE
8352 /*
8353 * g<*Mouse> : <C-*mouse>
8354 */
8355 case K_MIDDLEMOUSE:
8356 case K_MIDDLEDRAG:
8357 case K_MIDDLERELEASE:
8358 case K_LEFTMOUSE:
8359 case K_LEFTDRAG:
8360 case K_LEFTRELEASE:
8361 case K_RIGHTMOUSE:
8362 case K_RIGHTDRAG:
8363 case K_RIGHTRELEASE:
8364 case K_X1MOUSE:
8365 case K_X1DRAG:
8366 case K_X1RELEASE:
8367 case K_X2MOUSE:
8368 case K_X2DRAG:
8369 case K_X2RELEASE:
8370 mod_mask = MOD_MASK_CTRL;
8371 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8372 break;
8373#endif
8374
8375 case K_IGNORE:
8376 break;
8377
8378 /*
8379 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8380 */
8381 case 'p':
8382 case 'P':
8383 nv_put(cap);
8384 break;
8385
8386#ifdef FEAT_BYTEOFF
8387 /* "go": goto byte count from start of buffer */
8388 case 'o':
8389 goto_byte(cap->count0);
8390 break;
8391#endif
8392
8393 /* "gQ": improved Ex mode */
8394 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008395 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 {
8397 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008398 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 break;
8400 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008401
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 if (!checkclearopq(oap))
8403 do_exmode(TRUE);
8404 break;
8405
8406#ifdef FEAT_JUMPLIST
8407 case ',':
8408 nv_pcmark(cap);
8409 break;
8410
8411 case ';':
8412 cap->count1 = -cap->count1;
8413 nv_pcmark(cap);
8414 break;
8415#endif
8416
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008417 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008418 if (!checkclearop(oap))
8419 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008420 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008421 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008422 if (!checkclearop(oap))
8423 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008424 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008425
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008426 case '+':
8427 case '-': /* "g+" and "g-": undo or redo along the timeline */
8428 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008429 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008430 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008431 break;
8432
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 default:
8434 clearopbeep(oap);
8435 break;
8436 }
8437}
8438
8439/*
8440 * Handle "o" and "O" commands.
8441 */
8442 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008443n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008445#ifdef FEAT_CONCEAL
8446 linenr_T oldline = curwin->w_cursor.lnum;
8447#endif
8448
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 if (!checkclearopq(cap->oap))
8450 {
8451#ifdef FEAT_FOLDING
8452 if (cap->cmdchar == 'O')
8453 /* Open above the first line of a folded sequence of lines */
8454 (void)hasFolding(curwin->w_cursor.lnum,
8455 &curwin->w_cursor.lnum, NULL);
8456 else
8457 /* Open below the last line of a folded sequence of lines */
8458 (void)hasFolding(curwin->w_cursor.lnum,
8459 NULL, &curwin->w_cursor.lnum);
8460#endif
8461 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8462 (cap->cmdchar == 'O' ? 1 : 0)),
8463 (linenr_T)(curwin->w_cursor.lnum +
8464 (cap->cmdchar == 'o' ? 1 : 0))
8465 ) == OK
8466 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8467#ifdef FEAT_COMMENTS
8468 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8469#endif
8470 0, 0))
8471 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008472#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008473 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008474 update_single_line(curwin, oldline);
8475#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008476 /* When '#' is in 'cpoptions' ignore the count. */
8477 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8478 cap->count1 = 1;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008479#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02008480 if (curwin->w_p_cul)
8481 /* force redraw of cursorline */
8482 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8485 }
8486 }
8487}
8488
8489/*
8490 * "." command: redo last change.
8491 */
8492 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008493nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494{
8495 if (!checkclearopq(cap->oap))
8496 {
8497 /*
8498 * If "restart_edit" is TRUE, the last but one command is repeated
8499 * instead of the last command (inserting text). This is used for
8500 * CTRL-O <.> in insert mode.
8501 */
8502 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8503 clearopbeep(cap->oap);
8504 }
8505}
8506
8507/*
8508 * CTRL-R: undo undo
8509 */
8510 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008511nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512{
8513 if (!checkclearopq(cap->oap))
8514 {
8515 u_redo((int)cap->count1);
8516 curwin->w_set_curswant = TRUE;
8517 }
8518}
8519
8520/*
8521 * Handle "U" command.
8522 */
8523 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008524nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525{
8526 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008527 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 {
8529 /* translate "gUU" to "gUgU" */
8530 cap->cmdchar = 'g';
8531 cap->nchar = 'U';
8532 nv_operator(cap);
8533 }
8534 else if (!checkclearopq(cap->oap))
8535 {
8536 u_undoline();
8537 curwin->w_set_curswant = TRUE;
8538 }
8539}
8540
8541/*
8542 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8543 * single character.
8544 */
8545 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008546nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008548 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 n_swapchar(cap);
8550 else
8551 nv_operator(cap);
8552}
8553
8554/*
8555 * Handle an operator command.
8556 * The actual work is done by do_pending_operator().
8557 */
8558 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008559nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560{
8561 int op_type;
8562
8563 op_type = get_op_type(cap->cmdchar, cap->nchar);
8564
8565 if (op_type == cap->oap->op_type) /* double operator works on lines */
8566 nv_lineop(cap);
8567 else if (!checkclearop(cap->oap))
8568 {
8569 cap->oap->start = curwin->w_cursor;
8570 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008571#ifdef FEAT_EVAL
8572 set_op_var(op_type);
8573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 }
8575}
8576
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008577#ifdef FEAT_EVAL
8578/*
8579 * Set v:operator to the characters for "optype".
8580 */
8581 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008582set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008583{
8584 char_u opchars[3];
8585
8586 if (optype == OP_NOP)
8587 set_vim_var_string(VV_OP, NULL, 0);
8588 else
8589 {
8590 opchars[0] = get_op_char(optype);
8591 opchars[1] = get_extra_op_char(optype);
8592 opchars[2] = NUL;
8593 set_vim_var_string(VV_OP, opchars, -1);
8594 }
8595}
8596#endif
8597
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598/*
8599 * Handle linewise operator "dd", "yy", etc.
8600 *
8601 * "_" is is a strange motion command that helps make operators more logical.
8602 * It is actually implemented, but not documented in the real Vi. This motion
8603 * command actually refers to "the current line". Commands like "dd" and "yy"
8604 * are really an alternate form of "d_" and "y_". It does accept a count, so
8605 * "d3_" works to delete 3 lines.
8606 */
8607 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008608nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609{
8610 cap->oap->motion_type = MLINE;
8611 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8612 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008613 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8614 && cap->oap->motion_force != 'v'
8615 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 || cap->oap->op_type == OP_LSHIFT
8617 || cap->oap->op_type == OP_RSHIFT)
8618 beginline(BL_SOL | BL_FIX);
8619 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8620 beginline(BL_WHITE | BL_FIX);
8621}
8622
8623/*
8624 * <Home> command.
8625 */
8626 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008627nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008629 /* CTRL-HOME is like "gg" */
8630 if (mod_mask & MOD_MASK_CTRL)
8631 nv_goto(cap);
8632 else
8633 {
8634 cap->count0 = 1;
8635 nv_pipe(cap);
8636 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008637 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8638 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639}
8640
8641/*
8642 * "|" command.
8643 */
8644 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008645nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646{
8647 cap->oap->motion_type = MCHAR;
8648 cap->oap->inclusive = FALSE;
8649 beginline(0);
8650 if (cap->count0 > 0)
8651 {
8652 coladvance((colnr_T)(cap->count0 - 1));
8653 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8654 }
8655 else
8656 curwin->w_curswant = 0;
8657 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008658 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 curwin->w_set_curswant = FALSE;
8660}
8661
8662/*
8663 * Handle back-word command "b" and "B".
8664 * cap->arg is 1 for "B"
8665 */
8666 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008667nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668{
8669 cap->oap->motion_type = MCHAR;
8670 cap->oap->inclusive = FALSE;
8671 curwin->w_set_curswant = TRUE;
8672 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8673 clearopbeep(cap->oap);
8674#ifdef FEAT_FOLDING
8675 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8676 foldOpenCursor();
8677#endif
8678}
8679
8680/*
8681 * Handle word motion commands "e", "E", "w" and "W".
8682 * cap->arg is TRUE for "E" and "W".
8683 */
8684 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008685nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686{
8687 int n;
8688 int word_end;
8689 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008690 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691
8692 /*
8693 * Set inclusive for the "E" and "e" command.
8694 */
8695 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8696 word_end = TRUE;
8697 else
8698 word_end = FALSE;
8699 cap->oap->inclusive = word_end;
8700
8701 /*
8702 * "cw" and "cW" are a special case.
8703 */
8704 if (!word_end && cap->oap->op_type == OP_CHANGE)
8705 {
8706 n = gchar_cursor();
8707 if (n != NUL) /* not an empty line */
8708 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008709 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 {
8711 /*
8712 * Reproduce a funny Vi behaviour: "cw" on a blank only
8713 * changes one character, not all blanks until the start of
8714 * the next word. Only do this when the 'w' flag is included
8715 * in 'cpoptions'.
8716 */
8717 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8718 {
8719 cap->oap->inclusive = TRUE;
8720 cap->oap->motion_type = MCHAR;
8721 return;
8722 }
8723 }
8724 else
8725 {
8726 /*
8727 * This is a little strange. To match what the real Vi does,
8728 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8729 * that we are not on a space or a TAB. This seems impolite
8730 * at first, but it's really more what we mean when we say
8731 * 'cw'.
8732 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008733 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 * will change only one character! This is done by setting
8735 * flag.
8736 */
8737 cap->oap->inclusive = TRUE;
8738 word_end = TRUE;
8739 flag = TRUE;
8740 }
8741 }
8742 }
8743
8744 cap->oap->motion_type = MCHAR;
8745 curwin->w_set_curswant = TRUE;
8746 if (word_end)
8747 n = end_word(cap->count1, cap->arg, flag, FALSE);
8748 else
8749 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8750
Bram Moolenaardfefb982008-04-01 10:06:39 +00008751 /* Don't leave the cursor on the NUL past the end of line. Unless we
8752 * didn't move it forward. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008753 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008754 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755
8756 if (n == FAIL && cap->oap->op_type == OP_NOP)
8757 clearopbeep(cap->oap);
8758 else
8759 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761#ifdef FEAT_FOLDING
8762 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8763 foldOpenCursor();
8764#endif
8765 }
8766}
8767
8768/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008769 * Used after a movement command: If the cursor ends up on the NUL after the
8770 * end of the line, may move it back to the last character and make the motion
8771 * inclusive.
8772 */
8773 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008774adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008775{
8776 /* The cursor cannot remain on the NUL when:
8777 * - the column is > 0
8778 * - not in Visual mode or 'selection' is "o"
8779 * - 'virtualedit' is not "all" and not "onemore".
8780 */
8781 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008782 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008783#ifdef FEAT_VIRTUALEDIT
8784 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8785#endif
8786 )
8787 {
8788 --curwin->w_cursor.col;
8789#ifdef FEAT_MBYTE
8790 /* prevent cursor from moving on the trail byte */
8791 if (has_mbyte)
8792 mb_adjust_cursor();
8793#endif
8794 oap->inclusive = TRUE;
8795 }
8796}
8797
8798/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 * "0" and "^" commands.
8800 * cap->arg is the argument for beginline().
8801 */
8802 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008803nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804{
8805 cap->oap->motion_type = MCHAR;
8806 cap->oap->inclusive = FALSE;
8807 beginline(cap->arg);
8808#ifdef FEAT_FOLDING
8809 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8810 foldOpenCursor();
8811#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008812 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8813 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814}
8815
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816/*
8817 * In exclusive Visual mode, may include the last character.
8818 */
8819 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008820adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821{
8822 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008823 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008825#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 if (has_mbyte)
8827 inc_cursor();
8828 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 ++curwin->w_cursor.col;
8831 cap->oap->inclusive = FALSE;
8832 }
8833}
8834
8835/*
8836 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8837 * Should check VIsual_mode before calling this.
8838 * Returns TRUE when backed up to the previous line.
8839 */
8840 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008841unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842{
8843 pos_T *pp;
8844
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008845 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008847 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 pp = &curwin->w_cursor;
8849 else
8850 pp = &VIsual;
8851#ifdef FEAT_VIRTUALEDIT
8852 if (pp->coladd > 0)
8853 --pp->coladd;
8854 else
8855#endif
8856 if (pp->col > 0)
8857 {
8858 --pp->col;
8859#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008860 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861#endif
8862 }
8863 else if (pp->lnum > 1)
8864 {
8865 --pp->lnum;
8866 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8867 return TRUE;
8868 }
8869 }
8870 return FALSE;
8871}
8872
8873/*
8874 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8875 */
8876 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008877nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878{
8879 if (VIsual_active)
8880 VIsual_select = TRUE;
8881 else if (VIsual_reselect)
8882 {
8883 cap->nchar = 'v'; /* fake "gv" command */
8884 cap->arg = TRUE;
8885 nv_g_cmd(cap);
8886 }
8887}
8888
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889
8890/*
8891 * "G", "gg", CTRL-END, CTRL-HOME.
8892 * cap->arg is TRUE for "G".
8893 */
8894 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008895nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896{
8897 linenr_T lnum;
8898
8899 if (cap->arg)
8900 lnum = curbuf->b_ml.ml_line_count;
8901 else
8902 lnum = 1L;
8903 cap->oap->motion_type = MLINE;
8904 setpcmark();
8905
8906 /* When a count is given, use it instead of the default lnum */
8907 if (cap->count0 != 0)
8908 lnum = cap->count0;
8909 if (lnum < 1L)
8910 lnum = 1L;
8911 else if (lnum > curbuf->b_ml.ml_line_count)
8912 lnum = curbuf->b_ml.ml_line_count;
8913 curwin->w_cursor.lnum = lnum;
8914 beginline(BL_SOL | BL_FIX);
8915#ifdef FEAT_FOLDING
8916 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8917 foldOpenCursor();
8918#endif
8919}
8920
8921/*
8922 * CTRL-\ in Normal mode.
8923 */
8924 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008925nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926{
8927 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8928 {
8929 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008930 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 clear_cmdline = TRUE; /* unshow mode later */
8932 restart_edit = 0;
8933#ifdef FEAT_CMDWIN
8934 if (cmdwin_type != 0)
8935 cmdwin_result = Ctrl_C;
8936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 if (VIsual_active)
8938 {
8939 end_visual_mode(); /* stop Visual */
8940 redraw_curbuf_later(INVERTED);
8941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8943 if (cap->nchar == Ctrl_G && p_im)
8944 restart_edit = 'a';
8945 }
8946 else
8947 clearopbeep(cap->oap);
8948}
8949
8950/*
8951 * ESC in Normal mode: beep, but don't flush buffers.
8952 * Don't even beep if we are canceling a command.
8953 */
8954 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008955nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956{
8957 int no_reason;
8958
8959 no_reason = (cap->oap->op_type == OP_NOP
8960 && cap->opcount == 0
8961 && cap->count0 == 0
8962 && cap->oap->regname == 0
8963 && !p_im);
8964
8965 if (cap->arg) /* TRUE for CTRL-C */
8966 {
8967 if (restart_edit == 0
8968#ifdef FEAT_CMDWIN
8969 && cmdwin_type == 0
8970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 && no_reason)
Bram Moolenaar28a81932017-06-04 15:33:48 +02008973 MSG(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974
8975 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8976 * set again below when halfway a mapping. */
8977 if (!p_im)
8978 restart_edit = 0;
8979#ifdef FEAT_CMDWIN
8980 if (cmdwin_type != 0)
8981 {
8982 cmdwin_result = K_IGNORE;
8983 got_int = FALSE; /* don't stop executing autocommands et al. */
8984 return;
8985 }
8986#endif
8987 }
8988
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 if (VIsual_active)
8990 {
8991 end_visual_mode(); /* stop Visual */
8992 check_cursor_col(); /* make sure cursor is not beyond EOL */
8993 curwin->w_set_curswant = TRUE;
8994 redraw_curbuf_later(INVERTED);
8995 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008996 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008997 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 clearop(cap->oap);
8999
9000 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9001 * set return to Insert mode afterwards. */
Bram Moolenaare2c38102016-01-31 14:55:40 +01009002 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 restart_edit = 'a';
9004}
9005
9006/*
9007 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009008 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 */
9010 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009011nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012{
9013 /* <Insert> is equal to "i" */
9014 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9015 cap->cmdchar = 'i';
9016
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 /* in Visual mode "A" and "I" are an operator */
9018 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02009019 {
9020#ifdef FEAT_TERMINAL
9021 if (term_in_normal_mode())
9022 {
9023 end_visual_mode();
9024 clearop(cap->oap);
9025 term_enter_job_mode();
9026 return;
9027 }
9028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02009030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031
9032 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009033 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9034 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 {
9036#ifdef FEAT_TEXTOBJ
9037 nv_object(cap);
9038#else
9039 clearopbeep(cap->oap);
9040#endif
9041 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02009042#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02009043 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02009044 {
9045 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02009046 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02009047 return;
9048 }
9049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 else if (!curbuf->b_p_ma && !p_im)
9051 {
9052 /* Only give this error when 'insertmode' is off. */
9053 EMSG(_(e_modifiable));
9054 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009055 if (cap->cmdchar == K_PS)
9056 /* drop the pasted text */
9057 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 }
Bram Moolenaara1891842017-02-04 21:34:31 +01009059 else if (cap->cmdchar == K_PS && VIsual_active)
9060 {
9061 pos_T old_pos = curwin->w_cursor;
9062 pos_T old_visual = VIsual;
9063
9064 /* In Visual mode the selected text is deleted. */
9065 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
9066 {
9067 shift_delete_registers();
9068 cap->oap->regname = '1';
9069 }
9070 else
9071 cap->oap->regname = '-';
9072 cap->cmdchar = 'd';
9073 cap->nchar = NUL;
9074 nv_operator(cap);
9075 do_pending_operator(cap, 0, FALSE);
9076 cap->cmdchar = K_PS;
9077
9078 /* When the last char in the line was deleted then append. Detect this
9079 * by checking if the cursor moved to before the Visual area. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009080 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
9081 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01009082 inc_cursor();
9083
9084 /* Insert to replace the deleted text with the pasted text. */
9085 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 else if (!checkclearopq(cap->oap))
9088 {
9089 switch (cap->cmdchar)
9090 {
9091 case 'A': /* "A"ppend after the line */
9092 curwin->w_set_curswant = TRUE;
9093#ifdef FEAT_VIRTUALEDIT
9094 if (ve_flags == VE_ALL)
9095 {
9096 int save_State = State;
9097
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009098 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 * character past the end of the line */
9100 State = INSERT;
9101 coladvance((colnr_T)MAXCOL);
9102 State = save_State;
9103 }
9104 else
9105#endif
9106 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9107 break;
9108
9109 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009110 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9111 beginline(BL_WHITE);
9112 else
9113 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 break;
9115
Bram Moolenaara1891842017-02-04 21:34:31 +01009116 case K_PS:
9117 /* Bracketed paste works like "a"ppend, unless the cursor is in
9118 * the first column, then it inserts. */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009119 if (curwin->w_cursor.col == 0)
9120 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02009121 /* FALLTHROUGH */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009122
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9124#ifdef FEAT_VIRTUALEDIT
9125 /* increment coladd when in virtual space, increment the
9126 * column otherwise, also to append after an unprintable char */
9127 if (virtual_active()
9128 && (curwin->w_cursor.coladd > 0
9129 || *ml_get_cursor() == NUL
9130 || *ml_get_cursor() == TAB))
9131 curwin->w_cursor.coladd++;
9132 else
9133#endif
9134 if (*ml_get_cursor() != NUL)
9135 inc_cursor();
9136 break;
9137 }
9138
9139#ifdef FEAT_VIRTUALEDIT
9140 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9141 {
9142 int save_State = State;
9143
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009144 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 * character past the end of the line */
9146 State = INSERT;
9147 coladvance(getviscol());
9148 State = save_State;
9149 }
9150#endif
9151
9152 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9153 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009154 else if (cap->cmdchar == K_PS)
9155 /* drop the pasted text */
9156 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157}
9158
9159/*
9160 * Invoke edit() and take care of "restart_edit" and the return value.
9161 */
9162 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009163invoke_edit(
9164 cmdarg_T *cap,
9165 int repl, /* "r" or "gr" command */
9166 int cmd,
9167 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168{
9169 int restart_edit_save = 0;
9170
9171 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9172 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9173 * it. */
9174 if (repl || !stuff_empty())
9175 restart_edit_save = restart_edit;
9176 else
9177 restart_edit_save = 0;
9178
9179 /* Always reset "restart_edit", this is not a restarted edit. */
9180 restart_edit = 0;
9181
9182 if (edit(cmd, startln, cap->count1))
9183 cap->retval |= CA_COMMAND_BUSY;
9184
9185 if (restart_edit == 0)
9186 restart_edit = restart_edit_save;
9187}
9188
9189#ifdef FEAT_TEXTOBJ
9190/*
9191 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9192 */
9193 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009194nv_object(
9195 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196{
9197 int flag;
9198 int include;
9199 char_u *mps_save;
9200
9201 if (cap->cmdchar == 'i')
9202 include = FALSE; /* "ix" = inner object: exclude white space */
9203 else
9204 include = TRUE; /* "ax" = an object: include white space */
9205
9206 /* Make sure (), [], {} and <> are in 'matchpairs' */
9207 mps_save = curbuf->b_p_mps;
9208 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9209
9210 switch (cap->nchar)
9211 {
9212 case 'w': /* "aw" = a word */
9213 flag = current_word(cap->oap, cap->count1, include, FALSE);
9214 break;
9215 case 'W': /* "aW" = a WORD */
9216 flag = current_word(cap->oap, cap->count1, include, TRUE);
9217 break;
9218 case 'b': /* "ab" = a braces block */
9219 case '(':
9220 case ')':
9221 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9222 break;
9223 case 'B': /* "aB" = a Brackets block */
9224 case '{':
9225 case '}':
9226 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9227 break;
9228 case '[': /* "a[" = a [] block */
9229 case ']':
9230 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9231 break;
9232 case '<': /* "a<" = a <> block */
9233 case '>':
9234 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9235 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009236 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01009237 /* Do not adjust oap->end in do_pending_operator()
9238 * otherwise there are different results for 'dit'
9239 * (note leading whitespace in last line):
9240 * 1) <b> 2) <b>
9241 * foobar foobar
9242 * </b> </b>
9243 */
9244 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009245 flag = current_tagblock(cap->oap, cap->count1, include);
9246 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 case 'p': /* "ap" = a paragraph */
9248 flag = current_par(cap->oap, cap->count1, include, 'p');
9249 break;
9250 case 's': /* "as" = a sentence */
9251 flag = current_sent(cap->oap, cap->count1, include);
9252 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009253 case '"': /* "a"" = a double quoted string */
9254 case '\'': /* "a'" = a single quoted string */
9255 case '`': /* "a`" = a backtick quoted string */
9256 flag = current_quote(cap->oap, cap->count1, include,
9257 cap->nchar);
9258 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259#if 0 /* TODO */
9260 case 'S': /* "aS" = a section */
9261 case 'f': /* "af" = a filename */
9262 case 'u': /* "au" = a URL */
9263#endif
9264 default:
9265 flag = FAIL;
9266 break;
9267 }
9268
9269 curbuf->b_p_mps = mps_save;
9270 if (flag == FAIL)
9271 clearopbeep(cap->oap);
9272 adjust_cursor_col();
9273 curwin->w_set_curswant = TRUE;
9274}
9275#endif
9276
9277/*
9278 * "q" command: Start/stop recording.
9279 * "q:", "q/", "q?": edit command-line in command-line window.
9280 */
9281 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009282nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283{
9284 if (cap->oap->op_type == OP_FORMAT)
9285 {
9286 /* "gqq" is the same as "gqgq": format line */
9287 cap->cmdchar = 'g';
9288 cap->nchar = 'q';
9289 nv_operator(cap);
9290 }
9291 else if (!checkclearop(cap->oap))
9292 {
9293#ifdef FEAT_CMDWIN
9294 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9295 {
9296 stuffcharReadbuff(cap->nchar);
9297 stuffcharReadbuff(K_CMDWIN);
9298 }
9299 else
9300#endif
9301 /* (stop) recording into a named register, unless executing a
9302 * register */
9303 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9304 clearopbeep(cap->oap);
9305 }
9306}
9307
9308/*
9309 * Handle the "@r" command.
9310 */
9311 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009312nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313{
9314 if (checkclearop(cap->oap))
9315 return;
9316#ifdef FEAT_EVAL
9317 if (cap->nchar == '=')
9318 {
9319 if (get_expr_register() == NUL)
9320 return;
9321 }
9322#endif
9323 while (cap->count1-- && !got_int)
9324 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009325 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 {
9327 clearopbeep(cap->oap);
9328 break;
9329 }
9330 line_breakcheck();
9331 }
9332}
9333
9334/*
9335 * Handle the CTRL-U and CTRL-D commands.
9336 */
9337 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009338nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339{
9340 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9341 || (cap->cmdchar == Ctrl_D
9342 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9343 clearopbeep(cap->oap);
9344 else if (!checkclearop(cap->oap))
9345 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9346}
9347
9348/*
9349 * Handle "J" or "gJ" command.
9350 */
9351 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009352nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 if (VIsual_active) /* join the visual lines */
9355 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009356 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 {
9358 if (cap->count0 <= 1)
9359 cap->count0 = 2; /* default for join is two lines! */
9360 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9361 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 {
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009363 /* can't join when on the last line */
9364 if (cap->count0 <= 2)
9365 {
9366 clearopbeep(cap->oap);
9367 return;
9368 }
9369 cap->count0 = curbuf->b_ml.ml_line_count
9370 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009372
9373 prep_redo(cap->oap->regname, cap->count0,
9374 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9375 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 }
9377}
9378
9379/*
9380 * "P", "gP", "p" and "gp" commands.
9381 */
9382 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009383nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 int regname = 0;
9386 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009387 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009388 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 int dir;
9390 int flags = 0;
9391
9392 if (cap->oap->op_type != OP_NOP)
9393 {
9394#ifdef FEAT_DIFF
9395 /* "dp" is ":diffput" */
9396 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9397 {
9398 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009399 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 }
9401 else
9402#endif
9403 clearopbeep(cap->oap);
9404 }
9405 else
9406 {
9407 dir = (cap->cmdchar == 'P'
9408 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9409 ? BACKWARD : FORWARD;
9410 prep_redo_cmd(cap);
9411 if (cap->cmdchar == 'g')
9412 flags |= PUT_CURSEND;
9413
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 if (VIsual_active)
9415 {
9416 /* Putting in Visual mode: The put text replaces the selected
9417 * text. First delete the selected text, then put the new text.
9418 * Need to save and restore the registers that the delete
9419 * overwrites if the old contents is being put.
9420 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009421 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009423#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009425#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009426 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009427 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009428#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431
9432 )
9433 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009434 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435 * put, save it first. */
9436 reg1 = get_register(regname, TRUE);
9437 }
9438
9439 /* Now delete the selected text. */
9440 cap->cmdchar = 'd';
9441 cap->nchar = NUL;
9442 cap->oap->regname = NUL;
9443 nv_operator(cap);
9444 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009445 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446
9447 /* delete PUT_LINE_BACKWARD; */
9448 cap->oap->regname = regname;
9449
9450 if (reg1 != NULL)
9451 {
9452 /* Delete probably changed the register we want to put, save
9453 * it first. Then put back what was there before the delete. */
9454 reg2 = get_register(regname, FALSE);
9455 put_register(regname, reg1);
9456 }
9457
9458 /* When deleted a linewise Visual area, put the register as
9459 * lines to avoid it joined with the next line. When deletion was
9460 * characterwise, split a line when putting lines. */
9461 if (VIsual_mode == 'V')
9462 flags |= PUT_LINE;
9463 else if (VIsual_mode == 'v')
9464 flags |= PUT_LINE_SPLIT;
9465 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9466 flags |= PUT_LINE_FORWARD;
9467 dir = BACKWARD;
9468 if ((VIsual_mode != 'V'
9469 && curwin->w_cursor.col < curbuf->b_op_start.col)
9470 || (VIsual_mode == 'V'
9471 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9472 /* cursor is at the end of the line or end of file, put
9473 * forward. */
9474 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009475 /* May have been reset in do_put(). */
9476 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 do_put(cap->oap->regname, dir, cap->count1, flags);
9479
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 /* If a register was saved, put it back now. */
9481 if (reg2 != NULL)
9482 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009483
9484 /* What to reselect with "gv"? Selecting the just put text seems to
9485 * be the most useful, since the original text was removed. */
9486 if (was_visual)
9487 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009488 curbuf->b_visual.vi_start = curbuf->b_op_start;
9489 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01009490 /* need to adjust cursor position */
9491 if (*p_sel == 'e')
9492 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009493 }
9494
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009495 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009496 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009497 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009498 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009499 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009500
9501 /* If the cursor was in that line, move it to the end of the last
9502 * line. */
9503 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9504 {
9505 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9506 coladvance((colnr_T)MAXCOL);
9507 }
9508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 auto_format(FALSE, TRUE);
9510 }
9511}
9512
9513/*
9514 * "o" and "O" commands.
9515 */
9516 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009517nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518{
9519#ifdef FEAT_DIFF
9520 /* "do" is ":diffget" */
9521 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9522 {
9523 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009524 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 }
9526 else
9527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 if (VIsual_active) /* switch start and end of visual */
9529 v_swap_corners(cap->cmdchar);
9530 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 n_opencmd(cap);
9532}
9533
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534#ifdef FEAT_NETBEANS_INTG
9535 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009536nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537{
9538 netbeans_keycommand(cap->nchar);
9539}
9540#endif
9541
9542#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009544nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545{
9546 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9547}
9548#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009549
9550#ifdef FEAT_AUTOCMD
9551/*
9552 * Trigger CursorHold event.
9553 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9554 * input buffer. "did_cursorhold" is set to avoid retriggering.
9555 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009556 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009557nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00009558{
9559 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9560 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009561 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009562}
9563#endif
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009564
9565/*
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009566 * Calculate start/end virtual columns for operating in block mode.
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009567 */
9568 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009569get_op_vcol(
9570 oparg_T *oap,
9571 colnr_T redo_VIsual_vcol,
9572 int initial) /* when TRUE adjust position for 'selectmode' */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009573{
9574 colnr_T start, end;
9575
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009576 if (VIsual_mode != Ctrl_V
Bram Moolenaar02631462017-09-22 15:20:32 +02009577 || (!initial && oap->end.col < curwin->w_width))
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009578 return;
9579
Bram Moolenaar10ad1d92015-09-25 19:35:02 +02009580 oap->block_mode = TRUE;
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009581
9582#ifdef FEAT_MBYTE
9583 /* prevent from moving onto a trail byte */
9584 if (has_mbyte)
9585 mb_adjustpos(curwin->w_buffer, &oap->end);
9586#endif
9587
9588 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009589
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009590 if (!redo_VIsual_busy)
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009591 {
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009592 getvvcol(curwin, &(oap->end), &start, NULL, &end);
9593
9594 if (start < oap->start_vcol)
9595 oap->start_vcol = start;
9596 if (end > oap->end_vcol)
9597 {
9598 if (initial && *p_sel == 'e' && start >= 1
9599 && start - 1 >= oap->end_vcol)
9600 oap->end_vcol = start - 1;
9601 else
9602 oap->end_vcol = end;
9603 }
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009604 }
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009605
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009606 /* if '$' was used, get oap->end_vcol from longest line */
9607 if (curwin->w_curswant == MAXCOL)
9608 {
9609 curwin->w_cursor.col = MAXCOL;
9610 oap->end_vcol = 0;
9611 for (curwin->w_cursor.lnum = oap->start.lnum;
9612 curwin->w_cursor.lnum <= oap->end.lnum;
9613 ++curwin->w_cursor.lnum)
9614 {
9615 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
9616 if (end > oap->end_vcol)
9617 oap->end_vcol = end;
9618 }
9619 }
9620 else if (redo_VIsual_busy)
9621 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
9622 /*
9623 * Correct oap->end.col and oap->start.col to be the
9624 * upper-left and lower-right corner of the block area.
9625 *
9626 * (Actually, this does convert column positions into character
9627 * positions)
9628 */
9629 curwin->w_cursor.lnum = oap->end.lnum;
9630 coladvance(oap->end_vcol);
9631 oap->end = curwin->w_cursor;
9632
9633 curwin->w_cursor = oap->start;
9634 coladvance(oap->start_vcol);
9635 oap->start = curwin->w_cursor;
9636}