blob: a15ae358901971de94b9c4b44fc6900426a15d46 [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 Moolenaar792cf5e2019-09-30 23:12:16 +020017static int VIsual_mode_orig = NUL; // saved Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +000018static int restart_VIsual_select = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010020#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010021static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount);
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010022#endif
Bram Moolenaareae1b912019-05-09 15:12:55 +020023static int nv_compare(const void *s1, const void *s2);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010024static void unshift_special(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010026static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
28
29/*
30 * nv_*(): functions called to handle Normal and Visual mode commands.
31 * n_*(): functions called to handle Normal mode commands.
32 * v_*(): functions called to handle Visual mode commands.
33 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static void nv_ignore(cmdarg_T *cap);
35static void nv_nop(cmdarg_T *cap);
36static void nv_error(cmdarg_T *cap);
37static void nv_help(cmdarg_T *cap);
38static void nv_addsub(cmdarg_T *cap);
39static void nv_page(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010040static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void nv_ver_scrollbar(cmdarg_T *cap);
43static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000044#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000045#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010046static void nv_tabline(cmdarg_T *cap);
47static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000048#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010049static void nv_exmode(cmdarg_T *cap);
50static void nv_colon(cmdarg_T *cap);
51static void nv_ctrlg(cmdarg_T *cap);
52static void nv_ctrlh(cmdarg_T *cap);
53static void nv_clear(cmdarg_T *cap);
54static void nv_ctrlo(cmdarg_T *cap);
55static void nv_hat(cmdarg_T *cap);
56static void nv_Zet(cmdarg_T *cap);
57static void nv_ident(cmdarg_T *cap);
58static void nv_tagpop(cmdarg_T *cap);
59static void nv_scroll(cmdarg_T *cap);
60static void nv_right(cmdarg_T *cap);
61static void nv_left(cmdarg_T *cap);
62static void nv_up(cmdarg_T *cap);
63static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010064static void nv_end(cmdarg_T *cap);
65static void nv_dollar(cmdarg_T *cap);
66static void nv_search(cmdarg_T *cap);
67static void nv_next(cmdarg_T *cap);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +020068static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt, int *wrapped);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010069static void nv_csearch(cmdarg_T *cap);
70static void nv_brackets(cmdarg_T *cap);
71static void nv_percent(cmdarg_T *cap);
72static void nv_brace(cmdarg_T *cap);
73static void nv_mark(cmdarg_T *cap);
74static void nv_findpar(cmdarg_T *cap);
75static void nv_undo(cmdarg_T *cap);
76static void nv_kundo(cmdarg_T *cap);
77static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010078static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010079static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
80static void v_visop(cmdarg_T *cap);
81static void nv_subst(cmdarg_T *cap);
82static void nv_abbrev(cmdarg_T *cap);
83static void nv_optrans(cmdarg_T *cap);
84static void nv_gomark(cmdarg_T *cap);
85static void nv_pcmark(cmdarg_T *cap);
86static void nv_regname(cmdarg_T *cap);
87static void nv_visual(cmdarg_T *cap);
88static void n_start_visual_mode(int c);
89static void nv_window(cmdarg_T *cap);
90static void nv_suspend(cmdarg_T *cap);
91static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010092static void nv_dot(cmdarg_T *cap);
93static void nv_redo(cmdarg_T *cap);
94static void nv_Undo(cmdarg_T *cap);
95static void nv_tilde(cmdarg_T *cap);
96static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000097#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010098static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000099#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100100static void nv_lineop(cmdarg_T *cap);
101static void nv_home(cmdarg_T *cap);
102static void nv_pipe(cmdarg_T *cap);
103static void nv_bck_word(cmdarg_T *cap);
104static void nv_wordcmd(cmdarg_T *cap);
105static void nv_beginline(cmdarg_T *cap);
106static void adjust_cursor(oparg_T *oap);
107static void adjust_for_sel(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100108static void nv_select(cmdarg_T *cap);
109static void nv_goto(cmdarg_T *cap);
110static void nv_normal(cmdarg_T *cap);
111static void nv_esc(cmdarg_T *oap);
112static void nv_edit(cmdarg_T *cap);
113static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100115static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100117static void nv_record(cmdarg_T *cap);
118static void nv_at(cmdarg_T *cap);
119static void nv_halfpage(cmdarg_T *cap);
120static void nv_join(cmdarg_T *cap);
121static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200122static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100123static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100125static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#endif
127#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100128static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100130static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000132static char *e_noident = N_("E349: No identifier under cursor");
133
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134/*
135 * Function to be called for a Normal or Visual mode command.
136 * The argument is a cmdarg_T.
137 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100138typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100140// Values for cmd_flags.
141#define NV_NCH 0x01 // may need to get a second char
142#define NV_NCH_NOP (0x02|NV_NCH) // get second char when no operator pending
143#define NV_NCH_ALW (0x04|NV_NCH) // always get a second char
144#define NV_LANG 0x08 // second char needs language adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100146#define NV_SS 0x10 // may start selection
147#define NV_SSS 0x20 // may start selection with shift modifier
148#define NV_STS 0x40 // may stop selection without shift modif.
149#define NV_RL 0x80 // 'rightleft' modifies command
150#define NV_KEEPREG 0x100 // don't clear regname
151#define NV_NCW 0x200 // not allowed in command-line window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
153/*
154 * Generally speaking, every Normal mode command should either clear any
155 * pending operator (with *clearop*()), or set the motion type variable
156 * oap->motion_type.
157 *
158 * When a cursor motion command is made, it is marked as being a character or
159 * line oriented motion. Then, if an operator is in effect, the operation
160 * becomes character or line oriented accordingly.
161 */
162
163/*
164 * This table contains one entry for every Normal or Visual mode command.
165 * The order doesn't matter, init_normal_cmds() will create a sorted index.
166 * It is faster when all keys from zero to '~' are present.
167 */
168static const struct nv_cmd
169{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100170 int cmd_char; // (first) command character
171 nv_func_T cmd_func; // function for this command
172 short_u cmd_flags; // NV_ flags
173 short cmd_arg; // value for ca.arg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174} nv_cmds[] =
175{
176 {NUL, nv_error, 0, 0},
177 {Ctrl_A, nv_addsub, 0, 0},
178 {Ctrl_B, nv_page, NV_STS, BACKWARD},
179 {Ctrl_C, nv_esc, 0, TRUE},
180 {Ctrl_D, nv_halfpage, 0, 0},
181 {Ctrl_E, nv_scroll_line, 0, TRUE},
182 {Ctrl_F, nv_page, NV_STS, FORWARD},
183 {Ctrl_G, nv_ctrlg, 0, 0},
184 {Ctrl_H, nv_ctrlh, 0, 0},
185 {Ctrl_I, nv_pcmark, 0, 0},
186 {NL, nv_down, 0, FALSE},
187 {Ctrl_K, nv_error, 0, 0},
188 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar2c519cf2019-03-21 21:45:34 +0100189 {CAR, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 {Ctrl_N, nv_down, NV_STS, FALSE},
191 {Ctrl_O, nv_ctrlo, 0, 0},
192 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000193 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 {Ctrl_R, nv_redo, 0, 0},
195 {Ctrl_S, nv_ignore, 0, 0},
196 {Ctrl_T, nv_tagpop, NV_NCW, 0},
197 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 {Ctrl_V, nv_visual, 0, FALSE},
199 {'V', nv_visual, 0, FALSE},
200 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 {Ctrl_W, nv_window, 0, 0},
202 {Ctrl_X, nv_addsub, 0, 0},
203 {Ctrl_Y, nv_scroll_line, 0, FALSE},
204 {Ctrl_Z, nv_suspend, 0, 0},
205 {ESC, nv_esc, 0, FALSE},
206 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
207 {Ctrl_RSB, nv_ident, NV_NCW, 0},
208 {Ctrl_HAT, nv_hat, NV_NCW, 0},
209 {Ctrl__, nv_error, 0, 0},
210 {' ', nv_right, 0, 0},
211 {'!', nv_operator, 0, 0},
212 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
213 {'#', nv_ident, 0, 0},
214 {'$', nv_dollar, 0, 0},
215 {'%', nv_percent, 0, 0},
216 {'&', nv_optrans, 0, 0},
217 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
218 {'(', nv_brace, 0, BACKWARD},
219 {')', nv_brace, 0, FORWARD},
220 {'*', nv_ident, 0, 0},
221 {'+', nv_down, 0, TRUE},
222 {',', nv_csearch, 0, TRUE},
223 {'-', nv_up, 0, TRUE},
224 {'.', nv_dot, NV_KEEPREG, 0},
225 {'/', nv_search, 0, FALSE},
226 {'0', nv_beginline, 0, 0},
227 {'1', nv_ignore, 0, 0},
228 {'2', nv_ignore, 0, 0},
229 {'3', nv_ignore, 0, 0},
230 {'4', nv_ignore, 0, 0},
231 {'5', nv_ignore, 0, 0},
232 {'6', nv_ignore, 0, 0},
233 {'7', nv_ignore, 0, 0},
234 {'8', nv_ignore, 0, 0},
235 {'9', nv_ignore, 0, 0},
236 {':', nv_colon, 0, 0},
237 {';', nv_csearch, 0, FALSE},
238 {'<', nv_operator, NV_RL, 0},
239 {'=', nv_operator, 0, 0},
240 {'>', nv_operator, NV_RL, 0},
241 {'?', nv_search, 0, FALSE},
242 {'@', nv_at, NV_NCH_NOP, FALSE},
243 {'A', nv_edit, 0, 0},
244 {'B', nv_bck_word, 0, 1},
245 {'C', nv_abbrev, NV_KEEPREG, 0},
246 {'D', nv_abbrev, NV_KEEPREG, 0},
247 {'E', nv_wordcmd, 0, TRUE},
248 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
249 {'G', nv_goto, 0, TRUE},
250 {'H', nv_scroll, 0, 0},
251 {'I', nv_edit, 0, 0},
252 {'J', nv_join, 0, 0},
253 {'K', nv_ident, 0, 0},
254 {'L', nv_scroll, 0, 0},
255 {'M', nv_scroll, 0, 0},
256 {'N', nv_next, 0, SEARCH_REV},
257 {'O', nv_open, 0, 0},
258 {'P', nv_put, 0, 0},
259 {'Q', nv_exmode, NV_NCW, 0},
260 {'R', nv_Replace, 0, FALSE},
261 {'S', nv_subst, NV_KEEPREG, 0},
262 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
263 {'U', nv_Undo, 0, 0},
264 {'W', nv_wordcmd, 0, TRUE},
265 {'X', nv_abbrev, NV_KEEPREG, 0},
266 {'Y', nv_abbrev, NV_KEEPREG, 0},
267 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
268 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
269 {'\\', nv_error, 0, 0},
270 {']', nv_brackets, NV_NCH_ALW, FORWARD},
271 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
272 {'_', nv_lineop, 0, 0},
273 {'`', nv_gomark, NV_NCH_ALW, FALSE},
274 {'a', nv_edit, NV_NCH, 0},
275 {'b', nv_bck_word, 0, 0},
276 {'c', nv_operator, 0, 0},
277 {'d', nv_operator, 0, 0},
278 {'e', nv_wordcmd, 0, FALSE},
279 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
280 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
281 {'h', nv_left, NV_RL, 0},
282 {'i', nv_edit, NV_NCH, 0},
283 {'j', nv_down, 0, FALSE},
284 {'k', nv_up, 0, FALSE},
285 {'l', nv_right, NV_RL, 0},
286 {'m', nv_mark, NV_NCH_NOP, 0},
287 {'n', nv_next, 0, 0},
288 {'o', nv_open, 0, 0},
289 {'p', nv_put, 0, 0},
290 {'q', nv_record, NV_NCH, 0},
291 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
292 {'s', nv_subst, NV_KEEPREG, 0},
293 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
294 {'u', nv_undo, 0, 0},
295 {'w', nv_wordcmd, 0, FALSE},
296 {'x', nv_abbrev, NV_KEEPREG, 0},
297 {'y', nv_operator, 0, 0},
298 {'z', nv_zet, NV_NCH_ALW, 0},
299 {'{', nv_findpar, 0, BACKWARD},
300 {'|', nv_pipe, 0, 0},
301 {'}', nv_findpar, 0, FORWARD},
302 {'~', nv_tilde, 0, 0},
303
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100304 // pound sign
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 {POUND, nv_ident, 0, 0},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200306 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
307 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
308 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
309 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 {K_LEFTMOUSE, nv_mouse, 0, 0},
311 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
312 {K_LEFTDRAG, nv_mouse, 0, 0},
313 {K_LEFTRELEASE, nv_mouse, 0, 0},
314 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100315 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
317 {K_MIDDLEDRAG, nv_mouse, 0, 0},
318 {K_MIDDLERELEASE, nv_mouse, 0, 0},
319 {K_RIGHTMOUSE, nv_mouse, 0, 0},
320 {K_RIGHTDRAG, nv_mouse, 0, 0},
321 {K_RIGHTRELEASE, nv_mouse, 0, 0},
322 {K_X1MOUSE, nv_mouse, 0, 0},
323 {K_X1DRAG, nv_mouse, 0, 0},
324 {K_X1RELEASE, nv_mouse, 0, 0},
325 {K_X2MOUSE, nv_mouse, 0, 0},
326 {K_X2DRAG, nv_mouse, 0, 0},
327 {K_X2RELEASE, nv_mouse, 0, 0},
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000328 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000329 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 {K_INS, nv_edit, 0, 0},
331 {K_KINS, nv_edit, 0, 0},
332 {K_BS, nv_ctrlh, 0, 0},
333 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
334 {K_S_UP, nv_page, NV_SS, BACKWARD},
335 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
336 {K_S_DOWN, nv_page, NV_SS, FORWARD},
337 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
338 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
339 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
340 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
341 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
342 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
343 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
344 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
345 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
346 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
347 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
348 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 {K_S_END, nv_end, NV_SS, FALSE},
350 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
351 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
352 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 {K_S_HOME, nv_home, NV_SS, 0},
354 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
355 {K_DEL, nv_abbrev, 0, 0},
356 {K_KDEL, nv_abbrev, 0, 0},
357 {K_UNDO, nv_kundo, 0, 0},
358 {K_HELP, nv_help, NV_NCW, 0},
359 {K_F1, nv_help, NV_NCW, 0},
360 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362#ifdef FEAT_GUI
363 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
364 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
365#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000366#ifdef FEAT_GUI_TABLINE
367 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000368 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370#ifdef FEAT_NETBEANS_INTG
371 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
372#endif
373#ifdef FEAT_DND
374 {K_DROP, nv_drop, NV_STS, 0},
375#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000376 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100377 {K_PS, nv_edit, 0, 0},
Bram Moolenaar957cf672020-11-12 14:21:06 +0100378 {K_COMMAND, nv_colon, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379};
380
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100381// Number of commands in nv_cmds[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
383
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100384// Sorted index of commands in nv_cmds[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385static short nv_cmd_idx[NV_CMDS_SIZE];
386
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100387// The highest index for which
388// nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389static int nv_max_linear;
390
391/*
392 * Compare functions for qsort() below, that checks the command character
393 * through the index in nv_cmd_idx[].
394 */
395 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100396nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397{
398 int c1, c2;
399
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100400 // The commands are sorted on absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 c1 = nv_cmds[*(const short *)s1].cmd_char;
402 c2 = nv_cmds[*(const short *)s2].cmd_char;
403 if (c1 < 0)
404 c1 = -c1;
405 if (c2 < 0)
406 c2 = -c2;
407 return c1 - c2;
408}
409
410/*
411 * Initialize the nv_cmd_idx[] table.
412 */
413 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100414init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415{
416 int i;
417
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100418 // Fill the index table with a one to one relation.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000419 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 nv_cmd_idx[i] = i;
421
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100422 // Sort the commands by the command character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
424
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100425 // Find the first entry that can't be indexed by the command character.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000426 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
428 break;
429 nv_max_linear = i - 1;
430}
431
432/*
433 * Search for a command in the commands table.
434 * Returns -1 for invalid command.
435 */
436 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100437find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438{
439 int i;
440 int idx;
441 int top, bot;
442 int c;
443
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100444 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 if (cmdchar >= 0x100)
446 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100448 // We use the absolute value of the character. Special keys have a
449 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 if (cmdchar < 0)
451 cmdchar = -cmdchar;
452
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100453 // If the character is in the first part: The character is the index into
454 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 if (cmdchar <= nv_max_linear)
456 return nv_cmd_idx[cmdchar];
457
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100458 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 bot = nv_max_linear + 1;
460 top = NV_CMDS_SIZE - 1;
461 idx = -1;
462 while (bot <= top)
463 {
464 i = (top + bot) / 2;
465 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
466 if (c < 0)
467 c = -c;
468 if (cmdchar == c)
469 {
470 idx = nv_cmd_idx[i];
471 break;
472 }
473 if (cmdchar > c)
474 bot = i + 1;
475 else
476 top = i - 1;
477 }
478 return idx;
479}
480
481/*
482 * Execute a command in Normal mode.
483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100485normal_cmd(
486 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100487 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100489 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100491 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 int old_col = curwin->w_curswant;
493#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100494 int need_flushbuf; // need to call out_flush()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100496 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 static int old_mapped_len = 0;
499 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000500#ifdef FEAT_EVAL
501 int set_prevcount = FALSE;
502#endif
Bram Moolenaarb146e012020-07-19 23:06:05 +0200503 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504
Bram Moolenaara80faa82020-04-12 19:37:17 +0200505 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000507
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100508 // Use a count remembered from before entering an operator. After typing
509 // "3d" we return from normal_cmd() and come back here, the "3" is
510 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 ca.opcount = opcount;
512
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 /*
514 * If there is an operator pending, then the command we take this time
515 * will terminate it. Finish_op tells us to finish the operation before
516 * returning this time (unless the operation was cancelled).
517 */
518#ifdef CURSOR_SHAPE
519 c = finish_op;
520#endif
521 finish_op = (oap->op_type != OP_NOP);
522#ifdef CURSOR_SHAPE
523 if (finish_op != c)
524 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100525 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526# ifdef FEAT_MOUSESHAPE
527 update_mouseshape(-1);
528# endif
529 }
530#endif
531
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100532 // When not finishing an operator and no register name typed, reset the
533 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000535 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000537#ifdef FEAT_EVAL
538 set_prevcount = TRUE;
539#endif
540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100542 // Restore counts from before receiving K_CURSORHOLD. This means after
543 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
544 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000545 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
546 {
547 ca.opcount = oap->prev_opcount;
548 ca.count0 = oap->prev_count0;
549 oap->prev_opcount = 0;
550 oap->prev_count0 = 0;
551 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000552
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
555 State = NORMAL_BUSY;
556#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100557 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558#endif
559
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100560#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100561 // Set v:count here, when called from main() and not a stuffed
562 // command, so that v:count can be used in an expression mapping
563 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100564 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100565 set_vcount_ca(&ca, &set_prevcount);
566#endif
567
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 /*
569 * Get the command character from the user.
570 */
571 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100572 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
574 /*
575 * If a mapping was started in Visual or Select mode, remember the length
576 * of the mapping. This is used below to not return to Insert mode for as
577 * long as the mapping is being executed.
578 */
579 if (restart_edit == 0)
580 old_mapped_len = 0;
581 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000582 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 old_mapped_len = typebuf_maplen();
584
585 if (c == NUL)
586 c = K_ZERO;
587
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 /*
589 * In Select mode, typed text replaces the selection.
590 */
591 if (VIsual_active
592 && VIsual_select
593 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
594 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100595 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
596 // 'insertmode' is set) fake a "d"elete command, Insert mode will
597 // restart automatically.
598 // Insert the typed character in the typeahead buffer, so that it can
599 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +0200600 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000601 if (restart_edit != 0)
602 c = 'd';
603 else
604 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100605 msg_nowait = TRUE; // don't delay going to insert mode
606 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608
609#ifdef FEAT_CMDL_INFO
610 need_flushbuf = add_to_showcmd(c);
611#endif
612
613getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 {
616 /*
617 * Handle a count before a command and compute ca.count0.
618 * Note that '0' is a command and not the start of a count, but it's
619 * part of a count after other digits.
620 */
621 while ( (c >= '1' && c <= '9')
622 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
623 {
624 if (c == K_DEL || c == K_KDEL)
625 {
626 ca.count0 /= 10;
627#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100628 del_from_showcmd(4); // delete the digit and ~@%
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629#endif
630 }
631 else
632 ca.count0 = ca.count0 * 10 + (c - '0');
Bram Moolenaar1d859e22021-01-28 17:24:58 +0100633 if (ca.count0 < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000635#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100636 // Set v:count here, when called from main() and not a stuffed
637 // command, so that v:count can be used in an expression mapping
638 // right after the count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100639 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100640 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 if (ctrl_w)
643 {
644 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100645 ++allow_keys; // no mapping for nchar, but keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100647 ++no_zero_mapping; // don't map zero here
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000648 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 --no_zero_mapping;
651 if (ctrl_w)
652 {
653 --no_mapping;
654 --allow_keys;
655 }
656#ifdef FEAT_CMDL_INFO
657 need_flushbuf |= add_to_showcmd(c);
658#endif
659 }
660
661 /*
662 * If we got CTRL-W there may be a/another count
663 */
664 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
665 {
666 ctrl_w = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100667 ca.opcount = ca.count0; // remember first count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 ca.count0 = 0;
669 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100670 ++allow_keys; // no mapping for nchar, but keys
671 c = plain_vgetc(); // get next character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 --no_mapping;
674 --allow_keys;
675#ifdef FEAT_CMDL_INFO
676 need_flushbuf |= add_to_showcmd(c);
677#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100678 goto getcount; // jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 }
680 }
681
Bram Moolenaara983fe92008-07-31 20:04:27 +0000682 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100684 // Save the count values so that ca.opcount and ca.count0 are exactly
685 // the same when coming back here after handling K_CURSORHOLD.
Bram Moolenaara983fe92008-07-31 20:04:27 +0000686 oap->prev_opcount = ca.opcount;
687 oap->prev_count0 = ca.count0;
688 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100689 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000690 {
691 /*
692 * If we're in the middle of an operator (including after entering a
693 * yank buffer with '"') AND we had a count before the operator, then
694 * that count overrides the current value of ca.count0.
695 * What this means effectively, is that commands like "3dw" get turned
696 * into "d3w" which makes things fall into place pretty neatly.
697 * If you give a count before AND after the operator, they are
698 * multiplied.
699 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 if (ca.count0)
701 ca.count0 *= ca.opcount;
702 else
703 ca.count0 = ca.opcount;
Bram Moolenaar1d859e22021-01-28 17:24:58 +0100704 if (ca.count0 < 0) // overflow
705 ca.count0 = 999999999L;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 }
707
708 /*
709 * Always remember the count. It will be set to zero (on the next call,
710 * above) when there is no pending operator.
711 * When called from main(), save the count for use by the "count" built-in
712 * variable.
713 */
714 ca.opcount = ca.count0;
715 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
716
717#ifdef FEAT_EVAL
718 /*
719 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100720 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100722 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000723 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724#endif
725
726 /*
727 * Find the command character in the table of commands.
728 * For CTRL-W we already got nchar when looking for a count.
729 */
730 if (ctrl_w)
731 {
732 ca.nchar = c;
733 ca.cmdchar = Ctrl_W;
734 }
735 else
736 ca.cmdchar = c;
737 idx = find_command(ca.cmdchar);
738 if (idx < 0)
739 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100740 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 clearopbeep(oap);
742 goto normal_end;
743 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000744
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000745 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100747 // This command is not allowed while editing a cmdline: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000749 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 goto normal_end;
751 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000752 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
753 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 /*
756 * In Visual/Select mode, a few keys are handled in a special way.
757 */
758 if (VIsual_active)
759 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100760 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 if (km_stopsel
762 && (nv_cmds[idx].cmd_flags & NV_STS)
763 && !(mod_mask & MOD_MASK_SHIFT))
764 {
765 end_visual_mode();
766 redraw_curbuf_later(INVERTED);
767 }
768
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100769 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 if (km_startsel)
771 {
772 if (nv_cmds[idx].cmd_flags & NV_SS)
773 {
774 unshift_special(&ca);
775 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000776 if (idx < 0)
777 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100778 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000779 clearopbeep(oap);
780 goto normal_end;
781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 }
783 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
784 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 }
787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
789#ifdef FEAT_RIGHTLEFT
790 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
791 && (nv_cmds[idx].cmd_flags & NV_RL))
792 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100793 // Invert horizontal movements and operations. Only when typed by the
794 // user directly, not when the result of a mapping or "x" translated
795 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 switch (ca.cmdchar)
797 {
798 case 'l': ca.cmdchar = 'h'; break;
799 case K_RIGHT: ca.cmdchar = K_LEFT; break;
800 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
801 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
802 case 'h': ca.cmdchar = 'l'; break;
803 case K_LEFT: ca.cmdchar = K_RIGHT; break;
804 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
805 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
806 case '>': ca.cmdchar = '<'; break;
807 case '<': ca.cmdchar = '>'; break;
808 }
809 idx = find_command(ca.cmdchar);
810 }
811#endif
812
813 /*
814 * Get an additional character if we need one.
815 */
816 if ((nv_cmds[idx].cmd_flags & NV_NCH)
817 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
818 && oap->op_type == OP_NOP)
819 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
820 || (ca.cmdchar == 'q'
821 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200822 && reg_recording == 0
823 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100825 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 {
827 int *cp;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100828 int repl = FALSE; // get character for replace mode
829 int lit = FALSE; // get extra character literally
830 int langmap_active = FALSE; // using :lmap mappings
831 int lang; // getting a text character
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100832#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100833 int save_smd; // saved value of p_smd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834#endif
835
836 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100837 ++allow_keys; // no mapping for nchar, but allow key codes
838 // Don't generate a CursorHold event here, most commands can't handle
839 // it, e.g., nv_replace(), nv_csearch().
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000840 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 if (ca.cmdchar == 'g')
842 {
843 /*
844 * For 'g' get the next character now, so that we can check for
845 * "gr", "g'" and "g`".
846 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000847 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849#ifdef FEAT_CMDL_INFO
850 need_flushbuf |= add_to_showcmd(ca.nchar);
851#endif
852 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100853 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100855 cp = &ca.extra_char; // need to get a third character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 if (ca.nchar != 'r')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100857 lit = TRUE; // get it literally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100859 repl = TRUE; // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 }
861 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100862 cp = NULL; // no third character needed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 }
864 else
865 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100866 if (ca.cmdchar == 'r') // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 repl = TRUE;
868 cp = &ca.nchar;
869 }
870 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
871
872 /*
873 * Get a second or third character.
874 */
875 if (cp != NULL)
876 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 if (repl)
878 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100879 State = REPLACE; // pretend Replace mode
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100880#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100881 ui_cursor_shape(); // show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882#endif
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
885 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100886 // Allow mappings defined with ":lmap".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 --no_mapping;
888 --allow_keys;
889 if (repl)
890 State = LREPLACE;
891 else
892 State = LANGMAP;
893 langmap_active = TRUE;
894 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100895#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 save_smd = p_smd;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100897 p_smd = FALSE; // Don't let the IM code show the mode here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
899 im_set_active(TRUE);
900#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200901 if ((State & INSERT) && !p_ek)
902 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200903#ifdef FEAT_JOB_CHANNEL
904 ch_log_output = TRUE;
905#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200906 // Disable bracketed paste and modifyOtherKeys here, we won't
907 // recognize the escape sequences with 'esckeys' off.
908 out_str(T_BD);
909 out_str(T_CTE);
910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000912 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913
Bram Moolenaarca774f62020-08-30 20:46:38 +0200914 if ((State & INSERT) && !p_ek)
915 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200916#ifdef FEAT_JOB_CHANNEL
917 ch_log_output = TRUE;
918#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200919 // Re-enable bracketed paste mode and modifyOtherKeys
920 out_str(T_BE);
921 out_str(T_CTI);
922 }
923
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 if (langmap_active)
925 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100926 // Undo the decrement done above
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 ++no_mapping;
928 ++allow_keys;
929 State = NORMAL_BUSY;
930 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100931#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 if (lang)
933 {
934 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
935 im_save_status(&curbuf->b_p_iminsert);
936 im_set_active(FALSE);
937 }
938 p_smd = save_smd;
939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 State = NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941#ifdef FEAT_CMDL_INFO
942 need_flushbuf |= add_to_showcmd(*cp);
943#endif
944
945 if (!lit)
946 {
947#ifdef FEAT_DIGRAPHS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100948 // Typing CTRL-K gets a digraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 if (*cp == Ctrl_K
950 && ((nv_cmds[idx].cmd_flags & NV_LANG)
951 || cp == &ca.extra_char)
952 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
953 {
954 c = get_digraph(FALSE);
955 if (c > 0)
956 {
957 *cp = c;
958# ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100959 // Guessing how to update showcmd here...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 del_from_showcmd(3);
961 need_flushbuf |= add_to_showcmd(*cp);
962# endif
963 }
964 }
965#endif
966
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100967 // adjust chars > 127, except after "tTfFr" commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100970 // adjust Hebrew mapped char
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 if (p_hkmap && lang && KeyTyped)
972 *cp = hkmap(*cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973#endif
974 }
975
976 /*
977 * When the next character is CTRL-\ a following CTRL-N means the
978 * command is aborted and we go to Normal mode.
979 */
980 if (cp == &ca.extra_char
981 && ca.nchar == Ctrl_BSL
982 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
983 {
984 ca.cmdchar = Ctrl_BSL;
985 ca.nchar = ca.extra_char;
986 idx = find_command(ca.cmdchar);
987 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +0200988 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +0200989 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 else if (*cp == Ctrl_BSL)
991 {
992 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
993
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100994 // There is a busy wait here when typing "f<C-\>" and then
995 // something different from CTRL-N. Can't be avoided.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 while ((c = vpeekc()) <= 0 && towait > 0L)
997 {
Bram Moolenaare2edc2e2021-01-16 20:21:23 +0100998 do_sleep(towait > 50L ? 50L : towait, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 towait -= 50L;
1000 }
1001 if (c > 0)
1002 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001003 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 if (c != Ctrl_N && c != Ctrl_G)
1005 vungetc(c);
1006 else
1007 {
1008 ca.cmdchar = Ctrl_BSL;
1009 ca.nchar = c;
1010 idx = find_command(ca.cmdchar);
1011 }
1012 }
1013 }
1014
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001015 // When getting a text character and the next character is a
1016 // multi-byte character, it could be a composing character.
1017 // However, don't wait for it to arrive. Also, do enable mapping,
1018 // because if it's put back with vungetc() it's too late to apply
1019 // mapping.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001020 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 while (enc_utf8 && lang && (c = vpeekc()) > 0
1022 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1023 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001024 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 if (!utf_iscomposing(c))
1026 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001027 vungetc(c); // it wasn't, put it back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 break;
1029 }
1030 else if (ca.ncharC1 == 0)
1031 ca.ncharC1 = c;
1032 else
1033 ca.ncharC2 = c;
1034 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001035 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 }
1037 --no_mapping;
1038 --allow_keys;
1039 }
1040
1041#ifdef FEAT_CMDL_INFO
1042 /*
1043 * Flush the showcmd characters onto the screen so we can see them while
1044 * the command is being executed. Only do this when the shown command was
1045 * actually displayed, otherwise this will slow down a lot when executing
1046 * mappings.
1047 */
1048 if (need_flushbuf)
1049 out_flush();
1050#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001051 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +02001052 {
1053 if (ex_normal_busy)
1054 did_cursorhold = save_did_cursorhold;
1055 else
1056 did_cursorhold = FALSE;
1057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058
1059 State = NORMAL;
1060
1061 if (ca.nchar == ESC)
1062 {
1063 clearop(oap);
1064 if (restart_edit == 0 && goto_im())
1065 restart_edit = 'a';
1066 goto normal_end;
1067 }
1068
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001069 if (ca.cmdchar != K_IGNORE)
1070 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001071 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001072 msg_col = 0;
1073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001075 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001077 // When 'keymodel' contains "startsel" some keys start Select/Visual
1078 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 if (!VIsual_active && km_startsel)
1080 {
1081 if (nv_cmds[idx].cmd_flags & NV_SS)
1082 {
1083 start_selection();
1084 unshift_special(&ca);
1085 idx = find_command(ca.cmdchar);
1086 }
1087 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1088 && (mod_mask & MOD_MASK_SHIFT))
1089 {
1090 start_selection();
1091 mod_mask &= ~MOD_MASK_SHIFT;
1092 }
1093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094
1095 /*
1096 * Execute the command!
1097 * Call the command function found in the commands table.
1098 */
1099 ca.arg = nv_cmds[idx].cmd_arg;
1100 (nv_cmds[idx].cmd_func)(&ca);
1101
1102 /*
1103 * If we didn't start or finish an operator, reset oap->regname, unless we
1104 * need it later.
1105 */
1106 if (!finish_op
1107 && !oap->op_type
1108 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1109 {
1110 clearop(oap);
1111#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +02001112 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113#endif
1114 }
1115
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001116 // Get the length of mapped chars again after typing a count, second
1117 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001118 if (old_mapped_len > 0)
1119 old_mapped_len = typebuf_maplen();
1120
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 /*
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001122 * If an operation is pending, handle it. But not for K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 */
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001124 if (ca.cmdchar != K_IGNORE)
1125 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126
1127 /*
1128 * Wait for a moment when a message is displayed that will be overwritten
1129 * by the mode message.
1130 * In Visual mode and with "^O" in Insert mode, a short message will be
1131 * overwritten by the mode message. Wait a bit, until a key is hit.
1132 * In Visual mode, it's more important to keep the Visual area updated
1133 * than keeping a message (e.g. from a /pat search).
1134 * Only do this if the command was typed, not from a mapping.
1135 * Don't wait when emsg_silent is non-zero.
1136 * Also wait a bit after an error message, e.g. for "^O:".
1137 * Don't redraw the screen, it would remove the message.
1138 */
1139 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001140 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 || (VIsual_active
1143 && old_pos.lnum == curwin->w_cursor.lnum
1144 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 )
1146 && (clear_cmdline
1147 || redraw_cmdline)
1148 && (msg_didout || (msg_didany && msg_scroll))
1149 && !msg_nowait
1150 && KeyTyped)
1151 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 && (msg_scroll
1154 || emsg_on_display)))
1155 && oap->regname == 0
1156 && !(ca.retval & CA_COMMAND_BUSY)
1157 && stuff_empty()
1158 && typebuf_typed()
1159 && emsg_silent == 0
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001160 && !in_assert_fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 && !did_wait_return
1162 && oap->op_type == OP_NOP)
1163 {
1164 int save_State = State;
1165
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001166 // Draw the cursor with the right shape here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 if (restart_edit != 0)
1168 State = INSERT;
1169
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001170 // If need to redraw, and there is a "keep_msg", redraw before the
1171 // delay
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1173 {
1174 char_u *kmsg;
1175
1176 kmsg = keep_msg;
1177 keep_msg = NULL;
Bram Moolenaar5715b312020-03-16 22:08:45 +01001178 // Showmode() will clear keep_msg, but we want to use it anyway.
1179 // First update w_topline.
1180 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 update_screen(0);
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001182 // now reset it, otherwise it's put in the history again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 keep_msg = kmsg;
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001184
1185 kmsg = vim_strsave(keep_msg);
1186 if (kmsg != NULL)
1187 {
1188 msg_attr((char *)kmsg, keep_msg_attr);
1189 vim_free(kmsg);
1190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 }
1192 setcursor();
Bram Moolenaar5715b312020-03-16 22:08:45 +01001193#ifdef CURSOR_SHAPE
1194 ui_cursor_shape(); // may show different cursor shape
1195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 cursor_on();
1197 out_flush();
1198 if (msg_scroll || emsg_on_display)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001199 ui_delay(1003L, TRUE); // wait at least one second
1200 ui_delay(3003L, FALSE); // wait up to three seconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 State = save_State;
1202
1203 msg_scroll = FALSE;
1204 emsg_on_display = FALSE;
1205 }
1206
1207 /*
1208 * Finish up after executing a Normal mode command.
1209 */
1210normal_end:
1211
1212 msg_nowait = FALSE;
1213
Bram Moolenaarcc613032020-06-07 21:31:18 +02001214#ifdef FEAT_EVAL
1215 if (finish_op)
1216 reset_reg_var();
1217#endif
1218
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001219 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220#ifdef CURSOR_SHAPE
1221 c = finish_op;
1222#endif
1223 finish_op = FALSE;
1224#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001225 // Redraw the cursor with another shape, if we were in Operator-pending
1226 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 if (c || ca.cmdchar == 'r')
1228 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001229 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230# ifdef FEAT_MOUSESHAPE
1231 update_mouseshape(-1);
1232# endif
1233 }
1234#endif
1235
1236#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001237 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001238 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 clear_showcmd();
1240#endif
1241
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001242 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 vim_free(ca.searchbuf);
1244
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 if (has_mbyte)
1246 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 if (curwin->w_p_scb && toplevel)
1249 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001250 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 do_check_scrollbind(TRUE);
1252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253
Bram Moolenaar860cae12010-06-05 23:22:07 +02001254 if (curwin->w_p_crb && toplevel)
1255 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001256 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001257 do_check_cursorbind();
1258 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001259
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001260#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001261 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001262 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001263 restart_edit = 0;
1264#endif
1265
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 /*
1267 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1268 * if still inside a mapping that started in Visual mode).
1269 * May switch from Visual to Select mode after CTRL-O command.
1270 */
1271 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1273 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 && !(ca.retval & CA_COMMAND_BUSY)
1275 && stuff_empty()
1276 && oap->regname == 0)
1277 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 if (restart_VIsual_select == 1)
1279 {
1280 VIsual_select = TRUE;
1281 showmode();
1282 restart_VIsual_select = 0;
1283 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001284 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 (void)edit(restart_edit, FALSE, 1L);
1286 }
1287
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 if (restart_VIsual_select == 2)
1289 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001291 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 opcount = ca.opcount;
1293}
1294
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001295#ifdef FEAT_EVAL
1296/*
1297 * Set v:count and v:count1 according to "cap".
1298 * Set v:prevcount only when "set_prevcount" is TRUE.
1299 */
1300 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001301set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001302{
1303 long count = cap->count0;
1304
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001305 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001306 if (cap->opcount != 0)
1307 count = cap->opcount * (count == 0 ? 1 : count);
1308 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001309 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001310}
1311#endif
1312
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001314 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 * if not.
1316 */
1317 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001318check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319{
1320 static int did_check = FALSE;
1321
1322 if (full_screen)
1323 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001324 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001325 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 did_check = TRUE;
1327 }
1328}
1329
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001330#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1331/*
1332 * Call yank_do_autocmd() for "regname".
1333 */
1334 static void
1335call_yank_do_autocmd(int regname)
1336{
1337 oparg_T oa;
1338 yankreg_T *reg;
1339
1340 clear_oparg(&oa);
1341 oa.regname = regname;
1342 oa.op_type = OP_YANK;
1343 oa.is_VIsual = TRUE;
1344 reg = get_register(regname, TRUE);
1345 yank_do_autocmd(&oa, reg);
1346 free_register(reg);
1347}
1348#endif
1349
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001351 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 * This function should ALWAYS be called to end Visual mode, except from
1353 * do_pending_operator().
1354 */
1355 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001356end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357{
1358#ifdef FEAT_CLIPBOARD
1359 /*
1360 * If we are using the clipboard, then remember what was selected in case
1361 * we need to paste it somewhere while we still own the selection.
1362 * Only do this when the clipboard is already owned. Don't want to grab
1363 * the selection when hitting ESC.
1364 */
1365 if (clip_star.available && clip_star.owned)
1366 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001367
1368# if defined(FEAT_EVAL)
1369 // Emit a TextYankPost for the automatic copy of the selection into the
1370 // star and/or plus register.
1371 if (has_textyankpost())
1372 {
1373 if (clip_isautosel_star())
1374 call_yank_do_autocmd('*');
1375 if (clip_isautosel_plus())
1376 call_yank_do_autocmd('+');
1377 }
1378# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379#endif
1380
1381 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 setmouse();
1383 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001385 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001386 curbuf->b_visual.vi_mode = VIsual_mode;
1387 curbuf->b_visual.vi_start = VIsual;
1388 curbuf->b_visual.vi_end = curwin->w_cursor;
1389 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390#ifdef FEAT_EVAL
1391 curbuf->b_visual_mode_eval = VIsual_mode;
1392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 if (!virtual_active())
1394 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001395 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001397 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398}
1399
1400/*
1401 * Reset VIsual_active and VIsual_reselect.
1402 */
1403 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001404reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405{
1406 if (VIsual_active)
1407 {
1408 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001409 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 }
1411 VIsual_reselect = FALSE;
1412}
1413
1414/*
1415 * Reset VIsual_active and VIsual_reselect if it's set.
1416 */
1417 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001418reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419{
1420 if (VIsual_active)
1421 {
1422 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001423 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 VIsual_reselect = FALSE;
1425 }
1426}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001428 void
1429restore_visual_mode(void)
1430{
1431 if (VIsual_mode_orig != NUL)
1432 {
1433 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1434 VIsual_mode_orig = NUL;
1435 }
1436}
1437
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438/*
1439 * Check for a balloon-eval special item to include when searching for an
1440 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1441 * Returns TRUE if the character at "*ptr" should be included.
1442 * "dir" is FORWARD or BACKWARD, the direction of searching.
1443 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1444 * "bnp" points to a counter for square brackets.
1445 */
1446 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001447find_is_eval_item(
1448 char_u *ptr,
1449 int *colp,
1450 int *bnp,
1451 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001453 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1455 ++*bnp;
1456 if (*bnp > 0)
1457 {
1458 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1459 --*bnp;
1460 return TRUE;
1461 }
1462
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001463 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 if (*ptr == '.')
1465 return TRUE;
1466
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001467 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1469 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1470 {
1471 *colp += dir;
1472 return TRUE;
1473 }
1474 return FALSE;
1475}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476
1477/*
1478 * Find the identifier under or to the right of the cursor.
1479 * "find_type" can have one of three values:
1480 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001481 * FIND_STRING: find any non-white text
1482 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001483 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 *
1485 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001486 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001488 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 * This doesn't match the real Vi but I like it a little better and it
1490 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001491 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 * When FIND_IDENT isn't defined, we backup until a blank.
1493 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001494 * Returns the length of the text, or zero if no text is found.
1495 * If text is found, a pointer to the text is put in "*text". This
1496 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 */
1498 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001499find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500{
1501 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001502 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503}
1504
1505/*
1506 * Like find_ident_under_cursor(), but for any window and any position.
1507 * However: Uses 'iskeyword' from the current window!.
1508 */
1509 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001510find_ident_at_pos(
1511 win_T *wp,
1512 linenr_T lnum,
1513 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001514 char_u **text,
1515 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001516 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
1518 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001519 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 int this_class = 0;
1522 int prev_class;
1523 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001524 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525
1526 /*
1527 * if i == 0: try to find an identifier
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001528 * if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 */
1530 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1531 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1532 {
1533 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001534 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 */
1536 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 if (has_mbyte)
1538 {
1539 while (ptr[col] != NUL)
1540 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001541 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1543 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 this_class = mb_get_class(ptr + col);
1545 if (this_class != 0 && (i == 1 || this_class != 1))
1546 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001547 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 }
1549 }
1550 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001552 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 )
1555 ++col;
1556
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001557 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559
1560 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001561 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 if (has_mbyte)
1564 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001565 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1567 this_class = mb_get_class((char_u *)"a");
1568 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001570 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 {
1572 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1573 prev_class = mb_get_class(ptr + prevcol);
1574 if (this_class != prev_class
1575 && (i == 0
1576 || prev_class == 0
1577 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 && (!(find_type & FIND_EVAL)
1579 || prevcol == 0
1580 || !find_is_eval_item(ptr + prevcol, &prevcol,
1581 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 )
1583 break;
1584 col = prevcol;
1585 }
1586
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001587 // If we don't want just any old text, or we've found an
1588 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 if (this_class > 2)
1590 this_class = 2;
1591 if (!(find_type & FIND_STRING) || this_class == 2)
1592 break;
1593 }
1594 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 {
1596 while (col > 0
1597 && ((i == 0
1598 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001599 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 && (!(find_type & FIND_IDENT)
1601 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 || ((find_type & FIND_EVAL)
1603 && col > 1
1604 && find_is_eval_item(ptr + col - 1, &col,
1605 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 ))
1607 --col;
1608
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001609 // If we don't want just any old text, or we've found an
1610 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1612 break;
1613 }
1614 }
1615
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001616 if (ptr[col] == NUL || (i == 0
1617 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001619 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001620 if ((find_type & FIND_NOERROR) == 0)
1621 {
1622 if (find_type & FIND_STRING)
1623 emsg(_("E348: No string under cursor"));
1624 else
1625 emsg(_(e_noident));
1626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 return 0;
1628 }
1629 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001630 *text = ptr;
1631 if (textcol != NULL)
1632 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633
1634 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001635 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 bn = 0;
1638 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 if (has_mbyte)
1641 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001642 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 this_class = mb_get_class(ptr);
1644 while (ptr[col] != NUL
1645 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1646 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 || ((find_type & FIND_EVAL)
1648 && col <= (int)startcol
1649 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001651 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 }
1653 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001655 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 || ((find_type & FIND_EVAL)
1657 && col <= (int)startcol
1658 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661
1662 return col;
1663}
1664
1665/*
1666 * Prepare for redo of a normal command.
1667 */
1668 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001669prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670{
1671 prep_redo(cap->oap->regname, cap->count0,
1672 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1673}
1674
1675/*
1676 * Prepare for redo of any command.
1677 * Note that only the last argument can be a multi-byte char.
1678 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001679 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001680prep_redo(
1681 int regname,
1682 long num,
1683 int cmd1,
1684 int cmd2,
1685 int cmd3,
1686 int cmd4,
1687 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688{
1689 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001690 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 {
1692 AppendCharToRedobuff('"');
1693 AppendCharToRedobuff(regname);
1694 }
1695 if (num)
1696 AppendNumberToRedobuff(num);
1697
1698 if (cmd1 != NUL)
1699 AppendCharToRedobuff(cmd1);
1700 if (cmd2 != NUL)
1701 AppendCharToRedobuff(cmd2);
1702 if (cmd3 != NUL)
1703 AppendCharToRedobuff(cmd3);
1704 if (cmd4 != NUL)
1705 AppendCharToRedobuff(cmd4);
1706 if (cmd5 != NUL)
1707 AppendCharToRedobuff(cmd5);
1708}
1709
1710/*
1711 * check for operator active and clear it
1712 *
1713 * return TRUE if operator was active
1714 */
1715 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001716checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717{
1718 if (oap->op_type == OP_NOP)
1719 return FALSE;
1720 clearopbeep(oap);
1721 return TRUE;
1722}
1723
1724/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001725 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001727 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 */
1729 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001730checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001732 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 return FALSE;
1734 clearopbeep(oap);
1735 return TRUE;
1736}
1737
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001738 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001739clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740{
1741 oap->op_type = OP_NOP;
1742 oap->regname = 0;
1743 oap->motion_force = NUL;
1744 oap->use_reg_one = FALSE;
1745}
1746
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001747 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001748clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749{
1750 clearop(oap);
1751 beep_flush();
1752}
1753
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754/*
1755 * Remove the shift modifier from a special key.
1756 */
1757 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001758unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759{
1760 switch (cap->cmdchar)
1761 {
1762 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1763 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1764 case K_S_UP: cap->cmdchar = K_UP; break;
1765 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1766 case K_S_HOME: cap->cmdchar = K_HOME; break;
1767 case K_S_END: cap->cmdchar = K_END; break;
1768 }
1769 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1770}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001772/*
1773 * If the mode is currently displayed clear the command line or update the
1774 * command displayed.
1775 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001776 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001777may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001778{
1779 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001780 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001781#ifdef FEAT_CMDL_INFO
1782 else
1783 clear_showcmd();
1784#endif
1785}
1786
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1788/*
1789 * Routines for displaying a partly typed command
1790 */
1791
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001792#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001794static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795static int showcmd_is_clear = TRUE;
1796static int showcmd_visual = FALSE;
1797
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001798static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799
1800 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001801clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802{
1803 if (!p_sc)
1804 return;
1805
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 if (VIsual_active && !char_avail())
1807 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001808 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 long lines;
1810 colnr_T leftcol, rightcol;
1811 linenr_T top, bot;
1812
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001813 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001814 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 {
1816 top = VIsual.lnum;
1817 bot = curwin->w_cursor.lnum;
1818 }
1819 else
1820 {
1821 top = curwin->w_cursor.lnum;
1822 bot = VIsual.lnum;
1823 }
1824# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001825 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001826 (void)hasFolding(top, &top, NULL);
1827 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828# endif
1829 lines = bot - top + 1;
1830
1831 if (VIsual_mode == Ctrl_V)
1832 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001833# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001834 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001835 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001836
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001837 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001838 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001839 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001840# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001842# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001843 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001844 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001845# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1847 (long)(rightcol - leftcol + 1));
1848 }
1849 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1850 sprintf((char *)showcmd_buf, "%ld", lines);
1851 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001852 {
1853 char_u *s, *e;
1854 int l;
1855 int bytes = 0;
1856 int chars = 0;
1857
1858 if (cursor_bot)
1859 {
1860 s = ml_get_pos(&VIsual);
1861 e = ml_get_cursor();
1862 }
1863 else
1864 {
1865 s = ml_get_cursor();
1866 e = ml_get_pos(&VIsual);
1867 }
1868 while ((*p_sel != 'e') ? s <= e : s < e)
1869 {
1870 l = (*mb_ptr2len)(s);
1871 if (l == 0)
1872 {
1873 ++bytes;
1874 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001875 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001876 }
1877 bytes += l;
1878 ++chars;
1879 s += l;
1880 }
1881 if (bytes == chars)
1882 sprintf((char *)showcmd_buf, "%d", chars);
1883 else
1884 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1885 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001886 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 showcmd_visual = TRUE;
1888 }
1889 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 {
1891 showcmd_buf[0] = NUL;
1892 showcmd_visual = FALSE;
1893
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001894 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 if (showcmd_is_clear)
1896 return;
1897 }
1898
1899 display_showcmd();
1900}
1901
1902/*
1903 * Add 'c' to string of shown command chars.
1904 * Return TRUE if output has been written (and setcursor() has been called).
1905 */
1906 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001907add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908{
1909 char_u *p;
1910 int old_len;
1911 int extra_len;
1912 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 int i;
1914 static int ignore[] =
1915 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001916#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1918 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001919#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001920 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001921 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1923 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001924 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001926 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 0
1928 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929
Bram Moolenaar09df3122006-01-23 22:23:09 +00001930 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 return FALSE;
1932
1933 if (showcmd_visual)
1934 {
1935 showcmd_buf[0] = NUL;
1936 showcmd_visual = FALSE;
1937 }
1938
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001939 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 if (IS_SPECIAL(c))
1941 for (i = 0; ignore[i] != 0; ++i)
1942 if (ignore[i] == c)
1943 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944
1945 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001946 if (*p == ' ')
1947 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 old_len = (int)STRLEN(showcmd_buf);
1949 extra_len = (int)STRLEN(p);
1950 overflow = old_len + extra_len - SHOWCMD_COLS;
1951 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001952 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1953 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 STRCAT(showcmd_buf, p);
1955
1956 if (char_avail())
1957 return FALSE;
1958
1959 display_showcmd();
1960
1961 return TRUE;
1962}
1963
1964 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001965add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966{
1967 if (!add_to_showcmd(c))
1968 setcursor();
1969}
1970
1971/*
1972 * Delete 'len' characters from the end of the shown command.
1973 */
1974 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001975del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976{
1977 int old_len;
1978
1979 if (!p_sc)
1980 return;
1981
1982 old_len = (int)STRLEN(showcmd_buf);
1983 if (len > old_len)
1984 len = old_len;
1985 showcmd_buf[old_len - len] = NUL;
1986
1987 if (!char_avail())
1988 display_showcmd();
1989}
1990
1991/*
1992 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1993 * something and there is a partial mapping.
1994 */
1995 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001996push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997{
1998 if (p_sc)
1999 STRCPY(old_showcmd_buf, showcmd_buf);
2000}
2001
2002 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002003pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004{
2005 if (!p_sc)
2006 return;
2007
2008 STRCPY(showcmd_buf, old_showcmd_buf);
2009
2010 display_showcmd();
2011}
2012
2013 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002014display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015{
2016 int len;
2017
2018 cursor_off();
2019
2020 len = (int)STRLEN(showcmd_buf);
2021 if (len == 0)
2022 showcmd_is_clear = TRUE;
2023 else
2024 {
2025 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
2026 showcmd_is_clear = FALSE;
2027 }
2028
2029 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002030 * clear the rest of an old message by outputting up to SHOWCMD_COLS
2031 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 */
2033 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
2034
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002035 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036}
2037#endif
2038
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039/*
2040 * When "check" is FALSE, prepare for commands that scroll the window.
2041 * When "check" is TRUE, take care of scroll-binding after the window has
2042 * scrolled. Called from normal_cmd() and edit().
2043 */
2044 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002045do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046{
2047 static win_T *old_curwin = NULL;
2048 static linenr_T old_topline = 0;
2049#ifdef FEAT_DIFF
2050 static int old_topfill = 0;
2051#endif
2052 static buf_T *old_buf = NULL;
2053 static colnr_T old_leftcol = 0;
2054
2055 if (check && curwin->w_p_scb)
2056 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002057 // If a ":syncbind" command was just used, don't scroll, only reset
2058 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 if (did_syncbind)
2060 did_syncbind = FALSE;
2061 else if (curwin == old_curwin)
2062 {
2063 /*
2064 * Synchronize other windows, as necessary according to
2065 * 'scrollbind'. Don't do this after an ":edit" command, except
2066 * when 'diff' is set.
2067 */
2068 if ((curwin->w_buffer == old_buf
2069#ifdef FEAT_DIFF
2070 || curwin->w_p_diff
2071#endif
2072 )
2073 && (curwin->w_topline != old_topline
2074#ifdef FEAT_DIFF
2075 || curwin->w_topfill != old_topfill
2076#endif
2077 || curwin->w_leftcol != old_leftcol))
2078 {
2079 check_scrollbind(curwin->w_topline - old_topline,
2080 (long)(curwin->w_leftcol - old_leftcol));
2081 }
2082 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002083 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 {
2085 /*
2086 * When switching between windows, make sure that the relative
2087 * vertical offset is valid for the new window. The relative
2088 * offset is invalid whenever another 'scrollbind' window has
2089 * scrolled to a point that would force the current window to
2090 * scroll past the beginning or end of its buffer. When the
2091 * resync is performed, some of the other 'scrollbind' windows may
2092 * need to jump so that the current window's relative position is
2093 * visible on-screen.
2094 */
2095 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
2096 }
2097 curwin->w_scbind_pos = curwin->w_topline;
2098 }
2099
2100 old_curwin = curwin;
2101 old_topline = curwin->w_topline;
2102#ifdef FEAT_DIFF
2103 old_topfill = curwin->w_topfill;
2104#endif
2105 old_buf = curwin->w_buffer;
2106 old_leftcol = curwin->w_leftcol;
2107}
2108
2109/*
2110 * Synchronize any windows that have "scrollbind" set, based on the
2111 * number of rows by which the current window has changed
2112 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
2113 */
2114 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002115check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116{
2117 int want_ver;
2118 int want_hor;
2119 win_T *old_curwin = curwin;
2120 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 int old_VIsual_select = VIsual_select;
2122 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 colnr_T tgt_leftcol = curwin->w_leftcol;
2124 long topline;
2125 long y;
2126
2127 /*
2128 * check 'scrollopt' string for vertical and horizontal scroll options
2129 */
2130 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
2131#ifdef FEAT_DIFF
2132 want_ver |= old_curwin->w_p_diff;
2133#endif
2134 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
2135
2136 /*
2137 * loop through the scrollbound windows and scroll accordingly
2138 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002140 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 {
2142 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002143 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 if (curwin != old_curwin && curwin->w_p_scb)
2145 {
2146 /*
2147 * do the vertical scroll
2148 */
2149 if (want_ver)
2150 {
2151#ifdef FEAT_DIFF
2152 if (old_curwin->w_p_diff && curwin->w_p_diff)
2153 {
2154 diff_set_topline(old_curwin, curwin);
2155 }
2156 else
2157#endif
2158 {
2159 curwin->w_scbind_pos += topline_diff;
2160 topline = curwin->w_scbind_pos;
2161 if (topline > curbuf->b_ml.ml_line_count)
2162 topline = curbuf->b_ml.ml_line_count;
2163 if (topline < 1)
2164 topline = 1;
2165
2166 y = topline - curwin->w_topline;
2167 if (y > 0)
2168 scrollup(y, FALSE);
2169 else
2170 scrolldown(-y, FALSE);
2171 }
2172
2173 redraw_later(VALID);
2174 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 }
2177
2178 /*
2179 * do the horizontal scroll
2180 */
2181 if (want_hor && curwin->w_leftcol != tgt_leftcol)
2182 {
2183 curwin->w_leftcol = tgt_leftcol;
2184 leftcol_changed();
2185 }
2186 }
2187 }
2188
2189 /*
2190 * reset current-window
2191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 VIsual_select = old_VIsual_select;
2193 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 curwin = old_curwin;
2195 curbuf = old_curbuf;
2196}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197
2198/*
2199 * Command character that's ignored.
2200 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02002201 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002204nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002206 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207}
2208
2209/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002210 * Command character that doesn't do anything, but unlike nv_ignore() does
2211 * start edit(). Used for "startinsert" executed while starting up.
2212 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002213 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002214nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002215{
2216}
2217
2218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 * Command character doesn't exist.
2220 */
2221 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002222nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223{
2224 clearopbeep(cap->oap);
2225}
2226
2227/*
2228 * <Help> and <F1> commands.
2229 */
2230 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002231nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232{
2233 if (!checkclearopq(cap->oap))
2234 ex_help(NULL);
2235}
2236
2237/*
2238 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2239 */
2240 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002241nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002243#ifdef FEAT_JOB_CHANNEL
2244 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2245 clearopbeep(cap->oap);
2246 else
2247#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002248 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002249 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002250 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002251 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2252 op_addsub(cap->oap, cap->count1, cap->arg);
2253 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002254 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002255 else if (VIsual_active)
2256 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002257 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002258 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259}
2260
2261/*
2262 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2263 */
2264 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002265nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266{
2267 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002268 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002269 if (mod_mask & MOD_MASK_CTRL)
2270 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002271 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002272 if (cap->arg == BACKWARD)
2273 goto_tabpage(-(int)cap->count1);
2274 else
2275 goto_tabpage((int)cap->count0);
2276 }
2277 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002278 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280}
2281
2282/*
2283 * Implementation of "gd" and "gD" command.
2284 */
2285 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002286nv_gd(
2287 oparg_T *oap,
2288 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002289 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290{
2291 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 char_u *ptr;
2293
Bram Moolenaard9d30582005-05-18 22:10:28 +00002294 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002295 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
2296 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002298#ifdef FEAT_FOLDING
2299 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2300 foldOpenCursor();
2301#endif
2302}
2303
2304/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002305 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2306 * otherwise.
2307 */
2308 static int
2309is_ident(char_u *line, int offset)
2310{
2311 int i;
2312 int incomment = FALSE;
2313 int instring = 0;
2314 int prev = 0;
2315
2316 for (i = 0; i < offset && line[i] != NUL; i++)
2317 {
2318 if (instring != 0)
2319 {
2320 if (prev != '\\' && line[i] == instring)
2321 instring = 0;
2322 }
2323 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2324 {
2325 instring = line[i];
2326 }
2327 else
2328 {
2329 if (incomment)
2330 {
2331 if (prev == '*' && line[i] == '/')
2332 incomment = FALSE;
2333 }
2334 else if (prev == '/' && line[i] == '*')
2335 {
2336 incomment = TRUE;
2337 }
2338 else if (prev == '/' && line[i] == '/')
2339 {
2340 return FALSE;
2341 }
2342 }
2343
2344 prev = line[i];
2345 }
2346
2347 return incomment == FALSE && instring == 0;
2348}
2349
2350/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002351 * Search for variable declaration of "ptr[len]".
2352 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2353 * current file ("gD").
2354 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002355 * Return FAIL when not found.
2356 */
2357 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002358find_decl(
2359 char_u *ptr,
2360 int len,
2361 int locally,
2362 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002363 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002364{
2365 char_u *pat;
2366 pos_T old_pos;
2367 pos_T par_pos;
2368 pos_T found_pos;
2369 int t;
2370 int save_p_ws;
2371 int save_p_scs;
2372 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002373 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002374 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002375 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002376
2377 if ((pat = alloc(len + 7)) == NULL)
2378 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002379
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002380 // Put "\V" before the pattern to avoid that the special meaning of "."
2381 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002382 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2383 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 old_pos = curwin->w_cursor;
2385 save_p_ws = p_ws;
2386 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002387 p_ws = FALSE; // don't wrap around end of file now
2388 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389
2390 /*
2391 * With "gD" go to line 1.
2392 * With "gd" Search back for the start of the current function, then go
2393 * back until a blank line. If this fails go to line 1.
2394 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002395 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002397 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002399 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 }
2401 else
2402 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002403 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2405 --curwin->w_cursor.lnum;
2406 }
2407 curwin->w_cursor.col = 0;
2408
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002409 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002410 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002411 for (;;)
2412 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002413 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002414 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002415 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002416 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002417
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002418 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002419 {
2420 pos_T *pos;
2421
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002422 // Check that the block the match is in doesn't end before the
2423 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002424 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2425 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2426 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002427 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002428 // There can't be a useful match before the end of this block.
2429 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002430 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002431 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002432 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002433 }
2434
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002435 if (t == FAIL)
2436 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002437 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002438 if (found_pos.lnum != 0)
2439 {
2440 curwin->w_cursor = found_pos;
2441 t = OK;
2442 }
2443 break;
2444 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002445 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002446 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002447 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002448 ++curwin->w_cursor.lnum;
2449 curwin->w_cursor.col = 0;
2450 continue;
2451 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002452 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2453
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002454 // If the current position is not a valid identifier and a previous
2455 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002456 if (!valid && found_pos.lnum != 0)
2457 {
2458 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002459 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002460 }
2461
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002462 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002463 if (valid && !locally)
2464 break;
2465 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002466 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002467 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002468 if (found_pos.lnum != 0)
2469 curwin->w_cursor = found_pos;
2470 break;
2471 }
2472
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002473 // For finding a local variable and the match is before the "{" or
2474 // inside a comment, continue searching. For K&R style function
2475 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002476 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002477 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002478 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002479 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002480 // Remove SEARCH_START from flags to avoid getting stuck at one
2481 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002482 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002484
2485 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002487 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 curwin->w_cursor = old_pos;
2489 }
2490 else
2491 {
2492 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002493 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 reset_search_dir();
2495 }
2496
2497 vim_free(pat);
2498 p_ws = save_p_ws;
2499 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002500
2501 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502}
2503
2504/*
2505 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2506 * lines rather than lines in the file.
2507 * 'dist' must be positive.
2508 *
2509 * Return OK if able to move cursor, FAIL otherwise.
2510 */
2511 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002512nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513{
2514 int linelen = linetabsize(ml_get_curline());
2515 int retval = OK;
2516 int atend = FALSE;
2517 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002518 int col_off1; // margin offset for first screen line
2519 int col_off2; // margin offset for wrapped screen line
2520 int width1; // text width for first screen line
2521 int width2; // test width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522
2523 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002524 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525
2526 col_off1 = curwin_col_off();
2527 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002528 width1 = curwin->w_width - col_off1;
2529 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002530 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002531 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002534 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 /*
2536 * Instead of sticking at the last character of the buffer line we
2537 * try to stick in the last column of the screen.
2538 */
2539 if (curwin->w_curswant == MAXCOL)
2540 {
2541 atend = TRUE;
2542 validate_virtcol();
2543 if (width1 <= 0)
2544 curwin->w_curswant = 0;
2545 else
2546 {
2547 curwin->w_curswant = width1 - 1;
2548 if (curwin->w_virtcol > curwin->w_curswant)
2549 curwin->w_curswant += ((curwin->w_virtcol
2550 - curwin->w_curswant - 1) / width2 + 1) * width2;
2551 }
2552 }
2553 else
2554 {
2555 if (linelen > width1)
2556 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2557 else
2558 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002559 if (curwin->w_curswant >= (colnr_T)n)
2560 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 }
2562
2563 while (dist--)
2564 {
2565 if (dir == BACKWARD)
2566 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002567 if ((long)curwin->w_curswant >= width1
2568#ifdef FEAT_FOLDING
2569 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2570#endif
2571 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002572 // Move back within the line. This can give a negative value
2573 // for w_curswant if width1 < width2 (with cpoptions+=n),
2574 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 curwin->w_curswant -= width2;
2576 else
2577 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002578 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002580 // Move to the start of a closed fold. Don't do that when
2581 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 if (!(fdo_flags & FDO_ALL))
2583 (void)hasFolding(curwin->w_cursor.lnum,
2584 &curwin->w_cursor.lnum, NULL);
2585#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002586 if (curwin->w_cursor.lnum == 1)
2587 {
2588 retval = FAIL;
2589 break;
2590 }
2591 --curwin->w_cursor.lnum;
2592
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 linelen = linetabsize(ml_get_curline());
2594 if (linelen > width1)
2595 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2596 + 1) * width2;
2597 }
2598 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002599 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 {
2601 if (linelen > width1)
2602 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2603 else
2604 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002605 if (curwin->w_curswant + width2 < (colnr_T)n
2606#ifdef FEAT_FOLDING
2607 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2608#endif
2609 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002610 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 curwin->w_curswant += width2;
2612 else
2613 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002614 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002616 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2618 &curwin->w_cursor.lnum);
2619#endif
2620 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2621 {
2622 retval = FAIL;
2623 break;
2624 }
2625 curwin->w_cursor.lnum++;
2626 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002627 // Check if the cursor has moved below the number display
2628 // when width1 < width2 (with cpoptions+=n). Subtract width2
2629 // to get a negative value for w_curswant, which will get
2630 // clipped to column 0.
2631 if (curwin->w_curswant >= width1)
2632 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002633 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 }
2635 }
2636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002639 if (virtual_active() && atend)
2640 coladvance(MAXCOL);
2641 else
2642 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2645 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002646 colnr_T virtcol;
2647
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 /*
2649 * Check for landing on a character that got split at the end of the
2650 * last line. We want to advance a screenline, not end up in the same
2651 * screenline or move two screenlines.
2652 */
2653 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002654 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002655#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002656 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2657 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002658#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002659
2660 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 && (curwin->w_curswant < (colnr_T)width1
2662 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2663 : ((curwin->w_curswant - width1) % width2
2664 > (colnr_T)width2 / 2)))
2665 --curwin->w_cursor.col;
2666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667
2668 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002669 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670
2671 return retval;
2672}
2673
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674/*
2675 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2676 * cap->arg must be TRUE for CTRL-E.
2677 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002678 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002679nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
2681 if (!checkclearop(cap->oap))
2682 scroll_redraw(cap->arg, cap->count1);
2683}
2684
2685/*
2686 * Scroll "count" lines up or down, and redraw.
2687 */
2688 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002689scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690{
2691 linenr_T prev_topline = curwin->w_topline;
2692#ifdef FEAT_DIFF
2693 int prev_topfill = curwin->w_topfill;
2694#endif
2695 linenr_T prev_lnum = curwin->w_cursor.lnum;
2696
2697 if (up)
2698 scrollup(count, TRUE);
2699 else
2700 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002701 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002703 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2704 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 cursor_correct();
2706 check_cursor_moved(curwin);
2707 curwin->w_valid |= VALID_TOPLINE;
2708
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002709 // If moved back to where we were, at least move the cursor, otherwise
2710 // we get stuck at one position. Don't move the cursor up if the
2711 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 while (curwin->w_topline == prev_topline
2713#ifdef FEAT_DIFF
2714 && curwin->w_topfill == prev_topfill
2715#endif
2716 )
2717 {
2718 if (up)
2719 {
2720 if (curwin->w_cursor.lnum > prev_lnum
2721 || cursor_down(1L, FALSE) == FAIL)
2722 break;
2723 }
2724 else
2725 {
2726 if (curwin->w_cursor.lnum < prev_lnum
2727 || prev_topline == 1L
2728 || cursor_up(1L, FALSE) == FAIL)
2729 break;
2730 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002731 // Mark w_topline as valid, otherwise the screen jumps back at the
2732 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 check_cursor_moved(curwin);
2734 curwin->w_valid |= VALID_TOPLINE;
2735 }
2736 }
2737 if (curwin->w_cursor.lnum != prev_lnum)
2738 coladvance(curwin->w_curswant);
2739 redraw_later(VALID);
2740}
2741
2742/*
2743 * Commands that start with "z".
2744 */
2745 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002746nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747{
2748 long n;
2749 colnr_T col;
2750 int nchar = cap->nchar;
2751#ifdef FEAT_FOLDING
2752 long old_fdl = curwin->w_p_fdl;
2753 int old_fen = curwin->w_p_fen;
2754#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002755#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00002756 int undo = FALSE;
2757#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002758 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759
2760 if (VIM_ISDIGIT(nchar))
2761 {
2762 /*
2763 * "z123{nchar}": edit the count before obtaining {nchar}
2764 */
2765 if (checkclearop(cap->oap))
2766 return;
2767 n = nchar - '0';
2768 for (;;)
2769 {
2770#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002771 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772#endif
2773 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002774 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002775 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 --no_mapping;
2778 --allow_keys;
2779#ifdef FEAT_CMDL_INFO
2780 (void)add_to_showcmd(nchar);
2781#endif
2782 if (nchar == K_DEL || nchar == K_KDEL)
2783 n /= 10;
2784 else if (VIM_ISDIGIT(nchar))
2785 n = n * 10 + (nchar - '0');
2786 else if (nchar == CAR)
2787 {
2788#ifdef FEAT_GUI
2789 need_mouse_correct = TRUE;
2790#endif
2791 win_setheight((int)n);
2792 break;
2793 }
2794 else if (nchar == 'l'
2795 || nchar == 'h'
2796 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00002797 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 {
2799 cap->count1 = n ? n * cap->count1 : cap->count1;
2800 goto dozet;
2801 }
2802 else
2803 {
2804 clearopbeep(cap->oap);
2805 break;
2806 }
2807 }
2808 cap->oap->op_type = OP_NOP;
2809 return;
2810 }
2811
2812dozet:
2813 if (
2814#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002815 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2816 // and "zC" only in Visual mode. "zj" and "zk" are motion
2817 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 cap->nchar != 'f' && cap->nchar != 'F'
2819 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2820 && cap->nchar != 'j' && cap->nchar != 'k'
2821 &&
2822#endif
2823 checkclearop(cap->oap))
2824 return;
2825
2826 /*
2827 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2828 * If line number given, set cursor.
2829 */
2830 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2831 && cap->count0
2832 && cap->count0 != curwin->w_cursor.lnum)
2833 {
2834 setpcmark();
2835 if (cap->count0 > curbuf->b_ml.ml_line_count)
2836 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2837 else
2838 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002839 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 }
2841
2842 switch (nchar)
2843 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002844 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 case '+':
2846 if (cap->count0 == 0)
2847 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002848 // No count given: put cursor at the line below screen
2849 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2851 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2852 else
2853 curwin->w_cursor.lnum = curwin->w_botline;
2854 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002855 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 case NL:
2857 case CAR:
2858 case K_KENTER:
2859 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002860 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861
2862 case 't': scroll_cursor_top(0, TRUE);
2863 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002864 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 break;
2866
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002867 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002869 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870
2871 case 'z': scroll_cursor_halfway(TRUE);
2872 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002873 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 break;
2875
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002876 // "z^", "z-" and "zb": put cursor at bottom of screen
2877 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2878 // when <count> is at bottom of window, and puts that one at
2879 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 if (cap->count0 != 0)
2881 {
2882 scroll_cursor_bot(0, TRUE);
2883 curwin->w_cursor.lnum = curwin->w_topline;
2884 }
2885 else if (curwin->w_topline == 1)
2886 curwin->w_cursor.lnum = 1;
2887 else
2888 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002889 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 case '-':
2891 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002892 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893
2894 case 'b': scroll_cursor_bot(0, TRUE);
2895 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002896 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 break;
2898
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002899 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002901 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002902 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002904 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 case 'h':
2906 case K_LEFT:
2907 if (!curwin->w_p_wrap)
2908 {
2909 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2910 curwin->w_leftcol = 0;
2911 else
2912 curwin->w_leftcol -= (colnr_T)cap->count1;
2913 leftcol_changed();
2914 }
2915 break;
2916
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002917 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002918 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002919 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002921 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 case 'l':
2923 case K_RIGHT:
2924 if (!curwin->w_p_wrap)
2925 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002926 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 curwin->w_leftcol += (colnr_T)cap->count1;
2928 leftcol_changed();
2929 }
2930 break;
2931
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002932 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 case 's': if (!curwin->w_p_wrap)
2934 {
2935#ifdef FEAT_FOLDING
2936 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002937 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 else
2939#endif
2940 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002941 if ((long)col > siso)
2942 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 else
2944 col = 0;
2945 if (curwin->w_leftcol != col)
2946 {
2947 curwin->w_leftcol = col;
2948 redraw_later(NOT_VALID);
2949 }
2950 }
2951 break;
2952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002953 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 case 'e': if (!curwin->w_p_wrap)
2955 {
2956#ifdef FEAT_FOLDING
2957 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002958 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 else
2960#endif
2961 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002962 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002963 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 col = 0;
2965 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002966 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 if (curwin->w_leftcol != col)
2968 {
2969 curwin->w_leftcol = col;
2970 redraw_later(NOT_VALID);
2971 }
2972 }
2973 break;
2974
2975#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002976 // "zF": create fold command
2977 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 case 'F':
2979 case 'f': if (foldManualAllowed(TRUE))
2980 {
2981 cap->nchar = 'f';
2982 nv_operator(cap);
2983 curwin->w_p_fen = TRUE;
2984
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002985 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2987 {
2988 nv_operator(cap);
2989 finish_op = TRUE;
2990 }
2991 }
2992 else
2993 clearopbeep(cap->oap);
2994 break;
2995
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002996 // "zd": delete fold at cursor
2997 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 case 'd':
2999 case 'D': if (foldManualAllowed(FALSE))
3000 {
3001 if (VIsual_active)
3002 nv_operator(cap);
3003 else
3004 deleteFold(curwin->w_cursor.lnum,
3005 curwin->w_cursor.lnum, nchar == 'D', FALSE);
3006 }
3007 break;
3008
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003009 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 case 'E': if (foldmethodIsManual(curwin))
3011 {
3012 clearFolding(curwin);
3013 changed_window_setting();
3014 }
3015 else if (foldmethodIsMarker(curwin))
3016 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
3017 TRUE, FALSE);
3018 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003019 emsg(_("E352: Cannot erase folds with current 'foldmethod'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 break;
3021
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003022 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 case 'n': curwin->w_p_fen = FALSE;
3024 break;
3025
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003026 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 case 'N': curwin->w_p_fen = TRUE;
3028 break;
3029
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003030 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
3032 break;
3033
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003034 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3036 openFold(curwin->w_cursor.lnum, cap->count1);
3037 else
3038 {
3039 closeFold(curwin->w_cursor.lnum, cap->count1);
3040 curwin->w_p_fen = TRUE;
3041 }
3042 break;
3043
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003044 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3046 openFoldRecurse(curwin->w_cursor.lnum);
3047 else
3048 {
3049 closeFoldRecurse(curwin->w_cursor.lnum);
3050 curwin->w_p_fen = TRUE;
3051 }
3052 break;
3053
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003054 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 case 'o': if (VIsual_active)
3056 nv_operator(cap);
3057 else
3058 openFold(curwin->w_cursor.lnum, cap->count1);
3059 break;
3060
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003061 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 case 'O': if (VIsual_active)
3063 nv_operator(cap);
3064 else
3065 openFoldRecurse(curwin->w_cursor.lnum);
3066 break;
3067
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003068 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 case 'c': if (VIsual_active)
3070 nv_operator(cap);
3071 else
3072 closeFold(curwin->w_cursor.lnum, cap->count1);
3073 curwin->w_p_fen = TRUE;
3074 break;
3075
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003076 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 case 'C': if (VIsual_active)
3078 nv_operator(cap);
3079 else
3080 closeFoldRecurse(curwin->w_cursor.lnum);
3081 curwin->w_p_fen = TRUE;
3082 break;
3083
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003084 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 case 'v': foldOpenCursor();
3086 break;
3087
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003088 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003090 curwin->w_foldinvalid = TRUE; // recompute folds
3091 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 foldOpenCursor();
3093 break;
3094
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003095 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003097 curwin->w_foldinvalid = TRUE; // recompute folds
3098 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 break;
3100
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003101 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003103 {
3104 curwin->w_p_fdl -= cap->count1;
3105 if (curwin->w_p_fdl < 0)
3106 curwin->w_p_fdl = 0;
3107 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003108 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 curwin->w_p_fen = TRUE;
3110 break;
3111
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003112 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003114 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 curwin->w_p_fen = TRUE;
3116 break;
3117
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003118 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003119 case 'r': curwin->w_p_fdl += cap->count1;
3120 {
3121 int d = getDeepestNesting();
3122
3123 if (curwin->w_p_fdl >= d)
3124 curwin->w_p_fdl = d;
3125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 break;
3127
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003128 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003130 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 break;
3132
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003133 case 'j': // "zj" move to next fold downwards
3134 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
3136 cap->count1) == FAIL)
3137 clearopbeep(cap->oap);
3138 break;
3139
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003140#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003142#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003143 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaard0131a82006-03-04 21:46:13 +00003144 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003145 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003146 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00003147 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00003148 --no_mapping;
3149 --allow_keys;
3150#ifdef FEAT_CMDL_INFO
3151 (void)add_to_showcmd(nchar);
3152#endif
3153 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
3154 {
3155 clearopbeep(cap->oap);
3156 break;
3157 }
3158 undo = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003159 // FALLTHROUGH
Bram Moolenaard0131a82006-03-04 21:46:13 +00003160
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003161 case 'g': // "zg": add good word to word list
3162 case 'w': // "zw": add wrong word to word list
3163 case 'G': // "zG": add good word to temp word list
3164 case 'W': // "zW": add wrong word to temp word list
Bram Moolenaarb765d632005-06-07 21:00:02 +00003165 {
3166 char_u *ptr = NULL;
3167 int len;
3168
3169 if (checkclearop(cap->oap))
3170 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003171 if (VIsual_active && get_visual_text(cap, &ptr, &len)
3172 == FAIL)
3173 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003174 if (ptr == NULL)
3175 {
3176 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003177
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003178 // Find bad word under the cursor. When 'spell' is
3179 // off this fails and find_ident_under_cursor() is
3180 // used below.
Bram Moolenaar134bf072013-09-25 18:54:24 +02003181 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003182 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02003183 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003184 if (len != 0 && curwin->w_cursor.col <= pos.col)
3185 ptr = ml_get_pos(&curwin->w_cursor);
3186 curwin->w_cursor = pos;
3187 }
3188
Bram Moolenaarb765d632005-06-07 21:00:02 +00003189 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
3190 FIND_IDENT)) == 0)
3191 return;
Bram Moolenaar08cc3742019-08-11 22:51:14 +02003192 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
3193 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
Bram Moolenaard0131a82006-03-04 21:46:13 +00003194 (nchar == 'G' || nchar == 'W')
3195 ? 0 : (int)cap->count1,
3196 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003197 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00003198 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003199
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003200 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003201 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003202 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003203 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003204#endif
3205
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 default: clearopbeep(cap->oap);
3207 }
3208
3209#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003210 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 if (old_fen != curwin->w_p_fen)
3212 {
3213# ifdef FEAT_DIFF
3214 win_T *wp;
3215
3216 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3217 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003218 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 FOR_ALL_WINDOWS(wp)
3220 {
3221 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3222 {
3223 wp->w_p_fen = curwin->w_p_fen;
3224 changed_window_setting_win(wp);
3225 }
3226 }
3227 }
3228# endif
3229 changed_window_setting();
3230 }
3231
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003232 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 if (old_fdl != curwin->w_p_fdl)
3234 newFoldLevel();
3235#endif
3236}
3237
3238#ifdef FEAT_GUI
3239/*
3240 * Vertical scrollbar movement.
3241 */
3242 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003243nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244{
3245 if (cap->oap->op_type != OP_NOP)
3246 clearopbeep(cap->oap);
3247
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003248 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 gui_do_scroll();
3250}
3251
3252/*
3253 * Horizontal scrollbar movement.
3254 */
3255 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003256nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257{
3258 if (cap->oap->op_type != OP_NOP)
3259 clearopbeep(cap->oap);
3260
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003261 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003262 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263}
3264#endif
3265
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003266#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003267/*
3268 * Click in GUI tab.
3269 */
3270 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003271nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003272{
3273 if (cap->oap->op_type != OP_NOP)
3274 clearopbeep(cap->oap);
3275
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003276 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003277 goto_tabpage(current_tab);
3278}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003279
3280/*
3281 * Selected item in tab line menu.
3282 */
3283 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003284nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003285{
3286 if (cap->oap->op_type != OP_NOP)
3287 clearopbeep(cap->oap);
3288
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003289 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003290 handle_tabmenu();
3291}
3292
3293/*
3294 * Handle selecting an item of the GUI tab line menu.
3295 * Used in Normal and Insert mode.
3296 */
3297 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003298handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003299{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003300 switch (current_tabmenu)
3301 {
3302 case TABLINE_MENU_CLOSE:
3303 if (current_tab == 0)
3304 do_cmdline_cmd((char_u *)"tabclose");
3305 else
3306 {
3307 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3308 current_tab);
3309 do_cmdline_cmd(IObuff);
3310 }
3311 break;
3312
3313 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003314 if (current_tab == 0)
3315 do_cmdline_cmd((char_u *)"$tabnew");
3316 else
3317 {
3318 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3319 current_tab - 1);
3320 do_cmdline_cmd(IObuff);
3321 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003322 break;
3323
3324 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003325 if (current_tab == 0)
3326 do_cmdline_cmd((char_u *)"browse $tabnew");
3327 else
3328 {
3329 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3330 current_tab - 1);
3331 do_cmdline_cmd(IObuff);
3332 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003333 break;
3334 }
3335}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003336#endif
3337
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338/*
3339 * "Q" command.
3340 */
3341 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003342nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343{
3344 /*
3345 * Ignore 'Q' in Visual mode, just give a beep.
3346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003348 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003349 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 do_exmode(FALSE);
3351}
3352
3353/*
3354 * Handle a ":" command.
3355 */
3356 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003357nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003359 int old_p_im;
3360 int cmd_result;
3361 int is_cmdkey = cap->cmdchar == K_COMMAND;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362
Bram Moolenaar957cf672020-11-12 14:21:06 +01003363 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 nv_operator(cap);
3365 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 {
3367 if (cap->oap->op_type != OP_NOP)
3368 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003369 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 cap->oap->motion_type = MCHAR;
3371 cap->oap->inclusive = FALSE;
3372 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003373 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003375 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 stuffcharReadbuff('.');
3377 if (cap->count0 > 1)
3378 {
3379 stuffReadbuff((char_u *)",.+");
3380 stuffnumReadbuff((long)cap->count0 - 1L);
3381 }
3382 }
3383
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003384 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 if (KeyTyped)
3386 compute_cmdrow();
3387
3388 old_p_im = p_im;
3389
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003390 // get a command line and execute it
Bram Moolenaar957cf672020-11-12 14:21:06 +01003391 cmd_result = do_cmdline(NULL, is_cmdkey ? getcmdkeycmd : getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
3393
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003394 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 if (p_im != old_p_im)
3396 {
3397 if (p_im)
3398 restart_edit = 'i';
3399 else
3400 restart_edit = 0;
3401 }
3402
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003403 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003404 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003405 clearop(cap->oap);
3406 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3408 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003409 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3410 || did_emsg
3411 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003412 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 clearopbeep(cap->oap);
3414 }
3415}
3416
3417/*
3418 * Handle CTRL-G command.
3419 */
3420 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003421nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003423 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 {
3425 VIsual_select = !VIsual_select;
3426 showmode();
3427 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003428 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003429 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 fileinfo((int)cap->count0, FALSE, TRUE);
3431}
3432
3433/*
3434 * Handle CTRL-H <Backspace> command.
3435 */
3436 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003437nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 if (VIsual_active && VIsual_select)
3440 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003441 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 v_visop(cap);
3443 }
3444 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 nv_left(cap);
3446}
3447
3448/*
3449 * CTRL-L: clear screen and redraw.
3450 */
3451 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003452nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453{
3454 if (!checkclearop(cap->oap))
3455 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003457 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003458 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003459# ifdef FEAT_RELTIME
3460 {
3461 win_T *wp;
3462
3463 FOR_ALL_WINDOWS(wp)
3464 wp->w_s->b_syn_slow = FALSE;
3465 }
3466# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467#endif
3468 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003469#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3470# ifdef VIMDLL
3471 if (!gui.in_use)
3472# endif
3473 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 }
3476}
3477
3478/*
3479 * CTRL-O: In Select mode: switch to Visual mode for one command.
3480 * Otherwise: Go to older pcmark.
3481 */
3482 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003483nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 if (VIsual_active && VIsual_select)
3486 {
3487 VIsual_select = FALSE;
3488 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003489 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 }
3491 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 {
3493 cap->count1 = -cap->count1;
3494 nv_pcmark(cap);
3495 }
3496}
3497
3498/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003499 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3500 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 */
3502 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003503nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504{
3505 if (!checkclearopq(cap->oap))
3506 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3507 GETF_SETMARK|GETF_ALT, FALSE);
3508}
3509
3510/*
3511 * "Z" commands.
3512 */
3513 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003514nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515{
3516 if (!checkclearopq(cap->oap))
3517 {
3518 switch (cap->nchar)
3519 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003520 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 case 'Z': do_cmdline_cmd((char_u *)"x");
3522 break;
3523
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003524 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 case 'Q': do_cmdline_cmd((char_u *)"q!");
3526 break;
3527
3528 default: clearopbeep(cap->oap);
3529 }
3530 }
3531}
3532
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533/*
3534 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3535 */
3536 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003537do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538{
3539 oparg_T oa;
3540 cmdarg_T ca;
3541
3542 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003543 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 ca.oap = &oa;
3545 ca.cmdchar = c1;
3546 ca.nchar = c2;
3547 nv_ident(&ca);
3548}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549
3550/*
3551 * Handle the commands that use the word under the cursor.
3552 * [g] CTRL-] :ta to current identifier
3553 * [g] 'K' run program for current identifier
3554 * [g] '*' / to current identifier or string
3555 * [g] '#' ? to current identifier or string
3556 * g ']' :tselect for current identifier
3557 */
3558 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003559nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560{
3561 char_u *ptr = NULL;
3562 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003563 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003564 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003566 char_u *kp; // value of 'keywordprg'
3567 int kp_help; // 'keywordprg' is ":he"
3568 int kp_ex; // 'keywordprg' starts with ":"
3569 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003571 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003572 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 char_u *aux_ptr;
3574 int isman;
3575 int isman_s;
3576
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003577 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 {
3579 cmdchar = cap->nchar;
3580 g_cmd = TRUE;
3581 }
3582 else
3583 {
3584 cmdchar = cap->cmdchar;
3585 g_cmd = FALSE;
3586 }
3587
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003588 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 cmdchar = '#';
3590
3591 /*
3592 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
3593 */
3594 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3595 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3597 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 if (checkclearopq(cap->oap))
3599 return;
3600 }
3601
3602 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3603 (cmdchar == '*' || cmdchar == '#')
3604 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3605 {
3606 clearop(cap->oap);
3607 return;
3608 }
3609
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003610 // Allocate buffer to put the command in. Inserting backslashes can
3611 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3612 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3614 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3615 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003616 if (kp_help && *skipwhite(ptr) == NUL)
3617 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003618 emsg(_(e_noident)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003619 return;
3620 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003621 kp_ex = (*kp == ':');
3622 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3623 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 if (buf == NULL)
3625 return;
3626 buf[0] = NUL;
3627
3628 switch (cmdchar)
3629 {
3630 case '*':
3631 case '#':
3632 /*
3633 * Put cursor at start of word, makes search skip the word
3634 * under the cursor.
3635 * Call setpcmark() first, so "*``" puts the cursor back where
3636 * it was.
3637 */
3638 setpcmark();
3639 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3640
3641 if (!g_cmd && vim_iswordp(ptr))
3642 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003643 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 break;
3645
3646 case 'K':
3647 if (kp_help)
3648 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003649 else if (kp_ex)
3650 {
3651 if (cap->count0 != 0)
3652 vim_snprintf((char *)buf, buflen, "%s %ld",
3653 kp, cap->count0);
3654 else
3655 STRCPY(buf, kp);
3656 STRCAT(buf, " ");
3657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 else
3659 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003660 // An external command will probably use an argument starting
3661 // with "-" as an option. To avoid trouble we skip the "-".
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003662 while (*ptr == '-' && n > 0)
3663 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003664 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003665 --n;
3666 }
3667 if (n == 0)
3668 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003669 emsg(_(e_noident)); // found dashes only
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003670 vim_free(buf);
3671 return;
3672 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003673
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003674 // When a count is given, turn it into a range. Is this
3675 // really what we want?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 isman = (STRCMP(kp, "man") == 0);
3677 isman_s = (STRCMP(kp, "man -s") == 0);
3678 if (cap->count0 != 0 && !(isman || isman_s))
3679 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3680
3681 STRCAT(buf, "! ");
3682 if (cap->count0 == 0 && isman_s)
3683 STRCAT(buf, "man");
3684 else
3685 STRCAT(buf, kp);
3686 STRCAT(buf, " ");
3687 if (cap->count0 != 0 && (isman || isman_s))
3688 {
3689 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3690 STRCAT(buf, " ");
3691 }
3692 }
3693 break;
3694
3695 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003696 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697#ifdef FEAT_CSCOPE
3698 if (p_cst)
3699 STRCPY(buf, "cstag ");
3700 else
3701#endif
3702 STRCPY(buf, "ts ");
3703 break;
3704
3705 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003706 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 if (curbuf->b_help)
3708 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003710 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003711 if (g_cmd)
3712 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003713 else if (cap->count0 == 0)
3714 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003715 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003716 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 }
3719
3720 /*
3721 * Now grab the chars in the identifier
3722 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003723 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003725 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003726 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003727 // Escape the argument properly for an Ex command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003728 p = vim_strsave_fnameescape(ptr, FALSE);
3729 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003730 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003731 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003732 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003733 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003735 vim_free(buf);
3736 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003738 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003739 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003740 {
3741 vim_free(buf);
3742 vim_free(p);
3743 return;
3744 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003745 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003746 STRCAT(buf, p);
3747 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003749 else
3750 {
3751 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003752 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003753 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003754 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003755 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003756 {
3757 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003758 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003759 aux_ptr = (char_u *)"";
3760 else
3761 aux_ptr = (char_u *)"\\|\"\n[";
3762 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003763 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003764 aux_ptr = (char_u *)"\\|\"\n*?[";
3765
3766 p = buf + STRLEN(buf);
3767 while (n-- > 0)
3768 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003769 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003770 if (vim_strchr(aux_ptr, *ptr) != NULL)
3771 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003772 // When current byte is a part of multibyte character, copy all
3773 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003774 if (has_mbyte)
3775 {
3776 int i;
3777 int len = (*mb_ptr2len)(ptr) - 1;
3778
3779 for (i = 0; i < len && n >= 1; ++i, --n)
3780 *p++ = *ptr++;
3781 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003782 *p++ = *ptr++;
3783 }
3784 *p = NUL;
3785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786
3787 /*
3788 * Execute the command.
3789 */
3790 if (cmdchar == '*' || cmdchar == '#')
3791 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003792 if (!g_cmd && (has_mbyte
3793 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3794 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003796
3797 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003798 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003800
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003801 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 }
3803 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003804 {
3805 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003807 g_tag_at_cursor = FALSE;
3808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809
3810 vim_free(buf);
3811}
3812
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813/*
3814 * Get visually selected text, within one line only.
3815 * Returns FAIL if more than one line selected.
3816 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003817 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003818get_visual_text(
3819 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003820 char_u **pp, // return: start of selected text
3821 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822{
3823 if (VIsual_mode != 'V')
3824 unadjust_for_sel();
3825 if (VIsual.lnum != curwin->w_cursor.lnum)
3826 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003827 if (cap != NULL)
3828 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 return FAIL;
3830 }
3831 if (VIsual_mode == 'V')
3832 {
3833 *pp = ml_get_curline();
3834 *lenp = (int)STRLEN(*pp);
3835 }
3836 else
3837 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003838 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 {
3840 *pp = ml_get_pos(&curwin->w_cursor);
3841 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3842 }
3843 else
3844 {
3845 *pp = ml_get_pos(&VIsual);
3846 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 if (has_mbyte)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003849 // Correct the length to include the whole last character.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003850 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 }
3852 reset_VIsual_and_resel();
3853 return OK;
3854}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855
3856/*
3857 * CTRL-T: backwards in tag stack
3858 */
3859 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003860nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861{
3862 if (!checkclearopq(cap->oap))
3863 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3864}
3865
3866/*
3867 * Handle scrolling command 'H', 'L' and 'M'.
3868 */
3869 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003870nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871{
3872 int used = 0;
3873 long n;
3874#ifdef FEAT_FOLDING
3875 linenr_T lnum;
3876#endif
3877 int half;
3878
3879 cap->oap->motion_type = MLINE;
3880 setpcmark();
3881
3882 if (cap->cmdchar == 'L')
3883 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003884 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 curwin->w_cursor.lnum = curwin->w_botline - 1;
3886 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3887 curwin->w_cursor.lnum = 1;
3888 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003889 {
3890#ifdef FEAT_FOLDING
3891 if (hasAnyFolding(curwin))
3892 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003893 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003894 for (n = cap->count1 - 1; n > 0
3895 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3896 {
3897 (void)hasFolding(curwin->w_cursor.lnum,
3898 &curwin->w_cursor.lnum, NULL);
3899 --curwin->w_cursor.lnum;
3900 }
3901 }
3902 else
3903#endif
3904 curwin->w_cursor.lnum -= cap->count1 - 1;
3905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 }
3907 else
3908 {
3909 if (cap->cmdchar == 'M')
3910 {
3911#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003912 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 used -= diff_check_fill(curwin, curwin->w_topline)
3914 - curwin->w_topfill;
3915#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003916 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3918 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3919 {
3920#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003921 // Count half he number of filler lines to be "below this
3922 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3924 + n) / 2 >= half)
3925 {
3926 --n;
3927 break;
3928 }
3929#endif
3930 used += plines(curwin->w_topline + n);
3931 if (used >= half)
3932 break;
3933#ifdef FEAT_FOLDING
3934 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3935 n = lnum - curwin->w_topline;
3936#endif
3937 }
3938 if (n > 0 && used > curwin->w_height)
3939 --n;
3940 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003941 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003942 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003944#ifdef FEAT_FOLDING
3945 if (hasAnyFolding(curwin))
3946 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003947 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003948 lnum = curwin->w_topline;
3949 while (n-- > 0 && lnum < curwin->w_botline - 1)
3950 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003951 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003952 ++lnum;
3953 }
3954 n = lnum - curwin->w_topline;
3955 }
3956#endif
3957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 curwin->w_cursor.lnum = curwin->w_topline + n;
3959 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3960 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3961 }
3962
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003963 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003964 if (cap->oap->op_type == OP_NOP)
3965 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 beginline(BL_SOL | BL_FIX);
3967}
3968
3969/*
3970 * Cursor right commands.
3971 */
3972 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003973nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974{
3975 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003976 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003978 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3979 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003980 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003981 if (mod_mask & MOD_MASK_CTRL)
3982 cap->arg = TRUE;
3983 nv_wordcmd(cap);
3984 return;
3985 }
3986
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 cap->oap->motion_type = MCHAR;
3988 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003989 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003992 * In virtual edit mode, there's no such thing as "past_line", as lines
3993 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 */
3995 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003996 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997
3998 for (n = cap->count1; n > 0; --n)
3999 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004000 if ((!past_line && oneright() == FAIL)
4001 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004002 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 {
4004 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004005 * <Space> wraps to next line if 'whichwrap' has 's'.
4006 * 'l' wraps to next line if 'whichwrap' has 'l'.
4007 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 */
4009 if ( ((cap->cmdchar == ' '
4010 && vim_strchr(p_ww, 's') != NULL)
4011 || (cap->cmdchar == 'l'
4012 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004013 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 && vim_strchr(p_ww, '>') != NULL))
4015 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4016 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004017 // When deleting we also count the NL as a character.
4018 // Set cap->oap->inclusive when last char in the line is
4019 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004020 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004022 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 cap->oap->inclusive = TRUE;
4024 else
4025 {
4026 ++curwin->w_cursor.lnum;
4027 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 curwin->w_set_curswant = TRUE;
4030 cap->oap->inclusive = FALSE;
4031 }
4032 continue;
4033 }
4034 if (cap->oap->op_type == OP_NOP)
4035 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004036 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 if (n == cap->count1)
4038 beep_flush();
4039 }
4040 else
4041 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004042 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 cap->oap->inclusive = TRUE;
4044 }
4045 break;
4046 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004047 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 {
4049 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 if (virtual_active())
4051 oneright();
4052 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02004055 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 ++curwin->w_cursor.col;
4058 }
4059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 }
4061#ifdef FEAT_FOLDING
4062 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4063 && cap->oap->op_type == OP_NOP)
4064 foldOpenCursor();
4065#endif
4066}
4067
4068/*
4069 * Cursor left commands.
4070 *
4071 * Returns TRUE when operator end should not be adjusted.
4072 */
4073 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004074nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075{
4076 long n;
4077
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004078 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4079 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004080 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004081 if (mod_mask & MOD_MASK_CTRL)
4082 cap->arg = 1;
4083 nv_bck_word(cap);
4084 return;
4085 }
4086
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 cap->oap->motion_type = MCHAR;
4088 cap->oap->inclusive = FALSE;
4089 for (n = cap->count1; n > 0; --n)
4090 {
4091 if (oneleft() == FAIL)
4092 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004093 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
4094 // 'h' wraps to previous line if 'whichwrap' has 'h'.
4095 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 if ( (((cap->cmdchar == K_BS
4097 || cap->cmdchar == Ctrl_H)
4098 && vim_strchr(p_ww, 'b') != NULL)
4099 || (cap->cmdchar == 'h'
4100 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004101 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 && vim_strchr(p_ww, '<') != NULL))
4103 && curwin->w_cursor.lnum > 1)
4104 {
4105 --(curwin->w_cursor.lnum);
4106 coladvance((colnr_T)MAXCOL);
4107 curwin->w_set_curswant = TRUE;
4108
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004109 // When the NL before the first char has to be deleted we
4110 // put the cursor on the NUL after the previous line.
4111 // This is a very special case, be careful!
4112 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 if ( (cap->oap->op_type == OP_DELETE
4114 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004115 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004117 char_u *cp = ml_get_cursor();
4118
4119 if (*cp != NUL)
4120 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004121 if (has_mbyte)
4122 curwin->w_cursor.col += (*mb_ptr2len)(cp);
4123 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004124 ++curwin->w_cursor.col;
4125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 cap->retval |= CA_NO_ADJ_OP_END;
4127 }
4128 continue;
4129 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004130 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
4132 beep_flush();
4133 break;
4134 }
4135 }
4136#ifdef FEAT_FOLDING
4137 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4138 && cap->oap->op_type == OP_NOP)
4139 foldOpenCursor();
4140#endif
4141}
4142
4143/*
4144 * Cursor up commands.
4145 * cap->arg is TRUE for "-": Move cursor to first non-blank.
4146 */
4147 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004148nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004150 if (mod_mask & MOD_MASK_SHIFT)
4151 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004152 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004153 cap->arg = BACKWARD;
4154 nv_page(cap);
4155 }
4156 else
4157 {
4158 cap->oap->motion_type = MLINE;
4159 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4160 clearopbeep(cap->oap);
4161 else if (cap->arg)
4162 beginline(BL_WHITE | BL_FIX);
4163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164}
4165
4166/*
4167 * Cursor down commands.
4168 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4169 */
4170 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004171nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004173 if (mod_mask & MOD_MASK_SHIFT)
4174 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004175 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004176 cap->arg = FORWARD;
4177 nv_page(cap);
4178 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004179#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004180 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004181 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4182 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004184 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 {
4186#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004187 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004188 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 cmdwin_result = CAR;
4190 else
4191#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004192#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004193 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004194 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4195 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4196 {
4197 invoke_prompt_callback();
4198 if (restart_edit == 0)
4199 restart_edit = 'a';
4200 }
4201 else
4202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 {
4204 cap->oap->motion_type = MLINE;
4205 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4206 clearopbeep(cap->oap);
4207 else if (cap->arg)
4208 beginline(BL_WHITE | BL_FIX);
4209 }
4210 }
4211}
4212
4213#ifdef FEAT_SEARCHPATH
4214/*
4215 * Grab the file name under the cursor and edit it.
4216 */
4217 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004218nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219{
4220 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004221 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004223 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 {
4225 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004226 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 return;
4228 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004229 if (curbuf_locked())
4230 {
4231 clearop(cap->oap);
4232 return;
4233 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004234#ifdef FEAT_PROP_POPUP
4235 if (ERROR_IF_TERM_POPUP_WINDOW)
4236 return;
4237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004239 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240
4241 if (ptr != NULL)
4242 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004243 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004244 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004245 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004247 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004248 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004249 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004250 {
4251 curwin->w_cursor.lnum = lnum;
4252 check_cursor_lnum();
4253 beginline(BL_SOL | BL_FIX);
4254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 vim_free(ptr);
4256 }
4257 else
4258 clearop(cap->oap);
4259}
4260#endif
4261
4262/*
4263 * <End> command: to end of current line or last line.
4264 */
4265 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004266nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004268 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004270 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004272 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
4274 nv_dollar(cap);
4275}
4276
4277/*
4278 * Handle the "$" command.
4279 */
4280 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004281nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282{
4283 cap->oap->motion_type = MCHAR;
4284 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004285 // In virtual mode when off the edge of a line and an operator
4286 // is pending (whew!) keep the cursor where it is.
4287 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 if (!virtual_active() || gchar_cursor() != NUL
4289 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004290 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 if (cursor_down((long)(cap->count1 - 1),
4292 cap->oap->op_type == OP_NOP) == FAIL)
4293 clearopbeep(cap->oap);
4294#ifdef FEAT_FOLDING
4295 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4296 foldOpenCursor();
4297#endif
4298}
4299
4300/*
4301 * Implementation of '?' and '/' commands.
4302 * If cap->arg is TRUE don't set PC mark.
4303 */
4304 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004305nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306{
4307 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004308 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
4310 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4311 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004312 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 cap->cmdchar = 'g';
4314 cap->nchar = '?';
4315 nv_operator(cap);
4316 return;
4317 }
4318
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004319 // When using 'incsearch' the cursor may be moved to set a different search
4320 // start position.
Bram Moolenaare96a2492019-06-25 04:12:16 +02004321 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
4323 if (cap->searchbuf == NULL)
4324 {
4325 clearop(oap);
4326 return;
4327 }
4328
Bram Moolenaar46539112015-02-17 15:43:57 +01004329 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004330 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004331 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332}
4333
4334/*
4335 * Handle "N" and "n" commands.
4336 * cap->arg is SEARCH_REV for "N", 0 for "n".
4337 */
4338 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004339nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004341 pos_T old = curwin->w_cursor;
4342 int wrapped = FALSE;
4343 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004344
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004345 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004346 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004347 // Avoid getting stuck on the current cursor position, which can
4348 // happen when an offset is given and the cursor is on the last char
4349 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004350 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004351 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004352 cap->count1 -= 1;
4353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354}
4355
4356/*
4357 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4358 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004359 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004361 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004362normal_search(
4363 cmdarg_T *cap,
4364 int dir,
4365 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004366 int opt, // extra flags for do_search()
4367 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368{
4369 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004370 searchit_arg_T sia;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371
4372 cap->oap->motion_type = MCHAR;
4373 cap->oap->inclusive = FALSE;
4374 cap->oap->use_reg_one = TRUE;
4375 curwin->w_set_curswant = TRUE;
4376
Bram Moolenaara80faa82020-04-12 19:37:17 +02004377 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004378 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004379 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4380 if (wrapped != NULL)
4381 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 if (i == 0)
4383 clearop(cap->oap);
4384 else
4385 {
4386 if (i == 2)
4387 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389#ifdef FEAT_FOLDING
4390 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4391 foldOpenCursor();
4392#endif
4393 }
4394
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004395 // "/$" will put the cursor after the end of the line, may need to
4396 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004398 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399}
4400
4401/*
4402 * Character search commands.
4403 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4404 * ',' and FALSE for ';'.
4405 * cap->nchar is NUL for ',' and ';' (repeat the search)
4406 */
4407 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004408nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409{
4410 int t_cmd;
4411
4412 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4413 t_cmd = TRUE;
4414 else
4415 t_cmd = FALSE;
4416
4417 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4419 clearopbeep(cap->oap);
4420 else
4421 {
4422 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004423 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4425 && (t_cmd || cap->oap->op_type != OP_NOP))
4426 {
4427 colnr_T scol, ecol;
4428
4429 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4430 curwin->w_cursor.coladd = ecol - scol;
4431 }
4432 else
4433 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435#ifdef FEAT_FOLDING
4436 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4437 foldOpenCursor();
4438#endif
4439 }
4440}
4441
4442/*
4443 * "[" and "]" commands.
4444 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4445 */
4446 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004447nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004449 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004451 pos_T *pos = NULL; // init for GCC
4452 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 int flag;
4454 long n;
4455 int findc;
4456 int c;
4457
4458 cap->oap->motion_type = MCHAR;
4459 cap->oap->inclusive = FALSE;
4460 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004461 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462
4463#ifdef FEAT_SEARCHPATH
4464 /*
4465 * "[f" or "]f" : Edit file under the cursor (same as "gf")
4466 */
4467 if (cap->nchar == 'f')
4468 nv_gotofile(cap);
4469 else
4470#endif
4471
4472#ifdef FEAT_FIND_ID
4473 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004474 * Find the occurrence(s) of the identifier or define under cursor
4475 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 *
4477 * search list jump
4478 * fwd bwd fwd bwd fwd bwd
4479 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4480 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
4481 */
4482 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004483# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004485# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004487# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 cap->nchar) != NULL)
4489 {
4490 char_u *ptr;
4491 int len;
4492
4493 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4494 clearop(cap->oap);
4495 else
4496 {
4497 find_pattern_in_path(ptr, 0, len, TRUE,
4498 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4499 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4500 cap->count1,
4501 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4502 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4503 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4504 (linenr_T)MAXLNUM);
4505 curwin->w_set_curswant = TRUE;
4506 }
4507 }
4508 else
4509#endif
4510
4511 /*
4512 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4513 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4514 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4515 * "[m" or "]m" search for prev/next start of (Java) method.
4516 * "[M" or "]M" search for prev/next end of (Java) method.
4517 */
4518 if ( (cap->cmdchar == '['
4519 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4520 || (cap->cmdchar == ']'
4521 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
4522 {
4523 if (cap->nchar == '*')
4524 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 prev_pos.lnum = 0;
4526 if (cap->nchar == 'm' || cap->nchar == 'M')
4527 {
4528 if (cap->cmdchar == '[')
4529 findc = '{';
4530 else
4531 findc = '}';
4532 n = 9999;
4533 }
4534 else
4535 {
4536 findc = cap->nchar;
4537 n = cap->count1;
4538 }
4539 for ( ; n > 0; --n)
4540 {
4541 if ((pos = findmatchlimit(cap->oap, findc,
4542 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4543 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004544 if (new_pos.lnum == 0) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 {
4546 if (cap->nchar != 'm' && cap->nchar != 'M')
4547 clearopbeep(cap->oap);
4548 }
4549 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004550 pos = &new_pos; // use last one found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 break;
4552 }
4553 prev_pos = new_pos;
4554 curwin->w_cursor = *pos;
4555 new_pos = *pos;
4556 }
4557 curwin->w_cursor = old_pos;
4558
4559 /*
4560 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4561 * brought us to the match for "[m" and "]M" when inside a method.
4562 * Try finding the '{' or '}' we want to be at.
4563 * Also repeat for the given count.
4564 */
4565 if (cap->nchar == 'm' || cap->nchar == 'M')
4566 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004567 // norm is TRUE for "]M" and "[m"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 int norm = ((findc == '{') == (cap->nchar == 'm'));
4569
4570 n = cap->count1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004571 // found a match: we were inside a method
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 if (prev_pos.lnum != 0)
4573 {
4574 pos = &prev_pos;
4575 curwin->w_cursor = prev_pos;
4576 if (norm)
4577 --n;
4578 }
4579 else
4580 pos = NULL;
4581 while (n > 0)
4582 {
4583 for (;;)
4584 {
4585 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4586 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004587 // if not found anything, that's an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 if (pos == NULL)
4589 clearopbeep(cap->oap);
4590 n = 0;
4591 break;
4592 }
4593 c = gchar_cursor();
4594 if (c == '{' || c == '}')
4595 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004596 // Must have found end/start of class: use it.
4597 // Or found the place to be at.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 if ((c == findc && norm) || (n == 1 && !norm))
4599 {
4600 new_pos = curwin->w_cursor;
4601 pos = &new_pos;
4602 n = 0;
4603 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004604 // if no match found at all, we started outside of the
4605 // class and we're inside now. Just go on.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 else if (new_pos.lnum == 0)
4607 {
4608 new_pos = curwin->w_cursor;
4609 pos = &new_pos;
4610 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004611 // found start/end of other method: go to match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 else if ((pos = findmatchlimit(cap->oap, findc,
4613 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4614 0)) == NULL)
4615 n = 0;
4616 else
4617 curwin->w_cursor = *pos;
4618 break;
4619 }
4620 }
4621 --n;
4622 }
4623 curwin->w_cursor = old_pos;
4624 if (pos == NULL && new_pos.lnum != 0)
4625 clearopbeep(cap->oap);
4626 }
4627 if (pos != NULL)
4628 {
4629 setpcmark();
4630 curwin->w_cursor = *pos;
4631 curwin->w_set_curswant = TRUE;
4632#ifdef FEAT_FOLDING
4633 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4634 && cap->oap->op_type == OP_NOP)
4635 foldOpenCursor();
4636#endif
4637 }
4638 }
4639
4640 /*
4641 * "[[", "[]", "]]" and "][": move to start or end of function
4642 */
4643 else if (cap->nchar == '[' || cap->nchar == ']')
4644 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004645 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 flag = '{';
4647 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004648 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649
4650 curwin->w_set_curswant = TRUE;
4651 /*
4652 * Imitate strange Vi behaviour: When using "]]" with an operator
4653 * we also stop at '}'.
4654 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004655 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 (cap->oap->op_type != OP_NOP
4657 && cap->arg == FORWARD && flag == '{')))
4658 clearopbeep(cap->oap);
4659 else
4660 {
4661 if (cap->oap->op_type == OP_NOP)
4662 beginline(BL_WHITE | BL_FIX);
4663#ifdef FEAT_FOLDING
4664 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4665 foldOpenCursor();
4666#endif
4667 }
4668 }
4669
4670 /*
4671 * "[p", "[P", "]P" and "]p": put with indent adjustment
4672 */
4673 else if (cap->nchar == 'p' || cap->nchar == 'P')
4674 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004675 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 }
4677
4678 /*
4679 * "['", "[`", "]'" and "]`": jump to next mark
4680 */
4681 else if (cap->nchar == '\'' || cap->nchar == '`')
4682 {
4683 pos = &curwin->w_cursor;
4684 for (n = cap->count1; n > 0; --n)
4685 {
4686 prev_pos = *pos;
4687 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4688 cap->nchar == '\'');
4689 if (pos == NULL)
4690 break;
4691 }
4692 if (pos == NULL)
4693 pos = &prev_pos;
4694 nv_cursormark(cap, cap->nchar == '\'', pos);
4695 }
4696
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 /*
4698 * [ or ] followed by a middle mouse click: put selected text with
4699 * indent adjustment. Any other button just does as usual.
4700 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004701 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 {
4703 (void)do_mouse(cap->oap, cap->nchar,
4704 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4705 cap->count1, PUT_FIXINDENT);
4706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707
4708#ifdef FEAT_FOLDING
4709 /*
4710 * "[z" and "]z": move to start or end of open fold.
4711 */
4712 else if (cap->nchar == 'z')
4713 {
4714 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4715 cap->count1) == FAIL)
4716 clearopbeep(cap->oap);
4717 }
4718#endif
4719
4720#ifdef FEAT_DIFF
4721 /*
4722 * "[c" and "]c": move to next or previous diff-change.
4723 */
4724 else if (cap->nchar == 'c')
4725 {
4726 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4727 cap->count1) == FAIL)
4728 clearopbeep(cap->oap);
4729 }
4730#endif
4731
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004732#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004733 /*
4734 * "[s", "[S", "]s" and "]S": move to next spell error.
4735 */
4736 else if (cap->nchar == 's' || cap->nchar == 'S')
4737 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004738 setpcmark();
4739 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004740 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4741 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004742 {
4743 clearopbeep(cap->oap);
4744 break;
4745 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004746 else
4747 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004748# ifdef FEAT_FOLDING
4749 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4750 foldOpenCursor();
4751# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004752 }
4753#endif
4754
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004755 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 else
4757 clearopbeep(cap->oap);
4758}
4759
4760/*
4761 * Handle Normal mode "%" command.
4762 */
4763 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004764nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765{
4766 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004767#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 linenr_T lnum = curwin->w_cursor.lnum;
4769#endif
4770
4771 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004772 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 {
4774 if (cap->count0 > 100)
4775 clearopbeep(cap->oap);
4776 else
4777 {
4778 cap->oap->motion_type = MLINE;
4779 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004780 // Round up, so 'normal 100%' always jumps at the line line.
4781 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4782 // overflow on 32-bits, so use a formula with less accuracy
4783 // to avoid overflows.
4784 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4786 / 100L * cap->count0;
4787 else
4788 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4789 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004790 if (curwin->w_cursor.lnum < 1)
4791 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4793 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4794 beginline(BL_SOL | BL_FIX);
4795 }
4796 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004797 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 {
4799 cap->oap->motion_type = MCHAR;
4800 cap->oap->use_reg_one = TRUE;
4801 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4802 clearopbeep(cap->oap);
4803 else
4804 {
4805 setpcmark();
4806 curwin->w_cursor = *pos;
4807 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 }
4811 }
4812#ifdef FEAT_FOLDING
4813 if (cap->oap->op_type == OP_NOP
4814 && lnum != curwin->w_cursor.lnum
4815 && (fdo_flags & FDO_PERCENT)
4816 && KeyTyped)
4817 foldOpenCursor();
4818#endif
4819}
4820
4821/*
4822 * Handle "(" and ")" commands.
4823 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4824 */
4825 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004826nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827{
4828 cap->oap->motion_type = MCHAR;
4829 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004830 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004831 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 curwin->w_set_curswant = TRUE;
4833
4834 if (findsent(cap->arg, cap->count1) == FAIL)
4835 clearopbeep(cap->oap);
4836 else
4837 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004838 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004839 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841#ifdef FEAT_FOLDING
4842 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4843 foldOpenCursor();
4844#endif
4845 }
4846}
4847
4848/*
4849 * "m" command: Mark a position.
4850 */
4851 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004852nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853{
4854 if (!checkclearop(cap->oap))
4855 {
4856 if (setmark(cap->nchar) == FAIL)
4857 clearopbeep(cap->oap);
4858 }
4859}
4860
4861/*
4862 * "{" and "}" commands.
4863 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4864 */
4865 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004866nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867{
4868 cap->oap->motion_type = MCHAR;
4869 cap->oap->inclusive = FALSE;
4870 cap->oap->use_reg_one = TRUE;
4871 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004872 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 clearopbeep(cap->oap);
4874 else
4875 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877#ifdef FEAT_FOLDING
4878 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4879 foldOpenCursor();
4880#endif
4881 }
4882}
4883
4884/*
4885 * "u" command: Undo or make lower case.
4886 */
4887 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004888nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004890 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004892 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 cap->cmdchar = 'g';
4894 cap->nchar = 'u';
4895 nv_operator(cap);
4896 }
4897 else
4898 nv_kundo(cap);
4899}
4900
4901/*
4902 * <Undo> command.
4903 */
4904 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004905nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906{
4907 if (!checkclearopq(cap->oap))
4908 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004909#ifdef FEAT_JOB_CHANNEL
4910 if (bt_prompt(curbuf))
4911 {
4912 clearopbeep(cap->oap);
4913 return;
4914 }
4915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 u_undo((int)cap->count1);
4917 curwin->w_set_curswant = TRUE;
4918 }
4919}
4920
4921/*
4922 * Handle the "r" command.
4923 */
4924 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004925nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926{
4927 char_u *ptr;
4928 int had_ctrl_v;
4929 long n;
4930
4931 if (checkclearop(cap->oap))
4932 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004933#ifdef FEAT_JOB_CHANNEL
4934 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4935 {
4936 clearopbeep(cap->oap);
4937 return;
4938 }
4939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004941 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 if (cap->nchar == Ctrl_V)
4943 {
4944 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004945 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004946 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 if (cap->nchar > DEL)
4948 had_ctrl_v = NUL;
4949 }
4950 else
4951 had_ctrl_v = NUL;
4952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004953 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004954 if (IS_SPECIAL(cap->nchar))
4955 {
4956 clearopbeep(cap->oap);
4957 return;
4958 }
4959
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004960 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 if (VIsual_active)
4962 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004963 if (got_int)
4964 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004965 if (had_ctrl_v)
4966 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004967 // Use a special (negative) number to make a difference between a
4968 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004969 if (cap->nchar == CAR)
4970 cap->nchar = REPLACE_CR_NCHAR;
4971 else if (cap->nchar == NL)
4972 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 nv_operator(cap);
4975 return;
4976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004978 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 if (virtual_active())
4980 {
4981 if (u_save_cursor() == FAIL)
4982 return;
4983 if (gchar_cursor() == NUL)
4984 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004985 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 coladvance_force((colnr_T)(getviscol() + cap->count1));
4987 curwin->w_cursor.col -= cap->count1;
4988 }
4989 else if (gchar_cursor() == TAB)
4990 coladvance_force(getviscol());
4991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004993 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004995 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004996 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 {
4998 clearopbeep(cap->oap);
4999 return;
5000 }
5001
5002 /*
5003 * Replacing with a TAB is done by edit() when it is complicated because
5004 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
5005 * Other characters are done below to avoid problems with things like
5006 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
5007 */
5008 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
5009 {
5010 stuffnumReadbuff(cap->count1);
5011 stuffcharReadbuff('R');
5012 stuffcharReadbuff('\t');
5013 stuffcharReadbuff(ESC);
5014 return;
5015 }
5016
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005017 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 if (u_save_cursor() == FAIL)
5019 return;
5020
5021 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
5022 {
5023 /*
5024 * Replace character(s) by a single newline.
5025 * Strange vi behaviour: Only one newline is inserted.
5026 * Delete the characters here.
5027 * Insert the newline with an insert command, takes care of
5028 * autoindent. The insert command depends on being on the last
5029 * character of a line or not.
5030 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005031 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 stuffcharReadbuff('\r');
5033 stuffcharReadbuff(ESC);
5034
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005035 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 invoke_edit(cap, TRUE, 'r', FALSE);
5037 }
5038 else
5039 {
5040 prep_redo(cap->oap->regname, cap->count1,
5041 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
5042
5043 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 if (has_mbyte)
5045 {
5046 int old_State = State;
5047
5048 if (cap->ncharC1 != 0)
5049 AppendCharToRedobuff(cap->ncharC1);
5050 if (cap->ncharC2 != 0)
5051 AppendCharToRedobuff(cap->ncharC2);
5052
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005053 // This is slow, but it handles replacing a single-byte with a
5054 // multi-byte and the other way around. Also handles adding
5055 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 for (n = cap->count1; n > 0; --n)
5057 {
5058 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02005059 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5060 {
5061 int c = ins_copychar(curwin->w_cursor.lnum
5062 + (cap->nchar == Ctrl_Y ? -1 : 1));
5063 if (c != NUL)
5064 ins_char(c);
5065 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005066 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02005067 ++curwin->w_cursor.col;
5068 }
5069 else
5070 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 State = old_State;
5072 if (cap->ncharC1 != 0)
5073 ins_char(cap->ncharC1);
5074 if (cap->ncharC2 != 0)
5075 ins_char(cap->ncharC2);
5076 }
5077 }
5078 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 {
5080 /*
5081 * Replace the characters within one line.
5082 */
5083 for (n = cap->count1; n > 0; --n)
5084 {
5085 /*
5086 * Get ptr again, because u_save and/or showmatch() will have
5087 * released the line. At the same time we let know that the
5088 * line will be changed.
5089 */
5090 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005091 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5092 {
5093 int c = ins_copychar(curwin->w_cursor.lnum
5094 + (cap->nchar == Ctrl_Y ? -1 : 1));
5095 if (c != NUL)
5096 ptr[curwin->w_cursor.col] = c;
5097 }
5098 else
5099 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 if (p_sm && msg_silent == 0)
5101 showmatch(cap->nchar);
5102 ++curwin->w_cursor.col;
5103 }
5104#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005105 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005107 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108
Bram Moolenaar009b2592004-10-24 19:18:58 +00005109 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005110 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005112 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 }
5114#endif
5115
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005116 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 changed_bytes(curwin->w_cursor.lnum,
5118 (colnr_T)(curwin->w_cursor.col - cap->count1));
5119 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005120 --curwin->w_cursor.col; // cursor on the last replaced char
5121 // if the character on the left of the current cursor is a multi-byte
5122 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 if (has_mbyte)
5124 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 curbuf->b_op_end = curwin->w_cursor;
5126 curwin->w_set_curswant = TRUE;
5127 set_last_insert(cap->nchar);
5128 }
5129}
5130
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131/*
5132 * 'o': Exchange start and end of Visual area.
5133 * 'O': same, but in block mode exchange left and right corners.
5134 */
5135 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005136v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137{
5138 pos_T old_cursor;
5139 colnr_T left, right;
5140
5141 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
5142 {
5143 old_cursor = curwin->w_cursor;
5144 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
5145 curwin->w_cursor.lnum = VIsual.lnum;
5146 coladvance(left);
5147 VIsual = curwin->w_cursor;
5148
5149 curwin->w_cursor.lnum = old_cursor.lnum;
5150 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005151 // 'selection "exclusive" and cursor at right-bottom corner: move it
5152 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
5154 ++curwin->w_curswant;
5155 coladvance(curwin->w_curswant);
5156 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005158 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 {
5160 curwin->w_cursor.lnum = VIsual.lnum;
5161 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5162 ++right;
5163 coladvance(right);
5164 VIsual = curwin->w_cursor;
5165
5166 curwin->w_cursor.lnum = old_cursor.lnum;
5167 coladvance(left);
5168 curwin->w_curswant = left;
5169 }
5170 }
5171 else
5172 {
5173 old_cursor = curwin->w_cursor;
5174 curwin->w_cursor = VIsual;
5175 VIsual = old_cursor;
5176 curwin->w_set_curswant = TRUE;
5177 }
5178}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179
5180/*
5181 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5182 */
5183 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005184nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005186 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 {
5188 cap->cmdchar = 'c';
5189 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005190 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 VIsual_mode = 'V';
5192 nv_operator(cap);
5193 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005194 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 {
5196 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005197 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 else
5199 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 if (virtual_active())
5201 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5203 }
5204 }
5205}
5206
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207/*
5208 * "gr".
5209 */
5210 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005211nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 if (VIsual_active)
5214 {
5215 cap->cmdchar = 'r';
5216 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005217 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005219 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 {
5221 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005222 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 else
5224 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005225 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005226 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 stuffcharReadbuff(cap->extra_char);
5228 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 if (virtual_active())
5230 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 invoke_edit(cap, TRUE, 'v', FALSE);
5232 }
5233 }
5234}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235
5236/*
5237 * Swap case for "~" command, when it does not work like an operator.
5238 */
5239 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005240n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241{
5242 long n;
5243 pos_T startpos;
5244 int did_change = 0;
5245#ifdef FEAT_NETBEANS_INTG
5246 pos_T pos;
5247 char_u *ptr;
5248 int count;
5249#endif
5250
5251 if (checkclearopq(cap->oap))
5252 return;
5253
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005254 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 {
5256 clearopbeep(cap->oap);
5257 return;
5258 }
5259
5260 prep_redo_cmd(cap);
5261
5262 if (u_save_cursor() == FAIL)
5263 return;
5264
5265 startpos = curwin->w_cursor;
5266#ifdef FEAT_NETBEANS_INTG
5267 pos = startpos;
5268#endif
5269 for (n = cap->count1; n > 0; --n)
5270 {
5271 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5272 inc_cursor();
5273 if (gchar_cursor() == NUL)
5274 {
5275 if (vim_strchr(p_ww, '~') != NULL
5276 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5277 {
5278#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005279 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 {
5281 if (did_change)
5282 {
5283 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005284 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005285 netbeans_removed(curbuf, pos.lnum, pos.col,
5286 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005288 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 }
5290 pos.col = 0;
5291 pos.lnum++;
5292 }
5293#endif
5294 ++curwin->w_cursor.lnum;
5295 curwin->w_cursor.col = 0;
5296 if (n > 1)
5297 {
5298 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5299 break;
5300 u_clearline();
5301 }
5302 }
5303 else
5304 break;
5305 }
5306 }
5307#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005308 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 {
5310 ptr = ml_get(pos.lnum);
5311 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005312 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5313 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 }
5315#endif
5316
5317
5318 check_cursor();
5319 curwin->w_set_curswant = TRUE;
5320 if (did_change)
5321 {
5322 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5323 0L);
5324 curbuf->b_op_start = startpos;
5325 curbuf->b_op_end = curwin->w_cursor;
5326 if (curbuf->b_op_end.col > 0)
5327 --curbuf->b_op_end.col;
5328 }
5329}
5330
5331/*
5332 * Move cursor to mark.
5333 */
5334 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005335nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336{
5337 if (check_mark(pos) == FAIL)
5338 clearop(cap->oap);
5339 else
5340 {
5341 if (cap->cmdchar == '\''
5342 || cap->cmdchar == '`'
5343 || cap->cmdchar == '['
5344 || cap->cmdchar == ']')
5345 setpcmark();
5346 curwin->w_cursor = *pos;
5347 if (flag)
5348 beginline(BL_WHITE | BL_FIX);
5349 else
5350 check_cursor();
5351 }
5352 cap->oap->motion_type = flag ? MLINE : MCHAR;
5353 if (cap->cmdchar == '`')
5354 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005355 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 curwin->w_set_curswant = TRUE;
5357}
5358
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359/*
5360 * Handle commands that are operators in Visual mode.
5361 */
5362 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005363v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364{
5365 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5366
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005367 // Uppercase means linewise, except in block mode, then "D" deletes till
5368 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 if (isupper(cap->cmdchar))
5370 {
5371 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005372 {
5373 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5377 curwin->w_curswant = MAXCOL;
5378 }
5379 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5380 nv_operator(cap);
5381}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382
5383/*
5384 * "s" and "S" commands.
5385 */
5386 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005387nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005389#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005390 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005391 if (term_swap_diff() == OK)
5392 return;
5393#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005394#ifdef FEAT_JOB_CHANNEL
5395 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5396 {
5397 clearopbeep(cap->oap);
5398 return;
5399 }
5400#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005401 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 {
5403 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005404 {
5405 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 cap->cmdchar = 'c';
5409 nv_operator(cap);
5410 }
5411 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 nv_optrans(cap);
5413}
5414
5415/*
5416 * Abbreviated commands.
5417 */
5418 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005419nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420{
5421 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005422 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005424 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 if (VIsual_active)
5426 v_visop(cap);
5427 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 nv_optrans(cap);
5429}
5430
5431/*
5432 * Translate a command into another command.
5433 */
5434 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005435nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436{
5437 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5438 (char_u *)"d$", (char_u *)"c$",
5439 (char_u *)"cl", (char_u *)"cc",
5440 (char_u *)"yy", (char_u *)":s\r"};
5441 static char_u *str = (char_u *)"xXDCsSY&";
5442
5443 if (!checkclearopq(cap->oap))
5444 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005445 // In Vi "2D" doesn't delete the next line. Can't translate it
5446 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005447 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5448 {
5449 cap->oap->start = curwin->w_cursor;
5450 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005451#ifdef FEAT_EVAL
5452 set_op_var(OP_DELETE);
5453#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005454 cap->count1 = 1;
5455 nv_dollar(cap);
5456 finish_op = TRUE;
5457 ResetRedobuff();
5458 AppendCharToRedobuff('D');
5459 }
5460 else
5461 {
5462 if (cap->count0)
5463 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005464 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 }
5467 cap->opcount = 0;
5468}
5469
5470/*
5471 * "'" and "`" commands. Also for "g'" and "g`".
5472 * cap->arg is TRUE for "'" and "g'".
5473 */
5474 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005475nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476{
5477 pos_T *pos;
5478 int c;
5479#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005480 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005481 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482#endif
5483
5484 if (cap->cmdchar == 'g')
5485 c = cap->extra_char;
5486 else
5487 c = cap->nchar;
5488 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005489 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 {
5491 if (cap->arg)
5492 {
5493 check_cursor_lnum();
5494 beginline(BL_WHITE | BL_FIX);
5495 }
5496 else
5497 check_cursor();
5498 }
5499 else
5500 nv_cursormark(cap, cap->arg, pos);
5501
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005502 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 if (!virtual_active())
5504 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005505 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506#ifdef FEAT_FOLDING
5507 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005508 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005509 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 && (fdo_flags & FDO_MARK)
5511 && old_KeyTyped)
5512 foldOpenCursor();
5513#endif
5514}
5515
5516/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005517 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 */
5519 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005520nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521{
5522#ifdef FEAT_JUMPLIST
5523 pos_T *pos;
5524# ifdef FEAT_FOLDING
5525 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005526 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527# endif
5528
5529 if (!checkclearopq(cap->oap))
5530 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005531 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5532 {
5533 if (goto_tabpage_lastused() == FAIL)
5534 clearopbeep(cap->oap);
5535 return;
5536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 if (cap->cmdchar == 'g')
5538 pos = movechangelist((int)cap->count1);
5539 else
5540 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005541 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 {
5543 curwin->w_set_curswant = TRUE;
5544 check_cursor();
5545 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005546 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 nv_cursormark(cap, FALSE, pos);
5548 else if (cap->cmdchar == 'g')
5549 {
5550 if (curbuf->b_changelistlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005551 emsg(_("E664: changelist is empty"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 else if (cap->count1 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005553 emsg(_("E662: At start of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005555 emsg(_("E663: At end of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 }
5557 else
5558 clearopbeep(cap->oap);
5559# ifdef FEAT_FOLDING
5560 if (cap->oap->op_type == OP_NOP
5561 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5562 && (fdo_flags & FDO_MARK)
5563 && old_KeyTyped)
5564 foldOpenCursor();
5565# endif
5566 }
5567#else
5568 clearopbeep(cap->oap);
5569#endif
5570}
5571
5572/*
5573 * Handle '"' command.
5574 */
5575 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005576nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
5578 if (checkclearop(cap->oap))
5579 return;
5580#ifdef FEAT_EVAL
5581 if (cap->nchar == '=')
5582 cap->nchar = get_expr_register();
5583#endif
5584 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5585 {
5586 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005587 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588#ifdef FEAT_EVAL
5589 set_reg_var(cap->oap->regname);
5590#endif
5591 }
5592 else
5593 clearopbeep(cap->oap);
5594}
5595
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596/*
5597 * Handle "v", "V" and "CTRL-V" commands.
5598 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5599 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005600 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 */
5602 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005603nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005605 if (cap->cmdchar == Ctrl_Q)
5606 cap->cmdchar = Ctrl_V;
5607
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005608 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5609 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 if (cap->oap->op_type != OP_NOP)
5611 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005612 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005613 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 return;
5615 }
5616
5617 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005618 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005620 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005622 else // toggle char/block mode
5623 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 VIsual_mode = cap->cmdchar;
5625 showmode();
5626 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005627 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005629 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 {
5631 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005632 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005634 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 VIsual = curwin->w_cursor;
5636
5637 VIsual_active = TRUE;
5638 VIsual_reselect = TRUE;
5639 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005640 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005643 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005644 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 /*
5646 * For V and ^V, we multiply the number of lines even if there
5647 * was only one -- webb
5648 */
5649 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5650 {
5651 curwin->w_cursor.lnum +=
5652 resel_VIsual_line_count * cap->count0 - 1;
5653 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5654 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5655 }
5656 VIsual_mode = resel_VIsual_mode;
5657 if (VIsual_mode == 'v')
5658 {
5659 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005660 {
5661 validate_virtcol();
5662 curwin->w_curswant = curwin->w_virtcol
5663 + resel_VIsual_vcol * cap->count0 - 1;
5664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005666 curwin->w_curswant = resel_VIsual_vcol;
5667 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005669 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 {
5671 curwin->w_curswant = MAXCOL;
5672 coladvance((colnr_T)MAXCOL);
5673 }
5674 else if (VIsual_mode == Ctrl_V)
5675 {
5676 validate_virtcol();
5677 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005678 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 coladvance(curwin->w_curswant);
5680 }
5681 else
5682 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005683 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 }
5685 else
5686 {
5687 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005688 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 may_start_select('c');
5690 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005691 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005692 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005693 if (cap->count0 > 0 && --cap->count1 > 0)
5694 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005695 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005696 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5697 nv_right(cap);
5698 else if (VIsual_mode == 'V')
5699 nv_down(cap);
5700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 }
5702 }
5703}
5704
5705/*
5706 * Start selection for Shift-movement keys.
5707 */
5708 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005709start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005711 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 may_start_select('k');
5713 n_start_visual_mode('v');
5714}
5715
5716/*
5717 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
5718 */
5719 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005720may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721{
5722 VIsual_select = (stuff_empty() && typebuf_typed()
5723 && (vim_strchr(p_slm, c) != NULL));
5724}
5725
5726/*
5727 * Start Visual mode "c".
5728 * Should set VIsual_select before calling this.
5729 */
5730 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005731n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005733#ifdef FEAT_CONCEAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005734 // Check for redraw before changing the state.
Bram Moolenaarb9464822018-05-10 15:09:49 +02005735 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005736#endif
5737
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 VIsual_mode = c;
5739 VIsual_active = TRUE;
5740 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005741
5742 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005743 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005745 {
5746 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 VIsual = curwin->w_cursor;
5750
5751#ifdef FEAT_FOLDING
5752 foldAdjustVisual();
5753#endif
5754
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005756#ifdef FEAT_CONCEAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005757 // Check for redraw after changing the state.
Bram Moolenaarb9464822018-05-10 15:09:49 +02005758 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005759#endif
5760
Bram Moolenaar09df3122006-01-23 22:23:09 +00005761 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005762 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005764 // Make sure the clipboard gets updated. Needed because start and
5765 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 clip_star.vmode = NUL;
5767#endif
5768
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005769 // Only need to redraw this line, unless still need to redraw an old
5770 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 if (curwin->w_redr_type < INVERTED)
5772 {
5773 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5774 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5775 }
5776}
5777
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778
5779/*
5780 * CTRL-W: Window commands
5781 */
5782 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005783nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005785 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005786 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005787 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005788 cap->cmdchar = ':';
5789 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005790 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005791 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005792 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005793 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794}
5795
5796/*
5797 * CTRL-Z: Suspend
5798 */
5799 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005800nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801{
5802 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005804 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005805 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806}
5807
5808/*
5809 * Commands starting with "g".
5810 */
5811 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005812nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813{
5814 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 int i;
5817 int flag = FALSE;
5818
5819 switch (cap->nchar)
5820 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005821 case Ctrl_A:
5822 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823#ifdef MEM_PROFILE
5824 /*
5825 * "g^A": dump log of used memory.
5826 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005827 if (!VIsual_active && cap->nchar == Ctrl_A)
5828 vim_mem_profile_dump();
5829 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005831 /*
5832 * "g^A/g^X": sequentially increment visually selected region
5833 */
5834 if (VIsual_active)
5835 {
5836 cap->arg = TRUE;
5837 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005838 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005839 nv_addsub(cap);
5840 }
5841 else
5842 clearopbeep(oap);
5843 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 /*
5846 * "gR": Enter virtual replace mode.
5847 */
5848 case 'R':
5849 cap->arg = TRUE;
5850 nv_Replace(cap);
5851 break;
5852
5853 case 'r':
5854 nv_vreplace(cap);
5855 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856
5857 case '&':
5858 do_cmdline_cmd((char_u *)"%s//~/&");
5859 break;
5860
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 /*
5862 * "gv": Reselect the previous Visual area. If Visual already active,
5863 * exchange previous and current Visual area.
5864 */
5865 case 'v':
5866 if (checkclearop(oap))
5867 break;
5868
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005869 if ( curbuf->b_visual.vi_start.lnum == 0
5870 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5871 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 beep_flush();
5873 else
5874 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005875 // set w_cursor to the start of the Visual area, tpos to the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 if (VIsual_active)
5877 {
5878 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005879 VIsual_mode = curbuf->b_visual.vi_mode;
5880 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881# ifdef FEAT_EVAL
5882 curbuf->b_visual_mode_eval = i;
5883# endif
5884 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005885 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5886 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005888 tpos = curbuf->b_visual.vi_end;
5889 curbuf->b_visual.vi_end = curwin->w_cursor;
5890 curwin->w_cursor = curbuf->b_visual.vi_start;
5891 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 }
5893 else
5894 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005895 VIsual_mode = curbuf->b_visual.vi_mode;
5896 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5897 tpos = curbuf->b_visual.vi_end;
5898 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 }
5900
5901 VIsual_active = TRUE;
5902 VIsual_reselect = TRUE;
5903
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005904 // Set Visual to the start and w_cursor to the end of the Visual
5905 // area. Make sure they are on an existing character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 check_cursor();
5907 VIsual = curwin->w_cursor;
5908 curwin->w_cursor = tpos;
5909 check_cursor();
5910 update_topline();
5911 /*
5912 * When called from normal "g" command: start Select mode when
5913 * 'selectmode' contains "cmd". When called for K_SELECT, always
5914 * start Select mode.
5915 */
5916 if (cap->arg)
5917 VIsual_select = TRUE;
5918 else
5919 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005922 // Make sure the clipboard gets updated. Needed because start and
5923 // end are still the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 clip_star.vmode = NUL;
5925#endif
5926 redraw_curbuf_later(INVERTED);
5927 showmode();
5928 }
5929 break;
5930 /*
5931 * "gV": Don't reselect the previous Visual area after a Select mode
5932 * mapping of menu.
5933 */
5934 case 'V':
5935 VIsual_reselect = FALSE;
5936 break;
5937
5938 /*
5939 * "gh": start Select mode.
5940 * "gH": start Select line mode.
5941 * "g^H": start Select block mode.
5942 */
5943 case K_BS:
5944 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005945 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 case 'h':
5947 case 'H':
5948 case Ctrl_H:
5949# ifdef EBCDIC
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005950 // EBCDIC: 'v'-'h' != '^v'-'^h'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 if (cap->nchar == Ctrl_H)
5952 cap->cmdchar = Ctrl_V;
5953 else
5954# endif
5955 cap->cmdchar = cap->nchar + ('v' - 'h');
5956 cap->arg = TRUE;
5957 nv_visual(cap);
5958 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005959
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005960 // "gn", "gN" visually select next/previous search match
5961 // "gn" selects next match
5962 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005963 case 'N':
5964 case 'n':
5965 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005966 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005967 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968
5969 /*
5970 * "gj" and "gk" two new funny movement keys -- up and down
5971 * movement based on *screen* line rather than *file* line.
5972 */
5973 case 'j':
5974 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005975 // with 'nowrap' it works just like the normal "j" command.
5976 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 {
5978 oap->motion_type = MLINE;
5979 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5980 }
5981 else
5982 i = nv_screengo(oap, FORWARD, cap->count1);
5983 if (i == FAIL)
5984 clearopbeep(oap);
5985 break;
5986
5987 case 'k':
5988 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005989 // with 'nowrap' it works just like the normal "k" command.
5990 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 {
5992 oap->motion_type = MLINE;
5993 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5994 }
5995 else
5996 i = nv_screengo(oap, BACKWARD, cap->count1);
5997 if (i == FAIL)
5998 clearopbeep(oap);
5999 break;
6000
6001 /*
6002 * "gJ": join two lines without inserting a space.
6003 */
6004 case 'J':
6005 nv_join(cap);
6006 break;
6007
6008 /*
6009 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
6010 * "gm": middle of "g0" and "g$".
6011 */
6012 case '^':
6013 flag = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006014 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015
6016 case '0':
6017 case 'm':
6018 case K_HOME:
6019 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 oap->motion_type = MCHAR;
6021 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006022 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006024 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 int width2 = width1 + curwin_col_off2();
6026
6027 validate_virtcol();
6028 i = 0;
6029 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
6030 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
6031 }
6032 else
6033 i = curwin->w_leftcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006034 // Go to the middle of the screen line. When 'number' or
6035 // 'relativenumber' is on and lines are wrapping the middle can be more
6036 // to the left.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02006038 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 + ((curwin->w_p_wrap && i > 0)
6040 ? curwin_col_off2() : 0)) / 2;
6041 coladvance((colnr_T)i);
6042 if (flag)
6043 {
6044 do
6045 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006046 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01006047 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 }
6049 curwin->w_set_curswant = TRUE;
6050 break;
6051
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006052 case 'M':
6053 {
6054 char_u *ptr = ml_get_curline();
6055
6056 oap->motion_type = MCHAR;
6057 oap->inclusive = FALSE;
6058 if (has_mbyte)
Bram Moolenaar69bf6342019-10-29 04:16:57 +01006059 i = mb_string2cells(ptr, (int)STRLEN(ptr));
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006060 else
6061 i = (int)STRLEN(ptr);
6062 if (cap->count0 > 0 && cap->count0 <= 100)
6063 coladvance((colnr_T)(i * cap->count0 / 100));
6064 else
6065 coladvance((colnr_T)(i / 2));
6066 curwin->w_set_curswant = TRUE;
6067 }
6068 break;
6069
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 case '_':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006071 // "g_": to the last non-blank character in the line or <count> lines
6072 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 cap->oap->motion_type = MCHAR;
6074 cap->oap->inclusive = TRUE;
6075 curwin->w_curswant = MAXCOL;
6076 if (cursor_down((long)(cap->count1 - 1),
6077 cap->oap->op_type == OP_NOP) == FAIL)
6078 clearopbeep(cap->oap);
6079 else
6080 {
6081 char_u *ptr = ml_get_curline();
6082
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006083 // In Visual mode we may end up after the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
6085 --curwin->w_cursor.col;
6086
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006087 // Decrease the cursor column until it's on a non-blank.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01006089 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 --curwin->w_cursor.col;
6091 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01006092 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 }
6094 break;
6095
6096 case '$':
6097 case K_END:
6098 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 {
6100 int col_off = curwin_col_off();
6101
6102 oap->motion_type = MCHAR;
6103 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006104 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006106 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 if (cap->count1 == 1)
6108 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006109 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 int width2 = width1 + curwin_col_off2();
6111
6112 validate_virtcol();
6113 i = width1 - 1;
6114 if (curwin->w_virtcol >= (colnr_T)width1)
6115 i += ((curwin->w_virtcol - width1) / width2 + 1)
6116 * width2;
6117 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006118
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006119 // Make sure we stick in this column.
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006120 validate_virtcol();
6121 curwin->w_curswant = curwin->w_virtcol;
6122 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
6124 {
6125 /*
6126 * Check for landing on a character that got split at
6127 * the end of the line. We do not want to advance to
6128 * the next screen line.
6129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 if (curwin->w_virtcol > (colnr_T)i)
6131 --curwin->w_cursor.col;
6132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 }
6134 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
6135 clearopbeep(oap);
6136 }
6137 else
6138 {
Bram Moolenaard5c82342019-07-27 18:44:57 +02006139 if (cap->count1 > 1)
6140 // if it fails, let the cursor still move to the last char
Bram Moolenaar9c272a92019-08-16 21:54:27 +02006141 (void)cursor_down(cap->count1 - 1, FALSE);
Bram Moolenaard5c82342019-07-27 18:44:57 +02006142
Bram Moolenaar02631462017-09-22 15:20:32 +02006143 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006145
Bram Moolenaard5c82342019-07-27 18:44:57 +02006146 // Make sure we stick in this column.
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006147 validate_virtcol();
6148 curwin->w_curswant = curwin->w_virtcol;
6149 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 }
6151 }
6152 break;
6153
6154 /*
6155 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
6156 */
6157 case '*':
6158 case '#':
6159#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006160 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006162 case Ctrl_RSB: // :tag or :tselect for current identifier
6163 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 nv_ident(cap);
6165 break;
6166
6167 /*
6168 * ge and gE: go back to end of word
6169 */
6170 case 'e':
6171 case 'E':
6172 oap->motion_type = MCHAR;
6173 curwin->w_set_curswant = TRUE;
6174 oap->inclusive = TRUE;
6175 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6176 clearopbeep(oap);
6177 break;
6178
6179 /*
6180 * "g CTRL-G": display info about cursor position
6181 */
6182 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006183 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 break;
6185
6186 /*
6187 * "gi": start Insert at the last position.
6188 */
6189 case 'i':
6190 if (curbuf->b_last_insert.lnum != 0)
6191 {
6192 curwin->w_cursor = curbuf->b_last_insert;
6193 check_cursor_lnum();
6194 i = (int)STRLEN(ml_get_curline());
6195 if (curwin->w_cursor.col > (colnr_T)i)
6196 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 if (virtual_active())
6198 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 curwin->w_cursor.col = i;
6200 }
6201 }
6202 cap->cmdchar = 'i';
6203 nv_edit(cap);
6204 break;
6205
6206 /*
6207 * "gI": Start insert in column 1.
6208 */
6209 case 'I':
6210 beginline(0);
6211 if (!checkclearopq(oap))
6212 invoke_edit(cap, FALSE, 'g', FALSE);
6213 break;
6214
6215#ifdef FEAT_SEARCHPATH
6216 /*
6217 * "gf": goto file, edit file under cursor
6218 * "]f" and "[f": can also be used.
6219 */
6220 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006221 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 nv_gotofile(cap);
6223 break;
6224#endif
6225
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006226 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 case '\'':
6228 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006229 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 case '`':
6231 nv_gomark(cap);
6232 break;
6233
6234 /*
6235 * "gs": Goto sleep.
6236 */
6237 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006238 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 break;
6240
6241 /*
6242 * "ga": Display the ascii value of the character under the
6243 * cursor. It is displayed in decimal, hex, and octal. -- webb
6244 */
6245 case 'a':
6246 do_ascii(NULL);
6247 break;
6248
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 /*
6250 * "g8": Display the bytes used for the UTF-8 character under the
6251 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006252 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 */
6254 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006255 if (cap->count0 == 8)
6256 utf_find_illegal();
6257 else
6258 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006261 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006262 case '<':
6263 show_sb_text();
6264 break;
6265
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 /*
6267 * "gg": Goto the first line in file. With a count it goes to
6268 * that line number like for "G". -- webb
6269 */
6270 case 'g':
6271 cap->arg = FALSE;
6272 nv_goto(cap);
6273 break;
6274
6275 /*
6276 * Two-character operators:
6277 * "gq" Format text
6278 * "gw" Format text and keep cursor position
6279 * "g~" Toggle the case of the text.
6280 * "gu" Change text to lower case.
6281 * "gU" Change text to upper case.
6282 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006283 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 */
6285 case 'q':
6286 case 'w':
6287 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006288 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 case '~':
6290 case 'u':
6291 case 'U':
6292 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006293 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 nv_operator(cap);
6295 break;
6296
6297 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006298 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 * current function
6300 * "gD": idem, but in the current file.
6301 */
6302 case 'd':
6303 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006304 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 break;
6306
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 /*
6308 * g<*Mouse> : <C-*mouse>
6309 */
6310 case K_MIDDLEMOUSE:
6311 case K_MIDDLEDRAG:
6312 case K_MIDDLERELEASE:
6313 case K_LEFTMOUSE:
6314 case K_LEFTDRAG:
6315 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006316 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 case K_RIGHTMOUSE:
6318 case K_RIGHTDRAG:
6319 case K_RIGHTRELEASE:
6320 case K_X1MOUSE:
6321 case K_X1DRAG:
6322 case K_X1RELEASE:
6323 case K_X2MOUSE:
6324 case K_X2DRAG:
6325 case K_X2RELEASE:
6326 mod_mask = MOD_MASK_CTRL;
6327 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6328 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329
6330 case K_IGNORE:
6331 break;
6332
6333 /*
6334 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
6335 */
6336 case 'p':
6337 case 'P':
6338 nv_put(cap);
6339 break;
6340
6341#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006342 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 case 'o':
6344 goto_byte(cap->count0);
6345 break;
6346#endif
6347
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006348 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006350 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 {
6352 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006353 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 break;
6355 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006356
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 if (!checkclearopq(oap))
6358 do_exmode(TRUE);
6359 break;
6360
6361#ifdef FEAT_JUMPLIST
6362 case ',':
6363 nv_pcmark(cap);
6364 break;
6365
6366 case ';':
6367 cap->count1 = -cap->count1;
6368 nv_pcmark(cap);
6369 break;
6370#endif
6371
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006372 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006373 if (!checkclearop(oap))
6374 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006375 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006376 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006377 if (!checkclearop(oap))
6378 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006379 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006380
Bram Moolenaar62a23252020-08-09 14:04:42 +02006381 case TAB:
6382 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6383 clearopbeep(oap);
6384 break;
6385
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006386 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006387 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006388 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006389 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006390 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006391 break;
6392
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 default:
6394 clearopbeep(oap);
6395 break;
6396 }
6397}
6398
6399/*
6400 * Handle "o" and "O" commands.
6401 */
6402 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006403n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006405#ifdef FEAT_CONCEAL
6406 linenr_T oldline = curwin->w_cursor.lnum;
6407#endif
6408
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 if (!checkclearopq(cap->oap))
6410 {
6411#ifdef FEAT_FOLDING
6412 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006413 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 (void)hasFolding(curwin->w_cursor.lnum,
6415 &curwin->w_cursor.lnum, NULL);
6416 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006417 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 (void)hasFolding(curwin->w_cursor.lnum,
6419 NULL, &curwin->w_cursor.lnum);
6420#endif
6421 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6422 (cap->cmdchar == 'O' ? 1 : 0)),
6423 (linenr_T)(curwin->w_cursor.lnum +
6424 (cap->cmdchar == 'o' ? 1 : 0))
6425 ) == OK
6426 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006427 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6428 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006430#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006431 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006432 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006433#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006434#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006435 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006436 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006437 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006438#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006439 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006440 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6441 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6443 }
6444 }
6445}
6446
6447/*
6448 * "." command: redo last change.
6449 */
6450 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006451nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452{
6453 if (!checkclearopq(cap->oap))
6454 {
6455 /*
6456 * If "restart_edit" is TRUE, the last but one command is repeated
6457 * instead of the last command (inserting text). This is used for
6458 * CTRL-O <.> in insert mode.
6459 */
6460 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6461 clearopbeep(cap->oap);
6462 }
6463}
6464
6465/*
6466 * CTRL-R: undo undo
6467 */
6468 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006469nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470{
6471 if (!checkclearopq(cap->oap))
6472 {
6473 u_redo((int)cap->count1);
6474 curwin->w_set_curswant = TRUE;
6475 }
6476}
6477
6478/*
6479 * Handle "U" command.
6480 */
6481 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006482nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006484 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006485 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006487 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 cap->cmdchar = 'g';
6489 cap->nchar = 'U';
6490 nv_operator(cap);
6491 }
6492 else if (!checkclearopq(cap->oap))
6493 {
6494 u_undoline();
6495 curwin->w_set_curswant = TRUE;
6496 }
6497}
6498
6499/*
6500 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6501 * single character.
6502 */
6503 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006504nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006506 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006507 {
6508#ifdef FEAT_JOB_CHANNEL
6509 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6510 {
6511 clearopbeep(cap->oap);
6512 return;
6513 }
6514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 else
6518 nv_operator(cap);
6519}
6520
6521/*
6522 * Handle an operator command.
6523 * The actual work is done by do_pending_operator().
6524 */
6525 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006526nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527{
6528 int op_type;
6529
6530 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006531#ifdef FEAT_JOB_CHANNEL
6532 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6533 {
6534 clearopbeep(cap->oap);
6535 return;
6536 }
6537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006539 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 nv_lineop(cap);
6541 else if (!checkclearop(cap->oap))
6542 {
6543 cap->oap->start = curwin->w_cursor;
6544 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006545#ifdef FEAT_EVAL
6546 set_op_var(op_type);
6547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 }
6549}
6550
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006551#ifdef FEAT_EVAL
6552/*
6553 * Set v:operator to the characters for "optype".
6554 */
6555 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006556set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006557{
6558 char_u opchars[3];
6559
6560 if (optype == OP_NOP)
6561 set_vim_var_string(VV_OP, NULL, 0);
6562 else
6563 {
6564 opchars[0] = get_op_char(optype);
6565 opchars[1] = get_extra_op_char(optype);
6566 opchars[2] = NUL;
6567 set_vim_var_string(VV_OP, opchars, -1);
6568 }
6569}
6570#endif
6571
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572/*
6573 * Handle linewise operator "dd", "yy", etc.
6574 *
6575 * "_" is is a strange motion command that helps make operators more logical.
6576 * It is actually implemented, but not documented in the real Vi. This motion
6577 * command actually refers to "the current line". Commands like "dd" and "yy"
6578 * are really an alternate form of "d_" and "y_". It does accept a count, so
6579 * "d3_" works to delete 3 lines.
6580 */
6581 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006582nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583{
6584 cap->oap->motion_type = MLINE;
6585 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6586 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006587 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006588 && cap->oap->motion_force != 'v'
6589 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 || cap->oap->op_type == OP_LSHIFT
6591 || cap->oap->op_type == OP_RSHIFT)
6592 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006593 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 beginline(BL_WHITE | BL_FIX);
6595}
6596
6597/*
6598 * <Home> command.
6599 */
6600 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006601nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006603 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006604 if (mod_mask & MOD_MASK_CTRL)
6605 nv_goto(cap);
6606 else
6607 {
6608 cap->count0 = 1;
6609 nv_pipe(cap);
6610 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006611 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6612 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613}
6614
6615/*
6616 * "|" command.
6617 */
6618 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006619nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620{
6621 cap->oap->motion_type = MCHAR;
6622 cap->oap->inclusive = FALSE;
6623 beginline(0);
6624 if (cap->count0 > 0)
6625 {
6626 coladvance((colnr_T)(cap->count0 - 1));
6627 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6628 }
6629 else
6630 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006631 // keep curswant at the column where we wanted to go, not where
6632 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 curwin->w_set_curswant = FALSE;
6634}
6635
6636/*
6637 * Handle back-word command "b" and "B".
6638 * cap->arg is 1 for "B"
6639 */
6640 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006641nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642{
6643 cap->oap->motion_type = MCHAR;
6644 cap->oap->inclusive = FALSE;
6645 curwin->w_set_curswant = TRUE;
6646 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6647 clearopbeep(cap->oap);
6648#ifdef FEAT_FOLDING
6649 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6650 foldOpenCursor();
6651#endif
6652}
6653
6654/*
6655 * Handle word motion commands "e", "E", "w" and "W".
6656 * cap->arg is TRUE for "E" and "W".
6657 */
6658 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006659nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660{
6661 int n;
6662 int word_end;
6663 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006664 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665
6666 /*
6667 * Set inclusive for the "E" and "e" command.
6668 */
6669 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6670 word_end = TRUE;
6671 else
6672 word_end = FALSE;
6673 cap->oap->inclusive = word_end;
6674
6675 /*
6676 * "cw" and "cW" are a special case.
6677 */
6678 if (!word_end && cap->oap->op_type == OP_CHANGE)
6679 {
6680 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006681 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006683 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 {
6685 /*
6686 * Reproduce a funny Vi behaviour: "cw" on a blank only
6687 * changes one character, not all blanks until the start of
6688 * the next word. Only do this when the 'w' flag is included
6689 * in 'cpoptions'.
6690 */
6691 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6692 {
6693 cap->oap->inclusive = TRUE;
6694 cap->oap->motion_type = MCHAR;
6695 return;
6696 }
6697 }
6698 else
6699 {
6700 /*
6701 * This is a little strange. To match what the real Vi does,
6702 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6703 * that we are not on a space or a TAB. This seems impolite
6704 * at first, but it's really more what we mean when we say
6705 * 'cw'.
6706 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006707 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 * will change only one character! This is done by setting
6709 * flag.
6710 */
6711 cap->oap->inclusive = TRUE;
6712 word_end = TRUE;
6713 flag = TRUE;
6714 }
6715 }
6716 }
6717
6718 cap->oap->motion_type = MCHAR;
6719 curwin->w_set_curswant = TRUE;
6720 if (word_end)
6721 n = end_word(cap->count1, cap->arg, flag, FALSE);
6722 else
6723 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6724
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006725 // Don't leave the cursor on the NUL past the end of line. Unless we
6726 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006727 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006728 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729
6730 if (n == FAIL && cap->oap->op_type == OP_NOP)
6731 clearopbeep(cap->oap);
6732 else
6733 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735#ifdef FEAT_FOLDING
6736 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6737 foldOpenCursor();
6738#endif
6739 }
6740}
6741
6742/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006743 * Used after a movement command: If the cursor ends up on the NUL after the
6744 * end of the line, may move it back to the last character and make the motion
6745 * inclusive.
6746 */
6747 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006748adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006749{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006750 // The cursor cannot remain on the NUL when:
6751 // - the column is > 0
6752 // - not in Visual mode or 'selection' is "o"
6753 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006754 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006755 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006756 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006757 {
6758 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006759 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006760 if (has_mbyte)
6761 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006762 oap->inclusive = TRUE;
6763 }
6764}
6765
6766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 * "0" and "^" commands.
6768 * cap->arg is the argument for beginline().
6769 */
6770 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006771nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772{
6773 cap->oap->motion_type = MCHAR;
6774 cap->oap->inclusive = FALSE;
6775 beginline(cap->arg);
6776#ifdef FEAT_FOLDING
6777 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6778 foldOpenCursor();
6779#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006780 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6781 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782}
6783
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784/*
6785 * In exclusive Visual mode, may include the last character.
6786 */
6787 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006788adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789{
6790 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006791 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 if (has_mbyte)
6794 inc_cursor();
6795 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 ++curwin->w_cursor.col;
6797 cap->oap->inclusive = FALSE;
6798 }
6799}
6800
6801/*
6802 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6803 * Should check VIsual_mode before calling this.
6804 * Returns TRUE when backed up to the previous line.
6805 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006806 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006807unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808{
6809 pos_T *pp;
6810
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006811 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006813 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 pp = &curwin->w_cursor;
6815 else
6816 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 if (pp->coladd > 0)
6818 --pp->coladd;
6819 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 if (pp->col > 0)
6821 {
6822 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006823 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 }
6825 else if (pp->lnum > 1)
6826 {
6827 --pp->lnum;
6828 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6829 return TRUE;
6830 }
6831 }
6832 return FALSE;
6833}
6834
6835/*
6836 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6837 */
6838 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006839nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840{
6841 if (VIsual_active)
6842 VIsual_select = TRUE;
6843 else if (VIsual_reselect)
6844 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006845 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 cap->arg = TRUE;
6847 nv_g_cmd(cap);
6848 }
6849}
6850
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851
6852/*
6853 * "G", "gg", CTRL-END, CTRL-HOME.
6854 * cap->arg is TRUE for "G".
6855 */
6856 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006857nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858{
6859 linenr_T lnum;
6860
6861 if (cap->arg)
6862 lnum = curbuf->b_ml.ml_line_count;
6863 else
6864 lnum = 1L;
6865 cap->oap->motion_type = MLINE;
6866 setpcmark();
6867
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006868 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 if (cap->count0 != 0)
6870 lnum = cap->count0;
6871 if (lnum < 1L)
6872 lnum = 1L;
6873 else if (lnum > curbuf->b_ml.ml_line_count)
6874 lnum = curbuf->b_ml.ml_line_count;
6875 curwin->w_cursor.lnum = lnum;
6876 beginline(BL_SOL | BL_FIX);
6877#ifdef FEAT_FOLDING
6878 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6879 foldOpenCursor();
6880#endif
6881}
6882
6883/*
6884 * CTRL-\ in Normal mode.
6885 */
6886 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006887nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888{
6889 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6890 {
6891 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006892 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006893 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 restart_edit = 0;
6895#ifdef FEAT_CMDWIN
6896 if (cmdwin_type != 0)
6897 cmdwin_result = Ctrl_C;
6898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 if (VIsual_active)
6900 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006901 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 redraw_curbuf_later(INVERTED);
6903 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006904 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 if (cap->nchar == Ctrl_G && p_im)
6906 restart_edit = 'a';
6907 }
6908 else
6909 clearopbeep(cap->oap);
6910}
6911
6912/*
6913 * ESC in Normal mode: beep, but don't flush buffers.
6914 * Don't even beep if we are canceling a command.
6915 */
6916 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006917nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918{
6919 int no_reason;
6920
6921 no_reason = (cap->oap->op_type == OP_NOP
6922 && cap->opcount == 0
6923 && cap->count0 == 0
6924 && cap->oap->regname == 0
6925 && !p_im);
6926
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006927 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 {
6929 if (restart_edit == 0
6930#ifdef FEAT_CMDWIN
6931 && cmdwin_type == 0
6932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006935 {
6936 if (anyBufIsChanged())
6937 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6938 else
6939 msg(_("Type :qa and press <Enter> to exit Vim"));
6940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006942 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6943 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 if (!p_im)
6945 restart_edit = 0;
6946#ifdef FEAT_CMDWIN
6947 if (cmdwin_type != 0)
6948 {
6949 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006950 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 return;
6952 }
6953#endif
6954 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01006955#ifdef FEAT_CMDWIN
6956 else if (cmdwin_type != 0 && ex_normal_busy)
6957 {
6958 // When :normal runs out of characters while in the command line window
6959 // vgetorpeek() will return ESC. Exit the cmdline window to break the
6960 // loop.
6961 cmdwin_result = K_IGNORE;
6962 return;
6963 }
6964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 if (VIsual_active)
6967 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006968 end_visual_mode(); // stop Visual
6969 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 curwin->w_set_curswant = TRUE;
6971 redraw_curbuf_later(INVERTED);
6972 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006973 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02006974 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 clearop(cap->oap);
6976
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006977 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6978 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006979 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 restart_edit = 'a';
6981}
6982
6983/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006984 * Move the cursor for the "A" command.
6985 */
6986 void
6987set_cursor_for_append_to_line(void)
6988{
6989 curwin->w_set_curswant = TRUE;
6990 if (ve_flags == VE_ALL)
6991 {
6992 int save_State = State;
6993
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006994 // Pretend Insert mode here to allow the cursor on the
6995 // character past the end of the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006996 State = INSERT;
6997 coladvance((colnr_T)MAXCOL);
6998 State = save_State;
6999 }
7000 else
7001 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
7002}
7003
7004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01007006 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 */
7008 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007009nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007011 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
7013 cap->cmdchar = 'i';
7014
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007015 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02007017 {
7018#ifdef FEAT_TERMINAL
7019 if (term_in_normal_mode())
7020 {
7021 end_visual_mode();
7022 clearop(cap->oap);
7023 term_enter_job_mode();
7024 return;
7025 }
7026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02007028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007030 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007031 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
7032 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 {
7034#ifdef FEAT_TEXTOBJ
7035 nv_object(cap);
7036#else
7037 clearopbeep(cap->oap);
7038#endif
7039 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02007040#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02007041 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02007042 {
7043 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02007044 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02007045 return;
7046 }
7047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 else if (!curbuf->b_p_ma && !p_im)
7049 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007050 // Only give this error when 'insertmode' is off.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007051 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01007053 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007054 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007055 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007057 else if (cap->cmdchar == K_PS && VIsual_active)
7058 {
7059 pos_T old_pos = curwin->w_cursor;
7060 pos_T old_visual = VIsual;
7061
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007062 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01007063 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
7064 {
7065 shift_delete_registers();
7066 cap->oap->regname = '1';
7067 }
7068 else
7069 cap->oap->regname = '-';
7070 cap->cmdchar = 'd';
7071 cap->nchar = NUL;
7072 nv_operator(cap);
7073 do_pending_operator(cap, 0, FALSE);
7074 cap->cmdchar = K_PS;
7075
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007076 // When the last char in the line was deleted then append. Detect this
7077 // by checking if the cursor moved to before the Visual area.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007078 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
7079 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01007080 inc_cursor();
7081
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007082 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01007083 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 else if (!checkclearopq(cap->oap))
7086 {
7087 switch (cap->cmdchar)
7088 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007089 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007090 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 break;
7092
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007093 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007094 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7095 beginline(BL_WHITE);
7096 else
7097 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 break;
7099
Bram Moolenaara1891842017-02-04 21:34:31 +01007100 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007101 // Bracketed paste works like "a"ppend, unless the cursor is in
7102 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007103 if (curwin->w_cursor.col == 0)
7104 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007105 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007106
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007107 case 'a': // "a"ppend is like "i"nsert on the next character.
7108 // increment coladd when in virtual space, increment the
7109 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 if (virtual_active()
7111 && (curwin->w_cursor.coladd > 0
7112 || *ml_get_cursor() == NUL
7113 || *ml_get_cursor() == TAB))
7114 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007115 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 inc_cursor();
7117 break;
7118 }
7119
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7121 {
7122 int save_State = State;
7123
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007124 // Pretend Insert mode here to allow the cursor on the
7125 // character past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 State = INSERT;
7127 coladvance(getviscol());
7128 State = save_State;
7129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130
7131 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7132 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007133 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007134 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007135 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136}
7137
7138/*
7139 * Invoke edit() and take care of "restart_edit" and the return value.
7140 */
7141 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007142invoke_edit(
7143 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007144 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007145 int cmd,
7146 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147{
7148 int restart_edit_save = 0;
7149
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007150 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7151 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7152 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 if (repl || !stuff_empty())
7154 restart_edit_save = restart_edit;
7155 else
7156 restart_edit_save = 0;
7157
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007158 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 restart_edit = 0;
7160
7161 if (edit(cmd, startln, cap->count1))
7162 cap->retval |= CA_COMMAND_BUSY;
7163
7164 if (restart_edit == 0)
7165 restart_edit = restart_edit_save;
7166}
7167
7168#ifdef FEAT_TEXTOBJ
7169/*
7170 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7171 */
7172 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007173nv_object(
7174 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175{
7176 int flag;
7177 int include;
7178 char_u *mps_save;
7179
7180 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007181 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007183 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007185 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 mps_save = curbuf->b_p_mps;
7187 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7188
7189 switch (cap->nchar)
7190 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007191 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 flag = current_word(cap->oap, cap->count1, include, FALSE);
7193 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007194 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 flag = current_word(cap->oap, cap->count1, include, TRUE);
7196 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007197 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 case '(':
7199 case ')':
7200 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7201 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007202 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 case '{':
7204 case '}':
7205 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7206 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007207 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 case ']':
7209 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7210 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007211 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 case '>':
7213 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7214 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007215 case 't': // "at" = a tag block (xml and html)
7216 // Do not adjust oap->end in do_pending_operator()
7217 // otherwise there are different results for 'dit'
7218 // (note leading whitespace in last line):
7219 // 1) <b> 2) <b>
7220 // foobar foobar
7221 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007222 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007223 flag = current_tagblock(cap->oap, cap->count1, include);
7224 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007225 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 flag = current_par(cap->oap, cap->count1, include, 'p');
7227 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007228 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 flag = current_sent(cap->oap, cap->count1, include);
7230 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007231 case '"': // "a"" = a double quoted string
7232 case '\'': // "a'" = a single quoted string
7233 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007234 flag = current_quote(cap->oap, cap->count1, include,
7235 cap->nchar);
7236 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007237#if 0 // TODO
7238 case 'S': // "aS" = a section
7239 case 'f': // "af" = a filename
7240 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241#endif
7242 default:
7243 flag = FAIL;
7244 break;
7245 }
7246
7247 curbuf->b_p_mps = mps_save;
7248 if (flag == FAIL)
7249 clearopbeep(cap->oap);
7250 adjust_cursor_col();
7251 curwin->w_set_curswant = TRUE;
7252}
7253#endif
7254
7255/*
7256 * "q" command: Start/stop recording.
7257 * "q:", "q/", "q?": edit command-line in command-line window.
7258 */
7259 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007260nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261{
7262 if (cap->oap->op_type == OP_FORMAT)
7263 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007264 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 cap->cmdchar = 'g';
7266 cap->nchar = 'q';
7267 nv_operator(cap);
7268 }
7269 else if (!checkclearop(cap->oap))
7270 {
7271#ifdef FEAT_CMDWIN
7272 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7273 {
7274 stuffcharReadbuff(cap->nchar);
7275 stuffcharReadbuff(K_CMDWIN);
7276 }
7277 else
7278#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007279 // (stop) recording into a named register, unless executing a
7280 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007281 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 clearopbeep(cap->oap);
7283 }
7284}
7285
7286/*
7287 * Handle the "@r" command.
7288 */
7289 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007290nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291{
7292 if (checkclearop(cap->oap))
7293 return;
7294#ifdef FEAT_EVAL
7295 if (cap->nchar == '=')
7296 {
7297 if (get_expr_register() == NUL)
7298 return;
7299 }
7300#endif
7301 while (cap->count1-- && !got_int)
7302 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007303 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 {
7305 clearopbeep(cap->oap);
7306 break;
7307 }
7308 line_breakcheck();
7309 }
7310}
7311
7312/*
7313 * Handle the CTRL-U and CTRL-D commands.
7314 */
7315 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007316nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317{
7318 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7319 || (cap->cmdchar == Ctrl_D
7320 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7321 clearopbeep(cap->oap);
7322 else if (!checkclearop(cap->oap))
7323 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7324}
7325
7326/*
7327 * Handle "J" or "gJ" command.
7328 */
7329 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007330nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007332 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007334 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 {
7336 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007337 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7339 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007341 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007342 if (cap->count0 <= 2)
7343 {
7344 clearopbeep(cap->oap);
7345 return;
7346 }
7347 cap->count0 = curbuf->b_ml.ml_line_count
7348 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007350
7351 prep_redo(cap->oap->regname, cap->count0,
7352 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7353 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 }
7355}
7356
7357/*
7358 * "P", "gP", "p" and "gp" commands.
7359 */
7360 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007361nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007363 nv_put_opt(cap, FALSE);
7364}
7365
7366/*
7367 * "P", "gP", "p" and "gp" commands.
7368 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7369 */
7370 static void
7371nv_put_opt(cmdarg_T *cap, int fix_indent)
7372{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 int regname = 0;
7374 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007375 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007376 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 int dir;
7378 int flags = 0;
7379
7380 if (cap->oap->op_type != OP_NOP)
7381 {
7382#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007383 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7385 {
7386 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007387 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 }
7389 else
7390#endif
7391 clearopbeep(cap->oap);
7392 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007393#ifdef FEAT_JOB_CHANNEL
7394 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7395 {
7396 clearopbeep(cap->oap);
7397 }
7398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 else
7400 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007401 if (fix_indent)
7402 {
7403 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7404 ? FORWARD : BACKWARD;
7405 flags |= PUT_FIXINDENT;
7406 }
7407 else
7408 dir = (cap->cmdchar == 'P'
7409 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 ? BACKWARD : FORWARD;
7411 prep_redo_cmd(cap);
7412 if (cap->cmdchar == 'g')
7413 flags |= PUT_CURSEND;
7414
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 if (VIsual_active)
7416 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007417 // Putting in Visual mode: The put text replaces the selected
7418 // text. First delete the selected text, then put the new text.
7419 // Need to save and restore the registers that the delete
7420 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007421 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007423#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007425#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007426 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007427 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007428#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431
7432 )
7433 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007434 // The delete is going to overwrite the register we want to
7435 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 reg1 = get_register(regname, TRUE);
7437 }
7438
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007439 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 cap->cmdchar = 'd';
7441 cap->nchar = NUL;
7442 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007443 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 nv_operator(cap);
7445 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007446 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007447 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007449 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 cap->oap->regname = regname;
7451
7452 if (reg1 != NULL)
7453 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007454 // Delete probably changed the register we want to put, save
7455 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 reg2 = get_register(regname, FALSE);
7457 put_register(regname, reg1);
7458 }
7459
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007460 // When deleted a linewise Visual area, put the register as
7461 // lines to avoid it joined with the next line. When deletion was
7462 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 if (VIsual_mode == 'V')
7464 flags |= PUT_LINE;
7465 else if (VIsual_mode == 'v')
7466 flags |= PUT_LINE_SPLIT;
7467 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7468 flags |= PUT_LINE_FORWARD;
7469 dir = BACKWARD;
7470 if ((VIsual_mode != 'V'
7471 && curwin->w_cursor.col < curbuf->b_op_start.col)
7472 || (VIsual_mode == 'V'
7473 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007474 // cursor is at the end of the line or end of file, put
7475 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007477 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007478 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007480 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007482 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 if (reg2 != NULL)
7484 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007485
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007486 // What to reselect with "gv"? Selecting the just put text seems to
7487 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007488 if (was_visual)
7489 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007490 curbuf->b_visual.vi_start = curbuf->b_op_start;
7491 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007492 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007493 if (*p_sel == 'e')
7494 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007495 }
7496
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007497 // When all lines were selected and deleted do_put() leaves an empty
7498 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007499 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007500 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007501 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007502 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007503
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007504 // If the cursor was in that line, move it to the end of the last
7505 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007506 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7507 {
7508 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7509 coladvance((colnr_T)MAXCOL);
7510 }
7511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 auto_format(FALSE, TRUE);
7513 }
7514}
7515
7516/*
7517 * "o" and "O" commands.
7518 */
7519 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007520nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521{
7522#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007523 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7525 {
7526 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007527 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 }
7529 else
7530#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007531 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007533#ifdef FEAT_JOB_CHANNEL
7534 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007535 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 n_opencmd(cap);
7539}
7540
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541#ifdef FEAT_NETBEANS_INTG
7542 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007543nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544{
7545 netbeans_keycommand(cap->nchar);
7546}
7547#endif
7548
7549#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007551nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007553 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554}
7555#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007556
Bram Moolenaar3918c952005-03-15 22:34:55 +00007557/*
7558 * Trigger CursorHold event.
7559 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7560 * input buffer. "did_cursorhold" is set to avoid retriggering.
7561 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007562 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007563nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007564{
7565 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7566 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007567 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007568}