blob: 580eb72298d559b7e791da571a068cfd20f66e54 [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 *
ichizok672776d2022-01-31 12:27:18 +00003 * VIM - Vi IMproved by Bram Moolenaar et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004 *
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 +000018
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010019#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010020static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount);
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010021#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static void unshift_special(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010023static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25/*
26 * nv_*(): functions called to handle Normal and Visual mode commands.
27 * n_*(): functions called to handle Normal mode commands.
28 * v_*(): functions called to handle Visual mode commands.
29 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010030static void nv_ignore(cmdarg_T *cap);
31static void nv_nop(cmdarg_T *cap);
32static void nv_error(cmdarg_T *cap);
33static void nv_help(cmdarg_T *cap);
34static void nv_addsub(cmdarg_T *cap);
35static void nv_page(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010036static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010038static void nv_ver_scrollbar(cmdarg_T *cap);
39static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000041#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void nv_tabline(cmdarg_T *cap);
43static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000044#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010045static void nv_exmode(cmdarg_T *cap);
46static void nv_colon(cmdarg_T *cap);
47static void nv_ctrlg(cmdarg_T *cap);
48static void nv_ctrlh(cmdarg_T *cap);
49static void nv_clear(cmdarg_T *cap);
50static void nv_ctrlo(cmdarg_T *cap);
51static void nv_hat(cmdarg_T *cap);
52static void nv_Zet(cmdarg_T *cap);
53static void nv_ident(cmdarg_T *cap);
54static void nv_tagpop(cmdarg_T *cap);
55static void nv_scroll(cmdarg_T *cap);
56static void nv_right(cmdarg_T *cap);
57static void nv_left(cmdarg_T *cap);
58static void nv_up(cmdarg_T *cap);
59static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010060static void nv_end(cmdarg_T *cap);
61static void nv_dollar(cmdarg_T *cap);
62static void nv_search(cmdarg_T *cap);
63static void nv_next(cmdarg_T *cap);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +020064static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt, int *wrapped);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010065static void nv_csearch(cmdarg_T *cap);
66static void nv_brackets(cmdarg_T *cap);
67static void nv_percent(cmdarg_T *cap);
68static void nv_brace(cmdarg_T *cap);
69static void nv_mark(cmdarg_T *cap);
70static void nv_findpar(cmdarg_T *cap);
71static void nv_undo(cmdarg_T *cap);
72static void nv_kundo(cmdarg_T *cap);
73static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010074static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010075static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
76static void v_visop(cmdarg_T *cap);
77static void nv_subst(cmdarg_T *cap);
78static void nv_abbrev(cmdarg_T *cap);
79static void nv_optrans(cmdarg_T *cap);
80static void nv_gomark(cmdarg_T *cap);
81static void nv_pcmark(cmdarg_T *cap);
82static void nv_regname(cmdarg_T *cap);
83static void nv_visual(cmdarg_T *cap);
84static void n_start_visual_mode(int c);
85static void nv_window(cmdarg_T *cap);
86static void nv_suspend(cmdarg_T *cap);
87static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010088static void nv_dot(cmdarg_T *cap);
Shougo Matsushita4ede01f2022-01-20 15:26:03 +000089static void nv_redo_or_register(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010090static void nv_Undo(cmdarg_T *cap);
91static void nv_tilde(cmdarg_T *cap);
92static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000093#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010094static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000095#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010096static void nv_lineop(cmdarg_T *cap);
97static void nv_home(cmdarg_T *cap);
98static void nv_pipe(cmdarg_T *cap);
99static void nv_bck_word(cmdarg_T *cap);
100static void nv_wordcmd(cmdarg_T *cap);
101static void nv_beginline(cmdarg_T *cap);
102static void adjust_cursor(oparg_T *oap);
103static void adjust_for_sel(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100104static void nv_select(cmdarg_T *cap);
105static void nv_goto(cmdarg_T *cap);
106static void nv_normal(cmdarg_T *cap);
107static void nv_esc(cmdarg_T *oap);
108static void nv_edit(cmdarg_T *cap);
109static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100110static void nv_object(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100111static void nv_record(cmdarg_T *cap);
112static void nv_at(cmdarg_T *cap);
113static void nv_halfpage(cmdarg_T *cap);
114static void nv_join(cmdarg_T *cap);
115static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200116static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100117static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100119static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100122static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100124static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125
ichizok672776d2022-01-31 12:27:18 +0000126// Declare nv_cmds[].
127#define DO_DECLARE_NVCMD
128#include "nv_cmds.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Yegappan Lakshmanan4dc0dd82022-01-29 13:06:40 +0000130// Include the lookuptable generated by create_nvcmdidx.vim.
131#include "nv_cmdidxs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133/*
134 * Search for a command in the commands table.
135 * Returns -1 for invalid command.
136 */
137 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100138find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139{
140 int i;
141 int idx;
142 int top, bot;
143 int c;
144
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100145 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 if (cmdchar >= 0x100)
147 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100149 // We use the absolute value of the character. Special keys have a
150 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 if (cmdchar < 0)
152 cmdchar = -cmdchar;
153
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100154 // If the character is in the first part: The character is the index into
155 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 if (cmdchar <= nv_max_linear)
157 return nv_cmd_idx[cmdchar];
158
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100159 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 bot = nv_max_linear + 1;
161 top = NV_CMDS_SIZE - 1;
162 idx = -1;
163 while (bot <= top)
164 {
165 i = (top + bot) / 2;
166 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
167 if (c < 0)
168 c = -c;
169 if (cmdchar == c)
170 {
171 idx = nv_cmd_idx[i];
172 break;
173 }
174 if (cmdchar > c)
175 bot = i + 1;
176 else
177 top = i - 1;
178 }
179 return idx;
180}
181
182/*
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100183 * If currently editing a cmdline or text is locked: beep and give an error
184 * message, return TRUE.
185 */
186 static int
187check_text_locked(oparg_T *oap)
188{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000189 if (!text_locked())
190 return FALSE;
191
192 if (oap != NULL)
193 clearopbeep(oap);
194 text_locked_msg();
195 return TRUE;
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100196}
197
198/*
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000199 * If text is locked, "curbuf_lock" or "allbuf_lock" is set:
200 * Give an error message, possibly beep and return TRUE.
201 * "oap" may be NULL.
202 */
203 int
204check_text_or_curbuf_locked(oparg_T *oap)
205{
206 if (check_text_locked(oap))
207 return TRUE;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000208
209 if (!curbuf_locked())
210 return FALSE;
211
212 if (oap != NULL)
213 clearop(oap);
214 return TRUE;
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000215}
216
217/*
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000218 * Handle the count before a normal command and set cap->count0.
219 */
220 static int
221normal_cmd_get_count(
222 cmdarg_T *cap,
223 int c,
224 int toplevel UNUSED,
225 int set_prevcount UNUSED,
226 int *ctrl_w,
227 int *need_flushbuf UNUSED)
228{
229getcount:
230 if (!(VIsual_active && VIsual_select))
231 {
232 // Handle a count before a command and compute ca.count0.
233 // Note that '0' is a command and not the start of a count, but it's
234 // part of a count after other digits.
235 while ((c >= '1' && c <= '9')
236 || (cap->count0 != 0 && (c == K_DEL || c == K_KDEL
237 || c == '0')))
238 {
239 if (c == K_DEL || c == K_KDEL)
240 {
241 cap->count0 /= 10;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000242 del_from_showcmd(4); // delete the digit and ~@%
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000243 }
244 else if (cap->count0 > 99999999L)
245 {
246 cap->count0 = 999999999L;
247 }
248 else
249 {
250 cap->count0 = cap->count0 * 10 + (c - '0');
251 }
252#ifdef FEAT_EVAL
253 // Set v:count here, when called from main() and not a stuffed
254 // command, so that v:count can be used in an expression mapping
255 // right after the count. Do set it for redo.
256 if (toplevel && readbuf1_empty())
257 set_vcount_ca(cap, &set_prevcount);
258#endif
259 if (*ctrl_w)
260 {
261 ++no_mapping;
262 ++allow_keys; // no mapping for nchar, but keys
263 }
264 ++no_zero_mapping; // don't map zero here
265 c = plain_vgetc();
266 LANGMAP_ADJUST(c, TRUE);
267 --no_zero_mapping;
268 if (*ctrl_w)
269 {
270 --no_mapping;
271 --allow_keys;
272 }
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000273 *need_flushbuf |= add_to_showcmd(c);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000274 }
275
276 // If we got CTRL-W there may be a/another count
277 if (c == Ctrl_W && !*ctrl_w && cap->oap->op_type == OP_NOP)
278 {
279 *ctrl_w = TRUE;
280 cap->opcount = cap->count0; // remember first count
281 cap->count0 = 0;
282 ++no_mapping;
283 ++allow_keys; // no mapping for nchar, but keys
284 c = plain_vgetc(); // get next character
285 LANGMAP_ADJUST(c, TRUE);
286 --no_mapping;
287 --allow_keys;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000288 *need_flushbuf |= add_to_showcmd(c);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000289 goto getcount; // jump back
290 }
291 }
292
293 if (c == K_CURSORHOLD)
294 {
295 // Save the count values so that ca.opcount and ca.count0 are exactly
296 // the same when coming back here after handling K_CURSORHOLD.
297 cap->oap->prev_opcount = cap->opcount;
298 cap->oap->prev_count0 = cap->count0;
299 }
300 else if (cap->opcount != 0)
301 {
302 // If we're in the middle of an operator (including after entering a
303 // yank buffer with '"') AND we had a count before the operator, then
304 // that count overrides the current value of ca.count0.
305 // What this means effectively, is that commands like "3dw" get turned
306 // into "d3w" which makes things fall into place pretty neatly.
307 // If you give a count before AND after the operator, they are
308 // multiplied.
309 if (cap->count0)
310 {
311 if (cap->opcount >= 999999999L / cap->count0)
312 cap->count0 = 999999999L;
313 else
314 cap->count0 *= cap->opcount;
315 }
316 else
317 cap->count0 = cap->opcount;
318 }
319
320 // Always remember the count. It will be set to zero (on the next call,
321 // above) when there is no pending operator.
322 // When called from main(), save the count for use by the "count" built-in
323 // variable.
324 cap->opcount = cap->count0;
325 cap->count1 = (cap->count0 == 0 ? 1 : cap->count0);
326
327#ifdef FEAT_EVAL
328 // Only set v:count when called from main() and not a stuffed command.
329 // Do set it for redo.
330 if (toplevel && readbuf1_empty())
331 set_vcount(cap->count0, cap->count1, set_prevcount);
332#endif
333
334 return c;
335}
336
337/*
338 * Returns TRUE if the normal command (cap) needs a second character.
339 */
340 static int
341normal_cmd_needs_more_chars(cmdarg_T *cap, short_u cmd_flags)
342{
343 return ((cmd_flags & NV_NCH)
344 && (((cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
345 && cap->oap->op_type == OP_NOP)
346 || (cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
347 || (cap->cmdchar == 'q'
348 && cap->oap->op_type == OP_NOP
349 && reg_recording == 0
350 && reg_executing == 0)
351 || ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
352 && (cap->oap->op_type != OP_NOP || VIsual_active))));
353}
354
355/*
356 * Get one or more additional characters for a normal command.
357 * Return the updated command index (if changed).
358 */
359 static int
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000360normal_cmd_get_more_chars(
361 int idx_arg,
362 cmdarg_T *cap,
363 int *need_flushbuf UNUSED)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000364{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000365 int idx = idx_arg;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000366 int c;
367 int *cp;
368 int repl = FALSE; // get character for replace mode
369 int lit = FALSE; // get extra character literally
370 int langmap_active = FALSE; // using :lmap mappings
371 int lang; // getting a text character
372#ifdef HAVE_INPUT_METHOD
373 int save_smd; // saved value of p_smd
374#endif
375
376 ++no_mapping;
377 ++allow_keys; // no mapping for nchar, but allow key codes
378 // Don't generate a CursorHold event here, most commands can't handle
379 // it, e.g., nv_replace(), nv_csearch().
380 did_cursorhold = TRUE;
381 if (cap->cmdchar == 'g')
382 {
383 /*
384 * For 'g' get the next character now, so that we can check for
385 * "gr", "g'" and "g`".
386 */
387 cap->nchar = plain_vgetc();
388 LANGMAP_ADJUST(cap->nchar, TRUE);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000389 *need_flushbuf |= add_to_showcmd(cap->nchar);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000390 if (cap->nchar == 'r' || cap->nchar == '\'' || cap->nchar == '`'
391 || cap->nchar == Ctrl_BSL)
392 {
393 cp = &cap->extra_char; // need to get a third character
394 if (cap->nchar != 'r')
395 lit = TRUE; // get it literally
396 else
397 repl = TRUE; // get it in replace mode
398 }
399 else
400 cp = NULL; // no third character needed
401 }
402 else
403 {
404 if (cap->cmdchar == 'r') // get it in replace mode
405 repl = TRUE;
406 cp = &cap->nchar;
407 }
408 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
409
410 /*
411 * Get a second or third character.
412 */
413 if (cp != NULL)
414 {
415 if (repl)
416 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100417 State = MODE_REPLACE; // pretend Replace mode
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000418#ifdef CURSOR_SHAPE
419 ui_cursor_shape(); // show different cursor shape
420#endif
421 }
422 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
423 {
424 // Allow mappings defined with ":lmap".
425 --no_mapping;
426 --allow_keys;
427 if (repl)
Bram Moolenaar24959102022-05-07 20:01:16 +0100428 State = MODE_LREPLACE;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000429 else
Bram Moolenaar24959102022-05-07 20:01:16 +0100430 State = MODE_LANGMAP;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000431 langmap_active = TRUE;
432 }
433#ifdef HAVE_INPUT_METHOD
434 save_smd = p_smd;
435 p_smd = FALSE; // Don't let the IM code show the mode here
436 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
437 im_set_active(TRUE);
438#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100439 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000440 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100441 MAY_WANT_TO_LOG_THIS;
442
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000443 // Disable bracketed paste and modifyOtherKeys here, we won't
444 // recognize the escape sequences with 'esckeys' off.
445 out_str(T_BD);
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000446 out_str_t_TE();
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000447 }
448
449 *cp = plain_vgetc();
450
Bram Moolenaar24959102022-05-07 20:01:16 +0100451 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000452 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100453 MAY_WANT_TO_LOG_THIS;
454
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000455 // Re-enable bracketed paste mode and modifyOtherKeys
Bram Moolenaarfc966c12023-01-01 18:04:33 +0000456 out_str_t_BE();
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000457 out_str_t_TI();
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000458 }
459
460 if (langmap_active)
461 {
462 // Undo the decrement done above
463 ++no_mapping;
464 ++allow_keys;
Bram Moolenaar24959102022-05-07 20:01:16 +0100465 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000466 }
467#ifdef HAVE_INPUT_METHOD
468 if (lang)
469 {
470 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
471 im_save_status(&curbuf->b_p_iminsert);
472 im_set_active(FALSE);
473 }
474 p_smd = save_smd;
475#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100476 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000477 *need_flushbuf |= add_to_showcmd(*cp);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000478
479 if (!lit)
480 {
481#ifdef FEAT_DIGRAPHS
482 // Typing CTRL-K gets a digraph.
483 if (*cp == Ctrl_K
484 && ((nv_cmds[idx].cmd_flags & NV_LANG)
485 || cp == &cap->extra_char)
486 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
487 {
488 c = get_digraph(FALSE);
489 if (c > 0)
490 {
491 *cp = c;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000492 // Guessing how to update showcmd here...
493 del_from_showcmd(3);
494 *need_flushbuf |= add_to_showcmd(*cp);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000495 }
496 }
497#endif
498
499 // adjust chars > 127, except after "tTfFr" commands
500 LANGMAP_ADJUST(*cp, !lang);
501#ifdef FEAT_RIGHTLEFT
502 // adjust Hebrew mapped char
503 if (p_hkmap && lang && KeyTyped)
504 *cp = hkmap(*cp);
505#endif
506 }
507
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000508 // When the next character is CTRL-\ a following CTRL-N means the
509 // command is aborted and we go to Normal mode.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000510 if (cp == &cap->extra_char
511 && cap->nchar == Ctrl_BSL
512 && (cap->extra_char == Ctrl_N || cap->extra_char == Ctrl_G))
513 {
514 cap->cmdchar = Ctrl_BSL;
515 cap->nchar = cap->extra_char;
516 idx = find_command(cap->cmdchar);
517 }
zeertzjq4f026ea2023-02-26 14:47:24 +0000518 else if ((cap->nchar == 'n' || cap->nchar == 'N')
519 && cap->cmdchar == 'g')
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000520 cap->oap->op_type = get_op_type(*cp, NUL);
521 else if (*cp == Ctrl_BSL)
522 {
523 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
524
525 // There is a busy wait here when typing "f<C-\>" and then
526 // something different from CTRL-N. Can't be avoided.
527 while ((c = vpeekc()) <= 0 && towait > 0L)
528 {
529 do_sleep(towait > 50L ? 50L : towait, FALSE);
530 towait -= 50L;
531 }
532 if (c > 0)
533 {
534 c = plain_vgetc();
535 if (c != Ctrl_N && c != Ctrl_G)
536 vungetc(c);
537 else
538 {
539 cap->cmdchar = Ctrl_BSL;
540 cap->nchar = c;
541 idx = find_command(cap->cmdchar);
542 }
543 }
544 }
545
zeertzjqbacc8302023-08-12 00:09:31 +0200546 if (enc_utf8 && lang)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000547 {
zeertzjqbacc8302023-08-12 00:09:31 +0200548 // When getting a text character and the next character is a
549 // multi-byte character, it could be a composing character.
550 // However, don't wait for it to arrive. Also, do enable mapping,
551 // because if it's put back with vungetc() it's too late to apply
552 // mapping.
553 --no_mapping;
554 while ((c = vpeekc()) > 0
555 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000556 {
zeertzjqbacc8302023-08-12 00:09:31 +0200557 c = plain_vgetc();
558 if (!utf_iscomposing(c))
559 {
560 vungetc(c); // it wasn't, put it back
561 break;
562 }
563 else if (cap->ncharC1 == 0)
564 cap->ncharC1 = c;
565 else
566 cap->ncharC2 = c;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000567 }
zeertzjqbacc8302023-08-12 00:09:31 +0200568 ++no_mapping;
569 // Vim may be in a different mode when the user types the next key,
570 // but when replaying a recording the next key is already in the
zeertzjqbf321802024-01-28 19:03:00 +0100571 // typeahead buffer, so record an <Ignore> before that to prevent
572 // the vpeekc() above from applying wrong mappings when replaying.
zeertzjqdccc29c2023-09-04 22:25:07 +0200573 ++no_u_sync;
zeertzjqbf321802024-01-28 19:03:00 +0100574 gotchars_ignore();
zeertzjqdccc29c2023-09-04 22:25:07 +0200575 --no_u_sync;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000576 }
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000577 }
578 --no_mapping;
579 --allow_keys;
580
581 return idx;
582}
583
584/*
585 * Returns TRUE if after processing a normal mode command, need to wait for a
586 * moment when a message is displayed that will be overwritten by the mode
587 * message.
588 */
589 static int
590normal_cmd_need_to_wait_for_msg(cmdarg_T *cap, pos_T *old_pos)
591{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000592 // In Visual mode and with "^O" in Insert mode, a short message will be
593 // overwritten by the mode message. Wait a bit, until a key is hit.
594 // In Visual mode, it's more important to keep the Visual area updated
595 // than keeping a message (e.g. from a /pat search).
596 // Only do this if the command was typed, not from a mapping.
597 // Don't wait when emsg_silent is non-zero.
598 // Also wait a bit after an error message, e.g. for "^O:".
599 // Don't redraw the screen, it would remove the message.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000600 return ( ((p_smd
601 && msg_silent == 0
602 && (restart_edit != 0
603 || (VIsual_active
604 && old_pos->lnum == curwin->w_cursor.lnum
605 && old_pos->col == curwin->w_cursor.col)
606 )
607 && (clear_cmdline
608 || redraw_cmdline)
609 && (msg_didout || (msg_didany && msg_scroll))
610 && !msg_nowait
611 && KeyTyped)
612 || (restart_edit != 0
613 && !VIsual_active
614 && (msg_scroll
615 || emsg_on_display)))
616 && cap->oap->regname == 0
617 && !(cap->retval & CA_COMMAND_BUSY)
618 && stuff_empty()
619 && typebuf_typed()
620 && emsg_silent == 0
621 && !in_assert_fails
622 && !did_wait_return
623 && cap->oap->op_type == OP_NOP);
624}
625
626/*
627 * After processing a normal mode command, wait for a moment when a message is
628 * displayed that will be overwritten by the mode message.
629 */
630 static void
631normal_cmd_wait_for_msg(void)
632{
633 int save_State = State;
634
635 // Draw the cursor with the right shape here
636 if (restart_edit != 0)
Bram Moolenaar24959102022-05-07 20:01:16 +0100637 State = MODE_INSERT;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000638
639 // If need to redraw, and there is a "keep_msg", redraw before the
640 // delay
641 if (must_redraw && keep_msg != NULL && !emsg_on_display)
642 {
643 char_u *kmsg;
644
645 kmsg = keep_msg;
646 keep_msg = NULL;
647 // Showmode() will clear keep_msg, but we want to use it anyway.
648 // First update w_topline.
649 setcursor();
650 update_screen(0);
651 // now reset it, otherwise it's put in the history again
652 keep_msg = kmsg;
653
654 kmsg = vim_strsave(keep_msg);
655 if (kmsg != NULL)
656 {
657 msg_attr((char *)kmsg, keep_msg_attr);
658 vim_free(kmsg);
659 }
660 }
661 setcursor();
662#ifdef CURSOR_SHAPE
663 ui_cursor_shape(); // may show different cursor shape
664#endif
665 cursor_on();
666 out_flush();
667 if (msg_scroll || emsg_on_display)
668 ui_delay(1003L, TRUE); // wait at least one second
669 ui_delay(3003L, FALSE); // wait up to three seconds
670 State = save_State;
671
672 msg_scroll = FALSE;
673 emsg_on_display = FALSE;
674}
675
676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 * Execute a command in Normal mode.
678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100680normal_cmd(
681 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100682 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100684 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100686 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 int old_col = curwin->w_curswant;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000688 int need_flushbuf = FALSE; // need to call out_flush()
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100689 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 static int old_mapped_len = 0;
692 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000693 int set_prevcount = FALSE;
Bram Moolenaarb146e012020-07-19 23:06:05 +0200694 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695
Bram Moolenaara80faa82020-04-12 19:37:17 +0200696 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000698
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100699 // Use a count remembered from before entering an operator. After typing
700 // "3d" we return from normal_cmd() and come back here, the "3" is
701 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 ca.opcount = opcount;
703
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000704 // If there is an operator pending, then the command we take this time
705 // will terminate it. Finish_op tells us to finish the operation before
706 // returning this time (unless the operation was cancelled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707#ifdef CURSOR_SHAPE
708 c = finish_op;
709#endif
710 finish_op = (oap->op_type != OP_NOP);
711#ifdef CURSOR_SHAPE
712 if (finish_op != c)
713 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100714 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715# ifdef FEAT_MOUSESHAPE
716 update_mouseshape(-1);
717# endif
718 }
719#endif
LemonBoy2bf52dd2022-04-09 18:17:34 +0100720 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100722 // When not finishing an operator and no register name typed, reset the
723 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000725 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000727#ifdef FEAT_EVAL
728 set_prevcount = TRUE;
729#endif
730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100732 // Restore counts from before receiving K_CURSORHOLD. This means after
733 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
734 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000735 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
736 {
737 ca.opcount = oap->prev_opcount;
738 ca.count0 = oap->prev_count0;
739 oap->prev_opcount = 0;
740 oap->prev_count0 = 0;
741 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000742
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744
Bram Moolenaar24959102022-05-07 20:01:16 +0100745 State = MODE_NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100747 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748#endif
749
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100750#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100751 // Set v:count here, when called from main() and not a stuffed
752 // command, so that v:count can be used in an expression mapping
753 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100754 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100755 set_vcount_ca(&ca, &set_prevcount);
756#endif
757
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 /*
759 * Get the command character from the user.
760 */
761 c = safe_vgetc();
Bram Moolenaar24959102022-05-07 20:01:16 +0100762 LANGMAP_ADJUST(c, get_real_state() != MODE_SELECT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000764 // If a mapping was started in Visual or Select mode, remember the length
765 // of the mapping. This is used below to not return to Insert mode for as
766 // long as the mapping is being executed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 if (restart_edit == 0)
768 old_mapped_len = 0;
769 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000770 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 old_mapped_len = typebuf_maplen();
772
773 if (c == NUL)
774 c = K_ZERO;
775
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000776 // In Select mode, typed text replaces the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 if (VIsual_active
778 && VIsual_select
779 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
780 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000781 int len;
782
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100783 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
784 // 'insertmode' is set) fake a "d"elete command, Insert mode will
785 // restart automatically.
786 // Insert the typed character in the typeahead buffer, so that it can
787 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000788 len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
789
zeertzjqfbf4f1c2022-01-28 12:50:43 +0000790 // When recording and gotchars() was called the character will be
791 // recorded again, remove the previous recording.
792 if (KeyTyped)
793 ungetchars(len);
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000794
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000795 if (restart_edit != 0)
796 c = 'd';
797 else
798 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100799 msg_nowait = TRUE; // don't delay going to insert mode
800 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
Bram Moolenaard0fb9072021-12-09 11:57:22 +0000803 // If the window was made so small that nothing shows, make it at least one
804 // line and one column when typing a command.
805 if (KeyTyped && !KeyStuffed)
806 win_ensure_size();
807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 need_flushbuf = add_to_showcmd(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000810 // Get the command count
811 c = normal_cmd_get_count(&ca, c, toplevel, set_prevcount, &ctrl_w,
812 &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000814 // Find the command character in the table of commands.
815 // For CTRL-W we already got nchar when looking for a count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 if (ctrl_w)
817 {
818 ca.nchar = c;
819 ca.cmdchar = Ctrl_W;
820 }
821 else
822 ca.cmdchar = c;
823 idx = find_command(ca.cmdchar);
824 if (idx < 0)
825 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100826 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 clearopbeep(oap);
828 goto normal_end;
829 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000830
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000831 if ((nv_cmds[idx].cmd_flags & NV_NCW) && check_text_or_curbuf_locked(oap))
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100832 // this command is not allowed now
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000833 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000835 // In Visual/Select mode, a few keys are handled in a special way.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 if (VIsual_active)
837 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100838 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 if (km_stopsel
840 && (nv_cmds[idx].cmd_flags & NV_STS)
841 && !(mod_mask & MOD_MASK_SHIFT))
842 {
843 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100844 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 }
846
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100847 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 if (km_startsel)
849 {
850 if (nv_cmds[idx].cmd_flags & NV_SS)
851 {
852 unshift_special(&ca);
853 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000854 if (idx < 0)
855 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100856 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000857 clearopbeep(oap);
858 goto normal_end;
859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 }
861 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
862 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 }
865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866
867#ifdef FEAT_RIGHTLEFT
868 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
869 && (nv_cmds[idx].cmd_flags & NV_RL))
870 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100871 // Invert horizontal movements and operations. Only when typed by the
872 // user directly, not when the result of a mapping or "x" translated
873 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 switch (ca.cmdchar)
875 {
876 case 'l': ca.cmdchar = 'h'; break;
877 case K_RIGHT: ca.cmdchar = K_LEFT; break;
878 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
879 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
880 case 'h': ca.cmdchar = 'l'; break;
881 case K_LEFT: ca.cmdchar = K_RIGHT; break;
882 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
883 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
884 case '>': ca.cmdchar = '<'; break;
885 case '<': ca.cmdchar = '>'; break;
886 }
887 idx = find_command(ca.cmdchar);
888 }
889#endif
890
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000891 // Get additional characters if we need them.
892 if (normal_cmd_needs_more_chars(&ca, nv_cmds[idx].cmd_flags))
893 idx = normal_cmd_get_more_chars(idx, &ca, &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000895 // Flush the showcmd characters onto the screen so we can see them while
896 // the command is being executed. Only do this when the shown command was
897 // actually displayed, otherwise this will slow down a lot when executing
898 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 if (need_flushbuf)
900 out_flush();
Martin Tournoijba43e762022-10-13 22:12:15 +0100901
Bram Moolenaard9205ca2008-10-02 20:55:54 +0000902 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +0200903 {
904 if (ex_normal_busy)
905 did_cursorhold = save_did_cursorhold;
906 else
907 did_cursorhold = FALSE;
908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909
Bram Moolenaar24959102022-05-07 20:01:16 +0100910 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911
zeertzjqf86dea82023-03-05 21:15:06 +0000912 if (ca.nchar == ESC || ca.extra_char == ESC)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 {
914 clearop(oap);
915 if (restart_edit == 0 && goto_im())
916 restart_edit = 'a';
917 goto normal_end;
918 }
919
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000920 if (ca.cmdchar != K_IGNORE)
921 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100922 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000923 msg_col = 0;
924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925
Luuk van Baalaa7f25e2023-04-13 21:47:38 +0100926 old_pos = curwin->w_cursor; // remember where the cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100928 // When 'keymodel' contains "startsel" some keys start Select/Visual
929 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 if (!VIsual_active && km_startsel)
931 {
932 if (nv_cmds[idx].cmd_flags & NV_SS)
933 {
934 start_selection();
935 unshift_special(&ca);
936 idx = find_command(ca.cmdchar);
937 }
938 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
939 && (mod_mask & MOD_MASK_SHIFT))
940 {
941 start_selection();
942 mod_mask &= ~MOD_MASK_SHIFT;
943 }
944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000946 // Execute the command!
947 // Call the command function found in the commands table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 ca.arg = nv_cmds[idx].cmd_arg;
949 (nv_cmds[idx].cmd_func)(&ca);
950
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000951 // If we didn't start or finish an operator, reset oap->regname, unless we
952 // need it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 if (!finish_op
954 && !oap->op_type
955 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
956 {
957 clearop(oap);
958#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +0200959 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960#endif
961 }
962
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100963 // Get the length of mapped chars again after typing a count, second
964 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000965 if (old_mapped_len > 0)
966 old_mapped_len = typebuf_maplen();
967
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000968 // If an operation is pending, handle it. But not for K_IGNORE or
969 // K_MOUSEMOVE.
Bram Moolenaar1ad72c82021-05-04 21:56:28 +0200970 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +0100971 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000973 // Wait for a moment when a message is displayed that will be overwritten
974 // by the mode message.
975 if (normal_cmd_need_to_wait_for_msg(&ca, &old_pos))
976 normal_cmd_wait_for_msg();
977
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000978 // Finish up after executing a Normal mode command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979normal_end:
980
981 msg_nowait = FALSE;
982
Bram Moolenaarcc613032020-06-07 21:31:18 +0200983#ifdef FEAT_EVAL
984 if (finish_op)
985 reset_reg_var();
986#endif
987
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988#ifdef CURSOR_SHAPE
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000989 int prev_finish_op = finish_op;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990#endif
zeertzjq73916ba2023-04-26 16:50:19 +0100991 if (oap->op_type == OP_NOP)
992 {
993 // Reset finish_op, in case it was set
994 finish_op = FALSE;
995 may_trigger_modechanged();
996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100998 // Redraw the cursor with another shape, if we were in Operator-pending
999 // mode or did a replace command.
zeertzjqf86dea82023-03-05 21:15:06 +00001000 if (prev_finish_op || ca.cmdchar == 'r'
1001 || (ca.cmdchar == 'g' && ca.nchar == 'r'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001003 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004# ifdef FEAT_MOUSESHAPE
1005 update_mouseshape(-1);
1006# endif
1007 }
1008#endif
1009
Bram Moolenaara983fe92008-07-31 20:04:27 +00001010 if (oap->op_type == OP_NOP && oap->regname == 0
zeertzjq73916ba2023-04-26 16:50:19 +01001011 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001014 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 vim_free(ca.searchbuf);
1016
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 if (has_mbyte)
1018 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 if (curwin->w_p_scb && toplevel)
1021 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001022 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 do_check_scrollbind(TRUE);
1024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025
Bram Moolenaar860cae12010-06-05 23:22:07 +02001026 if (curwin->w_p_crb && toplevel)
1027 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001028 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001029 do_check_cursorbind();
1030 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001031
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001032#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001033 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001034 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001035 restart_edit = 0;
1036#endif
1037
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001038 // May restart edit(), if we got here with CTRL-O in Insert mode (but not
1039 // if still inside a mapping that started in Visual mode).
1040 // May switch from Visual to Select mode after CTRL-O command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1043 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 && !(ca.retval & CA_COMMAND_BUSY)
1045 && stuff_empty()
1046 && oap->regname == 0)
1047 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 if (restart_VIsual_select == 1)
1049 {
1050 VIsual_select = TRUE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01001051 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 showmode();
1053 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001054 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001056 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 (void)edit(restart_edit, FALSE, 1L);
1058 }
1059
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 if (restart_VIsual_select == 2)
1061 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001063 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 opcount = ca.opcount;
1065}
1066
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001067#ifdef FEAT_EVAL
1068/*
1069 * Set v:count and v:count1 according to "cap".
1070 * Set v:prevcount only when "set_prevcount" is TRUE.
1071 */
1072 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001073set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001074{
1075 long count = cap->count0;
1076
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001077 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001078 if (cap->opcount != 0)
1079 count = cap->opcount * (count == 0 ? 1 : count);
1080 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001081 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001082}
1083#endif
1084
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001086 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 * if not.
1088 */
1089 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001090check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091{
1092 static int did_check = FALSE;
1093
1094 if (full_screen)
1095 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001096 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001097 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 did_check = TRUE;
1099 }
1100}
1101
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001102#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1103/*
1104 * Call yank_do_autocmd() for "regname".
1105 */
1106 static void
1107call_yank_do_autocmd(int regname)
1108{
1109 oparg_T oa;
1110 yankreg_T *reg;
1111
1112 clear_oparg(&oa);
1113 oa.regname = regname;
1114 oa.op_type = OP_YANK;
1115 oa.is_VIsual = TRUE;
1116 reg = get_register(regname, TRUE);
1117 yank_do_autocmd(&oa, reg);
1118 free_register(reg);
1119}
1120#endif
1121
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001123 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001124 * This function or the next should ALWAYS be called to end Visual mode, except
1125 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 */
1127 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001128end_visual_mode(void)
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001129{
1130 end_visual_mode_keep_button();
1131 reset_held_button();
1132}
1133
1134 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001135end_visual_mode_keep_button(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136{
1137#ifdef FEAT_CLIPBOARD
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001138 // If we are using the clipboard, then remember what was selected in case
1139 // we need to paste it somewhere while we still own the selection.
1140 // Only do this when the clipboard is already owned. Don't want to grab
1141 // the selection when hitting ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 if (clip_star.available && clip_star.owned)
1143 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001144
1145# if defined(FEAT_EVAL)
1146 // Emit a TextYankPost for the automatic copy of the selection into the
1147 // star and/or plus register.
1148 if (has_textyankpost())
1149 {
1150 if (clip_isautosel_star())
1151 call_yank_do_autocmd('*');
1152 if (clip_isautosel_plus())
1153 call_yank_do_autocmd('+');
1154 }
1155# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156#endif
1157
1158 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 setmouse();
1160 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001162 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001163 curbuf->b_visual.vi_mode = VIsual_mode;
1164 curbuf->b_visual.vi_start = VIsual;
1165 curbuf->b_visual.vi_end = curwin->w_cursor;
1166 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167#ifdef FEAT_EVAL
1168 curbuf->b_visual_mode_eval = VIsual_mode;
1169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 if (!virtual_active())
1171 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001172 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001174 adjust_cursor_eol();
LemonBoy2bf52dd2022-04-09 18:17:34 +01001175 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176}
1177
1178/*
1179 * Reset VIsual_active and VIsual_reselect.
1180 */
1181 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001182reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183{
1184 if (VIsual_active)
1185 {
1186 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001187 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 }
1189 VIsual_reselect = FALSE;
1190}
1191
1192/*
1193 * Reset VIsual_active and VIsual_reselect if it's set.
1194 */
1195 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001196reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197{
1198 if (VIsual_active)
1199 {
1200 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001201 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 VIsual_reselect = FALSE;
1203 }
1204}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001206 void
1207restore_visual_mode(void)
1208{
1209 if (VIsual_mode_orig != NUL)
1210 {
1211 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1212 VIsual_mode_orig = NUL;
1213 }
1214}
1215
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216/*
1217 * Check for a balloon-eval special item to include when searching for an
1218 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1219 * Returns TRUE if the character at "*ptr" should be included.
1220 * "dir" is FORWARD or BACKWARD, the direction of searching.
1221 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1222 * "bnp" points to a counter for square brackets.
1223 */
1224 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001225find_is_eval_item(
1226 char_u *ptr,
1227 int *colp,
1228 int *bnp,
1229 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001231 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1233 ++*bnp;
1234 if (*bnp > 0)
1235 {
1236 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1237 --*bnp;
1238 return TRUE;
1239 }
1240
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001241 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 if (*ptr == '.')
1243 return TRUE;
1244
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001245 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1247 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1248 {
1249 *colp += dir;
1250 return TRUE;
1251 }
1252 return FALSE;
1253}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254
1255/*
1256 * Find the identifier under or to the right of the cursor.
1257 * "find_type" can have one of three values:
1258 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001259 * FIND_STRING: find any non-white text
1260 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001261 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 *
1263 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001264 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001266 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 * This doesn't match the real Vi but I like it a little better and it
1268 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001269 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 * When FIND_IDENT isn't defined, we backup until a blank.
1271 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001272 * Returns the length of the text, or zero if no text is found.
1273 * If text is found, a pointer to the text is put in "*text". This
1274 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 */
1276 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001277find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278{
1279 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001280 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281}
1282
1283/*
1284 * Like find_ident_under_cursor(), but for any window and any position.
1285 * However: Uses 'iskeyword' from the current window!.
1286 */
1287 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001288find_ident_at_pos(
1289 win_T *wp,
1290 linenr_T lnum,
1291 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001292 char_u **text,
1293 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001294 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295{
1296 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001297 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 int this_class = 0;
1300 int prev_class;
1301 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001302 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001304 // if i == 0: try to find an identifier
1305 // if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1307 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1308 {
1309 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001310 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 */
1312 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 if (has_mbyte)
1314 {
1315 while (ptr[col] != NUL)
1316 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001317 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1319 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 this_class = mb_get_class(ptr + col);
1321 if (this_class != 0 && (i == 1 || this_class != 1))
1322 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001323 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 }
1325 }
1326 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001328 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 )
1331 ++col;
1332
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001333 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
1336 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001337 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 if (has_mbyte)
1340 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001341 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1343 this_class = mb_get_class((char_u *)"a");
1344 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001346 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 {
1348 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1349 prev_class = mb_get_class(ptr + prevcol);
1350 if (this_class != prev_class
1351 && (i == 0
1352 || prev_class == 0
1353 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 && (!(find_type & FIND_EVAL)
1355 || prevcol == 0
1356 || !find_is_eval_item(ptr + prevcol, &prevcol,
1357 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 )
1359 break;
1360 col = prevcol;
1361 }
1362
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001363 // If we don't want just any old text, or we've found an
1364 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 if (this_class > 2)
1366 this_class = 2;
1367 if (!(find_type & FIND_STRING) || this_class == 2)
1368 break;
1369 }
1370 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 {
1372 while (col > 0
1373 && ((i == 0
1374 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001375 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 && (!(find_type & FIND_IDENT)
1377 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 || ((find_type & FIND_EVAL)
1379 && col > 1
1380 && find_is_eval_item(ptr + col - 1, &col,
1381 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 ))
1383 --col;
1384
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001385 // If we don't want just any old text, or we've found an
1386 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1388 break;
1389 }
1390 }
1391
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001392 if (ptr[col] == NUL || (i == 0
1393 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001395 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001396 if ((find_type & FIND_NOERROR) == 0)
1397 {
1398 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001399 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001400 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001401 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 return 0;
1404 }
1405 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001406 *text = ptr;
1407 if (textcol != NULL)
1408 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409
1410 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001411 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 bn = 0;
1414 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 if (has_mbyte)
1417 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001418 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 this_class = mb_get_class(ptr);
1420 while (ptr[col] != NUL
1421 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1422 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 || ((find_type & FIND_EVAL)
1424 && col <= (int)startcol
1425 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001427 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 }
1429 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001431 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 || ((find_type & FIND_EVAL)
1433 && col <= (int)startcol
1434 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437
1438 return col;
1439}
1440
1441/*
1442 * Prepare for redo of a normal command.
1443 */
1444 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001445prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446{
1447 prep_redo(cap->oap->regname, cap->count0,
1448 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1449}
1450
1451/*
1452 * Prepare for redo of any command.
1453 * Note that only the last argument can be a multi-byte char.
1454 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001455 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001456prep_redo(
1457 int regname,
1458 long num,
1459 int cmd1,
1460 int cmd2,
1461 int cmd3,
1462 int cmd4,
1463 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001465 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1466}
1467
1468/*
1469 * Prepare for redo of any command with extra count after "cmd2".
1470 */
1471 void
1472prep_redo_num2(
1473 int regname,
1474 long num1,
1475 int cmd1,
1476 int cmd2,
1477 long num2,
1478 int cmd3,
1479 int cmd4,
1480 int cmd5)
1481{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 ResetRedobuff();
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001483
1484#ifdef FEAT_EVAL
1485 // Put info about a mapping in the redo buffer, so that "." will use the
1486 // same script context.
1487 may_add_last_used_map_to_redobuff();
1488#endif
1489
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001490 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 {
1492 AppendCharToRedobuff('"');
1493 AppendCharToRedobuff(regname);
1494 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001495 if (num1 != 0)
1496 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 if (cmd1 != NUL)
1498 AppendCharToRedobuff(cmd1);
1499 if (cmd2 != NUL)
1500 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001501 if (num2 != 0)
1502 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 if (cmd3 != NUL)
1504 AppendCharToRedobuff(cmd3);
1505 if (cmd4 != NUL)
1506 AppendCharToRedobuff(cmd4);
1507 if (cmd5 != NUL)
1508 AppendCharToRedobuff(cmd5);
1509}
1510
1511/*
zeertzjq73916ba2023-04-26 16:50:19 +01001512 * Check for operator active and clear it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 *
zeertzjq73916ba2023-04-26 16:50:19 +01001514 * Beep and return TRUE if an operator was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 */
1516 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001517checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
1519 if (oap->op_type == OP_NOP)
1520 return FALSE;
1521 clearopbeep(oap);
1522 return TRUE;
1523}
1524
1525/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001526 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 *
zeertzjq73916ba2023-04-26 16:50:19 +01001528 * Beep and return TRUE if an operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 */
1530 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001531checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001533 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 return FALSE;
1535 clearopbeep(oap);
1536 return TRUE;
1537}
1538
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001539 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001540clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541{
1542 oap->op_type = OP_NOP;
1543 oap->regname = 0;
1544 oap->motion_force = NUL;
1545 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001546 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547}
1548
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001549 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001550clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551{
1552 clearop(oap);
1553 beep_flush();
1554}
1555
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556/*
1557 * Remove the shift modifier from a special key.
1558 */
1559 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001560unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561{
1562 switch (cap->cmdchar)
1563 {
1564 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1565 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1566 case K_S_UP: cap->cmdchar = K_UP; break;
1567 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1568 case K_S_HOME: cap->cmdchar = K_HOME; break;
1569 case K_S_END: cap->cmdchar = K_END; break;
1570 }
1571 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1572}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001574/*
1575 * If the mode is currently displayed clear the command line or update the
1576 * command displayed.
1577 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001578 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001579may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001580{
1581 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001582 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001583 else
1584 clear_showcmd();
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001585}
1586
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587/*
1588 * Routines for displaying a partly typed command
1589 */
1590
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001591static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592static int showcmd_is_clear = TRUE;
1593static int showcmd_visual = FALSE;
1594
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001595static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596
1597 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001598clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599{
1600 if (!p_sc)
1601 return;
1602
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 if (VIsual_active && !char_avail())
1604 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001605 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 long lines;
1607 colnr_T leftcol, rightcol;
1608 linenr_T top, bot;
1609
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001610 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001611 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 {
1613 top = VIsual.lnum;
1614 bot = curwin->w_cursor.lnum;
1615 }
1616 else
1617 {
1618 top = curwin->w_cursor.lnum;
1619 bot = VIsual.lnum;
1620 }
1621# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001622 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001623 (void)hasFolding(top, &top, NULL);
1624 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625# endif
1626 lines = bot - top + 1;
1627
1628 if (VIsual_mode == Ctrl_V)
1629 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001630# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001631 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001632 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001633
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001634 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001635 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001636 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001637# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001639# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001640 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001641 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001642# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1644 (long)(rightcol - leftcol + 1));
1645 }
1646 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1647 sprintf((char *)showcmd_buf, "%ld", lines);
1648 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001649 {
1650 char_u *s, *e;
1651 int l;
1652 int bytes = 0;
1653 int chars = 0;
1654
1655 if (cursor_bot)
1656 {
1657 s = ml_get_pos(&VIsual);
1658 e = ml_get_cursor();
1659 }
1660 else
1661 {
1662 s = ml_get_cursor();
1663 e = ml_get_pos(&VIsual);
1664 }
1665 while ((*p_sel != 'e') ? s <= e : s < e)
1666 {
1667 l = (*mb_ptr2len)(s);
1668 if (l == 0)
1669 {
1670 ++bytes;
1671 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001672 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001673 }
1674 bytes += l;
1675 ++chars;
1676 s += l;
1677 }
1678 if (bytes == chars)
1679 sprintf((char *)showcmd_buf, "%d", chars);
1680 else
1681 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1682 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001683 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 showcmd_visual = TRUE;
1685 }
1686 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 {
1688 showcmd_buf[0] = NUL;
1689 showcmd_visual = FALSE;
1690
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001691 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 if (showcmd_is_clear)
1693 return;
1694 }
1695
1696 display_showcmd();
1697}
1698
1699/*
1700 * Add 'c' to string of shown command chars.
1701 * Return TRUE if output has been written (and setcursor() has been called).
1702 */
1703 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001704add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705{
1706 char_u *p;
1707 int old_len;
1708 int extra_len;
1709 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 int i;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001711 char_u mbyte_buf[MB_MAXBYTES];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 static int ignore[] =
1713 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001714#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1716 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001717#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001718 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001719 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1721 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001722 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001724 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 0
1726 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
Bram Moolenaar09df3122006-01-23 22:23:09 +00001728 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 return FALSE;
1730
1731 if (showcmd_visual)
1732 {
1733 showcmd_buf[0] = NUL;
1734 showcmd_visual = FALSE;
1735 }
1736
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001737 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 if (IS_SPECIAL(c))
1739 for (i = 0; ignore[i] != 0; ++i)
1740 if (ignore[i] == c)
1741 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742
zeertzjqacdfb8a2024-04-17 21:28:54 +02001743 if (c <= 0x7f || !vim_isprintc(c))
1744 {
1745 p = transchar(c);
1746 if (*p == ' ')
1747 STRCPY(p, "<20>");
1748 }
1749 else
1750 {
1751 mbyte_buf[(*mb_char2bytes)(c, mbyte_buf)] = NUL;
1752 p = mbyte_buf;
1753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 old_len = (int)STRLEN(showcmd_buf);
1755 extra_len = (int)STRLEN(p);
1756 overflow = old_len + extra_len - SHOWCMD_COLS;
1757 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001758 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1759 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 STRCAT(showcmd_buf, p);
1761
1762 if (char_avail())
1763 return FALSE;
1764
1765 display_showcmd();
1766
1767 return TRUE;
1768}
1769
1770 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001771add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772{
1773 if (!add_to_showcmd(c))
1774 setcursor();
1775}
1776
1777/*
1778 * Delete 'len' characters from the end of the shown command.
1779 */
1780 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001781del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782{
1783 int old_len;
1784
1785 if (!p_sc)
1786 return;
1787
1788 old_len = (int)STRLEN(showcmd_buf);
1789 if (len > old_len)
1790 len = old_len;
1791 showcmd_buf[old_len - len] = NUL;
1792
1793 if (!char_avail())
1794 display_showcmd();
1795}
1796
1797/*
1798 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1799 * something and there is a partial mapping.
1800 */
1801 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001802push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803{
1804 if (p_sc)
1805 STRCPY(old_showcmd_buf, showcmd_buf);
1806}
1807
1808 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001809pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810{
1811 if (!p_sc)
1812 return;
1813
1814 STRCPY(showcmd_buf, old_showcmd_buf);
1815
1816 display_showcmd();
1817}
1818
1819 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001820display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821{
zeertzjqacdfb8a2024-04-17 21:28:54 +02001822 int len = vim_strsize(showcmd_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823
Luuk van Baalba936f62022-12-15 13:15:39 +00001824 showcmd_is_clear = (len == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 cursor_off();
1826
Luuk van Baalba936f62022-12-15 13:15:39 +00001827 if (*p_sloc == 's')
Luuk van Baalaa7f25e2023-04-13 21:47:38 +01001828 {
1829 if (showcmd_is_clear)
1830 curwin->w_redr_status = TRUE;
1831 else
1832 win_redr_status(curwin, FALSE);
1833 }
Luuk van Baalba936f62022-12-15 13:15:39 +00001834 else if (*p_sloc == 't')
Luuk van Baalaa7f25e2023-04-13 21:47:38 +01001835 {
1836 if (showcmd_is_clear)
1837 redraw_tabline = TRUE;
1838 else
1839 draw_tabline();
1840 }
Luuk van Baalba936f62022-12-15 13:15:39 +00001841 else // 'showcmdloc' is "last" or empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 {
Luuk van Baalba936f62022-12-15 13:15:39 +00001843 if (!showcmd_is_clear)
1844 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845
Luuk van Baalba936f62022-12-15 13:15:39 +00001846 // clear the rest of an old message by outputting up to SHOWCMD_COLS
1847 // spaces
1848 screen_puts((char_u *)" " + len,
1849 (int)Rows - 1, sc_col + len, 0);
1850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001852 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855/*
1856 * When "check" is FALSE, prepare for commands that scroll the window.
1857 * When "check" is TRUE, take care of scroll-binding after the window has
1858 * scrolled. Called from normal_cmd() and edit().
1859 */
1860 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001861do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862{
1863 static win_T *old_curwin = NULL;
1864 static linenr_T old_topline = 0;
1865#ifdef FEAT_DIFF
1866 static int old_topfill = 0;
1867#endif
1868 static buf_T *old_buf = NULL;
1869 static colnr_T old_leftcol = 0;
1870
1871 if (check && curwin->w_p_scb)
1872 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001873 // If a ":syncbind" command was just used, don't scroll, only reset
1874 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 if (did_syncbind)
1876 did_syncbind = FALSE;
1877 else if (curwin == old_curwin)
1878 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001879 // Synchronize other windows, as necessary according to
1880 // 'scrollbind'. Don't do this after an ":edit" command, except
1881 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if ((curwin->w_buffer == old_buf
1883#ifdef FEAT_DIFF
1884 || curwin->w_p_diff
1885#endif
1886 )
1887 && (curwin->w_topline != old_topline
1888#ifdef FEAT_DIFF
1889 || curwin->w_topfill != old_topfill
1890#endif
1891 || curwin->w_leftcol != old_leftcol))
1892 {
1893 check_scrollbind(curwin->w_topline - old_topline,
1894 (long)(curwin->w_leftcol - old_leftcol));
1895 }
1896 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001897 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001899 // When switching between windows, make sure that the relative
1900 // vertical offset is valid for the new window. The relative
1901 // offset is invalid whenever another 'scrollbind' window has
1902 // scrolled to a point that would force the current window to
1903 // scroll past the beginning or end of its buffer. When the
1904 // resync is performed, some of the other 'scrollbind' windows may
1905 // need to jump so that the current window's relative position is
1906 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
1908 }
1909 curwin->w_scbind_pos = curwin->w_topline;
1910 }
1911
1912 old_curwin = curwin;
1913 old_topline = curwin->w_topline;
1914#ifdef FEAT_DIFF
1915 old_topfill = curwin->w_topfill;
1916#endif
1917 old_buf = curwin->w_buffer;
1918 old_leftcol = curwin->w_leftcol;
1919}
1920
1921/*
1922 * Synchronize any windows that have "scrollbind" set, based on the
1923 * number of rows by which the current window has changed
1924 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
1925 */
1926 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001927check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928{
1929 int want_ver;
1930 int want_hor;
1931 win_T *old_curwin = curwin;
1932 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 int old_VIsual_select = VIsual_select;
1934 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 colnr_T tgt_leftcol = curwin->w_leftcol;
1936 long topline;
1937 long y;
1938
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001939 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
1941#ifdef FEAT_DIFF
1942 want_ver |= old_curwin->w_p_diff;
1943#endif
1944 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
1945
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001946 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001948 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 {
1950 curbuf = curwin->w_buffer;
zeertzjq101d57b2022-07-31 18:34:32 +01001951 // skip original window and windows with 'noscrollbind'
1952 if (curwin == old_curwin || !curwin->w_p_scb)
1953 continue;
1954
1955 // do the vertical scroll
1956 if (want_ver)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958#ifdef FEAT_DIFF
zeertzjq101d57b2022-07-31 18:34:32 +01001959 if (old_curwin->w_p_diff && curwin->w_p_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 {
zeertzjq101d57b2022-07-31 18:34:32 +01001961 diff_set_topline(old_curwin, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 }
zeertzjq101d57b2022-07-31 18:34:32 +01001963 else
1964#endif
1965 {
1966 curwin->w_scbind_pos += topline_diff;
1967 topline = curwin->w_scbind_pos;
1968 if (topline > curbuf->b_ml.ml_line_count)
1969 topline = curbuf->b_ml.ml_line_count;
1970 if (topline < 1)
1971 topline = 1;
1972
1973 y = topline - curwin->w_topline;
1974 if (y > 0)
1975 scrollup(y, FALSE);
1976 else
1977 scrolldown(-y, FALSE);
1978 }
1979
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001980 redraw_later(UPD_VALID);
zeertzjq101d57b2022-07-31 18:34:32 +01001981 cursor_correct();
1982 curwin->w_redr_status = TRUE;
1983 }
1984
1985 // do the horizontal scroll
Bram Moolenaar0c34d562022-11-18 14:07:20 +00001986 if (want_hor)
1987 (void)set_leftcol(tgt_leftcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 }
1989
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001990 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 VIsual_select = old_VIsual_select;
1992 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 curwin = old_curwin;
1994 curbuf = old_curbuf;
1995}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996
1997/*
1998 * Command character that's ignored.
1999 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02002000 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002003nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002005 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006}
2007
2008/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002009 * Command character that doesn't do anything, but unlike nv_ignore() does
2010 * start edit(). Used for "startinsert" executed while starting up.
2011 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002012 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002013nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002014{
2015}
2016
2017/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 * Command character doesn't exist.
2019 */
2020 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002021nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022{
2023 clearopbeep(cap->oap);
2024}
2025
2026/*
2027 * <Help> and <F1> commands.
2028 */
2029 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002030nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031{
2032 if (!checkclearopq(cap->oap))
2033 ex_help(NULL);
2034}
2035
2036/*
2037 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2038 */
2039 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002040nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002042#ifdef FEAT_JOB_CHANNEL
2043 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2044 clearopbeep(cap->oap);
2045 else
2046#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002047 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002048 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002049 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002050 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2051 op_addsub(cap->oap, cap->count1, cap->arg);
2052 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002053 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002054 else if (VIsual_active)
2055 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002056 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002057 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058}
2059
2060/*
2061 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2062 */
2063 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002064nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002066 if (checkclearop(cap->oap))
2067 return;
2068
2069 if (mod_mask & MOD_MASK_CTRL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002070 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002071 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
2072 if (cap->arg == BACKWARD)
2073 goto_tabpage(-(int)cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002074 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002075 goto_tabpage((int)cap->count0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002076 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002077 else
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01002078 (void)pagescroll(cap->arg, cap->count1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079}
2080
2081/*
2082 * Implementation of "gd" and "gD" command.
2083 */
2084 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002085nv_gd(
2086 oparg_T *oap,
2087 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002088 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089{
2090 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 char_u *ptr;
2092
Bram Moolenaard9d30582005-05-18 22:10:28 +00002093 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002094 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002095 == FAIL)
2096 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 clearopbeep(oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002098 return;
Bram Moolenaar0c711142021-11-12 10:30:04 +00002099 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002100
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002101#ifdef FEAT_FOLDING
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002102 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2103 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002104#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002105 // clear any search statistics
2106 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2107 clear_cmdline = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002108}
2109
2110/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002111 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2112 * otherwise.
2113 */
2114 static int
2115is_ident(char_u *line, int offset)
2116{
2117 int i;
2118 int incomment = FALSE;
2119 int instring = 0;
2120 int prev = 0;
2121
2122 for (i = 0; i < offset && line[i] != NUL; i++)
2123 {
2124 if (instring != 0)
2125 {
2126 if (prev != '\\' && line[i] == instring)
2127 instring = 0;
2128 }
2129 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2130 {
2131 instring = line[i];
2132 }
2133 else
2134 {
2135 if (incomment)
2136 {
2137 if (prev == '*' && line[i] == '/')
2138 incomment = FALSE;
2139 }
2140 else if (prev == '/' && line[i] == '*')
2141 {
2142 incomment = TRUE;
2143 }
2144 else if (prev == '/' && line[i] == '/')
2145 {
2146 return FALSE;
2147 }
2148 }
2149
2150 prev = line[i];
2151 }
2152
2153 return incomment == FALSE && instring == 0;
2154}
2155
2156/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002157 * Search for variable declaration of "ptr[len]".
2158 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2159 * current file ("gD").
2160 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002161 * Return FAIL when not found.
2162 */
2163 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002164find_decl(
2165 char_u *ptr,
2166 int len,
2167 int locally,
2168 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002169 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002170{
2171 char_u *pat;
2172 pos_T old_pos;
2173 pos_T par_pos;
2174 pos_T found_pos;
2175 int t;
2176 int save_p_ws;
2177 int save_p_scs;
2178 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002179 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002180 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002181 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002182
2183 if ((pat = alloc(len + 7)) == NULL)
2184 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002185
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002186 // Put "\V" before the pattern to avoid that the special meaning of "."
2187 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002188 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2189 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 old_pos = curwin->w_cursor;
2191 save_p_ws = p_ws;
2192 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002193 p_ws = FALSE; // don't wrap around end of file now
2194 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002196 // With "gD" go to line 1.
2197 // With "gd" Search back for the start of the current function, then go
2198 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002199 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002201 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002203 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 }
2205 else
2206 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002207 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2209 --curwin->w_cursor.lnum;
2210 }
2211 curwin->w_cursor.col = 0;
2212
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002213 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002214 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002215 for (;;)
2216 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002217 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002218 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002219 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002220 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002221
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002222 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002223 {
2224 pos_T *pos;
2225
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002226 // Check that the block the match is in doesn't end before the
2227 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002228 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2229 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2230 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002231 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002232 // There can't be a useful match before the end of this block.
2233 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002234 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002235 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002236 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002237 }
2238
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002239 if (t == FAIL)
2240 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002241 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002242 if (found_pos.lnum != 0)
2243 {
2244 curwin->w_cursor = found_pos;
2245 t = OK;
2246 }
2247 break;
2248 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002249 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002250 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002251 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002252 ++curwin->w_cursor.lnum;
2253 curwin->w_cursor.col = 0;
2254 continue;
2255 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002256 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2257
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002258 // If the current position is not a valid identifier and a previous
2259 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002260 if (!valid && found_pos.lnum != 0)
2261 {
2262 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002263 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002264 }
2265
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002266 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002267 if (valid && !locally)
2268 break;
2269 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002270 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002271 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002272 if (found_pos.lnum != 0)
2273 curwin->w_cursor = found_pos;
2274 break;
2275 }
2276
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002277 // For finding a local variable and the match is before the "{" or
2278 // inside a comment, continue searching. For K&R style function
2279 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002280 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002281 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002282 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002283 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002284 // Remove SEARCH_START from flags to avoid getting stuck at one
2285 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002286 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002288
2289 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002291 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 curwin->w_cursor = old_pos;
2293 }
2294 else
2295 {
2296 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002297 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 reset_search_dir();
2299 }
2300
2301 vim_free(pat);
2302 p_ws = save_p_ws;
2303 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002304
2305 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306}
2307
2308/*
2309 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2310 * lines rather than lines in the file.
2311 * 'dist' must be positive.
2312 *
2313 * Return OK if able to move cursor, FAIL otherwise.
2314 */
Luuk van Baalcb204e62024-04-02 20:49:45 +02002315 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002316nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317{
Dylan Thacker-Smithb2d124c2024-03-24 09:43:25 +01002318
2319 int linelen = linetabsize_no_outer(curwin, curwin->w_cursor.lnum);
2320
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 int retval = OK;
2322 int atend = FALSE;
2323 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002324 int col_off1; // margin offset for first screen line
2325 int col_off2; // margin offset for wrapped screen line
2326 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002327 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328
2329 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002330 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331
2332 col_off1 = curwin_col_off();
2333 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002334 width1 = curwin->w_width - col_off1;
2335 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002336 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002337 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002340 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002341 // Instead of sticking at the last character of the buffer line we
2342 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 if (curwin->w_curswant == MAXCOL)
2344 {
2345 atend = TRUE;
2346 validate_virtcol();
2347 if (width1 <= 0)
2348 curwin->w_curswant = 0;
2349 else
2350 {
2351 curwin->w_curswant = width1 - 1;
2352 if (curwin->w_virtcol > curwin->w_curswant)
2353 curwin->w_curswant += ((curwin->w_virtcol
2354 - curwin->w_curswant - 1) / width2 + 1) * width2;
2355 }
2356 }
2357 else
2358 {
2359 if (linelen > width1)
2360 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2361 else
2362 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002363 if (curwin->w_curswant >= (colnr_T)n)
2364 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 }
2366
2367 while (dist--)
2368 {
2369 if (dir == BACKWARD)
2370 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002371 if ((long)curwin->w_curswant >= width1
2372#ifdef FEAT_FOLDING
2373 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2374#endif
2375 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002376 // Move back within the line. This can give a negative value
2377 // for w_curswant if width1 < width2 (with cpoptions+=n),
2378 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 curwin->w_curswant -= width2;
2380 else
2381 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002382 // to previous line
Luuk van Baala109f392023-06-02 14:16:35 +01002383 if (curwin->w_cursor.lnum <= 1)
Bram Moolenaare71996b2021-01-21 17:03:07 +01002384 {
2385 retval = FAIL;
2386 break;
2387 }
Luuk van Baala109f392023-06-02 14:16:35 +01002388 cursor_up_inner(curwin, 1);
2389
Dylan Thacker-Smithb2d124c2024-03-24 09:43:25 +01002390 linelen = linetabsize_no_outer(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 if (linelen > width1)
2392 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2393 + 1) * width2;
2394 }
2395 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002396 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {
2398 if (linelen > width1)
2399 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2400 else
2401 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002402 if (curwin->w_curswant + width2 < (colnr_T)n
2403#ifdef FEAT_FOLDING
2404 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2405#endif
2406 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002407 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 curwin->w_curswant += width2;
2409 else
2410 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002411 // to next line
Luuk van Baala109f392023-06-02 14:16:35 +01002412 if (curwin->w_cursor.lnum
2413 >= curwin->w_buffer->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 {
2415 retval = FAIL;
2416 break;
2417 }
Luuk van Baala109f392023-06-02 14:16:35 +01002418 cursor_down_inner(curwin, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 curwin->w_curswant %= width2;
Luuk van Baala109f392023-06-02 14:16:35 +01002420
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002421 // Check if the cursor has moved below the number display
2422 // when width1 < width2 (with cpoptions+=n). Subtract width2
2423 // to get a negative value for w_curswant, which will get
2424 // clipped to column 0.
2425 if (curwin->w_curswant >= width1)
2426 curwin->w_curswant -= width2;
Dylan Thacker-Smithb2d124c2024-03-24 09:43:25 +01002427 linelen = linetabsize_no_outer(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 }
2429 }
2430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002433 if (virtual_active() && atend)
2434 coladvance(MAXCOL);
2435 else
2436 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2439 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002440 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002441 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002442
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002443 // Check for landing on a character that got split at the end of the
2444 // last line. We want to advance a screenline, not end up in the same
2445 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002447 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002448#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002449 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2450 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002451#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002452
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002453 c = (*mb_ptr2char)(ml_get_cursor());
2454 if (dir == FORWARD && virtcol < curwin->w_curswant
2455 && (curwin->w_curswant <= (colnr_T)width1)
2456 && !vim_isprintc(c) && c > 255)
2457 oneright();
2458
Bram Moolenaar773b1582014-08-29 14:20:51 +02002459 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 && (curwin->w_curswant < (colnr_T)width1
2461 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2462 : ((curwin->w_curswant - width1) % width2
2463 > (colnr_T)width2 / 2)))
2464 --curwin->w_cursor.col;
2465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
2467 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002468 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002469 adjust_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470
2471 return retval;
2472}
2473
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474/*
2475 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2476 * cap->arg must be TRUE for CTRL-E.
2477 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002478 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002479nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480{
2481 if (!checkclearop(cap->oap))
2482 scroll_redraw(cap->arg, cap->count1);
2483}
2484
2485/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002486 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2487 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2488 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002489 */
2490 static int
2491nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2492{
2493 int nchar = *nchar_arg;
2494 long n;
2495
2496 // "z123{nchar}": edit the count before obtaining {nchar}
2497 if (checkclearop(cap->oap))
2498 return FALSE;
2499 n = nchar - '0';
2500
2501 for (;;)
2502 {
2503#ifdef USE_ON_FLY_SCROLL
2504 dont_scroll = TRUE; // disallow scrolling here
2505#endif
2506 ++no_mapping;
2507 ++allow_keys; // no mapping for nchar, but allow key codes
2508 nchar = plain_vgetc();
2509 LANGMAP_ADJUST(nchar, TRUE);
2510 --no_mapping;
2511 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002512 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002513
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002514 if (nchar == K_DEL || nchar == K_KDEL)
2515 n /= 10;
2516 else if (VIM_ISDIGIT(nchar))
Christian Brabandt58f9bef2023-11-14 21:02:30 +01002517 {
Christian Brabandt22cbc8a2023-11-19 10:47:21 +01002518 if (vim_append_digit_long(&n, nchar - '0') == FAIL)
Christian Brabandt58f9bef2023-11-14 21:02:30 +01002519 {
2520 clearopbeep(cap->oap);
2521 break;
2522 }
Christian Brabandt58f9bef2023-11-14 21:02:30 +01002523 }
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002524 else if (nchar == CAR)
2525 {
2526#ifdef FEAT_GUI
2527 need_mouse_correct = TRUE;
2528#endif
2529 win_setheight((int)n);
2530 break;
2531 }
2532 else if (nchar == 'l'
2533 || nchar == 'h'
2534 || nchar == K_LEFT
2535 || nchar == K_RIGHT)
2536 {
2537 cap->count1 = n ? n * cap->count1 : cap->count1;
2538 *nchar_arg = nchar;
2539 return TRUE;
2540 }
2541 else
2542 {
2543 clearopbeep(cap->oap);
2544 break;
2545 }
2546 }
2547 cap->oap->op_type = OP_NOP;
2548 return FALSE;
2549}
2550
2551#ifdef FEAT_SPELL
2552/*
2553 * "zug" and "zuw": undo "zg" and "zw"
2554 * "zg": add good word to word list
2555 * "zw": add wrong word to word list
2556 * "zG": add good word to temp word list
2557 * "zW": add wrong word to temp word list
2558 */
2559 static int
2560nv_zg_zw(cmdarg_T *cap, int nchar)
2561{
2562 char_u *ptr = NULL;
2563 int len;
2564 int undo = FALSE;
2565
2566 if (nchar == 'u')
2567 {
2568 ++no_mapping;
2569 ++allow_keys; // no mapping for nchar, but allow key codes
2570 nchar = plain_vgetc();
2571 LANGMAP_ADJUST(nchar, TRUE);
2572 --no_mapping;
2573 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002574 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002575
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002576 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2577 {
2578 clearopbeep(cap->oap);
2579 return OK;
2580 }
2581 undo = TRUE;
2582 }
2583
2584 if (checkclearop(cap->oap))
2585 return OK;
2586 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2587 return FAIL;
2588 if (ptr == NULL)
2589 {
2590 pos_T pos = curwin->w_cursor;
2591
2592 // Find bad word under the cursor. When 'spell' is
2593 // off this fails and find_ident_under_cursor() is
2594 // used below.
2595 emsg_off++;
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02002596 len = spell_move_to(curwin, FORWARD, SMT_ALL, TRUE, NULL);
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002597 emsg_off--;
2598 if (len != 0 && curwin->w_cursor.col <= pos.col)
2599 ptr = ml_get_pos(&curwin->w_cursor);
2600 curwin->w_cursor = pos;
2601 }
2602
2603 if (ptr == NULL
2604 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2605 return FAIL;
2606 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2607 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2608 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2609
2610 return OK;
2611}
2612#endif
2613
2614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 * Commands that start with "z".
2616 */
2617 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002618nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619{
2620 long n;
2621 colnr_T col;
2622 int nchar = cap->nchar;
2623#ifdef FEAT_FOLDING
2624 long old_fdl = curwin->w_p_fdl;
2625 int old_fen = curwin->w_p_fen;
2626#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002627 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002629 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 if (
2633#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002634 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2635 // and "zC" only in Visual mode. "zj" and "zk" are motion
2636 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 cap->nchar != 'f' && cap->nchar != 'F'
2638 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2639 && cap->nchar != 'j' && cap->nchar != 'k'
2640 &&
2641#endif
2642 checkclearop(cap->oap))
2643 return;
2644
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002645 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2646 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2648 && cap->count0
2649 && cap->count0 != curwin->w_cursor.lnum)
2650 {
2651 setpcmark();
2652 if (cap->count0 > curbuf->b_ml.ml_line_count)
2653 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2654 else
2655 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002656 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 }
2658
2659 switch (nchar)
2660 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002661 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 case '+':
2663 if (cap->count0 == 0)
2664 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002665 // No count given: put cursor at the line below screen
2666 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2668 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2669 else
2670 curwin->w_cursor.lnum = curwin->w_botline;
2671 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002672 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 case NL:
2674 case CAR:
2675 case K_KENTER:
2676 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002677 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678
2679 case 't': scroll_cursor_top(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002680 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002681 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 break;
2683
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002684 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002686 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002688 case 'z': scroll_cursor_halfway(TRUE, FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002689 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002690 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 break;
2692
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002693 // "z^", "z-" and "zb": put cursor at bottom of screen
2694 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2695 // when <count> is at bottom of window, and puts that one at
2696 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 if (cap->count0 != 0)
2698 {
2699 scroll_cursor_bot(0, TRUE);
2700 curwin->w_cursor.lnum = curwin->w_topline;
2701 }
2702 else if (curwin->w_topline == 1)
2703 curwin->w_cursor.lnum = 1;
2704 else
2705 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002706 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 case '-':
2708 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002709 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710
2711 case 'b': scroll_cursor_bot(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002712 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002713 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 break;
2715
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002716 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002718 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002719 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002721 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 case 'h':
2723 case K_LEFT:
2724 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002725 (void)set_leftcol((colnr_T)cap->count1 > curwin->w_leftcol
2726 ? 0 : curwin->w_leftcol - (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 break;
2728
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002729 // "zL" - scroll window left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002730 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002731 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002733 // "zl" - scroll window to the left if not wrapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 case 'l':
2735 case K_RIGHT:
2736 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002737 (void)set_leftcol(curwin->w_leftcol + (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 break;
2739
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002740 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 case 's': if (!curwin->w_p_wrap)
2742 {
2743#ifdef FEAT_FOLDING
2744 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002745 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 else
2747#endif
2748 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002749 if ((long)col > siso)
2750 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 else
2752 col = 0;
2753 if (curwin->w_leftcol != col)
2754 {
2755 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002756 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 }
2758 }
2759 break;
2760
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002761 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 case 'e': if (!curwin->w_p_wrap)
2763 {
2764#ifdef FEAT_FOLDING
2765 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002766 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 else
2768#endif
2769 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002770 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002771 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 col = 0;
2773 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002774 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 if (curwin->w_leftcol != col)
2776 {
2777 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002778 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 }
2780 }
2781 break;
2782
Christian Brabandt2fa93842021-05-30 22:17:25 +02002783 // "zp", "zP" in block mode put without addind trailing spaces
2784 case 'P':
2785 case 'p': nv_put(cap);
2786 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002787 // "zy" Yank without trailing spaces
2788 case 'y': nv_operator(cap);
2789 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002791 // "zF": create fold command
2792 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 case 'F':
2794 case 'f': if (foldManualAllowed(TRUE))
2795 {
2796 cap->nchar = 'f';
2797 nv_operator(cap);
2798 curwin->w_p_fen = TRUE;
2799
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002800 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2802 {
2803 nv_operator(cap);
2804 finish_op = TRUE;
2805 }
2806 }
2807 else
2808 clearopbeep(cap->oap);
2809 break;
2810
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002811 // "zd": delete fold at cursor
2812 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 case 'd':
2814 case 'D': if (foldManualAllowed(FALSE))
2815 {
2816 if (VIsual_active)
2817 nv_operator(cap);
2818 else
2819 deleteFold(curwin->w_cursor.lnum,
2820 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2821 }
2822 break;
2823
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002824 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 case 'E': if (foldmethodIsManual(curwin))
2826 {
2827 clearFolding(curwin);
2828 changed_window_setting();
2829 }
2830 else if (foldmethodIsMarker(curwin))
2831 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2832 TRUE, FALSE);
2833 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002834 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 break;
2836
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002837 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 case 'n': curwin->w_p_fen = FALSE;
2839 break;
2840
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002841 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 case 'N': curwin->w_p_fen = TRUE;
2843 break;
2844
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002845 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2847 break;
2848
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002849 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2851 openFold(curwin->w_cursor.lnum, cap->count1);
2852 else
2853 {
2854 closeFold(curwin->w_cursor.lnum, cap->count1);
2855 curwin->w_p_fen = TRUE;
2856 }
2857 break;
2858
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002859 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2861 openFoldRecurse(curwin->w_cursor.lnum);
2862 else
2863 {
2864 closeFoldRecurse(curwin->w_cursor.lnum);
2865 curwin->w_p_fen = TRUE;
2866 }
2867 break;
2868
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002869 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 case 'o': if (VIsual_active)
2871 nv_operator(cap);
2872 else
2873 openFold(curwin->w_cursor.lnum, cap->count1);
2874 break;
2875
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002876 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 case 'O': if (VIsual_active)
2878 nv_operator(cap);
2879 else
2880 openFoldRecurse(curwin->w_cursor.lnum);
2881 break;
2882
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002883 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 case 'c': if (VIsual_active)
2885 nv_operator(cap);
2886 else
2887 closeFold(curwin->w_cursor.lnum, cap->count1);
2888 curwin->w_p_fen = TRUE;
2889 break;
2890
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002891 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 case 'C': if (VIsual_active)
2893 nv_operator(cap);
2894 else
2895 closeFoldRecurse(curwin->w_cursor.lnum);
2896 curwin->w_p_fen = TRUE;
2897 break;
2898
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002899 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 case 'v': foldOpenCursor();
2901 break;
2902
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002903 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002905 curwin->w_foldinvalid = TRUE; // recompute folds
2906 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 foldOpenCursor();
2908 break;
2909
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002910 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002912 curwin->w_foldinvalid = TRUE; // recompute folds
2913 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 break;
2915
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002916 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002918 {
2919 curwin->w_p_fdl -= cap->count1;
2920 if (curwin->w_p_fdl < 0)
2921 curwin->w_p_fdl = 0;
2922 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002923 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 curwin->w_p_fen = TRUE;
2925 break;
2926
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002927 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002929 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 curwin->w_p_fen = TRUE;
2931 break;
2932
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002933 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002934 case 'r': curwin->w_p_fdl += cap->count1;
2935 {
2936 int d = getDeepestNesting();
2937
2938 if (curwin->w_p_fdl >= d)
2939 curwin->w_p_fdl = d;
2940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 break;
2942
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002943 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002945 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 break;
2947
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002948 case 'j': // "zj" move to next fold downwards
2949 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2951 cap->count1) == FAIL)
2952 clearopbeep(cap->oap);
2953 break;
2954
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002955#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002957#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002958 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002959 case 'g': // "zg": add good word to word list
2960 case 'w': // "zw": add wrong word to word list
2961 case 'G': // "zG": add good word to temp word list
2962 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002963 if (nv_zg_zw(cap, nchar) == FAIL)
2964 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002965 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002966
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002967 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00002968 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00002969 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002970 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002971#endif
2972
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 default: clearopbeep(cap->oap);
2974 }
2975
2976#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002977 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 if (old_fen != curwin->w_p_fen)
2979 {
2980# ifdef FEAT_DIFF
2981 win_T *wp;
2982
2983 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
2984 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002985 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 FOR_ALL_WINDOWS(wp)
2987 {
2988 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
2989 {
2990 wp->w_p_fen = curwin->w_p_fen;
2991 changed_window_setting_win(wp);
2992 }
2993 }
2994 }
2995# endif
2996 changed_window_setting();
2997 }
2998
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002999 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 if (old_fdl != curwin->w_p_fdl)
3001 newFoldLevel();
3002#endif
3003}
3004
3005#ifdef FEAT_GUI
3006/*
3007 * Vertical scrollbar movement.
3008 */
3009 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003010nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011{
3012 if (cap->oap->op_type != OP_NOP)
3013 clearopbeep(cap->oap);
3014
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003015 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 gui_do_scroll();
3017}
3018
3019/*
3020 * Horizontal scrollbar movement.
3021 */
3022 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003023nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024{
3025 if (cap->oap->op_type != OP_NOP)
3026 clearopbeep(cap->oap);
3027
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003028 // Even if an operator was pending, we still want to scroll
Christopher Plewright44c22092022-11-15 17:43:36 +00003029 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030}
3031#endif
3032
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003033#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003034/*
3035 * Click in GUI tab.
3036 */
3037 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003038nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003039{
3040 if (cap->oap->op_type != OP_NOP)
3041 clearopbeep(cap->oap);
3042
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003043 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003044 goto_tabpage(current_tab);
3045}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003046
3047/*
3048 * Selected item in tab line menu.
3049 */
3050 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003051nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003052{
3053 if (cap->oap->op_type != OP_NOP)
3054 clearopbeep(cap->oap);
3055
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003056 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003057 handle_tabmenu();
3058}
3059
3060/*
3061 * Handle selecting an item of the GUI tab line menu.
3062 * Used in Normal and Insert mode.
3063 */
3064 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003065handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003066{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003067 switch (current_tabmenu)
3068 {
3069 case TABLINE_MENU_CLOSE:
3070 if (current_tab == 0)
3071 do_cmdline_cmd((char_u *)"tabclose");
3072 else
3073 {
3074 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3075 current_tab);
3076 do_cmdline_cmd(IObuff);
3077 }
3078 break;
3079
3080 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003081 if (current_tab == 0)
3082 do_cmdline_cmd((char_u *)"$tabnew");
3083 else
3084 {
3085 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3086 current_tab - 1);
3087 do_cmdline_cmd(IObuff);
3088 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003089 break;
3090
3091 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003092 if (current_tab == 0)
3093 do_cmdline_cmd((char_u *)"browse $tabnew");
3094 else
3095 {
3096 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3097 current_tab - 1);
3098 do_cmdline_cmd(IObuff);
3099 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003100 break;
3101 }
3102}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003103#endif
3104
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105/*
3106 * "Q" command.
3107 */
3108 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003109nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003111 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003113 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003114 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 do_exmode(FALSE);
3116}
3117
3118/*
3119 * Handle a ":" command.
3120 */
3121 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003122nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003124 int old_p_im;
3125 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003126 int is_cmdkey = cap->cmdchar == K_COMMAND
3127 || cap->cmdchar == K_SCRIPT_COMMAND;
3128 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129
Bram Moolenaar957cf672020-11-12 14:21:06 +01003130 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003132 nv_operator(cap);
3133 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003135
3136 if (cap->oap->op_type != OP_NOP)
3137 {
3138 // Using ":" as a movement is characterwise exclusive.
3139 cap->oap->motion_type = MCHAR;
3140 cap->oap->inclusive = FALSE;
3141 }
3142 else if (cap->count0 && !is_cmdkey)
3143 {
3144 // translate "count:" into ":.,.+(count - 1)"
3145 stuffcharReadbuff('.');
3146 if (cap->count0 > 1)
3147 {
3148 stuffReadbuff((char_u *)",.+");
3149 stuffnumReadbuff((long)cap->count0 - 1L);
3150 }
3151 }
3152
3153 // When typing, don't type below an old message
3154 if (KeyTyped)
3155 compute_cmdrow();
3156
3157 old_p_im = p_im;
3158
3159 // get a command line and execute it
3160 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3161 if (is_cmdkey)
3162 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3163 else
3164 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
3165
3166 // If 'insertmode' changed, enter or exit Insert mode
3167 if (p_im != old_p_im)
3168 {
3169 if (p_im)
3170 restart_edit = 'i';
3171 else
3172 restart_edit = 0;
3173 }
3174
3175 if (cmd_result == FAIL)
3176 // The Ex command failed, do not execute the operator.
3177 clearop(cap->oap);
3178 else if (cap->oap->op_type != OP_NOP
3179 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
John Marriott02d7a6c2024-02-26 21:21:17 +01003180 || cap->oap->start.col > ml_get_len(cap->oap->start.lnum)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003181 || did_emsg
3182 ))
3183 // The start of the operator has become invalid by the Ex command.
3184 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185}
3186
3187/*
3188 * Handle CTRL-G command.
3189 */
3190 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003191nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003193 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 {
3195 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003196 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 showmode();
3198 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003199 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003200 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 fileinfo((int)cap->count0, FALSE, TRUE);
3202}
3203
3204/*
3205 * Handle CTRL-H <Backspace> command.
3206 */
3207 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003208nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 if (VIsual_active && VIsual_select)
3211 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003212 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 v_visop(cap);
3214 }
3215 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 nv_left(cap);
3217}
3218
3219/*
3220 * CTRL-L: clear screen and redraw.
3221 */
3222 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003223nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003225 if (checkclearop(cap->oap))
3226 return;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003227
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003228#ifdef FEAT_SYN_HL
3229 // Clear all syntax states to force resyncing.
3230 syn_stack_free_all(curwin->w_s);
3231# ifdef FEAT_RELTIME
3232 {
3233 win_T *wp;
3234
3235 FOR_ALL_WINDOWS(wp)
3236 wp->w_s->b_syn_slow = FALSE;
3237 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003238# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003240 redraw_later(UPD_CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003241#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3242# ifdef VIMDLL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003243 if (!gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003244# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003245 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247}
3248
3249/*
3250 * CTRL-O: In Select mode: switch to Visual mode for one command.
3251 * Otherwise: Go to older pcmark.
3252 */
3253 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003254nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 if (VIsual_active && VIsual_select)
3257 {
3258 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003259 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003261 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 }
3263 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 {
3265 cap->count1 = -cap->count1;
3266 nv_pcmark(cap);
3267 }
3268}
3269
3270/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003271 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3272 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 */
3274 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003275nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276{
3277 if (!checkclearopq(cap->oap))
3278 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3279 GETF_SETMARK|GETF_ALT, FALSE);
3280}
3281
3282/*
3283 * "Z" commands.
3284 */
3285 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003286nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003288 if (checkclearopq(cap->oap))
3289 return;
3290
3291 switch (cap->nchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003293 // "ZZ": equivalent to ":x".
3294 case 'Z': do_cmdline_cmd((char_u *)"x");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 break;
3296
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003297 // "ZQ": equivalent to ":q!" (Elvis compatible).
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003298 case 'Q': do_cmdline_cmd((char_u *)"q!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 break;
3300
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003301 default: clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 }
3303}
3304
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305/*
3306 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3307 */
3308 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003309do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310{
3311 oparg_T oa;
3312 cmdarg_T ca;
3313
3314 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003315 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 ca.oap = &oa;
3317 ca.cmdchar = c1;
3318 ca.nchar = c2;
3319 nv_ident(&ca);
3320}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321
3322/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003323 * 'K' normal-mode command. Get the command to lookup the keyword under the
3324 * cursor.
3325 */
3326 static int
3327nv_K_getcmd(
3328 cmdarg_T *cap,
3329 char_u *kp,
3330 int kp_help,
3331 int kp_ex,
3332 char_u **ptr_arg,
3333 int n,
3334 char_u *buf,
3335 unsigned buflen)
3336{
3337 char_u *ptr = *ptr_arg;
3338 int isman;
3339 int isman_s;
3340
3341 if (kp_help)
3342 {
3343 // in the help buffer
3344 STRCPY(buf, "he! ");
3345 return n;
3346 }
3347
3348 if (kp_ex)
3349 {
3350 // 'keywordprog' is an ex command
3351 if (cap->count0 != 0)
3352 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3353 else
3354 STRCPY(buf, kp);
3355 STRCAT(buf, " ");
3356 return n;
3357 }
3358
3359 // An external command will probably use an argument starting
3360 // with "-" as an option. To avoid trouble we skip the "-".
3361 while (*ptr == '-' && n > 0)
3362 {
3363 ++ptr;
3364 --n;
3365 }
3366 if (n == 0)
3367 {
3368 // found dashes only
3369 emsg(_(e_no_identifier_under_cursor));
3370 vim_free(buf);
3371 *ptr_arg = ptr;
3372 return 0;
3373 }
3374
3375 // When a count is given, turn it into a range. Is this
3376 // really what we want?
3377 isman = (STRCMP(kp, "man") == 0);
3378 isman_s = (STRCMP(kp, "man -s") == 0);
3379 if (cap->count0 != 0 && !(isman || isman_s))
3380 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3381
3382 STRCAT(buf, "! ");
3383 if (cap->count0 == 0 && isman_s)
3384 STRCAT(buf, "man");
3385 else
3386 STRCAT(buf, kp);
3387 STRCAT(buf, " ");
3388 if (cap->count0 != 0 && (isman || isman_s))
3389 {
3390 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3391 STRCAT(buf, " ");
3392 }
3393
3394 *ptr_arg = ptr;
3395 return n;
3396}
3397
3398/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 * Handle the commands that use the word under the cursor.
3400 * [g] CTRL-] :ta to current identifier
3401 * [g] 'K' run program for current identifier
3402 * [g] '*' / to current identifier or string
3403 * [g] '#' ? to current identifier or string
3404 * g ']' :tselect for current identifier
3405 */
3406 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003407nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408{
3409 char_u *ptr = NULL;
3410 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003411 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003412 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003414 char_u *kp; // value of 'keywordprg'
3415 int kp_help; // 'keywordprg' is ":he"
3416 int kp_ex; // 'keywordprg' starts with ":"
3417 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003419 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003420 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003423 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 {
3425 cmdchar = cap->nchar;
3426 g_cmd = TRUE;
3427 }
3428 else
3429 {
3430 cmdchar = cap->cmdchar;
3431 g_cmd = FALSE;
3432 }
3433
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003434 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 cmdchar = '#';
3436
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003437 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3439 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3441 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 if (checkclearopq(cap->oap))
3443 return;
3444 }
3445
3446 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3447 (cmdchar == '*' || cmdchar == '#')
3448 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3449 {
3450 clearop(cap->oap);
3451 return;
3452 }
3453
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003454 // Allocate buffer to put the command in. Inserting backslashes can
3455 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3456 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3458 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3459 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003460 if (kp_help && *skipwhite(ptr) == NUL)
3461 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003462 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003463 return;
3464 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003465 kp_ex = (*kp == ':');
3466 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3467 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 if (buf == NULL)
3469 return;
3470 buf[0] = NUL;
3471
3472 switch (cmdchar)
3473 {
3474 case '*':
3475 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003476 // Put cursor at start of word, makes search skip the word
3477 // under the cursor.
3478 // Call setpcmark() first, so "*``" puts the cursor back where
3479 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 setpcmark();
3481 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3482
3483 if (!g_cmd && vim_iswordp(ptr))
3484 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003485 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 break;
3487
3488 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003489 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3490 if (n == 0)
3491 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 break;
3493
3494 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003495 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496#ifdef FEAT_CSCOPE
3497 if (p_cst)
3498 STRCPY(buf, "cstag ");
3499 else
3500#endif
3501 STRCPY(buf, "ts ");
3502 break;
3503
3504 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003505 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 if (curbuf->b_help)
3507 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003509 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003510 if (g_cmd)
3511 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003512 else if (cap->count0 == 0)
3513 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003514 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003515 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 }
3518
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003519 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003520 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003522 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003523 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003524 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003525 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003526 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003527 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003528 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003529 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003530 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003532 vim_free(buf);
3533 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003535 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003536 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003537 {
3538 vim_free(buf);
3539 vim_free(p);
3540 return;
3541 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003542 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003543 STRCAT(buf, p);
3544 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003546 else
3547 {
3548 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003549 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003550 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003551 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003552 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003553 {
3554 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003555 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003556 aux_ptr = (char_u *)"";
3557 else
3558 aux_ptr = (char_u *)"\\|\"\n[";
3559 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003560 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003561 aux_ptr = (char_u *)"\\|\"\n*?[";
3562
3563 p = buf + STRLEN(buf);
3564 while (n-- > 0)
3565 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003566 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003567 if (vim_strchr(aux_ptr, *ptr) != NULL)
3568 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003569 // When current byte is a part of multibyte character, copy all
3570 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003571 if (has_mbyte)
3572 {
3573 int i;
3574 int len = (*mb_ptr2len)(ptr) - 1;
3575
3576 for (i = 0; i < len && n >= 1; ++i, --n)
3577 *p++ = *ptr++;
3578 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003579 *p++ = *ptr++;
3580 }
3581 *p = NUL;
3582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003584 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 if (cmdchar == '*' || cmdchar == '#')
3586 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003587 if (!g_cmd && (has_mbyte
3588 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3589 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003591
3592 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003593 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003595
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003596 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 }
3598 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003599 {
3600 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003602 g_tag_at_cursor = FALSE;
3603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
3605 vim_free(buf);
3606}
3607
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608/*
3609 * Get visually selected text, within one line only.
3610 * Returns FAIL if more than one line selected.
3611 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003612 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003613get_visual_text(
3614 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003615 char_u **pp, // return: start of selected text
3616 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617{
3618 if (VIsual_mode != 'V')
3619 unadjust_for_sel();
3620 if (VIsual.lnum != curwin->w_cursor.lnum)
3621 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003622 if (cap != NULL)
3623 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 return FAIL;
3625 }
3626 if (VIsual_mode == 'V')
3627 {
3628 *pp = ml_get_curline();
John Marriott02d7a6c2024-02-26 21:21:17 +01003629 *lenp = (int)ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 }
3631 else
3632 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003633 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 {
3635 *pp = ml_get_pos(&curwin->w_cursor);
3636 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3637 }
3638 else
3639 {
3640 *pp = ml_get_pos(&VIsual);
3641 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3642 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003643 if (**pp == NUL)
3644 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003645 if (*lenp > 0)
3646 {
3647 if (has_mbyte)
3648 // Correct the length to include all bytes of the last
3649 // character.
3650 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3651 else if ((*pp)[*lenp - 1] == NUL)
3652 // Do not include a trailing NUL.
3653 *lenp -= 1;
3654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 }
3656 reset_VIsual_and_resel();
3657 return OK;
3658}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659
3660/*
3661 * CTRL-T: backwards in tag stack
3662 */
3663 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003664nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665{
3666 if (!checkclearopq(cap->oap))
3667 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3668}
3669
3670/*
3671 * Handle scrolling command 'H', 'L' and 'M'.
3672 */
3673 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003674nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675{
3676 int used = 0;
3677 long n;
3678#ifdef FEAT_FOLDING
3679 linenr_T lnum;
3680#endif
3681 int half;
3682
3683 cap->oap->motion_type = MLINE;
3684 setpcmark();
3685
3686 if (cap->cmdchar == 'L')
3687 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003688 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 curwin->w_cursor.lnum = curwin->w_botline - 1;
3690 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3691 curwin->w_cursor.lnum = 1;
3692 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003693 {
3694#ifdef FEAT_FOLDING
3695 if (hasAnyFolding(curwin))
3696 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003697 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003698 for (n = cap->count1 - 1; n > 0
3699 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3700 {
3701 (void)hasFolding(curwin->w_cursor.lnum,
3702 &curwin->w_cursor.lnum, NULL);
Bram Moolenaar232bdaa2023-01-13 14:17:58 +00003703 if (curwin->w_cursor.lnum > curwin->w_topline)
3704 --curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003705 }
3706 }
3707 else
3708#endif
3709 curwin->w_cursor.lnum -= cap->count1 - 1;
3710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 }
3712 else
3713 {
3714 if (cap->cmdchar == 'M')
3715 {
3716#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003717 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 used -= diff_check_fill(curwin, curwin->w_topline)
3719 - curwin->w_topfill;
3720#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003721 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3723 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3724 {
3725#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003726 // Count half he number of filler lines to be "below this
3727 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3729 + n) / 2 >= half)
3730 {
3731 --n;
3732 break;
3733 }
3734#endif
3735 used += plines(curwin->w_topline + n);
3736 if (used >= half)
3737 break;
3738#ifdef FEAT_FOLDING
3739 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3740 n = lnum - curwin->w_topline;
3741#endif
3742 }
3743 if (n > 0 && used > curwin->w_height)
3744 --n;
3745 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003746 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003747 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003749#ifdef FEAT_FOLDING
3750 if (hasAnyFolding(curwin))
3751 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003752 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003753 lnum = curwin->w_topline;
3754 while (n-- > 0 && lnum < curwin->w_botline - 1)
3755 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003756 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003757 ++lnum;
3758 }
3759 n = lnum - curwin->w_topline;
3760 }
3761#endif
3762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 curwin->w_cursor.lnum = curwin->w_topline + n;
3764 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3765 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3766 }
3767
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003768 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003769 if (cap->oap->op_type == OP_NOP)
3770 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 beginline(BL_SOL | BL_FIX);
3772}
3773
3774/*
3775 * Cursor right commands.
3776 */
3777 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003778nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779{
3780 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003781 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003783 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3784 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003785 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003786 if (mod_mask & MOD_MASK_CTRL)
3787 cap->arg = TRUE;
3788 nv_wordcmd(cap);
3789 return;
3790 }
3791
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 cap->oap->motion_type = MCHAR;
3793 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003794 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003796 // In virtual edit mode, there's no such thing as "past_line", as lines
3797 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003799 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800
3801 for (n = cap->count1; n > 0; --n)
3802 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003803 if ((!past_line && oneright() == FAIL)
3804 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003805 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003807 // <Space> wraps to next line if 'whichwrap' has 's'.
3808 // 'l' wraps to next line if 'whichwrap' has 'l'.
3809 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 if ( ((cap->cmdchar == ' '
3811 && vim_strchr(p_ww, 's') != NULL)
3812 || (cap->cmdchar == 'l'
3813 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003814 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 && vim_strchr(p_ww, '>') != NULL))
3816 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3817 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003818 // When deleting we also count the NL as a character.
3819 // Set cap->oap->inclusive when last char in the line is
3820 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003821 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003823 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 cap->oap->inclusive = TRUE;
3825 else
3826 {
3827 ++curwin->w_cursor.lnum;
3828 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 curwin->w_set_curswant = TRUE;
3831 cap->oap->inclusive = FALSE;
3832 }
3833 continue;
3834 }
3835 if (cap->oap->op_type == OP_NOP)
3836 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003837 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 if (n == cap->count1)
3839 beep_flush();
3840 }
3841 else
3842 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003843 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 cap->oap->inclusive = TRUE;
3845 }
3846 break;
3847 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003848 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 {
3850 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 if (virtual_active())
3852 oneright();
3853 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003856 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 ++curwin->w_cursor.col;
3859 }
3860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 }
3862#ifdef FEAT_FOLDING
3863 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3864 && cap->oap->op_type == OP_NOP)
3865 foldOpenCursor();
3866#endif
3867}
3868
3869/*
3870 * Cursor left commands.
3871 *
3872 * Returns TRUE when operator end should not be adjusted.
3873 */
3874 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003875nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876{
3877 long n;
3878
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003879 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3880 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003881 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003882 if (mod_mask & MOD_MASK_CTRL)
3883 cap->arg = 1;
3884 nv_bck_word(cap);
3885 return;
3886 }
3887
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 cap->oap->motion_type = MCHAR;
3889 cap->oap->inclusive = FALSE;
3890 for (n = cap->count1; n > 0; --n)
3891 {
3892 if (oneleft() == FAIL)
3893 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003894 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3895 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3896 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 if ( (((cap->cmdchar == K_BS
3898 || cap->cmdchar == Ctrl_H)
3899 && vim_strchr(p_ww, 'b') != NULL)
3900 || (cap->cmdchar == 'h'
3901 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003902 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 && vim_strchr(p_ww, '<') != NULL))
3904 && curwin->w_cursor.lnum > 1)
3905 {
3906 --(curwin->w_cursor.lnum);
3907 coladvance((colnr_T)MAXCOL);
3908 curwin->w_set_curswant = TRUE;
3909
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003910 // When the NL before the first char has to be deleted we
3911 // put the cursor on the NUL after the previous line.
3912 // This is a very special case, be careful!
3913 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 if ( (cap->oap->op_type == OP_DELETE
3915 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003916 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003918 char_u *cp = ml_get_cursor();
3919
3920 if (*cp != NUL)
3921 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003922 if (has_mbyte)
3923 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3924 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003925 ++curwin->w_cursor.col;
3926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 cap->retval |= CA_NO_ADJ_OP_END;
3928 }
3929 continue;
3930 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003931 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3933 beep_flush();
3934 break;
3935 }
3936 }
3937#ifdef FEAT_FOLDING
3938 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3939 && cap->oap->op_type == OP_NOP)
3940 foldOpenCursor();
3941#endif
3942}
3943
3944/*
3945 * Cursor up commands.
3946 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3947 */
3948 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003949nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003951 if (mod_mask & MOD_MASK_SHIFT)
3952 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003953 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003954 cap->arg = BACKWARD;
3955 nv_page(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003956 return;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003957 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003958
3959 cap->oap->motion_type = MLINE;
3960 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
3961 clearopbeep(cap->oap);
3962 else if (cap->arg)
3963 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964}
3965
3966/*
3967 * Cursor down commands.
3968 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
3969 */
3970 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02003971nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003973 if (mod_mask & MOD_MASK_SHIFT)
3974 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003975 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003976 cap->arg = FORWARD;
3977 nv_page(cap);
3978 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02003979#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003980 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003981 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
3982 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003984 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003986 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00003987 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 cmdwin_result = CAR;
3989 else
Bram Moolenaarf2732452018-06-03 14:47:35 +02003990#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003991 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02003992 if (bt_prompt(curbuf) && cap->cmdchar == CAR
3993 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
3994 {
3995 invoke_prompt_callback();
3996 if (restart_edit == 0)
3997 restart_edit = 'a';
3998 }
3999 else
4000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 {
4002 cap->oap->motion_type = MLINE;
4003 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4004 clearopbeep(cap->oap);
4005 else if (cap->arg)
4006 beginline(BL_WHITE | BL_FIX);
4007 }
4008 }
4009}
4010
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011/*
4012 * Grab the file name under the cursor and edit it.
4013 */
4014 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004015nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016{
4017 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004018 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004020 if (check_text_or_curbuf_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 return;
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004022
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004023#ifdef FEAT_PROP_POPUP
4024 if (ERROR_IF_TERM_POPUP_WINDOW)
4025 return;
4026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027
Colin Kennedy21570352024-03-03 16:16:47 +01004028 if (!check_can_set_curbuf_disabled())
4029 return;
4030
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004031 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032
4033 if (ptr != NULL)
4034 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004035 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004036 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004037 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004039 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004040 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004041 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004042 {
4043 curwin->w_cursor.lnum = lnum;
4044 check_cursor_lnum();
4045 beginline(BL_SOL | BL_FIX);
4046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 vim_free(ptr);
4048 }
4049 else
4050 clearop(cap->oap);
4051}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052
4053/*
4054 * <End> command: to end of current line or last line.
4055 */
4056 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004057nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004059 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004061 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004063 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 }
4065 nv_dollar(cap);
4066}
4067
4068/*
4069 * Handle the "$" command.
4070 */
4071 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004072nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073{
4074 cap->oap->motion_type = MCHAR;
4075 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004076 // In virtual mode when off the edge of a line and an operator
4077 // is pending (whew!) keep the cursor where it is.
4078 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 if (!virtual_active() || gchar_cursor() != NUL
4080 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004081 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 if (cursor_down((long)(cap->count1 - 1),
4083 cap->oap->op_type == OP_NOP) == FAIL)
4084 clearopbeep(cap->oap);
4085#ifdef FEAT_FOLDING
4086 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4087 foldOpenCursor();
4088#endif
4089}
4090
4091/*
4092 * Implementation of '?' and '/' commands.
4093 * If cap->arg is TRUE don't set PC mark.
4094 */
4095 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004096nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097{
4098 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004099 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100
4101 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4102 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004103 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 cap->cmdchar = 'g';
4105 cap->nchar = '?';
4106 nv_operator(cap);
4107 return;
4108 }
4109
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004110 // When using 'incsearch' the cursor may be moved to set a different search
4111 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004112 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113
4114 if (cap->searchbuf == NULL)
4115 {
4116 clearop(oap);
4117 return;
4118 }
4119
Bram Moolenaar46539112015-02-17 15:43:57 +01004120 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004121 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004122 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123}
4124
LemonBoya4399382022-04-09 21:04:08 +01004125
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126/*
4127 * Handle "N" and "n" commands.
4128 * cap->arg is SEARCH_REV for "N", 0 for "n".
4129 */
4130 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004131nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004133 pos_T old = curwin->w_cursor;
4134 int wrapped = FALSE;
4135 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004136
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004137 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004138 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004139 // Avoid getting stuck on the current cursor position, which can
4140 // happen when an offset is given and the cursor is on the last char
4141 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004142 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004143 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004144 cap->count1 -= 1;
4145 }
LemonBoya4399382022-04-09 21:04:08 +01004146
4147#ifdef FEAT_SEARCH_EXTRA
4148 // Redraw the window to refresh the highlighted matches.
4149 if (i > 0 && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004150 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152}
4153
4154/*
4155 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4156 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004157 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004159 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004160normal_search(
4161 cmdarg_T *cap,
4162 int dir,
4163 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004164 int opt, // extra flags for do_search()
4165 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166{
4167 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004168 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004169#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004170 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172
4173 cap->oap->motion_type = MCHAR;
4174 cap->oap->inclusive = FALSE;
4175 cap->oap->use_reg_one = TRUE;
4176 curwin->w_set_curswant = TRUE;
4177
Bram Moolenaara80faa82020-04-12 19:37:17 +02004178 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004179 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004180 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4181 if (wrapped != NULL)
4182 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 if (i == 0)
4184 clearop(cap->oap);
4185 else
4186 {
4187 if (i == 2)
4188 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190#ifdef FEAT_FOLDING
4191 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4192 foldOpenCursor();
4193#endif
4194 }
LemonBoya4399382022-04-09 21:04:08 +01004195#ifdef FEAT_SEARCH_EXTRA
4196 // Redraw the window to refresh the highlighted matches.
4197 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004198 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004201 // "/$" will put the cursor after the end of the line, may need to
4202 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004204 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205}
4206
4207/*
4208 * Character search commands.
4209 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4210 * ',' and FALSE for ';'.
4211 * cap->nchar is NUL for ',' and ';' (repeat the search)
4212 */
4213 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004214nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215{
4216 int t_cmd;
4217
4218 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4219 t_cmd = TRUE;
4220 else
4221 t_cmd = FALSE;
4222
4223 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004226 clearopbeep(cap->oap);
4227 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004229
4230 curwin->w_set_curswant = TRUE;
4231 // Include a Tab for "tx" and for "dfx".
4232 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4233 && (t_cmd || cap->oap->op_type != OP_NOP))
4234 {
4235 colnr_T scol, ecol;
4236
4237 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4238 curwin->w_cursor.coladd = ecol - scol;
4239 }
4240 else
4241 curwin->w_cursor.coladd = 0;
4242 adjust_for_sel(cap);
4243#ifdef FEAT_FOLDING
4244 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4245 foldOpenCursor();
4246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247}
4248
4249/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004250 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4251 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4252 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4253 * "[m" or "]m" search for prev/next start of (Java) method.
4254 * "[M" or "]M" search for prev/next end of (Java) method.
4255 */
4256 static void
4257nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4258{
4259 pos_T new_pos = {0, 0, 0};
4260 pos_T *pos = NULL; // init for GCC
4261 pos_T prev_pos;
4262 long n;
4263 int findc;
4264 int c;
4265
4266 if (cap->nchar == '*')
4267 cap->nchar = '/';
4268 prev_pos.lnum = 0;
4269 if (cap->nchar == 'm' || cap->nchar == 'M')
4270 {
4271 if (cap->cmdchar == '[')
4272 findc = '{';
4273 else
4274 findc = '}';
4275 n = 9999;
4276 }
4277 else
4278 {
4279 findc = cap->nchar;
4280 n = cap->count1;
4281 }
4282 for ( ; n > 0; --n)
4283 {
4284 if ((pos = findmatchlimit(cap->oap, findc,
4285 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4286 {
4287 if (new_pos.lnum == 0) // nothing found
4288 {
4289 if (cap->nchar != 'm' && cap->nchar != 'M')
4290 clearopbeep(cap->oap);
4291 }
4292 else
4293 pos = &new_pos; // use last one found
4294 break;
4295 }
4296 prev_pos = new_pos;
4297 curwin->w_cursor = *pos;
4298 new_pos = *pos;
4299 }
4300 curwin->w_cursor = *old_pos;
4301
4302 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4303 // brought us to the match for "[m" and "]M" when inside a method.
4304 // Try finding the '{' or '}' we want to be at.
4305 // Also repeat for the given count.
4306 if (cap->nchar == 'm' || cap->nchar == 'M')
4307 {
4308 // norm is TRUE for "]M" and "[m"
4309 int norm = ((findc == '{') == (cap->nchar == 'm'));
4310
4311 n = cap->count1;
4312 // found a match: we were inside a method
4313 if (prev_pos.lnum != 0)
4314 {
4315 pos = &prev_pos;
4316 curwin->w_cursor = prev_pos;
4317 if (norm)
4318 --n;
4319 }
4320 else
4321 pos = NULL;
4322 while (n > 0)
4323 {
4324 for (;;)
4325 {
4326 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4327 {
4328 // if not found anything, that's an error
4329 if (pos == NULL)
4330 clearopbeep(cap->oap);
4331 n = 0;
4332 break;
4333 }
4334 c = gchar_cursor();
4335 if (c == '{' || c == '}')
4336 {
4337 // Must have found end/start of class: use it.
4338 // Or found the place to be at.
4339 if ((c == findc && norm) || (n == 1 && !norm))
4340 {
4341 new_pos = curwin->w_cursor;
4342 pos = &new_pos;
4343 n = 0;
4344 }
4345 // if no match found at all, we started outside of the
4346 // class and we're inside now. Just go on.
4347 else if (new_pos.lnum == 0)
4348 {
4349 new_pos = curwin->w_cursor;
4350 pos = &new_pos;
4351 }
4352 // found start/end of other method: go to match
4353 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004354 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4355 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004356 n = 0;
4357 else
4358 curwin->w_cursor = *pos;
4359 break;
4360 }
4361 }
4362 --n;
4363 }
4364 curwin->w_cursor = *old_pos;
4365 if (pos == NULL && new_pos.lnum != 0)
4366 clearopbeep(cap->oap);
4367 }
4368 if (pos != NULL)
4369 {
4370 setpcmark();
4371 curwin->w_cursor = *pos;
4372 curwin->w_set_curswant = TRUE;
4373#ifdef FEAT_FOLDING
4374 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4375 && cap->oap->op_type == OP_NOP)
4376 foldOpenCursor();
4377#endif
4378 }
4379}
4380
4381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 * "[" and "]" commands.
4383 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4384 */
4385 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004386nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004389 pos_T *pos = NULL; // init for GCC
4390 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 int flag;
4392 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393
4394 cap->oap->motion_type = MCHAR;
4395 cap->oap->inclusive = FALSE;
4396 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004397 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004399 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 if (cap->nchar == 'f')
4401 nv_gotofile(cap);
4402 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403
4404#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004405 // Find the occurrence(s) of the identifier or define under cursor
4406 // in current and included files or jump to the first occurrence.
4407 //
4408 // search list jump
4409 // fwd bwd fwd bwd fwd bwd
4410 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4411 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004412 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 {
4414 char_u *ptr;
4415 int len;
4416
4417 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4418 clearop(cap->oap);
4419 else
4420 {
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004421 // Make a copy, if the line was changed it will be freed.
4422 ptr = vim_strnsave(ptr, len);
4423 if (ptr == NULL)
4424 return;
4425
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 find_pattern_in_path(ptr, 0, len, TRUE,
Keith Thompson184f71c2024-01-04 21:19:04 +01004427 cap->count0 == 0 ? !SAFE_isupper(cap->nchar) : FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4429 cap->count1,
Keith Thompson184f71c2024-01-04 21:19:04 +01004430 SAFE_isupper(cap->nchar) ? ACTION_SHOW_ALL :
4431 SAFE_islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
Colin Kennedy21570352024-03-03 16:16:47 +01004433 (linenr_T)MAXLNUM,
4434 FALSE);
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004435 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 curwin->w_set_curswant = TRUE;
4437 }
4438 }
4439 else
4440#endif
4441
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004442 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4443 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4444 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4445 // "[m" or "]m" search for prev/next start of (Java) method.
4446 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 if ( (cap->cmdchar == '['
4448 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4449 || (cap->cmdchar == ']'
4450 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004451 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004453 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 else if (cap->nchar == '[' || cap->nchar == ']')
4455 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004456 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 flag = '{';
4458 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004459 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460
4461 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004462 // Imitate strange Vi behaviour: When using "]]" with an operator
4463 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004464 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 (cap->oap->op_type != OP_NOP
4466 && cap->arg == FORWARD && flag == '{')))
4467 clearopbeep(cap->oap);
4468 else
4469 {
4470 if (cap->oap->op_type == OP_NOP)
4471 beginline(BL_WHITE | BL_FIX);
4472#ifdef FEAT_FOLDING
4473 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4474 foldOpenCursor();
4475#endif
4476 }
4477 }
4478
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004479 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 else if (cap->nchar == 'p' || cap->nchar == 'P')
4481 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004482 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 }
4484
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004485 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 else if (cap->nchar == '\'' || cap->nchar == '`')
4487 {
4488 pos = &curwin->w_cursor;
4489 for (n = cap->count1; n > 0; --n)
4490 {
4491 prev_pos = *pos;
4492 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4493 cap->nchar == '\'');
4494 if (pos == NULL)
4495 break;
4496 }
4497 if (pos == NULL)
4498 pos = &prev_pos;
4499 nv_cursormark(cap, cap->nchar == '\'', pos);
4500 }
4501
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004502 // [ or ] followed by a middle mouse click: put selected text with
4503 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004504 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 {
4506 (void)do_mouse(cap->oap, cap->nchar,
4507 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4508 cap->count1, PUT_FIXINDENT);
4509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510
4511#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004512 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 else if (cap->nchar == 'z')
4514 {
4515 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4516 cap->count1) == FAIL)
4517 clearopbeep(cap->oap);
4518 }
4519#endif
4520
4521#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004522 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 else if (cap->nchar == 'c')
4524 {
4525 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4526 cap->count1) == FAIL)
4527 clearopbeep(cap->oap);
4528 }
4529#endif
4530
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004531#ifdef FEAT_SPELL
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02004532 // "[r", "[s", "[S", "]r", "]s" and "]S": move to next spell error.
4533 else if (cap->nchar == 'r' || cap->nchar == 's' || cap->nchar == 'S')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004534 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004535 setpcmark();
4536 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004537 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02004538 cap->nchar == 's' ? SMT_ALL :
4539 cap->nchar == 'r' ? SMT_RARE :
4540 SMT_BAD, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004541 {
4542 clearopbeep(cap->oap);
4543 break;
4544 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004545 else
4546 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004547# ifdef FEAT_FOLDING
4548 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4549 foldOpenCursor();
4550# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004551 }
4552#endif
4553
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004554 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 else
4556 clearopbeep(cap->oap);
4557}
4558
4559/*
4560 * Handle Normal mode "%" command.
4561 */
4562 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004563nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564{
4565 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004566#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 linenr_T lnum = curwin->w_cursor.lnum;
4568#endif
4569
4570 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004571 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 {
4573 if (cap->count0 > 100)
4574 clearopbeep(cap->oap);
4575 else
4576 {
4577 cap->oap->motion_type = MLINE;
4578 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004579 // Round up, so 'normal 100%' always jumps at the line line.
4580 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4581 // overflow on 32-bits, so use a formula with less accuracy
4582 // to avoid overflows.
4583 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4585 / 100L * cap->count0;
4586 else
4587 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4588 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004589 if (curwin->w_cursor.lnum < 1)
4590 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4592 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4593 beginline(BL_SOL | BL_FIX);
4594 }
4595 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004596 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 {
4598 cap->oap->motion_type = MCHAR;
4599 cap->oap->use_reg_one = TRUE;
4600 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4601 clearopbeep(cap->oap);
4602 else
4603 {
4604 setpcmark();
4605 curwin->w_cursor = *pos;
4606 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 }
4610 }
4611#ifdef FEAT_FOLDING
4612 if (cap->oap->op_type == OP_NOP
4613 && lnum != curwin->w_cursor.lnum
4614 && (fdo_flags & FDO_PERCENT)
4615 && KeyTyped)
4616 foldOpenCursor();
4617#endif
4618}
4619
4620/*
4621 * Handle "(" and ")" commands.
4622 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4623 */
4624 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004625nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626{
4627 cap->oap->motion_type = MCHAR;
4628 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004629 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004630 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 curwin->w_set_curswant = TRUE;
4632
4633 if (findsent(cap->arg, cap->count1) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004635 clearopbeep(cap->oap);
4636 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004638
4639 // Don't leave the cursor on the NUL past end of line.
4640 adjust_cursor(cap->oap);
4641 curwin->w_cursor.coladd = 0;
4642#ifdef FEAT_FOLDING
4643 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4644 foldOpenCursor();
4645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646}
4647
4648/*
4649 * "m" command: Mark a position.
4650 */
4651 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004652nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004654 if (checkclearop(cap->oap))
4655 return;
4656
4657 if (setmark(cap->nchar) == FAIL)
4658 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659}
4660
4661/*
4662 * "{" and "}" commands.
4663 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4664 */
4665 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004666nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667{
4668 cap->oap->motion_type = MCHAR;
4669 cap->oap->inclusive = FALSE;
4670 cap->oap->use_reg_one = TRUE;
4671 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004672 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004674 clearopbeep(cap->oap);
4675 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004677
4678 curwin->w_cursor.coladd = 0;
4679#ifdef FEAT_FOLDING
4680 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4681 foldOpenCursor();
4682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683}
4684
4685/*
4686 * "u" command: Undo or make lower case.
4687 */
4688 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004689nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004691 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004693 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 cap->cmdchar = 'g';
4695 cap->nchar = 'u';
4696 nv_operator(cap);
4697 }
4698 else
4699 nv_kundo(cap);
4700}
4701
4702/*
4703 * <Undo> command.
4704 */
4705 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004706nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004708 if (checkclearopq(cap->oap))
4709 return;
4710
Bram Moolenaarf2732452018-06-03 14:47:35 +02004711#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004712 if (bt_prompt(curbuf))
4713 {
4714 clearopbeep(cap->oap);
4715 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004717#endif
4718 u_undo((int)cap->count1);
4719 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720}
4721
4722/*
4723 * Handle the "r" command.
4724 */
4725 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004726nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 int had_ctrl_v;
4729 long n;
4730
4731 if (checkclearop(cap->oap))
4732 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004733#ifdef FEAT_JOB_CHANNEL
4734 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4735 {
4736 clearopbeep(cap->oap);
4737 return;
4738 }
4739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004741 // get another character
Christian Brabandt2d63e4b2023-08-12 00:03:57 +02004742 if (cap->nchar == Ctrl_V || cap->nchar == Ctrl_Q)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 {
4744 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004745 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004746 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 if (cap->nchar > DEL)
4748 had_ctrl_v = NUL;
4749 }
4750 else
4751 had_ctrl_v = NUL;
4752
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004753 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004754 if (IS_SPECIAL(cap->nchar))
4755 {
4756 clearopbeep(cap->oap);
4757 return;
4758 }
4759
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004760 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 if (VIsual_active)
4762 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004763 if (got_int)
Christian Brabandt476733f2023-09-19 20:41:51 +02004764 got_int = FALSE;
Bram Moolenaard9820532013-11-04 01:41:17 +01004765 if (had_ctrl_v)
4766 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004767 // Use a special (negative) number to make a difference between a
4768 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004769 if (cap->nchar == CAR)
4770 cap->nchar = REPLACE_CR_NCHAR;
4771 else if (cap->nchar == NL)
4772 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 nv_operator(cap);
4775 return;
4776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004778 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 if (virtual_active())
4780 {
4781 if (u_save_cursor() == FAIL)
4782 return;
4783 if (gchar_cursor() == NUL)
4784 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004785 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 coladvance_force((colnr_T)(getviscol() + cap->count1));
4787 curwin->w_cursor.col -= cap->count1;
4788 }
4789 else if (gchar_cursor() == TAB)
4790 coladvance_force(getviscol());
4791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004793 // Abort if not enough characters to replace.
John Marriott02d7a6c2024-02-26 21:21:17 +01004794 if ((size_t)ml_get_cursor_len() < (unsigned)cap->count1
4795 || (has_mbyte && mb_charlen(ml_get_cursor()) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 {
4797 clearopbeep(cap->oap);
4798 return;
4799 }
4800
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004801 // Replacing with a TAB is done by edit() when it is complicated because
4802 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4803 // Other characters are done below to avoid problems with things like
4804 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4806 {
4807 stuffnumReadbuff(cap->count1);
4808 stuffcharReadbuff('R');
4809 stuffcharReadbuff('\t');
4810 stuffcharReadbuff(ESC);
4811 return;
4812 }
4813
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004814 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 if (u_save_cursor() == FAIL)
4816 return;
4817
4818 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4819 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004820 // Replace character(s) by a single newline.
4821 // Strange vi behaviour: Only one newline is inserted.
4822 // Delete the characters here.
4823 // Insert the newline with an insert command, takes care of
4824 // autoindent. The insert command depends on being on the last
4825 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004826 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 stuffcharReadbuff('\r');
4828 stuffcharReadbuff(ESC);
4829
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004830 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 invoke_edit(cap, TRUE, 'r', FALSE);
4832 }
4833 else
4834 {
4835 prep_redo(cap->oap->regname, cap->count1,
4836 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4837
4838 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 if (has_mbyte)
4840 {
4841 int old_State = State;
4842
4843 if (cap->ncharC1 != 0)
4844 AppendCharToRedobuff(cap->ncharC1);
4845 if (cap->ncharC2 != 0)
4846 AppendCharToRedobuff(cap->ncharC2);
4847
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004848 // This is slow, but it handles replacing a single-byte with a
4849 // multi-byte and the other way around. Also handles adding
4850 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 for (n = cap->count1; n > 0; --n)
4852 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004853 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004854 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4855 {
4856 int c = ins_copychar(curwin->w_cursor.lnum
4857 + (cap->nchar == Ctrl_Y ? -1 : 1));
4858 if (c != NUL)
4859 ins_char(c);
4860 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004861 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004862 ++curwin->w_cursor.col;
4863 }
4864 else
4865 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 State = old_State;
4867 if (cap->ncharC1 != 0)
4868 ins_char(cap->ncharC1);
4869 if (cap->ncharC2 != 0)
4870 ins_char(cap->ncharC2);
4871 }
4872 }
4873 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 {
John Marriott02d7a6c2024-02-26 21:21:17 +01004875 char_u *ptr;
4876
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004877 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 for (n = cap->count1; n > 0; --n)
4879 {
John Marriott02d7a6c2024-02-26 21:21:17 +01004880 // Get ptr again, because ins_copychar() and showmatch()
4881 // will have released the line.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004882 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004883 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4884 {
4885 int c = ins_copychar(curwin->w_cursor.lnum
4886 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004887
4888 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004889 if (c != NUL)
4890 ptr[curwin->w_cursor.col] = c;
4891 }
4892 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004893 {
4894 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004895 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 if (p_sm && msg_silent == 0)
4898 showmatch(cap->nchar);
4899 ++curwin->w_cursor.col;
4900 }
4901#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004902 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004904 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
John Marriott02d7a6c2024-02-26 21:21:17 +01004905 ptr = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906
Bram Moolenaar009b2592004-10-24 19:18:58 +00004907 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004908 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004910 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 }
4912#endif
4913
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004914 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 changed_bytes(curwin->w_cursor.lnum,
4916 (colnr_T)(curwin->w_cursor.col - cap->count1));
4917 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004918 --curwin->w_cursor.col; // cursor on the last replaced char
4919 // if the character on the left of the current cursor is a multi-byte
4920 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 if (has_mbyte)
4922 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 curbuf->b_op_end = curwin->w_cursor;
4924 curwin->w_set_curswant = TRUE;
4925 set_last_insert(cap->nchar);
4926 }
4927}
4928
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929/*
4930 * 'o': Exchange start and end of Visual area.
4931 * 'O': same, but in block mode exchange left and right corners.
4932 */
4933 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004934v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935{
4936 pos_T old_cursor;
4937 colnr_T left, right;
4938
4939 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4940 {
4941 old_cursor = curwin->w_cursor;
4942 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4943 curwin->w_cursor.lnum = VIsual.lnum;
4944 coladvance(left);
4945 VIsual = curwin->w_cursor;
4946
4947 curwin->w_cursor.lnum = old_cursor.lnum;
4948 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004949 // 'selection "exclusive" and cursor at right-bottom corner: move it
4950 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4952 ++curwin->w_curswant;
4953 coladvance(curwin->w_curswant);
4954 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004956 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
4958 curwin->w_cursor.lnum = VIsual.lnum;
4959 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
4960 ++right;
4961 coladvance(right);
4962 VIsual = curwin->w_cursor;
4963
4964 curwin->w_cursor.lnum = old_cursor.lnum;
4965 coladvance(left);
4966 curwin->w_curswant = left;
4967 }
4968 }
4969 else
4970 {
4971 old_cursor = curwin->w_cursor;
4972 curwin->w_cursor = VIsual;
4973 VIsual = old_cursor;
4974 curwin->w_set_curswant = TRUE;
4975 }
4976}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977
4978/*
4979 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
4980 */
4981 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004982nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004984 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
4986 cap->cmdchar = 'c';
4987 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004988 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 VIsual_mode = 'V';
4990 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004991 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004993
4994 if (checkclearopq(cap->oap))
4995 return;
4996
4997 if (!curbuf->b_p_ma)
4998 emsg(_(e_cannot_make_changes_modifiable_is_off));
4999 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005001 if (virtual_active())
5002 coladvance(getviscol());
5003 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 }
5005}
5006
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007/*
5008 * "gr".
5009 */
5010 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005011nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 if (VIsual_active)
5014 {
5015 cap->cmdchar = 'r';
5016 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005017 nv_replace(cap); // Do same as "r" in Visual mode for now
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005018 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005020
zeertzjqf86dea82023-03-05 21:15:06 +00005021 if (checkclearopq(cap->oap))
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005022 return;
5023
5024 if (!curbuf->b_p_ma)
5025 emsg(_(e_cannot_make_changes_modifiable_is_off));
5026 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 {
Christian Brabandt2d63e4b2023-08-12 00:03:57 +02005028 if (cap->extra_char == Ctrl_V || cap->extra_char == Ctrl_Q)
5029 // get another character
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005030 cap->extra_char = get_literal(FALSE);
Bram Moolenaard6a4ea32023-02-25 14:24:44 +00005031 if (cap->extra_char < ' ')
5032 // Prefix a control character with CTRL-V to avoid it being used as
5033 // a command.
5034 stuffcharReadbuff(Ctrl_V);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005035 stuffcharReadbuff(cap->extra_char);
5036 stuffcharReadbuff(ESC);
5037 if (virtual_active())
5038 coladvance(getviscol());
5039 invoke_edit(cap, TRUE, 'v', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 }
5041}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042
5043/*
5044 * Swap case for "~" command, when it does not work like an operator.
5045 */
5046 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005047n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048{
5049 long n;
5050 pos_T startpos;
5051 int did_change = 0;
5052#ifdef FEAT_NETBEANS_INTG
5053 pos_T pos;
5054 char_u *ptr;
5055 int count;
5056#endif
5057
5058 if (checkclearopq(cap->oap))
5059 return;
5060
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005061 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 {
5063 clearopbeep(cap->oap);
5064 return;
5065 }
5066
5067 prep_redo_cmd(cap);
5068
5069 if (u_save_cursor() == FAIL)
5070 return;
5071
5072 startpos = curwin->w_cursor;
5073#ifdef FEAT_NETBEANS_INTG
5074 pos = startpos;
5075#endif
5076 for (n = cap->count1; n > 0; --n)
5077 {
5078 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5079 inc_cursor();
5080 if (gchar_cursor() == NUL)
5081 {
5082 if (vim_strchr(p_ww, '~') != NULL
5083 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5084 {
5085#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005086 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 {
5088 if (did_change)
5089 {
5090 ptr = ml_get(pos.lnum);
John Marriott02d7a6c2024-02-26 21:21:17 +01005091 count = (int)ml_get_len(pos.lnum) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005092 netbeans_removed(curbuf, pos.lnum, pos.col,
5093 (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01005094 // line may have been flushed, get it again
5095 ptr = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005097 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 }
5099 pos.col = 0;
5100 pos.lnum++;
5101 }
5102#endif
5103 ++curwin->w_cursor.lnum;
5104 curwin->w_cursor.col = 0;
5105 if (n > 1)
5106 {
5107 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5108 break;
5109 u_clearline();
5110 }
5111 }
5112 else
5113 break;
5114 }
5115 }
5116#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005117 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 {
5119 ptr = ml_get(pos.lnum);
5120 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005121 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5122 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 }
5124#endif
5125
5126
5127 check_cursor();
5128 curwin->w_set_curswant = TRUE;
5129 if (did_change)
5130 {
5131 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5132 0L);
5133 curbuf->b_op_start = startpos;
5134 curbuf->b_op_end = curwin->w_cursor;
5135 if (curbuf->b_op_end.col > 0)
5136 --curbuf->b_op_end.col;
5137 }
5138}
5139
5140/*
5141 * Move cursor to mark.
5142 */
5143 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005144nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145{
5146 if (check_mark(pos) == FAIL)
5147 clearop(cap->oap);
5148 else
5149 {
5150 if (cap->cmdchar == '\''
5151 || cap->cmdchar == '`'
5152 || cap->cmdchar == '['
5153 || cap->cmdchar == ']')
5154 setpcmark();
5155 curwin->w_cursor = *pos;
5156 if (flag)
5157 beginline(BL_WHITE | BL_FIX);
5158 else
5159 check_cursor();
5160 }
5161 cap->oap->motion_type = flag ? MLINE : MCHAR;
5162 if (cap->cmdchar == '`')
5163 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005164 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 curwin->w_set_curswant = TRUE;
5166}
5167
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168/*
5169 * Handle commands that are operators in Visual mode.
5170 */
5171 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005172v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173{
5174 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5175
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005176 // Uppercase means linewise, except in block mode, then "D" deletes till
5177 // the end of the line, and "C" replaces till EOL
Keith Thompson184f71c2024-01-04 21:19:04 +01005178 if (SAFE_isupper(cap->cmdchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 {
5180 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005181 {
5182 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5186 curwin->w_curswant = MAXCOL;
5187 }
5188 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5189 nv_operator(cap);
5190}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191
5192/*
5193 * "s" and "S" commands.
5194 */
5195 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005196nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005198#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005199 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005200 if (term_swap_diff() == OK)
5201 return;
5202#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005203#ifdef FEAT_JOB_CHANNEL
5204 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5205 {
5206 clearopbeep(cap->oap);
5207 return;
5208 }
5209#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005210 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 {
5212 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005213 {
5214 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 cap->cmdchar = 'c';
5218 nv_operator(cap);
5219 }
5220 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 nv_optrans(cap);
5222}
5223
5224/*
5225 * Abbreviated commands.
5226 */
5227 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005228nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229{
5230 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005231 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005233 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 if (VIsual_active)
5235 v_visop(cap);
5236 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 nv_optrans(cap);
5238}
5239
5240/*
5241 * Translate a command into another command.
5242 */
5243 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005244nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245{
5246 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5247 (char_u *)"d$", (char_u *)"c$",
5248 (char_u *)"cl", (char_u *)"cc",
5249 (char_u *)"yy", (char_u *)":s\r"};
5250 static char_u *str = (char_u *)"xXDCsSY&";
5251
5252 if (!checkclearopq(cap->oap))
5253 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005254 // In Vi "2D" doesn't delete the next line. Can't translate it
5255 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005256 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5257 {
5258 cap->oap->start = curwin->w_cursor;
5259 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005260#ifdef FEAT_EVAL
5261 set_op_var(OP_DELETE);
5262#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005263 cap->count1 = 1;
5264 nv_dollar(cap);
5265 finish_op = TRUE;
5266 ResetRedobuff();
5267 AppendCharToRedobuff('D');
5268 }
5269 else
5270 {
5271 if (cap->count0)
5272 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005273 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 }
5276 cap->opcount = 0;
5277}
5278
5279/*
5280 * "'" and "`" commands. Also for "g'" and "g`".
5281 * cap->arg is TRUE for "'" and "g'".
5282 */
5283 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005284nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285{
5286 pos_T *pos;
5287 int c;
5288#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005289 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005290 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291#endif
5292
5293 if (cap->cmdchar == 'g')
5294 c = cap->extra_char;
5295 else
5296 c = cap->nchar;
5297 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005298 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 {
5300 if (cap->arg)
5301 {
5302 check_cursor_lnum();
5303 beginline(BL_WHITE | BL_FIX);
5304 }
5305 else
5306 check_cursor();
5307 }
5308 else
5309 nv_cursormark(cap, cap->arg, pos);
5310
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005311 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 if (!virtual_active())
5313 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005314 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315#ifdef FEAT_FOLDING
5316 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005317 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005318 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 && (fdo_flags & FDO_MARK)
5320 && old_KeyTyped)
5321 foldOpenCursor();
5322#endif
5323}
5324
5325/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005326 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 */
5328 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005329nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005332#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005334 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005337 if (checkclearopq(cap->oap))
5338 return;
5339
5340 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005342 if (goto_tabpage_lastused() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 clearopbeep(cap->oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005344 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005346 if (cap->cmdchar == 'g')
5347 pos = movechangelist((int)cap->count1);
5348 else
5349 pos = movemark((int)cap->count1);
5350 if (pos == (pos_T *)-1) // jump to other file
5351 {
5352 curwin->w_set_curswant = TRUE;
5353 check_cursor();
5354 }
5355 else if (pos != NULL) // can jump
5356 nv_cursormark(cap, FALSE, pos);
5357 else if (cap->cmdchar == 'g')
5358 {
5359 if (curbuf->b_changelistlen == 0)
5360 emsg(_(e_changelist_is_empty));
5361 else if (cap->count1 < 0)
5362 emsg(_(e_at_start_of_changelist));
5363 else
5364 emsg(_(e_at_end_of_changelist));
5365 }
5366 else
5367 clearopbeep(cap->oap);
5368# ifdef FEAT_FOLDING
5369 if (cap->oap->op_type == OP_NOP
5370 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5371 && (fdo_flags & FDO_MARK)
5372 && old_KeyTyped)
5373 foldOpenCursor();
5374# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375}
5376
5377/*
5378 * Handle '"' command.
5379 */
5380 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005381nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382{
5383 if (checkclearop(cap->oap))
5384 return;
5385#ifdef FEAT_EVAL
5386 if (cap->nchar == '=')
5387 cap->nchar = get_expr_register();
5388#endif
5389 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5390 {
5391 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005392 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393#ifdef FEAT_EVAL
5394 set_reg_var(cap->oap->regname);
5395#endif
5396 }
5397 else
5398 clearopbeep(cap->oap);
5399}
5400
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401/*
5402 * Handle "v", "V" and "CTRL-V" commands.
5403 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5404 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005405 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 */
5407 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005408nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005410 if (cap->cmdchar == Ctrl_Q)
5411 cap->cmdchar = Ctrl_V;
5412
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005413 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5414 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 if (cap->oap->op_type != OP_NOP)
5416 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005417 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005418 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 return;
5420 }
5421
5422 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005423 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005425 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005427 else // toggle char/block mode
5428 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 VIsual_mode = cap->cmdchar;
5430 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005431 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005433 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005435 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 {
5437 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005438 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005440 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 VIsual = curwin->w_cursor;
5442
5443 VIsual_active = TRUE;
5444 VIsual_reselect = TRUE;
5445 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005446 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005449 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005450 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005451 // For V and ^V, we multiply the number of lines even if there
5452 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5454 {
5455 curwin->w_cursor.lnum +=
5456 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005457 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 }
5459 VIsual_mode = resel_VIsual_mode;
5460 if (VIsual_mode == 'v')
5461 {
5462 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005463 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005464 update_curswant_force();
Bram Moolenaar79c11e32023-01-10 17:29:29 +00005465 curwin->w_curswant += resel_VIsual_vcol * cap->count0;
5466 if (*p_sel != 'e')
5467 --curwin->w_curswant;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005470 curwin->w_curswant = resel_VIsual_vcol;
5471 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005473 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 {
5475 curwin->w_curswant = MAXCOL;
5476 coladvance((colnr_T)MAXCOL);
5477 }
5478 else if (VIsual_mode == Ctrl_V)
5479 {
Bram Moolenaar8f531662023-02-01 17:33:18 +00005480 // Update curswant on the original line, that is where "col" is
5481 // valid.
5482 linenr_T lnum = curwin->w_cursor.lnum;
5483 curwin->w_cursor.lnum = VIsual.lnum;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005484 update_curswant_force();
Bram Moolenaar8f531662023-02-01 17:33:18 +00005485 curwin->w_curswant += resel_VIsual_vcol * cap->count0 - 1;
5486 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 coladvance(curwin->w_curswant);
5488 }
5489 else
5490 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005491 redraw_curbuf_later(UPD_INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 }
5493 else
5494 {
5495 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005496 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 may_start_select('c');
5498 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005499 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005500 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005501 if (cap->count0 > 0 && --cap->count1 > 0)
5502 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005503 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005504 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5505 nv_right(cap);
5506 else if (VIsual_mode == 'V')
5507 nv_down(cap);
5508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 }
5510 }
5511}
5512
5513/*
5514 * Start selection for Shift-movement keys.
5515 */
5516 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005517start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005519 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 may_start_select('k');
5521 n_start_visual_mode('v');
5522}
5523
5524/*
5525 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005526 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 */
5528 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005529may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005531 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5532 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533}
5534
5535/*
5536 * Start Visual mode "c".
5537 * Should set VIsual_select before calling this.
5538 */
5539 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005540n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005542#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005543 int cursor_line_was_concealed = curwin->w_p_cole > 0
5544 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005545#endif
5546
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 VIsual_mode = c;
5548 VIsual_active = TRUE;
5549 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005550
5551 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005552 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005553 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005554 {
5555 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 VIsual = curwin->w_cursor;
5559
5560#ifdef FEAT_FOLDING
5561 foldAdjustVisual();
5562#endif
5563
LemonBoy2bf52dd2022-04-09 18:17:34 +01005564 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005566#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005567 // Check if redraw is needed after changing the state.
5568 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005569#endif
5570
Bram Moolenaar09df3122006-01-23 22:23:09 +00005571 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005572 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005574 // Make sure the clipboard gets updated. Needed because start and
5575 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 clip_star.vmode = NUL;
5577#endif
5578
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005579 // Only need to redraw this line, unless still need to redraw an old
5580 // Visual area (when 'lazyredraw' is set).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005581 if (curwin->w_redr_type < UPD_INVERTED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 {
5583 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5584 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5585 }
5586}
5587
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588
5589/*
5590 * CTRL-W: Window commands
5591 */
5592 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005593nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005595 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005596 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005597 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005598 cap->cmdchar = ':';
5599 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005600 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005601 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005602 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005603 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604}
5605
5606/*
5607 * CTRL-Z: Suspend
5608 */
5609 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005610nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611{
5612 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005614 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005615 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616}
5617
5618/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005619 * "gv": Reselect the previous Visual area. If Visual already active,
5620 * exchange previous and current Visual area.
5621 */
5622 static void
5623nv_gv_cmd(cmdarg_T *cap)
5624{
5625 pos_T tpos;
5626 int i;
5627
5628 if (checkclearop(cap->oap))
5629 return;
5630
5631 if (curbuf->b_visual.vi_start.lnum == 0
5632 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5633 || curbuf->b_visual.vi_end.lnum == 0)
5634 {
5635 beep_flush();
5636 return;
5637 }
5638
5639 // set w_cursor to the start of the Visual area, tpos to the end
5640 if (VIsual_active)
5641 {
5642 i = VIsual_mode;
5643 VIsual_mode = curbuf->b_visual.vi_mode;
5644 curbuf->b_visual.vi_mode = i;
5645# ifdef FEAT_EVAL
5646 curbuf->b_visual_mode_eval = i;
5647# endif
5648 i = curwin->w_curswant;
5649 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5650 curbuf->b_visual.vi_curswant = i;
5651
5652 tpos = curbuf->b_visual.vi_end;
5653 curbuf->b_visual.vi_end = curwin->w_cursor;
5654 curwin->w_cursor = curbuf->b_visual.vi_start;
5655 curbuf->b_visual.vi_start = VIsual;
5656 }
5657 else
5658 {
5659 VIsual_mode = curbuf->b_visual.vi_mode;
5660 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5661 tpos = curbuf->b_visual.vi_end;
5662 curwin->w_cursor = curbuf->b_visual.vi_start;
5663 }
5664
5665 VIsual_active = TRUE;
5666 VIsual_reselect = TRUE;
5667
5668 // Set Visual to the start and w_cursor to the end of the Visual
5669 // area. Make sure they are on an existing character.
5670 check_cursor();
5671 VIsual = curwin->w_cursor;
5672 curwin->w_cursor = tpos;
5673 check_cursor();
5674 update_topline();
5675
5676 // When called from normal "g" command: start Select mode when
5677 // 'selectmode' contains "cmd". When called for K_SELECT, always
5678 // start Select mode.
5679 if (cap->arg)
5680 {
5681 VIsual_select = TRUE;
5682 VIsual_select_reg = 0;
5683 }
5684 else
5685 may_start_select('c');
5686 setmouse();
5687#ifdef FEAT_CLIPBOARD
5688 // Make sure the clipboard gets updated. Needed because start and
5689 // end are still the same, and the selection needs to be owned
5690 clip_star.vmode = NUL;
5691#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005692 redraw_curbuf_later(UPD_INVERTED);
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005693 showmode();
5694}
5695
5696/*
5697 * "g0", "g^" : Like "0" and "^" but for screen lines.
5698 * "gm": middle of "g0" and "g$".
5699 */
Luuk van Baalcb204e62024-04-02 20:49:45 +02005700 void
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005701nv_g_home_m_cmd(cmdarg_T *cap)
5702{
5703 int i;
5704 int flag = FALSE;
5705
5706 if (cap->nchar == '^')
5707 flag = TRUE;
5708
5709 cap->oap->motion_type = MCHAR;
5710 cap->oap->inclusive = FALSE;
5711 if (curwin->w_p_wrap && curwin->w_width != 0)
5712 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005713 int width1 = curwin->w_width - curwin_col_off();
5714 int width2 = width1 + curwin_col_off2();
5715 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005716
5717 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005718 virtcol = curwin->w_virtcol
5719#ifdef FEAT_PROP_POPUP
5720 - curwin->w_virtcol_first_char
5721#endif
5722 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005723 i = 0;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005724 if (virtcol >= (colnr_T)width1 && width2 > 0)
5725 i = (virtcol - width1) / width2 * width2 + width1;
Luuk van Baalcb204e62024-04-02 20:49:45 +02005726
5727 // When ending up below 'smoothscroll' marker, move just beyond it so
5728 // that skipcol is not adjusted later.
5729 if (curwin->w_skipcol > 0 && curwin->w_cursor.lnum == curwin->w_topline)
5730 {
5731 int overlap = sms_marker_overlap(curwin, -1);
5732 if (overlap > 0 && i == curwin->w_skipcol)
5733 i += overlap;
5734 }
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005735 }
5736 else
5737 i = curwin->w_leftcol;
5738 // Go to the middle of the screen line. When 'number' or
5739 // 'relativenumber' is on and lines are wrapping the middle can be more
5740 // to the left.
5741 if (cap->nchar == 'm')
5742 i += (curwin->w_width - curwin_col_off()
5743 + ((curwin->w_p_wrap && i > 0)
5744 ? curwin_col_off2() : 0)) / 2;
5745 coladvance((colnr_T)i);
5746 if (flag)
5747 {
5748 do
5749 i = gchar_cursor();
5750 while (VIM_ISWHITE(i) && oneright() == OK);
5751 curwin->w_valid &= ~VALID_WCOL;
5752 }
5753 curwin->w_set_curswant = TRUE;
Luuk van Baal8667a562023-05-12 15:47:25 +01005754 adjust_skipcol();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005755}
5756
5757/*
5758 * "g_": to the last non-blank character in the line or <count> lines
5759 * downward.
5760 */
5761 static void
5762nv_g_underscore_cmd(cmdarg_T *cap)
5763{
5764 char_u *ptr;
5765
5766 cap->oap->motion_type = MCHAR;
5767 cap->oap->inclusive = TRUE;
5768 curwin->w_curswant = MAXCOL;
5769 if (cursor_down((long)(cap->count1 - 1),
5770 cap->oap->op_type == OP_NOP) == FAIL)
5771 {
5772 clearopbeep(cap->oap);
5773 return;
5774 }
5775
5776 ptr = ml_get_curline();
5777
5778 // In Visual mode we may end up after the line.
5779 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5780 --curwin->w_cursor.col;
5781
5782 // Decrease the cursor column until it's on a non-blank.
5783 while (curwin->w_cursor.col > 0
5784 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5785 --curwin->w_cursor.col;
5786 curwin->w_set_curswant = TRUE;
5787 adjust_for_sel(cap);
5788}
5789
5790/*
5791 * "g$" : Like "$" but for screen lines.
5792 */
5793 static void
5794nv_g_dollar_cmd(cmdarg_T *cap)
5795{
5796 oparg_T *oap = cap->oap;
5797 int i;
5798 int col_off = curwin_col_off();
Christian Brabandtb5f6fe92023-08-19 15:53:16 +02005799 int flag = FALSE;
5800
zeertzjq654bdbb2023-08-20 18:24:20 +02005801 if (cap->nchar == K_END || cap->nchar == K_KEND)
Christian Brabandtb5f6fe92023-08-19 15:53:16 +02005802 flag = TRUE;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005803
5804 oap->motion_type = MCHAR;
5805 oap->inclusive = TRUE;
5806 if (curwin->w_p_wrap && curwin->w_width != 0)
5807 {
5808 curwin->w_curswant = MAXCOL; // so we stay at the end
5809 if (cap->count1 == 1)
5810 {
5811 int width1 = curwin->w_width - col_off;
5812 int width2 = width1 + curwin_col_off2();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005813 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005814
5815 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005816 virtcol = curwin->w_virtcol
5817#ifdef FEAT_PROP_POPUP
5818 - curwin->w_virtcol_first_char
5819#endif
5820 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005821 i = width1 - 1;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005822 if (virtcol >= (colnr_T)width1)
5823 i += ((virtcol - width1) / width2 + 1)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005824 * width2;
5825 coladvance((colnr_T)i);
5826
5827 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005828 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005829 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5830 {
5831 // Check for landing on a character that got split at
5832 // the end of the line. We do not want to advance to
5833 // the next screen line.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005834 if (curwin->w_virtcol
5835#ifdef FEAT_PROP_POPUP
5836 - curwin->w_virtcol_first_char
5837#endif
5838 > (colnr_T)i)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005839 --curwin->w_cursor.col;
5840 }
5841 }
5842 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5843 clearopbeep(oap);
5844 }
5845 else
5846 {
5847 if (cap->count1 > 1)
5848 // if it fails, let the cursor still move to the last char
5849 (void)cursor_down(cap->count1 - 1, FALSE);
5850
5851 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5852 coladvance((colnr_T)i);
5853
5854 // if the character doesn't fit move one back
5855 if (curwin->w_cursor.col > 0
5856 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5857 {
5858 colnr_T vcol;
5859
5860 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5861 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5862 --curwin->w_cursor.col;
5863 }
5864
5865 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005866 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005867 }
Christian Brabandtb5f6fe92023-08-19 15:53:16 +02005868 if (flag)
5869 {
5870 do
5871 i = gchar_cursor();
5872 while (VIM_ISWHITE(i) && oneleft() == OK);
5873 curwin->w_valid &= ~VALID_WCOL;
5874 }
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005875}
5876
5877/*
5878 * "gi": start Insert at the last position.
5879 */
5880 static void
5881nv_gi_cmd(cmdarg_T *cap)
5882{
5883 int i;
5884
5885 if (curbuf->b_last_insert.lnum != 0)
5886 {
5887 curwin->w_cursor = curbuf->b_last_insert;
5888 check_cursor_lnum();
John Marriott02d7a6c2024-02-26 21:21:17 +01005889 i = (int)ml_get_curline_len();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005890 if (curwin->w_cursor.col > (colnr_T)i)
5891 {
5892 if (virtual_active())
5893 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5894 curwin->w_cursor.col = i;
5895 }
5896 }
5897 cap->cmdchar = 'i';
5898 nv_edit(cap);
5899}
5900
5901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 * Commands starting with "g".
5903 */
5904 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005905nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906{
5907 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909
5910 switch (cap->nchar)
5911 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005912 case Ctrl_A:
5913 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005915 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005916 if (!VIsual_active && cap->nchar == Ctrl_A)
5917 vim_mem_profile_dump();
5918 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005920 // "g^A/g^X": sequentially increment visually selected region
zeertzjqf86dea82023-03-05 21:15:06 +00005921 if (VIsual_active)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005922 {
5923 cap->arg = TRUE;
5924 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005925 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005926 nv_addsub(cap);
5927 }
5928 else
5929 clearopbeep(oap);
5930 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005932 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 case 'R':
5934 cap->arg = TRUE;
5935 nv_Replace(cap);
5936 break;
5937
5938 case 'r':
5939 nv_vreplace(cap);
5940 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941
5942 case '&':
5943 do_cmdline_cmd((char_u *)"%s//~/&");
5944 break;
5945
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005946 // "gv": Reselect the previous Visual area. If Visual already active,
5947 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005949 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005951
5952 // "gV": Don't reselect the previous Visual area after a Select mode
5953 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 case 'V':
5955 VIsual_reselect = FALSE;
5956 break;
5957
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005958 // "gh": start Select mode.
5959 // "gH": start Select line mode.
5960 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 case K_BS:
5962 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005963 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 case 'h':
5965 case 'H':
5966 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 cap->cmdchar = cap->nchar + ('v' - 'h');
5968 cap->arg = TRUE;
5969 nv_visual(cap);
5970 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005971
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005972 // "gn", "gN" visually select next/previous search match
5973 // "gn" selects next match
5974 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005975 case 'N':
5976 case 'n':
5977 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005978 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005979 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005981 // "gj" and "gk" two new funny movement keys -- up and down
5982 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 case 'j':
5984 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005985 // with 'nowrap' it works just like the normal "j" command.
5986 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 {
5988 oap->motion_type = MLINE;
5989 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5990 }
5991 else
5992 i = nv_screengo(oap, FORWARD, cap->count1);
5993 if (i == FAIL)
5994 clearopbeep(oap);
5995 break;
5996
5997 case 'k':
5998 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005999 // with 'nowrap' it works just like the normal "k" command.
6000 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 {
6002 oap->motion_type = MLINE;
6003 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
6004 }
6005 else
6006 i = nv_screengo(oap, BACKWARD, cap->count1);
6007 if (i == FAIL)
6008 clearopbeep(oap);
6009 break;
6010
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006011 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 case 'J':
6013 nv_join(cap);
6014 break;
6015
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006016 // "g0", "g^" : Like "0" and "^" but for screen lines.
6017 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 case '0':
6020 case 'm':
6021 case K_HOME:
6022 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006023 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 break;
6025
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006026 case 'M':
6027 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006028 oap->motion_type = MCHAR;
6029 oap->inclusive = FALSE;
Dylan Thacker-Smith366c81a2024-03-24 09:46:56 +01006030 i = linetabsize_no_outer(curwin, curwin->w_cursor.lnum);
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006031 if (cap->count0 > 0 && cap->count0 <= 100)
6032 coladvance((colnr_T)(i * cap->count0 / 100));
6033 else
6034 coladvance((colnr_T)(i / 2));
6035 curwin->w_set_curswant = TRUE;
6036 }
6037 break;
6038
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006039 // "g_": to the last non-blank character in the line or <count> lines
6040 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006042 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 break;
6044
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006045 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 case '$':
6047 case K_END:
6048 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006049 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 break;
6051
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006052 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 case '*':
6054 case '#':
6055#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006056 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006058 case Ctrl_RSB: // :tag or :tselect for current identifier
6059 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 nv_ident(cap);
6061 break;
6062
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006063 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 case 'e':
6065 case 'E':
6066 oap->motion_type = MCHAR;
6067 curwin->w_set_curswant = TRUE;
6068 oap->inclusive = TRUE;
6069 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6070 clearopbeep(oap);
6071 break;
6072
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006073 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006075 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 break;
6077
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006078 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006080 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 break;
6082
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006083 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 case 'I':
6085 beginline(0);
6086 if (!checkclearopq(oap))
6087 invoke_edit(cap, FALSE, 'g', FALSE);
6088 break;
6089
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006090 // "gf": goto file, edit file under cursor
6091 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006093 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 nv_gotofile(cap);
6095 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006097 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 case '\'':
6099 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006100 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 case '`':
6102 nv_gomark(cap);
6103 break;
6104
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006105 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006107 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 break;
6109
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006110 // "ga": Display the ascii value of the character under the
6111 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 case 'a':
6113 do_ascii(NULL);
6114 break;
6115
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006116 // "g8": Display the bytes used for the UTF-8 character under the
6117 // cursor. It is displayed in hex.
6118 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006120 if (cap->count0 == 8)
6121 utf_find_illegal();
6122 else
6123 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006126 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006127 case '<':
6128 show_sb_text();
6129 break;
6130
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006131 // "gg": Goto the first line in file. With a count it goes to
6132 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 case 'g':
6134 cap->arg = FALSE;
6135 nv_goto(cap);
6136 break;
6137
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006138 // Two-character operators:
6139 // "gq" Format text
6140 // "gw" Format text and keep cursor position
6141 // "g~" Toggle the case of the text.
6142 // "gu" Change text to lower case.
6143 // "gU" Change text to upper case.
6144 // "g?" rot13 encoding
6145 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 case 'q':
6147 case 'w':
6148 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006149 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 case '~':
6151 case 'u':
6152 case 'U':
6153 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006154 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 nv_operator(cap);
6156 break;
6157
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006158 // "gd": Find first occurrence of pattern under the cursor in the
6159 // current function
6160 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 case 'd':
6162 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006163 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 break;
6165
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006166 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 case K_MIDDLEMOUSE:
6168 case K_MIDDLEDRAG:
6169 case K_MIDDLERELEASE:
6170 case K_LEFTMOUSE:
6171 case K_LEFTDRAG:
6172 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006173 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 case K_RIGHTMOUSE:
6175 case K_RIGHTDRAG:
6176 case K_RIGHTRELEASE:
6177 case K_X1MOUSE:
6178 case K_X1DRAG:
6179 case K_X1RELEASE:
6180 case K_X2MOUSE:
6181 case K_X2DRAG:
6182 case K_X2RELEASE:
6183 mod_mask = MOD_MASK_CTRL;
6184 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6185 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186
6187 case K_IGNORE:
6188 break;
6189
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006190 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 case 'p':
6192 case 'P':
6193 nv_put(cap);
6194 break;
6195
6196#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006197 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 case 'o':
6199 goto_byte(cap->count0);
6200 break;
6201#endif
6202
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006203 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006205 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 do_exmode(TRUE);
6207 break;
6208
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 case ',':
6210 nv_pcmark(cap);
6211 break;
6212
6213 case ';':
6214 cap->count1 = -cap->count1;
6215 nv_pcmark(cap);
6216 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006218 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006219 if (!checkclearop(oap))
6220 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006221 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006222 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006223 if (!checkclearop(oap))
6224 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006225 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006226
Bram Moolenaar62a23252020-08-09 14:04:42 +02006227 case TAB:
6228 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6229 clearopbeep(oap);
6230 break;
6231
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006232 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006233 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006234 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006235 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006236 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006237 break;
6238
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 default:
6240 clearopbeep(oap);
6241 break;
6242 }
6243}
6244
6245/*
6246 * Handle "o" and "O" commands.
6247 */
6248 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006249n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006251#ifdef FEAT_CONCEAL
6252 linenr_T oldline = curwin->w_cursor.lnum;
6253#endif
6254
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006255 if (checkclearopq(cap->oap))
6256 return;
6257
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258#ifdef FEAT_FOLDING
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006259 if (cap->cmdchar == 'O')
6260 // Open above the first line of a folded sequence of lines
6261 (void)hasFolding(curwin->w_cursor.lnum,
6262 &curwin->w_cursor.lnum, NULL);
6263 else
6264 // Open below the last line of a folded sequence of lines
6265 (void)hasFolding(curwin->w_cursor.lnum,
6266 NULL, &curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267#endif
Christian Brabandt4bca4892023-10-27 19:26:49 +02006268 // trigger TextChangedI for the 'o/O' command
6269 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006270 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6271 (cap->cmdchar == 'O' ? 1 : 0)),
6272 (linenr_T)(curwin->w_cursor.lnum +
6273 (cap->cmdchar == 'o' ? 1 : 0))
6274 ) == OK
6275 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
6276 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6277 0, NULL) == OK)
6278 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006279#ifdef FEAT_CONCEAL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006280 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
6281 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006282#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006283#ifdef FEAT_SYN_HL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006284 if (curwin->w_p_cul)
6285 // force redraw of cursorline
6286 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006287#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006288 // When '#' is in 'cpoptions' ignore the count.
6289 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6290 cap->count1 = 1;
6291 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 }
6293}
6294
6295/*
6296 * "." command: redo last change.
6297 */
6298 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006299nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006301 if (checkclearopq(cap->oap))
6302 return;
6303
6304 // If "restart_edit" is TRUE, the last but one command is repeated
6305 // instead of the last command (inserting text). This is used for
6306 // CTRL-O <.> in insert mode.
6307 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6308 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309}
6310
6311/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006312 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 */
6314 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006315nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006317 if (VIsual_select && VIsual_active)
6318 {
6319 int reg;
6320 // Get register name
6321 ++no_mapping;
6322 ++allow_keys;
6323 reg = plain_vgetc();
6324 LANGMAP_ADJUST(reg, TRUE);
6325 --no_mapping;
6326 --allow_keys;
6327
6328 if (reg == '"')
6329 // the unnamed register is 0
6330 reg = 0;
6331
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006332 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006333 return;
6334 }
6335
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006336 if (checkclearopq(cap->oap))
6337 return;
6338
6339 u_redo((int)cap->count1);
6340 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341}
6342
6343/*
6344 * Handle "U" command.
6345 */
6346 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006347nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006349 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006350 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006352 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 cap->cmdchar = 'g';
6354 cap->nchar = 'U';
6355 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006356 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006358
6359 if (checkclearopq(cap->oap))
6360 return;
6361
6362 u_undoline();
6363 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364}
6365
6366/*
6367 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6368 * single character.
6369 */
6370 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006371nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006373 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006374 {
6375#ifdef FEAT_JOB_CHANNEL
6376 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6377 {
6378 clearopbeep(cap->oap);
6379 return;
6380 }
6381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 else
6385 nv_operator(cap);
6386}
6387
6388/*
6389 * Handle an operator command.
6390 * The actual work is done by do_pending_operator().
6391 */
6392 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006393nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394{
6395 int op_type;
6396
6397 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006398#ifdef FEAT_JOB_CHANNEL
6399 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6400 {
6401 clearopbeep(cap->oap);
6402 return;
6403 }
6404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006406 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 nv_lineop(cap);
6408 else if (!checkclearop(cap->oap))
6409 {
6410 cap->oap->start = curwin->w_cursor;
6411 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006412#ifdef FEAT_EVAL
6413 set_op_var(op_type);
6414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 }
6416}
6417
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006418#ifdef FEAT_EVAL
6419/*
6420 * Set v:operator to the characters for "optype".
6421 */
6422 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006423set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006424{
6425 char_u opchars[3];
6426
6427 if (optype == OP_NOP)
6428 set_vim_var_string(VV_OP, NULL, 0);
6429 else
6430 {
6431 opchars[0] = get_op_char(optype);
6432 opchars[1] = get_extra_op_char(optype);
6433 opchars[2] = NUL;
6434 set_vim_var_string(VV_OP, opchars, -1);
6435 }
6436}
6437#endif
6438
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439/*
6440 * Handle linewise operator "dd", "yy", etc.
6441 *
6442 * "_" is is a strange motion command that helps make operators more logical.
6443 * It is actually implemented, but not documented in the real Vi. This motion
6444 * command actually refers to "the current line". Commands like "dd" and "yy"
6445 * are really an alternate form of "d_" and "y_". It does accept a count, so
6446 * "d3_" works to delete 3 lines.
6447 */
6448 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006449nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450{
6451 cap->oap->motion_type = MLINE;
6452 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6453 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006454 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006455 && cap->oap->motion_force != 'v'
6456 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 || cap->oap->op_type == OP_LSHIFT
6458 || cap->oap->op_type == OP_RSHIFT)
6459 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006460 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 beginline(BL_WHITE | BL_FIX);
6462}
6463
6464/*
6465 * <Home> command.
6466 */
6467 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006468nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006470 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006471 if (mod_mask & MOD_MASK_CTRL)
6472 nv_goto(cap);
6473 else
6474 {
6475 cap->count0 = 1;
6476 nv_pipe(cap);
6477 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006478 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6479 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480}
6481
6482/*
6483 * "|" command.
6484 */
6485 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006486nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487{
6488 cap->oap->motion_type = MCHAR;
6489 cap->oap->inclusive = FALSE;
6490 beginline(0);
6491 if (cap->count0 > 0)
6492 {
6493 coladvance((colnr_T)(cap->count0 - 1));
6494 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6495 }
6496 else
6497 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006498 // keep curswant at the column where we wanted to go, not where
6499 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 curwin->w_set_curswant = FALSE;
6501}
6502
6503/*
6504 * Handle back-word command "b" and "B".
6505 * cap->arg is 1 for "B"
6506 */
6507 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006508nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509{
6510 cap->oap->motion_type = MCHAR;
6511 cap->oap->inclusive = FALSE;
6512 curwin->w_set_curswant = TRUE;
6513 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6514 clearopbeep(cap->oap);
6515#ifdef FEAT_FOLDING
6516 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6517 foldOpenCursor();
6518#endif
6519}
6520
6521/*
6522 * Handle word motion commands "e", "E", "w" and "W".
6523 * cap->arg is TRUE for "E" and "W".
6524 */
6525 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006526nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527{
6528 int n;
6529 int word_end;
6530 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006531 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006533 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6535 word_end = TRUE;
6536 else
6537 word_end = FALSE;
6538 cap->oap->inclusive = word_end;
6539
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006540 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 if (!word_end && cap->oap->op_type == OP_CHANGE)
6542 {
6543 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006544 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006546 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006548 // Reproduce a funny Vi behaviour: "cw" on a blank only
6549 // changes one character, not all blanks until the start of
6550 // the next word. Only do this when the 'w' flag is included
6551 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6553 {
6554 cap->oap->inclusive = TRUE;
6555 cap->oap->motion_type = MCHAR;
6556 return;
6557 }
6558 }
6559 else
6560 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006561 // This is a little strange. To match what the real Vi does,
6562 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6563 // that we are not on a space or a TAB. This seems impolite
6564 // at first, but it's really more what we mean when we say
6565 // 'cw'.
6566 // Another strangeness: When standing on the end of a word
6567 // "ce" will change until the end of the next word, but "cw"
6568 // will change only one character! This is done by setting
6569 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 cap->oap->inclusive = TRUE;
6571 word_end = TRUE;
6572 flag = TRUE;
6573 }
6574 }
6575 }
6576
6577 cap->oap->motion_type = MCHAR;
6578 curwin->w_set_curswant = TRUE;
6579 if (word_end)
6580 n = end_word(cap->count1, cap->arg, flag, FALSE);
6581 else
6582 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6583
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006584 // Don't leave the cursor on the NUL past the end of line. Unless we
6585 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006586 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006587 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588
6589 if (n == FAIL && cap->oap->op_type == OP_NOP)
6590 clearopbeep(cap->oap);
6591 else
6592 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594#ifdef FEAT_FOLDING
6595 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6596 foldOpenCursor();
6597#endif
6598 }
6599}
6600
6601/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006602 * Used after a movement command: If the cursor ends up on the NUL after the
6603 * end of the line, may move it back to the last character and make the motion
6604 * inclusive.
6605 */
6606 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006607adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006608{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006609 // The cursor cannot remain on the NUL when:
6610 // - the column is > 0
6611 // - not in Visual mode or 'selection' is "o"
6612 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006613 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006614 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006615 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006616 {
6617 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006618 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006619 if (has_mbyte)
6620 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006621 oap->inclusive = TRUE;
6622 }
6623}
6624
6625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 * "0" and "^" commands.
6627 * cap->arg is the argument for beginline().
6628 */
6629 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006630nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631{
6632 cap->oap->motion_type = MCHAR;
6633 cap->oap->inclusive = FALSE;
6634 beginline(cap->arg);
6635#ifdef FEAT_FOLDING
6636 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6637 foldOpenCursor();
6638#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006639 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6640 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641}
6642
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643/*
6644 * In exclusive Visual mode, may include the last character.
6645 */
6646 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006647adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648{
6649 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006650 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 if (has_mbyte)
6653 inc_cursor();
6654 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 ++curwin->w_cursor.col;
6656 cap->oap->inclusive = FALSE;
6657 }
6658}
6659
6660/*
6661 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6662 * Should check VIsual_mode before calling this.
6663 * Returns TRUE when backed up to the previous line.
6664 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006665 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006666unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667{
6668 pos_T *pp;
6669
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006670 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006672 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 pp = &curwin->w_cursor;
6674 else
6675 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 if (pp->coladd > 0)
6677 --pp->coladd;
6678 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 if (pp->col > 0)
6680 {
6681 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006682 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 }
6684 else if (pp->lnum > 1)
6685 {
6686 --pp->lnum;
John Marriott02d7a6c2024-02-26 21:21:17 +01006687 pp->col = ml_get_len(pp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 return TRUE;
6689 }
6690 }
6691 return FALSE;
6692}
6693
6694/*
6695 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6696 */
6697 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006698nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699{
6700 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006701 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006703 VIsual_select_reg = 0;
6704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 else if (VIsual_reselect)
6706 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006707 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 cap->arg = TRUE;
6709 nv_g_cmd(cap);
6710 }
6711}
6712
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713
6714/*
6715 * "G", "gg", CTRL-END, CTRL-HOME.
6716 * cap->arg is TRUE for "G".
6717 */
6718 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006719nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720{
6721 linenr_T lnum;
6722
6723 if (cap->arg)
6724 lnum = curbuf->b_ml.ml_line_count;
6725 else
6726 lnum = 1L;
6727 cap->oap->motion_type = MLINE;
6728 setpcmark();
6729
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006730 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 if (cap->count0 != 0)
6732 lnum = cap->count0;
6733 if (lnum < 1L)
6734 lnum = 1L;
6735 else if (lnum > curbuf->b_ml.ml_line_count)
6736 lnum = curbuf->b_ml.ml_line_count;
6737 curwin->w_cursor.lnum = lnum;
6738 beginline(BL_SOL | BL_FIX);
6739#ifdef FEAT_FOLDING
6740 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6741 foldOpenCursor();
6742#endif
6743}
6744
6745/*
6746 * CTRL-\ in Normal mode.
6747 */
6748 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006749nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750{
6751 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6752 {
6753 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006754 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006755 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 if (cmdwin_type != 0)
6758 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 if (VIsual_active)
6760 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006761 end_visual_mode(); // stop Visual
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006762 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006764 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 if (cap->nchar == Ctrl_G && p_im)
6766 restart_edit = 'a';
6767 }
6768 else
6769 clearopbeep(cap->oap);
6770}
6771
6772/*
6773 * ESC in Normal mode: beep, but don't flush buffers.
6774 * Don't even beep if we are canceling a command.
6775 */
6776 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006777nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778{
6779 int no_reason;
6780
6781 no_reason = (cap->oap->op_type == OP_NOP
6782 && cap->opcount == 0
6783 && cap->count0 == 0
6784 && cap->oap->regname == 0
6785 && !p_im);
6786
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006787 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006789 if (restart_edit == 0 && cmdwin_type == 0
6790 && !VIsual_active && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006791 {
Bram Moolenaarf7e73022022-09-24 13:10:04 +01006792 int out_redir = !stdout_isatty && !is_not_a_term_or_gui();
6793
Bram Moolenaar5939c352022-09-24 12:50:45 +01006794 // The user may accidentally do "vim file | grep word" and then
6795 // CTRL-C doesn't show anything. With a changed buffer give the
6796 // message on stderr. Without any changes might as well exit.
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006797 if (anyBufIsChanged())
Bram Moolenaar5939c352022-09-24 12:50:45 +01006798 {
6799 char *ms = _("Type :qa! and press <Enter> to abandon all changes and exit Vim");
6800
6801 if (out_redir)
6802 mch_errmsg(ms);
6803 else
6804 msg(ms);
6805 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006806 else
Bram Moolenaar5939c352022-09-24 12:50:45 +01006807 {
6808 if (out_redir)
6809 {
6810 got_int = FALSE;
6811 do_cmdline_cmd((char_u *)"qa");
6812 }
6813 else
6814 msg(_("Type :qa and press <Enter> to exit Vim"));
6815 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01006818 if (restart_edit != 0)
6819 redraw_mode = TRUE; // remove "-- (insert) --"
6820
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006821 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6822 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 if (!p_im)
6824 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 if (cmdwin_type != 0)
6826 {
6827 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006828 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 return;
6830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 }
Bram Moolenaar8d696372022-08-21 10:40:07 +01006832 else if (cmdwin_type != 0 && ex_normal_busy && typebuf_was_empty)
Bram Moolenaar7d414102021-02-23 19:39:20 +01006833 {
6834 // When :normal runs out of characters while in the command line window
Bram Moolenaar8d696372022-08-21 10:40:07 +01006835 // vgetorpeek() will repeatedly return ESC. Exit the cmdline window to
6836 // break the loop.
Bram Moolenaar7d414102021-02-23 19:39:20 +01006837 cmdwin_result = K_IGNORE;
6838 return;
6839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 if (VIsual_active)
6842 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006843 end_visual_mode(); // stop Visual
6844 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006846 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006848 else if (no_reason)
Bram Moolenaarb849c822022-08-28 22:46:21 +01006849 {
6850#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar98d10692022-08-29 00:08:39 +01006851 if (!cap->arg && popup_message_win_visible())
Bram Moolenaarb849c822022-08-28 22:46:21 +01006852 popup_hide_message_win();
6853 else
6854#endif
6855 vim_beep(BO_ESC);
6856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 clearop(cap->oap);
6858
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006859 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6860 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006861 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 restart_edit = 'a';
6863}
6864
6865/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006866 * Move the cursor for the "A" command.
6867 */
6868 void
6869set_cursor_for_append_to_line(void)
6870{
6871 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006872 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006873 {
6874 int save_State = State;
6875
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006876 // Pretend Insert mode here to allow the cursor on the
6877 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006878 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006879 coladvance((colnr_T)MAXCOL);
6880 State = save_State;
6881 }
6882 else
6883 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6884}
6885
6886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006888 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 */
6890 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006891nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006893 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6895 cap->cmdchar = 'i';
6896
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006897 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006899 {
6900#ifdef FEAT_TERMINAL
6901 if (term_in_normal_mode())
6902 {
6903 end_visual_mode();
6904 clearop(cap->oap);
6905 term_enter_job_mode();
6906 return;
6907 }
6908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006912 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006913 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6914 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 nv_object(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006918#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006919 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006920 {
6921 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006922 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006923 return;
6924 }
6925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 else if (!curbuf->b_p_ma && !p_im)
6927 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006928 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006929 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006931 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006932 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006933 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006935 else if (cap->cmdchar == K_PS && VIsual_active)
6936 {
6937 pos_T old_pos = curwin->w_cursor;
6938 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006939 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006940
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006941 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006942 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6943 {
6944 shift_delete_registers();
6945 cap->oap->regname = '1';
6946 }
6947 else
6948 cap->oap->regname = '-';
6949 cap->cmdchar = 'd';
6950 cap->nchar = NUL;
6951 nv_operator(cap);
6952 do_pending_operator(cap, 0, FALSE);
6953 cap->cmdchar = K_PS;
6954
zeertzjq7a732522022-03-14 20:46:41 +00006955 if (*ml_get_cursor() != NUL)
6956 {
6957 if (old_visual_mode == 'V')
6958 {
6959 // In linewise Visual mode insert before the beginning of the
6960 // next line.
6961 // When the last line in the buffer was deleted then create a
6962 // new line, otherwise there is not need to move cursor.
6963 // Detect this by checking if cursor moved above Visual area.
6964 if (curwin->w_cursor.lnum < old_pos.lnum
6965 && curwin->w_cursor.lnum < old_visual.lnum)
6966 {
6967 if (u_save_cursor() == OK)
6968 {
6969 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6970 FALSE);
6971 appended_lines(curwin->w_cursor.lnum++, 1L);
6972 }
6973 }
6974 }
6975 // When the last char in the line was deleted then append.
6976 // Detect this by checking if cursor moved before Visual area.
6977 else if (curwin->w_cursor.col < old_pos.col
6978 && curwin->w_cursor.col < old_visual.col)
6979 inc_cursor();
6980 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006981
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006982 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006983 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 else if (!checkclearopq(cap->oap))
6986 {
6987 switch (cap->cmdchar)
6988 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006989 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006990 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 break;
6992
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006993 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006994 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6995 beginline(BL_WHITE);
6996 else
6997 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 break;
6999
Bram Moolenaara1891842017-02-04 21:34:31 +01007000 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007001 // Bracketed paste works like "a"ppend, unless the cursor is in
7002 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007003 if (curwin->w_cursor.col == 0)
7004 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007005 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007006
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007007 case 'a': // "a"ppend is like "i"nsert on the next character.
7008 // increment coladd when in virtual space, increment the
7009 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 if (virtual_active()
7011 && (curwin->w_cursor.coladd > 0
7012 || *ml_get_cursor() == NUL
7013 || *ml_get_cursor() == TAB))
7014 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007015 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 inc_cursor();
7017 break;
7018 }
7019
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7021 {
7022 int save_State = State;
7023
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007024 // Pretend Insert mode here to allow the cursor on the
7025 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01007026 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 coladvance(getviscol());
7028 State = save_State;
7029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030
7031 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7032 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007033 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007034 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007035 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036}
7037
7038/*
7039 * Invoke edit() and take care of "restart_edit" and the return value.
7040 */
7041 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007042invoke_edit(
7043 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007044 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007045 int cmd,
7046 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047{
7048 int restart_edit_save = 0;
7049
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007050 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7051 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7052 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 if (repl || !stuff_empty())
7054 restart_edit_save = restart_edit;
7055 else
7056 restart_edit_save = 0;
7057
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007058 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 restart_edit = 0;
7060
Christian Brabandt4bca4892023-10-27 19:26:49 +02007061 // Reset Changedtick_i, so that TextChangedI will only be triggered for stuff
7062 // from insert mode, for 'o/O' this has already been done in n_opencmd
7063 if (cap->cmdchar != 'O' && cap->cmdchar != 'o')
7064 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 if (edit(cmd, startln, cap->count1))
7066 cap->retval |= CA_COMMAND_BUSY;
7067
7068 if (restart_edit == 0)
7069 restart_edit = restart_edit_save;
7070}
7071
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072/*
7073 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7074 */
7075 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007076nv_object(
7077 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078{
7079 int flag;
7080 int include;
7081 char_u *mps_save;
7082
7083 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007084 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007086 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007088 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 mps_save = curbuf->b_p_mps;
7090 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7091
7092 switch (cap->nchar)
7093 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007094 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 flag = current_word(cap->oap, cap->count1, include, FALSE);
7096 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007097 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 flag = current_word(cap->oap, cap->count1, include, TRUE);
7099 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007100 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 case '(':
7102 case ')':
7103 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7104 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007105 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 case '{':
7107 case '}':
7108 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7109 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007110 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 case ']':
7112 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7113 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007114 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 case '>':
7116 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7117 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007118#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007119 case 't': // "at" = a tag block (xml and html)
7120 // Do not adjust oap->end in do_pending_operator()
7121 // otherwise there are different results for 'dit'
7122 // (note leading whitespace in last line):
7123 // 1) <b> 2) <b>
7124 // foobar foobar
7125 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007126 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007127 flag = current_tagblock(cap->oap, cap->count1, include);
7128 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007129#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007130 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 flag = current_par(cap->oap, cap->count1, include, 'p');
7132 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007133 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 flag = current_sent(cap->oap, cap->count1, include);
7135 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007136 case '"': // "a"" = a double quoted string
7137 case '\'': // "a'" = a single quoted string
7138 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007139 flag = current_quote(cap->oap, cap->count1, include,
7140 cap->nchar);
7141 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007142#if 0 // TODO
7143 case 'S': // "aS" = a section
7144 case 'f': // "af" = a filename
7145 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146#endif
7147 default:
7148 flag = FAIL;
7149 break;
7150 }
7151
7152 curbuf->b_p_mps = mps_save;
7153 if (flag == FAIL)
7154 clearopbeep(cap->oap);
7155 adjust_cursor_col();
7156 curwin->w_set_curswant = TRUE;
7157}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158
7159/*
7160 * "q" command: Start/stop recording.
7161 * "q:", "q/", "q?": edit command-line in command-line window.
7162 */
7163 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007164nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165{
7166 if (cap->oap->op_type == OP_FORMAT)
7167 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007168 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 cap->cmdchar = 'g';
7170 cap->nchar = 'q';
7171 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007172 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007174
7175 if (checkclearop(cap->oap))
7176 return;
7177
7178 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007180 if (cmdwin_type != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007182 emsg(_(e_cmdline_window_already_open));
7183 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007185 stuffcharReadbuff(cap->nchar);
7186 stuffcharReadbuff(K_CMDWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007188 else
7189 // (stop) recording into a named register, unless executing a
7190 // register
7191 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
7192 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193}
7194
7195/*
7196 * Handle the "@r" command.
7197 */
7198 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007199nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
7201 if (checkclearop(cap->oap))
7202 return;
7203#ifdef FEAT_EVAL
7204 if (cap->nchar == '=')
7205 {
7206 if (get_expr_register() == NUL)
7207 return;
7208 }
7209#endif
7210 while (cap->count1-- && !got_int)
7211 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007212 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 {
7214 clearopbeep(cap->oap);
7215 break;
7216 }
7217 line_breakcheck();
7218 }
7219}
7220
7221/*
7222 * Handle the CTRL-U and CTRL-D commands.
7223 */
7224 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007225nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226{
Luuk van Baalcb204e62024-04-02 20:49:45 +02007227 int dir = cap->cmdchar == Ctrl_D ? FORWARD : BACKWARD;
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01007228 if (!checkclearop(cap->oap))
Luuk van Baalcb204e62024-04-02 20:49:45 +02007229 pagescroll(dir, cap->count0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230}
7231
7232/*
7233 * Handle "J" or "gJ" command.
7234 */
7235 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007236nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007238 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007240 nv_operator(cap);
7241 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007243
7244 if (checkclearop(cap->oap))
7245 return;
7246
7247 if (cap->count0 <= 1)
7248 cap->count0 = 2; // default for join is two lines!
7249 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7250 curbuf->b_ml.ml_line_count)
7251 {
7252 // can't join when on the last line
7253 if (cap->count0 <= 2)
7254 {
7255 clearopbeep(cap->oap);
7256 return;
7257 }
7258 cap->count0 = curbuf->b_ml.ml_line_count
7259 - curwin->w_cursor.lnum + 1;
7260 }
7261
7262 prep_redo(cap->oap->regname, cap->count0,
7263 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7264 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265}
7266
7267/*
7268 * "P", "gP", "p" and "gp" commands.
7269 */
7270 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007271nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007273 nv_put_opt(cap, FALSE);
7274}
7275
7276/*
7277 * "P", "gP", "p" and "gp" commands.
7278 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7279 */
7280 static void
7281nv_put_opt(cmdarg_T *cap, int fix_indent)
7282{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 int regname = 0;
7284 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007285 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007286 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 int dir;
7288 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007289 int keep_registers = FALSE;
zeertzjq4e141c62024-02-28 21:49:51 +01007290#ifdef FEAT_FOLDING
7291 int save_fen = curwin->w_p_fen;
7292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293
7294 if (cap->oap->op_type != OP_NOP)
7295 {
7296#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007297 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7299 {
7300 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007301 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 }
7303 else
7304#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007305 clearopbeep(cap->oap);
7306 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007308
Bram Moolenaarf2732452018-06-03 14:47:35 +02007309#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007310 if (bt_prompt(curbuf) && !prompt_curpos_editable())
Bram Moolenaarf2732452018-06-03 14:47:35 +02007311 {
7312 clearopbeep(cap->oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007313 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02007314 }
7315#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007316
7317 if (fix_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007319 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7320 ? FORWARD : BACKWARD;
7321 flags |= PUT_FIXINDENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007323 else
7324 dir = (cap->cmdchar == 'P'
7325 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7326 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
7327 prep_redo_cmd(cap);
7328 if (cap->cmdchar == 'g')
7329 flags |= PUT_CURSEND;
7330 else if (cap->cmdchar == 'z')
7331 flags |= PUT_BLOCK_INNER;
7332
7333 if (VIsual_active)
7334 {
7335 // Putting in Visual mode: The put text replaces the selected
7336 // text. First delete the selected text, then put the new text.
7337 // Need to save and restore the registers that the delete
7338 // overwrites if the old contents is being put.
7339 was_visual = TRUE;
7340 regname = cap->oap->regname;
7341 keep_registers = cap->cmdchar == 'P';
7342#ifdef FEAT_CLIPBOARD
7343 adjust_clip_reg(&regname);
7344#endif
7345 if (regname == 0 || regname == '"'
7346 || VIM_ISDIGIT(regname) || regname == '-'
7347#ifdef FEAT_CLIPBOARD
7348 || (clip_unnamed && (regname == '*' || regname == '+'))
7349#endif
7350
7351 )
7352 {
7353 // The delete is going to overwrite the register we want to
7354 // put, save it first.
7355 reg1 = get_register(regname, TRUE);
7356 }
7357
zeertzjq4e141c62024-02-28 21:49:51 +01007358#ifdef FEAT_FOLDING
7359 // Temporarily disable folding, as deleting a fold marker may cause
7360 // the cursor to be included in a fold.
7361 curwin->w_p_fen = FALSE;
7362#endif
7363
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007364 // Now delete the selected text. Avoid messages here.
7365 cap->cmdchar = 'd';
7366 cap->nchar = NUL;
7367 cap->oap->regname = keep_registers ? '_' : NUL;
7368 ++msg_silent;
7369 nv_operator(cap);
7370 do_pending_operator(cap, 0, FALSE);
7371 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7372 --msg_silent;
7373
7374 // delete PUT_LINE_BACKWARD;
7375 cap->oap->regname = regname;
7376
7377 if (reg1 != NULL)
7378 {
7379 // Delete probably changed the register we want to put, save
7380 // it first. Then put back what was there before the delete.
7381 reg2 = get_register(regname, FALSE);
7382 put_register(regname, reg1);
7383 }
7384
7385 // When deleted a linewise Visual area, put the register as
7386 // lines to avoid it joined with the next line. When deletion was
7387 // characterwise, split a line when putting lines.
7388 if (VIsual_mode == 'V')
7389 flags |= PUT_LINE;
7390 else if (VIsual_mode == 'v')
7391 flags |= PUT_LINE_SPLIT;
7392 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7393 flags |= PUT_LINE_FORWARD;
7394 dir = BACKWARD;
7395 if ((VIsual_mode != 'V'
7396 && curwin->w_cursor.col < curbuf->b_op_start.col)
7397 || (VIsual_mode == 'V'
7398 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
7399 // cursor is at the end of the line or end of file, put
7400 // forward.
7401 dir = FORWARD;
7402 // May have been reset in do_put().
7403 VIsual_active = TRUE;
7404 }
7405 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
7406
7407 // If a register was saved, put it back now.
7408 if (reg2 != NULL)
7409 put_register(regname, reg2);
7410
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007411 if (was_visual)
7412 {
zeertzjq4e141c62024-02-28 21:49:51 +01007413#ifdef FEAT_FOLDING
7414 if (save_fen)
7415 curwin->w_p_fen = TRUE;
7416#endif
7417 // What to reselect with "gv"? Selecting the just put text seems to
7418 // be the most useful, since the original text was removed.
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007419 curbuf->b_visual.vi_start = curbuf->b_op_start;
7420 curbuf->b_visual.vi_end = curbuf->b_op_end;
7421 // need to adjust cursor position
7422 if (*p_sel == 'e')
7423 inc(&curbuf->b_visual.vi_end);
7424 }
7425
7426 // When all lines were selected and deleted do_put() leaves an empty
7427 // line that needs to be deleted now.
7428 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
7429 {
7430 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
7431 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
7432
7433 // If the cursor was in that line, move it to the end of the last
7434 // line.
7435 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7436 {
7437 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7438 coladvance((colnr_T)MAXCOL);
7439 }
7440 }
7441 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442}
7443
7444/*
7445 * "o" and "O" commands.
7446 */
7447 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007448nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449{
7450#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007451 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7453 {
7454 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007455 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 }
7457 else
7458#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007459 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007461#ifdef FEAT_JOB_CHANNEL
7462 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007463 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 n_opencmd(cap);
7467}
7468
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469#ifdef FEAT_NETBEANS_INTG
7470 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007471nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472{
7473 netbeans_keycommand(cap->nchar);
7474}
7475#endif
7476
7477#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007479nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007481 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482}
7483#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007484
Bram Moolenaar3918c952005-03-15 22:34:55 +00007485/*
7486 * Trigger CursorHold event.
7487 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7488 * input buffer. "did_cursorhold" is set to avoid retriggering.
7489 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007490 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007491nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007492{
7493 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7494 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007495 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007496}