blob: cbcfd746def78202400f0e665099d18b3d2cdb13 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
ichizok672776d2022-01-31 12:27:18 +00003 * VIM - Vi IMproved by Bram Moolenaar et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
12 * the operators.
13 */
14
15#include "vim.h"
16
Bram Moolenaar792cf5e2019-09-30 23:12:16 +020017static int VIsual_mode_orig = NUL; // saved Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +000018
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010019#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010020static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount);
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010021#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static void unshift_special(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010023static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25/*
26 * nv_*(): functions called to handle Normal and Visual mode commands.
27 * n_*(): functions called to handle Normal mode commands.
28 * v_*(): functions called to handle Visual mode commands.
29 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010030static void nv_ignore(cmdarg_T *cap);
31static void nv_nop(cmdarg_T *cap);
32static void nv_error(cmdarg_T *cap);
33static void nv_help(cmdarg_T *cap);
34static void nv_addsub(cmdarg_T *cap);
35static void nv_page(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010036static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010038static void nv_ver_scrollbar(cmdarg_T *cap);
39static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000041#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void nv_tabline(cmdarg_T *cap);
43static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000044#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010045static void nv_exmode(cmdarg_T *cap);
46static void nv_colon(cmdarg_T *cap);
47static void nv_ctrlg(cmdarg_T *cap);
48static void nv_ctrlh(cmdarg_T *cap);
49static void nv_clear(cmdarg_T *cap);
50static void nv_ctrlo(cmdarg_T *cap);
51static void nv_hat(cmdarg_T *cap);
52static void nv_Zet(cmdarg_T *cap);
53static void nv_ident(cmdarg_T *cap);
54static void nv_tagpop(cmdarg_T *cap);
55static void nv_scroll(cmdarg_T *cap);
56static void nv_right(cmdarg_T *cap);
57static void nv_left(cmdarg_T *cap);
58static void nv_up(cmdarg_T *cap);
59static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010060static void nv_end(cmdarg_T *cap);
61static void nv_dollar(cmdarg_T *cap);
62static void nv_search(cmdarg_T *cap);
63static void nv_next(cmdarg_T *cap);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +020064static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt, int *wrapped);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010065static void nv_csearch(cmdarg_T *cap);
66static void nv_brackets(cmdarg_T *cap);
67static void nv_percent(cmdarg_T *cap);
68static void nv_brace(cmdarg_T *cap);
69static void nv_mark(cmdarg_T *cap);
70static void nv_findpar(cmdarg_T *cap);
71static void nv_undo(cmdarg_T *cap);
72static void nv_kundo(cmdarg_T *cap);
73static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010074static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010075static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
76static void v_visop(cmdarg_T *cap);
77static void nv_subst(cmdarg_T *cap);
78static void nv_abbrev(cmdarg_T *cap);
79static void nv_optrans(cmdarg_T *cap);
80static void nv_gomark(cmdarg_T *cap);
81static void nv_pcmark(cmdarg_T *cap);
82static void nv_regname(cmdarg_T *cap);
83static void nv_visual(cmdarg_T *cap);
84static void n_start_visual_mode(int c);
85static void nv_window(cmdarg_T *cap);
86static void nv_suspend(cmdarg_T *cap);
87static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010088static void nv_dot(cmdarg_T *cap);
Shougo Matsushita4ede01f2022-01-20 15:26:03 +000089static void nv_redo_or_register(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010090static void nv_Undo(cmdarg_T *cap);
91static void nv_tilde(cmdarg_T *cap);
92static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000093#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010094static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000095#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010096static void nv_lineop(cmdarg_T *cap);
97static void nv_home(cmdarg_T *cap);
98static void nv_pipe(cmdarg_T *cap);
99static void nv_bck_word(cmdarg_T *cap);
100static void nv_wordcmd(cmdarg_T *cap);
101static void nv_beginline(cmdarg_T *cap);
102static void adjust_cursor(oparg_T *oap);
103static void adjust_for_sel(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100104static void nv_select(cmdarg_T *cap);
105static void nv_goto(cmdarg_T *cap);
106static void nv_normal(cmdarg_T *cap);
107static void nv_esc(cmdarg_T *oap);
108static void nv_edit(cmdarg_T *cap);
109static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100110static void nv_object(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100111static void nv_record(cmdarg_T *cap);
112static void nv_at(cmdarg_T *cap);
113static void nv_halfpage(cmdarg_T *cap);
114static void nv_join(cmdarg_T *cap);
115static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200116static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100117static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100119static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100122static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100124static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125
ichizok672776d2022-01-31 12:27:18 +0000126// Declare nv_cmds[].
127#define DO_DECLARE_NVCMD
128#include "nv_cmds.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Yegappan Lakshmanan4dc0dd82022-01-29 13:06:40 +0000130// Include the lookuptable generated by create_nvcmdidx.vim.
131#include "nv_cmdidxs.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133/*
134 * Search for a command in the commands table.
135 * Returns -1 for invalid command.
136 */
137 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100138find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139{
140 int i;
141 int idx;
142 int top, bot;
143 int c;
144
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100145 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 if (cmdchar >= 0x100)
147 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100149 // We use the absolute value of the character. Special keys have a
150 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 if (cmdchar < 0)
152 cmdchar = -cmdchar;
153
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100154 // If the character is in the first part: The character is the index into
155 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 if (cmdchar <= nv_max_linear)
157 return nv_cmd_idx[cmdchar];
158
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100159 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 bot = nv_max_linear + 1;
161 top = NV_CMDS_SIZE - 1;
162 idx = -1;
163 while (bot <= top)
164 {
165 i = (top + bot) / 2;
166 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
167 if (c < 0)
168 c = -c;
169 if (cmdchar == c)
170 {
171 idx = nv_cmd_idx[i];
172 break;
173 }
174 if (cmdchar > c)
175 bot = i + 1;
176 else
177 top = i - 1;
178 }
179 return idx;
180}
181
182/*
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100183 * If currently editing a cmdline or text is locked: beep and give an error
184 * message, return TRUE.
185 */
186 static int
187check_text_locked(oparg_T *oap)
188{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000189 if (!text_locked())
190 return FALSE;
191
192 if (oap != NULL)
193 clearopbeep(oap);
194 text_locked_msg();
195 return TRUE;
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100196}
197
198/*
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000199 * If text is locked, "curbuf_lock" or "allbuf_lock" is set:
200 * Give an error message, possibly beep and return TRUE.
201 * "oap" may be NULL.
202 */
203 int
204check_text_or_curbuf_locked(oparg_T *oap)
205{
206 if (check_text_locked(oap))
207 return TRUE;
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000208
209 if (!curbuf_locked())
210 return FALSE;
211
212 if (oap != NULL)
213 clearop(oap);
214 return TRUE;
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000215}
216
217/*
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000218 * Handle the count before a normal command and set cap->count0.
219 */
220 static int
221normal_cmd_get_count(
222 cmdarg_T *cap,
223 int c,
224 int toplevel UNUSED,
225 int set_prevcount UNUSED,
226 int *ctrl_w,
227 int *need_flushbuf UNUSED)
228{
229getcount:
230 if (!(VIsual_active && VIsual_select))
231 {
232 // Handle a count before a command and compute ca.count0.
233 // Note that '0' is a command and not the start of a count, but it's
234 // part of a count after other digits.
235 while ((c >= '1' && c <= '9')
236 || (cap->count0 != 0 && (c == K_DEL || c == K_KDEL
237 || c == '0')))
238 {
239 if (c == K_DEL || c == K_KDEL)
240 {
241 cap->count0 /= 10;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000242 del_from_showcmd(4); // delete the digit and ~@%
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000243 }
244 else if (cap->count0 > 99999999L)
245 {
246 cap->count0 = 999999999L;
247 }
248 else
249 {
250 cap->count0 = cap->count0 * 10 + (c - '0');
251 }
252#ifdef FEAT_EVAL
253 // Set v:count here, when called from main() and not a stuffed
254 // command, so that v:count can be used in an expression mapping
255 // right after the count. Do set it for redo.
256 if (toplevel && readbuf1_empty())
257 set_vcount_ca(cap, &set_prevcount);
258#endif
259 if (*ctrl_w)
260 {
261 ++no_mapping;
262 ++allow_keys; // no mapping for nchar, but keys
263 }
264 ++no_zero_mapping; // don't map zero here
265 c = plain_vgetc();
266 LANGMAP_ADJUST(c, TRUE);
267 --no_zero_mapping;
268 if (*ctrl_w)
269 {
270 --no_mapping;
271 --allow_keys;
272 }
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000273 *need_flushbuf |= add_to_showcmd(c);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000274 }
275
276 // If we got CTRL-W there may be a/another count
277 if (c == Ctrl_W && !*ctrl_w && cap->oap->op_type == OP_NOP)
278 {
279 *ctrl_w = TRUE;
280 cap->opcount = cap->count0; // remember first count
281 cap->count0 = 0;
282 ++no_mapping;
283 ++allow_keys; // no mapping for nchar, but keys
284 c = plain_vgetc(); // get next character
285 LANGMAP_ADJUST(c, TRUE);
286 --no_mapping;
287 --allow_keys;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000288 *need_flushbuf |= add_to_showcmd(c);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000289 goto getcount; // jump back
290 }
291 }
292
293 if (c == K_CURSORHOLD)
294 {
295 // Save the count values so that ca.opcount and ca.count0 are exactly
296 // the same when coming back here after handling K_CURSORHOLD.
297 cap->oap->prev_opcount = cap->opcount;
298 cap->oap->prev_count0 = cap->count0;
299 }
300 else if (cap->opcount != 0)
301 {
302 // If we're in the middle of an operator (including after entering a
303 // yank buffer with '"') AND we had a count before the operator, then
304 // that count overrides the current value of ca.count0.
305 // What this means effectively, is that commands like "3dw" get turned
306 // into "d3w" which makes things fall into place pretty neatly.
307 // If you give a count before AND after the operator, they are
308 // multiplied.
309 if (cap->count0)
310 {
311 if (cap->opcount >= 999999999L / cap->count0)
312 cap->count0 = 999999999L;
313 else
314 cap->count0 *= cap->opcount;
315 }
316 else
317 cap->count0 = cap->opcount;
318 }
319
320 // Always remember the count. It will be set to zero (on the next call,
321 // above) when there is no pending operator.
322 // When called from main(), save the count for use by the "count" built-in
323 // variable.
324 cap->opcount = cap->count0;
325 cap->count1 = (cap->count0 == 0 ? 1 : cap->count0);
326
327#ifdef FEAT_EVAL
328 // Only set v:count when called from main() and not a stuffed command.
329 // Do set it for redo.
330 if (toplevel && readbuf1_empty())
331 set_vcount(cap->count0, cap->count1, set_prevcount);
332#endif
333
334 return c;
335}
336
337/*
338 * Returns TRUE if the normal command (cap) needs a second character.
339 */
340 static int
341normal_cmd_needs_more_chars(cmdarg_T *cap, short_u cmd_flags)
342{
343 return ((cmd_flags & NV_NCH)
344 && (((cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
345 && cap->oap->op_type == OP_NOP)
346 || (cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
347 || (cap->cmdchar == 'q'
348 && cap->oap->op_type == OP_NOP
349 && reg_recording == 0
350 && reg_executing == 0)
351 || ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
352 && (cap->oap->op_type != OP_NOP || VIsual_active))));
353}
354
355/*
356 * Get one or more additional characters for a normal command.
357 * Return the updated command index (if changed).
358 */
359 static int
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000360normal_cmd_get_more_chars(
361 int idx_arg,
362 cmdarg_T *cap,
363 int *need_flushbuf UNUSED)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000364{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000365 int idx = idx_arg;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000366 int c;
367 int *cp;
368 int repl = FALSE; // get character for replace mode
369 int lit = FALSE; // get extra character literally
370 int langmap_active = FALSE; // using :lmap mappings
371 int lang; // getting a text character
372#ifdef HAVE_INPUT_METHOD
373 int save_smd; // saved value of p_smd
374#endif
375
376 ++no_mapping;
377 ++allow_keys; // no mapping for nchar, but allow key codes
378 // Don't generate a CursorHold event here, most commands can't handle
379 // it, e.g., nv_replace(), nv_csearch().
380 did_cursorhold = TRUE;
381 if (cap->cmdchar == 'g')
382 {
383 /*
384 * For 'g' get the next character now, so that we can check for
385 * "gr", "g'" and "g`".
386 */
387 cap->nchar = plain_vgetc();
388 LANGMAP_ADJUST(cap->nchar, TRUE);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000389 *need_flushbuf |= add_to_showcmd(cap->nchar);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000390 if (cap->nchar == 'r' || cap->nchar == '\'' || cap->nchar == '`'
391 || cap->nchar == Ctrl_BSL)
392 {
393 cp = &cap->extra_char; // need to get a third character
394 if (cap->nchar != 'r')
395 lit = TRUE; // get it literally
396 else
397 repl = TRUE; // get it in replace mode
398 }
399 else
400 cp = NULL; // no third character needed
401 }
402 else
403 {
404 if (cap->cmdchar == 'r') // get it in replace mode
405 repl = TRUE;
406 cp = &cap->nchar;
407 }
408 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
409
410 /*
411 * Get a second or third character.
412 */
413 if (cp != NULL)
414 {
415 if (repl)
416 {
Bram Moolenaar24959102022-05-07 20:01:16 +0100417 State = MODE_REPLACE; // pretend Replace mode
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000418#ifdef CURSOR_SHAPE
419 ui_cursor_shape(); // show different cursor shape
420#endif
421 }
422 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
423 {
424 // Allow mappings defined with ":lmap".
425 --no_mapping;
426 --allow_keys;
427 if (repl)
Bram Moolenaar24959102022-05-07 20:01:16 +0100428 State = MODE_LREPLACE;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000429 else
Bram Moolenaar24959102022-05-07 20:01:16 +0100430 State = MODE_LANGMAP;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000431 langmap_active = TRUE;
432 }
433#ifdef HAVE_INPUT_METHOD
434 save_smd = p_smd;
435 p_smd = FALSE; // Don't let the IM code show the mode here
436 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
437 im_set_active(TRUE);
438#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100439 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000440 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100441 MAY_WANT_TO_LOG_THIS;
442
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000443 // Disable bracketed paste and modifyOtherKeys here, we won't
444 // recognize the escape sequences with 'esckeys' off.
445 out_str(T_BD);
Bram Moolenaar63a2e362022-11-23 20:20:18 +0000446 out_str_t_TE();
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000447 }
448
449 *cp = plain_vgetc();
450
Bram Moolenaar24959102022-05-07 20:01:16 +0100451 if ((State & MODE_INSERT) && !p_ek)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000452 {
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100453 MAY_WANT_TO_LOG_THIS;
454
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000455 // Re-enable bracketed paste mode and modifyOtherKeys
Bram Moolenaarfc966c12023-01-01 18:04:33 +0000456 out_str_t_BE();
Bram Moolenaar733a69b2022-12-01 12:03:47 +0000457 out_str_t_TI();
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000458 }
459
460 if (langmap_active)
461 {
462 // Undo the decrement done above
463 ++no_mapping;
464 ++allow_keys;
Bram Moolenaar24959102022-05-07 20:01:16 +0100465 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000466 }
467#ifdef HAVE_INPUT_METHOD
468 if (lang)
469 {
470 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
471 im_save_status(&curbuf->b_p_iminsert);
472 im_set_active(FALSE);
473 }
474 p_smd = save_smd;
475#endif
Bram Moolenaar24959102022-05-07 20:01:16 +0100476 State = MODE_NORMAL_BUSY;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000477 *need_flushbuf |= add_to_showcmd(*cp);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000478
479 if (!lit)
480 {
481#ifdef FEAT_DIGRAPHS
482 // Typing CTRL-K gets a digraph.
483 if (*cp == Ctrl_K
484 && ((nv_cmds[idx].cmd_flags & NV_LANG)
485 || cp == &cap->extra_char)
486 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
487 {
488 c = get_digraph(FALSE);
489 if (c > 0)
490 {
491 *cp = c;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000492 // Guessing how to update showcmd here...
493 del_from_showcmd(3);
494 *need_flushbuf |= add_to_showcmd(*cp);
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000495 }
496 }
497#endif
498
499 // adjust chars > 127, except after "tTfFr" commands
500 LANGMAP_ADJUST(*cp, !lang);
501#ifdef FEAT_RIGHTLEFT
502 // adjust Hebrew mapped char
503 if (p_hkmap && lang && KeyTyped)
504 *cp = hkmap(*cp);
505#endif
506 }
507
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000508 // When the next character is CTRL-\ a following CTRL-N means the
509 // command is aborted and we go to Normal mode.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000510 if (cp == &cap->extra_char
511 && cap->nchar == Ctrl_BSL
512 && (cap->extra_char == Ctrl_N || cap->extra_char == Ctrl_G))
513 {
514 cap->cmdchar = Ctrl_BSL;
515 cap->nchar = cap->extra_char;
516 idx = find_command(cap->cmdchar);
517 }
zeertzjq4f026ea2023-02-26 14:47:24 +0000518 else if ((cap->nchar == 'n' || cap->nchar == 'N')
519 && cap->cmdchar == 'g')
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000520 cap->oap->op_type = get_op_type(*cp, NUL);
521 else if (*cp == Ctrl_BSL)
522 {
523 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
524
525 // There is a busy wait here when typing "f<C-\>" and then
526 // something different from CTRL-N. Can't be avoided.
527 while ((c = vpeekc()) <= 0 && towait > 0L)
528 {
529 do_sleep(towait > 50L ? 50L : towait, FALSE);
530 towait -= 50L;
531 }
532 if (c > 0)
533 {
534 c = plain_vgetc();
535 if (c != Ctrl_N && c != Ctrl_G)
536 vungetc(c);
537 else
538 {
539 cap->cmdchar = Ctrl_BSL;
540 cap->nchar = c;
541 idx = find_command(cap->cmdchar);
542 }
543 }
544 }
545
zeertzjqbacc8302023-08-12 00:09:31 +0200546 if (enc_utf8 && lang)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000547 {
zeertzjqbacc8302023-08-12 00:09:31 +0200548 // When getting a text character and the next character is a
549 // multi-byte character, it could be a composing character.
550 // However, don't wait for it to arrive. Also, do enable mapping,
551 // because if it's put back with vungetc() it's too late to apply
552 // mapping.
553 --no_mapping;
554 while ((c = vpeekc()) > 0
555 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000556 {
zeertzjqbacc8302023-08-12 00:09:31 +0200557 c = plain_vgetc();
558 if (!utf_iscomposing(c))
559 {
560 vungetc(c); // it wasn't, put it back
561 break;
562 }
563 else if (cap->ncharC1 == 0)
564 cap->ncharC1 = c;
565 else
566 cap->ncharC2 = c;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000567 }
zeertzjqbacc8302023-08-12 00:09:31 +0200568 ++no_mapping;
569 // Vim may be in a different mode when the user types the next key,
570 // but when replaying a recording the next key is already in the
571 // typeahead buffer, so record a <Nop> before that to prevent the
572 // vpeekc() above from applying wrong mappings when replaying.
573 gotchars_nop();
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000574 }
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000575 }
576 --no_mapping;
577 --allow_keys;
578
579 return idx;
580}
581
582/*
583 * Returns TRUE if after processing a normal mode command, need to wait for a
584 * moment when a message is displayed that will be overwritten by the mode
585 * message.
586 */
587 static int
588normal_cmd_need_to_wait_for_msg(cmdarg_T *cap, pos_T *old_pos)
589{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000590 // In Visual mode and with "^O" in Insert mode, a short message will be
591 // overwritten by the mode message. Wait a bit, until a key is hit.
592 // In Visual mode, it's more important to keep the Visual area updated
593 // than keeping a message (e.g. from a /pat search).
594 // Only do this if the command was typed, not from a mapping.
595 // Don't wait when emsg_silent is non-zero.
596 // Also wait a bit after an error message, e.g. for "^O:".
597 // Don't redraw the screen, it would remove the message.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000598 return ( ((p_smd
599 && msg_silent == 0
600 && (restart_edit != 0
601 || (VIsual_active
602 && old_pos->lnum == curwin->w_cursor.lnum
603 && old_pos->col == curwin->w_cursor.col)
604 )
605 && (clear_cmdline
606 || redraw_cmdline)
607 && (msg_didout || (msg_didany && msg_scroll))
608 && !msg_nowait
609 && KeyTyped)
610 || (restart_edit != 0
611 && !VIsual_active
612 && (msg_scroll
613 || emsg_on_display)))
614 && cap->oap->regname == 0
615 && !(cap->retval & CA_COMMAND_BUSY)
616 && stuff_empty()
617 && typebuf_typed()
618 && emsg_silent == 0
619 && !in_assert_fails
620 && !did_wait_return
621 && cap->oap->op_type == OP_NOP);
622}
623
624/*
625 * After processing a normal mode command, wait for a moment when a message is
626 * displayed that will be overwritten by the mode message.
627 */
628 static void
629normal_cmd_wait_for_msg(void)
630{
631 int save_State = State;
632
633 // Draw the cursor with the right shape here
634 if (restart_edit != 0)
Bram Moolenaar24959102022-05-07 20:01:16 +0100635 State = MODE_INSERT;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000636
637 // If need to redraw, and there is a "keep_msg", redraw before the
638 // delay
639 if (must_redraw && keep_msg != NULL && !emsg_on_display)
640 {
641 char_u *kmsg;
642
643 kmsg = keep_msg;
644 keep_msg = NULL;
645 // Showmode() will clear keep_msg, but we want to use it anyway.
646 // First update w_topline.
647 setcursor();
648 update_screen(0);
649 // now reset it, otherwise it's put in the history again
650 keep_msg = kmsg;
651
652 kmsg = vim_strsave(keep_msg);
653 if (kmsg != NULL)
654 {
655 msg_attr((char *)kmsg, keep_msg_attr);
656 vim_free(kmsg);
657 }
658 }
659 setcursor();
660#ifdef CURSOR_SHAPE
661 ui_cursor_shape(); // may show different cursor shape
662#endif
663 cursor_on();
664 out_flush();
665 if (msg_scroll || emsg_on_display)
666 ui_delay(1003L, TRUE); // wait at least one second
667 ui_delay(3003L, FALSE); // wait up to three seconds
668 State = save_State;
669
670 msg_scroll = FALSE;
671 emsg_on_display = FALSE;
672}
673
674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 * Execute a command in Normal mode.
676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100678normal_cmd(
679 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100680 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100682 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100684 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 int old_col = curwin->w_curswant;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000686 int need_flushbuf = FALSE; // need to call out_flush()
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100687 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 static int old_mapped_len = 0;
690 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000691 int set_prevcount = FALSE;
Bram Moolenaarb146e012020-07-19 23:06:05 +0200692 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693
Bram Moolenaara80faa82020-04-12 19:37:17 +0200694 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000696
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100697 // Use a count remembered from before entering an operator. After typing
698 // "3d" we return from normal_cmd() and come back here, the "3" is
699 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 ca.opcount = opcount;
701
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000702 // If there is an operator pending, then the command we take this time
703 // will terminate it. Finish_op tells us to finish the operation before
704 // returning this time (unless the operation was cancelled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705#ifdef CURSOR_SHAPE
706 c = finish_op;
707#endif
708 finish_op = (oap->op_type != OP_NOP);
709#ifdef CURSOR_SHAPE
710 if (finish_op != c)
711 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100712 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713# ifdef FEAT_MOUSESHAPE
714 update_mouseshape(-1);
715# endif
716 }
717#endif
LemonBoy2bf52dd2022-04-09 18:17:34 +0100718 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100720 // When not finishing an operator and no register name typed, reset the
721 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000723 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000725#ifdef FEAT_EVAL
726 set_prevcount = TRUE;
727#endif
728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100730 // Restore counts from before receiving K_CURSORHOLD. This means after
731 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
732 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000733 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
734 {
735 ca.opcount = oap->prev_opcount;
736 ca.count0 = oap->prev_count0;
737 oap->prev_opcount = 0;
738 oap->prev_count0 = 0;
739 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000740
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742
Bram Moolenaar24959102022-05-07 20:01:16 +0100743 State = MODE_NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100745 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746#endif
747
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100748#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100749 // Set v:count here, when called from main() and not a stuffed
750 // command, so that v:count can be used in an expression mapping
751 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100752 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100753 set_vcount_ca(&ca, &set_prevcount);
754#endif
755
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 /*
757 * Get the command character from the user.
758 */
759 c = safe_vgetc();
Bram Moolenaar24959102022-05-07 20:01:16 +0100760 LANGMAP_ADJUST(c, get_real_state() != MODE_SELECT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000762 // If a mapping was started in Visual or Select mode, remember the length
763 // of the mapping. This is used below to not return to Insert mode for as
764 // long as the mapping is being executed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 if (restart_edit == 0)
766 old_mapped_len = 0;
767 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000768 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 old_mapped_len = typebuf_maplen();
770
771 if (c == NUL)
772 c = K_ZERO;
773
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000774 // In Select mode, typed text replaces the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 if (VIsual_active
776 && VIsual_select
777 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
778 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000779 int len;
780
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100781 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
782 // 'insertmode' is set) fake a "d"elete command, Insert mode will
783 // restart automatically.
784 // Insert the typed character in the typeahead buffer, so that it can
785 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000786 len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
787
zeertzjqfbf4f1c2022-01-28 12:50:43 +0000788 // When recording and gotchars() was called the character will be
789 // recorded again, remove the previous recording.
790 if (KeyTyped)
791 ungetchars(len);
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000792
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000793 if (restart_edit != 0)
794 c = 'd';
795 else
796 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100797 msg_nowait = TRUE; // don't delay going to insert mode
798 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
Bram Moolenaard0fb9072021-12-09 11:57:22 +0000801 // If the window was made so small that nothing shows, make it at least one
802 // line and one column when typing a command.
803 if (KeyTyped && !KeyStuffed)
804 win_ensure_size();
805
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 need_flushbuf = add_to_showcmd(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000808 // Get the command count
809 c = normal_cmd_get_count(&ca, c, toplevel, set_prevcount, &ctrl_w,
810 &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000812 // Find the command character in the table of commands.
813 // For CTRL-W we already got nchar when looking for a count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 if (ctrl_w)
815 {
816 ca.nchar = c;
817 ca.cmdchar = Ctrl_W;
818 }
819 else
820 ca.cmdchar = c;
821 idx = find_command(ca.cmdchar);
822 if (idx < 0)
823 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100824 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 clearopbeep(oap);
826 goto normal_end;
827 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000828
Bram Moolenaarcc762a42022-11-25 13:03:31 +0000829 if ((nv_cmds[idx].cmd_flags & NV_NCW) && check_text_or_curbuf_locked(oap))
Bram Moolenaar338f1fc2022-05-26 15:56:23 +0100830 // this command is not allowed now
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000831 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000833 // In Visual/Select mode, a few keys are handled in a special way.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 if (VIsual_active)
835 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100836 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 if (km_stopsel
838 && (nv_cmds[idx].cmd_flags & NV_STS)
839 && !(mod_mask & MOD_MASK_SHIFT))
840 {
841 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100842 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 }
844
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100845 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 if (km_startsel)
847 {
848 if (nv_cmds[idx].cmd_flags & NV_SS)
849 {
850 unshift_special(&ca);
851 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000852 if (idx < 0)
853 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100854 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000855 clearopbeep(oap);
856 goto normal_end;
857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 }
859 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
860 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 }
863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864
865#ifdef FEAT_RIGHTLEFT
866 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
867 && (nv_cmds[idx].cmd_flags & NV_RL))
868 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100869 // Invert horizontal movements and operations. Only when typed by the
870 // user directly, not when the result of a mapping or "x" translated
871 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 switch (ca.cmdchar)
873 {
874 case 'l': ca.cmdchar = 'h'; break;
875 case K_RIGHT: ca.cmdchar = K_LEFT; break;
876 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
877 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
878 case 'h': ca.cmdchar = 'l'; break;
879 case K_LEFT: ca.cmdchar = K_RIGHT; break;
880 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
881 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
882 case '>': ca.cmdchar = '<'; break;
883 case '<': ca.cmdchar = '>'; break;
884 }
885 idx = find_command(ca.cmdchar);
886 }
887#endif
888
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000889 // Get additional characters if we need them.
890 if (normal_cmd_needs_more_chars(&ca, nv_cmds[idx].cmd_flags))
891 idx = normal_cmd_get_more_chars(idx, &ca, &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000893 // Flush the showcmd characters onto the screen so we can see them while
894 // the command is being executed. Only do this when the shown command was
895 // actually displayed, otherwise this will slow down a lot when executing
896 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 if (need_flushbuf)
898 out_flush();
Martin Tournoijba43e762022-10-13 22:12:15 +0100899
Bram Moolenaard9205ca2008-10-02 20:55:54 +0000900 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +0200901 {
902 if (ex_normal_busy)
903 did_cursorhold = save_did_cursorhold;
904 else
905 did_cursorhold = FALSE;
906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907
Bram Moolenaar24959102022-05-07 20:01:16 +0100908 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909
zeertzjqf86dea82023-03-05 21:15:06 +0000910 if (ca.nchar == ESC || ca.extra_char == ESC)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 {
912 clearop(oap);
913 if (restart_edit == 0 && goto_im())
914 restart_edit = 'a';
915 goto normal_end;
916 }
917
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000918 if (ca.cmdchar != K_IGNORE)
919 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100920 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000921 msg_col = 0;
922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923
Luuk van Baalaa7f25e2023-04-13 21:47:38 +0100924 old_pos = curwin->w_cursor; // remember where the cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100926 // When 'keymodel' contains "startsel" some keys start Select/Visual
927 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 if (!VIsual_active && km_startsel)
929 {
930 if (nv_cmds[idx].cmd_flags & NV_SS)
931 {
932 start_selection();
933 unshift_special(&ca);
934 idx = find_command(ca.cmdchar);
935 }
936 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
937 && (mod_mask & MOD_MASK_SHIFT))
938 {
939 start_selection();
940 mod_mask &= ~MOD_MASK_SHIFT;
941 }
942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000944 // Execute the command!
945 // Call the command function found in the commands table.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 ca.arg = nv_cmds[idx].cmd_arg;
947 (nv_cmds[idx].cmd_func)(&ca);
948
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000949 // If we didn't start or finish an operator, reset oap->regname, unless we
950 // need it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 if (!finish_op
952 && !oap->op_type
953 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
954 {
955 clearop(oap);
956#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +0200957 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958#endif
959 }
960
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100961 // Get the length of mapped chars again after typing a count, second
962 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000963 if (old_mapped_len > 0)
964 old_mapped_len = typebuf_maplen();
965
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000966 // If an operation is pending, handle it. But not for K_IGNORE or
967 // K_MOUSEMOVE.
Bram Moolenaar1ad72c82021-05-04 21:56:28 +0200968 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +0100969 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000971 // Wait for a moment when a message is displayed that will be overwritten
972 // by the mode message.
973 if (normal_cmd_need_to_wait_for_msg(&ca, &old_pos))
974 normal_cmd_wait_for_msg();
975
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000976 // Finish up after executing a Normal mode command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977normal_end:
978
979 msg_nowait = FALSE;
980
Bram Moolenaarcc613032020-06-07 21:31:18 +0200981#ifdef FEAT_EVAL
982 if (finish_op)
983 reset_reg_var();
984#endif
985
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986#ifdef CURSOR_SHAPE
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000987 int prev_finish_op = finish_op;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988#endif
zeertzjq73916ba2023-04-26 16:50:19 +0100989 if (oap->op_type == OP_NOP)
990 {
991 // Reset finish_op, in case it was set
992 finish_op = FALSE;
993 may_trigger_modechanged();
994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100996 // Redraw the cursor with another shape, if we were in Operator-pending
997 // mode or did a replace command.
zeertzjqf86dea82023-03-05 21:15:06 +0000998 if (prev_finish_op || ca.cmdchar == 'r'
999 || (ca.cmdchar == 'g' && ca.nchar == 'r'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001001 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002# ifdef FEAT_MOUSESHAPE
1003 update_mouseshape(-1);
1004# endif
1005 }
1006#endif
1007
Bram Moolenaara983fe92008-07-31 20:04:27 +00001008 if (oap->op_type == OP_NOP && oap->regname == 0
zeertzjq73916ba2023-04-26 16:50:19 +01001009 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001012 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 vim_free(ca.searchbuf);
1014
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 if (has_mbyte)
1016 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 if (curwin->w_p_scb && toplevel)
1019 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001020 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 do_check_scrollbind(TRUE);
1022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
Bram Moolenaar860cae12010-06-05 23:22:07 +02001024 if (curwin->w_p_crb && toplevel)
1025 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001026 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001027 do_check_cursorbind();
1028 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001029
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001030#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001031 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001032 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001033 restart_edit = 0;
1034#endif
1035
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001036 // May restart edit(), if we got here with CTRL-O in Insert mode (but not
1037 // if still inside a mapping that started in Visual mode).
1038 // May switch from Visual to Select mode after CTRL-O command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1041 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 && !(ca.retval & CA_COMMAND_BUSY)
1043 && stuff_empty()
1044 && oap->regname == 0)
1045 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 if (restart_VIsual_select == 1)
1047 {
1048 VIsual_select = TRUE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01001049 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 showmode();
1051 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001052 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001054 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 (void)edit(restart_edit, FALSE, 1L);
1056 }
1057
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 if (restart_VIsual_select == 2)
1059 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001061 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 opcount = ca.opcount;
1063}
1064
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001065#ifdef FEAT_EVAL
1066/*
1067 * Set v:count and v:count1 according to "cap".
1068 * Set v:prevcount only when "set_prevcount" is TRUE.
1069 */
1070 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001071set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001072{
1073 long count = cap->count0;
1074
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001075 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001076 if (cap->opcount != 0)
1077 count = cap->opcount * (count == 0 ? 1 : count);
1078 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001079 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001080}
1081#endif
1082
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001084 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 * if not.
1086 */
1087 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001088check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089{
1090 static int did_check = FALSE;
1091
1092 if (full_screen)
1093 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001094 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001095 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 did_check = TRUE;
1097 }
1098}
1099
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001100#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1101/*
1102 * Call yank_do_autocmd() for "regname".
1103 */
1104 static void
1105call_yank_do_autocmd(int regname)
1106{
1107 oparg_T oa;
1108 yankreg_T *reg;
1109
1110 clear_oparg(&oa);
1111 oa.regname = regname;
1112 oa.op_type = OP_YANK;
1113 oa.is_VIsual = TRUE;
1114 reg = get_register(regname, TRUE);
1115 yank_do_autocmd(&oa, reg);
1116 free_register(reg);
1117}
1118#endif
1119
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001121 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001122 * This function or the next should ALWAYS be called to end Visual mode, except
1123 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 */
1125 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001126end_visual_mode(void)
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001127{
1128 end_visual_mode_keep_button();
1129 reset_held_button();
1130}
1131
1132 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001133end_visual_mode_keep_button(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134{
1135#ifdef FEAT_CLIPBOARD
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001136 // If we are using the clipboard, then remember what was selected in case
1137 // we need to paste it somewhere while we still own the selection.
1138 // Only do this when the clipboard is already owned. Don't want to grab
1139 // the selection when hitting ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 if (clip_star.available && clip_star.owned)
1141 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001142
1143# if defined(FEAT_EVAL)
1144 // Emit a TextYankPost for the automatic copy of the selection into the
1145 // star and/or plus register.
1146 if (has_textyankpost())
1147 {
1148 if (clip_isautosel_star())
1149 call_yank_do_autocmd('*');
1150 if (clip_isautosel_plus())
1151 call_yank_do_autocmd('+');
1152 }
1153# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154#endif
1155
1156 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 setmouse();
1158 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001160 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001161 curbuf->b_visual.vi_mode = VIsual_mode;
1162 curbuf->b_visual.vi_start = VIsual;
1163 curbuf->b_visual.vi_end = curwin->w_cursor;
1164 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165#ifdef FEAT_EVAL
1166 curbuf->b_visual_mode_eval = VIsual_mode;
1167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 if (!virtual_active())
1169 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001170 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001172 adjust_cursor_eol();
LemonBoy2bf52dd2022-04-09 18:17:34 +01001173 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174}
1175
1176/*
1177 * Reset VIsual_active and VIsual_reselect.
1178 */
1179 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001180reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181{
1182 if (VIsual_active)
1183 {
1184 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001185 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 }
1187 VIsual_reselect = FALSE;
1188}
1189
1190/*
1191 * Reset VIsual_active and VIsual_reselect if it's set.
1192 */
1193 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001194reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195{
1196 if (VIsual_active)
1197 {
1198 end_visual_mode();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001199 redraw_curbuf_later(UPD_INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 VIsual_reselect = FALSE;
1201 }
1202}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001204 void
1205restore_visual_mode(void)
1206{
1207 if (VIsual_mode_orig != NUL)
1208 {
1209 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1210 VIsual_mode_orig = NUL;
1211 }
1212}
1213
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214/*
1215 * Check for a balloon-eval special item to include when searching for an
1216 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1217 * Returns TRUE if the character at "*ptr" should be included.
1218 * "dir" is FORWARD or BACKWARD, the direction of searching.
1219 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1220 * "bnp" points to a counter for square brackets.
1221 */
1222 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001223find_is_eval_item(
1224 char_u *ptr,
1225 int *colp,
1226 int *bnp,
1227 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001229 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1231 ++*bnp;
1232 if (*bnp > 0)
1233 {
1234 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1235 --*bnp;
1236 return TRUE;
1237 }
1238
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001239 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 if (*ptr == '.')
1241 return TRUE;
1242
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001243 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1245 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1246 {
1247 *colp += dir;
1248 return TRUE;
1249 }
1250 return FALSE;
1251}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252
1253/*
1254 * Find the identifier under or to the right of the cursor.
1255 * "find_type" can have one of three values:
1256 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001257 * FIND_STRING: find any non-white text
1258 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001259 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 *
1261 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001262 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001264 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 * This doesn't match the real Vi but I like it a little better and it
1266 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001267 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 * When FIND_IDENT isn't defined, we backup until a blank.
1269 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001270 * Returns the length of the text, or zero if no text is found.
1271 * If text is found, a pointer to the text is put in "*text". This
1272 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 */
1274 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001275find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276{
1277 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001278 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279}
1280
1281/*
1282 * Like find_ident_under_cursor(), but for any window and any position.
1283 * However: Uses 'iskeyword' from the current window!.
1284 */
1285 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001286find_ident_at_pos(
1287 win_T *wp,
1288 linenr_T lnum,
1289 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001290 char_u **text,
1291 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001292 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293{
1294 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001295 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 int this_class = 0;
1298 int prev_class;
1299 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001300 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001302 // if i == 0: try to find an identifier
1303 // if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1305 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1306 {
1307 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001308 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 */
1310 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 if (has_mbyte)
1312 {
1313 while (ptr[col] != NUL)
1314 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001315 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1317 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 this_class = mb_get_class(ptr + col);
1319 if (this_class != 0 && (i == 1 || this_class != 1))
1320 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001321 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 }
1323 }
1324 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001326 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 )
1329 ++col;
1330
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001331 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333
1334 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001335 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 if (has_mbyte)
1338 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001339 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1341 this_class = mb_get_class((char_u *)"a");
1342 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001344 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 {
1346 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1347 prev_class = mb_get_class(ptr + prevcol);
1348 if (this_class != prev_class
1349 && (i == 0
1350 || prev_class == 0
1351 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 && (!(find_type & FIND_EVAL)
1353 || prevcol == 0
1354 || !find_is_eval_item(ptr + prevcol, &prevcol,
1355 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 )
1357 break;
1358 col = prevcol;
1359 }
1360
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001361 // If we don't want just any old text, or we've found an
1362 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 if (this_class > 2)
1364 this_class = 2;
1365 if (!(find_type & FIND_STRING) || this_class == 2)
1366 break;
1367 }
1368 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 {
1370 while (col > 0
1371 && ((i == 0
1372 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001373 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 && (!(find_type & FIND_IDENT)
1375 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 || ((find_type & FIND_EVAL)
1377 && col > 1
1378 && find_is_eval_item(ptr + col - 1, &col,
1379 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 ))
1381 --col;
1382
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001383 // If we don't want just any old text, or we've found an
1384 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1386 break;
1387 }
1388 }
1389
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001390 if (ptr[col] == NUL || (i == 0
1391 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001393 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001394 if ((find_type & FIND_NOERROR) == 0)
1395 {
1396 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001397 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001398 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001399 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 return 0;
1402 }
1403 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001404 *text = ptr;
1405 if (textcol != NULL)
1406 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407
1408 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001409 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 bn = 0;
1412 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 if (has_mbyte)
1415 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001416 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 this_class = mb_get_class(ptr);
1418 while (ptr[col] != NUL
1419 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1420 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 || ((find_type & FIND_EVAL)
1422 && col <= (int)startcol
1423 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001425 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 }
1427 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001429 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 || ((find_type & FIND_EVAL)
1431 && col <= (int)startcol
1432 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435
1436 return col;
1437}
1438
1439/*
1440 * Prepare for redo of a normal command.
1441 */
1442 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001443prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444{
1445 prep_redo(cap->oap->regname, cap->count0,
1446 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1447}
1448
1449/*
1450 * Prepare for redo of any command.
1451 * Note that only the last argument can be a multi-byte char.
1452 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001453 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001454prep_redo(
1455 int regname,
1456 long num,
1457 int cmd1,
1458 int cmd2,
1459 int cmd3,
1460 int cmd4,
1461 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001463 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1464}
1465
1466/*
1467 * Prepare for redo of any command with extra count after "cmd2".
1468 */
1469 void
1470prep_redo_num2(
1471 int regname,
1472 long num1,
1473 int cmd1,
1474 int cmd2,
1475 long num2,
1476 int cmd3,
1477 int cmd4,
1478 int cmd5)
1479{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 ResetRedobuff();
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001481
1482#ifdef FEAT_EVAL
1483 // Put info about a mapping in the redo buffer, so that "." will use the
1484 // same script context.
1485 may_add_last_used_map_to_redobuff();
1486#endif
1487
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001488 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 {
1490 AppendCharToRedobuff('"');
1491 AppendCharToRedobuff(regname);
1492 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001493 if (num1 != 0)
1494 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 if (cmd1 != NUL)
1496 AppendCharToRedobuff(cmd1);
1497 if (cmd2 != NUL)
1498 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001499 if (num2 != 0)
1500 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 if (cmd3 != NUL)
1502 AppendCharToRedobuff(cmd3);
1503 if (cmd4 != NUL)
1504 AppendCharToRedobuff(cmd4);
1505 if (cmd5 != NUL)
1506 AppendCharToRedobuff(cmd5);
1507}
1508
1509/*
zeertzjq73916ba2023-04-26 16:50:19 +01001510 * Check for operator active and clear it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 *
zeertzjq73916ba2023-04-26 16:50:19 +01001512 * Beep and return TRUE if an operator was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 */
1514 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001515checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516{
1517 if (oap->op_type == OP_NOP)
1518 return FALSE;
1519 clearopbeep(oap);
1520 return TRUE;
1521}
1522
1523/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001524 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 *
zeertzjq73916ba2023-04-26 16:50:19 +01001526 * Beep and return TRUE if an operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 */
1528 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001529checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001531 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 return FALSE;
1533 clearopbeep(oap);
1534 return TRUE;
1535}
1536
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001537 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001538clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539{
1540 oap->op_type = OP_NOP;
1541 oap->regname = 0;
1542 oap->motion_force = NUL;
1543 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001544 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545}
1546
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001547 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001548clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
1550 clearop(oap);
1551 beep_flush();
1552}
1553
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554/*
1555 * Remove the shift modifier from a special key.
1556 */
1557 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001558unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559{
1560 switch (cap->cmdchar)
1561 {
1562 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1563 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1564 case K_S_UP: cap->cmdchar = K_UP; break;
1565 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1566 case K_S_HOME: cap->cmdchar = K_HOME; break;
1567 case K_S_END: cap->cmdchar = K_END; break;
1568 }
1569 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1570}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001572/*
1573 * If the mode is currently displayed clear the command line or update the
1574 * command displayed.
1575 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001576 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001577may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001578{
1579 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001580 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001581 else
1582 clear_showcmd();
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001583}
1584
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585/*
1586 * Routines for displaying a partly typed command
1587 */
1588
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001589static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590static int showcmd_is_clear = TRUE;
1591static int showcmd_visual = FALSE;
1592
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001593static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594
1595 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001596clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597{
1598 if (!p_sc)
1599 return;
1600
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 if (VIsual_active && !char_avail())
1602 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001603 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 long lines;
1605 colnr_T leftcol, rightcol;
1606 linenr_T top, bot;
1607
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001608 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001609 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
1611 top = VIsual.lnum;
1612 bot = curwin->w_cursor.lnum;
1613 }
1614 else
1615 {
1616 top = curwin->w_cursor.lnum;
1617 bot = VIsual.lnum;
1618 }
1619# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001620 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001621 (void)hasFolding(top, &top, NULL);
1622 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623# endif
1624 lines = bot - top + 1;
1625
1626 if (VIsual_mode == Ctrl_V)
1627 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001628# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001629 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001630 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001631
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001632 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001633 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001634 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001635# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001637# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001638 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001639 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001640# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1642 (long)(rightcol - leftcol + 1));
1643 }
1644 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1645 sprintf((char *)showcmd_buf, "%ld", lines);
1646 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001647 {
1648 char_u *s, *e;
1649 int l;
1650 int bytes = 0;
1651 int chars = 0;
1652
1653 if (cursor_bot)
1654 {
1655 s = ml_get_pos(&VIsual);
1656 e = ml_get_cursor();
1657 }
1658 else
1659 {
1660 s = ml_get_cursor();
1661 e = ml_get_pos(&VIsual);
1662 }
1663 while ((*p_sel != 'e') ? s <= e : s < e)
1664 {
1665 l = (*mb_ptr2len)(s);
1666 if (l == 0)
1667 {
1668 ++bytes;
1669 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001670 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001671 }
1672 bytes += l;
1673 ++chars;
1674 s += l;
1675 }
1676 if (bytes == chars)
1677 sprintf((char *)showcmd_buf, "%d", chars);
1678 else
1679 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1680 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001681 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 showcmd_visual = TRUE;
1683 }
1684 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 {
1686 showcmd_buf[0] = NUL;
1687 showcmd_visual = FALSE;
1688
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001689 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 if (showcmd_is_clear)
1691 return;
1692 }
1693
1694 display_showcmd();
1695}
1696
1697/*
1698 * Add 'c' to string of shown command chars.
1699 * Return TRUE if output has been written (and setcursor() has been called).
1700 */
1701 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001702add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703{
1704 char_u *p;
1705 int old_len;
1706 int extra_len;
1707 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 int i;
1709 static int ignore[] =
1710 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001711#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1713 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001714#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001715 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001716 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1718 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001719 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001721 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 0
1723 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724
Bram Moolenaar09df3122006-01-23 22:23:09 +00001725 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 return FALSE;
1727
1728 if (showcmd_visual)
1729 {
1730 showcmd_buf[0] = NUL;
1731 showcmd_visual = FALSE;
1732 }
1733
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001734 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 if (IS_SPECIAL(c))
1736 for (i = 0; ignore[i] != 0; ++i)
1737 if (ignore[i] == c)
1738 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
1740 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001741 if (*p == ' ')
1742 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 old_len = (int)STRLEN(showcmd_buf);
1744 extra_len = (int)STRLEN(p);
1745 overflow = old_len + extra_len - SHOWCMD_COLS;
1746 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001747 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1748 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 STRCAT(showcmd_buf, p);
1750
1751 if (char_avail())
1752 return FALSE;
1753
1754 display_showcmd();
1755
1756 return TRUE;
1757}
1758
1759 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001760add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761{
1762 if (!add_to_showcmd(c))
1763 setcursor();
1764}
1765
1766/*
1767 * Delete 'len' characters from the end of the shown command.
1768 */
1769 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001770del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771{
1772 int old_len;
1773
1774 if (!p_sc)
1775 return;
1776
1777 old_len = (int)STRLEN(showcmd_buf);
1778 if (len > old_len)
1779 len = old_len;
1780 showcmd_buf[old_len - len] = NUL;
1781
1782 if (!char_avail())
1783 display_showcmd();
1784}
1785
1786/*
1787 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1788 * something and there is a partial mapping.
1789 */
1790 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001791push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792{
1793 if (p_sc)
1794 STRCPY(old_showcmd_buf, showcmd_buf);
1795}
1796
1797 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001798pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799{
1800 if (!p_sc)
1801 return;
1802
1803 STRCPY(showcmd_buf, old_showcmd_buf);
1804
1805 display_showcmd();
1806}
1807
1808 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001809display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810{
Luuk van Baalba936f62022-12-15 13:15:39 +00001811 int len = (int)STRLEN(showcmd_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812
Luuk van Baalba936f62022-12-15 13:15:39 +00001813 showcmd_is_clear = (len == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 cursor_off();
1815
Luuk van Baalba936f62022-12-15 13:15:39 +00001816 if (*p_sloc == 's')
Luuk van Baalaa7f25e2023-04-13 21:47:38 +01001817 {
1818 if (showcmd_is_clear)
1819 curwin->w_redr_status = TRUE;
1820 else
1821 win_redr_status(curwin, FALSE);
1822 }
Luuk van Baalba936f62022-12-15 13:15:39 +00001823 else if (*p_sloc == 't')
Luuk van Baalaa7f25e2023-04-13 21:47:38 +01001824 {
1825 if (showcmd_is_clear)
1826 redraw_tabline = TRUE;
1827 else
1828 draw_tabline();
1829 }
Luuk van Baalba936f62022-12-15 13:15:39 +00001830 else // 'showcmdloc' is "last" or empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 {
Luuk van Baalba936f62022-12-15 13:15:39 +00001832 if (!showcmd_is_clear)
1833 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834
Luuk van Baalba936f62022-12-15 13:15:39 +00001835 // clear the rest of an old message by outputting up to SHOWCMD_COLS
1836 // spaces
1837 screen_puts((char_u *)" " + len,
1838 (int)Rows - 1, sc_col + len, 0);
1839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001841 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844/*
1845 * When "check" is FALSE, prepare for commands that scroll the window.
1846 * When "check" is TRUE, take care of scroll-binding after the window has
1847 * scrolled. Called from normal_cmd() and edit().
1848 */
1849 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001850do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851{
1852 static win_T *old_curwin = NULL;
1853 static linenr_T old_topline = 0;
1854#ifdef FEAT_DIFF
1855 static int old_topfill = 0;
1856#endif
1857 static buf_T *old_buf = NULL;
1858 static colnr_T old_leftcol = 0;
1859
1860 if (check && curwin->w_p_scb)
1861 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001862 // If a ":syncbind" command was just used, don't scroll, only reset
1863 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 if (did_syncbind)
1865 did_syncbind = FALSE;
1866 else if (curwin == old_curwin)
1867 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001868 // Synchronize other windows, as necessary according to
1869 // 'scrollbind'. Don't do this after an ":edit" command, except
1870 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 if ((curwin->w_buffer == old_buf
1872#ifdef FEAT_DIFF
1873 || curwin->w_p_diff
1874#endif
1875 )
1876 && (curwin->w_topline != old_topline
1877#ifdef FEAT_DIFF
1878 || curwin->w_topfill != old_topfill
1879#endif
1880 || curwin->w_leftcol != old_leftcol))
1881 {
1882 check_scrollbind(curwin->w_topline - old_topline,
1883 (long)(curwin->w_leftcol - old_leftcol));
1884 }
1885 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001886 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001888 // When switching between windows, make sure that the relative
1889 // vertical offset is valid for the new window. The relative
1890 // offset is invalid whenever another 'scrollbind' window has
1891 // scrolled to a point that would force the current window to
1892 // scroll past the beginning or end of its buffer. When the
1893 // resync is performed, some of the other 'scrollbind' windows may
1894 // need to jump so that the current window's relative position is
1895 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
1897 }
1898 curwin->w_scbind_pos = curwin->w_topline;
1899 }
1900
1901 old_curwin = curwin;
1902 old_topline = curwin->w_topline;
1903#ifdef FEAT_DIFF
1904 old_topfill = curwin->w_topfill;
1905#endif
1906 old_buf = curwin->w_buffer;
1907 old_leftcol = curwin->w_leftcol;
1908}
1909
1910/*
1911 * Synchronize any windows that have "scrollbind" set, based on the
1912 * number of rows by which the current window has changed
1913 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
1914 */
1915 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001916check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917{
1918 int want_ver;
1919 int want_hor;
1920 win_T *old_curwin = curwin;
1921 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 int old_VIsual_select = VIsual_select;
1923 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 colnr_T tgt_leftcol = curwin->w_leftcol;
1925 long topline;
1926 long y;
1927
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001928 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
1930#ifdef FEAT_DIFF
1931 want_ver |= old_curwin->w_p_diff;
1932#endif
1933 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
1934
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001935 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02001937 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 {
1939 curbuf = curwin->w_buffer;
zeertzjq101d57b2022-07-31 18:34:32 +01001940 // skip original window and windows with 'noscrollbind'
1941 if (curwin == old_curwin || !curwin->w_p_scb)
1942 continue;
1943
1944 // do the vertical scroll
1945 if (want_ver)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947#ifdef FEAT_DIFF
zeertzjq101d57b2022-07-31 18:34:32 +01001948 if (old_curwin->w_p_diff && curwin->w_p_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 {
zeertzjq101d57b2022-07-31 18:34:32 +01001950 diff_set_topline(old_curwin, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 }
zeertzjq101d57b2022-07-31 18:34:32 +01001952 else
1953#endif
1954 {
1955 curwin->w_scbind_pos += topline_diff;
1956 topline = curwin->w_scbind_pos;
1957 if (topline > curbuf->b_ml.ml_line_count)
1958 topline = curbuf->b_ml.ml_line_count;
1959 if (topline < 1)
1960 topline = 1;
1961
1962 y = topline - curwin->w_topline;
1963 if (y > 0)
1964 scrollup(y, FALSE);
1965 else
1966 scrolldown(-y, FALSE);
1967 }
1968
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001969 redraw_later(UPD_VALID);
zeertzjq101d57b2022-07-31 18:34:32 +01001970 cursor_correct();
1971 curwin->w_redr_status = TRUE;
1972 }
1973
1974 // do the horizontal scroll
Bram Moolenaar0c34d562022-11-18 14:07:20 +00001975 if (want_hor)
1976 (void)set_leftcol(tgt_leftcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 }
1978
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001979 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 VIsual_select = old_VIsual_select;
1981 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 curwin = old_curwin;
1983 curbuf = old_curbuf;
1984}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985
1986/*
1987 * Command character that's ignored.
1988 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02001989 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001992nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001994 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995}
1996
1997/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00001998 * Command character that doesn't do anything, but unlike nv_ignore() does
1999 * start edit(). Used for "startinsert" executed while starting up.
2000 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002001 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002002nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002003{
2004}
2005
2006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 * Command character doesn't exist.
2008 */
2009 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002010nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011{
2012 clearopbeep(cap->oap);
2013}
2014
2015/*
2016 * <Help> and <F1> commands.
2017 */
2018 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002019nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020{
2021 if (!checkclearopq(cap->oap))
2022 ex_help(NULL);
2023}
2024
2025/*
2026 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2027 */
2028 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002029nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002031#ifdef FEAT_JOB_CHANNEL
2032 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2033 clearopbeep(cap->oap);
2034 else
2035#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002036 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002037 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002038 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002039 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2040 op_addsub(cap->oap, cap->count1, cap->arg);
2041 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002042 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002043 else if (VIsual_active)
2044 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002045 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002046 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047}
2048
2049/*
2050 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2051 */
2052 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002053nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002055 if (checkclearop(cap->oap))
2056 return;
2057
2058 if (mod_mask & MOD_MASK_CTRL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002059 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002060 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
2061 if (cap->arg == BACKWARD)
2062 goto_tabpage(-(int)cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002063 else
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002064 goto_tabpage((int)cap->count0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002065 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002066 else
2067 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068}
2069
2070/*
2071 * Implementation of "gd" and "gD" command.
2072 */
2073 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002074nv_gd(
2075 oparg_T *oap,
2076 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002077 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078{
2079 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 char_u *ptr;
2081
Bram Moolenaard9d30582005-05-18 22:10:28 +00002082 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002083 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002084 == FAIL)
2085 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 clearopbeep(oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002087 return;
Bram Moolenaar0c711142021-11-12 10:30:04 +00002088 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002089
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002090#ifdef FEAT_FOLDING
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002091 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2092 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002093#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002094 // clear any search statistics
2095 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2096 clear_cmdline = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002097}
2098
2099/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002100 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2101 * otherwise.
2102 */
2103 static int
2104is_ident(char_u *line, int offset)
2105{
2106 int i;
2107 int incomment = FALSE;
2108 int instring = 0;
2109 int prev = 0;
2110
2111 for (i = 0; i < offset && line[i] != NUL; i++)
2112 {
2113 if (instring != 0)
2114 {
2115 if (prev != '\\' && line[i] == instring)
2116 instring = 0;
2117 }
2118 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2119 {
2120 instring = line[i];
2121 }
2122 else
2123 {
2124 if (incomment)
2125 {
2126 if (prev == '*' && line[i] == '/')
2127 incomment = FALSE;
2128 }
2129 else if (prev == '/' && line[i] == '*')
2130 {
2131 incomment = TRUE;
2132 }
2133 else if (prev == '/' && line[i] == '/')
2134 {
2135 return FALSE;
2136 }
2137 }
2138
2139 prev = line[i];
2140 }
2141
2142 return incomment == FALSE && instring == 0;
2143}
2144
2145/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002146 * Search for variable declaration of "ptr[len]".
2147 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2148 * current file ("gD").
2149 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002150 * Return FAIL when not found.
2151 */
2152 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002153find_decl(
2154 char_u *ptr,
2155 int len,
2156 int locally,
2157 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002158 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002159{
2160 char_u *pat;
2161 pos_T old_pos;
2162 pos_T par_pos;
2163 pos_T found_pos;
2164 int t;
2165 int save_p_ws;
2166 int save_p_scs;
2167 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002168 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002169 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002170 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002171
2172 if ((pat = alloc(len + 7)) == NULL)
2173 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002174
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002175 // Put "\V" before the pattern to avoid that the special meaning of "."
2176 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002177 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2178 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 old_pos = curwin->w_cursor;
2180 save_p_ws = p_ws;
2181 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002182 p_ws = FALSE; // don't wrap around end of file now
2183 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002185 // With "gD" go to line 1.
2186 // With "gd" Search back for the start of the current function, then go
2187 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002188 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002190 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002192 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 }
2194 else
2195 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002196 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2198 --curwin->w_cursor.lnum;
2199 }
2200 curwin->w_cursor.col = 0;
2201
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002202 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002203 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002204 for (;;)
2205 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002206 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002207 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002208 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002209 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002210
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002211 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002212 {
2213 pos_T *pos;
2214
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002215 // Check that the block the match is in doesn't end before the
2216 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002217 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2218 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2219 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002220 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002221 // There can't be a useful match before the end of this block.
2222 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002223 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002224 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002225 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002226 }
2227
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002228 if (t == FAIL)
2229 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002230 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002231 if (found_pos.lnum != 0)
2232 {
2233 curwin->w_cursor = found_pos;
2234 t = OK;
2235 }
2236 break;
2237 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002238 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002239 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002240 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002241 ++curwin->w_cursor.lnum;
2242 curwin->w_cursor.col = 0;
2243 continue;
2244 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002245 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2246
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002247 // If the current position is not a valid identifier and a previous
2248 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002249 if (!valid && found_pos.lnum != 0)
2250 {
2251 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002252 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002253 }
2254
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002255 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002256 if (valid && !locally)
2257 break;
2258 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002259 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002260 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002261 if (found_pos.lnum != 0)
2262 curwin->w_cursor = found_pos;
2263 break;
2264 }
2265
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002266 // For finding a local variable and the match is before the "{" or
2267 // inside a comment, continue searching. For K&R style function
2268 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002269 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002270 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002271 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002272 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002273 // Remove SEARCH_START from flags to avoid getting stuck at one
2274 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002275 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002277
2278 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002280 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 curwin->w_cursor = old_pos;
2282 }
2283 else
2284 {
2285 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002286 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 reset_search_dir();
2288 }
2289
2290 vim_free(pat);
2291 p_ws = save_p_ws;
2292 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002293
2294 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295}
2296
2297/*
2298 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2299 * lines rather than lines in the file.
2300 * 'dist' must be positive.
2301 *
2302 * Return OK if able to move cursor, FAIL otherwise.
2303 */
2304 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002305nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306{
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002307 int linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 int retval = OK;
2309 int atend = FALSE;
2310 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002311 int col_off1; // margin offset for first screen line
2312 int col_off2; // margin offset for wrapped screen line
2313 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002314 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315
2316 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002317 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318
2319 col_off1 = curwin_col_off();
2320 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002321 width1 = curwin->w_width - col_off1;
2322 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002323 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002324 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002327 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002328 // Instead of sticking at the last character of the buffer line we
2329 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 if (curwin->w_curswant == MAXCOL)
2331 {
2332 atend = TRUE;
2333 validate_virtcol();
2334 if (width1 <= 0)
2335 curwin->w_curswant = 0;
2336 else
2337 {
2338 curwin->w_curswant = width1 - 1;
2339 if (curwin->w_virtcol > curwin->w_curswant)
2340 curwin->w_curswant += ((curwin->w_virtcol
2341 - curwin->w_curswant - 1) / width2 + 1) * width2;
2342 }
2343 }
2344 else
2345 {
2346 if (linelen > width1)
2347 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2348 else
2349 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002350 if (curwin->w_curswant >= (colnr_T)n)
2351 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 }
2353
2354 while (dist--)
2355 {
2356 if (dir == BACKWARD)
2357 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002358 if ((long)curwin->w_curswant >= width1
2359#ifdef FEAT_FOLDING
2360 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2361#endif
2362 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002363 // Move back within the line. This can give a negative value
2364 // for w_curswant if width1 < width2 (with cpoptions+=n),
2365 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 curwin->w_curswant -= width2;
2367 else
2368 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002369 // to previous line
Luuk van Baala109f392023-06-02 14:16:35 +01002370 if (curwin->w_cursor.lnum <= 1)
Bram Moolenaare71996b2021-01-21 17:03:07 +01002371 {
2372 retval = FAIL;
2373 break;
2374 }
Luuk van Baala109f392023-06-02 14:16:35 +01002375 cursor_up_inner(curwin, 1);
2376
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002377 linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 if (linelen > width1)
2379 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2380 + 1) * width2;
2381 }
2382 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002383 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 {
2385 if (linelen > width1)
2386 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2387 else
2388 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002389 if (curwin->w_curswant + width2 < (colnr_T)n
2390#ifdef FEAT_FOLDING
2391 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2392#endif
2393 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002394 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 curwin->w_curswant += width2;
2396 else
2397 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002398 // to next line
Luuk van Baala109f392023-06-02 14:16:35 +01002399 if (curwin->w_cursor.lnum
2400 >= curwin->w_buffer->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 {
2402 retval = FAIL;
2403 break;
2404 }
Luuk van Baala109f392023-06-02 14:16:35 +01002405 cursor_down_inner(curwin, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 curwin->w_curswant %= width2;
Luuk van Baala109f392023-06-02 14:16:35 +01002407
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002408 // Check if the cursor has moved below the number display
2409 // when width1 < width2 (with cpoptions+=n). Subtract width2
2410 // to get a negative value for w_curswant, which will get
2411 // clipped to column 0.
2412 if (curwin->w_curswant >= width1)
2413 curwin->w_curswant -= width2;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01002414 linelen = linetabsize_str(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 }
2416 }
2417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002420 if (virtual_active() && atend)
2421 coladvance(MAXCOL);
2422 else
2423 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2426 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002427 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002428 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002429
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002430 // Check for landing on a character that got split at the end of the
2431 // last line. We want to advance a screenline, not end up in the same
2432 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002434 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002435#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002436 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2437 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002438#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002439
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002440 c = (*mb_ptr2char)(ml_get_cursor());
2441 if (dir == FORWARD && virtcol < curwin->w_curswant
2442 && (curwin->w_curswant <= (colnr_T)width1)
2443 && !vim_isprintc(c) && c > 255)
2444 oneright();
2445
Bram Moolenaar773b1582014-08-29 14:20:51 +02002446 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 && (curwin->w_curswant < (colnr_T)width1
2448 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2449 : ((curwin->w_curswant - width1) % width2
2450 > (colnr_T)width2 / 2)))
2451 --curwin->w_cursor.col;
2452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453
2454 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002455 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar2fbabd22022-10-12 19:53:38 +01002456 adjust_skipcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457
2458 return retval;
2459}
2460
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461/*
2462 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2463 * cap->arg must be TRUE for CTRL-E.
2464 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002465 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002466nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467{
2468 if (!checkclearop(cap->oap))
2469 scroll_redraw(cap->arg, cap->count1);
2470}
2471
2472/*
2473 * Scroll "count" lines up or down, and redraw.
2474 */
2475 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002476scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477{
2478 linenr_T prev_topline = curwin->w_topline;
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002479 int prev_skipcol = curwin->w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480#ifdef FEAT_DIFF
2481 int prev_topfill = curwin->w_topfill;
2482#endif
2483 linenr_T prev_lnum = curwin->w_cursor.lnum;
2484
2485 if (up)
2486 scrollup(count, TRUE);
2487 else
2488 scrolldown(count, TRUE);
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002489 if (get_scrolloff_value() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002491 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2492 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 cursor_correct();
2494 check_cursor_moved(curwin);
2495 curwin->w_valid |= VALID_TOPLINE;
2496
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002497 // If moved back to where we were, at least move the cursor, otherwise
2498 // we get stuck at one position. Don't move the cursor up if the
2499 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 while (curwin->w_topline == prev_topline
Bram Moolenaarb34c4b72022-10-02 22:32:08 +01002501 && curwin->w_skipcol == prev_skipcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502#ifdef FEAT_DIFF
2503 && curwin->w_topfill == prev_topfill
2504#endif
2505 )
2506 {
2507 if (up)
2508 {
2509 if (curwin->w_cursor.lnum > prev_lnum
2510 || cursor_down(1L, FALSE) == FAIL)
2511 break;
2512 }
2513 else
2514 {
2515 if (curwin->w_cursor.lnum < prev_lnum
2516 || prev_topline == 1L
2517 || cursor_up(1L, FALSE) == FAIL)
2518 break;
2519 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002520 // Mark w_topline as valid, otherwise the screen jumps back at the
2521 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 check_cursor_moved(curwin);
2523 curwin->w_valid |= VALID_TOPLINE;
2524 }
2525 }
2526 if (curwin->w_cursor.lnum != prev_lnum)
2527 coladvance(curwin->w_curswant);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002528 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529}
2530
2531/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002532 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2533 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2534 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002535 */
2536 static int
2537nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2538{
2539 int nchar = *nchar_arg;
2540 long n;
2541
2542 // "z123{nchar}": edit the count before obtaining {nchar}
2543 if (checkclearop(cap->oap))
2544 return FALSE;
2545 n = nchar - '0';
2546
2547 for (;;)
2548 {
2549#ifdef USE_ON_FLY_SCROLL
2550 dont_scroll = TRUE; // disallow scrolling here
2551#endif
2552 ++no_mapping;
2553 ++allow_keys; // no mapping for nchar, but allow key codes
2554 nchar = plain_vgetc();
2555 LANGMAP_ADJUST(nchar, TRUE);
2556 --no_mapping;
2557 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002558 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002559
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002560 if (nchar == K_DEL || nchar == K_KDEL)
2561 n /= 10;
2562 else if (VIM_ISDIGIT(nchar))
2563 n = n * 10 + (nchar - '0');
2564 else if (nchar == CAR)
2565 {
2566#ifdef FEAT_GUI
2567 need_mouse_correct = TRUE;
2568#endif
2569 win_setheight((int)n);
2570 break;
2571 }
2572 else if (nchar == 'l'
2573 || nchar == 'h'
2574 || nchar == K_LEFT
2575 || nchar == K_RIGHT)
2576 {
2577 cap->count1 = n ? n * cap->count1 : cap->count1;
2578 *nchar_arg = nchar;
2579 return TRUE;
2580 }
2581 else
2582 {
2583 clearopbeep(cap->oap);
2584 break;
2585 }
2586 }
2587 cap->oap->op_type = OP_NOP;
2588 return FALSE;
2589}
2590
2591#ifdef FEAT_SPELL
2592/*
2593 * "zug" and "zuw": undo "zg" and "zw"
2594 * "zg": add good word to word list
2595 * "zw": add wrong word to word list
2596 * "zG": add good word to temp word list
2597 * "zW": add wrong word to temp word list
2598 */
2599 static int
2600nv_zg_zw(cmdarg_T *cap, int nchar)
2601{
2602 char_u *ptr = NULL;
2603 int len;
2604 int undo = FALSE;
2605
2606 if (nchar == 'u')
2607 {
2608 ++no_mapping;
2609 ++allow_keys; // no mapping for nchar, but allow key codes
2610 nchar = plain_vgetc();
2611 LANGMAP_ADJUST(nchar, TRUE);
2612 --no_mapping;
2613 --allow_keys;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002614 (void)add_to_showcmd(nchar);
Martin Tournoijba43e762022-10-13 22:12:15 +01002615
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002616 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2617 {
2618 clearopbeep(cap->oap);
2619 return OK;
2620 }
2621 undo = TRUE;
2622 }
2623
2624 if (checkclearop(cap->oap))
2625 return OK;
2626 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2627 return FAIL;
2628 if (ptr == NULL)
2629 {
2630 pos_T pos = curwin->w_cursor;
2631
2632 // Find bad word under the cursor. When 'spell' is
2633 // off this fails and find_ident_under_cursor() is
2634 // used below.
2635 emsg_off++;
2636 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2637 emsg_off--;
2638 if (len != 0 && curwin->w_cursor.col <= pos.col)
2639 ptr = ml_get_pos(&curwin->w_cursor);
2640 curwin->w_cursor = pos;
2641 }
2642
2643 if (ptr == NULL
2644 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2645 return FAIL;
2646 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2647 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2648 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2649
2650 return OK;
2651}
2652#endif
2653
2654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 * Commands that start with "z".
2656 */
2657 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002658nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659{
2660 long n;
2661 colnr_T col;
2662 int nchar = cap->nchar;
2663#ifdef FEAT_FOLDING
2664 long old_fdl = curwin->w_p_fdl;
2665 int old_fen = curwin->w_p_fen;
2666#endif
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002667 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002669 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 if (
2673#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002674 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2675 // and "zC" only in Visual mode. "zj" and "zk" are motion
2676 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 cap->nchar != 'f' && cap->nchar != 'F'
2678 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2679 && cap->nchar != 'j' && cap->nchar != 'k'
2680 &&
2681#endif
2682 checkclearop(cap->oap))
2683 return;
2684
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002685 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2686 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2688 && cap->count0
2689 && cap->count0 != curwin->w_cursor.lnum)
2690 {
2691 setpcmark();
2692 if (cap->count0 > curbuf->b_ml.ml_line_count)
2693 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2694 else
2695 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002696 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 }
2698
2699 switch (nchar)
2700 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002701 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 case '+':
2703 if (cap->count0 == 0)
2704 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002705 // No count given: put cursor at the line below screen
2706 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2708 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2709 else
2710 curwin->w_cursor.lnum = curwin->w_botline;
2711 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002712 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 case NL:
2714 case CAR:
2715 case K_KENTER:
2716 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002717 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718
2719 case 't': scroll_cursor_top(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002720 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002721 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 break;
2723
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002724 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002726 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002728 case 'z': scroll_cursor_halfway(TRUE, FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002729 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002730 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 break;
2732
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002733 // "z^", "z-" and "zb": put cursor at bottom of screen
2734 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2735 // when <count> is at bottom of window, and puts that one at
2736 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 if (cap->count0 != 0)
2738 {
2739 scroll_cursor_bot(0, TRUE);
2740 curwin->w_cursor.lnum = curwin->w_topline;
2741 }
2742 else if (curwin->w_topline == 1)
2743 curwin->w_cursor.lnum = 1;
2744 else
2745 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002746 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 case '-':
2748 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002749 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750
2751 case 'b': scroll_cursor_bot(0, TRUE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002752 redraw_later(UPD_VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002753 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 break;
2755
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002756 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002758 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002759 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002761 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 case 'h':
2763 case K_LEFT:
2764 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002765 (void)set_leftcol((colnr_T)cap->count1 > curwin->w_leftcol
2766 ? 0 : curwin->w_leftcol - (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 break;
2768
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002769 // "zL" - scroll window left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002770 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002771 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002773 // "zl" - scroll window to the left if not wrapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 case 'l':
2775 case K_RIGHT:
2776 if (!curwin->w_p_wrap)
Bram Moolenaar0c34d562022-11-18 14:07:20 +00002777 (void)set_leftcol(curwin->w_leftcol + (colnr_T)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 break;
2779
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002780 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 case 's': if (!curwin->w_p_wrap)
2782 {
2783#ifdef FEAT_FOLDING
2784 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002785 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 else
2787#endif
2788 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002789 if ((long)col > siso)
2790 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 else
2792 col = 0;
2793 if (curwin->w_leftcol != col)
2794 {
2795 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002796 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 }
2798 }
2799 break;
2800
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002801 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 case 'e': if (!curwin->w_p_wrap)
2803 {
2804#ifdef FEAT_FOLDING
2805 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002806 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 else
2808#endif
2809 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002810 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002811 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 col = 0;
2813 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002814 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 if (curwin->w_leftcol != col)
2816 {
2817 curwin->w_leftcol = col;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002818 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 }
2820 }
2821 break;
2822
Christian Brabandt2fa93842021-05-30 22:17:25 +02002823 // "zp", "zP" in block mode put without addind trailing spaces
2824 case 'P':
2825 case 'p': nv_put(cap);
2826 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002827 // "zy" Yank without trailing spaces
2828 case 'y': nv_operator(cap);
2829 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002831 // "zF": create fold command
2832 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 case 'F':
2834 case 'f': if (foldManualAllowed(TRUE))
2835 {
2836 cap->nchar = 'f';
2837 nv_operator(cap);
2838 curwin->w_p_fen = TRUE;
2839
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002840 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2842 {
2843 nv_operator(cap);
2844 finish_op = TRUE;
2845 }
2846 }
2847 else
2848 clearopbeep(cap->oap);
2849 break;
2850
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002851 // "zd": delete fold at cursor
2852 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 case 'd':
2854 case 'D': if (foldManualAllowed(FALSE))
2855 {
2856 if (VIsual_active)
2857 nv_operator(cap);
2858 else
2859 deleteFold(curwin->w_cursor.lnum,
2860 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2861 }
2862 break;
2863
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002864 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 case 'E': if (foldmethodIsManual(curwin))
2866 {
2867 clearFolding(curwin);
2868 changed_window_setting();
2869 }
2870 else if (foldmethodIsMarker(curwin))
2871 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2872 TRUE, FALSE);
2873 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00002874 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 break;
2876
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002877 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 case 'n': curwin->w_p_fen = FALSE;
2879 break;
2880
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002881 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 case 'N': curwin->w_p_fen = TRUE;
2883 break;
2884
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002885 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2887 break;
2888
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002889 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2891 openFold(curwin->w_cursor.lnum, cap->count1);
2892 else
2893 {
2894 closeFold(curwin->w_cursor.lnum, cap->count1);
2895 curwin->w_p_fen = TRUE;
2896 }
2897 break;
2898
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002899 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2901 openFoldRecurse(curwin->w_cursor.lnum);
2902 else
2903 {
2904 closeFoldRecurse(curwin->w_cursor.lnum);
2905 curwin->w_p_fen = TRUE;
2906 }
2907 break;
2908
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002909 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 case 'o': if (VIsual_active)
2911 nv_operator(cap);
2912 else
2913 openFold(curwin->w_cursor.lnum, cap->count1);
2914 break;
2915
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002916 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 case 'O': if (VIsual_active)
2918 nv_operator(cap);
2919 else
2920 openFoldRecurse(curwin->w_cursor.lnum);
2921 break;
2922
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002923 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 case 'c': if (VIsual_active)
2925 nv_operator(cap);
2926 else
2927 closeFold(curwin->w_cursor.lnum, cap->count1);
2928 curwin->w_p_fen = TRUE;
2929 break;
2930
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002931 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 case 'C': if (VIsual_active)
2933 nv_operator(cap);
2934 else
2935 closeFoldRecurse(curwin->w_cursor.lnum);
2936 curwin->w_p_fen = TRUE;
2937 break;
2938
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002939 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 case 'v': foldOpenCursor();
2941 break;
2942
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002943 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002945 curwin->w_foldinvalid = TRUE; // recompute folds
2946 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 foldOpenCursor();
2948 break;
2949
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002950 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002952 curwin->w_foldinvalid = TRUE; // recompute folds
2953 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 break;
2955
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002956 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002958 {
2959 curwin->w_p_fdl -= cap->count1;
2960 if (curwin->w_p_fdl < 0)
2961 curwin->w_p_fdl = 0;
2962 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002963 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 curwin->w_p_fen = TRUE;
2965 break;
2966
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002967 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002969 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 curwin->w_p_fen = TRUE;
2971 break;
2972
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002973 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02002974 case 'r': curwin->w_p_fdl += cap->count1;
2975 {
2976 int d = getDeepestNesting();
2977
2978 if (curwin->w_p_fdl >= d)
2979 curwin->w_p_fdl = d;
2980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 break;
2982
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002983 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002985 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 break;
2987
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002988 case 'j': // "zj" move to next fold downwards
2989 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
2991 cap->count1) == FAIL)
2992 clearopbeep(cap->oap);
2993 break;
2994
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002995#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002997#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002998 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002999 case 'g': // "zg": add good word to word list
3000 case 'w': // "zw": add wrong word to word list
3001 case 'G': // "zG": add good word to temp word list
3002 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003003 if (nv_zg_zw(cap, nchar) == FAIL)
3004 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003005 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003006
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003007 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003008 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003009 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003010 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003011#endif
3012
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 default: clearopbeep(cap->oap);
3014 }
3015
3016#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003017 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 if (old_fen != curwin->w_p_fen)
3019 {
3020# ifdef FEAT_DIFF
3021 win_T *wp;
3022
3023 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3024 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003025 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 FOR_ALL_WINDOWS(wp)
3027 {
3028 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3029 {
3030 wp->w_p_fen = curwin->w_p_fen;
3031 changed_window_setting_win(wp);
3032 }
3033 }
3034 }
3035# endif
3036 changed_window_setting();
3037 }
3038
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003039 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 if (old_fdl != curwin->w_p_fdl)
3041 newFoldLevel();
3042#endif
3043}
3044
3045#ifdef FEAT_GUI
3046/*
3047 * Vertical scrollbar movement.
3048 */
3049 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003050nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051{
3052 if (cap->oap->op_type != OP_NOP)
3053 clearopbeep(cap->oap);
3054
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003055 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 gui_do_scroll();
3057}
3058
3059/*
3060 * Horizontal scrollbar movement.
3061 */
3062 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003063nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064{
3065 if (cap->oap->op_type != OP_NOP)
3066 clearopbeep(cap->oap);
3067
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003068 // Even if an operator was pending, we still want to scroll
Christopher Plewright44c22092022-11-15 17:43:36 +00003069 do_mousescroll_horiz(scrollbar_value);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070}
3071#endif
3072
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003073#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003074/*
3075 * Click in GUI tab.
3076 */
3077 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003078nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003079{
3080 if (cap->oap->op_type != OP_NOP)
3081 clearopbeep(cap->oap);
3082
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003083 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003084 goto_tabpage(current_tab);
3085}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003086
3087/*
3088 * Selected item in tab line menu.
3089 */
3090 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003091nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003092{
3093 if (cap->oap->op_type != OP_NOP)
3094 clearopbeep(cap->oap);
3095
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003096 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003097 handle_tabmenu();
3098}
3099
3100/*
3101 * Handle selecting an item of the GUI tab line menu.
3102 * Used in Normal and Insert mode.
3103 */
3104 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003105handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003106{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003107 switch (current_tabmenu)
3108 {
3109 case TABLINE_MENU_CLOSE:
3110 if (current_tab == 0)
3111 do_cmdline_cmd((char_u *)"tabclose");
3112 else
3113 {
3114 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3115 current_tab);
3116 do_cmdline_cmd(IObuff);
3117 }
3118 break;
3119
3120 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003121 if (current_tab == 0)
3122 do_cmdline_cmd((char_u *)"$tabnew");
3123 else
3124 {
3125 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3126 current_tab - 1);
3127 do_cmdline_cmd(IObuff);
3128 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003129 break;
3130
3131 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003132 if (current_tab == 0)
3133 do_cmdline_cmd((char_u *)"browse $tabnew");
3134 else
3135 {
3136 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3137 current_tab - 1);
3138 do_cmdline_cmd(IObuff);
3139 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003140 break;
3141 }
3142}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003143#endif
3144
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145/*
3146 * "Q" command.
3147 */
3148 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003149nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003151 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003153 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003154 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 do_exmode(FALSE);
3156}
3157
3158/*
3159 * Handle a ":" command.
3160 */
3161 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003162nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003164 int old_p_im;
3165 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003166 int is_cmdkey = cap->cmdchar == K_COMMAND
3167 || cap->cmdchar == K_SCRIPT_COMMAND;
3168 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169
Bram Moolenaar957cf672020-11-12 14:21:06 +01003170 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003172 nv_operator(cap);
3173 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003175
3176 if (cap->oap->op_type != OP_NOP)
3177 {
3178 // Using ":" as a movement is characterwise exclusive.
3179 cap->oap->motion_type = MCHAR;
3180 cap->oap->inclusive = FALSE;
3181 }
3182 else if (cap->count0 && !is_cmdkey)
3183 {
3184 // translate "count:" into ":.,.+(count - 1)"
3185 stuffcharReadbuff('.');
3186 if (cap->count0 > 1)
3187 {
3188 stuffReadbuff((char_u *)",.+");
3189 stuffnumReadbuff((long)cap->count0 - 1L);
3190 }
3191 }
3192
3193 // When typing, don't type below an old message
3194 if (KeyTyped)
3195 compute_cmdrow();
3196
3197 old_p_im = p_im;
3198
3199 // get a command line and execute it
3200 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3201 if (is_cmdkey)
3202 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3203 else
3204 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
3205
3206 // If 'insertmode' changed, enter or exit Insert mode
3207 if (p_im != old_p_im)
3208 {
3209 if (p_im)
3210 restart_edit = 'i';
3211 else
3212 restart_edit = 0;
3213 }
3214
3215 if (cmd_result == FAIL)
3216 // The Ex command failed, do not execute the operator.
3217 clearop(cap->oap);
3218 else if (cap->oap->op_type != OP_NOP
3219 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3220 || cap->oap->start.col >
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003221 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003222 || did_emsg
3223 ))
3224 // The start of the operator has become invalid by the Ex command.
3225 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226}
3227
3228/*
3229 * Handle CTRL-G command.
3230 */
3231 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003232nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003234 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 {
3236 VIsual_select = !VIsual_select;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003237 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 showmode();
3239 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003240 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003241 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 fileinfo((int)cap->count0, FALSE, TRUE);
3243}
3244
3245/*
3246 * Handle CTRL-H <Backspace> command.
3247 */
3248 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003249nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 if (VIsual_active && VIsual_select)
3252 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003253 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 v_visop(cap);
3255 }
3256 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 nv_left(cap);
3258}
3259
3260/*
3261 * CTRL-L: clear screen and redraw.
3262 */
3263 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003264nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003266 if (checkclearop(cap->oap))
3267 return;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003268
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003269#ifdef FEAT_SYN_HL
3270 // Clear all syntax states to force resyncing.
3271 syn_stack_free_all(curwin->w_s);
3272# ifdef FEAT_RELTIME
3273 {
3274 win_T *wp;
3275
3276 FOR_ALL_WINDOWS(wp)
3277 wp->w_s->b_syn_slow = FALSE;
3278 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003279# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003281 redraw_later(UPD_CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003282#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3283# ifdef VIMDLL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003284 if (!gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003285# endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003286 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288}
3289
3290/*
3291 * CTRL-O: In Select mode: switch to Visual mode for one command.
3292 * Otherwise: Go to older pcmark.
3293 */
3294 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003295nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 if (VIsual_active && VIsual_select)
3298 {
3299 VIsual_select = FALSE;
LemonBoy2bf52dd2022-04-09 18:17:34 +01003300 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003302 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 }
3304 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 {
3306 cap->count1 = -cap->count1;
3307 nv_pcmark(cap);
3308 }
3309}
3310
3311/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003312 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3313 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 */
3315 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003316nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317{
3318 if (!checkclearopq(cap->oap))
3319 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3320 GETF_SETMARK|GETF_ALT, FALSE);
3321}
3322
3323/*
3324 * "Z" commands.
3325 */
3326 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003327nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003329 if (checkclearopq(cap->oap))
3330 return;
3331
3332 switch (cap->nchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003334 // "ZZ": equivalent to ":x".
3335 case 'Z': do_cmdline_cmd((char_u *)"x");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 break;
3337
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003338 // "ZQ": equivalent to ":q!" (Elvis compatible).
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003339 case 'Q': do_cmdline_cmd((char_u *)"q!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 break;
3341
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003342 default: clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 }
3344}
3345
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346/*
3347 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3348 */
3349 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003350do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351{
3352 oparg_T oa;
3353 cmdarg_T ca;
3354
3355 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003356 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 ca.oap = &oa;
3358 ca.cmdchar = c1;
3359 ca.nchar = c2;
3360 nv_ident(&ca);
3361}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362
3363/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003364 * 'K' normal-mode command. Get the command to lookup the keyword under the
3365 * cursor.
3366 */
3367 static int
3368nv_K_getcmd(
3369 cmdarg_T *cap,
3370 char_u *kp,
3371 int kp_help,
3372 int kp_ex,
3373 char_u **ptr_arg,
3374 int n,
3375 char_u *buf,
3376 unsigned buflen)
3377{
3378 char_u *ptr = *ptr_arg;
3379 int isman;
3380 int isman_s;
3381
3382 if (kp_help)
3383 {
3384 // in the help buffer
3385 STRCPY(buf, "he! ");
3386 return n;
3387 }
3388
3389 if (kp_ex)
3390 {
3391 // 'keywordprog' is an ex command
3392 if (cap->count0 != 0)
3393 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3394 else
3395 STRCPY(buf, kp);
3396 STRCAT(buf, " ");
3397 return n;
3398 }
3399
3400 // An external command will probably use an argument starting
3401 // with "-" as an option. To avoid trouble we skip the "-".
3402 while (*ptr == '-' && n > 0)
3403 {
3404 ++ptr;
3405 --n;
3406 }
3407 if (n == 0)
3408 {
3409 // found dashes only
3410 emsg(_(e_no_identifier_under_cursor));
3411 vim_free(buf);
3412 *ptr_arg = ptr;
3413 return 0;
3414 }
3415
3416 // When a count is given, turn it into a range. Is this
3417 // really what we want?
3418 isman = (STRCMP(kp, "man") == 0);
3419 isman_s = (STRCMP(kp, "man -s") == 0);
3420 if (cap->count0 != 0 && !(isman || isman_s))
3421 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3422
3423 STRCAT(buf, "! ");
3424 if (cap->count0 == 0 && isman_s)
3425 STRCAT(buf, "man");
3426 else
3427 STRCAT(buf, kp);
3428 STRCAT(buf, " ");
3429 if (cap->count0 != 0 && (isman || isman_s))
3430 {
3431 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3432 STRCAT(buf, " ");
3433 }
3434
3435 *ptr_arg = ptr;
3436 return n;
3437}
3438
3439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 * Handle the commands that use the word under the cursor.
3441 * [g] CTRL-] :ta to current identifier
3442 * [g] 'K' run program for current identifier
3443 * [g] '*' / to current identifier or string
3444 * [g] '#' ? to current identifier or string
3445 * g ']' :tselect for current identifier
3446 */
3447 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003448nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449{
3450 char_u *ptr = NULL;
3451 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003452 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003453 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003455 char_u *kp; // value of 'keywordprg'
3456 int kp_help; // 'keywordprg' is ":he"
3457 int kp_ex; // 'keywordprg' starts with ":"
3458 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003460 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003461 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003464 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 {
3466 cmdchar = cap->nchar;
3467 g_cmd = TRUE;
3468 }
3469 else
3470 {
3471 cmdchar = cap->cmdchar;
3472 g_cmd = FALSE;
3473 }
3474
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003475 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 cmdchar = '#';
3477
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003478 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3480 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3482 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 if (checkclearopq(cap->oap))
3484 return;
3485 }
3486
3487 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3488 (cmdchar == '*' || cmdchar == '#')
3489 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3490 {
3491 clearop(cap->oap);
3492 return;
3493 }
3494
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003495 // Allocate buffer to put the command in. Inserting backslashes can
3496 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3497 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3499 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3500 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003501 if (kp_help && *skipwhite(ptr) == NUL)
3502 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003503 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003504 return;
3505 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003506 kp_ex = (*kp == ':');
3507 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3508 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 if (buf == NULL)
3510 return;
3511 buf[0] = NUL;
3512
3513 switch (cmdchar)
3514 {
3515 case '*':
3516 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003517 // Put cursor at start of word, makes search skip the word
3518 // under the cursor.
3519 // Call setpcmark() first, so "*``" puts the cursor back where
3520 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 setpcmark();
3522 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3523
3524 if (!g_cmd && vim_iswordp(ptr))
3525 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003526 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 break;
3528
3529 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003530 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3531 if (n == 0)
3532 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 break;
3534
3535 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003536 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537#ifdef FEAT_CSCOPE
3538 if (p_cst)
3539 STRCPY(buf, "cstag ");
3540 else
3541#endif
3542 STRCPY(buf, "ts ");
3543 break;
3544
3545 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003546 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 if (curbuf->b_help)
3548 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003550 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003551 if (g_cmd)
3552 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003553 else if (cap->count0 == 0)
3554 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003555 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003556 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
3559
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003560 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003561 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003563 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003564 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003565 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003566 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003567 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003568 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003569 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003570 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003571 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003573 vim_free(buf);
3574 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003576 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003577 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003578 {
3579 vim_free(buf);
3580 vim_free(p);
3581 return;
3582 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003583 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003584 STRCAT(buf, p);
3585 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003587 else
3588 {
3589 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003590 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003591 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003592 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003593 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003594 {
3595 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003596 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003597 aux_ptr = (char_u *)"";
3598 else
3599 aux_ptr = (char_u *)"\\|\"\n[";
3600 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003601 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003602 aux_ptr = (char_u *)"\\|\"\n*?[";
3603
3604 p = buf + STRLEN(buf);
3605 while (n-- > 0)
3606 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003607 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003608 if (vim_strchr(aux_ptr, *ptr) != NULL)
3609 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003610 // When current byte is a part of multibyte character, copy all
3611 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003612 if (has_mbyte)
3613 {
3614 int i;
3615 int len = (*mb_ptr2len)(ptr) - 1;
3616
3617 for (i = 0; i < len && n >= 1; ++i, --n)
3618 *p++ = *ptr++;
3619 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003620 *p++ = *ptr++;
3621 }
3622 *p = NUL;
3623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003625 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 if (cmdchar == '*' || cmdchar == '#')
3627 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003628 if (!g_cmd && (has_mbyte
3629 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3630 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003632
3633 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003634 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003636
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003637 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 }
3639 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003640 {
3641 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003643 g_tag_at_cursor = FALSE;
3644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645
3646 vim_free(buf);
3647}
3648
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649/*
3650 * Get visually selected text, within one line only.
3651 * Returns FAIL if more than one line selected.
3652 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003653 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003654get_visual_text(
3655 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003656 char_u **pp, // return: start of selected text
3657 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658{
3659 if (VIsual_mode != 'V')
3660 unadjust_for_sel();
3661 if (VIsual.lnum != curwin->w_cursor.lnum)
3662 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003663 if (cap != NULL)
3664 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 return FAIL;
3666 }
3667 if (VIsual_mode == 'V')
3668 {
3669 *pp = ml_get_curline();
3670 *lenp = (int)STRLEN(*pp);
3671 }
3672 else
3673 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003674 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 {
3676 *pp = ml_get_pos(&curwin->w_cursor);
3677 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3678 }
3679 else
3680 {
3681 *pp = ml_get_pos(&VIsual);
3682 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3683 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003684 if (**pp == NUL)
3685 *lenp = 0;
Bram Moolenaar395bd1f2022-05-14 21:29:44 +01003686 if (*lenp > 0)
3687 {
3688 if (has_mbyte)
3689 // Correct the length to include all bytes of the last
3690 // character.
3691 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
3692 else if ((*pp)[*lenp - 1] == NUL)
3693 // Do not include a trailing NUL.
3694 *lenp -= 1;
3695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 }
3697 reset_VIsual_and_resel();
3698 return OK;
3699}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700
3701/*
3702 * CTRL-T: backwards in tag stack
3703 */
3704 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003705nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706{
3707 if (!checkclearopq(cap->oap))
3708 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3709}
3710
3711/*
3712 * Handle scrolling command 'H', 'L' and 'M'.
3713 */
3714 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003715nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716{
3717 int used = 0;
3718 long n;
3719#ifdef FEAT_FOLDING
3720 linenr_T lnum;
3721#endif
3722 int half;
3723
3724 cap->oap->motion_type = MLINE;
3725 setpcmark();
3726
3727 if (cap->cmdchar == 'L')
3728 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003729 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 curwin->w_cursor.lnum = curwin->w_botline - 1;
3731 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3732 curwin->w_cursor.lnum = 1;
3733 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003734 {
3735#ifdef FEAT_FOLDING
3736 if (hasAnyFolding(curwin))
3737 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003738 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003739 for (n = cap->count1 - 1; n > 0
3740 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3741 {
3742 (void)hasFolding(curwin->w_cursor.lnum,
3743 &curwin->w_cursor.lnum, NULL);
Bram Moolenaar232bdaa2023-01-13 14:17:58 +00003744 if (curwin->w_cursor.lnum > curwin->w_topline)
3745 --curwin->w_cursor.lnum;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003746 }
3747 }
3748 else
3749#endif
3750 curwin->w_cursor.lnum -= cap->count1 - 1;
3751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 }
3753 else
3754 {
3755 if (cap->cmdchar == 'M')
3756 {
3757#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003758 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 used -= diff_check_fill(curwin, curwin->w_topline)
3760 - curwin->w_topfill;
3761#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003762 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3764 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3765 {
3766#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003767 // Count half he number of filler lines to be "below this
3768 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3770 + n) / 2 >= half)
3771 {
3772 --n;
3773 break;
3774 }
3775#endif
3776 used += plines(curwin->w_topline + n);
3777 if (used >= half)
3778 break;
3779#ifdef FEAT_FOLDING
3780 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3781 n = lnum - curwin->w_topline;
3782#endif
3783 }
3784 if (n > 0 && used > curwin->w_height)
3785 --n;
3786 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003787 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003788 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003790#ifdef FEAT_FOLDING
3791 if (hasAnyFolding(curwin))
3792 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003793 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003794 lnum = curwin->w_topline;
3795 while (n-- > 0 && lnum < curwin->w_botline - 1)
3796 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003797 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003798 ++lnum;
3799 }
3800 n = lnum - curwin->w_topline;
3801 }
3802#endif
3803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 curwin->w_cursor.lnum = curwin->w_topline + n;
3805 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3806 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3807 }
3808
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003809 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003810 if (cap->oap->op_type == OP_NOP)
3811 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 beginline(BL_SOL | BL_FIX);
3813}
3814
3815/*
3816 * Cursor right commands.
3817 */
3818 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003819nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820{
3821 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003822 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003824 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3825 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003826 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003827 if (mod_mask & MOD_MASK_CTRL)
3828 cap->arg = TRUE;
3829 nv_wordcmd(cap);
3830 return;
3831 }
3832
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 cap->oap->motion_type = MCHAR;
3834 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003835 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003837 // In virtual edit mode, there's no such thing as "past_line", as lines
3838 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003840 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841
3842 for (n = cap->count1; n > 0; --n)
3843 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003844 if ((!past_line && oneright() == FAIL)
3845 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003846 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003848 // <Space> wraps to next line if 'whichwrap' has 's'.
3849 // 'l' wraps to next line if 'whichwrap' has 'l'.
3850 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 if ( ((cap->cmdchar == ' '
3852 && vim_strchr(p_ww, 's') != NULL)
3853 || (cap->cmdchar == 'l'
3854 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003855 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 && vim_strchr(p_ww, '>') != NULL))
3857 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3858 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003859 // When deleting we also count the NL as a character.
3860 // Set cap->oap->inclusive when last char in the line is
3861 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003862 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003864 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 cap->oap->inclusive = TRUE;
3866 else
3867 {
3868 ++curwin->w_cursor.lnum;
3869 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 curwin->w_set_curswant = TRUE;
3872 cap->oap->inclusive = FALSE;
3873 }
3874 continue;
3875 }
3876 if (cap->oap->op_type == OP_NOP)
3877 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003878 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 if (n == cap->count1)
3880 beep_flush();
3881 }
3882 else
3883 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003884 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 cap->oap->inclusive = TRUE;
3886 }
3887 break;
3888 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003889 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 {
3891 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 if (virtual_active())
3893 oneright();
3894 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003897 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 ++curwin->w_cursor.col;
3900 }
3901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 }
3903#ifdef FEAT_FOLDING
3904 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3905 && cap->oap->op_type == OP_NOP)
3906 foldOpenCursor();
3907#endif
3908}
3909
3910/*
3911 * Cursor left commands.
3912 *
3913 * Returns TRUE when operator end should not be adjusted.
3914 */
3915 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003916nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917{
3918 long n;
3919
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003920 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3921 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003922 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003923 if (mod_mask & MOD_MASK_CTRL)
3924 cap->arg = 1;
3925 nv_bck_word(cap);
3926 return;
3927 }
3928
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 cap->oap->motion_type = MCHAR;
3930 cap->oap->inclusive = FALSE;
3931 for (n = cap->count1; n > 0; --n)
3932 {
3933 if (oneleft() == FAIL)
3934 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003935 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
3936 // 'h' wraps to previous line if 'whichwrap' has 'h'.
3937 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 if ( (((cap->cmdchar == K_BS
3939 || cap->cmdchar == Ctrl_H)
3940 && vim_strchr(p_ww, 'b') != NULL)
3941 || (cap->cmdchar == 'h'
3942 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003943 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 && vim_strchr(p_ww, '<') != NULL))
3945 && curwin->w_cursor.lnum > 1)
3946 {
3947 --(curwin->w_cursor.lnum);
3948 coladvance((colnr_T)MAXCOL);
3949 curwin->w_set_curswant = TRUE;
3950
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003951 // When the NL before the first char has to be deleted we
3952 // put the cursor on the NUL after the previous line.
3953 // This is a very special case, be careful!
3954 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 if ( (cap->oap->op_type == OP_DELETE
3956 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003957 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003959 char_u *cp = ml_get_cursor();
3960
3961 if (*cp != NUL)
3962 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003963 if (has_mbyte)
3964 curwin->w_cursor.col += (*mb_ptr2len)(cp);
3965 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01003966 ++curwin->w_cursor.col;
3967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 cap->retval |= CA_NO_ADJ_OP_END;
3969 }
3970 continue;
3971 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003972 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
3974 beep_flush();
3975 break;
3976 }
3977 }
3978#ifdef FEAT_FOLDING
3979 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3980 && cap->oap->op_type == OP_NOP)
3981 foldOpenCursor();
3982#endif
3983}
3984
3985/*
3986 * Cursor up commands.
3987 * cap->arg is TRUE for "-": Move cursor to first non-blank.
3988 */
3989 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003990nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003992 if (mod_mask & MOD_MASK_SHIFT)
3993 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003994 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003995 cap->arg = BACKWARD;
3996 nv_page(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003997 return;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003998 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00003999
4000 cap->oap->motion_type = MLINE;
4001 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4002 clearopbeep(cap->oap);
4003 else if (cap->arg)
4004 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005}
4006
4007/*
4008 * Cursor down commands.
4009 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4010 */
4011 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004012nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004014 if (mod_mask & MOD_MASK_SHIFT)
4015 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004016 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004017 cap->arg = FORWARD;
4018 nv_page(cap);
4019 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004020#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004021 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004022 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4023 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004025 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004027 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004028 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 cmdwin_result = CAR;
4030 else
Bram Moolenaarf2732452018-06-03 14:47:35 +02004031#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004032 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004033 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4034 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4035 {
4036 invoke_prompt_callback();
4037 if (restart_edit == 0)
4038 restart_edit = 'a';
4039 }
4040 else
4041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
4043 cap->oap->motion_type = MLINE;
4044 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4045 clearopbeep(cap->oap);
4046 else if (cap->arg)
4047 beginline(BL_WHITE | BL_FIX);
4048 }
4049 }
4050}
4051
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052/*
4053 * Grab the file name under the cursor and edit it.
4054 */
4055 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004056nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057{
4058 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004059 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004061 if (check_text_or_curbuf_locked(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 return;
Bram Moolenaarcc762a42022-11-25 13:03:31 +00004063
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004064#ifdef FEAT_PROP_POPUP
4065 if (ERROR_IF_TERM_POPUP_WINDOW)
4066 return;
4067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004069 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070
4071 if (ptr != NULL)
4072 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004073 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004074 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004075 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004077 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004078 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004079 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004080 {
4081 curwin->w_cursor.lnum = lnum;
4082 check_cursor_lnum();
4083 beginline(BL_SOL | BL_FIX);
4084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 vim_free(ptr);
4086 }
4087 else
4088 clearop(cap->oap);
4089}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090
4091/*
4092 * <End> command: to end of current line or last line.
4093 */
4094 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004095nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004097 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004099 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004101 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 }
4103 nv_dollar(cap);
4104}
4105
4106/*
4107 * Handle the "$" command.
4108 */
4109 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004110nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111{
4112 cap->oap->motion_type = MCHAR;
4113 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004114 // In virtual mode when off the edge of a line and an operator
4115 // is pending (whew!) keep the cursor where it is.
4116 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 if (!virtual_active() || gchar_cursor() != NUL
4118 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004119 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 if (cursor_down((long)(cap->count1 - 1),
4121 cap->oap->op_type == OP_NOP) == FAIL)
4122 clearopbeep(cap->oap);
4123#ifdef FEAT_FOLDING
4124 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4125 foldOpenCursor();
4126#endif
4127}
4128
4129/*
4130 * Implementation of '?' and '/' commands.
4131 * If cap->arg is TRUE don't set PC mark.
4132 */
4133 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004134nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135{
4136 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004137 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138
4139 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4140 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004141 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 cap->cmdchar = 'g';
4143 cap->nchar = '?';
4144 nv_operator(cap);
4145 return;
4146 }
4147
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004148 // When using 'incsearch' the cursor may be moved to set a different search
4149 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004150 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151
4152 if (cap->searchbuf == NULL)
4153 {
4154 clearop(oap);
4155 return;
4156 }
4157
Bram Moolenaar46539112015-02-17 15:43:57 +01004158 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004159 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004160 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161}
4162
LemonBoya4399382022-04-09 21:04:08 +01004163
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164/*
4165 * Handle "N" and "n" commands.
4166 * cap->arg is SEARCH_REV for "N", 0 for "n".
4167 */
4168 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004169nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004171 pos_T old = curwin->w_cursor;
4172 int wrapped = FALSE;
4173 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004174
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004175 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004176 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004177 // Avoid getting stuck on the current cursor position, which can
4178 // happen when an offset is given and the cursor is on the last char
4179 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004180 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004181 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004182 cap->count1 -= 1;
4183 }
LemonBoya4399382022-04-09 21:04:08 +01004184
4185#ifdef FEAT_SEARCH_EXTRA
4186 // Redraw the window to refresh the highlighted matches.
4187 if (i > 0 && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004188 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190}
4191
4192/*
4193 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4194 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004195 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004197 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004198normal_search(
4199 cmdarg_T *cap,
4200 int dir,
4201 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004202 int opt, // extra flags for do_search()
4203 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204{
4205 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004206 searchit_arg_T sia;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004207#ifdef FEAT_SEARCH_EXTRA
LemonBoya4399382022-04-09 21:04:08 +01004208 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar2ce97ae2022-04-09 21:13:51 +01004209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210
4211 cap->oap->motion_type = MCHAR;
4212 cap->oap->inclusive = FALSE;
4213 cap->oap->use_reg_one = TRUE;
4214 curwin->w_set_curswant = TRUE;
4215
Bram Moolenaara80faa82020-04-12 19:37:17 +02004216 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004217 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004218 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4219 if (wrapped != NULL)
4220 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 if (i == 0)
4222 clearop(cap->oap);
4223 else
4224 {
4225 if (i == 2)
4226 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228#ifdef FEAT_FOLDING
4229 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4230 foldOpenCursor();
4231#endif
4232 }
LemonBoya4399382022-04-09 21:04:08 +01004233#ifdef FEAT_SEARCH_EXTRA
4234 // Redraw the window to refresh the highlighted matches.
4235 if (!EQUAL_POS(curwin->w_cursor, prev_cursor) && p_hls && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004236 redraw_later(UPD_SOME_VALID);
LemonBoya4399382022-04-09 21:04:08 +01004237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004239 // "/$" will put the cursor after the end of the line, may need to
4240 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004242 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243}
4244
4245/*
4246 * Character search commands.
4247 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4248 * ',' and FALSE for ';'.
4249 * cap->nchar is NUL for ',' and ';' (repeat the search)
4250 */
4251 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004252nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253{
4254 int t_cmd;
4255
4256 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4257 t_cmd = TRUE;
4258 else
4259 t_cmd = FALSE;
4260
4261 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004264 clearopbeep(cap->oap);
4265 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004267
4268 curwin->w_set_curswant = TRUE;
4269 // Include a Tab for "tx" and for "dfx".
4270 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4271 && (t_cmd || cap->oap->op_type != OP_NOP))
4272 {
4273 colnr_T scol, ecol;
4274
4275 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4276 curwin->w_cursor.coladd = ecol - scol;
4277 }
4278 else
4279 curwin->w_cursor.coladd = 0;
4280 adjust_for_sel(cap);
4281#ifdef FEAT_FOLDING
4282 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4283 foldOpenCursor();
4284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285}
4286
4287/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004288 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4289 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4290 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4291 * "[m" or "]m" search for prev/next start of (Java) method.
4292 * "[M" or "]M" search for prev/next end of (Java) method.
4293 */
4294 static void
4295nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4296{
4297 pos_T new_pos = {0, 0, 0};
4298 pos_T *pos = NULL; // init for GCC
4299 pos_T prev_pos;
4300 long n;
4301 int findc;
4302 int c;
4303
4304 if (cap->nchar == '*')
4305 cap->nchar = '/';
4306 prev_pos.lnum = 0;
4307 if (cap->nchar == 'm' || cap->nchar == 'M')
4308 {
4309 if (cap->cmdchar == '[')
4310 findc = '{';
4311 else
4312 findc = '}';
4313 n = 9999;
4314 }
4315 else
4316 {
4317 findc = cap->nchar;
4318 n = cap->count1;
4319 }
4320 for ( ; n > 0; --n)
4321 {
4322 if ((pos = findmatchlimit(cap->oap, findc,
4323 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4324 {
4325 if (new_pos.lnum == 0) // nothing found
4326 {
4327 if (cap->nchar != 'm' && cap->nchar != 'M')
4328 clearopbeep(cap->oap);
4329 }
4330 else
4331 pos = &new_pos; // use last one found
4332 break;
4333 }
4334 prev_pos = new_pos;
4335 curwin->w_cursor = *pos;
4336 new_pos = *pos;
4337 }
4338 curwin->w_cursor = *old_pos;
4339
4340 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4341 // brought us to the match for "[m" and "]M" when inside a method.
4342 // Try finding the '{' or '}' we want to be at.
4343 // Also repeat for the given count.
4344 if (cap->nchar == 'm' || cap->nchar == 'M')
4345 {
4346 // norm is TRUE for "]M" and "[m"
4347 int norm = ((findc == '{') == (cap->nchar == 'm'));
4348
4349 n = cap->count1;
4350 // found a match: we were inside a method
4351 if (prev_pos.lnum != 0)
4352 {
4353 pos = &prev_pos;
4354 curwin->w_cursor = prev_pos;
4355 if (norm)
4356 --n;
4357 }
4358 else
4359 pos = NULL;
4360 while (n > 0)
4361 {
4362 for (;;)
4363 {
4364 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4365 {
4366 // if not found anything, that's an error
4367 if (pos == NULL)
4368 clearopbeep(cap->oap);
4369 n = 0;
4370 break;
4371 }
4372 c = gchar_cursor();
4373 if (c == '{' || c == '}')
4374 {
4375 // Must have found end/start of class: use it.
4376 // Or found the place to be at.
4377 if ((c == findc && norm) || (n == 1 && !norm))
4378 {
4379 new_pos = curwin->w_cursor;
4380 pos = &new_pos;
4381 n = 0;
4382 }
4383 // if no match found at all, we started outside of the
4384 // class and we're inside now. Just go on.
4385 else if (new_pos.lnum == 0)
4386 {
4387 new_pos = curwin->w_cursor;
4388 pos = &new_pos;
4389 }
4390 // found start/end of other method: go to match
4391 else if ((pos = findmatchlimit(cap->oap, findc,
Shougo Matsushitafb552072022-01-28 16:01:13 +00004392 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4393 0)) == NULL)
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004394 n = 0;
4395 else
4396 curwin->w_cursor = *pos;
4397 break;
4398 }
4399 }
4400 --n;
4401 }
4402 curwin->w_cursor = *old_pos;
4403 if (pos == NULL && new_pos.lnum != 0)
4404 clearopbeep(cap->oap);
4405 }
4406 if (pos != NULL)
4407 {
4408 setpcmark();
4409 curwin->w_cursor = *pos;
4410 curwin->w_set_curswant = TRUE;
4411#ifdef FEAT_FOLDING
4412 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4413 && cap->oap->op_type == OP_NOP)
4414 foldOpenCursor();
4415#endif
4416 }
4417}
4418
4419/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 * "[" and "]" commands.
4421 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4422 */
4423 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004424nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004427 pos_T *pos = NULL; // init for GCC
4428 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 int flag;
4430 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431
4432 cap->oap->motion_type = MCHAR;
4433 cap->oap->inclusive = FALSE;
4434 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004435 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004437 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 if (cap->nchar == 'f')
4439 nv_gotofile(cap);
4440 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
4442#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004443 // Find the occurrence(s) of the identifier or define under cursor
4444 // in current and included files or jump to the first occurrence.
4445 //
4446 // search list jump
4447 // fwd bwd fwd bwd fwd bwd
4448 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4449 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar424bcae2022-01-31 14:59:41 +00004450 if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 {
4452 char_u *ptr;
4453 int len;
4454
4455 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4456 clearop(cap->oap);
4457 else
4458 {
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004459 // Make a copy, if the line was changed it will be freed.
4460 ptr = vim_strnsave(ptr, len);
4461 if (ptr == NULL)
4462 return;
4463
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 find_pattern_in_path(ptr, 0, len, TRUE,
4465 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4466 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4467 cap->count1,
4468 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4469 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4470 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4471 (linenr_T)MAXLNUM);
Bram Moolenaare2fa2132022-05-26 16:32:44 +01004472 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 curwin->w_set_curswant = TRUE;
4474 }
4475 }
4476 else
4477#endif
4478
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004479 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4480 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4481 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4482 // "[m" or "]m" search for prev/next start of (Java) method.
4483 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 if ( (cap->cmdchar == '['
4485 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4486 || (cap->cmdchar == ']'
4487 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004488 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004490 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 else if (cap->nchar == '[' || cap->nchar == ']')
4492 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004493 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 flag = '{';
4495 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004496 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497
4498 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004499 // Imitate strange Vi behaviour: When using "]]" with an operator
4500 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004501 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 (cap->oap->op_type != OP_NOP
4503 && cap->arg == FORWARD && flag == '{')))
4504 clearopbeep(cap->oap);
4505 else
4506 {
4507 if (cap->oap->op_type == OP_NOP)
4508 beginline(BL_WHITE | BL_FIX);
4509#ifdef FEAT_FOLDING
4510 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4511 foldOpenCursor();
4512#endif
4513 }
4514 }
4515
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004516 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 else if (cap->nchar == 'p' || cap->nchar == 'P')
4518 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004519 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 }
4521
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004522 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 else if (cap->nchar == '\'' || cap->nchar == '`')
4524 {
4525 pos = &curwin->w_cursor;
4526 for (n = cap->count1; n > 0; --n)
4527 {
4528 prev_pos = *pos;
4529 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4530 cap->nchar == '\'');
4531 if (pos == NULL)
4532 break;
4533 }
4534 if (pos == NULL)
4535 pos = &prev_pos;
4536 nv_cursormark(cap, cap->nchar == '\'', pos);
4537 }
4538
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004539 // [ or ] followed by a middle mouse click: put selected text with
4540 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004541 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 {
4543 (void)do_mouse(cap->oap, cap->nchar,
4544 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4545 cap->count1, PUT_FIXINDENT);
4546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547
4548#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004549 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 else if (cap->nchar == 'z')
4551 {
4552 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4553 cap->count1) == FAIL)
4554 clearopbeep(cap->oap);
4555 }
4556#endif
4557
4558#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004559 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 else if (cap->nchar == 'c')
4561 {
4562 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4563 cap->count1) == FAIL)
4564 clearopbeep(cap->oap);
4565 }
4566#endif
4567
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004568#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004569 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004570 else if (cap->nchar == 's' || cap->nchar == 'S')
4571 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004572 setpcmark();
4573 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004574 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4575 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004576 {
4577 clearopbeep(cap->oap);
4578 break;
4579 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004580 else
4581 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004582# ifdef FEAT_FOLDING
4583 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4584 foldOpenCursor();
4585# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004586 }
4587#endif
4588
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004589 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 else
4591 clearopbeep(cap->oap);
4592}
4593
4594/*
4595 * Handle Normal mode "%" command.
4596 */
4597 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004598nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599{
4600 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004601#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 linenr_T lnum = curwin->w_cursor.lnum;
4603#endif
4604
4605 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004606 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 {
4608 if (cap->count0 > 100)
4609 clearopbeep(cap->oap);
4610 else
4611 {
4612 cap->oap->motion_type = MLINE;
4613 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004614 // Round up, so 'normal 100%' always jumps at the line line.
4615 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4616 // overflow on 32-bits, so use a formula with less accuracy
4617 // to avoid overflows.
4618 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4620 / 100L * cap->count0;
4621 else
4622 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4623 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004624 if (curwin->w_cursor.lnum < 1)
4625 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4627 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4628 beginline(BL_SOL | BL_FIX);
4629 }
4630 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004631 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 {
4633 cap->oap->motion_type = MCHAR;
4634 cap->oap->use_reg_one = TRUE;
4635 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4636 clearopbeep(cap->oap);
4637 else
4638 {
4639 setpcmark();
4640 curwin->w_cursor = *pos;
4641 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 }
4645 }
4646#ifdef FEAT_FOLDING
4647 if (cap->oap->op_type == OP_NOP
4648 && lnum != curwin->w_cursor.lnum
4649 && (fdo_flags & FDO_PERCENT)
4650 && KeyTyped)
4651 foldOpenCursor();
4652#endif
4653}
4654
4655/*
4656 * Handle "(" and ")" commands.
4657 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4658 */
4659 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004660nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661{
4662 cap->oap->motion_type = MCHAR;
4663 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004664 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004665 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 curwin->w_set_curswant = TRUE;
4667
4668 if (findsent(cap->arg, cap->count1) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004670 clearopbeep(cap->oap);
4671 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004673
4674 // Don't leave the cursor on the NUL past end of line.
4675 adjust_cursor(cap->oap);
4676 curwin->w_cursor.coladd = 0;
4677#ifdef FEAT_FOLDING
4678 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4679 foldOpenCursor();
4680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681}
4682
4683/*
4684 * "m" command: Mark a position.
4685 */
4686 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004687nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004689 if (checkclearop(cap->oap))
4690 return;
4691
4692 if (setmark(cap->nchar) == FAIL)
4693 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694}
4695
4696/*
4697 * "{" and "}" commands.
4698 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4699 */
4700 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004701nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702{
4703 cap->oap->motion_type = MCHAR;
4704 cap->oap->inclusive = FALSE;
4705 cap->oap->use_reg_one = TRUE;
4706 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004707 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004709 clearopbeep(cap->oap);
4710 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004712
4713 curwin->w_cursor.coladd = 0;
4714#ifdef FEAT_FOLDING
4715 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4716 foldOpenCursor();
4717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718}
4719
4720/*
4721 * "u" command: Undo or make lower case.
4722 */
4723 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004724nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004726 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004728 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 cap->cmdchar = 'g';
4730 cap->nchar = 'u';
4731 nv_operator(cap);
4732 }
4733 else
4734 nv_kundo(cap);
4735}
4736
4737/*
4738 * <Undo> command.
4739 */
4740 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004741nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004743 if (checkclearopq(cap->oap))
4744 return;
4745
Bram Moolenaarf2732452018-06-03 14:47:35 +02004746#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004747 if (bt_prompt(curbuf))
4748 {
4749 clearopbeep(cap->oap);
4750 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00004752#endif
4753 u_undo((int)cap->count1);
4754 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755}
4756
4757/*
4758 * Handle the "r" command.
4759 */
4760 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004761nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762{
4763 char_u *ptr;
4764 int had_ctrl_v;
4765 long n;
4766
4767 if (checkclearop(cap->oap))
4768 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004769#ifdef FEAT_JOB_CHANNEL
4770 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4771 {
4772 clearopbeep(cap->oap);
4773 return;
4774 }
4775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004777 // get another character
Christian Brabandt2d63e4b2023-08-12 00:03:57 +02004778 if (cap->nchar == Ctrl_V || cap->nchar == Ctrl_Q)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 {
4780 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004781 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004782 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 if (cap->nchar > DEL)
4784 had_ctrl_v = NUL;
4785 }
4786 else
4787 had_ctrl_v = NUL;
4788
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004789 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004790 if (IS_SPECIAL(cap->nchar))
4791 {
4792 clearopbeep(cap->oap);
4793 return;
4794 }
4795
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004796 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 if (VIsual_active)
4798 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004799 if (got_int)
4800 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004801 if (had_ctrl_v)
4802 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004803 // Use a special (negative) number to make a difference between a
4804 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004805 if (cap->nchar == CAR)
4806 cap->nchar = REPLACE_CR_NCHAR;
4807 else if (cap->nchar == NL)
4808 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 nv_operator(cap);
4811 return;
4812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004814 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 if (virtual_active())
4816 {
4817 if (u_save_cursor() == FAIL)
4818 return;
4819 if (gchar_cursor() == NUL)
4820 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004821 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 coladvance_force((colnr_T)(getviscol() + cap->count1));
4823 curwin->w_cursor.col -= cap->count1;
4824 }
4825 else if (gchar_cursor() == TAB)
4826 coladvance_force(getviscol());
4827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004829 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004831 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004832 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 {
4834 clearopbeep(cap->oap);
4835 return;
4836 }
4837
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004838 // Replacing with a TAB is done by edit() when it is complicated because
4839 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4840 // Other characters are done below to avoid problems with things like
4841 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4843 {
4844 stuffnumReadbuff(cap->count1);
4845 stuffcharReadbuff('R');
4846 stuffcharReadbuff('\t');
4847 stuffcharReadbuff(ESC);
4848 return;
4849 }
4850
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004851 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 if (u_save_cursor() == FAIL)
4853 return;
4854
4855 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4856 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004857 // Replace character(s) by a single newline.
4858 // Strange vi behaviour: Only one newline is inserted.
4859 // Delete the characters here.
4860 // Insert the newline with an insert command, takes care of
4861 // autoindent. The insert command depends on being on the last
4862 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004863 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 stuffcharReadbuff('\r');
4865 stuffcharReadbuff(ESC);
4866
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004867 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 invoke_edit(cap, TRUE, 'r', FALSE);
4869 }
4870 else
4871 {
4872 prep_redo(cap->oap->regname, cap->count1,
4873 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4874
4875 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 if (has_mbyte)
4877 {
4878 int old_State = State;
4879
4880 if (cap->ncharC1 != 0)
4881 AppendCharToRedobuff(cap->ncharC1);
4882 if (cap->ncharC2 != 0)
4883 AppendCharToRedobuff(cap->ncharC2);
4884
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004885 // This is slow, but it handles replacing a single-byte with a
4886 // multi-byte and the other way around. Also handles adding
4887 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 for (n = cap->count1; n > 0; --n)
4889 {
Bram Moolenaar24959102022-05-07 20:01:16 +01004890 State = MODE_REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004891 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4892 {
4893 int c = ins_copychar(curwin->w_cursor.lnum
4894 + (cap->nchar == Ctrl_Y ? -1 : 1));
4895 if (c != NUL)
4896 ins_char(c);
4897 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004898 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004899 ++curwin->w_cursor.col;
4900 }
4901 else
4902 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 State = old_State;
4904 if (cap->ncharC1 != 0)
4905 ins_char(cap->ncharC1);
4906 if (cap->ncharC2 != 0)
4907 ins_char(cap->ncharC2);
4908 }
4909 }
4910 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004912 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 for (n = cap->count1; n > 0; --n)
4914 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004915 // Get ptr again, because u_save and/or showmatch() will have
4916 // released the line. This may also happen in ins_copychar().
4917 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02004918 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4919 {
4920 int c = ins_copychar(curwin->w_cursor.lnum
4921 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004922
4923 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004924 if (c != NUL)
4925 ptr[curwin->w_cursor.col] = c;
4926 }
4927 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004928 {
4929 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02004930 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02004931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 if (p_sm && msg_silent == 0)
4933 showmatch(cap->nchar);
4934 ++curwin->w_cursor.col;
4935 }
4936#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004937 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02004939 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940
Bram Moolenaar009b2592004-10-24 19:18:58 +00004941 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00004942 cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00004944 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 }
4946#endif
4947
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004948 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 changed_bytes(curwin->w_cursor.lnum,
4950 (colnr_T)(curwin->w_cursor.col - cap->count1));
4951 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004952 --curwin->w_cursor.col; // cursor on the last replaced char
4953 // if the character on the left of the current cursor is a multi-byte
4954 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 if (has_mbyte)
4956 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 curbuf->b_op_end = curwin->w_cursor;
4958 curwin->w_set_curswant = TRUE;
4959 set_last_insert(cap->nchar);
4960 }
4961}
4962
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963/*
4964 * 'o': Exchange start and end of Visual area.
4965 * 'O': same, but in block mode exchange left and right corners.
4966 */
4967 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004968v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969{
4970 pos_T old_cursor;
4971 colnr_T left, right;
4972
4973 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
4974 {
4975 old_cursor = curwin->w_cursor;
4976 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
4977 curwin->w_cursor.lnum = VIsual.lnum;
4978 coladvance(left);
4979 VIsual = curwin->w_cursor;
4980
4981 curwin->w_cursor.lnum = old_cursor.lnum;
4982 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004983 // 'selection "exclusive" and cursor at right-bottom corner: move it
4984 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
4986 ++curwin->w_curswant;
4987 coladvance(curwin->w_curswant);
4988 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004990 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 {
4992 curwin->w_cursor.lnum = VIsual.lnum;
4993 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
4994 ++right;
4995 coladvance(right);
4996 VIsual = curwin->w_cursor;
4997
4998 curwin->w_cursor.lnum = old_cursor.lnum;
4999 coladvance(left);
5000 curwin->w_curswant = left;
5001 }
5002 }
5003 else
5004 {
5005 old_cursor = curwin->w_cursor;
5006 curwin->w_cursor = VIsual;
5007 VIsual = old_cursor;
5008 curwin->w_set_curswant = TRUE;
5009 }
5010}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011
5012/*
5013 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5014 */
5015 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005016nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005018 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
5020 cap->cmdchar = 'c';
5021 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005022 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 VIsual_mode = 'V';
5024 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005025 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005027
5028 if (checkclearopq(cap->oap))
5029 return;
5030
5031 if (!curbuf->b_p_ma)
5032 emsg(_(e_cannot_make_changes_modifiable_is_off));
5033 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005035 if (virtual_active())
5036 coladvance(getviscol());
5037 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 }
5039}
5040
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041/*
5042 * "gr".
5043 */
5044 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005045nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 if (VIsual_active)
5048 {
5049 cap->cmdchar = 'r';
5050 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005051 nv_replace(cap); // Do same as "r" in Visual mode for now
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005052 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005054
zeertzjqf86dea82023-03-05 21:15:06 +00005055 if (checkclearopq(cap->oap))
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005056 return;
5057
5058 if (!curbuf->b_p_ma)
5059 emsg(_(e_cannot_make_changes_modifiable_is_off));
5060 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 {
Christian Brabandt2d63e4b2023-08-12 00:03:57 +02005062 if (cap->extra_char == Ctrl_V || cap->extra_char == Ctrl_Q)
5063 // get another character
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005064 cap->extra_char = get_literal(FALSE);
Bram Moolenaard6a4ea32023-02-25 14:24:44 +00005065 if (cap->extra_char < ' ')
5066 // Prefix a control character with CTRL-V to avoid it being used as
5067 // a command.
5068 stuffcharReadbuff(Ctrl_V);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005069 stuffcharReadbuff(cap->extra_char);
5070 stuffcharReadbuff(ESC);
5071 if (virtual_active())
5072 coladvance(getviscol());
5073 invoke_edit(cap, TRUE, 'v', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 }
5075}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076
5077/*
5078 * Swap case for "~" command, when it does not work like an operator.
5079 */
5080 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005081n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082{
5083 long n;
5084 pos_T startpos;
5085 int did_change = 0;
5086#ifdef FEAT_NETBEANS_INTG
5087 pos_T pos;
5088 char_u *ptr;
5089 int count;
5090#endif
5091
5092 if (checkclearopq(cap->oap))
5093 return;
5094
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005095 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 {
5097 clearopbeep(cap->oap);
5098 return;
5099 }
5100
5101 prep_redo_cmd(cap);
5102
5103 if (u_save_cursor() == FAIL)
5104 return;
5105
5106 startpos = curwin->w_cursor;
5107#ifdef FEAT_NETBEANS_INTG
5108 pos = startpos;
5109#endif
5110 for (n = cap->count1; n > 0; --n)
5111 {
5112 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5113 inc_cursor();
5114 if (gchar_cursor() == NUL)
5115 {
5116 if (vim_strchr(p_ww, '~') != NULL
5117 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5118 {
5119#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005120 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 {
5122 if (did_change)
5123 {
5124 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005125 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005126 netbeans_removed(curbuf, pos.lnum, pos.col,
5127 (long)count);
Bram Moolenaarfa4873c2022-06-30 22:13:59 +01005128 // line may have been flushed, get it again
5129 ptr = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005131 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 }
5133 pos.col = 0;
5134 pos.lnum++;
5135 }
5136#endif
5137 ++curwin->w_cursor.lnum;
5138 curwin->w_cursor.col = 0;
5139 if (n > 1)
5140 {
5141 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5142 break;
5143 u_clearline();
5144 }
5145 }
5146 else
5147 break;
5148 }
5149 }
5150#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005151 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 {
5153 ptr = ml_get(pos.lnum);
5154 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005155 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5156 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 }
5158#endif
5159
5160
5161 check_cursor();
5162 curwin->w_set_curswant = TRUE;
5163 if (did_change)
5164 {
5165 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5166 0L);
5167 curbuf->b_op_start = startpos;
5168 curbuf->b_op_end = curwin->w_cursor;
5169 if (curbuf->b_op_end.col > 0)
5170 --curbuf->b_op_end.col;
5171 }
5172}
5173
5174/*
5175 * Move cursor to mark.
5176 */
5177 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005178nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179{
5180 if (check_mark(pos) == FAIL)
5181 clearop(cap->oap);
5182 else
5183 {
5184 if (cap->cmdchar == '\''
5185 || cap->cmdchar == '`'
5186 || cap->cmdchar == '['
5187 || cap->cmdchar == ']')
5188 setpcmark();
5189 curwin->w_cursor = *pos;
5190 if (flag)
5191 beginline(BL_WHITE | BL_FIX);
5192 else
5193 check_cursor();
5194 }
5195 cap->oap->motion_type = flag ? MLINE : MCHAR;
5196 if (cap->cmdchar == '`')
5197 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005198 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 curwin->w_set_curswant = TRUE;
5200}
5201
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202/*
5203 * Handle commands that are operators in Visual mode.
5204 */
5205 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005206v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207{
5208 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5209
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005210 // Uppercase means linewise, except in block mode, then "D" deletes till
5211 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 if (isupper(cap->cmdchar))
5213 {
5214 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005215 {
5216 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5220 curwin->w_curswant = MAXCOL;
5221 }
5222 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5223 nv_operator(cap);
5224}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225
5226/*
5227 * "s" and "S" commands.
5228 */
5229 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005230nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005232#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005233 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005234 if (term_swap_diff() == OK)
5235 return;
5236#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005237#ifdef FEAT_JOB_CHANNEL
5238 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5239 {
5240 clearopbeep(cap->oap);
5241 return;
5242 }
5243#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005244 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 {
5246 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005247 {
5248 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 cap->cmdchar = 'c';
5252 nv_operator(cap);
5253 }
5254 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 nv_optrans(cap);
5256}
5257
5258/*
5259 * Abbreviated commands.
5260 */
5261 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005262nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263{
5264 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005265 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005267 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 if (VIsual_active)
5269 v_visop(cap);
5270 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 nv_optrans(cap);
5272}
5273
5274/*
5275 * Translate a command into another command.
5276 */
5277 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005278nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279{
5280 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5281 (char_u *)"d$", (char_u *)"c$",
5282 (char_u *)"cl", (char_u *)"cc",
5283 (char_u *)"yy", (char_u *)":s\r"};
5284 static char_u *str = (char_u *)"xXDCsSY&";
5285
5286 if (!checkclearopq(cap->oap))
5287 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005288 // In Vi "2D" doesn't delete the next line. Can't translate it
5289 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005290 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5291 {
5292 cap->oap->start = curwin->w_cursor;
5293 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005294#ifdef FEAT_EVAL
5295 set_op_var(OP_DELETE);
5296#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005297 cap->count1 = 1;
5298 nv_dollar(cap);
5299 finish_op = TRUE;
5300 ResetRedobuff();
5301 AppendCharToRedobuff('D');
5302 }
5303 else
5304 {
5305 if (cap->count0)
5306 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005307 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 }
5310 cap->opcount = 0;
5311}
5312
5313/*
5314 * "'" and "`" commands. Also for "g'" and "g`".
5315 * cap->arg is TRUE for "'" and "g'".
5316 */
5317 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005318nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319{
5320 pos_T *pos;
5321 int c;
5322#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005323 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005324 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325#endif
5326
5327 if (cap->cmdchar == 'g')
5328 c = cap->extra_char;
5329 else
5330 c = cap->nchar;
5331 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005332 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 {
5334 if (cap->arg)
5335 {
5336 check_cursor_lnum();
5337 beginline(BL_WHITE | BL_FIX);
5338 }
5339 else
5340 check_cursor();
5341 }
5342 else
5343 nv_cursormark(cap, cap->arg, pos);
5344
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005345 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 if (!virtual_active())
5347 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005348 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349#ifdef FEAT_FOLDING
5350 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005351 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005352 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 && (fdo_flags & FDO_MARK)
5354 && old_KeyTyped)
5355 foldOpenCursor();
5356#endif
5357}
5358
5359/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005360 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 */
5362 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005363nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005366#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005368 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005371 if (checkclearopq(cap->oap))
5372 return;
5373
5374 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005376 if (goto_tabpage_lastused() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 clearopbeep(cap->oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005378 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00005380 if (cap->cmdchar == 'g')
5381 pos = movechangelist((int)cap->count1);
5382 else
5383 pos = movemark((int)cap->count1);
5384 if (pos == (pos_T *)-1) // jump to other file
5385 {
5386 curwin->w_set_curswant = TRUE;
5387 check_cursor();
5388 }
5389 else if (pos != NULL) // can jump
5390 nv_cursormark(cap, FALSE, pos);
5391 else if (cap->cmdchar == 'g')
5392 {
5393 if (curbuf->b_changelistlen == 0)
5394 emsg(_(e_changelist_is_empty));
5395 else if (cap->count1 < 0)
5396 emsg(_(e_at_start_of_changelist));
5397 else
5398 emsg(_(e_at_end_of_changelist));
5399 }
5400 else
5401 clearopbeep(cap->oap);
5402# ifdef FEAT_FOLDING
5403 if (cap->oap->op_type == OP_NOP
5404 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5405 && (fdo_flags & FDO_MARK)
5406 && old_KeyTyped)
5407 foldOpenCursor();
5408# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409}
5410
5411/*
5412 * Handle '"' command.
5413 */
5414 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005415nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416{
5417 if (checkclearop(cap->oap))
5418 return;
5419#ifdef FEAT_EVAL
5420 if (cap->nchar == '=')
5421 cap->nchar = get_expr_register();
5422#endif
5423 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5424 {
5425 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005426 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427#ifdef FEAT_EVAL
5428 set_reg_var(cap->oap->regname);
5429#endif
5430 }
5431 else
5432 clearopbeep(cap->oap);
5433}
5434
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435/*
5436 * Handle "v", "V" and "CTRL-V" commands.
5437 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5438 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005439 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 */
5441 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005442nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005444 if (cap->cmdchar == Ctrl_Q)
5445 cap->cmdchar = Ctrl_V;
5446
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005447 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5448 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 if (cap->oap->op_type != OP_NOP)
5450 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005451 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005452 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 return;
5454 }
5455
5456 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005457 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005459 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005461 else // toggle char/block mode
5462 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 VIsual_mode = cap->cmdchar;
5464 showmode();
LemonBoy2bf52dd2022-04-09 18:17:34 +01005465 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005467 redraw_curbuf_later(UPD_INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005469 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 {
5471 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005472 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005474 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 VIsual = curwin->w_cursor;
5476
5477 VIsual_active = TRUE;
5478 VIsual_reselect = TRUE;
5479 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005480 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005483 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005484 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005485 // For V and ^V, we multiply the number of lines even if there
5486 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5488 {
5489 curwin->w_cursor.lnum +=
5490 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005491 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 }
5493 VIsual_mode = resel_VIsual_mode;
5494 if (VIsual_mode == 'v')
5495 {
5496 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005497 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005498 update_curswant_force();
Bram Moolenaar79c11e32023-01-10 17:29:29 +00005499 curwin->w_curswant += resel_VIsual_vcol * cap->count0;
5500 if (*p_sel != 'e')
5501 --curwin->w_curswant;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005504 curwin->w_curswant = resel_VIsual_vcol;
5505 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005507 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 {
5509 curwin->w_curswant = MAXCOL;
5510 coladvance((colnr_T)MAXCOL);
5511 }
5512 else if (VIsual_mode == Ctrl_V)
5513 {
Bram Moolenaar8f531662023-02-01 17:33:18 +00005514 // Update curswant on the original line, that is where "col" is
5515 // valid.
5516 linenr_T lnum = curwin->w_cursor.lnum;
5517 curwin->w_cursor.lnum = VIsual.lnum;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005518 update_curswant_force();
Bram Moolenaar8f531662023-02-01 17:33:18 +00005519 curwin->w_curswant += resel_VIsual_vcol * cap->count0 - 1;
5520 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 coladvance(curwin->w_curswant);
5522 }
5523 else
5524 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005525 redraw_curbuf_later(UPD_INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 }
5527 else
5528 {
5529 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005530 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 may_start_select('c');
5532 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005533 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005534 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005535 if (cap->count0 > 0 && --cap->count1 > 0)
5536 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005537 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005538 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5539 nv_right(cap);
5540 else if (VIsual_mode == 'V')
5541 nv_down(cap);
5542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 }
5544 }
5545}
5546
5547/*
5548 * Start selection for Shift-movement keys.
5549 */
5550 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005551start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005553 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 may_start_select('k');
5555 n_start_visual_mode('v');
5556}
5557
5558/*
5559 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005560 * When "c" is 'o' (checking for "mouse") then also when mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 */
5562 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005563may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564{
Bram Moolenaar53ef5732022-04-22 21:20:26 +01005565 VIsual_select = (c == 'o' || (stuff_empty() && typebuf_typed()))
5566 && vim_strchr(p_slm, c) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567}
5568
5569/*
5570 * Start Visual mode "c".
5571 * Should set VIsual_select before calling this.
5572 */
5573 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005574n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005576#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005577 int cursor_line_was_concealed = curwin->w_p_cole > 0
5578 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005579#endif
5580
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 VIsual_mode = c;
5582 VIsual_active = TRUE;
5583 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005584
5585 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005586 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005587 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005588 {
5589 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 VIsual = curwin->w_cursor;
5593
5594#ifdef FEAT_FOLDING
5595 foldAdjustVisual();
5596#endif
5597
LemonBoy2bf52dd2022-04-09 18:17:34 +01005598 may_trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005600#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005601 // Check if redraw is needed after changing the state.
5602 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005603#endif
5604
Bram Moolenaar09df3122006-01-23 22:23:09 +00005605 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005606 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005608 // Make sure the clipboard gets updated. Needed because start and
5609 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 clip_star.vmode = NUL;
5611#endif
5612
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005613 // Only need to redraw this line, unless still need to redraw an old
5614 // Visual area (when 'lazyredraw' is set).
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005615 if (curwin->w_redr_type < UPD_INVERTED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 {
5617 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5618 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5619 }
5620}
5621
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622
5623/*
5624 * CTRL-W: Window commands
5625 */
5626 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005627nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005629 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005630 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005631 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005632 cap->cmdchar = ':';
5633 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005634 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005635 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005636 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005637 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638}
5639
5640/*
5641 * CTRL-Z: Suspend
5642 */
5643 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005644nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645{
5646 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005648 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005649 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650}
5651
5652/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005653 * "gv": Reselect the previous Visual area. If Visual already active,
5654 * exchange previous and current Visual area.
5655 */
5656 static void
5657nv_gv_cmd(cmdarg_T *cap)
5658{
5659 pos_T tpos;
5660 int i;
5661
5662 if (checkclearop(cap->oap))
5663 return;
5664
5665 if (curbuf->b_visual.vi_start.lnum == 0
5666 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5667 || curbuf->b_visual.vi_end.lnum == 0)
5668 {
5669 beep_flush();
5670 return;
5671 }
5672
5673 // set w_cursor to the start of the Visual area, tpos to the end
5674 if (VIsual_active)
5675 {
5676 i = VIsual_mode;
5677 VIsual_mode = curbuf->b_visual.vi_mode;
5678 curbuf->b_visual.vi_mode = i;
5679# ifdef FEAT_EVAL
5680 curbuf->b_visual_mode_eval = i;
5681# endif
5682 i = curwin->w_curswant;
5683 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5684 curbuf->b_visual.vi_curswant = i;
5685
5686 tpos = curbuf->b_visual.vi_end;
5687 curbuf->b_visual.vi_end = curwin->w_cursor;
5688 curwin->w_cursor = curbuf->b_visual.vi_start;
5689 curbuf->b_visual.vi_start = VIsual;
5690 }
5691 else
5692 {
5693 VIsual_mode = curbuf->b_visual.vi_mode;
5694 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5695 tpos = curbuf->b_visual.vi_end;
5696 curwin->w_cursor = curbuf->b_visual.vi_start;
5697 }
5698
5699 VIsual_active = TRUE;
5700 VIsual_reselect = TRUE;
5701
5702 // Set Visual to the start and w_cursor to the end of the Visual
5703 // area. Make sure they are on an existing character.
5704 check_cursor();
5705 VIsual = curwin->w_cursor;
5706 curwin->w_cursor = tpos;
5707 check_cursor();
5708 update_topline();
5709
5710 // When called from normal "g" command: start Select mode when
5711 // 'selectmode' contains "cmd". When called for K_SELECT, always
5712 // start Select mode.
5713 if (cap->arg)
5714 {
5715 VIsual_select = TRUE;
5716 VIsual_select_reg = 0;
5717 }
5718 else
5719 may_start_select('c');
5720 setmouse();
5721#ifdef FEAT_CLIPBOARD
5722 // Make sure the clipboard gets updated. Needed because start and
5723 // end are still the same, and the selection needs to be owned
5724 clip_star.vmode = NUL;
5725#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005726 redraw_curbuf_later(UPD_INVERTED);
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005727 showmode();
5728}
5729
5730/*
5731 * "g0", "g^" : Like "0" and "^" but for screen lines.
5732 * "gm": middle of "g0" and "g$".
5733 */
5734 static void
5735nv_g_home_m_cmd(cmdarg_T *cap)
5736{
5737 int i;
5738 int flag = FALSE;
5739
5740 if (cap->nchar == '^')
5741 flag = TRUE;
5742
5743 cap->oap->motion_type = MCHAR;
5744 cap->oap->inclusive = FALSE;
5745 if (curwin->w_p_wrap && curwin->w_width != 0)
5746 {
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005747 int width1 = curwin->w_width - curwin_col_off();
5748 int width2 = width1 + curwin_col_off2();
5749 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005750
5751 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005752 virtcol = curwin->w_virtcol
5753#ifdef FEAT_PROP_POPUP
5754 - curwin->w_virtcol_first_char
5755#endif
5756 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005757 i = 0;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005758 if (virtcol >= (colnr_T)width1 && width2 > 0)
5759 i = (virtcol - width1) / width2 * width2 + width1;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005760 }
5761 else
5762 i = curwin->w_leftcol;
5763 // Go to the middle of the screen line. When 'number' or
5764 // 'relativenumber' is on and lines are wrapping the middle can be more
5765 // to the left.
5766 if (cap->nchar == 'm')
5767 i += (curwin->w_width - curwin_col_off()
5768 + ((curwin->w_p_wrap && i > 0)
5769 ? curwin_col_off2() : 0)) / 2;
5770 coladvance((colnr_T)i);
5771 if (flag)
5772 {
5773 do
5774 i = gchar_cursor();
5775 while (VIM_ISWHITE(i) && oneright() == OK);
5776 curwin->w_valid &= ~VALID_WCOL;
5777 }
5778 curwin->w_set_curswant = TRUE;
Luuk van Baal8667a562023-05-12 15:47:25 +01005779 adjust_skipcol();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005780}
5781
5782/*
5783 * "g_": to the last non-blank character in the line or <count> lines
5784 * downward.
5785 */
5786 static void
5787nv_g_underscore_cmd(cmdarg_T *cap)
5788{
5789 char_u *ptr;
5790
5791 cap->oap->motion_type = MCHAR;
5792 cap->oap->inclusive = TRUE;
5793 curwin->w_curswant = MAXCOL;
5794 if (cursor_down((long)(cap->count1 - 1),
5795 cap->oap->op_type == OP_NOP) == FAIL)
5796 {
5797 clearopbeep(cap->oap);
5798 return;
5799 }
5800
5801 ptr = ml_get_curline();
5802
5803 // In Visual mode we may end up after the line.
5804 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5805 --curwin->w_cursor.col;
5806
5807 // Decrease the cursor column until it's on a non-blank.
5808 while (curwin->w_cursor.col > 0
5809 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
5810 --curwin->w_cursor.col;
5811 curwin->w_set_curswant = TRUE;
5812 adjust_for_sel(cap);
5813}
5814
5815/*
5816 * "g$" : Like "$" but for screen lines.
5817 */
5818 static void
5819nv_g_dollar_cmd(cmdarg_T *cap)
5820{
5821 oparg_T *oap = cap->oap;
5822 int i;
5823 int col_off = curwin_col_off();
Christian Brabandtb5f6fe92023-08-19 15:53:16 +02005824 int flag = FALSE;
5825
5826 if (cap->nchar == K_END)
5827 flag = TRUE;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005828
5829 oap->motion_type = MCHAR;
5830 oap->inclusive = TRUE;
5831 if (curwin->w_p_wrap && curwin->w_width != 0)
5832 {
5833 curwin->w_curswant = MAXCOL; // so we stay at the end
5834 if (cap->count1 == 1)
5835 {
5836 int width1 = curwin->w_width - col_off;
5837 int width2 = width1 + curwin_col_off2();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005838 int virtcol;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005839
5840 validate_virtcol();
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005841 virtcol = curwin->w_virtcol
5842#ifdef FEAT_PROP_POPUP
5843 - curwin->w_virtcol_first_char
5844#endif
5845 ;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005846 i = width1 - 1;
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005847 if (virtcol >= (colnr_T)width1)
5848 i += ((virtcol - width1) / width2 + 1)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005849 * width2;
5850 coladvance((colnr_T)i);
5851
5852 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005853 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005854 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
5855 {
5856 // Check for landing on a character that got split at
5857 // the end of the line. We do not want to advance to
5858 // the next screen line.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005859 if (curwin->w_virtcol
5860#ifdef FEAT_PROP_POPUP
5861 - curwin->w_virtcol_first_char
5862#endif
5863 > (colnr_T)i)
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005864 --curwin->w_cursor.col;
5865 }
5866 }
5867 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
5868 clearopbeep(oap);
5869 }
5870 else
5871 {
5872 if (cap->count1 > 1)
5873 // if it fails, let the cursor still move to the last char
5874 (void)cursor_down(cap->count1 - 1, FALSE);
5875
5876 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
5877 coladvance((colnr_T)i);
5878
5879 // if the character doesn't fit move one back
5880 if (curwin->w_cursor.col > 0
5881 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
5882 {
5883 colnr_T vcol;
5884
5885 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
5886 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
5887 --curwin->w_cursor.col;
5888 }
5889
5890 // Make sure we stick in this column.
Bram Moolenaare24b4ab2022-09-16 20:51:14 +01005891 update_curswant_force();
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005892 }
Christian Brabandtb5f6fe92023-08-19 15:53:16 +02005893 if (flag)
5894 {
5895 do
5896 i = gchar_cursor();
5897 while (VIM_ISWHITE(i) && oneleft() == OK);
5898 curwin->w_valid &= ~VALID_WCOL;
5899 }
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005900}
5901
5902/*
5903 * "gi": start Insert at the last position.
5904 */
5905 static void
5906nv_gi_cmd(cmdarg_T *cap)
5907{
5908 int i;
5909
5910 if (curbuf->b_last_insert.lnum != 0)
5911 {
5912 curwin->w_cursor = curbuf->b_last_insert;
5913 check_cursor_lnum();
5914 i = (int)STRLEN(ml_get_curline());
5915 if (curwin->w_cursor.col > (colnr_T)i)
5916 {
5917 if (virtual_active())
5918 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
5919 curwin->w_cursor.col = i;
5920 }
5921 }
5922 cap->cmdchar = 'i';
5923 nv_edit(cap);
5924}
5925
5926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 * Commands starting with "g".
5928 */
5929 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005930nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931{
5932 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934
5935 switch (cap->nchar)
5936 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005937 case Ctrl_A:
5938 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005940 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005941 if (!VIsual_active && cap->nchar == Ctrl_A)
5942 vim_mem_profile_dump();
5943 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005945 // "g^A/g^X": sequentially increment visually selected region
zeertzjqf86dea82023-03-05 21:15:06 +00005946 if (VIsual_active)
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005947 {
5948 cap->arg = TRUE;
5949 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005950 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005951 nv_addsub(cap);
5952 }
5953 else
5954 clearopbeep(oap);
5955 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005957 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 case 'R':
5959 cap->arg = TRUE;
5960 nv_Replace(cap);
5961 break;
5962
5963 case 'r':
5964 nv_vreplace(cap);
5965 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966
5967 case '&':
5968 do_cmdline_cmd((char_u *)"%s//~/&");
5969 break;
5970
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005971 // "gv": Reselect the previous Visual area. If Visual already active,
5972 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005974 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005976
5977 // "gV": Don't reselect the previous Visual area after a Select mode
5978 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 case 'V':
5980 VIsual_reselect = FALSE;
5981 break;
5982
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005983 // "gh": start Select mode.
5984 // "gH": start Select line mode.
5985 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 case K_BS:
5987 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005988 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 case 'h':
5990 case 'H':
5991 case Ctrl_H:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 cap->cmdchar = cap->nchar + ('v' - 'h');
5993 cap->arg = TRUE;
5994 nv_visual(cap);
5995 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005996
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005997 // "gn", "gN" visually select next/previous search match
5998 // "gn" selects next match
5999 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02006000 case 'N':
6001 case 'n':
6002 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02006003 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02006004 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006006 // "gj" and "gk" two new funny movement keys -- up and down
6007 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 case 'j':
6009 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006010 // with 'nowrap' it works just like the normal "j" command.
6011 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 {
6013 oap->motion_type = MLINE;
6014 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
6015 }
6016 else
6017 i = nv_screengo(oap, FORWARD, cap->count1);
6018 if (i == FAIL)
6019 clearopbeep(oap);
6020 break;
6021
6022 case 'k':
6023 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006024 // with 'nowrap' it works just like the normal "k" command.
6025 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 {
6027 oap->motion_type = MLINE;
6028 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
6029 }
6030 else
6031 i = nv_screengo(oap, BACKWARD, cap->count1);
6032 if (i == FAIL)
6033 clearopbeep(oap);
6034 break;
6035
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006036 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 case 'J':
6038 nv_join(cap);
6039 break;
6040
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006041 // "g0", "g^" : Like "0" and "^" but for screen lines.
6042 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 case '0':
6045 case 'm':
6046 case K_HOME:
6047 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006048 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 break;
6050
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006051 case 'M':
6052 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006053 oap->motion_type = MCHAR;
6054 oap->inclusive = FALSE;
Bram Moolenaarc9121f72022-10-14 20:09:04 +01006055 i = linetabsize_str(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006056 if (cap->count0 > 0 && cap->count0 <= 100)
6057 coladvance((colnr_T)(i * cap->count0 / 100));
6058 else
6059 coladvance((colnr_T)(i / 2));
6060 curwin->w_set_curswant = TRUE;
6061 }
6062 break;
6063
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006064 // "g_": to the last non-blank character in the line or <count> lines
6065 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006067 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 break;
6069
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006070 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 case '$':
6072 case K_END:
6073 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006074 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 break;
6076
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006077 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 case '*':
6079 case '#':
6080#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006081 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006083 case Ctrl_RSB: // :tag or :tselect for current identifier
6084 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 nv_ident(cap);
6086 break;
6087
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006088 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 case 'e':
6090 case 'E':
6091 oap->motion_type = MCHAR;
6092 curwin->w_set_curswant = TRUE;
6093 oap->inclusive = TRUE;
6094 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6095 clearopbeep(oap);
6096 break;
6097
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006098 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006100 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 break;
6102
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006103 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006105 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 break;
6107
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006108 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 case 'I':
6110 beginline(0);
6111 if (!checkclearopq(oap))
6112 invoke_edit(cap, FALSE, 'g', FALSE);
6113 break;
6114
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006115 // "gf": goto file, edit file under cursor
6116 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006118 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 nv_gotofile(cap);
6120 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006122 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 case '\'':
6124 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006125 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 case '`':
6127 nv_gomark(cap);
6128 break;
6129
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006130 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006132 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 break;
6134
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006135 // "ga": Display the ascii value of the character under the
6136 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 case 'a':
6138 do_ascii(NULL);
6139 break;
6140
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006141 // "g8": Display the bytes used for the UTF-8 character under the
6142 // cursor. It is displayed in hex.
6143 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006145 if (cap->count0 == 8)
6146 utf_find_illegal();
6147 else
6148 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006151 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006152 case '<':
6153 show_sb_text();
6154 break;
6155
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006156 // "gg": Goto the first line in file. With a count it goes to
6157 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 case 'g':
6159 cap->arg = FALSE;
6160 nv_goto(cap);
6161 break;
6162
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006163 // Two-character operators:
6164 // "gq" Format text
6165 // "gw" Format text and keep cursor position
6166 // "g~" Toggle the case of the text.
6167 // "gu" Change text to lower case.
6168 // "gU" Change text to upper case.
6169 // "g?" rot13 encoding
6170 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 case 'q':
6172 case 'w':
6173 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006174 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 case '~':
6176 case 'u':
6177 case 'U':
6178 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006179 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 nv_operator(cap);
6181 break;
6182
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006183 // "gd": Find first occurrence of pattern under the cursor in the
6184 // current function
6185 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 case 'd':
6187 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006188 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 break;
6190
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006191 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 case K_MIDDLEMOUSE:
6193 case K_MIDDLEDRAG:
6194 case K_MIDDLERELEASE:
6195 case K_LEFTMOUSE:
6196 case K_LEFTDRAG:
6197 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006198 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 case K_RIGHTMOUSE:
6200 case K_RIGHTDRAG:
6201 case K_RIGHTRELEASE:
6202 case K_X1MOUSE:
6203 case K_X1DRAG:
6204 case K_X1RELEASE:
6205 case K_X2MOUSE:
6206 case K_X2DRAG:
6207 case K_X2RELEASE:
6208 mod_mask = MOD_MASK_CTRL;
6209 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6210 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211
6212 case K_IGNORE:
6213 break;
6214
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006215 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 case 'p':
6217 case 'P':
6218 nv_put(cap);
6219 break;
6220
6221#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006222 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 case 'o':
6224 goto_byte(cap->count0);
6225 break;
6226#endif
6227
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006228 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 case 'Q':
Bram Moolenaar338f1fc2022-05-26 15:56:23 +01006230 if (!check_text_locked(cap->oap) && !checkclearopq(oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 do_exmode(TRUE);
6232 break;
6233
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 case ',':
6235 nv_pcmark(cap);
6236 break;
6237
6238 case ';':
6239 cap->count1 = -cap->count1;
6240 nv_pcmark(cap);
6241 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006243 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006244 if (!checkclearop(oap))
6245 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006246 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006247 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006248 if (!checkclearop(oap))
6249 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006250 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006251
Bram Moolenaar62a23252020-08-09 14:04:42 +02006252 case TAB:
6253 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6254 clearopbeep(oap);
6255 break;
6256
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006257 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006258 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006259 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006260 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006261 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006262 break;
6263
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 default:
6265 clearopbeep(oap);
6266 break;
6267 }
6268}
6269
6270/*
6271 * Handle "o" and "O" commands.
6272 */
6273 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006274n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006276#ifdef FEAT_CONCEAL
6277 linenr_T oldline = curwin->w_cursor.lnum;
6278#endif
6279
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006280 if (checkclearopq(cap->oap))
6281 return;
6282
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283#ifdef FEAT_FOLDING
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006284 if (cap->cmdchar == 'O')
6285 // Open above the first line of a folded sequence of lines
6286 (void)hasFolding(curwin->w_cursor.lnum,
6287 &curwin->w_cursor.lnum, NULL);
6288 else
6289 // Open below the last line of a folded sequence of lines
6290 (void)hasFolding(curwin->w_cursor.lnum,
6291 NULL, &curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006293 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6294 (cap->cmdchar == 'O' ? 1 : 0)),
6295 (linenr_T)(curwin->w_cursor.lnum +
6296 (cap->cmdchar == 'o' ? 1 : 0))
6297 ) == OK
6298 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
6299 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6300 0, NULL) == OK)
6301 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006302#ifdef FEAT_CONCEAL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006303 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
6304 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006305#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006306#ifdef FEAT_SYN_HL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006307 if (curwin->w_p_cul)
6308 // force redraw of cursorline
6309 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006310#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006311 // When '#' is in 'cpoptions' ignore the count.
6312 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6313 cap->count1 = 1;
6314 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 }
6316}
6317
6318/*
6319 * "." command: redo last change.
6320 */
6321 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006322nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006324 if (checkclearopq(cap->oap))
6325 return;
6326
6327 // If "restart_edit" is TRUE, the last but one command is repeated
6328 // instead of the last command (inserting text). This is used for
6329 // CTRL-O <.> in insert mode.
6330 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6331 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332}
6333
6334/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006335 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 */
6337 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006338nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006340 if (VIsual_select && VIsual_active)
6341 {
6342 int reg;
6343 // Get register name
6344 ++no_mapping;
6345 ++allow_keys;
6346 reg = plain_vgetc();
6347 LANGMAP_ADJUST(reg, TRUE);
6348 --no_mapping;
6349 --allow_keys;
6350
6351 if (reg == '"')
6352 // the unnamed register is 0
6353 reg = 0;
6354
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006355 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006356 return;
6357 }
6358
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006359 if (checkclearopq(cap->oap))
6360 return;
6361
6362 u_redo((int)cap->count1);
6363 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364}
6365
6366/*
6367 * Handle "U" command.
6368 */
6369 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006370nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006372 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006373 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006375 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 cap->cmdchar = 'g';
6377 cap->nchar = 'U';
6378 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006379 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00006381
6382 if (checkclearopq(cap->oap))
6383 return;
6384
6385 u_undoline();
6386 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387}
6388
6389/*
6390 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6391 * single character.
6392 */
6393 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006394nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006396 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006397 {
6398#ifdef FEAT_JOB_CHANNEL
6399 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6400 {
6401 clearopbeep(cap->oap);
6402 return;
6403 }
6404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 else
6408 nv_operator(cap);
6409}
6410
6411/*
6412 * Handle an operator command.
6413 * The actual work is done by do_pending_operator().
6414 */
6415 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006416nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417{
6418 int op_type;
6419
6420 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006421#ifdef FEAT_JOB_CHANNEL
6422 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6423 {
6424 clearopbeep(cap->oap);
6425 return;
6426 }
6427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006429 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 nv_lineop(cap);
6431 else if (!checkclearop(cap->oap))
6432 {
6433 cap->oap->start = curwin->w_cursor;
6434 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006435#ifdef FEAT_EVAL
6436 set_op_var(op_type);
6437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 }
6439}
6440
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006441#ifdef FEAT_EVAL
6442/*
6443 * Set v:operator to the characters for "optype".
6444 */
6445 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006446set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006447{
6448 char_u opchars[3];
6449
6450 if (optype == OP_NOP)
6451 set_vim_var_string(VV_OP, NULL, 0);
6452 else
6453 {
6454 opchars[0] = get_op_char(optype);
6455 opchars[1] = get_extra_op_char(optype);
6456 opchars[2] = NUL;
6457 set_vim_var_string(VV_OP, opchars, -1);
6458 }
6459}
6460#endif
6461
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462/*
6463 * Handle linewise operator "dd", "yy", etc.
6464 *
6465 * "_" is is a strange motion command that helps make operators more logical.
6466 * It is actually implemented, but not documented in the real Vi. This motion
6467 * command actually refers to "the current line". Commands like "dd" and "yy"
6468 * are really an alternate form of "d_" and "y_". It does accept a count, so
6469 * "d3_" works to delete 3 lines.
6470 */
6471 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006472nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473{
6474 cap->oap->motion_type = MLINE;
6475 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6476 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006477 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006478 && cap->oap->motion_force != 'v'
6479 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 || cap->oap->op_type == OP_LSHIFT
6481 || cap->oap->op_type == OP_RSHIFT)
6482 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006483 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 beginline(BL_WHITE | BL_FIX);
6485}
6486
6487/*
6488 * <Home> command.
6489 */
6490 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006491nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006493 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006494 if (mod_mask & MOD_MASK_CTRL)
6495 nv_goto(cap);
6496 else
6497 {
6498 cap->count0 = 1;
6499 nv_pipe(cap);
6500 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006501 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6502 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503}
6504
6505/*
6506 * "|" command.
6507 */
6508 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006509nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510{
6511 cap->oap->motion_type = MCHAR;
6512 cap->oap->inclusive = FALSE;
6513 beginline(0);
6514 if (cap->count0 > 0)
6515 {
6516 coladvance((colnr_T)(cap->count0 - 1));
6517 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6518 }
6519 else
6520 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006521 // keep curswant at the column where we wanted to go, not where
6522 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 curwin->w_set_curswant = FALSE;
6524}
6525
6526/*
6527 * Handle back-word command "b" and "B".
6528 * cap->arg is 1 for "B"
6529 */
6530 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006531nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532{
6533 cap->oap->motion_type = MCHAR;
6534 cap->oap->inclusive = FALSE;
6535 curwin->w_set_curswant = TRUE;
6536 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6537 clearopbeep(cap->oap);
6538#ifdef FEAT_FOLDING
6539 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6540 foldOpenCursor();
6541#endif
6542}
6543
6544/*
6545 * Handle word motion commands "e", "E", "w" and "W".
6546 * cap->arg is TRUE for "E" and "W".
6547 */
6548 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006549nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550{
6551 int n;
6552 int word_end;
6553 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006554 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006556 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6558 word_end = TRUE;
6559 else
6560 word_end = FALSE;
6561 cap->oap->inclusive = word_end;
6562
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006563 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 if (!word_end && cap->oap->op_type == OP_CHANGE)
6565 {
6566 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006567 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006569 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006571 // Reproduce a funny Vi behaviour: "cw" on a blank only
6572 // changes one character, not all blanks until the start of
6573 // the next word. Only do this when the 'w' flag is included
6574 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6576 {
6577 cap->oap->inclusive = TRUE;
6578 cap->oap->motion_type = MCHAR;
6579 return;
6580 }
6581 }
6582 else
6583 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006584 // This is a little strange. To match what the real Vi does,
6585 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6586 // that we are not on a space or a TAB. This seems impolite
6587 // at first, but it's really more what we mean when we say
6588 // 'cw'.
6589 // Another strangeness: When standing on the end of a word
6590 // "ce" will change until the end of the next word, but "cw"
6591 // will change only one character! This is done by setting
6592 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 cap->oap->inclusive = TRUE;
6594 word_end = TRUE;
6595 flag = TRUE;
6596 }
6597 }
6598 }
6599
6600 cap->oap->motion_type = MCHAR;
6601 curwin->w_set_curswant = TRUE;
6602 if (word_end)
6603 n = end_word(cap->count1, cap->arg, flag, FALSE);
6604 else
6605 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6606
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006607 // Don't leave the cursor on the NUL past the end of line. Unless we
6608 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006609 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006610 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611
6612 if (n == FAIL && cap->oap->op_type == OP_NOP)
6613 clearopbeep(cap->oap);
6614 else
6615 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617#ifdef FEAT_FOLDING
6618 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6619 foldOpenCursor();
6620#endif
6621 }
6622}
6623
6624/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006625 * Used after a movement command: If the cursor ends up on the NUL after the
6626 * end of the line, may move it back to the last character and make the motion
6627 * inclusive.
6628 */
6629 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006630adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006631{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006632 // The cursor cannot remain on the NUL when:
6633 // - the column is > 0
6634 // - not in Visual mode or 'selection' is "o"
6635 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006636 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006637 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006638 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006639 {
6640 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006641 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006642 if (has_mbyte)
6643 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006644 oap->inclusive = TRUE;
6645 }
6646}
6647
6648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 * "0" and "^" commands.
6650 * cap->arg is the argument for beginline().
6651 */
6652 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006653nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654{
6655 cap->oap->motion_type = MCHAR;
6656 cap->oap->inclusive = FALSE;
6657 beginline(cap->arg);
6658#ifdef FEAT_FOLDING
6659 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6660 foldOpenCursor();
6661#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006662 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6663 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664}
6665
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666/*
6667 * In exclusive Visual mode, may include the last character.
6668 */
6669 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006670adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671{
6672 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006673 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 if (has_mbyte)
6676 inc_cursor();
6677 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 ++curwin->w_cursor.col;
6679 cap->oap->inclusive = FALSE;
6680 }
6681}
6682
6683/*
6684 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6685 * Should check VIsual_mode before calling this.
6686 * Returns TRUE when backed up to the previous line.
6687 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006688 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006689unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690{
6691 pos_T *pp;
6692
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006693 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006695 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 pp = &curwin->w_cursor;
6697 else
6698 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 if (pp->coladd > 0)
6700 --pp->coladd;
6701 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 if (pp->col > 0)
6703 {
6704 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006705 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 }
6707 else if (pp->lnum > 1)
6708 {
6709 --pp->lnum;
6710 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6711 return TRUE;
6712 }
6713 }
6714 return FALSE;
6715}
6716
6717/*
6718 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6719 */
6720 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006721nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722{
6723 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006724 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006726 VIsual_select_reg = 0;
6727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 else if (VIsual_reselect)
6729 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006730 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 cap->arg = TRUE;
6732 nv_g_cmd(cap);
6733 }
6734}
6735
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736
6737/*
6738 * "G", "gg", CTRL-END, CTRL-HOME.
6739 * cap->arg is TRUE for "G".
6740 */
6741 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006742nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743{
6744 linenr_T lnum;
6745
6746 if (cap->arg)
6747 lnum = curbuf->b_ml.ml_line_count;
6748 else
6749 lnum = 1L;
6750 cap->oap->motion_type = MLINE;
6751 setpcmark();
6752
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006753 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 if (cap->count0 != 0)
6755 lnum = cap->count0;
6756 if (lnum < 1L)
6757 lnum = 1L;
6758 else if (lnum > curbuf->b_ml.ml_line_count)
6759 lnum = curbuf->b_ml.ml_line_count;
6760 curwin->w_cursor.lnum = lnum;
6761 beginline(BL_SOL | BL_FIX);
6762#ifdef FEAT_FOLDING
6763 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6764 foldOpenCursor();
6765#endif
6766}
6767
6768/*
6769 * CTRL-\ in Normal mode.
6770 */
6771 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006772nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773{
6774 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6775 {
6776 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006777 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006778 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 if (cmdwin_type != 0)
6781 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 if (VIsual_active)
6783 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006784 end_visual_mode(); // stop Visual
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006785 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006787 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 if (cap->nchar == Ctrl_G && p_im)
6789 restart_edit = 'a';
6790 }
6791 else
6792 clearopbeep(cap->oap);
6793}
6794
6795/*
6796 * ESC in Normal mode: beep, but don't flush buffers.
6797 * Don't even beep if we are canceling a command.
6798 */
6799 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006800nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801{
6802 int no_reason;
6803
6804 no_reason = (cap->oap->op_type == OP_NOP
6805 && cap->opcount == 0
6806 && cap->count0 == 0
6807 && cap->oap->regname == 0
6808 && !p_im);
6809
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006810 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 {
Martin Tournoij7904fa42022-10-04 16:28:45 +01006812 if (restart_edit == 0 && cmdwin_type == 0
6813 && !VIsual_active && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006814 {
Bram Moolenaarf7e73022022-09-24 13:10:04 +01006815 int out_redir = !stdout_isatty && !is_not_a_term_or_gui();
6816
Bram Moolenaar5939c352022-09-24 12:50:45 +01006817 // The user may accidentally do "vim file | grep word" and then
6818 // CTRL-C doesn't show anything. With a changed buffer give the
6819 // message on stderr. Without any changes might as well exit.
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006820 if (anyBufIsChanged())
Bram Moolenaar5939c352022-09-24 12:50:45 +01006821 {
6822 char *ms = _("Type :qa! and press <Enter> to abandon all changes and exit Vim");
6823
6824 if (out_redir)
6825 mch_errmsg(ms);
6826 else
6827 msg(ms);
6828 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006829 else
Bram Moolenaar5939c352022-09-24 12:50:45 +01006830 {
6831 if (out_redir)
6832 {
6833 got_int = FALSE;
6834 do_cmdline_cmd((char_u *)"qa");
6835 }
6836 else
6837 msg(_("Type :qa and press <Enter> to exit Vim"));
6838 }
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01006841 if (restart_edit != 0)
6842 redraw_mode = TRUE; // remove "-- (insert) --"
6843
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006844 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6845 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 if (!p_im)
6847 restart_edit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 if (cmdwin_type != 0)
6849 {
6850 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006851 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 return;
6853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 }
Bram Moolenaar8d696372022-08-21 10:40:07 +01006855 else if (cmdwin_type != 0 && ex_normal_busy && typebuf_was_empty)
Bram Moolenaar7d414102021-02-23 19:39:20 +01006856 {
6857 // When :normal runs out of characters while in the command line window
Bram Moolenaar8d696372022-08-21 10:40:07 +01006858 // vgetorpeek() will repeatedly return ESC. Exit the cmdline window to
6859 // break the loop.
Bram Moolenaar7d414102021-02-23 19:39:20 +01006860 cmdwin_result = K_IGNORE;
6861 return;
6862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 if (VIsual_active)
6865 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006866 end_visual_mode(); // stop Visual
6867 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 curwin->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006869 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006871 else if (no_reason)
Bram Moolenaarb849c822022-08-28 22:46:21 +01006872 {
6873#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar98d10692022-08-29 00:08:39 +01006874 if (!cap->arg && popup_message_win_visible())
Bram Moolenaarb849c822022-08-28 22:46:21 +01006875 popup_hide_message_win();
6876 else
6877#endif
6878 vim_beep(BO_ESC);
6879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 clearop(cap->oap);
6881
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006882 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6883 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006884 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 restart_edit = 'a';
6886}
6887
6888/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006889 * Move the cursor for the "A" command.
6890 */
6891 void
6892set_cursor_for_append_to_line(void)
6893{
6894 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02006895 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006896 {
6897 int save_State = State;
6898
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006899 // Pretend Insert mode here to allow the cursor on the
6900 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01006901 State = MODE_INSERT;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006902 coladvance((colnr_T)MAXCOL);
6903 State = save_State;
6904 }
6905 else
6906 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6907}
6908
6909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006911 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 */
6913 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006914nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006916 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6918 cap->cmdchar = 'i';
6919
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006920 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006922 {
6923#ifdef FEAT_TERMINAL
6924 if (term_in_normal_mode())
6925 {
6926 end_visual_mode();
6927 clearop(cap->oap);
6928 term_enter_job_mode();
6929 return;
6930 }
6931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006935 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006936 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6937 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 nv_object(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006941#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006942 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006943 {
6944 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006945 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006946 return;
6947 }
6948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 else if (!curbuf->b_p_ma && !p_im)
6950 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006951 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02006952 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006954 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006955 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006956 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006958 else if (cap->cmdchar == K_PS && VIsual_active)
6959 {
6960 pos_T old_pos = curwin->w_cursor;
6961 pos_T old_visual = VIsual;
zeertzjq7a732522022-03-14 20:46:41 +00006962 int old_visual_mode = VIsual_mode;
Bram Moolenaara1891842017-02-04 21:34:31 +01006963
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006964 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006965 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6966 {
6967 shift_delete_registers();
6968 cap->oap->regname = '1';
6969 }
6970 else
6971 cap->oap->regname = '-';
6972 cap->cmdchar = 'd';
6973 cap->nchar = NUL;
6974 nv_operator(cap);
6975 do_pending_operator(cap, 0, FALSE);
6976 cap->cmdchar = K_PS;
6977
zeertzjq7a732522022-03-14 20:46:41 +00006978 if (*ml_get_cursor() != NUL)
6979 {
6980 if (old_visual_mode == 'V')
6981 {
6982 // In linewise Visual mode insert before the beginning of the
6983 // next line.
6984 // When the last line in the buffer was deleted then create a
6985 // new line, otherwise there is not need to move cursor.
6986 // Detect this by checking if cursor moved above Visual area.
6987 if (curwin->w_cursor.lnum < old_pos.lnum
6988 && curwin->w_cursor.lnum < old_visual.lnum)
6989 {
6990 if (u_save_cursor() == OK)
6991 {
6992 ml_append(curwin->w_cursor.lnum, (char_u *)"", 0,
6993 FALSE);
6994 appended_lines(curwin->w_cursor.lnum++, 1L);
6995 }
6996 }
6997 }
6998 // When the last char in the line was deleted then append.
6999 // Detect this by checking if cursor moved before Visual area.
7000 else if (curwin->w_cursor.col < old_pos.col
7001 && curwin->w_cursor.col < old_visual.col)
7002 inc_cursor();
7003 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007004
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007005 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01007006 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 else if (!checkclearopq(cap->oap))
7009 {
7010 switch (cap->cmdchar)
7011 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007012 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007013 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 break;
7015
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007016 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007017 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7018 beginline(BL_WHITE);
7019 else
7020 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 break;
7022
Bram Moolenaara1891842017-02-04 21:34:31 +01007023 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007024 // Bracketed paste works like "a"ppend, unless the cursor is in
7025 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007026 if (curwin->w_cursor.col == 0)
7027 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007028 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007029
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007030 case 'a': // "a"ppend is like "i"nsert on the next character.
7031 // increment coladd when in virtual space, increment the
7032 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 if (virtual_active()
7034 && (curwin->w_cursor.coladd > 0
7035 || *ml_get_cursor() == NUL
7036 || *ml_get_cursor() == TAB))
7037 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007038 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 inc_cursor();
7040 break;
7041 }
7042
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7044 {
7045 int save_State = State;
7046
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007047 // Pretend Insert mode here to allow the cursor on the
7048 // character past the end of the line
Bram Moolenaar24959102022-05-07 20:01:16 +01007049 State = MODE_INSERT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 coladvance(getviscol());
7051 State = save_State;
7052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053
7054 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7055 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007056 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007057 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007058 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059}
7060
7061/*
7062 * Invoke edit() and take care of "restart_edit" and the return value.
7063 */
7064 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007065invoke_edit(
7066 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007067 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007068 int cmd,
7069 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070{
7071 int restart_edit_save = 0;
7072
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007073 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7074 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7075 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 if (repl || !stuff_empty())
7077 restart_edit_save = restart_edit;
7078 else
7079 restart_edit_save = 0;
7080
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007081 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 restart_edit = 0;
7083
7084 if (edit(cmd, startln, cap->count1))
7085 cap->retval |= CA_COMMAND_BUSY;
7086
7087 if (restart_edit == 0)
7088 restart_edit = restart_edit_save;
7089}
7090
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091/*
7092 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7093 */
7094 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007095nv_object(
7096 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097{
7098 int flag;
7099 int include;
7100 char_u *mps_save;
7101
7102 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007103 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007105 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007107 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 mps_save = curbuf->b_p_mps;
7109 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7110
7111 switch (cap->nchar)
7112 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007113 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 flag = current_word(cap->oap, cap->count1, include, FALSE);
7115 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007116 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 flag = current_word(cap->oap, cap->count1, include, TRUE);
7118 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007119 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 case '(':
7121 case ')':
7122 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7123 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007124 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 case '{':
7126 case '}':
7127 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7128 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007129 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 case ']':
7131 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7132 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007133 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 case '>':
7135 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7136 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007137#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007138 case 't': // "at" = a tag block (xml and html)
7139 // Do not adjust oap->end in do_pending_operator()
7140 // otherwise there are different results for 'dit'
7141 // (note leading whitespace in last line):
7142 // 1) <b> 2) <b>
7143 // foobar foobar
7144 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007145 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007146 flag = current_tagblock(cap->oap, cap->count1, include);
7147 break;
Bram Moolenaar88774872022-08-16 20:24:29 +01007148#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007149 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 flag = current_par(cap->oap, cap->count1, include, 'p');
7151 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007152 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 flag = current_sent(cap->oap, cap->count1, include);
7154 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007155 case '"': // "a"" = a double quoted string
7156 case '\'': // "a'" = a single quoted string
7157 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007158 flag = current_quote(cap->oap, cap->count1, include,
7159 cap->nchar);
7160 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007161#if 0 // TODO
7162 case 'S': // "aS" = a section
7163 case 'f': // "af" = a filename
7164 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165#endif
7166 default:
7167 flag = FAIL;
7168 break;
7169 }
7170
7171 curbuf->b_p_mps = mps_save;
7172 if (flag == FAIL)
7173 clearopbeep(cap->oap);
7174 adjust_cursor_col();
7175 curwin->w_set_curswant = TRUE;
7176}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177
7178/*
7179 * "q" command: Start/stop recording.
7180 * "q:", "q/", "q?": edit command-line in command-line window.
7181 */
7182 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007183nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184{
7185 if (cap->oap->op_type == OP_FORMAT)
7186 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007187 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 cap->cmdchar = 'g';
7189 cap->nchar = 'q';
7190 nv_operator(cap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007191 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007193
7194 if (checkclearop(cap->oap))
7195 return;
7196
7197 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007199 if (cmdwin_type != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007201 emsg(_(e_cmdline_window_already_open));
7202 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007204 stuffcharReadbuff(cap->nchar);
7205 stuffcharReadbuff(K_CMDWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007207 else
7208 // (stop) recording into a named register, unless executing a
7209 // register
7210 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
7211 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212}
7213
7214/*
7215 * Handle the "@r" command.
7216 */
7217 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007218nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219{
7220 if (checkclearop(cap->oap))
7221 return;
7222#ifdef FEAT_EVAL
7223 if (cap->nchar == '=')
7224 {
7225 if (get_expr_register() == NUL)
7226 return;
7227 }
7228#endif
7229 while (cap->count1-- && !got_int)
7230 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007231 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 {
7233 clearopbeep(cap->oap);
7234 break;
7235 }
7236 line_breakcheck();
7237 }
7238}
7239
7240/*
7241 * Handle the CTRL-U and CTRL-D commands.
7242 */
7243 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007244nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245{
7246 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7247 || (cap->cmdchar == Ctrl_D
7248 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7249 clearopbeep(cap->oap);
7250 else if (!checkclearop(cap->oap))
7251 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7252}
7253
7254/*
7255 * Handle "J" or "gJ" command.
7256 */
7257 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007258nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007260 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007262 nv_operator(cap);
7263 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007265
7266 if (checkclearop(cap->oap))
7267 return;
7268
7269 if (cap->count0 <= 1)
7270 cap->count0 = 2; // default for join is two lines!
7271 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7272 curbuf->b_ml.ml_line_count)
7273 {
7274 // can't join when on the last line
7275 if (cap->count0 <= 2)
7276 {
7277 clearopbeep(cap->oap);
7278 return;
7279 }
7280 cap->count0 = curbuf->b_ml.ml_line_count
7281 - curwin->w_cursor.lnum + 1;
7282 }
7283
7284 prep_redo(cap->oap->regname, cap->count0,
7285 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7286 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287}
7288
7289/*
7290 * "P", "gP", "p" and "gp" commands.
7291 */
7292 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007293nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007295 nv_put_opt(cap, FALSE);
7296}
7297
7298/*
7299 * "P", "gP", "p" and "gp" commands.
7300 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7301 */
7302 static void
7303nv_put_opt(cmdarg_T *cap, int fix_indent)
7304{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 int regname = 0;
7306 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007307 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007308 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 int dir;
7310 int flags = 0;
Shougo Matsushita509142a2022-05-06 11:45:09 +01007311 int keep_registers = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312
7313 if (cap->oap->op_type != OP_NOP)
7314 {
7315#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007316 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7318 {
7319 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007320 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 }
7322 else
7323#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007324 clearopbeep(cap->oap);
7325 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007327
Bram Moolenaarf2732452018-06-03 14:47:35 +02007328#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007329 if (bt_prompt(curbuf) && !prompt_curpos_editable())
Bram Moolenaarf2732452018-06-03 14:47:35 +02007330 {
7331 clearopbeep(cap->oap);
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007332 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02007333 }
7334#endif
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007335
7336 if (fix_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007338 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7339 ? FORWARD : BACKWARD;
7340 flags |= PUT_FIXINDENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00007342 else
7343 dir = (cap->cmdchar == 'P'
7344 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7345 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
7346 prep_redo_cmd(cap);
7347 if (cap->cmdchar == 'g')
7348 flags |= PUT_CURSEND;
7349 else if (cap->cmdchar == 'z')
7350 flags |= PUT_BLOCK_INNER;
7351
7352 if (VIsual_active)
7353 {
7354 // Putting in Visual mode: The put text replaces the selected
7355 // text. First delete the selected text, then put the new text.
7356 // Need to save and restore the registers that the delete
7357 // overwrites if the old contents is being put.
7358 was_visual = TRUE;
7359 regname = cap->oap->regname;
7360 keep_registers = cap->cmdchar == 'P';
7361#ifdef FEAT_CLIPBOARD
7362 adjust_clip_reg(&regname);
7363#endif
7364 if (regname == 0 || regname == '"'
7365 || VIM_ISDIGIT(regname) || regname == '-'
7366#ifdef FEAT_CLIPBOARD
7367 || (clip_unnamed && (regname == '*' || regname == '+'))
7368#endif
7369
7370 )
7371 {
7372 // The delete is going to overwrite the register we want to
7373 // put, save it first.
7374 reg1 = get_register(regname, TRUE);
7375 }
7376
7377 // Now delete the selected text. Avoid messages here.
7378 cap->cmdchar = 'd';
7379 cap->nchar = NUL;
7380 cap->oap->regname = keep_registers ? '_' : NUL;
7381 ++msg_silent;
7382 nv_operator(cap);
7383 do_pending_operator(cap, 0, FALSE);
7384 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7385 --msg_silent;
7386
7387 // delete PUT_LINE_BACKWARD;
7388 cap->oap->regname = regname;
7389
7390 if (reg1 != NULL)
7391 {
7392 // Delete probably changed the register we want to put, save
7393 // it first. Then put back what was there before the delete.
7394 reg2 = get_register(regname, FALSE);
7395 put_register(regname, reg1);
7396 }
7397
7398 // When deleted a linewise Visual area, put the register as
7399 // lines to avoid it joined with the next line. When deletion was
7400 // characterwise, split a line when putting lines.
7401 if (VIsual_mode == 'V')
7402 flags |= PUT_LINE;
7403 else if (VIsual_mode == 'v')
7404 flags |= PUT_LINE_SPLIT;
7405 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7406 flags |= PUT_LINE_FORWARD;
7407 dir = BACKWARD;
7408 if ((VIsual_mode != 'V'
7409 && curwin->w_cursor.col < curbuf->b_op_start.col)
7410 || (VIsual_mode == 'V'
7411 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
7412 // cursor is at the end of the line or end of file, put
7413 // forward.
7414 dir = FORWARD;
7415 // May have been reset in do_put().
7416 VIsual_active = TRUE;
7417 }
7418 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
7419
7420 // If a register was saved, put it back now.
7421 if (reg2 != NULL)
7422 put_register(regname, reg2);
7423
7424 // What to reselect with "gv"? Selecting the just put text seems to
7425 // be the most useful, since the original text was removed.
7426 if (was_visual)
7427 {
7428 curbuf->b_visual.vi_start = curbuf->b_op_start;
7429 curbuf->b_visual.vi_end = curbuf->b_op_end;
7430 // need to adjust cursor position
7431 if (*p_sel == 'e')
7432 inc(&curbuf->b_visual.vi_end);
7433 }
7434
7435 // When all lines were selected and deleted do_put() leaves an empty
7436 // line that needs to be deleted now.
7437 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
7438 {
7439 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
7440 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
7441
7442 // If the cursor was in that line, move it to the end of the last
7443 // line.
7444 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7445 {
7446 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7447 coladvance((colnr_T)MAXCOL);
7448 }
7449 }
7450 auto_format(FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451}
7452
7453/*
7454 * "o" and "O" commands.
7455 */
7456 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007457nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458{
7459#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007460 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7462 {
7463 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007464 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 }
7466 else
7467#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007468 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007470#ifdef FEAT_JOB_CHANNEL
7471 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007472 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 n_opencmd(cap);
7476}
7477
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478#ifdef FEAT_NETBEANS_INTG
7479 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007480nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481{
7482 netbeans_keycommand(cap->nchar);
7483}
7484#endif
7485
7486#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007488nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007490 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491}
7492#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007493
Bram Moolenaar3918c952005-03-15 22:34:55 +00007494/*
7495 * Trigger CursorHold event.
7496 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7497 * input buffer. "did_cursorhold" is set to avoid retriggering.
7498 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007499 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007500nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007501{
7502 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7503 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007504 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007505}