blob: b512b559ada3beb095f52ad493fa843bfcea63db [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 Moolenaar9c96f592005-06-30 21:52:39 +0000217 {Ctrl_M, 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_FKMAP
Bram Moolenaaree2615a2016-07-02 18:25:34 +0200401 {K_F8, farsi_f8, 0, 0},
402 {K_F9, farsi_f9, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404#ifdef FEAT_NETBEANS_INTG
405 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
406#endif
407#ifdef FEAT_DND
408 {K_DROP, nv_drop, NV_STS, 0},
409#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000410 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100411 {K_PS, nv_edit, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412};
413
414/* Number of commands in nv_cmds[]. */
415#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
416
417/* Sorted index of commands in nv_cmds[]. */
418static short nv_cmd_idx[NV_CMDS_SIZE];
419
420/* The highest index for which
421 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
422static int nv_max_linear;
423
424/*
425 * Compare functions for qsort() below, that checks the command character
426 * through the index in nv_cmd_idx[].
427 */
428 static int
429#ifdef __BORLANDC__
430_RTLENTRYF
431#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +0100432nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433{
434 int c1, c2;
435
436 /* The commands are sorted on absolute value. */
437 c1 = nv_cmds[*(const short *)s1].cmd_char;
438 c2 = nv_cmds[*(const short *)s2].cmd_char;
439 if (c1 < 0)
440 c1 = -c1;
441 if (c2 < 0)
442 c2 = -c2;
443 return c1 - c2;
444}
445
446/*
447 * Initialize the nv_cmd_idx[] table.
448 */
449 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100450init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451{
452 int i;
453
454 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000455 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 nv_cmd_idx[i] = i;
457
458 /* Sort the commands by the command character. */
459 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
460
461 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000462 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
464 break;
465 nv_max_linear = i - 1;
466}
467
468/*
469 * Search for a command in the commands table.
470 * Returns -1 for invalid command.
471 */
472 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100473find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474{
475 int i;
476 int idx;
477 int top, bot;
478 int c;
479
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 /* A multi-byte character is never a command. */
481 if (cmdchar >= 0x100)
482 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483
484 /* We use the absolute value of the character. Special keys have a
485 * negative value, but are sorted on their absolute value. */
486 if (cmdchar < 0)
487 cmdchar = -cmdchar;
488
489 /* If the character is in the first part: The character is the index into
490 * nv_cmd_idx[]. */
491 if (cmdchar <= nv_max_linear)
492 return nv_cmd_idx[cmdchar];
493
494 /* Perform a binary search. */
495 bot = nv_max_linear + 1;
496 top = NV_CMDS_SIZE - 1;
497 idx = -1;
498 while (bot <= top)
499 {
500 i = (top + bot) / 2;
501 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
502 if (c < 0)
503 c = -c;
504 if (cmdchar == c)
505 {
506 idx = nv_cmd_idx[i];
507 break;
508 }
509 if (cmdchar > c)
510 bot = i + 1;
511 else
512 top = i - 1;
513 }
514 return idx;
515}
516
517/*
518 * Execute a command in Normal mode.
519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100521normal_cmd(
522 oparg_T *oap,
523 int toplevel UNUSED) /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 cmdarg_T ca; /* command arguments */
526 int c;
527 int ctrl_w = FALSE; /* got CTRL-W command */
528 int old_col = curwin->w_curswant;
529#ifdef FEAT_CMDL_INFO
530 int need_flushbuf; /* need to call out_flush() */
531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 pos_T old_pos; /* cursor position before command */
533 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 static int old_mapped_len = 0;
535 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000536#ifdef FEAT_EVAL
537 int set_prevcount = FALSE;
538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
540 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
541 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000542
543 /* Use a count remembered from before entering an operator. After typing
544 * "3d" we return from normal_cmd() and come back here, the "3" is
545 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 ca.opcount = opcount;
547
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 /*
549 * If there is an operator pending, then the command we take this time
550 * will terminate it. Finish_op tells us to finish the operation before
551 * returning this time (unless the operation was cancelled).
552 */
553#ifdef CURSOR_SHAPE
554 c = finish_op;
555#endif
556 finish_op = (oap->op_type != OP_NOP);
557#ifdef CURSOR_SHAPE
558 if (finish_op != c)
559 {
560 ui_cursor_shape(); /* may show different cursor shape */
561# ifdef FEAT_MOUSESHAPE
562 update_mouseshape(-1);
563# endif
564 }
565#endif
566
Bram Moolenaara983fe92008-07-31 20:04:27 +0000567 /* When not finishing an operator and no register name typed, reset the
568 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000570 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000572#ifdef FEAT_EVAL
573 set_prevcount = TRUE;
574#endif
575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576
Bram Moolenaara983fe92008-07-31 20:04:27 +0000577 /* Restore counts from before receiving K_CURSORHOLD. This means after
578 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
579 * "3 * 2". */
580 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
581 {
582 ca.opcount = oap->prev_opcount;
583 ca.count0 = oap->prev_count0;
584 oap->prev_opcount = 0;
585 oap->prev_count0 = 0;
586 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000587
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589
590 State = NORMAL_BUSY;
591#ifdef USE_ON_FLY_SCROLL
592 dont_scroll = FALSE; /* allow scrolling here */
593#endif
594
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100595#ifdef FEAT_EVAL
596 /* Set v:count here, when called from main() and not a stuffed
597 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100598 * when there is no count. Do set it for redo. */
599 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100600 set_vcount_ca(&ca, &set_prevcount);
601#endif
602
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 /*
604 * Get the command character from the user.
605 */
606 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100607 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608
609 /*
610 * If a mapping was started in Visual or Select mode, remember the length
611 * of the mapping. This is used below to not return to Insert mode for as
612 * long as the mapping is being executed.
613 */
614 if (restart_edit == 0)
615 old_mapped_len = 0;
616 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000617 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 old_mapped_len = typebuf_maplen();
619
620 if (c == NUL)
621 c = K_ZERO;
622
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 /*
624 * In Select mode, typed text replaces the selection.
625 */
626 if (VIsual_active
627 && VIsual_select
628 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
629 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000630 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
631 * 'insertmode' is set) fake a "d"elete command, Insert mode will
632 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000633 * Insert the typed character in the typeahead buffer, so that it can
634 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000635 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000636 if (restart_edit != 0)
637 c = 'd';
638 else
639 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000640 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar9bad29d2013-05-21 12:46:02 +0200641 old_mapped_len = 0; /* do go to Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643
644#ifdef FEAT_CMDL_INFO
645 need_flushbuf = add_to_showcmd(c);
646#endif
647
648getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 {
651 /*
652 * Handle a count before a command and compute ca.count0.
653 * Note that '0' is a command and not the start of a count, but it's
654 * part of a count after other digits.
655 */
656 while ( (c >= '1' && c <= '9')
657 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
658 {
659 if (c == K_DEL || c == K_KDEL)
660 {
661 ca.count0 /= 10;
662#ifdef FEAT_CMDL_INFO
663 del_from_showcmd(4); /* delete the digit and ~@% */
664#endif
665 }
666 else
667 ca.count0 = ca.count0 * 10 + (c - '0');
668 if (ca.count0 < 0) /* got too large! */
669 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000670#ifdef FEAT_EVAL
671 /* Set v:count here, when called from main() and not a stuffed
672 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100673 * right after the count. Do set it for redo. */
674 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100675 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 if (ctrl_w)
678 {
679 ++no_mapping;
680 ++allow_keys; /* no mapping for nchar, but keys */
681 }
682 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000683 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 --no_zero_mapping;
686 if (ctrl_w)
687 {
688 --no_mapping;
689 --allow_keys;
690 }
691#ifdef FEAT_CMDL_INFO
692 need_flushbuf |= add_to_showcmd(c);
693#endif
694 }
695
696 /*
697 * If we got CTRL-W there may be a/another count
698 */
699 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
700 {
701 ctrl_w = TRUE;
702 ca.opcount = ca.count0; /* remember first count */
703 ca.count0 = 0;
704 ++no_mapping;
705 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000706 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 --no_mapping;
709 --allow_keys;
710#ifdef FEAT_CMDL_INFO
711 need_flushbuf |= add_to_showcmd(c);
712#endif
713 goto getcount; /* jump back */
714 }
715 }
716
Bram Moolenaara983fe92008-07-31 20:04:27 +0000717 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000719 /* Save the count values so that ca.opcount and ca.count0 are exactly
720 * the same when coming back here after handling K_CURSORHOLD. */
721 oap->prev_opcount = ca.opcount;
722 oap->prev_count0 = ca.count0;
723 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100724 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000725 {
726 /*
727 * If we're in the middle of an operator (including after entering a
728 * yank buffer with '"') AND we had a count before the operator, then
729 * that count overrides the current value of ca.count0.
730 * What this means effectively, is that commands like "3dw" get turned
731 * into "d3w" which makes things fall into place pretty neatly.
732 * If you give a count before AND after the operator, they are
733 * multiplied.
734 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 if (ca.count0)
736 ca.count0 *= ca.opcount;
737 else
738 ca.count0 = ca.opcount;
739 }
740
741 /*
742 * Always remember the count. It will be set to zero (on the next call,
743 * above) when there is no pending operator.
744 * When called from main(), save the count for use by the "count" built-in
745 * variable.
746 */
747 ca.opcount = ca.count0;
748 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
749
750#ifdef FEAT_EVAL
751 /*
752 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100753 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100755 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000756 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757#endif
758
759 /*
760 * Find the command character in the table of commands.
761 * For CTRL-W we already got nchar when looking for a count.
762 */
763 if (ctrl_w)
764 {
765 ca.nchar = c;
766 ca.cmdchar = Ctrl_W;
767 }
768 else
769 ca.cmdchar = c;
770 idx = find_command(ca.cmdchar);
771 if (idx < 0)
772 {
773 /* Not a known command: beep. */
774 clearopbeep(oap);
775 goto normal_end;
776 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000777
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000778 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 {
Bram Moolenaard69bd9a2014-04-29 12:15:40 +0200780 /* This command is not allowed while editing a cmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000782 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 goto normal_end;
784 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000785 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
786 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 /*
789 * In Visual/Select mode, a few keys are handled in a special way.
790 */
791 if (VIsual_active)
792 {
793 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
794 if (km_stopsel
795 && (nv_cmds[idx].cmd_flags & NV_STS)
796 && !(mod_mask & MOD_MASK_SHIFT))
797 {
798 end_visual_mode();
799 redraw_curbuf_later(INVERTED);
800 }
801
802 /* Keys that work different when 'keymodel' contains "startsel" */
803 if (km_startsel)
804 {
805 if (nv_cmds[idx].cmd_flags & NV_SS)
806 {
807 unshift_special(&ca);
808 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000809 if (idx < 0)
810 {
811 /* Just in case */
812 clearopbeep(oap);
813 goto normal_end;
814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 }
816 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
817 && (mod_mask & MOD_MASK_SHIFT))
818 {
819 mod_mask &= ~MOD_MASK_SHIFT;
820 }
821 }
822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823
824#ifdef FEAT_RIGHTLEFT
825 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
826 && (nv_cmds[idx].cmd_flags & NV_RL))
827 {
828 /* Invert horizontal movements and operations. Only when typed by the
829 * user directly, not when the result of a mapping or "x" translated
830 * to "dl". */
831 switch (ca.cmdchar)
832 {
833 case 'l': ca.cmdchar = 'h'; break;
834 case K_RIGHT: ca.cmdchar = K_LEFT; break;
835 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
836 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
837 case 'h': ca.cmdchar = 'l'; break;
838 case K_LEFT: ca.cmdchar = K_RIGHT; break;
839 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
840 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
841 case '>': ca.cmdchar = '<'; break;
842 case '<': ca.cmdchar = '>'; break;
843 }
844 idx = find_command(ca.cmdchar);
845 }
846#endif
847
848 /*
849 * Get an additional character if we need one.
850 */
851 if ((nv_cmds[idx].cmd_flags & NV_NCH)
852 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
853 && oap->op_type == OP_NOP)
854 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
855 || (ca.cmdchar == 'q'
856 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200857 && reg_recording == 0
858 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100860 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 {
862 int *cp;
863 int repl = FALSE; /* get character for replace mode */
864 int lit = FALSE; /* get extra character literally */
865 int langmap_active = FALSE; /* using :lmap mappings */
866 int lang; /* getting a text character */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100867#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 int save_smd; /* saved value of p_smd */
869#endif
870
871 ++no_mapping;
872 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000873 /* Don't generate a CursorHold event here, most commands can't handle
874 * it, e.g., nv_replace(), nv_csearch(). */
875 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (ca.cmdchar == 'g')
877 {
878 /*
879 * For 'g' get the next character now, so that we can check for
880 * "gr", "g'" and "g`".
881 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000882 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884#ifdef FEAT_CMDL_INFO
885 need_flushbuf |= add_to_showcmd(ca.nchar);
886#endif
887 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100888 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 {
890 cp = &ca.extra_char; /* need to get a third character */
891 if (ca.nchar != 'r')
892 lit = TRUE; /* get it literally */
893 else
894 repl = TRUE; /* get it in replace mode */
895 }
896 else
897 cp = NULL; /* no third character needed */
898 }
899 else
900 {
901 if (ca.cmdchar == 'r') /* get it in replace mode */
902 repl = TRUE;
903 cp = &ca.nchar;
904 }
905 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
906
907 /*
908 * Get a second or third character.
909 */
910 if (cp != NULL)
911 {
912#ifdef CURSOR_SHAPE
913 if (repl)
914 {
915 State = REPLACE; /* pretend Replace mode */
916 ui_cursor_shape(); /* show different cursor shape */
917 }
918#endif
919 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
920 {
921 /* Allow mappings defined with ":lmap". */
922 --no_mapping;
923 --allow_keys;
924 if (repl)
925 State = LREPLACE;
926 else
927 State = LANGMAP;
928 langmap_active = TRUE;
929 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100930#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 save_smd = p_smd;
932 p_smd = FALSE; /* Don't let the IM code show the mode here */
933 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
934 im_set_active(TRUE);
935#endif
936
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000937 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938
939 if (langmap_active)
940 {
941 /* Undo the decrement done above */
942 ++no_mapping;
943 ++allow_keys;
944 State = NORMAL_BUSY;
945 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100946#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 if (lang)
948 {
949 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
950 im_save_status(&curbuf->b_p_iminsert);
951 im_set_active(FALSE);
952 }
953 p_smd = save_smd;
954#endif
955#ifdef CURSOR_SHAPE
956 State = NORMAL_BUSY;
957#endif
958#ifdef FEAT_CMDL_INFO
959 need_flushbuf |= add_to_showcmd(*cp);
960#endif
961
962 if (!lit)
963 {
964#ifdef FEAT_DIGRAPHS
965 /* Typing CTRL-K gets a digraph. */
966 if (*cp == Ctrl_K
967 && ((nv_cmds[idx].cmd_flags & NV_LANG)
968 || cp == &ca.extra_char)
969 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
970 {
971 c = get_digraph(FALSE);
972 if (c > 0)
973 {
974 *cp = c;
975# ifdef FEAT_CMDL_INFO
976 /* Guessing how to update showcmd here... */
977 del_from_showcmd(3);
978 need_flushbuf |= add_to_showcmd(*cp);
979# endif
980 }
981 }
982#endif
983
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 /* adjust chars > 127, except after "tTfFr" commands */
985 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986#ifdef FEAT_RIGHTLEFT
987 /* adjust Hebrew mapped char */
988 if (p_hkmap && lang && KeyTyped)
989 *cp = hkmap(*cp);
990# ifdef FEAT_FKMAP
991 /* adjust Farsi mapped char */
992 if (p_fkmap && lang && KeyTyped)
993 *cp = fkmap(*cp);
994# endif
995#endif
996 }
997
998 /*
999 * When the next character is CTRL-\ a following CTRL-N means the
1000 * command is aborted and we go to Normal mode.
1001 */
1002 if (cp == &ca.extra_char
1003 && ca.nchar == Ctrl_BSL
1004 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1005 {
1006 ca.cmdchar = Ctrl_BSL;
1007 ca.nchar = ca.extra_char;
1008 idx = find_command(ca.cmdchar);
1009 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001010 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001011 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 else if (*cp == Ctrl_BSL)
1013 {
1014 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1015
1016 /* There is a busy wait here when typing "f<C-\>" and then
1017 * something different from CTRL-N. Can't be avoided. */
1018 while ((c = vpeekc()) <= 0 && towait > 0L)
1019 {
1020 do_sleep(towait > 50L ? 50L : towait);
1021 towait -= 50L;
1022 }
1023 if (c > 0)
1024 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001025 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 if (c != Ctrl_N && c != Ctrl_G)
1027 vungetc(c);
1028 else
1029 {
1030 ca.cmdchar = Ctrl_BSL;
1031 ca.nchar = c;
1032 idx = find_command(ca.cmdchar);
1033 }
1034 }
1035 }
1036
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 /* When getting a text character and the next character is a
1038 * multi-byte character, it could be a composing character.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001039 * However, don't wait for it to arrive. Also, do enable mapping,
1040 * because if it's put back with vungetc() it's too late to apply
1041 * mapping. */
1042 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 while (enc_utf8 && lang && (c = vpeekc()) > 0
1044 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1045 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001046 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 if (!utf_iscomposing(c))
1048 {
1049 vungetc(c); /* it wasn't, put it back */
1050 break;
1051 }
1052 else if (ca.ncharC1 == 0)
1053 ca.ncharC1 = c;
1054 else
1055 ca.ncharC2 = c;
1056 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001057 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 }
1059 --no_mapping;
1060 --allow_keys;
1061 }
1062
1063#ifdef FEAT_CMDL_INFO
1064 /*
1065 * Flush the showcmd characters onto the screen so we can see them while
1066 * the command is being executed. Only do this when the shown command was
1067 * actually displayed, otherwise this will slow down a lot when executing
1068 * mappings.
1069 */
1070 if (need_flushbuf)
1071 out_flush();
1072#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001073 if (ca.cmdchar != K_IGNORE)
1074 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075
1076 State = NORMAL;
1077
1078 if (ca.nchar == ESC)
1079 {
1080 clearop(oap);
1081 if (restart_edit == 0 && goto_im())
1082 restart_edit = 'a';
1083 goto normal_end;
1084 }
1085
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001086 if (ca.cmdchar != K_IGNORE)
1087 {
1088 msg_didout = FALSE; /* don't scroll screen up for normal command */
1089 msg_col = 0;
1090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 old_pos = curwin->w_cursor; /* remember where cursor was */
1093
1094 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1095 * mode. */
1096 if (!VIsual_active && km_startsel)
1097 {
1098 if (nv_cmds[idx].cmd_flags & NV_SS)
1099 {
1100 start_selection();
1101 unshift_special(&ca);
1102 idx = find_command(ca.cmdchar);
1103 }
1104 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1105 && (mod_mask & MOD_MASK_SHIFT))
1106 {
1107 start_selection();
1108 mod_mask &= ~MOD_MASK_SHIFT;
1109 }
1110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111
1112 /*
1113 * Execute the command!
1114 * Call the command function found in the commands table.
1115 */
1116 ca.arg = nv_cmds[idx].cmd_arg;
1117 (nv_cmds[idx].cmd_func)(&ca);
1118
1119 /*
1120 * If we didn't start or finish an operator, reset oap->regname, unless we
1121 * need it later.
1122 */
1123 if (!finish_op
1124 && !oap->op_type
1125 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1126 {
1127 clearop(oap);
1128#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001129 {
1130 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001131
Bram Moolenaar536681b2011-05-10 16:12:45 +02001132 /* Adjust the register according to 'clipboard', so that when
1133 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001134# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001135 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001136# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001137 set_reg_var(regname);
1138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139#endif
1140 }
1141
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001142 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001143 * character or "z333<cr>". */
1144 if (old_mapped_len > 0)
1145 old_mapped_len = typebuf_maplen();
1146
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 /*
1148 * If an operation is pending, handle it...
1149 */
1150 do_pending_operator(&ca, old_col, FALSE);
1151
1152 /*
1153 * Wait for a moment when a message is displayed that will be overwritten
1154 * by the mode message.
1155 * In Visual mode and with "^O" in Insert mode, a short message will be
1156 * overwritten by the mode message. Wait a bit, until a key is hit.
1157 * In Visual mode, it's more important to keep the Visual area updated
1158 * than keeping a message (e.g. from a /pat search).
1159 * Only do this if the command was typed, not from a mapping.
1160 * Don't wait when emsg_silent is non-zero.
1161 * Also wait a bit after an error message, e.g. for "^O:".
1162 * Don't redraw the screen, it would remove the message.
1163 */
1164 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001165 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 || (VIsual_active
1168 && old_pos.lnum == curwin->w_cursor.lnum
1169 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 )
1171 && (clear_cmdline
1172 || redraw_cmdline)
1173 && (msg_didout || (msg_didany && msg_scroll))
1174 && !msg_nowait
1175 && KeyTyped)
1176 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 && (msg_scroll
1179 || emsg_on_display)))
1180 && oap->regname == 0
1181 && !(ca.retval & CA_COMMAND_BUSY)
1182 && stuff_empty()
1183 && typebuf_typed()
1184 && emsg_silent == 0
1185 && !did_wait_return
1186 && oap->op_type == OP_NOP)
1187 {
1188 int save_State = State;
1189
1190 /* Draw the cursor with the right shape here */
1191 if (restart_edit != 0)
1192 State = INSERT;
1193
1194 /* If need to redraw, and there is a "keep_msg", redraw before the
1195 * delay */
1196 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1197 {
1198 char_u *kmsg;
1199
1200 kmsg = keep_msg;
1201 keep_msg = NULL;
1202 /* showmode() will clear keep_msg, but we want to use it anyway */
1203 update_screen(0);
1204 /* now reset it, otherwise it's put in the history again */
1205 keep_msg = kmsg;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001206 msg_attr((char *)kmsg, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 vim_free(kmsg);
1208 }
1209 setcursor();
1210 cursor_on();
1211 out_flush();
1212 if (msg_scroll || emsg_on_display)
1213 ui_delay(1000L, TRUE); /* wait at least one second */
1214 ui_delay(3000L, FALSE); /* wait up to three seconds */
1215 State = save_State;
1216
1217 msg_scroll = FALSE;
1218 emsg_on_display = FALSE;
1219 }
1220
1221 /*
1222 * Finish up after executing a Normal mode command.
1223 */
1224normal_end:
1225
1226 msg_nowait = FALSE;
1227
1228 /* Reset finish_op, in case it was set */
1229#ifdef CURSOR_SHAPE
1230 c = finish_op;
1231#endif
1232 finish_op = FALSE;
1233#ifdef CURSOR_SHAPE
1234 /* Redraw the cursor with another shape, if we were in Operator-pending
1235 * mode or did a replace command. */
1236 if (c || ca.cmdchar == 'r')
1237 {
1238 ui_cursor_shape(); /* may show different cursor shape */
1239# ifdef FEAT_MOUSESHAPE
1240 update_mouseshape(-1);
1241# endif
1242 }
1243#endif
1244
1245#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001246 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001247 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 clear_showcmd();
1249#endif
1250
1251 checkpcmark(); /* check if we moved since setting pcmark */
1252 vim_free(ca.searchbuf);
1253
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 if (has_mbyte)
1255 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 if (curwin->w_p_scb && toplevel)
1258 {
1259 validate_cursor(); /* may need to update w_leftcol */
1260 do_check_scrollbind(TRUE);
1261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262
Bram Moolenaar860cae12010-06-05 23:22:07 +02001263 if (curwin->w_p_crb && toplevel)
1264 {
1265 validate_cursor(); /* may need to update w_leftcol */
1266 do_check_cursorbind();
1267 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001268
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001269#ifdef FEAT_TERMINAL
Bram Moolenaareef9add2017-09-16 15:38:04 +02001270 /* don't go to Insert mode if a terminal has a running job */
1271 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001272 restart_edit = 0;
1273#endif
1274
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 /*
1276 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1277 * if still inside a mapping that started in Visual mode).
1278 * May switch from Visual to Select mode after CTRL-O command.
1279 */
1280 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1282 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 && !(ca.retval & CA_COMMAND_BUSY)
1284 && stuff_empty()
1285 && oap->regname == 0)
1286 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 if (restart_VIsual_select == 1)
1288 {
1289 VIsual_select = TRUE;
1290 showmode();
1291 restart_VIsual_select = 0;
1292 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001293 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 (void)edit(restart_edit, FALSE, 1L);
1295 }
1296
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 if (restart_VIsual_select == 2)
1298 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299
1300 /* Save count before an operator for next time. */
1301 opcount = ca.opcount;
1302}
1303
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001304#ifdef FEAT_EVAL
1305/*
1306 * Set v:count and v:count1 according to "cap".
1307 * Set v:prevcount only when "set_prevcount" is TRUE.
1308 */
1309 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001310set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001311{
1312 long count = cap->count0;
1313
1314 /* multiply with cap->opcount the same way as above */
1315 if (cap->opcount != 0)
1316 count = cap->opcount * (count == 0 ? 1 : count);
1317 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1318 *set_prevcount = FALSE; /* only set v:prevcount once */
1319}
1320#endif
1321
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322/*
Bram Moolenaar5823f842019-01-03 22:58:08 +01001323 * Handle an operator after Visual mode or when the movement is finished.
1324 * "gui_yank" is true when yanking text for the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 */
1326 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001327do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328{
1329 oparg_T *oap = cap->oap;
1330 pos_T old_cursor;
1331 int empty_region_error;
1332 int restart_edit_save;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001333#ifdef FEAT_LINEBREAK
1334 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 /* The visual area is remembered for redo */
1338 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1339 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001340 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaard79e5502016-01-10 22:13:02 +01001342 static int redo_VIsual_arg; /* extra argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 int include_line_break = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344
1345#if defined(FEAT_CLIPBOARD)
1346 /*
1347 * Yank the visual area into the GUI selection register before we operate
1348 * on it and lose it forever.
1349 * Don't do it if a specific register was specified, so that ""x"*P works.
1350 * This could call do_pending_operator() recursively, but that's OK
1351 * because gui_yank will be TRUE for the nested call.
1352 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001353 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 && oap->op_type != OP_NOP
1355 && !gui_yank
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 && VIsual_active
1357 && !redo_VIsual_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 && oap->regname == 0)
1359 clip_auto_select();
1360#endif
1361 old_cursor = curwin->w_cursor;
1362
1363 /*
1364 * If an operation is pending, handle it...
1365 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001366 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 {
Bram Moolenaar5823f842019-01-03 22:58:08 +01001368 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
1369 // for the clipboard.
1370 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
1371
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001372#ifdef FEAT_LINEBREAK
1373 /* Avoid a problem with unwanted linebreaks in block mode. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001374 if (curwin->w_p_lbr)
1375 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001376 curwin->w_p_lbr = FALSE;
1377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 oap->is_VIsual = VIsual_active;
1379 if (oap->motion_force == 'V')
1380 oap->motion_type = MLINE;
1381 else if (oap->motion_force == 'v')
1382 {
1383 /* If the motion was linewise, "inclusive" will not have been set.
1384 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1385 if (oap->motion_type == MLINE)
1386 oap->inclusive = FALSE;
1387 /* If the motion already was characterwise, toggle "inclusive" */
1388 else if (oap->motion_type == MCHAR)
1389 oap->inclusive = !oap->inclusive;
1390 oap->motion_type = MCHAR;
1391 }
1392 else if (oap->motion_force == Ctrl_V)
1393 {
1394 /* Change line- or characterwise motion into Visual block mode. */
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01001395 if (!VIsual_active)
1396 {
1397 VIsual_active = TRUE;
1398 VIsual = oap->start;
1399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 VIsual_mode = Ctrl_V;
1401 VIsual_select = FALSE;
1402 VIsual_reselect = FALSE;
1403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404
Bram Moolenaar7afea822013-04-24 18:34:45 +02001405 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1406 /* Never redo "zf" (define fold). */
Bram Moolenaar5823f842019-01-03 22:58:08 +01001407 if ((redo_yank || oap->op_type != OP_YANK)
Bram Moolenaar7afea822013-04-24 18:34:45 +02001408 && ((!VIsual_active || oap->motion_force)
1409 /* Also redo Operator-pending Visual mode mappings */
1410 || (VIsual_active && cap->cmdchar == ':'
1411 && oap->op_type != OP_COLON))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001412 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413#ifdef FEAT_FOLDING
1414 && oap->op_type != OP_FOLD
1415 && oap->op_type != OP_FOLDOPEN
1416 && oap->op_type != OP_FOLDOPENREC
1417 && oap->op_type != OP_FOLDCLOSE
1418 && oap->op_type != OP_FOLDCLOSEREC
1419 && oap->op_type != OP_FOLDDEL
1420 && oap->op_type != OP_FOLDDELREC
1421#endif
1422 )
1423 {
1424 prep_redo(oap->regname, cap->count0,
1425 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1426 oap->motion_force, cap->cmdchar, cap->nchar);
1427 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1428 {
1429 /*
1430 * If 'cpoptions' does not contain 'r', insert the search
1431 * pattern to really repeat the same command.
1432 */
1433 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001434 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 AppendToRedobuff(NL_STR);
1436 }
1437 else if (cap->cmdchar == ':')
1438 {
1439 /* do_cmdline() has stored the first typed line in
1440 * "repeat_cmdline". When several lines are typed repeating
1441 * won't be possible. */
1442 if (repeat_cmdline == NULL)
1443 ResetRedobuff();
1444 else
1445 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001446 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 AppendToRedobuff(NL_STR);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001448 VIM_CLEAR(repeat_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 }
1450 }
1451 }
1452
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 if (redo_VIsual_busy)
1454 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001455 /* Redo of an operation on a Visual area. Use the same size from
1456 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 oap->start = curwin->w_cursor;
1458 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1459 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1460 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1461 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001462 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001464 if (VIsual_mode == 'v')
1465 {
1466 if (redo_VIsual_line_count <= 1)
1467 {
1468 validate_virtcol();
1469 curwin->w_curswant =
1470 curwin->w_virtcol + redo_VIsual_vcol - 1;
1471 }
1472 else
1473 curwin->w_curswant = redo_VIsual_vcol;
1474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001476 {
1477 curwin->w_curswant = MAXCOL;
1478 }
1479 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 }
1481 cap->count0 = redo_VIsual_count;
1482 if (redo_VIsual_count != 0)
1483 cap->count1 = redo_VIsual_count;
1484 else
1485 cap->count1 = 1;
1486 }
1487 else if (VIsual_active)
1488 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001489 if (!gui_yank)
1490 {
1491 /* Save the current VIsual area for '< and '> marks, and "gv" */
1492 curbuf->b_visual.vi_start = VIsual;
1493 curbuf->b_visual.vi_end = curwin->w_cursor;
1494 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001495 if (VIsual_mode_orig != NUL)
1496 {
1497 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1498 VIsual_mode_orig = NUL;
1499 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001500 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001502 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505
1506 /* In Select mode, a linewise selection is operated upon like a
Bram Moolenaard009e862015-06-09 20:20:03 +02001507 * characterwise selection.
1508 * Special case: gH<Del> deletes the last line. */
1509 if (VIsual_select && VIsual_mode == 'V'
1510 && cap->oap->op_type != OP_DELETE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001512 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 {
1514 VIsual.col = 0;
1515 curwin->w_cursor.col =
1516 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1517 }
1518 else
1519 {
1520 curwin->w_cursor.col = 0;
1521 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1522 }
1523 VIsual_mode = 'v';
1524 }
1525 /* If 'selection' is "exclusive", backup one character for
1526 * charwise selections. */
1527 else if (VIsual_mode == 'v')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001528 include_line_break = unadjust_for_sel();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529
1530 oap->start = VIsual;
1531 if (VIsual_mode == 'V')
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001532 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 oap->start.col = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001534 oap->start.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537
1538 /*
1539 * Set oap->start to the first position of the operated text, oap->end
1540 * to the end of the operated text. w_cursor is equal to oap->start.
1541 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001542 if (LT_POS(oap->start, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 {
1544#ifdef FEAT_FOLDING
1545 /* Include folded lines completely. */
1546 if (!VIsual_active)
1547 {
1548 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1549 oap->start.col = 0;
1550 if (hasFolding(curwin->w_cursor.lnum, NULL,
1551 &curwin->w_cursor.lnum))
1552 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1553 }
1554#endif
1555 oap->end = curwin->w_cursor;
1556 curwin->w_cursor = oap->start;
1557
1558 /* w_virtcol may have been updated; if the cursor goes back to its
1559 * previous position w_virtcol becomes invalid and isn't updated
1560 * automatically. */
1561 curwin->w_valid &= ~VALID_VIRTCOL;
1562 }
1563 else
1564 {
1565#ifdef FEAT_FOLDING
1566 /* Include folded lines completely. */
1567 if (!VIsual_active && oap->motion_type == MLINE)
1568 {
1569 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1570 NULL))
1571 curwin->w_cursor.col = 0;
1572 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1573 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1574 }
1575#endif
1576 oap->end = oap->start;
1577 oap->start = curwin->w_cursor;
1578 }
1579
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001580 /* Just in case lines were deleted that make the position invalid. */
1581 check_pos(curwin->w_buffer, &oap->end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1583
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 /* Set "virtual_op" before resetting VIsual_active. */
1585 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 if (VIsual_active || redo_VIsual_busy)
1588 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001589 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590
1591 if (!redo_VIsual_busy && !gui_yank)
1592 {
1593 /*
1594 * Prepare to reselect and redo Visual: this is based on the
1595 * size of the Visual text
1596 */
1597 resel_VIsual_mode = VIsual_mode;
1598 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001599 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001601 {
1602 if (VIsual_mode != Ctrl_V)
1603 getvvcol(curwin, &(oap->end),
1604 NULL, NULL, &oap->end_vcol);
1605 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1606 {
1607 if (VIsual_mode != Ctrl_V)
1608 getvvcol(curwin, &(oap->start),
1609 &oap->start_vcol, NULL, NULL);
1610 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1611 }
1612 else
1613 resel_VIsual_vcol = oap->end_vcol;
1614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 resel_VIsual_line_count = oap->line_count;
1616 }
1617
1618 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
Bram Moolenaar5823f842019-01-03 22:58:08 +01001619 if ((redo_yank || oap->op_type != OP_YANK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 && oap->op_type != OP_COLON
1621#ifdef FEAT_FOLDING
1622 && oap->op_type != OP_FOLD
1623 && oap->op_type != OP_FOLDOPEN
1624 && oap->op_type != OP_FOLDOPENREC
1625 && oap->op_type != OP_FOLDCLOSE
1626 && oap->op_type != OP_FOLDCLOSEREC
1627 && oap->op_type != OP_FOLDDEL
1628 && oap->op_type != OP_FOLDDELREC
1629#endif
1630 && oap->motion_force == NUL
1631 )
1632 {
1633 /* Prepare for redoing. Only use the nchar field for "r",
1634 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001635 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1636 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001637 prep_redo(oap->regname, cap->count0,
1638 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1639 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001640 else if (cap->cmdchar != ':')
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001641 {
1642 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
1643
1644 /* reverse what nv_replace() did */
1645 if (nchar == REPLACE_CR_NCHAR)
1646 nchar = CAR;
1647 else if (nchar == REPLACE_NL_NCHAR)
1648 nchar = NL;
Bram Moolenaar641e2862012-07-25 15:06:34 +02001649 prep_redo(oap->regname, 0L, NUL, 'v',
1650 get_op_char(oap->op_type),
1651 get_extra_op_char(oap->op_type),
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001652 nchar);
1653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 if (!redo_VIsual_busy)
1655 {
1656 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001657 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 redo_VIsual_line_count = resel_VIsual_line_count;
1659 redo_VIsual_count = cap->count0;
Bram Moolenaard79e5502016-01-10 22:13:02 +01001660 redo_VIsual_arg = cap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 }
1662 }
1663
1664 /*
1665 * oap->inclusive defaults to TRUE.
1666 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1667 * FALSE. This makes "d}P" and "v}dP" work the same.
1668 */
1669 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1670 oap->inclusive = TRUE;
1671 if (VIsual_mode == 'V')
1672 oap->motion_type = MLINE;
1673 else
1674 {
1675 oap->motion_type = MCHAR;
1676 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001677 && (include_line_break || !virtual_op))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 {
1679 oap->inclusive = FALSE;
1680 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001681 * that works on lines only. */
Bram Moolenaard009e862015-06-09 20:20:03 +02001682 if (*p_sel != 'o'
1683 && !op_on_lines(oap->op_type)
1684 && oap->end.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 {
Bram Moolenaard009e862015-06-09 20:20:03 +02001686 ++oap->end.lnum;
1687 oap->end.col = 0;
Bram Moolenaard009e862015-06-09 20:20:03 +02001688 oap->end.coladd = 0;
Bram Moolenaard009e862015-06-09 20:20:03 +02001689 ++oap->line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 }
1691 }
1692 }
1693
1694 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001695
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 /*
1697 * Switch Visual off now, so screen updating does
1698 * not show inverted text when the screen is redrawn.
1699 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1700 * no screen redraw, so it is done here to remove the inverted
1701 * part.
1702 */
1703 if (!gui_yank)
1704 {
1705 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001706#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 setmouse();
1708 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001709#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001710 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 if ((oap->op_type == OP_YANK
1712 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001713 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 || oap->op_type == OP_FILTER)
1715 && oap->motion_force == NUL)
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001716 {
1717#ifdef FEAT_LINEBREAK
1718 /* make sure redrawing is correct */
1719 curwin->w_p_lbr = lbr_saved;
1720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 }
1724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 /* Include the trailing byte of a multi-byte char. */
1727 if (has_mbyte && oap->inclusive)
1728 {
1729 int l;
1730
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001731 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 if (l > 1)
1733 oap->end.col += l - 1;
1734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 curwin->w_set_curswant = TRUE;
1736
1737 /*
1738 * oap->empty is set when start and end are the same. The inclusive
1739 * flag affects this too, unless yanking and the end is on a NUL.
1740 */
1741 oap->empty = (oap->motion_type == MCHAR
1742 && (!oap->inclusive
1743 || (oap->op_type == OP_YANK
1744 && gchar_pos(&oap->end) == NUL))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001745 && EQUAL_POS(oap->start, oap->end)
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001746 && !(virtual_op && oap->start.coladd != oap->end.coladd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 /*
1748 * For delete, change and yank, it's an error to operate on an
1749 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1750 */
1751 empty_region_error = (oap->empty
1752 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1753
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 /* Force a redraw when operating on an empty Visual region, when
1755 * 'modifiable is off or creating a fold. */
1756 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001757#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 ))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001761 {
1762#ifdef FEAT_LINEBREAK
1763 curwin->w_p_lbr = lbr_saved;
1764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767
1768 /*
1769 * If the end of an operator is in column one while oap->motion_type
1770 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1771 * character in the previous line. If op_start is on or before the
1772 * first non-blank in the line, the operator becomes linewise
1773 * (strange, but that's the way vi does it).
1774 */
1775 if ( oap->motion_type == MCHAR
1776 && oap->inclusive == FALSE
1777 && !(cap->retval & CA_NO_ADJ_OP_END)
1778 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001780 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 && oap->line_count > 1)
1782 {
1783 oap->end_adjusted = TRUE; /* remember that we did this */
1784 --oap->line_count;
1785 --oap->end.lnum;
1786 if (inindent(0))
1787 oap->motion_type = MLINE;
1788 else
1789 {
1790 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1791 if (oap->end.col)
1792 {
1793 --oap->end.col;
1794 oap->inclusive = TRUE;
1795 }
1796 }
1797 }
1798 else
1799 oap->end_adjusted = FALSE;
1800
1801 switch (oap->op_type)
1802 {
1803 case OP_LSHIFT:
1804 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001805 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 auto_format(FALSE, TRUE);
1807 break;
1808
1809 case OP_JOIN_NS:
1810 case OP_JOIN:
1811 if (oap->line_count < 2)
1812 oap->line_count = 2;
1813 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1814 curbuf->b_ml.ml_line_count)
1815 beep_flush();
1816 else
1817 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001818 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001819 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 auto_format(FALSE, TRUE);
1821 }
1822 break;
1823
1824 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001827 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001828 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001829 CancelRedo();
1830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 else
1832 {
1833 (void)op_delete(oap);
1834 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1835 u_save_cursor(); /* cursor line wasn't saved yet */
1836 auto_format(FALSE, TRUE);
1837 }
1838 break;
1839
1840 case OP_YANK:
1841 if (empty_region_error)
1842 {
1843 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001844 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001845 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001846 CancelRedo();
1847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 }
1849 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001850 {
1851#ifdef FEAT_LINEBREAK
1852 curwin->w_p_lbr = lbr_saved;
1853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 (void)op_yank(oap, FALSE, !gui_yank);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 check_cursor_col();
1857 break;
1858
1859 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001862 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001863 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001864 CancelRedo();
1865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 else
1867 {
1868 /* This is a new edit command, not a restart. Need to
1869 * remember it to make 'insertmode' work with mappings for
1870 * Visual mode. But do this only once and not when typed and
1871 * 'insertmode' isn't set. */
1872 if (p_im || !KeyTyped)
1873 restart_edit_save = restart_edit;
1874 else
1875 restart_edit_save = 0;
1876 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001877#ifdef FEAT_LINEBREAK
1878 /* Restore linebreak, so that when the user edits it looks as
1879 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001880 if (curwin->w_p_lbr != lbr_saved)
1881 {
1882 curwin->w_p_lbr = lbr_saved;
1883 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1884 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 /* Reset finish_op now, don't want it set inside edit(). */
1887 finish_op = FALSE;
1888 if (op_change(oap)) /* will call edit() */
1889 cap->retval |= CA_COMMAND_BUSY;
1890 if (restart_edit == 0)
1891 restart_edit = restart_edit_save;
1892 }
1893 break;
1894
1895 case OP_FILTER:
1896 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1897 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1898 else
1899 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001900 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901
1902 case OP_INDENT:
1903 case OP_COLON:
1904
1905#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1906 /*
1907 * If 'equalprg' is empty, do the indenting internally.
1908 */
1909 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1910 {
1911# ifdef FEAT_LISP
1912 if (curbuf->b_p_lisp)
1913 {
1914 op_reindent(oap, get_lisp_indent);
1915 break;
1916 }
1917# endif
1918# ifdef FEAT_CINDENT
1919 op_reindent(oap,
1920# ifdef FEAT_EVAL
1921 *curbuf->b_p_inde != NUL ? get_expr_indent :
1922# endif
1923 get_c_indent);
1924 break;
1925# endif
1926 }
1927#endif
1928
1929 op_colon(oap);
1930 break;
1931
1932 case OP_TILDE:
1933 case OP_UPPER:
1934 case OP_LOWER:
1935 case OP_ROT13:
1936 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001937 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001938 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001939 CancelRedo();
1940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 else
1942 op_tilde(oap);
1943 check_cursor_col();
1944 break;
1945
1946 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001947#if defined(FEAT_EVAL)
1948 if (*curbuf->b_p_fex != NUL)
1949 op_formatexpr(oap); /* use expression */
1950 else
1951#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01001952 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 op_colon(oap); /* use external command */
1954 else
1955 op_format(oap, FALSE); /* use internal function */
1956 break;
1957
1958 case OP_FORMAT2:
1959 op_format(oap, TRUE); /* use internal function */
1960 break;
1961
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001962 case OP_FUNCTION:
Bram Moolenaar4a08b0d2016-11-05 21:55:13 +01001963#ifdef FEAT_LINEBREAK
1964 /* Restore linebreak, so that when the user edits it looks as
1965 * before. */
1966 curwin->w_p_lbr = lbr_saved;
1967#endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001968 op_function(oap); /* call 'operatorfunc' */
1969 break;
1970
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 case OP_INSERT:
1972 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001975 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001976 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001977 CancelRedo();
1978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 else
1980 {
1981 /* This is a new edit command, not a restart. Need to
1982 * remember it to make 'insertmode' work with mappings for
1983 * Visual mode. But do this only once. */
1984 restart_edit_save = restart_edit;
1985 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001986#ifdef FEAT_LINEBREAK
1987 /* Restore linebreak, so that when the user edits it looks as
1988 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001989 if (curwin->w_p_lbr != lbr_saved)
1990 {
1991 curwin->w_p_lbr = lbr_saved;
1992 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1993 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 op_insert(oap, cap->count1);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001996#ifdef FEAT_LINEBREAK
1997 /* Reset linebreak, so that formatting works correctly. */
1998 curwin->w_p_lbr = FALSE;
1999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000
2001 /* TODO: when inserting in several lines, should format all
2002 * the lines. */
2003 auto_format(FALSE, TRUE);
2004
2005 if (restart_edit == 0)
2006 restart_edit = restart_edit_save;
Bram Moolenaar0b5c93a2017-02-01 15:03:30 +01002007 else
2008 cap->retval |= CA_COMMAND_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 break;
2011
2012 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002015 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002016 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002017 CancelRedo();
2018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002020 {
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002021#ifdef FEAT_LINEBREAK
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002022 /* Restore linebreak, so that when the user edits it looks as
2023 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002024 if (curwin->w_p_lbr != lbr_saved)
2025 {
2026 curwin->w_p_lbr = lbr_saved;
2027 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2028 }
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 op_replace(oap, cap->nchar);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 break;
2033
2034#ifdef FEAT_FOLDING
2035 case OP_FOLD:
2036 VIsual_reselect = FALSE; /* don't reselect now */
2037 foldCreate(oap->start.lnum, oap->end.lnum);
2038 break;
2039
2040 case OP_FOLDOPEN:
2041 case OP_FOLDOPENREC:
2042 case OP_FOLDCLOSE:
2043 case OP_FOLDCLOSEREC:
2044 VIsual_reselect = FALSE; /* don't reselect now */
2045 opFoldRange(oap->start.lnum, oap->end.lnum,
2046 oap->op_type == OP_FOLDOPEN
2047 || oap->op_type == OP_FOLDOPENREC,
2048 oap->op_type == OP_FOLDOPENREC
2049 || oap->op_type == OP_FOLDCLOSEREC,
2050 oap->is_VIsual);
2051 break;
2052
2053 case OP_FOLDDEL:
2054 case OP_FOLDDELREC:
2055 VIsual_reselect = FALSE; /* don't reselect now */
2056 deleteFold(oap->start.lnum, oap->end.lnum,
2057 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2058 break;
2059#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002060 case OP_NR_ADD:
2061 case OP_NR_SUB:
2062 if (empty_region_error)
2063 {
2064 vim_beep(BO_OPER);
2065 CancelRedo();
2066 }
2067 else
2068 {
2069 VIsual_active = TRUE;
2070#ifdef FEAT_LINEBREAK
2071 curwin->w_p_lbr = lbr_saved;
2072#endif
2073 op_addsub(oap, cap->count1, redo_VIsual_arg);
2074 VIsual_active = FALSE;
2075 }
2076 check_cursor_col();
2077 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 default:
2079 clearopbeep(oap);
2080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 virtual_op = MAYBE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 if (!gui_yank)
2083 {
2084 /*
2085 * if 'sol' not set, go back to old column for some commands
2086 */
2087 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2088 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2089 || oap->op_type == OP_DELETE))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002090 {
2091#ifdef FEAT_LINEBREAK
2092 curwin->w_p_lbr = FALSE;
2093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 coladvance(curwin->w_curswant = old_col);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 }
2097 else
2098 {
2099 curwin->w_cursor = old_cursor;
2100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 clearop(oap);
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01002103 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 }
Bram Moolenaar404406a2014-10-09 13:24:43 +02002105#ifdef FEAT_LINEBREAK
2106 curwin->w_p_lbr = lbr_saved;
2107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108}
2109
2110/*
2111 * Handle indent and format operators and visual mode ":".
2112 */
2113 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002114op_colon(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115{
2116 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 if (oap->is_VIsual)
2118 stuffReadbuff((char_u *)"'<,'>");
2119 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 {
2121 /*
2122 * Make the range look nice, so it can be repeated.
2123 */
2124 if (oap->start.lnum == curwin->w_cursor.lnum)
2125 stuffcharReadbuff('.');
2126 else
2127 stuffnumReadbuff((long)oap->start.lnum);
2128 if (oap->end.lnum != oap->start.lnum)
2129 {
2130 stuffcharReadbuff(',');
2131 if (oap->end.lnum == curwin->w_cursor.lnum)
2132 stuffcharReadbuff('.');
2133 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2134 stuffcharReadbuff('$');
2135 else if (oap->start.lnum == curwin->w_cursor.lnum)
2136 {
2137 stuffReadbuff((char_u *)".+");
2138 stuffnumReadbuff((long)oap->line_count - 1);
2139 }
2140 else
2141 stuffnumReadbuff((long)oap->end.lnum);
2142 }
2143 }
2144 if (oap->op_type != OP_COLON)
2145 stuffReadbuff((char_u *)"!");
2146 if (oap->op_type == OP_INDENT)
2147 {
2148#ifndef FEAT_CINDENT
2149 if (*get_equalprg() == NUL)
2150 stuffReadbuff((char_u *)"indent");
2151 else
2152#endif
2153 stuffReadbuff(get_equalprg());
2154 stuffReadbuff((char_u *)"\n");
2155 }
2156 else if (oap->op_type == OP_FORMAT)
2157 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002158 if (*curbuf->b_p_fp != NUL)
2159 stuffReadbuff(curbuf->b_p_fp);
2160 else if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 stuffReadbuff(p_fp);
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002162 else
2163 stuffReadbuff((char_u *)"fmt");
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002164 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 }
2166
2167 /*
2168 * do_cmdline() does the rest
2169 */
2170}
2171
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002172/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002173 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002174 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002175 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002176op_function(oparg_T *oap UNUSED)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002177{
2178#ifdef FEAT_EVAL
Bram Moolenaarffa96842018-06-12 22:05:14 +02002179 typval_T argv[2];
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002180 int save_virtual_op = virtual_op;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002181
2182 if (*p_opfunc == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002183 emsg(_("E774: 'operatorfunc' is empty"));
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002184 else
2185 {
2186 /* Set '[ and '] marks to text to be operated on. */
2187 curbuf->b_op_start = oap->start;
2188 curbuf->b_op_end = oap->end;
2189 if (oap->motion_type != MLINE && !oap->inclusive)
2190 /* Exclude the end position. */
2191 decl(&curbuf->b_op_end);
2192
Bram Moolenaarffa96842018-06-12 22:05:14 +02002193 argv[0].v_type = VAR_STRING;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002194 if (oap->block_mode)
Bram Moolenaarffa96842018-06-12 22:05:14 +02002195 argv[0].vval.v_string = (char_u *)"block";
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002196 else if (oap->motion_type == MLINE)
Bram Moolenaarffa96842018-06-12 22:05:14 +02002197 argv[0].vval.v_string = (char_u *)"line";
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002198 else
Bram Moolenaarffa96842018-06-12 22:05:14 +02002199 argv[0].vval.v_string = (char_u *)"char";
2200 argv[1].v_type = VAR_UNKNOWN;
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002201
2202 /* Reset virtual_op so that 'virtualedit' can be changed in the
2203 * function. */
2204 virtual_op = MAYBE;
2205
Bram Moolenaarded27a12018-08-01 19:06:03 +02002206 (void)call_func_retnr(p_opfunc, 1, argv);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002207
2208 virtual_op = save_virtual_op;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002209 }
2210#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002211 emsg(_("E775: Eval feature not available"));
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002212#endif
2213}
2214
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215#if defined(FEAT_MOUSE) || defined(PROTO)
2216/*
2217 * Do the appropriate action for the current mouse click in the current mode.
2218 * Not used for Command-line mode.
2219 *
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002220 * Normal and Visual Mode:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 * event modi- position visual change action
2222 * fier cursor window
2223 * left press - yes end yes
2224 * left press C yes end yes "^]" (2)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002225 * left press S yes end (popup: extend) yes "*" (2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 * left drag - yes start if moved no
2227 * left relse - yes start if moved no
2228 * middle press - yes if not active no put register
2229 * middle press - yes if active no yank and put
2230 * right press - yes start or extend yes
2231 * right press S yes no change yes "#" (2)
2232 * right drag - yes extend no
2233 * right relse - yes extend no
2234 *
2235 * Insert or Replace Mode:
2236 * event modi- position visual change action
2237 * fier cursor window
2238 * left press - yes (cannot be active) yes
2239 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2240 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2241 * left drag - yes start or extend (1) no CTRL-O (1)
2242 * left relse - yes start or extend (1) no CTRL-O (1)
2243 * middle press - no (cannot be active) no put register
2244 * right press - yes start or extend yes CTRL-O
2245 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2246 *
2247 * (1) only if mouse pointer moved since press
2248 * (2) only if click is in same buffer
2249 *
2250 * Return TRUE if start_arrow() should be called for edit mode.
2251 */
2252 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002253do_mouse(
2254 oparg_T *oap, /* operator argument, can be NULL */
2255 int c, /* K_LEFTMOUSE, etc */
2256 int dir, /* Direction to 'put' if necessary */
2257 long count,
2258 int fixindent) /* PUT_FIXINDENT if fixing indent necessary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259{
2260 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2261 static int got_click = FALSE; /* got a click some time back */
2262
2263 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2264 int is_click; /* If FALSE it's a drag or release event */
2265 int is_drag; /* If TRUE it's a drag event */
2266 int jump_flags = 0; /* flags for jump_to_mouse() */
2267 pos_T start_visual;
2268 int moved; /* Has cursor moved? */
2269 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002270 static int in_tab_line = FALSE; /* mouse clicked in tab line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 int in_sep_line; /* mouse in vertical separator line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 int c1, c2;
2273#if defined(FEAT_FOLDING)
2274 pos_T save_cursor;
2275#endif
2276 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 static pos_T orig_cursor;
2278 colnr_T leftcol, rightcol;
2279 pos_T end_visual;
2280 int diff;
2281 int old_active = VIsual_active;
2282 int old_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 int regname;
2284
2285#if defined(FEAT_FOLDING)
2286 save_cursor = curwin->w_cursor;
2287#endif
2288
2289 /*
2290 * When GUI is active, always recognize mouse events, otherwise:
2291 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2292 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2293 * - For command line and insert mode 'mouse' is checked before calling
2294 * do_mouse().
2295 */
2296 if (do_always)
2297 do_always = FALSE;
2298 else
2299#ifdef FEAT_GUI
2300 if (!gui.in_use)
2301#endif
2302 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 if (VIsual_active)
2304 {
2305 if (!mouse_has(MOUSE_VISUAL))
2306 return FALSE;
2307 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002308 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 return FALSE;
2310 }
2311
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002312 for (;;)
2313 {
2314 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2315 if (is_drag)
2316 {
2317 /* If the next character is the same mouse event then use that
2318 * one. Speeds up dragging the status line. */
2319 if (vpeekc() != NUL)
2320 {
2321 int nc;
2322 int save_mouse_row = mouse_row;
2323 int save_mouse_col = mouse_col;
2324
2325 /* Need to get the character, peeking doesn't get the actual
2326 * one. */
2327 nc = safe_vgetc();
2328 if (c == nc)
2329 continue;
2330 vungetc(nc);
2331 mouse_row = save_mouse_row;
2332 mouse_col = save_mouse_col;
2333 }
2334 }
2335 break;
2336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002338 if (c == K_MOUSEMOVE)
2339 {
2340 /* Mouse moved without a button pressed. */
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002341#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002342 ui_may_remove_balloon();
2343 if (p_bevalterm && !VIsual_active)
2344 {
2345 profile_setlimit(p_bdlay, &bevalexpr_due);
2346 bevalexpr_due_set = TRUE;
2347 }
2348#endif
2349 return FALSE;
2350 }
2351
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352#ifdef FEAT_MOUSESHAPE
2353 /* May have stopped dragging the status or separator line. The pointer is
2354 * most likely still on the status or separator line. */
2355 if (!is_drag && drag_status_line)
2356 {
2357 drag_status_line = FALSE;
2358 update_mouseshape(SHAPE_IDX_STATUS);
2359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 if (!is_drag && drag_sep_line)
2361 {
2362 drag_sep_line = FALSE;
2363 update_mouseshape(SHAPE_IDX_VSEP);
2364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365#endif
2366
2367 /*
2368 * Ignore drag and release events if we didn't get a click.
2369 */
2370 if (is_click)
2371 got_click = TRUE;
2372 else
2373 {
2374 if (!got_click) /* didn't get click, ignore */
2375 return FALSE;
2376 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002377 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002379 if (in_tab_line)
2380 {
2381 in_tab_line = FALSE;
2382 return FALSE;
2383 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 }
2386
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 /*
2388 * CTRL right mouse button does CTRL-T
2389 */
2390 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2391 {
2392 if (State & INSERT)
2393 stuffcharReadbuff(Ctrl_O);
2394 if (count > 1)
2395 stuffnumReadbuff(count);
2396 stuffcharReadbuff(Ctrl_T);
2397 got_click = FALSE; /* ignore drag&release now */
2398 return FALSE;
2399 }
2400
2401 /*
2402 * CTRL only works with left mouse button
2403 */
2404 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2405 return FALSE;
2406
2407 /*
2408 * When a modifier is down, ignore drag and release events, as well as
2409 * multiple clicks and the middle mouse button.
2410 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2411 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002412 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2413 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 && (!is_click
2415 || (mod_mask & MOD_MASK_MULTI_CLICK)
2416 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002417 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 && mouse_model_popup()
2419 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002420 && !((mod_mask & MOD_MASK_ALT)
2421 && !mouse_model_popup()
2422 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 )
2424 return FALSE;
2425
2426 /*
2427 * If the button press was used as the movement command for an operator
2428 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2429 * drag/release events.
2430 */
2431 if (!is_click && which_button == MOUSE_MIDDLE)
2432 return FALSE;
2433
2434 if (oap != NULL)
2435 regname = oap->regname;
2436 else
2437 regname = 0;
2438
2439 /*
2440 * Middle mouse button does a 'put' of the selected text
2441 */
2442 if (which_button == MOUSE_MIDDLE)
2443 {
2444 if (State == NORMAL)
2445 {
2446 /*
2447 * If an operator was pending, we don't know what the user wanted
2448 * to do. Go back to normal mode: Clear the operator and beep().
2449 */
2450 if (oap != NULL && oap->op_type != OP_NOP)
2451 {
2452 clearopbeep(oap);
2453 return FALSE;
2454 }
2455
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 /*
2457 * If visual was active, yank the highlighted text and put it
2458 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002459 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 */
2461 if (VIsual_active)
2462 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002463 if (VIsual_select)
2464 {
2465 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002466 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002467 }
2468 else
2469 {
2470 stuffcharReadbuff('y');
2471 stuffcharReadbuff(K_MIDDLEMOUSE);
2472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 do_always = TRUE; /* ignore 'mouse' setting next time */
2474 return FALSE;
2475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 /*
2477 * The rest is below jump_to_mouse()
2478 */
2479 }
2480
2481 else if ((State & INSERT) == 0)
2482 return FALSE;
2483
2484 /*
2485 * Middle click in insert mode doesn't move the mouse, just insert the
2486 * contents of a register. '.' register is special, can't insert that
2487 * with do_put().
2488 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2489 * happens for the GUI).
2490 */
2491 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2492 {
2493 if (regname == '.')
2494 insert_reg(regname, TRUE);
2495 else
2496 {
2497#ifdef FEAT_CLIPBOARD
2498 if (clip_star.available && regname == 0)
2499 regname = '*';
2500#endif
2501 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2502 insert_reg(regname, TRUE);
2503 else
2504 {
2505 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2506
2507 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2508 AppendCharToRedobuff(Ctrl_R);
2509 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2510 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2511 }
2512 }
2513 return FALSE;
2514 }
2515 }
2516
2517 /* When dragging or button-up stay in the same window. */
2518 if (!is_click)
2519 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2520
2521 start_visual.lnum = 0;
2522
Bram Moolenaarf740b292006-02-16 22:11:02 +00002523 /* Check for clicking in the tab page line. */
2524 if (mouse_row == 0 && firstwin->w_winrow > 0)
2525 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002526 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002527 {
2528 if (in_tab_line)
2529 {
2530 c1 = TabPageIdxs[mouse_col];
Bram Moolenaar4a4b8212015-09-08 17:50:41 +02002531 tabpage_move(c1 <= 0 ? 9999 : c1 < tabpage_index(curtab)
2532 ? c1 - 1 : c1);
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002533 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002534 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002535 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002536
Bram Moolenaarf740b292006-02-16 22:11:02 +00002537 /* click in a tab selects that tab page */
2538 if (is_click
2539# ifdef FEAT_CMDWIN
2540 && cmdwin_type == 0
2541# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002542 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002543 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002544 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002545 c1 = TabPageIdxs[mouse_col];
2546 if (c1 >= 0)
2547 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002548 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2549 {
2550 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002551 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002552 tabpage_new();
2553 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2554 }
2555 else
2556 {
2557 /* Go to specified tab page, or next one if not clicking
2558 * on a label. */
2559 goto_tabpage(c1);
2560
2561 /* It's like clicking on the status line of a window. */
2562 if (curwin != old_curwin)
2563 end_visual_mode();
2564 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002565 }
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002566 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002567 {
2568 tabpage_T *tp;
2569
2570 /* Close the current or specified tab page. */
2571 if (c1 == -999)
2572 tp = curtab;
2573 else
2574 tp = find_tabpage(-c1);
2575 if (tp == curtab)
2576 {
2577 if (first_tabpage->tp_next != NULL)
2578 tabpage_close(FALSE);
2579 }
2580 else if (tp != NULL)
2581 tabpage_close_other(tp, FALSE);
2582 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002583 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002584 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002585 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002586 else if (is_drag && in_tab_line)
2587 {
2588 c1 = TabPageIdxs[mouse_col];
2589 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2590 return FALSE;
2591 }
2592
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 /*
2594 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2595 * right button up -> pop-up menu
2596 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002597 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 */
2599 if (mouse_model_popup())
2600 {
2601 if (which_button == MOUSE_RIGHT
2602 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2603 {
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002604#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002606 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON) \
2607 || defined(FEAT_TERM_POPUP_MENU)
2608# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 if (gui.in_use)
2610 {
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002611# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2612 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2613 if (!is_click)
2614 /* Ignore right button release events, only shows the popup
2615 * menu on the button down event. */
2616 return FALSE;
2617# endif
2618# if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2619 if (is_click || is_drag)
2620 /* Ignore right button down and drag mouse events. Windows
2621 * only shows the popup menu on the button up event. */
2622 return FALSE;
2623# endif
2624 }
2625# endif
2626# if defined(FEAT_GUI) && defined(FEAT_TERM_POPUP_MENU)
2627 else
2628# endif
2629# if defined(FEAT_TERM_POPUP_MENU)
2630 if (!is_click)
2631 /* Ignore right button release events, only shows the popup
2632 * menu on the button down event. */
2633 return FALSE;
2634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002636 jump_flags = 0;
2637 if (STRCMP(p_mousem, "popup_setpos") == 0)
2638 {
2639 /* First set the cursor position before showing the popup
2640 * menu. */
2641 if (VIsual_active)
2642 {
2643 pos_T m_pos;
2644
2645 /*
2646 * set MOUSE_MAY_STOP_VIS if we are outside the
2647 * selection or the current window (might have false
2648 * negative here)
2649 */
2650 if (mouse_row < curwin->w_winrow
2651 || mouse_row
2652 > (curwin->w_winrow + curwin->w_height))
2653 jump_flags = MOUSE_MAY_STOP_VIS;
2654 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2655 jump_flags = MOUSE_MAY_STOP_VIS;
2656 else
2657 {
2658 if ((LT_POS(curwin->w_cursor, VIsual)
2659 && (LT_POS(m_pos, curwin->w_cursor)
2660 || LT_POS(VIsual, m_pos)))
2661 || (LT_POS(VIsual, curwin->w_cursor)
2662 && (LT_POS(m_pos, VIsual)
2663 || LT_POS(curwin->w_cursor, m_pos))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 {
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002665 jump_flags = MOUSE_MAY_STOP_VIS;
2666 }
2667 else if (VIsual_mode == Ctrl_V)
2668 {
2669 getvcols(curwin, &curwin->w_cursor, &VIsual,
2670 &leftcol, &rightcol);
2671 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2672 if (m_pos.col < leftcol || m_pos.col > rightcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 }
2675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002677 else
2678 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002680 if (jump_flags)
2681 {
2682 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2683 update_curbuf(VIsual_active ? INVERTED : VALID);
2684 setcursor();
2685 out_flush(); /* Update before showing popup menu */
2686 }
2687# ifdef FEAT_MENU
2688 show_popupmenu();
2689 got_click = FALSE; /* ignore release events */
2690# endif
2691 return (jump_flags & CURSOR_MOVED) != 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692#else
2693 return FALSE;
2694#endif
2695 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002696 if (which_button == MOUSE_LEFT
2697 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 {
2699 which_button = MOUSE_RIGHT;
2700 mod_mask &= ~MOD_MASK_SHIFT;
2701 }
2702 }
2703
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 if ((State & (NORMAL | INSERT))
2705 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2706 {
2707 if (which_button == MOUSE_LEFT)
2708 {
2709 if (is_click)
2710 {
2711 /* stop Visual mode for a left click in a window, but not when
2712 * on a status line */
2713 if (VIsual_active)
2714 jump_flags |= MOUSE_MAY_STOP_VIS;
2715 }
2716 else if (mouse_has(MOUSE_VISUAL))
2717 jump_flags |= MOUSE_MAY_VIS;
2718 }
2719 else if (which_button == MOUSE_RIGHT)
2720 {
2721 if (is_click && VIsual_active)
2722 {
2723 /*
2724 * Remember the start and end of visual before moving the
2725 * cursor.
2726 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002727 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 {
2729 start_visual = curwin->w_cursor;
2730 end_visual = VIsual;
2731 }
2732 else
2733 {
2734 start_visual = VIsual;
2735 end_visual = curwin->w_cursor;
2736 }
2737 }
2738 jump_flags |= MOUSE_FOCUS;
2739 if (mouse_has(MOUSE_VISUAL))
2740 jump_flags |= MOUSE_MAY_VIS;
2741 }
2742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743
2744 /*
2745 * If an operator is pending, ignore all drags and releases until the
2746 * next mouse click.
2747 */
2748 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2749 {
2750 got_click = FALSE;
2751 oap->motion_type = MCHAR;
2752 }
2753
2754 /* When releasing the button let jump_to_mouse() know. */
2755 if (!is_click && !is_drag)
2756 jump_flags |= MOUSE_RELEASED;
2757
2758 /*
2759 * JUMP!
2760 */
2761 jump_flags = jump_to_mouse(jump_flags,
2762 oap == NULL ? NULL : &(oap->inclusive), which_button);
Bram Moolenaareb163d72017-09-23 15:08:17 +02002763
2764#ifdef FEAT_MENU
2765 /* A click in the window toolbar has no side effects. */
2766 if (jump_flags & MOUSE_WINBAR)
2767 return FALSE;
2768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 moved = (jump_flags & CURSOR_MOVED);
2770 in_status_line = (jump_flags & IN_STATUS_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 in_sep_line = (jump_flags & IN_SEP_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772
2773#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002774 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2776 {
2777 int key = KEY2TERMCAP1(c);
2778
2779 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2780 || key == (int)KE_RIGHTRELEASE)
2781 netbeans_button_release(which_button);
2782 }
2783#endif
2784
2785 /* When jumping to another window, clear a pending operator. That's a bit
2786 * friendlier than beeping and not jumping to that window. */
2787 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2788 clearop(oap);
2789
2790#ifdef FEAT_FOLDING
2791 if (mod_mask == 0
2792 && !is_drag
2793 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2794 && which_button == MOUSE_LEFT)
2795 {
2796 /* open or close a fold at this line */
2797 if (jump_flags & MOUSE_FOLD_OPEN)
2798 openFold(curwin->w_cursor.lnum, 1L);
2799 else
2800 closeFold(curwin->w_cursor.lnum, 1L);
2801 /* don't move the cursor if still in the same window */
2802 if (curwin == old_curwin)
2803 curwin->w_cursor = save_cursor;
2804 }
2805#endif
2806
2807#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2808 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2809 {
2810 clip_modeless(which_button, is_click, is_drag);
2811 return FALSE;
2812 }
2813#endif
2814
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002816 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 if (VIsual_active && is_drag && p_so)
2818 {
2819 /* In the very first line, allow scrolling one line */
2820 if (mouse_row == 0)
2821 mouse_dragging = 2;
2822 else
2823 mouse_dragging = 1;
2824 }
2825
2826 /* When dragging the mouse above the window, scroll down. */
2827 if (is_drag && mouse_row < 0 && !in_status_line)
2828 {
2829 scroll_redraw(FALSE, 1L);
2830 mouse_row = 0;
2831 }
2832
2833 if (start_visual.lnum) /* right click in visual mode */
2834 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002835 /* When ALT is pressed make Visual mode blockwise. */
2836 if (mod_mask & MOD_MASK_ALT)
2837 VIsual_mode = Ctrl_V;
2838
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 /*
2840 * In Visual-block mode, divide the area in four, pick up the corner
2841 * that is in the quarter that the cursor is in.
2842 */
2843 if (VIsual_mode == Ctrl_V)
2844 {
2845 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2846 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2847 end_visual.col = leftcol;
2848 else
2849 end_visual.col = rightcol;
Bram Moolenaarcde88542015-08-11 19:14:00 +02002850 if (curwin->w_cursor.lnum >=
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 (start_visual.lnum + end_visual.lnum) / 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 end_visual.lnum = start_visual.lnum;
2853
2854 /* move VIsual to the right column */
2855 start_visual = curwin->w_cursor; /* save the cursor pos */
2856 curwin->w_cursor = end_visual;
2857 coladvance(end_visual.col);
2858 VIsual = curwin->w_cursor;
2859 curwin->w_cursor = start_visual; /* restore the cursor */
2860 }
2861 else
2862 {
2863 /*
2864 * If the click is before the start of visual, change the start.
2865 * If the click is after the end of visual, change the end. If
2866 * the click is inside the visual, change the closest side.
2867 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002868 if (LT_POS(curwin->w_cursor, start_visual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 VIsual = end_visual;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002870 else if (LT_POS(end_visual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 VIsual = start_visual;
2872 else
2873 {
2874 /* In the same line, compare column number */
2875 if (end_visual.lnum == start_visual.lnum)
2876 {
2877 if (curwin->w_cursor.col - start_visual.col >
2878 end_visual.col - curwin->w_cursor.col)
2879 VIsual = start_visual;
2880 else
2881 VIsual = end_visual;
2882 }
2883
2884 /* In different lines, compare line number */
2885 else
2886 {
2887 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2888 (end_visual.lnum - curwin->w_cursor.lnum);
2889
2890 if (diff > 0) /* closest to end */
2891 VIsual = start_visual;
2892 else if (diff < 0) /* closest to start */
2893 VIsual = end_visual;
2894 else /* in the middle line */
2895 {
2896 if (curwin->w_cursor.col <
2897 (start_visual.col + end_visual.col) / 2)
2898 VIsual = end_visual;
2899 else
2900 VIsual = start_visual;
2901 }
2902 }
2903 }
2904 }
2905 }
2906 /*
2907 * If Visual mode started in insert mode, execute "CTRL-O"
2908 */
2909 else if ((State & INSERT) && VIsual_active)
2910 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911
2912 /*
2913 * Middle mouse click: Put text before cursor.
2914 */
2915 if (which_button == MOUSE_MIDDLE)
2916 {
2917#ifdef FEAT_CLIPBOARD
2918 if (clip_star.available && regname == 0)
2919 regname = '*';
2920#endif
2921 if (yank_register_mline(regname))
2922 {
2923 if (mouse_past_bottom)
2924 dir = FORWARD;
2925 }
2926 else if (mouse_past_eol)
2927 dir = FORWARD;
2928
2929 if (fixindent)
2930 {
2931 c1 = (dir == BACKWARD) ? '[' : ']';
2932 c2 = 'p';
2933 }
2934 else
2935 {
2936 c1 = (dir == FORWARD) ? 'p' : 'P';
2937 c2 = NUL;
2938 }
2939 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2940
2941 /*
2942 * Remember where the paste started, so in edit() Insstart can be set
2943 * to this position
2944 */
2945 if (restart_edit != 0)
2946 where_paste_started = curwin->w_cursor;
2947 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2948 }
2949
Bram Moolenaar4033c552017-09-16 20:54:51 +02002950#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 /*
2952 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2953 * error under the mouse pointer.
2954 */
2955 else if (((mod_mask & MOD_MASK_CTRL)
2956 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2957 && bt_quickfix(curbuf))
2958 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002959 if (curwin->w_llist_ref == NULL) /* quickfix window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01002960 do_cmdline_cmd((char_u *)".cc");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002961 else /* location list window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01002962 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 got_click = FALSE; /* ignore drag&release now */
2964 }
2965#endif
2966
2967 /*
2968 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2969 * under the mouse pointer.
2970 */
2971 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2972 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2973 {
2974 if (State & INSERT)
2975 stuffcharReadbuff(Ctrl_O);
2976 stuffcharReadbuff(Ctrl_RSB);
2977 got_click = FALSE; /* ignore drag&release now */
2978 }
2979
2980 /*
2981 * Shift-Mouse click searches for the next occurrence of the word under
2982 * the mouse pointer
2983 */
2984 else if ((mod_mask & MOD_MASK_SHIFT))
2985 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002986 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 stuffcharReadbuff(Ctrl_O);
2988 if (which_button == MOUSE_LEFT)
2989 stuffcharReadbuff('*');
2990 else /* MOUSE_RIGHT */
2991 stuffcharReadbuff('#');
2992 }
2993
2994 /* Handle double clicks, unless on status line */
2995 else if (in_status_line)
2996 {
2997#ifdef FEAT_MOUSESHAPE
2998 if ((is_drag || is_click) && !drag_status_line)
2999 {
3000 drag_status_line = TRUE;
3001 update_mouseshape(-1);
3002 }
3003#endif
3004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 else if (in_sep_line)
3006 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003007#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 if ((is_drag || is_click) && !drag_sep_line)
3009 {
3010 drag_sep_line = TRUE;
3011 update_mouseshape(-1);
3012 }
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003013#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3016 && mouse_has(MOUSE_VISUAL))
3017 {
3018 if (is_click || !VIsual_active)
3019 {
3020 if (VIsual_active)
3021 orig_cursor = VIsual;
3022 else
3023 {
3024 check_visual_highlight();
3025 VIsual = curwin->w_cursor;
3026 orig_cursor = VIsual;
3027 VIsual_active = TRUE;
3028 VIsual_reselect = TRUE;
3029 /* start Select mode if 'selectmode' contains "mouse" */
3030 may_start_select('o');
3031 setmouse();
3032 }
3033 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003034 {
3035 /* Double click with ALT pressed makes it blockwise. */
3036 if (mod_mask & MOD_MASK_ALT)
3037 VIsual_mode = Ctrl_V;
3038 else
3039 VIsual_mode = 'v';
3040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3042 VIsual_mode = 'V';
3043 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3044 VIsual_mode = Ctrl_V;
3045#ifdef FEAT_CLIPBOARD
3046 /* Make sure the clipboard gets updated. Needed because start and
3047 * end may still be the same, and the selection needs to be owned */
3048 clip_star.vmode = NUL;
3049#endif
3050 }
3051 /*
3052 * A double click selects a word or a block.
3053 */
3054 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3055 {
3056 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003057 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058
3059 if (is_click)
3060 {
3061 /* If the character under the cursor (skipping white space) is
3062 * not a word character, try finding a match and select a (),
3063 * {}, [], #if/#endif, etc. block. */
3064 end_visual = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01003065 while (gc = gchar_pos(&end_visual), VIM_ISWHITE(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 inc(&end_visual);
3067 if (oap != NULL)
3068 oap->motion_type = MCHAR;
3069 if (oap != NULL
3070 && VIsual_mode == 'v'
3071 && !vim_iswordc(gchar_pos(&end_visual))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003072 && EQUAL_POS(curwin->w_cursor, VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 && (pos = findmatch(oap, NUL)) != NULL)
3074 {
3075 curwin->w_cursor = *pos;
3076 if (oap->motion_type == MLINE)
3077 VIsual_mode = 'V';
3078 else if (*p_sel == 'e')
3079 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003080 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 ++VIsual.col;
3082 else
3083 ++curwin->w_cursor.col;
3084 }
3085 }
3086 }
3087
3088 if (pos == NULL && (is_click || is_drag))
3089 {
3090 /* When not found a match or when dragging: extend to include
3091 * a word. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003092 if (LT_POS(curwin->w_cursor, orig_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 {
3094 find_start_of_word(&curwin->w_cursor);
3095 find_end_of_word(&VIsual);
3096 }
3097 else
3098 {
3099 find_start_of_word(&VIsual);
3100 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003102 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 find_end_of_word(&curwin->w_cursor);
3104 }
3105 }
3106 curwin->w_set_curswant = TRUE;
3107 }
3108 if (is_click)
3109 redraw_curbuf_later(INVERTED); /* update the inversion */
3110 }
3111 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003112 {
3113 if (mod_mask & MOD_MASK_ALT)
3114 VIsual_mode = Ctrl_V;
3115 else
3116 VIsual_mode = 'v';
3117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118
3119 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003120 if ((!VIsual_active && old_active && mode_displayed)
3121 || (VIsual_active && p_smd && msg_silent == 0
3122 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124
3125 return moved;
3126}
3127
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128/*
3129 * Move "pos" back to the start of the word it's in.
3130 */
3131 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003132find_start_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133{
3134 char_u *line;
3135 int cclass;
3136 int col;
3137
3138 line = ml_get(pos->lnum);
3139 cclass = get_mouse_class(line + pos->col);
3140
3141 while (pos->col > 0)
3142 {
3143 col = pos->col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 col -= (*mb_head_off)(line, line + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 if (get_mouse_class(line + col) != cclass)
3146 break;
3147 pos->col = col;
3148 }
3149}
3150
3151/*
3152 * Move "pos" forward to the end of the word it's in.
3153 * When 'selection' is "exclusive", the position is just after the word.
3154 */
3155 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003156find_end_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157{
3158 char_u *line;
3159 int cclass;
3160 int col;
3161
3162 line = ml_get(pos->lnum);
3163 if (*p_sel == 'e' && pos->col > 0)
3164 {
3165 --pos->col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 pos->col -= (*mb_head_off)(line, line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 }
3168 cclass = get_mouse_class(line + pos->col);
3169 while (line[pos->col] != NUL)
3170 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003171 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 if (get_mouse_class(line + col) != cclass)
3173 {
3174 if (*p_sel == 'e')
3175 pos->col = col;
3176 break;
3177 }
3178 pos->col = col;
3179 }
3180}
3181
3182/*
3183 * Get class of a character for selection: same class means same word.
3184 * 0: blank
3185 * 1: punctuation groups
3186 * 2: normal word character
3187 * >2: multi-byte word character.
3188 */
3189 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003190get_mouse_class(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191{
3192 int c;
3193
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3195 return mb_get_class(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196
3197 c = *p;
3198 if (c == ' ' || c == '\t')
3199 return 0;
3200
3201 if (vim_iswordc(c))
3202 return 2;
3203
3204 /*
3205 * There are a few special cases where we want certain combinations of
3206 * characters to be considered as a single word. These are things like
3207 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003208 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 */
3210 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3211 return 1;
3212 return c;
3213}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214#endif /* FEAT_MOUSE */
3215
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216/*
3217 * Check if highlighting for visual mode is possible, give a warning message
3218 * if not.
3219 */
3220 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003221check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222{
3223 static int did_check = FALSE;
3224
3225 if (full_screen)
3226 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003227 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003228 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 did_check = TRUE;
3230 }
3231}
3232
3233/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003234 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 * This function should ALWAYS be called to end Visual mode, except from
3236 * do_pending_operator().
3237 */
3238 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003239end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240{
3241#ifdef FEAT_CLIPBOARD
3242 /*
3243 * If we are using the clipboard, then remember what was selected in case
3244 * we need to paste it somewhere while we still own the selection.
3245 * Only do this when the clipboard is already owned. Don't want to grab
3246 * the selection when hitting ESC.
3247 */
3248 if (clip_star.available && clip_star.owned)
3249 clip_auto_select();
3250#endif
3251
3252 VIsual_active = FALSE;
3253#ifdef FEAT_MOUSE
3254 setmouse();
3255 mouse_dragging = 0;
3256#endif
3257
3258 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003259 curbuf->b_visual.vi_mode = VIsual_mode;
3260 curbuf->b_visual.vi_start = VIsual;
3261 curbuf->b_visual.vi_end = curwin->w_cursor;
3262 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263#ifdef FEAT_EVAL
3264 curbuf->b_visual_mode_eval = VIsual_mode;
3265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 if (!virtual_active())
3267 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003268 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003270 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271}
3272
3273/*
3274 * Reset VIsual_active and VIsual_reselect.
3275 */
3276 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003277reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278{
3279 if (VIsual_active)
3280 {
3281 end_visual_mode();
3282 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3283 }
3284 VIsual_reselect = FALSE;
3285}
3286
3287/*
3288 * Reset VIsual_active and VIsual_reselect if it's set.
3289 */
3290 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003291reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
3293 if (VIsual_active)
3294 {
3295 end_visual_mode();
3296 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3297 VIsual_reselect = FALSE;
3298 }
3299}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301/*
3302 * Check for a balloon-eval special item to include when searching for an
3303 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3304 * Returns TRUE if the character at "*ptr" should be included.
3305 * "dir" is FORWARD or BACKWARD, the direction of searching.
3306 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3307 * "bnp" points to a counter for square brackets.
3308 */
3309 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003310find_is_eval_item(
3311 char_u *ptr,
3312 int *colp,
3313 int *bnp,
3314 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315{
3316 /* Accept everything inside []. */
3317 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3318 ++*bnp;
3319 if (*bnp > 0)
3320 {
3321 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3322 --*bnp;
3323 return TRUE;
3324 }
3325
3326 /* skip over "s.var" */
3327 if (*ptr == '.')
3328 return TRUE;
3329
3330 /* two-character item: s->var */
3331 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3332 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3333 {
3334 *colp += dir;
3335 return TRUE;
3336 }
3337 return FALSE;
3338}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339
3340/*
3341 * Find the identifier under or to the right of the cursor.
3342 * "find_type" can have one of three values:
3343 * FIND_IDENT: find an identifier (keyword)
3344 * FIND_STRING: find any non-white string
3345 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003346 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 *
3348 * There are three steps:
3349 * 1. Search forward for the start of an identifier/string. Doesn't move if
3350 * already on one.
3351 * 2. Search backward for the start of this identifier/string.
3352 * This doesn't match the real Vi but I like it a little better and it
3353 * shouldn't bother anyone.
3354 * 3. Search forward to the end of this identifier/string.
3355 * When FIND_IDENT isn't defined, we backup until a blank.
3356 *
3357 * Returns the length of the string, or zero if no string is found.
3358 * If a string is found, a pointer to the string is put in "*string". This
3359 * string is not always NUL terminated.
3360 */
3361 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003362find_ident_under_cursor(char_u **string, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363{
3364 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3365 curwin->w_cursor.col, string, find_type);
3366}
3367
3368/*
3369 * Like find_ident_under_cursor(), but for any window and any position.
3370 * However: Uses 'iskeyword' from the current window!.
3371 */
3372 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003373find_ident_at_pos(
3374 win_T *wp,
3375 linenr_T lnum,
3376 colnr_T startcol,
3377 char_u **string,
3378 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379{
3380 char_u *ptr;
3381 int col = 0; /* init to shut up GCC */
3382 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 int this_class = 0;
3384 int prev_class;
3385 int prevcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 int bn = 0; /* bracket nesting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387
3388 /*
3389 * if i == 0: try to find an identifier
3390 * if i == 1: try to find any non-white string
3391 */
3392 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3393 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3394 {
3395 /*
3396 * 1. skip to start of identifier/string
3397 */
3398 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 if (has_mbyte)
3400 {
3401 while (ptr[col] != NUL)
3402 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 /* Stop at a ']' to evaluate "a[x]". */
3404 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3405 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 this_class = mb_get_class(ptr + col);
3407 if (this_class != 0 && (i == 1 || this_class != 1))
3408 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003409 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 }
3411 }
3412 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01003414 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 )
3417 ++col;
3418
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 /* When starting on a ']' count it, so that we include the '['. */
3420 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
3422 /*
3423 * 2. Back up to start of identifier/string.
3424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 if (has_mbyte)
3426 {
3427 /* Remember class of character under cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3429 this_class = mb_get_class((char_u *)"a");
3430 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003432 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 {
3434 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3435 prev_class = mb_get_class(ptr + prevcol);
3436 if (this_class != prev_class
3437 && (i == 0
3438 || prev_class == 0
3439 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 && (!(find_type & FIND_EVAL)
3441 || prevcol == 0
3442 || !find_is_eval_item(ptr + prevcol, &prevcol,
3443 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 )
3445 break;
3446 col = prevcol;
3447 }
3448
3449 /* If we don't want just any old string, or we've found an
3450 * identifier, stop searching. */
3451 if (this_class > 2)
3452 this_class = 2;
3453 if (!(find_type & FIND_STRING) || this_class == 2)
3454 break;
3455 }
3456 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 {
3458 while (col > 0
3459 && ((i == 0
3460 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003461 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 && (!(find_type & FIND_IDENT)
3463 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 || ((find_type & FIND_EVAL)
3465 && col > 1
3466 && find_is_eval_item(ptr + col - 1, &col,
3467 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 ))
3469 --col;
3470
3471 /* If we don't want just any old string, or we've found an
3472 * identifier, stop searching. */
3473 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3474 break;
3475 }
3476 }
3477
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003478 if (ptr[col] == NUL || (i == 0
3479 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 {
3481 /*
3482 * didn't find an identifier or string
3483 */
3484 if (find_type & FIND_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003485 emsg(_("E348: No string under cursor"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003487 emsg(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 return 0;
3489 }
3490 ptr += col;
3491 *string = ptr;
3492
3493 /*
3494 * 3. Find the end if the identifier/string.
3495 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 bn = 0;
3497 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 if (has_mbyte)
3500 {
3501 /* Search for point of changing multibyte character class. */
3502 this_class = mb_get_class(ptr);
3503 while (ptr[col] != NUL
3504 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3505 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 || ((find_type & FIND_EVAL)
3507 && col <= (int)startcol
3508 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003510 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 }
3512 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003514 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 || ((find_type & FIND_EVAL)
3516 && col <= (int)startcol
3517 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 )
3519 {
3520 ++col;
3521 }
3522
3523 return col;
3524}
3525
3526/*
3527 * Prepare for redo of a normal command.
3528 */
3529 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003530prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531{
3532 prep_redo(cap->oap->regname, cap->count0,
3533 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3534}
3535
3536/*
3537 * Prepare for redo of any command.
3538 * Note that only the last argument can be a multi-byte char.
3539 */
3540 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003541prep_redo(
3542 int regname,
3543 long num,
3544 int cmd1,
3545 int cmd2,
3546 int cmd3,
3547 int cmd4,
3548 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549{
3550 ResetRedobuff();
3551 if (regname != 0) /* yank from specified buffer */
3552 {
3553 AppendCharToRedobuff('"');
3554 AppendCharToRedobuff(regname);
3555 }
3556 if (num)
3557 AppendNumberToRedobuff(num);
3558
3559 if (cmd1 != NUL)
3560 AppendCharToRedobuff(cmd1);
3561 if (cmd2 != NUL)
3562 AppendCharToRedobuff(cmd2);
3563 if (cmd3 != NUL)
3564 AppendCharToRedobuff(cmd3);
3565 if (cmd4 != NUL)
3566 AppendCharToRedobuff(cmd4);
3567 if (cmd5 != NUL)
3568 AppendCharToRedobuff(cmd5);
3569}
3570
3571/*
3572 * check for operator active and clear it
3573 *
3574 * return TRUE if operator was active
3575 */
3576 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003577checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578{
3579 if (oap->op_type == OP_NOP)
3580 return FALSE;
3581 clearopbeep(oap);
3582 return TRUE;
3583}
3584
3585/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003586 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003588 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 */
3590 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003591checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003593 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 return FALSE;
3595 clearopbeep(oap);
3596 return TRUE;
3597}
3598
3599 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003600clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601{
3602 oap->op_type = OP_NOP;
3603 oap->regname = 0;
3604 oap->motion_force = NUL;
3605 oap->use_reg_one = FALSE;
3606}
3607
3608 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003609clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610{
3611 clearop(oap);
3612 beep_flush();
3613}
3614
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615/*
3616 * Remove the shift modifier from a special key.
3617 */
3618 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003619unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620{
3621 switch (cap->cmdchar)
3622 {
3623 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3624 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3625 case K_S_UP: cap->cmdchar = K_UP; break;
3626 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3627 case K_S_HOME: cap->cmdchar = K_HOME; break;
3628 case K_S_END: cap->cmdchar = K_END; break;
3629 }
3630 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3631}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003633/*
3634 * If the mode is currently displayed clear the command line or update the
3635 * command displayed.
3636 */
3637 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003638may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003639{
3640 if (mode_displayed)
3641 clear_cmdline = TRUE; /* unshow visual mode later */
3642#ifdef FEAT_CMDL_INFO
3643 else
3644 clear_showcmd();
3645#endif
3646}
3647
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3649/*
3650 * Routines for displaying a partly typed command
3651 */
3652
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003653#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654static char_u showcmd_buf[SHOWCMD_BUFLEN];
3655static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3656static int showcmd_is_clear = TRUE;
3657static int showcmd_visual = FALSE;
3658
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003659static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660
3661 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003662clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663{
3664 if (!p_sc)
3665 return;
3666
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 if (VIsual_active && !char_avail())
3668 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003669 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 long lines;
3671 colnr_T leftcol, rightcol;
3672 linenr_T top, bot;
3673
3674 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003675 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 {
3677 top = VIsual.lnum;
3678 bot = curwin->w_cursor.lnum;
3679 }
3680 else
3681 {
3682 top = curwin->w_cursor.lnum;
3683 bot = VIsual.lnum;
3684 }
3685# ifdef FEAT_FOLDING
3686 /* Include closed folds as a whole. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02003687 (void)hasFolding(top, &top, NULL);
3688 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689# endif
3690 lines = bot - top + 1;
3691
3692 if (VIsual_mode == Ctrl_V)
3693 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003694# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003695 char_u *saved_sbr = p_sbr;
3696
3697 /* Make 'sbr' empty for a moment to get the correct size. */
3698 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003699# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003701# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003702 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003703# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3705 (long)(rightcol - leftcol + 1));
3706 }
3707 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3708 sprintf((char *)showcmd_buf, "%ld", lines);
3709 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003710 {
3711 char_u *s, *e;
3712 int l;
3713 int bytes = 0;
3714 int chars = 0;
3715
3716 if (cursor_bot)
3717 {
3718 s = ml_get_pos(&VIsual);
3719 e = ml_get_cursor();
3720 }
3721 else
3722 {
3723 s = ml_get_cursor();
3724 e = ml_get_pos(&VIsual);
3725 }
3726 while ((*p_sel != 'e') ? s <= e : s < e)
3727 {
3728 l = (*mb_ptr2len)(s);
3729 if (l == 0)
3730 {
3731 ++bytes;
3732 ++chars;
3733 break; /* end of line */
3734 }
3735 bytes += l;
3736 ++chars;
3737 s += l;
3738 }
3739 if (bytes == chars)
3740 sprintf((char *)showcmd_buf, "%d", chars);
3741 else
3742 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3745 showcmd_visual = TRUE;
3746 }
3747 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 {
3749 showcmd_buf[0] = NUL;
3750 showcmd_visual = FALSE;
3751
3752 /* Don't actually display something if there is nothing to clear. */
3753 if (showcmd_is_clear)
3754 return;
3755 }
3756
3757 display_showcmd();
3758}
3759
3760/*
3761 * Add 'c' to string of shown command chars.
3762 * Return TRUE if output has been written (and setcursor() has been called).
3763 */
3764 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003765add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766{
3767 char_u *p;
3768 int old_len;
3769 int extra_len;
3770 int overflow;
3771#if defined(FEAT_MOUSE)
3772 int i;
3773 static int ignore[] =
3774 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003775# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3777 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003778# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01003779 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003780 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3782 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003783 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003785 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 0
3787 };
3788#endif
3789
Bram Moolenaar09df3122006-01-23 22:23:09 +00003790 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 return FALSE;
3792
3793 if (showcmd_visual)
3794 {
3795 showcmd_buf[0] = NUL;
3796 showcmd_visual = FALSE;
3797 }
3798
3799#if defined(FEAT_MOUSE)
3800 /* Ignore keys that are scrollbar updates and mouse clicks */
3801 if (IS_SPECIAL(c))
3802 for (i = 0; ignore[i] != 0; ++i)
3803 if (ignore[i] == c)
3804 return FALSE;
3805#endif
3806
3807 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003808 if (*p == ' ')
3809 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 old_len = (int)STRLEN(showcmd_buf);
3811 extra_len = (int)STRLEN(p);
3812 overflow = old_len + extra_len - SHOWCMD_COLS;
3813 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003814 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3815 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 STRCAT(showcmd_buf, p);
3817
3818 if (char_avail())
3819 return FALSE;
3820
3821 display_showcmd();
3822
3823 return TRUE;
3824}
3825
3826 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003827add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828{
3829 if (!add_to_showcmd(c))
3830 setcursor();
3831}
3832
3833/*
3834 * Delete 'len' characters from the end of the shown command.
3835 */
3836 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003837del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838{
3839 int old_len;
3840
3841 if (!p_sc)
3842 return;
3843
3844 old_len = (int)STRLEN(showcmd_buf);
3845 if (len > old_len)
3846 len = old_len;
3847 showcmd_buf[old_len - len] = NUL;
3848
3849 if (!char_avail())
3850 display_showcmd();
3851}
3852
3853/*
3854 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3855 * something and there is a partial mapping.
3856 */
3857 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003858push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859{
3860 if (p_sc)
3861 STRCPY(old_showcmd_buf, showcmd_buf);
3862}
3863
3864 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003865pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866{
3867 if (!p_sc)
3868 return;
3869
3870 STRCPY(showcmd_buf, old_showcmd_buf);
3871
3872 display_showcmd();
3873}
3874
3875 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003876display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877{
3878 int len;
3879
3880 cursor_off();
3881
3882 len = (int)STRLEN(showcmd_buf);
3883 if (len == 0)
3884 showcmd_is_clear = TRUE;
3885 else
3886 {
3887 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3888 showcmd_is_clear = FALSE;
3889 }
3890
3891 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003892 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3893 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 */
3895 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3896
3897 setcursor(); /* put cursor back where it belongs */
3898}
3899#endif
3900
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901/*
3902 * When "check" is FALSE, prepare for commands that scroll the window.
3903 * When "check" is TRUE, take care of scroll-binding after the window has
3904 * scrolled. Called from normal_cmd() and edit().
3905 */
3906 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003907do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908{
3909 static win_T *old_curwin = NULL;
3910 static linenr_T old_topline = 0;
3911#ifdef FEAT_DIFF
3912 static int old_topfill = 0;
3913#endif
3914 static buf_T *old_buf = NULL;
3915 static colnr_T old_leftcol = 0;
3916
3917 if (check && curwin->w_p_scb)
3918 {
3919 /* If a ":syncbind" command was just used, don't scroll, only reset
3920 * the values. */
3921 if (did_syncbind)
3922 did_syncbind = FALSE;
3923 else if (curwin == old_curwin)
3924 {
3925 /*
3926 * Synchronize other windows, as necessary according to
3927 * 'scrollbind'. Don't do this after an ":edit" command, except
3928 * when 'diff' is set.
3929 */
3930 if ((curwin->w_buffer == old_buf
3931#ifdef FEAT_DIFF
3932 || curwin->w_p_diff
3933#endif
3934 )
3935 && (curwin->w_topline != old_topline
3936#ifdef FEAT_DIFF
3937 || curwin->w_topfill != old_topfill
3938#endif
3939 || curwin->w_leftcol != old_leftcol))
3940 {
3941 check_scrollbind(curwin->w_topline - old_topline,
3942 (long)(curwin->w_leftcol - old_leftcol));
3943 }
3944 }
3945 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
3946 {
3947 /*
3948 * When switching between windows, make sure that the relative
3949 * vertical offset is valid for the new window. The relative
3950 * offset is invalid whenever another 'scrollbind' window has
3951 * scrolled to a point that would force the current window to
3952 * scroll past the beginning or end of its buffer. When the
3953 * resync is performed, some of the other 'scrollbind' windows may
3954 * need to jump so that the current window's relative position is
3955 * visible on-screen.
3956 */
3957 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
3958 }
3959 curwin->w_scbind_pos = curwin->w_topline;
3960 }
3961
3962 old_curwin = curwin;
3963 old_topline = curwin->w_topline;
3964#ifdef FEAT_DIFF
3965 old_topfill = curwin->w_topfill;
3966#endif
3967 old_buf = curwin->w_buffer;
3968 old_leftcol = curwin->w_leftcol;
3969}
3970
3971/*
3972 * Synchronize any windows that have "scrollbind" set, based on the
3973 * number of rows by which the current window has changed
3974 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3975 */
3976 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003977check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978{
3979 int want_ver;
3980 int want_hor;
3981 win_T *old_curwin = curwin;
3982 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 int old_VIsual_select = VIsual_select;
3984 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 colnr_T tgt_leftcol = curwin->w_leftcol;
3986 long topline;
3987 long y;
3988
3989 /*
3990 * check 'scrollopt' string for vertical and horizontal scroll options
3991 */
3992 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
3993#ifdef FEAT_DIFF
3994 want_ver |= old_curwin->w_p_diff;
3995#endif
3996 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
3997
3998 /*
3999 * loop through the scrollbound windows and scroll accordingly
4000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02004002 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 {
4004 curbuf = curwin->w_buffer;
4005 /* skip original window and windows with 'noscrollbind' */
4006 if (curwin != old_curwin && curwin->w_p_scb)
4007 {
4008 /*
4009 * do the vertical scroll
4010 */
4011 if (want_ver)
4012 {
4013#ifdef FEAT_DIFF
4014 if (old_curwin->w_p_diff && curwin->w_p_diff)
4015 {
4016 diff_set_topline(old_curwin, curwin);
4017 }
4018 else
4019#endif
4020 {
4021 curwin->w_scbind_pos += topline_diff;
4022 topline = curwin->w_scbind_pos;
4023 if (topline > curbuf->b_ml.ml_line_count)
4024 topline = curbuf->b_ml.ml_line_count;
4025 if (topline < 1)
4026 topline = 1;
4027
4028 y = topline - curwin->w_topline;
4029 if (y > 0)
4030 scrollup(y, FALSE);
4031 else
4032 scrolldown(-y, FALSE);
4033 }
4034
4035 redraw_later(VALID);
4036 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 }
4039
4040 /*
4041 * do the horizontal scroll
4042 */
4043 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4044 {
4045 curwin->w_leftcol = tgt_leftcol;
4046 leftcol_changed();
4047 }
4048 }
4049 }
4050
4051 /*
4052 * reset current-window
4053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 VIsual_select = old_VIsual_select;
4055 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 curwin = old_curwin;
4057 curbuf = old_curbuf;
4058}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
4060/*
4061 * Command character that's ignored.
4062 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004063 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004066nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004068 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069}
4070
4071/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004072 * Command character that doesn't do anything, but unlike nv_ignore() does
4073 * start edit(). Used for "startinsert" executed while starting up.
4074 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004075 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004076nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00004077{
4078}
4079
4080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 * Command character doesn't exist.
4082 */
4083 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004084nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085{
4086 clearopbeep(cap->oap);
4087}
4088
4089/*
4090 * <Help> and <F1> commands.
4091 */
4092 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004093nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094{
4095 if (!checkclearopq(cap->oap))
4096 ex_help(NULL);
4097}
4098
4099/*
4100 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4101 */
4102 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004103nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104{
Bram Moolenaarf2732452018-06-03 14:47:35 +02004105#ifdef FEAT_JOB_CHANNEL
4106 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4107 clearopbeep(cap->oap);
4108 else
4109#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01004110 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004111 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01004112 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01004113 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
4114 op_addsub(cap->oap, cap->count1, cap->arg);
4115 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004116 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01004117 else if (VIsual_active)
4118 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004119 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01004120 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121}
4122
4123/*
4124 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4125 */
4126 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004127nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128{
4129 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004130 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004131 if (mod_mask & MOD_MASK_CTRL)
4132 {
4133 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4134 if (cap->arg == BACKWARD)
4135 goto_tabpage(-(int)cap->count1);
4136 else
4137 goto_tabpage((int)cap->count0);
4138 }
4139 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02004140 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142}
4143
4144/*
4145 * Implementation of "gd" and "gD" command.
4146 */
4147 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004148nv_gd(
4149 oparg_T *oap,
4150 int nchar,
4151 int thisblock) /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152{
4153 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 char_u *ptr;
4155
Bram Moolenaard9d30582005-05-18 22:10:28 +00004156 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02004157 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
4158 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004160#ifdef FEAT_FOLDING
4161 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4162 foldOpenCursor();
4163#endif
4164}
4165
4166/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02004167 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
4168 * otherwise.
4169 */
4170 static int
4171is_ident(char_u *line, int offset)
4172{
4173 int i;
4174 int incomment = FALSE;
4175 int instring = 0;
4176 int prev = 0;
4177
4178 for (i = 0; i < offset && line[i] != NUL; i++)
4179 {
4180 if (instring != 0)
4181 {
4182 if (prev != '\\' && line[i] == instring)
4183 instring = 0;
4184 }
4185 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
4186 {
4187 instring = line[i];
4188 }
4189 else
4190 {
4191 if (incomment)
4192 {
4193 if (prev == '*' && line[i] == '/')
4194 incomment = FALSE;
4195 }
4196 else if (prev == '/' && line[i] == '*')
4197 {
4198 incomment = TRUE;
4199 }
4200 else if (prev == '/' && line[i] == '/')
4201 {
4202 return FALSE;
4203 }
4204 }
4205
4206 prev = line[i];
4207 }
4208
4209 return incomment == FALSE && instring == 0;
4210}
4211
4212/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004213 * Search for variable declaration of "ptr[len]".
4214 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4215 * current file ("gD").
4216 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004217 * Return FAIL when not found.
4218 */
4219 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004220find_decl(
4221 char_u *ptr,
4222 int len,
4223 int locally,
4224 int thisblock,
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004225 int flags_arg) /* flags passed to searchit() */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004226{
4227 char_u *pat;
4228 pos_T old_pos;
4229 pos_T par_pos;
4230 pos_T found_pos;
4231 int t;
4232 int save_p_ws;
4233 int save_p_scs;
4234 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004235 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004236 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004237 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004238
4239 if ((pat = alloc(len + 7)) == NULL)
4240 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004241
4242 /* Put "\V" before the pattern to avoid that the special meaning of "."
4243 * and "~" causes trouble. */
4244 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4245 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 old_pos = curwin->w_cursor;
4247 save_p_ws = p_ws;
4248 save_p_scs = p_scs;
4249 p_ws = FALSE; /* don't wrap around end of file now */
4250 p_scs = FALSE; /* don't switch ignorecase off now */
4251
4252 /*
4253 * With "gD" go to line 1.
4254 * With "gd" Search back for the start of the current function, then go
4255 * back until a blank line. If this fails go to line 1.
4256 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004257 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 {
4259 setpcmark(); /* Set in findpar() otherwise */
4260 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004261 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 }
4263 else
4264 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004265 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4267 --curwin->w_cursor.lnum;
4268 }
4269 curwin->w_cursor.col = 0;
4270
4271 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004272 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004273 for (;;)
4274 {
Bram Moolenaar226630a2016-10-08 19:21:31 +02004275 valid = FALSE;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004276 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004277 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004278 if (curwin->w_cursor.lnum >= old_pos.lnum)
4279 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004280
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004281 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004282 {
4283 pos_T *pos;
4284
4285 /* Check that the block the match is in doesn't end before the
4286 * position where we started the search from. */
4287 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4288 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4289 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02004290 {
4291 /* There can't be a useful match before the end of this block.
4292 * Skip to the end. */
4293 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004294 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02004295 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004296 }
4297
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004298 if (t == FAIL)
4299 {
4300 /* If we previously found a valid position, use it. */
4301 if (found_pos.lnum != 0)
4302 {
4303 curwin->w_cursor = found_pos;
4304 t = OK;
4305 }
4306 break;
4307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004309 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004310 {
4311 /* Ignore this line, continue at start of next line. */
4312 ++curwin->w_cursor.lnum;
4313 curwin->w_cursor.col = 0;
4314 continue;
4315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316#endif
Bram Moolenaar226630a2016-10-08 19:21:31 +02004317 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
4318
4319 /* If the current position is not a valid identifier and a previous
4320 * match is present, favor that one instead. */
4321 if (!valid && found_pos.lnum != 0)
4322 {
4323 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004324 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004325 }
4326
4327 /* Global search: use first valid match found */
4328 if (valid && !locally)
4329 break;
4330 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004331 {
4332 /* If we previously found a valid position, use it. */
4333 if (found_pos.lnum != 0)
4334 curwin->w_cursor = found_pos;
4335 break;
4336 }
4337
Bram Moolenaar226630a2016-10-08 19:21:31 +02004338 /* For finding a local variable and the match is before the "{" or
4339 * inside a comment, continue searching. For K&R style function
4340 * declarations this skips the function header without types. */
4341 if (!valid)
4342 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004343 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02004344 }
4345 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02004346 found_pos = curwin->w_cursor;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004347 /* Remove SEARCH_START from flags to avoid getting stuck at one
4348 * position. */
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004349 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004351
4352 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004354 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 curwin->w_cursor = old_pos;
4356 }
4357 else
4358 {
4359 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 /* "n" searches forward now */
4361 reset_search_dir();
4362 }
4363
4364 vim_free(pat);
4365 p_ws = save_p_ws;
4366 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004367
4368 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369}
4370
4371/*
4372 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4373 * lines rather than lines in the file.
4374 * 'dist' must be positive.
4375 *
4376 * Return OK if able to move cursor, FAIL otherwise.
4377 */
4378 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004379nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380{
4381 int linelen = linetabsize(ml_get_curline());
4382 int retval = OK;
4383 int atend = FALSE;
4384 int n;
4385 int col_off1; /* margin offset for first screen line */
4386 int col_off2; /* margin offset for wrapped screen line */
4387 int width1; /* text width for first screen line */
4388 int width2; /* test width for wrapped screen line */
4389
4390 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004391 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
4393 col_off1 = curwin_col_off();
4394 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02004395 width1 = curwin->w_width - col_off1;
4396 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01004397 if (width2 == 0)
4398 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004401 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 /*
4403 * Instead of sticking at the last character of the buffer line we
4404 * try to stick in the last column of the screen.
4405 */
4406 if (curwin->w_curswant == MAXCOL)
4407 {
4408 atend = TRUE;
4409 validate_virtcol();
4410 if (width1 <= 0)
4411 curwin->w_curswant = 0;
4412 else
4413 {
4414 curwin->w_curswant = width1 - 1;
4415 if (curwin->w_virtcol > curwin->w_curswant)
4416 curwin->w_curswant += ((curwin->w_virtcol
4417 - curwin->w_curswant - 1) / width2 + 1) * width2;
4418 }
4419 }
4420 else
4421 {
4422 if (linelen > width1)
4423 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4424 else
4425 n = width1;
4426 if (curwin->w_curswant > (colnr_T)n + 1)
4427 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4428 * width2;
4429 }
4430
4431 while (dist--)
4432 {
4433 if (dir == BACKWARD)
4434 {
4435 if ((long)curwin->w_curswant >= width2)
4436 /* move back within line */
4437 curwin->w_curswant -= width2;
4438 else
4439 {
4440 /* to previous line */
4441 if (curwin->w_cursor.lnum == 1)
4442 {
4443 retval = FAIL;
4444 break;
4445 }
4446 --curwin->w_cursor.lnum;
4447#ifdef FEAT_FOLDING
4448 /* Move to the start of a closed fold. Don't do that when
4449 * 'foldopen' contains "all": it will open in a moment. */
4450 if (!(fdo_flags & FDO_ALL))
4451 (void)hasFolding(curwin->w_cursor.lnum,
4452 &curwin->w_cursor.lnum, NULL);
4453#endif
4454 linelen = linetabsize(ml_get_curline());
4455 if (linelen > width1)
4456 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4457 + 1) * width2;
4458 }
4459 }
4460 else /* dir == FORWARD */
4461 {
4462 if (linelen > width1)
4463 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4464 else
4465 n = width1;
4466 if (curwin->w_curswant + width2 < (colnr_T)n)
4467 /* move forward within line */
4468 curwin->w_curswant += width2;
4469 else
4470 {
4471 /* to next line */
4472#ifdef FEAT_FOLDING
4473 /* Move to the end of a closed fold. */
4474 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4475 &curwin->w_cursor.lnum);
4476#endif
4477 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4478 {
4479 retval = FAIL;
4480 break;
4481 }
4482 curwin->w_cursor.lnum++;
4483 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004484 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 }
4486 }
4487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004490 if (virtual_active() && atend)
4491 coladvance(MAXCOL);
4492 else
4493 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4496 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004497 colnr_T virtcol;
4498
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 /*
4500 * Check for landing on a character that got split at the end of the
4501 * last line. We want to advance a screenline, not end up in the same
4502 * screenline or move two screenlines.
4503 */
4504 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004505 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004506#if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004507 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4508 virtcol -= vim_strsize(p_sbr);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004509#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004510
4511 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 && (curwin->w_curswant < (colnr_T)width1
4513 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4514 : ((curwin->w_curswant - width1) % width2
4515 > (colnr_T)width2 / 2)))
4516 --curwin->w_cursor.col;
4517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518
4519 if (atend)
4520 curwin->w_curswant = MAXCOL; /* stick in the last column */
4521
4522 return retval;
4523}
4524
4525#ifdef FEAT_MOUSE
4526/*
4527 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4528 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004529 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4530 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 */
4532 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004533nv_mousescroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534{
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004535 win_T *old_curwin = curwin, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004537 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 {
4539 int row, col;
4540
4541 row = mouse_row;
4542 col = mouse_col;
4543
4544 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004545 wp = mouse_find_win(&row, &col);
4546 if (wp == NULL)
4547 return;
4548 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 curbuf = curwin->w_buffer;
4550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004552 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004554# ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02004555 if (term_use_loop())
Bram Moolenaar73675fb2017-11-20 21:49:19 +01004556 /* This window is a terminal window, send the mouse event there.
4557 * Set "typed" to FALSE to avoid an endless loop. */
4558 send_keys_to_term(curbuf->b_term, cap->cmdchar, FALSE);
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004559 else
4560# endif
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004561 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4562 {
4563 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4564 }
4565 else
4566 {
4567 cap->count1 = 3;
4568 cap->count0 = 3;
4569 nv_scroll_line(cap);
4570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004572# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 else
4574 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004575 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4576 if (!curwin->w_p_wrap)
4577 {
4578 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004579
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004580 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02004581 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004582 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4583 if (val < 0)
4584 val = 0;
4585
4586 gui_do_horiz_scroll(val, TRUE);
4587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004589# endif
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +01004590# ifdef FEAT_SYN_HL
4591 if (curwin != old_curwin && curwin->w_p_cul)
4592 redraw_for_cursorline(curwin);
4593# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 curwin->w_redr_status = TRUE;
4596
4597 curwin = old_curwin;
4598 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599}
4600
4601/*
4602 * Mouse clicks and drags.
4603 */
4604 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004605nv_mouse(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606{
4607 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4608}
4609#endif
4610
4611/*
4612 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4613 * cap->arg must be TRUE for CTRL-E.
4614 */
4615 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004616nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617{
4618 if (!checkclearop(cap->oap))
4619 scroll_redraw(cap->arg, cap->count1);
4620}
4621
4622/*
4623 * Scroll "count" lines up or down, and redraw.
4624 */
4625 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004626scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627{
4628 linenr_T prev_topline = curwin->w_topline;
4629#ifdef FEAT_DIFF
4630 int prev_topfill = curwin->w_topfill;
4631#endif
4632 linenr_T prev_lnum = curwin->w_cursor.lnum;
4633
4634 if (up)
4635 scrollup(count, TRUE);
4636 else
4637 scrolldown(count, TRUE);
4638 if (p_so)
4639 {
4640 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4641 * valid, otherwise the screen jumps back at the end of the file. */
4642 cursor_correct();
4643 check_cursor_moved(curwin);
4644 curwin->w_valid |= VALID_TOPLINE;
4645
4646 /* If moved back to where we were, at least move the cursor, otherwise
4647 * we get stuck at one position. Don't move the cursor up if the
4648 * first line of the buffer is already on the screen */
4649 while (curwin->w_topline == prev_topline
4650#ifdef FEAT_DIFF
4651 && curwin->w_topfill == prev_topfill
4652#endif
4653 )
4654 {
4655 if (up)
4656 {
4657 if (curwin->w_cursor.lnum > prev_lnum
4658 || cursor_down(1L, FALSE) == FAIL)
4659 break;
4660 }
4661 else
4662 {
4663 if (curwin->w_cursor.lnum < prev_lnum
4664 || prev_topline == 1L
4665 || cursor_up(1L, FALSE) == FAIL)
4666 break;
4667 }
4668 /* Mark w_topline as valid, otherwise the screen jumps back at the
4669 * end of the file. */
4670 check_cursor_moved(curwin);
4671 curwin->w_valid |= VALID_TOPLINE;
4672 }
4673 }
4674 if (curwin->w_cursor.lnum != prev_lnum)
4675 coladvance(curwin->w_curswant);
4676 redraw_later(VALID);
4677}
4678
4679/*
4680 * Commands that start with "z".
4681 */
4682 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004683nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684{
4685 long n;
4686 colnr_T col;
4687 int nchar = cap->nchar;
4688#ifdef FEAT_FOLDING
4689 long old_fdl = curwin->w_p_fdl;
4690 int old_fen = curwin->w_p_fen;
4691#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004692#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004693 int undo = FALSE;
4694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695
4696 if (VIM_ISDIGIT(nchar))
4697 {
4698 /*
4699 * "z123{nchar}": edit the count before obtaining {nchar}
4700 */
4701 if (checkclearop(cap->oap))
4702 return;
4703 n = nchar - '0';
4704 for (;;)
4705 {
4706#ifdef USE_ON_FLY_SCROLL
4707 dont_scroll = TRUE; /* disallow scrolling here */
4708#endif
4709 ++no_mapping;
4710 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004711 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 --no_mapping;
4714 --allow_keys;
4715#ifdef FEAT_CMDL_INFO
4716 (void)add_to_showcmd(nchar);
4717#endif
4718 if (nchar == K_DEL || nchar == K_KDEL)
4719 n /= 10;
4720 else if (VIM_ISDIGIT(nchar))
4721 n = n * 10 + (nchar - '0');
4722 else if (nchar == CAR)
4723 {
4724#ifdef FEAT_GUI
4725 need_mouse_correct = TRUE;
4726#endif
4727 win_setheight((int)n);
4728 break;
4729 }
4730 else if (nchar == 'l'
4731 || nchar == 'h'
4732 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004733 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 {
4735 cap->count1 = n ? n * cap->count1 : cap->count1;
4736 goto dozet;
4737 }
4738 else
4739 {
4740 clearopbeep(cap->oap);
4741 break;
4742 }
4743 }
4744 cap->oap->op_type = OP_NOP;
4745 return;
4746 }
4747
4748dozet:
4749 if (
4750#ifdef FEAT_FOLDING
4751 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4752 * and "zC" only in Visual mode. "zj" and "zk" are motion
4753 * commands. */
4754 cap->nchar != 'f' && cap->nchar != 'F'
4755 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4756 && cap->nchar != 'j' && cap->nchar != 'k'
4757 &&
4758#endif
4759 checkclearop(cap->oap))
4760 return;
4761
4762 /*
4763 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4764 * If line number given, set cursor.
4765 */
4766 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4767 && cap->count0
4768 && cap->count0 != curwin->w_cursor.lnum)
4769 {
4770 setpcmark();
4771 if (cap->count0 > curbuf->b_ml.ml_line_count)
4772 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4773 else
4774 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004775 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 }
4777
4778 switch (nchar)
4779 {
4780 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4781 case '+':
4782 if (cap->count0 == 0)
4783 {
4784 /* No count given: put cursor at the line below screen */
4785 validate_botline(); /* make sure w_botline is valid */
4786 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4787 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4788 else
4789 curwin->w_cursor.lnum = curwin->w_botline;
4790 }
4791 /* FALLTHROUGH */
4792 case NL:
4793 case CAR:
4794 case K_KENTER:
4795 beginline(BL_WHITE | BL_FIX);
4796 /* FALLTHROUGH */
4797
4798 case 't': scroll_cursor_top(0, TRUE);
4799 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004800 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 break;
4802
4803 /* "z." and "zz": put cursor in middle of screen */
4804 case '.': beginline(BL_WHITE | BL_FIX);
4805 /* FALLTHROUGH */
4806
4807 case 'z': scroll_cursor_halfway(TRUE);
4808 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004809 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 break;
4811
4812 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4813 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4814 * when <count> is at bottom of window, and puts that one at
4815 * bottom of window. */
4816 if (cap->count0 != 0)
4817 {
4818 scroll_cursor_bot(0, TRUE);
4819 curwin->w_cursor.lnum = curwin->w_topline;
4820 }
4821 else if (curwin->w_topline == 1)
4822 curwin->w_cursor.lnum = 1;
4823 else
4824 curwin->w_cursor.lnum = curwin->w_topline - 1;
4825 /* FALLTHROUGH */
4826 case '-':
4827 beginline(BL_WHITE | BL_FIX);
4828 /* FALLTHROUGH */
4829
4830 case 'b': scroll_cursor_bot(0, TRUE);
4831 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004832 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 break;
4834
4835 /* "zH" - scroll screen right half-page */
4836 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02004837 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 /* FALLTHROUGH */
4839
4840 /* "zh" - scroll screen to the right */
4841 case 'h':
4842 case K_LEFT:
4843 if (!curwin->w_p_wrap)
4844 {
4845 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4846 curwin->w_leftcol = 0;
4847 else
4848 curwin->w_leftcol -= (colnr_T)cap->count1;
4849 leftcol_changed();
4850 }
4851 break;
4852
4853 /* "zL" - scroll screen left half-page */
Bram Moolenaar02631462017-09-22 15:20:32 +02004854 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 /* FALLTHROUGH */
4856
4857 /* "zl" - scroll screen to the left */
4858 case 'l':
4859 case K_RIGHT:
4860 if (!curwin->w_p_wrap)
4861 {
4862 /* scroll the window left */
4863 curwin->w_leftcol += (colnr_T)cap->count1;
4864 leftcol_changed();
4865 }
4866 break;
4867
4868 /* "zs" - scroll screen, cursor at the start */
4869 case 's': if (!curwin->w_p_wrap)
4870 {
4871#ifdef FEAT_FOLDING
4872 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4873 col = 0; /* like the cursor is in col 0 */
4874 else
4875#endif
4876 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4877 if ((long)col > p_siso)
4878 col -= p_siso;
4879 else
4880 col = 0;
4881 if (curwin->w_leftcol != col)
4882 {
4883 curwin->w_leftcol = col;
4884 redraw_later(NOT_VALID);
4885 }
4886 }
4887 break;
4888
4889 /* "ze" - scroll screen, cursor at the end */
4890 case 'e': if (!curwin->w_p_wrap)
4891 {
4892#ifdef FEAT_FOLDING
4893 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4894 col = 0; /* like the cursor is in col 0 */
4895 else
4896#endif
4897 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02004898 n = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 if ((long)col + p_siso < n)
4900 col = 0;
4901 else
4902 col = col + p_siso - n + 1;
4903 if (curwin->w_leftcol != col)
4904 {
4905 curwin->w_leftcol = col;
4906 redraw_later(NOT_VALID);
4907 }
4908 }
4909 break;
4910
4911#ifdef FEAT_FOLDING
4912 /* "zF": create fold command */
4913 /* "zf": create fold operator */
4914 case 'F':
4915 case 'f': if (foldManualAllowed(TRUE))
4916 {
4917 cap->nchar = 'f';
4918 nv_operator(cap);
4919 curwin->w_p_fen = TRUE;
4920
4921 /* "zF" is like "zfzf" */
4922 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4923 {
4924 nv_operator(cap);
4925 finish_op = TRUE;
4926 }
4927 }
4928 else
4929 clearopbeep(cap->oap);
4930 break;
4931
4932 /* "zd": delete fold at cursor */
4933 /* "zD": delete fold at cursor recursively */
4934 case 'd':
4935 case 'D': if (foldManualAllowed(FALSE))
4936 {
4937 if (VIsual_active)
4938 nv_operator(cap);
4939 else
4940 deleteFold(curwin->w_cursor.lnum,
4941 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4942 }
4943 break;
4944
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004945 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 case 'E': if (foldmethodIsManual(curwin))
4947 {
4948 clearFolding(curwin);
4949 changed_window_setting();
4950 }
4951 else if (foldmethodIsMarker(curwin))
4952 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4953 TRUE, FALSE);
4954 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004955 emsg(_("E352: Cannot erase folds with current 'foldmethod'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 break;
4957
4958 /* "zn": fold none: reset 'foldenable' */
4959 case 'n': curwin->w_p_fen = FALSE;
4960 break;
4961
4962 /* "zN": fold Normal: set 'foldenable' */
4963 case 'N': curwin->w_p_fen = TRUE;
4964 break;
4965
4966 /* "zi": invert folding: toggle 'foldenable' */
4967 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4968 break;
4969
4970 /* "za": open closed fold or close open fold at cursor */
4971 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4972 openFold(curwin->w_cursor.lnum, cap->count1);
4973 else
4974 {
4975 closeFold(curwin->w_cursor.lnum, cap->count1);
4976 curwin->w_p_fen = TRUE;
4977 }
4978 break;
4979
4980 /* "zA": open fold at cursor recursively */
4981 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4982 openFoldRecurse(curwin->w_cursor.lnum);
4983 else
4984 {
4985 closeFoldRecurse(curwin->w_cursor.lnum);
4986 curwin->w_p_fen = TRUE;
4987 }
4988 break;
4989
4990 /* "zo": open fold at cursor or Visual area */
4991 case 'o': if (VIsual_active)
4992 nv_operator(cap);
4993 else
4994 openFold(curwin->w_cursor.lnum, cap->count1);
4995 break;
4996
4997 /* "zO": open fold recursively */
4998 case 'O': if (VIsual_active)
4999 nv_operator(cap);
5000 else
5001 openFoldRecurse(curwin->w_cursor.lnum);
5002 break;
5003
5004 /* "zc": close fold at cursor or Visual area */
5005 case 'c': if (VIsual_active)
5006 nv_operator(cap);
5007 else
5008 closeFold(curwin->w_cursor.lnum, cap->count1);
5009 curwin->w_p_fen = TRUE;
5010 break;
5011
5012 /* "zC": close fold recursively */
5013 case 'C': if (VIsual_active)
5014 nv_operator(cap);
5015 else
5016 closeFoldRecurse(curwin->w_cursor.lnum);
5017 curwin->w_p_fen = TRUE;
5018 break;
5019
5020 /* "zv": open folds at the cursor */
5021 case 'v': foldOpenCursor();
5022 break;
5023
5024 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5025 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005026 curwin->w_foldinvalid = TRUE; /* recompute folds */
5027 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 foldOpenCursor();
5029 break;
5030
5031 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5032 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005033 curwin->w_foldinvalid = TRUE; /* recompute folds */
5034 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 break;
5036
5037 /* "zm": fold more */
5038 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005039 {
5040 curwin->w_p_fdl -= cap->count1;
5041 if (curwin->w_p_fdl < 0)
5042 curwin->w_p_fdl = 0;
5043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 old_fdl = -1; /* force an update */
5045 curwin->w_p_fen = TRUE;
5046 break;
5047
5048 /* "zM": close all folds */
5049 case 'M': curwin->w_p_fdl = 0;
5050 old_fdl = -1; /* force an update */
5051 curwin->w_p_fen = TRUE;
5052 break;
5053
5054 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005055 case 'r': curwin->w_p_fdl += cap->count1;
5056 {
5057 int d = getDeepestNesting();
5058
5059 if (curwin->w_p_fdl >= d)
5060 curwin->w_p_fdl = d;
5061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 break;
5063
5064 /* "zR": open all folds */
5065 case 'R': curwin->w_p_fdl = getDeepestNesting();
5066 old_fdl = -1; /* force an update */
5067 break;
5068
5069 case 'j': /* "zj" move to next fold downwards */
5070 case 'k': /* "zk" move to next fold upwards */
5071 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5072 cap->count1) == FAIL)
5073 clearopbeep(cap->oap);
5074 break;
5075
5076#endif /* FEAT_FOLDING */
5077
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005078#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005079 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5080 ++no_mapping;
5081 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005082 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005083 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005084 --no_mapping;
5085 --allow_keys;
5086#ifdef FEAT_CMDL_INFO
5087 (void)add_to_showcmd(nchar);
5088#endif
5089 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5090 {
5091 clearopbeep(cap->oap);
5092 break;
5093 }
5094 undo = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005095 /* FALLTHROUGH */
Bram Moolenaard0131a82006-03-04 21:46:13 +00005096
Bram Moolenaarb765d632005-06-07 21:00:02 +00005097 case 'g': /* "zg": add good word to word list */
5098 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005099 case 'G': /* "zG": add good word to temp word list */
5100 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005101 {
5102 char_u *ptr = NULL;
5103 int len;
5104
5105 if (checkclearop(cap->oap))
5106 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005107 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5108 == FAIL)
5109 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005110 if (ptr == NULL)
5111 {
5112 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005113
Bram Moolenaar134bf072013-09-25 18:54:24 +02005114 /* Find bad word under the cursor. When 'spell' is
5115 * off this fails and find_ident_under_cursor() is
5116 * used below. */
5117 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005118 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005119 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005120 if (len != 0 && curwin->w_cursor.col <= pos.col)
5121 ptr = ml_get_pos(&curwin->w_cursor);
5122 curwin->w_cursor = pos;
5123 }
5124
Bram Moolenaarb765d632005-06-07 21:00:02 +00005125 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5126 FIND_IDENT)) == 0)
5127 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005128 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005129 (nchar == 'G' || nchar == 'W')
5130 ? 0 : (int)cap->count1,
5131 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005132 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005133 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005134
Bram Moolenaar43abc522005-12-10 20:15:02 +00005135 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005136 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005137 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005138 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005139#endif
5140
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 default: clearopbeep(cap->oap);
5142 }
5143
5144#ifdef FEAT_FOLDING
5145 /* Redraw when 'foldenable' changed */
5146 if (old_fen != curwin->w_p_fen)
5147 {
5148# ifdef FEAT_DIFF
5149 win_T *wp;
5150
5151 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5152 {
5153 /* Adjust 'foldenable' in diff-synced windows. */
5154 FOR_ALL_WINDOWS(wp)
5155 {
5156 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5157 {
5158 wp->w_p_fen = curwin->w_p_fen;
5159 changed_window_setting_win(wp);
5160 }
5161 }
5162 }
5163# endif
5164 changed_window_setting();
5165 }
5166
5167 /* Redraw when 'foldlevel' changed. */
5168 if (old_fdl != curwin->w_p_fdl)
5169 newFoldLevel();
5170#endif
5171}
5172
5173#ifdef FEAT_GUI
5174/*
5175 * Vertical scrollbar movement.
5176 */
5177 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005178nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179{
5180 if (cap->oap->op_type != OP_NOP)
5181 clearopbeep(cap->oap);
5182
5183 /* Even if an operator was pending, we still want to scroll */
5184 gui_do_scroll();
5185}
5186
5187/*
5188 * Horizontal scrollbar movement.
5189 */
5190 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005191nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +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 scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005197 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198}
5199#endif
5200
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005201#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005202/*
5203 * Click in GUI tab.
5204 */
5205 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005206nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005207{
5208 if (cap->oap->op_type != OP_NOP)
5209 clearopbeep(cap->oap);
5210
5211 /* Even if an operator was pending, we still want to jump tabs. */
5212 goto_tabpage(current_tab);
5213}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005214
5215/*
5216 * Selected item in tab line menu.
5217 */
5218 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005219nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005220{
5221 if (cap->oap->op_type != OP_NOP)
5222 clearopbeep(cap->oap);
5223
5224 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005225 handle_tabmenu();
5226}
5227
5228/*
5229 * Handle selecting an item of the GUI tab line menu.
5230 * Used in Normal and Insert mode.
5231 */
5232 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005233handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005234{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005235 switch (current_tabmenu)
5236 {
5237 case TABLINE_MENU_CLOSE:
5238 if (current_tab == 0)
5239 do_cmdline_cmd((char_u *)"tabclose");
5240 else
5241 {
5242 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5243 current_tab);
5244 do_cmdline_cmd(IObuff);
5245 }
5246 break;
5247
5248 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005249 if (current_tab == 0)
5250 do_cmdline_cmd((char_u *)"$tabnew");
5251 else
5252 {
5253 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5254 current_tab - 1);
5255 do_cmdline_cmd(IObuff);
5256 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005257 break;
5258
5259 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005260 if (current_tab == 0)
5261 do_cmdline_cmd((char_u *)"browse $tabnew");
5262 else
5263 {
5264 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5265 current_tab - 1);
5266 do_cmdline_cmd(IObuff);
5267 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005268 break;
5269 }
5270}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005271#endif
5272
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273/*
5274 * "Q" command.
5275 */
5276 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005277nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278{
5279 /*
5280 * Ignore 'Q' in Visual mode, just give a beep.
5281 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005283 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005284 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 do_exmode(FALSE);
5286}
5287
5288/*
5289 * Handle a ":" command.
5290 */
5291 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005292nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293{
5294 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005295 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 if (VIsual_active)
5298 nv_operator(cap);
5299 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 {
5301 if (cap->oap->op_type != OP_NOP)
5302 {
5303 /* Using ":" as a movement is characterwise exclusive. */
5304 cap->oap->motion_type = MCHAR;
5305 cap->oap->inclusive = FALSE;
5306 }
5307 else if (cap->count0)
5308 {
5309 /* translate "count:" into ":.,.+(count - 1)" */
5310 stuffcharReadbuff('.');
5311 if (cap->count0 > 1)
5312 {
5313 stuffReadbuff((char_u *)",.+");
5314 stuffnumReadbuff((long)cap->count0 - 1L);
5315 }
5316 }
5317
5318 /* When typing, don't type below an old message */
5319 if (KeyTyped)
5320 compute_cmdrow();
5321
5322 old_p_im = p_im;
5323
5324 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005325 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5327
5328 /* If 'insertmode' changed, enter or exit Insert mode */
5329 if (p_im != old_p_im)
5330 {
5331 if (p_im)
5332 restart_edit = 'i';
5333 else
5334 restart_edit = 0;
5335 }
5336
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005337 if (cmd_result == FAIL)
5338 /* The Ex command failed, do not execute the operator. */
5339 clearop(cap->oap);
5340 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5342 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005343 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5344 || did_emsg
5345 ))
5346 /* The start of the operator has become invalid by the Ex command.
5347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 clearopbeep(cap->oap);
5349 }
5350}
5351
5352/*
5353 * Handle CTRL-G command.
5354 */
5355 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005356nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 if (VIsual_active) /* toggle Selection/Visual mode */
5359 {
5360 VIsual_select = !VIsual_select;
5361 showmode();
5362 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005363 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 /* print full name if count given or :cd used */
5365 fileinfo((int)cap->count0, FALSE, TRUE);
5366}
5367
5368/*
5369 * Handle CTRL-H <Backspace> command.
5370 */
5371 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005372nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 if (VIsual_active && VIsual_select)
5375 {
5376 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5377 v_visop(cap);
5378 }
5379 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 nv_left(cap);
5381}
5382
5383/*
5384 * CTRL-L: clear screen and redraw.
5385 */
5386 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005387nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388{
5389 if (!checkclearop(cap->oap))
5390 {
5391#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5392 /*
5393 * Right now, the BeBox doesn't seem to have an easy way to detect
5394 * window resizing, so we cheat and make the user detect it
5395 * manually with CTRL-L instead
5396 */
5397 ui_get_shellsize();
5398#endif
5399#ifdef FEAT_SYN_HL
5400 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005401 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02005402# ifdef FEAT_RELTIME
5403 {
5404 win_T *wp;
5405
5406 FOR_ALL_WINDOWS(wp)
5407 wp->w_s->b_syn_slow = FALSE;
5408 }
5409# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410#endif
5411 redraw_later(CLEAR);
5412 }
5413}
5414
5415/*
5416 * CTRL-O: In Select mode: switch to Visual mode for one command.
5417 * Otherwise: Go to older pcmark.
5418 */
5419 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005420nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 if (VIsual_active && VIsual_select)
5423 {
5424 VIsual_select = FALSE;
5425 showmode();
5426 restart_VIsual_select = 2; /* restart Select mode later */
5427 }
5428 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 {
5430 cap->count1 = -cap->count1;
5431 nv_pcmark(cap);
5432 }
5433}
5434
5435/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01005436 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
5437 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 */
5439 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005440nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441{
5442 if (!checkclearopq(cap->oap))
5443 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5444 GETF_SETMARK|GETF_ALT, FALSE);
5445}
5446
5447/*
5448 * "Z" commands.
5449 */
5450 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005451nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452{
5453 if (!checkclearopq(cap->oap))
5454 {
5455 switch (cap->nchar)
5456 {
5457 /* "ZZ": equivalent to ":x". */
5458 case 'Z': do_cmdline_cmd((char_u *)"x");
5459 break;
5460
5461 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5462 case 'Q': do_cmdline_cmd((char_u *)"q!");
5463 break;
5464
5465 default: clearopbeep(cap->oap);
5466 }
5467 }
5468}
5469
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470/*
5471 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5472 */
5473 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005474do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475{
5476 oparg_T oa;
5477 cmdarg_T ca;
5478
5479 clear_oparg(&oa);
5480 vim_memset(&ca, 0, sizeof(ca));
5481 ca.oap = &oa;
5482 ca.cmdchar = c1;
5483 ca.nchar = c2;
5484 nv_ident(&ca);
5485}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486
5487/*
5488 * Handle the commands that use the word under the cursor.
5489 * [g] CTRL-] :ta to current identifier
5490 * [g] 'K' run program for current identifier
5491 * [g] '*' / to current identifier or string
5492 * [g] '#' ? to current identifier or string
5493 * g ']' :tselect for current identifier
5494 */
5495 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005496nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497{
5498 char_u *ptr = NULL;
5499 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005500 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005501 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 char_u *p;
5503 char_u *kp; /* value of 'keywordprg' */
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005504 int kp_help; /* 'keywordprg' is ":he" */
5505 int kp_ex; /* 'keywordprg' starts with ":" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 int n = 0; /* init for GCC */
5507 int cmdchar;
5508 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005509 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 char_u *aux_ptr;
5511 int isman;
5512 int isman_s;
5513
5514 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5515 {
5516 cmdchar = cap->nchar;
5517 g_cmd = TRUE;
5518 }
5519 else
5520 {
5521 cmdchar = cap->cmdchar;
5522 g_cmd = FALSE;
5523 }
5524
5525 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5526 cmdchar = '#';
5527
5528 /*
5529 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5530 */
5531 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5532 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5534 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 if (checkclearopq(cap->oap))
5536 return;
5537 }
5538
5539 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5540 (cmdchar == '*' || cmdchar == '#')
5541 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5542 {
5543 clearop(cap->oap);
5544 return;
5545 }
5546
5547 /* Allocate buffer to put the command in. Inserting backslashes can
5548 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5549 * and some numbers. */
5550 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5551 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5552 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02005553 if (kp_help && *skipwhite(ptr) == NUL)
5554 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005555 emsg(_(e_noident)); /* found white space only */
Bram Moolenaara4f99f52017-08-26 16:25:32 +02005556 return;
5557 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005558 kp_ex = (*kp == ':');
5559 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
5560 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 if (buf == NULL)
5562 return;
5563 buf[0] = NUL;
5564
5565 switch (cmdchar)
5566 {
5567 case '*':
5568 case '#':
5569 /*
5570 * Put cursor at start of word, makes search skip the word
5571 * under the cursor.
5572 * Call setpcmark() first, so "*``" puts the cursor back where
5573 * it was.
5574 */
5575 setpcmark();
5576 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5577
5578 if (!g_cmd && vim_iswordp(ptr))
5579 STRCPY(buf, "\\<");
5580 no_smartcase = TRUE; /* don't use 'smartcase' now */
5581 break;
5582
5583 case 'K':
5584 if (kp_help)
5585 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005586 else if (kp_ex)
5587 {
5588 if (cap->count0 != 0)
5589 vim_snprintf((char *)buf, buflen, "%s %ld",
5590 kp, cap->count0);
5591 else
5592 STRCPY(buf, kp);
5593 STRCAT(buf, " ");
5594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 else
5596 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005597 /* An external command will probably use an argument starting
5598 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005599 while (*ptr == '-' && n > 0)
5600 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005601 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005602 --n;
5603 }
5604 if (n == 0)
5605 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005606 emsg(_(e_noident)); /* found dashes only */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005607 vim_free(buf);
5608 return;
5609 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005610
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 /* When a count is given, turn it into a range. Is this
5612 * really what we want? */
5613 isman = (STRCMP(kp, "man") == 0);
5614 isman_s = (STRCMP(kp, "man -s") == 0);
5615 if (cap->count0 != 0 && !(isman || isman_s))
5616 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5617
5618 STRCAT(buf, "! ");
5619 if (cap->count0 == 0 && isman_s)
5620 STRCAT(buf, "man");
5621 else
5622 STRCAT(buf, kp);
5623 STRCAT(buf, " ");
5624 if (cap->count0 != 0 && (isman || isman_s))
5625 {
5626 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5627 STRCAT(buf, " ");
5628 }
5629 }
5630 break;
5631
5632 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005633 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634#ifdef FEAT_CSCOPE
5635 if (p_cst)
5636 STRCPY(buf, "cstag ");
5637 else
5638#endif
5639 STRCPY(buf, "ts ");
5640 break;
5641
5642 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005643 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 if (curbuf->b_help)
5645 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005647 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005648 if (g_cmd)
5649 STRCPY(buf, "tj ");
5650 else
5651 sprintf((char *)buf, "%ldta ", cap->count0);
5652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 }
5654
5655 /*
5656 * Now grab the chars in the identifier
5657 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005658 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005660 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01005661 if (kp_ex)
5662 /* Escape the argument properly for an Ex command */
5663 p = vim_strsave_fnameescape(ptr, FALSE);
5664 else
5665 /* Escape the argument properly for a shell command */
5666 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005667 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005668 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005670 vim_free(buf);
5671 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005673 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5674 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005675 {
5676 vim_free(buf);
5677 vim_free(p);
5678 return;
5679 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005680 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005681 STRCAT(buf, p);
5682 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005684 else
5685 {
5686 if (cmdchar == '*')
5687 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5688 else if (cmdchar == '#')
5689 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005690 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005691 {
5692 if (curbuf->b_help)
5693 /* ":help" handles unescaped argument */
5694 aux_ptr = (char_u *)"";
5695 else
5696 aux_ptr = (char_u *)"\\|\"\n[";
5697 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005698 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005699 aux_ptr = (char_u *)"\\|\"\n*?[";
5700
5701 p = buf + STRLEN(buf);
5702 while (n-- > 0)
5703 {
5704 /* put a backslash before \ and some others */
5705 if (vim_strchr(aux_ptr, *ptr) != NULL)
5706 *p++ = '\\';
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005707 /* When current byte is a part of multibyte character, copy all
5708 * bytes of that character. */
5709 if (has_mbyte)
5710 {
5711 int i;
5712 int len = (*mb_ptr2len)(ptr) - 1;
5713
5714 for (i = 0; i < len && n >= 1; ++i, --n)
5715 *p++ = *ptr++;
5716 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005717 *p++ = *ptr++;
5718 }
5719 *p = NUL;
5720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721
5722 /*
5723 * Execute the command.
5724 */
5725 if (cmdchar == '*' || cmdchar == '#')
5726 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005727 if (!g_cmd && (has_mbyte
5728 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
5729 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 STRCAT(buf, "\\>");
5731#ifdef FEAT_CMDHIST
5732 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005733 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5735#endif
Bram Moolenaar46539112015-02-17 15:43:57 +01005736 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 }
5738 else
5739 do_cmdline_cmd(buf);
5740
5741 vim_free(buf);
5742}
5743
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744/*
5745 * Get visually selected text, within one line only.
5746 * Returns FAIL if more than one line selected.
5747 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005748 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005749get_visual_text(
5750 cmdarg_T *cap,
5751 char_u **pp, /* return: start of selected text */
5752 int *lenp) /* return: length of selected text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753{
5754 if (VIsual_mode != 'V')
5755 unadjust_for_sel();
5756 if (VIsual.lnum != curwin->w_cursor.lnum)
5757 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005758 if (cap != NULL)
5759 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 return FAIL;
5761 }
5762 if (VIsual_mode == 'V')
5763 {
5764 *pp = ml_get_curline();
5765 *lenp = (int)STRLEN(*pp);
5766 }
5767 else
5768 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005769 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 {
5771 *pp = ml_get_pos(&curwin->w_cursor);
5772 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5773 }
5774 else
5775 {
5776 *pp = ml_get_pos(&VIsual);
5777 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 if (has_mbyte)
5780 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005781 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 }
5783 reset_VIsual_and_resel();
5784 return OK;
5785}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786
5787/*
5788 * CTRL-T: backwards in tag stack
5789 */
5790 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005791nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792{
5793 if (!checkclearopq(cap->oap))
5794 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5795}
5796
5797/*
5798 * Handle scrolling command 'H', 'L' and 'M'.
5799 */
5800 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005801nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802{
5803 int used = 0;
5804 long n;
5805#ifdef FEAT_FOLDING
5806 linenr_T lnum;
5807#endif
5808 int half;
5809
5810 cap->oap->motion_type = MLINE;
5811 setpcmark();
5812
5813 if (cap->cmdchar == 'L')
5814 {
5815 validate_botline(); /* make sure curwin->w_botline is valid */
5816 curwin->w_cursor.lnum = curwin->w_botline - 1;
5817 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5818 curwin->w_cursor.lnum = 1;
5819 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005820 {
5821#ifdef FEAT_FOLDING
5822 if (hasAnyFolding(curwin))
5823 {
5824 /* Count a fold for one screen line. */
5825 for (n = cap->count1 - 1; n > 0
5826 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5827 {
5828 (void)hasFolding(curwin->w_cursor.lnum,
5829 &curwin->w_cursor.lnum, NULL);
5830 --curwin->w_cursor.lnum;
5831 }
5832 }
5833 else
5834#endif
5835 curwin->w_cursor.lnum -= cap->count1 - 1;
5836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 }
5838 else
5839 {
5840 if (cap->cmdchar == 'M')
5841 {
5842#ifdef FEAT_DIFF
5843 /* Don't count filler lines above the window. */
5844 used -= diff_check_fill(curwin, curwin->w_topline)
5845 - curwin->w_topfill;
5846#endif
5847 validate_botline(); /* make sure w_empty_rows is valid */
5848 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5849 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5850 {
5851#ifdef FEAT_DIFF
5852 /* Count half he number of filler lines to be "below this
5853 * line" and half to be "above the next line". */
5854 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5855 + n) / 2 >= half)
5856 {
5857 --n;
5858 break;
5859 }
5860#endif
5861 used += plines(curwin->w_topline + n);
5862 if (used >= half)
5863 break;
5864#ifdef FEAT_FOLDING
5865 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5866 n = lnum - curwin->w_topline;
5867#endif
5868 }
5869 if (n > 0 && used > curwin->w_height)
5870 --n;
5871 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005872 else /* (cap->cmdchar == 'H') */
5873 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005875#ifdef FEAT_FOLDING
5876 if (hasAnyFolding(curwin))
5877 {
5878 /* Count a fold for one screen line. */
5879 lnum = curwin->w_topline;
5880 while (n-- > 0 && lnum < curwin->w_botline - 1)
5881 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02005882 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005883 ++lnum;
5884 }
5885 n = lnum - curwin->w_topline;
5886 }
5887#endif
5888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 curwin->w_cursor.lnum = curwin->w_topline + n;
5890 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5891 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5892 }
5893
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02005894 /* Correct for 'so', except when an operator is pending. */
5895 if (cap->oap->op_type == OP_NOP)
5896 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 beginline(BL_SOL | BL_FIX);
5898}
5899
5900/*
5901 * Cursor right commands.
5902 */
5903 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005904nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905{
5906 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005907 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005909 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5910 {
5911 /* <C-Right> and <S-Right> move a word or WORD right */
5912 if (mod_mask & MOD_MASK_CTRL)
5913 cap->arg = TRUE;
5914 nv_wordcmd(cap);
5915 return;
5916 }
5917
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 cap->oap->motion_type = MCHAR;
5919 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005920 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005923 * In virtual edit mode, there's no such thing as "past_line", as lines
5924 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 */
5926 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005927 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928
5929 for (n = cap->count1; n > 0; --n)
5930 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005931 if ((!past_line && oneright() == FAIL)
5932 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005933 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 {
5935 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005936 * <Space> wraps to next line if 'whichwrap' has 's'.
5937 * 'l' wraps to next line if 'whichwrap' has 'l'.
5938 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 */
5940 if ( ((cap->cmdchar == ' '
5941 && vim_strchr(p_ww, 's') != NULL)
5942 || (cap->cmdchar == 'l'
5943 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005944 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 && vim_strchr(p_ww, '>') != NULL))
5946 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5947 {
5948 /* When deleting we also count the NL as a character.
5949 * Set cap->oap->inclusive when last char in the line is
5950 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005951 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005953 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 cap->oap->inclusive = TRUE;
5955 else
5956 {
5957 ++curwin->w_cursor.lnum;
5958 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 curwin->w_set_curswant = TRUE;
5961 cap->oap->inclusive = FALSE;
5962 }
5963 continue;
5964 }
5965 if (cap->oap->op_type == OP_NOP)
5966 {
5967 /* Only beep and flush if not moved at all */
5968 if (n == cap->count1)
5969 beep_flush();
5970 }
5971 else
5972 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005973 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 cap->oap->inclusive = TRUE;
5975 }
5976 break;
5977 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005978 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 {
5980 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 if (virtual_active())
5982 oneright();
5983 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 if (has_mbyte)
5986 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005987 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 ++curwin->w_cursor.col;
5990 }
5991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 }
5993#ifdef FEAT_FOLDING
5994 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5995 && cap->oap->op_type == OP_NOP)
5996 foldOpenCursor();
5997#endif
5998}
5999
6000/*
6001 * Cursor left commands.
6002 *
6003 * Returns TRUE when operator end should not be adjusted.
6004 */
6005 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006006nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007{
6008 long n;
6009
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006010 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6011 {
6012 /* <C-Left> and <S-Left> move a word or WORD left */
6013 if (mod_mask & MOD_MASK_CTRL)
6014 cap->arg = 1;
6015 nv_bck_word(cap);
6016 return;
6017 }
6018
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 cap->oap->motion_type = MCHAR;
6020 cap->oap->inclusive = FALSE;
6021 for (n = cap->count1; n > 0; --n)
6022 {
6023 if (oneleft() == FAIL)
6024 {
6025 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6026 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6027 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6028 */
6029 if ( (((cap->cmdchar == K_BS
6030 || cap->cmdchar == Ctrl_H)
6031 && vim_strchr(p_ww, 'b') != NULL)
6032 || (cap->cmdchar == 'h'
6033 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006034 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 && vim_strchr(p_ww, '<') != NULL))
6036 && curwin->w_cursor.lnum > 1)
6037 {
6038 --(curwin->w_cursor.lnum);
6039 coladvance((colnr_T)MAXCOL);
6040 curwin->w_set_curswant = TRUE;
6041
6042 /* When the NL before the first char has to be deleted we
6043 * put the cursor on the NUL after the previous line.
6044 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006045 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 if ( (cap->oap->op_type == OP_DELETE
6047 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006048 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006050 char_u *cp = ml_get_cursor();
6051
6052 if (*cp != NUL)
6053 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006054 if (has_mbyte)
6055 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6056 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006057 ++curwin->w_cursor.col;
6058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 cap->retval |= CA_NO_ADJ_OP_END;
6060 }
6061 continue;
6062 }
6063 /* Only beep and flush if not moved at all */
6064 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6065 beep_flush();
6066 break;
6067 }
6068 }
6069#ifdef FEAT_FOLDING
6070 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6071 && cap->oap->op_type == OP_NOP)
6072 foldOpenCursor();
6073#endif
6074}
6075
6076/*
6077 * Cursor up commands.
6078 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6079 */
6080 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006081nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006083 if (mod_mask & MOD_MASK_SHIFT)
6084 {
6085 /* <S-Up> is page up */
6086 cap->arg = BACKWARD;
6087 nv_page(cap);
6088 }
6089 else
6090 {
6091 cap->oap->motion_type = MLINE;
6092 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6093 clearopbeep(cap->oap);
6094 else if (cap->arg)
6095 beginline(BL_WHITE | BL_FIX);
6096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097}
6098
6099/*
6100 * Cursor down commands.
6101 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6102 */
6103 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02006104nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006106 if (mod_mask & MOD_MASK_SHIFT)
6107 {
6108 /* <S-Down> is page down */
6109 cap->arg = FORWARD;
6110 nv_page(cap);
6111 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006112#if defined(FEAT_QUICKFIX)
Bram Moolenaar0a08c632018-07-25 22:36:52 +02006113 /* Quickfix window only: view the result under the cursor. */
6114 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
6115 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02006117 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 {
6119#ifdef FEAT_CMDWIN
6120 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006121 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 cmdwin_result = CAR;
6123 else
6124#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02006125#ifdef FEAT_JOB_CHANNEL
6126 /* In a prompt buffer a <CR> in the last line invokes the callback. */
6127 if (bt_prompt(curbuf) && cap->cmdchar == CAR
6128 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
6129 {
6130 invoke_prompt_callback();
6131 if (restart_edit == 0)
6132 restart_edit = 'a';
6133 }
6134 else
6135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 {
6137 cap->oap->motion_type = MLINE;
6138 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6139 clearopbeep(cap->oap);
6140 else if (cap->arg)
6141 beginline(BL_WHITE | BL_FIX);
6142 }
6143 }
6144}
6145
6146#ifdef FEAT_SEARCHPATH
6147/*
6148 * Grab the file name under the cursor and edit it.
6149 */
6150 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006151nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152{
6153 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006154 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006156 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 {
6158 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006159 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 return;
6161 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006162 if (curbuf_locked())
6163 {
6164 clearop(cap->oap);
6165 return;
6166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006168 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169
6170 if (ptr != NULL)
6171 {
6172 /* do autowrite if necessary */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006173 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02006174 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006176 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006177 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006178 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006179 {
6180 curwin->w_cursor.lnum = lnum;
6181 check_cursor_lnum();
6182 beginline(BL_SOL | BL_FIX);
6183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 vim_free(ptr);
6185 }
6186 else
6187 clearop(cap->oap);
6188}
6189#endif
6190
6191/*
6192 * <End> command: to end of current line or last line.
6193 */
6194 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006195nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006197 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006199 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 nv_goto(cap);
6201 cap->count1 = 1; /* to end of current line */
6202 }
6203 nv_dollar(cap);
6204}
6205
6206/*
6207 * Handle the "$" command.
6208 */
6209 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006210nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211{
6212 cap->oap->motion_type = MCHAR;
6213 cap->oap->inclusive = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 /* In virtual mode when off the edge of a line and an operator
6215 * is pending (whew!) keep the cursor where it is.
6216 * Otherwise, send it to the end of the line. */
6217 if (!virtual_active() || gchar_cursor() != NUL
6218 || cap->oap->op_type == OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6220 if (cursor_down((long)(cap->count1 - 1),
6221 cap->oap->op_type == OP_NOP) == FAIL)
6222 clearopbeep(cap->oap);
6223#ifdef FEAT_FOLDING
6224 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6225 foldOpenCursor();
6226#endif
6227}
6228
6229/*
6230 * Implementation of '?' and '/' commands.
6231 * If cap->arg is TRUE don't set PC mark.
6232 */
6233 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006234nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235{
6236 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02006237 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238
6239 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6240 {
6241 /* Translate "g??" to "g?g?" */
6242 cap->cmdchar = 'g';
6243 cap->nchar = '?';
6244 nv_operator(cap);
6245 return;
6246 }
6247
Bram Moolenaardda933d2016-09-03 21:04:58 +02006248 /* When using 'incsearch' the cursor may be moved to set a different search
6249 * start position. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6251
6252 if (cap->searchbuf == NULL)
6253 {
6254 clearop(oap);
6255 return;
6256 }
6257
Bram Moolenaar46539112015-02-17 15:43:57 +01006258 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006259 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaardda933d2016-09-03 21:04:58 +02006260 ? 0 : SEARCH_MARK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261}
6262
6263/*
6264 * Handle "N" and "n" commands.
6265 * cap->arg is SEARCH_REV for "N", 0 for "n".
6266 */
6267 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006268nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269{
Bram Moolenaar46539112015-02-17 15:43:57 +01006270 pos_T old = curwin->w_cursor;
6271 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6272
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006273 if (i == 1 && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01006274 {
6275 /* Avoid getting stuck on the current cursor position, which can
6276 * happen when an offset is given and the cursor is on the last char
6277 * in the buffer: Repeat with count + 1. */
6278 cap->count1 += 1;
6279 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6280 cap->count1 -= 1;
6281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282}
6283
6284/*
6285 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6286 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01006287 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 */
Bram Moolenaar46539112015-02-17 15:43:57 +01006289 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006290normal_search(
6291 cmdarg_T *cap,
6292 int dir,
6293 char_u *pat,
6294 int opt) /* extra flags for do_search() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295{
6296 int i;
6297
6298 cap->oap->motion_type = MCHAR;
6299 cap->oap->inclusive = FALSE;
6300 cap->oap->use_reg_one = TRUE;
6301 curwin->w_set_curswant = TRUE;
6302
6303 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006304 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 if (i == 0)
6306 clearop(cap->oap);
6307 else
6308 {
6309 if (i == 2)
6310 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312#ifdef FEAT_FOLDING
6313 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6314 foldOpenCursor();
6315#endif
6316 }
6317
6318 /* "/$" will put the cursor after the end of the line, may need to
6319 * correct that here */
6320 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01006321 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322}
6323
6324/*
6325 * Character search commands.
6326 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6327 * ',' and FALSE for ';'.
6328 * cap->nchar is NUL for ',' and ';' (repeat the search)
6329 */
6330 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006331nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332{
6333 int t_cmd;
6334
6335 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6336 t_cmd = TRUE;
6337 else
6338 t_cmd = FALSE;
6339
6340 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6342 clearopbeep(cap->oap);
6343 else
6344 {
6345 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 /* Include a Tab for "tx" and for "dfx". */
6347 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6348 && (t_cmd || cap->oap->op_type != OP_NOP))
6349 {
6350 colnr_T scol, ecol;
6351
6352 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6353 curwin->w_cursor.coladd = ecol - scol;
6354 }
6355 else
6356 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358#ifdef FEAT_FOLDING
6359 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6360 foldOpenCursor();
6361#endif
6362 }
6363}
6364
6365/*
6366 * "[" and "]" commands.
6367 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6368 */
6369 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006370nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006372 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 pos_T prev_pos;
6374 pos_T *pos = NULL; /* init for GCC */
6375 pos_T old_pos; /* cursor position before command */
6376 int flag;
6377 long n;
6378 int findc;
6379 int c;
6380
6381 cap->oap->motion_type = MCHAR;
6382 cap->oap->inclusive = FALSE;
6383 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01006384 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385
6386#ifdef FEAT_SEARCHPATH
6387 /*
6388 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6389 */
6390 if (cap->nchar == 'f')
6391 nv_gotofile(cap);
6392 else
6393#endif
6394
6395#ifdef FEAT_FIND_ID
6396 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006397 * Find the occurrence(s) of the identifier or define under cursor
6398 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 *
6400 * search list jump
6401 * fwd bwd fwd bwd fwd bwd
6402 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6403 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6404 */
6405 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006406# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01006408# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01006410# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 cap->nchar) != NULL)
6412 {
6413 char_u *ptr;
6414 int len;
6415
6416 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6417 clearop(cap->oap);
6418 else
6419 {
6420 find_pattern_in_path(ptr, 0, len, TRUE,
6421 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6422 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6423 cap->count1,
6424 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6425 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6426 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6427 (linenr_T)MAXLNUM);
6428 curwin->w_set_curswant = TRUE;
6429 }
6430 }
6431 else
6432#endif
6433
6434 /*
6435 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6436 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6437 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6438 * "[m" or "]m" search for prev/next start of (Java) method.
6439 * "[M" or "]M" search for prev/next end of (Java) method.
6440 */
6441 if ( (cap->cmdchar == '['
6442 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6443 || (cap->cmdchar == ']'
6444 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6445 {
6446 if (cap->nchar == '*')
6447 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 prev_pos.lnum = 0;
6449 if (cap->nchar == 'm' || cap->nchar == 'M')
6450 {
6451 if (cap->cmdchar == '[')
6452 findc = '{';
6453 else
6454 findc = '}';
6455 n = 9999;
6456 }
6457 else
6458 {
6459 findc = cap->nchar;
6460 n = cap->count1;
6461 }
6462 for ( ; n > 0; --n)
6463 {
6464 if ((pos = findmatchlimit(cap->oap, findc,
6465 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6466 {
6467 if (new_pos.lnum == 0) /* nothing found */
6468 {
6469 if (cap->nchar != 'm' && cap->nchar != 'M')
6470 clearopbeep(cap->oap);
6471 }
6472 else
6473 pos = &new_pos; /* use last one found */
6474 break;
6475 }
6476 prev_pos = new_pos;
6477 curwin->w_cursor = *pos;
6478 new_pos = *pos;
6479 }
6480 curwin->w_cursor = old_pos;
6481
6482 /*
6483 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6484 * brought us to the match for "[m" and "]M" when inside a method.
6485 * Try finding the '{' or '}' we want to be at.
6486 * Also repeat for the given count.
6487 */
6488 if (cap->nchar == 'm' || cap->nchar == 'M')
6489 {
6490 /* norm is TRUE for "]M" and "[m" */
6491 int norm = ((findc == '{') == (cap->nchar == 'm'));
6492
6493 n = cap->count1;
6494 /* found a match: we were inside a method */
6495 if (prev_pos.lnum != 0)
6496 {
6497 pos = &prev_pos;
6498 curwin->w_cursor = prev_pos;
6499 if (norm)
6500 --n;
6501 }
6502 else
6503 pos = NULL;
6504 while (n > 0)
6505 {
6506 for (;;)
6507 {
6508 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6509 {
6510 /* if not found anything, that's an error */
6511 if (pos == NULL)
6512 clearopbeep(cap->oap);
6513 n = 0;
6514 break;
6515 }
6516 c = gchar_cursor();
6517 if (c == '{' || c == '}')
6518 {
6519 /* Must have found end/start of class: use it.
6520 * Or found the place to be at. */
6521 if ((c == findc && norm) || (n == 1 && !norm))
6522 {
6523 new_pos = curwin->w_cursor;
6524 pos = &new_pos;
6525 n = 0;
6526 }
6527 /* if no match found at all, we started outside of the
6528 * class and we're inside now. Just go on. */
6529 else if (new_pos.lnum == 0)
6530 {
6531 new_pos = curwin->w_cursor;
6532 pos = &new_pos;
6533 }
6534 /* found start/end of other method: go to match */
6535 else if ((pos = findmatchlimit(cap->oap, findc,
6536 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6537 0)) == NULL)
6538 n = 0;
6539 else
6540 curwin->w_cursor = *pos;
6541 break;
6542 }
6543 }
6544 --n;
6545 }
6546 curwin->w_cursor = old_pos;
6547 if (pos == NULL && new_pos.lnum != 0)
6548 clearopbeep(cap->oap);
6549 }
6550 if (pos != NULL)
6551 {
6552 setpcmark();
6553 curwin->w_cursor = *pos;
6554 curwin->w_set_curswant = TRUE;
6555#ifdef FEAT_FOLDING
6556 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6557 && cap->oap->op_type == OP_NOP)
6558 foldOpenCursor();
6559#endif
6560 }
6561 }
6562
6563 /*
6564 * "[[", "[]", "]]" and "][": move to start or end of function
6565 */
6566 else if (cap->nchar == '[' || cap->nchar == ']')
6567 {
6568 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6569 flag = '{';
6570 else
6571 flag = '}'; /* "][" or "[]" */
6572
6573 curwin->w_set_curswant = TRUE;
6574 /*
6575 * Imitate strange Vi behaviour: When using "]]" with an operator
6576 * we also stop at '}'.
6577 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006578 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 (cap->oap->op_type != OP_NOP
6580 && cap->arg == FORWARD && flag == '{')))
6581 clearopbeep(cap->oap);
6582 else
6583 {
6584 if (cap->oap->op_type == OP_NOP)
6585 beginline(BL_WHITE | BL_FIX);
6586#ifdef FEAT_FOLDING
6587 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6588 foldOpenCursor();
6589#endif
6590 }
6591 }
6592
6593 /*
6594 * "[p", "[P", "]P" and "]p": put with indent adjustment
6595 */
6596 else if (cap->nchar == 'p' || cap->nchar == 'P')
6597 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006598 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006600 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6601 ? FORWARD : BACKWARD;
6602 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006603 int was_visual = VIsual_active;
6604 int line_count = curbuf->b_ml.ml_line_count;
6605 pos_T start, end;
6606
6607 if (VIsual_active)
6608 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006609 start = LTOREQ_POS(VIsual, curwin->w_cursor)
Bram Moolenaar27bed202014-03-12 17:42:04 +01006610 ? VIsual : curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006611 end = EQUAL_POS(start,VIsual) ? curwin->w_cursor : VIsual;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006612 curwin->w_cursor = (dir == BACKWARD ? start : end);
6613 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006614# ifdef FEAT_CLIPBOARD
6615 adjust_clip_reg(&regname);
6616# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006618
6619 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006620 if (was_visual)
6621 {
6622 VIsual = start;
6623 curwin->w_cursor = end;
6624 if (dir == BACKWARD)
6625 {
6626 /* adjust lines */
6627 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6628 curwin->w_cursor.lnum +=
6629 curbuf->b_ml.ml_line_count - line_count;
6630 }
6631
6632 VIsual_active = TRUE;
6633 if (VIsual_mode == 'V')
6634 {
6635 /* delete visually selected lines */
6636 cap->cmdchar = 'd';
6637 cap->nchar = NUL;
6638 cap->oap->regname = regname;
6639 nv_operator(cap);
6640 do_pending_operator(cap, 0, FALSE);
6641 }
6642 if (VIsual_active)
6643 {
6644 end_visual_mode();
6645 redraw_later(SOME_VALID);
6646 }
6647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 }
6649 }
6650
6651 /*
6652 * "['", "[`", "]'" and "]`": jump to next mark
6653 */
6654 else if (cap->nchar == '\'' || cap->nchar == '`')
6655 {
6656 pos = &curwin->w_cursor;
6657 for (n = cap->count1; n > 0; --n)
6658 {
6659 prev_pos = *pos;
6660 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6661 cap->nchar == '\'');
6662 if (pos == NULL)
6663 break;
6664 }
6665 if (pos == NULL)
6666 pos = &prev_pos;
6667 nv_cursormark(cap, cap->nchar == '\'', pos);
6668 }
6669
6670#ifdef FEAT_MOUSE
6671 /*
6672 * [ or ] followed by a middle mouse click: put selected text with
6673 * indent adjustment. Any other button just does as usual.
6674 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006675 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 {
6677 (void)do_mouse(cap->oap, cap->nchar,
6678 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6679 cap->count1, PUT_FIXINDENT);
6680 }
6681#endif /* FEAT_MOUSE */
6682
6683#ifdef FEAT_FOLDING
6684 /*
6685 * "[z" and "]z": move to start or end of open fold.
6686 */
6687 else if (cap->nchar == 'z')
6688 {
6689 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6690 cap->count1) == FAIL)
6691 clearopbeep(cap->oap);
6692 }
6693#endif
6694
6695#ifdef FEAT_DIFF
6696 /*
6697 * "[c" and "]c": move to next or previous diff-change.
6698 */
6699 else if (cap->nchar == 'c')
6700 {
6701 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6702 cap->count1) == FAIL)
6703 clearopbeep(cap->oap);
6704 }
6705#endif
6706
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006707#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006708 /*
6709 * "[s", "[S", "]s" and "]S": move to next spell error.
6710 */
6711 else if (cap->nchar == 's' || cap->nchar == 'S')
6712 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006713 setpcmark();
6714 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006715 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6716 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006717 {
6718 clearopbeep(cap->oap);
6719 break;
6720 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01006721 else
6722 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006723# ifdef FEAT_FOLDING
6724 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6725 foldOpenCursor();
6726# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006727 }
6728#endif
6729
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 /* Not a valid cap->nchar. */
6731 else
6732 clearopbeep(cap->oap);
6733}
6734
6735/*
6736 * Handle Normal mode "%" command.
6737 */
6738 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006739nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740{
6741 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006742#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 linenr_T lnum = curwin->w_cursor.lnum;
6744#endif
6745
6746 cap->oap->inclusive = TRUE;
6747 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6748 {
6749 if (cap->count0 > 100)
6750 clearopbeep(cap->oap);
6751 else
6752 {
6753 cap->oap->motion_type = MLINE;
6754 setpcmark();
6755 /* Round up, so CTRL-G will give same value. Watch out for a
6756 * large line count, the line number must not go negative! */
6757 if (curbuf->b_ml.ml_line_count > 1000000)
6758 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6759 / 100L * cap->count0;
6760 else
6761 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6762 cap->count0 + 99L) / 100L;
6763 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6764 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6765 beginline(BL_SOL | BL_FIX);
6766 }
6767 }
6768 else /* "%" : go to matching paren */
6769 {
6770 cap->oap->motion_type = MCHAR;
6771 cap->oap->use_reg_one = TRUE;
6772 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6773 clearopbeep(cap->oap);
6774 else
6775 {
6776 setpcmark();
6777 curwin->w_cursor = *pos;
6778 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 }
6782 }
6783#ifdef FEAT_FOLDING
6784 if (cap->oap->op_type == OP_NOP
6785 && lnum != curwin->w_cursor.lnum
6786 && (fdo_flags & FDO_PERCENT)
6787 && KeyTyped)
6788 foldOpenCursor();
6789#endif
6790}
6791
6792/*
6793 * Handle "(" and ")" commands.
6794 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6795 */
6796 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006797nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798{
6799 cap->oap->motion_type = MCHAR;
6800 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006801 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6802 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 curwin->w_set_curswant = TRUE;
6804
6805 if (findsent(cap->arg, cap->count1) == FAIL)
6806 clearopbeep(cap->oap);
6807 else
6808 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006809 /* Don't leave the cursor on the NUL past end of line. */
6810 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812#ifdef FEAT_FOLDING
6813 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6814 foldOpenCursor();
6815#endif
6816 }
6817}
6818
6819/*
6820 * "m" command: Mark a position.
6821 */
6822 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006823nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824{
6825 if (!checkclearop(cap->oap))
6826 {
6827 if (setmark(cap->nchar) == FAIL)
6828 clearopbeep(cap->oap);
6829 }
6830}
6831
6832/*
6833 * "{" and "}" commands.
6834 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6835 */
6836 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006837nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838{
6839 cap->oap->motion_type = MCHAR;
6840 cap->oap->inclusive = FALSE;
6841 cap->oap->use_reg_one = TRUE;
6842 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006843 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 clearopbeep(cap->oap);
6845 else
6846 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848#ifdef FEAT_FOLDING
6849 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6850 foldOpenCursor();
6851#endif
6852 }
6853}
6854
6855/*
6856 * "u" command: Undo or make lower case.
6857 */
6858 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006859nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006861 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 {
6863 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6864 cap->cmdchar = 'g';
6865 cap->nchar = 'u';
6866 nv_operator(cap);
6867 }
6868 else
6869 nv_kundo(cap);
6870}
6871
6872/*
6873 * <Undo> command.
6874 */
6875 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006876nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877{
6878 if (!checkclearopq(cap->oap))
6879 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02006880#ifdef FEAT_JOB_CHANNEL
6881 if (bt_prompt(curbuf))
6882 {
6883 clearopbeep(cap->oap);
6884 return;
6885 }
6886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 u_undo((int)cap->count1);
6888 curwin->w_set_curswant = TRUE;
6889 }
6890}
6891
6892/*
6893 * Handle the "r" command.
6894 */
6895 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006896nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897{
6898 char_u *ptr;
6899 int had_ctrl_v;
6900 long n;
6901
6902 if (checkclearop(cap->oap))
6903 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02006904#ifdef FEAT_JOB_CHANNEL
6905 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6906 {
6907 clearopbeep(cap->oap);
6908 return;
6909 }
6910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911
6912 /* get another character */
6913 if (cap->nchar == Ctrl_V)
6914 {
6915 had_ctrl_v = Ctrl_V;
6916 cap->nchar = get_literal();
6917 /* Don't redo a multibyte character with CTRL-V. */
6918 if (cap->nchar > DEL)
6919 had_ctrl_v = NUL;
6920 }
6921 else
6922 had_ctrl_v = NUL;
6923
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006924 /* Abort if the character is a special key. */
6925 if (IS_SPECIAL(cap->nchar))
6926 {
6927 clearopbeep(cap->oap);
6928 return;
6929 }
6930
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 /* Visual mode "r" */
6932 if (VIsual_active)
6933 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006934 if (got_int)
6935 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01006936 if (had_ctrl_v)
6937 {
Bram Moolenaarf12519d2018-02-06 22:52:49 +01006938 /* Use a special (negative) number to make a difference between a
6939 * literal CR or NL and a line break. */
6940 if (cap->nchar == CAR)
6941 cap->nchar = REPLACE_CR_NCHAR;
6942 else if (cap->nchar == NL)
6943 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01006944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 nv_operator(cap);
6946 return;
6947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 /* Break tabs, etc. */
6950 if (virtual_active())
6951 {
6952 if (u_save_cursor() == FAIL)
6953 return;
6954 if (gchar_cursor() == NUL)
6955 {
6956 /* Add extra space and put the cursor on the first one. */
6957 coladvance_force((colnr_T)(getviscol() + cap->count1));
6958 curwin->w_cursor.col -= cap->count1;
6959 }
6960 else if (gchar_cursor() == TAB)
6961 coladvance_force(getviscol());
6962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006964 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006966 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006967 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 {
6969 clearopbeep(cap->oap);
6970 return;
6971 }
6972
6973 /*
6974 * Replacing with a TAB is done by edit() when it is complicated because
6975 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6976 * Other characters are done below to avoid problems with things like
6977 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6978 */
6979 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6980 {
6981 stuffnumReadbuff(cap->count1);
6982 stuffcharReadbuff('R');
6983 stuffcharReadbuff('\t');
6984 stuffcharReadbuff(ESC);
6985 return;
6986 }
6987
6988 /* save line for undo */
6989 if (u_save_cursor() == FAIL)
6990 return;
6991
6992 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6993 {
6994 /*
6995 * Replace character(s) by a single newline.
6996 * Strange vi behaviour: Only one newline is inserted.
6997 * Delete the characters here.
6998 * Insert the newline with an insert command, takes care of
6999 * autoindent. The insert command depends on being on the last
7000 * character of a line or not.
7001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 (void)del_chars(cap->count1, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 stuffcharReadbuff('\r');
7004 stuffcharReadbuff(ESC);
7005
7006 /* Give 'r' to edit(), to get the redo command right. */
7007 invoke_edit(cap, TRUE, 'r', FALSE);
7008 }
7009 else
7010 {
7011 prep_redo(cap->oap->regname, cap->count1,
7012 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7013
7014 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 if (has_mbyte)
7016 {
7017 int old_State = State;
7018
7019 if (cap->ncharC1 != 0)
7020 AppendCharToRedobuff(cap->ncharC1);
7021 if (cap->ncharC2 != 0)
7022 AppendCharToRedobuff(cap->ncharC2);
7023
7024 /* This is slow, but it handles replacing a single-byte with a
7025 * multi-byte and the other way around. Also handles adding
7026 * composing characters for utf-8. */
7027 for (n = cap->count1; n > 0; --n)
7028 {
7029 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007030 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7031 {
7032 int c = ins_copychar(curwin->w_cursor.lnum
7033 + (cap->nchar == Ctrl_Y ? -1 : 1));
7034 if (c != NUL)
7035 ins_char(c);
7036 else
7037 /* will be decremented further down */
7038 ++curwin->w_cursor.col;
7039 }
7040 else
7041 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 State = old_State;
7043 if (cap->ncharC1 != 0)
7044 ins_char(cap->ncharC1);
7045 if (cap->ncharC2 != 0)
7046 ins_char(cap->ncharC2);
7047 }
7048 }
7049 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 {
7051 /*
7052 * Replace the characters within one line.
7053 */
7054 for (n = cap->count1; n > 0; --n)
7055 {
7056 /*
7057 * Get ptr again, because u_save and/or showmatch() will have
7058 * released the line. At the same time we let know that the
7059 * line will be changed.
7060 */
7061 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007062 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7063 {
7064 int c = ins_copychar(curwin->w_cursor.lnum
7065 + (cap->nchar == Ctrl_Y ? -1 : 1));
7066 if (c != NUL)
7067 ptr[curwin->w_cursor.col] = c;
7068 }
7069 else
7070 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 if (p_sm && msg_silent == 0)
7072 showmatch(cap->nchar);
7073 ++curwin->w_cursor.col;
7074 }
7075#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007076 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007078 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079
Bram Moolenaar009b2592004-10-24 19:18:58 +00007080 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007081 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007083 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 }
7085#endif
7086
7087 /* mark the buffer as changed and prepare for displaying */
7088 changed_bytes(curwin->w_cursor.lnum,
7089 (colnr_T)(curwin->w_cursor.col - cap->count1));
7090 }
7091 --curwin->w_cursor.col; /* cursor on the last replaced char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 /* if the character on the left of the current cursor is a multi-byte
7093 * character, move two characters left */
7094 if (has_mbyte)
7095 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 curbuf->b_op_end = curwin->w_cursor;
7097 curwin->w_set_curswant = TRUE;
7098 set_last_insert(cap->nchar);
7099 }
7100}
7101
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102/*
7103 * 'o': Exchange start and end of Visual area.
7104 * 'O': same, but in block mode exchange left and right corners.
7105 */
7106 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007107v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108{
7109 pos_T old_cursor;
7110 colnr_T left, right;
7111
7112 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7113 {
7114 old_cursor = curwin->w_cursor;
7115 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7116 curwin->w_cursor.lnum = VIsual.lnum;
7117 coladvance(left);
7118 VIsual = curwin->w_cursor;
7119
7120 curwin->w_cursor.lnum = old_cursor.lnum;
7121 curwin->w_curswant = right;
7122 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7123 * right one column */
7124 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7125 ++curwin->w_curswant;
7126 coladvance(curwin->w_curswant);
7127 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007129 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 {
7131 curwin->w_cursor.lnum = VIsual.lnum;
7132 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7133 ++right;
7134 coladvance(right);
7135 VIsual = curwin->w_cursor;
7136
7137 curwin->w_cursor.lnum = old_cursor.lnum;
7138 coladvance(left);
7139 curwin->w_curswant = left;
7140 }
7141 }
7142 else
7143 {
7144 old_cursor = curwin->w_cursor;
7145 curwin->w_cursor = VIsual;
7146 VIsual = old_cursor;
7147 curwin->w_set_curswant = TRUE;
7148 }
7149}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150
7151/*
7152 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7153 */
7154 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007155nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 if (VIsual_active) /* "R" is replace lines */
7158 {
7159 cap->cmdchar = 'c';
7160 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007161 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 VIsual_mode = 'V';
7163 nv_operator(cap);
7164 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007165 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 {
7167 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007168 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 else
7170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 if (virtual_active())
7172 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7174 }
7175 }
7176}
7177
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178/*
7179 * "gr".
7180 */
7181 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007182nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 if (VIsual_active)
7185 {
7186 cap->cmdchar = 'r';
7187 cap->nchar = cap->extra_char;
7188 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7189 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007190 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 {
7192 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007193 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 else
7195 {
7196 if (cap->extra_char == Ctrl_V) /* get another character */
7197 cap->extra_char = get_literal();
7198 stuffcharReadbuff(cap->extra_char);
7199 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 if (virtual_active())
7201 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 invoke_edit(cap, TRUE, 'v', FALSE);
7203 }
7204 }
7205}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206
7207/*
7208 * Swap case for "~" command, when it does not work like an operator.
7209 */
7210 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007211n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212{
7213 long n;
7214 pos_T startpos;
7215 int did_change = 0;
7216#ifdef FEAT_NETBEANS_INTG
7217 pos_T pos;
7218 char_u *ptr;
7219 int count;
7220#endif
7221
7222 if (checkclearopq(cap->oap))
7223 return;
7224
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007225 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 {
7227 clearopbeep(cap->oap);
7228 return;
7229 }
7230
7231 prep_redo_cmd(cap);
7232
7233 if (u_save_cursor() == FAIL)
7234 return;
7235
7236 startpos = curwin->w_cursor;
7237#ifdef FEAT_NETBEANS_INTG
7238 pos = startpos;
7239#endif
7240 for (n = cap->count1; n > 0; --n)
7241 {
7242 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7243 inc_cursor();
7244 if (gchar_cursor() == NUL)
7245 {
7246 if (vim_strchr(p_ww, '~') != NULL
7247 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7248 {
7249#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007250 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 {
7252 if (did_change)
7253 {
7254 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007255 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007256 netbeans_removed(curbuf, pos.lnum, pos.col,
7257 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007259 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 }
7261 pos.col = 0;
7262 pos.lnum++;
7263 }
7264#endif
7265 ++curwin->w_cursor.lnum;
7266 curwin->w_cursor.col = 0;
7267 if (n > 1)
7268 {
7269 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7270 break;
7271 u_clearline();
7272 }
7273 }
7274 else
7275 break;
7276 }
7277 }
7278#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007279 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 {
7281 ptr = ml_get(pos.lnum);
7282 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007283 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7284 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 }
7286#endif
7287
7288
7289 check_cursor();
7290 curwin->w_set_curswant = TRUE;
7291 if (did_change)
7292 {
7293 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7294 0L);
7295 curbuf->b_op_start = startpos;
7296 curbuf->b_op_end = curwin->w_cursor;
7297 if (curbuf->b_op_end.col > 0)
7298 --curbuf->b_op_end.col;
7299 }
7300}
7301
7302/*
7303 * Move cursor to mark.
7304 */
7305 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007306nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307{
7308 if (check_mark(pos) == FAIL)
7309 clearop(cap->oap);
7310 else
7311 {
7312 if (cap->cmdchar == '\''
7313 || cap->cmdchar == '`'
7314 || cap->cmdchar == '['
7315 || cap->cmdchar == ']')
7316 setpcmark();
7317 curwin->w_cursor = *pos;
7318 if (flag)
7319 beginline(BL_WHITE | BL_FIX);
7320 else
7321 check_cursor();
7322 }
7323 cap->oap->motion_type = flag ? MLINE : MCHAR;
7324 if (cap->cmdchar == '`')
7325 cap->oap->use_reg_one = TRUE;
7326 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7327 curwin->w_set_curswant = TRUE;
7328}
7329
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330/*
7331 * Handle commands that are operators in Visual mode.
7332 */
7333 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007334v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335{
7336 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7337
7338 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007339 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 if (isupper(cap->cmdchar))
7341 {
7342 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007343 {
7344 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7348 curwin->w_curswant = MAXCOL;
7349 }
7350 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7351 nv_operator(cap);
7352}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353
7354/*
7355 * "s" and "S" commands.
7356 */
7357 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007358nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359{
Bram Moolenaard96ff162018-02-18 22:13:29 +01007360#ifdef FEAT_TERMINAL
7361 /* When showing output of term_dumpdiff() swap the top and botom. */
7362 if (term_swap_diff() == OK)
7363 return;
7364#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02007365#ifdef FEAT_JOB_CHANNEL
7366 if (bt_prompt(curbuf) && !prompt_curpos_editable())
7367 {
7368 clearopbeep(cap->oap);
7369 return;
7370 }
7371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7373 {
7374 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007375 {
7376 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 cap->cmdchar = 'c';
7380 nv_operator(cap);
7381 }
7382 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 nv_optrans(cap);
7384}
7385
7386/*
7387 * Abbreviated commands.
7388 */
7389 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007390nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391{
7392 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7393 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7394
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 /* in Visual mode these commands are operators */
7396 if (VIsual_active)
7397 v_visop(cap);
7398 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 nv_optrans(cap);
7400}
7401
7402/*
7403 * Translate a command into another command.
7404 */
7405 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007406nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407{
7408 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7409 (char_u *)"d$", (char_u *)"c$",
7410 (char_u *)"cl", (char_u *)"cc",
7411 (char_u *)"yy", (char_u *)":s\r"};
7412 static char_u *str = (char_u *)"xXDCsSY&";
7413
7414 if (!checkclearopq(cap->oap))
7415 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007416 /* In Vi "2D" doesn't delete the next line. Can't translate it
7417 * either, because "2." should also not use the count. */
7418 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7419 {
7420 cap->oap->start = curwin->w_cursor;
7421 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007422#ifdef FEAT_EVAL
7423 set_op_var(OP_DELETE);
7424#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007425 cap->count1 = 1;
7426 nv_dollar(cap);
7427 finish_op = TRUE;
7428 ResetRedobuff();
7429 AppendCharToRedobuff('D');
7430 }
7431 else
7432 {
7433 if (cap->count0)
7434 stuffnumReadbuff(cap->count0);
7435 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 }
7438 cap->opcount = 0;
7439}
7440
7441/*
7442 * "'" and "`" commands. Also for "g'" and "g`".
7443 * cap->arg is TRUE for "'" and "g'".
7444 */
7445 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007446nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447{
7448 pos_T *pos;
7449 int c;
7450#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007451 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7453#endif
7454
7455 if (cap->cmdchar == 'g')
7456 c = cap->extra_char;
7457 else
7458 c = cap->nchar;
7459 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7460 if (pos == (pos_T *)-1) /* jumped to other file */
7461 {
7462 if (cap->arg)
7463 {
7464 check_cursor_lnum();
7465 beginline(BL_WHITE | BL_FIX);
7466 }
7467 else
7468 check_cursor();
7469 }
7470 else
7471 nv_cursormark(cap, cap->arg, pos);
7472
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 /* May need to clear the coladd that a mark includes. */
7474 if (!virtual_active())
7475 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02007476 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477#ifdef FEAT_FOLDING
7478 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007479 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007480 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 && (fdo_flags & FDO_MARK)
7482 && old_KeyTyped)
7483 foldOpenCursor();
7484#endif
7485}
7486
7487/*
7488 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7489 */
7490 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007491nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492{
7493#ifdef FEAT_JUMPLIST
7494 pos_T *pos;
7495# ifdef FEAT_FOLDING
7496 linenr_T lnum = curwin->w_cursor.lnum;
7497 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7498# endif
7499
7500 if (!checkclearopq(cap->oap))
7501 {
7502 if (cap->cmdchar == 'g')
7503 pos = movechangelist((int)cap->count1);
7504 else
7505 pos = movemark((int)cap->count1);
7506 if (pos == (pos_T *)-1) /* jump to other file */
7507 {
7508 curwin->w_set_curswant = TRUE;
7509 check_cursor();
7510 }
7511 else if (pos != NULL) /* can jump */
7512 nv_cursormark(cap, FALSE, pos);
7513 else if (cap->cmdchar == 'g')
7514 {
7515 if (curbuf->b_changelistlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007516 emsg(_("E664: changelist is empty"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 else if (cap->count1 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007518 emsg(_("E662: At start of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007520 emsg(_("E663: At end of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 }
7522 else
7523 clearopbeep(cap->oap);
7524# ifdef FEAT_FOLDING
7525 if (cap->oap->op_type == OP_NOP
7526 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7527 && (fdo_flags & FDO_MARK)
7528 && old_KeyTyped)
7529 foldOpenCursor();
7530# endif
7531 }
7532#else
7533 clearopbeep(cap->oap);
7534#endif
7535}
7536
7537/*
7538 * Handle '"' command.
7539 */
7540 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007541nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542{
7543 if (checkclearop(cap->oap))
7544 return;
7545#ifdef FEAT_EVAL
7546 if (cap->nchar == '=')
7547 cap->nchar = get_expr_register();
7548#endif
7549 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7550 {
7551 cap->oap->regname = cap->nchar;
7552 cap->opcount = cap->count0; /* remember count before '"' */
7553#ifdef FEAT_EVAL
7554 set_reg_var(cap->oap->regname);
7555#endif
7556 }
7557 else
7558 clearopbeep(cap->oap);
7559}
7560
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561/*
7562 * Handle "v", "V" and "CTRL-V" commands.
7563 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7564 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007565 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 */
7567 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007568nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007570 if (cap->cmdchar == Ctrl_Q)
7571 cap->cmdchar = Ctrl_V;
7572
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7574 * characterwise, linewise, or blockwise. */
7575 if (cap->oap->op_type != OP_NOP)
7576 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01007577 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 finish_op = FALSE; /* operator doesn't finish now but later */
7579 return;
7580 }
7581
7582 VIsual_select = cap->arg;
7583 if (VIsual_active) /* change Visual mode */
7584 {
7585 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7586 end_visual_mode();
7587 else /* toggle char/block mode */
7588 { /* or char/line mode */
7589 VIsual_mode = cap->cmdchar;
7590 showmode();
7591 }
7592 redraw_curbuf_later(INVERTED); /* update the inversion */
7593 }
7594 else /* start Visual mode */
7595 {
7596 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007597 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007599 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 VIsual = curwin->w_cursor;
7601
7602 VIsual_active = TRUE;
7603 VIsual_reselect = TRUE;
7604 if (!cap->arg)
7605 /* start Select mode when 'selectmode' contains "cmd" */
7606 may_start_select('c');
7607#ifdef FEAT_MOUSE
7608 setmouse();
7609#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007610 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 redraw_cmdline = TRUE; /* show visual mode later */
7612 /*
7613 * For V and ^V, we multiply the number of lines even if there
7614 * was only one -- webb
7615 */
7616 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7617 {
7618 curwin->w_cursor.lnum +=
7619 resel_VIsual_line_count * cap->count0 - 1;
7620 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7621 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7622 }
7623 VIsual_mode = resel_VIsual_mode;
7624 if (VIsual_mode == 'v')
7625 {
7626 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007627 {
7628 validate_virtcol();
7629 curwin->w_curswant = curwin->w_virtcol
7630 + resel_VIsual_vcol * cap->count0 - 1;
7631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007633 curwin->w_curswant = resel_VIsual_vcol;
7634 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007636 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 {
7638 curwin->w_curswant = MAXCOL;
7639 coladvance((colnr_T)MAXCOL);
7640 }
7641 else if (VIsual_mode == Ctrl_V)
7642 {
7643 validate_virtcol();
7644 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007645 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 coladvance(curwin->w_curswant);
7647 }
7648 else
7649 curwin->w_set_curswant = TRUE;
7650 redraw_curbuf_later(INVERTED); /* show the inversion */
7651 }
7652 else
7653 {
7654 if (!cap->arg)
7655 /* start Select mode when 'selectmode' contains "cmd" */
7656 may_start_select('c');
7657 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007658 if (VIsual_mode != 'V' && *p_sel == 'e')
7659 ++cap->count1; /* include one more char */
7660 if (cap->count0 > 0 && --cap->count1 > 0)
7661 {
7662 /* With a count select that many characters or lines. */
7663 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7664 nv_right(cap);
7665 else if (VIsual_mode == 'V')
7666 nv_down(cap);
7667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 }
7669 }
7670}
7671
7672/*
7673 * Start selection for Shift-movement keys.
7674 */
7675 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007676start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677{
7678 /* if 'selectmode' contains "key", start Select mode */
7679 may_start_select('k');
7680 n_start_visual_mode('v');
7681}
7682
7683/*
7684 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7685 */
7686 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007687may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688{
7689 VIsual_select = (stuff_empty() && typebuf_typed()
7690 && (vim_strchr(p_slm, c) != NULL));
7691}
7692
7693/*
7694 * Start Visual mode "c".
7695 * Should set VIsual_select before calling this.
7696 */
7697 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007698n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007700#ifdef FEAT_CONCEAL
7701 /* Check for redraw before changing the state. */
Bram Moolenaarb9464822018-05-10 15:09:49 +02007702 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007703#endif
7704
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 VIsual_mode = c;
7706 VIsual_active = TRUE;
7707 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007708
7709 // Corner case: the 0 position in a tab may change when going into
7710 // virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007712 {
7713 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 VIsual = curwin->w_cursor;
7717
7718#ifdef FEAT_FOLDING
7719 foldAdjustVisual();
7720#endif
7721
7722#ifdef FEAT_MOUSE
7723 setmouse();
7724#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007725#ifdef FEAT_CONCEAL
7726 /* Check for redraw after changing the state. */
Bram Moolenaarb9464822018-05-10 15:09:49 +02007727 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007728#endif
7729
Bram Moolenaar09df3122006-01-23 22:23:09 +00007730 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 redraw_cmdline = TRUE; /* show visual mode later */
7732#ifdef FEAT_CLIPBOARD
7733 /* Make sure the clipboard gets updated. Needed because start and
7734 * end may still be the same, and the selection needs to be owned */
7735 clip_star.vmode = NUL;
7736#endif
7737
7738 /* Only need to redraw this line, unless still need to redraw an old
7739 * Visual area (when 'lazyredraw' is set). */
7740 if (curwin->w_redr_type < INVERTED)
7741 {
7742 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7743 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7744 }
7745}
7746
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747
7748/*
7749 * CTRL-W: Window commands
7750 */
7751 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007752nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753{
Bram Moolenaar938783d2017-07-16 20:13:26 +02007754 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007755 {
Bram Moolenaar938783d2017-07-16 20:13:26 +02007756 /* "CTRL-W :" is the same as typing ":"; useful in a terminal window */
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007757 cap->cmdchar = ':';
7758 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02007759 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007760 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02007761 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763}
7764
7765/*
7766 * CTRL-Z: Suspend
7767 */
7768 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007769nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770{
7771 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 if (VIsual_active)
7773 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 do_cmdline_cmd((char_u *)"st");
7775}
7776
7777/*
7778 * Commands starting with "g".
7779 */
7780 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007781nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782{
7783 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 int i;
7786 int flag = FALSE;
7787
7788 switch (cap->nchar)
7789 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007790 case Ctrl_A:
7791 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792#ifdef MEM_PROFILE
7793 /*
7794 * "g^A": dump log of used memory.
7795 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007796 if (!VIsual_active && cap->nchar == Ctrl_A)
7797 vim_mem_profile_dump();
7798 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007800 /*
7801 * "g^A/g^X": sequentially increment visually selected region
7802 */
7803 if (VIsual_active)
7804 {
7805 cap->arg = TRUE;
7806 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01007807 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007808 nv_addsub(cap);
7809 }
7810 else
7811 clearopbeep(oap);
7812 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 /*
7815 * "gR": Enter virtual replace mode.
7816 */
7817 case 'R':
7818 cap->arg = TRUE;
7819 nv_Replace(cap);
7820 break;
7821
7822 case 'r':
7823 nv_vreplace(cap);
7824 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825
7826 case '&':
7827 do_cmdline_cmd((char_u *)"%s//~/&");
7828 break;
7829
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 /*
7831 * "gv": Reselect the previous Visual area. If Visual already active,
7832 * exchange previous and current Visual area.
7833 */
7834 case 'v':
7835 if (checkclearop(oap))
7836 break;
7837
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007838 if ( curbuf->b_visual.vi_start.lnum == 0
7839 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7840 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 beep_flush();
7842 else
7843 {
7844 /* set w_cursor to the start of the Visual area, tpos to the end */
7845 if (VIsual_active)
7846 {
7847 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007848 VIsual_mode = curbuf->b_visual.vi_mode;
7849 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850# ifdef FEAT_EVAL
7851 curbuf->b_visual_mode_eval = i;
7852# endif
7853 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007854 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7855 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007857 tpos = curbuf->b_visual.vi_end;
7858 curbuf->b_visual.vi_end = curwin->w_cursor;
7859 curwin->w_cursor = curbuf->b_visual.vi_start;
7860 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 }
7862 else
7863 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007864 VIsual_mode = curbuf->b_visual.vi_mode;
7865 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7866 tpos = curbuf->b_visual.vi_end;
7867 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 }
7869
7870 VIsual_active = TRUE;
7871 VIsual_reselect = TRUE;
7872
7873 /* Set Visual to the start and w_cursor to the end of the Visual
7874 * area. Make sure they are on an existing character. */
7875 check_cursor();
7876 VIsual = curwin->w_cursor;
7877 curwin->w_cursor = tpos;
7878 check_cursor();
7879 update_topline();
7880 /*
7881 * When called from normal "g" command: start Select mode when
7882 * 'selectmode' contains "cmd". When called for K_SELECT, always
7883 * start Select mode.
7884 */
7885 if (cap->arg)
7886 VIsual_select = TRUE;
7887 else
7888 may_start_select('c');
7889#ifdef FEAT_MOUSE
7890 setmouse();
7891#endif
7892#ifdef FEAT_CLIPBOARD
7893 /* Make sure the clipboard gets updated. Needed because start and
7894 * end are still the same, and the selection needs to be owned */
7895 clip_star.vmode = NUL;
7896#endif
7897 redraw_curbuf_later(INVERTED);
7898 showmode();
7899 }
7900 break;
7901 /*
7902 * "gV": Don't reselect the previous Visual area after a Select mode
7903 * mapping of menu.
7904 */
7905 case 'V':
7906 VIsual_reselect = FALSE;
7907 break;
7908
7909 /*
7910 * "gh": start Select mode.
7911 * "gH": start Select line mode.
7912 * "g^H": start Select block mode.
7913 */
7914 case K_BS:
7915 cap->nchar = Ctrl_H;
7916 /* FALLTHROUGH */
7917 case 'h':
7918 case 'H':
7919 case Ctrl_H:
7920# ifdef EBCDIC
7921 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7922 if (cap->nchar == Ctrl_H)
7923 cap->cmdchar = Ctrl_V;
7924 else
7925# endif
7926 cap->cmdchar = cap->nchar + ('v' - 'h');
7927 cap->arg = TRUE;
7928 nv_visual(cap);
7929 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02007930
7931 /* "gn", "gN" visually select next/previous search match
7932 * "gn" selects next match
7933 * "gN" selects previous match
7934 */
7935 case 'N':
7936 case 'n':
7937 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02007938 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02007939 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940
7941 /*
7942 * "gj" and "gk" two new funny movement keys -- up and down
7943 * movement based on *screen* line rather than *file* line.
7944 */
7945 case 'j':
7946 case K_DOWN:
7947 /* with 'nowrap' it works just like the normal "j" command; also when
7948 * in a closed fold */
7949 if (!curwin->w_p_wrap
7950#ifdef FEAT_FOLDING
7951 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7952#endif
7953 )
7954 {
7955 oap->motion_type = MLINE;
7956 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7957 }
7958 else
7959 i = nv_screengo(oap, FORWARD, cap->count1);
7960 if (i == FAIL)
7961 clearopbeep(oap);
7962 break;
7963
7964 case 'k':
7965 case K_UP:
7966 /* with 'nowrap' it works just like the normal "k" command; also when
7967 * in a closed fold */
7968 if (!curwin->w_p_wrap
7969#ifdef FEAT_FOLDING
7970 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7971#endif
7972 )
7973 {
7974 oap->motion_type = MLINE;
7975 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7976 }
7977 else
7978 i = nv_screengo(oap, BACKWARD, cap->count1);
7979 if (i == FAIL)
7980 clearopbeep(oap);
7981 break;
7982
7983 /*
7984 * "gJ": join two lines without inserting a space.
7985 */
7986 case 'J':
7987 nv_join(cap);
7988 break;
7989
7990 /*
7991 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7992 * "gm": middle of "g0" and "g$".
7993 */
7994 case '^':
7995 flag = TRUE;
7996 /* FALLTHROUGH */
7997
7998 case '0':
7999 case 'm':
8000 case K_HOME:
8001 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 oap->motion_type = MCHAR;
8003 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008004 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008006 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 int width2 = width1 + curwin_col_off2();
8008
8009 validate_virtcol();
8010 i = 0;
8011 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8012 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8013 }
8014 else
8015 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008016 /* Go to the middle of the screen line. When 'number' or
8017 * 'relativenumber' is on and lines are wrapping the middle can be more
8018 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02008020 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 + ((curwin->w_p_wrap && i > 0)
8022 ? curwin_col_off2() : 0)) / 2;
8023 coladvance((colnr_T)i);
8024 if (flag)
8025 {
8026 do
8027 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01008028 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008029 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 }
8031 curwin->w_set_curswant = TRUE;
8032 break;
8033
8034 case '_':
8035 /* "g_": to the last non-blank character in the line or <count> lines
8036 * downward. */
8037 cap->oap->motion_type = MCHAR;
8038 cap->oap->inclusive = TRUE;
8039 curwin->w_curswant = MAXCOL;
8040 if (cursor_down((long)(cap->count1 - 1),
8041 cap->oap->op_type == OP_NOP) == FAIL)
8042 clearopbeep(cap->oap);
8043 else
8044 {
8045 char_u *ptr = ml_get_curline();
8046
8047 /* In Visual mode we may end up after the line. */
8048 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8049 --curwin->w_cursor.col;
8050
8051 /* Decrease the cursor column until it's on a non-blank. */
8052 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01008053 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 --curwin->w_cursor.col;
8055 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008056 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 }
8058 break;
8059
8060 case '$':
8061 case K_END:
8062 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 {
8064 int col_off = curwin_col_off();
8065
8066 oap->motion_type = MCHAR;
8067 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008068 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 {
8070 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8071 if (cap->count1 == 1)
8072 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008073 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 int width2 = width1 + curwin_col_off2();
8075
8076 validate_virtcol();
8077 i = width1 - 1;
8078 if (curwin->w_virtcol >= (colnr_T)width1)
8079 i += ((curwin->w_virtcol - width1) / width2 + 1)
8080 * width2;
8081 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008082
8083 /* Make sure we stick in this column. */
8084 validate_virtcol();
8085 curwin->w_curswant = curwin->w_virtcol;
8086 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8088 {
8089 /*
8090 * Check for landing on a character that got split at
8091 * the end of the line. We do not want to advance to
8092 * the next screen line.
8093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 if (curwin->w_virtcol > (colnr_T)i)
8095 --curwin->w_cursor.col;
8096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 }
8098 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8099 clearopbeep(oap);
8100 }
8101 else
8102 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008103 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008105
8106 /* Make sure we stick in this column. */
8107 validate_virtcol();
8108 curwin->w_curswant = curwin->w_virtcol;
8109 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 }
8111 }
8112 break;
8113
8114 /*
8115 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8116 */
8117 case '*':
8118 case '#':
8119#if POUND != '#'
8120 case POUND: /* pound sign (sometimes equal to '#') */
8121#endif
8122 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8123 case ']': /* :tselect for current identifier */
8124 nv_ident(cap);
8125 break;
8126
8127 /*
8128 * ge and gE: go back to end of word
8129 */
8130 case 'e':
8131 case 'E':
8132 oap->motion_type = MCHAR;
8133 curwin->w_set_curswant = TRUE;
8134 oap->inclusive = TRUE;
8135 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8136 clearopbeep(oap);
8137 break;
8138
8139 /*
8140 * "g CTRL-G": display info about cursor position
8141 */
8142 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01008143 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 break;
8145
8146 /*
8147 * "gi": start Insert at the last position.
8148 */
8149 case 'i':
8150 if (curbuf->b_last_insert.lnum != 0)
8151 {
8152 curwin->w_cursor = curbuf->b_last_insert;
8153 check_cursor_lnum();
8154 i = (int)STRLEN(ml_get_curline());
8155 if (curwin->w_cursor.col > (colnr_T)i)
8156 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 if (virtual_active())
8158 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 curwin->w_cursor.col = i;
8160 }
8161 }
8162 cap->cmdchar = 'i';
8163 nv_edit(cap);
8164 break;
8165
8166 /*
8167 * "gI": Start insert in column 1.
8168 */
8169 case 'I':
8170 beginline(0);
8171 if (!checkclearopq(oap))
8172 invoke_edit(cap, FALSE, 'g', FALSE);
8173 break;
8174
8175#ifdef FEAT_SEARCHPATH
8176 /*
8177 * "gf": goto file, edit file under cursor
8178 * "]f" and "[f": can also be used.
8179 */
8180 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008181 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 nv_gotofile(cap);
8183 break;
8184#endif
8185
8186 /* "g'm" and "g`m": jump to mark without setting pcmark */
8187 case '\'':
8188 cap->arg = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008189 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 case '`':
8191 nv_gomark(cap);
8192 break;
8193
8194 /*
8195 * "gs": Goto sleep.
8196 */
8197 case 's':
8198 do_sleep(cap->count1 * 1000L);
8199 break;
8200
8201 /*
8202 * "ga": Display the ascii value of the character under the
8203 * cursor. It is displayed in decimal, hex, and octal. -- webb
8204 */
8205 case 'a':
8206 do_ascii(NULL);
8207 break;
8208
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 /*
8210 * "g8": Display the bytes used for the UTF-8 character under the
8211 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008212 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 */
8214 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008215 if (cap->count0 == 8)
8216 utf_find_illegal();
8217 else
8218 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220
Bram Moolenaar60402d62017-04-20 18:54:50 +02008221 /* "g<": show scrollback text */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008222 case '<':
8223 show_sb_text();
8224 break;
8225
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 /*
8227 * "gg": Goto the first line in file. With a count it goes to
8228 * that line number like for "G". -- webb
8229 */
8230 case 'g':
8231 cap->arg = FALSE;
8232 nv_goto(cap);
8233 break;
8234
8235 /*
8236 * Two-character operators:
8237 * "gq" Format text
8238 * "gw" Format text and keep cursor position
8239 * "g~" Toggle the case of the text.
8240 * "gu" Change text to lower case.
8241 * "gU" Change text to upper case.
8242 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008243 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 */
8245 case 'q':
8246 case 'w':
8247 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008248 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 case '~':
8250 case 'u':
8251 case 'U':
8252 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008253 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 nv_operator(cap);
8255 break;
8256
8257 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008258 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 * current function
8260 * "gD": idem, but in the current file.
8261 */
8262 case 'd':
8263 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008264 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 break;
8266
8267#ifdef FEAT_MOUSE
8268 /*
8269 * g<*Mouse> : <C-*mouse>
8270 */
8271 case K_MIDDLEMOUSE:
8272 case K_MIDDLEDRAG:
8273 case K_MIDDLERELEASE:
8274 case K_LEFTMOUSE:
8275 case K_LEFTDRAG:
8276 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01008277 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 case K_RIGHTMOUSE:
8279 case K_RIGHTDRAG:
8280 case K_RIGHTRELEASE:
8281 case K_X1MOUSE:
8282 case K_X1DRAG:
8283 case K_X1RELEASE:
8284 case K_X2MOUSE:
8285 case K_X2DRAG:
8286 case K_X2RELEASE:
8287 mod_mask = MOD_MASK_CTRL;
8288 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8289 break;
8290#endif
8291
8292 case K_IGNORE:
8293 break;
8294
8295 /*
8296 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8297 */
8298 case 'p':
8299 case 'P':
8300 nv_put(cap);
8301 break;
8302
8303#ifdef FEAT_BYTEOFF
8304 /* "go": goto byte count from start of buffer */
8305 case 'o':
8306 goto_byte(cap->count0);
8307 break;
8308#endif
8309
8310 /* "gQ": improved Ex mode */
8311 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008312 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 {
8314 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008315 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 break;
8317 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008318
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 if (!checkclearopq(oap))
8320 do_exmode(TRUE);
8321 break;
8322
8323#ifdef FEAT_JUMPLIST
8324 case ',':
8325 nv_pcmark(cap);
8326 break;
8327
8328 case ';':
8329 cap->count1 = -cap->count1;
8330 nv_pcmark(cap);
8331 break;
8332#endif
8333
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008334 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008335 if (!checkclearop(oap))
8336 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008337 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008338 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008339 if (!checkclearop(oap))
8340 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008341 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008342
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008343 case '+':
8344 case '-': /* "g+" and "g-": undo or redo along the timeline */
8345 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008346 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008347 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008348 break;
8349
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 default:
8351 clearopbeep(oap);
8352 break;
8353 }
8354}
8355
8356/*
8357 * Handle "o" and "O" commands.
8358 */
8359 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008360n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008362#ifdef FEAT_CONCEAL
8363 linenr_T oldline = curwin->w_cursor.lnum;
8364#endif
8365
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 if (!checkclearopq(cap->oap))
8367 {
8368#ifdef FEAT_FOLDING
8369 if (cap->cmdchar == 'O')
8370 /* Open above the first line of a folded sequence of lines */
8371 (void)hasFolding(curwin->w_cursor.lnum,
8372 &curwin->w_cursor.lnum, NULL);
8373 else
8374 /* Open below the last line of a folded sequence of lines */
8375 (void)hasFolding(curwin->w_cursor.lnum,
8376 NULL, &curwin->w_cursor.lnum);
8377#endif
8378 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8379 (cap->cmdchar == 'O' ? 1 : 0)),
8380 (linenr_T)(curwin->w_cursor.lnum +
8381 (cap->cmdchar == 'o' ? 1 : 0))
8382 ) == OK
8383 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8384#ifdef FEAT_COMMENTS
8385 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8386#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02008387 0, 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008389#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008390 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01008391 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02008392#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008393#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02008394 if (curwin->w_p_cul)
8395 /* force redraw of cursorline */
8396 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008397#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008398 /* When '#' is in 'cpoptions' ignore the count. */
8399 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8400 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8402 }
8403 }
8404}
8405
8406/*
8407 * "." command: redo last change.
8408 */
8409 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008410nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411{
8412 if (!checkclearopq(cap->oap))
8413 {
8414 /*
8415 * If "restart_edit" is TRUE, the last but one command is repeated
8416 * instead of the last command (inserting text). This is used for
8417 * CTRL-O <.> in insert mode.
8418 */
8419 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8420 clearopbeep(cap->oap);
8421 }
8422}
8423
8424/*
8425 * CTRL-R: undo undo
8426 */
8427 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008428nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429{
8430 if (!checkclearopq(cap->oap))
8431 {
8432 u_redo((int)cap->count1);
8433 curwin->w_set_curswant = TRUE;
8434 }
8435}
8436
8437/*
8438 * Handle "U" command.
8439 */
8440 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008441nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442{
8443 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008444 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 {
8446 /* translate "gUU" to "gUgU" */
8447 cap->cmdchar = 'g';
8448 cap->nchar = 'U';
8449 nv_operator(cap);
8450 }
8451 else if (!checkclearopq(cap->oap))
8452 {
8453 u_undoline();
8454 curwin->w_set_curswant = TRUE;
8455 }
8456}
8457
8458/*
8459 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8460 * single character.
8461 */
8462 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008463nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008465 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02008466 {
8467#ifdef FEAT_JOB_CHANNEL
8468 if (bt_prompt(curbuf) && !prompt_curpos_editable())
8469 {
8470 clearopbeep(cap->oap);
8471 return;
8472 }
8473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 else
8477 nv_operator(cap);
8478}
8479
8480/*
8481 * Handle an operator command.
8482 * The actual work is done by do_pending_operator().
8483 */
8484 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008485nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486{
8487 int op_type;
8488
8489 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008490#ifdef FEAT_JOB_CHANNEL
8491 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
8492 {
8493 clearopbeep(cap->oap);
8494 return;
8495 }
8496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497
8498 if (op_type == cap->oap->op_type) /* double operator works on lines */
8499 nv_lineop(cap);
8500 else if (!checkclearop(cap->oap))
8501 {
8502 cap->oap->start = curwin->w_cursor;
8503 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008504#ifdef FEAT_EVAL
8505 set_op_var(op_type);
8506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 }
8508}
8509
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008510#ifdef FEAT_EVAL
8511/*
8512 * Set v:operator to the characters for "optype".
8513 */
8514 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008515set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008516{
8517 char_u opchars[3];
8518
8519 if (optype == OP_NOP)
8520 set_vim_var_string(VV_OP, NULL, 0);
8521 else
8522 {
8523 opchars[0] = get_op_char(optype);
8524 opchars[1] = get_extra_op_char(optype);
8525 opchars[2] = NUL;
8526 set_vim_var_string(VV_OP, opchars, -1);
8527 }
8528}
8529#endif
8530
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531/*
8532 * Handle linewise operator "dd", "yy", etc.
8533 *
8534 * "_" is is a strange motion command that helps make operators more logical.
8535 * It is actually implemented, but not documented in the real Vi. This motion
8536 * command actually refers to "the current line". Commands like "dd" and "yy"
8537 * are really an alternate form of "d_" and "y_". It does accept a count, so
8538 * "d3_" works to delete 3 lines.
8539 */
8540 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008541nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542{
8543 cap->oap->motion_type = MLINE;
8544 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8545 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008546 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8547 && cap->oap->motion_force != 'v'
8548 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 || cap->oap->op_type == OP_LSHIFT
8550 || cap->oap->op_type == OP_RSHIFT)
8551 beginline(BL_SOL | BL_FIX);
8552 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8553 beginline(BL_WHITE | BL_FIX);
8554}
8555
8556/*
8557 * <Home> command.
8558 */
8559 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008560nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008562 /* CTRL-HOME is like "gg" */
8563 if (mod_mask & MOD_MASK_CTRL)
8564 nv_goto(cap);
8565 else
8566 {
8567 cap->count0 = 1;
8568 nv_pipe(cap);
8569 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008570 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8571 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572}
8573
8574/*
8575 * "|" command.
8576 */
8577 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008578nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579{
8580 cap->oap->motion_type = MCHAR;
8581 cap->oap->inclusive = FALSE;
8582 beginline(0);
8583 if (cap->count0 > 0)
8584 {
8585 coladvance((colnr_T)(cap->count0 - 1));
8586 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8587 }
8588 else
8589 curwin->w_curswant = 0;
8590 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008591 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 curwin->w_set_curswant = FALSE;
8593}
8594
8595/*
8596 * Handle back-word command "b" and "B".
8597 * cap->arg is 1 for "B"
8598 */
8599 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008600nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601{
8602 cap->oap->motion_type = MCHAR;
8603 cap->oap->inclusive = FALSE;
8604 curwin->w_set_curswant = TRUE;
8605 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8606 clearopbeep(cap->oap);
8607#ifdef FEAT_FOLDING
8608 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8609 foldOpenCursor();
8610#endif
8611}
8612
8613/*
8614 * Handle word motion commands "e", "E", "w" and "W".
8615 * cap->arg is TRUE for "E" and "W".
8616 */
8617 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008618nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619{
8620 int n;
8621 int word_end;
8622 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008623 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624
8625 /*
8626 * Set inclusive for the "E" and "e" command.
8627 */
8628 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8629 word_end = TRUE;
8630 else
8631 word_end = FALSE;
8632 cap->oap->inclusive = word_end;
8633
8634 /*
8635 * "cw" and "cW" are a special case.
8636 */
8637 if (!word_end && cap->oap->op_type == OP_CHANGE)
8638 {
8639 n = gchar_cursor();
8640 if (n != NUL) /* not an empty line */
8641 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008642 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 {
8644 /*
8645 * Reproduce a funny Vi behaviour: "cw" on a blank only
8646 * changes one character, not all blanks until the start of
8647 * the next word. Only do this when the 'w' flag is included
8648 * in 'cpoptions'.
8649 */
8650 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8651 {
8652 cap->oap->inclusive = TRUE;
8653 cap->oap->motion_type = MCHAR;
8654 return;
8655 }
8656 }
8657 else
8658 {
8659 /*
8660 * This is a little strange. To match what the real Vi does,
8661 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8662 * that we are not on a space or a TAB. This seems impolite
8663 * at first, but it's really more what we mean when we say
8664 * 'cw'.
8665 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008666 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 * will change only one character! This is done by setting
8668 * flag.
8669 */
8670 cap->oap->inclusive = TRUE;
8671 word_end = TRUE;
8672 flag = TRUE;
8673 }
8674 }
8675 }
8676
8677 cap->oap->motion_type = MCHAR;
8678 curwin->w_set_curswant = TRUE;
8679 if (word_end)
8680 n = end_word(cap->count1, cap->arg, flag, FALSE);
8681 else
8682 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8683
Bram Moolenaardfefb982008-04-01 10:06:39 +00008684 /* Don't leave the cursor on the NUL past the end of line. Unless we
8685 * didn't move it forward. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008686 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008687 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688
8689 if (n == FAIL && cap->oap->op_type == OP_NOP)
8690 clearopbeep(cap->oap);
8691 else
8692 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694#ifdef FEAT_FOLDING
8695 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8696 foldOpenCursor();
8697#endif
8698 }
8699}
8700
8701/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008702 * Used after a movement command: If the cursor ends up on the NUL after the
8703 * end of the line, may move it back to the last character and make the motion
8704 * inclusive.
8705 */
8706 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008707adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008708{
8709 /* The cursor cannot remain on the NUL when:
8710 * - the column is > 0
8711 * - not in Visual mode or 'selection' is "o"
8712 * - 'virtualedit' is not "all" and not "onemore".
8713 */
8714 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008715 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01008716 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008717 {
8718 --curwin->w_cursor.col;
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008719 /* prevent cursor from moving on the trail byte */
8720 if (has_mbyte)
8721 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008722 oap->inclusive = TRUE;
8723 }
8724}
8725
8726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 * "0" and "^" commands.
8728 * cap->arg is the argument for beginline().
8729 */
8730 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008731nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732{
8733 cap->oap->motion_type = MCHAR;
8734 cap->oap->inclusive = FALSE;
8735 beginline(cap->arg);
8736#ifdef FEAT_FOLDING
8737 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8738 foldOpenCursor();
8739#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008740 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8741 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742}
8743
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744/*
8745 * In exclusive Visual mode, may include the last character.
8746 */
8747 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008748adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749{
8750 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008751 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 if (has_mbyte)
8754 inc_cursor();
8755 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 ++curwin->w_cursor.col;
8757 cap->oap->inclusive = FALSE;
8758 }
8759}
8760
8761/*
8762 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8763 * Should check VIsual_mode before calling this.
8764 * Returns TRUE when backed up to the previous line.
8765 */
8766 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008767unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768{
8769 pos_T *pp;
8770
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008771 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008773 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 pp = &curwin->w_cursor;
8775 else
8776 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 if (pp->coladd > 0)
8778 --pp->coladd;
8779 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 if (pp->col > 0)
8781 {
8782 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008783 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 }
8785 else if (pp->lnum > 1)
8786 {
8787 --pp->lnum;
8788 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8789 return TRUE;
8790 }
8791 }
8792 return FALSE;
8793}
8794
8795/*
8796 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8797 */
8798 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008799nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800{
8801 if (VIsual_active)
8802 VIsual_select = TRUE;
8803 else if (VIsual_reselect)
8804 {
8805 cap->nchar = 'v'; /* fake "gv" command */
8806 cap->arg = TRUE;
8807 nv_g_cmd(cap);
8808 }
8809}
8810
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811
8812/*
8813 * "G", "gg", CTRL-END, CTRL-HOME.
8814 * cap->arg is TRUE for "G".
8815 */
8816 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008817nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818{
8819 linenr_T lnum;
8820
8821 if (cap->arg)
8822 lnum = curbuf->b_ml.ml_line_count;
8823 else
8824 lnum = 1L;
8825 cap->oap->motion_type = MLINE;
8826 setpcmark();
8827
8828 /* When a count is given, use it instead of the default lnum */
8829 if (cap->count0 != 0)
8830 lnum = cap->count0;
8831 if (lnum < 1L)
8832 lnum = 1L;
8833 else if (lnum > curbuf->b_ml.ml_line_count)
8834 lnum = curbuf->b_ml.ml_line_count;
8835 curwin->w_cursor.lnum = lnum;
8836 beginline(BL_SOL | BL_FIX);
8837#ifdef FEAT_FOLDING
8838 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8839 foldOpenCursor();
8840#endif
8841}
8842
8843/*
8844 * CTRL-\ in Normal mode.
8845 */
8846 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008847nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848{
8849 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8850 {
8851 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008852 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 clear_cmdline = TRUE; /* unshow mode later */
8854 restart_edit = 0;
8855#ifdef FEAT_CMDWIN
8856 if (cmdwin_type != 0)
8857 cmdwin_result = Ctrl_C;
8858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 if (VIsual_active)
8860 {
8861 end_visual_mode(); /* stop Visual */
8862 redraw_curbuf_later(INVERTED);
8863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8865 if (cap->nchar == Ctrl_G && p_im)
8866 restart_edit = 'a';
8867 }
8868 else
8869 clearopbeep(cap->oap);
8870}
8871
8872/*
8873 * ESC in Normal mode: beep, but don't flush buffers.
8874 * Don't even beep if we are canceling a command.
8875 */
8876 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008877nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878{
8879 int no_reason;
8880
8881 no_reason = (cap->oap->op_type == OP_NOP
8882 && cap->opcount == 0
8883 && cap->count0 == 0
8884 && cap->oap->regname == 0
8885 && !p_im);
8886
8887 if (cap->arg) /* TRUE for CTRL-C */
8888 {
8889 if (restart_edit == 0
8890#ifdef FEAT_CMDWIN
8891 && cmdwin_type == 0
8892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 && no_reason)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008895 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896
8897 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8898 * set again below when halfway a mapping. */
8899 if (!p_im)
8900 restart_edit = 0;
8901#ifdef FEAT_CMDWIN
8902 if (cmdwin_type != 0)
8903 {
8904 cmdwin_result = K_IGNORE;
8905 got_int = FALSE; /* don't stop executing autocommands et al. */
8906 return;
8907 }
8908#endif
8909 }
8910
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 if (VIsual_active)
8912 {
8913 end_visual_mode(); /* stop Visual */
8914 check_cursor_col(); /* make sure cursor is not beyond EOL */
8915 curwin->w_set_curswant = TRUE;
8916 redraw_curbuf_later(INVERTED);
8917 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008918 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008919 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 clearop(cap->oap);
8921
8922 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8923 * set return to Insert mode afterwards. */
Bram Moolenaare2c38102016-01-31 14:55:40 +01008924 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 restart_edit = 'a';
8926}
8927
8928/*
8929 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01008930 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 */
8932 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008933nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934{
8935 /* <Insert> is equal to "i" */
8936 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8937 cap->cmdchar = 'i';
8938
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 /* in Visual mode "A" and "I" are an operator */
8940 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02008941 {
8942#ifdef FEAT_TERMINAL
8943 if (term_in_normal_mode())
8944 {
8945 end_visual_mode();
8946 clearop(cap->oap);
8947 term_enter_job_mode();
8948 return;
8949 }
8950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02008952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953
8954 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008955 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8956 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 {
8958#ifdef FEAT_TEXTOBJ
8959 nv_object(cap);
8960#else
8961 clearopbeep(cap->oap);
8962#endif
8963 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02008964#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02008965 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02008966 {
8967 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02008968 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02008969 return;
8970 }
8971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 else if (!curbuf->b_p_ma && !p_im)
8973 {
8974 /* Only give this error when 'insertmode' is off. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008975 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01008977 if (cap->cmdchar == K_PS)
8978 /* drop the pasted text */
8979 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 }
Bram Moolenaara1891842017-02-04 21:34:31 +01008981 else if (cap->cmdchar == K_PS && VIsual_active)
8982 {
8983 pos_T old_pos = curwin->w_cursor;
8984 pos_T old_visual = VIsual;
8985
8986 /* In Visual mode the selected text is deleted. */
8987 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
8988 {
8989 shift_delete_registers();
8990 cap->oap->regname = '1';
8991 }
8992 else
8993 cap->oap->regname = '-';
8994 cap->cmdchar = 'd';
8995 cap->nchar = NUL;
8996 nv_operator(cap);
8997 do_pending_operator(cap, 0, FALSE);
8998 cap->cmdchar = K_PS;
8999
9000 /* When the last char in the line was deleted then append. Detect this
9001 * by checking if the cursor moved to before the Visual area. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009002 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
9003 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01009004 inc_cursor();
9005
9006 /* Insert to replace the deleted text with the pasted text. */
9007 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 else if (!checkclearopq(cap->oap))
9010 {
9011 switch (cap->cmdchar)
9012 {
9013 case 'A': /* "A"ppend after the line */
9014 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 if (ve_flags == VE_ALL)
9016 {
9017 int save_State = State;
9018
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009019 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 * character past the end of the line */
9021 State = INSERT;
9022 coladvance((colnr_T)MAXCOL);
9023 State = save_State;
9024 }
9025 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9027 break;
9028
9029 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009030 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9031 beginline(BL_WHITE);
9032 else
9033 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 break;
9035
Bram Moolenaara1891842017-02-04 21:34:31 +01009036 case K_PS:
9037 /* Bracketed paste works like "a"ppend, unless the cursor is in
9038 * the first column, then it inserts. */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009039 if (curwin->w_cursor.col == 0)
9040 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02009041 /* FALLTHROUGH */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009042
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 case 'a': /* "a"ppend is like "i"nsert on the next character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 /* increment coladd when in virtual space, increment the
9045 * column otherwise, also to append after an unprintable char */
9046 if (virtual_active()
9047 && (curwin->w_cursor.coladd > 0
9048 || *ml_get_cursor() == NUL
9049 || *ml_get_cursor() == TAB))
9050 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01009051 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 inc_cursor();
9053 break;
9054 }
9055
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9057 {
9058 int save_State = State;
9059
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009060 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 * character past the end of the line */
9062 State = INSERT;
9063 coladvance(getviscol());
9064 State = save_State;
9065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066
9067 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9068 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009069 else if (cap->cmdchar == K_PS)
9070 /* drop the pasted text */
9071 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072}
9073
9074/*
9075 * Invoke edit() and take care of "restart_edit" and the return value.
9076 */
9077 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009078invoke_edit(
9079 cmdarg_T *cap,
9080 int repl, /* "r" or "gr" command */
9081 int cmd,
9082 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083{
9084 int restart_edit_save = 0;
9085
9086 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9087 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9088 * it. */
9089 if (repl || !stuff_empty())
9090 restart_edit_save = restart_edit;
9091 else
9092 restart_edit_save = 0;
9093
9094 /* Always reset "restart_edit", this is not a restarted edit. */
9095 restart_edit = 0;
9096
9097 if (edit(cmd, startln, cap->count1))
9098 cap->retval |= CA_COMMAND_BUSY;
9099
9100 if (restart_edit == 0)
9101 restart_edit = restart_edit_save;
9102}
9103
9104#ifdef FEAT_TEXTOBJ
9105/*
9106 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9107 */
9108 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009109nv_object(
9110 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111{
9112 int flag;
9113 int include;
9114 char_u *mps_save;
9115
9116 if (cap->cmdchar == 'i')
9117 include = FALSE; /* "ix" = inner object: exclude white space */
9118 else
9119 include = TRUE; /* "ax" = an object: include white space */
9120
9121 /* Make sure (), [], {} and <> are in 'matchpairs' */
9122 mps_save = curbuf->b_p_mps;
9123 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9124
9125 switch (cap->nchar)
9126 {
9127 case 'w': /* "aw" = a word */
9128 flag = current_word(cap->oap, cap->count1, include, FALSE);
9129 break;
9130 case 'W': /* "aW" = a WORD */
9131 flag = current_word(cap->oap, cap->count1, include, TRUE);
9132 break;
9133 case 'b': /* "ab" = a braces block */
9134 case '(':
9135 case ')':
9136 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9137 break;
9138 case 'B': /* "aB" = a Brackets block */
9139 case '{':
9140 case '}':
9141 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9142 break;
9143 case '[': /* "a[" = a [] block */
9144 case ']':
9145 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9146 break;
9147 case '<': /* "a<" = a <> block */
9148 case '>':
9149 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9150 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009151 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01009152 /* Do not adjust oap->end in do_pending_operator()
9153 * otherwise there are different results for 'dit'
9154 * (note leading whitespace in last line):
9155 * 1) <b> 2) <b>
9156 * foobar foobar
9157 * </b> </b>
9158 */
9159 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009160 flag = current_tagblock(cap->oap, cap->count1, include);
9161 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 case 'p': /* "ap" = a paragraph */
9163 flag = current_par(cap->oap, cap->count1, include, 'p');
9164 break;
9165 case 's': /* "as" = a sentence */
9166 flag = current_sent(cap->oap, cap->count1, include);
9167 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009168 case '"': /* "a"" = a double quoted string */
9169 case '\'': /* "a'" = a single quoted string */
9170 case '`': /* "a`" = a backtick quoted string */
9171 flag = current_quote(cap->oap, cap->count1, include,
9172 cap->nchar);
9173 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174#if 0 /* TODO */
9175 case 'S': /* "aS" = a section */
9176 case 'f': /* "af" = a filename */
9177 case 'u': /* "au" = a URL */
9178#endif
9179 default:
9180 flag = FAIL;
9181 break;
9182 }
9183
9184 curbuf->b_p_mps = mps_save;
9185 if (flag == FAIL)
9186 clearopbeep(cap->oap);
9187 adjust_cursor_col();
9188 curwin->w_set_curswant = TRUE;
9189}
9190#endif
9191
9192/*
9193 * "q" command: Start/stop recording.
9194 * "q:", "q/", "q?": edit command-line in command-line window.
9195 */
9196 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009197nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198{
9199 if (cap->oap->op_type == OP_FORMAT)
9200 {
9201 /* "gqq" is the same as "gqgq": format line */
9202 cap->cmdchar = 'g';
9203 cap->nchar = 'q';
9204 nv_operator(cap);
9205 }
9206 else if (!checkclearop(cap->oap))
9207 {
9208#ifdef FEAT_CMDWIN
9209 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9210 {
9211 stuffcharReadbuff(cap->nchar);
9212 stuffcharReadbuff(K_CMDWIN);
9213 }
9214 else
9215#endif
9216 /* (stop) recording into a named register, unless executing a
9217 * register */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009218 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 clearopbeep(cap->oap);
9220 }
9221}
9222
9223/*
9224 * Handle the "@r" command.
9225 */
9226 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009227nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228{
9229 if (checkclearop(cap->oap))
9230 return;
9231#ifdef FEAT_EVAL
9232 if (cap->nchar == '=')
9233 {
9234 if (get_expr_register() == NUL)
9235 return;
9236 }
9237#endif
9238 while (cap->count1-- && !got_int)
9239 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009240 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 {
9242 clearopbeep(cap->oap);
9243 break;
9244 }
9245 line_breakcheck();
9246 }
9247}
9248
9249/*
9250 * Handle the CTRL-U and CTRL-D commands.
9251 */
9252 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009253nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254{
9255 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9256 || (cap->cmdchar == Ctrl_D
9257 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9258 clearopbeep(cap->oap);
9259 else if (!checkclearop(cap->oap))
9260 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9261}
9262
9263/*
9264 * Handle "J" or "gJ" command.
9265 */
9266 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009267nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 if (VIsual_active) /* join the visual lines */
9270 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009271 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 {
9273 if (cap->count0 <= 1)
9274 cap->count0 = 2; /* default for join is two lines! */
9275 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9276 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277 {
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009278 /* can't join when on the last line */
9279 if (cap->count0 <= 2)
9280 {
9281 clearopbeep(cap->oap);
9282 return;
9283 }
9284 cap->count0 = curbuf->b_ml.ml_line_count
9285 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009287
9288 prep_redo(cap->oap->regname, cap->count0,
9289 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9290 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 }
9292}
9293
9294/*
9295 * "P", "gP", "p" and "gp" commands.
9296 */
9297 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009298nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 int regname = 0;
9301 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009302 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009303 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 int dir;
9305 int flags = 0;
9306
9307 if (cap->oap->op_type != OP_NOP)
9308 {
9309#ifdef FEAT_DIFF
9310 /* "dp" is ":diffput" */
9311 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9312 {
9313 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009314 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 }
9316 else
9317#endif
9318 clearopbeep(cap->oap);
9319 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02009320#ifdef FEAT_JOB_CHANNEL
9321 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
9322 {
9323 clearopbeep(cap->oap);
9324 }
9325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 else
9327 {
9328 dir = (cap->cmdchar == 'P'
9329 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9330 ? BACKWARD : FORWARD;
9331 prep_redo_cmd(cap);
9332 if (cap->cmdchar == 'g')
9333 flags |= PUT_CURSEND;
9334
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 if (VIsual_active)
9336 {
9337 /* Putting in Visual mode: The put text replaces the selected
9338 * text. First delete the selected text, then put the new text.
9339 * Need to save and restore the registers that the delete
9340 * overwrites if the old contents is being put.
9341 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009342 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009344#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009346#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009347 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009348 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009349#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352
9353 )
9354 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009355 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 * put, save it first. */
9357 reg1 = get_register(regname, TRUE);
9358 }
9359
9360 /* Now delete the selected text. */
9361 cap->cmdchar = 'd';
9362 cap->nchar = NUL;
9363 cap->oap->regname = NUL;
9364 nv_operator(cap);
9365 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009366 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367
9368 /* delete PUT_LINE_BACKWARD; */
9369 cap->oap->regname = regname;
9370
9371 if (reg1 != NULL)
9372 {
9373 /* Delete probably changed the register we want to put, save
9374 * it first. Then put back what was there before the delete. */
9375 reg2 = get_register(regname, FALSE);
9376 put_register(regname, reg1);
9377 }
9378
9379 /* When deleted a linewise Visual area, put the register as
9380 * lines to avoid it joined with the next line. When deletion was
9381 * characterwise, split a line when putting lines. */
9382 if (VIsual_mode == 'V')
9383 flags |= PUT_LINE;
9384 else if (VIsual_mode == 'v')
9385 flags |= PUT_LINE_SPLIT;
9386 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9387 flags |= PUT_LINE_FORWARD;
9388 dir = BACKWARD;
9389 if ((VIsual_mode != 'V'
9390 && curwin->w_cursor.col < curbuf->b_op_start.col)
9391 || (VIsual_mode == 'V'
9392 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9393 /* cursor is at the end of the line or end of file, put
9394 * forward. */
9395 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009396 /* May have been reset in do_put(). */
9397 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 do_put(cap->oap->regname, dir, cap->count1, flags);
9400
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 /* If a register was saved, put it back now. */
9402 if (reg2 != NULL)
9403 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009404
9405 /* What to reselect with "gv"? Selecting the just put text seems to
9406 * be the most useful, since the original text was removed. */
9407 if (was_visual)
9408 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009409 curbuf->b_visual.vi_start = curbuf->b_op_start;
9410 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01009411 /* need to adjust cursor position */
9412 if (*p_sel == 'e')
9413 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009414 }
9415
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009416 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009417 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009418 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009419 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009420 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009421
9422 /* If the cursor was in that line, move it to the end of the last
9423 * line. */
9424 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9425 {
9426 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9427 coladvance((colnr_T)MAXCOL);
9428 }
9429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 auto_format(FALSE, TRUE);
9431 }
9432}
9433
9434/*
9435 * "o" and "O" commands.
9436 */
9437 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009438nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439{
9440#ifdef FEAT_DIFF
9441 /* "do" is ":diffget" */
9442 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9443 {
9444 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009445 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 }
9447 else
9448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 if (VIsual_active) /* switch start and end of visual */
9450 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009451#ifdef FEAT_JOB_CHANNEL
9452 else if (bt_prompt(curbuf))
9453 {
9454 clearopbeep(cap->oap);
9455 }
9456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 n_opencmd(cap);
9459}
9460
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461#ifdef FEAT_NETBEANS_INTG
9462 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009463nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464{
9465 netbeans_keycommand(cap->nchar);
9466}
9467#endif
9468
9469#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009471nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472{
9473 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9474}
9475#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009476
Bram Moolenaar3918c952005-03-15 22:34:55 +00009477/*
9478 * Trigger CursorHold event.
9479 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9480 * input buffer. "did_cursorhold" is set to avoid retriggering.
9481 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009482 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009483nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00009484{
9485 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9486 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009487 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009488}
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009489
9490/*
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009491 * Calculate start/end virtual columns for operating in block mode.
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009492 */
9493 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009494get_op_vcol(
9495 oparg_T *oap,
9496 colnr_T redo_VIsual_vcol,
9497 int initial) /* when TRUE adjust position for 'selectmode' */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009498{
9499 colnr_T start, end;
9500
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009501 if (VIsual_mode != Ctrl_V
Bram Moolenaar02631462017-09-22 15:20:32 +02009502 || (!initial && oap->end.col < curwin->w_width))
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009503 return;
9504
Bram Moolenaar10ad1d92015-09-25 19:35:02 +02009505 oap->block_mode = TRUE;
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009506
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009507 /* prevent from moving onto a trail byte */
9508 if (has_mbyte)
9509 mb_adjustpos(curwin->w_buffer, &oap->end);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009510
9511 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009512
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009513 if (!redo_VIsual_busy)
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009514 {
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009515 getvvcol(curwin, &(oap->end), &start, NULL, &end);
9516
9517 if (start < oap->start_vcol)
9518 oap->start_vcol = start;
9519 if (end > oap->end_vcol)
9520 {
9521 if (initial && *p_sel == 'e' && start >= 1
9522 && start - 1 >= oap->end_vcol)
9523 oap->end_vcol = start - 1;
9524 else
9525 oap->end_vcol = end;
9526 }
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009527 }
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009528
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009529 /* if '$' was used, get oap->end_vcol from longest line */
9530 if (curwin->w_curswant == MAXCOL)
9531 {
9532 curwin->w_cursor.col = MAXCOL;
9533 oap->end_vcol = 0;
9534 for (curwin->w_cursor.lnum = oap->start.lnum;
9535 curwin->w_cursor.lnum <= oap->end.lnum;
9536 ++curwin->w_cursor.lnum)
9537 {
9538 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
9539 if (end > oap->end_vcol)
9540 oap->end_vcol = end;
9541 }
9542 }
9543 else if (redo_VIsual_busy)
9544 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
9545 /*
9546 * Correct oap->end.col and oap->start.col to be the
9547 * upper-left and lower-right corner of the block area.
9548 *
9549 * (Actually, this does convert column positions into character
9550 * positions)
9551 */
9552 curwin->w_cursor.lnum = oap->end.lnum;
9553 coladvance(oap->end_vcol);
9554 oap->end = curwin->w_cursor;
9555
9556 curwin->w_cursor = oap->start;
9557 coladvance(oap->start_vcol);
9558 oap->start = curwin->w_cursor;
9559}