blob: 1b5326b1362dbb3d98647b8830d41b03aea10ba8 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
ichizok672776d2022-01-31 12:27:18 +00003 * VIM - Vi IMproved by Bram Moolenaar et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
12 * the operators.
13 */
14
15#include "vim.h"
16
Bram Moolenaar792cf5e2019-09-30 23:12:16 +020017static int VIsual_mode_orig = NUL; // saved Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +000018
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010019#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010020static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount);
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010021#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static void unshift_special(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010023static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25/*
26 * nv_*(): functions called to handle Normal and Visual mode commands.
27 * n_*(): functions called to handle Normal mode commands.
28 * v_*(): functions called to handle Visual mode commands.
29 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010030static void nv_ignore(cmdarg_T *cap);
31static void nv_nop(cmdarg_T *cap);
32static void nv_error(cmdarg_T *cap);
33static void nv_help(cmdarg_T *cap);
34static void nv_addsub(cmdarg_T *cap);
35static void nv_page(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010036static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010038static void nv_ver_scrollbar(cmdarg_T *cap);
39static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000041#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void nv_tabline(cmdarg_T *cap);
43static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000044#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010045static void nv_exmode(cmdarg_T *cap);
46static void nv_colon(cmdarg_T *cap);
47static void nv_ctrlg(cmdarg_T *cap);
48static void nv_ctrlh(cmdarg_T *cap);
49static void nv_clear(cmdarg_T *cap);
50static void nv_ctrlo(cmdarg_T *cap);
51static void nv_hat(cmdarg_T *cap);
52static void nv_Zet(cmdarg_T *cap);
53static void nv_ident(cmdarg_T *cap);
54static void nv_tagpop(cmdarg_T *cap);
55static void nv_scroll(cmdarg_T *cap);
56static void nv_right(cmdarg_T *cap);
57static void nv_left(cmdarg_T *cap);
58static void nv_up(cmdarg_T *cap);
59static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010060static void nv_end(cmdarg_T *cap);
61static void nv_dollar(cmdarg_T *cap);
62static void nv_search(cmdarg_T *cap);
63static void nv_next(cmdarg_T *cap);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +020064static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt, int *wrapped);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010065static void nv_csearch(cmdarg_T *cap);
66static void nv_brackets(cmdarg_T *cap);
67static void nv_percent(cmdarg_T *cap);
68static void nv_brace(cmdarg_T *cap);
69static void nv_mark(cmdarg_T *cap);
70static void nv_findpar(cmdarg_T *cap);
71static void nv_undo(cmdarg_T *cap);
72static void nv_kundo(cmdarg_T *cap);
73static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010074static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010075static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
76static void v_visop(cmdarg_T *cap);
77static void nv_subst(cmdarg_T *cap);
78static void nv_abbrev(cmdarg_T *cap);
79static void nv_optrans(cmdarg_T *cap);
80static void nv_gomark(cmdarg_T *cap);
81static void nv_pcmark(cmdarg_T *cap);
82static void nv_regname(cmdarg_T *cap);
83static void nv_visual(cmdarg_T *cap);
84static void n_start_visual_mode(int c);
85static void nv_window(cmdarg_T *cap);
86static void nv_suspend(cmdarg_T *cap);
87static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010088static void nv_dot(cmdarg_T *cap);
Shougo Matsushita4ede01f2022-01-20 15:26:03 +000089static void nv_redo_or_register(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010090static void nv_Undo(cmdarg_T *cap);
91static void nv_tilde(cmdarg_T *cap);
92static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000093#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010094static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000095#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010096static void nv_lineop(cmdarg_T *cap);
97static void nv_home(cmdarg_T *cap);
98static void nv_pipe(cmdarg_T *cap);
99static void nv_bck_word(cmdarg_T *cap);
100static void nv_wordcmd(cmdarg_T *cap);
101static void nv_beginline(cmdarg_T *cap);
102static void adjust_cursor(oparg_T *oap);
103static void adjust_for_sel(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100104static void nv_select(cmdarg_T *cap);
105static void nv_goto(cmdarg_T *cap);
106static void nv_normal(cmdarg_T *cap);
107static void nv_esc(cmdarg_T *oap);
108static void nv_edit(cmdarg_T *cap);
109static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100110static void nv_object(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100111static void nv_record(cmdarg_T *cap);
112static void nv_at(cmdarg_T *cap);
113static void nv_halfpage(cmdarg_T *cap);
114static void nv_join(cmdarg_T *cap);
115static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200116static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100117static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100119static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100122static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100124static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125
ichizok672776d2022-01-31 12:27:18 +0000126// Declare nv_cmds[].
127#define DO_DECLARE_NVCMD
128#include "nv_cmds.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Yegappan Lakshmanan4dc0dd82022-01-29 13:06:40 +0000130// Include the lookuptable generated by create_nvcmdidx.vim.
131#include "nv_cmdidxs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133/*
134 * Search for a command in the commands table.
135 * Returns -1 for invalid command.
136 */
137 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100138find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139{
140 int i;
141 int idx;
142 int top, bot;
143 int c;
144
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100145 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 if (cmdchar >= 0x100)
147 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100149 // We use the absolute value of the character. Special keys have a
150 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 if (cmdchar < 0)
152 cmdchar = -cmdchar;
153
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100154 // If the character is in the first part: The character is the index into
155 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 if (cmdchar <= nv_max_linear)
157 return nv_cmd_idx[cmdchar];
158
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100159 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 bot = nv_max_linear + 1;
161 top = NV_CMDS_SIZE - 1;
162 idx = -1;
163 while (bot <= top)
164 {
165 i = (top + bot) / 2;
166 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
167 if (c < 0)
168 c = -c;
169 if (cmdchar == c)
170 {
171 idx = nv_cmd_idx[i];
172 break;
173 }
174 if (cmdchar > c)
175 bot = i + 1;
176 else
177 top = i - 1;
178 }
179 return idx;
180}
181
182/*
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100183 * If currently editing a cmdline or text is locked: beep and give an error
184 * message, return TRUE.
185 */
186 static int
187check_text_locked(oparg_T *oap)
188{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000189 if (!text_locked())
190 return FALSE;
191
192 if (oap != NULL)
193 clearopbeep(oap);
194 text_locked_msg();
195 return TRUE;
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100196}
197
198/*
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000199 * If text is locked, "curbuf_lock" or "allbuf_lock" is set:
200 * Give an error message, possibly beep and return TRUE.
201 * "oap" may be NULL.
202 */
203 int
204check_text_or_curbuf_locked(oparg_T *oap)
205{
206 if (check_text_locked(oap))
207 return TRUE;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000208
209 if (!curbuf_locked())
210 return FALSE;
211
212 if (oap != NULL)
213 clearop(oap);
214 return TRUE;
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000215}
216
217/*
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000218 * Handle the count before a normal command and set cap->count0.
219 */
220 static int
221normal_cmd_get_count(
222 cmdarg_T *cap,
223 int c,
224 int toplevel UNUSED,
225 int set_prevcount UNUSED,
226 int *ctrl_w,
227 int *need_flushbuf UNUSED)
228{
229getcount:
230 if (!(VIsual_active && VIsual_select))
231 {
232 // Handle a count before a command and compute ca.count0.
233 // Note that '0' is a command and not the start of a count, but it's
234 // part of a count after other digits.
235 while ((c >= '1' && c <= '9')
236 || (cap->count0 != 0 && (c == K_DEL || c == K_KDEL
237 || c == '0')))
238 {
239 if (c == K_DEL || c == K_KDEL)
240 {
241 cap->count0 /= 10;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000242 del_from_showcmd(4); // delete the digit and ~@%
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000243 }
244 else if (cap->count0 > 99999999L)
245 {
246 cap->count0 = 999999999L;
247 }
248 else
249 {
250 cap->count0 = cap->count0 * 10 + (c - '0');
251 }
252#ifdef FEAT_EVAL
253 // Set v:count here, when called from main() and not a stuffed
254 // command, so that v:count can be used in an expression mapping
255 // right after the count. Do set it for redo.
256 if (toplevel && readbuf1_empty())
257 set_vcount_ca(cap, &set_prevcount);
258#endif
259 if (*ctrl_w)
260 {
261 ++no_mapping;
262 ++allow_keys; // no mapping for nchar, but keys
263 }
264 ++no_zero_mapping; // don't map zero here
265 c = plain_vgetc();
266 LANGMAP_ADJUST(c, TRUE);
267 --no_zero_mapping;
268 if (*ctrl_w)
269 {
270 --no_mapping;
271 --allow_keys;
272 }
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000273 *need_flushbuf |= add_to_showcmd(c);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000274 }
275
276 // If we got CTRL-W there may be a/another count
277 if (c == Ctrl_W && !*ctrl_w && cap->oap->op_type == OP_NOP)
278 {
279 *ctrl_w = TRUE;
280 cap->opcount = cap->count0; // remember first count
281 cap->count0 = 0;
282 ++no_mapping;
283 ++allow_keys; // no mapping for nchar, but keys
284 c = plain_vgetc(); // get next character
285 LANGMAP_ADJUST(c, TRUE);
286 --no_mapping;
287 --allow_keys;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000288 *need_flushbuf |= add_to_showcmd(c);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000289 goto getcount; // jump back
290 }
291 }
292
293 if (c == K_CURSORHOLD)
294 {
295 // Save the count values so that ca.opcount and ca.count0 are exactly
296 // the same when coming back here after handling K_CURSORHOLD.
297 cap->oap->prev_opcount = cap->opcount;
298 cap->oap->prev_count0 = cap->count0;
299 }
300 else if (cap->opcount != 0)
301 {
302 // If we're in the middle of an operator (including after entering a
303 // yank buffer with '"') AND we had a count before the operator, then
304 // that count overrides the current value of ca.count0.
305 // What this means effectively, is that commands like "3dw" get turned
306 // into "d3w" which makes things fall into place pretty neatly.
307 // If you give a count before AND after the operator, they are
308 // multiplied.
309 if (cap->count0)
310 {
311 if (cap->opcount >= 999999999L / cap->count0)
312 cap->count0 = 999999999L;
313 else
314 cap->count0 *= cap->opcount;
315 }
316 else
317 cap->count0 = cap->opcount;
318 }
319
320 // Always remember the count. It will be set to zero (on the next call,
321 // above) when there is no pending operator.
322 // When called from main(), save the count for use by the "count" built-in
323 // variable.
324 cap->opcount = cap->count0;
325 cap->count1 = (cap->count0 == 0 ? 1 : cap->count0);
326
327#ifdef FEAT_EVAL
328 // Only set v:count when called from main() and not a stuffed command.
329 // Do set it for redo.
330 if (toplevel && readbuf1_empty())
331 set_vcount(cap->count0, cap->count1, set_prevcount);
332#endif
333
334 return c;
335}
336
337/*
338 * Returns TRUE if the normal command (cap) needs a second character.
339 */
340 static int
341normal_cmd_needs_more_chars(cmdarg_T *cap, short_u cmd_flags)
342{
343 return ((cmd_flags & NV_NCH)
344 && (((cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
345 && cap->oap->op_type == OP_NOP)
346 || (cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
347 || (cap->cmdchar == 'q'
348 && cap->oap->op_type == OP_NOP
349 && reg_recording == 0
350 && reg_executing == 0)
351 || ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
352 && (cap->oap->op_type != OP_NOP || VIsual_active))));
353}
354
355/*
356 * Get one or more additional characters for a normal command.
357 * Return the updated command index (if changed).
358 */
359 static int
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000360normal_cmd_get_more_chars(
361 int idx_arg,
362 cmdarg_T *cap,
363 int *need_flushbuf UNUSED)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000364{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000365 int idx = idx_arg;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000366 int c;
367 int *cp;
368 int repl = FALSE; // get character for replace mode
369 int lit = FALSE; // get extra character literally
370 int langmap_active = FALSE; // using :lmap mappings
371 int lang; // getting a text character
372#ifdef HAVE_INPUT_METHOD
373 int save_smd; // saved value of p_smd
374#endif
375
376 ++no_mapping;
377 ++allow_keys; // no mapping for nchar, but allow key codes
378 // Don't generate a CursorHold event here, most commands can't handle
379 // it, e.g., nv_replace(), nv_csearch().
380 did_cursorhold = TRUE;
381 if (cap->cmdchar == 'g')
382 {
383 /*
384 * For 'g' get the next character now, so that we can check for
385 * "gr", "g'" and "g`".
386 */
387 cap->nchar = plain_vgetc();
388 LANGMAP_ADJUST(cap->nchar, TRUE);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000389 *need_flushbuf |= add_to_showcmd(cap->nchar);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000390 if (cap->nchar == 'r' || cap->nchar == '\'' || cap->nchar == '`'
391 || cap->nchar == Ctrl_BSL)
392 {
393 cp = &cap->extra_char; // need to get a third character
394 if (cap->nchar != 'r')
395 lit = TRUE; // get it literally
396 else
397 repl = TRUE; // get it in replace mode
398 }
399 else
400 cp = NULL; // no third character needed
401 }
402 else
403 {
404 if (cap->cmdchar == 'r') // get it in replace mode
405 repl = TRUE;
406 cp = &cap->nchar;
407 }
408 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
409
410 /*
411 * Get a second or third character.
412 */
413 if (cp != NULL)
414 {
415 if (repl)
416 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100417 State = MODE_REPLACE; // pretend Replace mode
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000418#ifdef CURSOR_SHAPE
419 ui_cursor_shape(); // show different cursor shape
420#endif
421 }
422 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
423 {
424 // Allow mappings defined with ":lmap".
425 --no_mapping;
426 --allow_keys;
427 if (repl)
Bram Moolenaar24959102022-05-07 20:01:16 +0100428 State = MODE_LREPLACE;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000429 else
Bram Moolenaar24959102022-05-07 20:01:16 +0100430 State = MODE_LANGMAP;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000431 langmap_active = TRUE;
432 }
433#ifdef HAVE_INPUT_METHOD
434 save_smd = p_smd;
435 p_smd = FALSE; // Don't let the IM code show the mode here
436 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
437 im_set_active(TRUE);
438#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100439 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000440 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100441 MAY_WANT_TO_LOG_THIS;
442
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000443 // Disable bracketed paste and modifyOtherKeys here, we won't
444 // recognize the escape sequences with 'esckeys' off.
445 out_str(T_BD);
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000446 out_str_t_TE();
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000447 }
448
449 *cp = plain_vgetc();
450
Bram Moolenaar24959102022-05-07 20:01:16 +0100451 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000452 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100453 MAY_WANT_TO_LOG_THIS;
454
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000455 // Re-enable bracketed paste mode and modifyOtherKeys
Bram Moolenaarfc966c12023-01-01 18:04:33 +0000456 out_str_t_BE();
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000457 out_str_t_TI();
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000458 }
459
460 if (langmap_active)
461 {
462 // Undo the decrement done above
463 ++no_mapping;
464 ++allow_keys;
Bram Moolenaar24959102022-05-07 20:01:16 +0100465 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000466 }
467#ifdef HAVE_INPUT_METHOD
468 if (lang)
469 {
470 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
471 im_save_status(&curbuf->b_p_iminsert);
472 im_set_active(FALSE);
473 }
474 p_smd = save_smd;
475#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100476 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000477 *need_flushbuf |= add_to_showcmd(*cp);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000478
479 if (!lit)
480 {
481#ifdef FEAT_DIGRAPHS
482 // Typing CTRL-K gets a digraph.
483 if (*cp == Ctrl_K
484 && ((nv_cmds[idx].cmd_flags & NV_LANG)
485 || cp == &cap->extra_char)
486 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
487 {
488 c = get_digraph(FALSE);
489 if (c > 0)
490 {
491 *cp = c;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000492 // Guessing how to update showcmd here...
493 del_from_showcmd(3);
494 *need_flushbuf |= add_to_showcmd(*cp);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000495 }
496 }
497#endif
498
499 // adjust chars > 127, except after "tTfFr" commands
500 LANGMAP_ADJUST(*cp, !lang);
501#ifdef FEAT_RIGHTLEFT
502 // adjust Hebrew mapped char
503 if (p_hkmap && lang && KeyTyped)
504 *cp = hkmap(*cp);
505#endif
506 }
507
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000508 // When the next character is CTRL-\ a following CTRL-N means the
509 // command is aborted and we go to Normal mode.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000510 if (cp == &cap->extra_char
511 && cap->nchar == Ctrl_BSL
512 && (cap->extra_char == Ctrl_N || cap->extra_char == Ctrl_G))
513 {
514 cap->cmdchar = Ctrl_BSL;
515 cap->nchar = cap->extra_char;
516 idx = find_command(cap->cmdchar);
517 }
zeertzjq4f026ea2023-02-26 14:47:24 +0000518 else if ((cap->nchar == 'n' || cap->nchar == 'N')
519 && cap->cmdchar == 'g')
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000520 cap->oap->op_type = get_op_type(*cp, NUL);
521 else if (*cp == Ctrl_BSL)
522 {
523 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
524
525 // There is a busy wait here when typing "f<C-\>" and then
526 // something different from CTRL-N. Can't be avoided.
527 while ((c = vpeekc()) <= 0 && towait > 0L)
528 {
529 do_sleep(towait > 50L ? 50L : towait, FALSE);
530 towait -= 50L;
531 }
532 if (c > 0)
533 {
534 c = plain_vgetc();
535 if (c != Ctrl_N && c != Ctrl_G)
536 vungetc(c);
537 else
538 {
539 cap->cmdchar = Ctrl_BSL;
540 cap->nchar = c;
541 idx = find_command(cap->cmdchar);
542 }
543 }
544 }
545
546 // When getting a text character and the next character is a
547 // multi-byte character, it could be a composing character.
548 // However, don't wait for it to arrive. Also, do enable mapping,
549 // because if it's put back with vungetc() it's too late to apply
550 // mapping.
551 --no_mapping;
552 while (enc_utf8 && lang && (c = vpeekc()) > 0
553 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
554 {
555 c = plain_vgetc();
556 if (!utf_iscomposing(c))
557 {
558 vungetc(c); // it wasn't, put it back
559 break;
560 }
561 else if (cap->ncharC1 == 0)
562 cap->ncharC1 = c;
563 else
564 cap->ncharC2 = c;
565 }
566 ++no_mapping;
567 }
568 --no_mapping;
569 --allow_keys;
570
571 return idx;
572}
573
574/*
575 * Returns TRUE if after processing a normal mode command, need to wait for a
576 * moment when a message is displayed that will be overwritten by the mode
577 * message.
578 */
579 static int
580normal_cmd_need_to_wait_for_msg(cmdarg_T *cap, pos_T *old_pos)
581{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000582 // In Visual mode and with "^O" in Insert mode, a short message will be
583 // overwritten by the mode message. Wait a bit, until a key is hit.
584 // In Visual mode, it's more important to keep the Visual area updated
585 // than keeping a message (e.g. from a /pat search).
586 // Only do this if the command was typed, not from a mapping.
587 // Don't wait when emsg_silent is non-zero.
588 // Also wait a bit after an error message, e.g. for "^O:".
589 // Don't redraw the screen, it would remove the message.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000590 return ( ((p_smd
591 && msg_silent == 0
592 && (restart_edit != 0
593 || (VIsual_active
594 && old_pos->lnum == curwin->w_cursor.lnum
595 && old_pos->col == curwin->w_cursor.col)
596 )
597 && (clear_cmdline
598 || redraw_cmdline)
599 && (msg_didout || (msg_didany && msg_scroll))
600 && !msg_nowait
601 && KeyTyped)
602 || (restart_edit != 0
603 && !VIsual_active
604 && (msg_scroll
605 || emsg_on_display)))
606 && cap->oap->regname == 0
607 && !(cap->retval & CA_COMMAND_BUSY)
608 && stuff_empty()
609 && typebuf_typed()
610 && emsg_silent == 0
611 && !in_assert_fails
612 && !did_wait_return
613 && cap->oap->op_type == OP_NOP);
614}
615
616/*
617 * After processing a normal mode command, wait for a moment when a message is
618 * displayed that will be overwritten by the mode message.
619 */
620 static void
621normal_cmd_wait_for_msg(void)
622{
623 int save_State = State;
624
625 // Draw the cursor with the right shape here
626 if (restart_edit != 0)
Bram Moolenaar24959102022-05-07 20:01:16 +0100627 State = MODE_INSERT;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000628
629 // If need to redraw, and there is a "keep_msg", redraw before the
630 // delay
631 if (must_redraw && keep_msg != NULL && !emsg_on_display)
632 {
633 char_u *kmsg;
634
635 kmsg = keep_msg;
636 keep_msg = NULL;
637 // Showmode() will clear keep_msg, but we want to use it anyway.
638 // First update w_topline.
639 setcursor();
640 update_screen(0);
641 // now reset it, otherwise it's put in the history again
642 keep_msg = kmsg;
643
644 kmsg = vim_strsave(keep_msg);
645 if (kmsg != NULL)
646 {
647 msg_attr((char *)kmsg, keep_msg_attr);
648 vim_free(kmsg);
649 }
650 }
651 setcursor();
652#ifdef CURSOR_SHAPE
653 ui_cursor_shape(); // may show different cursor shape
654#endif
655 cursor_on();
656 out_flush();
657 if (msg_scroll || emsg_on_display)
658 ui_delay(1003L, TRUE); // wait at least one second
659 ui_delay(3003L, FALSE); // wait up to three seconds
660 State = save_State;
661
662 msg_scroll = FALSE;
663 emsg_on_display = FALSE;
664}
665
666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 * Execute a command in Normal mode.
668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100670normal_cmd(
671 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100672 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100674 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100676 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 int old_col = curwin->w_curswant;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000678 int need_flushbuf = FALSE; // need to call out_flush()
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100679 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 static int old_mapped_len = 0;
682 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000683 int set_prevcount = FALSE;
Bram Moolenaarb146e012020-07-19 23:06:05 +0200684 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685
Bram Moolenaara80faa82020-04-12 19:37:17 +0200686 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000688
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100689 // Use a count remembered from before entering an operator. After typing
690 // "3d" we return from normal_cmd() and come back here, the "3" is
691 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 ca.opcount = opcount;
693
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000694 // If there is an operator pending, then the command we take this time
695 // will terminate it. Finish_op tells us to finish the operation before
696 // returning this time (unless the operation was cancelled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697#ifdef CURSOR_SHAPE
698 c = finish_op;
699#endif
700 finish_op = (oap->op_type != OP_NOP);
701#ifdef CURSOR_SHAPE
702 if (finish_op != c)
703 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100704 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705# ifdef FEAT_MOUSESHAPE
706 update_mouseshape(-1);
707# endif
708 }
709#endif
LemonBoy2bf52dd2022-04-09 18:17:34 +0100710 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100712 // When not finishing an operator and no register name typed, reset the
713 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000715 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000717#ifdef FEAT_EVAL
718 set_prevcount = TRUE;
719#endif
720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100722 // Restore counts from before receiving K_CURSORHOLD. This means after
723 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
724 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000725 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
726 {
727 ca.opcount = oap->prev_opcount;
728 ca.count0 = oap->prev_count0;
729 oap->prev_opcount = 0;
730 oap->prev_count0 = 0;
731 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000732
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734
Bram Moolenaar24959102022-05-07 20:01:16 +0100735 State = MODE_NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100737 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738#endif
739
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100740#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100741 // Set v:count here, when called from main() and not a stuffed
742 // command, so that v:count can be used in an expression mapping
743 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100744 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100745 set_vcount_ca(&ca, &set_prevcount);
746#endif
747
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 /*
749 * Get the command character from the user.
750 */
751 c = safe_vgetc();
Bram Moolenaar24959102022-05-07 20:01:16 +0100752 LANGMAP_ADJUST(c, get_real_state() != MODE_SELECT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000754 // If a mapping was started in Visual or Select mode, remember the length
755 // of the mapping. This is used below to not return to Insert mode for as
756 // long as the mapping is being executed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 if (restart_edit == 0)
758 old_mapped_len = 0;
759 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000760 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 old_mapped_len = typebuf_maplen();
762
763 if (c == NUL)
764 c = K_ZERO;
765
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000766 // In Select mode, typed text replaces the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 if (VIsual_active
768 && VIsual_select
769 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
770 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000771 int len;
772
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100773 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
774 // 'insertmode' is set) fake a "d"elete command, Insert mode will
775 // restart automatically.
776 // Insert the typed character in the typeahead buffer, so that it can
777 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000778 len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
779
zeertzjqfbf4f1c2022-01-28 12:50:43 +0000780 // When recording and gotchars() was called the character will be
781 // recorded again, remove the previous recording.
782 if (KeyTyped)
783 ungetchars(len);
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000784
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000785 if (restart_edit != 0)
786 c = 'd';
787 else
788 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100789 msg_nowait = TRUE; // don't delay going to insert mode
790 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
Bram Moolenaard0fb9072021-12-09 11:57:22 +0000793 // If the window was made so small that nothing shows, make it at least one
794 // line and one column when typing a command.
795 if (KeyTyped && !KeyStuffed)
796 win_ensure_size();
797
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 need_flushbuf = add_to_showcmd(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000800 // Get the command count
801 c = normal_cmd_get_count(&ca, c, toplevel, set_prevcount, &ctrl_w,
802 &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000804 // Find the command character in the table of commands.
805 // For CTRL-W we already got nchar when looking for a count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 if (ctrl_w)
807 {
808 ca.nchar = c;
809 ca.cmdchar = Ctrl_W;
810 }
811 else
812 ca.cmdchar = c;
813 idx = find_command(ca.cmdchar);
814 if (idx < 0)
815 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100816 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 clearopbeep(oap);
818 goto normal_end;
819 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000820
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000821 if ((nv_cmds[idx].cmd_flags & NV_NCW) && check_text_or_curbuf_locked(oap))
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100822 // this command is not allowed now
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000823 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000825 // In Visual/Select mode, a few keys are handled in a special way.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 if (VIsual_active)
827 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100828 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 if (km_stopsel
830 && (nv_cmds[idx].cmd_flags & NV_STS)
831 && !(mod_mask & MOD_MASK_SHIFT))
832 {
833 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100834 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 }
836
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100837 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 if (km_startsel)
839 {
840 if (nv_cmds[idx].cmd_flags & NV_SS)
841 {
842 unshift_special(&ca);
843 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000844 if (idx < 0)
845 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100846 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000847 clearopbeep(oap);
848 goto normal_end;
849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
852 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 }
855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856
857#ifdef FEAT_RIGHTLEFT
858 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
859 && (nv_cmds[idx].cmd_flags & NV_RL))
860 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100861 // Invert horizontal movements and operations. Only when typed by the
862 // user directly, not when the result of a mapping or "x" translated
863 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 switch (ca.cmdchar)
865 {
866 case 'l': ca.cmdchar = 'h'; break;
867 case K_RIGHT: ca.cmdchar = K_LEFT; break;
868 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
869 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
870 case 'h': ca.cmdchar = 'l'; break;
871 case K_LEFT: ca.cmdchar = K_RIGHT; break;
872 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
873 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
874 case '>': ca.cmdchar = '<'; break;
875 case '<': ca.cmdchar = '>'; break;
876 }
877 idx = find_command(ca.cmdchar);
878 }
879#endif
880
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000881 // Get additional characters if we need them.
882 if (normal_cmd_needs_more_chars(&ca, nv_cmds[idx].cmd_flags))
883 idx = normal_cmd_get_more_chars(idx, &ca, &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000885 // Flush the showcmd characters onto the screen so we can see them while
886 // the command is being executed. Only do this when the shown command was
887 // actually displayed, otherwise this will slow down a lot when executing
888 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (need_flushbuf)
890 out_flush();
Martin Tournoijba43e762022-10-13 22:12:15 +0100891
Bram Moolenaard9205ca2008-10-02 20:55:54 +0000892 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +0200893 {
894 if (ex_normal_busy)
895 did_cursorhold = save_did_cursorhold;
896 else
897 did_cursorhold = FALSE;
898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899
Bram Moolenaar24959102022-05-07 20:01:16 +0100900 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
zeertzjqf86dea82023-03-05 21:15:06 +0000902 if (ca.nchar == ESC || ca.extra_char == ESC)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 {
904 clearop(oap);
905 if (restart_edit == 0 && goto_im())
906 restart_edit = 'a';
907 goto normal_end;
908 }
909
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000910 if (ca.cmdchar != K_IGNORE)
911 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100912 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000913 msg_col = 0;
914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915
Luuk van Baalaa7f25e2023-04-13 21:47:38 +0100916 old_pos = curwin->w_cursor; // remember where the cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100918 // When 'keymodel' contains "startsel" some keys start Select/Visual
919 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 if (!VIsual_active && km_startsel)
921 {
922 if (nv_cmds[idx].cmd_flags & NV_SS)
923 {
924 start_selection();
925 unshift_special(&ca);
926 idx = find_command(ca.cmdchar);
927 }
928 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
929 && (mod_mask & MOD_MASK_SHIFT))
930 {
931 start_selection();
932 mod_mask &= ~MOD_MASK_SHIFT;
933 }
934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000936 // Execute the command!
937 // Call the command function found in the commands table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 ca.arg = nv_cmds[idx].cmd_arg;
939 (nv_cmds[idx].cmd_func)(&ca);
940
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000941 // If we didn't start or finish an operator, reset oap->regname, unless we
942 // need it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 if (!finish_op
944 && !oap->op_type
945 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
946 {
947 clearop(oap);
948#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +0200949 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950#endif
951 }
952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100953 // Get the length of mapped chars again after typing a count, second
954 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000955 if (old_mapped_len > 0)
956 old_mapped_len = typebuf_maplen();
957
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000958 // If an operation is pending, handle it. But not for K_IGNORE or
959 // K_MOUSEMOVE.
Bram Moolenaar1ad72c82021-05-04 21:56:28 +0200960 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +0100961 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000963 // Wait for a moment when a message is displayed that will be overwritten
964 // by the mode message.
965 if (normal_cmd_need_to_wait_for_msg(&ca, &old_pos))
966 normal_cmd_wait_for_msg();
967
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000968 // Finish up after executing a Normal mode command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969normal_end:
970
971 msg_nowait = FALSE;
972
Bram Moolenaarcc613032020-06-07 21:31:18 +0200973#ifdef FEAT_EVAL
974 if (finish_op)
975 reset_reg_var();
976#endif
977
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100978 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979#ifdef CURSOR_SHAPE
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000980 int prev_finish_op = finish_op;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981#endif
982 finish_op = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +0100983 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100985 // Redraw the cursor with another shape, if we were in Operator-pending
986 // mode or did a replace command.
zeertzjqf86dea82023-03-05 21:15:06 +0000987 if (prev_finish_op || ca.cmdchar == 'r'
988 || (ca.cmdchar == 'g' && ca.nchar == 'r'))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100990 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991# ifdef FEAT_MOUSESHAPE
992 update_mouseshape(-1);
993# endif
994 }
995#endif
996
Bram Moolenaara983fe92008-07-31 20:04:27 +0000997 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100998 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001001 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 vim_free(ca.searchbuf);
1003
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 if (has_mbyte)
1005 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 if (curwin->w_p_scb && toplevel)
1008 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001009 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 do_check_scrollbind(TRUE);
1011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012
Bram Moolenaar860cae12010-06-05 23:22:07 +02001013 if (curwin->w_p_crb && toplevel)
1014 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001015 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001016 do_check_cursorbind();
1017 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001018
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001019#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001020 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001021 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001022 restart_edit = 0;
1023#endif
1024
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001025 // May restart edit(), if we got here with CTRL-O in Insert mode (but not
1026 // if still inside a mapping that started in Visual mode).
1027 // May switch from Visual to Select mode after CTRL-O command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1030 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 && !(ca.retval & CA_COMMAND_BUSY)
1032 && stuff_empty()
1033 && oap->regname == 0)
1034 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 if (restart_VIsual_select == 1)
1036 {
1037 VIsual_select = TRUE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01001038 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 showmode();
1040 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001041 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001043 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 (void)edit(restart_edit, FALSE, 1L);
1045 }
1046
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 if (restart_VIsual_select == 2)
1048 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001050 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 opcount = ca.opcount;
1052}
1053
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001054#ifdef FEAT_EVAL
1055/*
1056 * Set v:count and v:count1 according to "cap".
1057 * Set v:prevcount only when "set_prevcount" is TRUE.
1058 */
1059 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001060set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001061{
1062 long count = cap->count0;
1063
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001064 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001065 if (cap->opcount != 0)
1066 count = cap->opcount * (count == 0 ? 1 : count);
1067 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001068 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001069}
1070#endif
1071
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001073 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 * if not.
1075 */
1076 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001077check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078{
1079 static int did_check = FALSE;
1080
1081 if (full_screen)
1082 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001083 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001084 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 did_check = TRUE;
1086 }
1087}
1088
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001089#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1090/*
1091 * Call yank_do_autocmd() for "regname".
1092 */
1093 static void
1094call_yank_do_autocmd(int regname)
1095{
1096 oparg_T oa;
1097 yankreg_T *reg;
1098
1099 clear_oparg(&oa);
1100 oa.regname = regname;
1101 oa.op_type = OP_YANK;
1102 oa.is_VIsual = TRUE;
1103 reg = get_register(regname, TRUE);
1104 yank_do_autocmd(&oa, reg);
1105 free_register(reg);
1106}
1107#endif
1108
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001110 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001111 * This function or the next should ALWAYS be called to end Visual mode, except
1112 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 */
1114 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001115end_visual_mode(void)
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001116{
1117 end_visual_mode_keep_button();
1118 reset_held_button();
1119}
1120
1121 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001122end_visual_mode_keep_button(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123{
1124#ifdef FEAT_CLIPBOARD
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001125 // If we are using the clipboard, then remember what was selected in case
1126 // we need to paste it somewhere while we still own the selection.
1127 // Only do this when the clipboard is already owned. Don't want to grab
1128 // the selection when hitting ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 if (clip_star.available && clip_star.owned)
1130 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001131
1132# if defined(FEAT_EVAL)
1133 // Emit a TextYankPost for the automatic copy of the selection into the
1134 // star and/or plus register.
1135 if (has_textyankpost())
1136 {
1137 if (clip_isautosel_star())
1138 call_yank_do_autocmd('*');
1139 if (clip_isautosel_plus())
1140 call_yank_do_autocmd('+');
1141 }
1142# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143#endif
1144
1145 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 setmouse();
1147 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001149 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001150 curbuf->b_visual.vi_mode = VIsual_mode;
1151 curbuf->b_visual.vi_start = VIsual;
1152 curbuf->b_visual.vi_end = curwin->w_cursor;
1153 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154#ifdef FEAT_EVAL
1155 curbuf->b_visual_mode_eval = VIsual_mode;
1156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 if (!virtual_active())
1158 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001159 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001161 adjust_cursor_eol();
LemonBoy2bf52dd2022-04-09 18:17:34 +01001162 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163}
1164
1165/*
1166 * Reset VIsual_active and VIsual_reselect.
1167 */
1168 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001169reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170{
1171 if (VIsual_active)
1172 {
1173 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001174 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 }
1176 VIsual_reselect = FALSE;
1177}
1178
1179/*
1180 * Reset VIsual_active and VIsual_reselect if it's set.
1181 */
1182 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001183reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184{
1185 if (VIsual_active)
1186 {
1187 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001188 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 VIsual_reselect = FALSE;
1190 }
1191}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001193 void
1194restore_visual_mode(void)
1195{
1196 if (VIsual_mode_orig != NUL)
1197 {
1198 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1199 VIsual_mode_orig = NUL;
1200 }
1201}
1202
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203/*
1204 * Check for a balloon-eval special item to include when searching for an
1205 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1206 * Returns TRUE if the character at "*ptr" should be included.
1207 * "dir" is FORWARD or BACKWARD, the direction of searching.
1208 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1209 * "bnp" points to a counter for square brackets.
1210 */
1211 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001212find_is_eval_item(
1213 char_u *ptr,
1214 int *colp,
1215 int *bnp,
1216 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001218 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1220 ++*bnp;
1221 if (*bnp > 0)
1222 {
1223 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1224 --*bnp;
1225 return TRUE;
1226 }
1227
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001228 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 if (*ptr == '.')
1230 return TRUE;
1231
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001232 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1234 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1235 {
1236 *colp += dir;
1237 return TRUE;
1238 }
1239 return FALSE;
1240}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241
1242/*
1243 * Find the identifier under or to the right of the cursor.
1244 * "find_type" can have one of three values:
1245 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001246 * FIND_STRING: find any non-white text
1247 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001248 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 *
1250 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001251 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001253 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 * This doesn't match the real Vi but I like it a little better and it
1255 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001256 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 * When FIND_IDENT isn't defined, we backup until a blank.
1258 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001259 * Returns the length of the text, or zero if no text is found.
1260 * If text is found, a pointer to the text is put in "*text". This
1261 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 */
1263 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001264find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265{
1266 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001267 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268}
1269
1270/*
1271 * Like find_ident_under_cursor(), but for any window and any position.
1272 * However: Uses 'iskeyword' from the current window!.
1273 */
1274 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001275find_ident_at_pos(
1276 win_T *wp,
1277 linenr_T lnum,
1278 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001279 char_u **text,
1280 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001281 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282{
1283 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001284 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 int this_class = 0;
1287 int prev_class;
1288 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001289 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001291 // if i == 0: try to find an identifier
1292 // if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1294 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1295 {
1296 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001297 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 */
1299 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 if (has_mbyte)
1301 {
1302 while (ptr[col] != NUL)
1303 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001304 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1306 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 this_class = mb_get_class(ptr + col);
1308 if (this_class != 0 && (i == 1 || this_class != 1))
1309 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001310 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
1312 }
1313 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001315 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 )
1318 ++col;
1319
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001320 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322
1323 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001324 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 if (has_mbyte)
1327 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001328 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1330 this_class = mb_get_class((char_u *)"a");
1331 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001333 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 {
1335 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1336 prev_class = mb_get_class(ptr + prevcol);
1337 if (this_class != prev_class
1338 && (i == 0
1339 || prev_class == 0
1340 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 && (!(find_type & FIND_EVAL)
1342 || prevcol == 0
1343 || !find_is_eval_item(ptr + prevcol, &prevcol,
1344 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 )
1346 break;
1347 col = prevcol;
1348 }
1349
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001350 // If we don't want just any old text, or we've found an
1351 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 if (this_class > 2)
1353 this_class = 2;
1354 if (!(find_type & FIND_STRING) || this_class == 2)
1355 break;
1356 }
1357 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 {
1359 while (col > 0
1360 && ((i == 0
1361 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001362 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 && (!(find_type & FIND_IDENT)
1364 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 || ((find_type & FIND_EVAL)
1366 && col > 1
1367 && find_is_eval_item(ptr + col - 1, &col,
1368 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 ))
1370 --col;
1371
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001372 // If we don't want just any old text, or we've found an
1373 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1375 break;
1376 }
1377 }
1378
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001379 if (ptr[col] == NUL || (i == 0
1380 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001382 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001383 if ((find_type & FIND_NOERROR) == 0)
1384 {
1385 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001386 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001387 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001388 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 return 0;
1391 }
1392 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001393 *text = ptr;
1394 if (textcol != NULL)
1395 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396
1397 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001398 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 bn = 0;
1401 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 if (has_mbyte)
1404 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001405 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 this_class = mb_get_class(ptr);
1407 while (ptr[col] != NUL
1408 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1409 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 || ((find_type & FIND_EVAL)
1411 && col <= (int)startcol
1412 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001414 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 }
1416 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001418 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 || ((find_type & FIND_EVAL)
1420 && col <= (int)startcol
1421 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424
1425 return col;
1426}
1427
1428/*
1429 * Prepare for redo of a normal command.
1430 */
1431 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001432prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433{
1434 prep_redo(cap->oap->regname, cap->count0,
1435 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1436}
1437
1438/*
1439 * Prepare for redo of any command.
1440 * Note that only the last argument can be a multi-byte char.
1441 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001442 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001443prep_redo(
1444 int regname,
1445 long num,
1446 int cmd1,
1447 int cmd2,
1448 int cmd3,
1449 int cmd4,
1450 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001452 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1453}
1454
1455/*
1456 * Prepare for redo of any command with extra count after "cmd2".
1457 */
1458 void
1459prep_redo_num2(
1460 int regname,
1461 long num1,
1462 int cmd1,
1463 int cmd2,
1464 long num2,
1465 int cmd3,
1466 int cmd4,
1467 int cmd5)
1468{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 ResetRedobuff();
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001470
1471#ifdef FEAT_EVAL
1472 // Put info about a mapping in the redo buffer, so that "." will use the
1473 // same script context.
1474 may_add_last_used_map_to_redobuff();
1475#endif
1476
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001477 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 {
1479 AppendCharToRedobuff('"');
1480 AppendCharToRedobuff(regname);
1481 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001482 if (num1 != 0)
1483 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 if (cmd1 != NUL)
1485 AppendCharToRedobuff(cmd1);
1486 if (cmd2 != NUL)
1487 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001488 if (num2 != 0)
1489 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 if (cmd3 != NUL)
1491 AppendCharToRedobuff(cmd3);
1492 if (cmd4 != NUL)
1493 AppendCharToRedobuff(cmd4);
1494 if (cmd5 != NUL)
1495 AppendCharToRedobuff(cmd5);
1496}
1497
1498/*
1499 * check for operator active and clear it
1500 *
1501 * return TRUE if operator was active
1502 */
1503 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001504checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505{
1506 if (oap->op_type == OP_NOP)
1507 return FALSE;
1508 clearopbeep(oap);
1509 return TRUE;
1510}
1511
1512/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001513 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001515 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 */
1517 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001518checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001520 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 return FALSE;
1522 clearopbeep(oap);
1523 return TRUE;
1524}
1525
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001526 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001527clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528{
1529 oap->op_type = OP_NOP;
1530 oap->regname = 0;
1531 oap->motion_force = NUL;
1532 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001533 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534}
1535
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001536 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001537clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538{
1539 clearop(oap);
1540 beep_flush();
1541}
1542
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543/*
1544 * Remove the shift modifier from a special key.
1545 */
1546 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001547unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548{
1549 switch (cap->cmdchar)
1550 {
1551 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1552 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1553 case K_S_UP: cap->cmdchar = K_UP; break;
1554 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1555 case K_S_HOME: cap->cmdchar = K_HOME; break;
1556 case K_S_END: cap->cmdchar = K_END; break;
1557 }
1558 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1559}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001561/*
1562 * If the mode is currently displayed clear the command line or update the
1563 * command displayed.
1564 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001565 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001566may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001567{
1568 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001569 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001570 else
1571 clear_showcmd();
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001572}
1573
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574/*
1575 * Routines for displaying a partly typed command
1576 */
1577
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001578static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579static int showcmd_is_clear = TRUE;
1580static int showcmd_visual = FALSE;
1581
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001582static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
1584 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001585clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586{
1587 if (!p_sc)
1588 return;
1589
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 if (VIsual_active && !char_avail())
1591 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001592 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 long lines;
1594 colnr_T leftcol, rightcol;
1595 linenr_T top, bot;
1596
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001597 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001598 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 {
1600 top = VIsual.lnum;
1601 bot = curwin->w_cursor.lnum;
1602 }
1603 else
1604 {
1605 top = curwin->w_cursor.lnum;
1606 bot = VIsual.lnum;
1607 }
1608# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001609 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001610 (void)hasFolding(top, &top, NULL);
1611 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612# endif
1613 lines = bot - top + 1;
1614
1615 if (VIsual_mode == Ctrl_V)
1616 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001617# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001618 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001619 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001620
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001621 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001622 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001623 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001624# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001626# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001627 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001628 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001629# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1631 (long)(rightcol - leftcol + 1));
1632 }
1633 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1634 sprintf((char *)showcmd_buf, "%ld", lines);
1635 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001636 {
1637 char_u *s, *e;
1638 int l;
1639 int bytes = 0;
1640 int chars = 0;
1641
1642 if (cursor_bot)
1643 {
1644 s = ml_get_pos(&VIsual);
1645 e = ml_get_cursor();
1646 }
1647 else
1648 {
1649 s = ml_get_cursor();
1650 e = ml_get_pos(&VIsual);
1651 }
1652 while ((*p_sel != 'e') ? s <= e : s < e)
1653 {
1654 l = (*mb_ptr2len)(s);
1655 if (l == 0)
1656 {
1657 ++bytes;
1658 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001659 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001660 }
1661 bytes += l;
1662 ++chars;
1663 s += l;
1664 }
1665 if (bytes == chars)
1666 sprintf((char *)showcmd_buf, "%d", chars);
1667 else
1668 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1669 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001670 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 showcmd_visual = TRUE;
1672 }
1673 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 {
1675 showcmd_buf[0] = NUL;
1676 showcmd_visual = FALSE;
1677
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001678 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 if (showcmd_is_clear)
1680 return;
1681 }
1682
1683 display_showcmd();
1684}
1685
1686/*
1687 * Add 'c' to string of shown command chars.
1688 * Return TRUE if output has been written (and setcursor() has been called).
1689 */
1690 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001691add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692{
1693 char_u *p;
1694 int old_len;
1695 int extra_len;
1696 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 int i;
1698 static int ignore[] =
1699 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001700#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1702 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001703#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001704 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001705 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1707 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001708 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001710 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 0
1712 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713
Bram Moolenaar09df3122006-01-23 22:23:09 +00001714 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 return FALSE;
1716
1717 if (showcmd_visual)
1718 {
1719 showcmd_buf[0] = NUL;
1720 showcmd_visual = FALSE;
1721 }
1722
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001723 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 if (IS_SPECIAL(c))
1725 for (i = 0; ignore[i] != 0; ++i)
1726 if (ignore[i] == c)
1727 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728
1729 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001730 if (*p == ' ')
1731 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 old_len = (int)STRLEN(showcmd_buf);
1733 extra_len = (int)STRLEN(p);
1734 overflow = old_len + extra_len - SHOWCMD_COLS;
1735 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001736 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1737 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 STRCAT(showcmd_buf, p);
1739
1740 if (char_avail())
1741 return FALSE;
1742
1743 display_showcmd();
1744
1745 return TRUE;
1746}
1747
1748 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001749add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750{
1751 if (!add_to_showcmd(c))
1752 setcursor();
1753}
1754
1755/*
1756 * Delete 'len' characters from the end of the shown command.
1757 */
1758 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001759del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760{
1761 int old_len;
1762
1763 if (!p_sc)
1764 return;
1765
1766 old_len = (int)STRLEN(showcmd_buf);
1767 if (len > old_len)
1768 len = old_len;
1769 showcmd_buf[old_len - len] = NUL;
1770
1771 if (!char_avail())
1772 display_showcmd();
1773}
1774
1775/*
1776 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1777 * something and there is a partial mapping.
1778 */
1779 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001780push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
1782 if (p_sc)
1783 STRCPY(old_showcmd_buf, showcmd_buf);
1784}
1785
1786 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001787pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788{
1789 if (!p_sc)
1790 return;
1791
1792 STRCPY(showcmd_buf, old_showcmd_buf);
1793
1794 display_showcmd();
1795}
1796
1797 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001798display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799{
Luuk van Baalba936f62022-12-15 13:15:39 +00001800 int len = (int)STRLEN(showcmd_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
Luuk van Baalba936f62022-12-15 13:15:39 +00001802 showcmd_is_clear = (len == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 cursor_off();
1804
Luuk van Baalba936f62022-12-15 13:15:39 +00001805 if (*p_sloc == 's')
Luuk van Baalaa7f25e2023-04-13 21:47:38 +01001806 {
1807 if (showcmd_is_clear)
1808 curwin->w_redr_status = TRUE;
1809 else
1810 win_redr_status(curwin, FALSE);
1811 }
Luuk van Baalba936f62022-12-15 13:15:39 +00001812 else if (*p_sloc == 't')
Luuk van Baalaa7f25e2023-04-13 21:47:38 +01001813 {
1814 if (showcmd_is_clear)
1815 redraw_tabline = TRUE;
1816 else
1817 draw_tabline();
1818 }
Luuk van Baalba936f62022-12-15 13:15:39 +00001819 else // 'showcmdloc' is "last" or empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 {
Luuk van Baalba936f62022-12-15 13:15:39 +00001821 if (!showcmd_is_clear)
1822 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823
Luuk van Baalba936f62022-12-15 13:15:39 +00001824 // clear the rest of an old message by outputting up to SHOWCMD_COLS
1825 // spaces
1826 screen_puts((char_u *)" " + len,
1827 (int)Rows - 1, sc_col + len, 0);
1828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001830 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833/*
1834 * When "check" is FALSE, prepare for commands that scroll the window.
1835 * When "check" is TRUE, take care of scroll-binding after the window has
1836 * scrolled. Called from normal_cmd() and edit().
1837 */
1838 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001839do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840{
1841 static win_T *old_curwin = NULL;
1842 static linenr_T old_topline = 0;
1843#ifdef FEAT_DIFF
1844 static int old_topfill = 0;
1845#endif
1846 static buf_T *old_buf = NULL;
1847 static colnr_T old_leftcol = 0;
1848
1849 if (check && curwin->w_p_scb)
1850 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001851 // If a ":syncbind" command was just used, don't scroll, only reset
1852 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 if (did_syncbind)
1854 did_syncbind = FALSE;
1855 else if (curwin == old_curwin)
1856 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001857 // Synchronize other windows, as necessary according to
1858 // 'scrollbind'. Don't do this after an ":edit" command, except
1859 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 if ((curwin->w_buffer == old_buf
1861#ifdef FEAT_DIFF
1862 || curwin->w_p_diff
1863#endif
1864 )
1865 && (curwin->w_topline != old_topline
1866#ifdef FEAT_DIFF
1867 || curwin->w_topfill != old_topfill
1868#endif
1869 || curwin->w_leftcol != old_leftcol))
1870 {
1871 check_scrollbind(curwin->w_topline - old_topline,
1872 (long)(curwin->w_leftcol - old_leftcol));
1873 }
1874 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001875 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001877 // When switching between windows, make sure that the relative
1878 // vertical offset is valid for the new window. The relative
1879 // offset is invalid whenever another 'scrollbind' window has
1880 // scrolled to a point that would force the current window to
1881 // scroll past the beginning or end of its buffer. When the
1882 // resync is performed, some of the other 'scrollbind' windows may
1883 // need to jump so that the current window's relative position is
1884 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
1886 }
1887 curwin->w_scbind_pos = curwin->w_topline;
1888 }
1889
1890 old_curwin = curwin;
1891 old_topline = curwin->w_topline;
1892#ifdef FEAT_DIFF
1893 old_topfill = curwin->w_topfill;
1894#endif
1895 old_buf = curwin->w_buffer;
1896 old_leftcol = curwin->w_leftcol;
1897}
1898
1899/*
1900 * Synchronize any windows that have "scrollbind" set, based on the
1901 * number of rows by which the current window has changed
1902 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
1903 */
1904 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001905check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906{
1907 int want_ver;
1908 int want_hor;
1909 win_T *old_curwin = curwin;
1910 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 int old_VIsual_select = VIsual_select;
1912 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 colnr_T tgt_leftcol = curwin->w_leftcol;
1914 long topline;
1915 long y;
1916
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001917 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
1919#ifdef FEAT_DIFF
1920 want_ver |= old_curwin->w_p_diff;
1921#endif
1922 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
1923
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001924 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001926 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 {
1928 curbuf = curwin->w_buffer;
zeertzjq101d57b2022-07-31 18:34:32 +01001929 // skip original window and windows with 'noscrollbind'
1930 if (curwin == old_curwin || !curwin->w_p_scb)
1931 continue;
1932
1933 // do the vertical scroll
1934 if (want_ver)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936#ifdef FEAT_DIFF
zeertzjq101d57b2022-07-31 18:34:32 +01001937 if (old_curwin->w_p_diff && curwin->w_p_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 {
zeertzjq101d57b2022-07-31 18:34:32 +01001939 diff_set_topline(old_curwin, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 }
zeertzjq101d57b2022-07-31 18:34:32 +01001941 else
1942#endif
1943 {
1944 curwin->w_scbind_pos += topline_diff;
1945 topline = curwin->w_scbind_pos;
1946 if (topline > curbuf->b_ml.ml_line_count)
1947 topline = curbuf->b_ml.ml_line_count;
1948 if (topline < 1)
1949 topline = 1;
1950
1951 y = topline - curwin->w_topline;
1952 if (y > 0)
1953 scrollup(y, FALSE);
1954 else
1955 scrolldown(-y, FALSE);
1956 }
1957
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001958 redraw_later(UPD_VALID);
zeertzjq101d57b2022-07-31 18:34:32 +01001959 cursor_correct();
1960 curwin->w_redr_status = TRUE;
1961 }
1962
1963 // do the horizontal scroll
Bram Moolenaar0c34d562022-11-18 14:07:20 +00001964 if (want_hor)
1965 (void)set_leftcol(tgt_leftcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 }
1967
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001968 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 VIsual_select = old_VIsual_select;
1970 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 curwin = old_curwin;
1972 curbuf = old_curbuf;
1973}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974
1975/*
1976 * Command character that's ignored.
1977 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02001978 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001981nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001983 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984}
1985
1986/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00001987 * Command character that doesn't do anything, but unlike nv_ignore() does
1988 * start edit(). Used for "startinsert" executed while starting up.
1989 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00001990 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001991nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001992{
1993}
1994
1995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 * Command character doesn't exist.
1997 */
1998 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001999nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000{
2001 clearopbeep(cap->oap);
2002}
2003
2004/*
2005 * <Help> and <F1> commands.
2006 */
2007 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002008nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009{
2010 if (!checkclearopq(cap->oap))
2011 ex_help(NULL);
2012}
2013
2014/*
2015 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2016 */
2017 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002018nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002020#ifdef FEAT_JOB_CHANNEL
2021 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2022 clearopbeep(cap->oap);
2023 else
2024#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002025 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002026 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002027 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002028 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2029 op_addsub(cap->oap, cap->count1, cap->arg);
2030 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002031 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002032 else if (VIsual_active)
2033 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002034 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002035 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036}
2037
2038/*
2039 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2040 */
2041 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002042nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002044 if (checkclearop(cap->oap))
2045 return;
2046
2047 if (mod_mask & MOD_MASK_CTRL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002048 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002049 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
2050 if (cap->arg == BACKWARD)
2051 goto_tabpage(-(int)cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002052 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002053 goto_tabpage((int)cap->count0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002054 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002055 else
2056 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057}
2058
2059/*
2060 * Implementation of "gd" and "gD" command.
2061 */
2062 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002063nv_gd(
2064 oparg_T *oap,
2065 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002066 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067{
2068 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 char_u *ptr;
2070
Bram Moolenaard9d30582005-05-18 22:10:28 +00002071 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002072 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002073 == FAIL)
2074 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 clearopbeep(oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002076 return;
Bram Moolenaar0c711142021-11-12 10:30:04 +00002077 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002078
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002079#ifdef FEAT_FOLDING
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002080 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2081 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002082#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002083 // clear any search statistics
2084 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2085 clear_cmdline = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002086}
2087
2088/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002089 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2090 * otherwise.
2091 */
2092 static int
2093is_ident(char_u *line, int offset)
2094{
2095 int i;
2096 int incomment = FALSE;
2097 int instring = 0;
2098 int prev = 0;
2099
2100 for (i = 0; i < offset && line[i] != NUL; i++)
2101 {
2102 if (instring != 0)
2103 {
2104 if (prev != '\\' && line[i] == instring)
2105 instring = 0;
2106 }
2107 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2108 {
2109 instring = line[i];
2110 }
2111 else
2112 {
2113 if (incomment)
2114 {
2115 if (prev == '*' && line[i] == '/')
2116 incomment = FALSE;
2117 }
2118 else if (prev == '/' && line[i] == '*')
2119 {
2120 incomment = TRUE;
2121 }
2122 else if (prev == '/' && line[i] == '/')
2123 {
2124 return FALSE;
2125 }
2126 }
2127
2128 prev = line[i];
2129 }
2130
2131 return incomment == FALSE && instring == 0;
2132}
2133
2134/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002135 * Search for variable declaration of "ptr[len]".
2136 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2137 * current file ("gD").
2138 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002139 * Return FAIL when not found.
2140 */
2141 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002142find_decl(
2143 char_u *ptr,
2144 int len,
2145 int locally,
2146 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002147 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002148{
2149 char_u *pat;
2150 pos_T old_pos;
2151 pos_T par_pos;
2152 pos_T found_pos;
2153 int t;
2154 int save_p_ws;
2155 int save_p_scs;
2156 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002157 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002158 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002159 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002160
2161 if ((pat = alloc(len + 7)) == NULL)
2162 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002163
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002164 // Put "\V" before the pattern to avoid that the special meaning of "."
2165 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002166 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2167 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 old_pos = curwin->w_cursor;
2169 save_p_ws = p_ws;
2170 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002171 p_ws = FALSE; // don't wrap around end of file now
2172 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002174 // With "gD" go to line 1.
2175 // With "gd" Search back for the start of the current function, then go
2176 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002177 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002179 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002181 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 }
2183 else
2184 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002185 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2187 --curwin->w_cursor.lnum;
2188 }
2189 curwin->w_cursor.col = 0;
2190
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002191 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002192 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002193 for (;;)
2194 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002195 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002196 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002197 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002198 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002199
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002200 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002201 {
2202 pos_T *pos;
2203
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002204 // Check that the block the match is in doesn't end before the
2205 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002206 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2207 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2208 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002209 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002210 // There can't be a useful match before the end of this block.
2211 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002212 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002213 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002214 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002215 }
2216
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002217 if (t == FAIL)
2218 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002219 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002220 if (found_pos.lnum != 0)
2221 {
2222 curwin->w_cursor = found_pos;
2223 t = OK;
2224 }
2225 break;
2226 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002227 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002228 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002229 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002230 ++curwin->w_cursor.lnum;
2231 curwin->w_cursor.col = 0;
2232 continue;
2233 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002234 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2235
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002236 // If the current position is not a valid identifier and a previous
2237 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002238 if (!valid && found_pos.lnum != 0)
2239 {
2240 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002241 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002242 }
2243
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002244 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002245 if (valid && !locally)
2246 break;
2247 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002248 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002249 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002250 if (found_pos.lnum != 0)
2251 curwin->w_cursor = found_pos;
2252 break;
2253 }
2254
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002255 // For finding a local variable and the match is before the "{" or
2256 // inside a comment, continue searching. For K&R style function
2257 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002258 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002259 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002260 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002261 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002262 // Remove SEARCH_START from flags to avoid getting stuck at one
2263 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002264 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002266
2267 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002269 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 curwin->w_cursor = old_pos;
2271 }
2272 else
2273 {
2274 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002275 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 reset_search_dir();
2277 }
2278
2279 vim_free(pat);
2280 p_ws = save_p_ws;
2281 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002282
2283 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284}
2285
2286/*
2287 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2288 * lines rather than lines in the file.
2289 * 'dist' must be positive.
2290 *
2291 * Return OK if able to move cursor, FAIL otherwise.
2292 */
2293 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002294nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295{
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002296 int linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 int retval = OK;
2298 int atend = FALSE;
2299 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002300 int col_off1; // margin offset for first screen line
2301 int col_off2; // margin offset for wrapped screen line
2302 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002303 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304
2305 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002306 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307
2308 col_off1 = curwin_col_off();
2309 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002310 width1 = curwin->w_width - col_off1;
2311 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002312 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002313 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002316 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002317 // Instead of sticking at the last character of the buffer line we
2318 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 if (curwin->w_curswant == MAXCOL)
2320 {
2321 atend = TRUE;
2322 validate_virtcol();
2323 if (width1 <= 0)
2324 curwin->w_curswant = 0;
2325 else
2326 {
2327 curwin->w_curswant = width1 - 1;
2328 if (curwin->w_virtcol > curwin->w_curswant)
2329 curwin->w_curswant += ((curwin->w_virtcol
2330 - curwin->w_curswant - 1) / width2 + 1) * width2;
2331 }
2332 }
2333 else
2334 {
2335 if (linelen > width1)
2336 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2337 else
2338 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002339 if (curwin->w_curswant >= (colnr_T)n)
2340 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 }
2342
2343 while (dist--)
2344 {
2345 if (dir == BACKWARD)
2346 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002347 if ((long)curwin->w_curswant >= width1
2348#ifdef FEAT_FOLDING
2349 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2350#endif
2351 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002352 // Move back within the line. This can give a negative value
2353 // for w_curswant if width1 < width2 (with cpoptions+=n),
2354 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 curwin->w_curswant -= width2;
2356 else
2357 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002358 // to previous line
Luuk van Baal441a7a92023-02-18 20:15:44 +00002359 if (!cursor_up_inner(curwin, 1))
Bram Moolenaare71996b2021-01-21 17:03:07 +01002360 {
2361 retval = FAIL;
2362 break;
2363 }
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002364 linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 if (linelen > width1)
2366 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2367 + 1) * width2;
2368 }
2369 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002370 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
2372 if (linelen > width1)
2373 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2374 else
2375 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002376 if (curwin->w_curswant + width2 < (colnr_T)n
2377#ifdef FEAT_FOLDING
2378 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2379#endif
2380 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002381 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 curwin->w_curswant += width2;
2383 else
2384 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002385 // to next line
Luuk van Baal441a7a92023-02-18 20:15:44 +00002386 if (!cursor_down_inner(curwin, 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 {
2388 retval = FAIL;
2389 break;
2390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002392 // Check if the cursor has moved below the number display
2393 // when width1 < width2 (with cpoptions+=n). Subtract width2
2394 // to get a negative value for w_curswant, which will get
2395 // clipped to column 0.
2396 if (curwin->w_curswant >= width1)
2397 curwin->w_curswant -= width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002398 linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
2400 }
2401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002404 if (virtual_active() && atend)
2405 coladvance(MAXCOL);
2406 else
2407 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2410 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002411 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002412 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002413
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002414 // Check for landing on a character that got split at the end of the
2415 // last line. We want to advance a screenline, not end up in the same
2416 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002418 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002419#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002420 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2421 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002422#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002423
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002424 c = (*mb_ptr2char)(ml_get_cursor());
2425 if (dir == FORWARD && virtcol < curwin->w_curswant
2426 && (curwin->w_curswant <= (colnr_T)width1)
2427 && !vim_isprintc(c) && c > 255)
2428 oneright();
2429
Bram Moolenaar773b1582014-08-29 14:20:51 +02002430 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 && (curwin->w_curswant < (colnr_T)width1
2432 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2433 : ((curwin->w_curswant - width1) % width2
2434 > (colnr_T)width2 / 2)))
2435 --curwin->w_cursor.col;
2436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437
2438 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002439 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002440 adjust_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441
2442 return retval;
2443}
2444
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445/*
2446 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2447 * cap->arg must be TRUE for CTRL-E.
2448 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002449 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002450nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451{
2452 if (!checkclearop(cap->oap))
2453 scroll_redraw(cap->arg, cap->count1);
2454}
2455
2456/*
2457 * Scroll "count" lines up or down, and redraw.
2458 */
2459 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002460scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461{
2462 linenr_T prev_topline = curwin->w_topline;
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002463 int prev_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464#ifdef FEAT_DIFF
2465 int prev_topfill = curwin->w_topfill;
2466#endif
2467 linenr_T prev_lnum = curwin->w_cursor.lnum;
2468
2469 if (up)
2470 scrollup(count, TRUE);
2471 else
2472 scrolldown(count, TRUE);
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002473 if (get_scrolloff_value() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002475 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2476 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 cursor_correct();
2478 check_cursor_moved(curwin);
2479 curwin->w_valid |= VALID_TOPLINE;
2480
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002481 // If moved back to where we were, at least move the cursor, otherwise
2482 // we get stuck at one position. Don't move the cursor up if the
2483 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 while (curwin->w_topline == prev_topline
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002485 && curwin->w_skipcol == prev_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486#ifdef FEAT_DIFF
2487 && curwin->w_topfill == prev_topfill
2488#endif
2489 )
2490 {
2491 if (up)
2492 {
2493 if (curwin->w_cursor.lnum > prev_lnum
2494 || cursor_down(1L, FALSE) == FAIL)
2495 break;
2496 }
2497 else
2498 {
2499 if (curwin->w_cursor.lnum < prev_lnum
2500 || prev_topline == 1L
2501 || cursor_up(1L, FALSE) == FAIL)
2502 break;
2503 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002504 // Mark w_topline as valid, otherwise the screen jumps back at the
2505 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 check_cursor_moved(curwin);
2507 curwin->w_valid |= VALID_TOPLINE;
2508 }
2509 }
2510 if (curwin->w_cursor.lnum != prev_lnum)
2511 coladvance(curwin->w_curswant);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002512 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513}
2514
2515/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002516 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2517 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2518 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002519 */
2520 static int
2521nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2522{
2523 int nchar = *nchar_arg;
2524 long n;
2525
2526 // "z123{nchar}": edit the count before obtaining {nchar}
2527 if (checkclearop(cap->oap))
2528 return FALSE;
2529 n = nchar - '0';
2530
2531 for (;;)
2532 {
2533#ifdef USE_ON_FLY_SCROLL
2534 dont_scroll = TRUE; // disallow scrolling here
2535#endif
2536 ++no_mapping;
2537 ++allow_keys; // no mapping for nchar, but allow key codes
2538 nchar = plain_vgetc();
2539 LANGMAP_ADJUST(nchar, TRUE);
2540 --no_mapping;
2541 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002542 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002543
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002544 if (nchar == K_DEL || nchar == K_KDEL)
2545 n /= 10;
2546 else if (VIM_ISDIGIT(nchar))
2547 n = n * 10 + (nchar - '0');
2548 else if (nchar == CAR)
2549 {
2550#ifdef FEAT_GUI
2551 need_mouse_correct = TRUE;
2552#endif
2553 win_setheight((int)n);
2554 break;
2555 }
2556 else if (nchar == 'l'
2557 || nchar == 'h'
2558 || nchar == K_LEFT
2559 || nchar == K_RIGHT)
2560 {
2561 cap->count1 = n ? n * cap->count1 : cap->count1;
2562 *nchar_arg = nchar;
2563 return TRUE;
2564 }
2565 else
2566 {
2567 clearopbeep(cap->oap);
2568 break;
2569 }
2570 }
2571 cap->oap->op_type = OP_NOP;
2572 return FALSE;
2573}
2574
2575#ifdef FEAT_SPELL
2576/*
2577 * "zug" and "zuw": undo "zg" and "zw"
2578 * "zg": add good word to word list
2579 * "zw": add wrong word to word list
2580 * "zG": add good word to temp word list
2581 * "zW": add wrong word to temp word list
2582 */
2583 static int
2584nv_zg_zw(cmdarg_T *cap, int nchar)
2585{
2586 char_u *ptr = NULL;
2587 int len;
2588 int undo = FALSE;
2589
2590 if (nchar == 'u')
2591 {
2592 ++no_mapping;
2593 ++allow_keys; // no mapping for nchar, but allow key codes
2594 nchar = plain_vgetc();
2595 LANGMAP_ADJUST(nchar, TRUE);
2596 --no_mapping;
2597 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002598 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002599
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002600 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2601 {
2602 clearopbeep(cap->oap);
2603 return OK;
2604 }
2605 undo = TRUE;
2606 }
2607
2608 if (checkclearop(cap->oap))
2609 return OK;
2610 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2611 return FAIL;
2612 if (ptr == NULL)
2613 {
2614 pos_T pos = curwin->w_cursor;
2615
2616 // Find bad word under the cursor. When 'spell' is
2617 // off this fails and find_ident_under_cursor() is
2618 // used below.
2619 emsg_off++;
2620 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2621 emsg_off--;
2622 if (len != 0 && curwin->w_cursor.col <= pos.col)
2623 ptr = ml_get_pos(&curwin->w_cursor);
2624 curwin->w_cursor = pos;
2625 }
2626
2627 if (ptr == NULL
2628 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2629 return FAIL;
2630 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2631 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2632 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2633
2634 return OK;
2635}
2636#endif
2637
2638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 * Commands that start with "z".
2640 */
2641 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002642nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643{
2644 long n;
2645 colnr_T col;
2646 int nchar = cap->nchar;
2647#ifdef FEAT_FOLDING
2648 long old_fdl = curwin->w_p_fdl;
2649 int old_fen = curwin->w_p_fen;
2650#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002651 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002653 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 if (
2657#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002658 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2659 // and "zC" only in Visual mode. "zj" and "zk" are motion
2660 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 cap->nchar != 'f' && cap->nchar != 'F'
2662 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2663 && cap->nchar != 'j' && cap->nchar != 'k'
2664 &&
2665#endif
2666 checkclearop(cap->oap))
2667 return;
2668
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002669 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2670 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2672 && cap->count0
2673 && cap->count0 != curwin->w_cursor.lnum)
2674 {
2675 setpcmark();
2676 if (cap->count0 > curbuf->b_ml.ml_line_count)
2677 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2678 else
2679 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002680 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 }
2682
2683 switch (nchar)
2684 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002685 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 case '+':
2687 if (cap->count0 == 0)
2688 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002689 // No count given: put cursor at the line below screen
2690 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2692 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2693 else
2694 curwin->w_cursor.lnum = curwin->w_botline;
2695 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002696 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 case NL:
2698 case CAR:
2699 case K_KENTER:
2700 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002701 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702
2703 case 't': scroll_cursor_top(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002704 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002705 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 break;
2707
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002708 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002710 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002712 case 'z': scroll_cursor_halfway(TRUE, FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002713 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002714 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 break;
2716
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002717 // "z^", "z-" and "zb": put cursor at bottom of screen
2718 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2719 // when <count> is at bottom of window, and puts that one at
2720 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 if (cap->count0 != 0)
2722 {
2723 scroll_cursor_bot(0, TRUE);
2724 curwin->w_cursor.lnum = curwin->w_topline;
2725 }
2726 else if (curwin->w_topline == 1)
2727 curwin->w_cursor.lnum = 1;
2728 else
2729 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002730 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 case '-':
2732 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002733 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734
2735 case 'b': scroll_cursor_bot(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002736 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002737 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 break;
2739
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002740 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002742 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002743 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002745 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 case 'h':
2747 case K_LEFT:
2748 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002749 (void)set_leftcol((colnr_T)cap->count1 > curwin->w_leftcol
2750 ? 0 : curwin->w_leftcol - (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 break;
2752
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002753 // "zL" - scroll window left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002754 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002755 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002757 // "zl" - scroll window to the left if not wrapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 case 'l':
2759 case K_RIGHT:
2760 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002761 (void)set_leftcol(curwin->w_leftcol + (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 break;
2763
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002764 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 case 's': if (!curwin->w_p_wrap)
2766 {
2767#ifdef FEAT_FOLDING
2768 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002769 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 else
2771#endif
2772 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002773 if ((long)col > siso)
2774 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 else
2776 col = 0;
2777 if (curwin->w_leftcol != col)
2778 {
2779 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002780 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 }
2782 }
2783 break;
2784
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002785 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 case 'e': if (!curwin->w_p_wrap)
2787 {
2788#ifdef FEAT_FOLDING
2789 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002790 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 else
2792#endif
2793 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002794 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002795 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 col = 0;
2797 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002798 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 if (curwin->w_leftcol != col)
2800 {
2801 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002802 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
2804 }
2805 break;
2806
Christian Brabandt2fa93842021-05-30 22:17:25 +02002807 // "zp", "zP" in block mode put without addind trailing spaces
2808 case 'P':
2809 case 'p': nv_put(cap);
2810 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002811 // "zy" Yank without trailing spaces
2812 case 'y': nv_operator(cap);
2813 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002815 // "zF": create fold command
2816 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 case 'F':
2818 case 'f': if (foldManualAllowed(TRUE))
2819 {
2820 cap->nchar = 'f';
2821 nv_operator(cap);
2822 curwin->w_p_fen = TRUE;
2823
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002824 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2826 {
2827 nv_operator(cap);
2828 finish_op = TRUE;
2829 }
2830 }
2831 else
2832 clearopbeep(cap->oap);
2833 break;
2834
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002835 // "zd": delete fold at cursor
2836 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 case 'd':
2838 case 'D': if (foldManualAllowed(FALSE))
2839 {
2840 if (VIsual_active)
2841 nv_operator(cap);
2842 else
2843 deleteFold(curwin->w_cursor.lnum,
2844 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2845 }
2846 break;
2847
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002848 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 case 'E': if (foldmethodIsManual(curwin))
2850 {
2851 clearFolding(curwin);
2852 changed_window_setting();
2853 }
2854 else if (foldmethodIsMarker(curwin))
2855 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2856 TRUE, FALSE);
2857 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002858 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 break;
2860
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002861 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 case 'n': curwin->w_p_fen = FALSE;
2863 break;
2864
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002865 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 case 'N': curwin->w_p_fen = TRUE;
2867 break;
2868
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002869 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2871 break;
2872
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002873 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2875 openFold(curwin->w_cursor.lnum, cap->count1);
2876 else
2877 {
2878 closeFold(curwin->w_cursor.lnum, cap->count1);
2879 curwin->w_p_fen = TRUE;
2880 }
2881 break;
2882
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002883 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2885 openFoldRecurse(curwin->w_cursor.lnum);
2886 else
2887 {
2888 closeFoldRecurse(curwin->w_cursor.lnum);
2889 curwin->w_p_fen = TRUE;
2890 }
2891 break;
2892
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002893 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 case 'o': if (VIsual_active)
2895 nv_operator(cap);
2896 else
2897 openFold(curwin->w_cursor.lnum, cap->count1);
2898 break;
2899
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002900 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 case 'O': if (VIsual_active)
2902 nv_operator(cap);
2903 else
2904 openFoldRecurse(curwin->w_cursor.lnum);
2905 break;
2906
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002907 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 case 'c': if (VIsual_active)
2909 nv_operator(cap);
2910 else
2911 closeFold(curwin->w_cursor.lnum, cap->count1);
2912 curwin->w_p_fen = TRUE;
2913 break;
2914
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002915 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 case 'C': if (VIsual_active)
2917 nv_operator(cap);
2918 else
2919 closeFoldRecurse(curwin->w_cursor.lnum);
2920 curwin->w_p_fen = TRUE;
2921 break;
2922
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002923 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 case 'v': foldOpenCursor();
2925 break;
2926
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002927 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002929 curwin->w_foldinvalid = TRUE; // recompute folds
2930 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 foldOpenCursor();
2932 break;
2933
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002934 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002936 curwin->w_foldinvalid = TRUE; // recompute folds
2937 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 break;
2939
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002940 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002942 {
2943 curwin->w_p_fdl -= cap->count1;
2944 if (curwin->w_p_fdl < 0)
2945 curwin->w_p_fdl = 0;
2946 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002947 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 curwin->w_p_fen = TRUE;
2949 break;
2950
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002951 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002953 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 curwin->w_p_fen = TRUE;
2955 break;
2956
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002957 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002958 case 'r': curwin->w_p_fdl += cap->count1;
2959 {
2960 int d = getDeepestNesting();
2961
2962 if (curwin->w_p_fdl >= d)
2963 curwin->w_p_fdl = d;
2964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 break;
2966
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002967 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002969 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 break;
2971
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002972 case 'j': // "zj" move to next fold downwards
2973 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2975 cap->count1) == FAIL)
2976 clearopbeep(cap->oap);
2977 break;
2978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002979#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002981#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002982 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002983 case 'g': // "zg": add good word to word list
2984 case 'w': // "zw": add wrong word to word list
2985 case 'G': // "zG": add good word to temp word list
2986 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002987 if (nv_zg_zw(cap, nchar) == FAIL)
2988 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002989 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002990
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002991 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00002992 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00002993 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002994 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002995#endif
2996
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 default: clearopbeep(cap->oap);
2998 }
2999
3000#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003001 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 if (old_fen != curwin->w_p_fen)
3003 {
3004# ifdef FEAT_DIFF
3005 win_T *wp;
3006
3007 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3008 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003009 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 FOR_ALL_WINDOWS(wp)
3011 {
3012 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3013 {
3014 wp->w_p_fen = curwin->w_p_fen;
3015 changed_window_setting_win(wp);
3016 }
3017 }
3018 }
3019# endif
3020 changed_window_setting();
3021 }
3022
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003023 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 if (old_fdl != curwin->w_p_fdl)
3025 newFoldLevel();
3026#endif
3027}
3028
3029#ifdef FEAT_GUI
3030/*
3031 * Vertical scrollbar movement.
3032 */
3033 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003034nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035{
3036 if (cap->oap->op_type != OP_NOP)
3037 clearopbeep(cap->oap);
3038
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003039 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 gui_do_scroll();
3041}
3042
3043/*
3044 * Horizontal scrollbar movement.
3045 */
3046 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003047nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048{
3049 if (cap->oap->op_type != OP_NOP)
3050 clearopbeep(cap->oap);
3051
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003052 // Even if an operator was pending, we still want to scroll
Christopher Plewright44c22092022-11-15 17:43:36 +00003053 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054}
3055#endif
3056
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003057#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003058/*
3059 * Click in GUI tab.
3060 */
3061 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003062nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003063{
3064 if (cap->oap->op_type != OP_NOP)
3065 clearopbeep(cap->oap);
3066
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003067 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003068 goto_tabpage(current_tab);
3069}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003070
3071/*
3072 * Selected item in tab line menu.
3073 */
3074 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003075nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003076{
3077 if (cap->oap->op_type != OP_NOP)
3078 clearopbeep(cap->oap);
3079
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003080 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003081 handle_tabmenu();
3082}
3083
3084/*
3085 * Handle selecting an item of the GUI tab line menu.
3086 * Used in Normal and Insert mode.
3087 */
3088 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003089handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003090{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003091 switch (current_tabmenu)
3092 {
3093 case TABLINE_MENU_CLOSE:
3094 if (current_tab == 0)
3095 do_cmdline_cmd((char_u *)"tabclose");
3096 else
3097 {
3098 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3099 current_tab);
3100 do_cmdline_cmd(IObuff);
3101 }
3102 break;
3103
3104 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003105 if (current_tab == 0)
3106 do_cmdline_cmd((char_u *)"$tabnew");
3107 else
3108 {
3109 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3110 current_tab - 1);
3111 do_cmdline_cmd(IObuff);
3112 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003113 break;
3114
3115 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003116 if (current_tab == 0)
3117 do_cmdline_cmd((char_u *)"browse $tabnew");
3118 else
3119 {
3120 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3121 current_tab - 1);
3122 do_cmdline_cmd(IObuff);
3123 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003124 break;
3125 }
3126}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003127#endif
3128
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129/*
3130 * "Q" command.
3131 */
3132 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003133nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003135 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003137 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003138 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 do_exmode(FALSE);
3140}
3141
3142/*
3143 * Handle a ":" command.
3144 */
3145 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003146nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003148 int old_p_im;
3149 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003150 int is_cmdkey = cap->cmdchar == K_COMMAND
3151 || cap->cmdchar == K_SCRIPT_COMMAND;
3152 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153
Bram Moolenaar957cf672020-11-12 14:21:06 +01003154 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003156 nv_operator(cap);
3157 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003159
3160 if (cap->oap->op_type != OP_NOP)
3161 {
3162 // Using ":" as a movement is characterwise exclusive.
3163 cap->oap->motion_type = MCHAR;
3164 cap->oap->inclusive = FALSE;
3165 }
3166 else if (cap->count0 && !is_cmdkey)
3167 {
3168 // translate "count:" into ":.,.+(count - 1)"
3169 stuffcharReadbuff('.');
3170 if (cap->count0 > 1)
3171 {
3172 stuffReadbuff((char_u *)",.+");
3173 stuffnumReadbuff((long)cap->count0 - 1L);
3174 }
3175 }
3176
3177 // When typing, don't type below an old message
3178 if (KeyTyped)
3179 compute_cmdrow();
3180
3181 old_p_im = p_im;
3182
3183 // get a command line and execute it
3184 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3185 if (is_cmdkey)
3186 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3187 else
3188 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
3189
3190 // If 'insertmode' changed, enter or exit Insert mode
3191 if (p_im != old_p_im)
3192 {
3193 if (p_im)
3194 restart_edit = 'i';
3195 else
3196 restart_edit = 0;
3197 }
3198
3199 if (cmd_result == FAIL)
3200 // The Ex command failed, do not execute the operator.
3201 clearop(cap->oap);
3202 else if (cap->oap->op_type != OP_NOP
3203 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3204 || cap->oap->start.col >
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003205 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003206 || did_emsg
3207 ))
3208 // The start of the operator has become invalid by the Ex command.
3209 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210}
3211
3212/*
3213 * Handle CTRL-G command.
3214 */
3215 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003216nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003218 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 {
3220 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003221 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 showmode();
3223 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003224 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003225 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 fileinfo((int)cap->count0, FALSE, TRUE);
3227}
3228
3229/*
3230 * Handle CTRL-H <Backspace> command.
3231 */
3232 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003233nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 if (VIsual_active && VIsual_select)
3236 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003237 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 v_visop(cap);
3239 }
3240 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 nv_left(cap);
3242}
3243
3244/*
3245 * CTRL-L: clear screen and redraw.
3246 */
3247 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003248nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003250 if (checkclearop(cap->oap))
3251 return;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003252
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003253#ifdef FEAT_SYN_HL
3254 // Clear all syntax states to force resyncing.
3255 syn_stack_free_all(curwin->w_s);
3256# ifdef FEAT_RELTIME
3257 {
3258 win_T *wp;
3259
3260 FOR_ALL_WINDOWS(wp)
3261 wp->w_s->b_syn_slow = FALSE;
3262 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003263# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003265 redraw_later(UPD_CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003266#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3267# ifdef VIMDLL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003268 if (!gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003269# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003270 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272}
3273
3274/*
3275 * CTRL-O: In Select mode: switch to Visual mode for one command.
3276 * Otherwise: Go to older pcmark.
3277 */
3278 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003279nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 if (VIsual_active && VIsual_select)
3282 {
3283 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003284 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003286 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 }
3288 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 {
3290 cap->count1 = -cap->count1;
3291 nv_pcmark(cap);
3292 }
3293}
3294
3295/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003296 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3297 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 */
3299 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003300nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301{
3302 if (!checkclearopq(cap->oap))
3303 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3304 GETF_SETMARK|GETF_ALT, FALSE);
3305}
3306
3307/*
3308 * "Z" commands.
3309 */
3310 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003311nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003313 if (checkclearopq(cap->oap))
3314 return;
3315
3316 switch (cap->nchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003318 // "ZZ": equivalent to ":x".
3319 case 'Z': do_cmdline_cmd((char_u *)"x");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 break;
3321
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003322 // "ZQ": equivalent to ":q!" (Elvis compatible).
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003323 case 'Q': do_cmdline_cmd((char_u *)"q!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 break;
3325
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003326 default: clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 }
3328}
3329
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330/*
3331 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3332 */
3333 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003334do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335{
3336 oparg_T oa;
3337 cmdarg_T ca;
3338
3339 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003340 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 ca.oap = &oa;
3342 ca.cmdchar = c1;
3343 ca.nchar = c2;
3344 nv_ident(&ca);
3345}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346
3347/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003348 * 'K' normal-mode command. Get the command to lookup the keyword under the
3349 * cursor.
3350 */
3351 static int
3352nv_K_getcmd(
3353 cmdarg_T *cap,
3354 char_u *kp,
3355 int kp_help,
3356 int kp_ex,
3357 char_u **ptr_arg,
3358 int n,
3359 char_u *buf,
3360 unsigned buflen)
3361{
3362 char_u *ptr = *ptr_arg;
3363 int isman;
3364 int isman_s;
3365
3366 if (kp_help)
3367 {
3368 // in the help buffer
3369 STRCPY(buf, "he! ");
3370 return n;
3371 }
3372
3373 if (kp_ex)
3374 {
3375 // 'keywordprog' is an ex command
3376 if (cap->count0 != 0)
3377 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3378 else
3379 STRCPY(buf, kp);
3380 STRCAT(buf, " ");
3381 return n;
3382 }
3383
3384 // An external command will probably use an argument starting
3385 // with "-" as an option. To avoid trouble we skip the "-".
3386 while (*ptr == '-' && n > 0)
3387 {
3388 ++ptr;
3389 --n;
3390 }
3391 if (n == 0)
3392 {
3393 // found dashes only
3394 emsg(_(e_no_identifier_under_cursor));
3395 vim_free(buf);
3396 *ptr_arg = ptr;
3397 return 0;
3398 }
3399
3400 // When a count is given, turn it into a range. Is this
3401 // really what we want?
3402 isman = (STRCMP(kp, "man") == 0);
3403 isman_s = (STRCMP(kp, "man -s") == 0);
3404 if (cap->count0 != 0 && !(isman || isman_s))
3405 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3406
3407 STRCAT(buf, "! ");
3408 if (cap->count0 == 0 && isman_s)
3409 STRCAT(buf, "man");
3410 else
3411 STRCAT(buf, kp);
3412 STRCAT(buf, " ");
3413 if (cap->count0 != 0 && (isman || isman_s))
3414 {
3415 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3416 STRCAT(buf, " ");
3417 }
3418
3419 *ptr_arg = ptr;
3420 return n;
3421}
3422
3423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 * Handle the commands that use the word under the cursor.
3425 * [g] CTRL-] :ta to current identifier
3426 * [g] 'K' run program for current identifier
3427 * [g] '*' / to current identifier or string
3428 * [g] '#' ? to current identifier or string
3429 * g ']' :tselect for current identifier
3430 */
3431 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003432nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
3434 char_u *ptr = NULL;
3435 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003436 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003437 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003439 char_u *kp; // value of 'keywordprg'
3440 int kp_help; // 'keywordprg' is ":he"
3441 int kp_ex; // 'keywordprg' starts with ":"
3442 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003444 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003445 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003448 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 {
3450 cmdchar = cap->nchar;
3451 g_cmd = TRUE;
3452 }
3453 else
3454 {
3455 cmdchar = cap->cmdchar;
3456 g_cmd = FALSE;
3457 }
3458
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003459 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 cmdchar = '#';
3461
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003462 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3464 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3466 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 if (checkclearopq(cap->oap))
3468 return;
3469 }
3470
3471 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3472 (cmdchar == '*' || cmdchar == '#')
3473 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3474 {
3475 clearop(cap->oap);
3476 return;
3477 }
3478
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003479 // Allocate buffer to put the command in. Inserting backslashes can
3480 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3481 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3483 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3484 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003485 if (kp_help && *skipwhite(ptr) == NUL)
3486 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003487 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003488 return;
3489 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003490 kp_ex = (*kp == ':');
3491 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3492 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 if (buf == NULL)
3494 return;
3495 buf[0] = NUL;
3496
3497 switch (cmdchar)
3498 {
3499 case '*':
3500 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003501 // Put cursor at start of word, makes search skip the word
3502 // under the cursor.
3503 // Call setpcmark() first, so "*``" puts the cursor back where
3504 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 setpcmark();
3506 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3507
3508 if (!g_cmd && vim_iswordp(ptr))
3509 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003510 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 break;
3512
3513 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003514 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3515 if (n == 0)
3516 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 break;
3518
3519 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003520 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521#ifdef FEAT_CSCOPE
3522 if (p_cst)
3523 STRCPY(buf, "cstag ");
3524 else
3525#endif
3526 STRCPY(buf, "ts ");
3527 break;
3528
3529 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003530 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 if (curbuf->b_help)
3532 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003534 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003535 if (g_cmd)
3536 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003537 else if (cap->count0 == 0)
3538 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003539 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003540 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 }
3543
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003544 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003545 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 {
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 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003560 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003561 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003562 {
3563 vim_free(buf);
3564 vim_free(p);
3565 return;
3566 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003567 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003568 STRCAT(buf, p);
3569 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003571 else
3572 {
3573 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003574 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003575 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003576 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003577 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003578 {
3579 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003580 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003581 aux_ptr = (char_u *)"";
3582 else
3583 aux_ptr = (char_u *)"\\|\"\n[";
3584 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003585 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003586 aux_ptr = (char_u *)"\\|\"\n*?[";
3587
3588 p = buf + STRLEN(buf);
3589 while (n-- > 0)
3590 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003591 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003592 if (vim_strchr(aux_ptr, *ptr) != NULL)
3593 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003594 // When current byte is a part of multibyte character, copy all
3595 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003596 if (has_mbyte)
3597 {
3598 int i;
3599 int len = (*mb_ptr2len)(ptr) - 1;
3600
3601 for (i = 0; i < len && n >= 1; ++i, --n)
3602 *p++ = *ptr++;
3603 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003604 *p++ = *ptr++;
3605 }
3606 *p = NUL;
3607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003609 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 if (cmdchar == '*' || cmdchar == '#')
3611 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003612 if (!g_cmd && (has_mbyte
3613 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3614 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003616
3617 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003618 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003620
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003621 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 }
3623 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003624 {
3625 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003627 g_tag_at_cursor = FALSE;
3628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629
3630 vim_free(buf);
3631}
3632
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633/*
3634 * Get visually selected text, within one line only.
3635 * Returns FAIL if more than one line selected.
3636 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003637 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003638get_visual_text(
3639 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003640 char_u **pp, // return: start of selected text
3641 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642{
3643 if (VIsual_mode != 'V')
3644 unadjust_for_sel();
3645 if (VIsual.lnum != curwin->w_cursor.lnum)
3646 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003647 if (cap != NULL)
3648 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 return FAIL;
3650 }
3651 if (VIsual_mode == 'V')
3652 {
3653 *pp = ml_get_curline();
3654 *lenp = (int)STRLEN(*pp);
3655 }
3656 else
3657 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003658 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
3660 *pp = ml_get_pos(&curwin->w_cursor);
3661 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3662 }
3663 else
3664 {
3665 *pp = ml_get_pos(&VIsual);
3666 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3667 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003668 if (**pp == NUL)
3669 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003670 if (*lenp > 0)
3671 {
3672 if (has_mbyte)
3673 // Correct the length to include all bytes of the last
3674 // character.
3675 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3676 else if ((*pp)[*lenp - 1] == NUL)
3677 // Do not include a trailing NUL.
3678 *lenp -= 1;
3679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
3681 reset_VIsual_and_resel();
3682 return OK;
3683}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684
3685/*
3686 * CTRL-T: backwards in tag stack
3687 */
3688 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003689nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690{
3691 if (!checkclearopq(cap->oap))
3692 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3693}
3694
3695/*
3696 * Handle scrolling command 'H', 'L' and 'M'.
3697 */
3698 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003699nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700{
3701 int used = 0;
3702 long n;
3703#ifdef FEAT_FOLDING
3704 linenr_T lnum;
3705#endif
3706 int half;
3707
3708 cap->oap->motion_type = MLINE;
3709 setpcmark();
3710
3711 if (cap->cmdchar == 'L')
3712 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003713 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 curwin->w_cursor.lnum = curwin->w_botline - 1;
3715 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3716 curwin->w_cursor.lnum = 1;
3717 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003718 {
3719#ifdef FEAT_FOLDING
3720 if (hasAnyFolding(curwin))
3721 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003722 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003723 for (n = cap->count1 - 1; n > 0
3724 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3725 {
3726 (void)hasFolding(curwin->w_cursor.lnum,
3727 &curwin->w_cursor.lnum, NULL);
Bram Moolenaar232bdaa2023-01-13 14:17:58 +00003728 if (curwin->w_cursor.lnum > curwin->w_topline)
3729 --curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003730 }
3731 }
3732 else
3733#endif
3734 curwin->w_cursor.lnum -= cap->count1 - 1;
3735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737 else
3738 {
3739 if (cap->cmdchar == 'M')
3740 {
3741#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003742 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 used -= diff_check_fill(curwin, curwin->w_topline)
3744 - curwin->w_topfill;
3745#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003746 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3748 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3749 {
3750#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003751 // Count half he number of filler lines to be "below this
3752 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3754 + n) / 2 >= half)
3755 {
3756 --n;
3757 break;
3758 }
3759#endif
3760 used += plines(curwin->w_topline + n);
3761 if (used >= half)
3762 break;
3763#ifdef FEAT_FOLDING
3764 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3765 n = lnum - curwin->w_topline;
3766#endif
3767 }
3768 if (n > 0 && used > curwin->w_height)
3769 --n;
3770 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003771 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003772 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003774#ifdef FEAT_FOLDING
3775 if (hasAnyFolding(curwin))
3776 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003777 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003778 lnum = curwin->w_topline;
3779 while (n-- > 0 && lnum < curwin->w_botline - 1)
3780 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003781 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003782 ++lnum;
3783 }
3784 n = lnum - curwin->w_topline;
3785 }
3786#endif
3787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 curwin->w_cursor.lnum = curwin->w_topline + n;
3789 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3790 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3791 }
3792
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003793 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003794 if (cap->oap->op_type == OP_NOP)
3795 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 beginline(BL_SOL | BL_FIX);
3797}
3798
3799/*
3800 * Cursor right commands.
3801 */
3802 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003803nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804{
3805 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003806 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003808 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3809 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003810 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003811 if (mod_mask & MOD_MASK_CTRL)
3812 cap->arg = TRUE;
3813 nv_wordcmd(cap);
3814 return;
3815 }
3816
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 cap->oap->motion_type = MCHAR;
3818 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003819 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003821 // In virtual edit mode, there's no such thing as "past_line", as lines
3822 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003824 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825
3826 for (n = cap->count1; n > 0; --n)
3827 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003828 if ((!past_line && oneright() == FAIL)
3829 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003830 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003832 // <Space> wraps to next line if 'whichwrap' has 's'.
3833 // 'l' wraps to next line if 'whichwrap' has 'l'.
3834 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 if ( ((cap->cmdchar == ' '
3836 && vim_strchr(p_ww, 's') != NULL)
3837 || (cap->cmdchar == 'l'
3838 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003839 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 && vim_strchr(p_ww, '>') != NULL))
3841 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3842 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003843 // When deleting we also count the NL as a character.
3844 // Set cap->oap->inclusive when last char in the line is
3845 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003846 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003848 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 cap->oap->inclusive = TRUE;
3850 else
3851 {
3852 ++curwin->w_cursor.lnum;
3853 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 curwin->w_set_curswant = TRUE;
3856 cap->oap->inclusive = FALSE;
3857 }
3858 continue;
3859 }
3860 if (cap->oap->op_type == OP_NOP)
3861 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003862 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 if (n == cap->count1)
3864 beep_flush();
3865 }
3866 else
3867 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003868 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 cap->oap->inclusive = TRUE;
3870 }
3871 break;
3872 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003873 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 {
3875 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 if (virtual_active())
3877 oneright();
3878 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003881 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 ++curwin->w_cursor.col;
3884 }
3885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 }
3887#ifdef FEAT_FOLDING
3888 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3889 && cap->oap->op_type == OP_NOP)
3890 foldOpenCursor();
3891#endif
3892}
3893
3894/*
3895 * Cursor left commands.
3896 *
3897 * Returns TRUE when operator end should not be adjusted.
3898 */
3899 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003900nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901{
3902 long n;
3903
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003904 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3905 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003906 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003907 if (mod_mask & MOD_MASK_CTRL)
3908 cap->arg = 1;
3909 nv_bck_word(cap);
3910 return;
3911 }
3912
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 cap->oap->motion_type = MCHAR;
3914 cap->oap->inclusive = FALSE;
3915 for (n = cap->count1; n > 0; --n)
3916 {
3917 if (oneleft() == FAIL)
3918 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003919 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3920 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3921 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 if ( (((cap->cmdchar == K_BS
3923 || cap->cmdchar == Ctrl_H)
3924 && vim_strchr(p_ww, 'b') != NULL)
3925 || (cap->cmdchar == 'h'
3926 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003927 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 && vim_strchr(p_ww, '<') != NULL))
3929 && curwin->w_cursor.lnum > 1)
3930 {
3931 --(curwin->w_cursor.lnum);
3932 coladvance((colnr_T)MAXCOL);
3933 curwin->w_set_curswant = TRUE;
3934
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003935 // When the NL before the first char has to be deleted we
3936 // put the cursor on the NUL after the previous line.
3937 // This is a very special case, be careful!
3938 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 if ( (cap->oap->op_type == OP_DELETE
3940 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003941 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003943 char_u *cp = ml_get_cursor();
3944
3945 if (*cp != NUL)
3946 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003947 if (has_mbyte)
3948 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3949 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003950 ++curwin->w_cursor.col;
3951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 cap->retval |= CA_NO_ADJ_OP_END;
3953 }
3954 continue;
3955 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003956 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3958 beep_flush();
3959 break;
3960 }
3961 }
3962#ifdef FEAT_FOLDING
3963 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3964 && cap->oap->op_type == OP_NOP)
3965 foldOpenCursor();
3966#endif
3967}
3968
3969/*
3970 * Cursor up commands.
3971 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3972 */
3973 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003974nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003976 if (mod_mask & MOD_MASK_SHIFT)
3977 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003978 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003979 cap->arg = BACKWARD;
3980 nv_page(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003981 return;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003982 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003983
3984 cap->oap->motion_type = MLINE;
3985 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
3986 clearopbeep(cap->oap);
3987 else if (cap->arg)
3988 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989}
3990
3991/*
3992 * Cursor down commands.
3993 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
3994 */
3995 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02003996nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003998 if (mod_mask & MOD_MASK_SHIFT)
3999 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004000 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004001 cap->arg = FORWARD;
4002 nv_page(cap);
4003 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004004#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004005 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004006 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4007 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004009 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004011 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004012 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 cmdwin_result = CAR;
4014 else
Bram Moolenaarf2732452018-06-03 14:47:35 +02004015#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004016 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004017 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4018 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4019 {
4020 invoke_prompt_callback();
4021 if (restart_edit == 0)
4022 restart_edit = 'a';
4023 }
4024 else
4025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 {
4027 cap->oap->motion_type = MLINE;
4028 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4029 clearopbeep(cap->oap);
4030 else if (cap->arg)
4031 beginline(BL_WHITE | BL_FIX);
4032 }
4033 }
4034}
4035
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036/*
4037 * Grab the file name under the cursor and edit it.
4038 */
4039 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004040nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041{
4042 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004043 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004045 if (check_text_or_curbuf_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 return;
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004047
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004048#ifdef FEAT_PROP_POPUP
4049 if (ERROR_IF_TERM_POPUP_WINDOW)
4050 return;
4051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004053 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054
4055 if (ptr != NULL)
4056 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004057 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004058 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004059 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004061 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004062 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004063 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004064 {
4065 curwin->w_cursor.lnum = lnum;
4066 check_cursor_lnum();
4067 beginline(BL_SOL | BL_FIX);
4068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 vim_free(ptr);
4070 }
4071 else
4072 clearop(cap->oap);
4073}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074
4075/*
4076 * <End> command: to end of current line or last line.
4077 */
4078 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004079nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004081 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004083 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004085 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 }
4087 nv_dollar(cap);
4088}
4089
4090/*
4091 * Handle the "$" command.
4092 */
4093 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004094nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095{
4096 cap->oap->motion_type = MCHAR;
4097 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004098 // In virtual mode when off the edge of a line and an operator
4099 // is pending (whew!) keep the cursor where it is.
4100 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 if (!virtual_active() || gchar_cursor() != NUL
4102 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004103 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 if (cursor_down((long)(cap->count1 - 1),
4105 cap->oap->op_type == OP_NOP) == FAIL)
4106 clearopbeep(cap->oap);
4107#ifdef FEAT_FOLDING
4108 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4109 foldOpenCursor();
4110#endif
4111}
4112
4113/*
4114 * Implementation of '?' and '/' commands.
4115 * If cap->arg is TRUE don't set PC mark.
4116 */
4117 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004118nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119{
4120 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004121 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122
4123 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4124 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004125 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 cap->cmdchar = 'g';
4127 cap->nchar = '?';
4128 nv_operator(cap);
4129 return;
4130 }
4131
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004132 // When using 'incsearch' the cursor may be moved to set a different search
4133 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004134 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135
4136 if (cap->searchbuf == NULL)
4137 {
4138 clearop(oap);
4139 return;
4140 }
4141
Bram Moolenaar46539112015-02-17 15:43:57 +01004142 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004143 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004144 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145}
4146
LemonBoya4399382022-04-09 21:04:08 +01004147
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148/*
4149 * Handle "N" and "n" commands.
4150 * cap->arg is SEARCH_REV for "N", 0 for "n".
4151 */
4152 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004153nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004155 pos_T old = curwin->w_cursor;
4156 int wrapped = FALSE;
4157 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004158
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004159 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004160 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004161 // Avoid getting stuck on the current cursor position, which can
4162 // happen when an offset is given and the cursor is on the last char
4163 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004164 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004165 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004166 cap->count1 -= 1;
4167 }
LemonBoya4399382022-04-09 21:04:08 +01004168
4169#ifdef FEAT_SEARCH_EXTRA
4170 // Redraw the window to refresh the highlighted matches.
4171 if (i > 0 && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004172 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174}
4175
4176/*
4177 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4178 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004179 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004181 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004182normal_search(
4183 cmdarg_T *cap,
4184 int dir,
4185 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004186 int opt, // extra flags for do_search()
4187 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188{
4189 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004190 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004191#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004192 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194
4195 cap->oap->motion_type = MCHAR;
4196 cap->oap->inclusive = FALSE;
4197 cap->oap->use_reg_one = TRUE;
4198 curwin->w_set_curswant = TRUE;
4199
Bram Moolenaara80faa82020-04-12 19:37:17 +02004200 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004201 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004202 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4203 if (wrapped != NULL)
4204 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 if (i == 0)
4206 clearop(cap->oap);
4207 else
4208 {
4209 if (i == 2)
4210 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212#ifdef FEAT_FOLDING
4213 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4214 foldOpenCursor();
4215#endif
4216 }
LemonBoya4399382022-04-09 21:04:08 +01004217#ifdef FEAT_SEARCH_EXTRA
4218 // Redraw the window to refresh the highlighted matches.
4219 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004220 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004223 // "/$" will put the cursor after the end of the line, may need to
4224 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004226 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227}
4228
4229/*
4230 * Character search commands.
4231 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4232 * ',' and FALSE for ';'.
4233 * cap->nchar is NUL for ',' and ';' (repeat the search)
4234 */
4235 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004236nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237{
4238 int t_cmd;
4239
4240 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4241 t_cmd = TRUE;
4242 else
4243 t_cmd = FALSE;
4244
4245 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004248 clearopbeep(cap->oap);
4249 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004251
4252 curwin->w_set_curswant = TRUE;
4253 // Include a Tab for "tx" and for "dfx".
4254 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4255 && (t_cmd || cap->oap->op_type != OP_NOP))
4256 {
4257 colnr_T scol, ecol;
4258
4259 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4260 curwin->w_cursor.coladd = ecol - scol;
4261 }
4262 else
4263 curwin->w_cursor.coladd = 0;
4264 adjust_for_sel(cap);
4265#ifdef FEAT_FOLDING
4266 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4267 foldOpenCursor();
4268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269}
4270
4271/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004272 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4273 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4274 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4275 * "[m" or "]m" search for prev/next start of (Java) method.
4276 * "[M" or "]M" search for prev/next end of (Java) method.
4277 */
4278 static void
4279nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4280{
4281 pos_T new_pos = {0, 0, 0};
4282 pos_T *pos = NULL; // init for GCC
4283 pos_T prev_pos;
4284 long n;
4285 int findc;
4286 int c;
4287
4288 if (cap->nchar == '*')
4289 cap->nchar = '/';
4290 prev_pos.lnum = 0;
4291 if (cap->nchar == 'm' || cap->nchar == 'M')
4292 {
4293 if (cap->cmdchar == '[')
4294 findc = '{';
4295 else
4296 findc = '}';
4297 n = 9999;
4298 }
4299 else
4300 {
4301 findc = cap->nchar;
4302 n = cap->count1;
4303 }
4304 for ( ; n > 0; --n)
4305 {
4306 if ((pos = findmatchlimit(cap->oap, findc,
4307 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4308 {
4309 if (new_pos.lnum == 0) // nothing found
4310 {
4311 if (cap->nchar != 'm' && cap->nchar != 'M')
4312 clearopbeep(cap->oap);
4313 }
4314 else
4315 pos = &new_pos; // use last one found
4316 break;
4317 }
4318 prev_pos = new_pos;
4319 curwin->w_cursor = *pos;
4320 new_pos = *pos;
4321 }
4322 curwin->w_cursor = *old_pos;
4323
4324 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4325 // brought us to the match for "[m" and "]M" when inside a method.
4326 // Try finding the '{' or '}' we want to be at.
4327 // Also repeat for the given count.
4328 if (cap->nchar == 'm' || cap->nchar == 'M')
4329 {
4330 // norm is TRUE for "]M" and "[m"
4331 int norm = ((findc == '{') == (cap->nchar == 'm'));
4332
4333 n = cap->count1;
4334 // found a match: we were inside a method
4335 if (prev_pos.lnum != 0)
4336 {
4337 pos = &prev_pos;
4338 curwin->w_cursor = prev_pos;
4339 if (norm)
4340 --n;
4341 }
4342 else
4343 pos = NULL;
4344 while (n > 0)
4345 {
4346 for (;;)
4347 {
4348 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4349 {
4350 // if not found anything, that's an error
4351 if (pos == NULL)
4352 clearopbeep(cap->oap);
4353 n = 0;
4354 break;
4355 }
4356 c = gchar_cursor();
4357 if (c == '{' || c == '}')
4358 {
4359 // Must have found end/start of class: use it.
4360 // Or found the place to be at.
4361 if ((c == findc && norm) || (n == 1 && !norm))
4362 {
4363 new_pos = curwin->w_cursor;
4364 pos = &new_pos;
4365 n = 0;
4366 }
4367 // if no match found at all, we started outside of the
4368 // class and we're inside now. Just go on.
4369 else if (new_pos.lnum == 0)
4370 {
4371 new_pos = curwin->w_cursor;
4372 pos = &new_pos;
4373 }
4374 // found start/end of other method: go to match
4375 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004376 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4377 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004378 n = 0;
4379 else
4380 curwin->w_cursor = *pos;
4381 break;
4382 }
4383 }
4384 --n;
4385 }
4386 curwin->w_cursor = *old_pos;
4387 if (pos == NULL && new_pos.lnum != 0)
4388 clearopbeep(cap->oap);
4389 }
4390 if (pos != NULL)
4391 {
4392 setpcmark();
4393 curwin->w_cursor = *pos;
4394 curwin->w_set_curswant = TRUE;
4395#ifdef FEAT_FOLDING
4396 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4397 && cap->oap->op_type == OP_NOP)
4398 foldOpenCursor();
4399#endif
4400 }
4401}
4402
4403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 * "[" and "]" commands.
4405 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4406 */
4407 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004408nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004411 pos_T *pos = NULL; // init for GCC
4412 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 int flag;
4414 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415
4416 cap->oap->motion_type = MCHAR;
4417 cap->oap->inclusive = FALSE;
4418 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004419 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004421 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 if (cap->nchar == 'f')
4423 nv_gotofile(cap);
4424 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425
4426#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004427 // Find the occurrence(s) of the identifier or define under cursor
4428 // in current and included files or jump to the first occurrence.
4429 //
4430 // search list jump
4431 // fwd bwd fwd bwd fwd bwd
4432 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4433 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004434 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 {
4436 char_u *ptr;
4437 int len;
4438
4439 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4440 clearop(cap->oap);
4441 else
4442 {
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004443 // Make a copy, if the line was changed it will be freed.
4444 ptr = vim_strnsave(ptr, len);
4445 if (ptr == NULL)
4446 return;
4447
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 find_pattern_in_path(ptr, 0, len, TRUE,
4449 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4450 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4451 cap->count1,
4452 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4453 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4454 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4455 (linenr_T)MAXLNUM);
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004456 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 curwin->w_set_curswant = TRUE;
4458 }
4459 }
4460 else
4461#endif
4462
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004463 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4464 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4465 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4466 // "[m" or "]m" search for prev/next start of (Java) method.
4467 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 if ( (cap->cmdchar == '['
4469 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4470 || (cap->cmdchar == ']'
4471 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004472 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004474 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 else if (cap->nchar == '[' || cap->nchar == ']')
4476 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004477 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 flag = '{';
4479 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004480 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481
4482 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004483 // Imitate strange Vi behaviour: When using "]]" with an operator
4484 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004485 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 (cap->oap->op_type != OP_NOP
4487 && cap->arg == FORWARD && flag == '{')))
4488 clearopbeep(cap->oap);
4489 else
4490 {
4491 if (cap->oap->op_type == OP_NOP)
4492 beginline(BL_WHITE | BL_FIX);
4493#ifdef FEAT_FOLDING
4494 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4495 foldOpenCursor();
4496#endif
4497 }
4498 }
4499
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004500 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 else if (cap->nchar == 'p' || cap->nchar == 'P')
4502 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004503 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 }
4505
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004506 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 else if (cap->nchar == '\'' || cap->nchar == '`')
4508 {
4509 pos = &curwin->w_cursor;
4510 for (n = cap->count1; n > 0; --n)
4511 {
4512 prev_pos = *pos;
4513 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4514 cap->nchar == '\'');
4515 if (pos == NULL)
4516 break;
4517 }
4518 if (pos == NULL)
4519 pos = &prev_pos;
4520 nv_cursormark(cap, cap->nchar == '\'', pos);
4521 }
4522
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004523 // [ or ] followed by a middle mouse click: put selected text with
4524 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004525 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 {
4527 (void)do_mouse(cap->oap, cap->nchar,
4528 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4529 cap->count1, PUT_FIXINDENT);
4530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531
4532#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004533 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 else if (cap->nchar == 'z')
4535 {
4536 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4537 cap->count1) == FAIL)
4538 clearopbeep(cap->oap);
4539 }
4540#endif
4541
4542#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004543 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 else if (cap->nchar == 'c')
4545 {
4546 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4547 cap->count1) == FAIL)
4548 clearopbeep(cap->oap);
4549 }
4550#endif
4551
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004552#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004553 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004554 else if (cap->nchar == 's' || cap->nchar == 'S')
4555 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004556 setpcmark();
4557 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004558 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4559 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004560 {
4561 clearopbeep(cap->oap);
4562 break;
4563 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004564 else
4565 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004566# ifdef FEAT_FOLDING
4567 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4568 foldOpenCursor();
4569# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004570 }
4571#endif
4572
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004573 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 else
4575 clearopbeep(cap->oap);
4576}
4577
4578/*
4579 * Handle Normal mode "%" command.
4580 */
4581 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004582nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583{
4584 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004585#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 linenr_T lnum = curwin->w_cursor.lnum;
4587#endif
4588
4589 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004590 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 {
4592 if (cap->count0 > 100)
4593 clearopbeep(cap->oap);
4594 else
4595 {
4596 cap->oap->motion_type = MLINE;
4597 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004598 // Round up, so 'normal 100%' always jumps at the line line.
4599 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4600 // overflow on 32-bits, so use a formula with less accuracy
4601 // to avoid overflows.
4602 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4604 / 100L * cap->count0;
4605 else
4606 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4607 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004608 if (curwin->w_cursor.lnum < 1)
4609 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4611 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4612 beginline(BL_SOL | BL_FIX);
4613 }
4614 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004615 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
4617 cap->oap->motion_type = MCHAR;
4618 cap->oap->use_reg_one = TRUE;
4619 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4620 clearopbeep(cap->oap);
4621 else
4622 {
4623 setpcmark();
4624 curwin->w_cursor = *pos;
4625 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 }
4629 }
4630#ifdef FEAT_FOLDING
4631 if (cap->oap->op_type == OP_NOP
4632 && lnum != curwin->w_cursor.lnum
4633 && (fdo_flags & FDO_PERCENT)
4634 && KeyTyped)
4635 foldOpenCursor();
4636#endif
4637}
4638
4639/*
4640 * Handle "(" and ")" commands.
4641 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4642 */
4643 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004644nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645{
4646 cap->oap->motion_type = MCHAR;
4647 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004648 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004649 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 curwin->w_set_curswant = TRUE;
4651
4652 if (findsent(cap->arg, cap->count1) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004654 clearopbeep(cap->oap);
4655 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004657
4658 // Don't leave the cursor on the NUL past end of line.
4659 adjust_cursor(cap->oap);
4660 curwin->w_cursor.coladd = 0;
4661#ifdef FEAT_FOLDING
4662 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4663 foldOpenCursor();
4664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665}
4666
4667/*
4668 * "m" command: Mark a position.
4669 */
4670 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004671nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004673 if (checkclearop(cap->oap))
4674 return;
4675
4676 if (setmark(cap->nchar) == FAIL)
4677 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678}
4679
4680/*
4681 * "{" and "}" commands.
4682 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4683 */
4684 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004685nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686{
4687 cap->oap->motion_type = MCHAR;
4688 cap->oap->inclusive = FALSE;
4689 cap->oap->use_reg_one = TRUE;
4690 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004691 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004693 clearopbeep(cap->oap);
4694 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004696
4697 curwin->w_cursor.coladd = 0;
4698#ifdef FEAT_FOLDING
4699 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4700 foldOpenCursor();
4701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702}
4703
4704/*
4705 * "u" command: Undo or make lower case.
4706 */
4707 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004708nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004710 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004712 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 cap->cmdchar = 'g';
4714 cap->nchar = 'u';
4715 nv_operator(cap);
4716 }
4717 else
4718 nv_kundo(cap);
4719}
4720
4721/*
4722 * <Undo> command.
4723 */
4724 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004725nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004727 if (checkclearopq(cap->oap))
4728 return;
4729
Bram Moolenaarf2732452018-06-03 14:47:35 +02004730#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004731 if (bt_prompt(curbuf))
4732 {
4733 clearopbeep(cap->oap);
4734 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004736#endif
4737 u_undo((int)cap->count1);
4738 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739}
4740
4741/*
4742 * Handle the "r" command.
4743 */
4744 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004745nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746{
4747 char_u *ptr;
4748 int had_ctrl_v;
4749 long n;
4750
4751 if (checkclearop(cap->oap))
4752 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004753#ifdef FEAT_JOB_CHANNEL
4754 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4755 {
4756 clearopbeep(cap->oap);
4757 return;
4758 }
4759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004761 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 if (cap->nchar == Ctrl_V)
4763 {
4764 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004765 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004766 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 if (cap->nchar > DEL)
4768 had_ctrl_v = NUL;
4769 }
4770 else
4771 had_ctrl_v = NUL;
4772
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004773 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004774 if (IS_SPECIAL(cap->nchar))
4775 {
4776 clearopbeep(cap->oap);
4777 return;
4778 }
4779
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004780 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 if (VIsual_active)
4782 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004783 if (got_int)
4784 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004785 if (had_ctrl_v)
4786 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004787 // Use a special (negative) number to make a difference between a
4788 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004789 if (cap->nchar == CAR)
4790 cap->nchar = REPLACE_CR_NCHAR;
4791 else if (cap->nchar == NL)
4792 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 nv_operator(cap);
4795 return;
4796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004798 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 if (virtual_active())
4800 {
4801 if (u_save_cursor() == FAIL)
4802 return;
4803 if (gchar_cursor() == NUL)
4804 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004805 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 coladvance_force((colnr_T)(getviscol() + cap->count1));
4807 curwin->w_cursor.col -= cap->count1;
4808 }
4809 else if (gchar_cursor() == TAB)
4810 coladvance_force(getviscol());
4811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004813 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004815 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004816 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 {
4818 clearopbeep(cap->oap);
4819 return;
4820 }
4821
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004822 // Replacing with a TAB is done by edit() when it is complicated because
4823 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4824 // Other characters are done below to avoid problems with things like
4825 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4827 {
4828 stuffnumReadbuff(cap->count1);
4829 stuffcharReadbuff('R');
4830 stuffcharReadbuff('\t');
4831 stuffcharReadbuff(ESC);
4832 return;
4833 }
4834
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004835 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 if (u_save_cursor() == FAIL)
4837 return;
4838
4839 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4840 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004841 // Replace character(s) by a single newline.
4842 // Strange vi behaviour: Only one newline is inserted.
4843 // Delete the characters here.
4844 // Insert the newline with an insert command, takes care of
4845 // autoindent. The insert command depends on being on the last
4846 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004847 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 stuffcharReadbuff('\r');
4849 stuffcharReadbuff(ESC);
4850
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004851 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 invoke_edit(cap, TRUE, 'r', FALSE);
4853 }
4854 else
4855 {
4856 prep_redo(cap->oap->regname, cap->count1,
4857 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4858
4859 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 if (has_mbyte)
4861 {
4862 int old_State = State;
4863
4864 if (cap->ncharC1 != 0)
4865 AppendCharToRedobuff(cap->ncharC1);
4866 if (cap->ncharC2 != 0)
4867 AppendCharToRedobuff(cap->ncharC2);
4868
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004869 // This is slow, but it handles replacing a single-byte with a
4870 // multi-byte and the other way around. Also handles adding
4871 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 for (n = cap->count1; n > 0; --n)
4873 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004874 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004875 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4876 {
4877 int c = ins_copychar(curwin->w_cursor.lnum
4878 + (cap->nchar == Ctrl_Y ? -1 : 1));
4879 if (c != NUL)
4880 ins_char(c);
4881 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004882 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004883 ++curwin->w_cursor.col;
4884 }
4885 else
4886 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 State = old_State;
4888 if (cap->ncharC1 != 0)
4889 ins_char(cap->ncharC1);
4890 if (cap->ncharC2 != 0)
4891 ins_char(cap->ncharC2);
4892 }
4893 }
4894 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004896 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 for (n = cap->count1; n > 0; --n)
4898 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004899 // Get ptr again, because u_save and/or showmatch() will have
4900 // released the line. This may also happen in ins_copychar().
4901 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004902 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4903 {
4904 int c = ins_copychar(curwin->w_cursor.lnum
4905 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004906
4907 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004908 if (c != NUL)
4909 ptr[curwin->w_cursor.col] = c;
4910 }
4911 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004912 {
4913 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004914 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 if (p_sm && msg_silent == 0)
4917 showmatch(cap->nchar);
4918 ++curwin->w_cursor.col;
4919 }
4920#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004921 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004923 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924
Bram Moolenaar009b2592004-10-24 19:18:58 +00004925 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004926 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004928 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 }
4930#endif
4931
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004932 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 changed_bytes(curwin->w_cursor.lnum,
4934 (colnr_T)(curwin->w_cursor.col - cap->count1));
4935 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004936 --curwin->w_cursor.col; // cursor on the last replaced char
4937 // if the character on the left of the current cursor is a multi-byte
4938 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 if (has_mbyte)
4940 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 curbuf->b_op_end = curwin->w_cursor;
4942 curwin->w_set_curswant = TRUE;
4943 set_last_insert(cap->nchar);
4944 }
4945}
4946
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947/*
4948 * 'o': Exchange start and end of Visual area.
4949 * 'O': same, but in block mode exchange left and right corners.
4950 */
4951 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004952v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953{
4954 pos_T old_cursor;
4955 colnr_T left, right;
4956
4957 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4958 {
4959 old_cursor = curwin->w_cursor;
4960 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4961 curwin->w_cursor.lnum = VIsual.lnum;
4962 coladvance(left);
4963 VIsual = curwin->w_cursor;
4964
4965 curwin->w_cursor.lnum = old_cursor.lnum;
4966 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004967 // 'selection "exclusive" and cursor at right-bottom corner: move it
4968 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4970 ++curwin->w_curswant;
4971 coladvance(curwin->w_curswant);
4972 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004974 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 {
4976 curwin->w_cursor.lnum = VIsual.lnum;
4977 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
4978 ++right;
4979 coladvance(right);
4980 VIsual = curwin->w_cursor;
4981
4982 curwin->w_cursor.lnum = old_cursor.lnum;
4983 coladvance(left);
4984 curwin->w_curswant = left;
4985 }
4986 }
4987 else
4988 {
4989 old_cursor = curwin->w_cursor;
4990 curwin->w_cursor = VIsual;
4991 VIsual = old_cursor;
4992 curwin->w_set_curswant = TRUE;
4993 }
4994}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995
4996/*
4997 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
4998 */
4999 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005000nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005002 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 {
5004 cap->cmdchar = 'c';
5005 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005006 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 VIsual_mode = 'V';
5008 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005009 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005011
5012 if (checkclearopq(cap->oap))
5013 return;
5014
5015 if (!curbuf->b_p_ma)
5016 emsg(_(e_cannot_make_changes_modifiable_is_off));
5017 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005019 if (virtual_active())
5020 coladvance(getviscol());
5021 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 }
5023}
5024
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025/*
5026 * "gr".
5027 */
5028 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005029nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 if (VIsual_active)
5032 {
5033 cap->cmdchar = 'r';
5034 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005035 nv_replace(cap); // Do same as "r" in Visual mode for now
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005036 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005038
zeertzjqf86dea82023-03-05 21:15:06 +00005039 if (checkclearopq(cap->oap))
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005040 return;
5041
5042 if (!curbuf->b_p_ma)
5043 emsg(_(e_cannot_make_changes_modifiable_is_off));
5044 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005046 if (cap->extra_char == Ctrl_V) // get another character
5047 cap->extra_char = get_literal(FALSE);
Bram Moolenaard6a4ea32023-02-25 14:24:44 +00005048 if (cap->extra_char < ' ')
5049 // Prefix a control character with CTRL-V to avoid it being used as
5050 // a command.
5051 stuffcharReadbuff(Ctrl_V);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005052 stuffcharReadbuff(cap->extra_char);
5053 stuffcharReadbuff(ESC);
5054 if (virtual_active())
5055 coladvance(getviscol());
5056 invoke_edit(cap, TRUE, 'v', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
5058}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059
5060/*
5061 * Swap case for "~" command, when it does not work like an operator.
5062 */
5063 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005064n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065{
5066 long n;
5067 pos_T startpos;
5068 int did_change = 0;
5069#ifdef FEAT_NETBEANS_INTG
5070 pos_T pos;
5071 char_u *ptr;
5072 int count;
5073#endif
5074
5075 if (checkclearopq(cap->oap))
5076 return;
5077
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005078 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 {
5080 clearopbeep(cap->oap);
5081 return;
5082 }
5083
5084 prep_redo_cmd(cap);
5085
5086 if (u_save_cursor() == FAIL)
5087 return;
5088
5089 startpos = curwin->w_cursor;
5090#ifdef FEAT_NETBEANS_INTG
5091 pos = startpos;
5092#endif
5093 for (n = cap->count1; n > 0; --n)
5094 {
5095 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5096 inc_cursor();
5097 if (gchar_cursor() == NUL)
5098 {
5099 if (vim_strchr(p_ww, '~') != NULL
5100 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5101 {
5102#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005103 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
5105 if (did_change)
5106 {
5107 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005108 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005109 netbeans_removed(curbuf, pos.lnum, pos.col,
5110 (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01005111 // line may have been flushed, get it again
5112 ptr = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005114 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 }
5116 pos.col = 0;
5117 pos.lnum++;
5118 }
5119#endif
5120 ++curwin->w_cursor.lnum;
5121 curwin->w_cursor.col = 0;
5122 if (n > 1)
5123 {
5124 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5125 break;
5126 u_clearline();
5127 }
5128 }
5129 else
5130 break;
5131 }
5132 }
5133#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005134 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 {
5136 ptr = ml_get(pos.lnum);
5137 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005138 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5139 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 }
5141#endif
5142
5143
5144 check_cursor();
5145 curwin->w_set_curswant = TRUE;
5146 if (did_change)
5147 {
5148 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5149 0L);
5150 curbuf->b_op_start = startpos;
5151 curbuf->b_op_end = curwin->w_cursor;
5152 if (curbuf->b_op_end.col > 0)
5153 --curbuf->b_op_end.col;
5154 }
5155}
5156
5157/*
5158 * Move cursor to mark.
5159 */
5160 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005161nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162{
5163 if (check_mark(pos) == FAIL)
5164 clearop(cap->oap);
5165 else
5166 {
5167 if (cap->cmdchar == '\''
5168 || cap->cmdchar == '`'
5169 || cap->cmdchar == '['
5170 || cap->cmdchar == ']')
5171 setpcmark();
5172 curwin->w_cursor = *pos;
5173 if (flag)
5174 beginline(BL_WHITE | BL_FIX);
5175 else
5176 check_cursor();
5177 }
5178 cap->oap->motion_type = flag ? MLINE : MCHAR;
5179 if (cap->cmdchar == '`')
5180 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005181 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 curwin->w_set_curswant = TRUE;
5183}
5184
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185/*
5186 * Handle commands that are operators in Visual mode.
5187 */
5188 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005189v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190{
5191 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5192
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005193 // Uppercase means linewise, except in block mode, then "D" deletes till
5194 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 if (isupper(cap->cmdchar))
5196 {
5197 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005198 {
5199 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5203 curwin->w_curswant = MAXCOL;
5204 }
5205 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5206 nv_operator(cap);
5207}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208
5209/*
5210 * "s" and "S" commands.
5211 */
5212 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005213nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005215#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005216 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005217 if (term_swap_diff() == OK)
5218 return;
5219#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005220#ifdef FEAT_JOB_CHANNEL
5221 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5222 {
5223 clearopbeep(cap->oap);
5224 return;
5225 }
5226#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005227 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 {
5229 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005230 {
5231 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 cap->cmdchar = 'c';
5235 nv_operator(cap);
5236 }
5237 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 nv_optrans(cap);
5239}
5240
5241/*
5242 * Abbreviated commands.
5243 */
5244 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005245nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246{
5247 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005248 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005250 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 if (VIsual_active)
5252 v_visop(cap);
5253 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 nv_optrans(cap);
5255}
5256
5257/*
5258 * Translate a command into another command.
5259 */
5260 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005261nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262{
5263 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5264 (char_u *)"d$", (char_u *)"c$",
5265 (char_u *)"cl", (char_u *)"cc",
5266 (char_u *)"yy", (char_u *)":s\r"};
5267 static char_u *str = (char_u *)"xXDCsSY&";
5268
5269 if (!checkclearopq(cap->oap))
5270 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005271 // In Vi "2D" doesn't delete the next line. Can't translate it
5272 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005273 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5274 {
5275 cap->oap->start = curwin->w_cursor;
5276 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005277#ifdef FEAT_EVAL
5278 set_op_var(OP_DELETE);
5279#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005280 cap->count1 = 1;
5281 nv_dollar(cap);
5282 finish_op = TRUE;
5283 ResetRedobuff();
5284 AppendCharToRedobuff('D');
5285 }
5286 else
5287 {
5288 if (cap->count0)
5289 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005290 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 }
5293 cap->opcount = 0;
5294}
5295
5296/*
5297 * "'" and "`" commands. Also for "g'" and "g`".
5298 * cap->arg is TRUE for "'" and "g'".
5299 */
5300 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005301nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302{
5303 pos_T *pos;
5304 int c;
5305#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005306 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005307 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308#endif
5309
5310 if (cap->cmdchar == 'g')
5311 c = cap->extra_char;
5312 else
5313 c = cap->nchar;
5314 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005315 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 {
5317 if (cap->arg)
5318 {
5319 check_cursor_lnum();
5320 beginline(BL_WHITE | BL_FIX);
5321 }
5322 else
5323 check_cursor();
5324 }
5325 else
5326 nv_cursormark(cap, cap->arg, pos);
5327
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005328 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 if (!virtual_active())
5330 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005331 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332#ifdef FEAT_FOLDING
5333 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005334 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005335 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 && (fdo_flags & FDO_MARK)
5337 && old_KeyTyped)
5338 foldOpenCursor();
5339#endif
5340}
5341
5342/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005343 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 */
5345 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005346nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005349#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005351 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005354 if (checkclearopq(cap->oap))
5355 return;
5356
5357 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005359 if (goto_tabpage_lastused() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 clearopbeep(cap->oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005361 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005363 if (cap->cmdchar == 'g')
5364 pos = movechangelist((int)cap->count1);
5365 else
5366 pos = movemark((int)cap->count1);
5367 if (pos == (pos_T *)-1) // jump to other file
5368 {
5369 curwin->w_set_curswant = TRUE;
5370 check_cursor();
5371 }
5372 else if (pos != NULL) // can jump
5373 nv_cursormark(cap, FALSE, pos);
5374 else if (cap->cmdchar == 'g')
5375 {
5376 if (curbuf->b_changelistlen == 0)
5377 emsg(_(e_changelist_is_empty));
5378 else if (cap->count1 < 0)
5379 emsg(_(e_at_start_of_changelist));
5380 else
5381 emsg(_(e_at_end_of_changelist));
5382 }
5383 else
5384 clearopbeep(cap->oap);
5385# ifdef FEAT_FOLDING
5386 if (cap->oap->op_type == OP_NOP
5387 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5388 && (fdo_flags & FDO_MARK)
5389 && old_KeyTyped)
5390 foldOpenCursor();
5391# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392}
5393
5394/*
5395 * Handle '"' command.
5396 */
5397 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005398nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399{
5400 if (checkclearop(cap->oap))
5401 return;
5402#ifdef FEAT_EVAL
5403 if (cap->nchar == '=')
5404 cap->nchar = get_expr_register();
5405#endif
5406 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5407 {
5408 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005409 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410#ifdef FEAT_EVAL
5411 set_reg_var(cap->oap->regname);
5412#endif
5413 }
5414 else
5415 clearopbeep(cap->oap);
5416}
5417
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418/*
5419 * Handle "v", "V" and "CTRL-V" commands.
5420 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5421 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005422 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 */
5424 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005425nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005427 if (cap->cmdchar == Ctrl_Q)
5428 cap->cmdchar = Ctrl_V;
5429
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005430 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5431 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 if (cap->oap->op_type != OP_NOP)
5433 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005434 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005435 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 return;
5437 }
5438
5439 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005440 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005442 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005444 else // toggle char/block mode
5445 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 VIsual_mode = cap->cmdchar;
5447 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005448 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005450 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005452 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 {
5454 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005455 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005457 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 VIsual = curwin->w_cursor;
5459
5460 VIsual_active = TRUE;
5461 VIsual_reselect = TRUE;
5462 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005463 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005466 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005467 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005468 // For V and ^V, we multiply the number of lines even if there
5469 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5471 {
5472 curwin->w_cursor.lnum +=
5473 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005474 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 }
5476 VIsual_mode = resel_VIsual_mode;
5477 if (VIsual_mode == 'v')
5478 {
5479 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005480 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005481 update_curswant_force();
Bram Moolenaar79c11e32023-01-10 17:29:29 +00005482 curwin->w_curswant += resel_VIsual_vcol * cap->count0;
5483 if (*p_sel != 'e')
5484 --curwin->w_curswant;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005487 curwin->w_curswant = resel_VIsual_vcol;
5488 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005490 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 {
5492 curwin->w_curswant = MAXCOL;
5493 coladvance((colnr_T)MAXCOL);
5494 }
5495 else if (VIsual_mode == Ctrl_V)
5496 {
Bram Moolenaar8f531662023-02-01 17:33:18 +00005497 // Update curswant on the original line, that is where "col" is
5498 // valid.
5499 linenr_T lnum = curwin->w_cursor.lnum;
5500 curwin->w_cursor.lnum = VIsual.lnum;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005501 update_curswant_force();
Bram Moolenaar8f531662023-02-01 17:33:18 +00005502 curwin->w_curswant += resel_VIsual_vcol * cap->count0 - 1;
5503 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 coladvance(curwin->w_curswant);
5505 }
5506 else
5507 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005508 redraw_curbuf_later(UPD_INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 }
5510 else
5511 {
5512 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005513 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 may_start_select('c');
5515 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005516 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005517 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005518 if (cap->count0 > 0 && --cap->count1 > 0)
5519 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005520 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005521 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5522 nv_right(cap);
5523 else if (VIsual_mode == 'V')
5524 nv_down(cap);
5525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 }
5527 }
5528}
5529
5530/*
5531 * Start selection for Shift-movement keys.
5532 */
5533 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005534start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005536 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 may_start_select('k');
5538 n_start_visual_mode('v');
5539}
5540
5541/*
5542 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005543 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 */
5545 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005546may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005548 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5549 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550}
5551
5552/*
5553 * Start Visual mode "c".
5554 * Should set VIsual_select before calling this.
5555 */
5556 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005557n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005559#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005560 int cursor_line_was_concealed = curwin->w_p_cole > 0
5561 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005562#endif
5563
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 VIsual_mode = c;
5565 VIsual_active = TRUE;
5566 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005567
5568 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005569 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005570 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005571 {
5572 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 VIsual = curwin->w_cursor;
5576
5577#ifdef FEAT_FOLDING
5578 foldAdjustVisual();
5579#endif
5580
LemonBoy2bf52dd2022-04-09 18:17:34 +01005581 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005583#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005584 // Check if redraw is needed after changing the state.
5585 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005586#endif
5587
Bram Moolenaar09df3122006-01-23 22:23:09 +00005588 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005589 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005591 // Make sure the clipboard gets updated. Needed because start and
5592 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 clip_star.vmode = NUL;
5594#endif
5595
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005596 // Only need to redraw this line, unless still need to redraw an old
5597 // Visual area (when 'lazyredraw' is set).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005598 if (curwin->w_redr_type < UPD_INVERTED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 {
5600 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5601 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5602 }
5603}
5604
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605
5606/*
5607 * CTRL-W: Window commands
5608 */
5609 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005610nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005612 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005613 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005614 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005615 cap->cmdchar = ':';
5616 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005617 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005618 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005619 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005620 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621}
5622
5623/*
5624 * CTRL-Z: Suspend
5625 */
5626 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005627nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628{
5629 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005631 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005632 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633}
5634
5635/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005636 * "gv": Reselect the previous Visual area. If Visual already active,
5637 * exchange previous and current Visual area.
5638 */
5639 static void
5640nv_gv_cmd(cmdarg_T *cap)
5641{
5642 pos_T tpos;
5643 int i;
5644
5645 if (checkclearop(cap->oap))
5646 return;
5647
5648 if (curbuf->b_visual.vi_start.lnum == 0
5649 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5650 || curbuf->b_visual.vi_end.lnum == 0)
5651 {
5652 beep_flush();
5653 return;
5654 }
5655
5656 // set w_cursor to the start of the Visual area, tpos to the end
5657 if (VIsual_active)
5658 {
5659 i = VIsual_mode;
5660 VIsual_mode = curbuf->b_visual.vi_mode;
5661 curbuf->b_visual.vi_mode = i;
5662# ifdef FEAT_EVAL
5663 curbuf->b_visual_mode_eval = i;
5664# endif
5665 i = curwin->w_curswant;
5666 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5667 curbuf->b_visual.vi_curswant = i;
5668
5669 tpos = curbuf->b_visual.vi_end;
5670 curbuf->b_visual.vi_end = curwin->w_cursor;
5671 curwin->w_cursor = curbuf->b_visual.vi_start;
5672 curbuf->b_visual.vi_start = VIsual;
5673 }
5674 else
5675 {
5676 VIsual_mode = curbuf->b_visual.vi_mode;
5677 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5678 tpos = curbuf->b_visual.vi_end;
5679 curwin->w_cursor = curbuf->b_visual.vi_start;
5680 }
5681
5682 VIsual_active = TRUE;
5683 VIsual_reselect = TRUE;
5684
5685 // Set Visual to the start and w_cursor to the end of the Visual
5686 // area. Make sure they are on an existing character.
5687 check_cursor();
5688 VIsual = curwin->w_cursor;
5689 curwin->w_cursor = tpos;
5690 check_cursor();
5691 update_topline();
5692
5693 // When called from normal "g" command: start Select mode when
5694 // 'selectmode' contains "cmd". When called for K_SELECT, always
5695 // start Select mode.
5696 if (cap->arg)
5697 {
5698 VIsual_select = TRUE;
5699 VIsual_select_reg = 0;
5700 }
5701 else
5702 may_start_select('c');
5703 setmouse();
5704#ifdef FEAT_CLIPBOARD
5705 // Make sure the clipboard gets updated. Needed because start and
5706 // end are still the same, and the selection needs to be owned
5707 clip_star.vmode = NUL;
5708#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005709 redraw_curbuf_later(UPD_INVERTED);
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005710 showmode();
5711}
5712
5713/*
5714 * "g0", "g^" : Like "0" and "^" but for screen lines.
5715 * "gm": middle of "g0" and "g$".
5716 */
5717 static void
5718nv_g_home_m_cmd(cmdarg_T *cap)
5719{
5720 int i;
5721 int flag = FALSE;
5722
5723 if (cap->nchar == '^')
5724 flag = TRUE;
5725
5726 cap->oap->motion_type = MCHAR;
5727 cap->oap->inclusive = FALSE;
5728 if (curwin->w_p_wrap && curwin->w_width != 0)
5729 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005730 int width1 = curwin->w_width - curwin_col_off();
5731 int width2 = width1 + curwin_col_off2();
5732 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005733
5734 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005735 virtcol = curwin->w_virtcol
5736#ifdef FEAT_PROP_POPUP
5737 - curwin->w_virtcol_first_char
5738#endif
5739 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005740 i = 0;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005741 if (virtcol >= (colnr_T)width1 && width2 > 0)
5742 i = (virtcol - width1) / width2 * width2 + width1;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005743 }
5744 else
5745 i = curwin->w_leftcol;
5746 // Go to the middle of the screen line. When 'number' or
5747 // 'relativenumber' is on and lines are wrapping the middle can be more
5748 // to the left.
5749 if (cap->nchar == 'm')
5750 i += (curwin->w_width - curwin_col_off()
5751 + ((curwin->w_p_wrap && i > 0)
5752 ? curwin_col_off2() : 0)) / 2;
5753 coladvance((colnr_T)i);
5754 if (flag)
5755 {
5756 do
5757 i = gchar_cursor();
5758 while (VIM_ISWHITE(i) && oneright() == OK);
5759 curwin->w_valid &= ~VALID_WCOL;
5760 }
5761 curwin->w_set_curswant = TRUE;
5762}
5763
5764/*
5765 * "g_": to the last non-blank character in the line or <count> lines
5766 * downward.
5767 */
5768 static void
5769nv_g_underscore_cmd(cmdarg_T *cap)
5770{
5771 char_u *ptr;
5772
5773 cap->oap->motion_type = MCHAR;
5774 cap->oap->inclusive = TRUE;
5775 curwin->w_curswant = MAXCOL;
5776 if (cursor_down((long)(cap->count1 - 1),
5777 cap->oap->op_type == OP_NOP) == FAIL)
5778 {
5779 clearopbeep(cap->oap);
5780 return;
5781 }
5782
5783 ptr = ml_get_curline();
5784
5785 // In Visual mode we may end up after the line.
5786 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5787 --curwin->w_cursor.col;
5788
5789 // Decrease the cursor column until it's on a non-blank.
5790 while (curwin->w_cursor.col > 0
5791 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5792 --curwin->w_cursor.col;
5793 curwin->w_set_curswant = TRUE;
5794 adjust_for_sel(cap);
5795}
5796
5797/*
5798 * "g$" : Like "$" but for screen lines.
5799 */
5800 static void
5801nv_g_dollar_cmd(cmdarg_T *cap)
5802{
5803 oparg_T *oap = cap->oap;
5804 int i;
5805 int col_off = curwin_col_off();
5806
5807 oap->motion_type = MCHAR;
5808 oap->inclusive = TRUE;
5809 if (curwin->w_p_wrap && curwin->w_width != 0)
5810 {
5811 curwin->w_curswant = MAXCOL; // so we stay at the end
5812 if (cap->count1 == 1)
5813 {
5814 int width1 = curwin->w_width - col_off;
5815 int width2 = width1 + curwin_col_off2();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005816 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005817
5818 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005819 virtcol = curwin->w_virtcol
5820#ifdef FEAT_PROP_POPUP
5821 - curwin->w_virtcol_first_char
5822#endif
5823 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005824 i = width1 - 1;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005825 if (virtcol >= (colnr_T)width1)
5826 i += ((virtcol - width1) / width2 + 1)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005827 * width2;
5828 coladvance((colnr_T)i);
5829
5830 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005831 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005832 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5833 {
5834 // Check for landing on a character that got split at
5835 // the end of the line. We do not want to advance to
5836 // the next screen line.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005837 if (curwin->w_virtcol
5838#ifdef FEAT_PROP_POPUP
5839 - curwin->w_virtcol_first_char
5840#endif
5841 > (colnr_T)i)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005842 --curwin->w_cursor.col;
5843 }
5844 }
5845 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5846 clearopbeep(oap);
5847 }
5848 else
5849 {
5850 if (cap->count1 > 1)
5851 // if it fails, let the cursor still move to the last char
5852 (void)cursor_down(cap->count1 - 1, FALSE);
5853
5854 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5855 coladvance((colnr_T)i);
5856
5857 // if the character doesn't fit move one back
5858 if (curwin->w_cursor.col > 0
5859 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5860 {
5861 colnr_T vcol;
5862
5863 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5864 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5865 --curwin->w_cursor.col;
5866 }
5867
5868 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005869 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005870 }
5871}
5872
5873/*
5874 * "gi": start Insert at the last position.
5875 */
5876 static void
5877nv_gi_cmd(cmdarg_T *cap)
5878{
5879 int i;
5880
5881 if (curbuf->b_last_insert.lnum != 0)
5882 {
5883 curwin->w_cursor = curbuf->b_last_insert;
5884 check_cursor_lnum();
5885 i = (int)STRLEN(ml_get_curline());
5886 if (curwin->w_cursor.col > (colnr_T)i)
5887 {
5888 if (virtual_active())
5889 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5890 curwin->w_cursor.col = i;
5891 }
5892 }
5893 cap->cmdchar = 'i';
5894 nv_edit(cap);
5895}
5896
5897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 * Commands starting with "g".
5899 */
5900 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005901nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902{
5903 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905
5906 switch (cap->nchar)
5907 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005908 case Ctrl_A:
5909 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005911 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005912 if (!VIsual_active && cap->nchar == Ctrl_A)
5913 vim_mem_profile_dump();
5914 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005916 // "g^A/g^X": sequentially increment visually selected region
zeertzjqf86dea82023-03-05 21:15:06 +00005917 if (VIsual_active)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005918 {
5919 cap->arg = TRUE;
5920 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005921 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005922 nv_addsub(cap);
5923 }
5924 else
5925 clearopbeep(oap);
5926 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005928 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 case 'R':
5930 cap->arg = TRUE;
5931 nv_Replace(cap);
5932 break;
5933
5934 case 'r':
5935 nv_vreplace(cap);
5936 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937
5938 case '&':
5939 do_cmdline_cmd((char_u *)"%s//~/&");
5940 break;
5941
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005942 // "gv": Reselect the previous Visual area. If Visual already active,
5943 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005945 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005947
5948 // "gV": Don't reselect the previous Visual area after a Select mode
5949 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 case 'V':
5951 VIsual_reselect = FALSE;
5952 break;
5953
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005954 // "gh": start Select mode.
5955 // "gH": start Select line mode.
5956 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 case K_BS:
5958 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005959 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 case 'h':
5961 case 'H':
5962 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 cap->cmdchar = cap->nchar + ('v' - 'h');
5964 cap->arg = TRUE;
5965 nv_visual(cap);
5966 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005967
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005968 // "gn", "gN" visually select next/previous search match
5969 // "gn" selects next match
5970 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005971 case 'N':
5972 case 'n':
5973 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005974 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005975 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005977 // "gj" and "gk" two new funny movement keys -- up and down
5978 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 case 'j':
5980 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005981 // with 'nowrap' it works just like the normal "j" command.
5982 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 {
5984 oap->motion_type = MLINE;
5985 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5986 }
5987 else
5988 i = nv_screengo(oap, FORWARD, cap->count1);
5989 if (i == FAIL)
5990 clearopbeep(oap);
5991 break;
5992
5993 case 'k':
5994 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005995 // with 'nowrap' it works just like the normal "k" command.
5996 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 {
5998 oap->motion_type = MLINE;
5999 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
6000 }
6001 else
6002 i = nv_screengo(oap, BACKWARD, cap->count1);
6003 if (i == FAIL)
6004 clearopbeep(oap);
6005 break;
6006
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006007 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 case 'J':
6009 nv_join(cap);
6010 break;
6011
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006012 // "g0", "g^" : Like "0" and "^" but for screen lines.
6013 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 case '0':
6016 case 'm':
6017 case K_HOME:
6018 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006019 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 break;
6021
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006022 case 'M':
6023 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006024 oap->motion_type = MCHAR;
6025 oap->inclusive = FALSE;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01006026 i = linetabsize_str(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006027 if (cap->count0 > 0 && cap->count0 <= 100)
6028 coladvance((colnr_T)(i * cap->count0 / 100));
6029 else
6030 coladvance((colnr_T)(i / 2));
6031 curwin->w_set_curswant = TRUE;
6032 }
6033 break;
6034
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006035 // "g_": to the last non-blank character in the line or <count> lines
6036 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006038 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 break;
6040
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006041 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 case '$':
6043 case K_END:
6044 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006045 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 break;
6047
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006048 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 case '*':
6050 case '#':
6051#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006052 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006054 case Ctrl_RSB: // :tag or :tselect for current identifier
6055 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 nv_ident(cap);
6057 break;
6058
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006059 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 case 'e':
6061 case 'E':
6062 oap->motion_type = MCHAR;
6063 curwin->w_set_curswant = TRUE;
6064 oap->inclusive = TRUE;
6065 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6066 clearopbeep(oap);
6067 break;
6068
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006069 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006071 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 break;
6073
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006074 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006076 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 break;
6078
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006079 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 case 'I':
6081 beginline(0);
6082 if (!checkclearopq(oap))
6083 invoke_edit(cap, FALSE, 'g', FALSE);
6084 break;
6085
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006086 // "gf": goto file, edit file under cursor
6087 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006089 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 nv_gotofile(cap);
6091 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006093 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 case '\'':
6095 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006096 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 case '`':
6098 nv_gomark(cap);
6099 break;
6100
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006101 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006103 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 break;
6105
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006106 // "ga": Display the ascii value of the character under the
6107 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 case 'a':
6109 do_ascii(NULL);
6110 break;
6111
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006112 // "g8": Display the bytes used for the UTF-8 character under the
6113 // cursor. It is displayed in hex.
6114 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006116 if (cap->count0 == 8)
6117 utf_find_illegal();
6118 else
6119 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006122 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006123 case '<':
6124 show_sb_text();
6125 break;
6126
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006127 // "gg": Goto the first line in file. With a count it goes to
6128 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 case 'g':
6130 cap->arg = FALSE;
6131 nv_goto(cap);
6132 break;
6133
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006134 // Two-character operators:
6135 // "gq" Format text
6136 // "gw" Format text and keep cursor position
6137 // "g~" Toggle the case of the text.
6138 // "gu" Change text to lower case.
6139 // "gU" Change text to upper case.
6140 // "g?" rot13 encoding
6141 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 case 'q':
6143 case 'w':
6144 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006145 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 case '~':
6147 case 'u':
6148 case 'U':
6149 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006150 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 nv_operator(cap);
6152 break;
6153
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006154 // "gd": Find first occurrence of pattern under the cursor in the
6155 // current function
6156 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 case 'd':
6158 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006159 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 break;
6161
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006162 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 case K_MIDDLEMOUSE:
6164 case K_MIDDLEDRAG:
6165 case K_MIDDLERELEASE:
6166 case K_LEFTMOUSE:
6167 case K_LEFTDRAG:
6168 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006169 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 case K_RIGHTMOUSE:
6171 case K_RIGHTDRAG:
6172 case K_RIGHTRELEASE:
6173 case K_X1MOUSE:
6174 case K_X1DRAG:
6175 case K_X1RELEASE:
6176 case K_X2MOUSE:
6177 case K_X2DRAG:
6178 case K_X2RELEASE:
6179 mod_mask = MOD_MASK_CTRL;
6180 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6181 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182
6183 case K_IGNORE:
6184 break;
6185
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006186 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 case 'p':
6188 case 'P':
6189 nv_put(cap);
6190 break;
6191
6192#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006193 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 case 'o':
6195 goto_byte(cap->count0);
6196 break;
6197#endif
6198
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006199 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006201 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 do_exmode(TRUE);
6203 break;
6204
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 case ',':
6206 nv_pcmark(cap);
6207 break;
6208
6209 case ';':
6210 cap->count1 = -cap->count1;
6211 nv_pcmark(cap);
6212 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006214 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006215 if (!checkclearop(oap))
6216 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006217 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006218 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006219 if (!checkclearop(oap))
6220 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006221 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006222
Bram Moolenaar62a23252020-08-09 14:04:42 +02006223 case TAB:
6224 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6225 clearopbeep(oap);
6226 break;
6227
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006228 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006229 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006230 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006231 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006232 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006233 break;
6234
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 default:
6236 clearopbeep(oap);
6237 break;
6238 }
6239}
6240
6241/*
6242 * Handle "o" and "O" commands.
6243 */
6244 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006245n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006247#ifdef FEAT_CONCEAL
6248 linenr_T oldline = curwin->w_cursor.lnum;
6249#endif
6250
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006251 if (checkclearopq(cap->oap))
6252 return;
6253
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254#ifdef FEAT_FOLDING
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006255 if (cap->cmdchar == 'O')
6256 // Open above the first line of a folded sequence of lines
6257 (void)hasFolding(curwin->w_cursor.lnum,
6258 &curwin->w_cursor.lnum, NULL);
6259 else
6260 // Open below the last line of a folded sequence of lines
6261 (void)hasFolding(curwin->w_cursor.lnum,
6262 NULL, &curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006264 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6265 (cap->cmdchar == 'O' ? 1 : 0)),
6266 (linenr_T)(curwin->w_cursor.lnum +
6267 (cap->cmdchar == 'o' ? 1 : 0))
6268 ) == OK
6269 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
6270 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6271 0, NULL) == OK)
6272 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006273#ifdef FEAT_CONCEAL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006274 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
6275 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006276#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006277#ifdef FEAT_SYN_HL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006278 if (curwin->w_p_cul)
6279 // force redraw of cursorline
6280 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006281#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006282 // When '#' is in 'cpoptions' ignore the count.
6283 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6284 cap->count1 = 1;
6285 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 }
6287}
6288
6289/*
6290 * "." command: redo last change.
6291 */
6292 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006293nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006295 if (checkclearopq(cap->oap))
6296 return;
6297
6298 // If "restart_edit" is TRUE, the last but one command is repeated
6299 // instead of the last command (inserting text). This is used for
6300 // CTRL-O <.> in insert mode.
6301 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6302 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303}
6304
6305/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006306 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 */
6308 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006309nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006311 if (VIsual_select && VIsual_active)
6312 {
6313 int reg;
6314 // Get register name
6315 ++no_mapping;
6316 ++allow_keys;
6317 reg = plain_vgetc();
6318 LANGMAP_ADJUST(reg, TRUE);
6319 --no_mapping;
6320 --allow_keys;
6321
6322 if (reg == '"')
6323 // the unnamed register is 0
6324 reg = 0;
6325
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006326 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006327 return;
6328 }
6329
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006330 if (checkclearopq(cap->oap))
6331 return;
6332
6333 u_redo((int)cap->count1);
6334 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335}
6336
6337/*
6338 * Handle "U" command.
6339 */
6340 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006341nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006343 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006344 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006346 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 cap->cmdchar = 'g';
6348 cap->nchar = 'U';
6349 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006350 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006352
6353 if (checkclearopq(cap->oap))
6354 return;
6355
6356 u_undoline();
6357 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358}
6359
6360/*
6361 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6362 * single character.
6363 */
6364 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006365nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006367 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006368 {
6369#ifdef FEAT_JOB_CHANNEL
6370 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6371 {
6372 clearopbeep(cap->oap);
6373 return;
6374 }
6375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 else
6379 nv_operator(cap);
6380}
6381
6382/*
6383 * Handle an operator command.
6384 * The actual work is done by do_pending_operator().
6385 */
6386 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006387nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388{
6389 int op_type;
6390
6391 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006392#ifdef FEAT_JOB_CHANNEL
6393 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6394 {
6395 clearopbeep(cap->oap);
6396 return;
6397 }
6398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006400 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 nv_lineop(cap);
6402 else if (!checkclearop(cap->oap))
6403 {
6404 cap->oap->start = curwin->w_cursor;
6405 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006406#ifdef FEAT_EVAL
6407 set_op_var(op_type);
6408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 }
6410}
6411
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006412#ifdef FEAT_EVAL
6413/*
6414 * Set v:operator to the characters for "optype".
6415 */
6416 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006417set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006418{
6419 char_u opchars[3];
6420
6421 if (optype == OP_NOP)
6422 set_vim_var_string(VV_OP, NULL, 0);
6423 else
6424 {
6425 opchars[0] = get_op_char(optype);
6426 opchars[1] = get_extra_op_char(optype);
6427 opchars[2] = NUL;
6428 set_vim_var_string(VV_OP, opchars, -1);
6429 }
6430}
6431#endif
6432
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433/*
6434 * Handle linewise operator "dd", "yy", etc.
6435 *
6436 * "_" is is a strange motion command that helps make operators more logical.
6437 * It is actually implemented, but not documented in the real Vi. This motion
6438 * command actually refers to "the current line". Commands like "dd" and "yy"
6439 * are really an alternate form of "d_" and "y_". It does accept a count, so
6440 * "d3_" works to delete 3 lines.
6441 */
6442 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006443nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444{
6445 cap->oap->motion_type = MLINE;
6446 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6447 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006448 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006449 && cap->oap->motion_force != 'v'
6450 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 || cap->oap->op_type == OP_LSHIFT
6452 || cap->oap->op_type == OP_RSHIFT)
6453 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006454 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 beginline(BL_WHITE | BL_FIX);
6456}
6457
6458/*
6459 * <Home> command.
6460 */
6461 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006462nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006464 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006465 if (mod_mask & MOD_MASK_CTRL)
6466 nv_goto(cap);
6467 else
6468 {
6469 cap->count0 = 1;
6470 nv_pipe(cap);
6471 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006472 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6473 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474}
6475
6476/*
6477 * "|" command.
6478 */
6479 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006480nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481{
6482 cap->oap->motion_type = MCHAR;
6483 cap->oap->inclusive = FALSE;
6484 beginline(0);
6485 if (cap->count0 > 0)
6486 {
6487 coladvance((colnr_T)(cap->count0 - 1));
6488 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6489 }
6490 else
6491 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006492 // keep curswant at the column where we wanted to go, not where
6493 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 curwin->w_set_curswant = FALSE;
6495}
6496
6497/*
6498 * Handle back-word command "b" and "B".
6499 * cap->arg is 1 for "B"
6500 */
6501 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006502nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503{
6504 cap->oap->motion_type = MCHAR;
6505 cap->oap->inclusive = FALSE;
6506 curwin->w_set_curswant = TRUE;
6507 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6508 clearopbeep(cap->oap);
6509#ifdef FEAT_FOLDING
6510 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6511 foldOpenCursor();
6512#endif
6513}
6514
6515/*
6516 * Handle word motion commands "e", "E", "w" and "W".
6517 * cap->arg is TRUE for "E" and "W".
6518 */
6519 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006520nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521{
6522 int n;
6523 int word_end;
6524 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006525 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006527 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6529 word_end = TRUE;
6530 else
6531 word_end = FALSE;
6532 cap->oap->inclusive = word_end;
6533
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006534 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 if (!word_end && cap->oap->op_type == OP_CHANGE)
6536 {
6537 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006538 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006540 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006542 // Reproduce a funny Vi behaviour: "cw" on a blank only
6543 // changes one character, not all blanks until the start of
6544 // the next word. Only do this when the 'w' flag is included
6545 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6547 {
6548 cap->oap->inclusive = TRUE;
6549 cap->oap->motion_type = MCHAR;
6550 return;
6551 }
6552 }
6553 else
6554 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006555 // This is a little strange. To match what the real Vi does,
6556 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6557 // that we are not on a space or a TAB. This seems impolite
6558 // at first, but it's really more what we mean when we say
6559 // 'cw'.
6560 // Another strangeness: When standing on the end of a word
6561 // "ce" will change until the end of the next word, but "cw"
6562 // will change only one character! This is done by setting
6563 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 cap->oap->inclusive = TRUE;
6565 word_end = TRUE;
6566 flag = TRUE;
6567 }
6568 }
6569 }
6570
6571 cap->oap->motion_type = MCHAR;
6572 curwin->w_set_curswant = TRUE;
6573 if (word_end)
6574 n = end_word(cap->count1, cap->arg, flag, FALSE);
6575 else
6576 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6577
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006578 // Don't leave the cursor on the NUL past the end of line. Unless we
6579 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006580 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006581 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582
6583 if (n == FAIL && cap->oap->op_type == OP_NOP)
6584 clearopbeep(cap->oap);
6585 else
6586 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588#ifdef FEAT_FOLDING
6589 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6590 foldOpenCursor();
6591#endif
6592 }
6593}
6594
6595/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006596 * Used after a movement command: If the cursor ends up on the NUL after the
6597 * end of the line, may move it back to the last character and make the motion
6598 * inclusive.
6599 */
6600 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006601adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006602{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006603 // The cursor cannot remain on the NUL when:
6604 // - the column is > 0
6605 // - not in Visual mode or 'selection' is "o"
6606 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006607 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006608 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006609 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006610 {
6611 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006612 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006613 if (has_mbyte)
6614 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006615 oap->inclusive = TRUE;
6616 }
6617}
6618
6619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 * "0" and "^" commands.
6621 * cap->arg is the argument for beginline().
6622 */
6623 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006624nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625{
6626 cap->oap->motion_type = MCHAR;
6627 cap->oap->inclusive = FALSE;
6628 beginline(cap->arg);
6629#ifdef FEAT_FOLDING
6630 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6631 foldOpenCursor();
6632#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006633 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6634 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635}
6636
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637/*
6638 * In exclusive Visual mode, may include the last character.
6639 */
6640 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006641adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642{
6643 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006644 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 if (has_mbyte)
6647 inc_cursor();
6648 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 ++curwin->w_cursor.col;
6650 cap->oap->inclusive = FALSE;
6651 }
6652}
6653
6654/*
6655 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6656 * Should check VIsual_mode before calling this.
6657 * Returns TRUE when backed up to the previous line.
6658 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006659 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006660unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661{
6662 pos_T *pp;
6663
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006664 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006666 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 pp = &curwin->w_cursor;
6668 else
6669 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 if (pp->coladd > 0)
6671 --pp->coladd;
6672 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 if (pp->col > 0)
6674 {
6675 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006676 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 }
6678 else if (pp->lnum > 1)
6679 {
6680 --pp->lnum;
6681 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6682 return TRUE;
6683 }
6684 }
6685 return FALSE;
6686}
6687
6688/*
6689 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6690 */
6691 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006692nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693{
6694 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006695 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006697 VIsual_select_reg = 0;
6698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 else if (VIsual_reselect)
6700 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006701 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 cap->arg = TRUE;
6703 nv_g_cmd(cap);
6704 }
6705}
6706
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707
6708/*
6709 * "G", "gg", CTRL-END, CTRL-HOME.
6710 * cap->arg is TRUE for "G".
6711 */
6712 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006713nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714{
6715 linenr_T lnum;
6716
6717 if (cap->arg)
6718 lnum = curbuf->b_ml.ml_line_count;
6719 else
6720 lnum = 1L;
6721 cap->oap->motion_type = MLINE;
6722 setpcmark();
6723
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006724 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 if (cap->count0 != 0)
6726 lnum = cap->count0;
6727 if (lnum < 1L)
6728 lnum = 1L;
6729 else if (lnum > curbuf->b_ml.ml_line_count)
6730 lnum = curbuf->b_ml.ml_line_count;
6731 curwin->w_cursor.lnum = lnum;
6732 beginline(BL_SOL | BL_FIX);
6733#ifdef FEAT_FOLDING
6734 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6735 foldOpenCursor();
6736#endif
6737}
6738
6739/*
6740 * CTRL-\ in Normal mode.
6741 */
6742 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006743nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744{
6745 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6746 {
6747 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006748 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006749 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 if (cmdwin_type != 0)
6752 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 if (VIsual_active)
6754 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006755 end_visual_mode(); // stop Visual
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006756 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006758 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 if (cap->nchar == Ctrl_G && p_im)
6760 restart_edit = 'a';
6761 }
6762 else
6763 clearopbeep(cap->oap);
6764}
6765
6766/*
6767 * ESC in Normal mode: beep, but don't flush buffers.
6768 * Don't even beep if we are canceling a command.
6769 */
6770 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006771nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772{
6773 int no_reason;
6774
6775 no_reason = (cap->oap->op_type == OP_NOP
6776 && cap->opcount == 0
6777 && cap->count0 == 0
6778 && cap->oap->regname == 0
6779 && !p_im);
6780
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006781 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006783 if (restart_edit == 0 && cmdwin_type == 0
6784 && !VIsual_active && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006785 {
Bram Moolenaarf7e73022022-09-24 13:10:04 +01006786 int out_redir = !stdout_isatty && !is_not_a_term_or_gui();
6787
Bram Moolenaar5939c352022-09-24 12:50:45 +01006788 // The user may accidentally do "vim file | grep word" and then
6789 // CTRL-C doesn't show anything. With a changed buffer give the
6790 // message on stderr. Without any changes might as well exit.
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006791 if (anyBufIsChanged())
Bram Moolenaar5939c352022-09-24 12:50:45 +01006792 {
6793 char *ms = _("Type :qa! and press <Enter> to abandon all changes and exit Vim");
6794
6795 if (out_redir)
6796 mch_errmsg(ms);
6797 else
6798 msg(ms);
6799 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006800 else
Bram Moolenaar5939c352022-09-24 12:50:45 +01006801 {
6802 if (out_redir)
6803 {
6804 got_int = FALSE;
6805 do_cmdline_cmd((char_u *)"qa");
6806 }
6807 else
6808 msg(_("Type :qa and press <Enter> to exit Vim"));
6809 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01006812 if (restart_edit != 0)
6813 redraw_mode = TRUE; // remove "-- (insert) --"
6814
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006815 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6816 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 if (!p_im)
6818 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 if (cmdwin_type != 0)
6820 {
6821 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006822 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 return;
6824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 }
Bram Moolenaar8d696372022-08-21 10:40:07 +01006826 else if (cmdwin_type != 0 && ex_normal_busy && typebuf_was_empty)
Bram Moolenaar7d414102021-02-23 19:39:20 +01006827 {
6828 // When :normal runs out of characters while in the command line window
Bram Moolenaar8d696372022-08-21 10:40:07 +01006829 // vgetorpeek() will repeatedly return ESC. Exit the cmdline window to
6830 // break the loop.
Bram Moolenaar7d414102021-02-23 19:39:20 +01006831 cmdwin_result = K_IGNORE;
6832 return;
6833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 if (VIsual_active)
6836 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006837 end_visual_mode(); // stop Visual
6838 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006840 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006842 else if (no_reason)
Bram Moolenaarb849c822022-08-28 22:46:21 +01006843 {
6844#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar98d10692022-08-29 00:08:39 +01006845 if (!cap->arg && popup_message_win_visible())
Bram Moolenaarb849c822022-08-28 22:46:21 +01006846 popup_hide_message_win();
6847 else
6848#endif
6849 vim_beep(BO_ESC);
6850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 clearop(cap->oap);
6852
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006853 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6854 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006855 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 restart_edit = 'a';
6857}
6858
6859/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006860 * Move the cursor for the "A" command.
6861 */
6862 void
6863set_cursor_for_append_to_line(void)
6864{
6865 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006866 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006867 {
6868 int save_State = State;
6869
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006870 // Pretend Insert mode here to allow the cursor on the
6871 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006872 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006873 coladvance((colnr_T)MAXCOL);
6874 State = save_State;
6875 }
6876 else
6877 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6878}
6879
6880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006882 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 */
6884 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006885nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006887 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6889 cap->cmdchar = 'i';
6890
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006891 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006893 {
6894#ifdef FEAT_TERMINAL
6895 if (term_in_normal_mode())
6896 {
6897 end_visual_mode();
6898 clearop(cap->oap);
6899 term_enter_job_mode();
6900 return;
6901 }
6902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006906 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006907 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6908 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 nv_object(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006912#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006913 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006914 {
6915 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006916 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006917 return;
6918 }
6919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 else if (!curbuf->b_p_ma && !p_im)
6921 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006922 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006923 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006925 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006926 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006927 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006929 else if (cap->cmdchar == K_PS && VIsual_active)
6930 {
6931 pos_T old_pos = curwin->w_cursor;
6932 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006933 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006934
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006935 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006936 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6937 {
6938 shift_delete_registers();
6939 cap->oap->regname = '1';
6940 }
6941 else
6942 cap->oap->regname = '-';
6943 cap->cmdchar = 'd';
6944 cap->nchar = NUL;
6945 nv_operator(cap);
6946 do_pending_operator(cap, 0, FALSE);
6947 cap->cmdchar = K_PS;
6948
zeertzjq7a732522022-03-14 20:46:41 +00006949 if (*ml_get_cursor() != NUL)
6950 {
6951 if (old_visual_mode == 'V')
6952 {
6953 // In linewise Visual mode insert before the beginning of the
6954 // next line.
6955 // When the last line in the buffer was deleted then create a
6956 // new line, otherwise there is not need to move cursor.
6957 // Detect this by checking if cursor moved above Visual area.
6958 if (curwin->w_cursor.lnum < old_pos.lnum
6959 && curwin->w_cursor.lnum < old_visual.lnum)
6960 {
6961 if (u_save_cursor() == OK)
6962 {
6963 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6964 FALSE);
6965 appended_lines(curwin->w_cursor.lnum++, 1L);
6966 }
6967 }
6968 }
6969 // When the last char in the line was deleted then append.
6970 // Detect this by checking if cursor moved before Visual area.
6971 else if (curwin->w_cursor.col < old_pos.col
6972 && curwin->w_cursor.col < old_visual.col)
6973 inc_cursor();
6974 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006975
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006976 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006977 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 else if (!checkclearopq(cap->oap))
6980 {
6981 switch (cap->cmdchar)
6982 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006983 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006984 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 break;
6986
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006987 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006988 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6989 beginline(BL_WHITE);
6990 else
6991 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 break;
6993
Bram Moolenaara1891842017-02-04 21:34:31 +01006994 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006995 // Bracketed paste works like "a"ppend, unless the cursor is in
6996 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01006997 if (curwin->w_cursor.col == 0)
6998 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006999 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007000
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007001 case 'a': // "a"ppend is like "i"nsert on the next character.
7002 // increment coladd when in virtual space, increment the
7003 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 if (virtual_active()
7005 && (curwin->w_cursor.coladd > 0
7006 || *ml_get_cursor() == NUL
7007 || *ml_get_cursor() == TAB))
7008 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007009 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 inc_cursor();
7011 break;
7012 }
7013
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7015 {
7016 int save_State = State;
7017
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007018 // Pretend Insert mode here to allow the cursor on the
7019 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01007020 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 coladvance(getviscol());
7022 State = save_State;
7023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024
7025 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7026 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007027 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007028 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007029 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030}
7031
7032/*
7033 * Invoke edit() and take care of "restart_edit" and the return value.
7034 */
7035 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007036invoke_edit(
7037 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007038 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007039 int cmd,
7040 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041{
7042 int restart_edit_save = 0;
7043
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007044 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7045 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7046 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 if (repl || !stuff_empty())
7048 restart_edit_save = restart_edit;
7049 else
7050 restart_edit_save = 0;
7051
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007052 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 restart_edit = 0;
7054
7055 if (edit(cmd, startln, cap->count1))
7056 cap->retval |= CA_COMMAND_BUSY;
7057
7058 if (restart_edit == 0)
7059 restart_edit = restart_edit_save;
7060}
7061
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062/*
7063 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7064 */
7065 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007066nv_object(
7067 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068{
7069 int flag;
7070 int include;
7071 char_u *mps_save;
7072
7073 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007074 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007076 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007078 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 mps_save = curbuf->b_p_mps;
7080 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7081
7082 switch (cap->nchar)
7083 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007084 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 flag = current_word(cap->oap, cap->count1, include, FALSE);
7086 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007087 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 flag = current_word(cap->oap, cap->count1, include, TRUE);
7089 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007090 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 case '(':
7092 case ')':
7093 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7094 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007095 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 case '{':
7097 case '}':
7098 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7099 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007100 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 case ']':
7102 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7103 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007104 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 case '>':
7106 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7107 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007108#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007109 case 't': // "at" = a tag block (xml and html)
7110 // Do not adjust oap->end in do_pending_operator()
7111 // otherwise there are different results for 'dit'
7112 // (note leading whitespace in last line):
7113 // 1) <b> 2) <b>
7114 // foobar foobar
7115 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007116 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007117 flag = current_tagblock(cap->oap, cap->count1, include);
7118 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007119#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007120 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 flag = current_par(cap->oap, cap->count1, include, 'p');
7122 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007123 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 flag = current_sent(cap->oap, cap->count1, include);
7125 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007126 case '"': // "a"" = a double quoted string
7127 case '\'': // "a'" = a single quoted string
7128 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007129 flag = current_quote(cap->oap, cap->count1, include,
7130 cap->nchar);
7131 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007132#if 0 // TODO
7133 case 'S': // "aS" = a section
7134 case 'f': // "af" = a filename
7135 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136#endif
7137 default:
7138 flag = FAIL;
7139 break;
7140 }
7141
7142 curbuf->b_p_mps = mps_save;
7143 if (flag == FAIL)
7144 clearopbeep(cap->oap);
7145 adjust_cursor_col();
7146 curwin->w_set_curswant = TRUE;
7147}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148
7149/*
7150 * "q" command: Start/stop recording.
7151 * "q:", "q/", "q?": edit command-line in command-line window.
7152 */
7153 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007154nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155{
7156 if (cap->oap->op_type == OP_FORMAT)
7157 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007158 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 cap->cmdchar = 'g';
7160 cap->nchar = 'q';
7161 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007162 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007164
7165 if (checkclearop(cap->oap))
7166 return;
7167
7168 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007170 if (cmdwin_type != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007172 emsg(_(e_cmdline_window_already_open));
7173 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007175 stuffcharReadbuff(cap->nchar);
7176 stuffcharReadbuff(K_CMDWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007178 else
7179 // (stop) recording into a named register, unless executing a
7180 // register
7181 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
7182 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183}
7184
7185/*
7186 * Handle the "@r" command.
7187 */
7188 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007189nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190{
7191 if (checkclearop(cap->oap))
7192 return;
7193#ifdef FEAT_EVAL
7194 if (cap->nchar == '=')
7195 {
7196 if (get_expr_register() == NUL)
7197 return;
7198 }
7199#endif
7200 while (cap->count1-- && !got_int)
7201 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007202 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 {
7204 clearopbeep(cap->oap);
7205 break;
7206 }
7207 line_breakcheck();
7208 }
7209}
7210
7211/*
7212 * Handle the CTRL-U and CTRL-D commands.
7213 */
7214 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007215nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216{
7217 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7218 || (cap->cmdchar == Ctrl_D
7219 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7220 clearopbeep(cap->oap);
7221 else if (!checkclearop(cap->oap))
7222 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7223}
7224
7225/*
7226 * Handle "J" or "gJ" command.
7227 */
7228 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007229nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007231 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007233 nv_operator(cap);
7234 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007236
7237 if (checkclearop(cap->oap))
7238 return;
7239
7240 if (cap->count0 <= 1)
7241 cap->count0 = 2; // default for join is two lines!
7242 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7243 curbuf->b_ml.ml_line_count)
7244 {
7245 // can't join when on the last line
7246 if (cap->count0 <= 2)
7247 {
7248 clearopbeep(cap->oap);
7249 return;
7250 }
7251 cap->count0 = curbuf->b_ml.ml_line_count
7252 - curwin->w_cursor.lnum + 1;
7253 }
7254
7255 prep_redo(cap->oap->regname, cap->count0,
7256 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7257 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258}
7259
7260/*
7261 * "P", "gP", "p" and "gp" commands.
7262 */
7263 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007264nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007266 nv_put_opt(cap, FALSE);
7267}
7268
7269/*
7270 * "P", "gP", "p" and "gp" commands.
7271 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7272 */
7273 static void
7274nv_put_opt(cmdarg_T *cap, int fix_indent)
7275{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 int regname = 0;
7277 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007278 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007279 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 int dir;
7281 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007282 int keep_registers = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283
7284 if (cap->oap->op_type != OP_NOP)
7285 {
7286#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007287 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7289 {
7290 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007291 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 }
7293 else
7294#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007295 clearopbeep(cap->oap);
7296 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007298
Bram Moolenaarf2732452018-06-03 14:47:35 +02007299#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007300 if (bt_prompt(curbuf) && !prompt_curpos_editable())
Bram Moolenaarf2732452018-06-03 14:47:35 +02007301 {
7302 clearopbeep(cap->oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007303 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02007304 }
7305#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007306
7307 if (fix_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007309 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7310 ? FORWARD : BACKWARD;
7311 flags |= PUT_FIXINDENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007313 else
7314 dir = (cap->cmdchar == 'P'
7315 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7316 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
7317 prep_redo_cmd(cap);
7318 if (cap->cmdchar == 'g')
7319 flags |= PUT_CURSEND;
7320 else if (cap->cmdchar == 'z')
7321 flags |= PUT_BLOCK_INNER;
7322
7323 if (VIsual_active)
7324 {
7325 // Putting in Visual mode: The put text replaces the selected
7326 // text. First delete the selected text, then put the new text.
7327 // Need to save and restore the registers that the delete
7328 // overwrites if the old contents is being put.
7329 was_visual = TRUE;
7330 regname = cap->oap->regname;
7331 keep_registers = cap->cmdchar == 'P';
7332#ifdef FEAT_CLIPBOARD
7333 adjust_clip_reg(&regname);
7334#endif
7335 if (regname == 0 || regname == '"'
7336 || VIM_ISDIGIT(regname) || regname == '-'
7337#ifdef FEAT_CLIPBOARD
7338 || (clip_unnamed && (regname == '*' || regname == '+'))
7339#endif
7340
7341 )
7342 {
7343 // The delete is going to overwrite the register we want to
7344 // put, save it first.
7345 reg1 = get_register(regname, TRUE);
7346 }
7347
7348 // Now delete the selected text. Avoid messages here.
7349 cap->cmdchar = 'd';
7350 cap->nchar = NUL;
7351 cap->oap->regname = keep_registers ? '_' : NUL;
7352 ++msg_silent;
7353 nv_operator(cap);
7354 do_pending_operator(cap, 0, FALSE);
7355 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7356 --msg_silent;
7357
7358 // delete PUT_LINE_BACKWARD;
7359 cap->oap->regname = regname;
7360
7361 if (reg1 != NULL)
7362 {
7363 // Delete probably changed the register we want to put, save
7364 // it first. Then put back what was there before the delete.
7365 reg2 = get_register(regname, FALSE);
7366 put_register(regname, reg1);
7367 }
7368
7369 // When deleted a linewise Visual area, put the register as
7370 // lines to avoid it joined with the next line. When deletion was
7371 // characterwise, split a line when putting lines.
7372 if (VIsual_mode == 'V')
7373 flags |= PUT_LINE;
7374 else if (VIsual_mode == 'v')
7375 flags |= PUT_LINE_SPLIT;
7376 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7377 flags |= PUT_LINE_FORWARD;
7378 dir = BACKWARD;
7379 if ((VIsual_mode != 'V'
7380 && curwin->w_cursor.col < curbuf->b_op_start.col)
7381 || (VIsual_mode == 'V'
7382 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
7383 // cursor is at the end of the line or end of file, put
7384 // forward.
7385 dir = FORWARD;
7386 // May have been reset in do_put().
7387 VIsual_active = TRUE;
7388 }
7389 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
7390
7391 // If a register was saved, put it back now.
7392 if (reg2 != NULL)
7393 put_register(regname, reg2);
7394
7395 // What to reselect with "gv"? Selecting the just put text seems to
7396 // be the most useful, since the original text was removed.
7397 if (was_visual)
7398 {
7399 curbuf->b_visual.vi_start = curbuf->b_op_start;
7400 curbuf->b_visual.vi_end = curbuf->b_op_end;
7401 // need to adjust cursor position
7402 if (*p_sel == 'e')
7403 inc(&curbuf->b_visual.vi_end);
7404 }
7405
7406 // When all lines were selected and deleted do_put() leaves an empty
7407 // line that needs to be deleted now.
7408 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
7409 {
7410 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
7411 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
7412
7413 // If the cursor was in that line, move it to the end of the last
7414 // line.
7415 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7416 {
7417 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7418 coladvance((colnr_T)MAXCOL);
7419 }
7420 }
7421 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422}
7423
7424/*
7425 * "o" and "O" commands.
7426 */
7427 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007428nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429{
7430#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007431 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7433 {
7434 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007435 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 }
7437 else
7438#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007439 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007441#ifdef FEAT_JOB_CHANNEL
7442 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007443 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 n_opencmd(cap);
7447}
7448
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449#ifdef FEAT_NETBEANS_INTG
7450 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007451nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452{
7453 netbeans_keycommand(cap->nchar);
7454}
7455#endif
7456
7457#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007459nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007461 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462}
7463#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007464
Bram Moolenaar3918c952005-03-15 22:34:55 +00007465/*
7466 * Trigger CursorHold event.
7467 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7468 * input buffer. "did_cursorhold" is set to avoid retriggering.
7469 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007470 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007471nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007472{
7473 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7474 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007475 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007476}