blob: 6cf2b9d2f3118abe9f2d2558b76c428bd15b7d04 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
ichizok672776d2022-01-31 12:27:18 +00003 * VIM - Vi IMproved by Bram Moolenaar et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
12 * the operators.
13 */
14
15#include "vim.h"
16
Bram Moolenaar792cf5e2019-09-30 23:12:16 +020017static int VIsual_mode_orig = NUL; // saved Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +000018
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010019#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010020static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount);
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010021#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static void unshift_special(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010023static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25/*
26 * nv_*(): functions called to handle Normal and Visual mode commands.
27 * n_*(): functions called to handle Normal mode commands.
28 * v_*(): functions called to handle Visual mode commands.
29 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010030static void nv_ignore(cmdarg_T *cap);
31static void nv_nop(cmdarg_T *cap);
32static void nv_error(cmdarg_T *cap);
33static void nv_help(cmdarg_T *cap);
34static void nv_addsub(cmdarg_T *cap);
35static void nv_page(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010036static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010038static void nv_ver_scrollbar(cmdarg_T *cap);
39static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000041#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void nv_tabline(cmdarg_T *cap);
43static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000044#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010045static void nv_exmode(cmdarg_T *cap);
46static void nv_colon(cmdarg_T *cap);
47static void nv_ctrlg(cmdarg_T *cap);
48static void nv_ctrlh(cmdarg_T *cap);
49static void nv_clear(cmdarg_T *cap);
50static void nv_ctrlo(cmdarg_T *cap);
51static void nv_hat(cmdarg_T *cap);
52static void nv_Zet(cmdarg_T *cap);
53static void nv_ident(cmdarg_T *cap);
54static void nv_tagpop(cmdarg_T *cap);
55static void nv_scroll(cmdarg_T *cap);
56static void nv_right(cmdarg_T *cap);
57static void nv_left(cmdarg_T *cap);
58static void nv_up(cmdarg_T *cap);
59static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010060static void nv_end(cmdarg_T *cap);
61static void nv_dollar(cmdarg_T *cap);
62static void nv_search(cmdarg_T *cap);
63static void nv_next(cmdarg_T *cap);
John Marriott8c85a2a2024-05-20 19:18:26 +020064static int normal_search(cmdarg_T *cap, int dir, char_u *pat, size_t patlen, int opt, int *wrapped);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010065static void nv_csearch(cmdarg_T *cap);
66static void nv_brackets(cmdarg_T *cap);
67static void nv_percent(cmdarg_T *cap);
68static void nv_brace(cmdarg_T *cap);
69static void nv_mark(cmdarg_T *cap);
70static void nv_findpar(cmdarg_T *cap);
71static void nv_undo(cmdarg_T *cap);
72static void nv_kundo(cmdarg_T *cap);
73static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010074static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010075static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
76static void v_visop(cmdarg_T *cap);
77static void nv_subst(cmdarg_T *cap);
78static void nv_abbrev(cmdarg_T *cap);
79static void nv_optrans(cmdarg_T *cap);
80static void nv_gomark(cmdarg_T *cap);
81static void nv_pcmark(cmdarg_T *cap);
82static void nv_regname(cmdarg_T *cap);
83static void nv_visual(cmdarg_T *cap);
84static void n_start_visual_mode(int c);
85static void nv_window(cmdarg_T *cap);
86static void nv_suspend(cmdarg_T *cap);
87static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010088static void nv_dot(cmdarg_T *cap);
Shougo Matsushita4ede01f2022-01-20 15:26:03 +000089static void nv_redo_or_register(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010090static void nv_Undo(cmdarg_T *cap);
91static void nv_tilde(cmdarg_T *cap);
92static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000093#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010094static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000095#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010096static void nv_lineop(cmdarg_T *cap);
97static void nv_home(cmdarg_T *cap);
98static void nv_pipe(cmdarg_T *cap);
99static void nv_bck_word(cmdarg_T *cap);
100static void nv_wordcmd(cmdarg_T *cap);
101static void nv_beginline(cmdarg_T *cap);
102static void adjust_cursor(oparg_T *oap);
103static void adjust_for_sel(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100104static void nv_select(cmdarg_T *cap);
105static void nv_goto(cmdarg_T *cap);
106static void nv_normal(cmdarg_T *cap);
107static void nv_esc(cmdarg_T *oap);
108static void nv_edit(cmdarg_T *cap);
109static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100110static void nv_object(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100111static void nv_record(cmdarg_T *cap);
112static void nv_at(cmdarg_T *cap);
113static void nv_halfpage(cmdarg_T *cap);
114static void nv_join(cmdarg_T *cap);
115static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200116static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100117static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100119static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100122static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100124static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125
ichizok672776d2022-01-31 12:27:18 +0000126// Declare nv_cmds[].
127#define DO_DECLARE_NVCMD
128#include "nv_cmds.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Yegappan Lakshmanan4dc0dd82022-01-29 13:06:40 +0000130// Include the lookuptable generated by create_nvcmdidx.vim.
131#include "nv_cmdidxs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133/*
134 * Search for a command in the commands table.
135 * Returns -1 for invalid command.
136 */
137 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100138find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139{
140 int i;
141 int idx;
142 int top, bot;
143 int c;
144
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100145 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 if (cmdchar >= 0x100)
147 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100149 // We use the absolute value of the character. Special keys have a
150 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 if (cmdchar < 0)
152 cmdchar = -cmdchar;
153
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100154 // If the character is in the first part: The character is the index into
155 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 if (cmdchar <= nv_max_linear)
157 return nv_cmd_idx[cmdchar];
158
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100159 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 bot = nv_max_linear + 1;
161 top = NV_CMDS_SIZE - 1;
162 idx = -1;
163 while (bot <= top)
164 {
165 i = (top + bot) / 2;
166 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
167 if (c < 0)
168 c = -c;
169 if (cmdchar == c)
170 {
171 idx = nv_cmd_idx[i];
172 break;
173 }
174 if (cmdchar > c)
175 bot = i + 1;
176 else
177 top = i - 1;
178 }
179 return idx;
180}
181
182/*
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100183 * If currently editing a cmdline or text is locked: beep and give an error
184 * message, return TRUE.
185 */
186 static int
187check_text_locked(oparg_T *oap)
188{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000189 if (!text_locked())
190 return FALSE;
191
192 if (oap != NULL)
193 clearopbeep(oap);
194 text_locked_msg();
195 return TRUE;
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100196}
197
198/*
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000199 * If text is locked, "curbuf_lock" or "allbuf_lock" is set:
200 * Give an error message, possibly beep and return TRUE.
201 * "oap" may be NULL.
202 */
203 int
204check_text_or_curbuf_locked(oparg_T *oap)
205{
206 if (check_text_locked(oap))
207 return TRUE;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000208
209 if (!curbuf_locked())
210 return FALSE;
211
212 if (oap != NULL)
213 clearop(oap);
214 return TRUE;
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000215}
216
217/*
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000218 * Handle the count before a normal command and set cap->count0.
219 */
220 static int
221normal_cmd_get_count(
222 cmdarg_T *cap,
223 int c,
224 int toplevel UNUSED,
225 int set_prevcount UNUSED,
226 int *ctrl_w,
227 int *need_flushbuf UNUSED)
228{
229getcount:
230 if (!(VIsual_active && VIsual_select))
231 {
232 // Handle a count before a command and compute ca.count0.
233 // Note that '0' is a command and not the start of a count, but it's
234 // part of a count after other digits.
235 while ((c >= '1' && c <= '9')
236 || (cap->count0 != 0 && (c == K_DEL || c == K_KDEL
237 || c == '0')))
238 {
239 if (c == K_DEL || c == K_KDEL)
240 {
241 cap->count0 /= 10;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000242 del_from_showcmd(4); // delete the digit and ~@%
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000243 }
244 else if (cap->count0 > 99999999L)
245 {
246 cap->count0 = 999999999L;
247 }
248 else
249 {
250 cap->count0 = cap->count0 * 10 + (c - '0');
251 }
252#ifdef FEAT_EVAL
253 // Set v:count here, when called from main() and not a stuffed
254 // command, so that v:count can be used in an expression mapping
255 // right after the count. Do set it for redo.
256 if (toplevel && readbuf1_empty())
257 set_vcount_ca(cap, &set_prevcount);
258#endif
259 if (*ctrl_w)
260 {
261 ++no_mapping;
262 ++allow_keys; // no mapping for nchar, but keys
263 }
264 ++no_zero_mapping; // don't map zero here
265 c = plain_vgetc();
266 LANGMAP_ADJUST(c, TRUE);
267 --no_zero_mapping;
268 if (*ctrl_w)
269 {
270 --no_mapping;
271 --allow_keys;
272 }
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000273 *need_flushbuf |= add_to_showcmd(c);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000274 }
275
276 // If we got CTRL-W there may be a/another count
277 if (c == Ctrl_W && !*ctrl_w && cap->oap->op_type == OP_NOP)
278 {
279 *ctrl_w = TRUE;
280 cap->opcount = cap->count0; // remember first count
281 cap->count0 = 0;
282 ++no_mapping;
283 ++allow_keys; // no mapping for nchar, but keys
284 c = plain_vgetc(); // get next character
285 LANGMAP_ADJUST(c, TRUE);
286 --no_mapping;
287 --allow_keys;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000288 *need_flushbuf |= add_to_showcmd(c);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000289 goto getcount; // jump back
290 }
291 }
292
293 if (c == K_CURSORHOLD)
294 {
295 // Save the count values so that ca.opcount and ca.count0 are exactly
296 // the same when coming back here after handling K_CURSORHOLD.
297 cap->oap->prev_opcount = cap->opcount;
298 cap->oap->prev_count0 = cap->count0;
299 }
300 else if (cap->opcount != 0)
301 {
302 // If we're in the middle of an operator (including after entering a
303 // yank buffer with '"') AND we had a count before the operator, then
304 // that count overrides the current value of ca.count0.
305 // What this means effectively, is that commands like "3dw" get turned
306 // into "d3w" which makes things fall into place pretty neatly.
307 // If you give a count before AND after the operator, they are
308 // multiplied.
309 if (cap->count0)
310 {
311 if (cap->opcount >= 999999999L / cap->count0)
312 cap->count0 = 999999999L;
313 else
314 cap->count0 *= cap->opcount;
315 }
316 else
317 cap->count0 = cap->opcount;
318 }
319
320 // Always remember the count. It will be set to zero (on the next call,
321 // above) when there is no pending operator.
322 // When called from main(), save the count for use by the "count" built-in
323 // variable.
324 cap->opcount = cap->count0;
325 cap->count1 = (cap->count0 == 0 ? 1 : cap->count0);
326
327#ifdef FEAT_EVAL
328 // Only set v:count when called from main() and not a stuffed command.
329 // Do set it for redo.
330 if (toplevel && readbuf1_empty())
331 set_vcount(cap->count0, cap->count1, set_prevcount);
332#endif
333
334 return c;
335}
336
337/*
338 * Returns TRUE if the normal command (cap) needs a second character.
339 */
340 static int
341normal_cmd_needs_more_chars(cmdarg_T *cap, short_u cmd_flags)
342{
343 return ((cmd_flags & NV_NCH)
344 && (((cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
345 && cap->oap->op_type == OP_NOP)
346 || (cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
347 || (cap->cmdchar == 'q'
348 && cap->oap->op_type == OP_NOP
349 && reg_recording == 0
350 && reg_executing == 0)
351 || ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
352 && (cap->oap->op_type != OP_NOP || VIsual_active))));
353}
354
355/*
356 * Get one or more additional characters for a normal command.
357 * Return the updated command index (if changed).
358 */
359 static int
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000360normal_cmd_get_more_chars(
361 int idx_arg,
362 cmdarg_T *cap,
363 int *need_flushbuf UNUSED)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000364{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000365 int idx = idx_arg;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000366 int c;
367 int *cp;
368 int repl = FALSE; // get character for replace mode
369 int lit = FALSE; // get extra character literally
370 int langmap_active = FALSE; // using :lmap mappings
371 int lang; // getting a text character
372#ifdef HAVE_INPUT_METHOD
373 int save_smd; // saved value of p_smd
374#endif
375
376 ++no_mapping;
377 ++allow_keys; // no mapping for nchar, but allow key codes
378 // Don't generate a CursorHold event here, most commands can't handle
379 // it, e.g., nv_replace(), nv_csearch().
380 did_cursorhold = TRUE;
381 if (cap->cmdchar == 'g')
382 {
383 /*
384 * For 'g' get the next character now, so that we can check for
385 * "gr", "g'" and "g`".
386 */
387 cap->nchar = plain_vgetc();
388 LANGMAP_ADJUST(cap->nchar, TRUE);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000389 *need_flushbuf |= add_to_showcmd(cap->nchar);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000390 if (cap->nchar == 'r' || cap->nchar == '\'' || cap->nchar == '`'
391 || cap->nchar == Ctrl_BSL)
392 {
393 cp = &cap->extra_char; // need to get a third character
394 if (cap->nchar != 'r')
395 lit = TRUE; // get it literally
396 else
397 repl = TRUE; // get it in replace mode
398 }
399 else
400 cp = NULL; // no third character needed
401 }
402 else
403 {
404 if (cap->cmdchar == 'r') // get it in replace mode
405 repl = TRUE;
406 cp = &cap->nchar;
407 }
408 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
409
410 /*
411 * Get a second or third character.
412 */
413 if (cp != NULL)
414 {
415 if (repl)
416 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100417 State = MODE_REPLACE; // pretend Replace mode
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000418#ifdef CURSOR_SHAPE
419 ui_cursor_shape(); // show different cursor shape
420#endif
Yee Cheng China1b654f2024-11-11 21:57:27 +0100421#ifdef FEAT_MOUSESHAPE
422 update_mouseshape(-1);
423#endif
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000424 }
425 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
426 {
427 // Allow mappings defined with ":lmap".
428 --no_mapping;
429 --allow_keys;
430 if (repl)
Bram Moolenaar24959102022-05-07 20:01:16 +0100431 State = MODE_LREPLACE;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000432 else
Bram Moolenaar24959102022-05-07 20:01:16 +0100433 State = MODE_LANGMAP;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000434 langmap_active = TRUE;
435 }
436#ifdef HAVE_INPUT_METHOD
437 save_smd = p_smd;
438 p_smd = FALSE; // Don't let the IM code show the mode here
439 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
440 im_set_active(TRUE);
441#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100442 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000443 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100444 MAY_WANT_TO_LOG_THIS;
445
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000446 // Disable bracketed paste and modifyOtherKeys here, we won't
447 // recognize the escape sequences with 'esckeys' off.
448 out_str(T_BD);
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000449 out_str_t_TE();
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000450 }
451
452 *cp = plain_vgetc();
453
Bram Moolenaar24959102022-05-07 20:01:16 +0100454 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000455 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100456 MAY_WANT_TO_LOG_THIS;
457
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000458 // Re-enable bracketed paste mode and modifyOtherKeys
Bram Moolenaarfc966c12023-01-01 18:04:33 +0000459 out_str_t_BE();
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000460 out_str_t_TI();
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000461 }
462
463 if (langmap_active)
464 {
465 // Undo the decrement done above
466 ++no_mapping;
467 ++allow_keys;
Bram Moolenaar24959102022-05-07 20:01:16 +0100468 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000469 }
470#ifdef HAVE_INPUT_METHOD
471 if (lang)
472 {
473 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
474 im_save_status(&curbuf->b_p_iminsert);
475 im_set_active(FALSE);
476 }
477 p_smd = save_smd;
478#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100479 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000480 *need_flushbuf |= add_to_showcmd(*cp);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000481
482 if (!lit)
483 {
484#ifdef FEAT_DIGRAPHS
485 // Typing CTRL-K gets a digraph.
486 if (*cp == Ctrl_K
487 && ((nv_cmds[idx].cmd_flags & NV_LANG)
488 || cp == &cap->extra_char)
489 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
490 {
491 c = get_digraph(FALSE);
492 if (c > 0)
493 {
494 *cp = c;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000495 // Guessing how to update showcmd here...
496 del_from_showcmd(3);
497 *need_flushbuf |= add_to_showcmd(*cp);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000498 }
499 }
500#endif
501
502 // adjust chars > 127, except after "tTfFr" commands
503 LANGMAP_ADJUST(*cp, !lang);
504#ifdef FEAT_RIGHTLEFT
505 // adjust Hebrew mapped char
506 if (p_hkmap && lang && KeyTyped)
507 *cp = hkmap(*cp);
508#endif
509 }
510
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000511 // When the next character is CTRL-\ a following CTRL-N means the
512 // command is aborted and we go to Normal mode.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000513 if (cp == &cap->extra_char
514 && cap->nchar == Ctrl_BSL
515 && (cap->extra_char == Ctrl_N || cap->extra_char == Ctrl_G))
516 {
517 cap->cmdchar = Ctrl_BSL;
518 cap->nchar = cap->extra_char;
519 idx = find_command(cap->cmdchar);
520 }
zeertzjq4f026ea2023-02-26 14:47:24 +0000521 else if ((cap->nchar == 'n' || cap->nchar == 'N')
522 && cap->cmdchar == 'g')
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000523 cap->oap->op_type = get_op_type(*cp, NUL);
524 else if (*cp == Ctrl_BSL)
525 {
526 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
527
528 // There is a busy wait here when typing "f<C-\>" and then
529 // something different from CTRL-N. Can't be avoided.
530 while ((c = vpeekc()) <= 0 && towait > 0L)
531 {
532 do_sleep(towait > 50L ? 50L : towait, FALSE);
533 towait -= 50L;
534 }
535 if (c > 0)
536 {
537 c = plain_vgetc();
538 if (c != Ctrl_N && c != Ctrl_G)
539 vungetc(c);
540 else
541 {
542 cap->cmdchar = Ctrl_BSL;
543 cap->nchar = c;
544 idx = find_command(cap->cmdchar);
545 }
546 }
547 }
548
zeertzjqbacc8302023-08-12 00:09:31 +0200549 if (enc_utf8 && lang)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000550 {
zeertzjqbacc8302023-08-12 00:09:31 +0200551 // When getting a text character and the next character is a
552 // multi-byte character, it could be a composing character.
553 // However, don't wait for it to arrive. Also, do enable mapping,
554 // because if it's put back with vungetc() it's too late to apply
555 // mapping.
556 --no_mapping;
557 while ((c = vpeekc()) > 0
558 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000559 {
zeertzjqbacc8302023-08-12 00:09:31 +0200560 c = plain_vgetc();
561 if (!utf_iscomposing(c))
562 {
563 vungetc(c); // it wasn't, put it back
564 break;
565 }
566 else if (cap->ncharC1 == 0)
567 cap->ncharC1 = c;
568 else
569 cap->ncharC2 = c;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000570 }
zeertzjqbacc8302023-08-12 00:09:31 +0200571 ++no_mapping;
572 // Vim may be in a different mode when the user types the next key,
573 // but when replaying a recording the next key is already in the
zeertzjqbf321802024-01-28 19:03:00 +0100574 // typeahead buffer, so record an <Ignore> before that to prevent
575 // the vpeekc() above from applying wrong mappings when replaying.
zeertzjqdccc29c2023-09-04 22:25:07 +0200576 ++no_u_sync;
zeertzjqbf321802024-01-28 19:03:00 +0100577 gotchars_ignore();
zeertzjqdccc29c2023-09-04 22:25:07 +0200578 --no_u_sync;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000579 }
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000580 }
581 --no_mapping;
582 --allow_keys;
583
584 return idx;
585}
586
587/*
588 * Returns TRUE if after processing a normal mode command, need to wait for a
589 * moment when a message is displayed that will be overwritten by the mode
590 * message.
591 */
592 static int
593normal_cmd_need_to_wait_for_msg(cmdarg_T *cap, pos_T *old_pos)
594{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000595 // In Visual mode and with "^O" in Insert mode, a short message will be
596 // overwritten by the mode message. Wait a bit, until a key is hit.
597 // In Visual mode, it's more important to keep the Visual area updated
598 // than keeping a message (e.g. from a /pat search).
599 // Only do this if the command was typed, not from a mapping.
600 // Don't wait when emsg_silent is non-zero.
601 // Also wait a bit after an error message, e.g. for "^O:".
602 // Don't redraw the screen, it would remove the message.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000603 return ( ((p_smd
604 && msg_silent == 0
605 && (restart_edit != 0
606 || (VIsual_active
607 && old_pos->lnum == curwin->w_cursor.lnum
608 && old_pos->col == curwin->w_cursor.col)
609 )
610 && (clear_cmdline
611 || redraw_cmdline)
612 && (msg_didout || (msg_didany && msg_scroll))
613 && !msg_nowait
614 && KeyTyped)
615 || (restart_edit != 0
616 && !VIsual_active
617 && (msg_scroll
618 || emsg_on_display)))
619 && cap->oap->regname == 0
620 && !(cap->retval & CA_COMMAND_BUSY)
621 && stuff_empty()
622 && typebuf_typed()
623 && emsg_silent == 0
624 && !in_assert_fails
625 && !did_wait_return
626 && cap->oap->op_type == OP_NOP);
627}
628
629/*
630 * After processing a normal mode command, wait for a moment when a message is
631 * displayed that will be overwritten by the mode message.
632 */
633 static void
634normal_cmd_wait_for_msg(void)
635{
636 int save_State = State;
637
638 // Draw the cursor with the right shape here
639 if (restart_edit != 0)
Bram Moolenaar24959102022-05-07 20:01:16 +0100640 State = MODE_INSERT;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000641
642 // If need to redraw, and there is a "keep_msg", redraw before the
643 // delay
644 if (must_redraw && keep_msg != NULL && !emsg_on_display)
645 {
646 char_u *kmsg;
647
648 kmsg = keep_msg;
649 keep_msg = NULL;
650 // Showmode() will clear keep_msg, but we want to use it anyway.
651 // First update w_topline.
652 setcursor();
653 update_screen(0);
654 // now reset it, otherwise it's put in the history again
655 keep_msg = kmsg;
656
657 kmsg = vim_strsave(keep_msg);
658 if (kmsg != NULL)
659 {
660 msg_attr((char *)kmsg, keep_msg_attr);
661 vim_free(kmsg);
662 }
663 }
664 setcursor();
665#ifdef CURSOR_SHAPE
666 ui_cursor_shape(); // may show different cursor shape
667#endif
668 cursor_on();
669 out_flush();
670 if (msg_scroll || emsg_on_display)
671 ui_delay(1003L, TRUE); // wait at least one second
672 ui_delay(3003L, FALSE); // wait up to three seconds
673 State = save_State;
674
675 msg_scroll = FALSE;
676 emsg_on_display = FALSE;
677}
678
679/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 * Execute a command in Normal mode.
681 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100683normal_cmd(
684 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100685 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100687 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100689 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 int old_col = curwin->w_curswant;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000691 int need_flushbuf = FALSE; // need to call out_flush()
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100692 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 static int old_mapped_len = 0;
695 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000696 int set_prevcount = FALSE;
Bram Moolenaarb146e012020-07-19 23:06:05 +0200697 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
Bram Moolenaara80faa82020-04-12 19:37:17 +0200699 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000701
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100702 // Use a count remembered from before entering an operator. After typing
703 // "3d" we return from normal_cmd() and come back here, the "3" is
704 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 ca.opcount = opcount;
706
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000707 // If there is an operator pending, then the command we take this time
708 // will terminate it. Finish_op tells us to finish the operation before
709 // returning this time (unless the operation was cancelled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710#ifdef CURSOR_SHAPE
711 c = finish_op;
712#endif
713 finish_op = (oap->op_type != OP_NOP);
714#ifdef CURSOR_SHAPE
715 if (finish_op != c)
716 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100717 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718# ifdef FEAT_MOUSESHAPE
719 update_mouseshape(-1);
720# endif
721 }
722#endif
LemonBoy2bf52dd2022-04-09 18:17:34 +0100723 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100725 // When not finishing an operator and no register name typed, reset the
726 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000728 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000730#ifdef FEAT_EVAL
731 set_prevcount = TRUE;
732#endif
733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100735 // Restore counts from before receiving K_CURSORHOLD. This means after
736 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
737 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000738 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
739 {
740 ca.opcount = oap->prev_opcount;
741 ca.count0 = oap->prev_count0;
742 oap->prev_opcount = 0;
743 oap->prev_count0 = 0;
744 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000745
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747
Bram Moolenaar24959102022-05-07 20:01:16 +0100748 State = MODE_NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100750 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751#endif
752
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100753#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100754 // Set v:count here, when called from main() and not a stuffed
755 // command, so that v:count can be used in an expression mapping
756 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100757 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100758 set_vcount_ca(&ca, &set_prevcount);
759#endif
760
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 /*
762 * Get the command character from the user.
763 */
764 c = safe_vgetc();
Bram Moolenaar24959102022-05-07 20:01:16 +0100765 LANGMAP_ADJUST(c, get_real_state() != MODE_SELECT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000767 // If a mapping was started in Visual or Select mode, remember the length
768 // of the mapping. This is used below to not return to Insert mode for as
769 // long as the mapping is being executed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 if (restart_edit == 0)
771 old_mapped_len = 0;
772 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000773 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 old_mapped_len = typebuf_maplen();
775
776 if (c == NUL)
777 c = K_ZERO;
778
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000779 // In Select mode, typed text replaces the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 if (VIsual_active
781 && VIsual_select
782 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
783 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000784 int len;
785
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100786 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
787 // 'insertmode' is set) fake a "d"elete command, Insert mode will
788 // restart automatically.
789 // Insert the typed character in the typeahead buffer, so that it can
790 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000791 len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
792
zeertzjqfbf4f1c2022-01-28 12:50:43 +0000793 // When recording and gotchars() was called the character will be
794 // recorded again, remove the previous recording.
795 if (KeyTyped)
796 ungetchars(len);
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000797
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000798 if (restart_edit != 0)
799 c = 'd';
800 else
801 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100802 msg_nowait = TRUE; // don't delay going to insert mode
803 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805
Bram Moolenaard0fb9072021-12-09 11:57:22 +0000806 // If the window was made so small that nothing shows, make it at least one
807 // line and one column when typing a command.
808 if (KeyTyped && !KeyStuffed)
809 win_ensure_size();
810
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 need_flushbuf = add_to_showcmd(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000813 // Get the command count
814 c = normal_cmd_get_count(&ca, c, toplevel, set_prevcount, &ctrl_w,
815 &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000817 // Find the command character in the table of commands.
818 // For CTRL-W we already got nchar when looking for a count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 if (ctrl_w)
820 {
821 ca.nchar = c;
822 ca.cmdchar = Ctrl_W;
823 }
824 else
825 ca.cmdchar = c;
826 idx = find_command(ca.cmdchar);
827 if (idx < 0)
828 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100829 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 clearopbeep(oap);
831 goto normal_end;
832 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000833
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000834 if ((nv_cmds[idx].cmd_flags & NV_NCW) && check_text_or_curbuf_locked(oap))
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100835 // this command is not allowed now
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000836 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000838 // In Visual/Select mode, a few keys are handled in a special way.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 if (VIsual_active)
840 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100841 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 if (km_stopsel
843 && (nv_cmds[idx].cmd_flags & NV_STS)
844 && !(mod_mask & MOD_MASK_SHIFT))
845 {
846 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100847 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 }
849
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100850 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 if (km_startsel)
852 {
853 if (nv_cmds[idx].cmd_flags & NV_SS)
854 {
855 unshift_special(&ca);
856 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000857 if (idx < 0)
858 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100859 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000860 clearopbeep(oap);
861 goto normal_end;
862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 }
864 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
865 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 }
868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869
870#ifdef FEAT_RIGHTLEFT
871 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
872 && (nv_cmds[idx].cmd_flags & NV_RL))
873 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100874 // Invert horizontal movements and operations. Only when typed by the
875 // user directly, not when the result of a mapping or "x" translated
876 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 switch (ca.cmdchar)
878 {
879 case 'l': ca.cmdchar = 'h'; break;
880 case K_RIGHT: ca.cmdchar = K_LEFT; break;
881 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
882 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
883 case 'h': ca.cmdchar = 'l'; break;
884 case K_LEFT: ca.cmdchar = K_RIGHT; break;
885 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
886 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
887 case '>': ca.cmdchar = '<'; break;
888 case '<': ca.cmdchar = '>'; break;
889 }
890 idx = find_command(ca.cmdchar);
891 }
892#endif
893
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000894 // Get additional characters if we need them.
895 if (normal_cmd_needs_more_chars(&ca, nv_cmds[idx].cmd_flags))
896 idx = normal_cmd_get_more_chars(idx, &ca, &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000898 // Flush the showcmd characters onto the screen so we can see them while
899 // the command is being executed. Only do this when the shown command was
900 // actually displayed, otherwise this will slow down a lot when executing
901 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 if (need_flushbuf)
903 out_flush();
Martin Tournoijba43e762022-10-13 22:12:15 +0100904
Bram Moolenaard9205ca2008-10-02 20:55:54 +0000905 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +0200906 {
907 if (ex_normal_busy)
908 did_cursorhold = save_did_cursorhold;
909 else
910 did_cursorhold = FALSE;
911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912
Bram Moolenaar24959102022-05-07 20:01:16 +0100913 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914
zeertzjqf86dea82023-03-05 21:15:06 +0000915 if (ca.nchar == ESC || ca.extra_char == ESC)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 {
917 clearop(oap);
918 if (restart_edit == 0 && goto_im())
919 restart_edit = 'a';
920 goto normal_end;
921 }
922
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000923 if (ca.cmdchar != K_IGNORE)
924 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100925 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000926 msg_col = 0;
927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928
Luuk van Baalaa7f25e2023-04-13 21:47:38 +0100929 old_pos = curwin->w_cursor; // remember where the cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100931 // When 'keymodel' contains "startsel" some keys start Select/Visual
932 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 if (!VIsual_active && km_startsel)
934 {
935 if (nv_cmds[idx].cmd_flags & NV_SS)
936 {
937 start_selection();
938 unshift_special(&ca);
939 idx = find_command(ca.cmdchar);
940 }
941 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
942 && (mod_mask & MOD_MASK_SHIFT))
943 {
944 start_selection();
945 mod_mask &= ~MOD_MASK_SHIFT;
946 }
947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000949 // Execute the command!
950 // Call the command function found in the commands table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 ca.arg = nv_cmds[idx].cmd_arg;
952 (nv_cmds[idx].cmd_func)(&ca);
953
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000954 // If we didn't start or finish an operator, reset oap->regname, unless we
955 // need it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 if (!finish_op
957 && !oap->op_type
958 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
959 {
960 clearop(oap);
961#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +0200962 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963#endif
964 }
965
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100966 // Get the length of mapped chars again after typing a count, second
967 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000968 if (old_mapped_len > 0)
969 old_mapped_len = typebuf_maplen();
970
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000971 // If an operation is pending, handle it. But not for K_IGNORE or
972 // K_MOUSEMOVE.
Bram Moolenaar1ad72c82021-05-04 21:56:28 +0200973 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +0100974 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000976 // Wait for a moment when a message is displayed that will be overwritten
977 // by the mode message.
978 if (normal_cmd_need_to_wait_for_msg(&ca, &old_pos))
979 normal_cmd_wait_for_msg();
980
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000981 // Finish up after executing a Normal mode command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982normal_end:
983
984 msg_nowait = FALSE;
985
Bram Moolenaarcc613032020-06-07 21:31:18 +0200986#ifdef FEAT_EVAL
987 if (finish_op)
988 reset_reg_var();
989#endif
990
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991#ifdef CURSOR_SHAPE
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000992 int prev_finish_op = finish_op;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#endif
zeertzjq73916ba2023-04-26 16:50:19 +0100994 if (oap->op_type == OP_NOP)
995 {
996 // Reset finish_op, in case it was set
997 finish_op = FALSE;
998 may_trigger_modechanged();
999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001001 // Redraw the cursor with another shape, if we were in Operator-pending
1002 // mode or did a replace command.
zeertzjqf86dea82023-03-05 21:15:06 +00001003 if (prev_finish_op || ca.cmdchar == 'r'
1004 || (ca.cmdchar == 'g' && ca.nchar == 'r'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001006 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007# ifdef FEAT_MOUSESHAPE
1008 update_mouseshape(-1);
1009# endif
1010 }
1011#endif
1012
Bram Moolenaara983fe92008-07-31 20:04:27 +00001013 if (oap->op_type == OP_NOP && oap->regname == 0
zeertzjq73916ba2023-04-26 16:50:19 +01001014 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001017 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 vim_free(ca.searchbuf);
1019
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 if (has_mbyte)
1021 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 if (curwin->w_p_scb && toplevel)
1024 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001025 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 do_check_scrollbind(TRUE);
1027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028
Bram Moolenaar860cae12010-06-05 23:22:07 +02001029 if (curwin->w_p_crb && toplevel)
1030 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001031 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001032 do_check_cursorbind();
1033 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001034
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001035#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001036 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001037 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001038 restart_edit = 0;
1039#endif
1040
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001041 // May restart edit(), if we got here with CTRL-O in Insert mode (but not
1042 // if still inside a mapping that started in Visual mode).
1043 // May switch from Visual to Select mode after CTRL-O command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1046 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 && !(ca.retval & CA_COMMAND_BUSY)
1048 && stuff_empty()
1049 && oap->regname == 0)
1050 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 if (restart_VIsual_select == 1)
1052 {
1053 VIsual_select = TRUE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01001054 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 showmode();
1056 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001057 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001059 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 (void)edit(restart_edit, FALSE, 1L);
1061 }
1062
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 if (restart_VIsual_select == 2)
1064 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001066 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 opcount = ca.opcount;
1068}
1069
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001070#ifdef FEAT_EVAL
1071/*
1072 * Set v:count and v:count1 according to "cap".
1073 * Set v:prevcount only when "set_prevcount" is TRUE.
1074 */
1075 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001076set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001077{
1078 long count = cap->count0;
1079
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001080 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001081 if (cap->opcount != 0)
1082 count = cap->opcount * (count == 0 ? 1 : count);
1083 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001084 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001085}
1086#endif
1087
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001089 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 * if not.
1091 */
1092 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001093check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
1095 static int did_check = FALSE;
1096
1097 if (full_screen)
1098 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001099 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001100 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 did_check = TRUE;
1102 }
1103}
1104
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001105#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1106/*
1107 * Call yank_do_autocmd() for "regname".
1108 */
1109 static void
1110call_yank_do_autocmd(int regname)
1111{
1112 oparg_T oa;
1113 yankreg_T *reg;
1114
1115 clear_oparg(&oa);
1116 oa.regname = regname;
1117 oa.op_type = OP_YANK;
1118 oa.is_VIsual = TRUE;
1119 reg = get_register(regname, TRUE);
1120 yank_do_autocmd(&oa, reg);
1121 free_register(reg);
1122}
1123#endif
1124
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001126 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001127 * This function or the next should ALWAYS be called to end Visual mode, except
1128 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 */
1130 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001131end_visual_mode(void)
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001132{
1133 end_visual_mode_keep_button();
1134 reset_held_button();
1135}
1136
1137 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001138end_visual_mode_keep_button(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139{
1140#ifdef FEAT_CLIPBOARD
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001141 // If we are using the clipboard, then remember what was selected in case
1142 // we need to paste it somewhere while we still own the selection.
1143 // Only do this when the clipboard is already owned. Don't want to grab
1144 // the selection when hitting ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 if (clip_star.available && clip_star.owned)
1146 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001147
1148# if defined(FEAT_EVAL)
1149 // Emit a TextYankPost for the automatic copy of the selection into the
1150 // star and/or plus register.
1151 if (has_textyankpost())
1152 {
1153 if (clip_isautosel_star())
1154 call_yank_do_autocmd('*');
1155 if (clip_isautosel_plus())
1156 call_yank_do_autocmd('+');
1157 }
1158# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159#endif
1160
1161 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 setmouse();
1163 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001165 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001166 curbuf->b_visual.vi_mode = VIsual_mode;
1167 curbuf->b_visual.vi_start = VIsual;
1168 curbuf->b_visual.vi_end = curwin->w_cursor;
1169 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170#ifdef FEAT_EVAL
1171 curbuf->b_visual_mode_eval = VIsual_mode;
1172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 if (!virtual_active())
1174 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001175 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001177 adjust_cursor_eol();
LemonBoy2bf52dd2022-04-09 18:17:34 +01001178 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179}
1180
1181/*
1182 * Reset VIsual_active and VIsual_reselect.
1183 */
1184 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001185reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186{
1187 if (VIsual_active)
1188 {
1189 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001190 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 }
1192 VIsual_reselect = FALSE;
1193}
1194
1195/*
1196 * Reset VIsual_active and VIsual_reselect if it's set.
1197 */
1198 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001199reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200{
1201 if (VIsual_active)
1202 {
1203 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001204 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 VIsual_reselect = FALSE;
1206 }
1207}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001209 void
1210restore_visual_mode(void)
1211{
1212 if (VIsual_mode_orig != NUL)
1213 {
1214 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1215 VIsual_mode_orig = NUL;
1216 }
1217}
1218
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219/*
1220 * Check for a balloon-eval special item to include when searching for an
1221 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1222 * Returns TRUE if the character at "*ptr" should be included.
1223 * "dir" is FORWARD or BACKWARD, the direction of searching.
1224 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1225 * "bnp" points to a counter for square brackets.
1226 */
1227 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001228find_is_eval_item(
1229 char_u *ptr,
1230 int *colp,
1231 int *bnp,
1232 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001234 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1236 ++*bnp;
1237 if (*bnp > 0)
1238 {
1239 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1240 --*bnp;
1241 return TRUE;
1242 }
1243
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001244 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 if (*ptr == '.')
1246 return TRUE;
1247
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001248 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1250 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1251 {
1252 *colp += dir;
1253 return TRUE;
1254 }
1255 return FALSE;
1256}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257
1258/*
1259 * Find the identifier under or to the right of the cursor.
1260 * "find_type" can have one of three values:
1261 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001262 * FIND_STRING: find any non-white text
1263 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001264 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 *
1266 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001267 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001269 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 * This doesn't match the real Vi but I like it a little better and it
1271 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001272 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 * When FIND_IDENT isn't defined, we backup until a blank.
1274 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001275 * Returns the length of the text, or zero if no text is found.
1276 * If text is found, a pointer to the text is put in "*text". This
1277 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 */
1279 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001280find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281{
1282 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001283 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284}
1285
1286/*
1287 * Like find_ident_under_cursor(), but for any window and any position.
1288 * However: Uses 'iskeyword' from the current window!.
1289 */
1290 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001291find_ident_at_pos(
1292 win_T *wp,
1293 linenr_T lnum,
1294 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001295 char_u **text,
1296 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001297 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298{
1299 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001300 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 int this_class = 0;
1303 int prev_class;
1304 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001305 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001307 // if i == 0: try to find an identifier
1308 // if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1310 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1311 {
1312 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001313 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 */
1315 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 if (has_mbyte)
1317 {
1318 while (ptr[col] != NUL)
1319 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001320 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1322 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 this_class = mb_get_class(ptr + col);
1324 if (this_class != 0 && (i == 1 || this_class != 1))
1325 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001326 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 }
1328 }
1329 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001331 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 )
1334 ++col;
1335
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001336 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
1339 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001340 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 if (has_mbyte)
1343 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001344 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1346 this_class = mb_get_class((char_u *)"a");
1347 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001349 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 {
1351 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1352 prev_class = mb_get_class(ptr + prevcol);
1353 if (this_class != prev_class
1354 && (i == 0
1355 || prev_class == 0
1356 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 && (!(find_type & FIND_EVAL)
1358 || prevcol == 0
1359 || !find_is_eval_item(ptr + prevcol, &prevcol,
1360 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 )
1362 break;
1363 col = prevcol;
1364 }
1365
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001366 // If we don't want just any old text, or we've found an
1367 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 if (this_class > 2)
1369 this_class = 2;
1370 if (!(find_type & FIND_STRING) || this_class == 2)
1371 break;
1372 }
1373 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 {
1375 while (col > 0
1376 && ((i == 0
1377 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001378 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 && (!(find_type & FIND_IDENT)
1380 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 || ((find_type & FIND_EVAL)
1382 && col > 1
1383 && find_is_eval_item(ptr + col - 1, &col,
1384 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 ))
1386 --col;
1387
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001388 // If we don't want just any old text, or we've found an
1389 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1391 break;
1392 }
1393 }
1394
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001395 if (ptr[col] == NUL || (i == 0
1396 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001398 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001399 if ((find_type & FIND_NOERROR) == 0)
1400 {
1401 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001402 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001403 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001404 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 return 0;
1407 }
1408 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001409 *text = ptr;
1410 if (textcol != NULL)
1411 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412
1413 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001414 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 bn = 0;
1417 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 if (has_mbyte)
1420 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001421 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 this_class = mb_get_class(ptr);
1423 while (ptr[col] != NUL
1424 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1425 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 || ((find_type & FIND_EVAL)
1427 && col <= (int)startcol
1428 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001430 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 }
1432 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001434 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 || ((find_type & FIND_EVAL)
1436 && col <= (int)startcol
1437 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
1441 return col;
1442}
1443
1444/*
1445 * Prepare for redo of a normal command.
1446 */
1447 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001448prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449{
1450 prep_redo(cap->oap->regname, cap->count0,
1451 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1452}
1453
1454/*
1455 * Prepare for redo of any command.
1456 * Note that only the last argument can be a multi-byte char.
1457 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001458 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001459prep_redo(
1460 int regname,
1461 long num,
1462 int cmd1,
1463 int cmd2,
1464 int cmd3,
1465 int cmd4,
1466 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001468 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1469}
1470
1471/*
1472 * Prepare for redo of any command with extra count after "cmd2".
1473 */
1474 void
1475prep_redo_num2(
1476 int regname,
1477 long num1,
1478 int cmd1,
1479 int cmd2,
1480 long num2,
1481 int cmd3,
1482 int cmd4,
1483 int cmd5)
1484{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 ResetRedobuff();
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001486
1487#ifdef FEAT_EVAL
1488 // Put info about a mapping in the redo buffer, so that "." will use the
1489 // same script context.
1490 may_add_last_used_map_to_redobuff();
1491#endif
1492
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001493 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 {
1495 AppendCharToRedobuff('"');
1496 AppendCharToRedobuff(regname);
1497 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001498 if (num1 != 0)
1499 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 if (cmd1 != NUL)
1501 AppendCharToRedobuff(cmd1);
1502 if (cmd2 != NUL)
1503 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001504 if (num2 != 0)
1505 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 if (cmd3 != NUL)
1507 AppendCharToRedobuff(cmd3);
1508 if (cmd4 != NUL)
1509 AppendCharToRedobuff(cmd4);
1510 if (cmd5 != NUL)
1511 AppendCharToRedobuff(cmd5);
1512}
1513
1514/*
zeertzjq73916ba2023-04-26 16:50:19 +01001515 * Check for operator active and clear it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 *
zeertzjq73916ba2023-04-26 16:50:19 +01001517 * Beep and return TRUE if an operator was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 */
1519 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001520checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521{
1522 if (oap->op_type == OP_NOP)
1523 return FALSE;
1524 clearopbeep(oap);
1525 return TRUE;
1526}
1527
1528/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001529 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 *
zeertzjq73916ba2023-04-26 16:50:19 +01001531 * Beep and return TRUE if an operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 */
1533 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001534checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001536 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 return FALSE;
1538 clearopbeep(oap);
1539 return TRUE;
1540}
1541
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001542 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001543clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544{
1545 oap->op_type = OP_NOP;
1546 oap->regname = 0;
1547 oap->motion_force = NUL;
1548 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001549 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550}
1551
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001552 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001553clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554{
1555 clearop(oap);
1556 beep_flush();
1557}
1558
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559/*
1560 * Remove the shift modifier from a special key.
1561 */
1562 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001563unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564{
1565 switch (cap->cmdchar)
1566 {
1567 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1568 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1569 case K_S_UP: cap->cmdchar = K_UP; break;
1570 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1571 case K_S_HOME: cap->cmdchar = K_HOME; break;
1572 case K_S_END: cap->cmdchar = K_END; break;
1573 }
1574 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1575}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001577/*
1578 * If the mode is currently displayed clear the command line or update the
1579 * command displayed.
1580 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001581 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001582may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001583{
1584 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001585 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001586 else
1587 clear_showcmd();
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001588}
1589
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590/*
1591 * Routines for displaying a partly typed command
1592 */
1593
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001594static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595static int showcmd_is_clear = TRUE;
1596static int showcmd_visual = FALSE;
1597
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001598static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599
1600 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001601clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602{
1603 if (!p_sc)
1604 return;
1605
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 if (VIsual_active && !char_avail())
1607 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001608 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 long lines;
1610 colnr_T leftcol, rightcol;
1611 linenr_T top, bot;
1612
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001613 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001614 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 {
1616 top = VIsual.lnum;
1617 bot = curwin->w_cursor.lnum;
1618 }
1619 else
1620 {
1621 top = curwin->w_cursor.lnum;
1622 bot = VIsual.lnum;
1623 }
1624# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001625 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001626 (void)hasFolding(top, &top, NULL);
1627 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628# endif
1629 lines = bot - top + 1;
1630
1631 if (VIsual_mode == Ctrl_V)
1632 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001633# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001634 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001635 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001636
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001637 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001638 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001639 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001640# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001642# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001643 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001644 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001645# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1647 (long)(rightcol - leftcol + 1));
1648 }
1649 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1650 sprintf((char *)showcmd_buf, "%ld", lines);
1651 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001652 {
1653 char_u *s, *e;
1654 int l;
1655 int bytes = 0;
1656 int chars = 0;
1657
1658 if (cursor_bot)
1659 {
1660 s = ml_get_pos(&VIsual);
1661 e = ml_get_cursor();
1662 }
1663 else
1664 {
1665 s = ml_get_cursor();
1666 e = ml_get_pos(&VIsual);
1667 }
1668 while ((*p_sel != 'e') ? s <= e : s < e)
1669 {
1670 l = (*mb_ptr2len)(s);
1671 if (l == 0)
1672 {
1673 ++bytes;
1674 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001675 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001676 }
1677 bytes += l;
1678 ++chars;
1679 s += l;
1680 }
1681 if (bytes == chars)
1682 sprintf((char *)showcmd_buf, "%d", chars);
1683 else
1684 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1685 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001686 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 showcmd_visual = TRUE;
1688 }
1689 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 {
1691 showcmd_buf[0] = NUL;
1692 showcmd_visual = FALSE;
1693
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001694 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 if (showcmd_is_clear)
1696 return;
1697 }
1698
1699 display_showcmd();
1700}
1701
1702/*
1703 * Add 'c' to string of shown command chars.
1704 * Return TRUE if output has been written (and setcursor() has been called).
1705 */
1706 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001707add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708{
1709 char_u *p;
1710 int old_len;
1711 int extra_len;
1712 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 int i;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001714 char_u mbyte_buf[MB_MAXBYTES];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 static int ignore[] =
1716 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001717#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1719 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001720#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001721 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001722 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1724 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001725 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001727 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 0
1729 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
Bram Moolenaar09df3122006-01-23 22:23:09 +00001731 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 return FALSE;
1733
1734 if (showcmd_visual)
1735 {
1736 showcmd_buf[0] = NUL;
1737 showcmd_visual = FALSE;
1738 }
1739
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001740 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 if (IS_SPECIAL(c))
1742 for (i = 0; ignore[i] != 0; ++i)
1743 if (ignore[i] == c)
1744 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745
zeertzjqacdfb8a2024-04-17 21:28:54 +02001746 if (c <= 0x7f || !vim_isprintc(c))
1747 {
1748 p = transchar(c);
1749 if (*p == ' ')
1750 STRCPY(p, "<20>");
1751 }
1752 else
1753 {
1754 mbyte_buf[(*mb_char2bytes)(c, mbyte_buf)] = NUL;
1755 p = mbyte_buf;
1756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 old_len = (int)STRLEN(showcmd_buf);
1758 extra_len = (int)STRLEN(p);
1759 overflow = old_len + extra_len - SHOWCMD_COLS;
1760 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001761 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1762 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 STRCAT(showcmd_buf, p);
1764
1765 if (char_avail())
1766 return FALSE;
1767
1768 display_showcmd();
1769
1770 return TRUE;
1771}
1772
1773 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001774add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775{
1776 if (!add_to_showcmd(c))
1777 setcursor();
1778}
1779
1780/*
1781 * Delete 'len' characters from the end of the shown command.
1782 */
1783 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001784del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785{
1786 int old_len;
1787
1788 if (!p_sc)
1789 return;
1790
1791 old_len = (int)STRLEN(showcmd_buf);
1792 if (len > old_len)
1793 len = old_len;
1794 showcmd_buf[old_len - len] = NUL;
1795
1796 if (!char_avail())
1797 display_showcmd();
1798}
1799
1800/*
1801 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1802 * something and there is a partial mapping.
1803 */
1804 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001805push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806{
1807 if (p_sc)
1808 STRCPY(old_showcmd_buf, showcmd_buf);
1809}
1810
1811 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001812pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813{
1814 if (!p_sc)
1815 return;
1816
1817 STRCPY(showcmd_buf, old_showcmd_buf);
1818
1819 display_showcmd();
1820}
1821
1822 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001823display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824{
zeertzjqacdfb8a2024-04-17 21:28:54 +02001825 int len = vim_strsize(showcmd_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826
Luuk van Baalba936f62022-12-15 13:15:39 +00001827 showcmd_is_clear = (len == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 cursor_off();
1829
Luuk van Baalba936f62022-12-15 13:15:39 +00001830 if (*p_sloc == 's')
Luuk van Baalaa7f25e2023-04-13 21:47:38 +01001831 {
1832 if (showcmd_is_clear)
1833 curwin->w_redr_status = TRUE;
1834 else
1835 win_redr_status(curwin, FALSE);
1836 }
Luuk van Baalba936f62022-12-15 13:15:39 +00001837 else if (*p_sloc == 't')
Luuk van Baalaa7f25e2023-04-13 21:47:38 +01001838 {
1839 if (showcmd_is_clear)
1840 redraw_tabline = TRUE;
1841 else
1842 draw_tabline();
1843 }
Luuk van Baalba936f62022-12-15 13:15:39 +00001844 else // 'showcmdloc' is "last" or empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 {
Luuk van Baalba936f62022-12-15 13:15:39 +00001846 if (!showcmd_is_clear)
1847 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848
Luuk van Baalba936f62022-12-15 13:15:39 +00001849 // clear the rest of an old message by outputting up to SHOWCMD_COLS
1850 // spaces
1851 screen_puts((char_u *)" " + len,
1852 (int)Rows - 1, sc_col + len, 0);
1853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001855 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858/*
1859 * When "check" is FALSE, prepare for commands that scroll the window.
1860 * When "check" is TRUE, take care of scroll-binding after the window has
1861 * scrolled. Called from normal_cmd() and edit().
1862 */
1863 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001864do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865{
1866 static win_T *old_curwin = NULL;
1867 static linenr_T old_topline = 0;
1868#ifdef FEAT_DIFF
1869 static int old_topfill = 0;
1870#endif
1871 static buf_T *old_buf = NULL;
1872 static colnr_T old_leftcol = 0;
1873
1874 if (check && curwin->w_p_scb)
1875 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001876 // If a ":syncbind" command was just used, don't scroll, only reset
1877 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 if (did_syncbind)
1879 did_syncbind = FALSE;
1880 else if (curwin == old_curwin)
1881 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001882 // Synchronize other windows, as necessary according to
1883 // 'scrollbind'. Don't do this after an ":edit" command, except
1884 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 if ((curwin->w_buffer == old_buf
1886#ifdef FEAT_DIFF
1887 || curwin->w_p_diff
1888#endif
1889 )
1890 && (curwin->w_topline != old_topline
1891#ifdef FEAT_DIFF
1892 || curwin->w_topfill != old_topfill
1893#endif
1894 || curwin->w_leftcol != old_leftcol))
1895 {
1896 check_scrollbind(curwin->w_topline - old_topline,
1897 (long)(curwin->w_leftcol - old_leftcol));
1898 }
1899 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001900 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001902 // When switching between windows, make sure that the relative
1903 // vertical offset is valid for the new window. The relative
1904 // offset is invalid whenever another 'scrollbind' window has
1905 // scrolled to a point that would force the current window to
1906 // scroll past the beginning or end of its buffer. When the
1907 // resync is performed, some of the other 'scrollbind' windows may
1908 // need to jump so that the current window's relative position is
1909 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
1911 }
1912 curwin->w_scbind_pos = curwin->w_topline;
1913 }
1914
1915 old_curwin = curwin;
1916 old_topline = curwin->w_topline;
1917#ifdef FEAT_DIFF
1918 old_topfill = curwin->w_topfill;
1919#endif
1920 old_buf = curwin->w_buffer;
1921 old_leftcol = curwin->w_leftcol;
1922}
1923
1924/*
1925 * Synchronize any windows that have "scrollbind" set, based on the
1926 * number of rows by which the current window has changed
1927 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
1928 */
1929 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001930check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931{
1932 int want_ver;
1933 int want_hor;
1934 win_T *old_curwin = curwin;
1935 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 int old_VIsual_select = VIsual_select;
1937 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 colnr_T tgt_leftcol = curwin->w_leftcol;
1939 long topline;
1940 long y;
1941
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001942 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
1944#ifdef FEAT_DIFF
1945 want_ver |= old_curwin->w_p_diff;
1946#endif
1947 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
1948
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001949 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001951 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 {
1953 curbuf = curwin->w_buffer;
zeertzjq101d57b2022-07-31 18:34:32 +01001954 // skip original window and windows with 'noscrollbind'
1955 if (curwin == old_curwin || !curwin->w_p_scb)
1956 continue;
1957
1958 // do the vertical scroll
1959 if (want_ver)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961#ifdef FEAT_DIFF
zeertzjq101d57b2022-07-31 18:34:32 +01001962 if (old_curwin->w_p_diff && curwin->w_p_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 {
zeertzjq101d57b2022-07-31 18:34:32 +01001964 diff_set_topline(old_curwin, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 }
zeertzjq101d57b2022-07-31 18:34:32 +01001966 else
1967#endif
1968 {
1969 curwin->w_scbind_pos += topline_diff;
1970 topline = curwin->w_scbind_pos;
1971 if (topline > curbuf->b_ml.ml_line_count)
1972 topline = curbuf->b_ml.ml_line_count;
1973 if (topline < 1)
1974 topline = 1;
1975
1976 y = topline - curwin->w_topline;
1977 if (y > 0)
1978 scrollup(y, FALSE);
1979 else
1980 scrolldown(-y, FALSE);
1981 }
1982
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001983 redraw_later(UPD_VALID);
zeertzjq101d57b2022-07-31 18:34:32 +01001984 cursor_correct();
1985 curwin->w_redr_status = TRUE;
1986 }
1987
1988 // do the horizontal scroll
Bram Moolenaar0c34d562022-11-18 14:07:20 +00001989 if (want_hor)
1990 (void)set_leftcol(tgt_leftcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 }
1992
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001993 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 VIsual_select = old_VIsual_select;
1995 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 curwin = old_curwin;
1997 curbuf = old_curbuf;
1998}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999
2000/*
2001 * Command character that's ignored.
2002 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02002003 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002006nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002008 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009}
2010
2011/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002012 * Command character that doesn't do anything, but unlike nv_ignore() does
2013 * start edit(). Used for "startinsert" executed while starting up.
2014 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002015 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002016nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002017{
2018}
2019
2020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 * Command character doesn't exist.
2022 */
2023 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002024nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025{
2026 clearopbeep(cap->oap);
2027}
2028
2029/*
2030 * <Help> and <F1> commands.
2031 */
2032 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002033nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034{
2035 if (!checkclearopq(cap->oap))
2036 ex_help(NULL);
2037}
2038
2039/*
2040 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2041 */
2042 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002043nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002045#ifdef FEAT_JOB_CHANNEL
2046 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2047 clearopbeep(cap->oap);
2048 else
2049#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002050 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002051 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002052 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002053 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2054 op_addsub(cap->oap, cap->count1, cap->arg);
2055 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002056 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002057 else if (VIsual_active)
2058 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002059 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002060 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061}
2062
2063/*
2064 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2065 */
2066 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002067nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002069 if (checkclearop(cap->oap))
2070 return;
2071
2072 if (mod_mask & MOD_MASK_CTRL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002073 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002074 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
2075 if (cap->arg == BACKWARD)
2076 goto_tabpage(-(int)cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002077 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002078 goto_tabpage((int)cap->count0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002079 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002080 else
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01002081 (void)pagescroll(cap->arg, cap->count1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082}
2083
2084/*
2085 * Implementation of "gd" and "gD" command.
2086 */
2087 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002088nv_gd(
2089 oparg_T *oap,
2090 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002091 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092{
2093 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 char_u *ptr;
2095
Bram Moolenaard9d30582005-05-18 22:10:28 +00002096 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002097 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002098 == FAIL)
2099 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 clearopbeep(oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002101 return;
Bram Moolenaar0c711142021-11-12 10:30:04 +00002102 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002103
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002104#ifdef FEAT_FOLDING
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002105 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2106 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002107#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002108 // clear any search statistics
2109 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2110 clear_cmdline = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002111}
2112
2113/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002114 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2115 * otherwise.
2116 */
2117 static int
2118is_ident(char_u *line, int offset)
2119{
2120 int i;
2121 int incomment = FALSE;
2122 int instring = 0;
2123 int prev = 0;
2124
2125 for (i = 0; i < offset && line[i] != NUL; i++)
2126 {
2127 if (instring != 0)
2128 {
2129 if (prev != '\\' && line[i] == instring)
2130 instring = 0;
2131 }
2132 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2133 {
2134 instring = line[i];
2135 }
2136 else
2137 {
2138 if (incomment)
2139 {
2140 if (prev == '*' && line[i] == '/')
2141 incomment = FALSE;
2142 }
2143 else if (prev == '/' && line[i] == '*')
2144 {
2145 incomment = TRUE;
2146 }
2147 else if (prev == '/' && line[i] == '/')
2148 {
2149 return FALSE;
2150 }
2151 }
2152
2153 prev = line[i];
2154 }
2155
2156 return incomment == FALSE && instring == 0;
2157}
2158
2159/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002160 * Search for variable declaration of "ptr[len]".
2161 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2162 * current file ("gD").
2163 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002164 * Return FAIL when not found.
2165 */
2166 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002167find_decl(
2168 char_u *ptr,
2169 int len,
2170 int locally,
2171 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002172 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002173{
2174 char_u *pat;
John Marriott8c85a2a2024-05-20 19:18:26 +02002175 size_t patlen;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002176 pos_T old_pos;
2177 pos_T par_pos;
2178 pos_T found_pos;
2179 int t;
2180 int save_p_ws;
2181 int save_p_scs;
2182 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002183 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002184 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002185 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002186
2187 if ((pat = alloc(len + 7)) == NULL)
2188 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002189
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002190 // Put "\V" before the pattern to avoid that the special meaning of "."
2191 // and "~" causes trouble.
John Marriott8c85a2a2024-05-20 19:18:26 +02002192 patlen = vim_snprintf((char *)pat, len + 7,
2193 vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s", len, ptr);
2194
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 old_pos = curwin->w_cursor;
2196 save_p_ws = p_ws;
2197 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002198 p_ws = FALSE; // don't wrap around end of file now
2199 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002201 // With "gD" go to line 1.
2202 // With "gd" Search back for the start of the current function, then go
2203 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002204 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002206 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002208 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 }
2210 else
2211 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002212 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2214 --curwin->w_cursor.lnum;
2215 }
2216 curwin->w_cursor.col = 0;
2217
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002218 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002219 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002220 for (;;)
2221 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002222 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
John Marriott8c85a2a2024-05-20 19:18:26 +02002223 pat, patlen, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002224 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002225 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002226
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002227 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002228 {
2229 pos_T *pos;
2230
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002231 // Check that the block the match is in doesn't end before the
2232 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002233 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2234 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2235 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002236 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002237 // There can't be a useful match before the end of this block.
2238 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002239 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002240 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002241 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002242 }
2243
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002244 if (t == FAIL)
2245 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002246 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002247 if (found_pos.lnum != 0)
2248 {
2249 curwin->w_cursor = found_pos;
2250 t = OK;
2251 }
2252 break;
2253 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002254 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002255 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002256 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002257 ++curwin->w_cursor.lnum;
2258 curwin->w_cursor.col = 0;
2259 continue;
2260 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002261 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2262
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002263 // If the current position is not a valid identifier and a previous
2264 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002265 if (!valid && found_pos.lnum != 0)
2266 {
2267 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002268 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002269 }
2270
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002271 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002272 if (valid && !locally)
2273 break;
2274 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002275 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002276 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002277 if (found_pos.lnum != 0)
2278 curwin->w_cursor = found_pos;
2279 break;
2280 }
2281
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002282 // For finding a local variable and the match is before the "{" or
2283 // inside a comment, continue searching. For K&R style function
2284 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002285 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002286 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002287 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002288 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002289 // Remove SEARCH_START from flags to avoid getting stuck at one
2290 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002291 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002293
2294 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002296 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 curwin->w_cursor = old_pos;
2298 }
2299 else
2300 {
2301 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002302 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 reset_search_dir();
2304 }
2305
2306 vim_free(pat);
2307 p_ws = save_p_ws;
2308 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002309
2310 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311}
2312
2313/*
2314 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2315 * lines rather than lines in the file.
2316 * 'dist' must be positive.
2317 *
2318 * Return OK if able to move cursor, FAIL otherwise.
2319 */
Luuk van Baalcb204e62024-04-02 20:49:45 +02002320 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002321nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322{
Dylan Thacker-Smithb2d124c2024-03-24 09:43:25 +01002323
2324 int linelen = linetabsize_no_outer(curwin, curwin->w_cursor.lnum);
2325
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 int retval = OK;
2327 int atend = FALSE;
2328 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002329 int col_off1; // margin offset for first screen line
2330 int col_off2; // margin offset for wrapped screen line
2331 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002332 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333
2334 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002335 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336
2337 col_off1 = curwin_col_off();
2338 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002339 width1 = curwin->w_width - col_off1;
2340 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002341 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002342 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002345 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002346 // Instead of sticking at the last character of the buffer line we
2347 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 if (curwin->w_curswant == MAXCOL)
2349 {
2350 atend = TRUE;
2351 validate_virtcol();
2352 if (width1 <= 0)
2353 curwin->w_curswant = 0;
2354 else
2355 {
2356 curwin->w_curswant = width1 - 1;
2357 if (curwin->w_virtcol > curwin->w_curswant)
2358 curwin->w_curswant += ((curwin->w_virtcol
2359 - curwin->w_curswant - 1) / width2 + 1) * width2;
2360 }
2361 }
2362 else
2363 {
2364 if (linelen > width1)
2365 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2366 else
2367 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002368 if (curwin->w_curswant >= (colnr_T)n)
2369 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 }
2371
2372 while (dist--)
2373 {
2374 if (dir == BACKWARD)
2375 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002376 if ((long)curwin->w_curswant >= width1
2377#ifdef FEAT_FOLDING
2378 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2379#endif
2380 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002381 // Move back within the line. This can give a negative value
2382 // for w_curswant if width1 < width2 (with cpoptions+=n),
2383 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 curwin->w_curswant -= width2;
2385 else
2386 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002387 // to previous line
Luuk van Baala109f392023-06-02 14:16:35 +01002388 if (curwin->w_cursor.lnum <= 1)
Bram Moolenaare71996b2021-01-21 17:03:07 +01002389 {
2390 retval = FAIL;
2391 break;
2392 }
Luuk van Baala109f392023-06-02 14:16:35 +01002393 cursor_up_inner(curwin, 1);
2394
Dylan Thacker-Smithb2d124c2024-03-24 09:43:25 +01002395 linelen = linetabsize_no_outer(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 if (linelen > width1)
2397 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2398 + 1) * width2;
2399 }
2400 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002401 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 {
2403 if (linelen > width1)
2404 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2405 else
2406 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002407 if (curwin->w_curswant + width2 < (colnr_T)n
2408#ifdef FEAT_FOLDING
2409 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2410#endif
2411 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002412 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 curwin->w_curswant += width2;
2414 else
2415 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002416 // to next line
Luuk van Baala109f392023-06-02 14:16:35 +01002417 if (curwin->w_cursor.lnum
2418 >= curwin->w_buffer->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 {
2420 retval = FAIL;
2421 break;
2422 }
Luuk van Baala109f392023-06-02 14:16:35 +01002423 cursor_down_inner(curwin, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 curwin->w_curswant %= width2;
Luuk van Baala109f392023-06-02 14:16:35 +01002425
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002426 // Check if the cursor has moved below the number display
2427 // when width1 < width2 (with cpoptions+=n). Subtract width2
2428 // to get a negative value for w_curswant, which will get
2429 // clipped to column 0.
2430 if (curwin->w_curswant >= width1)
2431 curwin->w_curswant -= width2;
Dylan Thacker-Smithb2d124c2024-03-24 09:43:25 +01002432 linelen = linetabsize_no_outer(curwin, curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 }
2434 }
2435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002438 if (virtual_active() && atend)
2439 coladvance(MAXCOL);
2440 else
2441 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2444 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002445 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002446 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002447
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002448 // Check for landing on a character that got split at the end of the
2449 // last line. We want to advance a screenline, not end up in the same
2450 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002452 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002453#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002454 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2455 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002456#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002457
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002458 c = (*mb_ptr2char)(ml_get_cursor());
2459 if (dir == FORWARD && virtcol < curwin->w_curswant
2460 && (curwin->w_curswant <= (colnr_T)width1)
2461 && !vim_isprintc(c) && c > 255)
2462 oneright();
2463
Bram Moolenaar773b1582014-08-29 14:20:51 +02002464 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 && (curwin->w_curswant < (colnr_T)width1
2466 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2467 : ((curwin->w_curswant - width1) % width2
2468 > (colnr_T)width2 / 2)))
2469 --curwin->w_cursor.col;
2470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471
2472 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002473 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002474 adjust_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475
2476 return retval;
2477}
2478
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479/*
2480 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2481 * cap->arg must be TRUE for CTRL-E.
2482 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002483 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002484nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485{
2486 if (!checkclearop(cap->oap))
2487 scroll_redraw(cap->arg, cap->count1);
2488}
2489
2490/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002491 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2492 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2493 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002494 */
2495 static int
2496nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2497{
2498 int nchar = *nchar_arg;
2499 long n;
2500
2501 // "z123{nchar}": edit the count before obtaining {nchar}
2502 if (checkclearop(cap->oap))
2503 return FALSE;
2504 n = nchar - '0';
2505
2506 for (;;)
2507 {
2508#ifdef USE_ON_FLY_SCROLL
2509 dont_scroll = TRUE; // disallow scrolling here
2510#endif
2511 ++no_mapping;
2512 ++allow_keys; // no mapping for nchar, but allow key codes
2513 nchar = plain_vgetc();
2514 LANGMAP_ADJUST(nchar, TRUE);
2515 --no_mapping;
2516 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002517 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002518
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002519 if (nchar == K_DEL || nchar == K_KDEL)
2520 n /= 10;
2521 else if (VIM_ISDIGIT(nchar))
Christian Brabandt58f9bef2023-11-14 21:02:30 +01002522 {
Christian Brabandt22cbc8a2023-11-19 10:47:21 +01002523 if (vim_append_digit_long(&n, nchar - '0') == FAIL)
Christian Brabandt58f9bef2023-11-14 21:02:30 +01002524 {
2525 clearopbeep(cap->oap);
2526 break;
2527 }
Christian Brabandt58f9bef2023-11-14 21:02:30 +01002528 }
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002529 else if (nchar == CAR)
2530 {
2531#ifdef FEAT_GUI
2532 need_mouse_correct = TRUE;
2533#endif
2534 win_setheight((int)n);
2535 break;
2536 }
2537 else if (nchar == 'l'
2538 || nchar == 'h'
2539 || nchar == K_LEFT
2540 || nchar == K_RIGHT)
2541 {
2542 cap->count1 = n ? n * cap->count1 : cap->count1;
2543 *nchar_arg = nchar;
2544 return TRUE;
2545 }
2546 else
2547 {
2548 clearopbeep(cap->oap);
2549 break;
2550 }
2551 }
2552 cap->oap->op_type = OP_NOP;
2553 return FALSE;
2554}
2555
2556#ifdef FEAT_SPELL
2557/*
2558 * "zug" and "zuw": undo "zg" and "zw"
2559 * "zg": add good word to word list
2560 * "zw": add wrong word to word list
2561 * "zG": add good word to temp word list
2562 * "zW": add wrong word to temp word list
2563 */
2564 static int
2565nv_zg_zw(cmdarg_T *cap, int nchar)
2566{
2567 char_u *ptr = NULL;
2568 int len;
2569 int undo = FALSE;
2570
2571 if (nchar == 'u')
2572 {
2573 ++no_mapping;
2574 ++allow_keys; // no mapping for nchar, but allow key codes
2575 nchar = plain_vgetc();
2576 LANGMAP_ADJUST(nchar, TRUE);
2577 --no_mapping;
2578 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002579 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002580
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002581 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2582 {
2583 clearopbeep(cap->oap);
2584 return OK;
2585 }
2586 undo = TRUE;
2587 }
2588
2589 if (checkclearop(cap->oap))
2590 return OK;
2591 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2592 return FAIL;
2593 if (ptr == NULL)
2594 {
2595 pos_T pos = curwin->w_cursor;
2596
2597 // Find bad word under the cursor. When 'spell' is
2598 // off this fails and find_ident_under_cursor() is
2599 // used below.
2600 emsg_off++;
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02002601 len = spell_move_to(curwin, FORWARD, SMT_ALL, TRUE, NULL);
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002602 emsg_off--;
2603 if (len != 0 && curwin->w_cursor.col <= pos.col)
2604 ptr = ml_get_pos(&curwin->w_cursor);
2605 curwin->w_cursor = pos;
2606 }
2607
2608 if (ptr == NULL
2609 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2610 return FAIL;
2611 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2612 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2613 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2614
2615 return OK;
2616}
2617#endif
2618
2619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 * Commands that start with "z".
2621 */
2622 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002623nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624{
2625 long n;
2626 colnr_T col;
2627 int nchar = cap->nchar;
2628#ifdef FEAT_FOLDING
2629 long old_fdl = curwin->w_p_fdl;
2630 int old_fen = curwin->w_p_fen;
2631#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002632 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002634 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 if (
2638#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002639 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2640 // and "zC" only in Visual mode. "zj" and "zk" are motion
2641 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 cap->nchar != 'f' && cap->nchar != 'F'
2643 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2644 && cap->nchar != 'j' && cap->nchar != 'k'
2645 &&
2646#endif
2647 checkclearop(cap->oap))
2648 return;
2649
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002650 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2651 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2653 && cap->count0
2654 && cap->count0 != curwin->w_cursor.lnum)
2655 {
2656 setpcmark();
2657 if (cap->count0 > curbuf->b_ml.ml_line_count)
2658 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2659 else
2660 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002661 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 }
2663
2664 switch (nchar)
2665 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002666 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 case '+':
2668 if (cap->count0 == 0)
2669 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002670 // No count given: put cursor at the line below screen
2671 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2673 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2674 else
2675 curwin->w_cursor.lnum = curwin->w_botline;
2676 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002677 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 case NL:
2679 case CAR:
2680 case K_KENTER:
2681 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002682 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683
2684 case 't': scroll_cursor_top(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002685 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002686 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 break;
2688
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002689 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002691 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002693 case 'z': scroll_cursor_halfway(TRUE, FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002694 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002695 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 break;
2697
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002698 // "z^", "z-" and "zb": put cursor at bottom of screen
2699 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2700 // when <count> is at bottom of window, and puts that one at
2701 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 if (cap->count0 != 0)
2703 {
2704 scroll_cursor_bot(0, TRUE);
2705 curwin->w_cursor.lnum = curwin->w_topline;
2706 }
2707 else if (curwin->w_topline == 1)
2708 curwin->w_cursor.lnum = 1;
2709 else
2710 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002711 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 case '-':
2713 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002714 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715
2716 case 'b': scroll_cursor_bot(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002717 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002718 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 break;
2720
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002721 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002723 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002724 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002726 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 case 'h':
2728 case K_LEFT:
2729 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002730 (void)set_leftcol((colnr_T)cap->count1 > curwin->w_leftcol
2731 ? 0 : curwin->w_leftcol - (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 break;
2733
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002734 // "zL" - scroll window left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002735 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002736 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002738 // "zl" - scroll window to the left if not wrapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 case 'l':
2740 case K_RIGHT:
2741 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002742 (void)set_leftcol(curwin->w_leftcol + (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 break;
2744
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002745 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 case 's': if (!curwin->w_p_wrap)
2747 {
2748#ifdef FEAT_FOLDING
2749 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002750 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 else
2752#endif
2753 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002754 if ((long)col > siso)
2755 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 else
2757 col = 0;
2758 if (curwin->w_leftcol != col)
2759 {
2760 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002761 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 }
2763 }
2764 break;
2765
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002766 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 case 'e': if (!curwin->w_p_wrap)
2768 {
2769#ifdef FEAT_FOLDING
2770 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002771 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 else
2773#endif
2774 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002775 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002776 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 col = 0;
2778 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002779 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 if (curwin->w_leftcol != col)
2781 {
2782 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002783 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 }
2785 }
2786 break;
2787
Christian Brabandt2fa93842021-05-30 22:17:25 +02002788 // "zp", "zP" in block mode put without addind trailing spaces
2789 case 'P':
2790 case 'p': nv_put(cap);
2791 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002792 // "zy" Yank without trailing spaces
2793 case 'y': nv_operator(cap);
2794 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002796 // "zF": create fold command
2797 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 case 'F':
2799 case 'f': if (foldManualAllowed(TRUE))
2800 {
2801 cap->nchar = 'f';
2802 nv_operator(cap);
2803 curwin->w_p_fen = TRUE;
2804
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002805 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2807 {
2808 nv_operator(cap);
2809 finish_op = TRUE;
2810 }
2811 }
2812 else
2813 clearopbeep(cap->oap);
2814 break;
2815
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002816 // "zd": delete fold at cursor
2817 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 case 'd':
2819 case 'D': if (foldManualAllowed(FALSE))
2820 {
2821 if (VIsual_active)
2822 nv_operator(cap);
2823 else
2824 deleteFold(curwin->w_cursor.lnum,
2825 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2826 }
2827 break;
2828
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002829 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 case 'E': if (foldmethodIsManual(curwin))
2831 {
2832 clearFolding(curwin);
2833 changed_window_setting();
2834 }
2835 else if (foldmethodIsMarker(curwin))
2836 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2837 TRUE, FALSE);
2838 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002839 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 break;
2841
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002842 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 case 'n': curwin->w_p_fen = FALSE;
2844 break;
2845
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002846 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 case 'N': curwin->w_p_fen = TRUE;
2848 break;
2849
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002850 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2852 break;
2853
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002854 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2856 openFold(curwin->w_cursor.lnum, cap->count1);
2857 else
2858 {
2859 closeFold(curwin->w_cursor.lnum, cap->count1);
2860 curwin->w_p_fen = TRUE;
2861 }
2862 break;
2863
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002864 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2866 openFoldRecurse(curwin->w_cursor.lnum);
2867 else
2868 {
2869 closeFoldRecurse(curwin->w_cursor.lnum);
2870 curwin->w_p_fen = TRUE;
2871 }
2872 break;
2873
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002874 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 case 'o': if (VIsual_active)
2876 nv_operator(cap);
2877 else
2878 openFold(curwin->w_cursor.lnum, cap->count1);
2879 break;
2880
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002881 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 case 'O': if (VIsual_active)
2883 nv_operator(cap);
2884 else
2885 openFoldRecurse(curwin->w_cursor.lnum);
2886 break;
2887
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002888 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 case 'c': if (VIsual_active)
2890 nv_operator(cap);
2891 else
2892 closeFold(curwin->w_cursor.lnum, cap->count1);
2893 curwin->w_p_fen = TRUE;
2894 break;
2895
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002896 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 case 'C': if (VIsual_active)
2898 nv_operator(cap);
2899 else
2900 closeFoldRecurse(curwin->w_cursor.lnum);
2901 curwin->w_p_fen = TRUE;
2902 break;
2903
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002904 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 case 'v': foldOpenCursor();
2906 break;
2907
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002908 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002910 curwin->w_foldinvalid = TRUE; // recompute folds
2911 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 foldOpenCursor();
2913 break;
2914
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002915 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002917 curwin->w_foldinvalid = TRUE; // recompute folds
2918 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 break;
2920
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002921 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002923 {
2924 curwin->w_p_fdl -= cap->count1;
2925 if (curwin->w_p_fdl < 0)
2926 curwin->w_p_fdl = 0;
2927 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002928 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 curwin->w_p_fen = TRUE;
2930 break;
2931
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002932 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002934 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 curwin->w_p_fen = TRUE;
2936 break;
2937
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002938 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002939 case 'r': curwin->w_p_fdl += cap->count1;
2940 {
2941 int d = getDeepestNesting();
2942
2943 if (curwin->w_p_fdl >= d)
2944 curwin->w_p_fdl = d;
2945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 break;
2947
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002948 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002950 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 break;
2952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002953 case 'j': // "zj" move to next fold downwards
2954 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2956 cap->count1) == FAIL)
2957 clearopbeep(cap->oap);
2958 break;
2959
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002960#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002962#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002963 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002964 case 'g': // "zg": add good word to word list
2965 case 'w': // "zw": add wrong word to word list
2966 case 'G': // "zG": add good word to temp word list
2967 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002968 if (nv_zg_zw(cap, nchar) == FAIL)
2969 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002970 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002971
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002972 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00002973 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00002974 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002975 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002976#endif
2977
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 default: clearopbeep(cap->oap);
2979 }
2980
2981#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002982 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 if (old_fen != curwin->w_p_fen)
2984 {
2985# ifdef FEAT_DIFF
2986 win_T *wp;
2987
2988 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
2989 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002990 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 FOR_ALL_WINDOWS(wp)
2992 {
2993 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
2994 {
2995 wp->w_p_fen = curwin->w_p_fen;
2996 changed_window_setting_win(wp);
2997 }
2998 }
2999 }
3000# endif
3001 changed_window_setting();
3002 }
3003
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003004 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 if (old_fdl != curwin->w_p_fdl)
3006 newFoldLevel();
3007#endif
3008}
3009
3010#ifdef FEAT_GUI
3011/*
3012 * Vertical scrollbar movement.
3013 */
3014 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003015nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016{
3017 if (cap->oap->op_type != OP_NOP)
3018 clearopbeep(cap->oap);
3019
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003020 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 gui_do_scroll();
3022}
3023
3024/*
3025 * Horizontal scrollbar movement.
3026 */
3027 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003028nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029{
3030 if (cap->oap->op_type != OP_NOP)
3031 clearopbeep(cap->oap);
3032
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003033 // Even if an operator was pending, we still want to scroll
Christopher Plewright44c22092022-11-15 17:43:36 +00003034 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035}
3036#endif
3037
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003038#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003039/*
3040 * Click in GUI tab.
3041 */
3042 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003043nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003044{
3045 if (cap->oap->op_type != OP_NOP)
3046 clearopbeep(cap->oap);
3047
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003048 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003049 goto_tabpage(current_tab);
3050}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003051
3052/*
3053 * Selected item in tab line menu.
3054 */
3055 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003056nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003057{
3058 if (cap->oap->op_type != OP_NOP)
3059 clearopbeep(cap->oap);
3060
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003061 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003062 handle_tabmenu();
3063}
3064
3065/*
3066 * Handle selecting an item of the GUI tab line menu.
3067 * Used in Normal and Insert mode.
3068 */
3069 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003070handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003071{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003072 switch (current_tabmenu)
3073 {
3074 case TABLINE_MENU_CLOSE:
3075 if (current_tab == 0)
3076 do_cmdline_cmd((char_u *)"tabclose");
3077 else
3078 {
3079 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3080 current_tab);
3081 do_cmdline_cmd(IObuff);
3082 }
3083 break;
3084
3085 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003086 if (current_tab == 0)
3087 do_cmdline_cmd((char_u *)"$tabnew");
3088 else
3089 {
3090 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3091 current_tab - 1);
3092 do_cmdline_cmd(IObuff);
3093 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003094 break;
3095
3096 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003097 if (current_tab == 0)
3098 do_cmdline_cmd((char_u *)"browse $tabnew");
3099 else
3100 {
3101 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3102 current_tab - 1);
3103 do_cmdline_cmd(IObuff);
3104 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003105 break;
3106 }
3107}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003108#endif
3109
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110/*
3111 * "Q" command.
3112 */
3113 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003114nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003116 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003118 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003119 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 do_exmode(FALSE);
3121}
3122
3123/*
3124 * Handle a ":" command.
3125 */
3126 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003127nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003129 int old_p_im;
3130 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003131 int is_cmdkey = cap->cmdchar == K_COMMAND
3132 || cap->cmdchar == K_SCRIPT_COMMAND;
3133 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134
Bram Moolenaar957cf672020-11-12 14:21:06 +01003135 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003137 nv_operator(cap);
3138 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003140
3141 if (cap->oap->op_type != OP_NOP)
3142 {
3143 // Using ":" as a movement is characterwise exclusive.
3144 cap->oap->motion_type = MCHAR;
3145 cap->oap->inclusive = FALSE;
3146 }
3147 else if (cap->count0 && !is_cmdkey)
3148 {
3149 // translate "count:" into ":.,.+(count - 1)"
3150 stuffcharReadbuff('.');
3151 if (cap->count0 > 1)
3152 {
3153 stuffReadbuff((char_u *)",.+");
3154 stuffnumReadbuff((long)cap->count0 - 1L);
3155 }
3156 }
3157
3158 // When typing, don't type below an old message
3159 if (KeyTyped)
3160 compute_cmdrow();
3161
3162 old_p_im = p_im;
3163
3164 // get a command line and execute it
3165 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3166 if (is_cmdkey)
3167 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3168 else
3169 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
3170
3171 // If 'insertmode' changed, enter or exit Insert mode
3172 if (p_im != old_p_im)
3173 {
3174 if (p_im)
3175 restart_edit = 'i';
3176 else
3177 restart_edit = 0;
3178 }
3179
3180 if (cmd_result == FAIL)
3181 // The Ex command failed, do not execute the operator.
3182 clearop(cap->oap);
3183 else if (cap->oap->op_type != OP_NOP
3184 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
John Marriott02d7a6c2024-02-26 21:21:17 +01003185 || cap->oap->start.col > ml_get_len(cap->oap->start.lnum)
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003186 || did_emsg
3187 ))
3188 // The start of the operator has become invalid by the Ex command.
3189 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190}
3191
3192/*
3193 * Handle CTRL-G command.
3194 */
3195 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003196nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003198 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 {
3200 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003201 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 showmode();
3203 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003204 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003205 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 fileinfo((int)cap->count0, FALSE, TRUE);
3207}
3208
3209/*
3210 * Handle CTRL-H <Backspace> command.
3211 */
3212 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003213nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 if (VIsual_active && VIsual_select)
3216 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003217 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 v_visop(cap);
3219 }
3220 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 nv_left(cap);
3222}
3223
3224/*
3225 * CTRL-L: clear screen and redraw.
3226 */
3227 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003228nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003230 if (checkclearop(cap->oap))
3231 return;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003232
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003233#ifdef FEAT_SYN_HL
3234 // Clear all syntax states to force resyncing.
3235 syn_stack_free_all(curwin->w_s);
3236# ifdef FEAT_RELTIME
3237 {
3238 win_T *wp;
3239
3240 FOR_ALL_WINDOWS(wp)
3241 wp->w_s->b_syn_slow = FALSE;
3242 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003243# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003245 redraw_later(UPD_CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003246#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3247# ifdef VIMDLL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003248 if (!gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003249# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003250 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252}
3253
3254/*
3255 * CTRL-O: In Select mode: switch to Visual mode for one command.
3256 * Otherwise: Go to older pcmark.
3257 */
3258 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003259nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 if (VIsual_active && VIsual_select)
3262 {
3263 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003264 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003266 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 }
3268 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 {
3270 cap->count1 = -cap->count1;
3271 nv_pcmark(cap);
3272 }
3273}
3274
3275/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003276 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3277 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 */
3279 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003280nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281{
3282 if (!checkclearopq(cap->oap))
3283 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3284 GETF_SETMARK|GETF_ALT, FALSE);
3285}
3286
3287/*
3288 * "Z" commands.
3289 */
3290 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003291nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003293 if (checkclearopq(cap->oap))
3294 return;
3295
3296 switch (cap->nchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003298 // "ZZ": equivalent to ":x".
3299 case 'Z': do_cmdline_cmd((char_u *)"x");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 break;
3301
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003302 // "ZQ": equivalent to ":q!" (Elvis compatible).
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003303 case 'Q': do_cmdline_cmd((char_u *)"q!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 break;
3305
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003306 default: clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 }
3308}
3309
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310/*
3311 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3312 */
3313 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003314do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315{
3316 oparg_T oa;
3317 cmdarg_T ca;
3318
3319 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003320 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 ca.oap = &oa;
3322 ca.cmdchar = c1;
3323 ca.nchar = c2;
3324 nv_ident(&ca);
3325}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326
3327/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003328 * 'K' normal-mode command. Get the command to lookup the keyword under the
3329 * cursor.
3330 */
3331 static int
3332nv_K_getcmd(
3333 cmdarg_T *cap,
3334 char_u *kp,
3335 int kp_help,
3336 int kp_ex,
3337 char_u **ptr_arg,
3338 int n,
3339 char_u *buf,
John Marriott8c85a2a2024-05-20 19:18:26 +02003340 size_t bufsize,
3341 size_t *buflen)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003342{
3343 char_u *ptr = *ptr_arg;
3344 int isman;
3345 int isman_s;
3346
3347 if (kp_help)
3348 {
3349 // in the help buffer
3350 STRCPY(buf, "he! ");
John Marriott8c85a2a2024-05-20 19:18:26 +02003351 *buflen = STRLEN_LITERAL("he! ");
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003352 return n;
3353 }
3354
3355 if (kp_ex)
3356 {
3357 // 'keywordprog' is an ex command
3358 if (cap->count0 != 0)
John Marriott8c85a2a2024-05-20 19:18:26 +02003359 *buflen = vim_snprintf((char *)buf, bufsize, "%s %ld ", kp, cap->count0);
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003360 else
John Marriott8c85a2a2024-05-20 19:18:26 +02003361 *buflen = vim_snprintf((char *)buf, bufsize, "%s ", kp);
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003362 return n;
3363 }
3364
3365 // An external command will probably use an argument starting
3366 // with "-" as an option. To avoid trouble we skip the "-".
3367 while (*ptr == '-' && n > 0)
3368 {
3369 ++ptr;
3370 --n;
3371 }
3372 if (n == 0)
3373 {
3374 // found dashes only
3375 emsg(_(e_no_identifier_under_cursor));
3376 vim_free(buf);
3377 *ptr_arg = ptr;
3378 return 0;
3379 }
3380
3381 // When a count is given, turn it into a range. Is this
3382 // really what we want?
3383 isman = (STRCMP(kp, "man") == 0);
3384 isman_s = (STRCMP(kp, "man -s") == 0);
3385 if (cap->count0 != 0 && !(isman || isman_s))
John Marriott8c85a2a2024-05-20 19:18:26 +02003386 *buflen = vim_snprintf((char *)buf, bufsize, ".,.+%ld! ", cap->count0 - 1);
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003387 else
John Marriott8c85a2a2024-05-20 19:18:26 +02003388 *buflen = vim_snprintf((char *)buf, bufsize, "! ");
3389
3390 if (cap->count0 == 0 && isman_s)
3391 *buflen += vim_snprintf((char *)buf + *buflen, bufsize - *buflen, "man ");
3392 else
3393 *buflen += vim_snprintf((char *)buf + *buflen, bufsize - *buflen, "%s ", kp);
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003394 if (cap->count0 != 0 && (isman || isman_s))
John Marriott8c85a2a2024-05-20 19:18:26 +02003395 *buflen += vim_snprintf((char *)buf + *buflen, bufsize - *buflen, "%ld ", cap->count0);
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003396
3397 *ptr_arg = ptr;
3398 return n;
3399}
3400
3401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 * Handle the commands that use the word under the cursor.
3403 * [g] CTRL-] :ta to current identifier
3404 * [g] 'K' run program for current identifier
3405 * [g] '*' / to current identifier or string
3406 * [g] '#' ? to current identifier or string
3407 * g ']' :tselect for current identifier
3408 */
3409 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003410nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411{
3412 char_u *ptr = NULL;
3413 char_u *buf;
John Marriott8c85a2a2024-05-20 19:18:26 +02003414 size_t bufsize;
3415 size_t buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003416 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003418 char_u *kp; // value of 'keywordprg'
3419 int kp_help; // 'keywordprg' is ":he"
3420 int kp_ex; // 'keywordprg' starts with ":"
3421 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003423 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003424 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003427 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 {
3429 cmdchar = cap->nchar;
3430 g_cmd = TRUE;
3431 }
3432 else
3433 {
3434 cmdchar = cap->cmdchar;
3435 g_cmd = FALSE;
3436 }
3437
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003438 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 cmdchar = '#';
3440
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003441 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3443 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3445 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 if (checkclearopq(cap->oap))
3447 return;
3448 }
3449
3450 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3451 (cmdchar == '*' || cmdchar == '#')
3452 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3453 {
3454 clearop(cap->oap);
3455 return;
3456 }
3457
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003458 // Allocate buffer to put the command in. Inserting backslashes can
3459 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3460 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3462 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3463 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003464 if (kp_help && *skipwhite(ptr) == NUL)
3465 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003466 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003467 return;
3468 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003469 kp_ex = (*kp == ':');
John Marriott8c85a2a2024-05-20 19:18:26 +02003470 bufsize = (size_t)(n * 2 + 30 + STRLEN(kp));
3471 buf = alloc(bufsize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 if (buf == NULL)
3473 return;
3474 buf[0] = NUL;
John Marriott8c85a2a2024-05-20 19:18:26 +02003475 buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476
3477 switch (cmdchar)
3478 {
3479 case '*':
3480 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003481 // Put cursor at start of word, makes search skip the word
3482 // under the cursor.
3483 // Call setpcmark() first, so "*``" puts the cursor back where
3484 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 setpcmark();
3486 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3487
3488 if (!g_cmd && vim_iswordp(ptr))
John Marriott8c85a2a2024-05-20 19:18:26 +02003489 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 STRCPY(buf, "\\<");
John Marriott8c85a2a2024-05-20 19:18:26 +02003491 buflen = STRLEN_LITERAL("\\<");
3492 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003493 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 break;
3495
3496 case 'K':
John Marriott8c85a2a2024-05-20 19:18:26 +02003497 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, bufsize, &buflen);
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003498 if (n == 0)
3499 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 break;
3501
3502 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003503 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504#ifdef FEAT_CSCOPE
3505 if (p_cst)
John Marriott8c85a2a2024-05-20 19:18:26 +02003506 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 STRCPY(buf, "cstag ");
John Marriott8c85a2a2024-05-20 19:18:26 +02003508 buflen = STRLEN_LITERAL("cstag ");
3509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 else
3511#endif
John Marriott8c85a2a2024-05-20 19:18:26 +02003512 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 STRCPY(buf, "ts ");
John Marriott8c85a2a2024-05-20 19:18:26 +02003514 buflen = STRLEN_LITERAL("ts ");
3515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 break;
3517
3518 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003519 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 if (curbuf->b_help)
John Marriott8c85a2a2024-05-20 19:18:26 +02003521 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 STRCPY(buf, "he! ");
John Marriott8c85a2a2024-05-20 19:18:26 +02003523 buflen = STRLEN_LITERAL("he! ");
3524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003526 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003527 if (g_cmd)
John Marriott8c85a2a2024-05-20 19:18:26 +02003528 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003529 STRCPY(buf, "tj ");
John Marriott8c85a2a2024-05-20 19:18:26 +02003530 buflen = STRLEN_LITERAL("tj ");
3531 }
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003532 else if (cap->count0 == 0)
John Marriott8c85a2a2024-05-20 19:18:26 +02003533 {
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003534 STRCPY(buf, "ta ");
John Marriott8c85a2a2024-05-20 19:18:26 +02003535 buflen = STRLEN_LITERAL("ta ");
3536 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003537 else
John Marriott8c85a2a2024-05-20 19:18:26 +02003538 buflen = vim_snprintf((char *)buf, bufsize, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 }
3541
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003542 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003543 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 {
John Marriott8c85a2a2024-05-20 19:18:26 +02003545 size_t plen;
3546
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003547 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003548 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003549 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003550 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003551 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003552 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003553 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003554 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003555 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003557 vim_free(buf);
3558 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 }
John Marriott8c85a2a2024-05-20 19:18:26 +02003560 plen = STRLEN(p);
3561 newbuf = vim_realloc(buf, buflen + plen + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003562 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003563 {
3564 vim_free(buf);
3565 vim_free(p);
3566 return;
3567 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003568 buf = newbuf;
John Marriott8c85a2a2024-05-20 19:18:26 +02003569 STRCPY(buf + buflen, p);
3570 buflen += plen;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003571 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003573 else
3574 {
3575 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003576 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003577 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003578 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003579 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003580 {
3581 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003582 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003583 aux_ptr = (char_u *)"";
3584 else
3585 aux_ptr = (char_u *)"\\|\"\n[";
3586 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003587 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003588 aux_ptr = (char_u *)"\\|\"\n*?[";
3589
John Marriott8c85a2a2024-05-20 19:18:26 +02003590 p = buf + buflen;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003591 while (n-- > 0)
3592 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003593 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003594 if (vim_strchr(aux_ptr, *ptr) != NULL)
3595 *p++ = '\\';
John Marriott8c85a2a2024-05-20 19:18:26 +02003596
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003597 // When current byte is a part of multibyte character, copy all
3598 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003599 if (has_mbyte)
3600 {
3601 int i;
3602 int len = (*mb_ptr2len)(ptr) - 1;
3603
3604 for (i = 0; i < len && n >= 1; ++i, --n)
3605 *p++ = *ptr++;
3606 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003607 *p++ = *ptr++;
3608 }
3609 *p = NUL;
John Marriott8c85a2a2024-05-20 19:18:26 +02003610 buflen = p - buf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003613 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (cmdchar == '*' || cmdchar == '#')
3615 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003616 if (!g_cmd && (has_mbyte
3617 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3618 : vim_iswordc(ptr[-1])))
John Marriott8c85a2a2024-05-20 19:18:26 +02003619 {
3620 STRCPY(buf + buflen, "\\>");
3621 buflen += STRLEN_LITERAL("\\>");
3622 }
Bram Moolenaard7663c22019-08-06 21:59:57 +02003623
3624 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003625 init_history();
John Marriott8c85a2a2024-05-20 19:18:26 +02003626 add_to_history(HIST_SEARCH, buf, buflen, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003627
John Marriott8c85a2a2024-05-20 19:18:26 +02003628 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, buflen, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 }
3630 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003631 {
3632 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003634 g_tag_at_cursor = FALSE;
3635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636
3637 vim_free(buf);
3638}
3639
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640/*
3641 * Get visually selected text, within one line only.
3642 * Returns FAIL if more than one line selected.
3643 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003644 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003645get_visual_text(
3646 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003647 char_u **pp, // return: start of selected text
3648 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649{
3650 if (VIsual_mode != 'V')
3651 unadjust_for_sel();
3652 if (VIsual.lnum != curwin->w_cursor.lnum)
3653 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003654 if (cap != NULL)
3655 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 return FAIL;
3657 }
3658 if (VIsual_mode == 'V')
3659 {
3660 *pp = ml_get_curline();
John Marriott02d7a6c2024-02-26 21:21:17 +01003661 *lenp = (int)ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 }
3663 else
3664 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003665 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 {
3667 *pp = ml_get_pos(&curwin->w_cursor);
3668 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3669 }
3670 else
3671 {
3672 *pp = ml_get_pos(&VIsual);
3673 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3674 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003675 if (**pp == NUL)
3676 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003677 if (*lenp > 0)
3678 {
3679 if (has_mbyte)
3680 // Correct the length to include all bytes of the last
3681 // character.
3682 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3683 else if ((*pp)[*lenp - 1] == NUL)
3684 // Do not include a trailing NUL.
3685 *lenp -= 1;
3686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 }
3688 reset_VIsual_and_resel();
3689 return OK;
3690}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691
3692/*
3693 * CTRL-T: backwards in tag stack
3694 */
3695 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003696nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697{
3698 if (!checkclearopq(cap->oap))
3699 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3700}
3701
3702/*
3703 * Handle scrolling command 'H', 'L' and 'M'.
3704 */
3705 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003706nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707{
3708 int used = 0;
3709 long n;
3710#ifdef FEAT_FOLDING
3711 linenr_T lnum;
3712#endif
3713 int half;
3714
3715 cap->oap->motion_type = MLINE;
3716 setpcmark();
3717
3718 if (cap->cmdchar == 'L')
3719 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003720 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 curwin->w_cursor.lnum = curwin->w_botline - 1;
3722 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3723 curwin->w_cursor.lnum = 1;
3724 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003725 {
3726#ifdef FEAT_FOLDING
3727 if (hasAnyFolding(curwin))
3728 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003729 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003730 for (n = cap->count1 - 1; n > 0
3731 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3732 {
3733 (void)hasFolding(curwin->w_cursor.lnum,
3734 &curwin->w_cursor.lnum, NULL);
Bram Moolenaar232bdaa2023-01-13 14:17:58 +00003735 if (curwin->w_cursor.lnum > curwin->w_topline)
3736 --curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003737 }
3738 }
3739 else
3740#endif
3741 curwin->w_cursor.lnum -= cap->count1 - 1;
3742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 }
3744 else
3745 {
3746 if (cap->cmdchar == 'M')
3747 {
3748#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003749 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 used -= diff_check_fill(curwin, curwin->w_topline)
3751 - curwin->w_topfill;
3752#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003753 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3755 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3756 {
3757#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003758 // Count half he number of filler lines to be "below this
3759 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3761 + n) / 2 >= half)
3762 {
3763 --n;
3764 break;
3765 }
3766#endif
3767 used += plines(curwin->w_topline + n);
3768 if (used >= half)
3769 break;
3770#ifdef FEAT_FOLDING
3771 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3772 n = lnum - curwin->w_topline;
3773#endif
3774 }
3775 if (n > 0 && used > curwin->w_height)
3776 --n;
3777 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003778 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003779 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003781#ifdef FEAT_FOLDING
3782 if (hasAnyFolding(curwin))
3783 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003784 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003785 lnum = curwin->w_topline;
3786 while (n-- > 0 && lnum < curwin->w_botline - 1)
3787 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003788 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003789 ++lnum;
3790 }
3791 n = lnum - curwin->w_topline;
3792 }
3793#endif
3794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 curwin->w_cursor.lnum = curwin->w_topline + n;
3796 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3797 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3798 }
3799
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003800 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003801 if (cap->oap->op_type == OP_NOP)
3802 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 beginline(BL_SOL | BL_FIX);
3804}
3805
3806/*
3807 * Cursor right commands.
3808 */
3809 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003810nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811{
3812 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003813 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003815 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3816 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003817 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003818 if (mod_mask & MOD_MASK_CTRL)
3819 cap->arg = TRUE;
3820 nv_wordcmd(cap);
3821 return;
3822 }
3823
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 cap->oap->motion_type = MCHAR;
3825 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003826 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003828 // In virtual edit mode, there's no such thing as "past_line", as lines
3829 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003831 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832
3833 for (n = cap->count1; n > 0; --n)
3834 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003835 if ((!past_line && oneright() == FAIL)
3836 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003837 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003839 // <Space> wraps to next line if 'whichwrap' has 's'.
3840 // 'l' wraps to next line if 'whichwrap' has 'l'.
3841 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 if ( ((cap->cmdchar == ' '
3843 && vim_strchr(p_ww, 's') != NULL)
3844 || (cap->cmdchar == 'l'
3845 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003846 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 && vim_strchr(p_ww, '>') != NULL))
3848 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3849 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003850 // When deleting we also count the NL as a character.
3851 // Set cap->oap->inclusive when last char in the line is
3852 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003853 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003855 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 cap->oap->inclusive = TRUE;
3857 else
3858 {
3859 ++curwin->w_cursor.lnum;
3860 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 curwin->w_set_curswant = TRUE;
3863 cap->oap->inclusive = FALSE;
3864 }
3865 continue;
3866 }
3867 if (cap->oap->op_type == OP_NOP)
3868 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003869 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 if (n == cap->count1)
3871 beep_flush();
3872 }
3873 else
3874 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003875 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 cap->oap->inclusive = TRUE;
3877 }
3878 break;
3879 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003880 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 {
3882 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 if (virtual_active())
3884 oneright();
3885 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003888 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 ++curwin->w_cursor.col;
3891 }
3892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 }
3894#ifdef FEAT_FOLDING
3895 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3896 && cap->oap->op_type == OP_NOP)
3897 foldOpenCursor();
3898#endif
3899}
3900
3901/*
3902 * Cursor left commands.
3903 *
3904 * Returns TRUE when operator end should not be adjusted.
3905 */
3906 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003907nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908{
3909 long n;
3910
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003911 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3912 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003913 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003914 if (mod_mask & MOD_MASK_CTRL)
3915 cap->arg = 1;
3916 nv_bck_word(cap);
3917 return;
3918 }
3919
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 cap->oap->motion_type = MCHAR;
3921 cap->oap->inclusive = FALSE;
3922 for (n = cap->count1; n > 0; --n)
3923 {
3924 if (oneleft() == FAIL)
3925 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003926 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3927 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3928 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 if ( (((cap->cmdchar == K_BS
3930 || cap->cmdchar == Ctrl_H)
3931 && vim_strchr(p_ww, 'b') != NULL)
3932 || (cap->cmdchar == 'h'
3933 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003934 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 && vim_strchr(p_ww, '<') != NULL))
3936 && curwin->w_cursor.lnum > 1)
3937 {
3938 --(curwin->w_cursor.lnum);
3939 coladvance((colnr_T)MAXCOL);
3940 curwin->w_set_curswant = TRUE;
3941
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003942 // When the NL before the first char has to be deleted we
3943 // put the cursor on the NUL after the previous line.
3944 // This is a very special case, be careful!
3945 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 if ( (cap->oap->op_type == OP_DELETE
3947 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003948 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003950 char_u *cp = ml_get_cursor();
3951
3952 if (*cp != NUL)
3953 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003954 if (has_mbyte)
3955 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3956 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003957 ++curwin->w_cursor.col;
3958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 cap->retval |= CA_NO_ADJ_OP_END;
3960 }
3961 continue;
3962 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003963 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3965 beep_flush();
3966 break;
3967 }
3968 }
3969#ifdef FEAT_FOLDING
3970 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3971 && cap->oap->op_type == OP_NOP)
3972 foldOpenCursor();
3973#endif
3974}
3975
3976/*
3977 * Cursor up commands.
3978 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3979 */
3980 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003981nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003983 if (mod_mask & MOD_MASK_SHIFT)
3984 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003985 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003986 cap->arg = BACKWARD;
3987 nv_page(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003988 return;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003989 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003990
3991 cap->oap->motion_type = MLINE;
3992 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
3993 clearopbeep(cap->oap);
3994 else if (cap->arg)
3995 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996}
3997
3998/*
3999 * Cursor down commands.
4000 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4001 */
4002 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004003nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004005 if (mod_mask & MOD_MASK_SHIFT)
4006 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004007 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004008 cap->arg = FORWARD;
4009 nv_page(cap);
4010 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004011#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004012 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004013 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4014 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004016 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004018 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004019 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 cmdwin_result = CAR;
4021 else
Bram Moolenaarf2732452018-06-03 14:47:35 +02004022#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004023 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004024 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4025 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4026 {
4027 invoke_prompt_callback();
4028 if (restart_edit == 0)
4029 restart_edit = 'a';
4030 }
4031 else
4032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 {
4034 cap->oap->motion_type = MLINE;
4035 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4036 clearopbeep(cap->oap);
4037 else if (cap->arg)
4038 beginline(BL_WHITE | BL_FIX);
4039 }
4040 }
4041}
4042
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043/*
4044 * Grab the file name under the cursor and edit it.
4045 */
4046 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004047nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048{
4049 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004050 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004052 if (check_text_or_curbuf_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 return;
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004054
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004055#ifdef FEAT_PROP_POPUP
4056 if (ERROR_IF_TERM_POPUP_WINDOW)
4057 return;
4058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
Colin Kennedy21570352024-03-03 16:16:47 +01004060 if (!check_can_set_curbuf_disabled())
4061 return;
4062
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004063 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064
4065 if (ptr != NULL)
4066 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004067 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004068 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004069 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004071 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004072 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004073 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004074 {
4075 curwin->w_cursor.lnum = lnum;
4076 check_cursor_lnum();
4077 beginline(BL_SOL | BL_FIX);
4078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 vim_free(ptr);
4080 }
4081 else
4082 clearop(cap->oap);
4083}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084
4085/*
4086 * <End> command: to end of current line or last line.
4087 */
4088 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004089nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004091 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004093 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004095 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
4097 nv_dollar(cap);
4098}
4099
4100/*
4101 * Handle the "$" command.
4102 */
4103 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004104nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105{
4106 cap->oap->motion_type = MCHAR;
4107 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004108 // In virtual mode when off the edge of a line and an operator
4109 // is pending (whew!) keep the cursor where it is.
4110 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 if (!virtual_active() || gchar_cursor() != NUL
4112 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004113 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 if (cursor_down((long)(cap->count1 - 1),
4115 cap->oap->op_type == OP_NOP) == FAIL)
4116 clearopbeep(cap->oap);
4117#ifdef FEAT_FOLDING
4118 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4119 foldOpenCursor();
4120#endif
4121}
4122
4123/*
4124 * Implementation of '?' and '/' commands.
4125 * If cap->arg is TRUE don't set PC mark.
4126 */
4127 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004128nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129{
4130 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004131 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132
4133 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4134 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004135 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 cap->cmdchar = 'g';
4137 cap->nchar = '?';
4138 nv_operator(cap);
4139 return;
4140 }
4141
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004142 // When using 'incsearch' the cursor may be moved to set a different search
4143 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004144 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145
4146 if (cap->searchbuf == NULL)
4147 {
4148 clearop(oap);
4149 return;
4150 }
4151
John Marriott8c85a2a2024-05-20 19:18:26 +02004152 (void)normal_search(cap, cap->cmdchar, cap->searchbuf, STRLEN(cap->searchbuf),
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004153 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004154 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155}
4156
LemonBoya4399382022-04-09 21:04:08 +01004157
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158/*
4159 * Handle "N" and "n" commands.
4160 * cap->arg is SEARCH_REV for "N", 0 for "n".
4161 */
4162 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004163nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004165 pos_T old = curwin->w_cursor;
4166 int wrapped = FALSE;
John Marriott8c85a2a2024-05-20 19:18:26 +02004167 int i = normal_search(cap, 0, NULL, 0, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004168
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004169 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004170 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004171 // Avoid getting stuck on the current cursor position, which can
4172 // happen when an offset is given and the cursor is on the last char
4173 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004174 cap->count1 += 1;
John Marriott8c85a2a2024-05-20 19:18:26 +02004175 (void)normal_search(cap, 0, NULL, 0, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004176 cap->count1 -= 1;
4177 }
LemonBoya4399382022-04-09 21:04:08 +01004178
4179#ifdef FEAT_SEARCH_EXTRA
4180 // Redraw the window to refresh the highlighted matches.
4181 if (i > 0 && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004182 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184}
4185
4186/*
4187 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4188 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004189 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004191 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004192normal_search(
4193 cmdarg_T *cap,
4194 int dir,
4195 char_u *pat,
John Marriott8c85a2a2024-05-20 19:18:26 +02004196 size_t patlen,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004197 int opt, // extra flags for do_search()
4198 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199{
4200 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004201 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004202#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004203 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205
4206 cap->oap->motion_type = MCHAR;
4207 cap->oap->inclusive = FALSE;
4208 cap->oap->use_reg_one = TRUE;
4209 curwin->w_set_curswant = TRUE;
4210
Bram Moolenaara80faa82020-04-12 19:37:17 +02004211 CLEAR_FIELD(sia);
John Marriott8c85a2a2024-05-20 19:18:26 +02004212 i = do_search(cap->oap, dir, dir, pat, patlen, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004213 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4214 if (wrapped != NULL)
4215 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 if (i == 0)
4217 clearop(cap->oap);
4218 else
4219 {
4220 if (i == 2)
4221 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223#ifdef FEAT_FOLDING
4224 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4225 foldOpenCursor();
4226#endif
4227 }
LemonBoya4399382022-04-09 21:04:08 +01004228#ifdef FEAT_SEARCH_EXTRA
4229 // Redraw the window to refresh the highlighted matches.
4230 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004231 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004234 // "/$" will put the cursor after the end of the line, may need to
4235 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 check_cursor();
John Marriott8c85a2a2024-05-20 19:18:26 +02004237
Bram Moolenaar46539112015-02-17 15:43:57 +01004238 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239}
4240
4241/*
4242 * Character search commands.
4243 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4244 * ',' and FALSE for ';'.
4245 * cap->nchar is NUL for ',' and ';' (repeat the search)
4246 */
4247 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004248nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249{
4250 int t_cmd;
4251
4252 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4253 t_cmd = TRUE;
4254 else
4255 t_cmd = FALSE;
4256
4257 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004260 clearopbeep(cap->oap);
4261 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004263
4264 curwin->w_set_curswant = TRUE;
4265 // Include a Tab for "tx" and for "dfx".
4266 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4267 && (t_cmd || cap->oap->op_type != OP_NOP))
4268 {
4269 colnr_T scol, ecol;
4270
4271 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4272 curwin->w_cursor.coladd = ecol - scol;
4273 }
4274 else
4275 curwin->w_cursor.coladd = 0;
4276 adjust_for_sel(cap);
4277#ifdef FEAT_FOLDING
4278 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4279 foldOpenCursor();
4280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281}
4282
4283/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004284 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4285 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4286 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4287 * "[m" or "]m" search for prev/next start of (Java) method.
4288 * "[M" or "]M" search for prev/next end of (Java) method.
4289 */
4290 static void
4291nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4292{
4293 pos_T new_pos = {0, 0, 0};
4294 pos_T *pos = NULL; // init for GCC
4295 pos_T prev_pos;
4296 long n;
4297 int findc;
4298 int c;
4299
4300 if (cap->nchar == '*')
4301 cap->nchar = '/';
4302 prev_pos.lnum = 0;
4303 if (cap->nchar == 'm' || cap->nchar == 'M')
4304 {
4305 if (cap->cmdchar == '[')
4306 findc = '{';
4307 else
4308 findc = '}';
4309 n = 9999;
4310 }
4311 else
4312 {
4313 findc = cap->nchar;
4314 n = cap->count1;
4315 }
4316 for ( ; n > 0; --n)
4317 {
4318 if ((pos = findmatchlimit(cap->oap, findc,
4319 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4320 {
4321 if (new_pos.lnum == 0) // nothing found
4322 {
4323 if (cap->nchar != 'm' && cap->nchar != 'M')
4324 clearopbeep(cap->oap);
4325 }
4326 else
4327 pos = &new_pos; // use last one found
4328 break;
4329 }
4330 prev_pos = new_pos;
4331 curwin->w_cursor = *pos;
4332 new_pos = *pos;
4333 }
4334 curwin->w_cursor = *old_pos;
4335
4336 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4337 // brought us to the match for "[m" and "]M" when inside a method.
4338 // Try finding the '{' or '}' we want to be at.
4339 // Also repeat for the given count.
4340 if (cap->nchar == 'm' || cap->nchar == 'M')
4341 {
4342 // norm is TRUE for "]M" and "[m"
4343 int norm = ((findc == '{') == (cap->nchar == 'm'));
4344
4345 n = cap->count1;
4346 // found a match: we were inside a method
4347 if (prev_pos.lnum != 0)
4348 {
4349 pos = &prev_pos;
4350 curwin->w_cursor = prev_pos;
4351 if (norm)
4352 --n;
4353 }
4354 else
4355 pos = NULL;
4356 while (n > 0)
4357 {
4358 for (;;)
4359 {
4360 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4361 {
4362 // if not found anything, that's an error
4363 if (pos == NULL)
4364 clearopbeep(cap->oap);
4365 n = 0;
4366 break;
4367 }
4368 c = gchar_cursor();
4369 if (c == '{' || c == '}')
4370 {
4371 // Must have found end/start of class: use it.
4372 // Or found the place to be at.
4373 if ((c == findc && norm) || (n == 1 && !norm))
4374 {
4375 new_pos = curwin->w_cursor;
4376 pos = &new_pos;
4377 n = 0;
4378 }
4379 // if no match found at all, we started outside of the
4380 // class and we're inside now. Just go on.
4381 else if (new_pos.lnum == 0)
4382 {
4383 new_pos = curwin->w_cursor;
4384 pos = &new_pos;
4385 }
4386 // found start/end of other method: go to match
4387 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004388 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4389 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004390 n = 0;
4391 else
4392 curwin->w_cursor = *pos;
4393 break;
4394 }
4395 }
4396 --n;
4397 }
4398 curwin->w_cursor = *old_pos;
4399 if (pos == NULL && new_pos.lnum != 0)
4400 clearopbeep(cap->oap);
4401 }
4402 if (pos != NULL)
4403 {
4404 setpcmark();
4405 curwin->w_cursor = *pos;
4406 curwin->w_set_curswant = TRUE;
4407#ifdef FEAT_FOLDING
4408 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4409 && cap->oap->op_type == OP_NOP)
4410 foldOpenCursor();
4411#endif
4412 }
4413}
4414
4415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 * "[" and "]" commands.
4417 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4418 */
4419 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004420nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004423 pos_T *pos = NULL; // init for GCC
4424 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 int flag;
4426 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427
4428 cap->oap->motion_type = MCHAR;
4429 cap->oap->inclusive = FALSE;
4430 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004431 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004433 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 if (cap->nchar == 'f')
4435 nv_gotofile(cap);
4436 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437
4438#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004439 // Find the occurrence(s) of the identifier or define under cursor
4440 // in current and included files or jump to the first occurrence.
4441 //
4442 // search list jump
4443 // fwd bwd fwd bwd fwd bwd
4444 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4445 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004446 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 {
4448 char_u *ptr;
4449 int len;
4450
4451 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4452 clearop(cap->oap);
4453 else
4454 {
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004455 // Make a copy, if the line was changed it will be freed.
4456 ptr = vim_strnsave(ptr, len);
4457 if (ptr == NULL)
4458 return;
4459
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 find_pattern_in_path(ptr, 0, len, TRUE,
Keith Thompson184f71c2024-01-04 21:19:04 +01004461 cap->count0 == 0 ? !SAFE_isupper(cap->nchar) : FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4463 cap->count1,
Keith Thompson184f71c2024-01-04 21:19:04 +01004464 SAFE_isupper(cap->nchar) ? ACTION_SHOW_ALL :
4465 SAFE_islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
Colin Kennedy21570352024-03-03 16:16:47 +01004467 (linenr_T)MAXLNUM,
zeertzjqd9be94c2024-07-14 10:20:20 +02004468 FALSE);
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004469 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 curwin->w_set_curswant = TRUE;
4471 }
4472 }
4473 else
4474#endif
4475
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004476 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4477 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4478 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4479 // "[m" or "]m" search for prev/next start of (Java) method.
4480 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 if ( (cap->cmdchar == '['
4482 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4483 || (cap->cmdchar == ']'
4484 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004485 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004487 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 else if (cap->nchar == '[' || cap->nchar == ']')
4489 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004490 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 flag = '{';
4492 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004493 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494
4495 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004496 // Imitate strange Vi behaviour: When using "]]" with an operator
4497 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004498 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 (cap->oap->op_type != OP_NOP
4500 && cap->arg == FORWARD && flag == '{')))
4501 clearopbeep(cap->oap);
4502 else
4503 {
4504 if (cap->oap->op_type == OP_NOP)
4505 beginline(BL_WHITE | BL_FIX);
4506#ifdef FEAT_FOLDING
4507 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4508 foldOpenCursor();
4509#endif
4510 }
4511 }
4512
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004513 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 else if (cap->nchar == 'p' || cap->nchar == 'P')
4515 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004516 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 }
4518
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004519 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 else if (cap->nchar == '\'' || cap->nchar == '`')
4521 {
4522 pos = &curwin->w_cursor;
4523 for (n = cap->count1; n > 0; --n)
4524 {
4525 prev_pos = *pos;
4526 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4527 cap->nchar == '\'');
4528 if (pos == NULL)
4529 break;
4530 }
4531 if (pos == NULL)
4532 pos = &prev_pos;
4533 nv_cursormark(cap, cap->nchar == '\'', pos);
4534 }
4535
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004536 // [ or ] followed by a middle mouse click: put selected text with
4537 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004538 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 {
4540 (void)do_mouse(cap->oap, cap->nchar,
4541 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4542 cap->count1, PUT_FIXINDENT);
4543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544
4545#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004546 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 else if (cap->nchar == 'z')
4548 {
4549 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4550 cap->count1) == FAIL)
4551 clearopbeep(cap->oap);
4552 }
4553#endif
4554
4555#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004556 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 else if (cap->nchar == 'c')
4558 {
4559 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4560 cap->count1) == FAIL)
4561 clearopbeep(cap->oap);
4562 }
4563#endif
4564
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004565#ifdef FEAT_SPELL
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02004566 // "[r", "[s", "[S", "]r", "]s" and "]S": move to next spell error.
4567 else if (cap->nchar == 'r' || cap->nchar == 's' || cap->nchar == 'S')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004568 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004569 setpcmark();
4570 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004571 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
Christ van Willegen - van Noort8e4c4c72024-05-17 18:49:27 +02004572 cap->nchar == 's' ? SMT_ALL :
4573 cap->nchar == 'r' ? SMT_RARE :
4574 SMT_BAD, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004575 {
4576 clearopbeep(cap->oap);
4577 break;
4578 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004579 else
4580 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004581# ifdef FEAT_FOLDING
4582 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4583 foldOpenCursor();
4584# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004585 }
4586#endif
4587
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004588 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 else
4590 clearopbeep(cap->oap);
4591}
4592
4593/*
4594 * Handle Normal mode "%" command.
4595 */
4596 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004597nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598{
4599 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004600#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 linenr_T lnum = curwin->w_cursor.lnum;
4602#endif
4603
4604 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004605 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 {
4607 if (cap->count0 > 100)
4608 clearopbeep(cap->oap);
4609 else
4610 {
4611 cap->oap->motion_type = MLINE;
4612 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004613 // Round up, so 'normal 100%' always jumps at the line line.
4614 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4615 // overflow on 32-bits, so use a formula with less accuracy
4616 // to avoid overflows.
4617 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4619 / 100L * cap->count0;
4620 else
4621 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4622 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004623 if (curwin->w_cursor.lnum < 1)
4624 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4626 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4627 beginline(BL_SOL | BL_FIX);
4628 }
4629 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004630 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 {
4632 cap->oap->motion_type = MCHAR;
4633 cap->oap->use_reg_one = TRUE;
4634 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4635 clearopbeep(cap->oap);
4636 else
4637 {
4638 setpcmark();
4639 curwin->w_cursor = *pos;
4640 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 }
4644 }
4645#ifdef FEAT_FOLDING
4646 if (cap->oap->op_type == OP_NOP
4647 && lnum != curwin->w_cursor.lnum
4648 && (fdo_flags & FDO_PERCENT)
4649 && KeyTyped)
4650 foldOpenCursor();
4651#endif
4652}
4653
4654/*
4655 * Handle "(" and ")" commands.
4656 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4657 */
4658 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004659nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660{
4661 cap->oap->motion_type = MCHAR;
4662 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004663 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004664 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 curwin->w_set_curswant = TRUE;
4666
4667 if (findsent(cap->arg, cap->count1) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004669 clearopbeep(cap->oap);
4670 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004672
4673 // Don't leave the cursor on the NUL past end of line.
4674 adjust_cursor(cap->oap);
4675 curwin->w_cursor.coladd = 0;
4676#ifdef FEAT_FOLDING
4677 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4678 foldOpenCursor();
4679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680}
4681
4682/*
4683 * "m" command: Mark a position.
4684 */
4685 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004686nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004688 if (checkclearop(cap->oap))
4689 return;
4690
4691 if (setmark(cap->nchar) == FAIL)
4692 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693}
4694
4695/*
4696 * "{" and "}" commands.
4697 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4698 */
4699 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004700nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701{
4702 cap->oap->motion_type = MCHAR;
4703 cap->oap->inclusive = FALSE;
4704 cap->oap->use_reg_one = TRUE;
4705 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004706 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004708 clearopbeep(cap->oap);
4709 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004711
4712 curwin->w_cursor.coladd = 0;
4713#ifdef FEAT_FOLDING
4714 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4715 foldOpenCursor();
4716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717}
4718
4719/*
4720 * "u" command: Undo or make lower case.
4721 */
4722 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004723nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004725 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004727 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 cap->cmdchar = 'g';
4729 cap->nchar = 'u';
4730 nv_operator(cap);
4731 }
4732 else
4733 nv_kundo(cap);
4734}
4735
4736/*
4737 * <Undo> command.
4738 */
4739 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004740nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004742 if (checkclearopq(cap->oap))
4743 return;
4744
Bram Moolenaarf2732452018-06-03 14:47:35 +02004745#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004746 if (bt_prompt(curbuf))
4747 {
4748 clearopbeep(cap->oap);
4749 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004751#endif
4752 u_undo((int)cap->count1);
4753 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754}
4755
4756/*
4757 * Handle the "r" command.
4758 */
4759 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004760nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 int had_ctrl_v;
4763 long n;
4764
4765 if (checkclearop(cap->oap))
4766 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004767#ifdef FEAT_JOB_CHANNEL
4768 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4769 {
4770 clearopbeep(cap->oap);
4771 return;
4772 }
4773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004775 // get another character
Christian Brabandt2d63e4b2023-08-12 00:03:57 +02004776 if (cap->nchar == Ctrl_V || cap->nchar == Ctrl_Q)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 {
4778 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004779 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004780 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 if (cap->nchar > DEL)
4782 had_ctrl_v = NUL;
4783 }
4784 else
4785 had_ctrl_v = NUL;
4786
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004787 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004788 if (IS_SPECIAL(cap->nchar))
4789 {
4790 clearopbeep(cap->oap);
4791 return;
4792 }
4793
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004794 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 if (VIsual_active)
4796 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004797 if (got_int)
Christian Brabandt476733f2023-09-19 20:41:51 +02004798 got_int = FALSE;
Bram Moolenaard9820532013-11-04 01:41:17 +01004799 if (had_ctrl_v)
4800 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004801 // Use a special (negative) number to make a difference between a
4802 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004803 if (cap->nchar == CAR)
4804 cap->nchar = REPLACE_CR_NCHAR;
4805 else if (cap->nchar == NL)
4806 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 nv_operator(cap);
4809 return;
4810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004812 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 if (virtual_active())
4814 {
4815 if (u_save_cursor() == FAIL)
4816 return;
4817 if (gchar_cursor() == NUL)
4818 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004819 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 coladvance_force((colnr_T)(getviscol() + cap->count1));
4821 curwin->w_cursor.col -= cap->count1;
4822 }
4823 else if (gchar_cursor() == TAB)
4824 coladvance_force(getviscol());
4825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004827 // Abort if not enough characters to replace.
John Marriott02d7a6c2024-02-26 21:21:17 +01004828 if ((size_t)ml_get_cursor_len() < (unsigned)cap->count1
4829 || (has_mbyte && mb_charlen(ml_get_cursor()) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 {
4831 clearopbeep(cap->oap);
4832 return;
4833 }
4834
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004835 // Replacing with a TAB is done by edit() when it is complicated because
4836 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4837 // Other characters are done below to avoid problems with things like
4838 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4840 {
4841 stuffnumReadbuff(cap->count1);
4842 stuffcharReadbuff('R');
4843 stuffcharReadbuff('\t');
4844 stuffcharReadbuff(ESC);
4845 return;
4846 }
4847
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004848 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 if (u_save_cursor() == FAIL)
4850 return;
4851
4852 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4853 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004854 // Replace character(s) by a single newline.
4855 // Strange vi behaviour: Only one newline is inserted.
4856 // Delete the characters here.
4857 // Insert the newline with an insert command, takes care of
4858 // autoindent. The insert command depends on being on the last
4859 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004860 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 stuffcharReadbuff('\r');
4862 stuffcharReadbuff(ESC);
4863
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004864 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 invoke_edit(cap, TRUE, 'r', FALSE);
4866 }
4867 else
4868 {
4869 prep_redo(cap->oap->regname, cap->count1,
4870 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4871
4872 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 if (has_mbyte)
4874 {
4875 int old_State = State;
4876
4877 if (cap->ncharC1 != 0)
4878 AppendCharToRedobuff(cap->ncharC1);
4879 if (cap->ncharC2 != 0)
4880 AppendCharToRedobuff(cap->ncharC2);
4881
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004882 // This is slow, but it handles replacing a single-byte with a
4883 // multi-byte and the other way around. Also handles adding
4884 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 for (n = cap->count1; n > 0; --n)
4886 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004887 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004888 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4889 {
4890 int c = ins_copychar(curwin->w_cursor.lnum
4891 + (cap->nchar == Ctrl_Y ? -1 : 1));
4892 if (c != NUL)
4893 ins_char(c);
4894 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004895 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004896 ++curwin->w_cursor.col;
4897 }
4898 else
4899 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 State = old_State;
4901 if (cap->ncharC1 != 0)
4902 ins_char(cap->ncharC1);
4903 if (cap->ncharC2 != 0)
4904 ins_char(cap->ncharC2);
4905 }
4906 }
4907 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 {
John Marriott02d7a6c2024-02-26 21:21:17 +01004909 char_u *ptr;
4910
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004911 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 for (n = cap->count1; n > 0; --n)
4913 {
John Marriott02d7a6c2024-02-26 21:21:17 +01004914 // Get ptr again, because ins_copychar() and showmatch()
4915 // will have released the line.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004916 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004917 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4918 {
4919 int c = ins_copychar(curwin->w_cursor.lnum
4920 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004921
4922 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004923 if (c != NUL)
4924 ptr[curwin->w_cursor.col] = c;
4925 }
4926 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004927 {
4928 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004929 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 if (p_sm && msg_silent == 0)
4932 showmatch(cap->nchar);
4933 ++curwin->w_cursor.col;
4934 }
4935#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004936 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004938 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
John Marriott02d7a6c2024-02-26 21:21:17 +01004939 ptr = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940
Bram Moolenaar009b2592004-10-24 19:18:58 +00004941 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004942 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004944 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 }
4946#endif
4947
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004948 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 changed_bytes(curwin->w_cursor.lnum,
4950 (colnr_T)(curwin->w_cursor.col - cap->count1));
4951 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004952 --curwin->w_cursor.col; // cursor on the last replaced char
4953 // if the character on the left of the current cursor is a multi-byte
4954 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 if (has_mbyte)
4956 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 curbuf->b_op_end = curwin->w_cursor;
4958 curwin->w_set_curswant = TRUE;
4959 set_last_insert(cap->nchar);
4960 }
4961}
4962
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963/*
4964 * 'o': Exchange start and end of Visual area.
4965 * 'O': same, but in block mode exchange left and right corners.
4966 */
4967 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004968v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969{
4970 pos_T old_cursor;
4971 colnr_T left, right;
4972
4973 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4974 {
4975 old_cursor = curwin->w_cursor;
4976 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4977 curwin->w_cursor.lnum = VIsual.lnum;
4978 coladvance(left);
4979 VIsual = curwin->w_cursor;
4980
4981 curwin->w_cursor.lnum = old_cursor.lnum;
4982 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004983 // 'selection "exclusive" and cursor at right-bottom corner: move it
4984 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4986 ++curwin->w_curswant;
4987 coladvance(curwin->w_curswant);
4988 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004990 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 {
4992 curwin->w_cursor.lnum = VIsual.lnum;
4993 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
4994 ++right;
4995 coladvance(right);
4996 VIsual = curwin->w_cursor;
4997
4998 curwin->w_cursor.lnum = old_cursor.lnum;
4999 coladvance(left);
5000 curwin->w_curswant = left;
5001 }
5002 }
5003 else
5004 {
5005 old_cursor = curwin->w_cursor;
5006 curwin->w_cursor = VIsual;
5007 VIsual = old_cursor;
5008 curwin->w_set_curswant = TRUE;
5009 }
5010}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011
5012/*
5013 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5014 */
5015 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005016nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005018 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
5020 cap->cmdchar = 'c';
5021 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005022 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 VIsual_mode = 'V';
5024 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005025 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005027
5028 if (checkclearopq(cap->oap))
5029 return;
5030
5031 if (!curbuf->b_p_ma)
5032 emsg(_(e_cannot_make_changes_modifiable_is_off));
5033 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005035 if (virtual_active())
5036 coladvance(getviscol());
5037 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 }
5039}
5040
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041/*
5042 * "gr".
5043 */
5044 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005045nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 if (VIsual_active)
5048 {
5049 cap->cmdchar = 'r';
5050 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005051 nv_replace(cap); // Do same as "r" in Visual mode for now
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005052 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005054
zeertzjqf86dea82023-03-05 21:15:06 +00005055 if (checkclearopq(cap->oap))
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005056 return;
5057
5058 if (!curbuf->b_p_ma)
5059 emsg(_(e_cannot_make_changes_modifiable_is_off));
5060 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 {
Christian Brabandt2d63e4b2023-08-12 00:03:57 +02005062 if (cap->extra_char == Ctrl_V || cap->extra_char == Ctrl_Q)
5063 // get another character
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005064 cap->extra_char = get_literal(FALSE);
Bram Moolenaard6a4ea32023-02-25 14:24:44 +00005065 if (cap->extra_char < ' ')
5066 // Prefix a control character with CTRL-V to avoid it being used as
5067 // a command.
5068 stuffcharReadbuff(Ctrl_V);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005069 stuffcharReadbuff(cap->extra_char);
5070 stuffcharReadbuff(ESC);
5071 if (virtual_active())
5072 coladvance(getviscol());
5073 invoke_edit(cap, TRUE, 'v', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 }
5075}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076
5077/*
5078 * Swap case for "~" command, when it does not work like an operator.
5079 */
5080 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005081n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082{
5083 long n;
5084 pos_T startpos;
5085 int did_change = 0;
5086#ifdef FEAT_NETBEANS_INTG
5087 pos_T pos;
5088 char_u *ptr;
5089 int count;
5090#endif
5091
5092 if (checkclearopq(cap->oap))
5093 return;
5094
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005095 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 {
5097 clearopbeep(cap->oap);
5098 return;
5099 }
5100
5101 prep_redo_cmd(cap);
5102
5103 if (u_save_cursor() == FAIL)
5104 return;
5105
5106 startpos = curwin->w_cursor;
5107#ifdef FEAT_NETBEANS_INTG
5108 pos = startpos;
5109#endif
5110 for (n = cap->count1; n > 0; --n)
5111 {
5112 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5113 inc_cursor();
5114 if (gchar_cursor() == NUL)
5115 {
5116 if (vim_strchr(p_ww, '~') != NULL
5117 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5118 {
5119#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005120 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 {
5122 if (did_change)
5123 {
5124 ptr = ml_get(pos.lnum);
John Marriott02d7a6c2024-02-26 21:21:17 +01005125 count = (int)ml_get_len(pos.lnum) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005126 netbeans_removed(curbuf, pos.lnum, pos.col,
5127 (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01005128 // line may have been flushed, get it again
5129 ptr = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005131 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 }
5133 pos.col = 0;
5134 pos.lnum++;
5135 }
5136#endif
5137 ++curwin->w_cursor.lnum;
5138 curwin->w_cursor.col = 0;
5139 if (n > 1)
5140 {
5141 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5142 break;
5143 u_clearline();
5144 }
5145 }
5146 else
5147 break;
5148 }
5149 }
5150#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005151 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 {
5153 ptr = ml_get(pos.lnum);
5154 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005155 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5156 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 }
5158#endif
5159
5160
5161 check_cursor();
5162 curwin->w_set_curswant = TRUE;
5163 if (did_change)
5164 {
5165 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5166 0L);
5167 curbuf->b_op_start = startpos;
5168 curbuf->b_op_end = curwin->w_cursor;
5169 if (curbuf->b_op_end.col > 0)
5170 --curbuf->b_op_end.col;
5171 }
5172}
5173
5174/*
5175 * Move cursor to mark.
5176 */
5177 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005178nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179{
5180 if (check_mark(pos) == FAIL)
5181 clearop(cap->oap);
5182 else
5183 {
5184 if (cap->cmdchar == '\''
5185 || cap->cmdchar == '`'
5186 || cap->cmdchar == '['
5187 || cap->cmdchar == ']')
5188 setpcmark();
5189 curwin->w_cursor = *pos;
5190 if (flag)
5191 beginline(BL_WHITE | BL_FIX);
5192 else
5193 check_cursor();
5194 }
5195 cap->oap->motion_type = flag ? MLINE : MCHAR;
5196 if (cap->cmdchar == '`')
5197 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005198 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 curwin->w_set_curswant = TRUE;
5200}
5201
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202/*
5203 * Handle commands that are operators in Visual mode.
5204 */
5205 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005206v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207{
5208 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5209
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005210 // Uppercase means linewise, except in block mode, then "D" deletes till
5211 // the end of the line, and "C" replaces till EOL
Keith Thompson184f71c2024-01-04 21:19:04 +01005212 if (SAFE_isupper(cap->cmdchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 {
5214 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005215 {
5216 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5220 curwin->w_curswant = MAXCOL;
5221 }
5222 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5223 nv_operator(cap);
5224}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225
5226/*
5227 * "s" and "S" commands.
5228 */
5229 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005230nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005232#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005233 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005234 if (term_swap_diff() == OK)
5235 return;
5236#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005237#ifdef FEAT_JOB_CHANNEL
5238 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5239 {
5240 clearopbeep(cap->oap);
5241 return;
5242 }
5243#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005244 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 {
5246 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005247 {
5248 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 cap->cmdchar = 'c';
5252 nv_operator(cap);
5253 }
5254 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 nv_optrans(cap);
5256}
5257
5258/*
5259 * Abbreviated commands.
5260 */
5261 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005262nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263{
5264 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005265 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005267 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 if (VIsual_active)
5269 v_visop(cap);
5270 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 nv_optrans(cap);
5272}
5273
5274/*
5275 * Translate a command into another command.
5276 */
5277 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005278nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279{
5280 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5281 (char_u *)"d$", (char_u *)"c$",
5282 (char_u *)"cl", (char_u *)"cc",
5283 (char_u *)"yy", (char_u *)":s\r"};
5284 static char_u *str = (char_u *)"xXDCsSY&";
5285
5286 if (!checkclearopq(cap->oap))
5287 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005288 // In Vi "2D" doesn't delete the next line. Can't translate it
5289 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005290 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5291 {
5292 cap->oap->start = curwin->w_cursor;
5293 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005294#ifdef FEAT_EVAL
5295 set_op_var(OP_DELETE);
5296#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005297 cap->count1 = 1;
5298 nv_dollar(cap);
5299 finish_op = TRUE;
5300 ResetRedobuff();
5301 AppendCharToRedobuff('D');
5302 }
5303 else
5304 {
5305 if (cap->count0)
5306 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005307 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 }
5310 cap->opcount = 0;
5311}
5312
5313/*
5314 * "'" and "`" commands. Also for "g'" and "g`".
5315 * cap->arg is TRUE for "'" and "g'".
5316 */
5317 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005318nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319{
5320 pos_T *pos;
5321 int c;
5322#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005323 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005324 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325#endif
5326
5327 if (cap->cmdchar == 'g')
5328 c = cap->extra_char;
5329 else
5330 c = cap->nchar;
5331 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005332 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 {
5334 if (cap->arg)
5335 {
5336 check_cursor_lnum();
5337 beginline(BL_WHITE | BL_FIX);
5338 }
5339 else
5340 check_cursor();
5341 }
5342 else
5343 nv_cursormark(cap, cap->arg, pos);
5344
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005345 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 if (!virtual_active())
5347 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005348 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349#ifdef FEAT_FOLDING
5350 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005351 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005352 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 && (fdo_flags & FDO_MARK)
5354 && old_KeyTyped)
5355 foldOpenCursor();
5356#endif
5357}
5358
5359/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005360 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 */
5362 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005363nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005366#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005368 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005371 if (checkclearopq(cap->oap))
5372 return;
5373
5374 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005376 if (goto_tabpage_lastused() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 clearopbeep(cap->oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005378 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005380 if (cap->cmdchar == 'g')
5381 pos = movechangelist((int)cap->count1);
5382 else
5383 pos = movemark((int)cap->count1);
5384 if (pos == (pos_T *)-1) // jump to other file
5385 {
5386 curwin->w_set_curswant = TRUE;
5387 check_cursor();
5388 }
5389 else if (pos != NULL) // can jump
5390 nv_cursormark(cap, FALSE, pos);
5391 else if (cap->cmdchar == 'g')
5392 {
5393 if (curbuf->b_changelistlen == 0)
5394 emsg(_(e_changelist_is_empty));
5395 else if (cap->count1 < 0)
5396 emsg(_(e_at_start_of_changelist));
5397 else
5398 emsg(_(e_at_end_of_changelist));
5399 }
5400 else
5401 clearopbeep(cap->oap);
5402# ifdef FEAT_FOLDING
5403 if (cap->oap->op_type == OP_NOP
5404 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5405 && (fdo_flags & FDO_MARK)
5406 && old_KeyTyped)
5407 foldOpenCursor();
5408# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409}
5410
5411/*
5412 * Handle '"' command.
5413 */
5414 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005415nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416{
5417 if (checkclearop(cap->oap))
5418 return;
5419#ifdef FEAT_EVAL
5420 if (cap->nchar == '=')
5421 cap->nchar = get_expr_register();
5422#endif
5423 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5424 {
5425 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005426 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427#ifdef FEAT_EVAL
5428 set_reg_var(cap->oap->regname);
5429#endif
5430 }
5431 else
5432 clearopbeep(cap->oap);
5433}
5434
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435/*
5436 * Handle "v", "V" and "CTRL-V" commands.
5437 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5438 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005439 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 */
5441 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005442nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005444 if (cap->cmdchar == Ctrl_Q)
5445 cap->cmdchar = Ctrl_V;
5446
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005447 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5448 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 if (cap->oap->op_type != OP_NOP)
5450 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005451 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005452 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 return;
5454 }
5455
5456 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005457 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005459 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005461 else // toggle char/block mode
5462 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 VIsual_mode = cap->cmdchar;
5464 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005465 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005467 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005469 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 {
5471 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005472 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005474 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 VIsual = curwin->w_cursor;
5476
5477 VIsual_active = TRUE;
5478 VIsual_reselect = TRUE;
5479 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005480 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005483 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005484 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005485 // For V and ^V, we multiply the number of lines even if there
5486 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5488 {
5489 curwin->w_cursor.lnum +=
5490 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005491 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 }
5493 VIsual_mode = resel_VIsual_mode;
5494 if (VIsual_mode == 'v')
5495 {
5496 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005497 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005498 update_curswant_force();
Bram Moolenaar79c11e32023-01-10 17:29:29 +00005499 curwin->w_curswant += resel_VIsual_vcol * cap->count0;
5500 if (*p_sel != 'e')
5501 --curwin->w_curswant;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005504 curwin->w_curswant = resel_VIsual_vcol;
5505 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005507 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 {
5509 curwin->w_curswant = MAXCOL;
5510 coladvance((colnr_T)MAXCOL);
5511 }
5512 else if (VIsual_mode == Ctrl_V)
5513 {
Bram Moolenaar8f531662023-02-01 17:33:18 +00005514 // Update curswant on the original line, that is where "col" is
5515 // valid.
5516 linenr_T lnum = curwin->w_cursor.lnum;
5517 curwin->w_cursor.lnum = VIsual.lnum;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005518 update_curswant_force();
Bram Moolenaar8f531662023-02-01 17:33:18 +00005519 curwin->w_curswant += resel_VIsual_vcol * cap->count0 - 1;
5520 curwin->w_cursor.lnum = lnum;
Christian Brabandtbb955892024-12-16 22:49:15 +01005521 if (*p_sel == 'e')
5522 ++curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 coladvance(curwin->w_curswant);
5524 }
5525 else
5526 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005527 redraw_curbuf_later(UPD_INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 }
5529 else
5530 {
5531 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005532 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 may_start_select('c');
5534 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005535 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005536 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005537 if (cap->count0 > 0 && --cap->count1 > 0)
5538 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005539 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005540 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5541 nv_right(cap);
5542 else if (VIsual_mode == 'V')
5543 nv_down(cap);
5544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 }
5546 }
5547}
5548
5549/*
5550 * Start selection for Shift-movement keys.
5551 */
5552 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005553start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005555 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 may_start_select('k');
5557 n_start_visual_mode('v');
5558}
5559
5560/*
5561 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005562 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 */
5564 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005565may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005567 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5568 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569}
5570
5571/*
5572 * Start Visual mode "c".
5573 * Should set VIsual_select before calling this.
5574 */
5575 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005576n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005578#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005579 int cursor_line_was_concealed = curwin->w_p_cole > 0
5580 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005581#endif
5582
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 VIsual_mode = c;
5584 VIsual_active = TRUE;
5585 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005586
5587 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005588 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005589 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005590 {
5591 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 VIsual = curwin->w_cursor;
5595
5596#ifdef FEAT_FOLDING
5597 foldAdjustVisual();
5598#endif
5599
LemonBoy2bf52dd2022-04-09 18:17:34 +01005600 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005602#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005603 // Check if redraw is needed after changing the state.
5604 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005605#endif
5606
Bram Moolenaar09df3122006-01-23 22:23:09 +00005607 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005608 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005610 // Make sure the clipboard gets updated. Needed because start and
5611 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 clip_star.vmode = NUL;
5613#endif
5614
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005615 // Only need to redraw this line, unless still need to redraw an old
5616 // Visual area (when 'lazyredraw' is set).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005617 if (curwin->w_redr_type < UPD_INVERTED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 {
5619 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5620 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5621 }
5622}
5623
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624
5625/*
5626 * CTRL-W: Window commands
5627 */
5628 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005629nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005631 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005632 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005633 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005634 cap->cmdchar = ':';
5635 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005636 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005637 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005638 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005639 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640}
5641
5642/*
5643 * CTRL-Z: Suspend
5644 */
5645 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005646nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647{
5648 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005650 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005651 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652}
5653
5654/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005655 * "gv": Reselect the previous Visual area. If Visual already active,
5656 * exchange previous and current Visual area.
5657 */
5658 static void
5659nv_gv_cmd(cmdarg_T *cap)
5660{
5661 pos_T tpos;
5662 int i;
5663
5664 if (checkclearop(cap->oap))
5665 return;
5666
5667 if (curbuf->b_visual.vi_start.lnum == 0
5668 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5669 || curbuf->b_visual.vi_end.lnum == 0)
5670 {
5671 beep_flush();
5672 return;
5673 }
5674
5675 // set w_cursor to the start of the Visual area, tpos to the end
5676 if (VIsual_active)
5677 {
5678 i = VIsual_mode;
5679 VIsual_mode = curbuf->b_visual.vi_mode;
5680 curbuf->b_visual.vi_mode = i;
5681# ifdef FEAT_EVAL
5682 curbuf->b_visual_mode_eval = i;
5683# endif
5684 i = curwin->w_curswant;
5685 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5686 curbuf->b_visual.vi_curswant = i;
5687
5688 tpos = curbuf->b_visual.vi_end;
5689 curbuf->b_visual.vi_end = curwin->w_cursor;
5690 curwin->w_cursor = curbuf->b_visual.vi_start;
5691 curbuf->b_visual.vi_start = VIsual;
5692 }
5693 else
5694 {
5695 VIsual_mode = curbuf->b_visual.vi_mode;
5696 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5697 tpos = curbuf->b_visual.vi_end;
5698 curwin->w_cursor = curbuf->b_visual.vi_start;
5699 }
5700
5701 VIsual_active = TRUE;
5702 VIsual_reselect = TRUE;
5703
5704 // Set Visual to the start and w_cursor to the end of the Visual
5705 // area. Make sure they are on an existing character.
5706 check_cursor();
5707 VIsual = curwin->w_cursor;
5708 curwin->w_cursor = tpos;
5709 check_cursor();
5710 update_topline();
5711
5712 // When called from normal "g" command: start Select mode when
5713 // 'selectmode' contains "cmd". When called for K_SELECT, always
5714 // start Select mode.
5715 if (cap->arg)
5716 {
5717 VIsual_select = TRUE;
5718 VIsual_select_reg = 0;
5719 }
5720 else
5721 may_start_select('c');
5722 setmouse();
5723#ifdef FEAT_CLIPBOARD
5724 // Make sure the clipboard gets updated. Needed because start and
5725 // end are still the same, and the selection needs to be owned
5726 clip_star.vmode = NUL;
5727#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005728 redraw_curbuf_later(UPD_INVERTED);
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005729 showmode();
5730}
5731
5732/*
5733 * "g0", "g^" : Like "0" and "^" but for screen lines.
5734 * "gm": middle of "g0" and "g$".
5735 */
Luuk van Baalcb204e62024-04-02 20:49:45 +02005736 void
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005737nv_g_home_m_cmd(cmdarg_T *cap)
5738{
5739 int i;
5740 int flag = FALSE;
5741
5742 if (cap->nchar == '^')
5743 flag = TRUE;
5744
5745 cap->oap->motion_type = MCHAR;
5746 cap->oap->inclusive = FALSE;
5747 if (curwin->w_p_wrap && curwin->w_width != 0)
5748 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005749 int width1 = curwin->w_width - curwin_col_off();
5750 int width2 = width1 + curwin_col_off2();
5751 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005752
5753 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005754 virtcol = curwin->w_virtcol
5755#ifdef FEAT_PROP_POPUP
5756 - curwin->w_virtcol_first_char
5757#endif
5758 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005759 i = 0;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005760 if (virtcol >= (colnr_T)width1 && width2 > 0)
5761 i = (virtcol - width1) / width2 * width2 + width1;
Luuk van Baalcb204e62024-04-02 20:49:45 +02005762
5763 // When ending up below 'smoothscroll' marker, move just beyond it so
5764 // that skipcol is not adjusted later.
5765 if (curwin->w_skipcol > 0 && curwin->w_cursor.lnum == curwin->w_topline)
5766 {
5767 int overlap = sms_marker_overlap(curwin, -1);
5768 if (overlap > 0 && i == curwin->w_skipcol)
5769 i += overlap;
5770 }
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005771 }
5772 else
5773 i = curwin->w_leftcol;
5774 // Go to the middle of the screen line. When 'number' or
5775 // 'relativenumber' is on and lines are wrapping the middle can be more
5776 // to the left.
5777 if (cap->nchar == 'm')
5778 i += (curwin->w_width - curwin_col_off()
5779 + ((curwin->w_p_wrap && i > 0)
5780 ? curwin_col_off2() : 0)) / 2;
5781 coladvance((colnr_T)i);
5782 if (flag)
5783 {
5784 do
5785 i = gchar_cursor();
5786 while (VIM_ISWHITE(i) && oneright() == OK);
5787 curwin->w_valid &= ~VALID_WCOL;
5788 }
5789 curwin->w_set_curswant = TRUE;
Christian Brabandt9848fac2024-11-17 16:18:00 +01005790#ifdef FEAT_FOLDING
5791 if (hasAnyFolding(curwin))
5792 {
5793 validate_cheight();
5794 if (curwin->w_cline_folded)
5795 update_curswant_force();
5796 }
5797#endif
Luuk van Baal8667a562023-05-12 15:47:25 +01005798 adjust_skipcol();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005799}
5800
5801/*
5802 * "g_": to the last non-blank character in the line or <count> lines
5803 * downward.
5804 */
5805 static void
5806nv_g_underscore_cmd(cmdarg_T *cap)
5807{
5808 char_u *ptr;
5809
5810 cap->oap->motion_type = MCHAR;
5811 cap->oap->inclusive = TRUE;
5812 curwin->w_curswant = MAXCOL;
5813 if (cursor_down((long)(cap->count1 - 1),
5814 cap->oap->op_type == OP_NOP) == FAIL)
5815 {
5816 clearopbeep(cap->oap);
5817 return;
5818 }
5819
5820 ptr = ml_get_curline();
5821
5822 // In Visual mode we may end up after the line.
5823 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5824 --curwin->w_cursor.col;
5825
5826 // Decrease the cursor column until it's on a non-blank.
5827 while (curwin->w_cursor.col > 0
5828 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5829 --curwin->w_cursor.col;
5830 curwin->w_set_curswant = TRUE;
5831 adjust_for_sel(cap);
5832}
5833
5834/*
5835 * "g$" : Like "$" but for screen lines.
5836 */
5837 static void
5838nv_g_dollar_cmd(cmdarg_T *cap)
5839{
5840 oparg_T *oap = cap->oap;
5841 int i;
5842 int col_off = curwin_col_off();
Christian Brabandtb5f6fe92023-08-19 15:53:16 +02005843 int flag = FALSE;
5844
zeertzjq654bdbb2023-08-20 18:24:20 +02005845 if (cap->nchar == K_END || cap->nchar == K_KEND)
Christian Brabandtb5f6fe92023-08-19 15:53:16 +02005846 flag = TRUE;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005847
5848 oap->motion_type = MCHAR;
5849 oap->inclusive = TRUE;
5850 if (curwin->w_p_wrap && curwin->w_width != 0)
5851 {
5852 curwin->w_curswant = MAXCOL; // so we stay at the end
5853 if (cap->count1 == 1)
5854 {
5855 int width1 = curwin->w_width - col_off;
5856 int width2 = width1 + curwin_col_off2();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005857 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005858
5859 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005860 virtcol = curwin->w_virtcol
5861#ifdef FEAT_PROP_POPUP
5862 - curwin->w_virtcol_first_char
5863#endif
5864 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005865 i = width1 - 1;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005866 if (virtcol >= (colnr_T)width1)
5867 i += ((virtcol - width1) / width2 + 1)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005868 * width2;
5869 coladvance((colnr_T)i);
5870
5871 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005872 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005873 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5874 {
5875 // Check for landing on a character that got split at
5876 // the end of the line. We do not want to advance to
5877 // the next screen line.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005878 if (curwin->w_virtcol
5879#ifdef FEAT_PROP_POPUP
5880 - curwin->w_virtcol_first_char
5881#endif
5882 > (colnr_T)i)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005883 --curwin->w_cursor.col;
5884 }
5885 }
5886 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5887 clearopbeep(oap);
5888 }
5889 else
5890 {
5891 if (cap->count1 > 1)
5892 // if it fails, let the cursor still move to the last char
5893 (void)cursor_down(cap->count1 - 1, FALSE);
5894
5895 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5896 coladvance((colnr_T)i);
5897
5898 // if the character doesn't fit move one back
5899 if (curwin->w_cursor.col > 0
5900 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5901 {
5902 colnr_T vcol;
5903
5904 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5905 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5906 --curwin->w_cursor.col;
5907 }
5908
5909 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005910 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005911 }
Christian Brabandtb5f6fe92023-08-19 15:53:16 +02005912 if (flag)
5913 {
5914 do
5915 i = gchar_cursor();
5916 while (VIM_ISWHITE(i) && oneleft() == OK);
5917 curwin->w_valid &= ~VALID_WCOL;
5918 }
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005919}
5920
5921/*
5922 * "gi": start Insert at the last position.
5923 */
5924 static void
5925nv_gi_cmd(cmdarg_T *cap)
5926{
5927 int i;
5928
5929 if (curbuf->b_last_insert.lnum != 0)
5930 {
5931 curwin->w_cursor = curbuf->b_last_insert;
5932 check_cursor_lnum();
John Marriott02d7a6c2024-02-26 21:21:17 +01005933 i = (int)ml_get_curline_len();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005934 if (curwin->w_cursor.col > (colnr_T)i)
5935 {
5936 if (virtual_active())
5937 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5938 curwin->w_cursor.col = i;
5939 }
5940 }
5941 cap->cmdchar = 'i';
5942 nv_edit(cap);
5943}
5944
5945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 * Commands starting with "g".
5947 */
5948 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005949nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950{
5951 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953
5954 switch (cap->nchar)
5955 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005956 case Ctrl_A:
5957 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005959 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005960 if (!VIsual_active && cap->nchar == Ctrl_A)
5961 vim_mem_profile_dump();
5962 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005964 // "g^A/g^X": sequentially increment visually selected region
zeertzjqf86dea82023-03-05 21:15:06 +00005965 if (VIsual_active)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005966 {
5967 cap->arg = TRUE;
5968 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005969 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005970 nv_addsub(cap);
5971 }
5972 else
5973 clearopbeep(oap);
5974 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005976 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 case 'R':
5978 cap->arg = TRUE;
5979 nv_Replace(cap);
5980 break;
5981
5982 case 'r':
5983 nv_vreplace(cap);
5984 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985
5986 case '&':
5987 do_cmdline_cmd((char_u *)"%s//~/&");
5988 break;
5989
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005990 // "gv": Reselect the previous Visual area. If Visual already active,
5991 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005993 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005995
5996 // "gV": Don't reselect the previous Visual area after a Select mode
5997 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 case 'V':
5999 VIsual_reselect = FALSE;
6000 break;
6001
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006002 // "gh": start Select mode.
6003 // "gH": start Select line mode.
6004 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 case K_BS:
6006 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006007 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 case 'h':
6009 case 'H':
6010 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 cap->cmdchar = cap->nchar + ('v' - 'h');
6012 cap->arg = TRUE;
6013 nv_visual(cap);
6014 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02006015
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006016 // "gn", "gN" visually select next/previous search match
6017 // "gn" selects next match
6018 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02006019 case 'N':
6020 case 'n':
6021 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02006022 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02006023 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006025 // "gj" and "gk" two new funny movement keys -- up and down
6026 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 case 'j':
6028 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006029 // with 'nowrap' it works just like the normal "j" command.
6030 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 {
6032 oap->motion_type = MLINE;
6033 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
6034 }
6035 else
6036 i = nv_screengo(oap, FORWARD, cap->count1);
6037 if (i == FAIL)
6038 clearopbeep(oap);
6039 break;
6040
6041 case 'k':
6042 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006043 // with 'nowrap' it works just like the normal "k" command.
6044 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 {
6046 oap->motion_type = MLINE;
6047 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
6048 }
6049 else
6050 i = nv_screengo(oap, BACKWARD, cap->count1);
6051 if (i == FAIL)
6052 clearopbeep(oap);
6053 break;
6054
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006055 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 case 'J':
6057 nv_join(cap);
6058 break;
6059
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006060 // "g0", "g^" : Like "0" and "^" but for screen lines.
6061 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 case '0':
6064 case 'm':
6065 case K_HOME:
6066 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006067 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 break;
6069
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006070 case 'M':
6071 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006072 oap->motion_type = MCHAR;
6073 oap->inclusive = FALSE;
Dylan Thacker-Smith366c81a2024-03-24 09:46:56 +01006074 i = linetabsize_no_outer(curwin, curwin->w_cursor.lnum);
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006075 if (cap->count0 > 0 && cap->count0 <= 100)
6076 coladvance((colnr_T)(i * cap->count0 / 100));
6077 else
6078 coladvance((colnr_T)(i / 2));
6079 curwin->w_set_curswant = TRUE;
6080 }
6081 break;
6082
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006083 // "g_": to the last non-blank character in the line or <count> lines
6084 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006086 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 break;
6088
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006089 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 case '$':
6091 case K_END:
6092 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006093 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 break;
6095
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006096 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 case '*':
6098 case '#':
6099#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006100 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006102 case Ctrl_RSB: // :tag or :tselect for current identifier
6103 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 nv_ident(cap);
6105 break;
6106
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006107 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 case 'e':
6109 case 'E':
6110 oap->motion_type = MCHAR;
6111 curwin->w_set_curswant = TRUE;
6112 oap->inclusive = TRUE;
6113 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6114 clearopbeep(oap);
6115 break;
6116
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006117 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006119 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 break;
6121
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006122 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006124 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 break;
6126
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006127 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 case 'I':
6129 beginline(0);
6130 if (!checkclearopq(oap))
6131 invoke_edit(cap, FALSE, 'g', FALSE);
6132 break;
6133
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006134 // "gf": goto file, edit file under cursor
6135 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006137 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 nv_gotofile(cap);
6139 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006141 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 case '\'':
6143 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006144 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 case '`':
6146 nv_gomark(cap);
6147 break;
6148
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006149 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006151 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 break;
6153
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006154 // "ga": Display the ascii value of the character under the
6155 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 case 'a':
6157 do_ascii(NULL);
6158 break;
6159
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006160 // "g8": Display the bytes used for the UTF-8 character under the
6161 // cursor. It is displayed in hex.
6162 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006164 if (cap->count0 == 8)
6165 utf_find_illegal();
6166 else
6167 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006170 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006171 case '<':
6172 show_sb_text();
6173 break;
6174
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006175 // "gg": Goto the first line in file. With a count it goes to
6176 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 case 'g':
6178 cap->arg = FALSE;
6179 nv_goto(cap);
6180 break;
6181
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006182 // Two-character operators:
6183 // "gq" Format text
6184 // "gw" Format text and keep cursor position
6185 // "g~" Toggle the case of the text.
6186 // "gu" Change text to lower case.
6187 // "gU" Change text to upper case.
6188 // "g?" rot13 encoding
6189 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 case 'q':
6191 case 'w':
6192 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006193 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 case '~':
6195 case 'u':
6196 case 'U':
6197 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006198 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 nv_operator(cap);
6200 break;
6201
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006202 // "gd": Find first occurrence of pattern under the cursor in the
6203 // current function
6204 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 case 'd':
6206 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006207 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 break;
6209
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006210 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 case K_MIDDLEMOUSE:
6212 case K_MIDDLEDRAG:
6213 case K_MIDDLERELEASE:
6214 case K_LEFTMOUSE:
6215 case K_LEFTDRAG:
6216 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006217 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 case K_RIGHTMOUSE:
6219 case K_RIGHTDRAG:
6220 case K_RIGHTRELEASE:
6221 case K_X1MOUSE:
6222 case K_X1DRAG:
6223 case K_X1RELEASE:
6224 case K_X2MOUSE:
6225 case K_X2DRAG:
6226 case K_X2RELEASE:
6227 mod_mask = MOD_MASK_CTRL;
6228 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6229 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230
6231 case K_IGNORE:
6232 break;
6233
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006234 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 case 'p':
6236 case 'P':
6237 nv_put(cap);
6238 break;
6239
6240#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006241 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 case 'o':
Christian Brabandtf8702ae2024-08-28 20:39:24 +02006243 oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 goto_byte(cap->count0);
6245 break;
6246#endif
6247
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006248 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006250 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 do_exmode(TRUE);
6252 break;
6253
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 case ',':
6255 nv_pcmark(cap);
6256 break;
6257
6258 case ';':
6259 cap->count1 = -cap->count1;
6260 nv_pcmark(cap);
6261 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006263 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006264 if (!checkclearop(oap))
6265 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006266 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006267 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006268 if (!checkclearop(oap))
6269 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006270 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006271
Bram Moolenaar62a23252020-08-09 14:04:42 +02006272 case TAB:
6273 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6274 clearopbeep(oap);
6275 break;
6276
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006277 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006278 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006279 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006280 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006281 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006282 break;
6283
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 default:
6285 clearopbeep(oap);
6286 break;
6287 }
6288}
6289
6290/*
6291 * Handle "o" and "O" commands.
6292 */
6293 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006294n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006296#ifdef FEAT_CONCEAL
6297 linenr_T oldline = curwin->w_cursor.lnum;
6298#endif
6299
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006300 if (checkclearopq(cap->oap))
6301 return;
6302
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303#ifdef FEAT_FOLDING
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006304 if (cap->cmdchar == 'O')
6305 // Open above the first line of a folded sequence of lines
6306 (void)hasFolding(curwin->w_cursor.lnum,
6307 &curwin->w_cursor.lnum, NULL);
6308 else
6309 // Open below the last line of a folded sequence of lines
6310 (void)hasFolding(curwin->w_cursor.lnum,
6311 NULL, &curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312#endif
Christian Brabandt4bca4892023-10-27 19:26:49 +02006313 // trigger TextChangedI for the 'o/O' command
6314 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006315 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6316 (cap->cmdchar == 'O' ? 1 : 0)),
6317 (linenr_T)(curwin->w_cursor.lnum +
6318 (cap->cmdchar == 'o' ? 1 : 0))
6319 ) == OK
6320 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
6321 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6322 0, NULL) == OK)
6323 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006324#ifdef FEAT_CONCEAL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006325 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
6326 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006327#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006328#ifdef FEAT_SYN_HL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006329 if (curwin->w_p_cul)
6330 // force redraw of cursorline
6331 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006332#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006333 // When '#' is in 'cpoptions' ignore the count.
6334 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6335 cap->count1 = 1;
6336 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 }
6338}
6339
6340/*
6341 * "." command: redo last change.
6342 */
6343 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006344nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006346 if (checkclearopq(cap->oap))
6347 return;
6348
6349 // If "restart_edit" is TRUE, the last but one command is repeated
6350 // instead of the last command (inserting text). This is used for
6351 // CTRL-O <.> in insert mode.
6352 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6353 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354}
6355
6356/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006357 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 */
6359 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006360nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006362 if (VIsual_select && VIsual_active)
6363 {
6364 int reg;
6365 // Get register name
6366 ++no_mapping;
6367 ++allow_keys;
6368 reg = plain_vgetc();
6369 LANGMAP_ADJUST(reg, TRUE);
6370 --no_mapping;
6371 --allow_keys;
6372
6373 if (reg == '"')
6374 // the unnamed register is 0
6375 reg = 0;
6376
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006377 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006378 return;
6379 }
6380
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006381 if (checkclearopq(cap->oap))
6382 return;
6383
6384 u_redo((int)cap->count1);
6385 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386}
6387
6388/*
6389 * Handle "U" command.
6390 */
6391 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006392nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006394 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006395 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006397 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 cap->cmdchar = 'g';
6399 cap->nchar = 'U';
6400 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006401 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006403
6404 if (checkclearopq(cap->oap))
6405 return;
6406
6407 u_undoline();
6408 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409}
6410
6411/*
6412 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6413 * single character.
6414 */
6415 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006416nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006418 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006419 {
6420#ifdef FEAT_JOB_CHANNEL
6421 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6422 {
6423 clearopbeep(cap->oap);
6424 return;
6425 }
6426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 else
6430 nv_operator(cap);
6431}
6432
6433/*
6434 * Handle an operator command.
6435 * The actual work is done by do_pending_operator().
6436 */
6437 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006438nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439{
6440 int op_type;
6441
6442 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006443#ifdef FEAT_JOB_CHANNEL
6444 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6445 {
6446 clearopbeep(cap->oap);
6447 return;
6448 }
6449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006451 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 nv_lineop(cap);
6453 else if (!checkclearop(cap->oap))
6454 {
6455 cap->oap->start = curwin->w_cursor;
6456 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006457#ifdef FEAT_EVAL
6458 set_op_var(op_type);
6459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 }
6461}
6462
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006463#ifdef FEAT_EVAL
6464/*
6465 * Set v:operator to the characters for "optype".
6466 */
6467 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006468set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006469{
6470 char_u opchars[3];
6471
6472 if (optype == OP_NOP)
6473 set_vim_var_string(VV_OP, NULL, 0);
6474 else
6475 {
6476 opchars[0] = get_op_char(optype);
6477 opchars[1] = get_extra_op_char(optype);
6478 opchars[2] = NUL;
6479 set_vim_var_string(VV_OP, opchars, -1);
6480 }
6481}
6482#endif
6483
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484/*
6485 * Handle linewise operator "dd", "yy", etc.
6486 *
6487 * "_" is is a strange motion command that helps make operators more logical.
6488 * It is actually implemented, but not documented in the real Vi. This motion
6489 * command actually refers to "the current line". Commands like "dd" and "yy"
6490 * are really an alternate form of "d_" and "y_". It does accept a count, so
6491 * "d3_" works to delete 3 lines.
6492 */
6493 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006494nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495{
6496 cap->oap->motion_type = MLINE;
6497 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6498 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006499 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006500 && cap->oap->motion_force != 'v'
6501 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 || cap->oap->op_type == OP_LSHIFT
6503 || cap->oap->op_type == OP_RSHIFT)
6504 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006505 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 beginline(BL_WHITE | BL_FIX);
6507}
6508
6509/*
6510 * <Home> command.
6511 */
6512 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006513nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006515 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006516 if (mod_mask & MOD_MASK_CTRL)
6517 nv_goto(cap);
6518 else
6519 {
6520 cap->count0 = 1;
6521 nv_pipe(cap);
6522 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006523 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6524 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525}
6526
6527/*
6528 * "|" command.
6529 */
6530 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006531nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532{
6533 cap->oap->motion_type = MCHAR;
6534 cap->oap->inclusive = FALSE;
6535 beginline(0);
6536 if (cap->count0 > 0)
6537 {
6538 coladvance((colnr_T)(cap->count0 - 1));
6539 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6540 }
6541 else
6542 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006543 // keep curswant at the column where we wanted to go, not where
6544 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 curwin->w_set_curswant = FALSE;
6546}
6547
6548/*
6549 * Handle back-word command "b" and "B".
6550 * cap->arg is 1 for "B"
6551 */
6552 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006553nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554{
6555 cap->oap->motion_type = MCHAR;
6556 cap->oap->inclusive = FALSE;
6557 curwin->w_set_curswant = TRUE;
6558 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6559 clearopbeep(cap->oap);
6560#ifdef FEAT_FOLDING
6561 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6562 foldOpenCursor();
6563#endif
6564}
6565
6566/*
6567 * Handle word motion commands "e", "E", "w" and "W".
6568 * cap->arg is TRUE for "E" and "W".
6569 */
6570 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006571nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572{
6573 int n;
6574 int word_end;
6575 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006576 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006578 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6580 word_end = TRUE;
6581 else
6582 word_end = FALSE;
6583 cap->oap->inclusive = word_end;
6584
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006585 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 if (!word_end && cap->oap->op_type == OP_CHANGE)
6587 {
6588 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006589 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006591 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006593 // Reproduce a funny Vi behaviour: "cw" on a blank only
6594 // changes one character, not all blanks until the start of
6595 // the next word. Only do this when the 'w' flag is included
6596 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6598 {
6599 cap->oap->inclusive = TRUE;
6600 cap->oap->motion_type = MCHAR;
6601 return;
6602 }
6603 }
6604 else
6605 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006606 // This is a little strange. To match what the real Vi does,
6607 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6608 // that we are not on a space or a TAB. This seems impolite
6609 // at first, but it's really more what we mean when we say
6610 // 'cw'.
6611 // Another strangeness: When standing on the end of a word
6612 // "ce" will change until the end of the next word, but "cw"
6613 // will change only one character! This is done by setting
6614 // flag.
Christian Brabandt22105fd2024-07-15 20:51:11 +02006615 // This can be configured using :set cpo-z
6616 if (vim_strchr(p_cpo, CPO_WORD) != NULL)
6617 {
6618 cap->oap->inclusive = TRUE;
6619 word_end = TRUE;
6620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 flag = TRUE;
6622 }
6623 }
6624 }
6625
6626 cap->oap->motion_type = MCHAR;
6627 curwin->w_set_curswant = TRUE;
6628 if (word_end)
6629 n = end_word(cap->count1, cap->arg, flag, FALSE);
6630 else
6631 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6632
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006633 // Don't leave the cursor on the NUL past the end of line. Unless we
6634 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006635 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006636 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637
6638 if (n == FAIL && cap->oap->op_type == OP_NOP)
6639 clearopbeep(cap->oap);
6640 else
6641 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643#ifdef FEAT_FOLDING
6644 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6645 foldOpenCursor();
6646#endif
6647 }
6648}
6649
6650/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006651 * Used after a movement command: If the cursor ends up on the NUL after the
6652 * end of the line, may move it back to the last character and make the motion
6653 * inclusive.
6654 */
6655 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006656adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006657{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006658 // The cursor cannot remain on the NUL when:
6659 // - the column is > 0
6660 // - not in Visual mode or 'selection' is "o"
6661 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006662 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006663 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006664 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006665 {
6666 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006667 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006668 if (has_mbyte)
6669 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006670 oap->inclusive = TRUE;
6671 }
6672}
6673
6674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 * "0" and "^" commands.
6676 * cap->arg is the argument for beginline().
6677 */
6678 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006679nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680{
6681 cap->oap->motion_type = MCHAR;
6682 cap->oap->inclusive = FALSE;
6683 beginline(cap->arg);
6684#ifdef FEAT_FOLDING
6685 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6686 foldOpenCursor();
6687#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006688 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6689 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690}
6691
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692/*
6693 * In exclusive Visual mode, may include the last character.
6694 */
6695 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006696adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697{
6698 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006699 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 if (has_mbyte)
6702 inc_cursor();
6703 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 ++curwin->w_cursor.col;
6705 cap->oap->inclusive = FALSE;
6706 }
6707}
6708
6709/*
6710 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6711 * Should check VIsual_mode before calling this.
6712 * Returns TRUE when backed up to the previous line.
6713 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006714 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006715unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716{
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006717 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
zeertzjq701ad502024-05-23 07:47:55 +02006718 return unadjust_for_sel_inner(LT_POS(VIsual, curwin->w_cursor)
6719 ? &curwin->w_cursor : &VIsual);
6720 return FALSE;
6721}
6722
6723/*
6724 * Move position "*pp" back one character for 'selection' == "exclusive".
6725 * Returns TRUE when backed up to the previous line.
6726 */
6727 int
6728unadjust_for_sel_inner(pos_T *pp)
6729{
6730 colnr_T cs, ce;
6731
6732 if (pp->coladd > 0)
6733 --pp->coladd;
6734 else if (pp->col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 {
zeertzjq701ad502024-05-23 07:47:55 +02006736 --pp->col;
6737 mb_adjustpos(curbuf, pp);
6738 if (virtual_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 {
zeertzjq701ad502024-05-23 07:47:55 +02006740 getvcol(curwin, pp, &cs, NULL, &ce);
6741 pp->coladd = ce - cs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 }
6743 }
zeertzjq701ad502024-05-23 07:47:55 +02006744 else if (pp->lnum > 1)
6745 {
6746 --pp->lnum;
6747 pp->col = ml_get_len(pp->lnum);
6748 return TRUE;
6749 }
6750
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 return FALSE;
6752}
6753
6754/*
6755 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6756 */
6757 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006758nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759{
6760 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006761 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006763 VIsual_select_reg = 0;
6764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 else if (VIsual_reselect)
6766 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006767 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 cap->arg = TRUE;
6769 nv_g_cmd(cap);
6770 }
6771}
6772
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773
6774/*
6775 * "G", "gg", CTRL-END, CTRL-HOME.
6776 * cap->arg is TRUE for "G".
6777 */
6778 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006779nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780{
6781 linenr_T lnum;
6782
6783 if (cap->arg)
6784 lnum = curbuf->b_ml.ml_line_count;
6785 else
6786 lnum = 1L;
6787 cap->oap->motion_type = MLINE;
6788 setpcmark();
6789
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006790 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 if (cap->count0 != 0)
6792 lnum = cap->count0;
6793 if (lnum < 1L)
6794 lnum = 1L;
6795 else if (lnum > curbuf->b_ml.ml_line_count)
6796 lnum = curbuf->b_ml.ml_line_count;
6797 curwin->w_cursor.lnum = lnum;
6798 beginline(BL_SOL | BL_FIX);
6799#ifdef FEAT_FOLDING
6800 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6801 foldOpenCursor();
6802#endif
6803}
6804
6805/*
6806 * CTRL-\ in Normal mode.
6807 */
6808 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006809nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810{
6811 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6812 {
6813 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006814 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006815 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 if (cmdwin_type != 0)
6818 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 if (VIsual_active)
6820 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006821 end_visual_mode(); // stop Visual
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006822 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006824 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 if (cap->nchar == Ctrl_G && p_im)
6826 restart_edit = 'a';
6827 }
6828 else
6829 clearopbeep(cap->oap);
6830}
6831
6832/*
6833 * ESC in Normal mode: beep, but don't flush buffers.
6834 * Don't even beep if we are canceling a command.
6835 */
6836 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006837nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838{
6839 int no_reason;
6840
6841 no_reason = (cap->oap->op_type == OP_NOP
6842 && cap->opcount == 0
6843 && cap->count0 == 0
6844 && cap->oap->regname == 0
6845 && !p_im);
6846
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006847 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006849 if (restart_edit == 0 && cmdwin_type == 0
6850 && !VIsual_active && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006851 {
Bram Moolenaarf7e73022022-09-24 13:10:04 +01006852 int out_redir = !stdout_isatty && !is_not_a_term_or_gui();
6853
Bram Moolenaar5939c352022-09-24 12:50:45 +01006854 // The user may accidentally do "vim file | grep word" and then
6855 // CTRL-C doesn't show anything. With a changed buffer give the
6856 // message on stderr. Without any changes might as well exit.
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006857 if (anyBufIsChanged())
Bram Moolenaar5939c352022-09-24 12:50:45 +01006858 {
6859 char *ms = _("Type :qa! and press <Enter> to abandon all changes and exit Vim");
6860
6861 if (out_redir)
6862 mch_errmsg(ms);
6863 else
6864 msg(ms);
6865 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006866 else
Bram Moolenaar5939c352022-09-24 12:50:45 +01006867 {
6868 if (out_redir)
6869 {
6870 got_int = FALSE;
6871 do_cmdline_cmd((char_u *)"qa");
6872 }
6873 else
6874 msg(_("Type :qa and press <Enter> to exit Vim"));
6875 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01006878 if (restart_edit != 0)
6879 redraw_mode = TRUE; // remove "-- (insert) --"
6880
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006881 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6882 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 if (!p_im)
6884 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 if (cmdwin_type != 0)
6886 {
6887 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006888 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 return;
6890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 }
Bram Moolenaar8d696372022-08-21 10:40:07 +01006892 else if (cmdwin_type != 0 && ex_normal_busy && typebuf_was_empty)
Bram Moolenaar7d414102021-02-23 19:39:20 +01006893 {
6894 // When :normal runs out of characters while in the command line window
Bram Moolenaar8d696372022-08-21 10:40:07 +01006895 // vgetorpeek() will repeatedly return ESC. Exit the cmdline window to
6896 // break the loop.
Bram Moolenaar7d414102021-02-23 19:39:20 +01006897 cmdwin_result = K_IGNORE;
6898 return;
6899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 if (VIsual_active)
6902 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006903 end_visual_mode(); // stop Visual
6904 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006906 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006908 else if (no_reason)
Bram Moolenaarb849c822022-08-28 22:46:21 +01006909 {
6910#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar98d10692022-08-29 00:08:39 +01006911 if (!cap->arg && popup_message_win_visible())
Bram Moolenaarb849c822022-08-28 22:46:21 +01006912 popup_hide_message_win();
6913 else
6914#endif
6915 vim_beep(BO_ESC);
6916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 clearop(cap->oap);
6918
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006919 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6920 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006921 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 restart_edit = 'a';
6923}
6924
6925/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006926 * Move the cursor for the "A" command.
6927 */
6928 void
6929set_cursor_for_append_to_line(void)
6930{
6931 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006932 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006933 {
6934 int save_State = State;
6935
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006936 // Pretend Insert mode here to allow the cursor on the
6937 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006938 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006939 coladvance((colnr_T)MAXCOL);
6940 State = save_State;
6941 }
6942 else
6943 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6944}
6945
6946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006948 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 */
6950 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006951nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006953 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6955 cap->cmdchar = 'i';
6956
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006957 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006959 {
6960#ifdef FEAT_TERMINAL
6961 if (term_in_normal_mode())
6962 {
6963 end_visual_mode();
6964 clearop(cap->oap);
6965 term_enter_job_mode();
6966 return;
6967 }
6968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006972 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006973 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6974 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 nv_object(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006978#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006979 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006980 {
6981 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006982 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006983 return;
6984 }
6985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 else if (!curbuf->b_p_ma && !p_im)
6987 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006988 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006989 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006991 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006992 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006993 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006995 else if (cap->cmdchar == K_PS && VIsual_active)
6996 {
6997 pos_T old_pos = curwin->w_cursor;
6998 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006999 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01007000
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007001 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01007002 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
7003 {
7004 shift_delete_registers();
7005 cap->oap->regname = '1';
7006 }
7007 else
7008 cap->oap->regname = '-';
7009 cap->cmdchar = 'd';
7010 cap->nchar = NUL;
7011 nv_operator(cap);
7012 do_pending_operator(cap, 0, FALSE);
7013 cap->cmdchar = K_PS;
7014
zeertzjq7a732522022-03-14 20:46:41 +00007015 if (*ml_get_cursor() != NUL)
7016 {
7017 if (old_visual_mode == 'V')
7018 {
7019 // In linewise Visual mode insert before the beginning of the
7020 // next line.
7021 // When the last line in the buffer was deleted then create a
7022 // new line, otherwise there is not need to move cursor.
7023 // Detect this by checking if cursor moved above Visual area.
7024 if (curwin->w_cursor.lnum < old_pos.lnum
7025 && curwin->w_cursor.lnum < old_visual.lnum)
7026 {
7027 if (u_save_cursor() == OK)
7028 {
7029 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
7030 FALSE);
7031 appended_lines(curwin->w_cursor.lnum++, 1L);
7032 }
7033 }
7034 }
7035 // When the last char in the line was deleted then append.
7036 // Detect this by checking if cursor moved before Visual area.
7037 else if (curwin->w_cursor.col < old_pos.col
7038 && curwin->w_cursor.col < old_visual.col)
7039 inc_cursor();
7040 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007041
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007042 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01007043 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 else if (!checkclearopq(cap->oap))
7046 {
7047 switch (cap->cmdchar)
7048 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007049 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007050 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 break;
7052
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007053 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007054 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7055 beginline(BL_WHITE);
7056 else
7057 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 break;
7059
Bram Moolenaara1891842017-02-04 21:34:31 +01007060 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007061 // Bracketed paste works like "a"ppend, unless the cursor is in
7062 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007063 if (curwin->w_cursor.col == 0)
7064 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007065 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007066
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007067 case 'a': // "a"ppend is like "i"nsert on the next character.
7068 // increment coladd when in virtual space, increment the
7069 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 if (virtual_active()
7071 && (curwin->w_cursor.coladd > 0
7072 || *ml_get_cursor() == NUL
7073 || *ml_get_cursor() == TAB))
7074 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007075 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 inc_cursor();
7077 break;
7078 }
7079
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7081 {
7082 int save_State = State;
7083
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007084 // Pretend Insert mode here to allow the cursor on the
7085 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01007086 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 coladvance(getviscol());
7088 State = save_State;
7089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090
7091 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7092 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007093 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007094 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007095 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096}
7097
7098/*
7099 * Invoke edit() and take care of "restart_edit" and the return value.
7100 */
7101 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007102invoke_edit(
7103 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007104 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007105 int cmd,
7106 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107{
7108 int restart_edit_save = 0;
7109
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007110 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7111 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7112 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 if (repl || !stuff_empty())
7114 restart_edit_save = restart_edit;
7115 else
7116 restart_edit_save = 0;
7117
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007118 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 restart_edit = 0;
7120
Christian Brabandt4bca4892023-10-27 19:26:49 +02007121 // Reset Changedtick_i, so that TextChangedI will only be triggered for stuff
7122 // from insert mode, for 'o/O' this has already been done in n_opencmd
7123 if (cap->cmdchar != 'O' && cap->cmdchar != 'o')
7124 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 if (edit(cmd, startln, cap->count1))
7126 cap->retval |= CA_COMMAND_BUSY;
7127
7128 if (restart_edit == 0)
7129 restart_edit = restart_edit_save;
7130}
7131
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132/*
7133 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7134 */
7135 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007136nv_object(
7137 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138{
7139 int flag;
7140 int include;
7141 char_u *mps_save;
7142
7143 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007144 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007146 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007148 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 mps_save = curbuf->b_p_mps;
7150 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7151
7152 switch (cap->nchar)
7153 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007154 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 flag = current_word(cap->oap, cap->count1, include, FALSE);
7156 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007157 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 flag = current_word(cap->oap, cap->count1, include, TRUE);
7159 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007160 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 case '(':
7162 case ')':
7163 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7164 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007165 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 case '{':
7167 case '}':
7168 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7169 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007170 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 case ']':
7172 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7173 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007174 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 case '>':
7176 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7177 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007178#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007179 case 't': // "at" = a tag block (xml and html)
7180 // Do not adjust oap->end in do_pending_operator()
7181 // otherwise there are different results for 'dit'
7182 // (note leading whitespace in last line):
7183 // 1) <b> 2) <b>
7184 // foobar foobar
7185 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007186 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007187 flag = current_tagblock(cap->oap, cap->count1, include);
7188 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007189#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007190 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 flag = current_par(cap->oap, cap->count1, include, 'p');
7192 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007193 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 flag = current_sent(cap->oap, cap->count1, include);
7195 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007196 case '"': // "a"" = a double quoted string
7197 case '\'': // "a'" = a single quoted string
7198 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007199 flag = current_quote(cap->oap, cap->count1, include,
7200 cap->nchar);
7201 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007202#if 0 // TODO
7203 case 'S': // "aS" = a section
7204 case 'f': // "af" = a filename
7205 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206#endif
7207 default:
7208 flag = FAIL;
7209 break;
7210 }
7211
7212 curbuf->b_p_mps = mps_save;
7213 if (flag == FAIL)
7214 clearopbeep(cap->oap);
7215 adjust_cursor_col();
7216 curwin->w_set_curswant = TRUE;
7217}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218
7219/*
7220 * "q" command: Start/stop recording.
7221 * "q:", "q/", "q?": edit command-line in command-line window.
7222 */
7223 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007224nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225{
7226 if (cap->oap->op_type == OP_FORMAT)
7227 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007228 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 cap->cmdchar = 'g';
7230 cap->nchar = 'q';
7231 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007232 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007234
7235 if (checkclearop(cap->oap))
7236 return;
7237
7238 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007240 if (cmdwin_type != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007242 emsg(_(e_cmdline_window_already_open));
7243 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007245 stuffcharReadbuff(cap->nchar);
7246 stuffcharReadbuff(K_CMDWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007248 else
7249 // (stop) recording into a named register, unless executing a
7250 // register
7251 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
7252 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253}
7254
7255/*
7256 * Handle the "@r" command.
7257 */
7258 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007259nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260{
7261 if (checkclearop(cap->oap))
7262 return;
7263#ifdef FEAT_EVAL
7264 if (cap->nchar == '=')
7265 {
7266 if (get_expr_register() == NUL)
7267 return;
7268 }
7269#endif
7270 while (cap->count1-- && !got_int)
7271 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007272 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 {
7274 clearopbeep(cap->oap);
7275 break;
7276 }
7277 line_breakcheck();
7278 }
7279}
7280
7281/*
7282 * Handle the CTRL-U and CTRL-D commands.
7283 */
7284 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007285nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286{
Luuk van Baalcb204e62024-04-02 20:49:45 +02007287 int dir = cap->cmdchar == Ctrl_D ? FORWARD : BACKWARD;
Luuk van Baal5a2e3ec2024-03-28 10:07:29 +01007288 if (!checkclearop(cap->oap))
Luuk van Baalcb204e62024-04-02 20:49:45 +02007289 pagescroll(dir, cap->count0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290}
7291
7292/*
7293 * Handle "J" or "gJ" command.
7294 */
7295 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007296nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007298 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007300 nv_operator(cap);
7301 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007303
7304 if (checkclearop(cap->oap))
7305 return;
7306
7307 if (cap->count0 <= 1)
7308 cap->count0 = 2; // default for join is two lines!
7309 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7310 curbuf->b_ml.ml_line_count)
7311 {
7312 // can't join when on the last line
7313 if (cap->count0 <= 2)
7314 {
7315 clearopbeep(cap->oap);
7316 return;
7317 }
7318 cap->count0 = curbuf->b_ml.ml_line_count
7319 - curwin->w_cursor.lnum + 1;
7320 }
7321
7322 prep_redo(cap->oap->regname, cap->count0,
7323 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7324 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325}
7326
7327/*
7328 * "P", "gP", "p" and "gp" commands.
7329 */
7330 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007331nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007333 nv_put_opt(cap, FALSE);
7334}
7335
7336/*
7337 * "P", "gP", "p" and "gp" commands.
7338 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7339 */
7340 static void
7341nv_put_opt(cmdarg_T *cap, int fix_indent)
7342{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 int regname = 0;
7344 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007345 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007346 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 int dir;
7348 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007349 int keep_registers = FALSE;
zeertzjq4e141c62024-02-28 21:49:51 +01007350#ifdef FEAT_FOLDING
7351 int save_fen = curwin->w_p_fen;
7352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353
7354 if (cap->oap->op_type != OP_NOP)
7355 {
7356#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007357 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7359 {
7360 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007361 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 }
7363 else
7364#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007365 clearopbeep(cap->oap);
7366 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007368
Bram Moolenaarf2732452018-06-03 14:47:35 +02007369#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007370 if (bt_prompt(curbuf) && !prompt_curpos_editable())
Bram Moolenaarf2732452018-06-03 14:47:35 +02007371 {
7372 clearopbeep(cap->oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007373 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02007374 }
7375#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007376
7377 if (fix_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007379 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7380 ? FORWARD : BACKWARD;
7381 flags |= PUT_FIXINDENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007383 else
7384 dir = (cap->cmdchar == 'P'
7385 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7386 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
7387 prep_redo_cmd(cap);
7388 if (cap->cmdchar == 'g')
7389 flags |= PUT_CURSEND;
7390 else if (cap->cmdchar == 'z')
7391 flags |= PUT_BLOCK_INNER;
7392
7393 if (VIsual_active)
7394 {
7395 // Putting in Visual mode: The put text replaces the selected
7396 // text. First delete the selected text, then put the new text.
7397 // Need to save and restore the registers that the delete
7398 // overwrites if the old contents is being put.
7399 was_visual = TRUE;
7400 regname = cap->oap->regname;
7401 keep_registers = cap->cmdchar == 'P';
7402#ifdef FEAT_CLIPBOARD
7403 adjust_clip_reg(&regname);
7404#endif
7405 if (regname == 0 || regname == '"'
7406 || VIM_ISDIGIT(regname) || regname == '-'
7407#ifdef FEAT_CLIPBOARD
7408 || (clip_unnamed && (regname == '*' || regname == '+'))
7409#endif
7410
7411 )
7412 {
7413 // The delete is going to overwrite the register we want to
7414 // put, save it first.
7415 reg1 = get_register(regname, TRUE);
7416 }
7417
zeertzjq4e141c62024-02-28 21:49:51 +01007418#ifdef FEAT_FOLDING
7419 // Temporarily disable folding, as deleting a fold marker may cause
7420 // the cursor to be included in a fold.
7421 curwin->w_p_fen = FALSE;
7422#endif
7423
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007424 // Now delete the selected text. Avoid messages here.
7425 cap->cmdchar = 'd';
7426 cap->nchar = NUL;
7427 cap->oap->regname = keep_registers ? '_' : NUL;
7428 ++msg_silent;
7429 nv_operator(cap);
7430 do_pending_operator(cap, 0, FALSE);
7431 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7432 --msg_silent;
7433
7434 // delete PUT_LINE_BACKWARD;
7435 cap->oap->regname = regname;
7436
7437 if (reg1 != NULL)
7438 {
7439 // Delete probably changed the register we want to put, save
7440 // it first. Then put back what was there before the delete.
7441 reg2 = get_register(regname, FALSE);
7442 put_register(regname, reg1);
7443 }
7444
7445 // When deleted a linewise Visual area, put the register as
7446 // lines to avoid it joined with the next line. When deletion was
7447 // characterwise, split a line when putting lines.
7448 if (VIsual_mode == 'V')
7449 flags |= PUT_LINE;
7450 else if (VIsual_mode == 'v')
7451 flags |= PUT_LINE_SPLIT;
7452 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7453 flags |= PUT_LINE_FORWARD;
7454 dir = BACKWARD;
7455 if ((VIsual_mode != 'V'
7456 && curwin->w_cursor.col < curbuf->b_op_start.col)
7457 || (VIsual_mode == 'V'
7458 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
7459 // cursor is at the end of the line or end of file, put
7460 // forward.
7461 dir = FORWARD;
7462 // May have been reset in do_put().
7463 VIsual_active = TRUE;
7464 }
7465 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
7466
7467 // If a register was saved, put it back now.
7468 if (reg2 != NULL)
7469 put_register(regname, reg2);
7470
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007471 if (was_visual)
7472 {
zeertzjq4e141c62024-02-28 21:49:51 +01007473#ifdef FEAT_FOLDING
7474 if (save_fen)
7475 curwin->w_p_fen = TRUE;
7476#endif
7477 // What to reselect with "gv"? Selecting the just put text seems to
7478 // be the most useful, since the original text was removed.
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007479 curbuf->b_visual.vi_start = curbuf->b_op_start;
7480 curbuf->b_visual.vi_end = curbuf->b_op_end;
7481 // need to adjust cursor position
7482 if (*p_sel == 'e')
7483 inc(&curbuf->b_visual.vi_end);
7484 }
7485
7486 // When all lines were selected and deleted do_put() leaves an empty
7487 // line that needs to be deleted now.
7488 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
7489 {
7490 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
7491 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
7492
7493 // If the cursor was in that line, move it to the end of the last
7494 // line.
7495 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7496 {
7497 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7498 coladvance((colnr_T)MAXCOL);
7499 }
7500 }
7501 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502}
7503
7504/*
7505 * "o" and "O" commands.
7506 */
7507 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007508nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509{
7510#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007511 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7513 {
7514 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007515 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 }
7517 else
7518#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007519 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007521#ifdef FEAT_JOB_CHANNEL
7522 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007523 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 n_opencmd(cap);
7527}
7528
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529#ifdef FEAT_NETBEANS_INTG
7530 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007531nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532{
7533 netbeans_keycommand(cap->nchar);
7534}
7535#endif
7536
7537#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007539nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007541 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542}
7543#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007544
Bram Moolenaar3918c952005-03-15 22:34:55 +00007545/*
7546 * Trigger CursorHold event.
7547 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7548 * input buffer. "did_cursorhold" is set to avoid retriggering.
7549 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007550 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007551nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007552{
7553 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7554 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007555 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007556}