blob: ea30607d6a548fadda2a558a9668cc1ed02b02ba [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);
John Marriott8c85a2a2024-05-20 19:18:26 +020064static int normal_search(cmdarg_T *cap, int dir, char_u *pat, size_t patlen, 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
Yee Cheng China1b654f2024-11-11 21:57:27 +0100421#ifdef FEAT_MOUSESHAPE
422 update_mouseshape(-1);
423#endif
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000424 }
425 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
426 {
427 // Allow mappings defined with ":lmap".
428 --no_mapping;
429 --allow_keys;
430 if (repl)
Bram Moolenaar24959102022-05-07 20:01:16 +0100431 State = MODE_LREPLACE;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000432 else
Bram Moolenaar24959102022-05-07 20:01:16 +0100433 State = MODE_LANGMAP;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000434 langmap_active = TRUE;
435 }
436#ifdef HAVE_INPUT_METHOD
437 save_smd = p_smd;
438 p_smd = FALSE; // Don't let the IM code show the mode here
439 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
440 im_set_active(TRUE);
441#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100442 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000443 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100444 MAY_WANT_TO_LOG_THIS;
445
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000446 // Disable bracketed paste and modifyOtherKeys here, we won't
447 // recognize the escape sequences with 'esckeys' off.
448 out_str(T_BD);
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000449 out_str_t_TE();
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000450 }
451
452 *cp = plain_vgetc();
453
Bram Moolenaar24959102022-05-07 20:01:16 +0100454 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000455 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100456 MAY_WANT_TO_LOG_THIS;
457
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000458 // Re-enable bracketed paste mode and modifyOtherKeys
Bram Moolenaarfc966c12023-01-01 18:04:33 +0000459 out_str_t_BE();
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000460 out_str_t_TI();
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000461 }
462
463 if (langmap_active)
464 {
465 // Undo the decrement done above
466 ++no_mapping;
467 ++allow_keys;
Bram Moolenaar24959102022-05-07 20:01:16 +0100468 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000469 }
470#ifdef HAVE_INPUT_METHOD
471 if (lang)
472 {
473 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
474 im_save_status(&curbuf->b_p_iminsert);
475 im_set_active(FALSE);
476 }
477 p_smd = save_smd;
478#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100479 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000480 *need_flushbuf |= add_to_showcmd(*cp);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000481
482 if (!lit)
483 {
484#ifdef FEAT_DIGRAPHS
485 // Typing CTRL-K gets a digraph.
486 if (*cp == Ctrl_K
487 && ((nv_cmds[idx].cmd_flags & NV_LANG)
488 || cp == &cap->extra_char)
489 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
490 {
491 c = get_digraph(FALSE);
492 if (c > 0)
493 {
494 *cp = c;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000495 // Guessing how to update showcmd here...
496 del_from_showcmd(3);
497 *need_flushbuf |= add_to_showcmd(*cp);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000498 }
499 }
500#endif
501
502 // adjust chars > 127, except after "tTfFr" commands
503 LANGMAP_ADJUST(*cp, !lang);
504#ifdef FEAT_RIGHTLEFT
505 // adjust Hebrew mapped char
506 if (p_hkmap && lang && KeyTyped)
507 *cp = hkmap(*cp);
508#endif
509 }
510
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000511 // When the next character is CTRL-\ a following CTRL-N means the
512 // command is aborted and we go to Normal mode.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000513 if (cp == &cap->extra_char
514 && cap->nchar == Ctrl_BSL
515 && (cap->extra_char == Ctrl_N || cap->extra_char == Ctrl_G))
516 {
517 cap->cmdchar = Ctrl_BSL;
518 cap->nchar = cap->extra_char;
519 idx = find_command(cap->cmdchar);
520 }
zeertzjq4f026ea2023-02-26 14:47:24 +0000521 else if ((cap->nchar == 'n' || cap->nchar == 'N')
522 && cap->cmdchar == 'g')
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000523 cap->oap->op_type = get_op_type(*cp, NUL);
524 else if (*cp == Ctrl_BSL)
525 {
526 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
527
528 // There is a busy wait here when typing "f<C-\>" and then
529 // something different from CTRL-N. Can't be avoided.
530 while ((c = vpeekc()) <= 0 && towait > 0L)
531 {
532 do_sleep(towait > 50L ? 50L : towait, FALSE);
533 towait -= 50L;
534 }
535 if (c > 0)
536 {
537 c = plain_vgetc();
538 if (c != Ctrl_N && c != Ctrl_G)
539 vungetc(c);
540 else
541 {
542 cap->cmdchar = Ctrl_BSL;
543 cap->nchar = c;
544 idx = find_command(cap->cmdchar);
545 }
546 }
547 }
548
zeertzjqbacc8302023-08-12 00:09:31 +0200549 if (enc_utf8 && lang)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000550 {
zeertzjqbacc8302023-08-12 00:09:31 +0200551 // When getting a text character and the next character is a
552 // multi-byte character, it could be a composing character.
553 // However, don't wait for it to arrive. Also, do enable mapping,
554 // because if it's put back with vungetc() it's too late to apply
555 // mapping.
556 --no_mapping;
557 while ((c = vpeekc()) > 0
558 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000559 {
zeertzjqbacc8302023-08-12 00:09:31 +0200560 c = plain_vgetc();
561 if (!utf_iscomposing(c))
562 {
563 vungetc(c); // it wasn't, put it back
564 break;
565 }
566 else if (cap->ncharC1 == 0)
567 cap->ncharC1 = c;
568 else
569 cap->ncharC2 = c;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000570 }
zeertzjqbacc8302023-08-12 00:09:31 +0200571 ++no_mapping;
572 // Vim may be in a different mode when the user types the next key,
573 // but when replaying a recording the next key is already in the
zeertzjqbf321802024-01-28 19:03:00 +0100574 // typeahead buffer, so record an <Ignore> before that to prevent
575 // the vpeekc() above from applying wrong mappings when replaying.
zeertzjqdccc29c2023-09-04 22:25:07 +0200576 ++no_u_sync;
zeertzjqbf321802024-01-28 19:03:00 +0100577 gotchars_ignore();
zeertzjqdccc29c2023-09-04 22:25:07 +0200578 --no_u_sync;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000579 }
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000580 }
581 --no_mapping;
582 --allow_keys;
583
584 return idx;
585}
586
587/*
588 * Returns TRUE if after processing a normal mode command, need to wait for a
589 * moment when a message is displayed that will be overwritten by the mode
590 * message.
591 */
592 static int
593normal_cmd_need_to_wait_for_msg(cmdarg_T *cap, pos_T *old_pos)
594{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000595 // In Visual mode and with "^O" in Insert mode, a short message will be
596 // overwritten by the mode message. Wait a bit, until a key is hit.
597 // In Visual mode, it's more important to keep the Visual area updated
598 // than keeping a message (e.g. from a /pat search).
599 // Only do this if the command was typed, not from a mapping.
600 // Don't wait when emsg_silent is non-zero.
601 // Also wait a bit after an error message, e.g. for "^O:".
602 // Don't redraw the screen, it would remove the message.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000603 return ( ((p_smd
604 && msg_silent == 0
605 && (restart_edit != 0
606 || (VIsual_active
607 && old_pos->lnum == curwin->w_cursor.lnum
608 && old_pos->col == curwin->w_cursor.col)
609 )
610 && (clear_cmdline
611 || redraw_cmdline)
612 && (msg_didout || (msg_didany && msg_scroll))
613 && !msg_nowait
614 && KeyTyped)
615 || (restart_edit != 0
616 && !VIsual_active
617 && (msg_scroll
618 || emsg_on_display)))
619 && cap->oap->regname == 0
620 && !(cap->retval & CA_COMMAND_BUSY)
621 && stuff_empty()
622 && typebuf_typed()
623 && emsg_silent == 0
624 && !in_assert_fails
625 && !did_wait_return
626 && cap->oap->op_type == OP_NOP);
627}
628
629/*
630 * After processing a normal mode command, wait for a moment when a message is
631 * displayed that will be overwritten by the mode message.
632 */
633 static void
634normal_cmd_wait_for_msg(void)
635{
636 int save_State = State;
637
638 // Draw the cursor with the right shape here
639 if (restart_edit != 0)
Bram Moolenaar24959102022-05-07 20:01:16 +0100640 State = MODE_INSERT;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000641
642 // If need to redraw, and there is a "keep_msg", redraw before the
643 // delay
644 if (must_redraw && keep_msg != NULL && !emsg_on_display)
645 {
646 char_u *kmsg;
647
648 kmsg = keep_msg;
649 keep_msg = NULL;
650 // Showmode() will clear keep_msg, but we want to use it anyway.
651 // First update w_topline.
652 setcursor();
653 update_screen(0);
654 // now reset it, otherwise it's put in the history again
655 keep_msg = kmsg;
656
657 kmsg = vim_strsave(keep_msg);
658 if (kmsg != NULL)
659 {
660 msg_attr((char *)kmsg, keep_msg_attr);
661 vim_free(kmsg);
662 }
663 }
664 setcursor();
665#ifdef CURSOR_SHAPE
666 ui_cursor_shape(); // may show different cursor shape
667#endif
668 cursor_on();
669 out_flush();
670 if (msg_scroll || emsg_on_display)
671 ui_delay(1003L, TRUE); // wait at least one second
672 ui_delay(3003L, FALSE); // wait up to three seconds
673 State = save_State;
674
675 msg_scroll = FALSE;
676 emsg_on_display = FALSE;
677}
678
679/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 * Execute a command in Normal mode.
681 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100683normal_cmd(
684 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100685 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100687 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100689 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 int old_col = curwin->w_curswant;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000691 int need_flushbuf = FALSE; // need to call out_flush()
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100692 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 static int old_mapped_len = 0;
695 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000696 int set_prevcount = FALSE;
Bram Moolenaarb146e012020-07-19 23:06:05 +0200697 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
Bram Moolenaara80faa82020-04-12 19:37:17 +0200699 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000701
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100702 // Use a count remembered from before entering an operator. After typing
703 // "3d" we return from normal_cmd() and come back here, the "3" is
704 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 ca.opcount = opcount;
706
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000707 // If there is an operator pending, then the command we take this time
708 // will terminate it. Finish_op tells us to finish the operation before
709 // returning this time (unless the operation was cancelled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710#ifdef CURSOR_SHAPE
711 c = finish_op;
712#endif
713 finish_op = (oap->op_type != OP_NOP);
714#ifdef CURSOR_SHAPE
715 if (finish_op != c)
716 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100717 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718# ifdef FEAT_MOUSESHAPE
719 update_mouseshape(-1);
720# endif
721 }
722#endif
LemonBoy2bf52dd2022-04-09 18:17:34 +0100723 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100725 // When not finishing an operator and no register name typed, reset the
726 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000728 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000730#ifdef FEAT_EVAL
731 set_prevcount = TRUE;
732#endif
733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100735 // Restore counts from before receiving K_CURSORHOLD. This means after
736 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
737 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000738 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
739 {
740 ca.opcount = oap->prev_opcount;
741 ca.count0 = oap->prev_count0;
742 oap->prev_opcount = 0;
743 oap->prev_count0 = 0;
744 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000745
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747
Bram Moolenaar24959102022-05-07 20:01:16 +0100748 State = MODE_NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100750 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751#endif
752
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100753#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100754 // Set v:count here, when called from main() and not a stuffed
755 // command, so that v:count can be used in an expression mapping
756 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100757 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100758 set_vcount_ca(&ca, &set_prevcount);
759#endif
760
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 /*
762 * Get the command character from the user.
763 */
764 c = safe_vgetc();
Bram Moolenaar24959102022-05-07 20:01:16 +0100765 LANGMAP_ADJUST(c, get_real_state() != MODE_SELECT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000767 // If a mapping was started in Visual or Select mode, remember the length
768 // of the mapping. This is used below to not return to Insert mode for as
769 // long as the mapping is being executed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 if (restart_edit == 0)
771 old_mapped_len = 0;
772 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000773 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 old_mapped_len = typebuf_maplen();
775
776 if (c == NUL)
777 c = K_ZERO;
778
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000779 // In Select mode, typed text replaces the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 if (VIsual_active
781 && VIsual_select
782 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
783 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000784 int len;
785
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100786 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
787 // 'insertmode' is set) fake a "d"elete command, Insert mode will
788 // restart automatically.
789 // Insert the typed character in the typeahead buffer, so that it can
790 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000791 len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
792
zeertzjqfbf4f1c2022-01-28 12:50:43 +0000793 // When recording and gotchars() was called the character will be
794 // recorded again, remove the previous recording.
795 if (KeyTyped)
796 ungetchars(len);
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000797
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000798 if (restart_edit != 0)
799 c = 'd';
800 else
801 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100802 msg_nowait = TRUE; // don't delay going to insert mode
803 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805
Bram Moolenaard0fb9072021-12-09 11:57:22 +0000806 // If the window was made so small that nothing shows, make it at least one
807 // line and one column when typing a command.
808 if (KeyTyped && !KeyStuffed)
809 win_ensure_size();
810
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 need_flushbuf = add_to_showcmd(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000813 // Get the command count
814 c = normal_cmd_get_count(&ca, c, toplevel, set_prevcount, &ctrl_w,
815 &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000817 // Find the command character in the table of commands.
818 // For CTRL-W we already got nchar when looking for a count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 if (ctrl_w)
820 {
821 ca.nchar = c;
822 ca.cmdchar = Ctrl_W;
823 }
824 else
825 ca.cmdchar = c;
826 idx = find_command(ca.cmdchar);
827 if (idx < 0)
828 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100829 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 clearopbeep(oap);
831 goto normal_end;
832 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000833
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000834 if ((nv_cmds[idx].cmd_flags & NV_NCW) && check_text_or_curbuf_locked(oap))
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100835 // this command is not allowed now
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000836 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000838 // In Visual/Select mode, a few keys are handled in a special way.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 if (VIsual_active)
840 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100841 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 if (km_stopsel
843 && (nv_cmds[idx].cmd_flags & NV_STS)
844 && !(mod_mask & MOD_MASK_SHIFT))
845 {
846 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100847 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 }
849
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100850 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 if (km_startsel)
852 {
853 if (nv_cmds[idx].cmd_flags & NV_SS)
854 {
855 unshift_special(&ca);
856 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000857 if (idx < 0)
858 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100859 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000860 clearopbeep(oap);
861 goto normal_end;
862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 }
864 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
865 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 }
868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869
870#ifdef FEAT_RIGHTLEFT
871 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
872 && (nv_cmds[idx].cmd_flags & NV_RL))
873 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100874 // Invert horizontal movements and operations. Only when typed by the
875 // user directly, not when the result of a mapping or "x" translated
876 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 switch (ca.cmdchar)
878 {
879 case 'l': ca.cmdchar = 'h'; break;
880 case K_RIGHT: ca.cmdchar = K_LEFT; break;
881 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
882 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
883 case 'h': ca.cmdchar = 'l'; break;
884 case K_LEFT: ca.cmdchar = K_RIGHT; break;
885 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
886 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
887 case '>': ca.cmdchar = '<'; break;
888 case '<': ca.cmdchar = '>'; break;
889 }
890 idx = find_command(ca.cmdchar);
891 }
892#endif
893
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000894 // Get additional characters if we need them.
895 if (normal_cmd_needs_more_chars(&ca, nv_cmds[idx].cmd_flags))
896 idx = normal_cmd_get_more_chars(idx, &ca, &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000898 // Flush the showcmd characters onto the screen so we can see them while
899 // the command is being executed. Only do this when the shown command was
900 // actually displayed, otherwise this will slow down a lot when executing
901 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 if (need_flushbuf)
903 out_flush();
Martin Tournoijba43e762022-10-13 22:12:15 +0100904
Bram Moolenaard9205ca2008-10-02 20:55:54 +0000905 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +0200906 {
907 if (ex_normal_busy)
908 did_cursorhold = save_did_cursorhold;
909 else
910 did_cursorhold = FALSE;
911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912
Bram Moolenaar24959102022-05-07 20:01:16 +0100913 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914
zeertzjqf86dea82023-03-05 21:15:06 +0000915 if (ca.nchar == ESC || ca.extra_char == ESC)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 {
917 clearop(oap);
918 if (restart_edit == 0 && goto_im())
919 restart_edit = 'a';
920 goto normal_end;
921 }
922
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000923 if (ca.cmdchar != K_IGNORE)
924 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100925 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000926 msg_col = 0;
927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928
Luuk van Baalaa7f25e2023-04-13 21:47:38 +0100929 old_pos = curwin->w_cursor; // remember where the cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100931 // When 'keymodel' contains "startsel" some keys start Select/Visual
932 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 if (!VIsual_active && km_startsel)
934 {
935 if (nv_cmds[idx].cmd_flags & NV_SS)
936 {
937 start_selection();
938 unshift_special(&ca);
939 idx = find_command(ca.cmdchar);
940 }
941 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
942 && (mod_mask & MOD_MASK_SHIFT))
943 {
944 start_selection();
945 mod_mask &= ~MOD_MASK_SHIFT;
946 }
947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000949 // Execute the command!
950 // Call the command function found in the commands table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 ca.arg = nv_cmds[idx].cmd_arg;
952 (nv_cmds[idx].cmd_func)(&ca);
953
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000954 // If we didn't start or finish an operator, reset oap->regname, unless we
955 // need it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 if (!finish_op
957 && !oap->op_type
958 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
959 {
960 clearop(oap);
961#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +0200962 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963#endif
964 }
965
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100966 // Get the length of mapped chars again after typing a count, second
967 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000968 if (old_mapped_len > 0)
969 old_mapped_len = typebuf_maplen();
970
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000971 // If an operation is pending, handle it. But not for K_IGNORE or
972 // K_MOUSEMOVE.
Bram Moolenaar1ad72c82021-05-04 21:56:28 +0200973 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +0100974 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000976 // Wait for a moment when a message is displayed that will be overwritten
977 // by the mode message.
978 if (normal_cmd_need_to_wait_for_msg(&ca, &old_pos))
979 normal_cmd_wait_for_msg();
980
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000981 // Finish up after executing a Normal mode command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982normal_end:
983
984 msg_nowait = FALSE;
985
Bram Moolenaarcc613032020-06-07 21:31:18 +0200986#ifdef FEAT_EVAL
987 if (finish_op)
988 reset_reg_var();
989#endif
990
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991#ifdef CURSOR_SHAPE
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000992 int prev_finish_op = finish_op;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#endif
zeertzjq73916ba2023-04-26 16:50:19 +0100994 if (oap->op_type == OP_NOP)
995 {
996 // Reset finish_op, in case it was set
997 finish_op = FALSE;
998 may_trigger_modechanged();
999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001001 // Redraw the cursor with another shape, if we were in Operator-pending
1002 // mode or did a replace command.
zeertzjqf86dea82023-03-05 21:15:06 +00001003 if (prev_finish_op || ca.cmdchar == 'r'
1004 || (ca.cmdchar == 'g' && ca.nchar == 'r'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001006 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007# ifdef FEAT_MOUSESHAPE
1008 update_mouseshape(-1);
1009# endif
1010 }
1011#endif
1012
Bram Moolenaara983fe92008-07-31 20:04:27 +00001013 if (oap->op_type == OP_NOP && oap->regname == 0
zeertzjq73916ba2023-04-26 16:50:19 +01001014 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001017 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 vim_free(ca.searchbuf);
1019
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 if (has_mbyte)
1021 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 if (curwin->w_p_scb && toplevel)
1024 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001025 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 do_check_scrollbind(TRUE);
1027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028
Bram Moolenaar860cae12010-06-05 23:22:07 +02001029 if (curwin->w_p_crb && toplevel)
1030 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001031 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001032 do_check_cursorbind();
1033 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001034
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001035#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001036 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001037 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001038 restart_edit = 0;
1039#endif
1040
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001041 // May restart edit(), if we got here with CTRL-O in Insert mode (but not
1042 // if still inside a mapping that started in Visual mode).
1043 // May switch from Visual to Select mode after CTRL-O command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1046 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 && !(ca.retval & CA_COMMAND_BUSY)
1048 && stuff_empty()
1049 && oap->regname == 0)
1050 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 if (restart_VIsual_select == 1)
1052 {
1053 VIsual_select = TRUE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01001054 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 showmode();
1056 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001057 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001059 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 (void)edit(restart_edit, FALSE, 1L);
1061 }
1062
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 if (restart_VIsual_select == 2)
1064 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001066 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 opcount = ca.opcount;
1068}
1069
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001070#ifdef FEAT_EVAL
1071/*
1072 * Set v:count and v:count1 according to "cap".
1073 * Set v:prevcount only when "set_prevcount" is TRUE.
1074 */
1075 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001076set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001077{
1078 long count = cap->count0;
1079
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001080 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001081 if (cap->opcount != 0)
1082 count = cap->opcount * (count == 0 ? 1 : count);
1083 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001084 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001085}
1086#endif
1087
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001089 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 * if not.
1091 */
1092 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001093check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
1095 static int did_check = FALSE;
1096
1097 if (full_screen)
1098 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001099 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001100 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 did_check = TRUE;
1102 }
1103}
1104
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001105#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1106/*
1107 * Call yank_do_autocmd() for "regname".
1108 */
1109 static void
1110call_yank_do_autocmd(int regname)
1111{
1112 oparg_T oa;
1113 yankreg_T *reg;
1114
1115 clear_oparg(&oa);
1116 oa.regname = regname;
1117 oa.op_type = OP_YANK;
1118 oa.is_VIsual = TRUE;
1119 reg = get_register(regname, TRUE);
1120 yank_do_autocmd(&oa, reg);
1121 free_register(reg);
1122}
1123#endif
1124
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001126 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001127 * This function or the next should ALWAYS be called to end Visual mode, except
1128 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 */
1130 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001131end_visual_mode(void)
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001132{
Jim Zhouc8cce712025-03-05 20:47:29 +01001133 VIsual_select_exclu_adj = FALSE;
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001134 end_visual_mode_keep_button();
1135 reset_held_button();
1136}
1137
1138 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001139end_visual_mode_keep_button(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140{
1141#ifdef FEAT_CLIPBOARD
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001142 // If we are using the clipboard, then remember what was selected in case
1143 // we need to paste it somewhere while we still own the selection.
1144 // Only do this when the clipboard is already owned. Don't want to grab
1145 // the selection when hitting ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 if (clip_star.available && clip_star.owned)
1147 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001148
1149# if defined(FEAT_EVAL)
1150 // Emit a TextYankPost for the automatic copy of the selection into the
1151 // star and/or plus register.
1152 if (has_textyankpost())
1153 {
1154 if (clip_isautosel_star())
1155 call_yank_do_autocmd('*');
1156 if (clip_isautosel_plus())
1157 call_yank_do_autocmd('+');
1158 }
1159# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160#endif
1161
1162 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 setmouse();
1164 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001166 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001167 curbuf->b_visual.vi_mode = VIsual_mode;
1168 curbuf->b_visual.vi_start = VIsual;
1169 curbuf->b_visual.vi_end = curwin->w_cursor;
1170 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171#ifdef FEAT_EVAL
1172 curbuf->b_visual_mode_eval = VIsual_mode;
1173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 if (!virtual_active())
1175 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001176 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001178 adjust_cursor_eol();
LemonBoy2bf52dd2022-04-09 18:17:34 +01001179 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180}
1181
1182/*
1183 * Reset VIsual_active and VIsual_reselect.
1184 */
1185 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001186reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187{
1188 if (VIsual_active)
1189 {
1190 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001191 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 }
1193 VIsual_reselect = FALSE;
1194}
1195
1196/*
1197 * Reset VIsual_active and VIsual_reselect if it's set.
1198 */
1199 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001200reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201{
1202 if (VIsual_active)
1203 {
1204 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001205 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 VIsual_reselect = FALSE;
1207 }
1208}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001210 void
1211restore_visual_mode(void)
1212{
1213 if (VIsual_mode_orig != NUL)
1214 {
1215 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1216 VIsual_mode_orig = NUL;
1217 }
1218}
1219
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220/*
1221 * Check for a balloon-eval special item to include when searching for an
1222 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1223 * Returns TRUE if the character at "*ptr" should be included.
1224 * "dir" is FORWARD or BACKWARD, the direction of searching.
1225 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1226 * "bnp" points to a counter for square brackets.
1227 */
1228 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001229find_is_eval_item(
1230 char_u *ptr,
1231 int *colp,
1232 int *bnp,
1233 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001235 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1237 ++*bnp;
1238 if (*bnp > 0)
1239 {
1240 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1241 --*bnp;
1242 return TRUE;
1243 }
1244
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001245 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 if (*ptr == '.')
1247 return TRUE;
1248
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001249 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1251 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1252 {
1253 *colp += dir;
1254 return TRUE;
1255 }
1256 return FALSE;
1257}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258
1259/*
1260 * Find the identifier under or to the right of the cursor.
1261 * "find_type" can have one of three values:
1262 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001263 * FIND_STRING: find any non-white text
1264 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001265 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 *
1267 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001268 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001270 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 * This doesn't match the real Vi but I like it a little better and it
1272 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001273 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 * When FIND_IDENT isn't defined, we backup until a blank.
1275 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001276 * Returns the length of the text, or zero if no text is found.
1277 * If text is found, a pointer to the text is put in "*text". This
1278 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 */
1280 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001281find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282{
1283 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001284 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285}
1286
1287/*
1288 * Like find_ident_under_cursor(), but for any window and any position.
1289 * However: Uses 'iskeyword' from the current window!.
1290 */
1291 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001292find_ident_at_pos(
1293 win_T *wp,
1294 linenr_T lnum,
1295 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001296 char_u **text,
1297 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001298 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299{
1300 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001301 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 int this_class = 0;
1304 int prev_class;
1305 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001306 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001308 // if i == 0: try to find an identifier
1309 // if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1311 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1312 {
1313 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001314 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 */
1316 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 if (has_mbyte)
1318 {
1319 while (ptr[col] != NUL)
1320 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001321 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1323 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 this_class = mb_get_class(ptr + col);
1325 if (this_class != 0 && (i == 1 || this_class != 1))
1326 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001327 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
1329 }
1330 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001332 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 )
1335 ++col;
1336
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001337 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339
1340 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001341 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 if (has_mbyte)
1344 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001345 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1347 this_class = mb_get_class((char_u *)"a");
1348 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001350 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 {
1352 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1353 prev_class = mb_get_class(ptr + prevcol);
1354 if (this_class != prev_class
1355 && (i == 0
1356 || prev_class == 0
1357 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 && (!(find_type & FIND_EVAL)
1359 || prevcol == 0
1360 || !find_is_eval_item(ptr + prevcol, &prevcol,
1361 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 )
1363 break;
1364 col = prevcol;
1365 }
1366
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001367 // If we don't want just any old text, or we've found an
1368 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 if (this_class > 2)
1370 this_class = 2;
1371 if (!(find_type & FIND_STRING) || this_class == 2)
1372 break;
1373 }
1374 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 {
1376 while (col > 0
1377 && ((i == 0
1378 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001379 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 && (!(find_type & FIND_IDENT)
1381 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 || ((find_type & FIND_EVAL)
1383 && col > 1
1384 && find_is_eval_item(ptr + col - 1, &col,
1385 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 ))
1387 --col;
1388
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001389 // If we don't want just any old text, or we've found an
1390 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1392 break;
1393 }
1394 }
1395
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001396 if (ptr[col] == NUL || (i == 0
1397 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001399 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001400 if ((find_type & FIND_NOERROR) == 0)
1401 {
1402 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001403 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001404 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001405 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 return 0;
1408 }
1409 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001410 *text = ptr;
1411 if (textcol != NULL)
1412 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413
1414 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001415 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 bn = 0;
1418 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 if (has_mbyte)
1421 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001422 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 this_class = mb_get_class(ptr);
1424 while (ptr[col] != NUL
1425 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1426 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 || ((find_type & FIND_EVAL)
1428 && col <= (int)startcol
1429 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001431 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 }
1433 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001435 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 || ((find_type & FIND_EVAL)
1437 && col <= (int)startcol
1438 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441
1442 return col;
1443}
1444
1445/*
1446 * Prepare for redo of a normal command.
1447 */
1448 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001449prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450{
1451 prep_redo(cap->oap->regname, cap->count0,
1452 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1453}
1454
1455/*
1456 * Prepare for redo of any command.
1457 * Note that only the last argument can be a multi-byte char.
1458 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001459 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001460prep_redo(
1461 int regname,
1462 long num,
1463 int cmd1,
1464 int cmd2,
1465 int cmd3,
1466 int cmd4,
1467 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001469 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1470}
1471
1472/*
1473 * Prepare for redo of any command with extra count after "cmd2".
1474 */
1475 void
1476prep_redo_num2(
1477 int regname,
1478 long num1,
1479 int cmd1,
1480 int cmd2,
1481 long num2,
1482 int cmd3,
1483 int cmd4,
1484 int cmd5)
1485{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 ResetRedobuff();
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001487
1488#ifdef FEAT_EVAL
1489 // Put info about a mapping in the redo buffer, so that "." will use the
1490 // same script context.
1491 may_add_last_used_map_to_redobuff();
1492#endif
1493
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001494 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 {
1496 AppendCharToRedobuff('"');
1497 AppendCharToRedobuff(regname);
1498 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001499 if (num1 != 0)
1500 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 if (cmd1 != NUL)
1502 AppendCharToRedobuff(cmd1);
1503 if (cmd2 != NUL)
1504 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001505 if (num2 != 0)
1506 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 if (cmd3 != NUL)
1508 AppendCharToRedobuff(cmd3);
1509 if (cmd4 != NUL)
1510 AppendCharToRedobuff(cmd4);
1511 if (cmd5 != NUL)
1512 AppendCharToRedobuff(cmd5);
1513}
1514
1515/*
zeertzjq73916ba2023-04-26 16:50:19 +01001516 * Check for operator active and clear it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 *
zeertzjq73916ba2023-04-26 16:50:19 +01001518 * Beep and return TRUE if an operator was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 */
1520 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001521checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
1523 if (oap->op_type == OP_NOP)
1524 return FALSE;
1525 clearopbeep(oap);
1526 return TRUE;
1527}
1528
1529/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001530 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 *
zeertzjq73916ba2023-04-26 16:50:19 +01001532 * Beep and return TRUE if an operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 */
1534 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001535checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001537 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 return FALSE;
1539 clearopbeep(oap);
1540 return TRUE;
1541}
1542
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001543 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001544clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545{
1546 oap->op_type = OP_NOP;
1547 oap->regname = 0;
1548 oap->motion_force = NUL;
1549 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001550 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551}
1552
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001553 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001554clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555{
1556 clearop(oap);
1557 beep_flush();
1558}
1559
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560/*
1561 * Remove the shift modifier from a special key.
1562 */
1563 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001564unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565{
1566 switch (cap->cmdchar)
1567 {
1568 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1569 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1570 case K_S_UP: cap->cmdchar = K_UP; break;
1571 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1572 case K_S_HOME: cap->cmdchar = K_HOME; break;
1573 case K_S_END: cap->cmdchar = K_END; break;
1574 }
1575 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1576}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001578/*
1579 * If the mode is currently displayed clear the command line or update the
1580 * command displayed.
1581 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001582 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001583may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001584{
1585 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001586 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001587 else
1588 clear_showcmd();
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001589}
1590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591/*
1592 * Routines for displaying a partly typed command
1593 */
1594
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001595static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596static int showcmd_is_clear = TRUE;
1597static int showcmd_visual = FALSE;
1598
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001599static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600
1601 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001602clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
1604 if (!p_sc)
1605 return;
1606
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 if (VIsual_active && !char_avail())
1608 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001609 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 long lines;
1611 colnr_T leftcol, rightcol;
1612 linenr_T top, bot;
1613
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001614 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001615 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 {
1617 top = VIsual.lnum;
1618 bot = curwin->w_cursor.lnum;
1619 }
1620 else
1621 {
1622 top = curwin->w_cursor.lnum;
1623 bot = VIsual.lnum;
1624 }
1625# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001626 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001627 (void)hasFolding(top, &top, NULL);
1628 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629# endif
1630 lines = bot - top + 1;
1631
1632 if (VIsual_mode == Ctrl_V)
1633 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001634# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001635 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001636 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001637
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001638 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001639 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001640 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001641# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001643# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001644 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001645 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001646# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1648 (long)(rightcol - leftcol + 1));
1649 }
1650 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1651 sprintf((char *)showcmd_buf, "%ld", lines);
1652 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001653 {
1654 char_u *s, *e;
1655 int l;
1656 int bytes = 0;
1657 int chars = 0;
1658
1659 if (cursor_bot)
1660 {
1661 s = ml_get_pos(&VIsual);
1662 e = ml_get_cursor();
1663 }
1664 else
1665 {
1666 s = ml_get_cursor();
1667 e = ml_get_pos(&VIsual);
1668 }
1669 while ((*p_sel != 'e') ? s <= e : s < e)
1670 {
1671 l = (*mb_ptr2len)(s);
1672 if (l == 0)
1673 {
1674 ++bytes;
1675 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001676 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001677 }
1678 bytes += l;
1679 ++chars;
1680 s += l;
1681 }
1682 if (bytes == chars)
1683 sprintf((char *)showcmd_buf, "%d", chars);
1684 else
1685 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1686 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001687 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 showcmd_visual = TRUE;
1689 }
1690 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 {
1692 showcmd_buf[0] = NUL;
1693 showcmd_visual = FALSE;
1694
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001695 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 if (showcmd_is_clear)
1697 return;
1698 }
1699
1700 display_showcmd();
1701}
1702
1703/*
1704 * Add 'c' to string of shown command chars.
1705 * Return TRUE if output has been written (and setcursor() has been called).
1706 */
1707 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001708add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709{
1710 char_u *p;
1711 int old_len;
1712 int extra_len;
1713 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 int i;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001715 char_u mbyte_buf[MB_MAXBYTES];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 static int ignore[] =
1717 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001718#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1720 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001721#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001722 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001723 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1725 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001726 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001728 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 0
1730 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731
Bram Moolenaar09df3122006-01-23 22:23:09 +00001732 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 return FALSE;
1734
1735 if (showcmd_visual)
1736 {
1737 showcmd_buf[0] = NUL;
1738 showcmd_visual = FALSE;
1739 }
1740
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001741 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 if (IS_SPECIAL(c))
1743 for (i = 0; ignore[i] != 0; ++i)
1744 if (ignore[i] == c)
1745 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
zeertzjqacdfb8a2024-04-17 21:28:54 +02001747 if (c <= 0x7f || !vim_isprintc(c))
1748 {
1749 p = transchar(c);
1750 if (*p == ' ')
1751 STRCPY(p, "<20>");
1752 }
1753 else
1754 {
1755 mbyte_buf[(*mb_char2bytes)(c, mbyte_buf)] = NUL;
1756 p = mbyte_buf;
1757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 old_len = (int)STRLEN(showcmd_buf);
1759 extra_len = (int)STRLEN(p);
1760 overflow = old_len + extra_len - SHOWCMD_COLS;
1761 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001762 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1763 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 STRCAT(showcmd_buf, p);
1765
1766 if (char_avail())
1767 return FALSE;
1768
1769 display_showcmd();
1770
1771 return TRUE;
1772}
1773
1774 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001775add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776{
1777 if (!add_to_showcmd(c))
1778 setcursor();
1779}
1780
1781/*
1782 * Delete 'len' characters from the end of the shown command.
1783 */
1784 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001785del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
1787 int old_len;
1788
1789 if (!p_sc)
1790 return;
1791
1792 old_len = (int)STRLEN(showcmd_buf);
1793 if (len > old_len)
1794 len = old_len;
1795 showcmd_buf[old_len - len] = NUL;
1796
1797 if (!char_avail())
1798 display_showcmd();
1799}
1800
1801/*
1802 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1803 * something and there is a partial mapping.
1804 */
1805 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001806push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807{
1808 if (p_sc)
1809 STRCPY(old_showcmd_buf, showcmd_buf);
1810}
1811
1812 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001813pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814{
1815 if (!p_sc)
1816 return;
1817
1818 STRCPY(showcmd_buf, old_showcmd_buf);
1819
1820 display_showcmd();
1821}
1822
1823 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001824display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825{
zeertzjqacdfb8a2024-04-17 21:28:54 +02001826 int len = vim_strsize(showcmd_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827
Luuk van Baalba936f62022-12-15 13:15:39 +00001828 showcmd_is_clear = (len == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 cursor_off();
1830
Luuk van Baalba936f62022-12-15 13:15:39 +00001831 if (*p_sloc == 's')
Luuk van Baalaa7f25e2023-04-13 21:47:38 +01001832 {
1833 if (showcmd_is_clear)
1834 curwin->w_redr_status = TRUE;
1835 else
1836 win_redr_status(curwin, FALSE);
1837 }
Luuk van Baalba936f62022-12-15 13:15:39 +00001838 else if (*p_sloc == 't')
Luuk van Baalaa7f25e2023-04-13 21:47:38 +01001839 {
1840 if (showcmd_is_clear)
1841 redraw_tabline = TRUE;
1842 else
1843 draw_tabline();
1844 }
Luuk van Baalba936f62022-12-15 13:15:39 +00001845 else // 'showcmdloc' is "last" or empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 {
Luuk van Baalba936f62022-12-15 13:15:39 +00001847 if (!showcmd_is_clear)
1848 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849
Luuk van Baalba936f62022-12-15 13:15:39 +00001850 // clear the rest of an old message by outputting up to SHOWCMD_COLS
1851 // spaces
1852 screen_puts((char_u *)" " + len,
1853 (int)Rows - 1, sc_col + len, 0);
1854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001856 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859/*
1860 * When "check" is FALSE, prepare for commands that scroll the window.
1861 * When "check" is TRUE, take care of scroll-binding after the window has
1862 * scrolled. Called from normal_cmd() and edit().
1863 */
1864 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001865do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866{
1867 static win_T *old_curwin = NULL;
1868 static linenr_T old_topline = 0;
1869#ifdef FEAT_DIFF
1870 static int old_topfill = 0;
1871#endif
1872 static buf_T *old_buf = NULL;
1873 static colnr_T old_leftcol = 0;
1874
1875 if (check && curwin->w_p_scb)
1876 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001877 // If a ":syncbind" command was just used, don't scroll, only reset
1878 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 if (did_syncbind)
1880 did_syncbind = FALSE;
1881 else if (curwin == old_curwin)
1882 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001883 // Synchronize other windows, as necessary according to
1884 // 'scrollbind'. Don't do this after an ":edit" command, except
1885 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 if ((curwin->w_buffer == old_buf
1887#ifdef FEAT_DIFF
1888 || curwin->w_p_diff
1889#endif
1890 )
1891 && (curwin->w_topline != old_topline
1892#ifdef FEAT_DIFF
1893 || curwin->w_topfill != old_topfill
1894#endif
1895 || curwin->w_leftcol != old_leftcol))
1896 {
1897 check_scrollbind(curwin->w_topline - old_topline,
1898 (long)(curwin->w_leftcol - old_leftcol));
1899 }
1900 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001901 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001903 // When switching between windows, make sure that the relative
1904 // vertical offset is valid for the new window. The relative
1905 // offset is invalid whenever another 'scrollbind' window has
1906 // scrolled to a point that would force the current window to
1907 // scroll past the beginning or end of its buffer. When the
1908 // resync is performed, some of the other 'scrollbind' windows may
1909 // need to jump so that the current window's relative position is
1910 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
1912 }
1913 curwin->w_scbind_pos = curwin->w_topline;
1914 }
1915
1916 old_curwin = curwin;
1917 old_topline = curwin->w_topline;
1918#ifdef FEAT_DIFF
1919 old_topfill = curwin->w_topfill;
1920#endif
1921 old_buf = curwin->w_buffer;
1922 old_leftcol = curwin->w_leftcol;
1923}
1924
1925/*
1926 * Synchronize any windows that have "scrollbind" set, based on the
1927 * number of rows by which the current window has changed
1928 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
1929 */
1930 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001931check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932{
1933 int want_ver;
1934 int want_hor;
1935 win_T *old_curwin = curwin;
1936 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 int old_VIsual_select = VIsual_select;
1938 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 colnr_T tgt_leftcol = curwin->w_leftcol;
1940 long topline;
1941 long y;
1942
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001943 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
1945#ifdef FEAT_DIFF
1946 want_ver |= old_curwin->w_p_diff;
1947#endif
1948 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
1949
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001950 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001952 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 {
1954 curbuf = curwin->w_buffer;
zeertzjq101d57b2022-07-31 18:34:32 +01001955 // skip original window and windows with 'noscrollbind'
1956 if (curwin == old_curwin || !curwin->w_p_scb)
1957 continue;
1958
1959 // do the vertical scroll
1960 if (want_ver)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962#ifdef FEAT_DIFF
zeertzjq101d57b2022-07-31 18:34:32 +01001963 if (old_curwin->w_p_diff && curwin->w_p_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
zeertzjq101d57b2022-07-31 18:34:32 +01001965 diff_set_topline(old_curwin, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 }
zeertzjq101d57b2022-07-31 18:34:32 +01001967 else
1968#endif
1969 {
1970 curwin->w_scbind_pos += topline_diff;
1971 topline = curwin->w_scbind_pos;
1972 if (topline > curbuf->b_ml.ml_line_count)
1973 topline = curbuf->b_ml.ml_line_count;
1974 if (topline < 1)
1975 topline = 1;
1976
1977 y = topline - curwin->w_topline;
1978 if (y > 0)
1979 scrollup(y, FALSE);
1980 else
1981 scrolldown(-y, FALSE);
1982 }
1983
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001984 redraw_later(UPD_VALID);
zeertzjq101d57b2022-07-31 18:34:32 +01001985 cursor_correct();
1986 curwin->w_redr_status = TRUE;
1987 }
1988
1989 // do the horizontal scroll
Bram Moolenaar0c34d562022-11-18 14:07:20 +00001990 if (want_hor)
1991 (void)set_leftcol(tgt_leftcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 }
1993
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001994 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 VIsual_select = old_VIsual_select;
1996 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 curwin = old_curwin;
1998 curbuf = old_curbuf;
1999}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000
2001/*
2002 * Command character that's ignored.
2003 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02002004 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002007nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002009 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010}
2011
2012/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002013 * Command character that doesn't do anything, but unlike nv_ignore() does
2014 * start edit(). Used for "startinsert" executed while starting up.
2015 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002016 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002017nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002018{
2019}
2020
2021/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 * Command character doesn't exist.
2023 */
2024 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002025nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026{
2027 clearopbeep(cap->oap);
2028}
2029
2030/*
2031 * <Help> and <F1> commands.
2032 */
2033 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002034nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035{
2036 if (!checkclearopq(cap->oap))
2037 ex_help(NULL);
2038}
2039
2040/*
2041 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2042 */
2043 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002044nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002046#ifdef FEAT_JOB_CHANNEL
2047 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2048 clearopbeep(cap->oap);
2049 else
2050#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002051 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002052 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002053 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002054 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2055 op_addsub(cap->oap, cap->count1, cap->arg);
2056 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002057 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002058 else if (VIsual_active)
2059 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002060 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002061 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062}
2063
2064/*
2065 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2066 */
2067 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002068nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002070 if (checkclearop(cap->oap))
2071 return;
2072
2073 if (mod_mask & MOD_MASK_CTRL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002074 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002075 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
2076 if (cap->arg == BACKWARD)
2077 goto_tabpage(-(int)cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002078 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002079 goto_tabpage((int)cap->count0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002080 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002081 else
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01002082 (void)pagescroll(cap->arg, cap->count1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083}
2084
2085/*
2086 * Implementation of "gd" and "gD" command.
2087 */
2088 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002089nv_gd(
2090 oparg_T *oap,
2091 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002092 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093{
2094 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 char_u *ptr;
2096
Bram Moolenaard9d30582005-05-18 22:10:28 +00002097 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002098 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002099 == FAIL)
2100 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 clearopbeep(oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002102 return;
Bram Moolenaar0c711142021-11-12 10:30:04 +00002103 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002104
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002105#ifdef FEAT_FOLDING
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002106 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2107 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002108#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002109 // clear any search statistics
2110 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2111 clear_cmdline = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002112}
2113
2114/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002115 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2116 * otherwise.
2117 */
2118 static int
2119is_ident(char_u *line, int offset)
2120{
2121 int i;
2122 int incomment = FALSE;
2123 int instring = 0;
2124 int prev = 0;
2125
2126 for (i = 0; i < offset && line[i] != NUL; i++)
2127 {
2128 if (instring != 0)
2129 {
2130 if (prev != '\\' && line[i] == instring)
2131 instring = 0;
2132 }
2133 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2134 {
2135 instring = line[i];
2136 }
2137 else
2138 {
2139 if (incomment)
2140 {
2141 if (prev == '*' && line[i] == '/')
2142 incomment = FALSE;
2143 }
2144 else if (prev == '/' && line[i] == '*')
2145 {
2146 incomment = TRUE;
2147 }
2148 else if (prev == '/' && line[i] == '/')
2149 {
2150 return FALSE;
2151 }
2152 }
2153
2154 prev = line[i];
2155 }
2156
2157 return incomment == FALSE && instring == 0;
2158}
2159
2160/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002161 * Search for variable declaration of "ptr[len]".
2162 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2163 * current file ("gD").
2164 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002165 * Return FAIL when not found.
2166 */
2167 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002168find_decl(
2169 char_u *ptr,
2170 int len,
2171 int locally,
2172 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002173 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002174{
2175 char_u *pat;
John Marriott8c85a2a2024-05-20 19:18:26 +02002176 size_t patlen;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002177 pos_T old_pos;
2178 pos_T par_pos;
2179 pos_T found_pos;
2180 int t;
2181 int save_p_ws;
2182 int save_p_scs;
2183 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002184 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002185 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002186 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002187
2188 if ((pat = alloc(len + 7)) == NULL)
2189 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002190
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002191 // Put "\V" before the pattern to avoid that the special meaning of "."
2192 // and "~" causes trouble.
John Marriott8c85a2a2024-05-20 19:18:26 +02002193 patlen = vim_snprintf((char *)pat, len + 7,
2194 vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s", len, ptr);
2195
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 old_pos = curwin->w_cursor;
2197 save_p_ws = p_ws;
2198 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002199 p_ws = FALSE; // don't wrap around end of file now
2200 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002202 // With "gD" go to line 1.
2203 // With "gd" Search back for the start of the current function, then go
2204 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002205 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002207 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002209 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 }
2211 else
2212 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002213 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2215 --curwin->w_cursor.lnum;
2216 }
2217 curwin->w_cursor.col = 0;
2218
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002219 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002220 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002221 for (;;)
2222 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002223 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
John Marriott8c85a2a2024-05-20 19:18:26 +02002224 pat, patlen, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002225 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002226 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002227
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002228 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002229 {
2230 pos_T *pos;
2231
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002232 // Check that the block the match is in doesn't end before the
2233 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002234 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2235 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2236 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002237 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002238 // There can't be a useful match before the end of this block.
2239 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002240 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002241 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002242 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002243 }
2244
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002245 if (t == FAIL)
2246 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002247 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002248 if (found_pos.lnum != 0)
2249 {
2250 curwin->w_cursor = found_pos;
2251 t = OK;
2252 }
2253 break;
2254 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002255 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002256 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002257 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002258 ++curwin->w_cursor.lnum;
2259 curwin->w_cursor.col = 0;
2260 continue;
2261 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002262 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2263
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002264 // If the current position is not a valid identifier and a previous
2265 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002266 if (!valid && found_pos.lnum != 0)
2267 {
2268 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002269 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002270 }
2271
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002272 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002273 if (valid && !locally)
2274 break;
2275 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002276 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002277 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002278 if (found_pos.lnum != 0)
2279 curwin->w_cursor = found_pos;
2280 break;
2281 }
2282
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002283 // For finding a local variable and the match is before the "{" or
2284 // inside a comment, continue searching. For K&R style function
2285 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002286 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002287 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002288 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002289 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002290 // Remove SEARCH_START from flags to avoid getting stuck at one
2291 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002292 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002294
2295 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002297 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 curwin->w_cursor = old_pos;
2299 }
2300 else
2301 {
2302 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002303 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 reset_search_dir();
2305 }
2306
2307 vim_free(pat);
2308 p_ws = save_p_ws;
2309 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002310
2311 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312}
2313
2314/*
2315 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2316 * lines rather than lines in the file.
2317 * 'dist' must be positive.
2318 *
2319 * Return OK if able to move cursor, FAIL otherwise.
2320 */
Luuk van Baalcb204e62024-04-02 20:49:45 +02002321 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002322nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323{
Dylan Thacker-Smithb2d124c2024-03-24 09:43:25 +01002324
2325 int linelen = linetabsize_no_outer(curwin, curwin->w_cursor.lnum);
2326
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 int retval = OK;
2328 int atend = FALSE;
2329 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002330 int col_off1; // margin offset for first screen line
2331 int col_off2; // margin offset for wrapped screen line
2332 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002333 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334
2335 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002336 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
2338 col_off1 = curwin_col_off();
2339 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002340 width1 = curwin->w_width - col_off1;
2341 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002342 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002343 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002346 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002347 // Instead of sticking at the last character of the buffer line we
2348 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 if (curwin->w_curswant == MAXCOL)
2350 {
2351 atend = TRUE;
2352 validate_virtcol();
2353 if (width1 <= 0)
2354 curwin->w_curswant = 0;
2355 else
2356 {
2357 curwin->w_curswant = width1 - 1;
2358 if (curwin->w_virtcol > curwin->w_curswant)
2359 curwin->w_curswant += ((curwin->w_virtcol
2360 - curwin->w_curswant - 1) / width2 + 1) * width2;
2361 }
2362 }
2363 else
2364 {
2365 if (linelen > width1)
2366 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2367 else
2368 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002369 if (curwin->w_curswant >= (colnr_T)n)
2370 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 }
2372
2373 while (dist--)
2374 {
2375 if (dir == BACKWARD)
2376 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002377 if ((long)curwin->w_curswant >= width1
2378#ifdef FEAT_FOLDING
2379 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2380#endif
2381 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002382 // Move back within the line. This can give a negative value
2383 // for w_curswant if width1 < width2 (with cpoptions+=n),
2384 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 curwin->w_curswant -= width2;
2386 else
2387 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002388 // to previous line
Luuk van Baala109f392023-06-02 14:16:35 +01002389 if (curwin->w_cursor.lnum <= 1)
Bram Moolenaare71996b2021-01-21 17:03:07 +01002390 {
2391 retval = FAIL;
2392 break;
2393 }
Luuk van Baala109f392023-06-02 14:16:35 +01002394 cursor_up_inner(curwin, 1);
2395
Dylan Thacker-Smithb2d124c2024-03-24 09:43:25 +01002396 linelen = linetabsize_no_outer(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 if (linelen > width1)
2398 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2399 + 1) * width2;
2400 }
2401 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002402 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 {
2404 if (linelen > width1)
2405 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2406 else
2407 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002408 if (curwin->w_curswant + width2 < (colnr_T)n
2409#ifdef FEAT_FOLDING
2410 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2411#endif
2412 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002413 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 curwin->w_curswant += width2;
2415 else
2416 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002417 // to next line
Luuk van Baala109f392023-06-02 14:16:35 +01002418 if (curwin->w_cursor.lnum
2419 >= curwin->w_buffer->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 {
2421 retval = FAIL;
2422 break;
2423 }
Luuk van Baala109f392023-06-02 14:16:35 +01002424 cursor_down_inner(curwin, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 curwin->w_curswant %= width2;
Luuk van Baala109f392023-06-02 14:16:35 +01002426
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002427 // Check if the cursor has moved below the number display
2428 // when width1 < width2 (with cpoptions+=n). Subtract width2
2429 // to get a negative value for w_curswant, which will get
2430 // clipped to column 0.
2431 if (curwin->w_curswant >= width1)
2432 curwin->w_curswant -= width2;
Dylan Thacker-Smithb2d124c2024-03-24 09:43:25 +01002433 linelen = linetabsize_no_outer(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 }
2435 }
2436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002439 if (virtual_active() && atend)
2440 coladvance(MAXCOL);
2441 else
2442 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2445 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002446 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002447 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002448
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002449 // Check for landing on a character that got split at the end of the
2450 // last line. We want to advance a screenline, not end up in the same
2451 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002453 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002454#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002455 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2456 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002457#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002458
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002459 c = (*mb_ptr2char)(ml_get_cursor());
2460 if (dir == FORWARD && virtcol < curwin->w_curswant
2461 && (curwin->w_curswant <= (colnr_T)width1)
2462 && !vim_isprintc(c) && c > 255)
2463 oneright();
2464
Bram Moolenaar773b1582014-08-29 14:20:51 +02002465 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 && (curwin->w_curswant < (colnr_T)width1
2467 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2468 : ((curwin->w_curswant - width1) % width2
2469 > (colnr_T)width2 / 2)))
2470 --curwin->w_cursor.col;
2471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472
2473 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002474 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002475 adjust_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476
2477 return retval;
2478}
2479
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480/*
2481 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2482 * cap->arg must be TRUE for CTRL-E.
2483 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002484 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002485nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486{
2487 if (!checkclearop(cap->oap))
2488 scroll_redraw(cap->arg, cap->count1);
2489}
2490
2491/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002492 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2493 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2494 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002495 */
2496 static int
2497nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2498{
2499 int nchar = *nchar_arg;
2500 long n;
2501
2502 // "z123{nchar}": edit the count before obtaining {nchar}
2503 if (checkclearop(cap->oap))
2504 return FALSE;
2505 n = nchar - '0';
2506
2507 for (;;)
2508 {
2509#ifdef USE_ON_FLY_SCROLL
2510 dont_scroll = TRUE; // disallow scrolling here
2511#endif
2512 ++no_mapping;
2513 ++allow_keys; // no mapping for nchar, but allow key codes
2514 nchar = plain_vgetc();
2515 LANGMAP_ADJUST(nchar, TRUE);
2516 --no_mapping;
2517 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002518 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002519
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002520 if (nchar == K_DEL || nchar == K_KDEL)
2521 n /= 10;
2522 else if (VIM_ISDIGIT(nchar))
Christian Brabandt58f9bef2023-11-14 21:02:30 +01002523 {
Christian Brabandt22cbc8a2023-11-19 10:47:21 +01002524 if (vim_append_digit_long(&n, nchar - '0') == FAIL)
Christian Brabandt58f9bef2023-11-14 21:02:30 +01002525 {
2526 clearopbeep(cap->oap);
2527 break;
2528 }
Christian Brabandt58f9bef2023-11-14 21:02:30 +01002529 }
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002530 else if (nchar == CAR)
2531 {
2532#ifdef FEAT_GUI
2533 need_mouse_correct = TRUE;
2534#endif
2535 win_setheight((int)n);
2536 break;
2537 }
2538 else if (nchar == 'l'
2539 || nchar == 'h'
2540 || nchar == K_LEFT
2541 || nchar == K_RIGHT)
2542 {
2543 cap->count1 = n ? n * cap->count1 : cap->count1;
2544 *nchar_arg = nchar;
2545 return TRUE;
2546 }
2547 else
2548 {
2549 clearopbeep(cap->oap);
2550 break;
2551 }
2552 }
2553 cap->oap->op_type = OP_NOP;
2554 return FALSE;
2555}
2556
2557#ifdef FEAT_SPELL
2558/*
2559 * "zug" and "zuw": undo "zg" and "zw"
2560 * "zg": add good word to word list
2561 * "zw": add wrong word to word list
2562 * "zG": add good word to temp word list
2563 * "zW": add wrong word to temp word list
2564 */
2565 static int
2566nv_zg_zw(cmdarg_T *cap, int nchar)
2567{
2568 char_u *ptr = NULL;
2569 int len;
2570 int undo = FALSE;
2571
2572 if (nchar == 'u')
2573 {
2574 ++no_mapping;
2575 ++allow_keys; // no mapping for nchar, but allow key codes
2576 nchar = plain_vgetc();
2577 LANGMAP_ADJUST(nchar, TRUE);
2578 --no_mapping;
2579 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002580 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002581
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002582 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2583 {
2584 clearopbeep(cap->oap);
2585 return OK;
2586 }
2587 undo = TRUE;
2588 }
2589
2590 if (checkclearop(cap->oap))
2591 return OK;
2592 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2593 return FAIL;
2594 if (ptr == NULL)
2595 {
2596 pos_T pos = curwin->w_cursor;
2597
2598 // Find bad word under the cursor. When 'spell' is
2599 // off this fails and find_ident_under_cursor() is
2600 // used below.
2601 emsg_off++;
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02002602 len = spell_move_to(curwin, FORWARD, SMT_ALL, TRUE, NULL);
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002603 emsg_off--;
2604 if (len != 0 && curwin->w_cursor.col <= pos.col)
2605 ptr = ml_get_pos(&curwin->w_cursor);
2606 curwin->w_cursor = pos;
2607 }
2608
2609 if (ptr == NULL
2610 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2611 return FAIL;
2612 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2613 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2614 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2615
2616 return OK;
2617}
2618#endif
2619
2620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 * Commands that start with "z".
2622 */
2623 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002624nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625{
2626 long n;
2627 colnr_T col;
2628 int nchar = cap->nchar;
2629#ifdef FEAT_FOLDING
2630 long old_fdl = curwin->w_p_fdl;
2631 int old_fen = curwin->w_p_fen;
2632#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002633 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002635 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 if (
2639#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002640 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2641 // and "zC" only in Visual mode. "zj" and "zk" are motion
2642 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 cap->nchar != 'f' && cap->nchar != 'F'
2644 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2645 && cap->nchar != 'j' && cap->nchar != 'k'
2646 &&
2647#endif
2648 checkclearop(cap->oap))
2649 return;
2650
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002651 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2652 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2654 && cap->count0
2655 && cap->count0 != curwin->w_cursor.lnum)
2656 {
2657 setpcmark();
2658 if (cap->count0 > curbuf->b_ml.ml_line_count)
2659 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2660 else
2661 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002662 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 }
2664
2665 switch (nchar)
2666 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002667 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 case '+':
2669 if (cap->count0 == 0)
2670 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002671 // No count given: put cursor at the line below screen
2672 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2674 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2675 else
2676 curwin->w_cursor.lnum = curwin->w_botline;
2677 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002678 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 case NL:
2680 case CAR:
2681 case K_KENTER:
2682 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002683 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684
2685 case 't': scroll_cursor_top(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002686 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002687 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 break;
2689
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002690 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002692 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002694 case 'z': scroll_cursor_halfway(TRUE, FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002695 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002696 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 break;
2698
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002699 // "z^", "z-" and "zb": put cursor at bottom of screen
2700 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2701 // when <count> is at bottom of window, and puts that one at
2702 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 if (cap->count0 != 0)
2704 {
2705 scroll_cursor_bot(0, TRUE);
2706 curwin->w_cursor.lnum = curwin->w_topline;
2707 }
2708 else if (curwin->w_topline == 1)
2709 curwin->w_cursor.lnum = 1;
2710 else
2711 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002712 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 case '-':
2714 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002715 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716
2717 case 'b': scroll_cursor_bot(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002718 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002719 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 break;
2721
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002722 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002724 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002725 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002727 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 case 'h':
2729 case K_LEFT:
2730 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002731 (void)set_leftcol((colnr_T)cap->count1 > curwin->w_leftcol
2732 ? 0 : curwin->w_leftcol - (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 break;
2734
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002735 // "zL" - scroll window left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002736 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002737 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002739 // "zl" - scroll window to the left if not wrapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 case 'l':
2741 case K_RIGHT:
2742 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002743 (void)set_leftcol(curwin->w_leftcol + (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 break;
2745
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002746 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 case 's': if (!curwin->w_p_wrap)
2748 {
2749#ifdef FEAT_FOLDING
2750 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002751 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 else
2753#endif
2754 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002755 if ((long)col > siso)
2756 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 else
2758 col = 0;
2759 if (curwin->w_leftcol != col)
2760 {
2761 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002762 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 }
2764 }
2765 break;
2766
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002767 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 case 'e': if (!curwin->w_p_wrap)
2769 {
2770#ifdef FEAT_FOLDING
2771 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002772 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 else
2774#endif
2775 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002776 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002777 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 col = 0;
2779 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002780 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 if (curwin->w_leftcol != col)
2782 {
2783 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002784 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 }
2786 }
2787 break;
2788
zeertzjq98800972025-04-18 10:57:33 +02002789 // "zp", "zP" in block mode put without adding trailing spaces
Christian Brabandt2fa93842021-05-30 22:17:25 +02002790 case 'P':
2791 case 'p': nv_put(cap);
2792 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002793 // "zy" Yank without trailing spaces
2794 case 'y': nv_operator(cap);
2795 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002797 // "zF": create fold command
2798 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 case 'F':
2800 case 'f': if (foldManualAllowed(TRUE))
2801 {
2802 cap->nchar = 'f';
2803 nv_operator(cap);
2804 curwin->w_p_fen = TRUE;
2805
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002806 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2808 {
2809 nv_operator(cap);
2810 finish_op = TRUE;
2811 }
2812 }
2813 else
2814 clearopbeep(cap->oap);
2815 break;
2816
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002817 // "zd": delete fold at cursor
2818 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 case 'd':
2820 case 'D': if (foldManualAllowed(FALSE))
2821 {
2822 if (VIsual_active)
2823 nv_operator(cap);
2824 else
2825 deleteFold(curwin->w_cursor.lnum,
2826 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2827 }
2828 break;
2829
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002830 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 case 'E': if (foldmethodIsManual(curwin))
2832 {
2833 clearFolding(curwin);
2834 changed_window_setting();
2835 }
2836 else if (foldmethodIsMarker(curwin))
2837 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2838 TRUE, FALSE);
2839 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002840 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 break;
2842
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002843 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 case 'n': curwin->w_p_fen = FALSE;
2845 break;
2846
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002847 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 case 'N': curwin->w_p_fen = TRUE;
2849 break;
2850
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002851 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2853 break;
2854
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002855 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2857 openFold(curwin->w_cursor.lnum, cap->count1);
2858 else
2859 {
2860 closeFold(curwin->w_cursor.lnum, cap->count1);
2861 curwin->w_p_fen = TRUE;
2862 }
2863 break;
2864
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002865 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2867 openFoldRecurse(curwin->w_cursor.lnum);
2868 else
2869 {
2870 closeFoldRecurse(curwin->w_cursor.lnum);
2871 curwin->w_p_fen = TRUE;
2872 }
2873 break;
2874
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002875 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 case 'o': if (VIsual_active)
2877 nv_operator(cap);
2878 else
2879 openFold(curwin->w_cursor.lnum, cap->count1);
2880 break;
2881
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002882 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 case 'O': if (VIsual_active)
2884 nv_operator(cap);
2885 else
2886 openFoldRecurse(curwin->w_cursor.lnum);
2887 break;
2888
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002889 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 case 'c': if (VIsual_active)
2891 nv_operator(cap);
2892 else
2893 closeFold(curwin->w_cursor.lnum, cap->count1);
2894 curwin->w_p_fen = TRUE;
2895 break;
2896
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002897 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 case 'C': if (VIsual_active)
2899 nv_operator(cap);
2900 else
2901 closeFoldRecurse(curwin->w_cursor.lnum);
2902 curwin->w_p_fen = TRUE;
2903 break;
2904
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002905 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 case 'v': foldOpenCursor();
2907 break;
2908
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002909 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002911 curwin->w_foldinvalid = TRUE; // recompute folds
2912 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 foldOpenCursor();
2914 break;
2915
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002916 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002918 curwin->w_foldinvalid = TRUE; // recompute folds
2919 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 break;
2921
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002922 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002924 {
2925 curwin->w_p_fdl -= cap->count1;
2926 if (curwin->w_p_fdl < 0)
2927 curwin->w_p_fdl = 0;
2928 }
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 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002935 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 curwin->w_p_fen = TRUE;
2937 break;
2938
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002939 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002940 case 'r': curwin->w_p_fdl += cap->count1;
2941 {
2942 int d = getDeepestNesting();
2943
2944 if (curwin->w_p_fdl >= d)
2945 curwin->w_p_fdl = d;
2946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 break;
2948
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002949 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002951 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 break;
2953
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002954 case 'j': // "zj" move to next fold downwards
2955 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2957 cap->count1) == FAIL)
2958 clearopbeep(cap->oap);
2959 break;
2960
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002961#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002963#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002964 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002965 case 'g': // "zg": add good word to word list
2966 case 'w': // "zw": add wrong word to word list
2967 case 'G': // "zG": add good word to temp word list
2968 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002969 if (nv_zg_zw(cap, nchar) == FAIL)
2970 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002971 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002972
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002973 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00002974 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00002975 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002976 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002977#endif
2978
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 default: clearopbeep(cap->oap);
2980 }
2981
2982#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002983 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 if (old_fen != curwin->w_p_fen)
2985 {
2986# ifdef FEAT_DIFF
2987 win_T *wp;
2988
2989 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
2990 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002991 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 FOR_ALL_WINDOWS(wp)
2993 {
2994 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
2995 {
2996 wp->w_p_fen = curwin->w_p_fen;
2997 changed_window_setting_win(wp);
2998 }
2999 }
3000 }
3001# endif
3002 changed_window_setting();
3003 }
3004
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003005 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 if (old_fdl != curwin->w_p_fdl)
3007 newFoldLevel();
3008#endif
3009}
3010
3011#ifdef FEAT_GUI
3012/*
3013 * Vertical scrollbar movement.
3014 */
3015 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003016nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017{
3018 if (cap->oap->op_type != OP_NOP)
3019 clearopbeep(cap->oap);
3020
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003021 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 gui_do_scroll();
3023}
3024
3025/*
3026 * Horizontal scrollbar movement.
3027 */
3028 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003029nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030{
3031 if (cap->oap->op_type != OP_NOP)
3032 clearopbeep(cap->oap);
3033
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003034 // Even if an operator was pending, we still want to scroll
Christopher Plewright44c22092022-11-15 17:43:36 +00003035 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036}
3037#endif
3038
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003039#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003040/*
3041 * Click in GUI tab.
3042 */
3043 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003044nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003045{
3046 if (cap->oap->op_type != OP_NOP)
3047 clearopbeep(cap->oap);
3048
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003049 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003050 goto_tabpage(current_tab);
3051}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003052
3053/*
3054 * Selected item in tab line menu.
3055 */
3056 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003057nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003058{
3059 if (cap->oap->op_type != OP_NOP)
3060 clearopbeep(cap->oap);
3061
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003062 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003063 handle_tabmenu();
3064}
3065
3066/*
3067 * Handle selecting an item of the GUI tab line menu.
3068 * Used in Normal and Insert mode.
3069 */
3070 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003071handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003072{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003073 switch (current_tabmenu)
3074 {
3075 case TABLINE_MENU_CLOSE:
3076 if (current_tab == 0)
JMcKiernbc27b6e2025-04-12 11:51:00 +02003077 do_cmdline_cmd((char_u *)"confirm tabclose");
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003078 else
3079 {
JMcKiernbc27b6e2025-04-12 11:51:00 +02003080 vim_snprintf((char *)IObuff, IOSIZE, "confirm tabclose %d",
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003081 current_tab);
3082 do_cmdline_cmd(IObuff);
3083 }
3084 break;
3085
3086 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003087 if (current_tab == 0)
3088 do_cmdline_cmd((char_u *)"$tabnew");
3089 else
3090 {
3091 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3092 current_tab - 1);
3093 do_cmdline_cmd(IObuff);
3094 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003095 break;
3096
3097 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003098 if (current_tab == 0)
3099 do_cmdline_cmd((char_u *)"browse $tabnew");
3100 else
3101 {
3102 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3103 current_tab - 1);
3104 do_cmdline_cmd(IObuff);
3105 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003106 break;
3107 }
3108}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003109#endif
3110
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111/*
3112 * "Q" command.
3113 */
3114 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003115nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003117 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003119 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003120 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 do_exmode(FALSE);
3122}
3123
3124/*
3125 * Handle a ":" command.
3126 */
3127 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003128nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003130 int old_p_im;
3131 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003132 int is_cmdkey = cap->cmdchar == K_COMMAND
3133 || cap->cmdchar == K_SCRIPT_COMMAND;
3134 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135
Bram Moolenaar957cf672020-11-12 14:21:06 +01003136 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003138 nv_operator(cap);
3139 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003141
3142 if (cap->oap->op_type != OP_NOP)
3143 {
3144 // Using ":" as a movement is characterwise exclusive.
3145 cap->oap->motion_type = MCHAR;
3146 cap->oap->inclusive = FALSE;
3147 }
3148 else if (cap->count0 && !is_cmdkey)
3149 {
3150 // translate "count:" into ":.,.+(count - 1)"
3151 stuffcharReadbuff('.');
3152 if (cap->count0 > 1)
3153 {
3154 stuffReadbuff((char_u *)",.+");
3155 stuffnumReadbuff((long)cap->count0 - 1L);
3156 }
3157 }
3158
3159 // When typing, don't type below an old message
3160 if (KeyTyped)
3161 compute_cmdrow();
3162
3163 old_p_im = p_im;
3164
3165 // get a command line and execute it
3166 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3167 if (is_cmdkey)
3168 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3169 else
3170 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
3171
3172 // If 'insertmode' changed, enter or exit Insert mode
3173 if (p_im != old_p_im)
3174 {
3175 if (p_im)
3176 restart_edit = 'i';
3177 else
3178 restart_edit = 0;
3179 }
3180
3181 if (cmd_result == FAIL)
3182 // The Ex command failed, do not execute the operator.
3183 clearop(cap->oap);
3184 else if (cap->oap->op_type != OP_NOP
3185 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
John Marriott02d7a6c2024-02-26 21:21:17 +01003186 || cap->oap->start.col > ml_get_len(cap->oap->start.lnum)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003187 || did_emsg
3188 ))
3189 // The start of the operator has become invalid by the Ex command.
3190 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191}
3192
3193/*
3194 * Handle CTRL-G command.
3195 */
3196 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003197nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003199 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 {
3201 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003202 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 showmode();
3204 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003205 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003206 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 fileinfo((int)cap->count0, FALSE, TRUE);
3208}
3209
3210/*
3211 * Handle CTRL-H <Backspace> command.
3212 */
3213 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003214nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 if (VIsual_active && VIsual_select)
3217 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003218 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 v_visop(cap);
3220 }
3221 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 nv_left(cap);
3223}
3224
3225/*
3226 * CTRL-L: clear screen and redraw.
3227 */
3228 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003229nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003231 if (checkclearop(cap->oap))
3232 return;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003233
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003234#ifdef FEAT_SYN_HL
3235 // Clear all syntax states to force resyncing.
3236 syn_stack_free_all(curwin->w_s);
3237# ifdef FEAT_RELTIME
3238 {
3239 win_T *wp;
3240
3241 FOR_ALL_WINDOWS(wp)
3242 wp->w_s->b_syn_slow = FALSE;
3243 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003244# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003246 redraw_later(UPD_CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003247#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3248# ifdef VIMDLL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003249 if (!gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003250# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003251 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253}
3254
3255/*
3256 * CTRL-O: In Select mode: switch to Visual mode for one command.
3257 * Otherwise: Go to older pcmark.
3258 */
3259 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003260nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 if (VIsual_active && VIsual_select)
3263 {
3264 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003265 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003267 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 }
3269 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 {
3271 cap->count1 = -cap->count1;
3272 nv_pcmark(cap);
3273 }
3274}
3275
3276/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003277 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3278 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 */
3280 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003281nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282{
3283 if (!checkclearopq(cap->oap))
3284 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3285 GETF_SETMARK|GETF_ALT, FALSE);
3286}
3287
3288/*
3289 * "Z" commands.
3290 */
3291 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003292nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003294 if (checkclearopq(cap->oap))
3295 return;
3296
3297 switch (cap->nchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003299 // "ZZ": equivalent to ":x".
3300 case 'Z': do_cmdline_cmd((char_u *)"x");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 break;
3302
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003303 // "ZQ": equivalent to ":q!" (Elvis compatible).
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003304 case 'Q': do_cmdline_cmd((char_u *)"q!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 break;
3306
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003307 default: clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 }
3309}
3310
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311/*
3312 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3313 */
3314 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003315do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316{
3317 oparg_T oa;
3318 cmdarg_T ca;
3319
3320 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003321 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 ca.oap = &oa;
3323 ca.cmdchar = c1;
3324 ca.nchar = c2;
3325 nv_ident(&ca);
3326}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327
3328/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003329 * 'K' normal-mode command. Get the command to lookup the keyword under the
3330 * cursor.
3331 */
3332 static int
3333nv_K_getcmd(
3334 cmdarg_T *cap,
3335 char_u *kp,
3336 int kp_help,
3337 int kp_ex,
3338 char_u **ptr_arg,
3339 int n,
3340 char_u *buf,
John Marriott8c85a2a2024-05-20 19:18:26 +02003341 size_t bufsize,
3342 size_t *buflen)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003343{
3344 char_u *ptr = *ptr_arg;
3345 int isman;
3346 int isman_s;
3347
3348 if (kp_help)
3349 {
3350 // in the help buffer
3351 STRCPY(buf, "he! ");
John Marriott8c85a2a2024-05-20 19:18:26 +02003352 *buflen = STRLEN_LITERAL("he! ");
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003353 return n;
3354 }
3355
3356 if (kp_ex)
3357 {
3358 // 'keywordprog' is an ex command
3359 if (cap->count0 != 0)
John Marriott8c85a2a2024-05-20 19:18:26 +02003360 *buflen = vim_snprintf((char *)buf, bufsize, "%s %ld ", kp, cap->count0);
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003361 else
John Marriott8c85a2a2024-05-20 19:18:26 +02003362 *buflen = vim_snprintf((char *)buf, bufsize, "%s ", kp);
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003363 return n;
3364 }
3365
3366 // An external command will probably use an argument starting
3367 // with "-" as an option. To avoid trouble we skip the "-".
3368 while (*ptr == '-' && n > 0)
3369 {
3370 ++ptr;
3371 --n;
3372 }
3373 if (n == 0)
3374 {
3375 // found dashes only
3376 emsg(_(e_no_identifier_under_cursor));
3377 vim_free(buf);
3378 *ptr_arg = ptr;
3379 return 0;
3380 }
3381
3382 // When a count is given, turn it into a range. Is this
3383 // really what we want?
3384 isman = (STRCMP(kp, "man") == 0);
3385 isman_s = (STRCMP(kp, "man -s") == 0);
3386 if (cap->count0 != 0 && !(isman || isman_s))
John Marriott8c85a2a2024-05-20 19:18:26 +02003387 *buflen = vim_snprintf((char *)buf, bufsize, ".,.+%ld! ", cap->count0 - 1);
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003388 else
John Marriott8c85a2a2024-05-20 19:18:26 +02003389 *buflen = vim_snprintf((char *)buf, bufsize, "! ");
3390
3391 if (cap->count0 == 0 && isman_s)
3392 *buflen += vim_snprintf((char *)buf + *buflen, bufsize - *buflen, "man ");
3393 else
3394 *buflen += vim_snprintf((char *)buf + *buflen, bufsize - *buflen, "%s ", kp);
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003395 if (cap->count0 != 0 && (isman || isman_s))
John Marriott8c85a2a2024-05-20 19:18:26 +02003396 *buflen += vim_snprintf((char *)buf + *buflen, bufsize - *buflen, "%ld ", cap->count0);
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003397
3398 *ptr_arg = ptr;
3399 return n;
3400}
3401
3402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 * Handle the commands that use the word under the cursor.
3404 * [g] CTRL-] :ta to current identifier
3405 * [g] 'K' run program for current identifier
3406 * [g] '*' / to current identifier or string
3407 * [g] '#' ? to current identifier or string
3408 * g ']' :tselect for current identifier
3409 */
3410 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003411nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412{
3413 char_u *ptr = NULL;
3414 char_u *buf;
John Marriott8c85a2a2024-05-20 19:18:26 +02003415 size_t bufsize;
3416 size_t buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003417 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003419 char_u *kp; // value of 'keywordprg'
3420 int kp_help; // 'keywordprg' is ":he"
3421 int kp_ex; // 'keywordprg' starts with ":"
3422 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003424 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003425 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003428 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 {
3430 cmdchar = cap->nchar;
3431 g_cmd = TRUE;
3432 }
3433 else
3434 {
3435 cmdchar = cap->cmdchar;
3436 g_cmd = FALSE;
3437 }
3438
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003439 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 cmdchar = '#';
3441
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003442 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3444 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3446 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 if (checkclearopq(cap->oap))
3448 return;
3449 }
3450
3451 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3452 (cmdchar == '*' || cmdchar == '#')
3453 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3454 {
3455 clearop(cap->oap);
3456 return;
3457 }
3458
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003459 // Allocate buffer to put the command in. Inserting backslashes can
3460 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3461 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3463 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3464 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003465 if (kp_help && *skipwhite(ptr) == NUL)
3466 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003467 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003468 return;
3469 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003470 kp_ex = (*kp == ':');
John Marriott8c85a2a2024-05-20 19:18:26 +02003471 bufsize = (size_t)(n * 2 + 30 + STRLEN(kp));
3472 buf = alloc(bufsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 if (buf == NULL)
3474 return;
3475 buf[0] = NUL;
John Marriott8c85a2a2024-05-20 19:18:26 +02003476 buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477
3478 switch (cmdchar)
3479 {
3480 case '*':
3481 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003482 // Put cursor at start of word, makes search skip the word
3483 // under the cursor.
3484 // Call setpcmark() first, so "*``" puts the cursor back where
3485 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 setpcmark();
3487 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3488
3489 if (!g_cmd && vim_iswordp(ptr))
John Marriott8c85a2a2024-05-20 19:18:26 +02003490 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 STRCPY(buf, "\\<");
John Marriott8c85a2a2024-05-20 19:18:26 +02003492 buflen = STRLEN_LITERAL("\\<");
3493 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003494 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 break;
3496
3497 case 'K':
John Marriott8c85a2a2024-05-20 19:18:26 +02003498 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, bufsize, &buflen);
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003499 if (n == 0)
3500 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 break;
3502
3503 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003504 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505#ifdef FEAT_CSCOPE
3506 if (p_cst)
John Marriott8c85a2a2024-05-20 19:18:26 +02003507 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 STRCPY(buf, "cstag ");
John Marriott8c85a2a2024-05-20 19:18:26 +02003509 buflen = STRLEN_LITERAL("cstag ");
3510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 else
3512#endif
John Marriott8c85a2a2024-05-20 19:18:26 +02003513 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 STRCPY(buf, "ts ");
John Marriott8c85a2a2024-05-20 19:18:26 +02003515 buflen = STRLEN_LITERAL("ts ");
3516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 break;
3518
3519 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003520 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 if (curbuf->b_help)
John Marriott8c85a2a2024-05-20 19:18:26 +02003522 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 STRCPY(buf, "he! ");
John Marriott8c85a2a2024-05-20 19:18:26 +02003524 buflen = STRLEN_LITERAL("he! ");
3525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003527 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003528 if (g_cmd)
John Marriott8c85a2a2024-05-20 19:18:26 +02003529 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003530 STRCPY(buf, "tj ");
John Marriott8c85a2a2024-05-20 19:18:26 +02003531 buflen = STRLEN_LITERAL("tj ");
3532 }
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003533 else if (cap->count0 == 0)
John Marriott8c85a2a2024-05-20 19:18:26 +02003534 {
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003535 STRCPY(buf, "ta ");
John Marriott8c85a2a2024-05-20 19:18:26 +02003536 buflen = STRLEN_LITERAL("ta ");
3537 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003538 else
John Marriott8c85a2a2024-05-20 19:18:26 +02003539 buflen = vim_snprintf((char *)buf, bufsize, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 }
3542
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003543 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003544 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 {
John Marriott8c85a2a2024-05-20 19:18:26 +02003546 size_t plen;
3547
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003548 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003549 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003550 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003551 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003552 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003553 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003554 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003555 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003556 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003558 vim_free(buf);
3559 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 }
John Marriott8c85a2a2024-05-20 19:18:26 +02003561 plen = STRLEN(p);
3562 newbuf = vim_realloc(buf, buflen + plen + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003563 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003564 {
3565 vim_free(buf);
3566 vim_free(p);
3567 return;
3568 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003569 buf = newbuf;
John Marriott8c85a2a2024-05-20 19:18:26 +02003570 STRCPY(buf + buflen, p);
3571 buflen += plen;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003572 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003574 else
3575 {
3576 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003577 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003578 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003579 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003580 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003581 {
3582 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003583 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003584 aux_ptr = (char_u *)"";
3585 else
3586 aux_ptr = (char_u *)"\\|\"\n[";
3587 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003588 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003589 aux_ptr = (char_u *)"\\|\"\n*?[";
3590
John Marriott8c85a2a2024-05-20 19:18:26 +02003591 p = buf + buflen;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003592 while (n-- > 0)
3593 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003594 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003595 if (vim_strchr(aux_ptr, *ptr) != NULL)
3596 *p++ = '\\';
John Marriott8c85a2a2024-05-20 19:18:26 +02003597
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003598 // When current byte is a part of multibyte character, copy all
3599 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003600 if (has_mbyte)
3601 {
3602 int i;
3603 int len = (*mb_ptr2len)(ptr) - 1;
3604
3605 for (i = 0; i < len && n >= 1; ++i, --n)
3606 *p++ = *ptr++;
3607 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003608 *p++ = *ptr++;
3609 }
3610 *p = NUL;
John Marriott8c85a2a2024-05-20 19:18:26 +02003611 buflen = p - buf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003614 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 if (cmdchar == '*' || cmdchar == '#')
3616 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003617 if (!g_cmd && (has_mbyte
3618 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3619 : vim_iswordc(ptr[-1])))
John Marriott8c85a2a2024-05-20 19:18:26 +02003620 {
3621 STRCPY(buf + buflen, "\\>");
3622 buflen += STRLEN_LITERAL("\\>");
3623 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02003624
3625 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003626 init_history();
John Marriott8c85a2a2024-05-20 19:18:26 +02003627 add_to_history(HIST_SEARCH, buf, buflen, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003628
John Marriott8c85a2a2024-05-20 19:18:26 +02003629 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, buflen, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 }
3631 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003632 {
3633 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003635 g_tag_at_cursor = FALSE;
3636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637
3638 vim_free(buf);
3639}
3640
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641/*
3642 * Get visually selected text, within one line only.
3643 * Returns FAIL if more than one line selected.
3644 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003645 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003646get_visual_text(
3647 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003648 char_u **pp, // return: start of selected text
3649 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650{
3651 if (VIsual_mode != 'V')
3652 unadjust_for_sel();
3653 if (VIsual.lnum != curwin->w_cursor.lnum)
3654 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003655 if (cap != NULL)
3656 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 return FAIL;
3658 }
3659 if (VIsual_mode == 'V')
3660 {
3661 *pp = ml_get_curline();
John Marriott02d7a6c2024-02-26 21:21:17 +01003662 *lenp = (int)ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 }
3664 else
3665 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003666 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 {
3668 *pp = ml_get_pos(&curwin->w_cursor);
3669 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3670 }
3671 else
3672 {
3673 *pp = ml_get_pos(&VIsual);
3674 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3675 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003676 if (**pp == NUL)
3677 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003678 if (*lenp > 0)
3679 {
3680 if (has_mbyte)
3681 // Correct the length to include all bytes of the last
3682 // character.
3683 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3684 else if ((*pp)[*lenp - 1] == NUL)
3685 // Do not include a trailing NUL.
3686 *lenp -= 1;
3687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 }
3689 reset_VIsual_and_resel();
3690 return OK;
3691}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692
3693/*
3694 * CTRL-T: backwards in tag stack
3695 */
3696 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003697nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698{
3699 if (!checkclearopq(cap->oap))
3700 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3701}
3702
3703/*
3704 * Handle scrolling command 'H', 'L' and 'M'.
3705 */
3706 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003707nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708{
3709 int used = 0;
3710 long n;
3711#ifdef FEAT_FOLDING
3712 linenr_T lnum;
3713#endif
3714 int half;
3715
3716 cap->oap->motion_type = MLINE;
3717 setpcmark();
3718
3719 if (cap->cmdchar == 'L')
3720 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003721 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 curwin->w_cursor.lnum = curwin->w_botline - 1;
3723 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3724 curwin->w_cursor.lnum = 1;
3725 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003726 {
3727#ifdef FEAT_FOLDING
3728 if (hasAnyFolding(curwin))
3729 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003730 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003731 for (n = cap->count1 - 1; n > 0
3732 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3733 {
3734 (void)hasFolding(curwin->w_cursor.lnum,
3735 &curwin->w_cursor.lnum, NULL);
Bram Moolenaar232bdaa2023-01-13 14:17:58 +00003736 if (curwin->w_cursor.lnum > curwin->w_topline)
3737 --curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003738 }
3739 }
3740 else
3741#endif
3742 curwin->w_cursor.lnum -= cap->count1 - 1;
3743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 }
3745 else
3746 {
3747 if (cap->cmdchar == 'M')
3748 {
3749#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003750 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 used -= diff_check_fill(curwin, curwin->w_topline)
3752 - curwin->w_topfill;
3753#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003754 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3756 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3757 {
3758#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003759 // Count half he number of filler lines to be "below this
3760 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3762 + n) / 2 >= half)
3763 {
3764 --n;
3765 break;
3766 }
3767#endif
3768 used += plines(curwin->w_topline + n);
3769 if (used >= half)
3770 break;
3771#ifdef FEAT_FOLDING
3772 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3773 n = lnum - curwin->w_topline;
3774#endif
3775 }
3776 if (n > 0 && used > curwin->w_height)
3777 --n;
3778 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003779 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003780 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003782#ifdef FEAT_FOLDING
3783 if (hasAnyFolding(curwin))
3784 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003785 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003786 lnum = curwin->w_topline;
3787 while (n-- > 0 && lnum < curwin->w_botline - 1)
3788 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003789 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003790 ++lnum;
3791 }
3792 n = lnum - curwin->w_topline;
3793 }
3794#endif
3795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 curwin->w_cursor.lnum = curwin->w_topline + n;
3797 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3798 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3799 }
3800
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003801 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003802 if (cap->oap->op_type == OP_NOP)
3803 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 beginline(BL_SOL | BL_FIX);
3805}
3806
3807/*
3808 * Cursor right commands.
3809 */
3810 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003811nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812{
3813 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003814 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003816 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3817 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003818 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003819 if (mod_mask & MOD_MASK_CTRL)
3820 cap->arg = TRUE;
3821 nv_wordcmd(cap);
3822 return;
3823 }
3824
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 cap->oap->motion_type = MCHAR;
3826 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003827 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003829 // In virtual edit mode, there's no such thing as "past_line", as lines
3830 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003832 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833
3834 for (n = cap->count1; n > 0; --n)
3835 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003836 if ((!past_line && oneright() == FAIL)
3837 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003838 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003840 // <Space> wraps to next line if 'whichwrap' has 's'.
3841 // 'l' wraps to next line if 'whichwrap' has 'l'.
3842 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 if ( ((cap->cmdchar == ' '
3844 && vim_strchr(p_ww, 's') != NULL)
3845 || (cap->cmdchar == 'l'
3846 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003847 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 && vim_strchr(p_ww, '>') != NULL))
3849 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3850 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003851 // When deleting we also count the NL as a character.
3852 // Set cap->oap->inclusive when last char in the line is
3853 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003854 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003856 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 cap->oap->inclusive = TRUE;
3858 else
3859 {
3860 ++curwin->w_cursor.lnum;
3861 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 curwin->w_set_curswant = TRUE;
3864 cap->oap->inclusive = FALSE;
3865 }
3866 continue;
3867 }
3868 if (cap->oap->op_type == OP_NOP)
3869 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003870 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 if (n == cap->count1)
3872 beep_flush();
3873 }
3874 else
3875 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003876 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 cap->oap->inclusive = TRUE;
3878 }
3879 break;
3880 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003881 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 {
3883 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 if (virtual_active())
3885 oneright();
3886 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003889 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 ++curwin->w_cursor.col;
3892 }
3893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 }
3895#ifdef FEAT_FOLDING
3896 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3897 && cap->oap->op_type == OP_NOP)
3898 foldOpenCursor();
3899#endif
3900}
3901
3902/*
3903 * Cursor left commands.
3904 *
3905 * Returns TRUE when operator end should not be adjusted.
3906 */
3907 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003908nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909{
3910 long n;
3911
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003912 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3913 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003914 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003915 if (mod_mask & MOD_MASK_CTRL)
3916 cap->arg = 1;
3917 nv_bck_word(cap);
3918 return;
3919 }
3920
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 cap->oap->motion_type = MCHAR;
3922 cap->oap->inclusive = FALSE;
3923 for (n = cap->count1; n > 0; --n)
3924 {
3925 if (oneleft() == FAIL)
3926 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003927 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3928 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3929 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 if ( (((cap->cmdchar == K_BS
3931 || cap->cmdchar == Ctrl_H)
3932 && vim_strchr(p_ww, 'b') != NULL)
3933 || (cap->cmdchar == 'h'
3934 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003935 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 && vim_strchr(p_ww, '<') != NULL))
3937 && curwin->w_cursor.lnum > 1)
3938 {
3939 --(curwin->w_cursor.lnum);
3940 coladvance((colnr_T)MAXCOL);
3941 curwin->w_set_curswant = TRUE;
3942
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003943 // When the NL before the first char has to be deleted we
3944 // put the cursor on the NUL after the previous line.
3945 // This is a very special case, be careful!
3946 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 if ( (cap->oap->op_type == OP_DELETE
3948 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003949 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003951 char_u *cp = ml_get_cursor();
3952
3953 if (*cp != NUL)
3954 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003955 if (has_mbyte)
3956 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3957 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003958 ++curwin->w_cursor.col;
3959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 cap->retval |= CA_NO_ADJ_OP_END;
3961 }
3962 continue;
3963 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003964 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3966 beep_flush();
3967 break;
3968 }
3969 }
3970#ifdef FEAT_FOLDING
3971 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3972 && cap->oap->op_type == OP_NOP)
3973 foldOpenCursor();
3974#endif
3975}
3976
3977/*
3978 * Cursor up commands.
3979 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3980 */
3981 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003982nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003984 if (mod_mask & MOD_MASK_SHIFT)
3985 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003986 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003987 cap->arg = BACKWARD;
3988 nv_page(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003989 return;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003990 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003991
3992 cap->oap->motion_type = MLINE;
3993 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
3994 clearopbeep(cap->oap);
3995 else if (cap->arg)
3996 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997}
3998
3999/*
4000 * Cursor down commands.
4001 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4002 */
4003 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004004nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004006 if (mod_mask & MOD_MASK_SHIFT)
4007 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004008 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004009 cap->arg = FORWARD;
4010 nv_page(cap);
4011 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004012#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004013 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004014 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4015 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004017 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004019 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004020 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 cmdwin_result = CAR;
4022 else
Bram Moolenaarf2732452018-06-03 14:47:35 +02004023#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004024 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004025 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4026 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4027 {
4028 invoke_prompt_callback();
4029 if (restart_edit == 0)
4030 restart_edit = 'a';
4031 }
4032 else
4033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 {
4035 cap->oap->motion_type = MLINE;
4036 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4037 clearopbeep(cap->oap);
4038 else if (cap->arg)
4039 beginline(BL_WHITE | BL_FIX);
4040 }
4041 }
4042}
4043
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044/*
4045 * Grab the file name under the cursor and edit it.
4046 */
4047 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004048nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049{
4050 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004051 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004053 if (check_text_or_curbuf_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 return;
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004055
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004056#ifdef FEAT_PROP_POPUP
4057 if (ERROR_IF_TERM_POPUP_WINDOW)
4058 return;
4059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060
Colin Kennedy21570352024-03-03 16:16:47 +01004061 if (!check_can_set_curbuf_disabled())
4062 return;
4063
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004064 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065
4066 if (ptr != NULL)
4067 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004068 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004069 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004070 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004072 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004073 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004074 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004075 {
4076 curwin->w_cursor.lnum = lnum;
4077 check_cursor_lnum();
4078 beginline(BL_SOL | BL_FIX);
4079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 vim_free(ptr);
4081 }
4082 else
4083 clearop(cap->oap);
4084}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085
4086/*
4087 * <End> command: to end of current line or last line.
4088 */
4089 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004090nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004092 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004094 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004096 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 }
4098 nv_dollar(cap);
4099}
4100
4101/*
4102 * Handle the "$" command.
4103 */
4104 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004105nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106{
4107 cap->oap->motion_type = MCHAR;
4108 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004109 // In virtual mode when off the edge of a line and an operator
4110 // is pending (whew!) keep the cursor where it is.
4111 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 if (!virtual_active() || gchar_cursor() != NUL
4113 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004114 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 if (cursor_down((long)(cap->count1 - 1),
4116 cap->oap->op_type == OP_NOP) == FAIL)
4117 clearopbeep(cap->oap);
4118#ifdef FEAT_FOLDING
4119 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4120 foldOpenCursor();
4121#endif
4122}
4123
4124/*
4125 * Implementation of '?' and '/' commands.
4126 * If cap->arg is TRUE don't set PC mark.
4127 */
4128 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004129nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130{
4131 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004132 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133
4134 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4135 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004136 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 cap->cmdchar = 'g';
4138 cap->nchar = '?';
4139 nv_operator(cap);
4140 return;
4141 }
4142
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004143 // When using 'incsearch' the cursor may be moved to set a different search
4144 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004145 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146
4147 if (cap->searchbuf == NULL)
4148 {
4149 clearop(oap);
4150 return;
4151 }
4152
John Marriott8c85a2a2024-05-20 19:18:26 +02004153 (void)normal_search(cap, cap->cmdchar, cap->searchbuf, STRLEN(cap->searchbuf),
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004154 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004155 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156}
4157
LemonBoya4399382022-04-09 21:04:08 +01004158
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159/*
4160 * Handle "N" and "n" commands.
4161 * cap->arg is SEARCH_REV for "N", 0 for "n".
4162 */
4163 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004164nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004166 pos_T old = curwin->w_cursor;
4167 int wrapped = FALSE;
John Marriott8c85a2a2024-05-20 19:18:26 +02004168 int i = normal_search(cap, 0, NULL, 0, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004169
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004170 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004171 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004172 // Avoid getting stuck on the current cursor position, which can
4173 // happen when an offset is given and the cursor is on the last char
4174 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004175 cap->count1 += 1;
John Marriott8c85a2a2024-05-20 19:18:26 +02004176 (void)normal_search(cap, 0, NULL, 0, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004177 cap->count1 -= 1;
4178 }
LemonBoya4399382022-04-09 21:04:08 +01004179
4180#ifdef FEAT_SEARCH_EXTRA
4181 // Redraw the window to refresh the highlighted matches.
4182 if (i > 0 && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004183 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185}
4186
4187/*
4188 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4189 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004190 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004192 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004193normal_search(
4194 cmdarg_T *cap,
4195 int dir,
4196 char_u *pat,
John Marriott8c85a2a2024-05-20 19:18:26 +02004197 size_t patlen,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004198 int opt, // extra flags for do_search()
4199 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200{
4201 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004202 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004203#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004204 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206
4207 cap->oap->motion_type = MCHAR;
4208 cap->oap->inclusive = FALSE;
4209 cap->oap->use_reg_one = TRUE;
4210 curwin->w_set_curswant = TRUE;
4211
Bram Moolenaara80faa82020-04-12 19:37:17 +02004212 CLEAR_FIELD(sia);
John Marriott8c85a2a2024-05-20 19:18:26 +02004213 i = do_search(cap->oap, dir, dir, pat, patlen, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004214 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4215 if (wrapped != NULL)
4216 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 if (i == 0)
4218 clearop(cap->oap);
4219 else
4220 {
4221 if (i == 2)
4222 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224#ifdef FEAT_FOLDING
4225 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4226 foldOpenCursor();
4227#endif
4228 }
LemonBoya4399382022-04-09 21:04:08 +01004229#ifdef FEAT_SEARCH_EXTRA
4230 // Redraw the window to refresh the highlighted matches.
4231 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004232 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004235 // "/$" will put the cursor after the end of the line, may need to
4236 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 check_cursor();
John Marriott8c85a2a2024-05-20 19:18:26 +02004238
Bram Moolenaar46539112015-02-17 15:43:57 +01004239 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240}
4241
4242/*
4243 * Character search commands.
4244 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4245 * ',' and FALSE for ';'.
4246 * cap->nchar is NUL for ',' and ';' (repeat the search)
4247 */
4248 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004249nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250{
4251 int t_cmd;
Jim Zhouc8cce712025-03-05 20:47:29 +01004252 int cursor_dec = FALSE;
4253
4254 // If adjusted cursor position previously, unadjust it.
4255 if (*p_sel == 'e' && VIsual_active && VIsual_mode == 'v'
4256 && VIsual_select_exclu_adj)
4257 {
4258 unadjust_for_sel();
4259 cursor_dec = TRUE;
4260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261
4262 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4263 t_cmd = TRUE;
4264 else
4265 t_cmd = FALSE;
4266
4267 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004270 clearopbeep(cap->oap);
Jim Zhouc8cce712025-03-05 20:47:29 +01004271 // Revert unadjust when failed.
4272 if (cursor_dec)
4273 adjust_for_sel(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004274 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004276
4277 curwin->w_set_curswant = TRUE;
4278 // Include a Tab for "tx" and for "dfx".
4279 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4280 && (t_cmd || cap->oap->op_type != OP_NOP))
4281 {
4282 colnr_T scol, ecol;
4283
4284 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4285 curwin->w_cursor.coladd = ecol - scol;
4286 }
4287 else
4288 curwin->w_cursor.coladd = 0;
4289 adjust_for_sel(cap);
4290#ifdef FEAT_FOLDING
4291 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4292 foldOpenCursor();
4293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294}
4295
4296/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004297 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4298 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4299 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4300 * "[m" or "]m" search for prev/next start of (Java) method.
4301 * "[M" or "]M" search for prev/next end of (Java) method.
4302 */
4303 static void
4304nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4305{
4306 pos_T new_pos = {0, 0, 0};
4307 pos_T *pos = NULL; // init for GCC
4308 pos_T prev_pos;
4309 long n;
4310 int findc;
4311 int c;
4312
4313 if (cap->nchar == '*')
4314 cap->nchar = '/';
4315 prev_pos.lnum = 0;
4316 if (cap->nchar == 'm' || cap->nchar == 'M')
4317 {
4318 if (cap->cmdchar == '[')
4319 findc = '{';
4320 else
4321 findc = '}';
4322 n = 9999;
4323 }
4324 else
4325 {
4326 findc = cap->nchar;
4327 n = cap->count1;
4328 }
4329 for ( ; n > 0; --n)
4330 {
4331 if ((pos = findmatchlimit(cap->oap, findc,
4332 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4333 {
4334 if (new_pos.lnum == 0) // nothing found
4335 {
4336 if (cap->nchar != 'm' && cap->nchar != 'M')
4337 clearopbeep(cap->oap);
4338 }
4339 else
4340 pos = &new_pos; // use last one found
4341 break;
4342 }
4343 prev_pos = new_pos;
4344 curwin->w_cursor = *pos;
4345 new_pos = *pos;
4346 }
4347 curwin->w_cursor = *old_pos;
4348
4349 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4350 // brought us to the match for "[m" and "]M" when inside a method.
4351 // Try finding the '{' or '}' we want to be at.
4352 // Also repeat for the given count.
4353 if (cap->nchar == 'm' || cap->nchar == 'M')
4354 {
4355 // norm is TRUE for "]M" and "[m"
4356 int norm = ((findc == '{') == (cap->nchar == 'm'));
4357
4358 n = cap->count1;
4359 // found a match: we were inside a method
4360 if (prev_pos.lnum != 0)
4361 {
4362 pos = &prev_pos;
4363 curwin->w_cursor = prev_pos;
4364 if (norm)
4365 --n;
4366 }
4367 else
4368 pos = NULL;
4369 while (n > 0)
4370 {
4371 for (;;)
4372 {
4373 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4374 {
4375 // if not found anything, that's an error
4376 if (pos == NULL)
4377 clearopbeep(cap->oap);
4378 n = 0;
4379 break;
4380 }
4381 c = gchar_cursor();
4382 if (c == '{' || c == '}')
4383 {
4384 // Must have found end/start of class: use it.
4385 // Or found the place to be at.
4386 if ((c == findc && norm) || (n == 1 && !norm))
4387 {
4388 new_pos = curwin->w_cursor;
4389 pos = &new_pos;
4390 n = 0;
4391 }
4392 // if no match found at all, we started outside of the
4393 // class and we're inside now. Just go on.
4394 else if (new_pos.lnum == 0)
4395 {
4396 new_pos = curwin->w_cursor;
4397 pos = &new_pos;
4398 }
4399 // found start/end of other method: go to match
4400 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004401 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4402 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004403 n = 0;
4404 else
4405 curwin->w_cursor = *pos;
4406 break;
4407 }
4408 }
4409 --n;
4410 }
4411 curwin->w_cursor = *old_pos;
4412 if (pos == NULL && new_pos.lnum != 0)
4413 clearopbeep(cap->oap);
4414 }
4415 if (pos != NULL)
4416 {
4417 setpcmark();
4418 curwin->w_cursor = *pos;
4419 curwin->w_set_curswant = TRUE;
4420#ifdef FEAT_FOLDING
4421 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4422 && cap->oap->op_type == OP_NOP)
4423 foldOpenCursor();
4424#endif
4425 }
4426}
4427
4428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 * "[" and "]" commands.
4430 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4431 */
4432 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004433nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004436 pos_T *pos = NULL; // init for GCC
4437 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 int flag;
4439 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440
4441 cap->oap->motion_type = MCHAR;
4442 cap->oap->inclusive = FALSE;
4443 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004444 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004446 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 if (cap->nchar == 'f')
4448 nv_gotofile(cap);
4449 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450
4451#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004452 // Find the occurrence(s) of the identifier or define under cursor
4453 // in current and included files or jump to the first occurrence.
4454 //
4455 // search list jump
4456 // fwd bwd fwd bwd fwd bwd
4457 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4458 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004459 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 {
4461 char_u *ptr;
4462 int len;
4463
4464 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4465 clearop(cap->oap);
4466 else
4467 {
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004468 // Make a copy, if the line was changed it will be freed.
4469 ptr = vim_strnsave(ptr, len);
4470 if (ptr == NULL)
4471 return;
4472
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 find_pattern_in_path(ptr, 0, len, TRUE,
Keith Thompson184f71c2024-01-04 21:19:04 +01004474 cap->count0 == 0 ? !SAFE_isupper(cap->nchar) : FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4476 cap->count1,
Keith Thompson184f71c2024-01-04 21:19:04 +01004477 SAFE_isupper(cap->nchar) ? ACTION_SHOW_ALL :
4478 SAFE_islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
Colin Kennedy21570352024-03-03 16:16:47 +01004480 (linenr_T)MAXLNUM,
zeertzjqd9be94c2024-07-14 10:20:20 +02004481 FALSE);
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004482 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 curwin->w_set_curswant = TRUE;
4484 }
4485 }
4486 else
4487#endif
4488
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004489 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4490 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4491 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4492 // "[m" or "]m" search for prev/next start of (Java) method.
4493 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 if ( (cap->cmdchar == '['
4495 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4496 || (cap->cmdchar == ']'
4497 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004498 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004500 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 else if (cap->nchar == '[' || cap->nchar == ']')
4502 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004503 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 flag = '{';
4505 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004506 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507
4508 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004509 // Imitate strange Vi behaviour: When using "]]" with an operator
4510 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004511 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 (cap->oap->op_type != OP_NOP
4513 && cap->arg == FORWARD && flag == '{')))
4514 clearopbeep(cap->oap);
4515 else
4516 {
4517 if (cap->oap->op_type == OP_NOP)
4518 beginline(BL_WHITE | BL_FIX);
4519#ifdef FEAT_FOLDING
4520 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4521 foldOpenCursor();
4522#endif
4523 }
4524 }
4525
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004526 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 else if (cap->nchar == 'p' || cap->nchar == 'P')
4528 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004529 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 }
4531
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004532 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 else if (cap->nchar == '\'' || cap->nchar == '`')
4534 {
4535 pos = &curwin->w_cursor;
4536 for (n = cap->count1; n > 0; --n)
4537 {
4538 prev_pos = *pos;
4539 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4540 cap->nchar == '\'');
4541 if (pos == NULL)
4542 break;
4543 }
4544 if (pos == NULL)
4545 pos = &prev_pos;
4546 nv_cursormark(cap, cap->nchar == '\'', pos);
4547 }
4548
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004549 // [ or ] followed by a middle mouse click: put selected text with
4550 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004551 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 {
4553 (void)do_mouse(cap->oap, cap->nchar,
4554 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4555 cap->count1, PUT_FIXINDENT);
4556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557
4558#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004559 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 else if (cap->nchar == 'z')
4561 {
4562 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4563 cap->count1) == FAIL)
4564 clearopbeep(cap->oap);
4565 }
4566#endif
4567
4568#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004569 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 else if (cap->nchar == 'c')
4571 {
4572 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4573 cap->count1) == FAIL)
4574 clearopbeep(cap->oap);
4575 }
4576#endif
4577
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004578#ifdef FEAT_SPELL
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02004579 // "[r", "[s", "[S", "]r", "]s" and "]S": move to next spell error.
4580 else if (cap->nchar == 'r' || cap->nchar == 's' || cap->nchar == 'S')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004581 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004582 setpcmark();
4583 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004584 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02004585 cap->nchar == 's' ? SMT_ALL :
4586 cap->nchar == 'r' ? SMT_RARE :
4587 SMT_BAD, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004588 {
4589 clearopbeep(cap->oap);
4590 break;
4591 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004592 else
4593 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004594# ifdef FEAT_FOLDING
4595 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4596 foldOpenCursor();
4597# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004598 }
4599#endif
4600
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004601 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 else
4603 clearopbeep(cap->oap);
4604}
4605
4606/*
4607 * Handle Normal mode "%" command.
4608 */
4609 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004610nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611{
4612 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004613#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 linenr_T lnum = curwin->w_cursor.lnum;
4615#endif
4616
4617 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004618 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 {
4620 if (cap->count0 > 100)
4621 clearopbeep(cap->oap);
4622 else
4623 {
4624 cap->oap->motion_type = MLINE;
4625 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004626 // Round up, so 'normal 100%' always jumps at the line line.
4627 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4628 // overflow on 32-bits, so use a formula with less accuracy
4629 // to avoid overflows.
4630 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4632 / 100L * cap->count0;
4633 else
4634 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4635 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004636 if (curwin->w_cursor.lnum < 1)
4637 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4639 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4640 beginline(BL_SOL | BL_FIX);
4641 }
4642 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004643 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 {
4645 cap->oap->motion_type = MCHAR;
4646 cap->oap->use_reg_one = TRUE;
4647 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4648 clearopbeep(cap->oap);
4649 else
4650 {
4651 setpcmark();
4652 curwin->w_cursor = *pos;
4653 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 }
4657 }
4658#ifdef FEAT_FOLDING
4659 if (cap->oap->op_type == OP_NOP
4660 && lnum != curwin->w_cursor.lnum
4661 && (fdo_flags & FDO_PERCENT)
4662 && KeyTyped)
4663 foldOpenCursor();
4664#endif
4665}
4666
4667/*
4668 * Handle "(" and ")" commands.
4669 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4670 */
4671 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004672nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673{
4674 cap->oap->motion_type = MCHAR;
4675 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004676 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004677 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 curwin->w_set_curswant = TRUE;
4679
4680 if (findsent(cap->arg, cap->count1) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004682 clearopbeep(cap->oap);
4683 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004685
4686 // Don't leave the cursor on the NUL past end of line.
4687 adjust_cursor(cap->oap);
4688 curwin->w_cursor.coladd = 0;
4689#ifdef FEAT_FOLDING
4690 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4691 foldOpenCursor();
4692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693}
4694
4695/*
4696 * "m" command: Mark a position.
4697 */
4698 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004699nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004701 if (checkclearop(cap->oap))
4702 return;
4703
4704 if (setmark(cap->nchar) == FAIL)
4705 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706}
4707
4708/*
4709 * "{" and "}" commands.
4710 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4711 */
4712 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004713nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714{
4715 cap->oap->motion_type = MCHAR;
4716 cap->oap->inclusive = FALSE;
4717 cap->oap->use_reg_one = TRUE;
4718 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004719 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004721 clearopbeep(cap->oap);
4722 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004724
4725 curwin->w_cursor.coladd = 0;
4726#ifdef FEAT_FOLDING
4727 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4728 foldOpenCursor();
4729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730}
4731
4732/*
4733 * "u" command: Undo or make lower case.
4734 */
4735 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004736nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004738 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004740 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 cap->cmdchar = 'g';
4742 cap->nchar = 'u';
4743 nv_operator(cap);
4744 }
4745 else
4746 nv_kundo(cap);
4747}
4748
4749/*
4750 * <Undo> command.
4751 */
4752 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004753nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004755 if (checkclearopq(cap->oap))
4756 return;
4757
Bram Moolenaarf2732452018-06-03 14:47:35 +02004758#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004759 if (bt_prompt(curbuf))
4760 {
4761 clearopbeep(cap->oap);
4762 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004764#endif
4765 u_undo((int)cap->count1);
4766 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767}
4768
4769/*
4770 * Handle the "r" command.
4771 */
4772 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004773nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 int had_ctrl_v;
4776 long n;
4777
4778 if (checkclearop(cap->oap))
4779 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004780#ifdef FEAT_JOB_CHANNEL
4781 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4782 {
4783 clearopbeep(cap->oap);
4784 return;
4785 }
4786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004788 // get another character
Christian Brabandt2d63e4b2023-08-12 00:03:57 +02004789 if (cap->nchar == Ctrl_V || cap->nchar == Ctrl_Q)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 {
4791 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004792 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004793 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 if (cap->nchar > DEL)
4795 had_ctrl_v = NUL;
4796 }
4797 else
4798 had_ctrl_v = NUL;
4799
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004800 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004801 if (IS_SPECIAL(cap->nchar))
4802 {
4803 clearopbeep(cap->oap);
4804 return;
4805 }
4806
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004807 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 if (VIsual_active)
4809 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004810 if (got_int)
Christian Brabandt476733f2023-09-19 20:41:51 +02004811 got_int = FALSE;
Bram Moolenaard9820532013-11-04 01:41:17 +01004812 if (had_ctrl_v)
4813 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004814 // Use a special (negative) number to make a difference between a
4815 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004816 if (cap->nchar == CAR)
4817 cap->nchar = REPLACE_CR_NCHAR;
4818 else if (cap->nchar == NL)
4819 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 nv_operator(cap);
4822 return;
4823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004825 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 if (virtual_active())
4827 {
4828 if (u_save_cursor() == FAIL)
4829 return;
4830 if (gchar_cursor() == NUL)
4831 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004832 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 coladvance_force((colnr_T)(getviscol() + cap->count1));
4834 curwin->w_cursor.col -= cap->count1;
4835 }
4836 else if (gchar_cursor() == TAB)
4837 coladvance_force(getviscol());
4838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004840 // Abort if not enough characters to replace.
John Marriott02d7a6c2024-02-26 21:21:17 +01004841 if ((size_t)ml_get_cursor_len() < (unsigned)cap->count1
4842 || (has_mbyte && mb_charlen(ml_get_cursor()) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 {
4844 clearopbeep(cap->oap);
4845 return;
4846 }
4847
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004848 // Replacing with a TAB is done by edit() when it is complicated because
4849 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4850 // Other characters are done below to avoid problems with things like
4851 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4853 {
4854 stuffnumReadbuff(cap->count1);
4855 stuffcharReadbuff('R');
4856 stuffcharReadbuff('\t');
4857 stuffcharReadbuff(ESC);
4858 return;
4859 }
4860
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004861 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 if (u_save_cursor() == FAIL)
4863 return;
4864
4865 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4866 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004867 // Replace character(s) by a single newline.
4868 // Strange vi behaviour: Only one newline is inserted.
4869 // Delete the characters here.
4870 // Insert the newline with an insert command, takes care of
4871 // autoindent. The insert command depends on being on the last
4872 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004873 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 stuffcharReadbuff('\r');
4875 stuffcharReadbuff(ESC);
4876
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004877 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 invoke_edit(cap, TRUE, 'r', FALSE);
4879 }
4880 else
4881 {
4882 prep_redo(cap->oap->regname, cap->count1,
4883 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4884
4885 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 if (has_mbyte)
4887 {
4888 int old_State = State;
4889
4890 if (cap->ncharC1 != 0)
4891 AppendCharToRedobuff(cap->ncharC1);
4892 if (cap->ncharC2 != 0)
4893 AppendCharToRedobuff(cap->ncharC2);
4894
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004895 // This is slow, but it handles replacing a single-byte with a
4896 // multi-byte and the other way around. Also handles adding
4897 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 for (n = cap->count1; n > 0; --n)
4899 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004900 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004901 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4902 {
4903 int c = ins_copychar(curwin->w_cursor.lnum
4904 + (cap->nchar == Ctrl_Y ? -1 : 1));
4905 if (c != NUL)
4906 ins_char(c);
4907 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004908 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004909 ++curwin->w_cursor.col;
4910 }
4911 else
4912 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 State = old_State;
4914 if (cap->ncharC1 != 0)
4915 ins_char(cap->ncharC1);
4916 if (cap->ncharC2 != 0)
4917 ins_char(cap->ncharC2);
4918 }
4919 }
4920 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 {
John Marriott02d7a6c2024-02-26 21:21:17 +01004922 char_u *ptr;
4923
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004924 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 for (n = cap->count1; n > 0; --n)
4926 {
John Marriott02d7a6c2024-02-26 21:21:17 +01004927 // Get ptr again, because ins_copychar() and showmatch()
4928 // will have released the line.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004929 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004930 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4931 {
4932 int c = ins_copychar(curwin->w_cursor.lnum
4933 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004934
4935 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004936 if (c != NUL)
4937 ptr[curwin->w_cursor.col] = c;
4938 }
4939 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004940 {
4941 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004942 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 if (p_sm && msg_silent == 0)
4945 showmatch(cap->nchar);
4946 ++curwin->w_cursor.col;
4947 }
4948#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004949 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004951 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
John Marriott02d7a6c2024-02-26 21:21:17 +01004952 ptr = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953
Bram Moolenaar009b2592004-10-24 19:18:58 +00004954 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004955 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004957 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 }
4959#endif
4960
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004961 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 changed_bytes(curwin->w_cursor.lnum,
4963 (colnr_T)(curwin->w_cursor.col - cap->count1));
4964 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004965 --curwin->w_cursor.col; // cursor on the last replaced char
4966 // if the character on the left of the current cursor is a multi-byte
4967 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 if (has_mbyte)
4969 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 curbuf->b_op_end = curwin->w_cursor;
4971 curwin->w_set_curswant = TRUE;
4972 set_last_insert(cap->nchar);
4973 }
4974}
4975
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976/*
4977 * 'o': Exchange start and end of Visual area.
4978 * 'O': same, but in block mode exchange left and right corners.
4979 */
4980 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004981v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982{
4983 pos_T old_cursor;
4984 colnr_T left, right;
4985
4986 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4987 {
4988 old_cursor = curwin->w_cursor;
4989 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4990 curwin->w_cursor.lnum = VIsual.lnum;
4991 coladvance(left);
4992 VIsual = curwin->w_cursor;
4993
4994 curwin->w_cursor.lnum = old_cursor.lnum;
4995 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004996 // 'selection "exclusive" and cursor at right-bottom corner: move it
4997 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4999 ++curwin->w_curswant;
5000 coladvance(curwin->w_curswant);
5001 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005003 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 {
5005 curwin->w_cursor.lnum = VIsual.lnum;
5006 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5007 ++right;
5008 coladvance(right);
5009 VIsual = curwin->w_cursor;
5010
5011 curwin->w_cursor.lnum = old_cursor.lnum;
5012 coladvance(left);
5013 curwin->w_curswant = left;
5014 }
5015 }
5016 else
5017 {
5018 old_cursor = curwin->w_cursor;
5019 curwin->w_cursor = VIsual;
5020 VIsual = old_cursor;
5021 curwin->w_set_curswant = TRUE;
5022 }
5023}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024
5025/*
5026 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5027 */
5028 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005029nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005031 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 {
5033 cap->cmdchar = 'c';
5034 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005035 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 VIsual_mode = 'V';
5037 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005038 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005040
5041 if (checkclearopq(cap->oap))
5042 return;
5043
5044 if (!curbuf->b_p_ma)
5045 emsg(_(e_cannot_make_changes_modifiable_is_off));
5046 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005048 if (virtual_active())
5049 coladvance(getviscol());
5050 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 }
5052}
5053
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054/*
5055 * "gr".
5056 */
5057 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005058nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 if (VIsual_active)
5061 {
5062 cap->cmdchar = 'r';
5063 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005064 nv_replace(cap); // Do same as "r" in Visual mode for now
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005065 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005067
zeertzjqf86dea82023-03-05 21:15:06 +00005068 if (checkclearopq(cap->oap))
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005069 return;
5070
5071 if (!curbuf->b_p_ma)
5072 emsg(_(e_cannot_make_changes_modifiable_is_off));
5073 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 {
Christian Brabandt2d63e4b2023-08-12 00:03:57 +02005075 if (cap->extra_char == Ctrl_V || cap->extra_char == Ctrl_Q)
5076 // get another character
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005077 cap->extra_char = get_literal(FALSE);
Bram Moolenaard6a4ea32023-02-25 14:24:44 +00005078 if (cap->extra_char < ' ')
5079 // Prefix a control character with CTRL-V to avoid it being used as
5080 // a command.
5081 stuffcharReadbuff(Ctrl_V);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005082 stuffcharReadbuff(cap->extra_char);
5083 stuffcharReadbuff(ESC);
5084 if (virtual_active())
5085 coladvance(getviscol());
5086 invoke_edit(cap, TRUE, 'v', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 }
5088}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089
5090/*
5091 * Swap case for "~" command, when it does not work like an operator.
5092 */
5093 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005094n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095{
5096 long n;
5097 pos_T startpos;
5098 int did_change = 0;
5099#ifdef FEAT_NETBEANS_INTG
5100 pos_T pos;
5101 char_u *ptr;
5102 int count;
5103#endif
5104
5105 if (checkclearopq(cap->oap))
5106 return;
5107
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005108 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 {
5110 clearopbeep(cap->oap);
5111 return;
5112 }
5113
5114 prep_redo_cmd(cap);
5115
5116 if (u_save_cursor() == FAIL)
5117 return;
5118
5119 startpos = curwin->w_cursor;
5120#ifdef FEAT_NETBEANS_INTG
5121 pos = startpos;
5122#endif
5123 for (n = cap->count1; n > 0; --n)
5124 {
5125 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5126 inc_cursor();
5127 if (gchar_cursor() == NUL)
5128 {
5129 if (vim_strchr(p_ww, '~') != NULL
5130 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5131 {
5132#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005133 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 {
5135 if (did_change)
5136 {
5137 ptr = ml_get(pos.lnum);
John Marriott02d7a6c2024-02-26 21:21:17 +01005138 count = (int)ml_get_len(pos.lnum) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005139 netbeans_removed(curbuf, pos.lnum, pos.col,
5140 (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01005141 // line may have been flushed, get it again
5142 ptr = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005144 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 }
5146 pos.col = 0;
5147 pos.lnum++;
5148 }
5149#endif
5150 ++curwin->w_cursor.lnum;
5151 curwin->w_cursor.col = 0;
5152 if (n > 1)
5153 {
5154 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5155 break;
5156 u_clearline();
5157 }
5158 }
5159 else
5160 break;
5161 }
5162 }
5163#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005164 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 {
5166 ptr = ml_get(pos.lnum);
5167 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005168 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5169 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 }
5171#endif
5172
5173
5174 check_cursor();
5175 curwin->w_set_curswant = TRUE;
5176 if (did_change)
5177 {
5178 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5179 0L);
5180 curbuf->b_op_start = startpos;
5181 curbuf->b_op_end = curwin->w_cursor;
5182 if (curbuf->b_op_end.col > 0)
5183 --curbuf->b_op_end.col;
5184 }
5185}
5186
5187/*
5188 * Move cursor to mark.
5189 */
5190 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005191nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192{
5193 if (check_mark(pos) == FAIL)
5194 clearop(cap->oap);
5195 else
5196 {
5197 if (cap->cmdchar == '\''
5198 || cap->cmdchar == '`'
5199 || cap->cmdchar == '['
5200 || cap->cmdchar == ']')
5201 setpcmark();
5202 curwin->w_cursor = *pos;
5203 if (flag)
5204 beginline(BL_WHITE | BL_FIX);
5205 else
5206 check_cursor();
5207 }
5208 cap->oap->motion_type = flag ? MLINE : MCHAR;
5209 if (cap->cmdchar == '`')
5210 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005211 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 curwin->w_set_curswant = TRUE;
5213}
5214
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215/*
5216 * Handle commands that are operators in Visual mode.
5217 */
5218 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005219v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220{
5221 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5222
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005223 // Uppercase means linewise, except in block mode, then "D" deletes till
5224 // the end of the line, and "C" replaces till EOL
Keith Thompson184f71c2024-01-04 21:19:04 +01005225 if (SAFE_isupper(cap->cmdchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 {
5227 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005228 {
5229 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5233 curwin->w_curswant = MAXCOL;
5234 }
5235 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5236 nv_operator(cap);
5237}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238
5239/*
5240 * "s" and "S" commands.
5241 */
5242 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005243nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005245#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005246 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005247 if (term_swap_diff() == OK)
5248 return;
5249#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005250#ifdef FEAT_JOB_CHANNEL
5251 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5252 {
5253 clearopbeep(cap->oap);
5254 return;
5255 }
5256#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005257 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 {
5259 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005260 {
5261 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 cap->cmdchar = 'c';
5265 nv_operator(cap);
5266 }
5267 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 nv_optrans(cap);
5269}
5270
5271/*
5272 * Abbreviated commands.
5273 */
5274 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005275nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276{
5277 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005278 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005280 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 if (VIsual_active)
5282 v_visop(cap);
5283 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 nv_optrans(cap);
5285}
5286
5287/*
5288 * Translate a command into another command.
5289 */
5290 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005291nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292{
5293 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5294 (char_u *)"d$", (char_u *)"c$",
5295 (char_u *)"cl", (char_u *)"cc",
5296 (char_u *)"yy", (char_u *)":s\r"};
5297 static char_u *str = (char_u *)"xXDCsSY&";
5298
5299 if (!checkclearopq(cap->oap))
5300 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005301 // In Vi "2D" doesn't delete the next line. Can't translate it
5302 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005303 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5304 {
5305 cap->oap->start = curwin->w_cursor;
5306 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005307#ifdef FEAT_EVAL
5308 set_op_var(OP_DELETE);
5309#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005310 cap->count1 = 1;
5311 nv_dollar(cap);
5312 finish_op = TRUE;
5313 ResetRedobuff();
5314 AppendCharToRedobuff('D');
5315 }
5316 else
5317 {
5318 if (cap->count0)
5319 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005320 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 }
5323 cap->opcount = 0;
5324}
5325
5326/*
5327 * "'" and "`" commands. Also for "g'" and "g`".
5328 * cap->arg is TRUE for "'" and "g'".
5329 */
5330 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005331nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332{
5333 pos_T *pos;
5334 int c;
5335#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005336 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005337 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338#endif
5339
5340 if (cap->cmdchar == 'g')
5341 c = cap->extra_char;
5342 else
5343 c = cap->nchar;
5344 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005345 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 {
5347 if (cap->arg)
5348 {
5349 check_cursor_lnum();
5350 beginline(BL_WHITE | BL_FIX);
5351 }
5352 else
5353 check_cursor();
5354 }
5355 else
5356 nv_cursormark(cap, cap->arg, pos);
5357
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005358 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 if (!virtual_active())
5360 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005361 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362#ifdef FEAT_FOLDING
5363 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005364 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005365 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 && (fdo_flags & FDO_MARK)
5367 && old_KeyTyped)
5368 foldOpenCursor();
5369#endif
5370}
5371
5372/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005373 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 */
5375 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005376nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005379#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005381 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005384 if (checkclearopq(cap->oap))
5385 return;
5386
5387 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005389 if (goto_tabpage_lastused() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 clearopbeep(cap->oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005391 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005393 if (cap->cmdchar == 'g')
5394 pos = movechangelist((int)cap->count1);
5395 else
5396 pos = movemark((int)cap->count1);
5397 if (pos == (pos_T *)-1) // jump to other file
5398 {
5399 curwin->w_set_curswant = TRUE;
5400 check_cursor();
5401 }
5402 else if (pos != NULL) // can jump
5403 nv_cursormark(cap, FALSE, pos);
5404 else if (cap->cmdchar == 'g')
5405 {
5406 if (curbuf->b_changelistlen == 0)
5407 emsg(_(e_changelist_is_empty));
5408 else if (cap->count1 < 0)
5409 emsg(_(e_at_start_of_changelist));
5410 else
5411 emsg(_(e_at_end_of_changelist));
5412 }
5413 else
5414 clearopbeep(cap->oap);
5415# ifdef FEAT_FOLDING
5416 if (cap->oap->op_type == OP_NOP
5417 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5418 && (fdo_flags & FDO_MARK)
5419 && old_KeyTyped)
5420 foldOpenCursor();
5421# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422}
5423
5424/*
5425 * Handle '"' command.
5426 */
5427 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005428nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429{
5430 if (checkclearop(cap->oap))
5431 return;
5432#ifdef FEAT_EVAL
5433 if (cap->nchar == '=')
5434 cap->nchar = get_expr_register();
5435#endif
5436 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5437 {
5438 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005439 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440#ifdef FEAT_EVAL
5441 set_reg_var(cap->oap->regname);
5442#endif
5443 }
5444 else
5445 clearopbeep(cap->oap);
5446}
5447
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448/*
5449 * Handle "v", "V" and "CTRL-V" commands.
5450 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5451 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005452 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 */
5454 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005455nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005457 if (cap->cmdchar == Ctrl_Q)
5458 cap->cmdchar = Ctrl_V;
5459
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005460 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5461 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 if (cap->oap->op_type != OP_NOP)
5463 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005464 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005465 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 return;
5467 }
5468
5469 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005470 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005472 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005474 else // toggle char/block mode
5475 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 VIsual_mode = cap->cmdchar;
5477 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005478 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005480 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005482 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 {
5484 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005485 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005487 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 VIsual = curwin->w_cursor;
5489
5490 VIsual_active = TRUE;
5491 VIsual_reselect = TRUE;
5492 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005493 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005496 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005497 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005498 // For V and ^V, we multiply the number of lines even if there
5499 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5501 {
5502 curwin->w_cursor.lnum +=
5503 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005504 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 }
5506 VIsual_mode = resel_VIsual_mode;
5507 if (VIsual_mode == 'v')
5508 {
5509 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005510 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005511 update_curswant_force();
Bram Moolenaar79c11e32023-01-10 17:29:29 +00005512 curwin->w_curswant += resel_VIsual_vcol * cap->count0;
5513 if (*p_sel != 'e')
5514 --curwin->w_curswant;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005517 curwin->w_curswant = resel_VIsual_vcol;
5518 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005520 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 {
5522 curwin->w_curswant = MAXCOL;
5523 coladvance((colnr_T)MAXCOL);
5524 }
5525 else if (VIsual_mode == Ctrl_V)
5526 {
Bram Moolenaar8f531662023-02-01 17:33:18 +00005527 // Update curswant on the original line, that is where "col" is
5528 // valid.
5529 linenr_T lnum = curwin->w_cursor.lnum;
5530 curwin->w_cursor.lnum = VIsual.lnum;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005531 update_curswant_force();
Bram Moolenaar8f531662023-02-01 17:33:18 +00005532 curwin->w_curswant += resel_VIsual_vcol * cap->count0 - 1;
5533 curwin->w_cursor.lnum = lnum;
Christian Brabandtbb955892024-12-16 22:49:15 +01005534 if (*p_sel == 'e')
5535 ++curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 coladvance(curwin->w_curswant);
5537 }
5538 else
5539 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005540 redraw_curbuf_later(UPD_INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 }
5542 else
5543 {
5544 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005545 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 may_start_select('c');
5547 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005548 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005549 ++cap->count1; // include one more char
Jim Zhouc8cce712025-03-05 20:47:29 +01005550 else
5551 VIsual_select_exclu_adj = FALSE;
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005552 if (cap->count0 > 0 && --cap->count1 > 0)
5553 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005554 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005555 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5556 nv_right(cap);
5557 else if (VIsual_mode == 'V')
5558 nv_down(cap);
5559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 }
5561 }
5562}
5563
5564/*
5565 * Start selection for Shift-movement keys.
5566 */
5567 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005568start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005570 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 may_start_select('k');
5572 n_start_visual_mode('v');
5573}
5574
5575/*
5576 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005577 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 */
5579 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005580may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005582 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5583 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584}
5585
5586/*
5587 * Start Visual mode "c".
5588 * Should set VIsual_select before calling this.
5589 */
5590 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005591n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005593#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005594 int cursor_line_was_concealed = curwin->w_p_cole > 0
5595 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005596#endif
5597
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 VIsual_mode = c;
5599 VIsual_active = TRUE;
5600 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005601
5602 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005603 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005604 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005605 {
5606 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 VIsual = curwin->w_cursor;
5610
5611#ifdef FEAT_FOLDING
5612 foldAdjustVisual();
5613#endif
5614
LemonBoy2bf52dd2022-04-09 18:17:34 +01005615 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005617#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005618 // Check if redraw is needed after changing the state.
5619 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005620#endif
5621
Bram Moolenaar09df3122006-01-23 22:23:09 +00005622 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005623 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005625 // Make sure the clipboard gets updated. Needed because start and
5626 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 clip_star.vmode = NUL;
5628#endif
5629
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005630 // Only need to redraw this line, unless still need to redraw an old
5631 // Visual area (when 'lazyredraw' is set).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005632 if (curwin->w_redr_type < UPD_INVERTED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 {
5634 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5635 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5636 }
5637}
5638
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639
5640/*
5641 * CTRL-W: Window commands
5642 */
5643 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005644nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005646 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005647 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005648 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005649 cap->cmdchar = ':';
5650 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005651 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005652 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005653 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005654 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655}
5656
5657/*
5658 * CTRL-Z: Suspend
5659 */
5660 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005661nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662{
5663 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005665 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005666 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667}
5668
5669/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005670 * "gv": Reselect the previous Visual area. If Visual already active,
5671 * exchange previous and current Visual area.
5672 */
5673 static void
5674nv_gv_cmd(cmdarg_T *cap)
5675{
5676 pos_T tpos;
5677 int i;
5678
5679 if (checkclearop(cap->oap))
5680 return;
5681
5682 if (curbuf->b_visual.vi_start.lnum == 0
5683 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5684 || curbuf->b_visual.vi_end.lnum == 0)
5685 {
5686 beep_flush();
5687 return;
5688 }
5689
5690 // set w_cursor to the start of the Visual area, tpos to the end
5691 if (VIsual_active)
5692 {
5693 i = VIsual_mode;
5694 VIsual_mode = curbuf->b_visual.vi_mode;
5695 curbuf->b_visual.vi_mode = i;
5696# ifdef FEAT_EVAL
5697 curbuf->b_visual_mode_eval = i;
5698# endif
5699 i = curwin->w_curswant;
5700 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5701 curbuf->b_visual.vi_curswant = i;
5702
5703 tpos = curbuf->b_visual.vi_end;
5704 curbuf->b_visual.vi_end = curwin->w_cursor;
5705 curwin->w_cursor = curbuf->b_visual.vi_start;
5706 curbuf->b_visual.vi_start = VIsual;
5707 }
5708 else
5709 {
5710 VIsual_mode = curbuf->b_visual.vi_mode;
5711 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5712 tpos = curbuf->b_visual.vi_end;
5713 curwin->w_cursor = curbuf->b_visual.vi_start;
5714 }
5715
5716 VIsual_active = TRUE;
5717 VIsual_reselect = TRUE;
5718
5719 // Set Visual to the start and w_cursor to the end of the Visual
5720 // area. Make sure they are on an existing character.
5721 check_cursor();
5722 VIsual = curwin->w_cursor;
5723 curwin->w_cursor = tpos;
5724 check_cursor();
5725 update_topline();
5726
5727 // When called from normal "g" command: start Select mode when
5728 // 'selectmode' contains "cmd". When called for K_SELECT, always
5729 // start Select mode.
5730 if (cap->arg)
5731 {
5732 VIsual_select = TRUE;
5733 VIsual_select_reg = 0;
5734 }
5735 else
5736 may_start_select('c');
5737 setmouse();
5738#ifdef FEAT_CLIPBOARD
5739 // Make sure the clipboard gets updated. Needed because start and
5740 // end are still the same, and the selection needs to be owned
5741 clip_star.vmode = NUL;
5742#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005743 redraw_curbuf_later(UPD_INVERTED);
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005744 showmode();
5745}
5746
5747/*
5748 * "g0", "g^" : Like "0" and "^" but for screen lines.
5749 * "gm": middle of "g0" and "g$".
5750 */
Luuk van Baalcb204e62024-04-02 20:49:45 +02005751 void
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005752nv_g_home_m_cmd(cmdarg_T *cap)
5753{
5754 int i;
5755 int flag = FALSE;
5756
5757 if (cap->nchar == '^')
5758 flag = TRUE;
5759
5760 cap->oap->motion_type = MCHAR;
5761 cap->oap->inclusive = FALSE;
5762 if (curwin->w_p_wrap && curwin->w_width != 0)
5763 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005764 int width1 = curwin->w_width - curwin_col_off();
5765 int width2 = width1 + curwin_col_off2();
5766 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005767
5768 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005769 virtcol = curwin->w_virtcol
5770#ifdef FEAT_PROP_POPUP
5771 - curwin->w_virtcol_first_char
5772#endif
5773 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005774 i = 0;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005775 if (virtcol >= (colnr_T)width1 && width2 > 0)
5776 i = (virtcol - width1) / width2 * width2 + width1;
Luuk van Baalcb204e62024-04-02 20:49:45 +02005777
5778 // When ending up below 'smoothscroll' marker, move just beyond it so
5779 // that skipcol is not adjusted later.
5780 if (curwin->w_skipcol > 0 && curwin->w_cursor.lnum == curwin->w_topline)
5781 {
zeertzjq2c47ab82025-02-13 20:34:34 +01005782 int overlap = sms_marker_overlap(curwin, curwin->w_width - width2);
Luuk van Baalcb204e62024-04-02 20:49:45 +02005783 if (overlap > 0 && i == curwin->w_skipcol)
5784 i += overlap;
5785 }
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005786 }
5787 else
5788 i = curwin->w_leftcol;
5789 // Go to the middle of the screen line. When 'number' or
5790 // 'relativenumber' is on and lines are wrapping the middle can be more
5791 // to the left.
5792 if (cap->nchar == 'm')
5793 i += (curwin->w_width - curwin_col_off()
5794 + ((curwin->w_p_wrap && i > 0)
5795 ? curwin_col_off2() : 0)) / 2;
5796 coladvance((colnr_T)i);
5797 if (flag)
5798 {
5799 do
5800 i = gchar_cursor();
5801 while (VIM_ISWHITE(i) && oneright() == OK);
5802 curwin->w_valid &= ~VALID_WCOL;
5803 }
5804 curwin->w_set_curswant = TRUE;
Christian Brabandt9848fac2024-11-17 16:18:00 +01005805#ifdef FEAT_FOLDING
5806 if (hasAnyFolding(curwin))
5807 {
5808 validate_cheight();
5809 if (curwin->w_cline_folded)
5810 update_curswant_force();
5811 }
5812#endif
Luuk van Baal8667a562023-05-12 15:47:25 +01005813 adjust_skipcol();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005814}
5815
5816/*
5817 * "g_": to the last non-blank character in the line or <count> lines
5818 * downward.
5819 */
5820 static void
5821nv_g_underscore_cmd(cmdarg_T *cap)
5822{
5823 char_u *ptr;
5824
5825 cap->oap->motion_type = MCHAR;
5826 cap->oap->inclusive = TRUE;
5827 curwin->w_curswant = MAXCOL;
5828 if (cursor_down((long)(cap->count1 - 1),
5829 cap->oap->op_type == OP_NOP) == FAIL)
5830 {
5831 clearopbeep(cap->oap);
5832 return;
5833 }
5834
5835 ptr = ml_get_curline();
5836
5837 // In Visual mode we may end up after the line.
5838 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5839 --curwin->w_cursor.col;
5840
5841 // Decrease the cursor column until it's on a non-blank.
5842 while (curwin->w_cursor.col > 0
5843 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5844 --curwin->w_cursor.col;
5845 curwin->w_set_curswant = TRUE;
5846 adjust_for_sel(cap);
5847}
5848
5849/*
5850 * "g$" : Like "$" but for screen lines.
5851 */
5852 static void
5853nv_g_dollar_cmd(cmdarg_T *cap)
5854{
5855 oparg_T *oap = cap->oap;
5856 int i;
5857 int col_off = curwin_col_off();
Christian Brabandtb5f6fe92023-08-19 15:53:16 +02005858 int flag = FALSE;
5859
zeertzjq654bdbb2023-08-20 18:24:20 +02005860 if (cap->nchar == K_END || cap->nchar == K_KEND)
Christian Brabandtb5f6fe92023-08-19 15:53:16 +02005861 flag = TRUE;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005862
5863 oap->motion_type = MCHAR;
5864 oap->inclusive = TRUE;
5865 if (curwin->w_p_wrap && curwin->w_width != 0)
5866 {
5867 curwin->w_curswant = MAXCOL; // so we stay at the end
5868 if (cap->count1 == 1)
5869 {
5870 int width1 = curwin->w_width - col_off;
5871 int width2 = width1 + curwin_col_off2();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005872 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005873
5874 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005875 virtcol = curwin->w_virtcol
5876#ifdef FEAT_PROP_POPUP
5877 - curwin->w_virtcol_first_char
5878#endif
5879 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005880 i = width1 - 1;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005881 if (virtcol >= (colnr_T)width1)
5882 i += ((virtcol - width1) / width2 + 1)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005883 * width2;
5884 coladvance((colnr_T)i);
5885
5886 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005887 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005888 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5889 {
5890 // Check for landing on a character that got split at
5891 // the end of the line. We do not want to advance to
5892 // the next screen line.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005893 if (curwin->w_virtcol
5894#ifdef FEAT_PROP_POPUP
5895 - curwin->w_virtcol_first_char
5896#endif
5897 > (colnr_T)i)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005898 --curwin->w_cursor.col;
5899 }
5900 }
5901 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5902 clearopbeep(oap);
5903 }
5904 else
5905 {
5906 if (cap->count1 > 1)
5907 // if it fails, let the cursor still move to the last char
5908 (void)cursor_down(cap->count1 - 1, FALSE);
5909
5910 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5911 coladvance((colnr_T)i);
5912
5913 // if the character doesn't fit move one back
5914 if (curwin->w_cursor.col > 0
5915 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5916 {
5917 colnr_T vcol;
5918
5919 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5920 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5921 --curwin->w_cursor.col;
5922 }
5923
5924 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005925 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005926 }
Christian Brabandtb5f6fe92023-08-19 15:53:16 +02005927 if (flag)
5928 {
5929 do
5930 i = gchar_cursor();
5931 while (VIM_ISWHITE(i) && oneleft() == OK);
5932 curwin->w_valid &= ~VALID_WCOL;
5933 }
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005934}
5935
5936/*
5937 * "gi": start Insert at the last position.
5938 */
5939 static void
5940nv_gi_cmd(cmdarg_T *cap)
5941{
5942 int i;
5943
5944 if (curbuf->b_last_insert.lnum != 0)
5945 {
5946 curwin->w_cursor = curbuf->b_last_insert;
5947 check_cursor_lnum();
John Marriott02d7a6c2024-02-26 21:21:17 +01005948 i = (int)ml_get_curline_len();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005949 if (curwin->w_cursor.col > (colnr_T)i)
5950 {
5951 if (virtual_active())
5952 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5953 curwin->w_cursor.col = i;
5954 }
5955 }
5956 cap->cmdchar = 'i';
5957 nv_edit(cap);
5958}
5959
5960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 * Commands starting with "g".
5962 */
5963 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005964nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965{
5966 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968
5969 switch (cap->nchar)
5970 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005971 case Ctrl_A:
5972 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005974 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005975 if (!VIsual_active && cap->nchar == Ctrl_A)
5976 vim_mem_profile_dump();
5977 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005979 // "g^A/g^X": sequentially increment visually selected region
zeertzjqf86dea82023-03-05 21:15:06 +00005980 if (VIsual_active)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005981 {
5982 cap->arg = TRUE;
5983 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005984 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005985 nv_addsub(cap);
5986 }
5987 else
5988 clearopbeep(oap);
5989 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005991 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 case 'R':
5993 cap->arg = TRUE;
5994 nv_Replace(cap);
5995 break;
5996
5997 case 'r':
5998 nv_vreplace(cap);
5999 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000
6001 case '&':
6002 do_cmdline_cmd((char_u *)"%s//~/&");
6003 break;
6004
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006005 // "gv": Reselect the previous Visual area. If Visual already active,
6006 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006008 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006010
6011 // "gV": Don't reselect the previous Visual area after a Select mode
6012 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 case 'V':
6014 VIsual_reselect = FALSE;
6015 break;
6016
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006017 // "gh": start Select mode.
6018 // "gH": start Select line mode.
6019 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 case K_BS:
6021 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006022 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 case 'h':
6024 case 'H':
6025 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 cap->cmdchar = cap->nchar + ('v' - 'h');
6027 cap->arg = TRUE;
6028 nv_visual(cap);
6029 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02006030
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006031 // "gn", "gN" visually select next/previous search match
6032 // "gn" selects next match
6033 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02006034 case 'N':
6035 case 'n':
6036 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02006037 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02006038 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006040 // "gj" and "gk" two new funny movement keys -- up and down
6041 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 case 'j':
6043 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006044 // with 'nowrap' it works just like the normal "j" command.
6045 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 {
6047 oap->motion_type = MLINE;
6048 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
6049 }
6050 else
6051 i = nv_screengo(oap, FORWARD, cap->count1);
6052 if (i == FAIL)
6053 clearopbeep(oap);
6054 break;
6055
6056 case 'k':
6057 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006058 // with 'nowrap' it works just like the normal "k" command.
6059 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 {
6061 oap->motion_type = MLINE;
6062 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
6063 }
6064 else
6065 i = nv_screengo(oap, BACKWARD, cap->count1);
6066 if (i == FAIL)
6067 clearopbeep(oap);
6068 break;
6069
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006070 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 case 'J':
6072 nv_join(cap);
6073 break;
6074
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006075 // "g0", "g^" : Like "0" and "^" but for screen lines.
6076 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 case '0':
6079 case 'm':
6080 case K_HOME:
6081 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006082 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 break;
6084
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006085 case 'M':
6086 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006087 oap->motion_type = MCHAR;
6088 oap->inclusive = FALSE;
Dylan Thacker-Smith366c81a2024-03-24 09:46:56 +01006089 i = linetabsize_no_outer(curwin, curwin->w_cursor.lnum);
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006090 if (cap->count0 > 0 && cap->count0 <= 100)
6091 coladvance((colnr_T)(i * cap->count0 / 100));
6092 else
6093 coladvance((colnr_T)(i / 2));
6094 curwin->w_set_curswant = TRUE;
6095 }
6096 break;
6097
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006098 // "g_": to the last non-blank character in the line or <count> lines
6099 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006101 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 break;
6103
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006104 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 case '$':
6106 case K_END:
6107 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006108 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 break;
6110
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006111 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 case '*':
6113 case '#':
6114#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006115 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006117 case Ctrl_RSB: // :tag or :tselect for current identifier
6118 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 nv_ident(cap);
6120 break;
6121
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006122 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 case 'e':
6124 case 'E':
6125 oap->motion_type = MCHAR;
6126 curwin->w_set_curswant = TRUE;
6127 oap->inclusive = TRUE;
6128 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6129 clearopbeep(oap);
6130 break;
6131
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006132 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006134 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 break;
6136
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006137 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006139 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 break;
6141
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006142 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 case 'I':
6144 beginline(0);
6145 if (!checkclearopq(oap))
6146 invoke_edit(cap, FALSE, 'g', FALSE);
6147 break;
6148
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006149 // "gf": goto file, edit file under cursor
6150 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006152 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 nv_gotofile(cap);
6154 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006156 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 case '\'':
6158 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006159 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 case '`':
6161 nv_gomark(cap);
6162 break;
6163
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006164 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006166 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 break;
6168
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006169 // "ga": Display the ascii value of the character under the
6170 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 case 'a':
6172 do_ascii(NULL);
6173 break;
6174
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006175 // "g8": Display the bytes used for the UTF-8 character under the
6176 // cursor. It is displayed in hex.
6177 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006179 if (cap->count0 == 8)
6180 utf_find_illegal();
6181 else
6182 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006185 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006186 case '<':
6187 show_sb_text();
6188 break;
6189
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006190 // "gg": Goto the first line in file. With a count it goes to
6191 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 case 'g':
6193 cap->arg = FALSE;
6194 nv_goto(cap);
6195 break;
6196
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006197 // Two-character operators:
6198 // "gq" Format text
6199 // "gw" Format text and keep cursor position
6200 // "g~" Toggle the case of the text.
6201 // "gu" Change text to lower case.
6202 // "gU" Change text to upper case.
6203 // "g?" rot13 encoding
6204 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 case 'q':
6206 case 'w':
6207 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006208 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 case '~':
6210 case 'u':
6211 case 'U':
6212 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006213 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 nv_operator(cap);
6215 break;
6216
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006217 // "gd": Find first occurrence of pattern under the cursor in the
6218 // current function
6219 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 case 'd':
6221 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006222 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 break;
6224
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006225 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 case K_MIDDLEMOUSE:
6227 case K_MIDDLEDRAG:
6228 case K_MIDDLERELEASE:
6229 case K_LEFTMOUSE:
6230 case K_LEFTDRAG:
6231 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006232 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 case K_RIGHTMOUSE:
6234 case K_RIGHTDRAG:
6235 case K_RIGHTRELEASE:
6236 case K_X1MOUSE:
6237 case K_X1DRAG:
6238 case K_X1RELEASE:
6239 case K_X2MOUSE:
6240 case K_X2DRAG:
6241 case K_X2RELEASE:
6242 mod_mask = MOD_MASK_CTRL;
6243 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6244 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245
6246 case K_IGNORE:
6247 break;
6248
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006249 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 case 'p':
6251 case 'P':
6252 nv_put(cap);
6253 break;
6254
6255#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006256 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 case 'o':
Christian Brabandtf8702ae2024-08-28 20:39:24 +02006258 oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 goto_byte(cap->count0);
6260 break;
6261#endif
6262
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006263 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006265 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 do_exmode(TRUE);
6267 break;
6268
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 case ',':
6270 nv_pcmark(cap);
6271 break;
6272
6273 case ';':
6274 cap->count1 = -cap->count1;
6275 nv_pcmark(cap);
6276 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006278 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006279 if (!checkclearop(oap))
6280 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006281 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006282 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006283 if (!checkclearop(oap))
6284 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006285 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006286
Bram Moolenaar62a23252020-08-09 14:04:42 +02006287 case TAB:
6288 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6289 clearopbeep(oap);
6290 break;
6291
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006292 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006293 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006294 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006295 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006296 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006297 break;
6298
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 default:
6300 clearopbeep(oap);
6301 break;
6302 }
6303}
6304
6305/*
6306 * Handle "o" and "O" commands.
6307 */
6308 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006309n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006311#ifdef FEAT_CONCEAL
6312 linenr_T oldline = curwin->w_cursor.lnum;
6313#endif
6314
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006315 if (checkclearopq(cap->oap))
6316 return;
6317
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318#ifdef FEAT_FOLDING
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006319 if (cap->cmdchar == 'O')
6320 // Open above the first line of a folded sequence of lines
6321 (void)hasFolding(curwin->w_cursor.lnum,
6322 &curwin->w_cursor.lnum, NULL);
6323 else
6324 // Open below the last line of a folded sequence of lines
6325 (void)hasFolding(curwin->w_cursor.lnum,
6326 NULL, &curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327#endif
Christian Brabandt4bca4892023-10-27 19:26:49 +02006328 // trigger TextChangedI for the 'o/O' command
6329 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006330 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6331 (cap->cmdchar == 'O' ? 1 : 0)),
6332 (linenr_T)(curwin->w_cursor.lnum +
6333 (cap->cmdchar == 'o' ? 1 : 0))
6334 ) == OK
6335 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
6336 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6337 0, NULL) == OK)
6338 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006339#ifdef FEAT_CONCEAL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006340 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
6341 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006342#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006343#ifdef FEAT_SYN_HL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006344 if (curwin->w_p_cul)
6345 // force redraw of cursorline
6346 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006347#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006348 // When '#' is in 'cpoptions' ignore the count.
6349 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6350 cap->count1 = 1;
6351 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 }
6353}
6354
6355/*
6356 * "." command: redo last change.
6357 */
6358 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006359nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006361 if (checkclearopq(cap->oap))
6362 return;
6363
6364 // If "restart_edit" is TRUE, the last but one command is repeated
6365 // instead of the last command (inserting text). This is used for
6366 // CTRL-O <.> in insert mode.
6367 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6368 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369}
6370
6371/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006372 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 */
6374 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006375nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006377 if (VIsual_select && VIsual_active)
6378 {
6379 int reg;
6380 // Get register name
6381 ++no_mapping;
6382 ++allow_keys;
6383 reg = plain_vgetc();
6384 LANGMAP_ADJUST(reg, TRUE);
6385 --no_mapping;
6386 --allow_keys;
6387
6388 if (reg == '"')
6389 // the unnamed register is 0
6390 reg = 0;
6391
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006392 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006393 return;
6394 }
6395
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006396 if (checkclearopq(cap->oap))
6397 return;
6398
6399 u_redo((int)cap->count1);
6400 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401}
6402
6403/*
6404 * Handle "U" command.
6405 */
6406 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006407nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006409 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006410 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006412 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 cap->cmdchar = 'g';
6414 cap->nchar = 'U';
6415 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006416 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006418
6419 if (checkclearopq(cap->oap))
6420 return;
6421
6422 u_undoline();
6423 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424}
6425
6426/*
6427 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6428 * single character.
6429 */
6430 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006431nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006433 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006434 {
6435#ifdef FEAT_JOB_CHANNEL
6436 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6437 {
6438 clearopbeep(cap->oap);
6439 return;
6440 }
6441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 else
6445 nv_operator(cap);
6446}
6447
6448/*
6449 * Handle an operator command.
6450 * The actual work is done by do_pending_operator().
6451 */
6452 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006453nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454{
6455 int op_type;
6456
6457 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006458#ifdef FEAT_JOB_CHANNEL
6459 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6460 {
6461 clearopbeep(cap->oap);
6462 return;
6463 }
6464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006466 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 nv_lineop(cap);
6468 else if (!checkclearop(cap->oap))
6469 {
6470 cap->oap->start = curwin->w_cursor;
6471 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006472#ifdef FEAT_EVAL
6473 set_op_var(op_type);
6474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 }
6476}
6477
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006478#ifdef FEAT_EVAL
6479/*
6480 * Set v:operator to the characters for "optype".
6481 */
6482 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006483set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006484{
6485 char_u opchars[3];
6486
6487 if (optype == OP_NOP)
6488 set_vim_var_string(VV_OP, NULL, 0);
6489 else
6490 {
6491 opchars[0] = get_op_char(optype);
6492 opchars[1] = get_extra_op_char(optype);
6493 opchars[2] = NUL;
6494 set_vim_var_string(VV_OP, opchars, -1);
6495 }
6496}
6497#endif
6498
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499/*
6500 * Handle linewise operator "dd", "yy", etc.
6501 *
6502 * "_" is is a strange motion command that helps make operators more logical.
6503 * It is actually implemented, but not documented in the real Vi. This motion
6504 * command actually refers to "the current line". Commands like "dd" and "yy"
6505 * are really an alternate form of "d_" and "y_". It does accept a count, so
6506 * "d3_" works to delete 3 lines.
6507 */
6508 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006509nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510{
6511 cap->oap->motion_type = MLINE;
6512 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6513 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006514 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006515 && cap->oap->motion_force != 'v'
6516 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 || cap->oap->op_type == OP_LSHIFT
6518 || cap->oap->op_type == OP_RSHIFT)
6519 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006520 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 beginline(BL_WHITE | BL_FIX);
6522}
6523
6524/*
6525 * <Home> command.
6526 */
6527 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006528nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006530 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006531 if (mod_mask & MOD_MASK_CTRL)
6532 nv_goto(cap);
6533 else
6534 {
6535 cap->count0 = 1;
6536 nv_pipe(cap);
6537 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006538 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6539 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540}
6541
6542/*
6543 * "|" command.
6544 */
6545 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006546nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547{
6548 cap->oap->motion_type = MCHAR;
6549 cap->oap->inclusive = FALSE;
6550 beginline(0);
6551 if (cap->count0 > 0)
6552 {
6553 coladvance((colnr_T)(cap->count0 - 1));
6554 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6555 }
6556 else
6557 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006558 // keep curswant at the column where we wanted to go, not where
6559 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 curwin->w_set_curswant = FALSE;
6561}
6562
6563/*
6564 * Handle back-word command "b" and "B".
6565 * cap->arg is 1 for "B"
6566 */
6567 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006568nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569{
6570 cap->oap->motion_type = MCHAR;
6571 cap->oap->inclusive = FALSE;
6572 curwin->w_set_curswant = TRUE;
6573 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6574 clearopbeep(cap->oap);
6575#ifdef FEAT_FOLDING
6576 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6577 foldOpenCursor();
6578#endif
6579}
6580
6581/*
6582 * Handle word motion commands "e", "E", "w" and "W".
6583 * cap->arg is TRUE for "E" and "W".
6584 */
6585 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006586nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587{
6588 int n;
6589 int word_end;
6590 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006591 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006593 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6595 word_end = TRUE;
6596 else
6597 word_end = FALSE;
6598 cap->oap->inclusive = word_end;
6599
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006600 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 if (!word_end && cap->oap->op_type == OP_CHANGE)
6602 {
6603 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006604 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006606 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006608 // Reproduce a funny Vi behaviour: "cw" on a blank only
6609 // changes one character, not all blanks until the start of
6610 // the next word. Only do this when the 'w' flag is included
6611 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6613 {
6614 cap->oap->inclusive = TRUE;
6615 cap->oap->motion_type = MCHAR;
6616 return;
6617 }
6618 }
6619 else
6620 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006621 // This is a little strange. To match what the real Vi does,
6622 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6623 // that we are not on a space or a TAB. This seems impolite
6624 // at first, but it's really more what we mean when we say
6625 // 'cw'.
6626 // Another strangeness: When standing on the end of a word
6627 // "ce" will change until the end of the next word, but "cw"
6628 // will change only one character! This is done by setting
6629 // flag.
Christian Brabandt22105fd2024-07-15 20:51:11 +02006630 // This can be configured using :set cpo-z
6631 if (vim_strchr(p_cpo, CPO_WORD) != NULL)
6632 {
6633 cap->oap->inclusive = TRUE;
6634 word_end = TRUE;
6635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 flag = TRUE;
6637 }
6638 }
6639 }
6640
6641 cap->oap->motion_type = MCHAR;
6642 curwin->w_set_curswant = TRUE;
6643 if (word_end)
6644 n = end_word(cap->count1, cap->arg, flag, FALSE);
6645 else
6646 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6647
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006648 // Don't leave the cursor on the NUL past the end of line. Unless we
6649 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006650 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006651 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652
6653 if (n == FAIL && cap->oap->op_type == OP_NOP)
6654 clearopbeep(cap->oap);
6655 else
6656 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658#ifdef FEAT_FOLDING
6659 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6660 foldOpenCursor();
6661#endif
6662 }
6663}
6664
6665/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006666 * Used after a movement command: If the cursor ends up on the NUL after the
6667 * end of the line, may move it back to the last character and make the motion
6668 * inclusive.
6669 */
6670 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006671adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006672{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006673 // The cursor cannot remain on the NUL when:
6674 // - the column is > 0
6675 // - not in Visual mode or 'selection' is "o"
6676 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006677 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006678 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006679 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006680 {
6681 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006682 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006683 if (has_mbyte)
6684 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006685 oap->inclusive = TRUE;
6686 }
6687}
6688
6689/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 * "0" and "^" commands.
6691 * cap->arg is the argument for beginline().
6692 */
6693 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006694nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695{
6696 cap->oap->motion_type = MCHAR;
6697 cap->oap->inclusive = FALSE;
6698 beginline(cap->arg);
6699#ifdef FEAT_FOLDING
6700 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6701 foldOpenCursor();
6702#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006703 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6704 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705}
6706
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707/*
6708 * In exclusive Visual mode, may include the last character.
6709 */
6710 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006711adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712{
6713 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006714 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 if (has_mbyte)
6717 inc_cursor();
6718 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 ++curwin->w_cursor.col;
6720 cap->oap->inclusive = FALSE;
Jim Zhouc8cce712025-03-05 20:47:29 +01006721 VIsual_select_exclu_adj = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 }
6723}
6724
6725/*
6726 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6727 * Should check VIsual_mode before calling this.
6728 * Returns TRUE when backed up to the previous line.
6729 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006730 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006731unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732{
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006733 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
zeertzjq701ad502024-05-23 07:47:55 +02006734 return unadjust_for_sel_inner(LT_POS(VIsual, curwin->w_cursor)
6735 ? &curwin->w_cursor : &VIsual);
6736 return FALSE;
6737}
6738
6739/*
6740 * Move position "*pp" back one character for 'selection' == "exclusive".
6741 * Returns TRUE when backed up to the previous line.
6742 */
6743 int
6744unadjust_for_sel_inner(pos_T *pp)
6745{
6746 colnr_T cs, ce;
Jim Zhouc8cce712025-03-05 20:47:29 +01006747 VIsual_select_exclu_adj = FALSE;
zeertzjq701ad502024-05-23 07:47:55 +02006748
6749 if (pp->coladd > 0)
6750 --pp->coladd;
6751 else if (pp->col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 {
zeertzjq701ad502024-05-23 07:47:55 +02006753 --pp->col;
6754 mb_adjustpos(curbuf, pp);
6755 if (virtual_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 {
zeertzjq701ad502024-05-23 07:47:55 +02006757 getvcol(curwin, pp, &cs, NULL, &ce);
6758 pp->coladd = ce - cs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 }
6760 }
zeertzjq701ad502024-05-23 07:47:55 +02006761 else if (pp->lnum > 1)
6762 {
6763 --pp->lnum;
6764 pp->col = ml_get_len(pp->lnum);
6765 return TRUE;
6766 }
6767
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 return FALSE;
6769}
6770
6771/*
6772 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6773 */
6774 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006775nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776{
6777 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006778 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006780 VIsual_select_reg = 0;
6781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 else if (VIsual_reselect)
6783 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006784 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 cap->arg = TRUE;
6786 nv_g_cmd(cap);
6787 }
6788}
6789
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790
6791/*
6792 * "G", "gg", CTRL-END, CTRL-HOME.
6793 * cap->arg is TRUE for "G".
6794 */
6795 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006796nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797{
6798 linenr_T lnum;
6799
6800 if (cap->arg)
6801 lnum = curbuf->b_ml.ml_line_count;
6802 else
6803 lnum = 1L;
6804 cap->oap->motion_type = MLINE;
6805 setpcmark();
6806
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006807 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 if (cap->count0 != 0)
6809 lnum = cap->count0;
6810 if (lnum < 1L)
6811 lnum = 1L;
6812 else if (lnum > curbuf->b_ml.ml_line_count)
6813 lnum = curbuf->b_ml.ml_line_count;
6814 curwin->w_cursor.lnum = lnum;
6815 beginline(BL_SOL | BL_FIX);
6816#ifdef FEAT_FOLDING
6817 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6818 foldOpenCursor();
6819#endif
6820}
6821
6822/*
6823 * CTRL-\ in Normal mode.
6824 */
6825 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006826nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827{
6828 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6829 {
6830 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006831 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006832 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 if (cmdwin_type != 0)
6835 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 if (VIsual_active)
6837 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006838 end_visual_mode(); // stop Visual
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006839 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006841 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 if (cap->nchar == Ctrl_G && p_im)
6843 restart_edit = 'a';
6844 }
6845 else
6846 clearopbeep(cap->oap);
6847}
6848
6849/*
6850 * ESC in Normal mode: beep, but don't flush buffers.
6851 * Don't even beep if we are canceling a command.
6852 */
6853 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006854nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855{
6856 int no_reason;
6857
6858 no_reason = (cap->oap->op_type == OP_NOP
6859 && cap->opcount == 0
6860 && cap->count0 == 0
6861 && cap->oap->regname == 0
6862 && !p_im);
6863
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006864 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006866 if (restart_edit == 0 && cmdwin_type == 0
6867 && !VIsual_active && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006868 {
Bram Moolenaarf7e73022022-09-24 13:10:04 +01006869 int out_redir = !stdout_isatty && !is_not_a_term_or_gui();
6870
Bram Moolenaar5939c352022-09-24 12:50:45 +01006871 // The user may accidentally do "vim file | grep word" and then
6872 // CTRL-C doesn't show anything. With a changed buffer give the
6873 // message on stderr. Without any changes might as well exit.
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006874 if (anyBufIsChanged())
Bram Moolenaar5939c352022-09-24 12:50:45 +01006875 {
6876 char *ms = _("Type :qa! and press <Enter> to abandon all changes and exit Vim");
6877
6878 if (out_redir)
6879 mch_errmsg(ms);
6880 else
6881 msg(ms);
6882 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006883 else
Bram Moolenaar5939c352022-09-24 12:50:45 +01006884 {
6885 if (out_redir)
6886 {
6887 got_int = FALSE;
6888 do_cmdline_cmd((char_u *)"qa");
6889 }
6890 else
6891 msg(_("Type :qa and press <Enter> to exit Vim"));
6892 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01006895 if (restart_edit != 0)
6896 redraw_mode = TRUE; // remove "-- (insert) --"
6897
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006898 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6899 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 if (!p_im)
6901 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 if (cmdwin_type != 0)
6903 {
6904 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006905 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 return;
6907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 }
Bram Moolenaar8d696372022-08-21 10:40:07 +01006909 else if (cmdwin_type != 0 && ex_normal_busy && typebuf_was_empty)
Bram Moolenaar7d414102021-02-23 19:39:20 +01006910 {
6911 // When :normal runs out of characters while in the command line window
Bram Moolenaar8d696372022-08-21 10:40:07 +01006912 // vgetorpeek() will repeatedly return ESC. Exit the cmdline window to
6913 // break the loop.
Bram Moolenaar7d414102021-02-23 19:39:20 +01006914 cmdwin_result = K_IGNORE;
6915 return;
6916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 if (VIsual_active)
6919 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006920 end_visual_mode(); // stop Visual
6921 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006923 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006925 else if (no_reason)
Bram Moolenaarb849c822022-08-28 22:46:21 +01006926 {
6927#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar98d10692022-08-29 00:08:39 +01006928 if (!cap->arg && popup_message_win_visible())
Bram Moolenaarb849c822022-08-28 22:46:21 +01006929 popup_hide_message_win();
6930 else
6931#endif
6932 vim_beep(BO_ESC);
6933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 clearop(cap->oap);
6935
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006936 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6937 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006938 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 restart_edit = 'a';
6940}
6941
6942/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006943 * Move the cursor for the "A" command.
6944 */
6945 void
6946set_cursor_for_append_to_line(void)
6947{
6948 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006949 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006950 {
6951 int save_State = State;
6952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006953 // Pretend Insert mode here to allow the cursor on the
6954 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006955 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006956 coladvance((colnr_T)MAXCOL);
6957 State = save_State;
6958 }
6959 else
6960 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6961}
6962
6963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006965 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 */
6967 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006968nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006970 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6972 cap->cmdchar = 'i';
6973
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006974 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006976 {
6977#ifdef FEAT_TERMINAL
6978 if (term_in_normal_mode())
6979 {
6980 end_visual_mode();
6981 clearop(cap->oap);
6982 term_enter_job_mode();
6983 return;
6984 }
6985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006989 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006990 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6991 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 nv_object(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006995#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006996 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006997 {
6998 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006999 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02007000 return;
7001 }
7002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 else if (!curbuf->b_p_ma && !p_im)
7004 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007005 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02007006 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01007008 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007009 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007010 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007012 else if (cap->cmdchar == K_PS && VIsual_active)
7013 {
7014 pos_T old_pos = curwin->w_cursor;
7015 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00007016 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01007017
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007018 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01007019 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
7020 {
7021 shift_delete_registers();
7022 cap->oap->regname = '1';
7023 }
7024 else
7025 cap->oap->regname = '-';
7026 cap->cmdchar = 'd';
7027 cap->nchar = NUL;
7028 nv_operator(cap);
7029 do_pending_operator(cap, 0, FALSE);
7030 cap->cmdchar = K_PS;
7031
zeertzjq7a732522022-03-14 20:46:41 +00007032 if (*ml_get_cursor() != NUL)
7033 {
7034 if (old_visual_mode == 'V')
7035 {
7036 // In linewise Visual mode insert before the beginning of the
7037 // next line.
7038 // When the last line in the buffer was deleted then create a
7039 // new line, otherwise there is not need to move cursor.
7040 // Detect this by checking if cursor moved above Visual area.
7041 if (curwin->w_cursor.lnum < old_pos.lnum
7042 && curwin->w_cursor.lnum < old_visual.lnum)
7043 {
7044 if (u_save_cursor() == OK)
7045 {
7046 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
7047 FALSE);
7048 appended_lines(curwin->w_cursor.lnum++, 1L);
7049 }
7050 }
7051 }
7052 // When the last char in the line was deleted then append.
7053 // Detect this by checking if cursor moved before Visual area.
7054 else if (curwin->w_cursor.col < old_pos.col
7055 && curwin->w_cursor.col < old_visual.col)
7056 inc_cursor();
7057 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007058
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007059 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01007060 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 else if (!checkclearopq(cap->oap))
7063 {
7064 switch (cap->cmdchar)
7065 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007066 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007067 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 break;
7069
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007070 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007071 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7072 beginline(BL_WHITE);
7073 else
7074 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 break;
7076
Bram Moolenaara1891842017-02-04 21:34:31 +01007077 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007078 // Bracketed paste works like "a"ppend, unless the cursor is in
7079 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007080 if (curwin->w_cursor.col == 0)
7081 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007082 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007083
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007084 case 'a': // "a"ppend is like "i"nsert on the next character.
7085 // increment coladd when in virtual space, increment the
7086 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 if (virtual_active()
7088 && (curwin->w_cursor.coladd > 0
7089 || *ml_get_cursor() == NUL
7090 || *ml_get_cursor() == TAB))
7091 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007092 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 inc_cursor();
7094 break;
7095 }
7096
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7098 {
7099 int save_State = State;
7100
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007101 // Pretend Insert mode here to allow the cursor on the
7102 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01007103 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 coladvance(getviscol());
7105 State = save_State;
7106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107
7108 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7109 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007110 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007111 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007112 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113}
7114
7115/*
7116 * Invoke edit() and take care of "restart_edit" and the return value.
7117 */
7118 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007119invoke_edit(
7120 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007121 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007122 int cmd,
7123 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124{
7125 int restart_edit_save = 0;
7126
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007127 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7128 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7129 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 if (repl || !stuff_empty())
7131 restart_edit_save = restart_edit;
7132 else
7133 restart_edit_save = 0;
7134
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007135 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 restart_edit = 0;
7137
Christian Brabandt4bca4892023-10-27 19:26:49 +02007138 // Reset Changedtick_i, so that TextChangedI will only be triggered for stuff
7139 // from insert mode, for 'o/O' this has already been done in n_opencmd
7140 if (cap->cmdchar != 'O' && cap->cmdchar != 'o')
7141 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 if (edit(cmd, startln, cap->count1))
7143 cap->retval |= CA_COMMAND_BUSY;
7144
7145 if (restart_edit == 0)
7146 restart_edit = restart_edit_save;
7147}
7148
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149/*
7150 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7151 */
7152 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007153nv_object(
7154 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155{
7156 int flag;
7157 int include;
7158 char_u *mps_save;
7159
7160 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007161 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007163 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007165 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 mps_save = curbuf->b_p_mps;
7167 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7168
7169 switch (cap->nchar)
7170 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007171 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 flag = current_word(cap->oap, cap->count1, include, FALSE);
7173 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007174 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 flag = current_word(cap->oap, cap->count1, include, TRUE);
7176 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007177 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 case '(':
7179 case ')':
7180 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7181 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007182 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 case '{':
7184 case '}':
7185 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7186 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007187 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 case ']':
7189 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7190 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007191 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 case '>':
7193 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7194 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007195#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007196 case 't': // "at" = a tag block (xml and html)
7197 // Do not adjust oap->end in do_pending_operator()
7198 // otherwise there are different results for 'dit'
7199 // (note leading whitespace in last line):
7200 // 1) <b> 2) <b>
7201 // foobar foobar
7202 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007203 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007204 flag = current_tagblock(cap->oap, cap->count1, include);
7205 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007206#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007207 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 flag = current_par(cap->oap, cap->count1, include, 'p');
7209 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007210 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 flag = current_sent(cap->oap, cap->count1, include);
7212 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007213 case '"': // "a"" = a double quoted string
7214 case '\'': // "a'" = a single quoted string
7215 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007216 flag = current_quote(cap->oap, cap->count1, include,
7217 cap->nchar);
7218 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007219#if 0 // TODO
7220 case 'S': // "aS" = a section
7221 case 'f': // "af" = a filename
7222 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223#endif
7224 default:
7225 flag = FAIL;
7226 break;
7227 }
7228
7229 curbuf->b_p_mps = mps_save;
7230 if (flag == FAIL)
7231 clearopbeep(cap->oap);
7232 adjust_cursor_col();
7233 curwin->w_set_curswant = TRUE;
7234}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235
7236/*
7237 * "q" command: Start/stop recording.
7238 * "q:", "q/", "q?": edit command-line in command-line window.
7239 */
7240 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007241nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242{
7243 if (cap->oap->op_type == OP_FORMAT)
7244 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007245 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 cap->cmdchar = 'g';
7247 cap->nchar = 'q';
7248 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007249 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007251
7252 if (checkclearop(cap->oap))
7253 return;
7254
7255 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007257 if (cmdwin_type != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007259 emsg(_(e_cmdline_window_already_open));
7260 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007262 stuffcharReadbuff(cap->nchar);
7263 stuffcharReadbuff(K_CMDWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007265 else
7266 // (stop) recording into a named register, unless executing a
7267 // register
7268 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
7269 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270}
7271
7272/*
7273 * Handle the "@r" command.
7274 */
7275 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007276nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277{
7278 if (checkclearop(cap->oap))
7279 return;
7280#ifdef FEAT_EVAL
7281 if (cap->nchar == '=')
7282 {
7283 if (get_expr_register() == NUL)
7284 return;
7285 }
7286#endif
7287 while (cap->count1-- && !got_int)
7288 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007289 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 {
7291 clearopbeep(cap->oap);
7292 break;
7293 }
7294 line_breakcheck();
7295 }
7296}
7297
7298/*
7299 * Handle the CTRL-U and CTRL-D commands.
7300 */
7301 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007302nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303{
Luuk van Baalcb204e62024-04-02 20:49:45 +02007304 int dir = cap->cmdchar == Ctrl_D ? FORWARD : BACKWARD;
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01007305 if (!checkclearop(cap->oap))
Luuk van Baalcb204e62024-04-02 20:49:45 +02007306 pagescroll(dir, cap->count0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307}
7308
7309/*
7310 * Handle "J" or "gJ" command.
7311 */
7312 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007313nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007315 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007317 nv_operator(cap);
7318 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007320
7321 if (checkclearop(cap->oap))
7322 return;
7323
7324 if (cap->count0 <= 1)
7325 cap->count0 = 2; // default for join is two lines!
7326 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7327 curbuf->b_ml.ml_line_count)
7328 {
7329 // can't join when on the last line
7330 if (cap->count0 <= 2)
7331 {
7332 clearopbeep(cap->oap);
7333 return;
7334 }
7335 cap->count0 = curbuf->b_ml.ml_line_count
7336 - curwin->w_cursor.lnum + 1;
7337 }
7338
7339 prep_redo(cap->oap->regname, cap->count0,
7340 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7341 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342}
7343
7344/*
7345 * "P", "gP", "p" and "gp" commands.
7346 */
7347 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007348nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007350 nv_put_opt(cap, FALSE);
7351}
7352
7353/*
7354 * "P", "gP", "p" and "gp" commands.
7355 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7356 */
7357 static void
7358nv_put_opt(cmdarg_T *cap, int fix_indent)
7359{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 int regname = 0;
7361 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007362 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007363 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 int dir;
7365 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007366 int keep_registers = FALSE;
zeertzjq4e141c62024-02-28 21:49:51 +01007367#ifdef FEAT_FOLDING
7368 int save_fen = curwin->w_p_fen;
7369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370
7371 if (cap->oap->op_type != OP_NOP)
7372 {
7373#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007374 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7376 {
7377 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007378 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 }
7380 else
7381#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007382 clearopbeep(cap->oap);
7383 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007385
Bram Moolenaarf2732452018-06-03 14:47:35 +02007386#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007387 if (bt_prompt(curbuf) && !prompt_curpos_editable())
Bram Moolenaarf2732452018-06-03 14:47:35 +02007388 {
7389 clearopbeep(cap->oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007390 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02007391 }
7392#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007393
7394 if (fix_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007396 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7397 ? FORWARD : BACKWARD;
7398 flags |= PUT_FIXINDENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007400 else
7401 dir = (cap->cmdchar == 'P'
7402 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7403 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
7404 prep_redo_cmd(cap);
7405 if (cap->cmdchar == 'g')
7406 flags |= PUT_CURSEND;
7407 else if (cap->cmdchar == 'z')
7408 flags |= PUT_BLOCK_INNER;
7409
7410 if (VIsual_active)
7411 {
7412 // Putting in Visual mode: The put text replaces the selected
7413 // text. First delete the selected text, then put the new text.
7414 // Need to save and restore the registers that the delete
7415 // overwrites if the old contents is being put.
7416 was_visual = TRUE;
7417 regname = cap->oap->regname;
7418 keep_registers = cap->cmdchar == 'P';
7419#ifdef FEAT_CLIPBOARD
7420 adjust_clip_reg(&regname);
7421#endif
7422 if (regname == 0 || regname == '"'
7423 || VIM_ISDIGIT(regname) || regname == '-'
7424#ifdef FEAT_CLIPBOARD
7425 || (clip_unnamed && (regname == '*' || regname == '+'))
7426#endif
7427
7428 )
7429 {
7430 // The delete is going to overwrite the register we want to
7431 // put, save it first.
7432 reg1 = get_register(regname, TRUE);
7433 }
7434
zeertzjq4e141c62024-02-28 21:49:51 +01007435#ifdef FEAT_FOLDING
7436 // Temporarily disable folding, as deleting a fold marker may cause
7437 // the cursor to be included in a fold.
7438 curwin->w_p_fen = FALSE;
7439#endif
7440
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007441 // Now delete the selected text. Avoid messages here.
7442 cap->cmdchar = 'd';
7443 cap->nchar = NUL;
7444 cap->oap->regname = keep_registers ? '_' : NUL;
7445 ++msg_silent;
7446 nv_operator(cap);
7447 do_pending_operator(cap, 0, FALSE);
7448 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7449 --msg_silent;
7450
7451 // delete PUT_LINE_BACKWARD;
7452 cap->oap->regname = regname;
7453
7454 if (reg1 != NULL)
7455 {
7456 // Delete probably changed the register we want to put, save
7457 // it first. Then put back what was there before the delete.
7458 reg2 = get_register(regname, FALSE);
7459 put_register(regname, reg1);
7460 }
7461
7462 // When deleted a linewise Visual area, put the register as
7463 // lines to avoid it joined with the next line. When deletion was
7464 // characterwise, split a line when putting lines.
7465 if (VIsual_mode == 'V')
7466 flags |= PUT_LINE;
7467 else if (VIsual_mode == 'v')
7468 flags |= PUT_LINE_SPLIT;
7469 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7470 flags |= PUT_LINE_FORWARD;
7471 dir = BACKWARD;
7472 if ((VIsual_mode != 'V'
7473 && curwin->w_cursor.col < curbuf->b_op_start.col)
7474 || (VIsual_mode == 'V'
7475 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
7476 // cursor is at the end of the line or end of file, put
7477 // forward.
7478 dir = FORWARD;
7479 // May have been reset in do_put().
7480 VIsual_active = TRUE;
7481 }
7482 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
7483
7484 // If a register was saved, put it back now.
7485 if (reg2 != NULL)
7486 put_register(regname, reg2);
7487
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007488 if (was_visual)
7489 {
zeertzjq4e141c62024-02-28 21:49:51 +01007490#ifdef FEAT_FOLDING
7491 if (save_fen)
7492 curwin->w_p_fen = TRUE;
7493#endif
7494 // What to reselect with "gv"? Selecting the just put text seems to
7495 // be the most useful, since the original text was removed.
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007496 curbuf->b_visual.vi_start = curbuf->b_op_start;
7497 curbuf->b_visual.vi_end = curbuf->b_op_end;
7498 // need to adjust cursor position
7499 if (*p_sel == 'e')
7500 inc(&curbuf->b_visual.vi_end);
7501 }
7502
7503 // When all lines were selected and deleted do_put() leaves an empty
7504 // line that needs to be deleted now.
7505 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
7506 {
7507 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
7508 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
7509
7510 // If the cursor was in that line, move it to the end of the last
7511 // line.
7512 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7513 {
7514 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7515 coladvance((colnr_T)MAXCOL);
7516 }
7517 }
7518 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519}
7520
7521/*
7522 * "o" and "O" commands.
7523 */
7524 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007525nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526{
7527#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007528 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7530 {
7531 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007532 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 }
7534 else
7535#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007536 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007538#ifdef FEAT_JOB_CHANNEL
7539 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007540 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 n_opencmd(cap);
7544}
7545
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546#ifdef FEAT_NETBEANS_INTG
7547 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007548nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549{
7550 netbeans_keycommand(cap->nchar);
7551}
7552#endif
7553
7554#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007556nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007558 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559}
7560#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007561
Bram Moolenaar3918c952005-03-15 22:34:55 +00007562/*
7563 * Trigger CursorHold event.
7564 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7565 * input buffer. "did_cursorhold" is set to avoid retriggering.
7566 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007567 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007568nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007569{
7570 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7571 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007572 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007573}