blob: e597e92458f65d861ba32517bac31666dde3d772 [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);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static void op_colon(oparg_T *oap);
36static void op_function(oparg_T *oap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010037#if defined(FEAT_MOUSE)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010038static void find_start_of_word(pos_T *);
39static void find_end_of_word(pos_T *);
40static int get_mouse_class(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void prep_redo(int regname, long, int, int, int, int, int);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010043static void clearop(oparg_T *oap);
44static void clearopbeep(oparg_T *oap);
45static void unshift_special(cmdarg_T *cap);
46static void may_clear_cmdline(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000047#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010048static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049#endif
50
51/*
52 * nv_*(): functions called to handle Normal and Visual mode commands.
53 * n_*(): functions called to handle Normal mode commands.
54 * v_*(): functions called to handle Visual mode commands.
55 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010056static void nv_ignore(cmdarg_T *cap);
57static void nv_nop(cmdarg_T *cap);
58static void nv_error(cmdarg_T *cap);
59static void nv_help(cmdarg_T *cap);
60static void nv_addsub(cmdarg_T *cap);
61static void nv_page(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#ifdef FEAT_MOUSE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010063static void nv_mousescroll(cmdarg_T *cap);
64static void nv_mouse(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010066static void nv_scroll_line(cmdarg_T *cap);
67static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000068#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010069static void nv_ver_scrollbar(cmdarg_T *cap);
70static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000072#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010073static void nv_tabline(cmdarg_T *cap);
74static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000075#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010076static void nv_exmode(cmdarg_T *cap);
77static void nv_colon(cmdarg_T *cap);
78static void nv_ctrlg(cmdarg_T *cap);
79static void nv_ctrlh(cmdarg_T *cap);
80static void nv_clear(cmdarg_T *cap);
81static void nv_ctrlo(cmdarg_T *cap);
82static void nv_hat(cmdarg_T *cap);
83static void nv_Zet(cmdarg_T *cap);
84static void nv_ident(cmdarg_T *cap);
85static void nv_tagpop(cmdarg_T *cap);
86static void nv_scroll(cmdarg_T *cap);
87static void nv_right(cmdarg_T *cap);
88static void nv_left(cmdarg_T *cap);
89static void nv_up(cmdarg_T *cap);
90static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010091static void nv_end(cmdarg_T *cap);
92static void nv_dollar(cmdarg_T *cap);
93static void nv_search(cmdarg_T *cap);
94static void nv_next(cmdarg_T *cap);
95static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt);
96static void nv_csearch(cmdarg_T *cap);
97static void nv_brackets(cmdarg_T *cap);
98static void nv_percent(cmdarg_T *cap);
99static void nv_brace(cmdarg_T *cap);
100static void nv_mark(cmdarg_T *cap);
101static void nv_findpar(cmdarg_T *cap);
102static void nv_undo(cmdarg_T *cap);
103static void nv_kundo(cmdarg_T *cap);
104static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100105static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100106static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
107static void v_visop(cmdarg_T *cap);
108static void nv_subst(cmdarg_T *cap);
109static void nv_abbrev(cmdarg_T *cap);
110static void nv_optrans(cmdarg_T *cap);
111static void nv_gomark(cmdarg_T *cap);
112static void nv_pcmark(cmdarg_T *cap);
113static void nv_regname(cmdarg_T *cap);
114static void nv_visual(cmdarg_T *cap);
115static void n_start_visual_mode(int c);
116static void nv_window(cmdarg_T *cap);
117static void nv_suspend(cmdarg_T *cap);
118static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100119static void nv_dot(cmdarg_T *cap);
120static void nv_redo(cmdarg_T *cap);
121static void nv_Undo(cmdarg_T *cap);
122static void nv_tilde(cmdarg_T *cap);
123static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000124#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100125static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000126#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100127static void nv_lineop(cmdarg_T *cap);
128static void nv_home(cmdarg_T *cap);
129static void nv_pipe(cmdarg_T *cap);
130static void nv_bck_word(cmdarg_T *cap);
131static void nv_wordcmd(cmdarg_T *cap);
132static void nv_beginline(cmdarg_T *cap);
133static void adjust_cursor(oparg_T *oap);
134static void adjust_for_sel(cmdarg_T *cap);
135static int unadjust_for_sel(void);
136static void nv_select(cmdarg_T *cap);
137static void nv_goto(cmdarg_T *cap);
138static void nv_normal(cmdarg_T *cap);
139static void nv_esc(cmdarg_T *oap);
140static void nv_edit(cmdarg_T *cap);
141static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100143static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100145static void nv_record(cmdarg_T *cap);
146static void nv_at(cmdarg_T *cap);
147static void nv_halfpage(cmdarg_T *cap);
148static void nv_join(cmdarg_T *cap);
149static void nv_put(cmdarg_T *cap);
150static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100152static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153#endif
154#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100155static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100157static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100158static void get_op_vcol(oparg_T *oap, colnr_T col, int initial);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000160static char *e_noident = N_("E349: No identifier under cursor");
161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Function to be called for a Normal or Visual mode command.
164 * The argument is a cmdarg_T.
165 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100166typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167
168/* Values for cmd_flags. */
169#define NV_NCH 0x01 /* may need to get a second char */
170#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
171#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
172#define NV_LANG 0x08 /* second char needs language adjustment */
173
174#define NV_SS 0x10 /* may start selection */
175#define NV_SSS 0x20 /* may start selection with shift modifier */
176#define NV_STS 0x40 /* may stop selection without shift modif. */
177#define NV_RL 0x80 /* 'rightleft' modifies command */
178#define NV_KEEPREG 0x100 /* don't clear regname */
179#define NV_NCW 0x200 /* not allowed in command-line window */
180
181/*
182 * Generally speaking, every Normal mode command should either clear any
183 * pending operator (with *clearop*()), or set the motion type variable
184 * oap->motion_type.
185 *
186 * When a cursor motion command is made, it is marked as being a character or
187 * line oriented motion. Then, if an operator is in effect, the operation
188 * becomes character or line oriented accordingly.
189 */
190
191/*
192 * This table contains one entry for every Normal or Visual mode command.
193 * The order doesn't matter, init_normal_cmds() will create a sorted index.
194 * It is faster when all keys from zero to '~' are present.
195 */
196static const struct nv_cmd
197{
198 int cmd_char; /* (first) command character */
199 nv_func_T cmd_func; /* function for this command */
200 short_u cmd_flags; /* NV_ flags */
201 short cmd_arg; /* value for ca.arg */
202} nv_cmds[] =
203{
204 {NUL, nv_error, 0, 0},
205 {Ctrl_A, nv_addsub, 0, 0},
206 {Ctrl_B, nv_page, NV_STS, BACKWARD},
207 {Ctrl_C, nv_esc, 0, TRUE},
208 {Ctrl_D, nv_halfpage, 0, 0},
209 {Ctrl_E, nv_scroll_line, 0, TRUE},
210 {Ctrl_F, nv_page, NV_STS, FORWARD},
211 {Ctrl_G, nv_ctrlg, 0, 0},
212 {Ctrl_H, nv_ctrlh, 0, 0},
213 {Ctrl_I, nv_pcmark, 0, 0},
214 {NL, nv_down, 0, FALSE},
215 {Ctrl_K, nv_error, 0, 0},
216 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar2c519cf2019-03-21 21:45:34 +0100217 {CAR, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 {Ctrl_N, nv_down, NV_STS, FALSE},
219 {Ctrl_O, nv_ctrlo, 0, 0},
220 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000221 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222 {Ctrl_R, nv_redo, 0, 0},
223 {Ctrl_S, nv_ignore, 0, 0},
224 {Ctrl_T, nv_tagpop, NV_NCW, 0},
225 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 {Ctrl_V, nv_visual, 0, FALSE},
227 {'V', nv_visual, 0, FALSE},
228 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 {Ctrl_W, nv_window, 0, 0},
230 {Ctrl_X, nv_addsub, 0, 0},
231 {Ctrl_Y, nv_scroll_line, 0, FALSE},
232 {Ctrl_Z, nv_suspend, 0, 0},
233 {ESC, nv_esc, 0, FALSE},
234 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
235 {Ctrl_RSB, nv_ident, NV_NCW, 0},
236 {Ctrl_HAT, nv_hat, NV_NCW, 0},
237 {Ctrl__, nv_error, 0, 0},
238 {' ', nv_right, 0, 0},
239 {'!', nv_operator, 0, 0},
240 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
241 {'#', nv_ident, 0, 0},
242 {'$', nv_dollar, 0, 0},
243 {'%', nv_percent, 0, 0},
244 {'&', nv_optrans, 0, 0},
245 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
246 {'(', nv_brace, 0, BACKWARD},
247 {')', nv_brace, 0, FORWARD},
248 {'*', nv_ident, 0, 0},
249 {'+', nv_down, 0, TRUE},
250 {',', nv_csearch, 0, TRUE},
251 {'-', nv_up, 0, TRUE},
252 {'.', nv_dot, NV_KEEPREG, 0},
253 {'/', nv_search, 0, FALSE},
254 {'0', nv_beginline, 0, 0},
255 {'1', nv_ignore, 0, 0},
256 {'2', nv_ignore, 0, 0},
257 {'3', nv_ignore, 0, 0},
258 {'4', nv_ignore, 0, 0},
259 {'5', nv_ignore, 0, 0},
260 {'6', nv_ignore, 0, 0},
261 {'7', nv_ignore, 0, 0},
262 {'8', nv_ignore, 0, 0},
263 {'9', nv_ignore, 0, 0},
264 {':', nv_colon, 0, 0},
265 {';', nv_csearch, 0, FALSE},
266 {'<', nv_operator, NV_RL, 0},
267 {'=', nv_operator, 0, 0},
268 {'>', nv_operator, NV_RL, 0},
269 {'?', nv_search, 0, FALSE},
270 {'@', nv_at, NV_NCH_NOP, FALSE},
271 {'A', nv_edit, 0, 0},
272 {'B', nv_bck_word, 0, 1},
273 {'C', nv_abbrev, NV_KEEPREG, 0},
274 {'D', nv_abbrev, NV_KEEPREG, 0},
275 {'E', nv_wordcmd, 0, TRUE},
276 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
277 {'G', nv_goto, 0, TRUE},
278 {'H', nv_scroll, 0, 0},
279 {'I', nv_edit, 0, 0},
280 {'J', nv_join, 0, 0},
281 {'K', nv_ident, 0, 0},
282 {'L', nv_scroll, 0, 0},
283 {'M', nv_scroll, 0, 0},
284 {'N', nv_next, 0, SEARCH_REV},
285 {'O', nv_open, 0, 0},
286 {'P', nv_put, 0, 0},
287 {'Q', nv_exmode, NV_NCW, 0},
288 {'R', nv_Replace, 0, FALSE},
289 {'S', nv_subst, NV_KEEPREG, 0},
290 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
291 {'U', nv_Undo, 0, 0},
292 {'W', nv_wordcmd, 0, TRUE},
293 {'X', nv_abbrev, NV_KEEPREG, 0},
294 {'Y', nv_abbrev, NV_KEEPREG, 0},
295 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
296 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
297 {'\\', nv_error, 0, 0},
298 {']', nv_brackets, NV_NCH_ALW, FORWARD},
299 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
300 {'_', nv_lineop, 0, 0},
301 {'`', nv_gomark, NV_NCH_ALW, FALSE},
302 {'a', nv_edit, NV_NCH, 0},
303 {'b', nv_bck_word, 0, 0},
304 {'c', nv_operator, 0, 0},
305 {'d', nv_operator, 0, 0},
306 {'e', nv_wordcmd, 0, FALSE},
307 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
308 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
309 {'h', nv_left, NV_RL, 0},
310 {'i', nv_edit, NV_NCH, 0},
311 {'j', nv_down, 0, FALSE},
312 {'k', nv_up, 0, FALSE},
313 {'l', nv_right, NV_RL, 0},
314 {'m', nv_mark, NV_NCH_NOP, 0},
315 {'n', nv_next, 0, 0},
316 {'o', nv_open, 0, 0},
317 {'p', nv_put, 0, 0},
318 {'q', nv_record, NV_NCH, 0},
319 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
320 {'s', nv_subst, NV_KEEPREG, 0},
321 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
322 {'u', nv_undo, 0, 0},
323 {'w', nv_wordcmd, 0, FALSE},
324 {'x', nv_abbrev, NV_KEEPREG, 0},
325 {'y', nv_operator, 0, 0},
326 {'z', nv_zet, NV_NCH_ALW, 0},
327 {'{', nv_findpar, 0, BACKWARD},
328 {'|', nv_pipe, 0, 0},
329 {'}', nv_findpar, 0, FORWARD},
330 {'~', nv_tilde, 0, 0},
331
332 /* pound sign */
333 {POUND, nv_ident, 0, 0},
334#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200335 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
336 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
337 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
338 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 {K_LEFTMOUSE, nv_mouse, 0, 0},
340 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
341 {K_LEFTDRAG, nv_mouse, 0, 0},
342 {K_LEFTRELEASE, nv_mouse, 0, 0},
343 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100344 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
346 {K_MIDDLEDRAG, nv_mouse, 0, 0},
347 {K_MIDDLERELEASE, nv_mouse, 0, 0},
348 {K_RIGHTMOUSE, nv_mouse, 0, 0},
349 {K_RIGHTDRAG, nv_mouse, 0, 0},
350 {K_RIGHTRELEASE, nv_mouse, 0, 0},
351 {K_X1MOUSE, nv_mouse, 0, 0},
352 {K_X1DRAG, nv_mouse, 0, 0},
353 {K_X1RELEASE, nv_mouse, 0, 0},
354 {K_X2MOUSE, nv_mouse, 0, 0},
355 {K_X2DRAG, nv_mouse, 0, 0},
356 {K_X2RELEASE, nv_mouse, 0, 0},
357#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000358 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000359 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 {K_INS, nv_edit, 0, 0},
361 {K_KINS, nv_edit, 0, 0},
362 {K_BS, nv_ctrlh, 0, 0},
363 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
364 {K_S_UP, nv_page, NV_SS, BACKWARD},
365 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
366 {K_S_DOWN, nv_page, NV_SS, FORWARD},
367 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
368 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
369 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
370 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
371 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
372 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
373 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
374 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
375 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
376 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
377 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
378 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 {K_S_END, nv_end, NV_SS, FALSE},
380 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
381 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
382 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {K_S_HOME, nv_home, NV_SS, 0},
384 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
385 {K_DEL, nv_abbrev, 0, 0},
386 {K_KDEL, nv_abbrev, 0, 0},
387 {K_UNDO, nv_kundo, 0, 0},
388 {K_HELP, nv_help, NV_NCW, 0},
389 {K_F1, nv_help, NV_NCW, 0},
390 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#ifdef FEAT_GUI
393 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
394 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
395#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000396#ifdef FEAT_GUI_TABLINE
397 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000398 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400#ifdef FEAT_NETBEANS_INTG
401 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
402#endif
403#ifdef FEAT_DND
404 {K_DROP, nv_drop, NV_STS, 0},
405#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000406 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100407 {K_PS, nv_edit, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408};
409
410/* Number of commands in nv_cmds[]. */
411#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
412
413/* Sorted index of commands in nv_cmds[]. */
414static short nv_cmd_idx[NV_CMDS_SIZE];
415
416/* The highest index for which
417 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
418static int nv_max_linear;
419
420/*
421 * Compare functions for qsort() below, that checks the command character
422 * through the index in nv_cmd_idx[].
423 */
424 static int
425#ifdef __BORLANDC__
426_RTLENTRYF
427#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +0100428nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429{
430 int c1, c2;
431
432 /* The commands are sorted on absolute value. */
433 c1 = nv_cmds[*(const short *)s1].cmd_char;
434 c2 = nv_cmds[*(const short *)s2].cmd_char;
435 if (c1 < 0)
436 c1 = -c1;
437 if (c2 < 0)
438 c2 = -c2;
439 return c1 - c2;
440}
441
442/*
443 * Initialize the nv_cmd_idx[] table.
444 */
445 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100446init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447{
448 int i;
449
450 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000451 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 nv_cmd_idx[i] = i;
453
454 /* Sort the commands by the command character. */
455 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
456
457 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000458 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
460 break;
461 nv_max_linear = i - 1;
462}
463
464/*
465 * Search for a command in the commands table.
466 * Returns -1 for invalid command.
467 */
468 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100469find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470{
471 int i;
472 int idx;
473 int top, bot;
474 int c;
475
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 /* A multi-byte character is never a command. */
477 if (cmdchar >= 0x100)
478 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479
480 /* We use the absolute value of the character. Special keys have a
481 * negative value, but are sorted on their absolute value. */
482 if (cmdchar < 0)
483 cmdchar = -cmdchar;
484
485 /* If the character is in the first part: The character is the index into
486 * nv_cmd_idx[]. */
487 if (cmdchar <= nv_max_linear)
488 return nv_cmd_idx[cmdchar];
489
490 /* Perform a binary search. */
491 bot = nv_max_linear + 1;
492 top = NV_CMDS_SIZE - 1;
493 idx = -1;
494 while (bot <= top)
495 {
496 i = (top + bot) / 2;
497 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
498 if (c < 0)
499 c = -c;
500 if (cmdchar == c)
501 {
502 idx = nv_cmd_idx[i];
503 break;
504 }
505 if (cmdchar > c)
506 bot = i + 1;
507 else
508 top = i - 1;
509 }
510 return idx;
511}
512
513/*
514 * Execute a command in Normal mode.
515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100517normal_cmd(
518 oparg_T *oap,
519 int toplevel UNUSED) /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 cmdarg_T ca; /* command arguments */
522 int c;
523 int ctrl_w = FALSE; /* got CTRL-W command */
524 int old_col = curwin->w_curswant;
525#ifdef FEAT_CMDL_INFO
526 int need_flushbuf; /* need to call out_flush() */
527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 pos_T old_pos; /* cursor position before command */
529 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 static int old_mapped_len = 0;
531 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000532#ifdef FEAT_EVAL
533 int set_prevcount = FALSE;
534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535
536 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
537 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000538
539 /* Use a count remembered from before entering an operator. After typing
540 * "3d" we return from normal_cmd() and come back here, the "3" is
541 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 ca.opcount = opcount;
543
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 /*
545 * If there is an operator pending, then the command we take this time
546 * will terminate it. Finish_op tells us to finish the operation before
547 * returning this time (unless the operation was cancelled).
548 */
549#ifdef CURSOR_SHAPE
550 c = finish_op;
551#endif
552 finish_op = (oap->op_type != OP_NOP);
553#ifdef CURSOR_SHAPE
554 if (finish_op != c)
555 {
556 ui_cursor_shape(); /* may show different cursor shape */
557# ifdef FEAT_MOUSESHAPE
558 update_mouseshape(-1);
559# endif
560 }
561#endif
562
Bram Moolenaara983fe92008-07-31 20:04:27 +0000563 /* When not finishing an operator and no register name typed, reset the
564 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000566 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000568#ifdef FEAT_EVAL
569 set_prevcount = TRUE;
570#endif
571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572
Bram Moolenaara983fe92008-07-31 20:04:27 +0000573 /* Restore counts from before receiving K_CURSORHOLD. This means after
574 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
575 * "3 * 2". */
576 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
577 {
578 ca.opcount = oap->prev_opcount;
579 ca.count0 = oap->prev_count0;
580 oap->prev_opcount = 0;
581 oap->prev_count0 = 0;
582 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000583
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585
586 State = NORMAL_BUSY;
587#ifdef USE_ON_FLY_SCROLL
588 dont_scroll = FALSE; /* allow scrolling here */
589#endif
590
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100591#ifdef FEAT_EVAL
592 /* Set v:count here, when called from main() and not a stuffed
593 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100594 * when there is no count. Do set it for redo. */
595 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100596 set_vcount_ca(&ca, &set_prevcount);
597#endif
598
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 /*
600 * Get the command character from the user.
601 */
602 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100603 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604
605 /*
606 * If a mapping was started in Visual or Select mode, remember the length
607 * of the mapping. This is used below to not return to Insert mode for as
608 * long as the mapping is being executed.
609 */
610 if (restart_edit == 0)
611 old_mapped_len = 0;
612 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000613 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 old_mapped_len = typebuf_maplen();
615
616 if (c == NUL)
617 c = K_ZERO;
618
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 /*
620 * In Select mode, typed text replaces the selection.
621 */
622 if (VIsual_active
623 && VIsual_select
624 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
625 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000626 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
627 * 'insertmode' is set) fake a "d"elete command, Insert mode will
628 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000629 * Insert the typed character in the typeahead buffer, so that it can
630 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000631 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000632 if (restart_edit != 0)
633 c = 'd';
634 else
635 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000636 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar9bad29d2013-05-21 12:46:02 +0200637 old_mapped_len = 0; /* do go to Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
640#ifdef FEAT_CMDL_INFO
641 need_flushbuf = add_to_showcmd(c);
642#endif
643
644getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 {
647 /*
648 * Handle a count before a command and compute ca.count0.
649 * Note that '0' is a command and not the start of a count, but it's
650 * part of a count after other digits.
651 */
652 while ( (c >= '1' && c <= '9')
653 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
654 {
655 if (c == K_DEL || c == K_KDEL)
656 {
657 ca.count0 /= 10;
658#ifdef FEAT_CMDL_INFO
659 del_from_showcmd(4); /* delete the digit and ~@% */
660#endif
661 }
662 else
663 ca.count0 = ca.count0 * 10 + (c - '0');
664 if (ca.count0 < 0) /* got too large! */
665 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000666#ifdef FEAT_EVAL
667 /* Set v:count here, when called from main() and not a stuffed
668 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100669 * right after the count. Do set it for redo. */
670 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100671 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 if (ctrl_w)
674 {
675 ++no_mapping;
676 ++allow_keys; /* no mapping for nchar, but keys */
677 }
678 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000679 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 --no_zero_mapping;
682 if (ctrl_w)
683 {
684 --no_mapping;
685 --allow_keys;
686 }
687#ifdef FEAT_CMDL_INFO
688 need_flushbuf |= add_to_showcmd(c);
689#endif
690 }
691
692 /*
693 * If we got CTRL-W there may be a/another count
694 */
695 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
696 {
697 ctrl_w = TRUE;
698 ca.opcount = ca.count0; /* remember first count */
699 ca.count0 = 0;
700 ++no_mapping;
701 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000702 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 --no_mapping;
705 --allow_keys;
706#ifdef FEAT_CMDL_INFO
707 need_flushbuf |= add_to_showcmd(c);
708#endif
709 goto getcount; /* jump back */
710 }
711 }
712
Bram Moolenaara983fe92008-07-31 20:04:27 +0000713 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000715 /* Save the count values so that ca.opcount and ca.count0 are exactly
716 * the same when coming back here after handling K_CURSORHOLD. */
717 oap->prev_opcount = ca.opcount;
718 oap->prev_count0 = ca.count0;
719 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100720 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000721 {
722 /*
723 * If we're in the middle of an operator (including after entering a
724 * yank buffer with '"') AND we had a count before the operator, then
725 * that count overrides the current value of ca.count0.
726 * What this means effectively, is that commands like "3dw" get turned
727 * into "d3w" which makes things fall into place pretty neatly.
728 * If you give a count before AND after the operator, they are
729 * multiplied.
730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 if (ca.count0)
732 ca.count0 *= ca.opcount;
733 else
734 ca.count0 = ca.opcount;
735 }
736
737 /*
738 * Always remember the count. It will be set to zero (on the next call,
739 * above) when there is no pending operator.
740 * When called from main(), save the count for use by the "count" built-in
741 * variable.
742 */
743 ca.opcount = ca.count0;
744 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
745
746#ifdef FEAT_EVAL
747 /*
748 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100749 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100751 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000752 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753#endif
754
755 /*
756 * Find the command character in the table of commands.
757 * For CTRL-W we already got nchar when looking for a count.
758 */
759 if (ctrl_w)
760 {
761 ca.nchar = c;
762 ca.cmdchar = Ctrl_W;
763 }
764 else
765 ca.cmdchar = c;
766 idx = find_command(ca.cmdchar);
767 if (idx < 0)
768 {
769 /* Not a known command: beep. */
770 clearopbeep(oap);
771 goto normal_end;
772 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000773
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000774 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 {
Bram Moolenaard69bd9a2014-04-29 12:15:40 +0200776 /* This command is not allowed while editing a cmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000778 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 goto normal_end;
780 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000781 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
782 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 /*
785 * In Visual/Select mode, a few keys are handled in a special way.
786 */
787 if (VIsual_active)
788 {
789 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
790 if (km_stopsel
791 && (nv_cmds[idx].cmd_flags & NV_STS)
792 && !(mod_mask & MOD_MASK_SHIFT))
793 {
794 end_visual_mode();
795 redraw_curbuf_later(INVERTED);
796 }
797
798 /* Keys that work different when 'keymodel' contains "startsel" */
799 if (km_startsel)
800 {
801 if (nv_cmds[idx].cmd_flags & NV_SS)
802 {
803 unshift_special(&ca);
804 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000805 if (idx < 0)
806 {
807 /* Just in case */
808 clearopbeep(oap);
809 goto normal_end;
810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 }
812 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
813 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 }
816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817
818#ifdef FEAT_RIGHTLEFT
819 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
820 && (nv_cmds[idx].cmd_flags & NV_RL))
821 {
822 /* Invert horizontal movements and operations. Only when typed by the
823 * user directly, not when the result of a mapping or "x" translated
824 * to "dl". */
825 switch (ca.cmdchar)
826 {
827 case 'l': ca.cmdchar = 'h'; break;
828 case K_RIGHT: ca.cmdchar = K_LEFT; break;
829 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
830 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
831 case 'h': ca.cmdchar = 'l'; break;
832 case K_LEFT: ca.cmdchar = K_RIGHT; break;
833 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
834 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
835 case '>': ca.cmdchar = '<'; break;
836 case '<': ca.cmdchar = '>'; break;
837 }
838 idx = find_command(ca.cmdchar);
839 }
840#endif
841
842 /*
843 * Get an additional character if we need one.
844 */
845 if ((nv_cmds[idx].cmd_flags & NV_NCH)
846 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
847 && oap->op_type == OP_NOP)
848 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
849 || (ca.cmdchar == 'q'
850 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200851 && reg_recording == 0
852 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100854 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 {
856 int *cp;
857 int repl = FALSE; /* get character for replace mode */
858 int lit = FALSE; /* get extra character literally */
859 int langmap_active = FALSE; /* using :lmap mappings */
860 int lang; /* getting a text character */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100861#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 int save_smd; /* saved value of p_smd */
863#endif
864
865 ++no_mapping;
866 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000867 /* Don't generate a CursorHold event here, most commands can't handle
868 * it, e.g., nv_replace(), nv_csearch(). */
869 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 if (ca.cmdchar == 'g')
871 {
872 /*
873 * For 'g' get the next character now, so that we can check for
874 * "gr", "g'" and "g`".
875 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000876 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878#ifdef FEAT_CMDL_INFO
879 need_flushbuf |= add_to_showcmd(ca.nchar);
880#endif
881 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100882 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 {
884 cp = &ca.extra_char; /* need to get a third character */
885 if (ca.nchar != 'r')
886 lit = TRUE; /* get it literally */
887 else
888 repl = TRUE; /* get it in replace mode */
889 }
890 else
891 cp = NULL; /* no third character needed */
892 }
893 else
894 {
895 if (ca.cmdchar == 'r') /* get it in replace mode */
896 repl = TRUE;
897 cp = &ca.nchar;
898 }
899 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
900
901 /*
902 * Get a second or third character.
903 */
904 if (cp != NULL)
905 {
906#ifdef CURSOR_SHAPE
907 if (repl)
908 {
909 State = REPLACE; /* pretend Replace mode */
910 ui_cursor_shape(); /* show different cursor shape */
911 }
912#endif
913 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
914 {
915 /* Allow mappings defined with ":lmap". */
916 --no_mapping;
917 --allow_keys;
918 if (repl)
919 State = LREPLACE;
920 else
921 State = LANGMAP;
922 langmap_active = TRUE;
923 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100924#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 save_smd = p_smd;
926 p_smd = FALSE; /* Don't let the IM code show the mode here */
927 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
928 im_set_active(TRUE);
929#endif
930
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000931 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932
933 if (langmap_active)
934 {
935 /* Undo the decrement done above */
936 ++no_mapping;
937 ++allow_keys;
938 State = NORMAL_BUSY;
939 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100940#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 if (lang)
942 {
943 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
944 im_save_status(&curbuf->b_p_iminsert);
945 im_set_active(FALSE);
946 }
947 p_smd = save_smd;
948#endif
949#ifdef CURSOR_SHAPE
950 State = NORMAL_BUSY;
951#endif
952#ifdef FEAT_CMDL_INFO
953 need_flushbuf |= add_to_showcmd(*cp);
954#endif
955
956 if (!lit)
957 {
958#ifdef FEAT_DIGRAPHS
959 /* Typing CTRL-K gets a digraph. */
960 if (*cp == Ctrl_K
961 && ((nv_cmds[idx].cmd_flags & NV_LANG)
962 || cp == &ca.extra_char)
963 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
964 {
965 c = get_digraph(FALSE);
966 if (c > 0)
967 {
968 *cp = c;
969# ifdef FEAT_CMDL_INFO
970 /* Guessing how to update showcmd here... */
971 del_from_showcmd(3);
972 need_flushbuf |= add_to_showcmd(*cp);
973# endif
974 }
975 }
976#endif
977
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 /* adjust chars > 127, except after "tTfFr" commands */
979 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980#ifdef FEAT_RIGHTLEFT
981 /* adjust Hebrew mapped char */
982 if (p_hkmap && lang && KeyTyped)
983 *cp = hkmap(*cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984#endif
985 }
986
987 /*
988 * When the next character is CTRL-\ a following CTRL-N means the
989 * command is aborted and we go to Normal mode.
990 */
991 if (cp == &ca.extra_char
992 && ca.nchar == Ctrl_BSL
993 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
994 {
995 ca.cmdchar = Ctrl_BSL;
996 ca.nchar = ca.extra_char;
997 idx = find_command(ca.cmdchar);
998 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +0200999 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001000 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 else if (*cp == Ctrl_BSL)
1002 {
1003 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1004
1005 /* There is a busy wait here when typing "f<C-\>" and then
1006 * something different from CTRL-N. Can't be avoided. */
1007 while ((c = vpeekc()) <= 0 && towait > 0L)
1008 {
1009 do_sleep(towait > 50L ? 50L : towait);
1010 towait -= 50L;
1011 }
1012 if (c > 0)
1013 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001014 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 if (c != Ctrl_N && c != Ctrl_G)
1016 vungetc(c);
1017 else
1018 {
1019 ca.cmdchar = Ctrl_BSL;
1020 ca.nchar = c;
1021 idx = find_command(ca.cmdchar);
1022 }
1023 }
1024 }
1025
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 /* When getting a text character and the next character is a
1027 * multi-byte character, it could be a composing character.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001028 * However, don't wait for it to arrive. Also, do enable mapping,
1029 * because if it's put back with vungetc() it's too late to apply
1030 * mapping. */
1031 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 while (enc_utf8 && lang && (c = vpeekc()) > 0
1033 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1034 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001035 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 if (!utf_iscomposing(c))
1037 {
1038 vungetc(c); /* it wasn't, put it back */
1039 break;
1040 }
1041 else if (ca.ncharC1 == 0)
1042 ca.ncharC1 = c;
1043 else
1044 ca.ncharC2 = c;
1045 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001046 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 }
1048 --no_mapping;
1049 --allow_keys;
1050 }
1051
1052#ifdef FEAT_CMDL_INFO
1053 /*
1054 * Flush the showcmd characters onto the screen so we can see them while
1055 * the command is being executed. Only do this when the shown command was
1056 * actually displayed, otherwise this will slow down a lot when executing
1057 * mappings.
1058 */
1059 if (need_flushbuf)
1060 out_flush();
1061#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001062 if (ca.cmdchar != K_IGNORE)
1063 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064
1065 State = NORMAL;
1066
1067 if (ca.nchar == ESC)
1068 {
1069 clearop(oap);
1070 if (restart_edit == 0 && goto_im())
1071 restart_edit = 'a';
1072 goto normal_end;
1073 }
1074
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001075 if (ca.cmdchar != K_IGNORE)
1076 {
1077 msg_didout = FALSE; /* don't scroll screen up for normal command */
1078 msg_col = 0;
1079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 old_pos = curwin->w_cursor; /* remember where cursor was */
1082
1083 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1084 * mode. */
1085 if (!VIsual_active && km_startsel)
1086 {
1087 if (nv_cmds[idx].cmd_flags & NV_SS)
1088 {
1089 start_selection();
1090 unshift_special(&ca);
1091 idx = find_command(ca.cmdchar);
1092 }
1093 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1094 && (mod_mask & MOD_MASK_SHIFT))
1095 {
1096 start_selection();
1097 mod_mask &= ~MOD_MASK_SHIFT;
1098 }
1099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100
1101 /*
1102 * Execute the command!
1103 * Call the command function found in the commands table.
1104 */
1105 ca.arg = nv_cmds[idx].cmd_arg;
1106 (nv_cmds[idx].cmd_func)(&ca);
1107
1108 /*
1109 * If we didn't start or finish an operator, reset oap->regname, unless we
1110 * need it later.
1111 */
1112 if (!finish_op
1113 && !oap->op_type
1114 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1115 {
1116 clearop(oap);
1117#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001118 {
1119 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001120
Bram Moolenaar536681b2011-05-10 16:12:45 +02001121 /* Adjust the register according to 'clipboard', so that when
1122 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001123# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001124 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001125# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001126 set_reg_var(regname);
1127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128#endif
1129 }
1130
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001131 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001132 * character or "z333<cr>". */
1133 if (old_mapped_len > 0)
1134 old_mapped_len = typebuf_maplen();
1135
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 /*
1137 * If an operation is pending, handle it...
1138 */
1139 do_pending_operator(&ca, old_col, FALSE);
1140
1141 /*
1142 * Wait for a moment when a message is displayed that will be overwritten
1143 * by the mode message.
1144 * In Visual mode and with "^O" in Insert mode, a short message will be
1145 * overwritten by the mode message. Wait a bit, until a key is hit.
1146 * In Visual mode, it's more important to keep the Visual area updated
1147 * than keeping a message (e.g. from a /pat search).
1148 * Only do this if the command was typed, not from a mapping.
1149 * Don't wait when emsg_silent is non-zero.
1150 * Also wait a bit after an error message, e.g. for "^O:".
1151 * Don't redraw the screen, it would remove the message.
1152 */
1153 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001154 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 || (VIsual_active
1157 && old_pos.lnum == curwin->w_cursor.lnum
1158 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 )
1160 && (clear_cmdline
1161 || redraw_cmdline)
1162 && (msg_didout || (msg_didany && msg_scroll))
1163 && !msg_nowait
1164 && KeyTyped)
1165 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 && (msg_scroll
1168 || emsg_on_display)))
1169 && oap->regname == 0
1170 && !(ca.retval & CA_COMMAND_BUSY)
1171 && stuff_empty()
1172 && typebuf_typed()
1173 && emsg_silent == 0
1174 && !did_wait_return
1175 && oap->op_type == OP_NOP)
1176 {
1177 int save_State = State;
1178
1179 /* Draw the cursor with the right shape here */
1180 if (restart_edit != 0)
1181 State = INSERT;
1182
1183 /* If need to redraw, and there is a "keep_msg", redraw before the
1184 * delay */
1185 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1186 {
1187 char_u *kmsg;
1188
1189 kmsg = keep_msg;
1190 keep_msg = NULL;
1191 /* showmode() will clear keep_msg, but we want to use it anyway */
1192 update_screen(0);
1193 /* now reset it, otherwise it's put in the history again */
1194 keep_msg = kmsg;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001195 msg_attr((char *)kmsg, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 vim_free(kmsg);
1197 }
1198 setcursor();
1199 cursor_on();
1200 out_flush();
1201 if (msg_scroll || emsg_on_display)
1202 ui_delay(1000L, TRUE); /* wait at least one second */
1203 ui_delay(3000L, FALSE); /* wait up to three seconds */
1204 State = save_State;
1205
1206 msg_scroll = FALSE;
1207 emsg_on_display = FALSE;
1208 }
1209
1210 /*
1211 * Finish up after executing a Normal mode command.
1212 */
1213normal_end:
1214
1215 msg_nowait = FALSE;
1216
1217 /* Reset finish_op, in case it was set */
1218#ifdef CURSOR_SHAPE
1219 c = finish_op;
1220#endif
1221 finish_op = FALSE;
1222#ifdef CURSOR_SHAPE
1223 /* Redraw the cursor with another shape, if we were in Operator-pending
1224 * mode or did a replace command. */
1225 if (c || ca.cmdchar == 'r')
1226 {
1227 ui_cursor_shape(); /* may show different cursor shape */
1228# ifdef FEAT_MOUSESHAPE
1229 update_mouseshape(-1);
1230# endif
1231 }
1232#endif
1233
1234#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001235 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001236 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 clear_showcmd();
1238#endif
1239
1240 checkpcmark(); /* check if we moved since setting pcmark */
1241 vim_free(ca.searchbuf);
1242
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 if (has_mbyte)
1244 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 if (curwin->w_p_scb && toplevel)
1247 {
1248 validate_cursor(); /* may need to update w_leftcol */
1249 do_check_scrollbind(TRUE);
1250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251
Bram Moolenaar860cae12010-06-05 23:22:07 +02001252 if (curwin->w_p_crb && toplevel)
1253 {
1254 validate_cursor(); /* may need to update w_leftcol */
1255 do_check_cursorbind();
1256 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001257
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001258#ifdef FEAT_TERMINAL
Bram Moolenaareef9add2017-09-16 15:38:04 +02001259 /* don't go to Insert mode if a terminal has a running job */
1260 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001261 restart_edit = 0;
1262#endif
1263
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 /*
1265 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1266 * if still inside a mapping that started in Visual mode).
1267 * May switch from Visual to Select mode after CTRL-O command.
1268 */
1269 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1271 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 && !(ca.retval & CA_COMMAND_BUSY)
1273 && stuff_empty()
1274 && oap->regname == 0)
1275 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 if (restart_VIsual_select == 1)
1277 {
1278 VIsual_select = TRUE;
1279 showmode();
1280 restart_VIsual_select = 0;
1281 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001282 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 (void)edit(restart_edit, FALSE, 1L);
1284 }
1285
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 if (restart_VIsual_select == 2)
1287 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288
1289 /* Save count before an operator for next time. */
1290 opcount = ca.opcount;
1291}
1292
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001293#ifdef FEAT_EVAL
1294/*
1295 * Set v:count and v:count1 according to "cap".
1296 * Set v:prevcount only when "set_prevcount" is TRUE.
1297 */
1298 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001299set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001300{
1301 long count = cap->count0;
1302
1303 /* multiply with cap->opcount the same way as above */
1304 if (cap->opcount != 0)
1305 count = cap->opcount * (count == 0 ? 1 : count);
1306 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1307 *set_prevcount = FALSE; /* only set v:prevcount once */
1308}
1309#endif
1310
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311/*
Bram Moolenaar5823f842019-01-03 22:58:08 +01001312 * Handle an operator after Visual mode or when the movement is finished.
1313 * "gui_yank" is true when yanking text for the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 */
1315 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001316do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317{
1318 oparg_T *oap = cap->oap;
1319 pos_T old_cursor;
1320 int empty_region_error;
1321 int restart_edit_save;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001322#ifdef FEAT_LINEBREAK
1323 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 /* The visual area is remembered for redo */
1327 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1328 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001329 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaard79e5502016-01-10 22:13:02 +01001331 static int redo_VIsual_arg; /* extra argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 int include_line_break = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333
1334#if defined(FEAT_CLIPBOARD)
1335 /*
1336 * Yank the visual area into the GUI selection register before we operate
1337 * on it and lose it forever.
1338 * Don't do it if a specific register was specified, so that ""x"*P works.
1339 * This could call do_pending_operator() recursively, but that's OK
1340 * because gui_yank will be TRUE for the nested call.
1341 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001342 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 && oap->op_type != OP_NOP
1344 && !gui_yank
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 && VIsual_active
1346 && !redo_VIsual_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 && oap->regname == 0)
1348 clip_auto_select();
1349#endif
1350 old_cursor = curwin->w_cursor;
1351
1352 /*
1353 * If an operation is pending, handle it...
1354 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001355 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 {
Bram Moolenaar5823f842019-01-03 22:58:08 +01001357 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
1358 // for the clipboard.
1359 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
1360
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001361#ifdef FEAT_LINEBREAK
1362 /* Avoid a problem with unwanted linebreaks in block mode. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001363 if (curwin->w_p_lbr)
1364 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001365 curwin->w_p_lbr = FALSE;
1366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 oap->is_VIsual = VIsual_active;
1368 if (oap->motion_force == 'V')
1369 oap->motion_type = MLINE;
1370 else if (oap->motion_force == 'v')
1371 {
1372 /* If the motion was linewise, "inclusive" will not have been set.
1373 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1374 if (oap->motion_type == MLINE)
1375 oap->inclusive = FALSE;
1376 /* If the motion already was characterwise, toggle "inclusive" */
1377 else if (oap->motion_type == MCHAR)
1378 oap->inclusive = !oap->inclusive;
1379 oap->motion_type = MCHAR;
1380 }
1381 else if (oap->motion_force == Ctrl_V)
1382 {
1383 /* Change line- or characterwise motion into Visual block mode. */
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01001384 if (!VIsual_active)
1385 {
1386 VIsual_active = TRUE;
1387 VIsual = oap->start;
1388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 VIsual_mode = Ctrl_V;
1390 VIsual_select = FALSE;
1391 VIsual_reselect = FALSE;
1392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393
Bram Moolenaar7afea822013-04-24 18:34:45 +02001394 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1395 /* Never redo "zf" (define fold). */
Bram Moolenaar5823f842019-01-03 22:58:08 +01001396 if ((redo_yank || oap->op_type != OP_YANK)
Bram Moolenaar7afea822013-04-24 18:34:45 +02001397 && ((!VIsual_active || oap->motion_force)
1398 /* Also redo Operator-pending Visual mode mappings */
1399 || (VIsual_active && cap->cmdchar == ':'
1400 && oap->op_type != OP_COLON))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001401 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402#ifdef FEAT_FOLDING
1403 && oap->op_type != OP_FOLD
1404 && oap->op_type != OP_FOLDOPEN
1405 && oap->op_type != OP_FOLDOPENREC
1406 && oap->op_type != OP_FOLDCLOSE
1407 && oap->op_type != OP_FOLDCLOSEREC
1408 && oap->op_type != OP_FOLDDEL
1409 && oap->op_type != OP_FOLDDELREC
1410#endif
1411 )
1412 {
1413 prep_redo(oap->regname, cap->count0,
1414 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1415 oap->motion_force, cap->cmdchar, cap->nchar);
1416 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1417 {
1418 /*
1419 * If 'cpoptions' does not contain 'r', insert the search
1420 * pattern to really repeat the same command.
1421 */
1422 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001423 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 AppendToRedobuff(NL_STR);
1425 }
1426 else if (cap->cmdchar == ':')
1427 {
1428 /* do_cmdline() has stored the first typed line in
1429 * "repeat_cmdline". When several lines are typed repeating
1430 * won't be possible. */
1431 if (repeat_cmdline == NULL)
1432 ResetRedobuff();
1433 else
1434 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001435 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 AppendToRedobuff(NL_STR);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001437 VIM_CLEAR(repeat_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 }
1439 }
1440 }
1441
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 if (redo_VIsual_busy)
1443 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001444 /* Redo of an operation on a Visual area. Use the same size from
1445 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 oap->start = curwin->w_cursor;
1447 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1448 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1449 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1450 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001451 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001453 if (VIsual_mode == 'v')
1454 {
1455 if (redo_VIsual_line_count <= 1)
1456 {
1457 validate_virtcol();
1458 curwin->w_curswant =
1459 curwin->w_virtcol + redo_VIsual_vcol - 1;
1460 }
1461 else
1462 curwin->w_curswant = redo_VIsual_vcol;
1463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001465 {
1466 curwin->w_curswant = MAXCOL;
1467 }
1468 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 }
1470 cap->count0 = redo_VIsual_count;
1471 if (redo_VIsual_count != 0)
1472 cap->count1 = redo_VIsual_count;
1473 else
1474 cap->count1 = 1;
1475 }
1476 else if (VIsual_active)
1477 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001478 if (!gui_yank)
1479 {
1480 /* Save the current VIsual area for '< and '> marks, and "gv" */
1481 curbuf->b_visual.vi_start = VIsual;
1482 curbuf->b_visual.vi_end = curwin->w_cursor;
1483 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001484 if (VIsual_mode_orig != NUL)
1485 {
1486 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1487 VIsual_mode_orig = NUL;
1488 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001489 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001491 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494
1495 /* In Select mode, a linewise selection is operated upon like a
Bram Moolenaard009e862015-06-09 20:20:03 +02001496 * characterwise selection.
1497 * Special case: gH<Del> deletes the last line. */
1498 if (VIsual_select && VIsual_mode == 'V'
1499 && cap->oap->op_type != OP_DELETE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001501 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 {
1503 VIsual.col = 0;
1504 curwin->w_cursor.col =
1505 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1506 }
1507 else
1508 {
1509 curwin->w_cursor.col = 0;
1510 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1511 }
1512 VIsual_mode = 'v';
1513 }
1514 /* If 'selection' is "exclusive", backup one character for
1515 * charwise selections. */
1516 else if (VIsual_mode == 'v')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001517 include_line_break = unadjust_for_sel();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518
1519 oap->start = VIsual;
1520 if (VIsual_mode == 'V')
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001521 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 oap->start.col = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001523 oap->start.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
1527 /*
1528 * Set oap->start to the first position of the operated text, oap->end
1529 * to the end of the operated text. w_cursor is equal to oap->start.
1530 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001531 if (LT_POS(oap->start, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 {
1533#ifdef FEAT_FOLDING
1534 /* Include folded lines completely. */
1535 if (!VIsual_active)
1536 {
1537 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1538 oap->start.col = 0;
1539 if (hasFolding(curwin->w_cursor.lnum, NULL,
1540 &curwin->w_cursor.lnum))
1541 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1542 }
1543#endif
1544 oap->end = curwin->w_cursor;
1545 curwin->w_cursor = oap->start;
1546
1547 /* w_virtcol may have been updated; if the cursor goes back to its
1548 * previous position w_virtcol becomes invalid and isn't updated
1549 * automatically. */
1550 curwin->w_valid &= ~VALID_VIRTCOL;
1551 }
1552 else
1553 {
1554#ifdef FEAT_FOLDING
1555 /* Include folded lines completely. */
1556 if (!VIsual_active && oap->motion_type == MLINE)
1557 {
1558 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1559 NULL))
1560 curwin->w_cursor.col = 0;
1561 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1562 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1563 }
1564#endif
1565 oap->end = oap->start;
1566 oap->start = curwin->w_cursor;
1567 }
1568
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001569 /* Just in case lines were deleted that make the position invalid. */
1570 check_pos(curwin->w_buffer, &oap->end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1572
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 /* Set "virtual_op" before resetting VIsual_active. */
1574 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 if (VIsual_active || redo_VIsual_busy)
1577 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001578 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
1580 if (!redo_VIsual_busy && !gui_yank)
1581 {
1582 /*
1583 * Prepare to reselect and redo Visual: this is based on the
1584 * size of the Visual text
1585 */
1586 resel_VIsual_mode = VIsual_mode;
1587 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001588 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001590 {
1591 if (VIsual_mode != Ctrl_V)
1592 getvvcol(curwin, &(oap->end),
1593 NULL, NULL, &oap->end_vcol);
1594 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1595 {
1596 if (VIsual_mode != Ctrl_V)
1597 getvvcol(curwin, &(oap->start),
1598 &oap->start_vcol, NULL, NULL);
1599 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1600 }
1601 else
1602 resel_VIsual_vcol = oap->end_vcol;
1603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 resel_VIsual_line_count = oap->line_count;
1605 }
1606
1607 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
Bram Moolenaar5823f842019-01-03 22:58:08 +01001608 if ((redo_yank || oap->op_type != OP_YANK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 && oap->op_type != OP_COLON
1610#ifdef FEAT_FOLDING
1611 && oap->op_type != OP_FOLD
1612 && oap->op_type != OP_FOLDOPEN
1613 && oap->op_type != OP_FOLDOPENREC
1614 && oap->op_type != OP_FOLDCLOSE
1615 && oap->op_type != OP_FOLDCLOSEREC
1616 && oap->op_type != OP_FOLDDEL
1617 && oap->op_type != OP_FOLDDELREC
1618#endif
1619 && oap->motion_force == NUL
1620 )
1621 {
1622 /* Prepare for redoing. Only use the nchar field for "r",
1623 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001624 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1625 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001626 prep_redo(oap->regname, cap->count0,
1627 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1628 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001629 else if (cap->cmdchar != ':')
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001630 {
1631 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
1632
1633 /* reverse what nv_replace() did */
1634 if (nchar == REPLACE_CR_NCHAR)
1635 nchar = CAR;
1636 else if (nchar == REPLACE_NL_NCHAR)
1637 nchar = NL;
Bram Moolenaar641e2862012-07-25 15:06:34 +02001638 prep_redo(oap->regname, 0L, NUL, 'v',
1639 get_op_char(oap->op_type),
1640 get_extra_op_char(oap->op_type),
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001641 nchar);
1642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 if (!redo_VIsual_busy)
1644 {
1645 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001646 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 redo_VIsual_line_count = resel_VIsual_line_count;
1648 redo_VIsual_count = cap->count0;
Bram Moolenaard79e5502016-01-10 22:13:02 +01001649 redo_VIsual_arg = cap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 }
1651 }
1652
1653 /*
1654 * oap->inclusive defaults to TRUE.
1655 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1656 * FALSE. This makes "d}P" and "v}dP" work the same.
1657 */
1658 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1659 oap->inclusive = TRUE;
1660 if (VIsual_mode == 'V')
1661 oap->motion_type = MLINE;
1662 else
1663 {
1664 oap->motion_type = MCHAR;
1665 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001666 && (include_line_break || !virtual_op))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 {
1668 oap->inclusive = FALSE;
1669 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001670 * that works on lines only. */
Bram Moolenaard009e862015-06-09 20:20:03 +02001671 if (*p_sel != 'o'
1672 && !op_on_lines(oap->op_type)
1673 && oap->end.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 {
Bram Moolenaard009e862015-06-09 20:20:03 +02001675 ++oap->end.lnum;
1676 oap->end.col = 0;
Bram Moolenaard009e862015-06-09 20:20:03 +02001677 oap->end.coladd = 0;
Bram Moolenaard009e862015-06-09 20:20:03 +02001678 ++oap->line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 }
1680 }
1681 }
1682
1683 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001684
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 /*
1686 * Switch Visual off now, so screen updating does
1687 * not show inverted text when the screen is redrawn.
1688 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1689 * no screen redraw, so it is done here to remove the inverted
1690 * part.
1691 */
1692 if (!gui_yank)
1693 {
1694 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001695#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 setmouse();
1697 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001698#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001699 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 if ((oap->op_type == OP_YANK
1701 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001702 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 || oap->op_type == OP_FILTER)
1704 && oap->motion_force == NUL)
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001705 {
1706#ifdef FEAT_LINEBREAK
1707 /* make sure redrawing is correct */
1708 curwin->w_p_lbr = lbr_saved;
1709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 }
1713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 /* Include the trailing byte of a multi-byte char. */
1716 if (has_mbyte && oap->inclusive)
1717 {
1718 int l;
1719
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001720 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 if (l > 1)
1722 oap->end.col += l - 1;
1723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 curwin->w_set_curswant = TRUE;
1725
1726 /*
1727 * oap->empty is set when start and end are the same. The inclusive
1728 * flag affects this too, unless yanking and the end is on a NUL.
1729 */
1730 oap->empty = (oap->motion_type == MCHAR
1731 && (!oap->inclusive
1732 || (oap->op_type == OP_YANK
1733 && gchar_pos(&oap->end) == NUL))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001734 && EQUAL_POS(oap->start, oap->end)
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001735 && !(virtual_op && oap->start.coladd != oap->end.coladd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 /*
1737 * For delete, change and yank, it's an error to operate on an
1738 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1739 */
1740 empty_region_error = (oap->empty
1741 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1742
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 /* Force a redraw when operating on an empty Visual region, when
1744 * 'modifiable is off or creating a fold. */
1745 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001746#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 ))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001750 {
1751#ifdef FEAT_LINEBREAK
1752 curwin->w_p_lbr = lbr_saved;
1753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756
1757 /*
1758 * If the end of an operator is in column one while oap->motion_type
1759 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1760 * character in the previous line. If op_start is on or before the
1761 * first non-blank in the line, the operator becomes linewise
1762 * (strange, but that's the way vi does it).
1763 */
1764 if ( oap->motion_type == MCHAR
1765 && oap->inclusive == FALSE
1766 && !(cap->retval & CA_NO_ADJ_OP_END)
1767 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001769 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 && oap->line_count > 1)
1771 {
1772 oap->end_adjusted = TRUE; /* remember that we did this */
1773 --oap->line_count;
1774 --oap->end.lnum;
1775 if (inindent(0))
1776 oap->motion_type = MLINE;
1777 else
1778 {
1779 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1780 if (oap->end.col)
1781 {
1782 --oap->end.col;
1783 oap->inclusive = TRUE;
1784 }
1785 }
1786 }
1787 else
1788 oap->end_adjusted = FALSE;
1789
1790 switch (oap->op_type)
1791 {
1792 case OP_LSHIFT:
1793 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001794 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 auto_format(FALSE, TRUE);
1796 break;
1797
1798 case OP_JOIN_NS:
1799 case OP_JOIN:
1800 if (oap->line_count < 2)
1801 oap->line_count = 2;
1802 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1803 curbuf->b_ml.ml_line_count)
1804 beep_flush();
1805 else
1806 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001807 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001808 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 auto_format(FALSE, TRUE);
1810 }
1811 break;
1812
1813 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001816 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001817 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001818 CancelRedo();
1819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 else
1821 {
1822 (void)op_delete(oap);
1823 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1824 u_save_cursor(); /* cursor line wasn't saved yet */
1825 auto_format(FALSE, TRUE);
1826 }
1827 break;
1828
1829 case OP_YANK:
1830 if (empty_region_error)
1831 {
1832 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001833 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001834 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001835 CancelRedo();
1836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 }
1838 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001839 {
1840#ifdef FEAT_LINEBREAK
1841 curwin->w_p_lbr = lbr_saved;
1842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 (void)op_yank(oap, FALSE, !gui_yank);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 check_cursor_col();
1846 break;
1847
1848 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001851 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001852 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001853 CancelRedo();
1854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 else
1856 {
1857 /* This is a new edit command, not a restart. Need to
1858 * remember it to make 'insertmode' work with mappings for
1859 * Visual mode. But do this only once and not when typed and
1860 * 'insertmode' isn't set. */
1861 if (p_im || !KeyTyped)
1862 restart_edit_save = restart_edit;
1863 else
1864 restart_edit_save = 0;
1865 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001866#ifdef FEAT_LINEBREAK
1867 /* Restore linebreak, so that when the user edits it looks as
1868 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001869 if (curwin->w_p_lbr != lbr_saved)
1870 {
1871 curwin->w_p_lbr = lbr_saved;
1872 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1873 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 /* Reset finish_op now, don't want it set inside edit(). */
1876 finish_op = FALSE;
1877 if (op_change(oap)) /* will call edit() */
1878 cap->retval |= CA_COMMAND_BUSY;
1879 if (restart_edit == 0)
1880 restart_edit = restart_edit_save;
1881 }
1882 break;
1883
1884 case OP_FILTER:
1885 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1886 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1887 else
1888 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001889 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890
1891 case OP_INDENT:
1892 case OP_COLON:
1893
1894#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1895 /*
1896 * If 'equalprg' is empty, do the indenting internally.
1897 */
1898 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1899 {
1900# ifdef FEAT_LISP
1901 if (curbuf->b_p_lisp)
1902 {
1903 op_reindent(oap, get_lisp_indent);
1904 break;
1905 }
1906# endif
1907# ifdef FEAT_CINDENT
1908 op_reindent(oap,
1909# ifdef FEAT_EVAL
1910 *curbuf->b_p_inde != NUL ? get_expr_indent :
1911# endif
1912 get_c_indent);
1913 break;
1914# endif
1915 }
1916#endif
1917
1918 op_colon(oap);
1919 break;
1920
1921 case OP_TILDE:
1922 case OP_UPPER:
1923 case OP_LOWER:
1924 case OP_ROT13:
1925 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001926 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001927 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001928 CancelRedo();
1929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 else
1931 op_tilde(oap);
1932 check_cursor_col();
1933 break;
1934
1935 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001936#if defined(FEAT_EVAL)
1937 if (*curbuf->b_p_fex != NUL)
1938 op_formatexpr(oap); /* use expression */
1939 else
1940#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01001941 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 op_colon(oap); /* use external command */
1943 else
1944 op_format(oap, FALSE); /* use internal function */
1945 break;
1946
1947 case OP_FORMAT2:
1948 op_format(oap, TRUE); /* use internal function */
1949 break;
1950
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001951 case OP_FUNCTION:
Bram Moolenaar4a08b0d2016-11-05 21:55:13 +01001952#ifdef FEAT_LINEBREAK
1953 /* Restore linebreak, so that when the user edits it looks as
1954 * before. */
1955 curwin->w_p_lbr = lbr_saved;
1956#endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001957 op_function(oap); /* call 'operatorfunc' */
1958 break;
1959
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 case OP_INSERT:
1961 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001964 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001965 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001966 CancelRedo();
1967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 else
1969 {
1970 /* This is a new edit command, not a restart. Need to
1971 * remember it to make 'insertmode' work with mappings for
1972 * Visual mode. But do this only once. */
1973 restart_edit_save = restart_edit;
1974 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001975#ifdef FEAT_LINEBREAK
1976 /* Restore linebreak, so that when the user edits it looks as
1977 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001978 if (curwin->w_p_lbr != lbr_saved)
1979 {
1980 curwin->w_p_lbr = lbr_saved;
1981 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1982 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 op_insert(oap, cap->count1);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001985#ifdef FEAT_LINEBREAK
1986 /* Reset linebreak, so that formatting works correctly. */
1987 curwin->w_p_lbr = FALSE;
1988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989
1990 /* TODO: when inserting in several lines, should format all
1991 * the lines. */
1992 auto_format(FALSE, TRUE);
1993
1994 if (restart_edit == 0)
1995 restart_edit = restart_edit_save;
Bram Moolenaar0b5c93a2017-02-01 15:03:30 +01001996 else
1997 cap->retval |= CA_COMMAND_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 break;
2000
2001 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002004 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002005 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002006 CancelRedo();
2007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002009 {
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002010#ifdef FEAT_LINEBREAK
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002011 /* Restore linebreak, so that when the user edits it looks as
2012 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002013 if (curwin->w_p_lbr != lbr_saved)
2014 {
2015 curwin->w_p_lbr = lbr_saved;
2016 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2017 }
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 op_replace(oap, cap->nchar);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 break;
2022
2023#ifdef FEAT_FOLDING
2024 case OP_FOLD:
2025 VIsual_reselect = FALSE; /* don't reselect now */
2026 foldCreate(oap->start.lnum, oap->end.lnum);
2027 break;
2028
2029 case OP_FOLDOPEN:
2030 case OP_FOLDOPENREC:
2031 case OP_FOLDCLOSE:
2032 case OP_FOLDCLOSEREC:
2033 VIsual_reselect = FALSE; /* don't reselect now */
2034 opFoldRange(oap->start.lnum, oap->end.lnum,
2035 oap->op_type == OP_FOLDOPEN
2036 || oap->op_type == OP_FOLDOPENREC,
2037 oap->op_type == OP_FOLDOPENREC
2038 || oap->op_type == OP_FOLDCLOSEREC,
2039 oap->is_VIsual);
2040 break;
2041
2042 case OP_FOLDDEL:
2043 case OP_FOLDDELREC:
2044 VIsual_reselect = FALSE; /* don't reselect now */
2045 deleteFold(oap->start.lnum, oap->end.lnum,
2046 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2047 break;
2048#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002049 case OP_NR_ADD:
2050 case OP_NR_SUB:
2051 if (empty_region_error)
2052 {
2053 vim_beep(BO_OPER);
2054 CancelRedo();
2055 }
2056 else
2057 {
2058 VIsual_active = TRUE;
2059#ifdef FEAT_LINEBREAK
2060 curwin->w_p_lbr = lbr_saved;
2061#endif
2062 op_addsub(oap, cap->count1, redo_VIsual_arg);
2063 VIsual_active = FALSE;
2064 }
2065 check_cursor_col();
2066 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 default:
2068 clearopbeep(oap);
2069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 virtual_op = MAYBE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 if (!gui_yank)
2072 {
2073 /*
2074 * if 'sol' not set, go back to old column for some commands
2075 */
2076 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2077 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2078 || oap->op_type == OP_DELETE))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002079 {
2080#ifdef FEAT_LINEBREAK
2081 curwin->w_p_lbr = FALSE;
2082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 coladvance(curwin->w_curswant = old_col);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 }
2086 else
2087 {
2088 curwin->w_cursor = old_cursor;
2089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 clearop(oap);
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01002092 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 }
Bram Moolenaar404406a2014-10-09 13:24:43 +02002094#ifdef FEAT_LINEBREAK
2095 curwin->w_p_lbr = lbr_saved;
2096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097}
2098
2099/*
2100 * Handle indent and format operators and visual mode ":".
2101 */
2102 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002103op_colon(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104{
2105 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 if (oap->is_VIsual)
2107 stuffReadbuff((char_u *)"'<,'>");
2108 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 {
2110 /*
2111 * Make the range look nice, so it can be repeated.
2112 */
2113 if (oap->start.lnum == curwin->w_cursor.lnum)
2114 stuffcharReadbuff('.');
2115 else
2116 stuffnumReadbuff((long)oap->start.lnum);
2117 if (oap->end.lnum != oap->start.lnum)
2118 {
2119 stuffcharReadbuff(',');
2120 if (oap->end.lnum == curwin->w_cursor.lnum)
2121 stuffcharReadbuff('.');
2122 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2123 stuffcharReadbuff('$');
2124 else if (oap->start.lnum == curwin->w_cursor.lnum)
2125 {
2126 stuffReadbuff((char_u *)".+");
2127 stuffnumReadbuff((long)oap->line_count - 1);
2128 }
2129 else
2130 stuffnumReadbuff((long)oap->end.lnum);
2131 }
2132 }
2133 if (oap->op_type != OP_COLON)
2134 stuffReadbuff((char_u *)"!");
2135 if (oap->op_type == OP_INDENT)
2136 {
2137#ifndef FEAT_CINDENT
2138 if (*get_equalprg() == NUL)
2139 stuffReadbuff((char_u *)"indent");
2140 else
2141#endif
2142 stuffReadbuff(get_equalprg());
2143 stuffReadbuff((char_u *)"\n");
2144 }
2145 else if (oap->op_type == OP_FORMAT)
2146 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002147 if (*curbuf->b_p_fp != NUL)
2148 stuffReadbuff(curbuf->b_p_fp);
2149 else if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 stuffReadbuff(p_fp);
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002151 else
2152 stuffReadbuff((char_u *)"fmt");
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002153 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 }
2155
2156 /*
2157 * do_cmdline() does the rest
2158 */
2159}
2160
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002161/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002162 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002163 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002164 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002165op_function(oparg_T *oap UNUSED)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002166{
2167#ifdef FEAT_EVAL
Bram Moolenaarffa96842018-06-12 22:05:14 +02002168 typval_T argv[2];
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002169 int save_virtual_op = virtual_op;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002170
2171 if (*p_opfunc == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002172 emsg(_("E774: 'operatorfunc' is empty"));
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002173 else
2174 {
2175 /* Set '[ and '] marks to text to be operated on. */
2176 curbuf->b_op_start = oap->start;
2177 curbuf->b_op_end = oap->end;
2178 if (oap->motion_type != MLINE && !oap->inclusive)
2179 /* Exclude the end position. */
2180 decl(&curbuf->b_op_end);
2181
Bram Moolenaarffa96842018-06-12 22:05:14 +02002182 argv[0].v_type = VAR_STRING;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002183 if (oap->block_mode)
Bram Moolenaarffa96842018-06-12 22:05:14 +02002184 argv[0].vval.v_string = (char_u *)"block";
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002185 else if (oap->motion_type == MLINE)
Bram Moolenaarffa96842018-06-12 22:05:14 +02002186 argv[0].vval.v_string = (char_u *)"line";
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002187 else
Bram Moolenaarffa96842018-06-12 22:05:14 +02002188 argv[0].vval.v_string = (char_u *)"char";
2189 argv[1].v_type = VAR_UNKNOWN;
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002190
2191 /* Reset virtual_op so that 'virtualedit' can be changed in the
2192 * function. */
2193 virtual_op = MAYBE;
2194
Bram Moolenaarded27a12018-08-01 19:06:03 +02002195 (void)call_func_retnr(p_opfunc, 1, argv);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002196
2197 virtual_op = save_virtual_op;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002198 }
2199#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002200 emsg(_("E775: Eval feature not available"));
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002201#endif
2202}
2203
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204#if defined(FEAT_MOUSE) || defined(PROTO)
2205/*
2206 * Do the appropriate action for the current mouse click in the current mode.
2207 * Not used for Command-line mode.
2208 *
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002209 * Normal and Visual Mode:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 * event modi- position visual change action
2211 * fier cursor window
2212 * left press - yes end yes
2213 * left press C yes end yes "^]" (2)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002214 * left press S yes end (popup: extend) yes "*" (2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 * left drag - yes start if moved no
2216 * left relse - yes start if moved no
2217 * middle press - yes if not active no put register
2218 * middle press - yes if active no yank and put
2219 * right press - yes start or extend yes
2220 * right press S yes no change yes "#" (2)
2221 * right drag - yes extend no
2222 * right relse - yes extend no
2223 *
2224 * Insert or Replace Mode:
2225 * event modi- position visual change action
2226 * fier cursor window
2227 * left press - yes (cannot be active) yes
2228 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2229 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2230 * left drag - yes start or extend (1) no CTRL-O (1)
2231 * left relse - yes start or extend (1) no CTRL-O (1)
2232 * middle press - no (cannot be active) no put register
2233 * right press - yes start or extend yes CTRL-O
2234 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2235 *
2236 * (1) only if mouse pointer moved since press
2237 * (2) only if click is in same buffer
2238 *
2239 * Return TRUE if start_arrow() should be called for edit mode.
2240 */
2241 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002242do_mouse(
2243 oparg_T *oap, /* operator argument, can be NULL */
2244 int c, /* K_LEFTMOUSE, etc */
2245 int dir, /* Direction to 'put' if necessary */
2246 long count,
2247 int fixindent) /* PUT_FIXINDENT if fixing indent necessary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248{
2249 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2250 static int got_click = FALSE; /* got a click some time back */
2251
2252 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2253 int is_click; /* If FALSE it's a drag or release event */
2254 int is_drag; /* If TRUE it's a drag event */
2255 int jump_flags = 0; /* flags for jump_to_mouse() */
2256 pos_T start_visual;
2257 int moved; /* Has cursor moved? */
2258 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002259 static int in_tab_line = FALSE; /* mouse clicked in tab line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 int in_sep_line; /* mouse in vertical separator line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 int c1, c2;
2262#if defined(FEAT_FOLDING)
2263 pos_T save_cursor;
2264#endif
2265 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 static pos_T orig_cursor;
2267 colnr_T leftcol, rightcol;
2268 pos_T end_visual;
2269 int diff;
2270 int old_active = VIsual_active;
2271 int old_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 int regname;
2273
2274#if defined(FEAT_FOLDING)
2275 save_cursor = curwin->w_cursor;
2276#endif
2277
2278 /*
2279 * When GUI is active, always recognize mouse events, otherwise:
2280 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2281 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2282 * - For command line and insert mode 'mouse' is checked before calling
2283 * do_mouse().
2284 */
2285 if (do_always)
2286 do_always = FALSE;
2287 else
2288#ifdef FEAT_GUI
2289 if (!gui.in_use)
2290#endif
2291 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 if (VIsual_active)
2293 {
2294 if (!mouse_has(MOUSE_VISUAL))
2295 return FALSE;
2296 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002297 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 return FALSE;
2299 }
2300
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002301 for (;;)
2302 {
2303 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2304 if (is_drag)
2305 {
2306 /* If the next character is the same mouse event then use that
2307 * one. Speeds up dragging the status line. */
2308 if (vpeekc() != NUL)
2309 {
2310 int nc;
2311 int save_mouse_row = mouse_row;
2312 int save_mouse_col = mouse_col;
2313
2314 /* Need to get the character, peeking doesn't get the actual
2315 * one. */
2316 nc = safe_vgetc();
2317 if (c == nc)
2318 continue;
2319 vungetc(nc);
2320 mouse_row = save_mouse_row;
2321 mouse_col = save_mouse_col;
2322 }
2323 }
2324 break;
2325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002327 if (c == K_MOUSEMOVE)
2328 {
2329 /* Mouse moved without a button pressed. */
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002330#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002331 ui_may_remove_balloon();
2332 if (p_bevalterm && !VIsual_active)
2333 {
2334 profile_setlimit(p_bdlay, &bevalexpr_due);
2335 bevalexpr_due_set = TRUE;
2336 }
2337#endif
2338 return FALSE;
2339 }
2340
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341#ifdef FEAT_MOUSESHAPE
2342 /* May have stopped dragging the status or separator line. The pointer is
2343 * most likely still on the status or separator line. */
2344 if (!is_drag && drag_status_line)
2345 {
2346 drag_status_line = FALSE;
2347 update_mouseshape(SHAPE_IDX_STATUS);
2348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 if (!is_drag && drag_sep_line)
2350 {
2351 drag_sep_line = FALSE;
2352 update_mouseshape(SHAPE_IDX_VSEP);
2353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354#endif
2355
2356 /*
2357 * Ignore drag and release events if we didn't get a click.
2358 */
2359 if (is_click)
2360 got_click = TRUE;
2361 else
2362 {
2363 if (!got_click) /* didn't get click, ignore */
2364 return FALSE;
2365 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002366 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002368 if (in_tab_line)
2369 {
2370 in_tab_line = FALSE;
2371 return FALSE;
2372 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 }
2375
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 /*
2377 * CTRL right mouse button does CTRL-T
2378 */
2379 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2380 {
2381 if (State & INSERT)
2382 stuffcharReadbuff(Ctrl_O);
2383 if (count > 1)
2384 stuffnumReadbuff(count);
2385 stuffcharReadbuff(Ctrl_T);
2386 got_click = FALSE; /* ignore drag&release now */
2387 return FALSE;
2388 }
2389
2390 /*
2391 * CTRL only works with left mouse button
2392 */
2393 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2394 return FALSE;
2395
2396 /*
2397 * When a modifier is down, ignore drag and release events, as well as
2398 * multiple clicks and the middle mouse button.
2399 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2400 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002401 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2402 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 && (!is_click
2404 || (mod_mask & MOD_MASK_MULTI_CLICK)
2405 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002406 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 && mouse_model_popup()
2408 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002409 && !((mod_mask & MOD_MASK_ALT)
2410 && !mouse_model_popup()
2411 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 )
2413 return FALSE;
2414
2415 /*
2416 * If the button press was used as the movement command for an operator
2417 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2418 * drag/release events.
2419 */
2420 if (!is_click && which_button == MOUSE_MIDDLE)
2421 return FALSE;
2422
2423 if (oap != NULL)
2424 regname = oap->regname;
2425 else
2426 regname = 0;
2427
2428 /*
2429 * Middle mouse button does a 'put' of the selected text
2430 */
2431 if (which_button == MOUSE_MIDDLE)
2432 {
2433 if (State == NORMAL)
2434 {
2435 /*
2436 * If an operator was pending, we don't know what the user wanted
2437 * to do. Go back to normal mode: Clear the operator and beep().
2438 */
2439 if (oap != NULL && oap->op_type != OP_NOP)
2440 {
2441 clearopbeep(oap);
2442 return FALSE;
2443 }
2444
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 /*
2446 * If visual was active, yank the highlighted text and put it
2447 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002448 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 */
2450 if (VIsual_active)
2451 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002452 if (VIsual_select)
2453 {
2454 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002455 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002456 }
2457 else
2458 {
2459 stuffcharReadbuff('y');
2460 stuffcharReadbuff(K_MIDDLEMOUSE);
2461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 do_always = TRUE; /* ignore 'mouse' setting next time */
2463 return FALSE;
2464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 /*
2466 * The rest is below jump_to_mouse()
2467 */
2468 }
2469
2470 else if ((State & INSERT) == 0)
2471 return FALSE;
2472
2473 /*
2474 * Middle click in insert mode doesn't move the mouse, just insert the
2475 * contents of a register. '.' register is special, can't insert that
2476 * with do_put().
2477 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2478 * happens for the GUI).
2479 */
2480 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2481 {
2482 if (regname == '.')
2483 insert_reg(regname, TRUE);
2484 else
2485 {
2486#ifdef FEAT_CLIPBOARD
2487 if (clip_star.available && regname == 0)
2488 regname = '*';
2489#endif
2490 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2491 insert_reg(regname, TRUE);
2492 else
2493 {
2494 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2495
2496 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2497 AppendCharToRedobuff(Ctrl_R);
2498 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2499 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2500 }
2501 }
2502 return FALSE;
2503 }
2504 }
2505
2506 /* When dragging or button-up stay in the same window. */
2507 if (!is_click)
2508 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2509
2510 start_visual.lnum = 0;
2511
Bram Moolenaarf740b292006-02-16 22:11:02 +00002512 /* Check for clicking in the tab page line. */
2513 if (mouse_row == 0 && firstwin->w_winrow > 0)
2514 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002515 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002516 {
2517 if (in_tab_line)
2518 {
2519 c1 = TabPageIdxs[mouse_col];
Bram Moolenaar4a4b8212015-09-08 17:50:41 +02002520 tabpage_move(c1 <= 0 ? 9999 : c1 < tabpage_index(curtab)
2521 ? c1 - 1 : c1);
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002522 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002523 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002524 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002525
Bram Moolenaarf740b292006-02-16 22:11:02 +00002526 /* click in a tab selects that tab page */
2527 if (is_click
2528# ifdef FEAT_CMDWIN
2529 && cmdwin_type == 0
2530# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002531 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002532 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002533 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002534 c1 = TabPageIdxs[mouse_col];
2535 if (c1 >= 0)
2536 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002537 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2538 {
2539 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002540 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002541 tabpage_new();
2542 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2543 }
2544 else
2545 {
2546 /* Go to specified tab page, or next one if not clicking
2547 * on a label. */
2548 goto_tabpage(c1);
2549
2550 /* It's like clicking on the status line of a window. */
2551 if (curwin != old_curwin)
2552 end_visual_mode();
2553 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002554 }
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002555 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002556 {
2557 tabpage_T *tp;
2558
2559 /* Close the current or specified tab page. */
2560 if (c1 == -999)
2561 tp = curtab;
2562 else
2563 tp = find_tabpage(-c1);
2564 if (tp == curtab)
2565 {
2566 if (first_tabpage->tp_next != NULL)
2567 tabpage_close(FALSE);
2568 }
2569 else if (tp != NULL)
2570 tabpage_close_other(tp, FALSE);
2571 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002572 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002573 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002574 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002575 else if (is_drag && in_tab_line)
2576 {
2577 c1 = TabPageIdxs[mouse_col];
2578 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2579 return FALSE;
2580 }
2581
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 /*
2583 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2584 * right button up -> pop-up menu
2585 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002586 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 */
2588 if (mouse_model_popup())
2589 {
2590 if (which_button == MOUSE_RIGHT
2591 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2592 {
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002593#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002595 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON) \
2596 || defined(FEAT_TERM_POPUP_MENU)
2597# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 if (gui.in_use)
2599 {
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002600# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2601 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2602 if (!is_click)
2603 /* Ignore right button release events, only shows the popup
2604 * menu on the button down event. */
2605 return FALSE;
2606# endif
2607# if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2608 if (is_click || is_drag)
2609 /* Ignore right button down and drag mouse events. Windows
2610 * only shows the popup menu on the button up event. */
2611 return FALSE;
2612# endif
2613 }
2614# endif
2615# if defined(FEAT_GUI) && defined(FEAT_TERM_POPUP_MENU)
2616 else
2617# endif
2618# if defined(FEAT_TERM_POPUP_MENU)
2619 if (!is_click)
2620 /* Ignore right button release events, only shows the popup
2621 * menu on the button down event. */
2622 return FALSE;
2623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002625 jump_flags = 0;
2626 if (STRCMP(p_mousem, "popup_setpos") == 0)
2627 {
2628 /* First set the cursor position before showing the popup
2629 * menu. */
2630 if (VIsual_active)
2631 {
2632 pos_T m_pos;
2633
2634 /*
2635 * set MOUSE_MAY_STOP_VIS if we are outside the
2636 * selection or the current window (might have false
2637 * negative here)
2638 */
2639 if (mouse_row < curwin->w_winrow
2640 || mouse_row
2641 > (curwin->w_winrow + curwin->w_height))
2642 jump_flags = MOUSE_MAY_STOP_VIS;
2643 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2644 jump_flags = MOUSE_MAY_STOP_VIS;
2645 else
2646 {
2647 if ((LT_POS(curwin->w_cursor, VIsual)
2648 && (LT_POS(m_pos, curwin->w_cursor)
2649 || LT_POS(VIsual, m_pos)))
2650 || (LT_POS(VIsual, curwin->w_cursor)
2651 && (LT_POS(m_pos, VIsual)
2652 || LT_POS(curwin->w_cursor, m_pos))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 {
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002654 jump_flags = MOUSE_MAY_STOP_VIS;
2655 }
2656 else if (VIsual_mode == Ctrl_V)
2657 {
2658 getvcols(curwin, &curwin->w_cursor, &VIsual,
2659 &leftcol, &rightcol);
2660 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2661 if (m_pos.col < leftcol || m_pos.col > rightcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 }
2664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002666 else
2667 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002669 if (jump_flags)
2670 {
2671 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2672 update_curbuf(VIsual_active ? INVERTED : VALID);
2673 setcursor();
2674 out_flush(); /* Update before showing popup menu */
2675 }
2676# ifdef FEAT_MENU
2677 show_popupmenu();
2678 got_click = FALSE; /* ignore release events */
2679# endif
2680 return (jump_flags & CURSOR_MOVED) != 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681#else
2682 return FALSE;
2683#endif
2684 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002685 if (which_button == MOUSE_LEFT
2686 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 {
2688 which_button = MOUSE_RIGHT;
2689 mod_mask &= ~MOD_MASK_SHIFT;
2690 }
2691 }
2692
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 if ((State & (NORMAL | INSERT))
2694 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2695 {
2696 if (which_button == MOUSE_LEFT)
2697 {
2698 if (is_click)
2699 {
2700 /* stop Visual mode for a left click in a window, but not when
2701 * on a status line */
2702 if (VIsual_active)
2703 jump_flags |= MOUSE_MAY_STOP_VIS;
2704 }
2705 else if (mouse_has(MOUSE_VISUAL))
2706 jump_flags |= MOUSE_MAY_VIS;
2707 }
2708 else if (which_button == MOUSE_RIGHT)
2709 {
2710 if (is_click && VIsual_active)
2711 {
2712 /*
2713 * Remember the start and end of visual before moving the
2714 * cursor.
2715 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002716 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 {
2718 start_visual = curwin->w_cursor;
2719 end_visual = VIsual;
2720 }
2721 else
2722 {
2723 start_visual = VIsual;
2724 end_visual = curwin->w_cursor;
2725 }
2726 }
2727 jump_flags |= MOUSE_FOCUS;
2728 if (mouse_has(MOUSE_VISUAL))
2729 jump_flags |= MOUSE_MAY_VIS;
2730 }
2731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732
2733 /*
2734 * If an operator is pending, ignore all drags and releases until the
2735 * next mouse click.
2736 */
2737 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2738 {
2739 got_click = FALSE;
2740 oap->motion_type = MCHAR;
2741 }
2742
2743 /* When releasing the button let jump_to_mouse() know. */
2744 if (!is_click && !is_drag)
2745 jump_flags |= MOUSE_RELEASED;
2746
2747 /*
2748 * JUMP!
2749 */
2750 jump_flags = jump_to_mouse(jump_flags,
2751 oap == NULL ? NULL : &(oap->inclusive), which_button);
Bram Moolenaareb163d72017-09-23 15:08:17 +02002752
2753#ifdef FEAT_MENU
2754 /* A click in the window toolbar has no side effects. */
2755 if (jump_flags & MOUSE_WINBAR)
2756 return FALSE;
2757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 moved = (jump_flags & CURSOR_MOVED);
2759 in_status_line = (jump_flags & IN_STATUS_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 in_sep_line = (jump_flags & IN_SEP_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761
2762#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002763 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2765 {
2766 int key = KEY2TERMCAP1(c);
2767
2768 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2769 || key == (int)KE_RIGHTRELEASE)
2770 netbeans_button_release(which_button);
2771 }
2772#endif
2773
2774 /* When jumping to another window, clear a pending operator. That's a bit
2775 * friendlier than beeping and not jumping to that window. */
2776 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2777 clearop(oap);
2778
2779#ifdef FEAT_FOLDING
2780 if (mod_mask == 0
2781 && !is_drag
2782 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2783 && which_button == MOUSE_LEFT)
2784 {
2785 /* open or close a fold at this line */
2786 if (jump_flags & MOUSE_FOLD_OPEN)
2787 openFold(curwin->w_cursor.lnum, 1L);
2788 else
2789 closeFold(curwin->w_cursor.lnum, 1L);
2790 /* don't move the cursor if still in the same window */
2791 if (curwin == old_curwin)
2792 curwin->w_cursor = save_cursor;
2793 }
2794#endif
2795
2796#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2797 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2798 {
2799 clip_modeless(which_button, is_click, is_drag);
2800 return FALSE;
2801 }
2802#endif
2803
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002805 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002806 if (VIsual_active && is_drag && get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 {
2808 /* In the very first line, allow scrolling one line */
2809 if (mouse_row == 0)
2810 mouse_dragging = 2;
2811 else
2812 mouse_dragging = 1;
2813 }
2814
2815 /* When dragging the mouse above the window, scroll down. */
2816 if (is_drag && mouse_row < 0 && !in_status_line)
2817 {
2818 scroll_redraw(FALSE, 1L);
2819 mouse_row = 0;
2820 }
2821
2822 if (start_visual.lnum) /* right click in visual mode */
2823 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002824 /* When ALT is pressed make Visual mode blockwise. */
2825 if (mod_mask & MOD_MASK_ALT)
2826 VIsual_mode = Ctrl_V;
2827
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 /*
2829 * In Visual-block mode, divide the area in four, pick up the corner
2830 * that is in the quarter that the cursor is in.
2831 */
2832 if (VIsual_mode == Ctrl_V)
2833 {
2834 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2835 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2836 end_visual.col = leftcol;
2837 else
2838 end_visual.col = rightcol;
Bram Moolenaarcde88542015-08-11 19:14:00 +02002839 if (curwin->w_cursor.lnum >=
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 (start_visual.lnum + end_visual.lnum) / 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 end_visual.lnum = start_visual.lnum;
2842
2843 /* move VIsual to the right column */
2844 start_visual = curwin->w_cursor; /* save the cursor pos */
2845 curwin->w_cursor = end_visual;
2846 coladvance(end_visual.col);
2847 VIsual = curwin->w_cursor;
2848 curwin->w_cursor = start_visual; /* restore the cursor */
2849 }
2850 else
2851 {
2852 /*
2853 * If the click is before the start of visual, change the start.
2854 * If the click is after the end of visual, change the end. If
2855 * the click is inside the visual, change the closest side.
2856 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002857 if (LT_POS(curwin->w_cursor, start_visual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 VIsual = end_visual;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002859 else if (LT_POS(end_visual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 VIsual = start_visual;
2861 else
2862 {
2863 /* In the same line, compare column number */
2864 if (end_visual.lnum == start_visual.lnum)
2865 {
2866 if (curwin->w_cursor.col - start_visual.col >
2867 end_visual.col - curwin->w_cursor.col)
2868 VIsual = start_visual;
2869 else
2870 VIsual = end_visual;
2871 }
2872
2873 /* In different lines, compare line number */
2874 else
2875 {
2876 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2877 (end_visual.lnum - curwin->w_cursor.lnum);
2878
2879 if (diff > 0) /* closest to end */
2880 VIsual = start_visual;
2881 else if (diff < 0) /* closest to start */
2882 VIsual = end_visual;
2883 else /* in the middle line */
2884 {
2885 if (curwin->w_cursor.col <
2886 (start_visual.col + end_visual.col) / 2)
2887 VIsual = end_visual;
2888 else
2889 VIsual = start_visual;
2890 }
2891 }
2892 }
2893 }
2894 }
2895 /*
2896 * If Visual mode started in insert mode, execute "CTRL-O"
2897 */
2898 else if ((State & INSERT) && VIsual_active)
2899 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900
2901 /*
2902 * Middle mouse click: Put text before cursor.
2903 */
2904 if (which_button == MOUSE_MIDDLE)
2905 {
2906#ifdef FEAT_CLIPBOARD
2907 if (clip_star.available && regname == 0)
2908 regname = '*';
2909#endif
2910 if (yank_register_mline(regname))
2911 {
2912 if (mouse_past_bottom)
2913 dir = FORWARD;
2914 }
2915 else if (mouse_past_eol)
2916 dir = FORWARD;
2917
2918 if (fixindent)
2919 {
2920 c1 = (dir == BACKWARD) ? '[' : ']';
2921 c2 = 'p';
2922 }
2923 else
2924 {
2925 c1 = (dir == FORWARD) ? 'p' : 'P';
2926 c2 = NUL;
2927 }
2928 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2929
2930 /*
2931 * Remember where the paste started, so in edit() Insstart can be set
2932 * to this position
2933 */
2934 if (restart_edit != 0)
2935 where_paste_started = curwin->w_cursor;
2936 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2937 }
2938
Bram Moolenaar4033c552017-09-16 20:54:51 +02002939#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 /*
2941 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2942 * error under the mouse pointer.
2943 */
2944 else if (((mod_mask & MOD_MASK_CTRL)
2945 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2946 && bt_quickfix(curbuf))
2947 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002948 if (curwin->w_llist_ref == NULL) /* quickfix window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01002949 do_cmdline_cmd((char_u *)".cc");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002950 else /* location list window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01002951 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 got_click = FALSE; /* ignore drag&release now */
2953 }
2954#endif
2955
2956 /*
2957 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2958 * under the mouse pointer.
2959 */
2960 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2961 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2962 {
2963 if (State & INSERT)
2964 stuffcharReadbuff(Ctrl_O);
2965 stuffcharReadbuff(Ctrl_RSB);
2966 got_click = FALSE; /* ignore drag&release now */
2967 }
2968
2969 /*
2970 * Shift-Mouse click searches for the next occurrence of the word under
2971 * the mouse pointer
2972 */
2973 else if ((mod_mask & MOD_MASK_SHIFT))
2974 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002975 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 stuffcharReadbuff(Ctrl_O);
2977 if (which_button == MOUSE_LEFT)
2978 stuffcharReadbuff('*');
2979 else /* MOUSE_RIGHT */
2980 stuffcharReadbuff('#');
2981 }
2982
2983 /* Handle double clicks, unless on status line */
2984 else if (in_status_line)
2985 {
2986#ifdef FEAT_MOUSESHAPE
2987 if ((is_drag || is_click) && !drag_status_line)
2988 {
2989 drag_status_line = TRUE;
2990 update_mouseshape(-1);
2991 }
2992#endif
2993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 else if (in_sep_line)
2995 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02002996#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 if ((is_drag || is_click) && !drag_sep_line)
2998 {
2999 drag_sep_line = TRUE;
3000 update_mouseshape(-1);
3001 }
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003002#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3005 && mouse_has(MOUSE_VISUAL))
3006 {
3007 if (is_click || !VIsual_active)
3008 {
3009 if (VIsual_active)
3010 orig_cursor = VIsual;
3011 else
3012 {
3013 check_visual_highlight();
3014 VIsual = curwin->w_cursor;
3015 orig_cursor = VIsual;
3016 VIsual_active = TRUE;
3017 VIsual_reselect = TRUE;
3018 /* start Select mode if 'selectmode' contains "mouse" */
3019 may_start_select('o');
3020 setmouse();
3021 }
3022 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003023 {
3024 /* Double click with ALT pressed makes it blockwise. */
3025 if (mod_mask & MOD_MASK_ALT)
3026 VIsual_mode = Ctrl_V;
3027 else
3028 VIsual_mode = 'v';
3029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3031 VIsual_mode = 'V';
3032 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3033 VIsual_mode = Ctrl_V;
3034#ifdef FEAT_CLIPBOARD
3035 /* Make sure the clipboard gets updated. Needed because start and
3036 * end may still be the same, and the selection needs to be owned */
3037 clip_star.vmode = NUL;
3038#endif
3039 }
3040 /*
3041 * A double click selects a word or a block.
3042 */
3043 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3044 {
3045 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003046 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047
3048 if (is_click)
3049 {
3050 /* If the character under the cursor (skipping white space) is
3051 * not a word character, try finding a match and select a (),
3052 * {}, [], #if/#endif, etc. block. */
3053 end_visual = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01003054 while (gc = gchar_pos(&end_visual), VIM_ISWHITE(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 inc(&end_visual);
3056 if (oap != NULL)
3057 oap->motion_type = MCHAR;
3058 if (oap != NULL
3059 && VIsual_mode == 'v'
3060 && !vim_iswordc(gchar_pos(&end_visual))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003061 && EQUAL_POS(curwin->w_cursor, VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 && (pos = findmatch(oap, NUL)) != NULL)
3063 {
3064 curwin->w_cursor = *pos;
3065 if (oap->motion_type == MLINE)
3066 VIsual_mode = 'V';
3067 else if (*p_sel == 'e')
3068 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003069 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 ++VIsual.col;
3071 else
3072 ++curwin->w_cursor.col;
3073 }
3074 }
3075 }
3076
3077 if (pos == NULL && (is_click || is_drag))
3078 {
3079 /* When not found a match or when dragging: extend to include
3080 * a word. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003081 if (LT_POS(curwin->w_cursor, orig_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 {
3083 find_start_of_word(&curwin->w_cursor);
3084 find_end_of_word(&VIsual);
3085 }
3086 else
3087 {
3088 find_start_of_word(&VIsual);
3089 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003091 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 find_end_of_word(&curwin->w_cursor);
3093 }
3094 }
3095 curwin->w_set_curswant = TRUE;
3096 }
3097 if (is_click)
3098 redraw_curbuf_later(INVERTED); /* update the inversion */
3099 }
3100 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003101 {
3102 if (mod_mask & MOD_MASK_ALT)
3103 VIsual_mode = Ctrl_V;
3104 else
3105 VIsual_mode = 'v';
3106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107
3108 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003109 if ((!VIsual_active && old_active && mode_displayed)
3110 || (VIsual_active && p_smd && msg_silent == 0
3111 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113
3114 return moved;
3115}
3116
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117/*
3118 * Move "pos" back to the start of the word it's in.
3119 */
3120 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003121find_start_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122{
3123 char_u *line;
3124 int cclass;
3125 int col;
3126
3127 line = ml_get(pos->lnum);
3128 cclass = get_mouse_class(line + pos->col);
3129
3130 while (pos->col > 0)
3131 {
3132 col = pos->col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 col -= (*mb_head_off)(line, line + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 if (get_mouse_class(line + col) != cclass)
3135 break;
3136 pos->col = col;
3137 }
3138}
3139
3140/*
3141 * Move "pos" forward to the end of the word it's in.
3142 * When 'selection' is "exclusive", the position is just after the word.
3143 */
3144 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003145find_end_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146{
3147 char_u *line;
3148 int cclass;
3149 int col;
3150
3151 line = ml_get(pos->lnum);
3152 if (*p_sel == 'e' && pos->col > 0)
3153 {
3154 --pos->col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 pos->col -= (*mb_head_off)(line, line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 }
3157 cclass = get_mouse_class(line + pos->col);
3158 while (line[pos->col] != NUL)
3159 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003160 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 if (get_mouse_class(line + col) != cclass)
3162 {
3163 if (*p_sel == 'e')
3164 pos->col = col;
3165 break;
3166 }
3167 pos->col = col;
3168 }
3169}
3170
3171/*
3172 * Get class of a character for selection: same class means same word.
3173 * 0: blank
3174 * 1: punctuation groups
3175 * 2: normal word character
3176 * >2: multi-byte word character.
3177 */
3178 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003179get_mouse_class(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180{
3181 int c;
3182
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3184 return mb_get_class(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185
3186 c = *p;
3187 if (c == ' ' || c == '\t')
3188 return 0;
3189
3190 if (vim_iswordc(c))
3191 return 2;
3192
3193 /*
3194 * There are a few special cases where we want certain combinations of
3195 * characters to be considered as a single word. These are things like
3196 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003197 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 */
3199 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3200 return 1;
3201 return c;
3202}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203#endif /* FEAT_MOUSE */
3204
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205/*
3206 * Check if highlighting for visual mode is possible, give a warning message
3207 * if not.
3208 */
3209 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003210check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211{
3212 static int did_check = FALSE;
3213
3214 if (full_screen)
3215 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003216 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003217 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 did_check = TRUE;
3219 }
3220}
3221
3222/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003223 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 * This function should ALWAYS be called to end Visual mode, except from
3225 * do_pending_operator().
3226 */
3227 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003228end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229{
3230#ifdef FEAT_CLIPBOARD
3231 /*
3232 * If we are using the clipboard, then remember what was selected in case
3233 * we need to paste it somewhere while we still own the selection.
3234 * Only do this when the clipboard is already owned. Don't want to grab
3235 * the selection when hitting ESC.
3236 */
3237 if (clip_star.available && clip_star.owned)
3238 clip_auto_select();
3239#endif
3240
3241 VIsual_active = FALSE;
3242#ifdef FEAT_MOUSE
3243 setmouse();
3244 mouse_dragging = 0;
3245#endif
3246
3247 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003248 curbuf->b_visual.vi_mode = VIsual_mode;
3249 curbuf->b_visual.vi_start = VIsual;
3250 curbuf->b_visual.vi_end = curwin->w_cursor;
3251 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252#ifdef FEAT_EVAL
3253 curbuf->b_visual_mode_eval = VIsual_mode;
3254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 if (!virtual_active())
3256 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003257 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003259 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260}
3261
3262/*
3263 * Reset VIsual_active and VIsual_reselect.
3264 */
3265 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003266reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267{
3268 if (VIsual_active)
3269 {
3270 end_visual_mode();
3271 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3272 }
3273 VIsual_reselect = FALSE;
3274}
3275
3276/*
3277 * Reset VIsual_active and VIsual_reselect if it's set.
3278 */
3279 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003280reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281{
3282 if (VIsual_active)
3283 {
3284 end_visual_mode();
3285 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3286 VIsual_reselect = FALSE;
3287 }
3288}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290/*
3291 * Check for a balloon-eval special item to include when searching for an
3292 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3293 * Returns TRUE if the character at "*ptr" should be included.
3294 * "dir" is FORWARD or BACKWARD, the direction of searching.
3295 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3296 * "bnp" points to a counter for square brackets.
3297 */
3298 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003299find_is_eval_item(
3300 char_u *ptr,
3301 int *colp,
3302 int *bnp,
3303 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304{
3305 /* Accept everything inside []. */
3306 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3307 ++*bnp;
3308 if (*bnp > 0)
3309 {
3310 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3311 --*bnp;
3312 return TRUE;
3313 }
3314
3315 /* skip over "s.var" */
3316 if (*ptr == '.')
3317 return TRUE;
3318
3319 /* two-character item: s->var */
3320 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3321 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3322 {
3323 *colp += dir;
3324 return TRUE;
3325 }
3326 return FALSE;
3327}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
3329/*
3330 * Find the identifier under or to the right of the cursor.
3331 * "find_type" can have one of three values:
3332 * FIND_IDENT: find an identifier (keyword)
3333 * FIND_STRING: find any non-white string
3334 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003335 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 *
3337 * There are three steps:
3338 * 1. Search forward for the start of an identifier/string. Doesn't move if
3339 * already on one.
3340 * 2. Search backward for the start of this identifier/string.
3341 * This doesn't match the real Vi but I like it a little better and it
3342 * shouldn't bother anyone.
3343 * 3. Search forward to the end of this identifier/string.
3344 * When FIND_IDENT isn't defined, we backup until a blank.
3345 *
3346 * Returns the length of the string, or zero if no string is found.
3347 * If a string is found, a pointer to the string is put in "*string". This
3348 * string is not always NUL terminated.
3349 */
3350 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003351find_ident_under_cursor(char_u **string, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352{
3353 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3354 curwin->w_cursor.col, string, find_type);
3355}
3356
3357/*
3358 * Like find_ident_under_cursor(), but for any window and any position.
3359 * However: Uses 'iskeyword' from the current window!.
3360 */
3361 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003362find_ident_at_pos(
3363 win_T *wp,
3364 linenr_T lnum,
3365 colnr_T startcol,
3366 char_u **string,
3367 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368{
3369 char_u *ptr;
3370 int col = 0; /* init to shut up GCC */
3371 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 int this_class = 0;
3373 int prev_class;
3374 int prevcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 int bn = 0; /* bracket nesting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376
3377 /*
3378 * if i == 0: try to find an identifier
3379 * if i == 1: try to find any non-white string
3380 */
3381 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3382 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3383 {
3384 /*
3385 * 1. skip to start of identifier/string
3386 */
3387 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 if (has_mbyte)
3389 {
3390 while (ptr[col] != NUL)
3391 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 /* Stop at a ']' to evaluate "a[x]". */
3393 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3394 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 this_class = mb_get_class(ptr + col);
3396 if (this_class != 0 && (i == 1 || this_class != 1))
3397 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003398 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 }
3400 }
3401 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01003403 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 )
3406 ++col;
3407
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 /* When starting on a ']' count it, so that we include the '['. */
3409 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410
3411 /*
3412 * 2. Back up to start of identifier/string.
3413 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 if (has_mbyte)
3415 {
3416 /* Remember class of character under cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3418 this_class = mb_get_class((char_u *)"a");
3419 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003421 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 {
3423 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3424 prev_class = mb_get_class(ptr + prevcol);
3425 if (this_class != prev_class
3426 && (i == 0
3427 || prev_class == 0
3428 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 && (!(find_type & FIND_EVAL)
3430 || prevcol == 0
3431 || !find_is_eval_item(ptr + prevcol, &prevcol,
3432 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 )
3434 break;
3435 col = prevcol;
3436 }
3437
3438 /* If we don't want just any old string, or we've found an
3439 * identifier, stop searching. */
3440 if (this_class > 2)
3441 this_class = 2;
3442 if (!(find_type & FIND_STRING) || this_class == 2)
3443 break;
3444 }
3445 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 {
3447 while (col > 0
3448 && ((i == 0
3449 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003450 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 && (!(find_type & FIND_IDENT)
3452 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 || ((find_type & FIND_EVAL)
3454 && col > 1
3455 && find_is_eval_item(ptr + col - 1, &col,
3456 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 ))
3458 --col;
3459
3460 /* If we don't want just any old string, or we've found an
3461 * identifier, stop searching. */
3462 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3463 break;
3464 }
3465 }
3466
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003467 if (ptr[col] == NUL || (i == 0
3468 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 {
3470 /*
3471 * didn't find an identifier or string
3472 */
3473 if (find_type & FIND_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003474 emsg(_("E348: No string under cursor"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003476 emsg(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 return 0;
3478 }
3479 ptr += col;
3480 *string = ptr;
3481
3482 /*
3483 * 3. Find the end if the identifier/string.
3484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 bn = 0;
3486 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 if (has_mbyte)
3489 {
3490 /* Search for point of changing multibyte character class. */
3491 this_class = mb_get_class(ptr);
3492 while (ptr[col] != NUL
3493 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3494 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 || ((find_type & FIND_EVAL)
3496 && col <= (int)startcol
3497 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003499 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 }
3501 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003503 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 || ((find_type & FIND_EVAL)
3505 && col <= (int)startcol
3506 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509
3510 return col;
3511}
3512
3513/*
3514 * Prepare for redo of a normal command.
3515 */
3516 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003517prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518{
3519 prep_redo(cap->oap->regname, cap->count0,
3520 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3521}
3522
3523/*
3524 * Prepare for redo of any command.
3525 * Note that only the last argument can be a multi-byte char.
3526 */
3527 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003528prep_redo(
3529 int regname,
3530 long num,
3531 int cmd1,
3532 int cmd2,
3533 int cmd3,
3534 int cmd4,
3535 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536{
3537 ResetRedobuff();
3538 if (regname != 0) /* yank from specified buffer */
3539 {
3540 AppendCharToRedobuff('"');
3541 AppendCharToRedobuff(regname);
3542 }
3543 if (num)
3544 AppendNumberToRedobuff(num);
3545
3546 if (cmd1 != NUL)
3547 AppendCharToRedobuff(cmd1);
3548 if (cmd2 != NUL)
3549 AppendCharToRedobuff(cmd2);
3550 if (cmd3 != NUL)
3551 AppendCharToRedobuff(cmd3);
3552 if (cmd4 != NUL)
3553 AppendCharToRedobuff(cmd4);
3554 if (cmd5 != NUL)
3555 AppendCharToRedobuff(cmd5);
3556}
3557
3558/*
3559 * check for operator active and clear it
3560 *
3561 * return TRUE if operator was active
3562 */
3563 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003564checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565{
3566 if (oap->op_type == OP_NOP)
3567 return FALSE;
3568 clearopbeep(oap);
3569 return TRUE;
3570}
3571
3572/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003573 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003575 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 */
3577 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003578checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003580 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 return FALSE;
3582 clearopbeep(oap);
3583 return TRUE;
3584}
3585
3586 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003587clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588{
3589 oap->op_type = OP_NOP;
3590 oap->regname = 0;
3591 oap->motion_force = NUL;
3592 oap->use_reg_one = FALSE;
3593}
3594
3595 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003596clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597{
3598 clearop(oap);
3599 beep_flush();
3600}
3601
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602/*
3603 * Remove the shift modifier from a special key.
3604 */
3605 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003606unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607{
3608 switch (cap->cmdchar)
3609 {
3610 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3611 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3612 case K_S_UP: cap->cmdchar = K_UP; break;
3613 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3614 case K_S_HOME: cap->cmdchar = K_HOME; break;
3615 case K_S_END: cap->cmdchar = K_END; break;
3616 }
3617 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3618}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003620/*
3621 * If the mode is currently displayed clear the command line or update the
3622 * command displayed.
3623 */
3624 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003625may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003626{
3627 if (mode_displayed)
3628 clear_cmdline = TRUE; /* unshow visual mode later */
3629#ifdef FEAT_CMDL_INFO
3630 else
3631 clear_showcmd();
3632#endif
3633}
3634
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3636/*
3637 * Routines for displaying a partly typed command
3638 */
3639
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003640#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641static char_u showcmd_buf[SHOWCMD_BUFLEN];
3642static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3643static int showcmd_is_clear = TRUE;
3644static int showcmd_visual = FALSE;
3645
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003646static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647
3648 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003649clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650{
3651 if (!p_sc)
3652 return;
3653
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 if (VIsual_active && !char_avail())
3655 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003656 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 long lines;
3658 colnr_T leftcol, rightcol;
3659 linenr_T top, bot;
3660
3661 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003662 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 {
3664 top = VIsual.lnum;
3665 bot = curwin->w_cursor.lnum;
3666 }
3667 else
3668 {
3669 top = curwin->w_cursor.lnum;
3670 bot = VIsual.lnum;
3671 }
3672# ifdef FEAT_FOLDING
3673 /* Include closed folds as a whole. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02003674 (void)hasFolding(top, &top, NULL);
3675 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676# endif
3677 lines = bot - top + 1;
3678
3679 if (VIsual_mode == Ctrl_V)
3680 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003681# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003682 char_u *saved_sbr = p_sbr;
3683
3684 /* Make 'sbr' empty for a moment to get the correct size. */
3685 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003686# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003688# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003689 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003690# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3692 (long)(rightcol - leftcol + 1));
3693 }
3694 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3695 sprintf((char *)showcmd_buf, "%ld", lines);
3696 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003697 {
3698 char_u *s, *e;
3699 int l;
3700 int bytes = 0;
3701 int chars = 0;
3702
3703 if (cursor_bot)
3704 {
3705 s = ml_get_pos(&VIsual);
3706 e = ml_get_cursor();
3707 }
3708 else
3709 {
3710 s = ml_get_cursor();
3711 e = ml_get_pos(&VIsual);
3712 }
3713 while ((*p_sel != 'e') ? s <= e : s < e)
3714 {
3715 l = (*mb_ptr2len)(s);
3716 if (l == 0)
3717 {
3718 ++bytes;
3719 ++chars;
3720 break; /* end of line */
3721 }
3722 bytes += l;
3723 ++chars;
3724 s += l;
3725 }
3726 if (bytes == chars)
3727 sprintf((char *)showcmd_buf, "%d", chars);
3728 else
3729 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3732 showcmd_visual = TRUE;
3733 }
3734 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 {
3736 showcmd_buf[0] = NUL;
3737 showcmd_visual = FALSE;
3738
3739 /* Don't actually display something if there is nothing to clear. */
3740 if (showcmd_is_clear)
3741 return;
3742 }
3743
3744 display_showcmd();
3745}
3746
3747/*
3748 * Add 'c' to string of shown command chars.
3749 * Return TRUE if output has been written (and setcursor() has been called).
3750 */
3751 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003752add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753{
3754 char_u *p;
3755 int old_len;
3756 int extra_len;
3757 int overflow;
3758#if defined(FEAT_MOUSE)
3759 int i;
3760 static int ignore[] =
3761 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003762# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3764 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003765# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01003766 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003767 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3769 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003770 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003772 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 0
3774 };
3775#endif
3776
Bram Moolenaar09df3122006-01-23 22:23:09 +00003777 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 return FALSE;
3779
3780 if (showcmd_visual)
3781 {
3782 showcmd_buf[0] = NUL;
3783 showcmd_visual = FALSE;
3784 }
3785
3786#if defined(FEAT_MOUSE)
3787 /* Ignore keys that are scrollbar updates and mouse clicks */
3788 if (IS_SPECIAL(c))
3789 for (i = 0; ignore[i] != 0; ++i)
3790 if (ignore[i] == c)
3791 return FALSE;
3792#endif
3793
3794 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003795 if (*p == ' ')
3796 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 old_len = (int)STRLEN(showcmd_buf);
3798 extra_len = (int)STRLEN(p);
3799 overflow = old_len + extra_len - SHOWCMD_COLS;
3800 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003801 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3802 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 STRCAT(showcmd_buf, p);
3804
3805 if (char_avail())
3806 return FALSE;
3807
3808 display_showcmd();
3809
3810 return TRUE;
3811}
3812
3813 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003814add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815{
3816 if (!add_to_showcmd(c))
3817 setcursor();
3818}
3819
3820/*
3821 * Delete 'len' characters from the end of the shown command.
3822 */
3823 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003824del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825{
3826 int old_len;
3827
3828 if (!p_sc)
3829 return;
3830
3831 old_len = (int)STRLEN(showcmd_buf);
3832 if (len > old_len)
3833 len = old_len;
3834 showcmd_buf[old_len - len] = NUL;
3835
3836 if (!char_avail())
3837 display_showcmd();
3838}
3839
3840/*
3841 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3842 * something and there is a partial mapping.
3843 */
3844 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003845push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846{
3847 if (p_sc)
3848 STRCPY(old_showcmd_buf, showcmd_buf);
3849}
3850
3851 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003852pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853{
3854 if (!p_sc)
3855 return;
3856
3857 STRCPY(showcmd_buf, old_showcmd_buf);
3858
3859 display_showcmd();
3860}
3861
3862 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003863display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864{
3865 int len;
3866
3867 cursor_off();
3868
3869 len = (int)STRLEN(showcmd_buf);
3870 if (len == 0)
3871 showcmd_is_clear = TRUE;
3872 else
3873 {
3874 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3875 showcmd_is_clear = FALSE;
3876 }
3877
3878 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003879 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3880 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 */
3882 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3883
3884 setcursor(); /* put cursor back where it belongs */
3885}
3886#endif
3887
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888/*
3889 * When "check" is FALSE, prepare for commands that scroll the window.
3890 * When "check" is TRUE, take care of scroll-binding after the window has
3891 * scrolled. Called from normal_cmd() and edit().
3892 */
3893 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003894do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895{
3896 static win_T *old_curwin = NULL;
3897 static linenr_T old_topline = 0;
3898#ifdef FEAT_DIFF
3899 static int old_topfill = 0;
3900#endif
3901 static buf_T *old_buf = NULL;
3902 static colnr_T old_leftcol = 0;
3903
3904 if (check && curwin->w_p_scb)
3905 {
3906 /* If a ":syncbind" command was just used, don't scroll, only reset
3907 * the values. */
3908 if (did_syncbind)
3909 did_syncbind = FALSE;
3910 else if (curwin == old_curwin)
3911 {
3912 /*
3913 * Synchronize other windows, as necessary according to
3914 * 'scrollbind'. Don't do this after an ":edit" command, except
3915 * when 'diff' is set.
3916 */
3917 if ((curwin->w_buffer == old_buf
3918#ifdef FEAT_DIFF
3919 || curwin->w_p_diff
3920#endif
3921 )
3922 && (curwin->w_topline != old_topline
3923#ifdef FEAT_DIFF
3924 || curwin->w_topfill != old_topfill
3925#endif
3926 || curwin->w_leftcol != old_leftcol))
3927 {
3928 check_scrollbind(curwin->w_topline - old_topline,
3929 (long)(curwin->w_leftcol - old_leftcol));
3930 }
3931 }
3932 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
3933 {
3934 /*
3935 * When switching between windows, make sure that the relative
3936 * vertical offset is valid for the new window. The relative
3937 * offset is invalid whenever another 'scrollbind' window has
3938 * scrolled to a point that would force the current window to
3939 * scroll past the beginning or end of its buffer. When the
3940 * resync is performed, some of the other 'scrollbind' windows may
3941 * need to jump so that the current window's relative position is
3942 * visible on-screen.
3943 */
3944 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
3945 }
3946 curwin->w_scbind_pos = curwin->w_topline;
3947 }
3948
3949 old_curwin = curwin;
3950 old_topline = curwin->w_topline;
3951#ifdef FEAT_DIFF
3952 old_topfill = curwin->w_topfill;
3953#endif
3954 old_buf = curwin->w_buffer;
3955 old_leftcol = curwin->w_leftcol;
3956}
3957
3958/*
3959 * Synchronize any windows that have "scrollbind" set, based on the
3960 * number of rows by which the current window has changed
3961 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3962 */
3963 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003964check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965{
3966 int want_ver;
3967 int want_hor;
3968 win_T *old_curwin = curwin;
3969 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 int old_VIsual_select = VIsual_select;
3971 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 colnr_T tgt_leftcol = curwin->w_leftcol;
3973 long topline;
3974 long y;
3975
3976 /*
3977 * check 'scrollopt' string for vertical and horizontal scroll options
3978 */
3979 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
3980#ifdef FEAT_DIFF
3981 want_ver |= old_curwin->w_p_diff;
3982#endif
3983 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
3984
3985 /*
3986 * loop through the scrollbound windows and scroll accordingly
3987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003989 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 {
3991 curbuf = curwin->w_buffer;
3992 /* skip original window and windows with 'noscrollbind' */
3993 if (curwin != old_curwin && curwin->w_p_scb)
3994 {
3995 /*
3996 * do the vertical scroll
3997 */
3998 if (want_ver)
3999 {
4000#ifdef FEAT_DIFF
4001 if (old_curwin->w_p_diff && curwin->w_p_diff)
4002 {
4003 diff_set_topline(old_curwin, curwin);
4004 }
4005 else
4006#endif
4007 {
4008 curwin->w_scbind_pos += topline_diff;
4009 topline = curwin->w_scbind_pos;
4010 if (topline > curbuf->b_ml.ml_line_count)
4011 topline = curbuf->b_ml.ml_line_count;
4012 if (topline < 1)
4013 topline = 1;
4014
4015 y = topline - curwin->w_topline;
4016 if (y > 0)
4017 scrollup(y, FALSE);
4018 else
4019 scrolldown(-y, FALSE);
4020 }
4021
4022 redraw_later(VALID);
4023 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 }
4026
4027 /*
4028 * do the horizontal scroll
4029 */
4030 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4031 {
4032 curwin->w_leftcol = tgt_leftcol;
4033 leftcol_changed();
4034 }
4035 }
4036 }
4037
4038 /*
4039 * reset current-window
4040 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 VIsual_select = old_VIsual_select;
4042 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 curwin = old_curwin;
4044 curbuf = old_curbuf;
4045}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046
4047/*
4048 * Command character that's ignored.
4049 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004050 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004053nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004055 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056}
4057
4058/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004059 * Command character that doesn't do anything, but unlike nv_ignore() does
4060 * start edit(). Used for "startinsert" executed while starting up.
4061 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004062 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004063nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00004064{
4065}
4066
4067/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 * Command character doesn't exist.
4069 */
4070 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004071nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072{
4073 clearopbeep(cap->oap);
4074}
4075
4076/*
4077 * <Help> and <F1> commands.
4078 */
4079 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004080nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081{
4082 if (!checkclearopq(cap->oap))
4083 ex_help(NULL);
4084}
4085
4086/*
4087 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4088 */
4089 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004090nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091{
Bram Moolenaarf2732452018-06-03 14:47:35 +02004092#ifdef FEAT_JOB_CHANNEL
4093 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4094 clearopbeep(cap->oap);
4095 else
4096#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01004097 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004098 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01004099 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01004100 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
4101 op_addsub(cap->oap, cap->count1, cap->arg);
4102 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004103 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01004104 else if (VIsual_active)
4105 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004106 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01004107 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108}
4109
4110/*
4111 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4112 */
4113 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004114nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115{
4116 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004117 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004118 if (mod_mask & MOD_MASK_CTRL)
4119 {
4120 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4121 if (cap->arg == BACKWARD)
4122 goto_tabpage(-(int)cap->count1);
4123 else
4124 goto_tabpage((int)cap->count0);
4125 }
4126 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02004127 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129}
4130
4131/*
4132 * Implementation of "gd" and "gD" command.
4133 */
4134 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004135nv_gd(
4136 oparg_T *oap,
4137 int nchar,
4138 int thisblock) /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139{
4140 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 char_u *ptr;
4142
Bram Moolenaard9d30582005-05-18 22:10:28 +00004143 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02004144 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
4145 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004147#ifdef FEAT_FOLDING
4148 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4149 foldOpenCursor();
4150#endif
4151}
4152
4153/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02004154 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
4155 * otherwise.
4156 */
4157 static int
4158is_ident(char_u *line, int offset)
4159{
4160 int i;
4161 int incomment = FALSE;
4162 int instring = 0;
4163 int prev = 0;
4164
4165 for (i = 0; i < offset && line[i] != NUL; i++)
4166 {
4167 if (instring != 0)
4168 {
4169 if (prev != '\\' && line[i] == instring)
4170 instring = 0;
4171 }
4172 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
4173 {
4174 instring = line[i];
4175 }
4176 else
4177 {
4178 if (incomment)
4179 {
4180 if (prev == '*' && line[i] == '/')
4181 incomment = FALSE;
4182 }
4183 else if (prev == '/' && line[i] == '*')
4184 {
4185 incomment = TRUE;
4186 }
4187 else if (prev == '/' && line[i] == '/')
4188 {
4189 return FALSE;
4190 }
4191 }
4192
4193 prev = line[i];
4194 }
4195
4196 return incomment == FALSE && instring == 0;
4197}
4198
4199/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004200 * Search for variable declaration of "ptr[len]".
4201 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4202 * current file ("gD").
4203 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004204 * Return FAIL when not found.
4205 */
4206 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004207find_decl(
4208 char_u *ptr,
4209 int len,
4210 int locally,
4211 int thisblock,
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004212 int flags_arg) /* flags passed to searchit() */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004213{
4214 char_u *pat;
4215 pos_T old_pos;
4216 pos_T par_pos;
4217 pos_T found_pos;
4218 int t;
4219 int save_p_ws;
4220 int save_p_scs;
4221 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004222 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004223 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004224 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004225
4226 if ((pat = alloc(len + 7)) == NULL)
4227 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004228
4229 /* Put "\V" before the pattern to avoid that the special meaning of "."
4230 * and "~" causes trouble. */
4231 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4232 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 old_pos = curwin->w_cursor;
4234 save_p_ws = p_ws;
4235 save_p_scs = p_scs;
4236 p_ws = FALSE; /* don't wrap around end of file now */
4237 p_scs = FALSE; /* don't switch ignorecase off now */
4238
4239 /*
4240 * With "gD" go to line 1.
4241 * With "gd" Search back for the start of the current function, then go
4242 * back until a blank line. If this fails go to line 1.
4243 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004244 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 {
4246 setpcmark(); /* Set in findpar() otherwise */
4247 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004248 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 }
4250 else
4251 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004252 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4254 --curwin->w_cursor.lnum;
4255 }
4256 curwin->w_cursor.col = 0;
4257
4258 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004259 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004260 for (;;)
4261 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004262 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004263 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004264 if (curwin->w_cursor.lnum >= old_pos.lnum)
4265 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004266
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004267 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004268 {
4269 pos_T *pos;
4270
4271 /* Check that the block the match is in doesn't end before the
4272 * position where we started the search from. */
4273 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4274 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4275 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02004276 {
4277 /* There can't be a useful match before the end of this block.
4278 * Skip to the end. */
4279 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004280 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02004281 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004282 }
4283
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004284 if (t == FAIL)
4285 {
4286 /* If we previously found a valid position, use it. */
4287 if (found_pos.lnum != 0)
4288 {
4289 curwin->w_cursor = found_pos;
4290 t = OK;
4291 }
4292 break;
4293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004295 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004296 {
4297 /* Ignore this line, continue at start of next line. */
4298 ++curwin->w_cursor.lnum;
4299 curwin->w_cursor.col = 0;
4300 continue;
4301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302#endif
Bram Moolenaar226630a2016-10-08 19:21:31 +02004303 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
4304
4305 /* If the current position is not a valid identifier and a previous
4306 * match is present, favor that one instead. */
4307 if (!valid && found_pos.lnum != 0)
4308 {
4309 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004310 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004311 }
4312
4313 /* Global search: use first valid match found */
4314 if (valid && !locally)
4315 break;
4316 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004317 {
4318 /* If we previously found a valid position, use it. */
4319 if (found_pos.lnum != 0)
4320 curwin->w_cursor = found_pos;
4321 break;
4322 }
4323
Bram Moolenaar226630a2016-10-08 19:21:31 +02004324 /* For finding a local variable and the match is before the "{" or
4325 * inside a comment, continue searching. For K&R style function
4326 * declarations this skips the function header without types. */
4327 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004328 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02004329 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02004330 found_pos = curwin->w_cursor;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004331 /* Remove SEARCH_START from flags to avoid getting stuck at one
4332 * position. */
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004333 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004335
4336 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004338 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 curwin->w_cursor = old_pos;
4340 }
4341 else
4342 {
4343 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 /* "n" searches forward now */
4345 reset_search_dir();
4346 }
4347
4348 vim_free(pat);
4349 p_ws = save_p_ws;
4350 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004351
4352 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353}
4354
4355/*
4356 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4357 * lines rather than lines in the file.
4358 * 'dist' must be positive.
4359 *
4360 * Return OK if able to move cursor, FAIL otherwise.
4361 */
4362 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004363nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364{
4365 int linelen = linetabsize(ml_get_curline());
4366 int retval = OK;
4367 int atend = FALSE;
4368 int n;
4369 int col_off1; /* margin offset for first screen line */
4370 int col_off2; /* margin offset for wrapped screen line */
4371 int width1; /* text width for first screen line */
4372 int width2; /* test width for wrapped screen line */
4373
4374 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004375 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376
4377 col_off1 = curwin_col_off();
4378 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02004379 width1 = curwin->w_width - col_off1;
4380 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01004381 if (width2 == 0)
4382 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004385 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 /*
4387 * Instead of sticking at the last character of the buffer line we
4388 * try to stick in the last column of the screen.
4389 */
4390 if (curwin->w_curswant == MAXCOL)
4391 {
4392 atend = TRUE;
4393 validate_virtcol();
4394 if (width1 <= 0)
4395 curwin->w_curswant = 0;
4396 else
4397 {
4398 curwin->w_curswant = width1 - 1;
4399 if (curwin->w_virtcol > curwin->w_curswant)
4400 curwin->w_curswant += ((curwin->w_virtcol
4401 - curwin->w_curswant - 1) / width2 + 1) * width2;
4402 }
4403 }
4404 else
4405 {
4406 if (linelen > width1)
4407 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4408 else
4409 n = width1;
4410 if (curwin->w_curswant > (colnr_T)n + 1)
4411 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4412 * width2;
4413 }
4414
4415 while (dist--)
4416 {
4417 if (dir == BACKWARD)
4418 {
4419 if ((long)curwin->w_curswant >= width2)
4420 /* move back within line */
4421 curwin->w_curswant -= width2;
4422 else
4423 {
4424 /* to previous line */
4425 if (curwin->w_cursor.lnum == 1)
4426 {
4427 retval = FAIL;
4428 break;
4429 }
4430 --curwin->w_cursor.lnum;
4431#ifdef FEAT_FOLDING
4432 /* Move to the start of a closed fold. Don't do that when
4433 * 'foldopen' contains "all": it will open in a moment. */
4434 if (!(fdo_flags & FDO_ALL))
4435 (void)hasFolding(curwin->w_cursor.lnum,
4436 &curwin->w_cursor.lnum, NULL);
4437#endif
4438 linelen = linetabsize(ml_get_curline());
4439 if (linelen > width1)
4440 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4441 + 1) * width2;
4442 }
4443 }
4444 else /* dir == FORWARD */
4445 {
4446 if (linelen > width1)
4447 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4448 else
4449 n = width1;
4450 if (curwin->w_curswant + width2 < (colnr_T)n)
4451 /* move forward within line */
4452 curwin->w_curswant += width2;
4453 else
4454 {
4455 /* to next line */
4456#ifdef FEAT_FOLDING
4457 /* Move to the end of a closed fold. */
4458 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4459 &curwin->w_cursor.lnum);
4460#endif
4461 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4462 {
4463 retval = FAIL;
4464 break;
4465 }
4466 curwin->w_cursor.lnum++;
4467 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004468 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 }
4470 }
4471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004474 if (virtual_active() && atend)
4475 coladvance(MAXCOL);
4476 else
4477 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4480 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004481 colnr_T virtcol;
4482
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 /*
4484 * Check for landing on a character that got split at the end of the
4485 * last line. We want to advance a screenline, not end up in the same
4486 * screenline or move two screenlines.
4487 */
4488 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004489 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004490#if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004491 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4492 virtcol -= vim_strsize(p_sbr);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004493#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004494
4495 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 && (curwin->w_curswant < (colnr_T)width1
4497 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4498 : ((curwin->w_curswant - width1) % width2
4499 > (colnr_T)width2 / 2)))
4500 --curwin->w_cursor.col;
4501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502
4503 if (atend)
4504 curwin->w_curswant = MAXCOL; /* stick in the last column */
4505
4506 return retval;
4507}
4508
4509#ifdef FEAT_MOUSE
4510/*
4511 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4512 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004513 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4514 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 */
4516 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004517nv_mousescroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518{
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004519 win_T *old_curwin = curwin, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004521 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 {
4523 int row, col;
4524
4525 row = mouse_row;
4526 col = mouse_col;
4527
4528 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004529 wp = mouse_find_win(&row, &col);
4530 if (wp == NULL)
4531 return;
4532 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 curbuf = curwin->w_buffer;
4534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004536 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004538# ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02004539 if (term_use_loop())
Bram Moolenaar73675fb2017-11-20 21:49:19 +01004540 /* This window is a terminal window, send the mouse event there.
4541 * Set "typed" to FALSE to avoid an endless loop. */
4542 send_keys_to_term(curbuf->b_term, cap->cmdchar, FALSE);
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004543 else
4544# endif
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004545 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4546 {
4547 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4548 }
4549 else
4550 {
4551 cap->count1 = 3;
4552 cap->count0 = 3;
4553 nv_scroll_line(cap);
4554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004556# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 else
4558 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004559 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4560 if (!curwin->w_p_wrap)
4561 {
4562 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004563
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004564 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02004565 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004566 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4567 if (val < 0)
4568 val = 0;
4569
4570 gui_do_horiz_scroll(val, TRUE);
4571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004573# endif
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +01004574# ifdef FEAT_SYN_HL
4575 if (curwin != old_curwin && curwin->w_p_cul)
4576 redraw_for_cursorline(curwin);
4577# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 curwin->w_redr_status = TRUE;
4580
4581 curwin = old_curwin;
4582 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583}
4584
4585/*
4586 * Mouse clicks and drags.
4587 */
4588 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004589nv_mouse(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590{
4591 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4592}
4593#endif
4594
4595/*
4596 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4597 * cap->arg must be TRUE for CTRL-E.
4598 */
4599 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004600nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601{
4602 if (!checkclearop(cap->oap))
4603 scroll_redraw(cap->arg, cap->count1);
4604}
4605
4606/*
4607 * Scroll "count" lines up or down, and redraw.
4608 */
4609 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004610scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611{
4612 linenr_T prev_topline = curwin->w_topline;
4613#ifdef FEAT_DIFF
4614 int prev_topfill = curwin->w_topfill;
4615#endif
4616 linenr_T prev_lnum = curwin->w_cursor.lnum;
4617
4618 if (up)
4619 scrollup(count, TRUE);
4620 else
4621 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01004622 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 {
4624 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4625 * valid, otherwise the screen jumps back at the end of the file. */
4626 cursor_correct();
4627 check_cursor_moved(curwin);
4628 curwin->w_valid |= VALID_TOPLINE;
4629
4630 /* If moved back to where we were, at least move the cursor, otherwise
4631 * we get stuck at one position. Don't move the cursor up if the
4632 * first line of the buffer is already on the screen */
4633 while (curwin->w_topline == prev_topline
4634#ifdef FEAT_DIFF
4635 && curwin->w_topfill == prev_topfill
4636#endif
4637 )
4638 {
4639 if (up)
4640 {
4641 if (curwin->w_cursor.lnum > prev_lnum
4642 || cursor_down(1L, FALSE) == FAIL)
4643 break;
4644 }
4645 else
4646 {
4647 if (curwin->w_cursor.lnum < prev_lnum
4648 || prev_topline == 1L
4649 || cursor_up(1L, FALSE) == FAIL)
4650 break;
4651 }
4652 /* Mark w_topline as valid, otherwise the screen jumps back at the
4653 * end of the file. */
4654 check_cursor_moved(curwin);
4655 curwin->w_valid |= VALID_TOPLINE;
4656 }
4657 }
4658 if (curwin->w_cursor.lnum != prev_lnum)
4659 coladvance(curwin->w_curswant);
4660 redraw_later(VALID);
4661}
4662
4663/*
4664 * Commands that start with "z".
4665 */
4666 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004667nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668{
4669 long n;
4670 colnr_T col;
4671 int nchar = cap->nchar;
4672#ifdef FEAT_FOLDING
4673 long old_fdl = curwin->w_p_fdl;
4674 int old_fen = curwin->w_p_fen;
4675#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004676#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004677 int undo = FALSE;
4678#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01004679 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680
4681 if (VIM_ISDIGIT(nchar))
4682 {
4683 /*
4684 * "z123{nchar}": edit the count before obtaining {nchar}
4685 */
4686 if (checkclearop(cap->oap))
4687 return;
4688 n = nchar - '0';
4689 for (;;)
4690 {
4691#ifdef USE_ON_FLY_SCROLL
4692 dont_scroll = TRUE; /* disallow scrolling here */
4693#endif
4694 ++no_mapping;
4695 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004696 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 --no_mapping;
4699 --allow_keys;
4700#ifdef FEAT_CMDL_INFO
4701 (void)add_to_showcmd(nchar);
4702#endif
4703 if (nchar == K_DEL || nchar == K_KDEL)
4704 n /= 10;
4705 else if (VIM_ISDIGIT(nchar))
4706 n = n * 10 + (nchar - '0');
4707 else if (nchar == CAR)
4708 {
4709#ifdef FEAT_GUI
4710 need_mouse_correct = TRUE;
4711#endif
4712 win_setheight((int)n);
4713 break;
4714 }
4715 else if (nchar == 'l'
4716 || nchar == 'h'
4717 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004718 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 {
4720 cap->count1 = n ? n * cap->count1 : cap->count1;
4721 goto dozet;
4722 }
4723 else
4724 {
4725 clearopbeep(cap->oap);
4726 break;
4727 }
4728 }
4729 cap->oap->op_type = OP_NOP;
4730 return;
4731 }
4732
4733dozet:
4734 if (
4735#ifdef FEAT_FOLDING
4736 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4737 * and "zC" only in Visual mode. "zj" and "zk" are motion
4738 * commands. */
4739 cap->nchar != 'f' && cap->nchar != 'F'
4740 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4741 && cap->nchar != 'j' && cap->nchar != 'k'
4742 &&
4743#endif
4744 checkclearop(cap->oap))
4745 return;
4746
4747 /*
4748 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4749 * If line number given, set cursor.
4750 */
4751 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4752 && cap->count0
4753 && cap->count0 != curwin->w_cursor.lnum)
4754 {
4755 setpcmark();
4756 if (cap->count0 > curbuf->b_ml.ml_line_count)
4757 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4758 else
4759 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004760 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 }
4762
4763 switch (nchar)
4764 {
4765 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4766 case '+':
4767 if (cap->count0 == 0)
4768 {
4769 /* No count given: put cursor at the line below screen */
4770 validate_botline(); /* make sure w_botline is valid */
4771 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4772 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4773 else
4774 curwin->w_cursor.lnum = curwin->w_botline;
4775 }
4776 /* FALLTHROUGH */
4777 case NL:
4778 case CAR:
4779 case K_KENTER:
4780 beginline(BL_WHITE | BL_FIX);
4781 /* FALLTHROUGH */
4782
4783 case 't': scroll_cursor_top(0, TRUE);
4784 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004785 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 break;
4787
4788 /* "z." and "zz": put cursor in middle of screen */
4789 case '.': beginline(BL_WHITE | BL_FIX);
4790 /* FALLTHROUGH */
4791
4792 case 'z': scroll_cursor_halfway(TRUE);
4793 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004794 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 break;
4796
4797 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4798 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4799 * when <count> is at bottom of window, and puts that one at
4800 * bottom of window. */
4801 if (cap->count0 != 0)
4802 {
4803 scroll_cursor_bot(0, TRUE);
4804 curwin->w_cursor.lnum = curwin->w_topline;
4805 }
4806 else if (curwin->w_topline == 1)
4807 curwin->w_cursor.lnum = 1;
4808 else
4809 curwin->w_cursor.lnum = curwin->w_topline - 1;
4810 /* FALLTHROUGH */
4811 case '-':
4812 beginline(BL_WHITE | BL_FIX);
4813 /* FALLTHROUGH */
4814
4815 case 'b': scroll_cursor_bot(0, TRUE);
4816 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004817 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 break;
4819
4820 /* "zH" - scroll screen right half-page */
4821 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02004822 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 /* FALLTHROUGH */
4824
4825 /* "zh" - scroll screen to the right */
4826 case 'h':
4827 case K_LEFT:
4828 if (!curwin->w_p_wrap)
4829 {
4830 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4831 curwin->w_leftcol = 0;
4832 else
4833 curwin->w_leftcol -= (colnr_T)cap->count1;
4834 leftcol_changed();
4835 }
4836 break;
4837
4838 /* "zL" - scroll screen left half-page */
Bram Moolenaar02631462017-09-22 15:20:32 +02004839 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 /* FALLTHROUGH */
4841
4842 /* "zl" - scroll screen to the left */
4843 case 'l':
4844 case K_RIGHT:
4845 if (!curwin->w_p_wrap)
4846 {
4847 /* scroll the window left */
4848 curwin->w_leftcol += (colnr_T)cap->count1;
4849 leftcol_changed();
4850 }
4851 break;
4852
4853 /* "zs" - scroll screen, cursor at the start */
4854 case 's': if (!curwin->w_p_wrap)
4855 {
4856#ifdef FEAT_FOLDING
4857 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4858 col = 0; /* like the cursor is in col 0 */
4859 else
4860#endif
4861 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01004862 if ((long)col > siso)
4863 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 else
4865 col = 0;
4866 if (curwin->w_leftcol != col)
4867 {
4868 curwin->w_leftcol = col;
4869 redraw_later(NOT_VALID);
4870 }
4871 }
4872 break;
4873
4874 /* "ze" - scroll screen, cursor at the end */
4875 case 'e': if (!curwin->w_p_wrap)
4876 {
4877#ifdef FEAT_FOLDING
4878 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4879 col = 0; /* like the cursor is in col 0 */
4880 else
4881#endif
4882 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02004883 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01004884 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 col = 0;
4886 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01004887 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 if (curwin->w_leftcol != col)
4889 {
4890 curwin->w_leftcol = col;
4891 redraw_later(NOT_VALID);
4892 }
4893 }
4894 break;
4895
4896#ifdef FEAT_FOLDING
4897 /* "zF": create fold command */
4898 /* "zf": create fold operator */
4899 case 'F':
4900 case 'f': if (foldManualAllowed(TRUE))
4901 {
4902 cap->nchar = 'f';
4903 nv_operator(cap);
4904 curwin->w_p_fen = TRUE;
4905
4906 /* "zF" is like "zfzf" */
4907 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4908 {
4909 nv_operator(cap);
4910 finish_op = TRUE;
4911 }
4912 }
4913 else
4914 clearopbeep(cap->oap);
4915 break;
4916
4917 /* "zd": delete fold at cursor */
4918 /* "zD": delete fold at cursor recursively */
4919 case 'd':
4920 case 'D': if (foldManualAllowed(FALSE))
4921 {
4922 if (VIsual_active)
4923 nv_operator(cap);
4924 else
4925 deleteFold(curwin->w_cursor.lnum,
4926 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4927 }
4928 break;
4929
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004930 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 case 'E': if (foldmethodIsManual(curwin))
4932 {
4933 clearFolding(curwin);
4934 changed_window_setting();
4935 }
4936 else if (foldmethodIsMarker(curwin))
4937 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4938 TRUE, FALSE);
4939 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004940 emsg(_("E352: Cannot erase folds with current 'foldmethod'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 break;
4942
4943 /* "zn": fold none: reset 'foldenable' */
4944 case 'n': curwin->w_p_fen = FALSE;
4945 break;
4946
4947 /* "zN": fold Normal: set 'foldenable' */
4948 case 'N': curwin->w_p_fen = TRUE;
4949 break;
4950
4951 /* "zi": invert folding: toggle 'foldenable' */
4952 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4953 break;
4954
4955 /* "za": open closed fold or close open fold at cursor */
4956 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4957 openFold(curwin->w_cursor.lnum, cap->count1);
4958 else
4959 {
4960 closeFold(curwin->w_cursor.lnum, cap->count1);
4961 curwin->w_p_fen = TRUE;
4962 }
4963 break;
4964
4965 /* "zA": open fold at cursor recursively */
4966 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4967 openFoldRecurse(curwin->w_cursor.lnum);
4968 else
4969 {
4970 closeFoldRecurse(curwin->w_cursor.lnum);
4971 curwin->w_p_fen = TRUE;
4972 }
4973 break;
4974
4975 /* "zo": open fold at cursor or Visual area */
4976 case 'o': if (VIsual_active)
4977 nv_operator(cap);
4978 else
4979 openFold(curwin->w_cursor.lnum, cap->count1);
4980 break;
4981
4982 /* "zO": open fold recursively */
4983 case 'O': if (VIsual_active)
4984 nv_operator(cap);
4985 else
4986 openFoldRecurse(curwin->w_cursor.lnum);
4987 break;
4988
4989 /* "zc": close fold at cursor or Visual area */
4990 case 'c': if (VIsual_active)
4991 nv_operator(cap);
4992 else
4993 closeFold(curwin->w_cursor.lnum, cap->count1);
4994 curwin->w_p_fen = TRUE;
4995 break;
4996
4997 /* "zC": close fold recursively */
4998 case 'C': if (VIsual_active)
4999 nv_operator(cap);
5000 else
5001 closeFoldRecurse(curwin->w_cursor.lnum);
5002 curwin->w_p_fen = TRUE;
5003 break;
5004
5005 /* "zv": open folds at the cursor */
5006 case 'v': foldOpenCursor();
5007 break;
5008
5009 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5010 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005011 curwin->w_foldinvalid = TRUE; /* recompute folds */
5012 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 foldOpenCursor();
5014 break;
5015
5016 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5017 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005018 curwin->w_foldinvalid = TRUE; /* recompute folds */
5019 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 break;
5021
5022 /* "zm": fold more */
5023 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005024 {
5025 curwin->w_p_fdl -= cap->count1;
5026 if (curwin->w_p_fdl < 0)
5027 curwin->w_p_fdl = 0;
5028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 old_fdl = -1; /* force an update */
5030 curwin->w_p_fen = TRUE;
5031 break;
5032
5033 /* "zM": close all folds */
5034 case 'M': curwin->w_p_fdl = 0;
5035 old_fdl = -1; /* force an update */
5036 curwin->w_p_fen = TRUE;
5037 break;
5038
5039 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005040 case 'r': curwin->w_p_fdl += cap->count1;
5041 {
5042 int d = getDeepestNesting();
5043
5044 if (curwin->w_p_fdl >= d)
5045 curwin->w_p_fdl = d;
5046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 break;
5048
5049 /* "zR": open all folds */
5050 case 'R': curwin->w_p_fdl = getDeepestNesting();
5051 old_fdl = -1; /* force an update */
5052 break;
5053
5054 case 'j': /* "zj" move to next fold downwards */
5055 case 'k': /* "zk" move to next fold upwards */
5056 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5057 cap->count1) == FAIL)
5058 clearopbeep(cap->oap);
5059 break;
5060
5061#endif /* FEAT_FOLDING */
5062
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005063#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005064 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5065 ++no_mapping;
5066 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005067 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005068 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005069 --no_mapping;
5070 --allow_keys;
5071#ifdef FEAT_CMDL_INFO
5072 (void)add_to_showcmd(nchar);
5073#endif
5074 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5075 {
5076 clearopbeep(cap->oap);
5077 break;
5078 }
5079 undo = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005080 /* FALLTHROUGH */
Bram Moolenaard0131a82006-03-04 21:46:13 +00005081
Bram Moolenaarb765d632005-06-07 21:00:02 +00005082 case 'g': /* "zg": add good word to word list */
5083 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005084 case 'G': /* "zG": add good word to temp word list */
5085 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005086 {
5087 char_u *ptr = NULL;
5088 int len;
5089
5090 if (checkclearop(cap->oap))
5091 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005092 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5093 == FAIL)
5094 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005095 if (ptr == NULL)
5096 {
5097 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005098
Bram Moolenaar134bf072013-09-25 18:54:24 +02005099 /* Find bad word under the cursor. When 'spell' is
5100 * off this fails and find_ident_under_cursor() is
5101 * used below. */
5102 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005103 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005104 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005105 if (len != 0 && curwin->w_cursor.col <= pos.col)
5106 ptr = ml_get_pos(&curwin->w_cursor);
5107 curwin->w_cursor = pos;
5108 }
5109
Bram Moolenaarb765d632005-06-07 21:00:02 +00005110 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5111 FIND_IDENT)) == 0)
5112 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005113 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005114 (nchar == 'G' || nchar == 'W')
5115 ? 0 : (int)cap->count1,
5116 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005117 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005118 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005119
Bram Moolenaar43abc522005-12-10 20:15:02 +00005120 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005121 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005122 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005123 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005124#endif
5125
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 default: clearopbeep(cap->oap);
5127 }
5128
5129#ifdef FEAT_FOLDING
5130 /* Redraw when 'foldenable' changed */
5131 if (old_fen != curwin->w_p_fen)
5132 {
5133# ifdef FEAT_DIFF
5134 win_T *wp;
5135
5136 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5137 {
5138 /* Adjust 'foldenable' in diff-synced windows. */
5139 FOR_ALL_WINDOWS(wp)
5140 {
5141 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5142 {
5143 wp->w_p_fen = curwin->w_p_fen;
5144 changed_window_setting_win(wp);
5145 }
5146 }
5147 }
5148# endif
5149 changed_window_setting();
5150 }
5151
5152 /* Redraw when 'foldlevel' changed. */
5153 if (old_fdl != curwin->w_p_fdl)
5154 newFoldLevel();
5155#endif
5156}
5157
5158#ifdef FEAT_GUI
5159/*
5160 * Vertical scrollbar movement.
5161 */
5162 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005163nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164{
5165 if (cap->oap->op_type != OP_NOP)
5166 clearopbeep(cap->oap);
5167
5168 /* Even if an operator was pending, we still want to scroll */
5169 gui_do_scroll();
5170}
5171
5172/*
5173 * Horizontal scrollbar movement.
5174 */
5175 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005176nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177{
5178 if (cap->oap->op_type != OP_NOP)
5179 clearopbeep(cap->oap);
5180
5181 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005182 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183}
5184#endif
5185
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005186#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005187/*
5188 * Click in GUI tab.
5189 */
5190 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005191nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005192{
5193 if (cap->oap->op_type != OP_NOP)
5194 clearopbeep(cap->oap);
5195
5196 /* Even if an operator was pending, we still want to jump tabs. */
5197 goto_tabpage(current_tab);
5198}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005199
5200/*
5201 * Selected item in tab line menu.
5202 */
5203 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005204nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005205{
5206 if (cap->oap->op_type != OP_NOP)
5207 clearopbeep(cap->oap);
5208
5209 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005210 handle_tabmenu();
5211}
5212
5213/*
5214 * Handle selecting an item of the GUI tab line menu.
5215 * Used in Normal and Insert mode.
5216 */
5217 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005218handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005219{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005220 switch (current_tabmenu)
5221 {
5222 case TABLINE_MENU_CLOSE:
5223 if (current_tab == 0)
5224 do_cmdline_cmd((char_u *)"tabclose");
5225 else
5226 {
5227 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5228 current_tab);
5229 do_cmdline_cmd(IObuff);
5230 }
5231 break;
5232
5233 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005234 if (current_tab == 0)
5235 do_cmdline_cmd((char_u *)"$tabnew");
5236 else
5237 {
5238 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5239 current_tab - 1);
5240 do_cmdline_cmd(IObuff);
5241 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005242 break;
5243
5244 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005245 if (current_tab == 0)
5246 do_cmdline_cmd((char_u *)"browse $tabnew");
5247 else
5248 {
5249 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5250 current_tab - 1);
5251 do_cmdline_cmd(IObuff);
5252 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005253 break;
5254 }
5255}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005256#endif
5257
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258/*
5259 * "Q" command.
5260 */
5261 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005262nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263{
5264 /*
5265 * Ignore 'Q' in Visual mode, just give a beep.
5266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005268 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005269 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 do_exmode(FALSE);
5271}
5272
5273/*
5274 * Handle a ":" command.
5275 */
5276 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005277nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278{
5279 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005280 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 if (VIsual_active)
5283 nv_operator(cap);
5284 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 {
5286 if (cap->oap->op_type != OP_NOP)
5287 {
5288 /* Using ":" as a movement is characterwise exclusive. */
5289 cap->oap->motion_type = MCHAR;
5290 cap->oap->inclusive = FALSE;
5291 }
5292 else if (cap->count0)
5293 {
5294 /* translate "count:" into ":.,.+(count - 1)" */
5295 stuffcharReadbuff('.');
5296 if (cap->count0 > 1)
5297 {
5298 stuffReadbuff((char_u *)",.+");
5299 stuffnumReadbuff((long)cap->count0 - 1L);
5300 }
5301 }
5302
5303 /* When typing, don't type below an old message */
5304 if (KeyTyped)
5305 compute_cmdrow();
5306
5307 old_p_im = p_im;
5308
5309 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005310 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5312
5313 /* If 'insertmode' changed, enter or exit Insert mode */
5314 if (p_im != old_p_im)
5315 {
5316 if (p_im)
5317 restart_edit = 'i';
5318 else
5319 restart_edit = 0;
5320 }
5321
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005322 if (cmd_result == FAIL)
5323 /* The Ex command failed, do not execute the operator. */
5324 clearop(cap->oap);
5325 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5327 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005328 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5329 || did_emsg
5330 ))
5331 /* The start of the operator has become invalid by the Ex command.
5332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 clearopbeep(cap->oap);
5334 }
5335}
5336
5337/*
5338 * Handle CTRL-G command.
5339 */
5340 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005341nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 if (VIsual_active) /* toggle Selection/Visual mode */
5344 {
5345 VIsual_select = !VIsual_select;
5346 showmode();
5347 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005348 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 /* print full name if count given or :cd used */
5350 fileinfo((int)cap->count0, FALSE, TRUE);
5351}
5352
5353/*
5354 * Handle CTRL-H <Backspace> command.
5355 */
5356 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005357nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 if (VIsual_active && VIsual_select)
5360 {
5361 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5362 v_visop(cap);
5363 }
5364 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 nv_left(cap);
5366}
5367
5368/*
5369 * CTRL-L: clear screen and redraw.
5370 */
5371 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005372nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373{
5374 if (!checkclearop(cap->oap))
5375 {
5376#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5377 /*
5378 * Right now, the BeBox doesn't seem to have an easy way to detect
5379 * window resizing, so we cheat and make the user detect it
5380 * manually with CTRL-L instead
5381 */
5382 ui_get_shellsize();
5383#endif
5384#ifdef FEAT_SYN_HL
5385 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005386 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02005387# ifdef FEAT_RELTIME
5388 {
5389 win_T *wp;
5390
5391 FOR_ALL_WINDOWS(wp)
5392 wp->w_s->b_syn_slow = FALSE;
5393 }
5394# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395#endif
5396 redraw_later(CLEAR);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005397#if defined(MSWIN) && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar78d21da2019-02-17 15:00:52 +01005398 resize_console_buf();
5399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 }
5401}
5402
5403/*
5404 * CTRL-O: In Select mode: switch to Visual mode for one command.
5405 * Otherwise: Go to older pcmark.
5406 */
5407 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005408nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 if (VIsual_active && VIsual_select)
5411 {
5412 VIsual_select = FALSE;
5413 showmode();
5414 restart_VIsual_select = 2; /* restart Select mode later */
5415 }
5416 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 {
5418 cap->count1 = -cap->count1;
5419 nv_pcmark(cap);
5420 }
5421}
5422
5423/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01005424 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
5425 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 */
5427 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005428nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429{
5430 if (!checkclearopq(cap->oap))
5431 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5432 GETF_SETMARK|GETF_ALT, FALSE);
5433}
5434
5435/*
5436 * "Z" commands.
5437 */
5438 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005439nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440{
5441 if (!checkclearopq(cap->oap))
5442 {
5443 switch (cap->nchar)
5444 {
5445 /* "ZZ": equivalent to ":x". */
5446 case 'Z': do_cmdline_cmd((char_u *)"x");
5447 break;
5448
5449 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5450 case 'Q': do_cmdline_cmd((char_u *)"q!");
5451 break;
5452
5453 default: clearopbeep(cap->oap);
5454 }
5455 }
5456}
5457
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458/*
5459 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5460 */
5461 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005462do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463{
5464 oparg_T oa;
5465 cmdarg_T ca;
5466
5467 clear_oparg(&oa);
5468 vim_memset(&ca, 0, sizeof(ca));
5469 ca.oap = &oa;
5470 ca.cmdchar = c1;
5471 ca.nchar = c2;
5472 nv_ident(&ca);
5473}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474
5475/*
5476 * Handle the commands that use the word under the cursor.
5477 * [g] CTRL-] :ta to current identifier
5478 * [g] 'K' run program for current identifier
5479 * [g] '*' / to current identifier or string
5480 * [g] '#' ? to current identifier or string
5481 * g ']' :tselect for current identifier
5482 */
5483 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005484nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485{
5486 char_u *ptr = NULL;
5487 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005488 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005489 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 char_u *p;
5491 char_u *kp; /* value of 'keywordprg' */
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005492 int kp_help; /* 'keywordprg' is ":he" */
5493 int kp_ex; /* 'keywordprg' starts with ":" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 int n = 0; /* init for GCC */
5495 int cmdchar;
5496 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005497 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 char_u *aux_ptr;
5499 int isman;
5500 int isman_s;
5501
5502 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5503 {
5504 cmdchar = cap->nchar;
5505 g_cmd = TRUE;
5506 }
5507 else
5508 {
5509 cmdchar = cap->cmdchar;
5510 g_cmd = FALSE;
5511 }
5512
5513 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5514 cmdchar = '#';
5515
5516 /*
5517 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5518 */
5519 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5520 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5522 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 if (checkclearopq(cap->oap))
5524 return;
5525 }
5526
5527 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5528 (cmdchar == '*' || cmdchar == '#')
5529 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5530 {
5531 clearop(cap->oap);
5532 return;
5533 }
5534
5535 /* Allocate buffer to put the command in. Inserting backslashes can
5536 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5537 * and some numbers. */
5538 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5539 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5540 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02005541 if (kp_help && *skipwhite(ptr) == NUL)
5542 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005543 emsg(_(e_noident)); /* found white space only */
Bram Moolenaara4f99f52017-08-26 16:25:32 +02005544 return;
5545 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005546 kp_ex = (*kp == ':');
5547 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
5548 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 if (buf == NULL)
5550 return;
5551 buf[0] = NUL;
5552
5553 switch (cmdchar)
5554 {
5555 case '*':
5556 case '#':
5557 /*
5558 * Put cursor at start of word, makes search skip the word
5559 * under the cursor.
5560 * Call setpcmark() first, so "*``" puts the cursor back where
5561 * it was.
5562 */
5563 setpcmark();
5564 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5565
5566 if (!g_cmd && vim_iswordp(ptr))
5567 STRCPY(buf, "\\<");
5568 no_smartcase = TRUE; /* don't use 'smartcase' now */
5569 break;
5570
5571 case 'K':
5572 if (kp_help)
5573 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005574 else if (kp_ex)
5575 {
5576 if (cap->count0 != 0)
5577 vim_snprintf((char *)buf, buflen, "%s %ld",
5578 kp, cap->count0);
5579 else
5580 STRCPY(buf, kp);
5581 STRCAT(buf, " ");
5582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 else
5584 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005585 /* An external command will probably use an argument starting
5586 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005587 while (*ptr == '-' && n > 0)
5588 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005589 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005590 --n;
5591 }
5592 if (n == 0)
5593 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005594 emsg(_(e_noident)); /* found dashes only */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005595 vim_free(buf);
5596 return;
5597 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005598
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 /* When a count is given, turn it into a range. Is this
5600 * really what we want? */
5601 isman = (STRCMP(kp, "man") == 0);
5602 isman_s = (STRCMP(kp, "man -s") == 0);
5603 if (cap->count0 != 0 && !(isman || isman_s))
5604 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5605
5606 STRCAT(buf, "! ");
5607 if (cap->count0 == 0 && isman_s)
5608 STRCAT(buf, "man");
5609 else
5610 STRCAT(buf, kp);
5611 STRCAT(buf, " ");
5612 if (cap->count0 != 0 && (isman || isman_s))
5613 {
5614 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5615 STRCAT(buf, " ");
5616 }
5617 }
5618 break;
5619
5620 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005621 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622#ifdef FEAT_CSCOPE
5623 if (p_cst)
5624 STRCPY(buf, "cstag ");
5625 else
5626#endif
5627 STRCPY(buf, "ts ");
5628 break;
5629
5630 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005631 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 if (curbuf->b_help)
5633 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005635 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005636 if (g_cmd)
5637 STRCPY(buf, "tj ");
5638 else
5639 sprintf((char *)buf, "%ldta ", cap->count0);
5640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 }
5642
5643 /*
5644 * Now grab the chars in the identifier
5645 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005646 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005648 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01005649 if (kp_ex)
5650 /* Escape the argument properly for an Ex command */
5651 p = vim_strsave_fnameescape(ptr, FALSE);
5652 else
5653 /* Escape the argument properly for a shell command */
5654 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005655 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005656 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005658 vim_free(buf);
5659 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005661 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5662 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005663 {
5664 vim_free(buf);
5665 vim_free(p);
5666 return;
5667 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005668 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005669 STRCAT(buf, p);
5670 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005672 else
5673 {
5674 if (cmdchar == '*')
5675 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5676 else if (cmdchar == '#')
5677 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005678 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005679 {
5680 if (curbuf->b_help)
5681 /* ":help" handles unescaped argument */
5682 aux_ptr = (char_u *)"";
5683 else
5684 aux_ptr = (char_u *)"\\|\"\n[";
5685 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005686 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005687 aux_ptr = (char_u *)"\\|\"\n*?[";
5688
5689 p = buf + STRLEN(buf);
5690 while (n-- > 0)
5691 {
5692 /* put a backslash before \ and some others */
5693 if (vim_strchr(aux_ptr, *ptr) != NULL)
5694 *p++ = '\\';
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005695 /* When current byte is a part of multibyte character, copy all
5696 * bytes of that character. */
5697 if (has_mbyte)
5698 {
5699 int i;
5700 int len = (*mb_ptr2len)(ptr) - 1;
5701
5702 for (i = 0; i < len && n >= 1; ++i, --n)
5703 *p++ = *ptr++;
5704 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005705 *p++ = *ptr++;
5706 }
5707 *p = NUL;
5708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709
5710 /*
5711 * Execute the command.
5712 */
5713 if (cmdchar == '*' || cmdchar == '#')
5714 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005715 if (!g_cmd && (has_mbyte
5716 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
5717 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 STRCAT(buf, "\\>");
5719#ifdef FEAT_CMDHIST
5720 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005721 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5723#endif
Bram Moolenaar46539112015-02-17 15:43:57 +01005724 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 }
5726 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005727 {
5728 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005730 g_tag_at_cursor = FALSE;
5731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732
5733 vim_free(buf);
5734}
5735
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736/*
5737 * Get visually selected text, within one line only.
5738 * Returns FAIL if more than one line selected.
5739 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005740 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005741get_visual_text(
5742 cmdarg_T *cap,
5743 char_u **pp, /* return: start of selected text */
5744 int *lenp) /* return: length of selected text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745{
5746 if (VIsual_mode != 'V')
5747 unadjust_for_sel();
5748 if (VIsual.lnum != curwin->w_cursor.lnum)
5749 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005750 if (cap != NULL)
5751 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 return FAIL;
5753 }
5754 if (VIsual_mode == 'V')
5755 {
5756 *pp = ml_get_curline();
5757 *lenp = (int)STRLEN(*pp);
5758 }
5759 else
5760 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005761 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 {
5763 *pp = ml_get_pos(&curwin->w_cursor);
5764 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5765 }
5766 else
5767 {
5768 *pp = ml_get_pos(&VIsual);
5769 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 if (has_mbyte)
5772 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005773 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 }
5775 reset_VIsual_and_resel();
5776 return OK;
5777}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778
5779/*
5780 * CTRL-T: backwards in tag stack
5781 */
5782 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005783nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784{
5785 if (!checkclearopq(cap->oap))
5786 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5787}
5788
5789/*
5790 * Handle scrolling command 'H', 'L' and 'M'.
5791 */
5792 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005793nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794{
5795 int used = 0;
5796 long n;
5797#ifdef FEAT_FOLDING
5798 linenr_T lnum;
5799#endif
5800 int half;
5801
5802 cap->oap->motion_type = MLINE;
5803 setpcmark();
5804
5805 if (cap->cmdchar == 'L')
5806 {
5807 validate_botline(); /* make sure curwin->w_botline is valid */
5808 curwin->w_cursor.lnum = curwin->w_botline - 1;
5809 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5810 curwin->w_cursor.lnum = 1;
5811 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005812 {
5813#ifdef FEAT_FOLDING
5814 if (hasAnyFolding(curwin))
5815 {
5816 /* Count a fold for one screen line. */
5817 for (n = cap->count1 - 1; n > 0
5818 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5819 {
5820 (void)hasFolding(curwin->w_cursor.lnum,
5821 &curwin->w_cursor.lnum, NULL);
5822 --curwin->w_cursor.lnum;
5823 }
5824 }
5825 else
5826#endif
5827 curwin->w_cursor.lnum -= cap->count1 - 1;
5828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 }
5830 else
5831 {
5832 if (cap->cmdchar == 'M')
5833 {
5834#ifdef FEAT_DIFF
5835 /* Don't count filler lines above the window. */
5836 used -= diff_check_fill(curwin, curwin->w_topline)
5837 - curwin->w_topfill;
5838#endif
5839 validate_botline(); /* make sure w_empty_rows is valid */
5840 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5841 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5842 {
5843#ifdef FEAT_DIFF
5844 /* Count half he number of filler lines to be "below this
5845 * line" and half to be "above the next line". */
5846 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5847 + n) / 2 >= half)
5848 {
5849 --n;
5850 break;
5851 }
5852#endif
5853 used += plines(curwin->w_topline + n);
5854 if (used >= half)
5855 break;
5856#ifdef FEAT_FOLDING
5857 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5858 n = lnum - curwin->w_topline;
5859#endif
5860 }
5861 if (n > 0 && used > curwin->w_height)
5862 --n;
5863 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005864 else /* (cap->cmdchar == 'H') */
5865 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005867#ifdef FEAT_FOLDING
5868 if (hasAnyFolding(curwin))
5869 {
5870 /* Count a fold for one screen line. */
5871 lnum = curwin->w_topline;
5872 while (n-- > 0 && lnum < curwin->w_botline - 1)
5873 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02005874 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005875 ++lnum;
5876 }
5877 n = lnum - curwin->w_topline;
5878 }
5879#endif
5880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 curwin->w_cursor.lnum = curwin->w_topline + n;
5882 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5883 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5884 }
5885
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02005886 /* Correct for 'so', except when an operator is pending. */
5887 if (cap->oap->op_type == OP_NOP)
5888 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 beginline(BL_SOL | BL_FIX);
5890}
5891
5892/*
5893 * Cursor right commands.
5894 */
5895 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005896nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897{
5898 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005899 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005901 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5902 {
5903 /* <C-Right> and <S-Right> move a word or WORD right */
5904 if (mod_mask & MOD_MASK_CTRL)
5905 cap->arg = TRUE;
5906 nv_wordcmd(cap);
5907 return;
5908 }
5909
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 cap->oap->motion_type = MCHAR;
5911 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005912 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005915 * In virtual edit mode, there's no such thing as "past_line", as lines
5916 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 */
5918 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005919 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920
5921 for (n = cap->count1; n > 0; --n)
5922 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005923 if ((!past_line && oneright() == FAIL)
5924 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005925 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 {
5927 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005928 * <Space> wraps to next line if 'whichwrap' has 's'.
5929 * 'l' wraps to next line if 'whichwrap' has 'l'.
5930 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 */
5932 if ( ((cap->cmdchar == ' '
5933 && vim_strchr(p_ww, 's') != NULL)
5934 || (cap->cmdchar == 'l'
5935 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005936 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 && vim_strchr(p_ww, '>') != NULL))
5938 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5939 {
5940 /* When deleting we also count the NL as a character.
5941 * Set cap->oap->inclusive when last char in the line is
5942 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005943 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005945 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 cap->oap->inclusive = TRUE;
5947 else
5948 {
5949 ++curwin->w_cursor.lnum;
5950 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 curwin->w_set_curswant = TRUE;
5953 cap->oap->inclusive = FALSE;
5954 }
5955 continue;
5956 }
5957 if (cap->oap->op_type == OP_NOP)
5958 {
5959 /* Only beep and flush if not moved at all */
5960 if (n == cap->count1)
5961 beep_flush();
5962 }
5963 else
5964 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005965 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 cap->oap->inclusive = TRUE;
5967 }
5968 break;
5969 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005970 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 {
5972 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 if (virtual_active())
5974 oneright();
5975 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 if (has_mbyte)
5978 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005979 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 ++curwin->w_cursor.col;
5982 }
5983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 }
5985#ifdef FEAT_FOLDING
5986 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5987 && cap->oap->op_type == OP_NOP)
5988 foldOpenCursor();
5989#endif
5990}
5991
5992/*
5993 * Cursor left commands.
5994 *
5995 * Returns TRUE when operator end should not be adjusted.
5996 */
5997 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005998nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999{
6000 long n;
6001
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006002 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6003 {
6004 /* <C-Left> and <S-Left> move a word or WORD left */
6005 if (mod_mask & MOD_MASK_CTRL)
6006 cap->arg = 1;
6007 nv_bck_word(cap);
6008 return;
6009 }
6010
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 cap->oap->motion_type = MCHAR;
6012 cap->oap->inclusive = FALSE;
6013 for (n = cap->count1; n > 0; --n)
6014 {
6015 if (oneleft() == FAIL)
6016 {
6017 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6018 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6019 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6020 */
6021 if ( (((cap->cmdchar == K_BS
6022 || cap->cmdchar == Ctrl_H)
6023 && vim_strchr(p_ww, 'b') != NULL)
6024 || (cap->cmdchar == 'h'
6025 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006026 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 && vim_strchr(p_ww, '<') != NULL))
6028 && curwin->w_cursor.lnum > 1)
6029 {
6030 --(curwin->w_cursor.lnum);
6031 coladvance((colnr_T)MAXCOL);
6032 curwin->w_set_curswant = TRUE;
6033
6034 /* When the NL before the first char has to be deleted we
6035 * put the cursor on the NUL after the previous line.
6036 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006037 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 if ( (cap->oap->op_type == OP_DELETE
6039 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006040 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006042 char_u *cp = ml_get_cursor();
6043
6044 if (*cp != NUL)
6045 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006046 if (has_mbyte)
6047 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6048 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006049 ++curwin->w_cursor.col;
6050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 cap->retval |= CA_NO_ADJ_OP_END;
6052 }
6053 continue;
6054 }
6055 /* Only beep and flush if not moved at all */
6056 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6057 beep_flush();
6058 break;
6059 }
6060 }
6061#ifdef FEAT_FOLDING
6062 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6063 && cap->oap->op_type == OP_NOP)
6064 foldOpenCursor();
6065#endif
6066}
6067
6068/*
6069 * Cursor up commands.
6070 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6071 */
6072 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006073nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006075 if (mod_mask & MOD_MASK_SHIFT)
6076 {
6077 /* <S-Up> is page up */
6078 cap->arg = BACKWARD;
6079 nv_page(cap);
6080 }
6081 else
6082 {
6083 cap->oap->motion_type = MLINE;
6084 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6085 clearopbeep(cap->oap);
6086 else if (cap->arg)
6087 beginline(BL_WHITE | BL_FIX);
6088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089}
6090
6091/*
6092 * Cursor down commands.
6093 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6094 */
6095 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02006096nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006098 if (mod_mask & MOD_MASK_SHIFT)
6099 {
6100 /* <S-Down> is page down */
6101 cap->arg = FORWARD;
6102 nv_page(cap);
6103 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006104#if defined(FEAT_QUICKFIX)
Bram Moolenaar0a08c632018-07-25 22:36:52 +02006105 /* Quickfix window only: view the result under the cursor. */
6106 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
6107 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02006109 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 {
6111#ifdef FEAT_CMDWIN
6112 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006113 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 cmdwin_result = CAR;
6115 else
6116#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02006117#ifdef FEAT_JOB_CHANNEL
6118 /* In a prompt buffer a <CR> in the last line invokes the callback. */
6119 if (bt_prompt(curbuf) && cap->cmdchar == CAR
6120 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
6121 {
6122 invoke_prompt_callback();
6123 if (restart_edit == 0)
6124 restart_edit = 'a';
6125 }
6126 else
6127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 {
6129 cap->oap->motion_type = MLINE;
6130 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6131 clearopbeep(cap->oap);
6132 else if (cap->arg)
6133 beginline(BL_WHITE | BL_FIX);
6134 }
6135 }
6136}
6137
6138#ifdef FEAT_SEARCHPATH
6139/*
6140 * Grab the file name under the cursor and edit it.
6141 */
6142 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006143nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144{
6145 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006146 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006148 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 {
6150 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006151 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 return;
6153 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006154 if (curbuf_locked())
6155 {
6156 clearop(cap->oap);
6157 return;
6158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006160 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161
6162 if (ptr != NULL)
6163 {
6164 /* do autowrite if necessary */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006165 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02006166 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006168 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006169 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006170 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006171 {
6172 curwin->w_cursor.lnum = lnum;
6173 check_cursor_lnum();
6174 beginline(BL_SOL | BL_FIX);
6175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 vim_free(ptr);
6177 }
6178 else
6179 clearop(cap->oap);
6180}
6181#endif
6182
6183/*
6184 * <End> command: to end of current line or last line.
6185 */
6186 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006187nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006189 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006191 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 nv_goto(cap);
6193 cap->count1 = 1; /* to end of current line */
6194 }
6195 nv_dollar(cap);
6196}
6197
6198/*
6199 * Handle the "$" command.
6200 */
6201 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006202nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203{
6204 cap->oap->motion_type = MCHAR;
6205 cap->oap->inclusive = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 /* In virtual mode when off the edge of a line and an operator
6207 * is pending (whew!) keep the cursor where it is.
6208 * Otherwise, send it to the end of the line. */
6209 if (!virtual_active() || gchar_cursor() != NUL
6210 || cap->oap->op_type == OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6212 if (cursor_down((long)(cap->count1 - 1),
6213 cap->oap->op_type == OP_NOP) == FAIL)
6214 clearopbeep(cap->oap);
6215#ifdef FEAT_FOLDING
6216 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6217 foldOpenCursor();
6218#endif
6219}
6220
6221/*
6222 * Implementation of '?' and '/' commands.
6223 * If cap->arg is TRUE don't set PC mark.
6224 */
6225 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006226nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227{
6228 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02006229 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230
6231 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6232 {
6233 /* Translate "g??" to "g?g?" */
6234 cap->cmdchar = 'g';
6235 cap->nchar = '?';
6236 nv_operator(cap);
6237 return;
6238 }
6239
Bram Moolenaardda933d2016-09-03 21:04:58 +02006240 /* When using 'incsearch' the cursor may be moved to set a different search
6241 * start position. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6243
6244 if (cap->searchbuf == NULL)
6245 {
6246 clearop(oap);
6247 return;
6248 }
6249
Bram Moolenaar46539112015-02-17 15:43:57 +01006250 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006251 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaardda933d2016-09-03 21:04:58 +02006252 ? 0 : SEARCH_MARK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253}
6254
6255/*
6256 * Handle "N" and "n" commands.
6257 * cap->arg is SEARCH_REV for "N", 0 for "n".
6258 */
6259 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006260nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261{
Bram Moolenaar46539112015-02-17 15:43:57 +01006262 pos_T old = curwin->w_cursor;
6263 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6264
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006265 if (i == 1 && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01006266 {
6267 /* Avoid getting stuck on the current cursor position, which can
6268 * happen when an offset is given and the cursor is on the last char
6269 * in the buffer: Repeat with count + 1. */
6270 cap->count1 += 1;
6271 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6272 cap->count1 -= 1;
6273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274}
6275
6276/*
6277 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6278 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01006279 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 */
Bram Moolenaar46539112015-02-17 15:43:57 +01006281 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006282normal_search(
6283 cmdarg_T *cap,
6284 int dir,
6285 char_u *pat,
6286 int opt) /* extra flags for do_search() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287{
6288 int i;
6289
6290 cap->oap->motion_type = MCHAR;
6291 cap->oap->inclusive = FALSE;
6292 cap->oap->use_reg_one = TRUE;
6293 curwin->w_set_curswant = TRUE;
6294
6295 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006296 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 if (i == 0)
6298 clearop(cap->oap);
6299 else
6300 {
6301 if (i == 2)
6302 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304#ifdef FEAT_FOLDING
6305 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6306 foldOpenCursor();
6307#endif
6308 }
6309
6310 /* "/$" will put the cursor after the end of the line, may need to
6311 * correct that here */
6312 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01006313 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314}
6315
6316/*
6317 * Character search commands.
6318 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6319 * ',' and FALSE for ';'.
6320 * cap->nchar is NUL for ',' and ';' (repeat the search)
6321 */
6322 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006323nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324{
6325 int t_cmd;
6326
6327 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6328 t_cmd = TRUE;
6329 else
6330 t_cmd = FALSE;
6331
6332 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6334 clearopbeep(cap->oap);
6335 else
6336 {
6337 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 /* Include a Tab for "tx" and for "dfx". */
6339 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6340 && (t_cmd || cap->oap->op_type != OP_NOP))
6341 {
6342 colnr_T scol, ecol;
6343
6344 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6345 curwin->w_cursor.coladd = ecol - scol;
6346 }
6347 else
6348 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350#ifdef FEAT_FOLDING
6351 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6352 foldOpenCursor();
6353#endif
6354 }
6355}
6356
6357/*
6358 * "[" and "]" commands.
6359 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6360 */
6361 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006362nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006364 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 pos_T prev_pos;
6366 pos_T *pos = NULL; /* init for GCC */
6367 pos_T old_pos; /* cursor position before command */
6368 int flag;
6369 long n;
6370 int findc;
6371 int c;
6372
6373 cap->oap->motion_type = MCHAR;
6374 cap->oap->inclusive = FALSE;
6375 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01006376 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377
6378#ifdef FEAT_SEARCHPATH
6379 /*
6380 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6381 */
6382 if (cap->nchar == 'f')
6383 nv_gotofile(cap);
6384 else
6385#endif
6386
6387#ifdef FEAT_FIND_ID
6388 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006389 * Find the occurrence(s) of the identifier or define under cursor
6390 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 *
6392 * search list jump
6393 * fwd bwd fwd bwd fwd bwd
6394 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6395 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6396 */
6397 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006398# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01006400# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01006402# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 cap->nchar) != NULL)
6404 {
6405 char_u *ptr;
6406 int len;
6407
6408 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6409 clearop(cap->oap);
6410 else
6411 {
6412 find_pattern_in_path(ptr, 0, len, TRUE,
6413 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6414 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6415 cap->count1,
6416 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6417 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6418 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6419 (linenr_T)MAXLNUM);
6420 curwin->w_set_curswant = TRUE;
6421 }
6422 }
6423 else
6424#endif
6425
6426 /*
6427 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6428 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6429 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6430 * "[m" or "]m" search for prev/next start of (Java) method.
6431 * "[M" or "]M" search for prev/next end of (Java) method.
6432 */
6433 if ( (cap->cmdchar == '['
6434 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6435 || (cap->cmdchar == ']'
6436 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6437 {
6438 if (cap->nchar == '*')
6439 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 prev_pos.lnum = 0;
6441 if (cap->nchar == 'm' || cap->nchar == 'M')
6442 {
6443 if (cap->cmdchar == '[')
6444 findc = '{';
6445 else
6446 findc = '}';
6447 n = 9999;
6448 }
6449 else
6450 {
6451 findc = cap->nchar;
6452 n = cap->count1;
6453 }
6454 for ( ; n > 0; --n)
6455 {
6456 if ((pos = findmatchlimit(cap->oap, findc,
6457 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6458 {
6459 if (new_pos.lnum == 0) /* nothing found */
6460 {
6461 if (cap->nchar != 'm' && cap->nchar != 'M')
6462 clearopbeep(cap->oap);
6463 }
6464 else
6465 pos = &new_pos; /* use last one found */
6466 break;
6467 }
6468 prev_pos = new_pos;
6469 curwin->w_cursor = *pos;
6470 new_pos = *pos;
6471 }
6472 curwin->w_cursor = old_pos;
6473
6474 /*
6475 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6476 * brought us to the match for "[m" and "]M" when inside a method.
6477 * Try finding the '{' or '}' we want to be at.
6478 * Also repeat for the given count.
6479 */
6480 if (cap->nchar == 'm' || cap->nchar == 'M')
6481 {
6482 /* norm is TRUE for "]M" and "[m" */
6483 int norm = ((findc == '{') == (cap->nchar == 'm'));
6484
6485 n = cap->count1;
6486 /* found a match: we were inside a method */
6487 if (prev_pos.lnum != 0)
6488 {
6489 pos = &prev_pos;
6490 curwin->w_cursor = prev_pos;
6491 if (norm)
6492 --n;
6493 }
6494 else
6495 pos = NULL;
6496 while (n > 0)
6497 {
6498 for (;;)
6499 {
6500 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6501 {
6502 /* if not found anything, that's an error */
6503 if (pos == NULL)
6504 clearopbeep(cap->oap);
6505 n = 0;
6506 break;
6507 }
6508 c = gchar_cursor();
6509 if (c == '{' || c == '}')
6510 {
6511 /* Must have found end/start of class: use it.
6512 * Or found the place to be at. */
6513 if ((c == findc && norm) || (n == 1 && !norm))
6514 {
6515 new_pos = curwin->w_cursor;
6516 pos = &new_pos;
6517 n = 0;
6518 }
6519 /* if no match found at all, we started outside of the
6520 * class and we're inside now. Just go on. */
6521 else if (new_pos.lnum == 0)
6522 {
6523 new_pos = curwin->w_cursor;
6524 pos = &new_pos;
6525 }
6526 /* found start/end of other method: go to match */
6527 else if ((pos = findmatchlimit(cap->oap, findc,
6528 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6529 0)) == NULL)
6530 n = 0;
6531 else
6532 curwin->w_cursor = *pos;
6533 break;
6534 }
6535 }
6536 --n;
6537 }
6538 curwin->w_cursor = old_pos;
6539 if (pos == NULL && new_pos.lnum != 0)
6540 clearopbeep(cap->oap);
6541 }
6542 if (pos != NULL)
6543 {
6544 setpcmark();
6545 curwin->w_cursor = *pos;
6546 curwin->w_set_curswant = TRUE;
6547#ifdef FEAT_FOLDING
6548 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6549 && cap->oap->op_type == OP_NOP)
6550 foldOpenCursor();
6551#endif
6552 }
6553 }
6554
6555 /*
6556 * "[[", "[]", "]]" and "][": move to start or end of function
6557 */
6558 else if (cap->nchar == '[' || cap->nchar == ']')
6559 {
6560 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6561 flag = '{';
6562 else
6563 flag = '}'; /* "][" or "[]" */
6564
6565 curwin->w_set_curswant = TRUE;
6566 /*
6567 * Imitate strange Vi behaviour: When using "]]" with an operator
6568 * we also stop at '}'.
6569 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006570 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 (cap->oap->op_type != OP_NOP
6572 && cap->arg == FORWARD && flag == '{')))
6573 clearopbeep(cap->oap);
6574 else
6575 {
6576 if (cap->oap->op_type == OP_NOP)
6577 beginline(BL_WHITE | BL_FIX);
6578#ifdef FEAT_FOLDING
6579 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6580 foldOpenCursor();
6581#endif
6582 }
6583 }
6584
6585 /*
6586 * "[p", "[P", "]P" and "]p": put with indent adjustment
6587 */
6588 else if (cap->nchar == 'p' || cap->nchar == 'P')
6589 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006590 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006592 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6593 ? FORWARD : BACKWARD;
6594 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006595 int was_visual = VIsual_active;
6596 int line_count = curbuf->b_ml.ml_line_count;
6597 pos_T start, end;
6598
6599 if (VIsual_active)
6600 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006601 start = LTOREQ_POS(VIsual, curwin->w_cursor)
Bram Moolenaar27bed202014-03-12 17:42:04 +01006602 ? VIsual : curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006603 end = EQUAL_POS(start,VIsual) ? curwin->w_cursor : VIsual;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006604 curwin->w_cursor = (dir == BACKWARD ? start : end);
6605 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006606# ifdef FEAT_CLIPBOARD
6607 adjust_clip_reg(&regname);
6608# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006610
6611 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006612 if (was_visual)
6613 {
6614 VIsual = start;
6615 curwin->w_cursor = end;
6616 if (dir == BACKWARD)
6617 {
6618 /* adjust lines */
6619 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6620 curwin->w_cursor.lnum +=
6621 curbuf->b_ml.ml_line_count - line_count;
6622 }
6623
6624 VIsual_active = TRUE;
6625 if (VIsual_mode == 'V')
6626 {
6627 /* delete visually selected lines */
6628 cap->cmdchar = 'd';
6629 cap->nchar = NUL;
6630 cap->oap->regname = regname;
6631 nv_operator(cap);
6632 do_pending_operator(cap, 0, FALSE);
6633 }
6634 if (VIsual_active)
6635 {
6636 end_visual_mode();
6637 redraw_later(SOME_VALID);
6638 }
6639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 }
6641 }
6642
6643 /*
6644 * "['", "[`", "]'" and "]`": jump to next mark
6645 */
6646 else if (cap->nchar == '\'' || cap->nchar == '`')
6647 {
6648 pos = &curwin->w_cursor;
6649 for (n = cap->count1; n > 0; --n)
6650 {
6651 prev_pos = *pos;
6652 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6653 cap->nchar == '\'');
6654 if (pos == NULL)
6655 break;
6656 }
6657 if (pos == NULL)
6658 pos = &prev_pos;
6659 nv_cursormark(cap, cap->nchar == '\'', pos);
6660 }
6661
6662#ifdef FEAT_MOUSE
6663 /*
6664 * [ or ] followed by a middle mouse click: put selected text with
6665 * indent adjustment. Any other button just does as usual.
6666 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006667 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 {
6669 (void)do_mouse(cap->oap, cap->nchar,
6670 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6671 cap->count1, PUT_FIXINDENT);
6672 }
6673#endif /* FEAT_MOUSE */
6674
6675#ifdef FEAT_FOLDING
6676 /*
6677 * "[z" and "]z": move to start or end of open fold.
6678 */
6679 else if (cap->nchar == 'z')
6680 {
6681 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6682 cap->count1) == FAIL)
6683 clearopbeep(cap->oap);
6684 }
6685#endif
6686
6687#ifdef FEAT_DIFF
6688 /*
6689 * "[c" and "]c": move to next or previous diff-change.
6690 */
6691 else if (cap->nchar == 'c')
6692 {
6693 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6694 cap->count1) == FAIL)
6695 clearopbeep(cap->oap);
6696 }
6697#endif
6698
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006699#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006700 /*
6701 * "[s", "[S", "]s" and "]S": move to next spell error.
6702 */
6703 else if (cap->nchar == 's' || cap->nchar == 'S')
6704 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006705 setpcmark();
6706 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006707 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6708 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006709 {
6710 clearopbeep(cap->oap);
6711 break;
6712 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01006713 else
6714 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006715# ifdef FEAT_FOLDING
6716 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6717 foldOpenCursor();
6718# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006719 }
6720#endif
6721
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 /* Not a valid cap->nchar. */
6723 else
6724 clearopbeep(cap->oap);
6725}
6726
6727/*
6728 * Handle Normal mode "%" command.
6729 */
6730 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006731nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732{
6733 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006734#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 linenr_T lnum = curwin->w_cursor.lnum;
6736#endif
6737
6738 cap->oap->inclusive = TRUE;
6739 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6740 {
6741 if (cap->count0 > 100)
6742 clearopbeep(cap->oap);
6743 else
6744 {
6745 cap->oap->motion_type = MLINE;
6746 setpcmark();
6747 /* Round up, so CTRL-G will give same value. Watch out for a
6748 * large line count, the line number must not go negative! */
6749 if (curbuf->b_ml.ml_line_count > 1000000)
6750 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6751 / 100L * cap->count0;
6752 else
6753 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6754 cap->count0 + 99L) / 100L;
6755 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6756 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6757 beginline(BL_SOL | BL_FIX);
6758 }
6759 }
6760 else /* "%" : go to matching paren */
6761 {
6762 cap->oap->motion_type = MCHAR;
6763 cap->oap->use_reg_one = TRUE;
6764 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6765 clearopbeep(cap->oap);
6766 else
6767 {
6768 setpcmark();
6769 curwin->w_cursor = *pos;
6770 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 }
6774 }
6775#ifdef FEAT_FOLDING
6776 if (cap->oap->op_type == OP_NOP
6777 && lnum != curwin->w_cursor.lnum
6778 && (fdo_flags & FDO_PERCENT)
6779 && KeyTyped)
6780 foldOpenCursor();
6781#endif
6782}
6783
6784/*
6785 * Handle "(" and ")" commands.
6786 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6787 */
6788 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006789nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790{
6791 cap->oap->motion_type = MCHAR;
6792 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006793 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6794 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 curwin->w_set_curswant = TRUE;
6796
6797 if (findsent(cap->arg, cap->count1) == FAIL)
6798 clearopbeep(cap->oap);
6799 else
6800 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006801 /* Don't leave the cursor on the NUL past end of line. */
6802 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804#ifdef FEAT_FOLDING
6805 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6806 foldOpenCursor();
6807#endif
6808 }
6809}
6810
6811/*
6812 * "m" command: Mark a position.
6813 */
6814 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006815nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816{
6817 if (!checkclearop(cap->oap))
6818 {
6819 if (setmark(cap->nchar) == FAIL)
6820 clearopbeep(cap->oap);
6821 }
6822}
6823
6824/*
6825 * "{" and "}" commands.
6826 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6827 */
6828 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006829nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830{
6831 cap->oap->motion_type = MCHAR;
6832 cap->oap->inclusive = FALSE;
6833 cap->oap->use_reg_one = TRUE;
6834 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006835 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 clearopbeep(cap->oap);
6837 else
6838 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840#ifdef FEAT_FOLDING
6841 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6842 foldOpenCursor();
6843#endif
6844 }
6845}
6846
6847/*
6848 * "u" command: Undo or make lower case.
6849 */
6850 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006851nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006853 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 {
6855 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6856 cap->cmdchar = 'g';
6857 cap->nchar = 'u';
6858 nv_operator(cap);
6859 }
6860 else
6861 nv_kundo(cap);
6862}
6863
6864/*
6865 * <Undo> command.
6866 */
6867 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006868nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869{
6870 if (!checkclearopq(cap->oap))
6871 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02006872#ifdef FEAT_JOB_CHANNEL
6873 if (bt_prompt(curbuf))
6874 {
6875 clearopbeep(cap->oap);
6876 return;
6877 }
6878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 u_undo((int)cap->count1);
6880 curwin->w_set_curswant = TRUE;
6881 }
6882}
6883
6884/*
6885 * Handle the "r" command.
6886 */
6887 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006888nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889{
6890 char_u *ptr;
6891 int had_ctrl_v;
6892 long n;
6893
6894 if (checkclearop(cap->oap))
6895 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02006896#ifdef FEAT_JOB_CHANNEL
6897 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6898 {
6899 clearopbeep(cap->oap);
6900 return;
6901 }
6902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903
6904 /* get another character */
6905 if (cap->nchar == Ctrl_V)
6906 {
6907 had_ctrl_v = Ctrl_V;
6908 cap->nchar = get_literal();
6909 /* Don't redo a multibyte character with CTRL-V. */
6910 if (cap->nchar > DEL)
6911 had_ctrl_v = NUL;
6912 }
6913 else
6914 had_ctrl_v = NUL;
6915
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006916 /* Abort if the character is a special key. */
6917 if (IS_SPECIAL(cap->nchar))
6918 {
6919 clearopbeep(cap->oap);
6920 return;
6921 }
6922
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 /* Visual mode "r" */
6924 if (VIsual_active)
6925 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006926 if (got_int)
6927 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01006928 if (had_ctrl_v)
6929 {
Bram Moolenaarf12519d2018-02-06 22:52:49 +01006930 /* Use a special (negative) number to make a difference between a
6931 * literal CR or NL and a line break. */
6932 if (cap->nchar == CAR)
6933 cap->nchar = REPLACE_CR_NCHAR;
6934 else if (cap->nchar == NL)
6935 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01006936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 nv_operator(cap);
6938 return;
6939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 /* Break tabs, etc. */
6942 if (virtual_active())
6943 {
6944 if (u_save_cursor() == FAIL)
6945 return;
6946 if (gchar_cursor() == NUL)
6947 {
6948 /* Add extra space and put the cursor on the first one. */
6949 coladvance_force((colnr_T)(getviscol() + cap->count1));
6950 curwin->w_cursor.col -= cap->count1;
6951 }
6952 else if (gchar_cursor() == TAB)
6953 coladvance_force(getviscol());
6954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006956 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006958 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006959 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 {
6961 clearopbeep(cap->oap);
6962 return;
6963 }
6964
6965 /*
6966 * Replacing with a TAB is done by edit() when it is complicated because
6967 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6968 * Other characters are done below to avoid problems with things like
6969 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6970 */
6971 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6972 {
6973 stuffnumReadbuff(cap->count1);
6974 stuffcharReadbuff('R');
6975 stuffcharReadbuff('\t');
6976 stuffcharReadbuff(ESC);
6977 return;
6978 }
6979
6980 /* save line for undo */
6981 if (u_save_cursor() == FAIL)
6982 return;
6983
6984 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6985 {
6986 /*
6987 * Replace character(s) by a single newline.
6988 * Strange vi behaviour: Only one newline is inserted.
6989 * Delete the characters here.
6990 * Insert the newline with an insert command, takes care of
6991 * autoindent. The insert command depends on being on the last
6992 * character of a line or not.
6993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 (void)del_chars(cap->count1, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 stuffcharReadbuff('\r');
6996 stuffcharReadbuff(ESC);
6997
6998 /* Give 'r' to edit(), to get the redo command right. */
6999 invoke_edit(cap, TRUE, 'r', FALSE);
7000 }
7001 else
7002 {
7003 prep_redo(cap->oap->regname, cap->count1,
7004 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7005
7006 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 if (has_mbyte)
7008 {
7009 int old_State = State;
7010
7011 if (cap->ncharC1 != 0)
7012 AppendCharToRedobuff(cap->ncharC1);
7013 if (cap->ncharC2 != 0)
7014 AppendCharToRedobuff(cap->ncharC2);
7015
7016 /* This is slow, but it handles replacing a single-byte with a
7017 * multi-byte and the other way around. Also handles adding
7018 * composing characters for utf-8. */
7019 for (n = cap->count1; n > 0; --n)
7020 {
7021 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007022 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7023 {
7024 int c = ins_copychar(curwin->w_cursor.lnum
7025 + (cap->nchar == Ctrl_Y ? -1 : 1));
7026 if (c != NUL)
7027 ins_char(c);
7028 else
7029 /* will be decremented further down */
7030 ++curwin->w_cursor.col;
7031 }
7032 else
7033 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 State = old_State;
7035 if (cap->ncharC1 != 0)
7036 ins_char(cap->ncharC1);
7037 if (cap->ncharC2 != 0)
7038 ins_char(cap->ncharC2);
7039 }
7040 }
7041 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 {
7043 /*
7044 * Replace the characters within one line.
7045 */
7046 for (n = cap->count1; n > 0; --n)
7047 {
7048 /*
7049 * Get ptr again, because u_save and/or showmatch() will have
7050 * released the line. At the same time we let know that the
7051 * line will be changed.
7052 */
7053 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007054 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7055 {
7056 int c = ins_copychar(curwin->w_cursor.lnum
7057 + (cap->nchar == Ctrl_Y ? -1 : 1));
7058 if (c != NUL)
7059 ptr[curwin->w_cursor.col] = c;
7060 }
7061 else
7062 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 if (p_sm && msg_silent == 0)
7064 showmatch(cap->nchar);
7065 ++curwin->w_cursor.col;
7066 }
7067#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007068 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007070 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071
Bram Moolenaar009b2592004-10-24 19:18:58 +00007072 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007073 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007075 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 }
7077#endif
7078
7079 /* mark the buffer as changed and prepare for displaying */
7080 changed_bytes(curwin->w_cursor.lnum,
7081 (colnr_T)(curwin->w_cursor.col - cap->count1));
7082 }
7083 --curwin->w_cursor.col; /* cursor on the last replaced char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 /* if the character on the left of the current cursor is a multi-byte
7085 * character, move two characters left */
7086 if (has_mbyte)
7087 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 curbuf->b_op_end = curwin->w_cursor;
7089 curwin->w_set_curswant = TRUE;
7090 set_last_insert(cap->nchar);
7091 }
7092}
7093
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094/*
7095 * 'o': Exchange start and end of Visual area.
7096 * 'O': same, but in block mode exchange left and right corners.
7097 */
7098 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007099v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100{
7101 pos_T old_cursor;
7102 colnr_T left, right;
7103
7104 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7105 {
7106 old_cursor = curwin->w_cursor;
7107 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7108 curwin->w_cursor.lnum = VIsual.lnum;
7109 coladvance(left);
7110 VIsual = curwin->w_cursor;
7111
7112 curwin->w_cursor.lnum = old_cursor.lnum;
7113 curwin->w_curswant = right;
7114 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7115 * right one column */
7116 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7117 ++curwin->w_curswant;
7118 coladvance(curwin->w_curswant);
7119 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007121 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 {
7123 curwin->w_cursor.lnum = VIsual.lnum;
7124 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7125 ++right;
7126 coladvance(right);
7127 VIsual = curwin->w_cursor;
7128
7129 curwin->w_cursor.lnum = old_cursor.lnum;
7130 coladvance(left);
7131 curwin->w_curswant = left;
7132 }
7133 }
7134 else
7135 {
7136 old_cursor = curwin->w_cursor;
7137 curwin->w_cursor = VIsual;
7138 VIsual = old_cursor;
7139 curwin->w_set_curswant = TRUE;
7140 }
7141}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142
7143/*
7144 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7145 */
7146 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007147nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 if (VIsual_active) /* "R" is replace lines */
7150 {
7151 cap->cmdchar = 'c';
7152 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007153 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 VIsual_mode = 'V';
7155 nv_operator(cap);
7156 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007157 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 {
7159 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007160 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 else
7162 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 if (virtual_active())
7164 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7166 }
7167 }
7168}
7169
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170/*
7171 * "gr".
7172 */
7173 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007174nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 if (VIsual_active)
7177 {
7178 cap->cmdchar = 'r';
7179 cap->nchar = cap->extra_char;
7180 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7181 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007182 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 {
7184 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007185 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 else
7187 {
7188 if (cap->extra_char == Ctrl_V) /* get another character */
7189 cap->extra_char = get_literal();
7190 stuffcharReadbuff(cap->extra_char);
7191 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 if (virtual_active())
7193 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 invoke_edit(cap, TRUE, 'v', FALSE);
7195 }
7196 }
7197}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198
7199/*
7200 * Swap case for "~" command, when it does not work like an operator.
7201 */
7202 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007203n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204{
7205 long n;
7206 pos_T startpos;
7207 int did_change = 0;
7208#ifdef FEAT_NETBEANS_INTG
7209 pos_T pos;
7210 char_u *ptr;
7211 int count;
7212#endif
7213
7214 if (checkclearopq(cap->oap))
7215 return;
7216
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007217 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 {
7219 clearopbeep(cap->oap);
7220 return;
7221 }
7222
7223 prep_redo_cmd(cap);
7224
7225 if (u_save_cursor() == FAIL)
7226 return;
7227
7228 startpos = curwin->w_cursor;
7229#ifdef FEAT_NETBEANS_INTG
7230 pos = startpos;
7231#endif
7232 for (n = cap->count1; n > 0; --n)
7233 {
7234 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7235 inc_cursor();
7236 if (gchar_cursor() == NUL)
7237 {
7238 if (vim_strchr(p_ww, '~') != NULL
7239 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7240 {
7241#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007242 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 {
7244 if (did_change)
7245 {
7246 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007247 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007248 netbeans_removed(curbuf, pos.lnum, pos.col,
7249 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007251 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 }
7253 pos.col = 0;
7254 pos.lnum++;
7255 }
7256#endif
7257 ++curwin->w_cursor.lnum;
7258 curwin->w_cursor.col = 0;
7259 if (n > 1)
7260 {
7261 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7262 break;
7263 u_clearline();
7264 }
7265 }
7266 else
7267 break;
7268 }
7269 }
7270#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007271 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 {
7273 ptr = ml_get(pos.lnum);
7274 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007275 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7276 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 }
7278#endif
7279
7280
7281 check_cursor();
7282 curwin->w_set_curswant = TRUE;
7283 if (did_change)
7284 {
7285 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7286 0L);
7287 curbuf->b_op_start = startpos;
7288 curbuf->b_op_end = curwin->w_cursor;
7289 if (curbuf->b_op_end.col > 0)
7290 --curbuf->b_op_end.col;
7291 }
7292}
7293
7294/*
7295 * Move cursor to mark.
7296 */
7297 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007298nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299{
7300 if (check_mark(pos) == FAIL)
7301 clearop(cap->oap);
7302 else
7303 {
7304 if (cap->cmdchar == '\''
7305 || cap->cmdchar == '`'
7306 || cap->cmdchar == '['
7307 || cap->cmdchar == ']')
7308 setpcmark();
7309 curwin->w_cursor = *pos;
7310 if (flag)
7311 beginline(BL_WHITE | BL_FIX);
7312 else
7313 check_cursor();
7314 }
7315 cap->oap->motion_type = flag ? MLINE : MCHAR;
7316 if (cap->cmdchar == '`')
7317 cap->oap->use_reg_one = TRUE;
7318 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7319 curwin->w_set_curswant = TRUE;
7320}
7321
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322/*
7323 * Handle commands that are operators in Visual mode.
7324 */
7325 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007326v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327{
7328 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7329
7330 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007331 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 if (isupper(cap->cmdchar))
7333 {
7334 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007335 {
7336 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7340 curwin->w_curswant = MAXCOL;
7341 }
7342 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7343 nv_operator(cap);
7344}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345
7346/*
7347 * "s" and "S" commands.
7348 */
7349 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007350nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351{
Bram Moolenaard96ff162018-02-18 22:13:29 +01007352#ifdef FEAT_TERMINAL
7353 /* When showing output of term_dumpdiff() swap the top and botom. */
7354 if (term_swap_diff() == OK)
7355 return;
7356#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02007357#ifdef FEAT_JOB_CHANNEL
7358 if (bt_prompt(curbuf) && !prompt_curpos_editable())
7359 {
7360 clearopbeep(cap->oap);
7361 return;
7362 }
7363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7365 {
7366 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007367 {
7368 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 cap->cmdchar = 'c';
7372 nv_operator(cap);
7373 }
7374 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 nv_optrans(cap);
7376}
7377
7378/*
7379 * Abbreviated commands.
7380 */
7381 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007382nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383{
7384 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7385 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7386
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 /* in Visual mode these commands are operators */
7388 if (VIsual_active)
7389 v_visop(cap);
7390 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 nv_optrans(cap);
7392}
7393
7394/*
7395 * Translate a command into another command.
7396 */
7397 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007398nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399{
7400 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7401 (char_u *)"d$", (char_u *)"c$",
7402 (char_u *)"cl", (char_u *)"cc",
7403 (char_u *)"yy", (char_u *)":s\r"};
7404 static char_u *str = (char_u *)"xXDCsSY&";
7405
7406 if (!checkclearopq(cap->oap))
7407 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007408 /* In Vi "2D" doesn't delete the next line. Can't translate it
7409 * either, because "2." should also not use the count. */
7410 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7411 {
7412 cap->oap->start = curwin->w_cursor;
7413 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007414#ifdef FEAT_EVAL
7415 set_op_var(OP_DELETE);
7416#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007417 cap->count1 = 1;
7418 nv_dollar(cap);
7419 finish_op = TRUE;
7420 ResetRedobuff();
7421 AppendCharToRedobuff('D');
7422 }
7423 else
7424 {
7425 if (cap->count0)
7426 stuffnumReadbuff(cap->count0);
7427 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 }
7430 cap->opcount = 0;
7431}
7432
7433/*
7434 * "'" and "`" commands. Also for "g'" and "g`".
7435 * cap->arg is TRUE for "'" and "g'".
7436 */
7437 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007438nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439{
7440 pos_T *pos;
7441 int c;
7442#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007443 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7445#endif
7446
7447 if (cap->cmdchar == 'g')
7448 c = cap->extra_char;
7449 else
7450 c = cap->nchar;
7451 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7452 if (pos == (pos_T *)-1) /* jumped to other file */
7453 {
7454 if (cap->arg)
7455 {
7456 check_cursor_lnum();
7457 beginline(BL_WHITE | BL_FIX);
7458 }
7459 else
7460 check_cursor();
7461 }
7462 else
7463 nv_cursormark(cap, cap->arg, pos);
7464
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 /* May need to clear the coladd that a mark includes. */
7466 if (!virtual_active())
7467 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02007468 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469#ifdef FEAT_FOLDING
7470 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007471 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007472 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 && (fdo_flags & FDO_MARK)
7474 && old_KeyTyped)
7475 foldOpenCursor();
7476#endif
7477}
7478
7479/*
7480 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7481 */
7482 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007483nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484{
7485#ifdef FEAT_JUMPLIST
7486 pos_T *pos;
7487# ifdef FEAT_FOLDING
7488 linenr_T lnum = curwin->w_cursor.lnum;
7489 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7490# endif
7491
7492 if (!checkclearopq(cap->oap))
7493 {
7494 if (cap->cmdchar == 'g')
7495 pos = movechangelist((int)cap->count1);
7496 else
7497 pos = movemark((int)cap->count1);
7498 if (pos == (pos_T *)-1) /* jump to other file */
7499 {
7500 curwin->w_set_curswant = TRUE;
7501 check_cursor();
7502 }
7503 else if (pos != NULL) /* can jump */
7504 nv_cursormark(cap, FALSE, pos);
7505 else if (cap->cmdchar == 'g')
7506 {
7507 if (curbuf->b_changelistlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007508 emsg(_("E664: changelist is empty"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 else if (cap->count1 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007510 emsg(_("E662: At start of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007512 emsg(_("E663: At end of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 }
7514 else
7515 clearopbeep(cap->oap);
7516# ifdef FEAT_FOLDING
7517 if (cap->oap->op_type == OP_NOP
7518 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7519 && (fdo_flags & FDO_MARK)
7520 && old_KeyTyped)
7521 foldOpenCursor();
7522# endif
7523 }
7524#else
7525 clearopbeep(cap->oap);
7526#endif
7527}
7528
7529/*
7530 * Handle '"' command.
7531 */
7532 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007533nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534{
7535 if (checkclearop(cap->oap))
7536 return;
7537#ifdef FEAT_EVAL
7538 if (cap->nchar == '=')
7539 cap->nchar = get_expr_register();
7540#endif
7541 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7542 {
7543 cap->oap->regname = cap->nchar;
7544 cap->opcount = cap->count0; /* remember count before '"' */
7545#ifdef FEAT_EVAL
7546 set_reg_var(cap->oap->regname);
7547#endif
7548 }
7549 else
7550 clearopbeep(cap->oap);
7551}
7552
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553/*
7554 * Handle "v", "V" and "CTRL-V" commands.
7555 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7556 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007557 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 */
7559 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007560nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007562 if (cap->cmdchar == Ctrl_Q)
7563 cap->cmdchar = Ctrl_V;
7564
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7566 * characterwise, linewise, or blockwise. */
7567 if (cap->oap->op_type != OP_NOP)
7568 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01007569 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 finish_op = FALSE; /* operator doesn't finish now but later */
7571 return;
7572 }
7573
7574 VIsual_select = cap->arg;
7575 if (VIsual_active) /* change Visual mode */
7576 {
7577 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7578 end_visual_mode();
7579 else /* toggle char/block mode */
7580 { /* or char/line mode */
7581 VIsual_mode = cap->cmdchar;
7582 showmode();
7583 }
7584 redraw_curbuf_later(INVERTED); /* update the inversion */
7585 }
7586 else /* start Visual mode */
7587 {
7588 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007589 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007591 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 VIsual = curwin->w_cursor;
7593
7594 VIsual_active = TRUE;
7595 VIsual_reselect = TRUE;
7596 if (!cap->arg)
7597 /* start Select mode when 'selectmode' contains "cmd" */
7598 may_start_select('c');
7599#ifdef FEAT_MOUSE
7600 setmouse();
7601#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007602 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 redraw_cmdline = TRUE; /* show visual mode later */
7604 /*
7605 * For V and ^V, we multiply the number of lines even if there
7606 * was only one -- webb
7607 */
7608 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7609 {
7610 curwin->w_cursor.lnum +=
7611 resel_VIsual_line_count * cap->count0 - 1;
7612 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7613 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7614 }
7615 VIsual_mode = resel_VIsual_mode;
7616 if (VIsual_mode == 'v')
7617 {
7618 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007619 {
7620 validate_virtcol();
7621 curwin->w_curswant = curwin->w_virtcol
7622 + resel_VIsual_vcol * cap->count0 - 1;
7623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007625 curwin->w_curswant = resel_VIsual_vcol;
7626 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007628 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 {
7630 curwin->w_curswant = MAXCOL;
7631 coladvance((colnr_T)MAXCOL);
7632 }
7633 else if (VIsual_mode == Ctrl_V)
7634 {
7635 validate_virtcol();
7636 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007637 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 coladvance(curwin->w_curswant);
7639 }
7640 else
7641 curwin->w_set_curswant = TRUE;
7642 redraw_curbuf_later(INVERTED); /* show the inversion */
7643 }
7644 else
7645 {
7646 if (!cap->arg)
7647 /* start Select mode when 'selectmode' contains "cmd" */
7648 may_start_select('c');
7649 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007650 if (VIsual_mode != 'V' && *p_sel == 'e')
7651 ++cap->count1; /* include one more char */
7652 if (cap->count0 > 0 && --cap->count1 > 0)
7653 {
7654 /* With a count select that many characters or lines. */
7655 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7656 nv_right(cap);
7657 else if (VIsual_mode == 'V')
7658 nv_down(cap);
7659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 }
7661 }
7662}
7663
7664/*
7665 * Start selection for Shift-movement keys.
7666 */
7667 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007668start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669{
7670 /* if 'selectmode' contains "key", start Select mode */
7671 may_start_select('k');
7672 n_start_visual_mode('v');
7673}
7674
7675/*
7676 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7677 */
7678 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007679may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680{
7681 VIsual_select = (stuff_empty() && typebuf_typed()
7682 && (vim_strchr(p_slm, c) != NULL));
7683}
7684
7685/*
7686 * Start Visual mode "c".
7687 * Should set VIsual_select before calling this.
7688 */
7689 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007690n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007692#ifdef FEAT_CONCEAL
7693 /* Check for redraw before changing the state. */
Bram Moolenaarb9464822018-05-10 15:09:49 +02007694 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007695#endif
7696
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 VIsual_mode = c;
7698 VIsual_active = TRUE;
7699 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007700
7701 // Corner case: the 0 position in a tab may change when going into
7702 // virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007704 {
7705 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 VIsual = curwin->w_cursor;
7709
7710#ifdef FEAT_FOLDING
7711 foldAdjustVisual();
7712#endif
7713
7714#ifdef FEAT_MOUSE
7715 setmouse();
7716#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007717#ifdef FEAT_CONCEAL
7718 /* Check for redraw after changing the state. */
Bram Moolenaarb9464822018-05-10 15:09:49 +02007719 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007720#endif
7721
Bram Moolenaar09df3122006-01-23 22:23:09 +00007722 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 redraw_cmdline = TRUE; /* show visual mode later */
7724#ifdef FEAT_CLIPBOARD
7725 /* Make sure the clipboard gets updated. Needed because start and
7726 * end may still be the same, and the selection needs to be owned */
7727 clip_star.vmode = NUL;
7728#endif
7729
7730 /* Only need to redraw this line, unless still need to redraw an old
7731 * Visual area (when 'lazyredraw' is set). */
7732 if (curwin->w_redr_type < INVERTED)
7733 {
7734 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7735 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7736 }
7737}
7738
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739
7740/*
7741 * CTRL-W: Window commands
7742 */
7743 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007744nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745{
Bram Moolenaar938783d2017-07-16 20:13:26 +02007746 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007747 {
Bram Moolenaar938783d2017-07-16 20:13:26 +02007748 /* "CTRL-W :" is the same as typing ":"; useful in a terminal window */
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007749 cap->cmdchar = ':';
7750 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02007751 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007752 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02007753 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755}
7756
7757/*
7758 * CTRL-Z: Suspend
7759 */
7760 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007761nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762{
7763 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 if (VIsual_active)
7765 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 do_cmdline_cmd((char_u *)"st");
7767}
7768
7769/*
7770 * Commands starting with "g".
7771 */
7772 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007773nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774{
7775 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 int i;
7778 int flag = FALSE;
7779
7780 switch (cap->nchar)
7781 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007782 case Ctrl_A:
7783 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784#ifdef MEM_PROFILE
7785 /*
7786 * "g^A": dump log of used memory.
7787 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007788 if (!VIsual_active && cap->nchar == Ctrl_A)
7789 vim_mem_profile_dump();
7790 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007792 /*
7793 * "g^A/g^X": sequentially increment visually selected region
7794 */
7795 if (VIsual_active)
7796 {
7797 cap->arg = TRUE;
7798 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01007799 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007800 nv_addsub(cap);
7801 }
7802 else
7803 clearopbeep(oap);
7804 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 /*
7807 * "gR": Enter virtual replace mode.
7808 */
7809 case 'R':
7810 cap->arg = TRUE;
7811 nv_Replace(cap);
7812 break;
7813
7814 case 'r':
7815 nv_vreplace(cap);
7816 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817
7818 case '&':
7819 do_cmdline_cmd((char_u *)"%s//~/&");
7820 break;
7821
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 /*
7823 * "gv": Reselect the previous Visual area. If Visual already active,
7824 * exchange previous and current Visual area.
7825 */
7826 case 'v':
7827 if (checkclearop(oap))
7828 break;
7829
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007830 if ( curbuf->b_visual.vi_start.lnum == 0
7831 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7832 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 beep_flush();
7834 else
7835 {
7836 /* set w_cursor to the start of the Visual area, tpos to the end */
7837 if (VIsual_active)
7838 {
7839 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007840 VIsual_mode = curbuf->b_visual.vi_mode;
7841 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842# ifdef FEAT_EVAL
7843 curbuf->b_visual_mode_eval = i;
7844# endif
7845 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007846 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7847 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007849 tpos = curbuf->b_visual.vi_end;
7850 curbuf->b_visual.vi_end = curwin->w_cursor;
7851 curwin->w_cursor = curbuf->b_visual.vi_start;
7852 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 }
7854 else
7855 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007856 VIsual_mode = curbuf->b_visual.vi_mode;
7857 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7858 tpos = curbuf->b_visual.vi_end;
7859 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 }
7861
7862 VIsual_active = TRUE;
7863 VIsual_reselect = TRUE;
7864
7865 /* Set Visual to the start and w_cursor to the end of the Visual
7866 * area. Make sure they are on an existing character. */
7867 check_cursor();
7868 VIsual = curwin->w_cursor;
7869 curwin->w_cursor = tpos;
7870 check_cursor();
7871 update_topline();
7872 /*
7873 * When called from normal "g" command: start Select mode when
7874 * 'selectmode' contains "cmd". When called for K_SELECT, always
7875 * start Select mode.
7876 */
7877 if (cap->arg)
7878 VIsual_select = TRUE;
7879 else
7880 may_start_select('c');
7881#ifdef FEAT_MOUSE
7882 setmouse();
7883#endif
7884#ifdef FEAT_CLIPBOARD
7885 /* Make sure the clipboard gets updated. Needed because start and
7886 * end are still the same, and the selection needs to be owned */
7887 clip_star.vmode = NUL;
7888#endif
7889 redraw_curbuf_later(INVERTED);
7890 showmode();
7891 }
7892 break;
7893 /*
7894 * "gV": Don't reselect the previous Visual area after a Select mode
7895 * mapping of menu.
7896 */
7897 case 'V':
7898 VIsual_reselect = FALSE;
7899 break;
7900
7901 /*
7902 * "gh": start Select mode.
7903 * "gH": start Select line mode.
7904 * "g^H": start Select block mode.
7905 */
7906 case K_BS:
7907 cap->nchar = Ctrl_H;
7908 /* FALLTHROUGH */
7909 case 'h':
7910 case 'H':
7911 case Ctrl_H:
7912# ifdef EBCDIC
7913 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7914 if (cap->nchar == Ctrl_H)
7915 cap->cmdchar = Ctrl_V;
7916 else
7917# endif
7918 cap->cmdchar = cap->nchar + ('v' - 'h');
7919 cap->arg = TRUE;
7920 nv_visual(cap);
7921 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02007922
7923 /* "gn", "gN" visually select next/previous search match
7924 * "gn" selects next match
7925 * "gN" selects previous match
7926 */
7927 case 'N':
7928 case 'n':
7929 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02007930 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02007931 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932
7933 /*
7934 * "gj" and "gk" two new funny movement keys -- up and down
7935 * movement based on *screen* line rather than *file* line.
7936 */
7937 case 'j':
7938 case K_DOWN:
7939 /* with 'nowrap' it works just like the normal "j" command; also when
7940 * in a closed fold */
7941 if (!curwin->w_p_wrap
7942#ifdef FEAT_FOLDING
7943 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7944#endif
7945 )
7946 {
7947 oap->motion_type = MLINE;
7948 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7949 }
7950 else
7951 i = nv_screengo(oap, FORWARD, cap->count1);
7952 if (i == FAIL)
7953 clearopbeep(oap);
7954 break;
7955
7956 case 'k':
7957 case K_UP:
7958 /* with 'nowrap' it works just like the normal "k" command; also when
7959 * in a closed fold */
7960 if (!curwin->w_p_wrap
7961#ifdef FEAT_FOLDING
7962 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7963#endif
7964 )
7965 {
7966 oap->motion_type = MLINE;
7967 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7968 }
7969 else
7970 i = nv_screengo(oap, BACKWARD, cap->count1);
7971 if (i == FAIL)
7972 clearopbeep(oap);
7973 break;
7974
7975 /*
7976 * "gJ": join two lines without inserting a space.
7977 */
7978 case 'J':
7979 nv_join(cap);
7980 break;
7981
7982 /*
7983 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7984 * "gm": middle of "g0" and "g$".
7985 */
7986 case '^':
7987 flag = TRUE;
7988 /* FALLTHROUGH */
7989
7990 case '0':
7991 case 'm':
7992 case K_HOME:
7993 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 oap->motion_type = MCHAR;
7995 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007996 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 {
Bram Moolenaar02631462017-09-22 15:20:32 +02007998 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 int width2 = width1 + curwin_col_off2();
8000
8001 validate_virtcol();
8002 i = 0;
8003 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8004 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8005 }
8006 else
8007 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008008 /* Go to the middle of the screen line. When 'number' or
8009 * 'relativenumber' is on and lines are wrapping the middle can be more
8010 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02008012 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 + ((curwin->w_p_wrap && i > 0)
8014 ? curwin_col_off2() : 0)) / 2;
8015 coladvance((colnr_T)i);
8016 if (flag)
8017 {
8018 do
8019 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01008020 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008021 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 }
8023 curwin->w_set_curswant = TRUE;
8024 break;
8025
8026 case '_':
8027 /* "g_": to the last non-blank character in the line or <count> lines
8028 * downward. */
8029 cap->oap->motion_type = MCHAR;
8030 cap->oap->inclusive = TRUE;
8031 curwin->w_curswant = MAXCOL;
8032 if (cursor_down((long)(cap->count1 - 1),
8033 cap->oap->op_type == OP_NOP) == FAIL)
8034 clearopbeep(cap->oap);
8035 else
8036 {
8037 char_u *ptr = ml_get_curline();
8038
8039 /* In Visual mode we may end up after the line. */
8040 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8041 --curwin->w_cursor.col;
8042
8043 /* Decrease the cursor column until it's on a non-blank. */
8044 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01008045 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 --curwin->w_cursor.col;
8047 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008048 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 }
8050 break;
8051
8052 case '$':
8053 case K_END:
8054 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 {
8056 int col_off = curwin_col_off();
8057
8058 oap->motion_type = MCHAR;
8059 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008060 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 {
8062 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8063 if (cap->count1 == 1)
8064 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008065 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 int width2 = width1 + curwin_col_off2();
8067
8068 validate_virtcol();
8069 i = width1 - 1;
8070 if (curwin->w_virtcol >= (colnr_T)width1)
8071 i += ((curwin->w_virtcol - width1) / width2 + 1)
8072 * width2;
8073 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008074
8075 /* Make sure we stick in this column. */
8076 validate_virtcol();
8077 curwin->w_curswant = curwin->w_virtcol;
8078 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8080 {
8081 /*
8082 * Check for landing on a character that got split at
8083 * the end of the line. We do not want to advance to
8084 * the next screen line.
8085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 if (curwin->w_virtcol > (colnr_T)i)
8087 --curwin->w_cursor.col;
8088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 }
8090 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8091 clearopbeep(oap);
8092 }
8093 else
8094 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008095 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008097
8098 /* Make sure we stick in this column. */
8099 validate_virtcol();
8100 curwin->w_curswant = curwin->w_virtcol;
8101 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 }
8103 }
8104 break;
8105
8106 /*
8107 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8108 */
8109 case '*':
8110 case '#':
8111#if POUND != '#'
8112 case POUND: /* pound sign (sometimes equal to '#') */
8113#endif
8114 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8115 case ']': /* :tselect for current identifier */
8116 nv_ident(cap);
8117 break;
8118
8119 /*
8120 * ge and gE: go back to end of word
8121 */
8122 case 'e':
8123 case 'E':
8124 oap->motion_type = MCHAR;
8125 curwin->w_set_curswant = TRUE;
8126 oap->inclusive = TRUE;
8127 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8128 clearopbeep(oap);
8129 break;
8130
8131 /*
8132 * "g CTRL-G": display info about cursor position
8133 */
8134 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01008135 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 break;
8137
8138 /*
8139 * "gi": start Insert at the last position.
8140 */
8141 case 'i':
8142 if (curbuf->b_last_insert.lnum != 0)
8143 {
8144 curwin->w_cursor = curbuf->b_last_insert;
8145 check_cursor_lnum();
8146 i = (int)STRLEN(ml_get_curline());
8147 if (curwin->w_cursor.col > (colnr_T)i)
8148 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 if (virtual_active())
8150 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 curwin->w_cursor.col = i;
8152 }
8153 }
8154 cap->cmdchar = 'i';
8155 nv_edit(cap);
8156 break;
8157
8158 /*
8159 * "gI": Start insert in column 1.
8160 */
8161 case 'I':
8162 beginline(0);
8163 if (!checkclearopq(oap))
8164 invoke_edit(cap, FALSE, 'g', FALSE);
8165 break;
8166
8167#ifdef FEAT_SEARCHPATH
8168 /*
8169 * "gf": goto file, edit file under cursor
8170 * "]f" and "[f": can also be used.
8171 */
8172 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008173 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 nv_gotofile(cap);
8175 break;
8176#endif
8177
8178 /* "g'm" and "g`m": jump to mark without setting pcmark */
8179 case '\'':
8180 cap->arg = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008181 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 case '`':
8183 nv_gomark(cap);
8184 break;
8185
8186 /*
8187 * "gs": Goto sleep.
8188 */
8189 case 's':
8190 do_sleep(cap->count1 * 1000L);
8191 break;
8192
8193 /*
8194 * "ga": Display the ascii value of the character under the
8195 * cursor. It is displayed in decimal, hex, and octal. -- webb
8196 */
8197 case 'a':
8198 do_ascii(NULL);
8199 break;
8200
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 /*
8202 * "g8": Display the bytes used for the UTF-8 character under the
8203 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008204 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 */
8206 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008207 if (cap->count0 == 8)
8208 utf_find_illegal();
8209 else
8210 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212
Bram Moolenaar60402d62017-04-20 18:54:50 +02008213 /* "g<": show scrollback text */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008214 case '<':
8215 show_sb_text();
8216 break;
8217
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 /*
8219 * "gg": Goto the first line in file. With a count it goes to
8220 * that line number like for "G". -- webb
8221 */
8222 case 'g':
8223 cap->arg = FALSE;
8224 nv_goto(cap);
8225 break;
8226
8227 /*
8228 * Two-character operators:
8229 * "gq" Format text
8230 * "gw" Format text and keep cursor position
8231 * "g~" Toggle the case of the text.
8232 * "gu" Change text to lower case.
8233 * "gU" Change text to upper case.
8234 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008235 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 */
8237 case 'q':
8238 case 'w':
8239 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008240 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 case '~':
8242 case 'u':
8243 case 'U':
8244 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008245 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 nv_operator(cap);
8247 break;
8248
8249 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008250 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 * current function
8252 * "gD": idem, but in the current file.
8253 */
8254 case 'd':
8255 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008256 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 break;
8258
8259#ifdef FEAT_MOUSE
8260 /*
8261 * g<*Mouse> : <C-*mouse>
8262 */
8263 case K_MIDDLEMOUSE:
8264 case K_MIDDLEDRAG:
8265 case K_MIDDLERELEASE:
8266 case K_LEFTMOUSE:
8267 case K_LEFTDRAG:
8268 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01008269 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 case K_RIGHTMOUSE:
8271 case K_RIGHTDRAG:
8272 case K_RIGHTRELEASE:
8273 case K_X1MOUSE:
8274 case K_X1DRAG:
8275 case K_X1RELEASE:
8276 case K_X2MOUSE:
8277 case K_X2DRAG:
8278 case K_X2RELEASE:
8279 mod_mask = MOD_MASK_CTRL;
8280 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8281 break;
8282#endif
8283
8284 case K_IGNORE:
8285 break;
8286
8287 /*
8288 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8289 */
8290 case 'p':
8291 case 'P':
8292 nv_put(cap);
8293 break;
8294
8295#ifdef FEAT_BYTEOFF
8296 /* "go": goto byte count from start of buffer */
8297 case 'o':
8298 goto_byte(cap->count0);
8299 break;
8300#endif
8301
8302 /* "gQ": improved Ex mode */
8303 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008304 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 {
8306 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008307 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 break;
8309 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008310
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 if (!checkclearopq(oap))
8312 do_exmode(TRUE);
8313 break;
8314
8315#ifdef FEAT_JUMPLIST
8316 case ',':
8317 nv_pcmark(cap);
8318 break;
8319
8320 case ';':
8321 cap->count1 = -cap->count1;
8322 nv_pcmark(cap);
8323 break;
8324#endif
8325
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008326 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008327 if (!checkclearop(oap))
8328 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008329 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008330 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008331 if (!checkclearop(oap))
8332 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008333 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008334
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008335 case '+':
8336 case '-': /* "g+" and "g-": undo or redo along the timeline */
8337 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008338 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008339 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008340 break;
8341
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 default:
8343 clearopbeep(oap);
8344 break;
8345 }
8346}
8347
8348/*
8349 * Handle "o" and "O" commands.
8350 */
8351 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008352n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008354#ifdef FEAT_CONCEAL
8355 linenr_T oldline = curwin->w_cursor.lnum;
8356#endif
8357
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 if (!checkclearopq(cap->oap))
8359 {
8360#ifdef FEAT_FOLDING
8361 if (cap->cmdchar == 'O')
8362 /* Open above the first line of a folded sequence of lines */
8363 (void)hasFolding(curwin->w_cursor.lnum,
8364 &curwin->w_cursor.lnum, NULL);
8365 else
8366 /* Open below the last line of a folded sequence of lines */
8367 (void)hasFolding(curwin->w_cursor.lnum,
8368 NULL, &curwin->w_cursor.lnum);
8369#endif
8370 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8371 (cap->cmdchar == 'O' ? 1 : 0)),
8372 (linenr_T)(curwin->w_cursor.lnum +
8373 (cap->cmdchar == 'o' ? 1 : 0))
8374 ) == OK
8375 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8376#ifdef FEAT_COMMENTS
8377 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8378#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02008379 0, 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008381#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008382 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01008383 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02008384#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008385#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02008386 if (curwin->w_p_cul)
8387 /* force redraw of cursorline */
8388 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008389#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008390 /* When '#' is in 'cpoptions' ignore the count. */
8391 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8392 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8394 }
8395 }
8396}
8397
8398/*
8399 * "." command: redo last change.
8400 */
8401 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008402nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403{
8404 if (!checkclearopq(cap->oap))
8405 {
8406 /*
8407 * If "restart_edit" is TRUE, the last but one command is repeated
8408 * instead of the last command (inserting text). This is used for
8409 * CTRL-O <.> in insert mode.
8410 */
8411 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8412 clearopbeep(cap->oap);
8413 }
8414}
8415
8416/*
8417 * CTRL-R: undo undo
8418 */
8419 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008420nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421{
8422 if (!checkclearopq(cap->oap))
8423 {
8424 u_redo((int)cap->count1);
8425 curwin->w_set_curswant = TRUE;
8426 }
8427}
8428
8429/*
8430 * Handle "U" command.
8431 */
8432 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008433nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434{
8435 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008436 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 {
8438 /* translate "gUU" to "gUgU" */
8439 cap->cmdchar = 'g';
8440 cap->nchar = 'U';
8441 nv_operator(cap);
8442 }
8443 else if (!checkclearopq(cap->oap))
8444 {
8445 u_undoline();
8446 curwin->w_set_curswant = TRUE;
8447 }
8448}
8449
8450/*
8451 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8452 * single character.
8453 */
8454 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008455nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008457 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02008458 {
8459#ifdef FEAT_JOB_CHANNEL
8460 if (bt_prompt(curbuf) && !prompt_curpos_editable())
8461 {
8462 clearopbeep(cap->oap);
8463 return;
8464 }
8465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 else
8469 nv_operator(cap);
8470}
8471
8472/*
8473 * Handle an operator command.
8474 * The actual work is done by do_pending_operator().
8475 */
8476 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008477nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478{
8479 int op_type;
8480
8481 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008482#ifdef FEAT_JOB_CHANNEL
8483 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
8484 {
8485 clearopbeep(cap->oap);
8486 return;
8487 }
8488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489
8490 if (op_type == cap->oap->op_type) /* double operator works on lines */
8491 nv_lineop(cap);
8492 else if (!checkclearop(cap->oap))
8493 {
8494 cap->oap->start = curwin->w_cursor;
8495 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008496#ifdef FEAT_EVAL
8497 set_op_var(op_type);
8498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 }
8500}
8501
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008502#ifdef FEAT_EVAL
8503/*
8504 * Set v:operator to the characters for "optype".
8505 */
8506 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008507set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008508{
8509 char_u opchars[3];
8510
8511 if (optype == OP_NOP)
8512 set_vim_var_string(VV_OP, NULL, 0);
8513 else
8514 {
8515 opchars[0] = get_op_char(optype);
8516 opchars[1] = get_extra_op_char(optype);
8517 opchars[2] = NUL;
8518 set_vim_var_string(VV_OP, opchars, -1);
8519 }
8520}
8521#endif
8522
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523/*
8524 * Handle linewise operator "dd", "yy", etc.
8525 *
8526 * "_" is is a strange motion command that helps make operators more logical.
8527 * It is actually implemented, but not documented in the real Vi. This motion
8528 * command actually refers to "the current line". Commands like "dd" and "yy"
8529 * are really an alternate form of "d_" and "y_". It does accept a count, so
8530 * "d3_" works to delete 3 lines.
8531 */
8532 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008533nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534{
8535 cap->oap->motion_type = MLINE;
8536 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8537 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008538 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8539 && cap->oap->motion_force != 'v'
8540 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 || cap->oap->op_type == OP_LSHIFT
8542 || cap->oap->op_type == OP_RSHIFT)
8543 beginline(BL_SOL | BL_FIX);
8544 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8545 beginline(BL_WHITE | BL_FIX);
8546}
8547
8548/*
8549 * <Home> command.
8550 */
8551 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008552nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008554 /* CTRL-HOME is like "gg" */
8555 if (mod_mask & MOD_MASK_CTRL)
8556 nv_goto(cap);
8557 else
8558 {
8559 cap->count0 = 1;
8560 nv_pipe(cap);
8561 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008562 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8563 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564}
8565
8566/*
8567 * "|" command.
8568 */
8569 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008570nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571{
8572 cap->oap->motion_type = MCHAR;
8573 cap->oap->inclusive = FALSE;
8574 beginline(0);
8575 if (cap->count0 > 0)
8576 {
8577 coladvance((colnr_T)(cap->count0 - 1));
8578 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8579 }
8580 else
8581 curwin->w_curswant = 0;
8582 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008583 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 curwin->w_set_curswant = FALSE;
8585}
8586
8587/*
8588 * Handle back-word command "b" and "B".
8589 * cap->arg is 1 for "B"
8590 */
8591 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008592nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593{
8594 cap->oap->motion_type = MCHAR;
8595 cap->oap->inclusive = FALSE;
8596 curwin->w_set_curswant = TRUE;
8597 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8598 clearopbeep(cap->oap);
8599#ifdef FEAT_FOLDING
8600 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8601 foldOpenCursor();
8602#endif
8603}
8604
8605/*
8606 * Handle word motion commands "e", "E", "w" and "W".
8607 * cap->arg is TRUE for "E" and "W".
8608 */
8609 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008610nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611{
8612 int n;
8613 int word_end;
8614 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008615 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616
8617 /*
8618 * Set inclusive for the "E" and "e" command.
8619 */
8620 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8621 word_end = TRUE;
8622 else
8623 word_end = FALSE;
8624 cap->oap->inclusive = word_end;
8625
8626 /*
8627 * "cw" and "cW" are a special case.
8628 */
8629 if (!word_end && cap->oap->op_type == OP_CHANGE)
8630 {
8631 n = gchar_cursor();
8632 if (n != NUL) /* not an empty line */
8633 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008634 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 {
8636 /*
8637 * Reproduce a funny Vi behaviour: "cw" on a blank only
8638 * changes one character, not all blanks until the start of
8639 * the next word. Only do this when the 'w' flag is included
8640 * in 'cpoptions'.
8641 */
8642 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8643 {
8644 cap->oap->inclusive = TRUE;
8645 cap->oap->motion_type = MCHAR;
8646 return;
8647 }
8648 }
8649 else
8650 {
8651 /*
8652 * This is a little strange. To match what the real Vi does,
8653 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8654 * that we are not on a space or a TAB. This seems impolite
8655 * at first, but it's really more what we mean when we say
8656 * 'cw'.
8657 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008658 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 * will change only one character! This is done by setting
8660 * flag.
8661 */
8662 cap->oap->inclusive = TRUE;
8663 word_end = TRUE;
8664 flag = TRUE;
8665 }
8666 }
8667 }
8668
8669 cap->oap->motion_type = MCHAR;
8670 curwin->w_set_curswant = TRUE;
8671 if (word_end)
8672 n = end_word(cap->count1, cap->arg, flag, FALSE);
8673 else
8674 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8675
Bram Moolenaardfefb982008-04-01 10:06:39 +00008676 /* Don't leave the cursor on the NUL past the end of line. Unless we
8677 * didn't move it forward. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008678 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008679 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680
8681 if (n == FAIL && cap->oap->op_type == OP_NOP)
8682 clearopbeep(cap->oap);
8683 else
8684 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686#ifdef FEAT_FOLDING
8687 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8688 foldOpenCursor();
8689#endif
8690 }
8691}
8692
8693/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008694 * Used after a movement command: If the cursor ends up on the NUL after the
8695 * end of the line, may move it back to the last character and make the motion
8696 * inclusive.
8697 */
8698 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008699adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008700{
8701 /* The cursor cannot remain on the NUL when:
8702 * - the column is > 0
8703 * - not in Visual mode or 'selection' is "o"
8704 * - 'virtualedit' is not "all" and not "onemore".
8705 */
8706 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008707 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01008708 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008709 {
8710 --curwin->w_cursor.col;
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008711 /* prevent cursor from moving on the trail byte */
8712 if (has_mbyte)
8713 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008714 oap->inclusive = TRUE;
8715 }
8716}
8717
8718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 * "0" and "^" commands.
8720 * cap->arg is the argument for beginline().
8721 */
8722 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008723nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724{
8725 cap->oap->motion_type = MCHAR;
8726 cap->oap->inclusive = FALSE;
8727 beginline(cap->arg);
8728#ifdef FEAT_FOLDING
8729 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8730 foldOpenCursor();
8731#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008732 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8733 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734}
8735
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736/*
8737 * In exclusive Visual mode, may include the last character.
8738 */
8739 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008740adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741{
8742 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008743 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 if (has_mbyte)
8746 inc_cursor();
8747 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 ++curwin->w_cursor.col;
8749 cap->oap->inclusive = FALSE;
8750 }
8751}
8752
8753/*
8754 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8755 * Should check VIsual_mode before calling this.
8756 * Returns TRUE when backed up to the previous line.
8757 */
8758 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008759unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760{
8761 pos_T *pp;
8762
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008763 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008765 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 pp = &curwin->w_cursor;
8767 else
8768 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 if (pp->coladd > 0)
8770 --pp->coladd;
8771 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 if (pp->col > 0)
8773 {
8774 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008775 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 }
8777 else if (pp->lnum > 1)
8778 {
8779 --pp->lnum;
8780 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8781 return TRUE;
8782 }
8783 }
8784 return FALSE;
8785}
8786
8787/*
8788 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8789 */
8790 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008791nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792{
8793 if (VIsual_active)
8794 VIsual_select = TRUE;
8795 else if (VIsual_reselect)
8796 {
8797 cap->nchar = 'v'; /* fake "gv" command */
8798 cap->arg = TRUE;
8799 nv_g_cmd(cap);
8800 }
8801}
8802
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803
8804/*
8805 * "G", "gg", CTRL-END, CTRL-HOME.
8806 * cap->arg is TRUE for "G".
8807 */
8808 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008809nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810{
8811 linenr_T lnum;
8812
8813 if (cap->arg)
8814 lnum = curbuf->b_ml.ml_line_count;
8815 else
8816 lnum = 1L;
8817 cap->oap->motion_type = MLINE;
8818 setpcmark();
8819
8820 /* When a count is given, use it instead of the default lnum */
8821 if (cap->count0 != 0)
8822 lnum = cap->count0;
8823 if (lnum < 1L)
8824 lnum = 1L;
8825 else if (lnum > curbuf->b_ml.ml_line_count)
8826 lnum = curbuf->b_ml.ml_line_count;
8827 curwin->w_cursor.lnum = lnum;
8828 beginline(BL_SOL | BL_FIX);
8829#ifdef FEAT_FOLDING
8830 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8831 foldOpenCursor();
8832#endif
8833}
8834
8835/*
8836 * CTRL-\ in Normal mode.
8837 */
8838 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008839nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840{
8841 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8842 {
8843 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008844 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 clear_cmdline = TRUE; /* unshow mode later */
8846 restart_edit = 0;
8847#ifdef FEAT_CMDWIN
8848 if (cmdwin_type != 0)
8849 cmdwin_result = Ctrl_C;
8850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 if (VIsual_active)
8852 {
8853 end_visual_mode(); /* stop Visual */
8854 redraw_curbuf_later(INVERTED);
8855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8857 if (cap->nchar == Ctrl_G && p_im)
8858 restart_edit = 'a';
8859 }
8860 else
8861 clearopbeep(cap->oap);
8862}
8863
8864/*
8865 * ESC in Normal mode: beep, but don't flush buffers.
8866 * Don't even beep if we are canceling a command.
8867 */
8868 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008869nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870{
8871 int no_reason;
8872
8873 no_reason = (cap->oap->op_type == OP_NOP
8874 && cap->opcount == 0
8875 && cap->count0 == 0
8876 && cap->oap->regname == 0
8877 && !p_im);
8878
8879 if (cap->arg) /* TRUE for CTRL-C */
8880 {
8881 if (restart_edit == 0
8882#ifdef FEAT_CMDWIN
8883 && cmdwin_type == 0
8884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01008887 {
8888 if (anyBufIsChanged())
8889 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
8890 else
8891 msg(_("Type :qa and press <Enter> to exit Vim"));
8892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893
8894 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8895 * set again below when halfway a mapping. */
8896 if (!p_im)
8897 restart_edit = 0;
8898#ifdef FEAT_CMDWIN
8899 if (cmdwin_type != 0)
8900 {
8901 cmdwin_result = K_IGNORE;
8902 got_int = FALSE; /* don't stop executing autocommands et al. */
8903 return;
8904 }
8905#endif
8906 }
8907
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 if (VIsual_active)
8909 {
8910 end_visual_mode(); /* stop Visual */
8911 check_cursor_col(); /* make sure cursor is not beyond EOL */
8912 curwin->w_set_curswant = TRUE;
8913 redraw_curbuf_later(INVERTED);
8914 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008915 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008916 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 clearop(cap->oap);
8918
8919 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8920 * set return to Insert mode afterwards. */
Bram Moolenaare2c38102016-01-31 14:55:40 +01008921 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 restart_edit = 'a';
8923}
8924
8925/*
8926 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01008927 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 */
8929 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008930nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931{
8932 /* <Insert> is equal to "i" */
8933 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8934 cap->cmdchar = 'i';
8935
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 /* in Visual mode "A" and "I" are an operator */
8937 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02008938 {
8939#ifdef FEAT_TERMINAL
8940 if (term_in_normal_mode())
8941 {
8942 end_visual_mode();
8943 clearop(cap->oap);
8944 term_enter_job_mode();
8945 return;
8946 }
8947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02008949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950
8951 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008952 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8953 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 {
8955#ifdef FEAT_TEXTOBJ
8956 nv_object(cap);
8957#else
8958 clearopbeep(cap->oap);
8959#endif
8960 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02008961#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02008962 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02008963 {
8964 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02008965 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02008966 return;
8967 }
8968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 else if (!curbuf->b_p_ma && !p_im)
8970 {
8971 /* Only give this error when 'insertmode' is off. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008972 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01008974 if (cap->cmdchar == K_PS)
8975 /* drop the pasted text */
8976 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 }
Bram Moolenaara1891842017-02-04 21:34:31 +01008978 else if (cap->cmdchar == K_PS && VIsual_active)
8979 {
8980 pos_T old_pos = curwin->w_cursor;
8981 pos_T old_visual = VIsual;
8982
8983 /* In Visual mode the selected text is deleted. */
8984 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
8985 {
8986 shift_delete_registers();
8987 cap->oap->regname = '1';
8988 }
8989 else
8990 cap->oap->regname = '-';
8991 cap->cmdchar = 'd';
8992 cap->nchar = NUL;
8993 nv_operator(cap);
8994 do_pending_operator(cap, 0, FALSE);
8995 cap->cmdchar = K_PS;
8996
8997 /* When the last char in the line was deleted then append. Detect this
8998 * by checking if the cursor moved to before the Visual area. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008999 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
9000 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01009001 inc_cursor();
9002
9003 /* Insert to replace the deleted text with the pasted text. */
9004 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 else if (!checkclearopq(cap->oap))
9007 {
9008 switch (cap->cmdchar)
9009 {
9010 case 'A': /* "A"ppend after the line */
9011 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 if (ve_flags == VE_ALL)
9013 {
9014 int save_State = State;
9015
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009016 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 * character past the end of the line */
9018 State = INSERT;
9019 coladvance((colnr_T)MAXCOL);
9020 State = save_State;
9021 }
9022 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9024 break;
9025
9026 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009027 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9028 beginline(BL_WHITE);
9029 else
9030 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 break;
9032
Bram Moolenaara1891842017-02-04 21:34:31 +01009033 case K_PS:
9034 /* Bracketed paste works like "a"ppend, unless the cursor is in
9035 * the first column, then it inserts. */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009036 if (curwin->w_cursor.col == 0)
9037 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02009038 /* FALLTHROUGH */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009039
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 case 'a': /* "a"ppend is like "i"nsert on the next character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 /* increment coladd when in virtual space, increment the
9042 * column otherwise, also to append after an unprintable char */
9043 if (virtual_active()
9044 && (curwin->w_cursor.coladd > 0
9045 || *ml_get_cursor() == NUL
9046 || *ml_get_cursor() == TAB))
9047 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01009048 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 inc_cursor();
9050 break;
9051 }
9052
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9054 {
9055 int save_State = State;
9056
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009057 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 * character past the end of the line */
9059 State = INSERT;
9060 coladvance(getviscol());
9061 State = save_State;
9062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063
9064 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9065 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009066 else if (cap->cmdchar == K_PS)
9067 /* drop the pasted text */
9068 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069}
9070
9071/*
9072 * Invoke edit() and take care of "restart_edit" and the return value.
9073 */
9074 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009075invoke_edit(
9076 cmdarg_T *cap,
9077 int repl, /* "r" or "gr" command */
9078 int cmd,
9079 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080{
9081 int restart_edit_save = 0;
9082
9083 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9084 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9085 * it. */
9086 if (repl || !stuff_empty())
9087 restart_edit_save = restart_edit;
9088 else
9089 restart_edit_save = 0;
9090
9091 /* Always reset "restart_edit", this is not a restarted edit. */
9092 restart_edit = 0;
9093
9094 if (edit(cmd, startln, cap->count1))
9095 cap->retval |= CA_COMMAND_BUSY;
9096
9097 if (restart_edit == 0)
9098 restart_edit = restart_edit_save;
9099}
9100
9101#ifdef FEAT_TEXTOBJ
9102/*
9103 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9104 */
9105 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009106nv_object(
9107 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108{
9109 int flag;
9110 int include;
9111 char_u *mps_save;
9112
9113 if (cap->cmdchar == 'i')
9114 include = FALSE; /* "ix" = inner object: exclude white space */
9115 else
9116 include = TRUE; /* "ax" = an object: include white space */
9117
9118 /* Make sure (), [], {} and <> are in 'matchpairs' */
9119 mps_save = curbuf->b_p_mps;
9120 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9121
9122 switch (cap->nchar)
9123 {
9124 case 'w': /* "aw" = a word */
9125 flag = current_word(cap->oap, cap->count1, include, FALSE);
9126 break;
9127 case 'W': /* "aW" = a WORD */
9128 flag = current_word(cap->oap, cap->count1, include, TRUE);
9129 break;
9130 case 'b': /* "ab" = a braces block */
9131 case '(':
9132 case ')':
9133 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9134 break;
9135 case 'B': /* "aB" = a Brackets block */
9136 case '{':
9137 case '}':
9138 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9139 break;
9140 case '[': /* "a[" = a [] block */
9141 case ']':
9142 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9143 break;
9144 case '<': /* "a<" = a <> block */
9145 case '>':
9146 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9147 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009148 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01009149 /* Do not adjust oap->end in do_pending_operator()
9150 * otherwise there are different results for 'dit'
9151 * (note leading whitespace in last line):
9152 * 1) <b> 2) <b>
9153 * foobar foobar
9154 * </b> </b>
9155 */
9156 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009157 flag = current_tagblock(cap->oap, cap->count1, include);
9158 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 case 'p': /* "ap" = a paragraph */
9160 flag = current_par(cap->oap, cap->count1, include, 'p');
9161 break;
9162 case 's': /* "as" = a sentence */
9163 flag = current_sent(cap->oap, cap->count1, include);
9164 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009165 case '"': /* "a"" = a double quoted string */
9166 case '\'': /* "a'" = a single quoted string */
9167 case '`': /* "a`" = a backtick quoted string */
9168 flag = current_quote(cap->oap, cap->count1, include,
9169 cap->nchar);
9170 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171#if 0 /* TODO */
9172 case 'S': /* "aS" = a section */
9173 case 'f': /* "af" = a filename */
9174 case 'u': /* "au" = a URL */
9175#endif
9176 default:
9177 flag = FAIL;
9178 break;
9179 }
9180
9181 curbuf->b_p_mps = mps_save;
9182 if (flag == FAIL)
9183 clearopbeep(cap->oap);
9184 adjust_cursor_col();
9185 curwin->w_set_curswant = TRUE;
9186}
9187#endif
9188
9189/*
9190 * "q" command: Start/stop recording.
9191 * "q:", "q/", "q?": edit command-line in command-line window.
9192 */
9193 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009194nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195{
9196 if (cap->oap->op_type == OP_FORMAT)
9197 {
9198 /* "gqq" is the same as "gqgq": format line */
9199 cap->cmdchar = 'g';
9200 cap->nchar = 'q';
9201 nv_operator(cap);
9202 }
9203 else if (!checkclearop(cap->oap))
9204 {
9205#ifdef FEAT_CMDWIN
9206 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9207 {
9208 stuffcharReadbuff(cap->nchar);
9209 stuffcharReadbuff(K_CMDWIN);
9210 }
9211 else
9212#endif
9213 /* (stop) recording into a named register, unless executing a
9214 * register */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009215 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 clearopbeep(cap->oap);
9217 }
9218}
9219
9220/*
9221 * Handle the "@r" command.
9222 */
9223 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009224nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225{
9226 if (checkclearop(cap->oap))
9227 return;
9228#ifdef FEAT_EVAL
9229 if (cap->nchar == '=')
9230 {
9231 if (get_expr_register() == NUL)
9232 return;
9233 }
9234#endif
9235 while (cap->count1-- && !got_int)
9236 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009237 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 {
9239 clearopbeep(cap->oap);
9240 break;
9241 }
9242 line_breakcheck();
9243 }
9244}
9245
9246/*
9247 * Handle the CTRL-U and CTRL-D commands.
9248 */
9249 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009250nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251{
9252 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9253 || (cap->cmdchar == Ctrl_D
9254 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9255 clearopbeep(cap->oap);
9256 else if (!checkclearop(cap->oap))
9257 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9258}
9259
9260/*
9261 * Handle "J" or "gJ" command.
9262 */
9263 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009264nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 if (VIsual_active) /* join the visual lines */
9267 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009268 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 {
9270 if (cap->count0 <= 1)
9271 cap->count0 = 2; /* default for join is two lines! */
9272 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9273 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 {
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009275 /* can't join when on the last line */
9276 if (cap->count0 <= 2)
9277 {
9278 clearopbeep(cap->oap);
9279 return;
9280 }
9281 cap->count0 = curbuf->b_ml.ml_line_count
9282 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009284
9285 prep_redo(cap->oap->regname, cap->count0,
9286 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9287 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 }
9289}
9290
9291/*
9292 * "P", "gP", "p" and "gp" commands.
9293 */
9294 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009295nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 int regname = 0;
9298 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009299 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009300 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 int dir;
9302 int flags = 0;
9303
9304 if (cap->oap->op_type != OP_NOP)
9305 {
9306#ifdef FEAT_DIFF
9307 /* "dp" is ":diffput" */
9308 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9309 {
9310 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009311 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 }
9313 else
9314#endif
9315 clearopbeep(cap->oap);
9316 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02009317#ifdef FEAT_JOB_CHANNEL
9318 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
9319 {
9320 clearopbeep(cap->oap);
9321 }
9322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 else
9324 {
9325 dir = (cap->cmdchar == 'P'
9326 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9327 ? BACKWARD : FORWARD;
9328 prep_redo_cmd(cap);
9329 if (cap->cmdchar == 'g')
9330 flags |= PUT_CURSEND;
9331
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 if (VIsual_active)
9333 {
9334 /* Putting in Visual mode: The put text replaces the selected
9335 * text. First delete the selected text, then put the new text.
9336 * Need to save and restore the registers that the delete
9337 * overwrites if the old contents is being put.
9338 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009339 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009341#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009343#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009344 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009345 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009346#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349
9350 )
9351 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009352 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 * put, save it first. */
9354 reg1 = get_register(regname, TRUE);
9355 }
9356
9357 /* Now delete the selected text. */
9358 cap->cmdchar = 'd';
9359 cap->nchar = NUL;
9360 cap->oap->regname = NUL;
9361 nv_operator(cap);
9362 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009363 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364
9365 /* delete PUT_LINE_BACKWARD; */
9366 cap->oap->regname = regname;
9367
9368 if (reg1 != NULL)
9369 {
9370 /* Delete probably changed the register we want to put, save
9371 * it first. Then put back what was there before the delete. */
9372 reg2 = get_register(regname, FALSE);
9373 put_register(regname, reg1);
9374 }
9375
9376 /* When deleted a linewise Visual area, put the register as
9377 * lines to avoid it joined with the next line. When deletion was
9378 * characterwise, split a line when putting lines. */
9379 if (VIsual_mode == 'V')
9380 flags |= PUT_LINE;
9381 else if (VIsual_mode == 'v')
9382 flags |= PUT_LINE_SPLIT;
9383 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9384 flags |= PUT_LINE_FORWARD;
9385 dir = BACKWARD;
9386 if ((VIsual_mode != 'V'
9387 && curwin->w_cursor.col < curbuf->b_op_start.col)
9388 || (VIsual_mode == 'V'
9389 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9390 /* cursor is at the end of the line or end of file, put
9391 * forward. */
9392 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009393 /* May have been reset in do_put(). */
9394 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 do_put(cap->oap->regname, dir, cap->count1, flags);
9397
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 /* If a register was saved, put it back now. */
9399 if (reg2 != NULL)
9400 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009401
9402 /* What to reselect with "gv"? Selecting the just put text seems to
9403 * be the most useful, since the original text was removed. */
9404 if (was_visual)
9405 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009406 curbuf->b_visual.vi_start = curbuf->b_op_start;
9407 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01009408 /* need to adjust cursor position */
9409 if (*p_sel == 'e')
9410 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009411 }
9412
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009413 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009414 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009415 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009416 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009417 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009418
9419 /* If the cursor was in that line, move it to the end of the last
9420 * line. */
9421 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9422 {
9423 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9424 coladvance((colnr_T)MAXCOL);
9425 }
9426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 auto_format(FALSE, TRUE);
9428 }
9429}
9430
9431/*
9432 * "o" and "O" commands.
9433 */
9434 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009435nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436{
9437#ifdef FEAT_DIFF
9438 /* "do" is ":diffget" */
9439 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9440 {
9441 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009442 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 }
9444 else
9445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 if (VIsual_active) /* switch start and end of visual */
9447 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009448#ifdef FEAT_JOB_CHANNEL
9449 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02009450 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 n_opencmd(cap);
9454}
9455
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456#ifdef FEAT_NETBEANS_INTG
9457 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009458nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459{
9460 netbeans_keycommand(cap->nchar);
9461}
9462#endif
9463
9464#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009466nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467{
9468 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9469}
9470#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009471
Bram Moolenaar3918c952005-03-15 22:34:55 +00009472/*
9473 * Trigger CursorHold event.
9474 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9475 * input buffer. "did_cursorhold" is set to avoid retriggering.
9476 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009477 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009478nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00009479{
9480 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9481 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009482 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009483}
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009484
9485/*
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009486 * Calculate start/end virtual columns for operating in block mode.
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009487 */
9488 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009489get_op_vcol(
9490 oparg_T *oap,
9491 colnr_T redo_VIsual_vcol,
9492 int initial) /* when TRUE adjust position for 'selectmode' */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009493{
9494 colnr_T start, end;
9495
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009496 if (VIsual_mode != Ctrl_V
Bram Moolenaar02631462017-09-22 15:20:32 +02009497 || (!initial && oap->end.col < curwin->w_width))
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009498 return;
9499
Bram Moolenaar10ad1d92015-09-25 19:35:02 +02009500 oap->block_mode = TRUE;
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009501
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009502 /* prevent from moving onto a trail byte */
9503 if (has_mbyte)
9504 mb_adjustpos(curwin->w_buffer, &oap->end);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009505
9506 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009507
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009508 if (!redo_VIsual_busy)
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009509 {
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009510 getvvcol(curwin, &(oap->end), &start, NULL, &end);
9511
9512 if (start < oap->start_vcol)
9513 oap->start_vcol = start;
9514 if (end > oap->end_vcol)
9515 {
9516 if (initial && *p_sel == 'e' && start >= 1
9517 && start - 1 >= oap->end_vcol)
9518 oap->end_vcol = start - 1;
9519 else
9520 oap->end_vcol = end;
9521 }
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009522 }
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009523
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009524 /* if '$' was used, get oap->end_vcol from longest line */
9525 if (curwin->w_curswant == MAXCOL)
9526 {
9527 curwin->w_cursor.col = MAXCOL;
9528 oap->end_vcol = 0;
9529 for (curwin->w_cursor.lnum = oap->start.lnum;
9530 curwin->w_cursor.lnum <= oap->end.lnum;
9531 ++curwin->w_cursor.lnum)
9532 {
9533 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
9534 if (end > oap->end_vcol)
9535 oap->end_vcol = end;
9536 }
9537 }
9538 else if (redo_VIsual_busy)
9539 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
9540 /*
9541 * Correct oap->end.col and oap->start.col to be the
9542 * upper-left and lower-right corner of the block area.
9543 *
9544 * (Actually, this does convert column positions into character
9545 * positions)
9546 */
9547 curwin->w_cursor.lnum = oap->end.lnum;
9548 coladvance(oap->end_vcol);
9549 oap->end = curwin->w_cursor;
9550
9551 curwin->w_cursor = oap->start;
9552 coladvance(oap->start_vcol);
9553 oap->start = curwin->w_cursor;
9554}