blob: fcd6311c7ab75b6de4f7c63c404cd8ca2a7c50c5 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
ichizok672776d2022-01-31 12:27:18 +00003 * VIM - Vi IMproved by Bram Moolenaar et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
12 * the operators.
13 */
14
15#include "vim.h"
16
Bram Moolenaar792cf5e2019-09-30 23:12:16 +020017static int VIsual_mode_orig = NUL; // saved Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +000018
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010019#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010020static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount);
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010021#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static void unshift_special(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010023static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25/*
26 * nv_*(): functions called to handle Normal and Visual mode commands.
27 * n_*(): functions called to handle Normal mode commands.
28 * v_*(): functions called to handle Visual mode commands.
29 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010030static void nv_ignore(cmdarg_T *cap);
31static void nv_nop(cmdarg_T *cap);
32static void nv_error(cmdarg_T *cap);
33static void nv_help(cmdarg_T *cap);
34static void nv_addsub(cmdarg_T *cap);
35static void nv_page(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010036static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010038static void nv_ver_scrollbar(cmdarg_T *cap);
39static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000041#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void nv_tabline(cmdarg_T *cap);
43static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000044#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010045static void nv_exmode(cmdarg_T *cap);
46static void nv_colon(cmdarg_T *cap);
47static void nv_ctrlg(cmdarg_T *cap);
48static void nv_ctrlh(cmdarg_T *cap);
49static void nv_clear(cmdarg_T *cap);
50static void nv_ctrlo(cmdarg_T *cap);
51static void nv_hat(cmdarg_T *cap);
52static void nv_Zet(cmdarg_T *cap);
53static void nv_ident(cmdarg_T *cap);
54static void nv_tagpop(cmdarg_T *cap);
55static void nv_scroll(cmdarg_T *cap);
56static void nv_right(cmdarg_T *cap);
57static void nv_left(cmdarg_T *cap);
58static void nv_up(cmdarg_T *cap);
59static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010060static void nv_end(cmdarg_T *cap);
61static void nv_dollar(cmdarg_T *cap);
62static void nv_search(cmdarg_T *cap);
63static void nv_next(cmdarg_T *cap);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +020064static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt, int *wrapped);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010065static void nv_csearch(cmdarg_T *cap);
66static void nv_brackets(cmdarg_T *cap);
67static void nv_percent(cmdarg_T *cap);
68static void nv_brace(cmdarg_T *cap);
69static void nv_mark(cmdarg_T *cap);
70static void nv_findpar(cmdarg_T *cap);
71static void nv_undo(cmdarg_T *cap);
72static void nv_kundo(cmdarg_T *cap);
73static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010074static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010075static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
76static void v_visop(cmdarg_T *cap);
77static void nv_subst(cmdarg_T *cap);
78static void nv_abbrev(cmdarg_T *cap);
79static void nv_optrans(cmdarg_T *cap);
80static void nv_gomark(cmdarg_T *cap);
81static void nv_pcmark(cmdarg_T *cap);
82static void nv_regname(cmdarg_T *cap);
83static void nv_visual(cmdarg_T *cap);
84static void n_start_visual_mode(int c);
85static void nv_window(cmdarg_T *cap);
86static void nv_suspend(cmdarg_T *cap);
87static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010088static void nv_dot(cmdarg_T *cap);
Shougo Matsushita4ede01f2022-01-20 15:26:03 +000089static void nv_redo_or_register(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010090static void nv_Undo(cmdarg_T *cap);
91static void nv_tilde(cmdarg_T *cap);
92static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000093#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010094static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000095#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010096static void nv_lineop(cmdarg_T *cap);
97static void nv_home(cmdarg_T *cap);
98static void nv_pipe(cmdarg_T *cap);
99static void nv_bck_word(cmdarg_T *cap);
100static void nv_wordcmd(cmdarg_T *cap);
101static void nv_beginline(cmdarg_T *cap);
102static void adjust_cursor(oparg_T *oap);
103static void adjust_for_sel(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100104static void nv_select(cmdarg_T *cap);
105static void nv_goto(cmdarg_T *cap);
106static void nv_normal(cmdarg_T *cap);
107static void nv_esc(cmdarg_T *oap);
108static void nv_edit(cmdarg_T *cap);
109static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100110static void nv_object(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100111static void nv_record(cmdarg_T *cap);
112static void nv_at(cmdarg_T *cap);
113static void nv_halfpage(cmdarg_T *cap);
114static void nv_join(cmdarg_T *cap);
115static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200116static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100117static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100119static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100122static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100124static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125
ichizok672776d2022-01-31 12:27:18 +0000126// Declare nv_cmds[].
127#define DO_DECLARE_NVCMD
128#include "nv_cmds.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Yegappan Lakshmanan4dc0dd82022-01-29 13:06:40 +0000130// Include the lookuptable generated by create_nvcmdidx.vim.
131#include "nv_cmdidxs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133/*
134 * Search for a command in the commands table.
135 * Returns -1 for invalid command.
136 */
137 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100138find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139{
140 int i;
141 int idx;
142 int top, bot;
143 int c;
144
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100145 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 if (cmdchar >= 0x100)
147 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100149 // We use the absolute value of the character. Special keys have a
150 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 if (cmdchar < 0)
152 cmdchar = -cmdchar;
153
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100154 // If the character is in the first part: The character is the index into
155 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 if (cmdchar <= nv_max_linear)
157 return nv_cmd_idx[cmdchar];
158
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100159 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 bot = nv_max_linear + 1;
161 top = NV_CMDS_SIZE - 1;
162 idx = -1;
163 while (bot <= top)
164 {
165 i = (top + bot) / 2;
166 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
167 if (c < 0)
168 c = -c;
169 if (cmdchar == c)
170 {
171 idx = nv_cmd_idx[i];
172 break;
173 }
174 if (cmdchar > c)
175 bot = i + 1;
176 else
177 top = i - 1;
178 }
179 return idx;
180}
181
182/*
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100183 * If currently editing a cmdline or text is locked: beep and give an error
184 * message, return TRUE.
185 */
186 static int
187check_text_locked(oparg_T *oap)
188{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000189 if (!text_locked())
190 return FALSE;
191
192 if (oap != NULL)
193 clearopbeep(oap);
194 text_locked_msg();
195 return TRUE;
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100196}
197
198/*
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000199 * If text is locked, "curbuf_lock" or "allbuf_lock" is set:
200 * Give an error message, possibly beep and return TRUE.
201 * "oap" may be NULL.
202 */
203 int
204check_text_or_curbuf_locked(oparg_T *oap)
205{
206 if (check_text_locked(oap))
207 return TRUE;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000208
209 if (!curbuf_locked())
210 return FALSE;
211
212 if (oap != NULL)
213 clearop(oap);
214 return TRUE;
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000215}
216
217/*
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000218 * Handle the count before a normal command and set cap->count0.
219 */
220 static int
221normal_cmd_get_count(
222 cmdarg_T *cap,
223 int c,
224 int toplevel UNUSED,
225 int set_prevcount UNUSED,
226 int *ctrl_w,
227 int *need_flushbuf UNUSED)
228{
229getcount:
230 if (!(VIsual_active && VIsual_select))
231 {
232 // Handle a count before a command and compute ca.count0.
233 // Note that '0' is a command and not the start of a count, but it's
234 // part of a count after other digits.
235 while ((c >= '1' && c <= '9')
236 || (cap->count0 != 0 && (c == K_DEL || c == K_KDEL
237 || c == '0')))
238 {
239 if (c == K_DEL || c == K_KDEL)
240 {
241 cap->count0 /= 10;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000242 del_from_showcmd(4); // delete the digit and ~@%
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000243 }
244 else if (cap->count0 > 99999999L)
245 {
246 cap->count0 = 999999999L;
247 }
248 else
249 {
250 cap->count0 = cap->count0 * 10 + (c - '0');
251 }
252#ifdef FEAT_EVAL
253 // Set v:count here, when called from main() and not a stuffed
254 // command, so that v:count can be used in an expression mapping
255 // right after the count. Do set it for redo.
256 if (toplevel && readbuf1_empty())
257 set_vcount_ca(cap, &set_prevcount);
258#endif
259 if (*ctrl_w)
260 {
261 ++no_mapping;
262 ++allow_keys; // no mapping for nchar, but keys
263 }
264 ++no_zero_mapping; // don't map zero here
265 c = plain_vgetc();
266 LANGMAP_ADJUST(c, TRUE);
267 --no_zero_mapping;
268 if (*ctrl_w)
269 {
270 --no_mapping;
271 --allow_keys;
272 }
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000273 *need_flushbuf |= add_to_showcmd(c);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000274 }
275
276 // If we got CTRL-W there may be a/another count
277 if (c == Ctrl_W && !*ctrl_w && cap->oap->op_type == OP_NOP)
278 {
279 *ctrl_w = TRUE;
280 cap->opcount = cap->count0; // remember first count
281 cap->count0 = 0;
282 ++no_mapping;
283 ++allow_keys; // no mapping for nchar, but keys
284 c = plain_vgetc(); // get next character
285 LANGMAP_ADJUST(c, TRUE);
286 --no_mapping;
287 --allow_keys;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000288 *need_flushbuf |= add_to_showcmd(c);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000289 goto getcount; // jump back
290 }
291 }
292
293 if (c == K_CURSORHOLD)
294 {
295 // Save the count values so that ca.opcount and ca.count0 are exactly
296 // the same when coming back here after handling K_CURSORHOLD.
297 cap->oap->prev_opcount = cap->opcount;
298 cap->oap->prev_count0 = cap->count0;
299 }
300 else if (cap->opcount != 0)
301 {
302 // If we're in the middle of an operator (including after entering a
303 // yank buffer with '"') AND we had a count before the operator, then
304 // that count overrides the current value of ca.count0.
305 // What this means effectively, is that commands like "3dw" get turned
306 // into "d3w" which makes things fall into place pretty neatly.
307 // If you give a count before AND after the operator, they are
308 // multiplied.
309 if (cap->count0)
310 {
311 if (cap->opcount >= 999999999L / cap->count0)
312 cap->count0 = 999999999L;
313 else
314 cap->count0 *= cap->opcount;
315 }
316 else
317 cap->count0 = cap->opcount;
318 }
319
320 // Always remember the count. It will be set to zero (on the next call,
321 // above) when there is no pending operator.
322 // When called from main(), save the count for use by the "count" built-in
323 // variable.
324 cap->opcount = cap->count0;
325 cap->count1 = (cap->count0 == 0 ? 1 : cap->count0);
326
327#ifdef FEAT_EVAL
328 // Only set v:count when called from main() and not a stuffed command.
329 // Do set it for redo.
330 if (toplevel && readbuf1_empty())
331 set_vcount(cap->count0, cap->count1, set_prevcount);
332#endif
333
334 return c;
335}
336
337/*
338 * Returns TRUE if the normal command (cap) needs a second character.
339 */
340 static int
341normal_cmd_needs_more_chars(cmdarg_T *cap, short_u cmd_flags)
342{
343 return ((cmd_flags & NV_NCH)
344 && (((cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
345 && cap->oap->op_type == OP_NOP)
346 || (cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
347 || (cap->cmdchar == 'q'
348 && cap->oap->op_type == OP_NOP
349 && reg_recording == 0
350 && reg_executing == 0)
351 || ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
352 && (cap->oap->op_type != OP_NOP || VIsual_active))));
353}
354
355/*
356 * Get one or more additional characters for a normal command.
357 * Return the updated command index (if changed).
358 */
359 static int
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000360normal_cmd_get_more_chars(
361 int idx_arg,
362 cmdarg_T *cap,
363 int *need_flushbuf UNUSED)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000364{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000365 int idx = idx_arg;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000366 int c;
367 int *cp;
368 int repl = FALSE; // get character for replace mode
369 int lit = FALSE; // get extra character literally
370 int langmap_active = FALSE; // using :lmap mappings
371 int lang; // getting a text character
372#ifdef HAVE_INPUT_METHOD
373 int save_smd; // saved value of p_smd
374#endif
375
376 ++no_mapping;
377 ++allow_keys; // no mapping for nchar, but allow key codes
378 // Don't generate a CursorHold event here, most commands can't handle
379 // it, e.g., nv_replace(), nv_csearch().
380 did_cursorhold = TRUE;
381 if (cap->cmdchar == 'g')
382 {
383 /*
384 * For 'g' get the next character now, so that we can check for
385 * "gr", "g'" and "g`".
386 */
387 cap->nchar = plain_vgetc();
388 LANGMAP_ADJUST(cap->nchar, TRUE);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000389 *need_flushbuf |= add_to_showcmd(cap->nchar);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000390 if (cap->nchar == 'r' || cap->nchar == '\'' || cap->nchar == '`'
391 || cap->nchar == Ctrl_BSL)
392 {
393 cp = &cap->extra_char; // need to get a third character
394 if (cap->nchar != 'r')
395 lit = TRUE; // get it literally
396 else
397 repl = TRUE; // get it in replace mode
398 }
399 else
400 cp = NULL; // no third character needed
401 }
402 else
403 {
404 if (cap->cmdchar == 'r') // get it in replace mode
405 repl = TRUE;
406 cp = &cap->nchar;
407 }
408 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
409
410 /*
411 * Get a second or third character.
412 */
413 if (cp != NULL)
414 {
415 if (repl)
416 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100417 State = MODE_REPLACE; // pretend Replace mode
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000418#ifdef CURSOR_SHAPE
419 ui_cursor_shape(); // show different cursor shape
420#endif
421 }
422 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
423 {
424 // Allow mappings defined with ":lmap".
425 --no_mapping;
426 --allow_keys;
427 if (repl)
Bram Moolenaar24959102022-05-07 20:01:16 +0100428 State = MODE_LREPLACE;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000429 else
Bram Moolenaar24959102022-05-07 20:01:16 +0100430 State = MODE_LANGMAP;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000431 langmap_active = TRUE;
432 }
433#ifdef HAVE_INPUT_METHOD
434 save_smd = p_smd;
435 p_smd = FALSE; // Don't let the IM code show the mode here
436 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
437 im_set_active(TRUE);
438#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100439 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000440 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100441 MAY_WANT_TO_LOG_THIS;
442
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000443 // Disable bracketed paste and modifyOtherKeys here, we won't
444 // recognize the escape sequences with 'esckeys' off.
445 out_str(T_BD);
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000446 out_str_t_TE();
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000447 }
448
449 *cp = plain_vgetc();
450
Bram Moolenaar24959102022-05-07 20:01:16 +0100451 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000452 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100453 MAY_WANT_TO_LOG_THIS;
454
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000455 // Re-enable bracketed paste mode and modifyOtherKeys
Bram Moolenaarfc966c12023-01-01 18:04:33 +0000456 out_str_t_BE();
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000457 out_str_t_TI();
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000458 }
459
460 if (langmap_active)
461 {
462 // Undo the decrement done above
463 ++no_mapping;
464 ++allow_keys;
Bram Moolenaar24959102022-05-07 20:01:16 +0100465 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000466 }
467#ifdef HAVE_INPUT_METHOD
468 if (lang)
469 {
470 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
471 im_save_status(&curbuf->b_p_iminsert);
472 im_set_active(FALSE);
473 }
474 p_smd = save_smd;
475#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100476 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000477 *need_flushbuf |= add_to_showcmd(*cp);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000478
479 if (!lit)
480 {
481#ifdef FEAT_DIGRAPHS
482 // Typing CTRL-K gets a digraph.
483 if (*cp == Ctrl_K
484 && ((nv_cmds[idx].cmd_flags & NV_LANG)
485 || cp == &cap->extra_char)
486 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
487 {
488 c = get_digraph(FALSE);
489 if (c > 0)
490 {
491 *cp = c;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000492 // Guessing how to update showcmd here...
493 del_from_showcmd(3);
494 *need_flushbuf |= add_to_showcmd(*cp);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000495 }
496 }
497#endif
498
499 // adjust chars > 127, except after "tTfFr" commands
500 LANGMAP_ADJUST(*cp, !lang);
501#ifdef FEAT_RIGHTLEFT
502 // adjust Hebrew mapped char
503 if (p_hkmap && lang && KeyTyped)
504 *cp = hkmap(*cp);
505#endif
506 }
507
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000508 // When the next character is CTRL-\ a following CTRL-N means the
509 // command is aborted and we go to Normal mode.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000510 if (cp == &cap->extra_char
511 && cap->nchar == Ctrl_BSL
512 && (cap->extra_char == Ctrl_N || cap->extra_char == Ctrl_G))
513 {
514 cap->cmdchar = Ctrl_BSL;
515 cap->nchar = cap->extra_char;
516 idx = find_command(cap->cmdchar);
517 }
zeertzjq4f026ea2023-02-26 14:47:24 +0000518 else if ((cap->nchar == 'n' || cap->nchar == 'N')
519 && cap->cmdchar == 'g')
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000520 cap->oap->op_type = get_op_type(*cp, NUL);
521 else if (*cp == Ctrl_BSL)
522 {
523 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
524
525 // There is a busy wait here when typing "f<C-\>" and then
526 // something different from CTRL-N. Can't be avoided.
527 while ((c = vpeekc()) <= 0 && towait > 0L)
528 {
529 do_sleep(towait > 50L ? 50L : towait, FALSE);
530 towait -= 50L;
531 }
532 if (c > 0)
533 {
534 c = plain_vgetc();
535 if (c != Ctrl_N && c != Ctrl_G)
536 vungetc(c);
537 else
538 {
539 cap->cmdchar = Ctrl_BSL;
540 cap->nchar = c;
541 idx = find_command(cap->cmdchar);
542 }
543 }
544 }
545
zeertzjqbacc8302023-08-12 00:09:31 +0200546 if (enc_utf8 && lang)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000547 {
zeertzjqbacc8302023-08-12 00:09:31 +0200548 // When getting a text character and the next character is a
549 // multi-byte character, it could be a composing character.
550 // However, don't wait for it to arrive. Also, do enable mapping,
551 // because if it's put back with vungetc() it's too late to apply
552 // mapping.
553 --no_mapping;
554 while ((c = vpeekc()) > 0
555 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000556 {
zeertzjqbacc8302023-08-12 00:09:31 +0200557 c = plain_vgetc();
558 if (!utf_iscomposing(c))
559 {
560 vungetc(c); // it wasn't, put it back
561 break;
562 }
563 else if (cap->ncharC1 == 0)
564 cap->ncharC1 = c;
565 else
566 cap->ncharC2 = c;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000567 }
zeertzjqbacc8302023-08-12 00:09:31 +0200568 ++no_mapping;
569 // Vim may be in a different mode when the user types the next key,
570 // but when replaying a recording the next key is already in the
zeertzjqbf321802024-01-28 19:03:00 +0100571 // typeahead buffer, so record an <Ignore> before that to prevent
572 // the vpeekc() above from applying wrong mappings when replaying.
zeertzjqdccc29c2023-09-04 22:25:07 +0200573 ++no_u_sync;
zeertzjqbf321802024-01-28 19:03:00 +0100574 gotchars_ignore();
zeertzjqdccc29c2023-09-04 22:25:07 +0200575 --no_u_sync;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000576 }
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000577 }
578 --no_mapping;
579 --allow_keys;
580
581 return idx;
582}
583
584/*
585 * Returns TRUE if after processing a normal mode command, need to wait for a
586 * moment when a message is displayed that will be overwritten by the mode
587 * message.
588 */
589 static int
590normal_cmd_need_to_wait_for_msg(cmdarg_T *cap, pos_T *old_pos)
591{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000592 // In Visual mode and with "^O" in Insert mode, a short message will be
593 // overwritten by the mode message. Wait a bit, until a key is hit.
594 // In Visual mode, it's more important to keep the Visual area updated
595 // than keeping a message (e.g. from a /pat search).
596 // Only do this if the command was typed, not from a mapping.
597 // Don't wait when emsg_silent is non-zero.
598 // Also wait a bit after an error message, e.g. for "^O:".
599 // Don't redraw the screen, it would remove the message.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000600 return ( ((p_smd
601 && msg_silent == 0
602 && (restart_edit != 0
603 || (VIsual_active
604 && old_pos->lnum == curwin->w_cursor.lnum
605 && old_pos->col == curwin->w_cursor.col)
606 )
607 && (clear_cmdline
608 || redraw_cmdline)
609 && (msg_didout || (msg_didany && msg_scroll))
610 && !msg_nowait
611 && KeyTyped)
612 || (restart_edit != 0
613 && !VIsual_active
614 && (msg_scroll
615 || emsg_on_display)))
616 && cap->oap->regname == 0
617 && !(cap->retval & CA_COMMAND_BUSY)
618 && stuff_empty()
619 && typebuf_typed()
620 && emsg_silent == 0
621 && !in_assert_fails
622 && !did_wait_return
623 && cap->oap->op_type == OP_NOP);
624}
625
626/*
627 * After processing a normal mode command, wait for a moment when a message is
628 * displayed that will be overwritten by the mode message.
629 */
630 static void
631normal_cmd_wait_for_msg(void)
632{
633 int save_State = State;
634
635 // Draw the cursor with the right shape here
636 if (restart_edit != 0)
Bram Moolenaar24959102022-05-07 20:01:16 +0100637 State = MODE_INSERT;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000638
639 // If need to redraw, and there is a "keep_msg", redraw before the
640 // delay
641 if (must_redraw && keep_msg != NULL && !emsg_on_display)
642 {
643 char_u *kmsg;
644
645 kmsg = keep_msg;
646 keep_msg = NULL;
647 // Showmode() will clear keep_msg, but we want to use it anyway.
648 // First update w_topline.
649 setcursor();
650 update_screen(0);
651 // now reset it, otherwise it's put in the history again
652 keep_msg = kmsg;
653
654 kmsg = vim_strsave(keep_msg);
655 if (kmsg != NULL)
656 {
657 msg_attr((char *)kmsg, keep_msg_attr);
658 vim_free(kmsg);
659 }
660 }
661 setcursor();
662#ifdef CURSOR_SHAPE
663 ui_cursor_shape(); // may show different cursor shape
664#endif
665 cursor_on();
666 out_flush();
667 if (msg_scroll || emsg_on_display)
668 ui_delay(1003L, TRUE); // wait at least one second
669 ui_delay(3003L, FALSE); // wait up to three seconds
670 State = save_State;
671
672 msg_scroll = FALSE;
673 emsg_on_display = FALSE;
674}
675
676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 * Execute a command in Normal mode.
678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100680normal_cmd(
681 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100682 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100684 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100686 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 int old_col = curwin->w_curswant;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000688 int need_flushbuf = FALSE; // need to call out_flush()
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100689 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 static int old_mapped_len = 0;
692 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000693 int set_prevcount = FALSE;
Bram Moolenaarb146e012020-07-19 23:06:05 +0200694 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695
Bram Moolenaara80faa82020-04-12 19:37:17 +0200696 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000698
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100699 // Use a count remembered from before entering an operator. After typing
700 // "3d" we return from normal_cmd() and come back here, the "3" is
701 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 ca.opcount = opcount;
703
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000704 // If there is an operator pending, then the command we take this time
705 // will terminate it. Finish_op tells us to finish the operation before
706 // returning this time (unless the operation was cancelled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707#ifdef CURSOR_SHAPE
708 c = finish_op;
709#endif
710 finish_op = (oap->op_type != OP_NOP);
711#ifdef CURSOR_SHAPE
712 if (finish_op != c)
713 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100714 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715# ifdef FEAT_MOUSESHAPE
716 update_mouseshape(-1);
717# endif
718 }
719#endif
LemonBoy2bf52dd2022-04-09 18:17:34 +0100720 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100722 // When not finishing an operator and no register name typed, reset the
723 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000725 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000727#ifdef FEAT_EVAL
728 set_prevcount = TRUE;
729#endif
730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100732 // Restore counts from before receiving K_CURSORHOLD. This means after
733 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
734 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000735 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
736 {
737 ca.opcount = oap->prev_opcount;
738 ca.count0 = oap->prev_count0;
739 oap->prev_opcount = 0;
740 oap->prev_count0 = 0;
741 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000742
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744
Bram Moolenaar24959102022-05-07 20:01:16 +0100745 State = MODE_NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100747 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748#endif
749
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100750#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100751 // Set v:count here, when called from main() and not a stuffed
752 // command, so that v:count can be used in an expression mapping
753 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100754 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100755 set_vcount_ca(&ca, &set_prevcount);
756#endif
757
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 /*
759 * Get the command character from the user.
760 */
761 c = safe_vgetc();
Bram Moolenaar24959102022-05-07 20:01:16 +0100762 LANGMAP_ADJUST(c, get_real_state() != MODE_SELECT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000764 // If a mapping was started in Visual or Select mode, remember the length
765 // of the mapping. This is used below to not return to Insert mode for as
766 // long as the mapping is being executed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 if (restart_edit == 0)
768 old_mapped_len = 0;
769 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000770 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 old_mapped_len = typebuf_maplen();
772
773 if (c == NUL)
774 c = K_ZERO;
775
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000776 // In Select mode, typed text replaces the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 if (VIsual_active
778 && VIsual_select
779 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
780 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000781 int len;
782
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100783 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
784 // 'insertmode' is set) fake a "d"elete command, Insert mode will
785 // restart automatically.
786 // Insert the typed character in the typeahead buffer, so that it can
787 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000788 len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
789
zeertzjqfbf4f1c2022-01-28 12:50:43 +0000790 // When recording and gotchars() was called the character will be
791 // recorded again, remove the previous recording.
792 if (KeyTyped)
793 ungetchars(len);
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000794
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000795 if (restart_edit != 0)
796 c = 'd';
797 else
798 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100799 msg_nowait = TRUE; // don't delay going to insert mode
800 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
Bram Moolenaard0fb9072021-12-09 11:57:22 +0000803 // If the window was made so small that nothing shows, make it at least one
804 // line and one column when typing a command.
805 if (KeyTyped && !KeyStuffed)
806 win_ensure_size();
807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 need_flushbuf = add_to_showcmd(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000810 // Get the command count
811 c = normal_cmd_get_count(&ca, c, toplevel, set_prevcount, &ctrl_w,
812 &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000814 // Find the command character in the table of commands.
815 // For CTRL-W we already got nchar when looking for a count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 if (ctrl_w)
817 {
818 ca.nchar = c;
819 ca.cmdchar = Ctrl_W;
820 }
821 else
822 ca.cmdchar = c;
823 idx = find_command(ca.cmdchar);
824 if (idx < 0)
825 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100826 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 clearopbeep(oap);
828 goto normal_end;
829 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000830
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000831 if ((nv_cmds[idx].cmd_flags & NV_NCW) && check_text_or_curbuf_locked(oap))
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100832 // this command is not allowed now
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000833 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000835 // In Visual/Select mode, a few keys are handled in a special way.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 if (VIsual_active)
837 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100838 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 if (km_stopsel
840 && (nv_cmds[idx].cmd_flags & NV_STS)
841 && !(mod_mask & MOD_MASK_SHIFT))
842 {
843 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100844 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 }
846
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100847 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 if (km_startsel)
849 {
850 if (nv_cmds[idx].cmd_flags & NV_SS)
851 {
852 unshift_special(&ca);
853 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000854 if (idx < 0)
855 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100856 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000857 clearopbeep(oap);
858 goto normal_end;
859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 }
861 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
862 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 }
865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866
867#ifdef FEAT_RIGHTLEFT
868 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
869 && (nv_cmds[idx].cmd_flags & NV_RL))
870 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100871 // Invert horizontal movements and operations. Only when typed by the
872 // user directly, not when the result of a mapping or "x" translated
873 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 switch (ca.cmdchar)
875 {
876 case 'l': ca.cmdchar = 'h'; break;
877 case K_RIGHT: ca.cmdchar = K_LEFT; break;
878 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
879 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
880 case 'h': ca.cmdchar = 'l'; break;
881 case K_LEFT: ca.cmdchar = K_RIGHT; break;
882 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
883 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
884 case '>': ca.cmdchar = '<'; break;
885 case '<': ca.cmdchar = '>'; break;
886 }
887 idx = find_command(ca.cmdchar);
888 }
889#endif
890
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000891 // Get additional characters if we need them.
892 if (normal_cmd_needs_more_chars(&ca, nv_cmds[idx].cmd_flags))
893 idx = normal_cmd_get_more_chars(idx, &ca, &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000895 // Flush the showcmd characters onto the screen so we can see them while
896 // the command is being executed. Only do this when the shown command was
897 // actually displayed, otherwise this will slow down a lot when executing
898 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 if (need_flushbuf)
900 out_flush();
Martin Tournoijba43e762022-10-13 22:12:15 +0100901
Bram Moolenaard9205ca2008-10-02 20:55:54 +0000902 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +0200903 {
904 if (ex_normal_busy)
905 did_cursorhold = save_did_cursorhold;
906 else
907 did_cursorhold = FALSE;
908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909
Bram Moolenaar24959102022-05-07 20:01:16 +0100910 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911
zeertzjqf86dea82023-03-05 21:15:06 +0000912 if (ca.nchar == ESC || ca.extra_char == ESC)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 {
914 clearop(oap);
915 if (restart_edit == 0 && goto_im())
916 restart_edit = 'a';
917 goto normal_end;
918 }
919
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000920 if (ca.cmdchar != K_IGNORE)
921 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100922 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000923 msg_col = 0;
924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925
Luuk van Baalaa7f25e2023-04-13 21:47:38 +0100926 old_pos = curwin->w_cursor; // remember where the cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100928 // When 'keymodel' contains "startsel" some keys start Select/Visual
929 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 if (!VIsual_active && km_startsel)
931 {
932 if (nv_cmds[idx].cmd_flags & NV_SS)
933 {
934 start_selection();
935 unshift_special(&ca);
936 idx = find_command(ca.cmdchar);
937 }
938 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
939 && (mod_mask & MOD_MASK_SHIFT))
940 {
941 start_selection();
942 mod_mask &= ~MOD_MASK_SHIFT;
943 }
944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000946 // Execute the command!
947 // Call the command function found in the commands table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 ca.arg = nv_cmds[idx].cmd_arg;
949 (nv_cmds[idx].cmd_func)(&ca);
950
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000951 // If we didn't start or finish an operator, reset oap->regname, unless we
952 // need it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 if (!finish_op
954 && !oap->op_type
955 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
956 {
957 clearop(oap);
958#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +0200959 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960#endif
961 }
962
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100963 // Get the length of mapped chars again after typing a count, second
964 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000965 if (old_mapped_len > 0)
966 old_mapped_len = typebuf_maplen();
967
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000968 // If an operation is pending, handle it. But not for K_IGNORE or
969 // K_MOUSEMOVE.
Bram Moolenaar1ad72c82021-05-04 21:56:28 +0200970 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +0100971 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000973 // Wait for a moment when a message is displayed that will be overwritten
974 // by the mode message.
975 if (normal_cmd_need_to_wait_for_msg(&ca, &old_pos))
976 normal_cmd_wait_for_msg();
977
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000978 // Finish up after executing a Normal mode command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979normal_end:
980
981 msg_nowait = FALSE;
982
Bram Moolenaarcc613032020-06-07 21:31:18 +0200983#ifdef FEAT_EVAL
984 if (finish_op)
985 reset_reg_var();
986#endif
987
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988#ifdef CURSOR_SHAPE
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000989 int prev_finish_op = finish_op;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990#endif
zeertzjq73916ba2023-04-26 16:50:19 +0100991 if (oap->op_type == OP_NOP)
992 {
993 // Reset finish_op, in case it was set
994 finish_op = FALSE;
995 may_trigger_modechanged();
996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100998 // Redraw the cursor with another shape, if we were in Operator-pending
999 // mode or did a replace command.
zeertzjqf86dea82023-03-05 21:15:06 +00001000 if (prev_finish_op || ca.cmdchar == 'r'
1001 || (ca.cmdchar == 'g' && ca.nchar == 'r'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001003 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004# ifdef FEAT_MOUSESHAPE
1005 update_mouseshape(-1);
1006# endif
1007 }
1008#endif
1009
Bram Moolenaara983fe92008-07-31 20:04:27 +00001010 if (oap->op_type == OP_NOP && oap->regname == 0
zeertzjq73916ba2023-04-26 16:50:19 +01001011 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001014 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 vim_free(ca.searchbuf);
1016
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 if (has_mbyte)
1018 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 if (curwin->w_p_scb && toplevel)
1021 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001022 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 do_check_scrollbind(TRUE);
1024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025
Bram Moolenaar860cae12010-06-05 23:22:07 +02001026 if (curwin->w_p_crb && toplevel)
1027 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001028 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001029 do_check_cursorbind();
1030 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001031
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001032#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001033 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001034 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001035 restart_edit = 0;
1036#endif
1037
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001038 // May restart edit(), if we got here with CTRL-O in Insert mode (but not
1039 // if still inside a mapping that started in Visual mode).
1040 // May switch from Visual to Select mode after CTRL-O command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1043 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 && !(ca.retval & CA_COMMAND_BUSY)
1045 && stuff_empty()
1046 && oap->regname == 0)
1047 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 if (restart_VIsual_select == 1)
1049 {
1050 VIsual_select = TRUE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01001051 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 showmode();
1053 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001054 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001056 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 (void)edit(restart_edit, FALSE, 1L);
1058 }
1059
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 if (restart_VIsual_select == 2)
1061 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001063 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 opcount = ca.opcount;
1065}
1066
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001067#ifdef FEAT_EVAL
1068/*
1069 * Set v:count and v:count1 according to "cap".
1070 * Set v:prevcount only when "set_prevcount" is TRUE.
1071 */
1072 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001073set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001074{
1075 long count = cap->count0;
1076
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001077 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001078 if (cap->opcount != 0)
1079 count = cap->opcount * (count == 0 ? 1 : count);
1080 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001081 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001082}
1083#endif
1084
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001086 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 * if not.
1088 */
1089 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001090check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091{
1092 static int did_check = FALSE;
1093
1094 if (full_screen)
1095 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001096 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001097 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 did_check = TRUE;
1099 }
1100}
1101
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001102#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1103/*
1104 * Call yank_do_autocmd() for "regname".
1105 */
1106 static void
1107call_yank_do_autocmd(int regname)
1108{
1109 oparg_T oa;
1110 yankreg_T *reg;
1111
1112 clear_oparg(&oa);
1113 oa.regname = regname;
1114 oa.op_type = OP_YANK;
1115 oa.is_VIsual = TRUE;
1116 reg = get_register(regname, TRUE);
1117 yank_do_autocmd(&oa, reg);
1118 free_register(reg);
1119}
1120#endif
1121
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001123 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001124 * This function or the next should ALWAYS be called to end Visual mode, except
1125 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 */
1127 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001128end_visual_mode(void)
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001129{
1130 end_visual_mode_keep_button();
1131 reset_held_button();
1132}
1133
1134 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001135end_visual_mode_keep_button(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136{
1137#ifdef FEAT_CLIPBOARD
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001138 // If we are using the clipboard, then remember what was selected in case
1139 // we need to paste it somewhere while we still own the selection.
1140 // Only do this when the clipboard is already owned. Don't want to grab
1141 // the selection when hitting ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 if (clip_star.available && clip_star.owned)
1143 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001144
1145# if defined(FEAT_EVAL)
1146 // Emit a TextYankPost for the automatic copy of the selection into the
1147 // star and/or plus register.
1148 if (has_textyankpost())
1149 {
1150 if (clip_isautosel_star())
1151 call_yank_do_autocmd('*');
1152 if (clip_isautosel_plus())
1153 call_yank_do_autocmd('+');
1154 }
1155# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156#endif
1157
1158 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 setmouse();
1160 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001162 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001163 curbuf->b_visual.vi_mode = VIsual_mode;
1164 curbuf->b_visual.vi_start = VIsual;
1165 curbuf->b_visual.vi_end = curwin->w_cursor;
1166 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167#ifdef FEAT_EVAL
1168 curbuf->b_visual_mode_eval = VIsual_mode;
1169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 if (!virtual_active())
1171 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001172 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001174 adjust_cursor_eol();
LemonBoy2bf52dd2022-04-09 18:17:34 +01001175 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176}
1177
1178/*
1179 * Reset VIsual_active and VIsual_reselect.
1180 */
1181 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001182reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183{
1184 if (VIsual_active)
1185 {
1186 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001187 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 }
1189 VIsual_reselect = FALSE;
1190}
1191
1192/*
1193 * Reset VIsual_active and VIsual_reselect if it's set.
1194 */
1195 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001196reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197{
1198 if (VIsual_active)
1199 {
1200 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001201 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 VIsual_reselect = FALSE;
1203 }
1204}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001206 void
1207restore_visual_mode(void)
1208{
1209 if (VIsual_mode_orig != NUL)
1210 {
1211 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1212 VIsual_mode_orig = NUL;
1213 }
1214}
1215
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216/*
1217 * Check for a balloon-eval special item to include when searching for an
1218 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1219 * Returns TRUE if the character at "*ptr" should be included.
1220 * "dir" is FORWARD or BACKWARD, the direction of searching.
1221 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1222 * "bnp" points to a counter for square brackets.
1223 */
1224 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001225find_is_eval_item(
1226 char_u *ptr,
1227 int *colp,
1228 int *bnp,
1229 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001231 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1233 ++*bnp;
1234 if (*bnp > 0)
1235 {
1236 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1237 --*bnp;
1238 return TRUE;
1239 }
1240
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001241 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 if (*ptr == '.')
1243 return TRUE;
1244
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001245 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1247 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1248 {
1249 *colp += dir;
1250 return TRUE;
1251 }
1252 return FALSE;
1253}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254
1255/*
1256 * Find the identifier under or to the right of the cursor.
1257 * "find_type" can have one of three values:
1258 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001259 * FIND_STRING: find any non-white text
1260 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001261 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 *
1263 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001264 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001266 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 * This doesn't match the real Vi but I like it a little better and it
1268 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001269 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 * When FIND_IDENT isn't defined, we backup until a blank.
1271 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001272 * Returns the length of the text, or zero if no text is found.
1273 * If text is found, a pointer to the text is put in "*text". This
1274 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 */
1276 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001277find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278{
1279 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001280 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281}
1282
1283/*
1284 * Like find_ident_under_cursor(), but for any window and any position.
1285 * However: Uses 'iskeyword' from the current window!.
1286 */
1287 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001288find_ident_at_pos(
1289 win_T *wp,
1290 linenr_T lnum,
1291 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001292 char_u **text,
1293 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001294 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295{
1296 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001297 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 int this_class = 0;
1300 int prev_class;
1301 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001302 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001304 // if i == 0: try to find an identifier
1305 // if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1307 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1308 {
1309 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001310 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 */
1312 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 if (has_mbyte)
1314 {
1315 while (ptr[col] != NUL)
1316 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001317 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1319 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 this_class = mb_get_class(ptr + col);
1321 if (this_class != 0 && (i == 1 || this_class != 1))
1322 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001323 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 }
1325 }
1326 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001328 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 )
1331 ++col;
1332
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001333 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
1336 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001337 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 if (has_mbyte)
1340 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001341 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1343 this_class = mb_get_class((char_u *)"a");
1344 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001346 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 {
1348 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1349 prev_class = mb_get_class(ptr + prevcol);
1350 if (this_class != prev_class
1351 && (i == 0
1352 || prev_class == 0
1353 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 && (!(find_type & FIND_EVAL)
1355 || prevcol == 0
1356 || !find_is_eval_item(ptr + prevcol, &prevcol,
1357 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 )
1359 break;
1360 col = prevcol;
1361 }
1362
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001363 // If we don't want just any old text, or we've found an
1364 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 if (this_class > 2)
1366 this_class = 2;
1367 if (!(find_type & FIND_STRING) || this_class == 2)
1368 break;
1369 }
1370 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 {
1372 while (col > 0
1373 && ((i == 0
1374 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001375 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 && (!(find_type & FIND_IDENT)
1377 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 || ((find_type & FIND_EVAL)
1379 && col > 1
1380 && find_is_eval_item(ptr + col - 1, &col,
1381 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 ))
1383 --col;
1384
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001385 // If we don't want just any old text, or we've found an
1386 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1388 break;
1389 }
1390 }
1391
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001392 if (ptr[col] == NUL || (i == 0
1393 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001395 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001396 if ((find_type & FIND_NOERROR) == 0)
1397 {
1398 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001399 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001400 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001401 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 return 0;
1404 }
1405 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001406 *text = ptr;
1407 if (textcol != NULL)
1408 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409
1410 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001411 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 bn = 0;
1414 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 if (has_mbyte)
1417 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001418 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 this_class = mb_get_class(ptr);
1420 while (ptr[col] != NUL
1421 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1422 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 || ((find_type & FIND_EVAL)
1424 && col <= (int)startcol
1425 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001427 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 }
1429 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001431 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 || ((find_type & FIND_EVAL)
1433 && col <= (int)startcol
1434 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437
1438 return col;
1439}
1440
1441/*
1442 * Prepare for redo of a normal command.
1443 */
1444 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001445prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446{
1447 prep_redo(cap->oap->regname, cap->count0,
1448 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1449}
1450
1451/*
1452 * Prepare for redo of any command.
1453 * Note that only the last argument can be a multi-byte char.
1454 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001455 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001456prep_redo(
1457 int regname,
1458 long num,
1459 int cmd1,
1460 int cmd2,
1461 int cmd3,
1462 int cmd4,
1463 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001465 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1466}
1467
1468/*
1469 * Prepare for redo of any command with extra count after "cmd2".
1470 */
1471 void
1472prep_redo_num2(
1473 int regname,
1474 long num1,
1475 int cmd1,
1476 int cmd2,
1477 long num2,
1478 int cmd3,
1479 int cmd4,
1480 int cmd5)
1481{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 ResetRedobuff();
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001483
1484#ifdef FEAT_EVAL
1485 // Put info about a mapping in the redo buffer, so that "." will use the
1486 // same script context.
1487 may_add_last_used_map_to_redobuff();
1488#endif
1489
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001490 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 {
1492 AppendCharToRedobuff('"');
1493 AppendCharToRedobuff(regname);
1494 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001495 if (num1 != 0)
1496 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 if (cmd1 != NUL)
1498 AppendCharToRedobuff(cmd1);
1499 if (cmd2 != NUL)
1500 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001501 if (num2 != 0)
1502 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 if (cmd3 != NUL)
1504 AppendCharToRedobuff(cmd3);
1505 if (cmd4 != NUL)
1506 AppendCharToRedobuff(cmd4);
1507 if (cmd5 != NUL)
1508 AppendCharToRedobuff(cmd5);
1509}
1510
1511/*
zeertzjq73916ba2023-04-26 16:50:19 +01001512 * Check for operator active and clear it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 *
zeertzjq73916ba2023-04-26 16:50:19 +01001514 * Beep and return TRUE if an operator was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 */
1516 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001517checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
1519 if (oap->op_type == OP_NOP)
1520 return FALSE;
1521 clearopbeep(oap);
1522 return TRUE;
1523}
1524
1525/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001526 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 *
zeertzjq73916ba2023-04-26 16:50:19 +01001528 * Beep and return TRUE if an operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 */
1530 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001531checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001533 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 return FALSE;
1535 clearopbeep(oap);
1536 return TRUE;
1537}
1538
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001539 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001540clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541{
1542 oap->op_type = OP_NOP;
1543 oap->regname = 0;
1544 oap->motion_force = NUL;
1545 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001546 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547}
1548
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001549 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001550clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551{
1552 clearop(oap);
1553 beep_flush();
1554}
1555
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556/*
1557 * Remove the shift modifier from a special key.
1558 */
1559 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001560unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561{
1562 switch (cap->cmdchar)
1563 {
1564 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1565 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1566 case K_S_UP: cap->cmdchar = K_UP; break;
1567 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1568 case K_S_HOME: cap->cmdchar = K_HOME; break;
1569 case K_S_END: cap->cmdchar = K_END; break;
1570 }
1571 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1572}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001574/*
1575 * If the mode is currently displayed clear the command line or update the
1576 * command displayed.
1577 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001578 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001579may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001580{
1581 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001582 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001583 else
1584 clear_showcmd();
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001585}
1586
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587/*
1588 * Routines for displaying a partly typed command
1589 */
1590
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001591static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592static int showcmd_is_clear = TRUE;
1593static int showcmd_visual = FALSE;
1594
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001595static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596
1597 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001598clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599{
1600 if (!p_sc)
1601 return;
1602
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 if (VIsual_active && !char_avail())
1604 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001605 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 long lines;
1607 colnr_T leftcol, rightcol;
1608 linenr_T top, bot;
1609
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001610 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001611 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 {
1613 top = VIsual.lnum;
1614 bot = curwin->w_cursor.lnum;
1615 }
1616 else
1617 {
1618 top = curwin->w_cursor.lnum;
1619 bot = VIsual.lnum;
1620 }
1621# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001622 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001623 (void)hasFolding(top, &top, NULL);
1624 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625# endif
1626 lines = bot - top + 1;
1627
1628 if (VIsual_mode == Ctrl_V)
1629 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001630# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001631 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001632 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001633
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001634 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001635 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001636 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001637# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001639# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001640 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001641 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001642# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1644 (long)(rightcol - leftcol + 1));
1645 }
1646 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1647 sprintf((char *)showcmd_buf, "%ld", lines);
1648 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001649 {
1650 char_u *s, *e;
1651 int l;
1652 int bytes = 0;
1653 int chars = 0;
1654
1655 if (cursor_bot)
1656 {
1657 s = ml_get_pos(&VIsual);
1658 e = ml_get_cursor();
1659 }
1660 else
1661 {
1662 s = ml_get_cursor();
1663 e = ml_get_pos(&VIsual);
1664 }
1665 while ((*p_sel != 'e') ? s <= e : s < e)
1666 {
1667 l = (*mb_ptr2len)(s);
1668 if (l == 0)
1669 {
1670 ++bytes;
1671 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001672 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001673 }
1674 bytes += l;
1675 ++chars;
1676 s += l;
1677 }
1678 if (bytes == chars)
1679 sprintf((char *)showcmd_buf, "%d", chars);
1680 else
1681 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1682 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001683 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 showcmd_visual = TRUE;
1685 }
1686 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 {
1688 showcmd_buf[0] = NUL;
1689 showcmd_visual = FALSE;
1690
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001691 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 if (showcmd_is_clear)
1693 return;
1694 }
1695
1696 display_showcmd();
1697}
1698
1699/*
1700 * Add 'c' to string of shown command chars.
1701 * Return TRUE if output has been written (and setcursor() has been called).
1702 */
1703 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001704add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705{
1706 char_u *p;
1707 int old_len;
1708 int extra_len;
1709 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 int i;
1711 static int ignore[] =
1712 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001713#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1715 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001716#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001717 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001718 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1720 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001721 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001723 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 0
1725 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726
Bram Moolenaar09df3122006-01-23 22:23:09 +00001727 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 return FALSE;
1729
1730 if (showcmd_visual)
1731 {
1732 showcmd_buf[0] = NUL;
1733 showcmd_visual = FALSE;
1734 }
1735
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001736 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 if (IS_SPECIAL(c))
1738 for (i = 0; ignore[i] != 0; ++i)
1739 if (ignore[i] == c)
1740 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741
1742 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001743 if (*p == ' ')
1744 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 old_len = (int)STRLEN(showcmd_buf);
1746 extra_len = (int)STRLEN(p);
1747 overflow = old_len + extra_len - SHOWCMD_COLS;
1748 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001749 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1750 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 STRCAT(showcmd_buf, p);
1752
1753 if (char_avail())
1754 return FALSE;
1755
1756 display_showcmd();
1757
1758 return TRUE;
1759}
1760
1761 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001762add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763{
1764 if (!add_to_showcmd(c))
1765 setcursor();
1766}
1767
1768/*
1769 * Delete 'len' characters from the end of the shown command.
1770 */
1771 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001772del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773{
1774 int old_len;
1775
1776 if (!p_sc)
1777 return;
1778
1779 old_len = (int)STRLEN(showcmd_buf);
1780 if (len > old_len)
1781 len = old_len;
1782 showcmd_buf[old_len - len] = NUL;
1783
1784 if (!char_avail())
1785 display_showcmd();
1786}
1787
1788/*
1789 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1790 * something and there is a partial mapping.
1791 */
1792 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001793push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794{
1795 if (p_sc)
1796 STRCPY(old_showcmd_buf, showcmd_buf);
1797}
1798
1799 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001800pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801{
1802 if (!p_sc)
1803 return;
1804
1805 STRCPY(showcmd_buf, old_showcmd_buf);
1806
1807 display_showcmd();
1808}
1809
1810 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001811display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812{
Luuk van Baalba936f62022-12-15 13:15:39 +00001813 int len = (int)STRLEN(showcmd_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814
Luuk van Baalba936f62022-12-15 13:15:39 +00001815 showcmd_is_clear = (len == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 cursor_off();
1817
Luuk van Baalba936f62022-12-15 13:15:39 +00001818 if (*p_sloc == 's')
Luuk van Baalaa7f25e2023-04-13 21:47:38 +01001819 {
1820 if (showcmd_is_clear)
1821 curwin->w_redr_status = TRUE;
1822 else
1823 win_redr_status(curwin, FALSE);
1824 }
Luuk van Baalba936f62022-12-15 13:15:39 +00001825 else if (*p_sloc == 't')
Luuk van Baalaa7f25e2023-04-13 21:47:38 +01001826 {
1827 if (showcmd_is_clear)
1828 redraw_tabline = TRUE;
1829 else
1830 draw_tabline();
1831 }
Luuk van Baalba936f62022-12-15 13:15:39 +00001832 else // 'showcmdloc' is "last" or empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 {
Luuk van Baalba936f62022-12-15 13:15:39 +00001834 if (!showcmd_is_clear)
1835 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836
Luuk van Baalba936f62022-12-15 13:15:39 +00001837 // clear the rest of an old message by outputting up to SHOWCMD_COLS
1838 // spaces
1839 screen_puts((char_u *)" " + len,
1840 (int)Rows - 1, sc_col + len, 0);
1841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001843 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846/*
1847 * When "check" is FALSE, prepare for commands that scroll the window.
1848 * When "check" is TRUE, take care of scroll-binding after the window has
1849 * scrolled. Called from normal_cmd() and edit().
1850 */
1851 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001852do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853{
1854 static win_T *old_curwin = NULL;
1855 static linenr_T old_topline = 0;
1856#ifdef FEAT_DIFF
1857 static int old_topfill = 0;
1858#endif
1859 static buf_T *old_buf = NULL;
1860 static colnr_T old_leftcol = 0;
1861
1862 if (check && curwin->w_p_scb)
1863 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001864 // If a ":syncbind" command was just used, don't scroll, only reset
1865 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 if (did_syncbind)
1867 did_syncbind = FALSE;
1868 else if (curwin == old_curwin)
1869 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001870 // Synchronize other windows, as necessary according to
1871 // 'scrollbind'. Don't do this after an ":edit" command, except
1872 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 if ((curwin->w_buffer == old_buf
1874#ifdef FEAT_DIFF
1875 || curwin->w_p_diff
1876#endif
1877 )
1878 && (curwin->w_topline != old_topline
1879#ifdef FEAT_DIFF
1880 || curwin->w_topfill != old_topfill
1881#endif
1882 || curwin->w_leftcol != old_leftcol))
1883 {
1884 check_scrollbind(curwin->w_topline - old_topline,
1885 (long)(curwin->w_leftcol - old_leftcol));
1886 }
1887 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001888 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001890 // When switching between windows, make sure that the relative
1891 // vertical offset is valid for the new window. The relative
1892 // offset is invalid whenever another 'scrollbind' window has
1893 // scrolled to a point that would force the current window to
1894 // scroll past the beginning or end of its buffer. When the
1895 // resync is performed, some of the other 'scrollbind' windows may
1896 // need to jump so that the current window's relative position is
1897 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
1899 }
1900 curwin->w_scbind_pos = curwin->w_topline;
1901 }
1902
1903 old_curwin = curwin;
1904 old_topline = curwin->w_topline;
1905#ifdef FEAT_DIFF
1906 old_topfill = curwin->w_topfill;
1907#endif
1908 old_buf = curwin->w_buffer;
1909 old_leftcol = curwin->w_leftcol;
1910}
1911
1912/*
1913 * Synchronize any windows that have "scrollbind" set, based on the
1914 * number of rows by which the current window has changed
1915 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
1916 */
1917 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001918check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919{
1920 int want_ver;
1921 int want_hor;
1922 win_T *old_curwin = curwin;
1923 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 int old_VIsual_select = VIsual_select;
1925 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 colnr_T tgt_leftcol = curwin->w_leftcol;
1927 long topline;
1928 long y;
1929
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001930 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
1932#ifdef FEAT_DIFF
1933 want_ver |= old_curwin->w_p_diff;
1934#endif
1935 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
1936
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001937 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001939 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 {
1941 curbuf = curwin->w_buffer;
zeertzjq101d57b2022-07-31 18:34:32 +01001942 // skip original window and windows with 'noscrollbind'
1943 if (curwin == old_curwin || !curwin->w_p_scb)
1944 continue;
1945
1946 // do the vertical scroll
1947 if (want_ver)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949#ifdef FEAT_DIFF
zeertzjq101d57b2022-07-31 18:34:32 +01001950 if (old_curwin->w_p_diff && curwin->w_p_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 {
zeertzjq101d57b2022-07-31 18:34:32 +01001952 diff_set_topline(old_curwin, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 }
zeertzjq101d57b2022-07-31 18:34:32 +01001954 else
1955#endif
1956 {
1957 curwin->w_scbind_pos += topline_diff;
1958 topline = curwin->w_scbind_pos;
1959 if (topline > curbuf->b_ml.ml_line_count)
1960 topline = curbuf->b_ml.ml_line_count;
1961 if (topline < 1)
1962 topline = 1;
1963
1964 y = topline - curwin->w_topline;
1965 if (y > 0)
1966 scrollup(y, FALSE);
1967 else
1968 scrolldown(-y, FALSE);
1969 }
1970
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001971 redraw_later(UPD_VALID);
zeertzjq101d57b2022-07-31 18:34:32 +01001972 cursor_correct();
1973 curwin->w_redr_status = TRUE;
1974 }
1975
1976 // do the horizontal scroll
Bram Moolenaar0c34d562022-11-18 14:07:20 +00001977 if (want_hor)
1978 (void)set_leftcol(tgt_leftcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 }
1980
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001981 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 VIsual_select = old_VIsual_select;
1983 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 curwin = old_curwin;
1985 curbuf = old_curbuf;
1986}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987
1988/*
1989 * Command character that's ignored.
1990 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02001991 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001994nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001996 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997}
1998
1999/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002000 * Command character that doesn't do anything, but unlike nv_ignore() does
2001 * start edit(). Used for "startinsert" executed while starting up.
2002 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002003 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002004nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002005{
2006}
2007
2008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 * Command character doesn't exist.
2010 */
2011 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002012nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013{
2014 clearopbeep(cap->oap);
2015}
2016
2017/*
2018 * <Help> and <F1> commands.
2019 */
2020 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002021nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022{
2023 if (!checkclearopq(cap->oap))
2024 ex_help(NULL);
2025}
2026
2027/*
2028 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2029 */
2030 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002031nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002033#ifdef FEAT_JOB_CHANNEL
2034 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2035 clearopbeep(cap->oap);
2036 else
2037#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002038 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002039 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002040 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002041 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2042 op_addsub(cap->oap, cap->count1, cap->arg);
2043 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002044 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002045 else if (VIsual_active)
2046 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002047 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002048 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049}
2050
2051/*
2052 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2053 */
2054 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002055nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002057 if (checkclearop(cap->oap))
2058 return;
2059
2060 if (mod_mask & MOD_MASK_CTRL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002061 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002062 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
2063 if (cap->arg == BACKWARD)
2064 goto_tabpage(-(int)cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002065 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002066 goto_tabpage((int)cap->count0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002067 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002068 else
2069 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070}
2071
2072/*
2073 * Implementation of "gd" and "gD" command.
2074 */
2075 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002076nv_gd(
2077 oparg_T *oap,
2078 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002079 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080{
2081 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 char_u *ptr;
2083
Bram Moolenaard9d30582005-05-18 22:10:28 +00002084 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002085 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002086 == FAIL)
2087 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 clearopbeep(oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002089 return;
Bram Moolenaar0c711142021-11-12 10:30:04 +00002090 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002091
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002092#ifdef FEAT_FOLDING
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002093 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2094 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002095#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002096 // clear any search statistics
2097 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2098 clear_cmdline = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002099}
2100
2101/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002102 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2103 * otherwise.
2104 */
2105 static int
2106is_ident(char_u *line, int offset)
2107{
2108 int i;
2109 int incomment = FALSE;
2110 int instring = 0;
2111 int prev = 0;
2112
2113 for (i = 0; i < offset && line[i] != NUL; i++)
2114 {
2115 if (instring != 0)
2116 {
2117 if (prev != '\\' && line[i] == instring)
2118 instring = 0;
2119 }
2120 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2121 {
2122 instring = line[i];
2123 }
2124 else
2125 {
2126 if (incomment)
2127 {
2128 if (prev == '*' && line[i] == '/')
2129 incomment = FALSE;
2130 }
2131 else if (prev == '/' && line[i] == '*')
2132 {
2133 incomment = TRUE;
2134 }
2135 else if (prev == '/' && line[i] == '/')
2136 {
2137 return FALSE;
2138 }
2139 }
2140
2141 prev = line[i];
2142 }
2143
2144 return incomment == FALSE && instring == 0;
2145}
2146
2147/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002148 * Search for variable declaration of "ptr[len]".
2149 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2150 * current file ("gD").
2151 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002152 * Return FAIL when not found.
2153 */
2154 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002155find_decl(
2156 char_u *ptr,
2157 int len,
2158 int locally,
2159 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002160 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002161{
2162 char_u *pat;
2163 pos_T old_pos;
2164 pos_T par_pos;
2165 pos_T found_pos;
2166 int t;
2167 int save_p_ws;
2168 int save_p_scs;
2169 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002170 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002171 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002172 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002173
2174 if ((pat = alloc(len + 7)) == NULL)
2175 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002176
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002177 // Put "\V" before the pattern to avoid that the special meaning of "."
2178 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002179 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2180 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 old_pos = curwin->w_cursor;
2182 save_p_ws = p_ws;
2183 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002184 p_ws = FALSE; // don't wrap around end of file now
2185 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002187 // With "gD" go to line 1.
2188 // With "gd" Search back for the start of the current function, then go
2189 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002190 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002192 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002194 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 }
2196 else
2197 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002198 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2200 --curwin->w_cursor.lnum;
2201 }
2202 curwin->w_cursor.col = 0;
2203
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002204 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002205 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002206 for (;;)
2207 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002208 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002209 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002210 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002211 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002212
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002213 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002214 {
2215 pos_T *pos;
2216
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002217 // Check that the block the match is in doesn't end before the
2218 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002219 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2220 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2221 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002222 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002223 // There can't be a useful match before the end of this block.
2224 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002225 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002226 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002227 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002228 }
2229
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002230 if (t == FAIL)
2231 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002232 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002233 if (found_pos.lnum != 0)
2234 {
2235 curwin->w_cursor = found_pos;
2236 t = OK;
2237 }
2238 break;
2239 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002240 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002241 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002242 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002243 ++curwin->w_cursor.lnum;
2244 curwin->w_cursor.col = 0;
2245 continue;
2246 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002247 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2248
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002249 // If the current position is not a valid identifier and a previous
2250 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002251 if (!valid && found_pos.lnum != 0)
2252 {
2253 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002254 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002255 }
2256
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002257 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002258 if (valid && !locally)
2259 break;
2260 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002261 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002262 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002263 if (found_pos.lnum != 0)
2264 curwin->w_cursor = found_pos;
2265 break;
2266 }
2267
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002268 // For finding a local variable and the match is before the "{" or
2269 // inside a comment, continue searching. For K&R style function
2270 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002271 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002272 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002273 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002274 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002275 // Remove SEARCH_START from flags to avoid getting stuck at one
2276 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002277 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002279
2280 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002282 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 curwin->w_cursor = old_pos;
2284 }
2285 else
2286 {
2287 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002288 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 reset_search_dir();
2290 }
2291
2292 vim_free(pat);
2293 p_ws = save_p_ws;
2294 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002295
2296 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297}
2298
2299/*
2300 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2301 * lines rather than lines in the file.
2302 * 'dist' must be positive.
2303 *
2304 * Return OK if able to move cursor, FAIL otherwise.
2305 */
2306 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002307nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308{
Dylan Thacker-Smithb2d124c2024-03-24 09:43:25 +01002309
2310 int linelen = linetabsize_no_outer(curwin, curwin->w_cursor.lnum);
2311
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 int retval = OK;
2313 int atend = FALSE;
2314 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002315 int col_off1; // margin offset for first screen line
2316 int col_off2; // margin offset for wrapped screen line
2317 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002318 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319
2320 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002321 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322
2323 col_off1 = curwin_col_off();
2324 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002325 width1 = curwin->w_width - col_off1;
2326 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002327 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002328 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002331 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002332 // Instead of sticking at the last character of the buffer line we
2333 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 if (curwin->w_curswant == MAXCOL)
2335 {
2336 atend = TRUE;
2337 validate_virtcol();
2338 if (width1 <= 0)
2339 curwin->w_curswant = 0;
2340 else
2341 {
2342 curwin->w_curswant = width1 - 1;
2343 if (curwin->w_virtcol > curwin->w_curswant)
2344 curwin->w_curswant += ((curwin->w_virtcol
2345 - curwin->w_curswant - 1) / width2 + 1) * width2;
2346 }
2347 }
2348 else
2349 {
2350 if (linelen > width1)
2351 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2352 else
2353 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002354 if (curwin->w_curswant >= (colnr_T)n)
2355 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 }
2357
2358 while (dist--)
2359 {
2360 if (dir == BACKWARD)
2361 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002362 if ((long)curwin->w_curswant >= width1
2363#ifdef FEAT_FOLDING
2364 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2365#endif
2366 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002367 // Move back within the line. This can give a negative value
2368 // for w_curswant if width1 < width2 (with cpoptions+=n),
2369 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 curwin->w_curswant -= width2;
2371 else
2372 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002373 // to previous line
Luuk van Baala109f392023-06-02 14:16:35 +01002374 if (curwin->w_cursor.lnum <= 1)
Bram Moolenaare71996b2021-01-21 17:03:07 +01002375 {
2376 retval = FAIL;
2377 break;
2378 }
Luuk van Baala109f392023-06-02 14:16:35 +01002379 cursor_up_inner(curwin, 1);
2380
Dylan Thacker-Smithb2d124c2024-03-24 09:43:25 +01002381 linelen = linetabsize_no_outer(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 if (linelen > width1)
2383 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2384 + 1) * width2;
2385 }
2386 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002387 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 {
2389 if (linelen > width1)
2390 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2391 else
2392 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002393 if (curwin->w_curswant + width2 < (colnr_T)n
2394#ifdef FEAT_FOLDING
2395 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2396#endif
2397 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002398 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 curwin->w_curswant += width2;
2400 else
2401 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002402 // to next line
Luuk van Baala109f392023-06-02 14:16:35 +01002403 if (curwin->w_cursor.lnum
2404 >= curwin->w_buffer->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 {
2406 retval = FAIL;
2407 break;
2408 }
Luuk van Baala109f392023-06-02 14:16:35 +01002409 cursor_down_inner(curwin, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 curwin->w_curswant %= width2;
Luuk van Baala109f392023-06-02 14:16:35 +01002411
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002412 // Check if the cursor has moved below the number display
2413 // when width1 < width2 (with cpoptions+=n). Subtract width2
2414 // to get a negative value for w_curswant, which will get
2415 // clipped to column 0.
2416 if (curwin->w_curswant >= width1)
2417 curwin->w_curswant -= width2;
Dylan Thacker-Smithb2d124c2024-03-24 09:43:25 +01002418 linelen = linetabsize_no_outer(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 }
2420 }
2421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002424 if (virtual_active() && atend)
2425 coladvance(MAXCOL);
2426 else
2427 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2430 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002431 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002432 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002433
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002434 // Check for landing on a character that got split at the end of the
2435 // last line. We want to advance a screenline, not end up in the same
2436 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002438 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002439#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002440 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2441 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002442#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002443
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002444 c = (*mb_ptr2char)(ml_get_cursor());
2445 if (dir == FORWARD && virtcol < curwin->w_curswant
2446 && (curwin->w_curswant <= (colnr_T)width1)
2447 && !vim_isprintc(c) && c > 255)
2448 oneright();
2449
Bram Moolenaar773b1582014-08-29 14:20:51 +02002450 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 && (curwin->w_curswant < (colnr_T)width1
2452 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2453 : ((curwin->w_curswant - width1) % width2
2454 > (colnr_T)width2 / 2)))
2455 --curwin->w_cursor.col;
2456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457
2458 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002459 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002460 adjust_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
2462 return retval;
2463}
2464
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465/*
2466 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2467 * cap->arg must be TRUE for CTRL-E.
2468 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002469 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002470nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471{
2472 if (!checkclearop(cap->oap))
2473 scroll_redraw(cap->arg, cap->count1);
2474}
2475
2476/*
2477 * Scroll "count" lines up or down, and redraw.
2478 */
2479 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002480scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481{
2482 linenr_T prev_topline = curwin->w_topline;
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002483 int prev_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484#ifdef FEAT_DIFF
2485 int prev_topfill = curwin->w_topfill;
2486#endif
2487 linenr_T prev_lnum = curwin->w_cursor.lnum;
2488
2489 if (up)
2490 scrollup(count, TRUE);
2491 else
2492 scrolldown(count, TRUE);
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002493 if (get_scrolloff_value() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002495 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2496 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 cursor_correct();
2498 check_cursor_moved(curwin);
2499 curwin->w_valid |= VALID_TOPLINE;
2500
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002501 // If moved back to where we were, at least move the cursor, otherwise
2502 // we get stuck at one position. Don't move the cursor up if the
2503 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 while (curwin->w_topline == prev_topline
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002505 && curwin->w_skipcol == prev_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506#ifdef FEAT_DIFF
2507 && curwin->w_topfill == prev_topfill
2508#endif
2509 )
2510 {
2511 if (up)
2512 {
2513 if (curwin->w_cursor.lnum > prev_lnum
2514 || cursor_down(1L, FALSE) == FAIL)
2515 break;
2516 }
2517 else
2518 {
2519 if (curwin->w_cursor.lnum < prev_lnum
2520 || prev_topline == 1L
2521 || cursor_up(1L, FALSE) == FAIL)
2522 break;
2523 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002524 // Mark w_topline as valid, otherwise the screen jumps back at the
2525 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 check_cursor_moved(curwin);
2527 curwin->w_valid |= VALID_TOPLINE;
2528 }
2529 }
2530 if (curwin->w_cursor.lnum != prev_lnum)
2531 coladvance(curwin->w_curswant);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002532 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533}
2534
2535/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002536 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2537 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2538 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002539 */
2540 static int
2541nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2542{
2543 int nchar = *nchar_arg;
2544 long n;
2545
2546 // "z123{nchar}": edit the count before obtaining {nchar}
2547 if (checkclearop(cap->oap))
2548 return FALSE;
2549 n = nchar - '0';
2550
2551 for (;;)
2552 {
2553#ifdef USE_ON_FLY_SCROLL
2554 dont_scroll = TRUE; // disallow scrolling here
2555#endif
2556 ++no_mapping;
2557 ++allow_keys; // no mapping for nchar, but allow key codes
2558 nchar = plain_vgetc();
2559 LANGMAP_ADJUST(nchar, TRUE);
2560 --no_mapping;
2561 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002562 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002563
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002564 if (nchar == K_DEL || nchar == K_KDEL)
2565 n /= 10;
2566 else if (VIM_ISDIGIT(nchar))
Christian Brabandt58f9bef2023-11-14 21:02:30 +01002567 {
Christian Brabandt22cbc8a2023-11-19 10:47:21 +01002568 if (vim_append_digit_long(&n, nchar - '0') == FAIL)
Christian Brabandt58f9bef2023-11-14 21:02:30 +01002569 {
2570 clearopbeep(cap->oap);
2571 break;
2572 }
Christian Brabandt58f9bef2023-11-14 21:02:30 +01002573 }
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002574 else if (nchar == CAR)
2575 {
2576#ifdef FEAT_GUI
2577 need_mouse_correct = TRUE;
2578#endif
2579 win_setheight((int)n);
2580 break;
2581 }
2582 else if (nchar == 'l'
2583 || nchar == 'h'
2584 || nchar == K_LEFT
2585 || nchar == K_RIGHT)
2586 {
2587 cap->count1 = n ? n * cap->count1 : cap->count1;
2588 *nchar_arg = nchar;
2589 return TRUE;
2590 }
2591 else
2592 {
2593 clearopbeep(cap->oap);
2594 break;
2595 }
2596 }
2597 cap->oap->op_type = OP_NOP;
2598 return FALSE;
2599}
2600
2601#ifdef FEAT_SPELL
2602/*
2603 * "zug" and "zuw": undo "zg" and "zw"
2604 * "zg": add good word to word list
2605 * "zw": add wrong word to word list
2606 * "zG": add good word to temp word list
2607 * "zW": add wrong word to temp word list
2608 */
2609 static int
2610nv_zg_zw(cmdarg_T *cap, int nchar)
2611{
2612 char_u *ptr = NULL;
2613 int len;
2614 int undo = FALSE;
2615
2616 if (nchar == 'u')
2617 {
2618 ++no_mapping;
2619 ++allow_keys; // no mapping for nchar, but allow key codes
2620 nchar = plain_vgetc();
2621 LANGMAP_ADJUST(nchar, TRUE);
2622 --no_mapping;
2623 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002624 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002625
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002626 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2627 {
2628 clearopbeep(cap->oap);
2629 return OK;
2630 }
2631 undo = TRUE;
2632 }
2633
2634 if (checkclearop(cap->oap))
2635 return OK;
2636 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2637 return FAIL;
2638 if (ptr == NULL)
2639 {
2640 pos_T pos = curwin->w_cursor;
2641
2642 // Find bad word under the cursor. When 'spell' is
2643 // off this fails and find_ident_under_cursor() is
2644 // used below.
2645 emsg_off++;
2646 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2647 emsg_off--;
2648 if (len != 0 && curwin->w_cursor.col <= pos.col)
2649 ptr = ml_get_pos(&curwin->w_cursor);
2650 curwin->w_cursor = pos;
2651 }
2652
2653 if (ptr == NULL
2654 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2655 return FAIL;
2656 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2657 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2658 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2659
2660 return OK;
2661}
2662#endif
2663
2664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 * Commands that start with "z".
2666 */
2667 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002668nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669{
2670 long n;
2671 colnr_T col;
2672 int nchar = cap->nchar;
2673#ifdef FEAT_FOLDING
2674 long old_fdl = curwin->w_p_fdl;
2675 int old_fen = curwin->w_p_fen;
2676#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002677 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002679 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 if (
2683#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002684 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2685 // and "zC" only in Visual mode. "zj" and "zk" are motion
2686 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 cap->nchar != 'f' && cap->nchar != 'F'
2688 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2689 && cap->nchar != 'j' && cap->nchar != 'k'
2690 &&
2691#endif
2692 checkclearop(cap->oap))
2693 return;
2694
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002695 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2696 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2698 && cap->count0
2699 && cap->count0 != curwin->w_cursor.lnum)
2700 {
2701 setpcmark();
2702 if (cap->count0 > curbuf->b_ml.ml_line_count)
2703 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2704 else
2705 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002706 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 }
2708
2709 switch (nchar)
2710 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002711 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 case '+':
2713 if (cap->count0 == 0)
2714 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002715 // No count given: put cursor at the line below screen
2716 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2718 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2719 else
2720 curwin->w_cursor.lnum = curwin->w_botline;
2721 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002722 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 case NL:
2724 case CAR:
2725 case K_KENTER:
2726 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002727 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
2729 case 't': scroll_cursor_top(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002730 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002731 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 break;
2733
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002734 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002736 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002738 case 'z': scroll_cursor_halfway(TRUE, FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002739 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002740 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 break;
2742
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002743 // "z^", "z-" and "zb": put cursor at bottom of screen
2744 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2745 // when <count> is at bottom of window, and puts that one at
2746 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 if (cap->count0 != 0)
2748 {
2749 scroll_cursor_bot(0, TRUE);
2750 curwin->w_cursor.lnum = curwin->w_topline;
2751 }
2752 else if (curwin->w_topline == 1)
2753 curwin->w_cursor.lnum = 1;
2754 else
2755 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002756 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 case '-':
2758 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002759 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
2761 case 'b': scroll_cursor_bot(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002762 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002763 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 break;
2765
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002766 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002768 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002769 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002771 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 case 'h':
2773 case K_LEFT:
2774 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002775 (void)set_leftcol((colnr_T)cap->count1 > curwin->w_leftcol
2776 ? 0 : curwin->w_leftcol - (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 break;
2778
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002779 // "zL" - scroll window left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002780 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002781 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002783 // "zl" - scroll window to the left if not wrapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 case 'l':
2785 case K_RIGHT:
2786 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002787 (void)set_leftcol(curwin->w_leftcol + (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 break;
2789
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002790 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 case 's': if (!curwin->w_p_wrap)
2792 {
2793#ifdef FEAT_FOLDING
2794 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002795 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 else
2797#endif
2798 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002799 if ((long)col > siso)
2800 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 else
2802 col = 0;
2803 if (curwin->w_leftcol != col)
2804 {
2805 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002806 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 }
2808 }
2809 break;
2810
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002811 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 case 'e': if (!curwin->w_p_wrap)
2813 {
2814#ifdef FEAT_FOLDING
2815 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002816 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 else
2818#endif
2819 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002820 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002821 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 col = 0;
2823 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002824 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 if (curwin->w_leftcol != col)
2826 {
2827 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002828 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 }
2830 }
2831 break;
2832
Christian Brabandt2fa93842021-05-30 22:17:25 +02002833 // "zp", "zP" in block mode put without addind trailing spaces
2834 case 'P':
2835 case 'p': nv_put(cap);
2836 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002837 // "zy" Yank without trailing spaces
2838 case 'y': nv_operator(cap);
2839 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002841 // "zF": create fold command
2842 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 case 'F':
2844 case 'f': if (foldManualAllowed(TRUE))
2845 {
2846 cap->nchar = 'f';
2847 nv_operator(cap);
2848 curwin->w_p_fen = TRUE;
2849
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002850 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2852 {
2853 nv_operator(cap);
2854 finish_op = TRUE;
2855 }
2856 }
2857 else
2858 clearopbeep(cap->oap);
2859 break;
2860
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002861 // "zd": delete fold at cursor
2862 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 case 'd':
2864 case 'D': if (foldManualAllowed(FALSE))
2865 {
2866 if (VIsual_active)
2867 nv_operator(cap);
2868 else
2869 deleteFold(curwin->w_cursor.lnum,
2870 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2871 }
2872 break;
2873
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002874 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 case 'E': if (foldmethodIsManual(curwin))
2876 {
2877 clearFolding(curwin);
2878 changed_window_setting();
2879 }
2880 else if (foldmethodIsMarker(curwin))
2881 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2882 TRUE, FALSE);
2883 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002884 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 break;
2886
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002887 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 case 'n': curwin->w_p_fen = FALSE;
2889 break;
2890
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002891 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 case 'N': curwin->w_p_fen = TRUE;
2893 break;
2894
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002895 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2897 break;
2898
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002899 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2901 openFold(curwin->w_cursor.lnum, cap->count1);
2902 else
2903 {
2904 closeFold(curwin->w_cursor.lnum, cap->count1);
2905 curwin->w_p_fen = TRUE;
2906 }
2907 break;
2908
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002909 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2911 openFoldRecurse(curwin->w_cursor.lnum);
2912 else
2913 {
2914 closeFoldRecurse(curwin->w_cursor.lnum);
2915 curwin->w_p_fen = TRUE;
2916 }
2917 break;
2918
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002919 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 case 'o': if (VIsual_active)
2921 nv_operator(cap);
2922 else
2923 openFold(curwin->w_cursor.lnum, cap->count1);
2924 break;
2925
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002926 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 case 'O': if (VIsual_active)
2928 nv_operator(cap);
2929 else
2930 openFoldRecurse(curwin->w_cursor.lnum);
2931 break;
2932
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002933 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 case 'c': if (VIsual_active)
2935 nv_operator(cap);
2936 else
2937 closeFold(curwin->w_cursor.lnum, cap->count1);
2938 curwin->w_p_fen = TRUE;
2939 break;
2940
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002941 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 case 'C': if (VIsual_active)
2943 nv_operator(cap);
2944 else
2945 closeFoldRecurse(curwin->w_cursor.lnum);
2946 curwin->w_p_fen = TRUE;
2947 break;
2948
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002949 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 case 'v': foldOpenCursor();
2951 break;
2952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002953 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002955 curwin->w_foldinvalid = TRUE; // recompute folds
2956 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 foldOpenCursor();
2958 break;
2959
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002960 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002962 curwin->w_foldinvalid = TRUE; // recompute folds
2963 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 break;
2965
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002966 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002968 {
2969 curwin->w_p_fdl -= cap->count1;
2970 if (curwin->w_p_fdl < 0)
2971 curwin->w_p_fdl = 0;
2972 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002973 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 curwin->w_p_fen = TRUE;
2975 break;
2976
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002977 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002979 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 curwin->w_p_fen = TRUE;
2981 break;
2982
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002983 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002984 case 'r': curwin->w_p_fdl += cap->count1;
2985 {
2986 int d = getDeepestNesting();
2987
2988 if (curwin->w_p_fdl >= d)
2989 curwin->w_p_fdl = d;
2990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 break;
2992
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002993 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002995 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 break;
2997
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002998 case 'j': // "zj" move to next fold downwards
2999 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
3001 cap->count1) == FAIL)
3002 clearopbeep(cap->oap);
3003 break;
3004
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003005#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003007#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003008 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003009 case 'g': // "zg": add good word to word list
3010 case 'w': // "zw": add wrong word to word list
3011 case 'G': // "zG": add good word to temp word list
3012 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003013 if (nv_zg_zw(cap, nchar) == FAIL)
3014 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003015 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003016
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003017 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003018 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003019 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003020 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003021#endif
3022
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 default: clearopbeep(cap->oap);
3024 }
3025
3026#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003027 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 if (old_fen != curwin->w_p_fen)
3029 {
3030# ifdef FEAT_DIFF
3031 win_T *wp;
3032
3033 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3034 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003035 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 FOR_ALL_WINDOWS(wp)
3037 {
3038 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3039 {
3040 wp->w_p_fen = curwin->w_p_fen;
3041 changed_window_setting_win(wp);
3042 }
3043 }
3044 }
3045# endif
3046 changed_window_setting();
3047 }
3048
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003049 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 if (old_fdl != curwin->w_p_fdl)
3051 newFoldLevel();
3052#endif
3053}
3054
3055#ifdef FEAT_GUI
3056/*
3057 * Vertical scrollbar movement.
3058 */
3059 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003060nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061{
3062 if (cap->oap->op_type != OP_NOP)
3063 clearopbeep(cap->oap);
3064
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003065 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 gui_do_scroll();
3067}
3068
3069/*
3070 * Horizontal scrollbar movement.
3071 */
3072 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003073nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074{
3075 if (cap->oap->op_type != OP_NOP)
3076 clearopbeep(cap->oap);
3077
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003078 // Even if an operator was pending, we still want to scroll
Christopher Plewright44c22092022-11-15 17:43:36 +00003079 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080}
3081#endif
3082
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003083#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003084/*
3085 * Click in GUI tab.
3086 */
3087 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003088nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003089{
3090 if (cap->oap->op_type != OP_NOP)
3091 clearopbeep(cap->oap);
3092
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003093 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003094 goto_tabpage(current_tab);
3095}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003096
3097/*
3098 * Selected item in tab line menu.
3099 */
3100 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003101nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003102{
3103 if (cap->oap->op_type != OP_NOP)
3104 clearopbeep(cap->oap);
3105
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003106 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003107 handle_tabmenu();
3108}
3109
3110/*
3111 * Handle selecting an item of the GUI tab line menu.
3112 * Used in Normal and Insert mode.
3113 */
3114 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003115handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003116{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003117 switch (current_tabmenu)
3118 {
3119 case TABLINE_MENU_CLOSE:
3120 if (current_tab == 0)
3121 do_cmdline_cmd((char_u *)"tabclose");
3122 else
3123 {
3124 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3125 current_tab);
3126 do_cmdline_cmd(IObuff);
3127 }
3128 break;
3129
3130 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003131 if (current_tab == 0)
3132 do_cmdline_cmd((char_u *)"$tabnew");
3133 else
3134 {
3135 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3136 current_tab - 1);
3137 do_cmdline_cmd(IObuff);
3138 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003139 break;
3140
3141 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003142 if (current_tab == 0)
3143 do_cmdline_cmd((char_u *)"browse $tabnew");
3144 else
3145 {
3146 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3147 current_tab - 1);
3148 do_cmdline_cmd(IObuff);
3149 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003150 break;
3151 }
3152}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003153#endif
3154
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155/*
3156 * "Q" command.
3157 */
3158 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003159nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003161 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003163 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003164 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 do_exmode(FALSE);
3166}
3167
3168/*
3169 * Handle a ":" command.
3170 */
3171 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003172nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003174 int old_p_im;
3175 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003176 int is_cmdkey = cap->cmdchar == K_COMMAND
3177 || cap->cmdchar == K_SCRIPT_COMMAND;
3178 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179
Bram Moolenaar957cf672020-11-12 14:21:06 +01003180 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003182 nv_operator(cap);
3183 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003185
3186 if (cap->oap->op_type != OP_NOP)
3187 {
3188 // Using ":" as a movement is characterwise exclusive.
3189 cap->oap->motion_type = MCHAR;
3190 cap->oap->inclusive = FALSE;
3191 }
3192 else if (cap->count0 && !is_cmdkey)
3193 {
3194 // translate "count:" into ":.,.+(count - 1)"
3195 stuffcharReadbuff('.');
3196 if (cap->count0 > 1)
3197 {
3198 stuffReadbuff((char_u *)",.+");
3199 stuffnumReadbuff((long)cap->count0 - 1L);
3200 }
3201 }
3202
3203 // When typing, don't type below an old message
3204 if (KeyTyped)
3205 compute_cmdrow();
3206
3207 old_p_im = p_im;
3208
3209 // get a command line and execute it
3210 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3211 if (is_cmdkey)
3212 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3213 else
3214 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
3215
3216 // If 'insertmode' changed, enter or exit Insert mode
3217 if (p_im != old_p_im)
3218 {
3219 if (p_im)
3220 restart_edit = 'i';
3221 else
3222 restart_edit = 0;
3223 }
3224
3225 if (cmd_result == FAIL)
3226 // The Ex command failed, do not execute the operator.
3227 clearop(cap->oap);
3228 else if (cap->oap->op_type != OP_NOP
3229 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
John Marriott02d7a6c2024-02-26 21:21:17 +01003230 || cap->oap->start.col > ml_get_len(cap->oap->start.lnum)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003231 || did_emsg
3232 ))
3233 // The start of the operator has become invalid by the Ex command.
3234 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235}
3236
3237/*
3238 * Handle CTRL-G command.
3239 */
3240 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003241nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003243 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 {
3245 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003246 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 showmode();
3248 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003249 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003250 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 fileinfo((int)cap->count0, FALSE, TRUE);
3252}
3253
3254/*
3255 * Handle CTRL-H <Backspace> command.
3256 */
3257 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003258nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 if (VIsual_active && VIsual_select)
3261 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003262 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 v_visop(cap);
3264 }
3265 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 nv_left(cap);
3267}
3268
3269/*
3270 * CTRL-L: clear screen and redraw.
3271 */
3272 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003273nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003275 if (checkclearop(cap->oap))
3276 return;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003277
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003278#ifdef FEAT_SYN_HL
3279 // Clear all syntax states to force resyncing.
3280 syn_stack_free_all(curwin->w_s);
3281# ifdef FEAT_RELTIME
3282 {
3283 win_T *wp;
3284
3285 FOR_ALL_WINDOWS(wp)
3286 wp->w_s->b_syn_slow = FALSE;
3287 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003288# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003290 redraw_later(UPD_CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003291#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3292# ifdef VIMDLL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003293 if (!gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003294# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003295 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297}
3298
3299/*
3300 * CTRL-O: In Select mode: switch to Visual mode for one command.
3301 * Otherwise: Go to older pcmark.
3302 */
3303 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003304nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 if (VIsual_active && VIsual_select)
3307 {
3308 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003309 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003311 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 }
3313 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 {
3315 cap->count1 = -cap->count1;
3316 nv_pcmark(cap);
3317 }
3318}
3319
3320/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003321 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3322 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 */
3324 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003325nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326{
3327 if (!checkclearopq(cap->oap))
3328 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3329 GETF_SETMARK|GETF_ALT, FALSE);
3330}
3331
3332/*
3333 * "Z" commands.
3334 */
3335 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003336nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003338 if (checkclearopq(cap->oap))
3339 return;
3340
3341 switch (cap->nchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003343 // "ZZ": equivalent to ":x".
3344 case 'Z': do_cmdline_cmd((char_u *)"x");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 break;
3346
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003347 // "ZQ": equivalent to ":q!" (Elvis compatible).
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003348 case 'Q': do_cmdline_cmd((char_u *)"q!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 break;
3350
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003351 default: clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 }
3353}
3354
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355/*
3356 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3357 */
3358 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003359do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360{
3361 oparg_T oa;
3362 cmdarg_T ca;
3363
3364 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003365 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 ca.oap = &oa;
3367 ca.cmdchar = c1;
3368 ca.nchar = c2;
3369 nv_ident(&ca);
3370}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371
3372/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003373 * 'K' normal-mode command. Get the command to lookup the keyword under the
3374 * cursor.
3375 */
3376 static int
3377nv_K_getcmd(
3378 cmdarg_T *cap,
3379 char_u *kp,
3380 int kp_help,
3381 int kp_ex,
3382 char_u **ptr_arg,
3383 int n,
3384 char_u *buf,
3385 unsigned buflen)
3386{
3387 char_u *ptr = *ptr_arg;
3388 int isman;
3389 int isman_s;
3390
3391 if (kp_help)
3392 {
3393 // in the help buffer
3394 STRCPY(buf, "he! ");
3395 return n;
3396 }
3397
3398 if (kp_ex)
3399 {
3400 // 'keywordprog' is an ex command
3401 if (cap->count0 != 0)
3402 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3403 else
3404 STRCPY(buf, kp);
3405 STRCAT(buf, " ");
3406 return n;
3407 }
3408
3409 // An external command will probably use an argument starting
3410 // with "-" as an option. To avoid trouble we skip the "-".
3411 while (*ptr == '-' && n > 0)
3412 {
3413 ++ptr;
3414 --n;
3415 }
3416 if (n == 0)
3417 {
3418 // found dashes only
3419 emsg(_(e_no_identifier_under_cursor));
3420 vim_free(buf);
3421 *ptr_arg = ptr;
3422 return 0;
3423 }
3424
3425 // When a count is given, turn it into a range. Is this
3426 // really what we want?
3427 isman = (STRCMP(kp, "man") == 0);
3428 isman_s = (STRCMP(kp, "man -s") == 0);
3429 if (cap->count0 != 0 && !(isman || isman_s))
3430 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3431
3432 STRCAT(buf, "! ");
3433 if (cap->count0 == 0 && isman_s)
3434 STRCAT(buf, "man");
3435 else
3436 STRCAT(buf, kp);
3437 STRCAT(buf, " ");
3438 if (cap->count0 != 0 && (isman || isman_s))
3439 {
3440 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3441 STRCAT(buf, " ");
3442 }
3443
3444 *ptr_arg = ptr;
3445 return n;
3446}
3447
3448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 * Handle the commands that use the word under the cursor.
3450 * [g] CTRL-] :ta to current identifier
3451 * [g] 'K' run program for current identifier
3452 * [g] '*' / to current identifier or string
3453 * [g] '#' ? to current identifier or string
3454 * g ']' :tselect for current identifier
3455 */
3456 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003457nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458{
3459 char_u *ptr = NULL;
3460 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003461 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003462 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003464 char_u *kp; // value of 'keywordprg'
3465 int kp_help; // 'keywordprg' is ":he"
3466 int kp_ex; // 'keywordprg' starts with ":"
3467 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003469 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003470 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003473 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 {
3475 cmdchar = cap->nchar;
3476 g_cmd = TRUE;
3477 }
3478 else
3479 {
3480 cmdchar = cap->cmdchar;
3481 g_cmd = FALSE;
3482 }
3483
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003484 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 cmdchar = '#';
3486
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003487 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3489 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3491 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 if (checkclearopq(cap->oap))
3493 return;
3494 }
3495
3496 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3497 (cmdchar == '*' || cmdchar == '#')
3498 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3499 {
3500 clearop(cap->oap);
3501 return;
3502 }
3503
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003504 // Allocate buffer to put the command in. Inserting backslashes can
3505 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3506 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3508 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3509 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003510 if (kp_help && *skipwhite(ptr) == NUL)
3511 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003512 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003513 return;
3514 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003515 kp_ex = (*kp == ':');
3516 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3517 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 if (buf == NULL)
3519 return;
3520 buf[0] = NUL;
3521
3522 switch (cmdchar)
3523 {
3524 case '*':
3525 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003526 // Put cursor at start of word, makes search skip the word
3527 // under the cursor.
3528 // Call setpcmark() first, so "*``" puts the cursor back where
3529 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 setpcmark();
3531 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3532
3533 if (!g_cmd && vim_iswordp(ptr))
3534 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003535 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 break;
3537
3538 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003539 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3540 if (n == 0)
3541 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 break;
3543
3544 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003545 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546#ifdef FEAT_CSCOPE
3547 if (p_cst)
3548 STRCPY(buf, "cstag ");
3549 else
3550#endif
3551 STRCPY(buf, "ts ");
3552 break;
3553
3554 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003555 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 if (curbuf->b_help)
3557 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003559 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003560 if (g_cmd)
3561 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003562 else if (cap->count0 == 0)
3563 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003564 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003565 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 }
3568
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003569 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003570 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003572 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003573 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003574 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003575 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003576 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003577 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003578 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003579 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003580 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003582 vim_free(buf);
3583 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003585 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003586 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003587 {
3588 vim_free(buf);
3589 vim_free(p);
3590 return;
3591 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003592 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003593 STRCAT(buf, p);
3594 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003596 else
3597 {
3598 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003599 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003600 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003601 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003602 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003603 {
3604 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003605 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003606 aux_ptr = (char_u *)"";
3607 else
3608 aux_ptr = (char_u *)"\\|\"\n[";
3609 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003610 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003611 aux_ptr = (char_u *)"\\|\"\n*?[";
3612
3613 p = buf + STRLEN(buf);
3614 while (n-- > 0)
3615 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003616 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003617 if (vim_strchr(aux_ptr, *ptr) != NULL)
3618 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003619 // When current byte is a part of multibyte character, copy all
3620 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003621 if (has_mbyte)
3622 {
3623 int i;
3624 int len = (*mb_ptr2len)(ptr) - 1;
3625
3626 for (i = 0; i < len && n >= 1; ++i, --n)
3627 *p++ = *ptr++;
3628 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003629 *p++ = *ptr++;
3630 }
3631 *p = NUL;
3632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003634 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 if (cmdchar == '*' || cmdchar == '#')
3636 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003637 if (!g_cmd && (has_mbyte
3638 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3639 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003641
3642 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003643 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003645
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003646 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 }
3648 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003649 {
3650 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003652 g_tag_at_cursor = FALSE;
3653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654
3655 vim_free(buf);
3656}
3657
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658/*
3659 * Get visually selected text, within one line only.
3660 * Returns FAIL if more than one line selected.
3661 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003662 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003663get_visual_text(
3664 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003665 char_u **pp, // return: start of selected text
3666 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667{
3668 if (VIsual_mode != 'V')
3669 unadjust_for_sel();
3670 if (VIsual.lnum != curwin->w_cursor.lnum)
3671 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003672 if (cap != NULL)
3673 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 return FAIL;
3675 }
3676 if (VIsual_mode == 'V')
3677 {
3678 *pp = ml_get_curline();
John Marriott02d7a6c2024-02-26 21:21:17 +01003679 *lenp = (int)ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
3681 else
3682 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003683 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 {
3685 *pp = ml_get_pos(&curwin->w_cursor);
3686 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3687 }
3688 else
3689 {
3690 *pp = ml_get_pos(&VIsual);
3691 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3692 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003693 if (**pp == NUL)
3694 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003695 if (*lenp > 0)
3696 {
3697 if (has_mbyte)
3698 // Correct the length to include all bytes of the last
3699 // character.
3700 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3701 else if ((*pp)[*lenp - 1] == NUL)
3702 // Do not include a trailing NUL.
3703 *lenp -= 1;
3704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 }
3706 reset_VIsual_and_resel();
3707 return OK;
3708}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709
3710/*
3711 * CTRL-T: backwards in tag stack
3712 */
3713 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003714nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715{
3716 if (!checkclearopq(cap->oap))
3717 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3718}
3719
3720/*
3721 * Handle scrolling command 'H', 'L' and 'M'.
3722 */
3723 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003724nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725{
3726 int used = 0;
3727 long n;
3728#ifdef FEAT_FOLDING
3729 linenr_T lnum;
3730#endif
3731 int half;
3732
3733 cap->oap->motion_type = MLINE;
3734 setpcmark();
3735
3736 if (cap->cmdchar == 'L')
3737 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003738 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 curwin->w_cursor.lnum = curwin->w_botline - 1;
3740 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3741 curwin->w_cursor.lnum = 1;
3742 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003743 {
3744#ifdef FEAT_FOLDING
3745 if (hasAnyFolding(curwin))
3746 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003747 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003748 for (n = cap->count1 - 1; n > 0
3749 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3750 {
3751 (void)hasFolding(curwin->w_cursor.lnum,
3752 &curwin->w_cursor.lnum, NULL);
Bram Moolenaar232bdaa2023-01-13 14:17:58 +00003753 if (curwin->w_cursor.lnum > curwin->w_topline)
3754 --curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003755 }
3756 }
3757 else
3758#endif
3759 curwin->w_cursor.lnum -= cap->count1 - 1;
3760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 }
3762 else
3763 {
3764 if (cap->cmdchar == 'M')
3765 {
3766#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003767 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 used -= diff_check_fill(curwin, curwin->w_topline)
3769 - curwin->w_topfill;
3770#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003771 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3773 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3774 {
3775#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003776 // Count half he number of filler lines to be "below this
3777 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3779 + n) / 2 >= half)
3780 {
3781 --n;
3782 break;
3783 }
3784#endif
3785 used += plines(curwin->w_topline + n);
3786 if (used >= half)
3787 break;
3788#ifdef FEAT_FOLDING
3789 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3790 n = lnum - curwin->w_topline;
3791#endif
3792 }
3793 if (n > 0 && used > curwin->w_height)
3794 --n;
3795 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003796 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003797 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003799#ifdef FEAT_FOLDING
3800 if (hasAnyFolding(curwin))
3801 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003802 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003803 lnum = curwin->w_topline;
3804 while (n-- > 0 && lnum < curwin->w_botline - 1)
3805 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003806 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003807 ++lnum;
3808 }
3809 n = lnum - curwin->w_topline;
3810 }
3811#endif
3812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 curwin->w_cursor.lnum = curwin->w_topline + n;
3814 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3815 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3816 }
3817
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003818 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003819 if (cap->oap->op_type == OP_NOP)
3820 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 beginline(BL_SOL | BL_FIX);
3822}
3823
3824/*
3825 * Cursor right commands.
3826 */
3827 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003828nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829{
3830 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003831 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003833 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3834 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003835 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003836 if (mod_mask & MOD_MASK_CTRL)
3837 cap->arg = TRUE;
3838 nv_wordcmd(cap);
3839 return;
3840 }
3841
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 cap->oap->motion_type = MCHAR;
3843 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003844 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003846 // In virtual edit mode, there's no such thing as "past_line", as lines
3847 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003849 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850
3851 for (n = cap->count1; n > 0; --n)
3852 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003853 if ((!past_line && oneright() == FAIL)
3854 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003855 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003857 // <Space> wraps to next line if 'whichwrap' has 's'.
3858 // 'l' wraps to next line if 'whichwrap' has 'l'.
3859 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 if ( ((cap->cmdchar == ' '
3861 && vim_strchr(p_ww, 's') != NULL)
3862 || (cap->cmdchar == 'l'
3863 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003864 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 && vim_strchr(p_ww, '>') != NULL))
3866 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3867 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003868 // When deleting we also count the NL as a character.
3869 // Set cap->oap->inclusive when last char in the line is
3870 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003871 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003873 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 cap->oap->inclusive = TRUE;
3875 else
3876 {
3877 ++curwin->w_cursor.lnum;
3878 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 curwin->w_set_curswant = TRUE;
3881 cap->oap->inclusive = FALSE;
3882 }
3883 continue;
3884 }
3885 if (cap->oap->op_type == OP_NOP)
3886 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003887 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 if (n == cap->count1)
3889 beep_flush();
3890 }
3891 else
3892 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003893 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 cap->oap->inclusive = TRUE;
3895 }
3896 break;
3897 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003898 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 {
3900 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 if (virtual_active())
3902 oneright();
3903 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003906 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 ++curwin->w_cursor.col;
3909 }
3910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 }
3912#ifdef FEAT_FOLDING
3913 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3914 && cap->oap->op_type == OP_NOP)
3915 foldOpenCursor();
3916#endif
3917}
3918
3919/*
3920 * Cursor left commands.
3921 *
3922 * Returns TRUE when operator end should not be adjusted.
3923 */
3924 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003925nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926{
3927 long n;
3928
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003929 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3930 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003931 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003932 if (mod_mask & MOD_MASK_CTRL)
3933 cap->arg = 1;
3934 nv_bck_word(cap);
3935 return;
3936 }
3937
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 cap->oap->motion_type = MCHAR;
3939 cap->oap->inclusive = FALSE;
3940 for (n = cap->count1; n > 0; --n)
3941 {
3942 if (oneleft() == FAIL)
3943 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003944 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3945 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3946 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 if ( (((cap->cmdchar == K_BS
3948 || cap->cmdchar == Ctrl_H)
3949 && vim_strchr(p_ww, 'b') != NULL)
3950 || (cap->cmdchar == 'h'
3951 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003952 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 && vim_strchr(p_ww, '<') != NULL))
3954 && curwin->w_cursor.lnum > 1)
3955 {
3956 --(curwin->w_cursor.lnum);
3957 coladvance((colnr_T)MAXCOL);
3958 curwin->w_set_curswant = TRUE;
3959
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003960 // When the NL before the first char has to be deleted we
3961 // put the cursor on the NUL after the previous line.
3962 // This is a very special case, be careful!
3963 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 if ( (cap->oap->op_type == OP_DELETE
3965 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003966 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003968 char_u *cp = ml_get_cursor();
3969
3970 if (*cp != NUL)
3971 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003972 if (has_mbyte)
3973 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3974 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003975 ++curwin->w_cursor.col;
3976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 cap->retval |= CA_NO_ADJ_OP_END;
3978 }
3979 continue;
3980 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003981 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3983 beep_flush();
3984 break;
3985 }
3986 }
3987#ifdef FEAT_FOLDING
3988 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3989 && cap->oap->op_type == OP_NOP)
3990 foldOpenCursor();
3991#endif
3992}
3993
3994/*
3995 * Cursor up commands.
3996 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3997 */
3998 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003999nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004001 if (mod_mask & MOD_MASK_SHIFT)
4002 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004003 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004004 cap->arg = BACKWARD;
4005 nv_page(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004006 return;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004007 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004008
4009 cap->oap->motion_type = MLINE;
4010 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4011 clearopbeep(cap->oap);
4012 else if (cap->arg)
4013 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014}
4015
4016/*
4017 * Cursor down commands.
4018 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4019 */
4020 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004021nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004023 if (mod_mask & MOD_MASK_SHIFT)
4024 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004025 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004026 cap->arg = FORWARD;
4027 nv_page(cap);
4028 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004029#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004030 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004031 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4032 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004034 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004036 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004037 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 cmdwin_result = CAR;
4039 else
Bram Moolenaarf2732452018-06-03 14:47:35 +02004040#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004041 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004042 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4043 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4044 {
4045 invoke_prompt_callback();
4046 if (restart_edit == 0)
4047 restart_edit = 'a';
4048 }
4049 else
4050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 {
4052 cap->oap->motion_type = MLINE;
4053 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4054 clearopbeep(cap->oap);
4055 else if (cap->arg)
4056 beginline(BL_WHITE | BL_FIX);
4057 }
4058 }
4059}
4060
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061/*
4062 * Grab the file name under the cursor and edit it.
4063 */
4064 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004065nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066{
4067 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004068 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004070 if (check_text_or_curbuf_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 return;
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004072
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004073#ifdef FEAT_PROP_POPUP
4074 if (ERROR_IF_TERM_POPUP_WINDOW)
4075 return;
4076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077
Colin Kennedy21570352024-03-03 16:16:47 +01004078 if (!check_can_set_curbuf_disabled())
4079 return;
4080
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004081 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082
4083 if (ptr != NULL)
4084 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004085 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004086 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004087 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004089 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004090 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004091 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004092 {
4093 curwin->w_cursor.lnum = lnum;
4094 check_cursor_lnum();
4095 beginline(BL_SOL | BL_FIX);
4096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 vim_free(ptr);
4098 }
4099 else
4100 clearop(cap->oap);
4101}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102
4103/*
4104 * <End> command: to end of current line or last line.
4105 */
4106 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004107nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004109 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004111 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004113 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
4115 nv_dollar(cap);
4116}
4117
4118/*
4119 * Handle the "$" command.
4120 */
4121 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004122nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123{
4124 cap->oap->motion_type = MCHAR;
4125 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004126 // In virtual mode when off the edge of a line and an operator
4127 // is pending (whew!) keep the cursor where it is.
4128 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 if (!virtual_active() || gchar_cursor() != NUL
4130 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004131 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 if (cursor_down((long)(cap->count1 - 1),
4133 cap->oap->op_type == OP_NOP) == FAIL)
4134 clearopbeep(cap->oap);
4135#ifdef FEAT_FOLDING
4136 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4137 foldOpenCursor();
4138#endif
4139}
4140
4141/*
4142 * Implementation of '?' and '/' commands.
4143 * If cap->arg is TRUE don't set PC mark.
4144 */
4145 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004146nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147{
4148 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004149 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150
4151 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4152 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004153 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 cap->cmdchar = 'g';
4155 cap->nchar = '?';
4156 nv_operator(cap);
4157 return;
4158 }
4159
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004160 // When using 'incsearch' the cursor may be moved to set a different search
4161 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004162 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163
4164 if (cap->searchbuf == NULL)
4165 {
4166 clearop(oap);
4167 return;
4168 }
4169
Bram Moolenaar46539112015-02-17 15:43:57 +01004170 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004171 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004172 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173}
4174
LemonBoya4399382022-04-09 21:04:08 +01004175
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176/*
4177 * Handle "N" and "n" commands.
4178 * cap->arg is SEARCH_REV for "N", 0 for "n".
4179 */
4180 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004181nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004183 pos_T old = curwin->w_cursor;
4184 int wrapped = FALSE;
4185 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004186
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004187 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004188 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004189 // Avoid getting stuck on the current cursor position, which can
4190 // happen when an offset is given and the cursor is on the last char
4191 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004192 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004193 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004194 cap->count1 -= 1;
4195 }
LemonBoya4399382022-04-09 21:04:08 +01004196
4197#ifdef FEAT_SEARCH_EXTRA
4198 // Redraw the window to refresh the highlighted matches.
4199 if (i > 0 && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004200 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202}
4203
4204/*
4205 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4206 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004207 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004209 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004210normal_search(
4211 cmdarg_T *cap,
4212 int dir,
4213 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004214 int opt, // extra flags for do_search()
4215 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216{
4217 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004218 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004219#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004220 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222
4223 cap->oap->motion_type = MCHAR;
4224 cap->oap->inclusive = FALSE;
4225 cap->oap->use_reg_one = TRUE;
4226 curwin->w_set_curswant = TRUE;
4227
Bram Moolenaara80faa82020-04-12 19:37:17 +02004228 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004229 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004230 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4231 if (wrapped != NULL)
4232 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 if (i == 0)
4234 clearop(cap->oap);
4235 else
4236 {
4237 if (i == 2)
4238 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240#ifdef FEAT_FOLDING
4241 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4242 foldOpenCursor();
4243#endif
4244 }
LemonBoya4399382022-04-09 21:04:08 +01004245#ifdef FEAT_SEARCH_EXTRA
4246 // Redraw the window to refresh the highlighted matches.
4247 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004248 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004251 // "/$" will put the cursor after the end of the line, may need to
4252 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004254 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255}
4256
4257/*
4258 * Character search commands.
4259 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4260 * ',' and FALSE for ';'.
4261 * cap->nchar is NUL for ',' and ';' (repeat the search)
4262 */
4263 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004264nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265{
4266 int t_cmd;
4267
4268 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4269 t_cmd = TRUE;
4270 else
4271 t_cmd = FALSE;
4272
4273 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004276 clearopbeep(cap->oap);
4277 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004279
4280 curwin->w_set_curswant = TRUE;
4281 // Include a Tab for "tx" and for "dfx".
4282 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4283 && (t_cmd || cap->oap->op_type != OP_NOP))
4284 {
4285 colnr_T scol, ecol;
4286
4287 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4288 curwin->w_cursor.coladd = ecol - scol;
4289 }
4290 else
4291 curwin->w_cursor.coladd = 0;
4292 adjust_for_sel(cap);
4293#ifdef FEAT_FOLDING
4294 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4295 foldOpenCursor();
4296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297}
4298
4299/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004300 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4301 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4302 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4303 * "[m" or "]m" search for prev/next start of (Java) method.
4304 * "[M" or "]M" search for prev/next end of (Java) method.
4305 */
4306 static void
4307nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4308{
4309 pos_T new_pos = {0, 0, 0};
4310 pos_T *pos = NULL; // init for GCC
4311 pos_T prev_pos;
4312 long n;
4313 int findc;
4314 int c;
4315
4316 if (cap->nchar == '*')
4317 cap->nchar = '/';
4318 prev_pos.lnum = 0;
4319 if (cap->nchar == 'm' || cap->nchar == 'M')
4320 {
4321 if (cap->cmdchar == '[')
4322 findc = '{';
4323 else
4324 findc = '}';
4325 n = 9999;
4326 }
4327 else
4328 {
4329 findc = cap->nchar;
4330 n = cap->count1;
4331 }
4332 for ( ; n > 0; --n)
4333 {
4334 if ((pos = findmatchlimit(cap->oap, findc,
4335 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4336 {
4337 if (new_pos.lnum == 0) // nothing found
4338 {
4339 if (cap->nchar != 'm' && cap->nchar != 'M')
4340 clearopbeep(cap->oap);
4341 }
4342 else
4343 pos = &new_pos; // use last one found
4344 break;
4345 }
4346 prev_pos = new_pos;
4347 curwin->w_cursor = *pos;
4348 new_pos = *pos;
4349 }
4350 curwin->w_cursor = *old_pos;
4351
4352 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4353 // brought us to the match for "[m" and "]M" when inside a method.
4354 // Try finding the '{' or '}' we want to be at.
4355 // Also repeat for the given count.
4356 if (cap->nchar == 'm' || cap->nchar == 'M')
4357 {
4358 // norm is TRUE for "]M" and "[m"
4359 int norm = ((findc == '{') == (cap->nchar == 'm'));
4360
4361 n = cap->count1;
4362 // found a match: we were inside a method
4363 if (prev_pos.lnum != 0)
4364 {
4365 pos = &prev_pos;
4366 curwin->w_cursor = prev_pos;
4367 if (norm)
4368 --n;
4369 }
4370 else
4371 pos = NULL;
4372 while (n > 0)
4373 {
4374 for (;;)
4375 {
4376 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4377 {
4378 // if not found anything, that's an error
4379 if (pos == NULL)
4380 clearopbeep(cap->oap);
4381 n = 0;
4382 break;
4383 }
4384 c = gchar_cursor();
4385 if (c == '{' || c == '}')
4386 {
4387 // Must have found end/start of class: use it.
4388 // Or found the place to be at.
4389 if ((c == findc && norm) || (n == 1 && !norm))
4390 {
4391 new_pos = curwin->w_cursor;
4392 pos = &new_pos;
4393 n = 0;
4394 }
4395 // if no match found at all, we started outside of the
4396 // class and we're inside now. Just go on.
4397 else if (new_pos.lnum == 0)
4398 {
4399 new_pos = curwin->w_cursor;
4400 pos = &new_pos;
4401 }
4402 // found start/end of other method: go to match
4403 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004404 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4405 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004406 n = 0;
4407 else
4408 curwin->w_cursor = *pos;
4409 break;
4410 }
4411 }
4412 --n;
4413 }
4414 curwin->w_cursor = *old_pos;
4415 if (pos == NULL && new_pos.lnum != 0)
4416 clearopbeep(cap->oap);
4417 }
4418 if (pos != NULL)
4419 {
4420 setpcmark();
4421 curwin->w_cursor = *pos;
4422 curwin->w_set_curswant = TRUE;
4423#ifdef FEAT_FOLDING
4424 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4425 && cap->oap->op_type == OP_NOP)
4426 foldOpenCursor();
4427#endif
4428 }
4429}
4430
4431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 * "[" and "]" commands.
4433 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4434 */
4435 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004436nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004439 pos_T *pos = NULL; // init for GCC
4440 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 int flag;
4442 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443
4444 cap->oap->motion_type = MCHAR;
4445 cap->oap->inclusive = FALSE;
4446 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004447 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004449 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 if (cap->nchar == 'f')
4451 nv_gotofile(cap);
4452 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
4454#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004455 // Find the occurrence(s) of the identifier or define under cursor
4456 // in current and included files or jump to the first occurrence.
4457 //
4458 // search list jump
4459 // fwd bwd fwd bwd fwd bwd
4460 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4461 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004462 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 {
4464 char_u *ptr;
4465 int len;
4466
4467 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4468 clearop(cap->oap);
4469 else
4470 {
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004471 // Make a copy, if the line was changed it will be freed.
4472 ptr = vim_strnsave(ptr, len);
4473 if (ptr == NULL)
4474 return;
4475
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 find_pattern_in_path(ptr, 0, len, TRUE,
Keith Thompson184f71c2024-01-04 21:19:04 +01004477 cap->count0 == 0 ? !SAFE_isupper(cap->nchar) : FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4479 cap->count1,
Keith Thompson184f71c2024-01-04 21:19:04 +01004480 SAFE_isupper(cap->nchar) ? ACTION_SHOW_ALL :
4481 SAFE_islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
Colin Kennedy21570352024-03-03 16:16:47 +01004483 (linenr_T)MAXLNUM,
4484 FALSE);
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004485 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 curwin->w_set_curswant = TRUE;
4487 }
4488 }
4489 else
4490#endif
4491
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004492 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4493 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4494 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4495 // "[m" or "]m" search for prev/next start of (Java) method.
4496 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 if ( (cap->cmdchar == '['
4498 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4499 || (cap->cmdchar == ']'
4500 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004501 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004503 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 else if (cap->nchar == '[' || cap->nchar == ']')
4505 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004506 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 flag = '{';
4508 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004509 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510
4511 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004512 // Imitate strange Vi behaviour: When using "]]" with an operator
4513 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004514 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 (cap->oap->op_type != OP_NOP
4516 && cap->arg == FORWARD && flag == '{')))
4517 clearopbeep(cap->oap);
4518 else
4519 {
4520 if (cap->oap->op_type == OP_NOP)
4521 beginline(BL_WHITE | BL_FIX);
4522#ifdef FEAT_FOLDING
4523 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4524 foldOpenCursor();
4525#endif
4526 }
4527 }
4528
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004529 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 else if (cap->nchar == 'p' || cap->nchar == 'P')
4531 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004532 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 }
4534
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004535 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 else if (cap->nchar == '\'' || cap->nchar == '`')
4537 {
4538 pos = &curwin->w_cursor;
4539 for (n = cap->count1; n > 0; --n)
4540 {
4541 prev_pos = *pos;
4542 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4543 cap->nchar == '\'');
4544 if (pos == NULL)
4545 break;
4546 }
4547 if (pos == NULL)
4548 pos = &prev_pos;
4549 nv_cursormark(cap, cap->nchar == '\'', pos);
4550 }
4551
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004552 // [ or ] followed by a middle mouse click: put selected text with
4553 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004554 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 {
4556 (void)do_mouse(cap->oap, cap->nchar,
4557 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4558 cap->count1, PUT_FIXINDENT);
4559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560
4561#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004562 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 else if (cap->nchar == 'z')
4564 {
4565 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4566 cap->count1) == FAIL)
4567 clearopbeep(cap->oap);
4568 }
4569#endif
4570
4571#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004572 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 else if (cap->nchar == 'c')
4574 {
4575 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4576 cap->count1) == FAIL)
4577 clearopbeep(cap->oap);
4578 }
4579#endif
4580
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004581#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004582 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004583 else if (cap->nchar == 's' || cap->nchar == 'S')
4584 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004585 setpcmark();
4586 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004587 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4588 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004589 {
4590 clearopbeep(cap->oap);
4591 break;
4592 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004593 else
4594 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004595# ifdef FEAT_FOLDING
4596 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4597 foldOpenCursor();
4598# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004599 }
4600#endif
4601
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004602 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 else
4604 clearopbeep(cap->oap);
4605}
4606
4607/*
4608 * Handle Normal mode "%" command.
4609 */
4610 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004611nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612{
4613 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004614#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 linenr_T lnum = curwin->w_cursor.lnum;
4616#endif
4617
4618 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004619 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 {
4621 if (cap->count0 > 100)
4622 clearopbeep(cap->oap);
4623 else
4624 {
4625 cap->oap->motion_type = MLINE;
4626 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004627 // Round up, so 'normal 100%' always jumps at the line line.
4628 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4629 // overflow on 32-bits, so use a formula with less accuracy
4630 // to avoid overflows.
4631 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4633 / 100L * cap->count0;
4634 else
4635 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4636 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004637 if (curwin->w_cursor.lnum < 1)
4638 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4640 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4641 beginline(BL_SOL | BL_FIX);
4642 }
4643 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004644 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 {
4646 cap->oap->motion_type = MCHAR;
4647 cap->oap->use_reg_one = TRUE;
4648 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4649 clearopbeep(cap->oap);
4650 else
4651 {
4652 setpcmark();
4653 curwin->w_cursor = *pos;
4654 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 }
4658 }
4659#ifdef FEAT_FOLDING
4660 if (cap->oap->op_type == OP_NOP
4661 && lnum != curwin->w_cursor.lnum
4662 && (fdo_flags & FDO_PERCENT)
4663 && KeyTyped)
4664 foldOpenCursor();
4665#endif
4666}
4667
4668/*
4669 * Handle "(" and ")" commands.
4670 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4671 */
4672 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004673nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674{
4675 cap->oap->motion_type = MCHAR;
4676 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004677 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004678 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 curwin->w_set_curswant = TRUE;
4680
4681 if (findsent(cap->arg, cap->count1) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004683 clearopbeep(cap->oap);
4684 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004686
4687 // Don't leave the cursor on the NUL past end of line.
4688 adjust_cursor(cap->oap);
4689 curwin->w_cursor.coladd = 0;
4690#ifdef FEAT_FOLDING
4691 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4692 foldOpenCursor();
4693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694}
4695
4696/*
4697 * "m" command: Mark a position.
4698 */
4699 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004700nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004702 if (checkclearop(cap->oap))
4703 return;
4704
4705 if (setmark(cap->nchar) == FAIL)
4706 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707}
4708
4709/*
4710 * "{" and "}" commands.
4711 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4712 */
4713 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004714nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715{
4716 cap->oap->motion_type = MCHAR;
4717 cap->oap->inclusive = FALSE;
4718 cap->oap->use_reg_one = TRUE;
4719 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004720 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004722 clearopbeep(cap->oap);
4723 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004725
4726 curwin->w_cursor.coladd = 0;
4727#ifdef FEAT_FOLDING
4728 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4729 foldOpenCursor();
4730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731}
4732
4733/*
4734 * "u" command: Undo or make lower case.
4735 */
4736 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004737nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004739 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004741 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 cap->cmdchar = 'g';
4743 cap->nchar = 'u';
4744 nv_operator(cap);
4745 }
4746 else
4747 nv_kundo(cap);
4748}
4749
4750/*
4751 * <Undo> command.
4752 */
4753 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004754nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004756 if (checkclearopq(cap->oap))
4757 return;
4758
Bram Moolenaarf2732452018-06-03 14:47:35 +02004759#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004760 if (bt_prompt(curbuf))
4761 {
4762 clearopbeep(cap->oap);
4763 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004765#endif
4766 u_undo((int)cap->count1);
4767 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768}
4769
4770/*
4771 * Handle the "r" command.
4772 */
4773 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004774nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 int had_ctrl_v;
4777 long n;
4778
4779 if (checkclearop(cap->oap))
4780 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004781#ifdef FEAT_JOB_CHANNEL
4782 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4783 {
4784 clearopbeep(cap->oap);
4785 return;
4786 }
4787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004789 // get another character
Christian Brabandt2d63e4b2023-08-12 00:03:57 +02004790 if (cap->nchar == Ctrl_V || cap->nchar == Ctrl_Q)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 {
4792 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004793 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004794 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 if (cap->nchar > DEL)
4796 had_ctrl_v = NUL;
4797 }
4798 else
4799 had_ctrl_v = NUL;
4800
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004801 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004802 if (IS_SPECIAL(cap->nchar))
4803 {
4804 clearopbeep(cap->oap);
4805 return;
4806 }
4807
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004808 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 if (VIsual_active)
4810 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004811 if (got_int)
Christian Brabandt476733f2023-09-19 20:41:51 +02004812 got_int = FALSE;
Bram Moolenaard9820532013-11-04 01:41:17 +01004813 if (had_ctrl_v)
4814 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004815 // Use a special (negative) number to make a difference between a
4816 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004817 if (cap->nchar == CAR)
4818 cap->nchar = REPLACE_CR_NCHAR;
4819 else if (cap->nchar == NL)
4820 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 nv_operator(cap);
4823 return;
4824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004826 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 if (virtual_active())
4828 {
4829 if (u_save_cursor() == FAIL)
4830 return;
4831 if (gchar_cursor() == NUL)
4832 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004833 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 coladvance_force((colnr_T)(getviscol() + cap->count1));
4835 curwin->w_cursor.col -= cap->count1;
4836 }
4837 else if (gchar_cursor() == TAB)
4838 coladvance_force(getviscol());
4839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004841 // Abort if not enough characters to replace.
John Marriott02d7a6c2024-02-26 21:21:17 +01004842 if ((size_t)ml_get_cursor_len() < (unsigned)cap->count1
4843 || (has_mbyte && mb_charlen(ml_get_cursor()) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 {
4845 clearopbeep(cap->oap);
4846 return;
4847 }
4848
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004849 // Replacing with a TAB is done by edit() when it is complicated because
4850 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4851 // Other characters are done below to avoid problems with things like
4852 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4854 {
4855 stuffnumReadbuff(cap->count1);
4856 stuffcharReadbuff('R');
4857 stuffcharReadbuff('\t');
4858 stuffcharReadbuff(ESC);
4859 return;
4860 }
4861
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004862 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 if (u_save_cursor() == FAIL)
4864 return;
4865
4866 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4867 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004868 // Replace character(s) by a single newline.
4869 // Strange vi behaviour: Only one newline is inserted.
4870 // Delete the characters here.
4871 // Insert the newline with an insert command, takes care of
4872 // autoindent. The insert command depends on being on the last
4873 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004874 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 stuffcharReadbuff('\r');
4876 stuffcharReadbuff(ESC);
4877
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004878 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 invoke_edit(cap, TRUE, 'r', FALSE);
4880 }
4881 else
4882 {
4883 prep_redo(cap->oap->regname, cap->count1,
4884 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4885
4886 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 if (has_mbyte)
4888 {
4889 int old_State = State;
4890
4891 if (cap->ncharC1 != 0)
4892 AppendCharToRedobuff(cap->ncharC1);
4893 if (cap->ncharC2 != 0)
4894 AppendCharToRedobuff(cap->ncharC2);
4895
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004896 // This is slow, but it handles replacing a single-byte with a
4897 // multi-byte and the other way around. Also handles adding
4898 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 for (n = cap->count1; n > 0; --n)
4900 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004901 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004902 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4903 {
4904 int c = ins_copychar(curwin->w_cursor.lnum
4905 + (cap->nchar == Ctrl_Y ? -1 : 1));
4906 if (c != NUL)
4907 ins_char(c);
4908 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004909 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004910 ++curwin->w_cursor.col;
4911 }
4912 else
4913 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 State = old_State;
4915 if (cap->ncharC1 != 0)
4916 ins_char(cap->ncharC1);
4917 if (cap->ncharC2 != 0)
4918 ins_char(cap->ncharC2);
4919 }
4920 }
4921 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 {
John Marriott02d7a6c2024-02-26 21:21:17 +01004923 char_u *ptr;
4924
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004925 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 for (n = cap->count1; n > 0; --n)
4927 {
John Marriott02d7a6c2024-02-26 21:21:17 +01004928 // Get ptr again, because ins_copychar() and showmatch()
4929 // will have released the line.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004930 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004931 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4932 {
4933 int c = ins_copychar(curwin->w_cursor.lnum
4934 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004935
4936 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004937 if (c != NUL)
4938 ptr[curwin->w_cursor.col] = c;
4939 }
4940 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004941 {
4942 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004943 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 if (p_sm && msg_silent == 0)
4946 showmatch(cap->nchar);
4947 ++curwin->w_cursor.col;
4948 }
4949#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004950 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004952 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
John Marriott02d7a6c2024-02-26 21:21:17 +01004953 ptr = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954
Bram Moolenaar009b2592004-10-24 19:18:58 +00004955 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004956 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004958 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 }
4960#endif
4961
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004962 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 changed_bytes(curwin->w_cursor.lnum,
4964 (colnr_T)(curwin->w_cursor.col - cap->count1));
4965 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004966 --curwin->w_cursor.col; // cursor on the last replaced char
4967 // if the character on the left of the current cursor is a multi-byte
4968 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 if (has_mbyte)
4970 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 curbuf->b_op_end = curwin->w_cursor;
4972 curwin->w_set_curswant = TRUE;
4973 set_last_insert(cap->nchar);
4974 }
4975}
4976
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977/*
4978 * 'o': Exchange start and end of Visual area.
4979 * 'O': same, but in block mode exchange left and right corners.
4980 */
4981 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004982v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983{
4984 pos_T old_cursor;
4985 colnr_T left, right;
4986
4987 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4988 {
4989 old_cursor = curwin->w_cursor;
4990 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4991 curwin->w_cursor.lnum = VIsual.lnum;
4992 coladvance(left);
4993 VIsual = curwin->w_cursor;
4994
4995 curwin->w_cursor.lnum = old_cursor.lnum;
4996 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004997 // 'selection "exclusive" and cursor at right-bottom corner: move it
4998 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
5000 ++curwin->w_curswant;
5001 coladvance(curwin->w_curswant);
5002 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005004 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 {
5006 curwin->w_cursor.lnum = VIsual.lnum;
5007 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5008 ++right;
5009 coladvance(right);
5010 VIsual = curwin->w_cursor;
5011
5012 curwin->w_cursor.lnum = old_cursor.lnum;
5013 coladvance(left);
5014 curwin->w_curswant = left;
5015 }
5016 }
5017 else
5018 {
5019 old_cursor = curwin->w_cursor;
5020 curwin->w_cursor = VIsual;
5021 VIsual = old_cursor;
5022 curwin->w_set_curswant = TRUE;
5023 }
5024}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025
5026/*
5027 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5028 */
5029 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005030nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005032 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
5034 cap->cmdchar = 'c';
5035 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005036 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 VIsual_mode = 'V';
5038 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005039 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005041
5042 if (checkclearopq(cap->oap))
5043 return;
5044
5045 if (!curbuf->b_p_ma)
5046 emsg(_(e_cannot_make_changes_modifiable_is_off));
5047 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005049 if (virtual_active())
5050 coladvance(getviscol());
5051 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 }
5053}
5054
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055/*
5056 * "gr".
5057 */
5058 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005059nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 if (VIsual_active)
5062 {
5063 cap->cmdchar = 'r';
5064 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005065 nv_replace(cap); // Do same as "r" in Visual mode for now
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005066 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005068
zeertzjqf86dea82023-03-05 21:15:06 +00005069 if (checkclearopq(cap->oap))
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005070 return;
5071
5072 if (!curbuf->b_p_ma)
5073 emsg(_(e_cannot_make_changes_modifiable_is_off));
5074 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 {
Christian Brabandt2d63e4b2023-08-12 00:03:57 +02005076 if (cap->extra_char == Ctrl_V || cap->extra_char == Ctrl_Q)
5077 // get another character
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005078 cap->extra_char = get_literal(FALSE);
Bram Moolenaard6a4ea32023-02-25 14:24:44 +00005079 if (cap->extra_char < ' ')
5080 // Prefix a control character with CTRL-V to avoid it being used as
5081 // a command.
5082 stuffcharReadbuff(Ctrl_V);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005083 stuffcharReadbuff(cap->extra_char);
5084 stuffcharReadbuff(ESC);
5085 if (virtual_active())
5086 coladvance(getviscol());
5087 invoke_edit(cap, TRUE, 'v', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 }
5089}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090
5091/*
5092 * Swap case for "~" command, when it does not work like an operator.
5093 */
5094 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005095n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096{
5097 long n;
5098 pos_T startpos;
5099 int did_change = 0;
5100#ifdef FEAT_NETBEANS_INTG
5101 pos_T pos;
5102 char_u *ptr;
5103 int count;
5104#endif
5105
5106 if (checkclearopq(cap->oap))
5107 return;
5108
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005109 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
5111 clearopbeep(cap->oap);
5112 return;
5113 }
5114
5115 prep_redo_cmd(cap);
5116
5117 if (u_save_cursor() == FAIL)
5118 return;
5119
5120 startpos = curwin->w_cursor;
5121#ifdef FEAT_NETBEANS_INTG
5122 pos = startpos;
5123#endif
5124 for (n = cap->count1; n > 0; --n)
5125 {
5126 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5127 inc_cursor();
5128 if (gchar_cursor() == NUL)
5129 {
5130 if (vim_strchr(p_ww, '~') != NULL
5131 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5132 {
5133#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005134 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 {
5136 if (did_change)
5137 {
5138 ptr = ml_get(pos.lnum);
John Marriott02d7a6c2024-02-26 21:21:17 +01005139 count = (int)ml_get_len(pos.lnum) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005140 netbeans_removed(curbuf, pos.lnum, pos.col,
5141 (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01005142 // line may have been flushed, get it again
5143 ptr = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005145 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 }
5147 pos.col = 0;
5148 pos.lnum++;
5149 }
5150#endif
5151 ++curwin->w_cursor.lnum;
5152 curwin->w_cursor.col = 0;
5153 if (n > 1)
5154 {
5155 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5156 break;
5157 u_clearline();
5158 }
5159 }
5160 else
5161 break;
5162 }
5163 }
5164#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005165 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 {
5167 ptr = ml_get(pos.lnum);
5168 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005169 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5170 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 }
5172#endif
5173
5174
5175 check_cursor();
5176 curwin->w_set_curswant = TRUE;
5177 if (did_change)
5178 {
5179 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5180 0L);
5181 curbuf->b_op_start = startpos;
5182 curbuf->b_op_end = curwin->w_cursor;
5183 if (curbuf->b_op_end.col > 0)
5184 --curbuf->b_op_end.col;
5185 }
5186}
5187
5188/*
5189 * Move cursor to mark.
5190 */
5191 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005192nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193{
5194 if (check_mark(pos) == FAIL)
5195 clearop(cap->oap);
5196 else
5197 {
5198 if (cap->cmdchar == '\''
5199 || cap->cmdchar == '`'
5200 || cap->cmdchar == '['
5201 || cap->cmdchar == ']')
5202 setpcmark();
5203 curwin->w_cursor = *pos;
5204 if (flag)
5205 beginline(BL_WHITE | BL_FIX);
5206 else
5207 check_cursor();
5208 }
5209 cap->oap->motion_type = flag ? MLINE : MCHAR;
5210 if (cap->cmdchar == '`')
5211 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005212 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 curwin->w_set_curswant = TRUE;
5214}
5215
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216/*
5217 * Handle commands that are operators in Visual mode.
5218 */
5219 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005220v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221{
5222 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5223
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005224 // Uppercase means linewise, except in block mode, then "D" deletes till
5225 // the end of the line, and "C" replaces till EOL
Keith Thompson184f71c2024-01-04 21:19:04 +01005226 if (SAFE_isupper(cap->cmdchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 {
5228 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005229 {
5230 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5234 curwin->w_curswant = MAXCOL;
5235 }
5236 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5237 nv_operator(cap);
5238}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239
5240/*
5241 * "s" and "S" commands.
5242 */
5243 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005244nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005246#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005247 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005248 if (term_swap_diff() == OK)
5249 return;
5250#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005251#ifdef FEAT_JOB_CHANNEL
5252 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5253 {
5254 clearopbeep(cap->oap);
5255 return;
5256 }
5257#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005258 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 {
5260 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005261 {
5262 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 cap->cmdchar = 'c';
5266 nv_operator(cap);
5267 }
5268 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 nv_optrans(cap);
5270}
5271
5272/*
5273 * Abbreviated commands.
5274 */
5275 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005276nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277{
5278 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005279 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005281 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 if (VIsual_active)
5283 v_visop(cap);
5284 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 nv_optrans(cap);
5286}
5287
5288/*
5289 * Translate a command into another command.
5290 */
5291 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005292nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293{
5294 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5295 (char_u *)"d$", (char_u *)"c$",
5296 (char_u *)"cl", (char_u *)"cc",
5297 (char_u *)"yy", (char_u *)":s\r"};
5298 static char_u *str = (char_u *)"xXDCsSY&";
5299
5300 if (!checkclearopq(cap->oap))
5301 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005302 // In Vi "2D" doesn't delete the next line. Can't translate it
5303 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005304 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5305 {
5306 cap->oap->start = curwin->w_cursor;
5307 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005308#ifdef FEAT_EVAL
5309 set_op_var(OP_DELETE);
5310#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005311 cap->count1 = 1;
5312 nv_dollar(cap);
5313 finish_op = TRUE;
5314 ResetRedobuff();
5315 AppendCharToRedobuff('D');
5316 }
5317 else
5318 {
5319 if (cap->count0)
5320 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005321 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 }
5324 cap->opcount = 0;
5325}
5326
5327/*
5328 * "'" and "`" commands. Also for "g'" and "g`".
5329 * cap->arg is TRUE for "'" and "g'".
5330 */
5331 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005332nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333{
5334 pos_T *pos;
5335 int c;
5336#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005337 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005338 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339#endif
5340
5341 if (cap->cmdchar == 'g')
5342 c = cap->extra_char;
5343 else
5344 c = cap->nchar;
5345 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005346 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 {
5348 if (cap->arg)
5349 {
5350 check_cursor_lnum();
5351 beginline(BL_WHITE | BL_FIX);
5352 }
5353 else
5354 check_cursor();
5355 }
5356 else
5357 nv_cursormark(cap, cap->arg, pos);
5358
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005359 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 if (!virtual_active())
5361 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005362 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363#ifdef FEAT_FOLDING
5364 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005365 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005366 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 && (fdo_flags & FDO_MARK)
5368 && old_KeyTyped)
5369 foldOpenCursor();
5370#endif
5371}
5372
5373/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005374 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 */
5376 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005377nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005380#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005382 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005385 if (checkclearopq(cap->oap))
5386 return;
5387
5388 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005390 if (goto_tabpage_lastused() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 clearopbeep(cap->oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005392 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005394 if (cap->cmdchar == 'g')
5395 pos = movechangelist((int)cap->count1);
5396 else
5397 pos = movemark((int)cap->count1);
5398 if (pos == (pos_T *)-1) // jump to other file
5399 {
5400 curwin->w_set_curswant = TRUE;
5401 check_cursor();
5402 }
5403 else if (pos != NULL) // can jump
5404 nv_cursormark(cap, FALSE, pos);
5405 else if (cap->cmdchar == 'g')
5406 {
5407 if (curbuf->b_changelistlen == 0)
5408 emsg(_(e_changelist_is_empty));
5409 else if (cap->count1 < 0)
5410 emsg(_(e_at_start_of_changelist));
5411 else
5412 emsg(_(e_at_end_of_changelist));
5413 }
5414 else
5415 clearopbeep(cap->oap);
5416# ifdef FEAT_FOLDING
5417 if (cap->oap->op_type == OP_NOP
5418 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5419 && (fdo_flags & FDO_MARK)
5420 && old_KeyTyped)
5421 foldOpenCursor();
5422# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423}
5424
5425/*
5426 * Handle '"' command.
5427 */
5428 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005429nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430{
5431 if (checkclearop(cap->oap))
5432 return;
5433#ifdef FEAT_EVAL
5434 if (cap->nchar == '=')
5435 cap->nchar = get_expr_register();
5436#endif
5437 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5438 {
5439 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005440 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441#ifdef FEAT_EVAL
5442 set_reg_var(cap->oap->regname);
5443#endif
5444 }
5445 else
5446 clearopbeep(cap->oap);
5447}
5448
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449/*
5450 * Handle "v", "V" and "CTRL-V" commands.
5451 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5452 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005453 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 */
5455 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005456nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005458 if (cap->cmdchar == Ctrl_Q)
5459 cap->cmdchar = Ctrl_V;
5460
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005461 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5462 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 if (cap->oap->op_type != OP_NOP)
5464 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005465 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005466 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 return;
5468 }
5469
5470 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005471 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005473 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005475 else // toggle char/block mode
5476 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 VIsual_mode = cap->cmdchar;
5478 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005479 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005481 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005483 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 {
5485 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005486 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005488 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 VIsual = curwin->w_cursor;
5490
5491 VIsual_active = TRUE;
5492 VIsual_reselect = TRUE;
5493 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005494 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005497 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005498 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005499 // For V and ^V, we multiply the number of lines even if there
5500 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5502 {
5503 curwin->w_cursor.lnum +=
5504 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005505 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 }
5507 VIsual_mode = resel_VIsual_mode;
5508 if (VIsual_mode == 'v')
5509 {
5510 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005511 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005512 update_curswant_force();
Bram Moolenaar79c11e32023-01-10 17:29:29 +00005513 curwin->w_curswant += resel_VIsual_vcol * cap->count0;
5514 if (*p_sel != 'e')
5515 --curwin->w_curswant;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005518 curwin->w_curswant = resel_VIsual_vcol;
5519 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005521 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 {
5523 curwin->w_curswant = MAXCOL;
5524 coladvance((colnr_T)MAXCOL);
5525 }
5526 else if (VIsual_mode == Ctrl_V)
5527 {
Bram Moolenaar8f531662023-02-01 17:33:18 +00005528 // Update curswant on the original line, that is where "col" is
5529 // valid.
5530 linenr_T lnum = curwin->w_cursor.lnum;
5531 curwin->w_cursor.lnum = VIsual.lnum;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005532 update_curswant_force();
Bram Moolenaar8f531662023-02-01 17:33:18 +00005533 curwin->w_curswant += resel_VIsual_vcol * cap->count0 - 1;
5534 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 coladvance(curwin->w_curswant);
5536 }
5537 else
5538 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005539 redraw_curbuf_later(UPD_INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 }
5541 else
5542 {
5543 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005544 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 may_start_select('c');
5546 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005547 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005548 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005549 if (cap->count0 > 0 && --cap->count1 > 0)
5550 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005551 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005552 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5553 nv_right(cap);
5554 else if (VIsual_mode == 'V')
5555 nv_down(cap);
5556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 }
5558 }
5559}
5560
5561/*
5562 * Start selection for Shift-movement keys.
5563 */
5564 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005565start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005567 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 may_start_select('k');
5569 n_start_visual_mode('v');
5570}
5571
5572/*
5573 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005574 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 */
5576 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005577may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005579 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5580 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581}
5582
5583/*
5584 * Start Visual mode "c".
5585 * Should set VIsual_select before calling this.
5586 */
5587 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005588n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005590#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005591 int cursor_line_was_concealed = curwin->w_p_cole > 0
5592 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005593#endif
5594
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 VIsual_mode = c;
5596 VIsual_active = TRUE;
5597 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005598
5599 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005600 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005601 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005602 {
5603 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 VIsual = curwin->w_cursor;
5607
5608#ifdef FEAT_FOLDING
5609 foldAdjustVisual();
5610#endif
5611
LemonBoy2bf52dd2022-04-09 18:17:34 +01005612 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005614#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005615 // Check if redraw is needed after changing the state.
5616 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005617#endif
5618
Bram Moolenaar09df3122006-01-23 22:23:09 +00005619 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005620 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005622 // Make sure the clipboard gets updated. Needed because start and
5623 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 clip_star.vmode = NUL;
5625#endif
5626
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005627 // Only need to redraw this line, unless still need to redraw an old
5628 // Visual area (when 'lazyredraw' is set).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005629 if (curwin->w_redr_type < UPD_INVERTED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 {
5631 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5632 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5633 }
5634}
5635
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636
5637/*
5638 * CTRL-W: Window commands
5639 */
5640 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005641nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005643 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005644 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005645 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005646 cap->cmdchar = ':';
5647 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005648 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005649 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005650 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005651 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652}
5653
5654/*
5655 * CTRL-Z: Suspend
5656 */
5657 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005658nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659{
5660 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005662 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005663 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664}
5665
5666/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005667 * "gv": Reselect the previous Visual area. If Visual already active,
5668 * exchange previous and current Visual area.
5669 */
5670 static void
5671nv_gv_cmd(cmdarg_T *cap)
5672{
5673 pos_T tpos;
5674 int i;
5675
5676 if (checkclearop(cap->oap))
5677 return;
5678
5679 if (curbuf->b_visual.vi_start.lnum == 0
5680 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5681 || curbuf->b_visual.vi_end.lnum == 0)
5682 {
5683 beep_flush();
5684 return;
5685 }
5686
5687 // set w_cursor to the start of the Visual area, tpos to the end
5688 if (VIsual_active)
5689 {
5690 i = VIsual_mode;
5691 VIsual_mode = curbuf->b_visual.vi_mode;
5692 curbuf->b_visual.vi_mode = i;
5693# ifdef FEAT_EVAL
5694 curbuf->b_visual_mode_eval = i;
5695# endif
5696 i = curwin->w_curswant;
5697 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5698 curbuf->b_visual.vi_curswant = i;
5699
5700 tpos = curbuf->b_visual.vi_end;
5701 curbuf->b_visual.vi_end = curwin->w_cursor;
5702 curwin->w_cursor = curbuf->b_visual.vi_start;
5703 curbuf->b_visual.vi_start = VIsual;
5704 }
5705 else
5706 {
5707 VIsual_mode = curbuf->b_visual.vi_mode;
5708 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5709 tpos = curbuf->b_visual.vi_end;
5710 curwin->w_cursor = curbuf->b_visual.vi_start;
5711 }
5712
5713 VIsual_active = TRUE;
5714 VIsual_reselect = TRUE;
5715
5716 // Set Visual to the start and w_cursor to the end of the Visual
5717 // area. Make sure they are on an existing character.
5718 check_cursor();
5719 VIsual = curwin->w_cursor;
5720 curwin->w_cursor = tpos;
5721 check_cursor();
5722 update_topline();
5723
5724 // When called from normal "g" command: start Select mode when
5725 // 'selectmode' contains "cmd". When called for K_SELECT, always
5726 // start Select mode.
5727 if (cap->arg)
5728 {
5729 VIsual_select = TRUE;
5730 VIsual_select_reg = 0;
5731 }
5732 else
5733 may_start_select('c');
5734 setmouse();
5735#ifdef FEAT_CLIPBOARD
5736 // Make sure the clipboard gets updated. Needed because start and
5737 // end are still the same, and the selection needs to be owned
5738 clip_star.vmode = NUL;
5739#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005740 redraw_curbuf_later(UPD_INVERTED);
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005741 showmode();
5742}
5743
5744/*
5745 * "g0", "g^" : Like "0" and "^" but for screen lines.
5746 * "gm": middle of "g0" and "g$".
5747 */
5748 static void
5749nv_g_home_m_cmd(cmdarg_T *cap)
5750{
5751 int i;
5752 int flag = FALSE;
5753
5754 if (cap->nchar == '^')
5755 flag = TRUE;
5756
5757 cap->oap->motion_type = MCHAR;
5758 cap->oap->inclusive = FALSE;
5759 if (curwin->w_p_wrap && curwin->w_width != 0)
5760 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005761 int width1 = curwin->w_width - curwin_col_off();
5762 int width2 = width1 + curwin_col_off2();
5763 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005764
5765 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005766 virtcol = curwin->w_virtcol
5767#ifdef FEAT_PROP_POPUP
5768 - curwin->w_virtcol_first_char
5769#endif
5770 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005771 i = 0;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005772 if (virtcol >= (colnr_T)width1 && width2 > 0)
5773 i = (virtcol - width1) / width2 * width2 + width1;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005774 }
5775 else
5776 i = curwin->w_leftcol;
5777 // Go to the middle of the screen line. When 'number' or
5778 // 'relativenumber' is on and lines are wrapping the middle can be more
5779 // to the left.
5780 if (cap->nchar == 'm')
5781 i += (curwin->w_width - curwin_col_off()
5782 + ((curwin->w_p_wrap && i > 0)
5783 ? curwin_col_off2() : 0)) / 2;
5784 coladvance((colnr_T)i);
5785 if (flag)
5786 {
5787 do
5788 i = gchar_cursor();
5789 while (VIM_ISWHITE(i) && oneright() == OK);
5790 curwin->w_valid &= ~VALID_WCOL;
5791 }
5792 curwin->w_set_curswant = TRUE;
Luuk van Baal8667a562023-05-12 15:47:25 +01005793 adjust_skipcol();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005794}
5795
5796/*
5797 * "g_": to the last non-blank character in the line or <count> lines
5798 * downward.
5799 */
5800 static void
5801nv_g_underscore_cmd(cmdarg_T *cap)
5802{
5803 char_u *ptr;
5804
5805 cap->oap->motion_type = MCHAR;
5806 cap->oap->inclusive = TRUE;
5807 curwin->w_curswant = MAXCOL;
5808 if (cursor_down((long)(cap->count1 - 1),
5809 cap->oap->op_type == OP_NOP) == FAIL)
5810 {
5811 clearopbeep(cap->oap);
5812 return;
5813 }
5814
5815 ptr = ml_get_curline();
5816
5817 // In Visual mode we may end up after the line.
5818 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5819 --curwin->w_cursor.col;
5820
5821 // Decrease the cursor column until it's on a non-blank.
5822 while (curwin->w_cursor.col > 0
5823 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5824 --curwin->w_cursor.col;
5825 curwin->w_set_curswant = TRUE;
5826 adjust_for_sel(cap);
5827}
5828
5829/*
5830 * "g$" : Like "$" but for screen lines.
5831 */
5832 static void
5833nv_g_dollar_cmd(cmdarg_T *cap)
5834{
5835 oparg_T *oap = cap->oap;
5836 int i;
5837 int col_off = curwin_col_off();
Christian Brabandtb5f6fe92023-08-19 15:53:16 +02005838 int flag = FALSE;
5839
zeertzjq654bdbb2023-08-20 18:24:20 +02005840 if (cap->nchar == K_END || cap->nchar == K_KEND)
Christian Brabandtb5f6fe92023-08-19 15:53:16 +02005841 flag = TRUE;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005842
5843 oap->motion_type = MCHAR;
5844 oap->inclusive = TRUE;
5845 if (curwin->w_p_wrap && curwin->w_width != 0)
5846 {
5847 curwin->w_curswant = MAXCOL; // so we stay at the end
5848 if (cap->count1 == 1)
5849 {
5850 int width1 = curwin->w_width - col_off;
5851 int width2 = width1 + curwin_col_off2();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005852 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005853
5854 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005855 virtcol = curwin->w_virtcol
5856#ifdef FEAT_PROP_POPUP
5857 - curwin->w_virtcol_first_char
5858#endif
5859 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005860 i = width1 - 1;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005861 if (virtcol >= (colnr_T)width1)
5862 i += ((virtcol - width1) / width2 + 1)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005863 * width2;
5864 coladvance((colnr_T)i);
5865
5866 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005867 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005868 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5869 {
5870 // Check for landing on a character that got split at
5871 // the end of the line. We do not want to advance to
5872 // the next screen line.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005873 if (curwin->w_virtcol
5874#ifdef FEAT_PROP_POPUP
5875 - curwin->w_virtcol_first_char
5876#endif
5877 > (colnr_T)i)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005878 --curwin->w_cursor.col;
5879 }
5880 }
5881 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5882 clearopbeep(oap);
5883 }
5884 else
5885 {
5886 if (cap->count1 > 1)
5887 // if it fails, let the cursor still move to the last char
5888 (void)cursor_down(cap->count1 - 1, FALSE);
5889
5890 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5891 coladvance((colnr_T)i);
5892
5893 // if the character doesn't fit move one back
5894 if (curwin->w_cursor.col > 0
5895 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5896 {
5897 colnr_T vcol;
5898
5899 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5900 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5901 --curwin->w_cursor.col;
5902 }
5903
5904 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005905 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005906 }
Christian Brabandtb5f6fe92023-08-19 15:53:16 +02005907 if (flag)
5908 {
5909 do
5910 i = gchar_cursor();
5911 while (VIM_ISWHITE(i) && oneleft() == OK);
5912 curwin->w_valid &= ~VALID_WCOL;
5913 }
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005914}
5915
5916/*
5917 * "gi": start Insert at the last position.
5918 */
5919 static void
5920nv_gi_cmd(cmdarg_T *cap)
5921{
5922 int i;
5923
5924 if (curbuf->b_last_insert.lnum != 0)
5925 {
5926 curwin->w_cursor = curbuf->b_last_insert;
5927 check_cursor_lnum();
John Marriott02d7a6c2024-02-26 21:21:17 +01005928 i = (int)ml_get_curline_len();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005929 if (curwin->w_cursor.col > (colnr_T)i)
5930 {
5931 if (virtual_active())
5932 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5933 curwin->w_cursor.col = i;
5934 }
5935 }
5936 cap->cmdchar = 'i';
5937 nv_edit(cap);
5938}
5939
5940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 * Commands starting with "g".
5942 */
5943 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005944nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945{
5946 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948
5949 switch (cap->nchar)
5950 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005951 case Ctrl_A:
5952 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005954 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005955 if (!VIsual_active && cap->nchar == Ctrl_A)
5956 vim_mem_profile_dump();
5957 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005959 // "g^A/g^X": sequentially increment visually selected region
zeertzjqf86dea82023-03-05 21:15:06 +00005960 if (VIsual_active)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005961 {
5962 cap->arg = TRUE;
5963 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005964 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005965 nv_addsub(cap);
5966 }
5967 else
5968 clearopbeep(oap);
5969 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005971 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 case 'R':
5973 cap->arg = TRUE;
5974 nv_Replace(cap);
5975 break;
5976
5977 case 'r':
5978 nv_vreplace(cap);
5979 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980
5981 case '&':
5982 do_cmdline_cmd((char_u *)"%s//~/&");
5983 break;
5984
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005985 // "gv": Reselect the previous Visual area. If Visual already active,
5986 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005988 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005990
5991 // "gV": Don't reselect the previous Visual area after a Select mode
5992 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 case 'V':
5994 VIsual_reselect = FALSE;
5995 break;
5996
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005997 // "gh": start Select mode.
5998 // "gH": start Select line mode.
5999 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 case K_BS:
6001 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006002 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 case 'h':
6004 case 'H':
6005 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 cap->cmdchar = cap->nchar + ('v' - 'h');
6007 cap->arg = TRUE;
6008 nv_visual(cap);
6009 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02006010
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006011 // "gn", "gN" visually select next/previous search match
6012 // "gn" selects next match
6013 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02006014 case 'N':
6015 case 'n':
6016 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02006017 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02006018 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006020 // "gj" and "gk" two new funny movement keys -- up and down
6021 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 case 'j':
6023 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006024 // with 'nowrap' it works just like the normal "j" command.
6025 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 {
6027 oap->motion_type = MLINE;
6028 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
6029 }
6030 else
6031 i = nv_screengo(oap, FORWARD, cap->count1);
6032 if (i == FAIL)
6033 clearopbeep(oap);
6034 break;
6035
6036 case 'k':
6037 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006038 // with 'nowrap' it works just like the normal "k" command.
6039 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 {
6041 oap->motion_type = MLINE;
6042 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
6043 }
6044 else
6045 i = nv_screengo(oap, BACKWARD, cap->count1);
6046 if (i == FAIL)
6047 clearopbeep(oap);
6048 break;
6049
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006050 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 case 'J':
6052 nv_join(cap);
6053 break;
6054
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006055 // "g0", "g^" : Like "0" and "^" but for screen lines.
6056 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 case '0':
6059 case 'm':
6060 case K_HOME:
6061 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006062 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 break;
6064
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006065 case 'M':
6066 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006067 oap->motion_type = MCHAR;
6068 oap->inclusive = FALSE;
Dylan Thacker-Smith366c81a2024-03-24 09:46:56 +01006069 i = linetabsize_no_outer(curwin, curwin->w_cursor.lnum);
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006070 if (cap->count0 > 0 && cap->count0 <= 100)
6071 coladvance((colnr_T)(i * cap->count0 / 100));
6072 else
6073 coladvance((colnr_T)(i / 2));
6074 curwin->w_set_curswant = TRUE;
6075 }
6076 break;
6077
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006078 // "g_": to the last non-blank character in the line or <count> lines
6079 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006081 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 break;
6083
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006084 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 case '$':
6086 case K_END:
6087 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006088 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 break;
6090
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006091 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 case '*':
6093 case '#':
6094#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006095 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006097 case Ctrl_RSB: // :tag or :tselect for current identifier
6098 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 nv_ident(cap);
6100 break;
6101
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006102 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 case 'e':
6104 case 'E':
6105 oap->motion_type = MCHAR;
6106 curwin->w_set_curswant = TRUE;
6107 oap->inclusive = TRUE;
6108 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6109 clearopbeep(oap);
6110 break;
6111
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006112 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006114 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 break;
6116
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006117 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006119 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 break;
6121
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006122 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 case 'I':
6124 beginline(0);
6125 if (!checkclearopq(oap))
6126 invoke_edit(cap, FALSE, 'g', FALSE);
6127 break;
6128
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006129 // "gf": goto file, edit file under cursor
6130 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006132 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 nv_gotofile(cap);
6134 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006136 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 case '\'':
6138 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006139 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 case '`':
6141 nv_gomark(cap);
6142 break;
6143
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006144 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006146 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 break;
6148
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006149 // "ga": Display the ascii value of the character under the
6150 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 case 'a':
6152 do_ascii(NULL);
6153 break;
6154
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006155 // "g8": Display the bytes used for the UTF-8 character under the
6156 // cursor. It is displayed in hex.
6157 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006159 if (cap->count0 == 8)
6160 utf_find_illegal();
6161 else
6162 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006165 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006166 case '<':
6167 show_sb_text();
6168 break;
6169
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006170 // "gg": Goto the first line in file. With a count it goes to
6171 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 case 'g':
6173 cap->arg = FALSE;
6174 nv_goto(cap);
6175 break;
6176
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006177 // Two-character operators:
6178 // "gq" Format text
6179 // "gw" Format text and keep cursor position
6180 // "g~" Toggle the case of the text.
6181 // "gu" Change text to lower case.
6182 // "gU" Change text to upper case.
6183 // "g?" rot13 encoding
6184 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 case 'q':
6186 case 'w':
6187 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006188 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 case '~':
6190 case 'u':
6191 case 'U':
6192 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006193 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 nv_operator(cap);
6195 break;
6196
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006197 // "gd": Find first occurrence of pattern under the cursor in the
6198 // current function
6199 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 case 'd':
6201 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006202 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 break;
6204
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006205 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 case K_MIDDLEMOUSE:
6207 case K_MIDDLEDRAG:
6208 case K_MIDDLERELEASE:
6209 case K_LEFTMOUSE:
6210 case K_LEFTDRAG:
6211 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006212 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 case K_RIGHTMOUSE:
6214 case K_RIGHTDRAG:
6215 case K_RIGHTRELEASE:
6216 case K_X1MOUSE:
6217 case K_X1DRAG:
6218 case K_X1RELEASE:
6219 case K_X2MOUSE:
6220 case K_X2DRAG:
6221 case K_X2RELEASE:
6222 mod_mask = MOD_MASK_CTRL;
6223 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6224 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225
6226 case K_IGNORE:
6227 break;
6228
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006229 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 case 'p':
6231 case 'P':
6232 nv_put(cap);
6233 break;
6234
6235#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006236 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 case 'o':
6238 goto_byte(cap->count0);
6239 break;
6240#endif
6241
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006242 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006244 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 do_exmode(TRUE);
6246 break;
6247
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 case ',':
6249 nv_pcmark(cap);
6250 break;
6251
6252 case ';':
6253 cap->count1 = -cap->count1;
6254 nv_pcmark(cap);
6255 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006257 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006258 if (!checkclearop(oap))
6259 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006260 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006261 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006262 if (!checkclearop(oap))
6263 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006264 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006265
Bram Moolenaar62a23252020-08-09 14:04:42 +02006266 case TAB:
6267 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6268 clearopbeep(oap);
6269 break;
6270
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006271 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006272 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006273 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006274 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006275 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006276 break;
6277
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 default:
6279 clearopbeep(oap);
6280 break;
6281 }
6282}
6283
6284/*
6285 * Handle "o" and "O" commands.
6286 */
6287 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006288n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006290#ifdef FEAT_CONCEAL
6291 linenr_T oldline = curwin->w_cursor.lnum;
6292#endif
6293
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006294 if (checkclearopq(cap->oap))
6295 return;
6296
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297#ifdef FEAT_FOLDING
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006298 if (cap->cmdchar == 'O')
6299 // Open above the first line of a folded sequence of lines
6300 (void)hasFolding(curwin->w_cursor.lnum,
6301 &curwin->w_cursor.lnum, NULL);
6302 else
6303 // Open below the last line of a folded sequence of lines
6304 (void)hasFolding(curwin->w_cursor.lnum,
6305 NULL, &curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306#endif
Christian Brabandt4bca4892023-10-27 19:26:49 +02006307 // trigger TextChangedI for the 'o/O' command
6308 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006309 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6310 (cap->cmdchar == 'O' ? 1 : 0)),
6311 (linenr_T)(curwin->w_cursor.lnum +
6312 (cap->cmdchar == 'o' ? 1 : 0))
6313 ) == OK
6314 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
6315 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6316 0, NULL) == OK)
6317 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006318#ifdef FEAT_CONCEAL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006319 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
6320 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006321#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006322#ifdef FEAT_SYN_HL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006323 if (curwin->w_p_cul)
6324 // force redraw of cursorline
6325 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006326#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006327 // When '#' is in 'cpoptions' ignore the count.
6328 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6329 cap->count1 = 1;
6330 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 }
6332}
6333
6334/*
6335 * "." command: redo last change.
6336 */
6337 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006338nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006340 if (checkclearopq(cap->oap))
6341 return;
6342
6343 // If "restart_edit" is TRUE, the last but one command is repeated
6344 // instead of the last command (inserting text). This is used for
6345 // CTRL-O <.> in insert mode.
6346 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6347 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348}
6349
6350/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006351 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 */
6353 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006354nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006356 if (VIsual_select && VIsual_active)
6357 {
6358 int reg;
6359 // Get register name
6360 ++no_mapping;
6361 ++allow_keys;
6362 reg = plain_vgetc();
6363 LANGMAP_ADJUST(reg, TRUE);
6364 --no_mapping;
6365 --allow_keys;
6366
6367 if (reg == '"')
6368 // the unnamed register is 0
6369 reg = 0;
6370
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006371 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006372 return;
6373 }
6374
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006375 if (checkclearopq(cap->oap))
6376 return;
6377
6378 u_redo((int)cap->count1);
6379 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380}
6381
6382/*
6383 * Handle "U" command.
6384 */
6385 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006386nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006388 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006389 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006391 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 cap->cmdchar = 'g';
6393 cap->nchar = 'U';
6394 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006395 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006397
6398 if (checkclearopq(cap->oap))
6399 return;
6400
6401 u_undoline();
6402 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403}
6404
6405/*
6406 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6407 * single character.
6408 */
6409 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006410nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006412 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006413 {
6414#ifdef FEAT_JOB_CHANNEL
6415 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6416 {
6417 clearopbeep(cap->oap);
6418 return;
6419 }
6420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 else
6424 nv_operator(cap);
6425}
6426
6427/*
6428 * Handle an operator command.
6429 * The actual work is done by do_pending_operator().
6430 */
6431 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006432nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433{
6434 int op_type;
6435
6436 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006437#ifdef FEAT_JOB_CHANNEL
6438 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6439 {
6440 clearopbeep(cap->oap);
6441 return;
6442 }
6443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006445 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 nv_lineop(cap);
6447 else if (!checkclearop(cap->oap))
6448 {
6449 cap->oap->start = curwin->w_cursor;
6450 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006451#ifdef FEAT_EVAL
6452 set_op_var(op_type);
6453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 }
6455}
6456
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006457#ifdef FEAT_EVAL
6458/*
6459 * Set v:operator to the characters for "optype".
6460 */
6461 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006462set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006463{
6464 char_u opchars[3];
6465
6466 if (optype == OP_NOP)
6467 set_vim_var_string(VV_OP, NULL, 0);
6468 else
6469 {
6470 opchars[0] = get_op_char(optype);
6471 opchars[1] = get_extra_op_char(optype);
6472 opchars[2] = NUL;
6473 set_vim_var_string(VV_OP, opchars, -1);
6474 }
6475}
6476#endif
6477
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478/*
6479 * Handle linewise operator "dd", "yy", etc.
6480 *
6481 * "_" is is a strange motion command that helps make operators more logical.
6482 * It is actually implemented, but not documented in the real Vi. This motion
6483 * command actually refers to "the current line". Commands like "dd" and "yy"
6484 * are really an alternate form of "d_" and "y_". It does accept a count, so
6485 * "d3_" works to delete 3 lines.
6486 */
6487 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006488nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489{
6490 cap->oap->motion_type = MLINE;
6491 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6492 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006493 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006494 && cap->oap->motion_force != 'v'
6495 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 || cap->oap->op_type == OP_LSHIFT
6497 || cap->oap->op_type == OP_RSHIFT)
6498 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006499 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 beginline(BL_WHITE | BL_FIX);
6501}
6502
6503/*
6504 * <Home> command.
6505 */
6506 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006507nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006509 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006510 if (mod_mask & MOD_MASK_CTRL)
6511 nv_goto(cap);
6512 else
6513 {
6514 cap->count0 = 1;
6515 nv_pipe(cap);
6516 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006517 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6518 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519}
6520
6521/*
6522 * "|" command.
6523 */
6524 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006525nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526{
6527 cap->oap->motion_type = MCHAR;
6528 cap->oap->inclusive = FALSE;
6529 beginline(0);
6530 if (cap->count0 > 0)
6531 {
6532 coladvance((colnr_T)(cap->count0 - 1));
6533 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6534 }
6535 else
6536 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006537 // keep curswant at the column where we wanted to go, not where
6538 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 curwin->w_set_curswant = FALSE;
6540}
6541
6542/*
6543 * Handle back-word command "b" and "B".
6544 * cap->arg is 1 for "B"
6545 */
6546 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006547nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548{
6549 cap->oap->motion_type = MCHAR;
6550 cap->oap->inclusive = FALSE;
6551 curwin->w_set_curswant = TRUE;
6552 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6553 clearopbeep(cap->oap);
6554#ifdef FEAT_FOLDING
6555 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6556 foldOpenCursor();
6557#endif
6558}
6559
6560/*
6561 * Handle word motion commands "e", "E", "w" and "W".
6562 * cap->arg is TRUE for "E" and "W".
6563 */
6564 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006565nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566{
6567 int n;
6568 int word_end;
6569 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006570 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006572 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6574 word_end = TRUE;
6575 else
6576 word_end = FALSE;
6577 cap->oap->inclusive = word_end;
6578
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006579 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 if (!word_end && cap->oap->op_type == OP_CHANGE)
6581 {
6582 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006583 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006585 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006587 // Reproduce a funny Vi behaviour: "cw" on a blank only
6588 // changes one character, not all blanks until the start of
6589 // the next word. Only do this when the 'w' flag is included
6590 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6592 {
6593 cap->oap->inclusive = TRUE;
6594 cap->oap->motion_type = MCHAR;
6595 return;
6596 }
6597 }
6598 else
6599 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006600 // This is a little strange. To match what the real Vi does,
6601 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6602 // that we are not on a space or a TAB. This seems impolite
6603 // at first, but it's really more what we mean when we say
6604 // 'cw'.
6605 // Another strangeness: When standing on the end of a word
6606 // "ce" will change until the end of the next word, but "cw"
6607 // will change only one character! This is done by setting
6608 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 cap->oap->inclusive = TRUE;
6610 word_end = TRUE;
6611 flag = TRUE;
6612 }
6613 }
6614 }
6615
6616 cap->oap->motion_type = MCHAR;
6617 curwin->w_set_curswant = TRUE;
6618 if (word_end)
6619 n = end_word(cap->count1, cap->arg, flag, FALSE);
6620 else
6621 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6622
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006623 // Don't leave the cursor on the NUL past the end of line. Unless we
6624 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006625 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006626 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627
6628 if (n == FAIL && cap->oap->op_type == OP_NOP)
6629 clearopbeep(cap->oap);
6630 else
6631 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633#ifdef FEAT_FOLDING
6634 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6635 foldOpenCursor();
6636#endif
6637 }
6638}
6639
6640/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006641 * Used after a movement command: If the cursor ends up on the NUL after the
6642 * end of the line, may move it back to the last character and make the motion
6643 * inclusive.
6644 */
6645 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006646adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006647{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006648 // The cursor cannot remain on the NUL when:
6649 // - the column is > 0
6650 // - not in Visual mode or 'selection' is "o"
6651 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006652 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006653 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006654 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006655 {
6656 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006657 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006658 if (has_mbyte)
6659 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006660 oap->inclusive = TRUE;
6661 }
6662}
6663
6664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 * "0" and "^" commands.
6666 * cap->arg is the argument for beginline().
6667 */
6668 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006669nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670{
6671 cap->oap->motion_type = MCHAR;
6672 cap->oap->inclusive = FALSE;
6673 beginline(cap->arg);
6674#ifdef FEAT_FOLDING
6675 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6676 foldOpenCursor();
6677#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006678 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6679 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680}
6681
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682/*
6683 * In exclusive Visual mode, may include the last character.
6684 */
6685 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006686adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687{
6688 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006689 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 if (has_mbyte)
6692 inc_cursor();
6693 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 ++curwin->w_cursor.col;
6695 cap->oap->inclusive = FALSE;
6696 }
6697}
6698
6699/*
6700 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6701 * Should check VIsual_mode before calling this.
6702 * Returns TRUE when backed up to the previous line.
6703 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006704 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006705unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706{
6707 pos_T *pp;
6708
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006709 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006711 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 pp = &curwin->w_cursor;
6713 else
6714 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 if (pp->coladd > 0)
6716 --pp->coladd;
6717 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 if (pp->col > 0)
6719 {
6720 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006721 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 }
6723 else if (pp->lnum > 1)
6724 {
6725 --pp->lnum;
John Marriott02d7a6c2024-02-26 21:21:17 +01006726 pp->col = ml_get_len(pp->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 return TRUE;
6728 }
6729 }
6730 return FALSE;
6731}
6732
6733/*
6734 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6735 */
6736 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006737nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738{
6739 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006740 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006742 VIsual_select_reg = 0;
6743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 else if (VIsual_reselect)
6745 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006746 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 cap->arg = TRUE;
6748 nv_g_cmd(cap);
6749 }
6750}
6751
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752
6753/*
6754 * "G", "gg", CTRL-END, CTRL-HOME.
6755 * cap->arg is TRUE for "G".
6756 */
6757 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006758nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759{
6760 linenr_T lnum;
6761
6762 if (cap->arg)
6763 lnum = curbuf->b_ml.ml_line_count;
6764 else
6765 lnum = 1L;
6766 cap->oap->motion_type = MLINE;
6767 setpcmark();
6768
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006769 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 if (cap->count0 != 0)
6771 lnum = cap->count0;
6772 if (lnum < 1L)
6773 lnum = 1L;
6774 else if (lnum > curbuf->b_ml.ml_line_count)
6775 lnum = curbuf->b_ml.ml_line_count;
6776 curwin->w_cursor.lnum = lnum;
6777 beginline(BL_SOL | BL_FIX);
6778#ifdef FEAT_FOLDING
6779 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6780 foldOpenCursor();
6781#endif
6782}
6783
6784/*
6785 * CTRL-\ in Normal mode.
6786 */
6787 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006788nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789{
6790 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6791 {
6792 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006793 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006794 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 if (cmdwin_type != 0)
6797 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 if (VIsual_active)
6799 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006800 end_visual_mode(); // stop Visual
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006801 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006803 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 if (cap->nchar == Ctrl_G && p_im)
6805 restart_edit = 'a';
6806 }
6807 else
6808 clearopbeep(cap->oap);
6809}
6810
6811/*
6812 * ESC in Normal mode: beep, but don't flush buffers.
6813 * Don't even beep if we are canceling a command.
6814 */
6815 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006816nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817{
6818 int no_reason;
6819
6820 no_reason = (cap->oap->op_type == OP_NOP
6821 && cap->opcount == 0
6822 && cap->count0 == 0
6823 && cap->oap->regname == 0
6824 && !p_im);
6825
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006826 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006828 if (restart_edit == 0 && cmdwin_type == 0
6829 && !VIsual_active && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006830 {
Bram Moolenaarf7e73022022-09-24 13:10:04 +01006831 int out_redir = !stdout_isatty && !is_not_a_term_or_gui();
6832
Bram Moolenaar5939c352022-09-24 12:50:45 +01006833 // The user may accidentally do "vim file | grep word" and then
6834 // CTRL-C doesn't show anything. With a changed buffer give the
6835 // message on stderr. Without any changes might as well exit.
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006836 if (anyBufIsChanged())
Bram Moolenaar5939c352022-09-24 12:50:45 +01006837 {
6838 char *ms = _("Type :qa! and press <Enter> to abandon all changes and exit Vim");
6839
6840 if (out_redir)
6841 mch_errmsg(ms);
6842 else
6843 msg(ms);
6844 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006845 else
Bram Moolenaar5939c352022-09-24 12:50:45 +01006846 {
6847 if (out_redir)
6848 {
6849 got_int = FALSE;
6850 do_cmdline_cmd((char_u *)"qa");
6851 }
6852 else
6853 msg(_("Type :qa and press <Enter> to exit Vim"));
6854 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01006857 if (restart_edit != 0)
6858 redraw_mode = TRUE; // remove "-- (insert) --"
6859
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006860 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6861 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 if (!p_im)
6863 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 if (cmdwin_type != 0)
6865 {
6866 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006867 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 return;
6869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 }
Bram Moolenaar8d696372022-08-21 10:40:07 +01006871 else if (cmdwin_type != 0 && ex_normal_busy && typebuf_was_empty)
Bram Moolenaar7d414102021-02-23 19:39:20 +01006872 {
6873 // When :normal runs out of characters while in the command line window
Bram Moolenaar8d696372022-08-21 10:40:07 +01006874 // vgetorpeek() will repeatedly return ESC. Exit the cmdline window to
6875 // break the loop.
Bram Moolenaar7d414102021-02-23 19:39:20 +01006876 cmdwin_result = K_IGNORE;
6877 return;
6878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 if (VIsual_active)
6881 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006882 end_visual_mode(); // stop Visual
6883 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006885 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006887 else if (no_reason)
Bram Moolenaarb849c822022-08-28 22:46:21 +01006888 {
6889#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar98d10692022-08-29 00:08:39 +01006890 if (!cap->arg && popup_message_win_visible())
Bram Moolenaarb849c822022-08-28 22:46:21 +01006891 popup_hide_message_win();
6892 else
6893#endif
6894 vim_beep(BO_ESC);
6895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 clearop(cap->oap);
6897
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006898 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6899 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006900 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 restart_edit = 'a';
6902}
6903
6904/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006905 * Move the cursor for the "A" command.
6906 */
6907 void
6908set_cursor_for_append_to_line(void)
6909{
6910 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006911 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006912 {
6913 int save_State = State;
6914
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006915 // Pretend Insert mode here to allow the cursor on the
6916 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006917 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006918 coladvance((colnr_T)MAXCOL);
6919 State = save_State;
6920 }
6921 else
6922 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6923}
6924
6925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006927 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 */
6929 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006930nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006932 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6934 cap->cmdchar = 'i';
6935
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006936 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006938 {
6939#ifdef FEAT_TERMINAL
6940 if (term_in_normal_mode())
6941 {
6942 end_visual_mode();
6943 clearop(cap->oap);
6944 term_enter_job_mode();
6945 return;
6946 }
6947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006951 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006952 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6953 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 nv_object(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006957#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006958 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006959 {
6960 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006961 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006962 return;
6963 }
6964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 else if (!curbuf->b_p_ma && !p_im)
6966 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006967 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006968 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006970 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006971 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006972 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006974 else if (cap->cmdchar == K_PS && VIsual_active)
6975 {
6976 pos_T old_pos = curwin->w_cursor;
6977 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006978 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006979
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006980 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006981 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6982 {
6983 shift_delete_registers();
6984 cap->oap->regname = '1';
6985 }
6986 else
6987 cap->oap->regname = '-';
6988 cap->cmdchar = 'd';
6989 cap->nchar = NUL;
6990 nv_operator(cap);
6991 do_pending_operator(cap, 0, FALSE);
6992 cap->cmdchar = K_PS;
6993
zeertzjq7a732522022-03-14 20:46:41 +00006994 if (*ml_get_cursor() != NUL)
6995 {
6996 if (old_visual_mode == 'V')
6997 {
6998 // In linewise Visual mode insert before the beginning of the
6999 // next line.
7000 // When the last line in the buffer was deleted then create a
7001 // new line, otherwise there is not need to move cursor.
7002 // Detect this by checking if cursor moved above Visual area.
7003 if (curwin->w_cursor.lnum < old_pos.lnum
7004 && curwin->w_cursor.lnum < old_visual.lnum)
7005 {
7006 if (u_save_cursor() == OK)
7007 {
7008 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
7009 FALSE);
7010 appended_lines(curwin->w_cursor.lnum++, 1L);
7011 }
7012 }
7013 }
7014 // When the last char in the line was deleted then append.
7015 // Detect this by checking if cursor moved before Visual area.
7016 else if (curwin->w_cursor.col < old_pos.col
7017 && curwin->w_cursor.col < old_visual.col)
7018 inc_cursor();
7019 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007020
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007021 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01007022 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 else if (!checkclearopq(cap->oap))
7025 {
7026 switch (cap->cmdchar)
7027 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007028 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007029 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 break;
7031
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007032 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007033 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7034 beginline(BL_WHITE);
7035 else
7036 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 break;
7038
Bram Moolenaara1891842017-02-04 21:34:31 +01007039 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007040 // Bracketed paste works like "a"ppend, unless the cursor is in
7041 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007042 if (curwin->w_cursor.col == 0)
7043 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007044 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007045
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007046 case 'a': // "a"ppend is like "i"nsert on the next character.
7047 // increment coladd when in virtual space, increment the
7048 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 if (virtual_active()
7050 && (curwin->w_cursor.coladd > 0
7051 || *ml_get_cursor() == NUL
7052 || *ml_get_cursor() == TAB))
7053 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007054 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 inc_cursor();
7056 break;
7057 }
7058
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7060 {
7061 int save_State = State;
7062
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007063 // Pretend Insert mode here to allow the cursor on the
7064 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01007065 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 coladvance(getviscol());
7067 State = save_State;
7068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069
7070 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7071 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007072 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007073 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007074 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075}
7076
7077/*
7078 * Invoke edit() and take care of "restart_edit" and the return value.
7079 */
7080 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007081invoke_edit(
7082 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007083 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007084 int cmd,
7085 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086{
7087 int restart_edit_save = 0;
7088
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007089 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7090 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7091 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 if (repl || !stuff_empty())
7093 restart_edit_save = restart_edit;
7094 else
7095 restart_edit_save = 0;
7096
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007097 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 restart_edit = 0;
7099
Christian Brabandt4bca4892023-10-27 19:26:49 +02007100 // Reset Changedtick_i, so that TextChangedI will only be triggered for stuff
7101 // from insert mode, for 'o/O' this has already been done in n_opencmd
7102 if (cap->cmdchar != 'O' && cap->cmdchar != 'o')
7103 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 if (edit(cmd, startln, cap->count1))
7105 cap->retval |= CA_COMMAND_BUSY;
7106
7107 if (restart_edit == 0)
7108 restart_edit = restart_edit_save;
7109}
7110
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111/*
7112 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7113 */
7114 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007115nv_object(
7116 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117{
7118 int flag;
7119 int include;
7120 char_u *mps_save;
7121
7122 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007123 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007125 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007127 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 mps_save = curbuf->b_p_mps;
7129 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7130
7131 switch (cap->nchar)
7132 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007133 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 flag = current_word(cap->oap, cap->count1, include, FALSE);
7135 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007136 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 flag = current_word(cap->oap, cap->count1, include, TRUE);
7138 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007139 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 case '(':
7141 case ')':
7142 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7143 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007144 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 case '{':
7146 case '}':
7147 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7148 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007149 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 case ']':
7151 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7152 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007153 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 case '>':
7155 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7156 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007157#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007158 case 't': // "at" = a tag block (xml and html)
7159 // Do not adjust oap->end in do_pending_operator()
7160 // otherwise there are different results for 'dit'
7161 // (note leading whitespace in last line):
7162 // 1) <b> 2) <b>
7163 // foobar foobar
7164 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007165 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007166 flag = current_tagblock(cap->oap, cap->count1, include);
7167 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007168#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007169 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 flag = current_par(cap->oap, cap->count1, include, 'p');
7171 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007172 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 flag = current_sent(cap->oap, cap->count1, include);
7174 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007175 case '"': // "a"" = a double quoted string
7176 case '\'': // "a'" = a single quoted string
7177 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007178 flag = current_quote(cap->oap, cap->count1, include,
7179 cap->nchar);
7180 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007181#if 0 // TODO
7182 case 'S': // "aS" = a section
7183 case 'f': // "af" = a filename
7184 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185#endif
7186 default:
7187 flag = FAIL;
7188 break;
7189 }
7190
7191 curbuf->b_p_mps = mps_save;
7192 if (flag == FAIL)
7193 clearopbeep(cap->oap);
7194 adjust_cursor_col();
7195 curwin->w_set_curswant = TRUE;
7196}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197
7198/*
7199 * "q" command: Start/stop recording.
7200 * "q:", "q/", "q?": edit command-line in command-line window.
7201 */
7202 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007203nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204{
7205 if (cap->oap->op_type == OP_FORMAT)
7206 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007207 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 cap->cmdchar = 'g';
7209 cap->nchar = 'q';
7210 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007211 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007213
7214 if (checkclearop(cap->oap))
7215 return;
7216
7217 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007219 if (cmdwin_type != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007221 emsg(_(e_cmdline_window_already_open));
7222 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007224 stuffcharReadbuff(cap->nchar);
7225 stuffcharReadbuff(K_CMDWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007227 else
7228 // (stop) recording into a named register, unless executing a
7229 // register
7230 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
7231 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232}
7233
7234/*
7235 * Handle the "@r" command.
7236 */
7237 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007238nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239{
7240 if (checkclearop(cap->oap))
7241 return;
7242#ifdef FEAT_EVAL
7243 if (cap->nchar == '=')
7244 {
7245 if (get_expr_register() == NUL)
7246 return;
7247 }
7248#endif
7249 while (cap->count1-- && !got_int)
7250 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007251 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 {
7253 clearopbeep(cap->oap);
7254 break;
7255 }
7256 line_breakcheck();
7257 }
7258}
7259
7260/*
7261 * Handle the CTRL-U and CTRL-D commands.
7262 */
7263 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007264nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265{
7266 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7267 || (cap->cmdchar == Ctrl_D
7268 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7269 clearopbeep(cap->oap);
7270 else if (!checkclearop(cap->oap))
7271 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7272}
7273
7274/*
7275 * Handle "J" or "gJ" command.
7276 */
7277 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007278nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007280 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007282 nv_operator(cap);
7283 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007285
7286 if (checkclearop(cap->oap))
7287 return;
7288
7289 if (cap->count0 <= 1)
7290 cap->count0 = 2; // default for join is two lines!
7291 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7292 curbuf->b_ml.ml_line_count)
7293 {
7294 // can't join when on the last line
7295 if (cap->count0 <= 2)
7296 {
7297 clearopbeep(cap->oap);
7298 return;
7299 }
7300 cap->count0 = curbuf->b_ml.ml_line_count
7301 - curwin->w_cursor.lnum + 1;
7302 }
7303
7304 prep_redo(cap->oap->regname, cap->count0,
7305 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7306 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307}
7308
7309/*
7310 * "P", "gP", "p" and "gp" commands.
7311 */
7312 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007313nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007315 nv_put_opt(cap, FALSE);
7316}
7317
7318/*
7319 * "P", "gP", "p" and "gp" commands.
7320 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7321 */
7322 static void
7323nv_put_opt(cmdarg_T *cap, int fix_indent)
7324{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 int regname = 0;
7326 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007327 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007328 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 int dir;
7330 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007331 int keep_registers = FALSE;
zeertzjq4e141c62024-02-28 21:49:51 +01007332#ifdef FEAT_FOLDING
7333 int save_fen = curwin->w_p_fen;
7334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335
7336 if (cap->oap->op_type != OP_NOP)
7337 {
7338#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007339 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7341 {
7342 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007343 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 }
7345 else
7346#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007347 clearopbeep(cap->oap);
7348 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007350
Bram Moolenaarf2732452018-06-03 14:47:35 +02007351#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007352 if (bt_prompt(curbuf) && !prompt_curpos_editable())
Bram Moolenaarf2732452018-06-03 14:47:35 +02007353 {
7354 clearopbeep(cap->oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007355 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02007356 }
7357#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007358
7359 if (fix_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007361 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7362 ? FORWARD : BACKWARD;
7363 flags |= PUT_FIXINDENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007365 else
7366 dir = (cap->cmdchar == 'P'
7367 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7368 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
7369 prep_redo_cmd(cap);
7370 if (cap->cmdchar == 'g')
7371 flags |= PUT_CURSEND;
7372 else if (cap->cmdchar == 'z')
7373 flags |= PUT_BLOCK_INNER;
7374
7375 if (VIsual_active)
7376 {
7377 // Putting in Visual mode: The put text replaces the selected
7378 // text. First delete the selected text, then put the new text.
7379 // Need to save and restore the registers that the delete
7380 // overwrites if the old contents is being put.
7381 was_visual = TRUE;
7382 regname = cap->oap->regname;
7383 keep_registers = cap->cmdchar == 'P';
7384#ifdef FEAT_CLIPBOARD
7385 adjust_clip_reg(&regname);
7386#endif
7387 if (regname == 0 || regname == '"'
7388 || VIM_ISDIGIT(regname) || regname == '-'
7389#ifdef FEAT_CLIPBOARD
7390 || (clip_unnamed && (regname == '*' || regname == '+'))
7391#endif
7392
7393 )
7394 {
7395 // The delete is going to overwrite the register we want to
7396 // put, save it first.
7397 reg1 = get_register(regname, TRUE);
7398 }
7399
zeertzjq4e141c62024-02-28 21:49:51 +01007400#ifdef FEAT_FOLDING
7401 // Temporarily disable folding, as deleting a fold marker may cause
7402 // the cursor to be included in a fold.
7403 curwin->w_p_fen = FALSE;
7404#endif
7405
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007406 // Now delete the selected text. Avoid messages here.
7407 cap->cmdchar = 'd';
7408 cap->nchar = NUL;
7409 cap->oap->regname = keep_registers ? '_' : NUL;
7410 ++msg_silent;
7411 nv_operator(cap);
7412 do_pending_operator(cap, 0, FALSE);
7413 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7414 --msg_silent;
7415
7416 // delete PUT_LINE_BACKWARD;
7417 cap->oap->regname = regname;
7418
7419 if (reg1 != NULL)
7420 {
7421 // Delete probably changed the register we want to put, save
7422 // it first. Then put back what was there before the delete.
7423 reg2 = get_register(regname, FALSE);
7424 put_register(regname, reg1);
7425 }
7426
7427 // When deleted a linewise Visual area, put the register as
7428 // lines to avoid it joined with the next line. When deletion was
7429 // characterwise, split a line when putting lines.
7430 if (VIsual_mode == 'V')
7431 flags |= PUT_LINE;
7432 else if (VIsual_mode == 'v')
7433 flags |= PUT_LINE_SPLIT;
7434 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7435 flags |= PUT_LINE_FORWARD;
7436 dir = BACKWARD;
7437 if ((VIsual_mode != 'V'
7438 && curwin->w_cursor.col < curbuf->b_op_start.col)
7439 || (VIsual_mode == 'V'
7440 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
7441 // cursor is at the end of the line or end of file, put
7442 // forward.
7443 dir = FORWARD;
7444 // May have been reset in do_put().
7445 VIsual_active = TRUE;
7446 }
7447 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
7448
7449 // If a register was saved, put it back now.
7450 if (reg2 != NULL)
7451 put_register(regname, reg2);
7452
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007453 if (was_visual)
7454 {
zeertzjq4e141c62024-02-28 21:49:51 +01007455#ifdef FEAT_FOLDING
7456 if (save_fen)
7457 curwin->w_p_fen = TRUE;
7458#endif
7459 // What to reselect with "gv"? Selecting the just put text seems to
7460 // be the most useful, since the original text was removed.
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007461 curbuf->b_visual.vi_start = curbuf->b_op_start;
7462 curbuf->b_visual.vi_end = curbuf->b_op_end;
7463 // need to adjust cursor position
7464 if (*p_sel == 'e')
7465 inc(&curbuf->b_visual.vi_end);
7466 }
7467
7468 // When all lines were selected and deleted do_put() leaves an empty
7469 // line that needs to be deleted now.
7470 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
7471 {
7472 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
7473 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
7474
7475 // If the cursor was in that line, move it to the end of the last
7476 // line.
7477 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7478 {
7479 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7480 coladvance((colnr_T)MAXCOL);
7481 }
7482 }
7483 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484}
7485
7486/*
7487 * "o" and "O" commands.
7488 */
7489 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007490nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491{
7492#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007493 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7495 {
7496 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007497 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 }
7499 else
7500#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007501 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007503#ifdef FEAT_JOB_CHANNEL
7504 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007505 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 n_opencmd(cap);
7509}
7510
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511#ifdef FEAT_NETBEANS_INTG
7512 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007513nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514{
7515 netbeans_keycommand(cap->nchar);
7516}
7517#endif
7518
7519#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007521nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007523 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524}
7525#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007526
Bram Moolenaar3918c952005-03-15 22:34:55 +00007527/*
7528 * Trigger CursorHold event.
7529 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7530 * input buffer. "did_cursorhold" is set to avoid retriggering.
7531 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007532 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007533nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007534{
7535 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7536 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007537 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007538}